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 | // |
| 4 | // Copyright (C) 2008 Daniel Gomez Ferro <dgomezferro@gmail.com> |
| 5 | // |
| 6 | // Eigen is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU Lesser General Public |
| 8 | // License as published by the Free Software Foundation; either |
| 9 | // version 3 of the License, or (at your option) any later version. |
| 10 | // |
| 11 | // Alternatively, you can redistribute it and/or |
| 12 | // modify it under the terms of the GNU General Public License as |
| 13 | // published by the Free Software Foundation; either version 2 of |
| 14 | // the License, or (at your option) any later version. |
| 15 | // |
| 16 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 17 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 19 | // GNU General Public License for more details. |
| 20 | // |
| 21 | // You should have received a copy of the GNU Lesser General Public |
| 22 | // License and a copy of the GNU General Public License along with |
| 23 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | #include "sparse.h" |
| 26 | |
| 27 | template<typename Scalar> void sparse_vector(int rows, int cols) |
| 28 | { |
| 29 | double densityMat = std::max(8./(rows*cols), 0.01); |
| 30 | double densityVec = std::max(8./float(rows), 0.1); |
| 31 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 32 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 33 | typedef SparseVector<Scalar> SparseVectorType; |
| 34 | typedef SparseMatrix<Scalar> SparseMatrixType; |
| 35 | Scalar eps = 1e-6; |
| 36 | |
| 37 | SparseMatrixType m1(rows,cols); |
| 38 | SparseVectorType v1(rows), v2(rows), v3(rows); |
| 39 | DenseMatrix refM1 = DenseMatrix::Zero(rows, cols); |
| 40 | DenseVector refV1 = DenseVector::Random(rows), |
| 41 | refV2 = DenseVector::Random(rows), |
| 42 | refV3 = DenseVector::Random(rows); |
| 43 | |
| 44 | std::vector<int> zerocoords, nonzerocoords; |
| 45 | initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords); |
| 46 | initSparse<Scalar>(densityMat, refM1, m1); |
| 47 | |
| 48 | initSparse<Scalar>(densityVec, refV2, v2); |
| 49 | initSparse<Scalar>(densityVec, refV3, v3); |
| 50 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 51 | Scalar s1 = internal::random<Scalar>(); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 52 | |
| 53 | // test coeff and coeffRef |
| 54 | for (unsigned int i=0; i<zerocoords.size(); ++i) |
| 55 | { |
| 56 | VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps ); |
Gael Guennebaud | 36c478c | 2009-01-19 22:29:28 +0000 | [diff] [blame] | 57 | //VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 ); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 58 | } |
| 59 | { |
| 60 | VERIFY(int(nonzerocoords.size()) == v1.nonZeros()); |
| 61 | int j=0; |
| 62 | for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j) |
| 63 | { |
| 64 | VERIFY(nonzerocoords[j]==it.index()); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 65 | VERIFY(it.value()==v1.coeff(it.index())); |
| 66 | VERIFY(it.value()==refV1.coeff(it.index())); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | VERIFY_IS_APPROX(v1, refV1); |
| 70 | |
| 71 | v1.coeffRef(nonzerocoords[0]) = Scalar(5); |
| 72 | refV1.coeffRef(nonzerocoords[0]) = Scalar(5); |
| 73 | VERIFY_IS_APPROX(v1, refV1); |
| 74 | |
| 75 | VERIFY_IS_APPROX(v1+v2, refV1+refV2); |
| 76 | VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3); |
| 77 | |
| 78 | VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2); |
| 79 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 80 | VERIFY_IS_APPROX(v1*=s1, refV1*=s1); |
| 81 | VERIFY_IS_APPROX(v1/=s1, refV1/=s1); |
Gael Guennebaud | a2324d6 | 2010-04-13 10:40:55 +0200 | [diff] [blame] | 82 | |
Gael Guennebaud | e7c48fa | 2009-01-23 13:59:32 +0000 | [diff] [blame] | 83 | VERIFY_IS_APPROX(v1+=v2, refV1+=refV2); |
| 84 | VERIFY_IS_APPROX(v1-=v2, refV1-=refV2); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 85 | |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 86 | VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2)); |
Gael Guennebaud | a9688f0 | 2009-02-09 09:59:30 +0000 | [diff] [blame] | 87 | VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2)); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 88 | |
Gael Guennebaud | a2324d6 | 2010-04-13 10:40:55 +0200 | [diff] [blame] | 89 | VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm()); |
| 90 | |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void test_sparse_vector() |
| 94 | { |
| 95 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 96 | CALL_SUBTEST_1( sparse_vector<double>(8, 8) ); |
| 97 | CALL_SUBTEST_2( sparse_vector<std::complex<double> >(16, 16) ); |
| 98 | CALL_SUBTEST_1( sparse_vector<double>(299, 535) ); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |