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; }
55 conj(m_c * conj(other.m_s) + conj(m_s) * conj(other.m_c)));
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>
96 RealScalar tau = (x-z)/(RealScalar(2)*abs(y));
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);
231 else if(abs(p) > abs(q))
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());
300 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
303 typedef typename VectorX::Scalar Scalar;
304 enum { PacketSize = packet_traits<Scalar>::size };
305 typedef typename packet_traits<Scalar>::type Packet;
306 eigen_assert(xpr_x.size() == xpr_y.size());
307 Index size = xpr_x.size();
308 Index incrx = xpr_x.derived().innerStride();
309 Index incry = xpr_y.derived().innerStride();
311 Scalar* EIGEN_RESTRICT x = &xpr_x.derived().coeffRef(0);
312 Scalar* EIGEN_RESTRICT y = &xpr_y.derived().coeffRef(0);
314 OtherScalar c = j.c();
315 OtherScalar s = j.s();
316 if (c==OtherScalar(1) && s==OtherScalar(0))
321 if(VectorX::SizeAtCompileTime == Dynamic &&
323 ((incrx==1 && incry==1) || PacketSize == 1))
326 enum { Peeling = 2 };
328 Index alignedStart = internal::first_default_aligned(y, size);
329 Index alignedEnd = alignedStart + ((size-alignedStart)/PacketSize)*PacketSize;
331 const Packet pc = pset1<Packet>(c);
332 const Packet ps = pset1<Packet>(s);
333 conj_helper<Packet,Packet,NumTraits<Scalar>::IsComplex,
false> pcj;
335 for(Index i=0; i<alignedStart; ++i)
339 x[i] = c * xi + numext::conj(s) * yi;
340 y[i] = -s * xi + numext::conj(c) * yi;
343 Scalar* EIGEN_RESTRICT px = x + alignedStart;
344 Scalar* EIGEN_RESTRICT py = y + alignedStart;
346 if(internal::first_default_aligned(x, size)==alignedStart)
348 for(Index i=alignedStart; i<alignedEnd; i+=PacketSize)
350 Packet xi = pload<Packet>(px);
351 Packet yi = pload<Packet>(py);
352 pstore(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
353 pstore(py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
360 Index peelingEnd = alignedStart + ((size-alignedStart)/(Peeling*PacketSize))*(Peeling*PacketSize);
361 for(Index i=alignedStart; i<peelingEnd; i+=Peeling*PacketSize)
363 Packet xi = ploadu<Packet>(px);
364 Packet xi1 = ploadu<Packet>(px+PacketSize);
365 Packet yi = pload <Packet>(py);
366 Packet yi1 = pload <Packet>(py+PacketSize);
367 pstoreu(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
368 pstoreu(px+PacketSize, padd(pmul(pc,xi1),pcj.pmul(ps,yi1)));
369 pstore (py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
370 pstore (py+PacketSize, psub(pcj.pmul(pc,yi1),pmul(ps,xi1)));
371 px += Peeling*PacketSize;
372 py += Peeling*PacketSize;
374 if(alignedEnd!=peelingEnd)
376 Packet xi = ploadu<Packet>(x+peelingEnd);
377 Packet yi = pload <Packet>(y+peelingEnd);
378 pstoreu(x+peelingEnd, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
379 pstore (y+peelingEnd, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
383 for(Index i=alignedEnd; i<size; ++i)
387 x[i] = c * xi + numext::conj(s) * yi;
388 y[i] = -s * xi + numext::conj(c) * yi;
393 else if(VectorX::SizeAtCompileTime != Dynamic &&
394 (VectorX::Flags & VectorY::Flags & PacketAccessBit) &&
395 (EIGEN_PLAIN_ENUM_MIN(evaluator<VectorX>::Alignment, evaluator<VectorY>::Alignment)>0))
397 const Packet pc = pset1<Packet>(c);
398 const Packet ps = pset1<Packet>(s);
399 conj_helper<Packet,Packet,NumTraits<Scalar>::IsComplex,
false> pcj;
400 Scalar* EIGEN_RESTRICT px = x;
401 Scalar* EIGEN_RESTRICT py = y;
402 for(Index i=0; i<size; i+=PacketSize)
404 Packet xi = pload<Packet>(px);
405 Packet yi = pload<Packet>(py);
406 pstore(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
407 pstore(py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
416 for(Index i=0; i<size; ++i)
420 *x = c * xi + numext::conj(s) * yi;
421 *y = -s * xi + numext::conj(c) * yi;
432 #endif // EIGEN_JACOBI_H
JacobiRotation operator*(const JacobiRotation &other)
Definition: Jacobi.h:51
void applyOnTheLeft(const EigenBase< OtherDerived > &other)
Definition: MatrixBase.h:520
void makeGivens(const Scalar &p, const Scalar &q, Scalar *z=0)
Definition: Jacobi.h:148
JacobiRotation(const Scalar &c, const Scalar &s)
Definition: Jacobi.h:43
void applyOnTheRight(const EigenBase< OtherDerived > &other)
Definition: MatrixBase.h:508
Rotation given by a cosine-sine pair.
Definition: ForwardDeclarations.h:260
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:107
bool makeJacobi(const MatrixBase< Derived > &, Index p, Index q)
Definition: Jacobi.h:126
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
JacobiRotation transpose() const
Definition: Jacobi.h:59
const unsigned int PacketAccessBit
Definition: Constants.h:88
JacobiRotation adjoint() const
Definition: Jacobi.h:62
Definition: Eigen_Colamd.h:54
JacobiRotation()
Definition: Jacobi.h:40
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48