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* z=0);
71 void makeGivens(
const Scalar& p,
const Scalar& q, Scalar* z, internal::true_type);
72 void makeGivens(
const Scalar& p,
const Scalar& q, Scalar* z, internal::false_type);
82 template<
typename Scalar>
88 RealScalar deno = RealScalar(2)*
abs(y);
89 if(deno < (std::numeric_limits<RealScalar>::min)())
97 RealScalar tau = (x-z)/deno;
98 RealScalar w =
sqrt(numext::abs2(tau) + RealScalar(1));
100 if(tau>RealScalar(0))
102 t = RealScalar(1) / (tau + w);
106 t = RealScalar(1) / (tau - w);
108 RealScalar sign_t = t > RealScalar(0) ? RealScalar(1) : RealScalar(-1);
109 RealScalar n = RealScalar(1) /
sqrt(numext::abs2(t)+RealScalar(1));
110 m_s = - sign_t * (numext::conj(y) /
abs(y)) *
abs(t) * n;
125 template<
typename Scalar>
126 template<
typename Derived>
148 template<
typename Scalar>
156 template<
typename Scalar>
165 m_c = numext::real(p)<0 ? Scalar(-1) : Scalar(1);
169 else if(p==Scalar(0))
177 RealScalar p1 = numext::norm1(p);
178 RealScalar q1 = numext::norm1(q);
182 RealScalar p2 = numext::abs2(ps);
184 RealScalar q2 = numext::abs2(qs);
186 RealScalar u =
sqrt(RealScalar(1) + q2/p2);
187 if(numext::real(p)<RealScalar(0))
191 m_s = -qs*
conj(ps)*(m_c/p2);
197 RealScalar p2 = numext::abs2(ps);
199 RealScalar q2 = numext::abs2(qs);
201 RealScalar u = q1 *
sqrt(p2 + q2);
202 if(numext::real(p)<RealScalar(0))
208 m_s = -
conj(ps) * (q/u);
215 template<
typename Scalar>
222 m_c = p<Scalar(0) ? Scalar(-1) : Scalar(1);
226 else if(p==Scalar(0))
229 m_s = q<Scalar(0) ? Scalar(1) : Scalar(-1);
235 Scalar u =
sqrt(Scalar(1) + numext::abs2(t));
245 Scalar u =
sqrt(Scalar(1) + numext::abs2(t));
266 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
276 template<
typename Derived>
277 template<
typename OtherScalar>
280 RowXpr x(this->row(p));
281 RowXpr y(this->row(q));
282 internal::apply_rotation_in_the_plane(x, y, j);
291 template<
typename Derived>
292 template<
typename OtherScalar>
295 ColXpr x(this->col(p));
296 ColXpr y(this->col(q));
297 internal::apply_rotation_in_the_plane(x, y, j.transpose());
301 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
304 typedef typename VectorX::Scalar
Scalar;
306 PacketSize = packet_traits<Scalar>::size,
307 OtherPacketSize = packet_traits<OtherScalar>::size
309 typedef typename packet_traits<Scalar>::type Packet;
310 typedef typename packet_traits<OtherScalar>::type OtherPacket;
311 eigen_assert(xpr_x.
size() == xpr_y.
size());
316 Scalar* EIGEN_RESTRICT x = &xpr_x.
derived().coeffRef(0);
317 Scalar* EIGEN_RESTRICT y = &xpr_y.
derived().coeffRef(0);
319 OtherScalar c = j.c();
320 OtherScalar s = j.s();
321 if (c==OtherScalar(1) && s==OtherScalar(0))
326 if(VectorX::SizeAtCompileTime ==
Dynamic &&
328 (PacketSize == OtherPacketSize) &&
329 ((incrx==1 && incry==1) || PacketSize == 1))
332 enum { Peeling = 2 };
334 Index alignedStart = internal::first_default_aligned(y, size);
335 Index alignedEnd = alignedStart + ((size-alignedStart)/PacketSize)*PacketSize;
337 const OtherPacket pc = pset1<OtherPacket>(c);
338 const OtherPacket ps = pset1<OtherPacket>(s);
339 conj_helper<OtherPacket,Packet,NumTraits<OtherScalar>::IsComplex,
false> pcj;
340 conj_helper<OtherPacket,Packet,false,false> pm;
342 for(
Index i=0; i<alignedStart; ++i)
346 x[i] = c * xi + numext::conj(s) * yi;
347 y[i] = -s * xi + numext::conj(c) * yi;
350 Scalar* EIGEN_RESTRICT px = x + alignedStart;
351 Scalar* EIGEN_RESTRICT py = y + alignedStart;
353 if(internal::first_default_aligned(x, size)==alignedStart)
355 for(
Index i=alignedStart; i<alignedEnd; i+=PacketSize)
357 Packet xi = pload<Packet>(px);
358 Packet yi = pload<Packet>(py);
359 pstore(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
360 pstore(py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
367 Index peelingEnd = alignedStart + ((size-alignedStart)/(Peeling*PacketSize))*(Peeling*PacketSize);
368 for(
Index i=alignedStart; i<peelingEnd; i+=Peeling*PacketSize)
370 Packet xi = ploadu<Packet>(px);
371 Packet xi1 = ploadu<Packet>(px+PacketSize);
372 Packet yi = pload <Packet>(py);
373 Packet yi1 = pload <Packet>(py+PacketSize);
374 pstoreu(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
375 pstoreu(px+PacketSize, padd(pm.pmul(pc,xi1),pcj.pmul(ps,yi1)));
376 pstore (py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
377 pstore (py+PacketSize, psub(pcj.pmul(pc,yi1),pm.pmul(ps,xi1)));
378 px += Peeling*PacketSize;
379 py += Peeling*PacketSize;
381 if(alignedEnd!=peelingEnd)
383 Packet xi = ploadu<Packet>(x+peelingEnd);
384 Packet yi = pload <Packet>(y+peelingEnd);
385 pstoreu(x+peelingEnd, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
386 pstore (y+peelingEnd, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
390 for(
Index i=alignedEnd; i<size; ++i)
394 x[i] = c * xi + numext::conj(s) * yi;
395 y[i] = -s * xi + numext::conj(c) * yi;
400 else if(VectorX::SizeAtCompileTime !=
Dynamic &&
401 (VectorX::Flags & VectorY::Flags & PacketAccessBit) &&
402 (PacketSize == OtherPacketSize) &&
403 (EIGEN_PLAIN_ENUM_MIN(evaluator<VectorX>::Alignment, evaluator<VectorY>::Alignment)>0))
405 const OtherPacket pc = pset1<OtherPacket>(c);
406 const OtherPacket ps = pset1<OtherPacket>(s);
407 conj_helper<OtherPacket,Packet,NumTraits<OtherPacket>::IsComplex,
false> pcj;
408 conj_helper<OtherPacket,Packet,false,false> pm;
409 Scalar* EIGEN_RESTRICT px = x;
410 Scalar* EIGEN_RESTRICT py = y;
411 for(
Index i=0; i<size; i+=PacketSize)
413 Packet xi = pload<Packet>(px);
414 Packet yi = pload<Packet>(py);
415 pstore(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
416 pstore(py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
425 for(
Index i=0; i<size; ++i)
429 *x = c * xi + numext::conj(s) * yi;
430 *y = -s * xi + numext::conj(c) * yi;
441 #endif // EIGEN_JACOBI_H internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
JacobiRotation operator*(const JacobiRotation &other)
Definition: Jacobi.h:51
void applyOnTheLeft(const EigenBase< OtherDerived > &other)
Definition: MatrixBase.h:523
void makeGivens(const Scalar &p, const Scalar &q, Scalar *z=0)
Definition: Jacobi.h:149
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:511
Namespace containing all symbols from the Eigen library.
Definition: Core:287
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:127
Derived & derived()
Definition: EigenBase.h:45
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
CoeffReturnType coeff(Index row, Index col) const
Definition: DenseCoeffsBase.h:96
const unsigned int PacketAccessBit
Definition: Constants.h:89
Index size() const
Definition: EigenBase.h:66
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)
Definition: Eigen_Colamd.h:50
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