// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008-2011 Gael Guennebaud // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include "sparse.h" template void sparse_vector(int rows, int cols) { double densityMat = (std::max)(8./(rows*cols), 0.01); double densityVec = (std::max)(8./float(rows), 0.1); typedef Matrix DenseMatrix; typedef Matrix DenseVector; typedef SparseVector SparseVectorType; typedef SparseMatrix SparseMatrixType; Scalar eps = 1e-6; SparseMatrixType m1(rows,rows); SparseVectorType v1(rows), v2(rows), v3(rows); DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); DenseVector refV1 = DenseVector::Random(rows), refV2 = DenseVector::Random(rows), refV3 = DenseVector::Random(rows); std::vector zerocoords, nonzerocoords; initSparse(densityVec, refV1, v1, &zerocoords, &nonzerocoords); initSparse(densityMat, refM1, m1); initSparse(densityVec, refV2, v2); initSparse(densityVec, refV3, v3); Scalar s1 = internal::random(); // test coeff and coeffRef for (unsigned int i=0; i(0,rows-1); VERIFY_IS_APPROX(v1.dot(m1.col(i)), refV1.dot(refM1.col(i))); VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm()); VERIFY_IS_APPROX(v1.blueNorm(), refV1.blueNorm()); // test aliasing VERIFY_IS_APPROX((v1 = -v1), (refV1 = -refV1)); VERIFY_IS_APPROX((v1 = v1.transpose()), (refV1 = refV1.transpose().eval())); VERIFY_IS_APPROX((v1 += -v1), (refV1 += -refV1)); // sparse matrix to sparse vector SparseMatrixType mv1; VERIFY_IS_APPROX((mv1=v1),v1); VERIFY_IS_APPROX(mv1,(v1=mv1)); VERIFY_IS_APPROX(mv1,(v1=mv1.transpose())); } void test_sparse_vector() { for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST_1( sparse_vector(8, 8) ); CALL_SUBTEST_2( sparse_vector >(16, 16) ); CALL_SUBTEST_1( sparse_vector(299, 535) ); } }