Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "sparse.h"
00026
00027 template<typename Scalar> void sparse_vector(int rows, int cols)
00028 {
00029 double densityMat = std::max(8./(rows*cols), 0.01);
00030 double densityVec = std::max(8./float(rows), 0.1);
00031 typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
00032 typedef Matrix<Scalar,Dynamic,1> DenseVector;
00033 typedef SparseVector<Scalar> SparseVectorType;
00034 typedef SparseMatrix<Scalar> SparseMatrixType;
00035 Scalar eps = 1e-6;
00036
00037 SparseMatrixType m1(rows,cols);
00038 SparseVectorType v1(rows), v2(rows), v3(rows);
00039 DenseMatrix refM1 = DenseMatrix::Zero(rows, cols);
00040 DenseVector refV1 = DenseVector::Random(rows),
00041 refV2 = DenseVector::Random(rows),
00042 refV3 = DenseVector::Random(rows);
00043
00044 std::vector<int> zerocoords, nonzerocoords;
00045 initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords);
00046 initSparse<Scalar>(densityMat, refM1, m1);
00047
00048 initSparse<Scalar>(densityVec, refV2, v2);
00049 initSparse<Scalar>(densityVec, refV3, v3);
00050
00051 Scalar s1 = internal::random<Scalar>();
00052
00053
00054 for (unsigned int i=0; i<zerocoords.size(); ++i)
00055 {
00056 VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps );
00057
00058 }
00059 {
00060 VERIFY(int(nonzerocoords.size()) == v1.nonZeros());
00061 int j=0;
00062 for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j)
00063 {
00064 VERIFY(nonzerocoords[j]==it.index());
00065 VERIFY(it.value()==v1.coeff(it.index()));
00066 VERIFY(it.value()==refV1.coeff(it.index()));
00067 }
00068 }
00069 VERIFY_IS_APPROX(v1, refV1);
00070
00071 v1.coeffRef(nonzerocoords[0]) = Scalar(5);
00072 refV1.coeffRef(nonzerocoords[0]) = Scalar(5);
00073 VERIFY_IS_APPROX(v1, refV1);
00074
00075 VERIFY_IS_APPROX(v1+v2, refV1+refV2);
00076 VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3);
00077
00078 VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2);
00079
00080 VERIFY_IS_APPROX(v1*=s1, refV1*=s1);
00081 VERIFY_IS_APPROX(v1/=s1, refV1/=s1);
00082
00083 VERIFY_IS_APPROX(v1+=v2, refV1+=refV2);
00084 VERIFY_IS_APPROX(v1-=v2, refV1-=refV2);
00085
00086 VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
00087 VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2));
00088
00089 VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm());
00090
00091 }
00092
00093 void test_sparse_vector()
00094 {
00095 for(int i = 0; i < g_repeat; i++) {
00096 CALL_SUBTEST_1( sparse_vector<double>(8, 8) );
00097 CALL_SUBTEST_2( sparse_vector<std::complex<double> >(16, 16) );
00098 CALL_SUBTEST_1( sparse_vector<double>(299, 535) );
00099 }
00100 }
00101