2009-01-08 01:01:57 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2009-01-08 01:01:57 +08:00
|
|
|
//
|
2011-03-22 18:58:22 +08:00
|
|
|
// Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-01-08 01:01:57 +08:00
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// 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/.
|
2009-01-08 01:01:57 +08:00
|
|
|
|
|
|
|
#include "sparse.h"
|
|
|
|
|
2014-02-15 16:35:23 +08:00
|
|
|
template<typename Scalar,typename Index> void sparse_vector(int rows, int cols)
|
2009-01-08 01:01:57 +08:00
|
|
|
{
|
2011-08-19 20:18:05 +08:00
|
|
|
double densityMat = (std::max)(8./(rows*cols), 0.01);
|
|
|
|
double densityVec = (std::max)(8./float(rows), 0.1);
|
2009-01-08 01:01:57 +08:00
|
|
|
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
|
|
|
|
typedef Matrix<Scalar,Dynamic,1> DenseVector;
|
2014-02-15 16:35:23 +08:00
|
|
|
typedef SparseVector<Scalar,0,Index> SparseVectorType;
|
|
|
|
typedef SparseMatrix<Scalar,0,Index> SparseMatrixType;
|
2009-01-08 01:01:57 +08:00
|
|
|
Scalar eps = 1e-6;
|
|
|
|
|
2011-12-22 21:01:06 +08:00
|
|
|
SparseMatrixType m1(rows,rows);
|
2009-01-08 01:01:57 +08:00
|
|
|
SparseVectorType v1(rows), v2(rows), v3(rows);
|
2011-12-22 21:01:06 +08:00
|
|
|
DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
|
2009-01-08 01:01:57 +08:00
|
|
|
DenseVector refV1 = DenseVector::Random(rows),
|
2014-10-10 05:35:49 +08:00
|
|
|
refV2 = DenseVector::Random(rows),
|
|
|
|
refV3 = DenseVector::Random(rows);
|
2009-01-08 01:01:57 +08:00
|
|
|
|
|
|
|
std::vector<int> zerocoords, nonzerocoords;
|
|
|
|
initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords);
|
|
|
|
initSparse<Scalar>(densityMat, refM1, m1);
|
|
|
|
|
|
|
|
initSparse<Scalar>(densityVec, refV2, v2);
|
|
|
|
initSparse<Scalar>(densityVec, refV3, v3);
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
Scalar s1 = internal::random<Scalar>();
|
2009-01-08 01:01:57 +08:00
|
|
|
|
|
|
|
// test coeff and coeffRef
|
|
|
|
for (unsigned int i=0; i<zerocoords.size(); ++i)
|
|
|
|
{
|
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps );
|
2009-01-20 06:29:28 +08:00
|
|
|
//VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 );
|
2009-01-08 01:01:57 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
VERIFY(int(nonzerocoords.size()) == v1.nonZeros());
|
|
|
|
int j=0;
|
|
|
|
for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j)
|
|
|
|
{
|
|
|
|
VERIFY(nonzerocoords[j]==it.index());
|
2009-01-14 22:24:10 +08:00
|
|
|
VERIFY(it.value()==v1.coeff(it.index()));
|
|
|
|
VERIFY(it.value()==refV1.coeff(it.index()));
|
2009-01-08 01:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
VERIFY_IS_APPROX(v1, refV1);
|
2014-10-10 05:35:49 +08:00
|
|
|
|
|
|
|
// test coeffRef with reallocation
|
|
|
|
{
|
|
|
|
SparseVectorType v1(rows);
|
|
|
|
DenseVector v2 = DenseVector::Zero(rows);
|
|
|
|
for(int k=0; k<rows; ++k)
|
|
|
|
{
|
|
|
|
int i = internal::random<int>(0,rows-1);
|
|
|
|
Scalar v = internal::random<Scalar>();
|
|
|
|
v1.coeffRef(i) += v;
|
|
|
|
v2.coeffRef(i) += v;
|
|
|
|
}
|
|
|
|
VERIFY_IS_APPROX(v1,v2);
|
|
|
|
}
|
2009-01-08 01:01:57 +08:00
|
|
|
|
|
|
|
v1.coeffRef(nonzerocoords[0]) = Scalar(5);
|
|
|
|
refV1.coeffRef(nonzerocoords[0]) = Scalar(5);
|
|
|
|
VERIFY_IS_APPROX(v1, refV1);
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(v1+v2, refV1+refV2);
|
|
|
|
VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3);
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2);
|
|
|
|
|
2009-01-15 05:27:54 +08:00
|
|
|
VERIFY_IS_APPROX(v1*=s1, refV1*=s1);
|
|
|
|
VERIFY_IS_APPROX(v1/=s1, refV1/=s1);
|
2010-04-13 16:40:55 +08:00
|
|
|
|
2009-01-23 21:59:32 +08:00
|
|
|
VERIFY_IS_APPROX(v1+=v2, refV1+=refV2);
|
|
|
|
VERIFY_IS_APPROX(v1-=v2, refV1-=refV2);
|
2009-01-15 05:27:54 +08:00
|
|
|
|
2009-01-08 01:01:57 +08:00
|
|
|
VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
|
2009-02-09 17:59:30 +08:00
|
|
|
VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2));
|
2009-01-08 01:01:57 +08:00
|
|
|
|
2014-09-01 22:53:52 +08:00
|
|
|
VERIFY_IS_APPROX(m1*v2, refM1*refV2);
|
2011-12-22 21:01:06 +08:00
|
|
|
VERIFY_IS_APPROX(v1.dot(m1*v2), refV1.dot(refM1*refV2));
|
|
|
|
int i = internal::random<int>(0,rows-1);
|
|
|
|
VERIFY_IS_APPROX(v1.dot(m1.col(i)), refV1.dot(refM1.col(i)));
|
|
|
|
|
|
|
|
|
2010-04-13 16:40:55 +08:00
|
|
|
VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm());
|
2013-01-21 22:37:06 +08:00
|
|
|
|
|
|
|
VERIFY_IS_APPROX(v1.blueNorm(), refV1.blueNorm());
|
2010-04-13 16:40:55 +08:00
|
|
|
|
2012-07-25 15:33:50 +08:00
|
|
|
// 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));
|
2013-03-06 18:58:22 +08:00
|
|
|
|
|
|
|
// 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()));
|
2013-06-10 06:06:40 +08:00
|
|
|
|
|
|
|
// check copy to dense vector with transpose
|
|
|
|
refV3.resize(0);
|
|
|
|
VERIFY_IS_APPROX(refV3 = v1.transpose(),v1.toDense());
|
|
|
|
VERIFY_IS_APPROX(DenseVector(v1),v1.toDense());
|
2012-07-25 15:33:50 +08:00
|
|
|
|
2009-01-08 01:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_sparse_vector()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
2014-02-15 16:35:23 +08:00
|
|
|
CALL_SUBTEST_1(( sparse_vector<double,int>(8, 8) ));
|
|
|
|
CALL_SUBTEST_2(( sparse_vector<std::complex<double>, int>(16, 16) ));
|
|
|
|
CALL_SUBTEST_1(( sparse_vector<double,long int>(299, 535) ));
|
|
|
|
CALL_SUBTEST_1(( sparse_vector<double,short>(299, 535) ));
|
2009-01-08 01:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|