sisi4s
Loading...
Searching...
No Matches
MathFunctions.hpp
Go to the documentation of this file.
1#ifndef MATH_FUNCTIONS_DEFINED
2#define MATH_FUNCTIONS_DEFINED
3
4#include <math/Complex.hpp>
5#include <cmath>
6#include <string>
7#include <util/Tensor.hpp>
8#include <util/Log.hpp>
9
10namespace sisi4s {
11// constants
12template <typename F = double>
13constexpr F Pi() {
14 return std::acos(F(-1));
15}
16
17template <typename F = double>
18constexpr F Tau() {
19 return 2 * Pi<F>();
20}
21
22// univariate functions
23template <typename F = double>
24inline F sqrt(F const x) {
25 return std::sqrt(x);
26}
27
28template <typename F = double>
29inline F abs(F const x) {
30 return std::abs(x);
31}
32
33template <typename F>
34inline F conj(F const x) {
35 return std::conj(x);
36}
37
38template <>
39inline double conj(double const x) {
40 return x;
41}
42
43// bivariate functions
44template <typename F = double>
45inline F dot(F const x, F const y) {
46 return conj(x) * y;
47}
48
52template <typename F = double>
53inline F realDot(F const x, F const y) {
54 return std::real(x * conj(y));
55}
56
57template <typename F = double>
58inline F divide(F const x, F const y) {
59 return x / y;
60}
61
62template <typename F = double>
63inline F multiply(F const x, F const y) {
64 return x * y;
65}
66
67template <typename F>
68inline double frobeniusNorm(Tensor<F> &t) {
69 char *indices(new char[t.order + 1]);
70 for (int index(0); index < t.order; ++index) indices[index] = 'a' + index;
71 indices[t.order] = 0;
72 CTF::Bivar_Function<F> fRealDot(&sisi4s::realDot<F>);
73 CTF::Scalar<F> s(*t.wrld);
74 s.contract(1.0, t, indices, t, indices, 0.0, "", fRealDot);
75 return std::sqrt(std::real(s.get_val()));
76}
77
84template <typename F>
85inline void symmetrize(std::string indices,
86 std::string permuted,
87 Tensor<F> &t,
88 F prefactor = 1) {
89 t[indices.c_str()] += prefactor * t[permuted.c_str()];
90}
91template <typename F>
93 Tensor<F> testResultUp(t);
94 Tensor<F> testResultDown(t);
95 F normValue;
96 testResultUp["abij"] += testResultUp["baij"];
97 testResultDown["abij"] += testResultDown["abji"];
98 normValue = testResultUp.norm1();
99 normValue += testResultDown.norm1();
100 if (normValue >= 1e-3) {
101 t.print();
102 LOG(0, "AntisymmetryCheck")
103 << t.get_name() << ": zero tensor norm " << normValue << std::endl;
104 exit(1);
105 }
106}
107} // namespace sisi4s
108
109#endif
#define LOG(...)
Definition Log.hpp:119
Definition Algorithm.hpp:10
void checkAntisymmetry(Tensor< F > &t)
Definition MathFunctions.hpp:92
CTF::Tensor< F > Tensor
Definition Tensor.hpp:9
void symmetrize(std::string indices, std::string permuted, Tensor< F > &t, F prefactor=1)
Apply a permutation operator and antisymmetrize accordingly, e.g. antiSymmetrize(X,...
Definition MathFunctions.hpp:85
F sqrt(F const x)
Definition MathFunctions.hpp:24
F divide(F const x, F const y)
Definition MathFunctions.hpp:58
constexpr F Pi()
Definition MathFunctions.hpp:13
F multiply(F const x, F const y)
Definition MathFunctions.hpp:63
F dot(F const x, F const y)
Definition MathFunctions.hpp:45
constexpr F Tau()
Definition MathFunctions.hpp:18
F realDot(F const x, F const y)
Calculates only the real part of x*conj(y).
Definition MathFunctions.hpp:53
double frobeniusNorm(Tensor< F > &t)
Definition MathFunctions.hpp:68
F abs(F const x)
Definition MathFunctions.hpp:29
F conj(F const x)
Definition MathFunctions.hpp:34