3#ifndef pRC_ALGORITHMS_SVD_H
4#define pRC_ALGORITHMS_SVD_H
23 template<
class X,
class R = RemoveReference<X>, If<IsTensorish<R>> = 0,
24 If<IsFloat<
typename R::Value>> = 0,
25 If<IsSatisfied<(
typename R::Dimension() == 2)>> = 0>
28 using T =
typename R::Type;
29 constexpr auto M = R::size(0);
30 constexpr auto N = R::size(1);
31 constexpr auto D =
min(M, N);
53 else if constexpr(M > N)
58 else if constexpr(M < N)
73 auto const threshold = [](
auto const &
diag)
106 auto const z =
abs(a(
p,
q)) / a(
p,
q);
121 auto const z =
abs(a(
p,
q)) / a(
p,
q);
129 auto const z =
abs(a(
q,
q)) / a(
q,
q);
150 auto const t = m(0, 0) + m(1, 1);
151 auto const d = m(1, 0) - m(0, 1);
172 [&a, &
u, &s](
auto const i)
233 "SVD decomposition failed: Found negative singular "
241 template<Size C,
class X,
class R = RemoveReference<X>,
242 If<IsTensorish<R>> = 0,
class V =
typename R::Value,
class VT = V,
243 If<All<IsFloat<V>, IsFloat<VT>>> = 0,
244 If<IsSatisfied<(
typename R::Dimension() == 2)>> = 0,
245 If<IsSatisfied<(C < min(R::size(0), R::size(1)))>> = 0>
246 static inline constexpr auto svd(X &&input,
247 VT const &tolerance = NumericLimits<VT>::tolerance())
249 auto const [fU, fS, fV] = svd(forward<X>(input));
251 constexpr auto M = decltype(fU)::size(0);
252 constexpr auto D = decltype(fS)::size();
253 constexpr auto N = decltype(fV)::size(0);
255 auto const sNorm = norm<2, 1>(fS)();
256 auto sError = norm<2, 1>(slice<D - C>(fS, C))();
258 auto result = tuple(eval(slice<M, C>(fU, 0, 0)), eval(slice<C>(fS, 0)),
259 eval(slice<N, C>(fV, 0, 0)));
261 auto &u = get<0>(result);
262 auto &s = get<1>(result);
263 auto &v = get<2>(result);
294 template<Size C,
class X,
class R = RemoveReference<X>,
295 If<IsTensorish<R>> = 0,
class V =
typename R::Value,
class VT = V,
296 If<All<IsFloat<V>, IsFloat<VT>>> = 0,
297 If<IsSatisfied<(
typename R::Dimension() == 2)>> = 0,
298 If<IsSatisfied<(C == min(R::size(0), R::size(1)))>> = 0>
299 static inline constexpr auto svd(X &&input,
300 VT
const &tolerance = NumericLimits<VT>::tolerance())
302 auto result =
svd(forward<X>(input));
303 auto &u = get<0>(result);
304 auto &s = get<1>(result);
305 auto &v = get<2>(result);
307 auto const sNorm = norm<2, 1>(s)();
308 auto sError = zero<decltype(sNorm)>();
310 for(Index k = C; k != 0;)
312 if(sError + square(s(--k)) > square(tolerance) * sNorm)
317 sError += square(s(k));
320 chip<1>(u, k) = zero();
321 chip<1>(v, k) = zero();
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition jacobi_rotation.hpp:17
static constexpr auto MakeGivens(R1 const &x, R2 const &y)
Definition jacobi_rotation.hpp:33
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
static constexpr auto extractDiagonal(X &&a)
Definition extract_diagonal.hpp:17
static constexpr X eval(X &&a)
Definition eval.hpp:11
bool Bool
Definition type_traits.hpp:18
static constexpr decltype(auto) apply(JacobiRotation< T > const &r, X &&m, Index const p, Index const q)
Definition jacobi_rotation.hpp:334
static constexpr X min(X &&a)
Definition min.hpp:13
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
Size Index
Definition type_traits.hpp:21
static constexpr decltype(auto) imag(X &&a)
Definition imag.hpp:11
static constexpr decltype(auto) real(X &&a)
Definition real.hpp:11
static constexpr auto zero()
Definition zero.hpp:12
JacobiRotation(Tensor< T, M, N > const &a, Index const p, Index const q) -> JacobiRotation< T >
static constexpr auto qr(X &&input)
Definition qr.hpp:24
static constexpr auto transpose(JacobiRotation< T > const &a)
Definition jacobi_rotation.hpp:319
static constexpr auto adjoint(JacobiRotation< T > const &a)
Definition jacobi_rotation.hpp:325
static constexpr auto square(Complex< T > const &a)
Definition square.hpp:14
static constexpr auto isApprox(XA &&a, XB &&b, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_approx.hpp:24
static constexpr auto fromDiagonal(X &&a)
Definition from_diagonal.hpp:21
static constexpr auto abs(Complex< T > const &a)
Definition abs.hpp:12
constexpr auto cDebugLevel
Definition config.hpp:46
static constexpr auto sqrt(Complex< T > const &a)
Definition sqrt.hpp:12
static constexpr auto swap(XA &&a, XB &&b)
Definition swap.hpp:21
static constexpr auto svd(X &&input)
Definition svd.hpp:26
static constexpr auto conj(Complex< T > const &a)
Definition conj.hpp:11
static constexpr auto identity()
Definition identity.hpp:12
static constexpr auto isUnitary(X &&a, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_unitary.hpp:19
static constexpr X max(X &&a)
Definition max.hpp:13