![]() |
Eigen
3.3.3
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr> 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 } // namespace internal 00025 00059 template<typename NullaryOp, typename PlainObjectType> 00060 class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator 00061 { 00062 public: 00063 00064 typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base; 00065 EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp) 00066 00067 EIGEN_DEVICE_FUNC 00068 CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp()) 00069 : m_rows(rows), m_cols(cols), m_functor(func) 00070 { 00071 eigen_assert(rows >= 0 00072 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) 00073 && cols >= 0 00074 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)); 00075 } 00076 00077 EIGEN_DEVICE_FUNC 00078 EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); } 00079 EIGEN_DEVICE_FUNC 00080 EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); } 00081 00083 EIGEN_DEVICE_FUNC 00084 const NullaryOp& functor() const { return m_functor; } 00085 00086 protected: 00087 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows; 00088 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols; 00089 const NullaryOp m_functor; 00090 }; 00091 00092 00106 template<typename Derived> 00107 template<typename CustomNullaryOp> 00108 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject> 00109 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func) 00110 { 00111 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func); 00112 } 00113 00132 template<typename Derived> 00133 template<typename CustomNullaryOp> 00134 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject> 00135 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func) 00136 { 00137 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00138 if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func); 00139 else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func); 00140 } 00141 00151 template<typename Derived> 00152 template<typename CustomNullaryOp> 00153 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject> 00154 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func) 00155 { 00156 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func); 00157 } 00158 00172 template<typename Derived> 00173 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00174 DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value) 00175 { 00176 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value)); 00177 } 00178 00194 template<typename Derived> 00195 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00196 DenseBase<Derived>::Constant(Index size, const Scalar& value) 00197 { 00198 return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value)); 00199 } 00200 00210 template<typename Derived> 00211 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00212 DenseBase<Derived>::Constant(const Scalar& value) 00213 { 00214 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00215 return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value)); 00216 } 00217 00222 template<typename Derived> 00223 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType 00224 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high) 00225 { 00226 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00227 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size)); 00228 } 00229 00234 template<typename Derived> 00235 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType 00236 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high) 00237 { 00238 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00239 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00240 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime)); 00241 } 00242 00266 template<typename Derived> 00267 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType 00268 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high) 00269 { 00270 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00271 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size)); 00272 } 00273 00278 template<typename Derived> 00279 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType 00280 DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high) 00281 { 00282 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00283 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00284 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime)); 00285 } 00286 00288 template<typename Derived> 00289 bool DenseBase<Derived>::isApproxToConstant 00290 (const Scalar& val, const RealScalar& prec) const 00291 { 00292 typename internal::nested_eval<Derived,1>::type self(derived()); 00293 for(Index j = 0; j < cols(); ++j) 00294 for(Index i = 0; i < rows(); ++i) 00295 if(!internal::isApprox(self.coeff(i, j), val, prec)) 00296 return false; 00297 return true; 00298 } 00299 00303 template<typename Derived> 00304 bool DenseBase<Derived>::isConstant 00305 (const Scalar& val, const RealScalar& prec) const 00306 { 00307 return isApproxToConstant(val, prec); 00308 } 00309 00314 template<typename Derived> 00315 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val) 00316 { 00317 setConstant(val); 00318 } 00319 00324 template<typename Derived> 00325 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val) 00326 { 00327 return derived() = Constant(rows(), cols(), val); 00328 } 00329 00339 template<typename Derived> 00340 EIGEN_STRONG_INLINE Derived& 00341 PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val) 00342 { 00343 resize(size); 00344 return setConstant(val); 00345 } 00346 00358 template<typename Derived> 00359 EIGEN_STRONG_INLINE Derived& 00360 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val) 00361 { 00362 resize(rows, cols); 00363 return setConstant(val); 00364 } 00365 00382 template<typename Derived> 00383 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high) 00384 { 00385 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00386 return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,PacketScalar>(low,high,newSize)); 00387 } 00388 00402 template<typename Derived> 00403 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high) 00404 { 00405 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00406 return setLinSpaced(size(), low, high); 00407 } 00408 00409 // zero: 00410 00425 template<typename Derived> 00426 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00427 DenseBase<Derived>::Zero(Index rows, Index cols) 00428 { 00429 return Constant(rows, cols, Scalar(0)); 00430 } 00431 00448 template<typename Derived> 00449 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00450 DenseBase<Derived>::Zero(Index size) 00451 { 00452 return Constant(size, Scalar(0)); 00453 } 00454 00465 template<typename Derived> 00466 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00467 DenseBase<Derived>::Zero() 00468 { 00469 return Constant(Scalar(0)); 00470 } 00471 00480 template<typename Derived> 00481 bool DenseBase<Derived>::isZero(const RealScalar& prec) const 00482 { 00483 typename internal::nested_eval<Derived,1>::type self(derived()); 00484 for(Index j = 0; j < cols(); ++j) 00485 for(Index i = 0; i < rows(); ++i) 00486 if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec)) 00487 return false; 00488 return true; 00489 } 00490 00498 template<typename Derived> 00499 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero() 00500 { 00501 return setConstant(Scalar(0)); 00502 } 00503 00513 template<typename Derived> 00514 EIGEN_STRONG_INLINE Derived& 00515 PlainObjectBase<Derived>::setZero(Index newSize) 00516 { 00517 resize(newSize); 00518 return setConstant(Scalar(0)); 00519 } 00520 00531 template<typename Derived> 00532 EIGEN_STRONG_INLINE Derived& 00533 PlainObjectBase<Derived>::setZero(Index rows, Index cols) 00534 { 00535 resize(rows, cols); 00536 return setConstant(Scalar(0)); 00537 } 00538 00539 // ones: 00540 00555 template<typename Derived> 00556 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00557 DenseBase<Derived>::Ones(Index rows, Index cols) 00558 { 00559 return Constant(rows, cols, Scalar(1)); 00560 } 00561 00578 template<typename Derived> 00579 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00580 DenseBase<Derived>::Ones(Index newSize) 00581 { 00582 return Constant(newSize, Scalar(1)); 00583 } 00584 00595 template<typename Derived> 00596 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType 00597 DenseBase<Derived>::Ones() 00598 { 00599 return Constant(Scalar(1)); 00600 } 00601 00610 template<typename Derived> 00611 bool DenseBase<Derived>::isOnes 00612 (const RealScalar& prec) const 00613 { 00614 return isApproxToConstant(Scalar(1), prec); 00615 } 00616 00624 template<typename Derived> 00625 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes() 00626 { 00627 return setConstant(Scalar(1)); 00628 } 00629 00639 template<typename Derived> 00640 EIGEN_STRONG_INLINE Derived& 00641 PlainObjectBase<Derived>::setOnes(Index newSize) 00642 { 00643 resize(newSize); 00644 return setConstant(Scalar(1)); 00645 } 00646 00657 template<typename Derived> 00658 EIGEN_STRONG_INLINE Derived& 00659 PlainObjectBase<Derived>::setOnes(Index rows, Index cols) 00660 { 00661 resize(rows, cols); 00662 return setConstant(Scalar(1)); 00663 } 00664 00665 // Identity: 00666 00681 template<typename Derived> 00682 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType 00683 MatrixBase<Derived>::Identity(Index rows, Index cols) 00684 { 00685 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>()); 00686 } 00687 00698 template<typename Derived> 00699 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType 00700 MatrixBase<Derived>::Identity() 00701 { 00702 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived) 00703 return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>()); 00704 } 00705 00715 template<typename Derived> 00716 bool MatrixBase<Derived>::isIdentity 00717 (const RealScalar& prec) const 00718 { 00719 typename internal::nested_eval<Derived,1>::type self(derived()); 00720 for(Index j = 0; j < cols(); ++j) 00721 { 00722 for(Index i = 0; i < rows(); ++i) 00723 { 00724 if(i == j) 00725 { 00726 if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec)) 00727 return false; 00728 } 00729 else 00730 { 00731 if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec)) 00732 return false; 00733 } 00734 } 00735 } 00736 return true; 00737 } 00738 00739 namespace internal { 00740 00741 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)> 00742 struct setIdentity_impl 00743 { 00744 EIGEN_DEVICE_FUNC 00745 static EIGEN_STRONG_INLINE Derived& run(Derived& m) 00746 { 00747 return m = Derived::Identity(m.rows(), m.cols()); 00748 } 00749 }; 00750 00751 template<typename Derived> 00752 struct setIdentity_impl<Derived, true> 00753 { 00754 EIGEN_DEVICE_FUNC 00755 static EIGEN_STRONG_INLINE Derived& run(Derived& m) 00756 { 00757 m.setZero(); 00758 const Index size = numext::mini(m.rows(), m.cols()); 00759 for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1); 00760 return m; 00761 } 00762 }; 00763 00764 } // end namespace internal 00765 00773 template<typename Derived> 00774 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity() 00775 { 00776 return internal::setIdentity_impl<Derived>::run(derived()); 00777 } 00778 00789 template<typename Derived> 00790 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols) 00791 { 00792 derived().resize(rows, cols); 00793 return setIdentity(); 00794 } 00795 00802 template<typename Derived> 00803 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i) 00804 { 00805 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00806 return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i); 00807 } 00808 00817 template<typename Derived> 00818 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i) 00819 { 00820 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) 00821 return BasisReturnType(SquareMatrixType::Identity(),i); 00822 } 00823 00830 template<typename Derived> 00831 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX() 00832 { return Derived::Unit(0); } 00833 00840 template<typename Derived> 00841 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY() 00842 { return Derived::Unit(1); } 00843 00850 template<typename Derived> 00851 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ() 00852 { return Derived::Unit(2); } 00853 00860 template<typename Derived> 00861 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW() 00862 { return Derived::Unit(3); } 00863 00864 } // end namespace Eigen 00865 00866 #endif // EIGEN_CWISE_NULLARY_OP_H