forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixUtils.hh
180 lines (161 loc) · 5.89 KB
/
MatrixUtils.hh
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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file orange/MatrixUtils.hh
// TODO: split into BLAS and host-only utils
//---------------------------------------------------------------------------//
#pragma once
#include <cmath>
#include "corecel/Macros.hh"
#include "corecel/cont/Array.hh"
#include "corecel/math/Algorithms.hh"
#include "corecel/math/Turn.hh"
#include "geocel/Types.hh"
namespace celeritas
{
namespace matrix
{
//---------------------------------------------------------------------------//
//!@{
//! Policy tags
struct TransposePolicy
{
};
inline constexpr TransposePolicy transpose{};
//!@}
} // namespace matrix
//---------------------------------------------------------------------------//
// Apply a matrix to an array
template<class T, size_type N>
inline CELER_FUNCTION Array<T, N> gemv(T alpha,
SquareMatrix<T, N> const& a,
Array<T, N> const& x,
T beta,
Array<T, N> const& y);
//---------------------------------------------------------------------------//
// Apply the transpose of a matrix to an array
template<class T, size_type N>
inline CELER_FUNCTION Array<T, N> gemv(matrix::TransposePolicy,
T alpha,
SquareMatrix<T, N> const& a,
Array<T, N> const& x,
T beta,
Array<T, N> const& y);
//---------------------------------------------------------------------------//
//!@{
//! Apply a matrix or its transpose to an array, without scaling or addition
template<class T, size_type N>
inline CELER_FUNCTION Array<T, N>
gemv(SquareMatrix<T, N> const& a, Array<T, N> const& x)
{
return gemv(T{1}, a, x, T{0}, x);
}
template<class T, size_type N>
inline CELER_FUNCTION Array<T, N>
gemv(matrix::TransposePolicy, SquareMatrix<T, N> const& a, Array<T, N> const& x)
{
return gemv(matrix::transpose, T{1}, a, x, T{0}, x);
}
//!@}
//---------------------------------------------------------------------------//
// Host-only declarations
// (double and float (and some int) for N=3 are instantiated in MatrixUtils.cc)
//---------------------------------------------------------------------------//
// Calculate the determinant of a matrix
template<class T>
T determinant(SquareMatrix<T, 3> const& mat);
// Calculate the trace of a matrix
template<class T>
T trace(SquareMatrix<T, 3> const& mat);
// Perform a matrix-matrix multiply
template<class T, size_type N>
SquareMatrix<T, N>
gemm(SquareMatrix<T, N> const& a, SquareMatrix<T, N> const& b);
// Perform a matrix-matrix multiply with A transposed
template<class T, size_type N>
SquareMatrix<T, N> gemm(matrix::TransposePolicy,
SquareMatrix<T, N> const& a,
SquareMatrix<T, N> const& b);
// Normalize and orthogonalize a small, dense matrix
template<class T, size_type N>
void orthonormalize(SquareMatrix<T, N>* mat);
// Create a C-ordered rotation matrix about an arbitrary axis
SquareMatrixReal3 make_rotation(Real3 const& ax, Turn rev);
// Create a C-ordered rotation matrix about a cartesian axis
SquareMatrixReal3 make_rotation(Axis ax, Turn rev);
// Apply a rotation to an existing C-ordered rotation matrix
SquareMatrixReal3 make_rotation(Axis ax, Turn rev, SquareMatrixReal3 const&);
// Construct a transposed matrix
SquareMatrixReal3 make_transpose(SquareMatrixReal3 const&);
//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Naive generalized matrix-vector multiply.
*
* \f[
* z \gets \alpha A x + \beta y
* \f]
*
* This should be equivalent to BLAS' GEMV without transposition. All
* matrix orderings are C-style: mat[i][j] is for row i, column j .
*
* \warning This implementation is limited and slow.
*/
template<class T, size_type N>
CELER_FUNCTION Array<T, N> gemv(T alpha,
SquareMatrix<T, N> const& a,
Array<T, N> const& x,
T beta,
Array<T, N> const& y)
{
Array<T, N> result;
for (size_type i = 0; i != N; ++i)
{
result[i] = beta * y[i];
for (size_type j = 0; j != N; ++j)
{
result[i] = fma(alpha, a[i][j] * x[j], result[i]);
}
}
return result;
}
//---------------------------------------------------------------------------//
/*!
* Naive transposed generalized matrix-vector multiply.
*
* \f[
* z \gets \alpha A^T x + \beta y
* \f]
*
* This should be equivalent to BLAS' GEMV with the 't' option. All
* matrix orderings are C-style: mat[i][j] is for row i, column j .
*
* \warning This implementation is limited and slow.
*/
template<class T, size_type N>
CELER_FUNCTION Array<T, N> gemv(matrix::TransposePolicy,
T alpha,
SquareMatrix<T, N> const& a,
Array<T, N> const& x,
T beta,
Array<T, N> const& y)
{
Array<T, N> result;
for (size_type i = 0; i != N; ++i)
{
result[i] = beta * y[i];
}
for (size_type j = 0; j != N; ++j)
{
for (size_type i = 0; i != N; ++i)
{
result[i] = fma(alpha, a[j][i] * x[j], result[i]);
}
}
return result;
}
//---------------------------------------------------------------------------//
} // namespace celeritas