11 #ifndef EIGEN_JACOBI_H
12 #define EIGEN_JACOBI_H
47 EIGEN_DEVICE_FUNC Scalar& c() {
return m_c; }
48 EIGEN_DEVICE_FUNC Scalar c()
const {
return m_c; }
49 EIGEN_DEVICE_FUNC Scalar& s() {
return m_s; }
50 EIGEN_DEVICE_FUNC Scalar s()
const {
return m_s; }
58 conj(m_c * conj(other.m_s) + conj(m_s) * conj(other.m_c)));
69 template<
typename Derived>
73 bool makeJacobi(
const RealScalar& x,
const Scalar& y,
const RealScalar& z);
76 void makeGivens(
const Scalar& p,
const Scalar& q, Scalar* r=0);
92 template<
typename Scalar>
99 RealScalar deno = RealScalar(2)*abs(y);
100 if(deno < (std::numeric_limits<RealScalar>::min)())
108 RealScalar tau = (x-z)/deno;
109 RealScalar w = sqrt(numext::abs2(tau) + RealScalar(1));
111 if(tau>RealScalar(0))
113 t = RealScalar(1) / (tau + w);
117 t = RealScalar(1) / (tau - w);
119 RealScalar sign_t = t > RealScalar(0) ? RealScalar(1) : RealScalar(-1);
120 RealScalar n = RealScalar(1) / sqrt(numext::abs2(t)+RealScalar(1));
121 m_s = - sign_t * (numext::conj(y) / abs(y)) * abs(t) * n;
136 template<
typename Scalar>
137 template<
typename Derived>
141 return makeJacobi(numext::real(m.coeff(p,p)), m.coeff(p,q), numext::real(m.coeff(q,q)));
160 template<
typename Scalar>
169 template<
typename Scalar>
179 m_c = numext::real(p)<0 ? Scalar(-1) : Scalar(1);
183 else if(p==Scalar(0))
191 RealScalar p1 = numext::norm1(p);
192 RealScalar q1 = numext::norm1(q);
196 RealScalar p2 = numext::abs2(ps);
198 RealScalar q2 = numext::abs2(qs);
200 RealScalar u = sqrt(RealScalar(1) + q2/p2);
201 if(numext::real(p)<RealScalar(0))
205 m_s = -qs*conj(ps)*(m_c/p2);
211 RealScalar p2 = numext::abs2(ps);
213 RealScalar q2 = numext::abs2(qs);
215 RealScalar u = q1 * sqrt(p2 + q2);
216 if(numext::real(p)<RealScalar(0))
222 m_s = -conj(ps) * (q/u);
229 template<
typename Scalar>
237 m_c = p<Scalar(0) ? Scalar(-1) : Scalar(1);
241 else if(p==Scalar(0))
244 m_s = q<Scalar(0) ? Scalar(1) : Scalar(-1);
247 else if(abs(p) > abs(q))
250 Scalar u = sqrt(Scalar(1) + numext::abs2(t));
260 Scalar u = sqrt(Scalar(1) + numext::abs2(t));
281 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
283 void apply_rotation_in_the_plane(DenseBase<VectorX>& xpr_x, DenseBase<VectorY>& xpr_y,
const JacobiRotation<OtherScalar>& j);
292 template<
typename Derived>
293 template<
typename OtherScalar>
297 RowXpr x(this->row(p));
298 RowXpr y(this->row(q));
299 internal::apply_rotation_in_the_plane(x, y, j);
308 template<
typename Derived>
309 template<
typename OtherScalar>
313 ColXpr x(this->col(p));
314 ColXpr y(this->col(q));
315 internal::apply_rotation_in_the_plane(x, y, j.
transpose());
320 template<
typename Scalar,
typename OtherScalar,
321 int SizeAtCompileTime,
int MinAlignment,
bool Vectorizable>
324 static EIGEN_DEVICE_FUNC
325 inline void run(Scalar *x,
Index incrx, Scalar *y,
Index incry,
Index size, OtherScalar c, OtherScalar s)
327 for(
Index i=0; i<size; ++i)
331 *x = c * xi + numext::conj(s) * yi;
332 *y = -s * xi + numext::conj(c) * yi;
339 template<
typename Scalar,
typename OtherScalar,
340 int SizeAtCompileTime,
int MinAlignment>
343 static inline void run(Scalar *x,
Index incrx, Scalar *y,
Index incry,
Index size, OtherScalar c, OtherScalar s)
349 typedef typename packet_traits<Scalar>::type
Packet;
350 typedef typename packet_traits<OtherScalar>::type OtherPacket;
353 if(SizeAtCompileTime ==
Dynamic && ((incrx==1 && incry==1) || PacketSize == 1))
356 enum { Peeling = 2 };
358 Index alignedStart = internal::first_default_aligned(y, size);
359 Index alignedEnd = alignedStart + ((size-alignedStart)/PacketSize)*PacketSize;
361 const OtherPacket pc = pset1<OtherPacket>(c);
362 const OtherPacket ps = pset1<OtherPacket>(s);
366 for(
Index i=0; i<alignedStart; ++i)
370 x[i] = c * xi + numext::conj(s) * yi;
371 y[i] = -s * xi + numext::conj(c) * yi;
374 Scalar* EIGEN_RESTRICT px = x + alignedStart;
375 Scalar* EIGEN_RESTRICT py = y + alignedStart;
377 if(internal::first_default_aligned(x, size)==alignedStart)
379 for(
Index i=alignedStart; i<alignedEnd; i+=PacketSize)
381 Packet xi = pload<Packet>(px);
382 Packet yi = pload<Packet>(py);
383 pstore(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
384 pstore(py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
391 Index peelingEnd = alignedStart + ((size-alignedStart)/(Peeling*PacketSize))*(Peeling*PacketSize);
392 for(
Index i=alignedStart; i<peelingEnd; i+=Peeling*PacketSize)
394 Packet xi = ploadu<Packet>(px);
395 Packet xi1 = ploadu<Packet>(px+PacketSize);
396 Packet yi = pload <Packet>(py);
397 Packet yi1 = pload <Packet>(py+PacketSize);
398 pstoreu(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
399 pstoreu(px+PacketSize, padd(pm.pmul(pc,xi1),pcj.pmul(ps,yi1)));
400 pstore (py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
401 pstore (py+PacketSize, psub(pcj.pmul(pc,yi1),pm.pmul(ps,xi1)));
402 px += Peeling*PacketSize;
403 py += Peeling*PacketSize;
405 if(alignedEnd!=peelingEnd)
407 Packet xi = ploadu<Packet>(x+peelingEnd);
408 Packet yi = pload <Packet>(y+peelingEnd);
409 pstoreu(x+peelingEnd, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
410 pstore (y+peelingEnd, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
414 for(
Index i=alignedEnd; i<size; ++i)
418 x[i] = c * xi + numext::conj(s) * yi;
419 y[i] = -s * xi + numext::conj(c) * yi;
424 else if(SizeAtCompileTime !=
Dynamic && MinAlignment>0)
426 const OtherPacket pc = pset1<OtherPacket>(c);
427 const OtherPacket ps = pset1<OtherPacket>(s);
430 Scalar* EIGEN_RESTRICT px = x;
431 Scalar* EIGEN_RESTRICT py = y;
432 for(
Index i=0; i<size; i+=PacketSize)
434 Packet xi = pload<Packet>(px);
435 Packet yi = pload<Packet>(py);
436 pstore(px, padd(pm.pmul(pc,xi),pcj.pmul(ps,yi)));
437 pstore(py, psub(pcj.pmul(pc,yi),pm.pmul(ps,xi)));
446 apply_rotation_in_the_plane_selector<Scalar,OtherScalar,SizeAtCompileTime,MinAlignment,false>::run(x,incrx,y,incry,size,c,s);
451 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
453 void apply_rotation_in_the_plane(DenseBase<VectorX>& xpr_x, DenseBase<VectorY>& xpr_y,
const JacobiRotation<OtherScalar>& j)
455 typedef typename VectorX::Scalar Scalar;
456 const bool Vectorizable = (VectorX::Flags & VectorY::Flags &
PacketAccessBit)
457 && (
int(packet_traits<Scalar>::size) == int(packet_traits<OtherScalar>::size));
459 eigen_assert(xpr_x.size() == xpr_y.size());
460 Index size = xpr_x.size();
461 Index incrx = xpr_x.derived().innerStride();
462 Index incry = xpr_y.derived().innerStride();
464 Scalar* EIGEN_RESTRICT x = &xpr_x.derived().coeffRef(0);
465 Scalar* EIGEN_RESTRICT y = &xpr_y.derived().coeffRef(0);
467 OtherScalar c = j.c();
468 OtherScalar s = j.s();
469 if (c==OtherScalar(1) && s==OtherScalar(0))
472 apply_rotation_in_the_plane_selector<
474 VectorX::SizeAtCompileTime,
475 EIGEN_PLAIN_ENUM_MIN(evaluator<VectorX>::Alignment, evaluator<VectorY>::Alignment),
476 Vectorizable>::run(x,incrx,y,incry,size,c,s);
483 #endif // EIGEN_JACOBI_H