10 #include <basix/mdspan.hpp>
13 #include <type_traits>
15 namespace dolfinx::math
22 template <
typename U,
typename V>
23 std::array<typename U::value_type, 3> cross(
const U& u,
const V& v)
25 assert(u.size() == 3);
26 assert(v.size() == 3);
27 return {u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2],
28 u[0] * v[1] - u[1] * v[0]};
34 T difference_of_products(T a, T b, T c, T d) noexcept
37 T err = std::fma(-b, c, w);
38 T diff = std::fma(a, d, -w);
49 auto det(
const T* A, std::array<std::size_t, 2> shape)
51 assert(shape[0] == shape[1]);
60 return difference_of_products(A[0], A[1], A[2], A[3]);
65 T w0 = difference_of_products(A[3 + 1], A[3 + 2], A[3 * 2 + 1],
67 T w1 = difference_of_products(A[3], A[3 + 2], A[3 * 2], A[3 * 2 + 2]);
68 T w2 = difference_of_products(A[3], A[3 + 1], A[3 * 2], A[3 * 2 + 1]);
69 T w3 = difference_of_products(A[0], A[1], w1, w0);
70 T w4 = std::fma(A[2], w2, w3);
74 throw std::runtime_error(
"math::det is not implemented for "
75 + std::to_string(A[0]) +
"x" + std::to_string(A[1])
84 template <
typename Matrix>
87 static_assert(Matrix::rank() == 2,
"Must be rank 2");
88 assert(A.extent(0) == A.extent(1));
90 using value_type =
typename Matrix::value_type;
91 const int nrows = A.extent(0);
97 return difference_of_products(A(0, 0), A(0, 1), A(1, 0), A(1, 1));
102 value_type w0 = difference_of_products(A(1, 1), A(1, 2), A(2, 1), A(2, 2));
103 value_type w1 = difference_of_products(A(1, 0), A(1, 2), A(2, 0), A(2, 2));
104 value_type w2 = difference_of_products(A(1, 0), A(1, 1), A(2, 0), A(2, 1));
105 value_type w3 = difference_of_products(A(0, 0), A(0, 1), w1, w0);
106 value_type w4 = std::fma(A(0, 2), w2, w3);
110 throw std::runtime_error(
"math::det is not implemented for "
111 + std::to_string(A.extent(0)) +
"x"
112 + std::to_string(A.extent(1)) +
" matrices.");
122 template <
typename U,
typename V>
125 static_assert(U::rank() == 2,
"Must be rank 2");
126 static_assert(V::rank() == 2,
"Must be rank 2");
128 using value_type =
typename U::value_type;
129 const std::size_t nrows = A.extent(0);
133 B(0, 0) = 1 / A(0, 0);
137 value_type idet = 1. / det(A);
138 B(0, 0) = idet * A(1, 1);
139 B(0, 1) = -idet * A(0, 1);
140 B(1, 0) = -idet * A(1, 0);
141 B(1, 1) = idet * A(0, 0);
146 value_type w0 = difference_of_products(A(1, 1), A(1, 2), A(2, 1), A(2, 2));
147 value_type w1 = difference_of_products(A(1, 0), A(1, 2), A(2, 0), A(2, 2));
148 value_type w2 = difference_of_products(A(1, 0), A(1, 1), A(2, 0), A(2, 1));
149 value_type w3 = difference_of_products(A(0, 0), A(0, 1), w1, w0);
150 value_type det = std::fma(A(0, 2), w2, w3);
152 value_type idet = 1 / det;
155 B(1, 0) = -w1 * idet;
157 B(0, 1) = difference_of_products(A(0, 2), A(0, 1), A(2, 2), A(2, 1)) * idet;
158 B(0, 2) = difference_of_products(A(0, 1), A(0, 2), A(1, 1), A(1, 2)) * idet;
159 B(1, 1) = difference_of_products(A(0, 0), A(0, 2), A(2, 0), A(2, 2)) * idet;
160 B(1, 2) = difference_of_products(A(1, 0), A(0, 0), A(1, 2), A(0, 2)) * idet;
161 B(2, 1) = difference_of_products(A(2, 0), A(0, 0), A(2, 1), A(0, 1)) * idet;
162 B(2, 2) = difference_of_products(A(0, 0), A(1, 0), A(0, 1), A(1, 1)) * idet;
166 throw std::runtime_error(
"math::inv is not implemented for "
167 + std::to_string(A.extent(0)) +
"x"
168 + std::to_string(A.extent(1)) +
" matrices.");
178 template <
typename U,
typename V,
typename P>
179 void dot(U A, V B, P C,
bool transpose =
false)
181 static_assert(U::rank() == 2,
"Must be rank 2");
182 static_assert(V::rank() == 2,
"Must be rank 2");
183 static_assert(P::rank() == 2,
"Must be rank 2");
187 assert(A.extent(0) == B.extent(1));
188 for (std::size_t i = 0; i < A.extent(1); i++)
189 for (std::size_t j = 0; j < B.extent(0); j++)
190 for (std::size_t k = 0; k < A.extent(0); k++)
191 C(i, j) += A(k, i) * B(j, k);
195 assert(A.extent(1) == B.extent(0));
196 for (std::size_t i = 0; i < A.extent(0); i++)
197 for (std::size_t j = 0; j < B.extent(1); j++)
198 for (std::size_t k = 0; k < A.extent(1); k++)
199 C(i, j) += A(i, k) * B(k, j);
209 template <
typename U,
typename V>
212 static_assert(U::rank() == 2,
"Must be rank 2");
213 static_assert(V::rank() == 2,
"Must be rank 2");
215 assert(A.extent(0) > A.extent(1));
216 assert(P.extent(1) == A.extent(0));
217 assert(P.extent(0) == A.extent(1));
218 using T =
typename U::value_type;
219 if (A.extent(1) == 2)
221 namespace stdex = std::experimental;
222 std::array<T, 6> ATb;
223 std::array<T, 4> ATAb, Invb;
224 stdex::mdspan<T, stdex::extents<std::size_t, 2, 3>> AT(ATb.data(), 2, 3);
225 stdex::mdspan<T, stdex::extents<std::size_t, 2, 2>> ATA(ATAb.data(), 2, 2);
226 stdex::mdspan<T, stdex::extents<std::size_t, 2, 2>> Inv(Invb.data(), 2, 2);
228 for (std::size_t i = 0; i < AT.extent(0); ++i)
229 for (std::size_t j = 0; j < AT.extent(1); ++j)
232 std::fill(ATAb.begin(), ATAb.end(), 0.0);
233 for (std::size_t i = 0; i < P.extent(0); ++i)
234 for (std::size_t j = 0; j < P.extent(1); ++j)
238 dolfinx::math::dot(AT, A, ATA);
239 dolfinx::math::inv(ATA, Inv);
240 dolfinx::math::dot(Inv, AT, P);
242 else if (A.extent(1) == 1)
245 for (std::size_t i = 0; i < A.extent(0); ++i)
246 for (std::size_t j = 0; j < A.extent(1); ++j)
247 res += A(i, j) * A(i, j);
249 for (std::size_t i = 0; i < A.extent(0); ++i)
250 for (std::size_t j = 0; j < A.extent(1); ++j)
251 P(j, i) = (1 / res) * A(i, j);
255 throw std::runtime_error(
"math::pinv is not implemented for "
256 + std::to_string(A.extent(0)) +
"x"
257 + std::to_string(A.extent(1)) +
" matrices.");