Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | 22c7609 | 2011-03-22 11:58:22 +0100 | [diff] [blame] | 4 | // Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr> |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 5 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 6 | // This Source Code Form is subject to the terms of the Mozilla |
| 7 | // Public License v. 2.0. If a copy of the MPL was not distributed |
| 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 9 | |
| 10 | #include "sparse.h" |
| 11 | |
| 12 | template<typename Scalar> void sparse_vector(int rows, int cols) |
| 13 | { |
Gael Guennebaud | 42e2578 | 2011-08-19 14:18:05 +0200 | [diff] [blame] | 14 | double densityMat = (std::max)(8./(rows*cols), 0.01); |
| 15 | double densityVec = (std::max)(8./float(rows), 0.1); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 16 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 17 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 18 | typedef SparseVector<Scalar> SparseVectorType; |
| 19 | typedef SparseMatrix<Scalar> SparseMatrixType; |
| 20 | Scalar eps = 1e-6; |
| 21 | |
Gael Guennebaud | 2c03e6f | 2011-12-22 14:01:06 +0100 | [diff] [blame] | 22 | SparseMatrixType m1(rows,rows); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 23 | SparseVectorType v1(rows), v2(rows), v3(rows); |
Gael Guennebaud | 2c03e6f | 2011-12-22 14:01:06 +0100 | [diff] [blame] | 24 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 25 | DenseVector refV1 = DenseVector::Random(rows), |
| 26 | refV2 = DenseVector::Random(rows), |
| 27 | refV3 = DenseVector::Random(rows); |
| 28 | |
| 29 | std::vector<int> zerocoords, nonzerocoords; |
| 30 | initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords); |
| 31 | initSparse<Scalar>(densityMat, refM1, m1); |
| 32 | |
| 33 | initSparse<Scalar>(densityVec, refV2, v2); |
| 34 | initSparse<Scalar>(densityVec, refV3, v3); |
| 35 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 36 | Scalar s1 = internal::random<Scalar>(); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 37 | |
| 38 | // test coeff and coeffRef |
| 39 | for (unsigned int i=0; i<zerocoords.size(); ++i) |
| 40 | { |
| 41 | VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps ); |
Gael Guennebaud | 36c478c | 2009-01-19 22:29:28 +0000 | [diff] [blame] | 42 | //VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 ); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 43 | } |
| 44 | { |
| 45 | VERIFY(int(nonzerocoords.size()) == v1.nonZeros()); |
| 46 | int j=0; |
| 47 | for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j) |
| 48 | { |
| 49 | VERIFY(nonzerocoords[j]==it.index()); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 50 | VERIFY(it.value()==v1.coeff(it.index())); |
| 51 | VERIFY(it.value()==refV1.coeff(it.index())); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | VERIFY_IS_APPROX(v1, refV1); |
| 55 | |
| 56 | v1.coeffRef(nonzerocoords[0]) = Scalar(5); |
| 57 | refV1.coeffRef(nonzerocoords[0]) = Scalar(5); |
| 58 | VERIFY_IS_APPROX(v1, refV1); |
| 59 | |
| 60 | VERIFY_IS_APPROX(v1+v2, refV1+refV2); |
| 61 | VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3); |
| 62 | |
| 63 | VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2); |
| 64 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 65 | VERIFY_IS_APPROX(v1*=s1, refV1*=s1); |
| 66 | VERIFY_IS_APPROX(v1/=s1, refV1/=s1); |
Gael Guennebaud | a2324d6 | 2010-04-13 10:40:55 +0200 | [diff] [blame] | 67 | |
Gael Guennebaud | e7c48fa | 2009-01-23 13:59:32 +0000 | [diff] [blame] | 68 | VERIFY_IS_APPROX(v1+=v2, refV1+=refV2); |
| 69 | VERIFY_IS_APPROX(v1-=v2, refV1-=refV2); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 70 | |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 71 | VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2)); |
Gael Guennebaud | a9688f0 | 2009-02-09 09:59:30 +0000 | [diff] [blame] | 72 | VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2)); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 73 | |
Gael Guennebaud | 2c03e6f | 2011-12-22 14:01:06 +0100 | [diff] [blame] | 74 | VERIFY_IS_APPROX(v1.dot(m1*v2), refV1.dot(refM1*refV2)); |
| 75 | int i = internal::random<int>(0,rows-1); |
| 76 | VERIFY_IS_APPROX(v1.dot(m1.col(i)), refV1.dot(refM1.col(i))); |
| 77 | |
| 78 | |
Gael Guennebaud | a2324d6 | 2010-04-13 10:40:55 +0200 | [diff] [blame] | 79 | VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm()); |
Desire NUENTSA | 5b9bb00 | 2013-01-21 15:37:06 +0100 | [diff] [blame] | 80 | |
| 81 | VERIFY_IS_APPROX(v1.blueNorm(), refV1.blueNorm()); |
Gael Guennebaud | a2324d6 | 2010-04-13 10:40:55 +0200 | [diff] [blame] | 82 | |
Gael Guennebaud | e75b1eb | 2012-07-25 09:33:50 +0200 | [diff] [blame] | 83 | // test aliasing |
| 84 | VERIFY_IS_APPROX((v1 = -v1), (refV1 = -refV1)); |
| 85 | VERIFY_IS_APPROX((v1 = v1.transpose()), (refV1 = refV1.transpose().eval())); |
| 86 | VERIFY_IS_APPROX((v1 += -v1), (refV1 += -refV1)); |
| 87 | |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void test_sparse_vector() |
| 91 | { |
| 92 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 93 | CALL_SUBTEST_1( sparse_vector<double>(8, 8) ); |
| 94 | CALL_SUBTEST_2( sparse_vector<std::complex<double> >(16, 16) ); |
| 95 | CALL_SUBTEST_1( sparse_vector<double>(299, 535) ); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |