diff options
author | Gael Guennebaud <g.gael@free.fr> | 2009-07-13 13:17:55 +0200 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2009-07-13 13:17:55 +0200 |
commit | a2cf7ba9552bbe6b9371e1a2678914bab185968a (patch) | |
tree | 069f785e47348452411420a51241c1646e3efcdf /test | |
parent | a2087cd7a3674c3d3ef74a474e417a3ea1f1e82b (diff) |
add triangular * vector product
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | test/product_triangular.cpp | 92 | ||||
-rw-r--r-- | test/triangular.cpp | 4 |
3 files changed, 96 insertions, 1 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 229c7123b..886731d45 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -110,6 +110,7 @@ ei_add_test(array) ei_add_test(array_replicate) ei_add_test(array_reverse) ei_add_test(triangular) +ei_add_test(product_triangular) ei_add_test(cholesky " " "${GSL_LIBRARIES}") ei_add_test(lu ${EI_OFLAG}) ei_add_test(determinant) diff --git a/test/product_triangular.cpp b/test/product_triangular.cpp new file mode 100644 index 000000000..876fb4388 --- /dev/null +++ b/test/product_triangular.cpp @@ -0,0 +1,92 @@ +// This file is triangularView of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@gmail.com> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#include "main.h" + +template<typename MatrixType> void product_triangular(const MatrixType& m) +{ + typedef typename MatrixType::Scalar Scalar; + typedef typename NumTraits<Scalar>::Real RealScalar; + typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; + + RealScalar largerEps = 10*test_precision<RealScalar>(); + + int rows = m.rows(); + int cols = m.cols(); + + MatrixType m1 = MatrixType::Random(rows, cols), + m3(rows, cols); + VectorType v1 = VectorType::Random(rows); + + Scalar s1 = ei_random<Scalar>(); + + m1 = MatrixType::Random(rows, cols); + + // check with a column-major matrix + m3 = m1.template triangularView<Eigen::LowerTriangular>(); + VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::LowerTriangular>() * v1, largerEps)); + m3 = m1.template triangularView<Eigen::UpperTriangular>(); + VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::UpperTriangular>() * v1, largerEps)); + m3 = m1.template triangularView<Eigen::UnitLowerTriangular>(); + VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::UnitLowerTriangular>() * v1, largerEps)); + m3 = m1.template triangularView<Eigen::UnitUpperTriangular>(); + VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::UnitUpperTriangular>() * v1, largerEps)); + + // check conjugated and scalar multiple expressions (col-major) + m3 = m1.template triangularView<Eigen::LowerTriangular>(); + VERIFY(((s1*m3).conjugate() * v1).isApprox((s1*m1).conjugate().template triangularView<Eigen::LowerTriangular>() * v1, largerEps)); + m3 = m1.template triangularView<Eigen::UpperTriangular>(); + VERIFY((m3.conjugate() * v1.conjugate()).isApprox(m1.conjugate().template triangularView<Eigen::UpperTriangular>() * v1.conjugate(), largerEps)); + + // check with a row-major matrix + m3 = m1.template triangularView<Eigen::UpperTriangular>(); + VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::LowerTriangular>() * v1, largerEps)); + m3 = m1.template triangularView<Eigen::LowerTriangular>(); + VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::UpperTriangular>() * v1, largerEps)); + m3 = m1.template triangularView<Eigen::UnitUpperTriangular>(); + VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::UnitLowerTriangular>() * v1, largerEps)); + m3 = m1.template triangularView<Eigen::UnitLowerTriangular>(); + VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::UnitUpperTriangular>() * v1, largerEps)); + + // check conjugated and scalar multiple expressions (row-major) + m3 = m1.template triangularView<Eigen::UpperTriangular>(); + VERIFY((m3.adjoint() * v1).isApprox(m1.adjoint().template triangularView<Eigen::LowerTriangular>() * v1, largerEps)); + m3 = m1.template triangularView<Eigen::LowerTriangular>(); + VERIFY((m3.adjoint() * (s1*v1.conjugate())).isApprox(m1.adjoint().template triangularView<Eigen::UpperTriangular>() * (s1*v1.conjugate()), largerEps)); + m3 = m1.template triangularView<Eigen::UnitUpperTriangular>(); + + // TODO check with sub-matrices +} + +void test_product_triangular() +{ + for(int i = 0; i < g_repeat ; i++) { + CALL_SUBTEST( product_triangular(Matrix<float, 1, 1>()) ); + CALL_SUBTEST( product_triangular(Matrix<float, 2, 2>()) ); + CALL_SUBTEST( product_triangular(Matrix3d()) ); + CALL_SUBTEST( product_triangular(Matrix<std::complex<float>,23, 23>()) ); + CALL_SUBTEST( product_triangular(MatrixXcd(17,17)) ); + CALL_SUBTEST( product_triangular(Matrix<float,Dynamic,Dynamic,RowMajor>(19, 19)) ); + } +} diff --git a/test/triangular.cpp b/test/triangular.cpp index 7c680a8ed..6385bffd1 100644 --- a/test/triangular.cpp +++ b/test/triangular.cpp @@ -51,6 +51,8 @@ template<typename MatrixType> void triangular(const MatrixType& m) v2 = VectorType::Random(rows), vzero = VectorType::Zero(rows); + Scalar s1 = ei_random<Scalar>(); + MatrixType m1up = m1.template triangularView<Eigen::UpperTriangular>(); MatrixType m2up = m2.template triangularView<Eigen::UpperTriangular>(); @@ -142,7 +144,7 @@ void test_triangular() CALL_SUBTEST( triangular(Matrix3d()) ); CALL_SUBTEST( triangular(MatrixXcf(4, 4)) ); CALL_SUBTEST( triangular(Matrix<std::complex<float>,8, 8>()) ); - CALL_SUBTEST( triangular(MatrixXd(17,17)) ); + CALL_SUBTEST( triangular(MatrixXcd(17,17)) ); CALL_SUBTEST( triangular(Matrix<float,Dynamic,Dynamic,RowMajor>(5, 5)) ); } } |