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(VectorX& _x, VectorY& _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::Index Index;
304 typedef typename VectorX::Scalar Scalar;
305 enum { PacketSize = packet_traits<Scalar>::size };
306 typedef typename packet_traits<Scalar>::type Packet;
307 eigen_assert(_x.size() == _y.size());
308 Index size = _x.size();
309 Index incrx = _x.innerStride();
310 Index incry = _y.innerStride();
312 Scalar* EIGEN_RESTRICT x = &_x.coeffRef(0);
313 Scalar* EIGEN_RESTRICT y = &_y.coeffRef(0);
315 OtherScalar c = j.c();
316 OtherScalar s = j.s();
317 if (c==OtherScalar(1) && s==OtherScalar(0))
322 if(VectorX::SizeAtCompileTime ==
Dynamic &&
324 ((incrx==1 && incry==1) || PacketSize == 1))
327 enum { Peeling = 2 };
329 Index alignedStart = internal::first_aligned(y, size);
330 Index alignedEnd = alignedStart + ((size-alignedStart)/PacketSize)*PacketSize;
332 const Packet pc = pset1<Packet>(c);
333 const Packet ps = pset1<Packet>(s);
334 conj_helper<Packet,Packet,NumTraits<Scalar>::IsComplex,
false> pcj;
336 for(Index i=0; i<alignedStart; ++i)
340 x[i] = c * xi + numext::conj(s) * yi;
341 y[i] = -s * xi + numext::conj(c) * yi;
344 Scalar* EIGEN_RESTRICT px = x + alignedStart;
345 Scalar* EIGEN_RESTRICT py = y + alignedStart;
347 if(internal::first_aligned(x, size)==alignedStart)
349 for(Index i=alignedStart; i<alignedEnd; i+=PacketSize)
351 Packet xi = pload<Packet>(px);
352 Packet yi = pload<Packet>(py);
353 pstore(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
354 pstore(py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
361 Index peelingEnd = alignedStart + ((size-alignedStart)/(Peeling*PacketSize))*(Peeling*PacketSize);
362 for(Index i=alignedStart; i<peelingEnd; i+=Peeling*PacketSize)
364 Packet xi = ploadu<Packet>(px);
365 Packet xi1 = ploadu<Packet>(px+PacketSize);
366 Packet yi = pload <Packet>(py);
367 Packet yi1 = pload <Packet>(py+PacketSize);
368 pstoreu(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
369 pstoreu(px+PacketSize, padd(pmul(pc,xi1),pcj.pmul(ps,yi1)));
370 pstore (py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
371 pstore (py+PacketSize, psub(pcj.pmul(pc,yi1),pmul(ps,xi1)));
372 px += Peeling*PacketSize;
373 py += Peeling*PacketSize;
375 if(alignedEnd!=peelingEnd)
377 Packet xi = ploadu<Packet>(x+peelingEnd);
378 Packet yi = pload <Packet>(y+peelingEnd);
379 pstoreu(x+peelingEnd, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
380 pstore (y+peelingEnd, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
384 for(Index i=alignedEnd; i<size; ++i)
388 x[i] = c * xi + numext::conj(s) * yi;
389 y[i] = -s * xi + numext::conj(c) * yi;
394 else if(VectorX::SizeAtCompileTime !=
Dynamic &&
395 (VectorX::Flags & VectorY::Flags & PacketAccessBit) &&
396 (VectorX::Flags & VectorY::Flags &
AlignedBit))
398 const Packet pc = pset1<Packet>(c);
399 const Packet ps = pset1<Packet>(s);
400 conj_helper<Packet,Packet,NumTraits<Scalar>::IsComplex,
false> pcj;
401 Scalar* EIGEN_RESTRICT px = x;
402 Scalar* EIGEN_RESTRICT py = y;
403 for(Index i=0; i<size; i+=PacketSize)
405 Packet xi = pload<Packet>(px);
406 Packet yi = pload<Packet>(py);
407 pstore(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
408 pstore(py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
417 for(Index i=0; i<size; ++i)
421 *x = c * xi + numext::conj(s) * yi;
422 *y = -s * xi + numext::conj(c) * yi;
433 #endif // EIGEN_JACOBI_H
JacobiRotation()
Definition: Jacobi.h:40
Rotation given by a cosine-sine pair.
Definition: ForwardDeclarations.h:228
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
const int Dynamic
Definition: Constants.h:21
const unsigned int PacketAccessBit
Definition: Constants.h:81
void applyOnTheLeft(const EigenBase< OtherDerived > &other)
Definition: MatrixBase.h:556
void applyOnTheRight(const EigenBase< OtherDerived > &other)
Definition: MatrixBase.h:544
JacobiRotation transpose() const
Definition: Jacobi.h:59
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
internal::traits< Derived >::Index Index
The type of indices.
Definition: DenseBase.h:60
JacobiRotation operator*(const JacobiRotation &other)
Definition: Jacobi.h:51
JacobiRotation adjoint() const
Definition: Jacobi.h:62
const unsigned int AlignedBit
Definition: Constants.h:147
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
bool makeJacobi(const MatrixBase< Derived > &, typename Derived::Index p, typename Derived::Index q)
Definition: Jacobi.h:126