File: SpinMatrix.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (188 lines) | stat: -rw-r--r-- 3,526 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Base/Spin/SpinMatrix.cpp
//! @brief     Implements class SpinMatrix.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Base/Spin/SpinMatrix.h"
#include "Base/Spin/Spinor.h"

SpinMatrix::SpinMatrix(complex_t a_, complex_t b_, complex_t c_, complex_t d_)
    : a(a_)
    , b(b_)
    , c(c_)
    , d(d_)
{
}

SpinMatrix::SpinMatrix()
    : SpinMatrix(0, 0, 0, 0)
{
}

SpinMatrix SpinMatrix::Diag(complex_t a_, complex_t d_)
{
    return {a_, 0, 0, d_};
}

SpinMatrix SpinMatrix::One()
{
    return Diag(1, 1);
}

SpinMatrix SpinMatrix::Null()
{
    return Diag(0, 0);
}

SpinMatrix SpinMatrix::FromBlochVector(const R3& v)
{
    return {(1.0 + v.z()) / 2.0, complex_t(v.x(), -v.y()) / 2.0, complex_t(v.x(), v.y()) / 2.0,
            (1.0 - v.z()) / 2.0};
}

SpinMatrix SpinMatrix::operator-() const
{
    return {-a, -b, -c, -d};
}

SpinMatrix SpinMatrix::operator+(const SpinMatrix& o) const
{
    return {a + o.a, b + o.b, c + o.c, d + o.d};
}

SpinMatrix SpinMatrix::operator-(const SpinMatrix& o) const
{
    return {a - o.a, b - o.b, c - o.c, d - o.d};
}

SpinMatrix SpinMatrix::operator+=(const SpinMatrix& o)
{
    a += o.a;
    b += o.b;
    c += o.c;
    d += o.d;

    return *this;
}

SpinMatrix SpinMatrix::operator*(const SpinMatrix& o) const
{
    return {a * o.a + b * o.c, a * o.b + b * o.d, c * o.a + d * o.c, c * o.b + d * o.d};
}

SpinMatrix SpinMatrix::operator*=(const SpinMatrix& o)
{
    const SpinMatrix tmp(*this * o);
    *this = tmp;
    return *this;
}

Spinor SpinMatrix::operator*(const Spinor& s) const
{
    return {a * s.u + b * s.v, c * s.u + d * s.v};
}

SpinMatrix SpinMatrix::operator*(complex_t f) const
{
    return {a * f, b * f, c * f, d * f};
}

SpinMatrix SpinMatrix::operator*(double f) const
{
    return {a * f, b * f, c * f, d * f};
}

SpinMatrix SpinMatrix::operator/(complex_t f) const
{
    return {a / f, b / f, c / f, d / f};
}

SpinMatrix SpinMatrix::operator/(double f) const
{
    return {a / f, b / f, c / f, d / f};
}

SpinMatrix SpinMatrix::operator*=(complex_t f)
{
    a *= f;
    b *= f;
    c *= f;
    d *= f;
    return *this;
}

SpinMatrix SpinMatrix::operator*=(double f)
{
    a *= f;
    b *= f;
    c *= f;
    d *= f;
    return *this;
}

SpinMatrix SpinMatrix::operator/=(complex_t f)
{
    a /= f;
    b /= f;
    c /= f;
    d /= f;
    return *this;
}

SpinMatrix SpinMatrix::operator/=(double f)
{
    a /= f;
    b /= f;
    c /= f;
    d /= f;
    return *this;
}

SpinMatrix operator*(complex_t f, const SpinMatrix& m)
{
    return m * f;
}

SpinMatrix operator*(double f, const SpinMatrix& m)
{
    return m * f;
}

Spinor SpinMatrix::col0() const
{
    return {a, c};
}

Spinor SpinMatrix::col1() const
{
    return {b, d};
}

complex_t SpinMatrix::trace() const
{
    return a + d;
}

complex_t SpinMatrix::determinant() const
{
    return a * d - b * c;
}

bool SpinMatrix::allFinite() const
{
    return isfinite(a) && isfinite(b) && isfinite(c) && isfinite(d);
}

SpinMatrix SpinMatrix::adjoint() const
{
    return {conj(a), conj(c), conj(b), conj(d)};
}