MakeJacobi computes a rotation matrix G = [[c, s], [-s, c]], such that G_T * [[ps, pqs], [pqs, qs]] * G is diagonalized. def make_jacobi(ps, qs, pqs, eps): if np.abs(a_pq) > eps: tau = (a_qq - a_pp) / (2 * a_pq) if tau >= 0: t = 1.0 / (tau + np.sqrt(1 + tau ** 2)) else: t = -1.0 / (-tau + np.sqrt(1 + tau ** 2)) c = 1.0 / np.sqrt(1.0 + t ** 2) s = t * c else: c = 1.0 s = 0.0 return c, s
| 357 | // return c, s |
| 358 | // |
| 359 | StatusOr<JacobiRotation> MakeJacobi(XlaOp ps, XlaOp qs, XlaOp pqs, XlaOp eps) { |
| 360 | auto zero = ScalarLike(ps, 0.0); |
| 361 | auto one = ScalarLike(ps, 1.0); |
| 362 | auto two = ScalarLike(ps, 2.0); |
| 363 | |
| 364 | auto tau = (qs - ps) / (pqs * two); |
| 365 | auto t_pos = one / (tau + Sqrt(one + Square(tau))); |
| 366 | auto t_neg = -one / (-tau + Sqrt(one + Square(tau))); |
| 367 | auto t = Select(Ge(tau, zero), t_pos, t_neg); |
| 368 | |
| 369 | auto c_temp = Rsqrt(one + Square(t)); |
| 370 | auto s_temp = t * c_temp; |
| 371 | |
| 372 | auto c = Select(Ge(Abs(pqs), eps), c_temp, ZerosLike(c_temp) + one); |
| 373 | auto s = Select(Ge(Abs(pqs), eps), s_temp, ZerosLike(s_temp)); |
| 374 | // Renormalize c and s to compensate for low precision arithmetic, this step |
| 375 | // is redundant if high precision float is used, like float64. |
| 376 | auto rnorm = Rsqrt(Square(c) + Square(s)); |
| 377 | |
| 378 | JacobiRotation rot; |
| 379 | |
| 380 | rot.c = c * rnorm; |
| 381 | rot.s = s * rnorm; |
| 382 | |
| 383 | return rot; |
| 384 | } |
| 385 | |
| 386 | // One sided Jacobi rotations. For a matrix, |
| 387 | // [a_pp, a_pq] |
no test coverage detected