Eigen  3.3.0
 
Loading...
Searching...
No Matches
CwiseNullaryOp.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_CWISE_NULLARY_OP_H
11#define EIGEN_CWISE_NULLARY_OP_H
12
13namespace Eigen {
14
15namespace internal {
16template<typename NullaryOp, typename PlainObjectType>
17struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
18{
19 enum {
20 Flags = traits<PlainObjectType>::Flags & RowMajorBit
21 };
22};
23
24} // namespace internal
25
59template<typename NullaryOp, typename PlainObjectType>
60class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
61{
62 public:
63
64 typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
65 EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
66
67 EIGEN_DEVICE_FUNC
68 CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
69 : m_rows(rows), m_cols(cols), m_functor(func)
70 {
71 eigen_assert(rows >= 0
72 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
73 && cols >= 0
74 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
75 }
76
77 EIGEN_DEVICE_FUNC
78 EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
79 EIGEN_DEVICE_FUNC
80 EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
81
83 EIGEN_DEVICE_FUNC
84 const NullaryOp& functor() const { return m_functor; }
85
86 protected:
87 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
88 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
89 const NullaryOp m_functor;
90};
91
92
106template<typename Derived>
107template<typename CustomNullaryOp>
108EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
109DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
110{
111 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
112}
113
132template<typename Derived>
133template<typename CustomNullaryOp>
135DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
136{
137 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
138 if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
139 else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
140}
141
151template<typename Derived>
152template<typename CustomNullaryOp>
154DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
155{
156 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
157}
158
172template<typename Derived>
173EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
175{
176 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
177}
178
194template<typename Derived>
195EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
197{
198 return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
199}
200
210template<typename Derived>
211EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
213{
214 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
215 return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
216}
217
222template<typename Derived>
223EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
224DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
225{
226 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
227 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size));
228}
229
234template<typename Derived>
235EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
236DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
237{
238 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
239 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
240 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime));
241}
242
266template<typename Derived>
268DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
270 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
271 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size));
272}
273
278template<typename Derived>
282 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
283 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
284 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime));
285}
286
288template<typename Derived>
290(const Scalar& val, const RealScalar& prec) const
291{
292 typename internal::nested_eval<Derived,1>::type self(derived());
293 for(Index j = 0; j < cols(); ++j)
294 for(Index i = 0; i < rows(); ++i)
295 if(!internal::isApprox(self.coeff(i, j), val, prec))
296 return false;
297 return true;
298}
299
303template<typename Derived>
305(const Scalar& val, const RealScalar& prec) const
306{
307 return isApproxToConstant(val, prec);
308}
309
314template<typename Derived>
315EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
316{
317 setConstant(val);
318}
319
324template<typename Derived>
325EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
326{
327 return derived() = Constant(rows(), cols(), val);
328}
339template<typename Derived>
340EIGEN_STRONG_INLINE Derived&
343 resize(size);
344 return setConstant(val);
345}
346
358template<typename Derived>
359EIGEN_STRONG_INLINE Derived&
362 resize(rows, cols);
363 return setConstant(val);
382template<typename Derived>
383EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
384{
385 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
386 return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,PacketScalar>(low,high,newSize));
387}
388
402template<typename Derived>
403EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
404{
405 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
406 return setLinSpaced(size(), low, high);
407}
408
409// zero:
410
425template<typename Derived>
426EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
428{
429 return Constant(rows, cols, Scalar(0));
430}
431
448template<typename Derived>
449EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
451{
452 return Constant(size, Scalar(0));
453}
454
465template<typename Derived>
466EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
468{
469 return Constant(Scalar(0));
470}
471
480template<typename Derived>
481bool DenseBase<Derived>::isZero(const RealScalar& prec) const
482{
483 typename internal::nested_eval<Derived,1>::type self(derived());
484 for(Index j = 0; j < cols(); ++j)
485 for(Index i = 0; i < rows(); ++i)
486 if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
487 return false;
488 return true;
489}
490
498template<typename Derived>
499EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
500{
501 return setConstant(Scalar(0));
502}
503
513template<typename Derived>
514EIGEN_STRONG_INLINE Derived&
516{
517 resize(newSize);
518 return setConstant(Scalar(0));
519}
520
531template<typename Derived>
532EIGEN_STRONG_INLINE Derived&
534{
535 resize(rows, cols);
536 return setConstant(Scalar(0));
537}
538
539// ones:
540
555template<typename Derived>
556EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
558{
559 return Constant(rows, cols, Scalar(1));
560}
561
578template<typename Derived>
579EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
581{
582 return Constant(newSize, Scalar(1));
583}
584
595template<typename Derived>
596EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
598{
599 return Constant(Scalar(1));
600}
601
610template<typename Derived>
612(const RealScalar& prec) const
613{
614 return isApproxToConstant(Scalar(1), prec);
615}
616
624template<typename Derived>
625EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
626{
627 return setConstant(Scalar(1));
628}
629
639template<typename Derived>
640EIGEN_STRONG_INLINE Derived&
643 resize(newSize);
644 return setConstant(Scalar(1));
645}
657template<typename Derived>
658EIGEN_STRONG_INLINE Derived&
660{
661 resize(rows, cols);
662 return setConstant(Scalar(1));
663}
664
665// Identity:
666
681template<typename Derived>
682EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
684{
685 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
686}
687
698template<typename Derived>
699EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
701{
702 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
703 return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
704}
705
715template<typename Derived>
717(const RealScalar& prec) const
718{
719 typename internal::nested_eval<Derived,1>::type self(derived());
720 for(Index j = 0; j < cols(); ++j)
721 {
722 for(Index i = 0; i < rows(); ++i)
723 {
724 if(i == j)
725 {
726 if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
727 return false;
728 }
729 else
730 {
731 if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
732 return false;
733 }
734 }
735 }
736 return true;
737}
738
739namespace internal {
740
741template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
742struct setIdentity_impl
743{
744 EIGEN_DEVICE_FUNC
745 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
746 {
747 return m = Derived::Identity(m.rows(), m.cols());
748 }
749};
750
751template<typename Derived>
752struct setIdentity_impl<Derived, true>
753{
754 EIGEN_DEVICE_FUNC
755 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
756 {
757 m.setZero();
758 const Index size = numext::mini(m.rows(), m.cols());
759 for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
760 return m;
761 }
762};
763
764} // end namespace internal
765
773template<typename Derived>
774EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
775{
776 return internal::setIdentity_impl<Derived>::run(derived());
777}
778
789template<typename Derived>
790EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
791{
792 derived().resize(rows, cols);
793 return setIdentity();
794}
795
802template<typename Derived>
803EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
804{
805 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
806 return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
807}
808
817template<typename Derived>
819{
820 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
821 return BasisReturnType(SquareMatrixType::Identity(),i);
822}
823
830template<typename Derived>
832{ return Derived::Unit(0); }
833
840template<typename Derived>
842{ return Derived::Unit(1); }
843
850template<typename Derived>
852{ return Derived::Unit(2); }
853
860template<typename Derived>
862{ return Derived::Unit(3); }
863
864} // end namespace Eigen
865
866#endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:61
const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:84
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:47
void resize(Index newSize)
Definition: DenseBase.h:241
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
Dense storage base class for matrices and arrays.
Definition: PlainObjectBase.h:94
const unsigned int RowMajorBit
Definition: Constants.h:61
Namespace containing all symbols from the Eigen library.
Definition: Core:287
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
const int Dynamic
Definition: Constants.h:21