MOAB
4.9.3pre
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2010 Gael Guennebaud <[email protected]> 00005 // 00006 // This Source Code Form is subject to the terms of the Mozilla 00007 // Public License v. 2.0. If a copy of the MPL was not distributed 00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00009 00010 #ifndef EIGEN_CWISE_NULLARY_OP_H 00011 #define EIGEN_CWISE_NULLARY_OP_H 00012 00013 namespace Eigen { 00014 00015 namespace internal { 00016 template<typename NullaryOp, typename PlainObjectType> 00017 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType> 00018 { 00019 enum { 00020 Flags = traits<PlainObjectType>::Flags & RowMajorBit 00021 }; 00022 }; 00023 } 00024 00042 template<typename NullaryOp, typename PlainObjectType> 00043 class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator 00044 { 00045 public: 00046 00047 typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base; 00048 EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp) 00049 00050 EIGEN_DEVICE_FUNC 00051 CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp()) 00052 : m_rows(rows), m_cols(cols), m_functor(func) 00053 { 00054 eigen_assert(rows >= 0 00055 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) 00056 && cols >= 0 00057 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)); 00058 } 00059 00060 EIGEN_DEVICE_FUNC 00061 EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); } 00062 EIGEN_DEVICE_FUNC 00063 EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); } 00064 00065 EIGEN_DEVICE_FUNC 00066 EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const 00067 { 00068 return m_functor(rowId, colId); 00069 } 00070 00071 template<int LoadMode> 00072 EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const 00073 { 00074 return m_functor.packetOp(rowId, colId); 00075 } 00076 00077 EIGEN_DEVICE_FUNC 00078 EIGEN_STRONG_INLINE const Scalar coeff(Index index) const 00079 { 00080 return m_functor(index); 00081 } 00082 00083 template<int LoadMode> 00084 EIGEN_STRONG_INLINE PacketScalar packet(Index index) const 00085 { 00086 return m_functor.packetOp(index); 00087 } 00088 00090 EIGEN_DEVICE_FUNC 00091 const NullaryOp& functor() const { return m_functor; } 00092 00093 protected: 00094 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows; 00095 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols; 00096 const NullaryOp m_functor; 00097 }; 00098 00099 00113 template<typename Derived> 00114 template<typename CustomNullaryOp> 00115 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject> 00116 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func) 00117 { 00118 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func); 00119 } 00120 00139 template<typename Derived> 00140 template<typename CustomNullaryOp> 00141 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject> 00142 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func) 00143 { 00144 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00145 if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func); 00146 else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func); 00147 } 00148 00158 template<typename Derived> 00159 template<typename CustomNullaryOp> 00160 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject> 00161 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func) 00162 { 00163 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func); 00164 } 00165 00179 template<typename Derived> 00180 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00181 DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value) 00182 { 00183 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value)); 00184 } 00185 00201 template<typename Derived> 00202 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00203 DenseBase<Derived>::Constant(Index size, const Scalar& value) 00204 { 00205 return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value)); 00206 } 00207 00217 template<typename Derived> 00218 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00219 DenseBase<Derived>::Constant(const Scalar& value) 00220 { 00221 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00222 return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value)); 00223 } 00224 00242 template<typename Derived> 00243 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType 00244 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high) 00245 { 00246 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00247 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar,false>(low,high,size)); 00248 } 00249 00254 template<typename Derived> 00255 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType 00256 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high) 00257 { 00258 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00259 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00260 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar,false>(low,high,Derived::SizeAtCompileTime)); 00261 } 00262 00276 template<typename Derived> 00277 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType 00278 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high) 00279 { 00280 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00281 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar,true>(low,high,size)); 00282 } 00283 00288 template<typename Derived> 00289 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType 00290 DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high) 00291 { 00292 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00293 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00294 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar,true>(low,high,Derived::SizeAtCompileTime)); 00295 } 00296 00298 template<typename Derived> 00299 bool DenseBase<Derived>::isApproxToConstant 00300 (const Scalar& val, const RealScalar& prec) const 00301 { 00302 typename internal::nested_eval<Derived,1>::type self(derived()); 00303 for(Index j = 0; j < cols(); ++j) 00304 for(Index i = 0; i < rows(); ++i) 00305 if(!internal::isApprox(self.coeff(i, j), val, prec)) 00306 return false; 00307 return true; 00308 } 00309 00313 template<typename Derived> 00314 bool DenseBase<Derived>::isConstant 00315 (const Scalar& val, const RealScalar& prec) const 00316 { 00317 return isApproxToConstant(val, prec); 00318 } 00319 00324 template<typename Derived> 00325 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val) 00326 { 00327 setConstant(val); 00328 } 00329 00334 template<typename Derived> 00335 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val) 00336 { 00337 return derived() = Constant(rows(), cols(), val); 00338 } 00339 00349 template<typename Derived> 00350 EIGEN_STRONG_INLINE Derived& 00351 PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val) 00352 { 00353 resize(size); 00354 return setConstant(val); 00355 } 00356 00368 template<typename Derived> 00369 EIGEN_STRONG_INLINE Derived& 00370 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val) 00371 { 00372 resize(rows, cols); 00373 return setConstant(val); 00374 } 00375 00389 template<typename Derived> 00390 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high) 00391 { 00392 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00393 return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,PacketScalar,false>(low,high,newSize)); 00394 } 00395 00406 template<typename Derived> 00407 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high) 00408 { 00409 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00410 return setLinSpaced(size(), low, high); 00411 } 00412 00413 // zero: 00414 00429 template<typename Derived> 00430 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00431 DenseBase<Derived>::Zero(Index rows, Index cols) 00432 { 00433 return Constant(rows, cols, Scalar(0)); 00434 } 00435 00452 template<typename Derived> 00453 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00454 DenseBase<Derived>::Zero(Index size) 00455 { 00456 return Constant(size, Scalar(0)); 00457 } 00458 00469 template<typename Derived> 00470 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00471 DenseBase<Derived>::Zero() 00472 { 00473 return Constant(Scalar(0)); 00474 } 00475 00484 template<typename Derived> 00485 bool DenseBase<Derived>::isZero(const RealScalar& prec) const 00486 { 00487 typename internal::nested_eval<Derived,1>::type self(derived()); 00488 for(Index j = 0; j < cols(); ++j) 00489 for(Index i = 0; i < rows(); ++i) 00490 if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec)) 00491 return false; 00492 return true; 00493 } 00494 00502 template<typename Derived> 00503 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero() 00504 { 00505 return setConstant(Scalar(0)); 00506 } 00507 00517 template<typename Derived> 00518 EIGEN_STRONG_INLINE Derived& 00519 PlainObjectBase<Derived>::setZero(Index newSize) 00520 { 00521 resize(newSize); 00522 return setConstant(Scalar(0)); 00523 } 00524 00535 template<typename Derived> 00536 EIGEN_STRONG_INLINE Derived& 00537 PlainObjectBase<Derived>::setZero(Index rows, Index cols) 00538 { 00539 resize(rows, cols); 00540 return setConstant(Scalar(0)); 00541 } 00542 00543 // ones: 00544 00559 template<typename Derived> 00560 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00561 DenseBase<Derived>::Ones(Index rows, Index cols) 00562 { 00563 return Constant(rows, cols, Scalar(1)); 00564 } 00565 00582 template<typename Derived> 00583 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00584 DenseBase<Derived>::Ones(Index newSize) 00585 { 00586 return Constant(newSize, Scalar(1)); 00587 } 00588 00599 template<typename Derived> 00600 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00601 DenseBase<Derived>::Ones() 00602 { 00603 return Constant(Scalar(1)); 00604 } 00605 00614 template<typename Derived> 00615 bool DenseBase<Derived>::isOnes 00616 (const RealScalar& prec) const 00617 { 00618 return isApproxToConstant(Scalar(1), prec); 00619 } 00620 00628 template<typename Derived> 00629 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes() 00630 { 00631 return setConstant(Scalar(1)); 00632 } 00633 00643 template<typename Derived> 00644 EIGEN_STRONG_INLINE Derived& 00645 PlainObjectBase<Derived>::setOnes(Index newSize) 00646 { 00647 resize(newSize); 00648 return setConstant(Scalar(1)); 00649 } 00650 00661 template<typename Derived> 00662 EIGEN_STRONG_INLINE Derived& 00663 PlainObjectBase<Derived>::setOnes(Index rows, Index cols) 00664 { 00665 resize(rows, cols); 00666 return setConstant(Scalar(1)); 00667 } 00668 00669 // Identity: 00670 00685 template<typename Derived> 00686 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType 00687 MatrixBase<Derived>::Identity(Index rows, Index cols) 00688 { 00689 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>()); 00690 } 00691 00702 template<typename Derived> 00703 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType 00704 MatrixBase<Derived>::Identity() 00705 { 00706 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00707 return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>()); 00708 } 00709 00719 template<typename Derived> 00720 bool MatrixBase<Derived>::isIdentity 00721 (const RealScalar& prec) const 00722 { 00723 typename internal::nested_eval<Derived,1>::type self(derived()); 00724 for(Index j = 0; j < cols(); ++j) 00725 { 00726 for(Index i = 0; i < rows(); ++i) 00727 { 00728 if(i == j) 00729 { 00730 if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec)) 00731 return false; 00732 } 00733 else 00734 { 00735 if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec)) 00736 return false; 00737 } 00738 } 00739 } 00740 return true; 00741 } 00742 00743 namespace internal { 00744 00745 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)> 00746 struct setIdentity_impl 00747 { 00748 EIGEN_DEVICE_FUNC 00749 static EIGEN_STRONG_INLINE Derived& run(Derived& m) 00750 { 00751 return m = Derived::Identity(m.rows(), m.cols()); 00752 } 00753 }; 00754 00755 template<typename Derived> 00756 struct setIdentity_impl<Derived, true> 00757 { 00758 EIGEN_DEVICE_FUNC 00759 static EIGEN_STRONG_INLINE Derived& run(Derived& m) 00760 { 00761 m.setZero(); 00762 const Index size = (std::min)(m.rows(), m.cols()); 00763 for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1); 00764 return m; 00765 } 00766 }; 00767 00768 } // end namespace internal 00769 00777 template<typename Derived> 00778 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity() 00779 { 00780 return internal::setIdentity_impl<Derived>::run(derived()); 00781 } 00782 00793 template<typename Derived> 00794 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index p_rows, Index p_cols) 00795 { 00796 derived().resize(p_rows, p_cols); 00797 return setIdentity(); 00798 } 00799 00806 template<typename Derived> 00807 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i) 00808 { 00809 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00810 return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i); 00811 } 00812 00821 template<typename Derived> 00822 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i) 00823 { 00824 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00825 return BasisReturnType(SquareMatrixType::Identity(),i); 00826 } 00827 00834 template<typename Derived> 00835 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX() 00836 { return Derived::Unit(0); } 00837 00844 template<typename Derived> 00845 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY() 00846 { return Derived::Unit(1); } 00847 00854 template<typename Derived> 00855 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ() 00856 { return Derived::Unit(2); } 00857 00864 template<typename Derived> 00865 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW() 00866 { return Derived::Unit(3); } 00867 00868 } // end namespace Eigen 00869 00870 #endif // EIGEN_CWISE_NULLARY_OP_H