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 #ifndef EIGEN_CWISE_NULLARY_OP_H
00026 #define EIGEN_CWISE_NULLARY_OP_H
00027
00043 template<typename NullaryOp, typename MatrixType>
00044 struct ei_traits<CwiseNullaryOp<NullaryOp, MatrixType> > : ei_traits<MatrixType>
00045 {
00046 enum {
00047 Flags = (ei_traits<MatrixType>::Flags
00048 & ( HereditaryBits
00049 | (ei_functor_has_linear_access<NullaryOp>::ret ? LinearAccessBit : 0)
00050 | (ei_functor_traits<NullaryOp>::PacketAccess ? PacketAccessBit : 0)))
00051 | (ei_functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit),
00052 CoeffReadCost = ei_functor_traits<NullaryOp>::Cost
00053 };
00054 };
00055
00056 template<typename NullaryOp, typename MatrixType>
00057 class CwiseNullaryOp : ei_no_assignment_operator,
00058 public MatrixBase<CwiseNullaryOp<NullaryOp, MatrixType> >
00059 {
00060 public:
00061
00062 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseNullaryOp)
00063
00064 CwiseNullaryOp(int rows, int cols, const NullaryOp& func = NullaryOp())
00065 : m_rows(rows), m_cols(cols), m_functor(func)
00066 {
00067 ei_assert(rows > 0
00068 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
00069 && cols > 0
00070 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
00071 }
00072
00073 EIGEN_STRONG_INLINE int rows() const { return m_rows.value(); }
00074 EIGEN_STRONG_INLINE int cols() const { return m_cols.value(); }
00075
00076 EIGEN_STRONG_INLINE const Scalar coeff(int rows, int cols) const
00077 {
00078 return m_functor(rows, cols);
00079 }
00080
00081 template<int LoadMode>
00082 EIGEN_STRONG_INLINE PacketScalar packet(int, int) const
00083 {
00084 return m_functor.packetOp();
00085 }
00086
00087 EIGEN_STRONG_INLINE const Scalar coeff(int index) const
00088 {
00089 if(RowsAtCompileTime == 1)
00090 return m_functor(0, index);
00091 else
00092 return m_functor(index, 0);
00093 }
00094
00095 template<int LoadMode>
00096 EIGEN_STRONG_INLINE PacketScalar packet(int) const
00097 {
00098 return m_functor.packetOp();
00099 }
00100
00101 protected:
00102 const ei_int_if_dynamic<RowsAtCompileTime> m_rows;
00103 const ei_int_if_dynamic<ColsAtCompileTime> m_cols;
00104 const NullaryOp m_functor;
00105 };
00106
00107
00121 template<typename Derived>
00122 template<typename CustomNullaryOp>
00123 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00124 MatrixBase<Derived>::NullaryExpr(int rows, int cols, const CustomNullaryOp& func)
00125 {
00126 return CwiseNullaryOp<CustomNullaryOp, Derived>(rows, cols, func);
00127 }
00128
00144 template<typename Derived>
00145 template<typename CustomNullaryOp>
00146 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00147 MatrixBase<Derived>::NullaryExpr(int size, const CustomNullaryOp& func)
00148 {
00149 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00150 ei_assert(IsVectorAtCompileTime);
00151 if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, Derived>(1, size, func);
00152 else return CwiseNullaryOp<CustomNullaryOp, Derived>(size, 1, func);
00153 }
00154
00164 template<typename Derived>
00165 template<typename CustomNullaryOp>
00166 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
00167 MatrixBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
00168 {
00169 return CwiseNullaryOp<CustomNullaryOp, Derived>(RowsAtCompileTime, ColsAtCompileTime, func);
00170 }
00171
00185 template<typename Derived>
00186 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00187 MatrixBase<Derived>::Constant(int rows, int cols, const Scalar& value)
00188 {
00189 return NullaryExpr(rows, cols, ei_scalar_constant_op<Scalar>(value));
00190 }
00191
00207 template<typename Derived>
00208 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00209 MatrixBase<Derived>::Constant(int size, const Scalar& value)
00210 {
00211 return NullaryExpr(size, ei_scalar_constant_op<Scalar>(value));
00212 }
00213
00223 template<typename Derived>
00224 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00225 MatrixBase<Derived>::Constant(const Scalar& value)
00226 {
00227 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00228 return NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_constant_op<Scalar>(value));
00229 }
00230
00232 template<typename Derived>
00233 bool MatrixBase<Derived>::isApproxToConstant
00234 (const Scalar& value, RealScalar prec) const
00235 {
00236 for(int j = 0; j < cols(); ++j)
00237 for(int i = 0; i < rows(); ++i)
00238 if(!ei_isApprox(coeff(i, j), value, prec))
00239 return false;
00240 return true;
00241 }
00242
00246 template<typename Derived>
00247 bool MatrixBase<Derived>::isConstant
00248 (const Scalar& value, RealScalar prec) const
00249 {
00250 return isApproxToConstant(value, prec);
00251 }
00252
00257 template<typename Derived>
00258 EIGEN_STRONG_INLINE void MatrixBase<Derived>::fill(const Scalar& value)
00259 {
00260 setConstant(value);
00261 }
00262
00267 template<typename Derived>
00268 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setConstant(const Scalar& value)
00269 {
00270 return derived() = Constant(rows(), cols(), value);
00271 }
00272
00282 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00283 EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
00284 Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setConstant(int size, const Scalar& value)
00285 {
00286 resize(size);
00287 return setConstant(value);
00288 }
00289
00300 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00301 EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
00302 Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setConstant(int rows, int cols, const Scalar& value)
00303 {
00304 resize(rows, cols);
00305 return setConstant(value);
00306 }
00307
00308
00309
00310
00327 template<typename Derived>
00328 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00329 MatrixBase<Derived>::Zero(int rows, int cols)
00330 {
00331 return Constant(rows, cols, Scalar(0));
00332 }
00333
00350 template<typename Derived>
00351 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00352 MatrixBase<Derived>::Zero(int size)
00353 {
00354 return Constant(size, Scalar(0));
00355 }
00356
00367 template<typename Derived>
00368 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00369 MatrixBase<Derived>::Zero()
00370 {
00371 return Constant(Scalar(0));
00372 }
00373
00382 template<typename Derived>
00383 bool MatrixBase<Derived>::isZero(RealScalar prec) const
00384 {
00385 for(int j = 0; j < cols(); ++j)
00386 for(int i = 0; i < rows(); ++i)
00387 if(!ei_isMuchSmallerThan(coeff(i, j), static_cast<Scalar>(1), prec))
00388 return false;
00389 return true;
00390 }
00391
00399 template<typename Derived>
00400 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setZero()
00401 {
00402 return setConstant(Scalar(0));
00403 }
00404
00414 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00415 EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
00416 Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setZero(int size)
00417 {
00418 resize(size);
00419 return setConstant(Scalar(0));
00420 }
00421
00432 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00433 EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
00434 Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setZero(int rows, int cols)
00435 {
00436 resize(rows, cols);
00437 return setConstant(Scalar(0));
00438 }
00439
00440
00441
00458 template<typename Derived>
00459 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00460 MatrixBase<Derived>::Ones(int rows, int cols)
00461 {
00462 return Constant(rows, cols, Scalar(1));
00463 }
00464
00481 template<typename Derived>
00482 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00483 MatrixBase<Derived>::Ones(int size)
00484 {
00485 return Constant(size, Scalar(1));
00486 }
00487
00498 template<typename Derived>
00499 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::ConstantReturnType
00500 MatrixBase<Derived>::Ones()
00501 {
00502 return Constant(Scalar(1));
00503 }
00504
00513 template<typename Derived>
00514 bool MatrixBase<Derived>::isOnes
00515 (RealScalar prec) const
00516 {
00517 return isApproxToConstant(Scalar(1), prec);
00518 }
00519
00527 template<typename Derived>
00528 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setOnes()
00529 {
00530 return setConstant(Scalar(1));
00531 }
00532
00542 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00543 EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
00544 Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setOnes(int size)
00545 {
00546 resize(size);
00547 return setConstant(Scalar(1));
00548 }
00549
00560 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00561 EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
00562 Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setOnes(int rows, int cols)
00563 {
00564 resize(rows, cols);
00565 return setConstant(Scalar(1));
00566 }
00567
00568
00569
00586 template<typename Derived>
00587 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
00588 MatrixBase<Derived>::Identity(int rows, int cols)
00589 {
00590 return NullaryExpr(rows, cols, ei_scalar_identity_op<Scalar>());
00591 }
00592
00603 template<typename Derived>
00604 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
00605 MatrixBase<Derived>::Identity()
00606 {
00607 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00608 return NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_identity_op<Scalar>());
00609 }
00610
00620 template<typename Derived>
00621 bool MatrixBase<Derived>::isIdentity
00622 (RealScalar prec) const
00623 {
00624 for(int j = 0; j < cols(); ++j)
00625 {
00626 for(int i = 0; i < rows(); ++i)
00627 {
00628 if(i == j)
00629 {
00630 if(!ei_isApprox(coeff(i, j), static_cast<Scalar>(1), prec))
00631 return false;
00632 }
00633 else
00634 {
00635 if(!ei_isMuchSmallerThan(coeff(i, j), static_cast<RealScalar>(1), prec))
00636 return false;
00637 }
00638 }
00639 }
00640 return true;
00641 }
00642
00643 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
00644 struct ei_setIdentity_impl
00645 {
00646 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
00647 {
00648 return m = Derived::Identity(m.rows(), m.cols());
00649 }
00650 };
00651
00652 template<typename Derived>
00653 struct ei_setIdentity_impl<Derived, true>
00654 {
00655 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
00656 {
00657 m.setZero();
00658 const int size = std::min(m.rows(), m.cols());
00659 for(int i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
00660 return m;
00661 }
00662 };
00663
00671 template<typename Derived>
00672 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
00673 {
00674 return ei_setIdentity_impl<Derived>::run(derived());
00675 }
00676
00687 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00688 EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
00689 Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setIdentity(int rows, int cols)
00690 {
00691 resize(rows, cols);
00692 return setIdentity();
00693 }
00694
00701 template<typename Derived>
00702 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(int size, int i)
00703 {
00704 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00705 return BasisReturnType(SquareMatrixType::Identity(size,size), i);
00706 }
00707
00716 template<typename Derived>
00717 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(int i)
00718 {
00719 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00720 return BasisReturnType(SquareMatrixType::Identity(),i);
00721 }
00722
00729 template<typename Derived>
00730 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
00731 { return Derived::Unit(0); }
00732
00739 template<typename Derived>
00740 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
00741 { return Derived::Unit(1); }
00742
00749 template<typename Derived>
00750 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
00751 { return Derived::Unit(2); }
00752
00759 template<typename Derived>
00760 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
00761 { return Derived::Unit(3); }
00762
00763 #endif // EIGEN_CWISE_NULLARY_OP_H