17faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// This file is part of Eigen, a lightweight C++ template library
27faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// for linear algebra.
37faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez//
47faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// Copyright (C) 20013 Gael Guennebaud <gael.guennebaud@inria.fr>
57faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez//
67faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// This Source Code Form is subject to the terms of the Mozilla
77faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// Public License v. 2.0. If a copy of the MPL was not distributed
87faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
97faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
107faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// This unit test cannot be easily written to work with EIGEN_DEFAULT_TO_ROW_MAJOR
117faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#ifdef EIGEN_DEFAULT_TO_ROW_MAJOR
127faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#undef EIGEN_DEFAULT_TO_ROW_MAJOR
137faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#endif
147faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
152b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#define TEST_ENABLE_TEMPORARY_TRACKING
167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#include "main.h"
187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
192b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang// test Ref.h
207faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
212b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang// Deal with i387 extended precision
222b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#if EIGEN_ARCH_i386 && !(EIGEN_ARCH_x86_64)
237faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
242b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_AT_LEAST(4,4)
252b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#pragma GCC optimize ("-ffloat-store")
262b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#else
272b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#undef VERIFY_IS_EQUAL
282b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#define VERIFY_IS_EQUAL(X,Y) VERIFY_IS_APPROX(X,Y)
292b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#endif
302b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang
312b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang#endif
327faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename MatrixType> void ref_matrix(const MatrixType& m)
347faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez{
357faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename MatrixType::Index Index;
367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename MatrixType::Scalar Scalar;
377faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename MatrixType::RealScalar RealScalar;
387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Matrix<Scalar,Dynamic,Dynamic,MatrixType::Options> DynMatrixType;
397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Matrix<RealScalar,Dynamic,Dynamic,MatrixType::Options> RealDynMatrixType;
407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<MatrixType> RefMat;
427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<DynMatrixType> RefDynMat;
437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<const DynMatrixType> ConstRefDynMat;
447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<RealDynMatrixType , 0, Stride<Dynamic,Dynamic> > RefRealMatWithStride;
457faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
467faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Index rows = m.rows(), cols = m.cols();
477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
487faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  MatrixType  m1 = MatrixType::Random(rows, cols),
497faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez              m2 = m1;
507faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
517faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Index i = internal::random<Index>(0,rows-1);
527faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Index j = internal::random<Index>(0,cols-1);
537faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Index brows = internal::random<Index>(1,rows-i);
547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Index bcols = internal::random<Index>(1,cols-j);
557faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
567faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefMat rm0 = m1;
577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm0, m1);
587faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefDynMat rm1 = m1;
597faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm1, m1);
607faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefDynMat rm2 = m1.block(i,j,brows,bcols);
617faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm2, m1.block(i,j,brows,bcols));
627faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  rm2.setOnes();
637faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  m2.block(i,j,brows,bcols).setOnes();
647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(m1, m2);
657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  m2.block(i,j,brows,bcols).setRandom();
677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  rm2 = m2.block(i,j,brows,bcols);
687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(m1, m2);
697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  ConstRefDynMat rm3 = m1.block(i,j,brows,bcols);
717faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  m1.block(i,j,brows,bcols) *= 2;
727faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  m2.block(i,j,brows,bcols) *= 2;
737faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm3, m2.block(i,j,brows,bcols));
747faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefRealMatWithStride rm4 = m1.real();
757faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm4, m2.real());
767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  rm4.array() += 1;
777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  m2.real().array() += 1;
787faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(m1, m2);
797faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez}
807faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
817faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename VectorType> void ref_vector(const VectorType& m)
827faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez{
837faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename VectorType::Index Index;
847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename VectorType::Scalar Scalar;
857faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename VectorType::RealScalar RealScalar;
867faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Matrix<Scalar,Dynamic,1,VectorType::Options> DynMatrixType;
877faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Matrix<Scalar,Dynamic,Dynamic,ColMajor> MatrixType;
887faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Matrix<RealScalar,Dynamic,1,VectorType::Options> RealDynMatrixType;
897faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
907faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<VectorType> RefMat;
917faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<DynMatrixType> RefDynMat;
927faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<const DynMatrixType> ConstRefDynMat;
937faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<RealDynMatrixType , 0, InnerStride<> > RefRealMatWithStride;
947faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Ref<DynMatrixType , 0, InnerStride<> > RefMatWithStride;
957faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
967faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Index size = m.size();
977faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
987faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VectorType  v1 = VectorType::Random(size),
997faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez              v2 = v1;
1007faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  MatrixType mat1 = MatrixType::Random(size,size),
1017faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez             mat2 = mat1,
1027faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez             mat3 = MatrixType::Random(size,size);
1037faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1047faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Index i = internal::random<Index>(0,size-1);
1057faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Index bsize = internal::random<Index>(1,size-i);
1067faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1077faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefMat rm0 = v1;
1087faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm0, v1);
1097faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefDynMat rv1 = v1;
1107faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rv1, v1);
1117faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefDynMat rv2 = v1.segment(i,bsize);
1127faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rv2, v1.segment(i,bsize));
1137faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  rv2.setOnes();
1147faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  v2.segment(i,bsize).setOnes();
1157faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(v1, v2);
1167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  v2.segment(i,bsize).setRandom();
1187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  rv2 = v2.segment(i,bsize);
1197faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(v1, v2);
1207faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1217faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  ConstRefDynMat rm3 = v1.segment(i,bsize);
1227faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  v1.segment(i,bsize) *= 2;
1237faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  v2.segment(i,bsize) *= 2;
1247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm3, v2.segment(i,bsize));
1257faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1267faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefRealMatWithStride rm4 = v1.real();
1277faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm4, v2.real());
1287faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  rm4.array() += 1;
1297faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  v2.real().array() += 1;
1307faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(v1, v2);
1317faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1327faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RefMatWithStride rm5 = mat1.row(i).transpose();
1337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(rm5, mat1.row(i).transpose());
1347faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  rm5.array() += 1;
1357faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  mat2.row(i).array() += 1;
1367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_EQUAL(mat1, mat2);
1377faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  rm5.noalias() = rm4.transpose() * mat3;
1387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  mat2.row(i) = v2.real().transpose() * mat3;
1397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_APPROX(mat1, mat2);
1407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez}
1417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
1437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez{
1447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  // verify that ref-to-const don't have LvalueBit
1457faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
1467faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY( !(internal::traits<Ref<ConstPlainObjectType> >::Flags & LvalueBit) );
1477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY( !(internal::traits<Ref<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
1487faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY( !(Ref<ConstPlainObjectType>::Flags & LvalueBit) );
1497faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY( !(Ref<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
1507faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez}
1517faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1527faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename B>
1537faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezEIGEN_DONT_INLINE void call_ref_1(Ref<VectorXf> a, const B &b) { VERIFY_IS_EQUAL(a,b); }
1547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename B>
1557faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezEIGEN_DONT_INLINE void call_ref_2(const Ref<const VectorXf>& a, const B &b) { VERIFY_IS_EQUAL(a,b); }
1567faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename B>
1577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezEIGEN_DONT_INLINE void call_ref_3(Ref<VectorXf,0,InnerStride<> > a, const B &b) { VERIFY_IS_EQUAL(a,b); }
1587faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename B>
1597faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezEIGEN_DONT_INLINE void call_ref_4(const Ref<const VectorXf,0,InnerStride<> >& a, const B &b) { VERIFY_IS_EQUAL(a,b); }
1607faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename B>
1617faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezEIGEN_DONT_INLINE void call_ref_5(Ref<MatrixXf,0,OuterStride<> > a, const B &b) { VERIFY_IS_EQUAL(a,b); }
1627faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename B>
1637faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezEIGEN_DONT_INLINE void call_ref_6(const Ref<const MatrixXf,0,OuterStride<> >& a, const B &b) { VERIFY_IS_EQUAL(a,b); }
1647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename B>
1657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezEIGEN_DONT_INLINE void call_ref_7(Ref<Matrix<float,Dynamic,3> > a, const B &b) { VERIFY_IS_EQUAL(a,b); }
1667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezvoid call_ref()
1687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez{
1697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VectorXcf ca  = VectorXcf::Random(10);
1707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VectorXf a    = VectorXf::Random(10);
1717faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RowVectorXf b = RowVectorXf::Random(10);
1727faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  MatrixXf A    = MatrixXf::Random(10,10);
1737faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  RowVector3f c = RowVector3f::Random();
1747faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  const VectorXf& ac(a);
1757faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VectorBlock<VectorXf> ab(a,0,3);
1767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  const VectorBlock<VectorXf> abc(a,0,3);
1777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1787faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1797faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_1(a,a), 0);
1807faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_1(b,b.transpose()), 0);
181615d816d068b4d0f5e8df601930b5f160bf7eda1Tim Murray//   call_ref_1(ac,a<c);           // does not compile because ac is const
1827faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_1(ab,ab), 0);
1837faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_1(a.head(4),a.head(4)), 0);
1847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_1(abc,abc), 0);
1857faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_1(A.col(3),A.col(3)), 0);
186615d816d068b4d0f5e8df601930b5f160bf7eda1Tim Murray//   call_ref_1(A.row(3),A.row(3));    // does not compile because innerstride!=1
1877faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_3(A.row(3),A.row(3).transpose()), 0);
1887faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_4(A.row(3),A.row(3).transpose()), 0);
189615d816d068b4d0f5e8df601930b5f160bf7eda1Tim Murray//   call_ref_1(a+a, a+a);          // does not compile for obvious reason
1907faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1917faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  MatrixXf tmp = A*A.col(1);
1927faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_2(A*A.col(1), tmp), 1);     // evaluated into a temp
1937faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_2(ac.head(5),ac.head(5)), 0);
1947faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_2(ac,ac), 0);
1957faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_2(a,a), 0);
1967faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_2(ab,ab), 0);
1977faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_2(a.head(4),a.head(4)), 0);
1987faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  tmp = a+a;
1997faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_2(a+a,tmp), 1);            // evaluated into a temp
2007faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_2(ca.imag(),ca.imag()), 1);      // evaluated into a temp
2017faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2027faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_4(ac.head(5),ac.head(5)), 0);
2037faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  tmp = a+a;
2047faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_4(a+a,tmp), 1);           // evaluated into a temp
2057faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_4(ca.imag(),ca.imag()), 0);
2067faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2077faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_5(a,a), 0);
2087faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_5(a.head(3),a.head(3)), 0);
2097faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_5(A,A), 0);
210615d816d068b4d0f5e8df601930b5f160bf7eda1Tim Murray//   call_ref_5(A.transpose(),A.transpose());   // does not compile because storage order does not match
2117faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_5(A.block(1,1,2,2),A.block(1,1,2,2)), 0);
2127faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_5(b,b), 0);             // storage order do not match, but this is a degenerate case that should work
2137faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_5(a.row(3),a.row(3)), 0);
2147faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2157faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_6(a,a), 0);
2167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_6(a.head(3),a.head(3)), 0);
2177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_6(A.row(3),A.row(3)), 1);           // evaluated into a temp thouth it could be avoided by viewing it as a 1xn matrix
2187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  tmp = A+A;
2197faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_6(A+A,tmp), 1);                // evaluated into a temp
2207faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_6(A,A), 0);
2217faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_6(A.transpose(),A.transpose()), 1);      // evaluated into a temp because the storage orders do not match
2227faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_6(A.block(1,1,2,2),A.block(1,1,2,2)), 0);
2237faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_EVALUATION_COUNT( call_ref_7(c,c), 0);
2257faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez}
2267faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
227a829215e078ace896f52702caa0c27608f40e3b0Miao Wangtypedef Matrix<double,Dynamic,Dynamic,RowMajor> RowMatrixXd;
228a829215e078ace896f52702caa0c27608f40e3b0Miao Wangint test_ref_overload_fun1(Ref<MatrixXd> )       { return 1; }
229a829215e078ace896f52702caa0c27608f40e3b0Miao Wangint test_ref_overload_fun1(Ref<RowMatrixXd> )    { return 2; }
230a829215e078ace896f52702caa0c27608f40e3b0Miao Wangint test_ref_overload_fun1(Ref<MatrixXf> )       { return 3; }
231a829215e078ace896f52702caa0c27608f40e3b0Miao Wang
232a829215e078ace896f52702caa0c27608f40e3b0Miao Wangint test_ref_overload_fun2(Ref<const MatrixXd> ) { return 4; }
233a829215e078ace896f52702caa0c27608f40e3b0Miao Wangint test_ref_overload_fun2(Ref<const MatrixXf> ) { return 5; }
234a829215e078ace896f52702caa0c27608f40e3b0Miao Wang
2352b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wangvoid test_ref_ambiguous(const Ref<const ArrayXd> &A, Ref<ArrayXd> B)
2362b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang{
2372b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang  B = A;
2382b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang  B = A - A;
2392b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang}
2402b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang
241a829215e078ace896f52702caa0c27608f40e3b0Miao Wang// See also bug 969
242a829215e078ace896f52702caa0c27608f40e3b0Miao Wangvoid test_ref_overloads()
243a829215e078ace896f52702caa0c27608f40e3b0Miao Wang{
244a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  MatrixXd Ad, Bd;
245a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  RowMatrixXd rAd, rBd;
246a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  VERIFY( test_ref_overload_fun1(Ad)==1 );
247a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  VERIFY( test_ref_overload_fun1(rAd)==2 );
248a829215e078ace896f52702caa0c27608f40e3b0Miao Wang
249a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  MatrixXf Af, Bf;
250a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  VERIFY( test_ref_overload_fun2(Ad)==4 );
251a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  VERIFY( test_ref_overload_fun2(Ad+Bd)==4 );
252a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  VERIFY( test_ref_overload_fun2(Af+Bf)==5 );
2532b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang
2542b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang  ArrayXd A, B;
2552b8756b6f1de65d3f8bffab45be6c44ceb7411fcMiao Wang  test_ref_ambiguous(A, B);
256a829215e078ace896f52702caa0c27608f40e3b0Miao Wang}
257a829215e078ace896f52702caa0c27608f40e3b0Miao Wang
2587faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezvoid test_ref()
2597faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez{
2607faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  for(int i = 0; i < g_repeat; i++) {
2617faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_1( ref_vector(Matrix<float, 1, 1>()) );
2627faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_1( check_const_correctness(Matrix<float, 1, 1>()) );
2637faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_2( ref_vector(Vector4d()) );
2647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_2( check_const_correctness(Matrix4d()) );
2657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_3( ref_vector(Vector4cf()) );
2667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_4( ref_vector(VectorXcf(8)) );
2677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_5( ref_vector(VectorXi(12)) );
2687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_5( check_const_correctness(VectorXi(12)) );
2697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
2707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_1( ref_matrix(Matrix<float, 1, 1>()) );
2717faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_2( ref_matrix(Matrix4d()) );
2727faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_1( ref_matrix(Matrix<float,3,5>()) );
2737faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_4( ref_matrix(MatrixXcf(internal::random<int>(1,10),internal::random<int>(1,10))) );
2747faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_4( ref_matrix(Matrix<std::complex<double>,10,15>()) );
2757faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_5( ref_matrix(MatrixXi(internal::random<int>(1,10),internal::random<int>(1,10))) );
2767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    CALL_SUBTEST_6( call_ref() );
2777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  }
278a829215e078ace896f52702caa0c27608f40e3b0Miao Wang
279a829215e078ace896f52702caa0c27608f40e3b0Miao Wang  CALL_SUBTEST_7( test_ref_overloads() );
2807faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez}
281