cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
norm.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_TENSOR_TRAIN_COMMON_FUNCTIONS_NORM_H
4#define pRC_TENSOR_TRAIN_COMMON_FUNCTIONS_NORM_H
5
10
11namespace pRC
12{
13 template<Index P = 2, Index Q = P, class X, class R = RemoveReference<X>>
14 requires(TensorTrain::IsTensorish<R> ||
15 TensorTrain::IsOperatorish<R>) &&
16 (P == 2 && Q == 2)
17 static inline constexpr auto norm(X &&a)
18 {
19 using T = NonComplex<Value<R>>;
20 T lambda = zero();
21
22 return RecursiveLambda(
23 [&a, &lambda]<Index I = 0>(auto const &self, auto const &p) {
24 auto const f = [&p, &a, &lambda]()
25 {
26 Tensor const tmp = contract<1, 0>(p, a.template core<I>());
27 auto [q, r] = orthogonalize<Position::Left>(tmp);
28
29 auto const rNorm = norm(r)();
30 if(rNorm > NumericLimits<T>::min())
31 {
32 lambda += log(rNorm);
33 r /= rNorm;
34 }
35
36 return r;
37 };
38
39 if constexpr(I == R::Dimension - 1)
40 {
41 return exp(lambda) * reshape<>(f());
42 }
43 else
44 {
45 return self.template operator()<I + 1>(f());
46 }
48 }
49}
50#endif // pRC_TENSOR_TRAIN_COMMON_FUNCTIONS_NORM_H
Definition value.hpp:12
Definition tensor.hpp:25
const char * p
Definition gmock-matchers-containers_test.cc:379
Definition cholesky.hpp:10
static constexpr auto contract(X &&a)
Definition contract.hpp:20
Size Index
Definition basics.hpp:32
RecursiveLambda(X &&) -> RecursiveLambda< RemoveReference< X > >
static constexpr auto reshape(X &&a)
Definition reshape.hpp:14
static constexpr auto orthogonalize(X &&a)
Definition orthogonalize.hpp:13
RemoveConstReference< ResultOf< Eval, ResultOf< Real, T > > > NonComplex
Definition complex.hpp:201
static constexpr auto identity()
Definition identity.hpp:13
static constexpr auto zero()
Definition zero.hpp:12
static constexpr auto log(T const &a)
Definition log.hpp:11
static constexpr auto exp(T const &a)
Definition exp.hpp:11
static constexpr auto norm(T const &a)
Definition norm.hpp:12
Definition limits.hpp:13