11 #ifndef EIGEN_JACOBI_H 12 #define EIGEN_JACOBI_H 34 template<
typename Scalar>
class JacobiRotation
37 typedef typename NumTraits<Scalar>::Real RealScalar;
45 Scalar& c() {
return m_c; }
46 Scalar c()
const {
return m_c; }
47 Scalar& s() {
return m_s; }
48 Scalar s()
const {
return m_s; }
64 template<
typename Derived>
66 bool makeJacobi(
const RealScalar& x,
const Scalar& y,
const RealScalar& z);
68 void makeGivens(
const Scalar& p,
const Scalar& q, Scalar* r=0);
71 void makeGivens(
const Scalar& p,
const Scalar& q, Scalar* r, internal::true_type);
72 void makeGivens(
const Scalar& p,
const Scalar& q, Scalar* r, internal::false_type);
82 template<
typename Scalar>
87 RealScalar deno = RealScalar(2)*
abs(y);
88 if(deno < (std::numeric_limits<RealScalar>::min)())
96 RealScalar tau = (x-z)/deno;
97 RealScalar w =
sqrt(numext::abs2(tau) + RealScalar(1));
101 t = RealScalar(1) / (tau + w);
105 t = RealScalar(1) / (tau - w);
107 RealScalar sign_t = t > RealScalar(0) ? RealScalar(1) : RealScalar(-1);
108 RealScalar n = RealScalar(1) /
sqrt(numext::abs2(t)+RealScalar(1));
109 m_s = - sign_t * (numext::conj(y) /
abs(y)) *
abs(t) * n;
124 template<
typename Scalar>
125 template<
typename Derived>
128 return makeJacobi(numext::real(m.
coeff(p,p)), m.
coeff(p,q), numext::real(m.
coeff(q,q)));
147 template<
typename Scalar>
155 template<
typename Scalar>
164 m_c = numext::real(p)<0 ? Scalar(-1) : Scalar(1);
168 else if(p==Scalar(0))
176 RealScalar p1 = numext::norm1(p);
177 RealScalar q1 = numext::norm1(q);
181 RealScalar p2 = numext::abs2(ps);
183 RealScalar q2 = numext::abs2(qs);
185 RealScalar u =
sqrt(RealScalar(1) + q2/p2);
186 if(numext::real(p)<RealScalar(0))
190 m_s = -qs*
conj(ps)*(m_c/p2);
196 RealScalar p2 = numext::abs2(ps);
198 RealScalar q2 = numext::abs2(qs);
200 RealScalar u = q1 *
sqrt(p2 + q2);
201 if(numext::real(p)<RealScalar(0))
207 m_s = -
conj(ps) * (q/u);
214 template<
typename Scalar>
221 m_c = p<Scalar(0) ? Scalar(-1) : Scalar(1);
225 else if(p==Scalar(0))
228 m_s = q<Scalar(0) ? Scalar(1) : Scalar(-1);
234 Scalar u =
sqrt(Scalar(1) + numext::abs2(t));
244 Scalar u =
sqrt(Scalar(1) + numext::abs2(t));
265 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
266 void apply_rotation_in_the_plane(DenseBase<VectorX>& xpr_x, DenseBase<VectorY>& xpr_y,
const JacobiRotation<OtherScalar>& j);
275 template<
typename Derived>
276 template<
typename OtherScalar>
279 RowXpr x(this->row(p));
280 RowXpr y(this->row(q));
281 internal::apply_rotation_in_the_plane(x, y, j);
290 template<
typename Derived>
291 template<
typename OtherScalar>
294 ColXpr x(this->col(p));
295 ColXpr y(this->col(q));
296 internal::apply_rotation_in_the_plane(x, y, j.transpose());
301 template<
typename Scalar,
typename OtherScalar,
302 int SizeAtCompileTime,
int MinAlignment,
bool Vectorizable>
303 struct apply_rotation_in_the_plane_selector
305 static inline void run(Scalar *x,
Index incrx, Scalar *y,
Index incry,
Index size, OtherScalar c, OtherScalar s)
307 for(
Index i=0; i<size; ++i)
311 *x = c * xi + numext::conj(s) * yi;
312 *y = -s * xi + numext::conj(c) * yi;
319 template<
typename Scalar,
typename OtherScalar,
320 int SizeAtCompileTime,
int MinAlignment>
321 struct apply_rotation_in_the_plane_selector<Scalar,OtherScalar,SizeAtCompileTime,MinAlignment,true >
323 static inline void run(Scalar *x,
Index incrx, Scalar *y,
Index incry,
Index size, OtherScalar c, OtherScalar s)
326 PacketSize = packet_traits<Scalar>::size,
327 OtherPacketSize = packet_traits<OtherScalar>::size
329 typedef typename packet_traits<Scalar>::type Packet;
330 typedef typename packet_traits<OtherScalar>::type OtherPacket;
333 if(SizeAtCompileTime ==
Dynamic && ((incrx==1 && incry==1) || PacketSize == 1))
336 enum { Peeling = 2 };
338 Index alignedStart = internal::first_default_aligned(y, size);
339 Index alignedEnd = alignedStart + ((size-alignedStart)/PacketSize)*PacketSize;
341 const OtherPacket pc = pset1<OtherPacket>(c);
342 const OtherPacket ps = pset1<OtherPacket>(s);
343 conj_helper<OtherPacket,Packet,NumTraits<OtherScalar>::IsComplex,
false> pcj;
344 conj_helper<OtherPacket,Packet,false,false> pm;
346 for(
Index i=0; i<alignedStart; ++i)
350 x[i] = c * xi + numext::conj(s) * yi;
351 y[i] = -s * xi + numext::conj(c) * yi;
354 Scalar* EIGEN_RESTRICT px = x + alignedStart;
355 Scalar* EIGEN_RESTRICT py = y + alignedStart;
357 if(internal::first_default_aligned(x, size)==alignedStart)
359 for(
Index i=alignedStart; i<alignedEnd; i+=PacketSize)
361 Packet xi = pload<Packet>(px);
362 Packet yi = pload<Packet>(py);
363 pstore(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
364 pstore(py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
371 Index peelingEnd = alignedStart + ((size-alignedStart)/(Peeling*PacketSize))*(Peeling*PacketSize);
372 for(
Index i=alignedStart; i<peelingEnd; i+=Peeling*PacketSize)
374 Packet xi = ploadu<Packet>(px);
375 Packet xi1 = ploadu<Packet>(px+PacketSize);
376 Packet yi = pload <Packet>(py);
377 Packet yi1 = pload <Packet>(py+PacketSize);
378 pstoreu(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
379 pstoreu(px+PacketSize, padd(pm.pmul(pc,xi1),pcj.pmul(ps,yi1)));
380 pstore (py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
381 pstore (py+PacketSize, psub(pcj.pmul(pc,yi1),pm.pmul(ps,xi1)));
382 px += Peeling*PacketSize;
383 py += Peeling*PacketSize;
385 if(alignedEnd!=peelingEnd)
387 Packet xi = ploadu<Packet>(x+peelingEnd);
388 Packet yi = pload <Packet>(y+peelingEnd);
389 pstoreu(x+peelingEnd, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
390 pstore (y+peelingEnd, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
394 for(
Index i=alignedEnd; i<size; ++i)
398 x[i] = c * xi + numext::conj(s) * yi;
399 y[i] = -s * xi + numext::conj(c) * yi;
404 else if(SizeAtCompileTime !=
Dynamic && MinAlignment>0)
406 const OtherPacket pc = pset1<OtherPacket>(c);
407 const OtherPacket ps = pset1<OtherPacket>(s);
408 conj_helper<OtherPacket,Packet,NumTraits<OtherPacket>::IsComplex,
false> pcj;
409 conj_helper<OtherPacket,Packet,false,false> pm;
410 Scalar* EIGEN_RESTRICT px = x;
411 Scalar* EIGEN_RESTRICT py = y;
412 for(
Index i=0; i<size; i+=PacketSize)
414 Packet xi = pload<Packet>(px);
415 Packet yi = pload<Packet>(py);
416 pstore(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
417 pstore(py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
426 apply_rotation_in_the_plane_selector<Scalar,OtherScalar,SizeAtCompileTime,MinAlignment,false>::run(x,incrx,y,incry,size,c,s);
431 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
432 void apply_rotation_in_the_plane(DenseBase<VectorX>& xpr_x, DenseBase<VectorY>& xpr_y,
const JacobiRotation<OtherScalar>& j)
434 typedef typename VectorX::Scalar Scalar;
435 const bool Vectorizable = (VectorX::Flags & VectorY::Flags &
PacketAccessBit)
436 && (
int(packet_traits<Scalar>::size) == int(packet_traits<OtherScalar>::size));
438 eigen_assert(xpr_x.size() == xpr_y.size());
439 Index size = xpr_x.size();
440 Index incrx = xpr_x.derived().innerStride();
441 Index incry = xpr_y.derived().innerStride();
443 Scalar* EIGEN_RESTRICT x = &xpr_x.derived().coeffRef(0);
444 Scalar* EIGEN_RESTRICT y = &xpr_y.derived().coeffRef(0);
446 OtherScalar c = j.c();
447 OtherScalar s = j.s();
448 if (c==OtherScalar(1) && s==OtherScalar(0))
451 apply_rotation_in_the_plane_selector<
453 VectorX::SizeAtCompileTime,
454 EIGEN_PLAIN_ENUM_MIN(evaluator<VectorX>::Alignment, evaluator<VectorY>::Alignment),
455 Vectorizable>::run(x,incrx,y,incry,size,c,s);
462 #endif // EIGEN_JACOBI_H JacobiRotation operator*(const JacobiRotation &other)
Definition: Jacobi.h:51
void applyOnTheLeft(const EigenBase< OtherDerived > &other)
Definition: MatrixBase.h:522
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
JacobiRotation(const Scalar &c, const Scalar &s)
Definition: Jacobi.h:43
void applyOnTheRight(const EigenBase< OtherDerived > &other)
Definition: MatrixBase.h:510
Namespace containing all symbols from the Eigen library.
Definition: Core:306
Rotation given by a cosine-sine pair.
Definition: ForwardDeclarations.h:263
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:150
bool makeJacobi(const MatrixBase< Derived > &, Index p, Index q)
Definition: Jacobi.h:126
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
CoeffReturnType coeff(Index row, Index col) const
Definition: DenseCoeffsBase.h:96
const unsigned int PacketAccessBit
Definition: Constants.h:89
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
JacobiRotation transpose() const
Definition: Jacobi.h:59
const int Dynamic
Definition: Constants.h:21
JacobiRotation()
Definition: Jacobi.h:40
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
JacobiRotation adjoint() const
Definition: Jacobi.h:62
void makeGivens(const Scalar &p, const Scalar &q, Scalar *r=0)
Definition: Jacobi.h:148