Basix 0.4.0

Home     Installation     Demos     C++ docs     Python docs

math.h
1 // Copyright (C) 2021 Igor Baratta
2 //
3 // This file is part of DOLFINx (https://www.fenicsproject.org)
4 //
5 // SPDX-License-Identifier: LGPL-3.0-or-later
6 
7 #pragma once
8 
9 #include <xtensor/xarray.hpp>
10 #include <xtensor/xfixed.hpp>
11 #include <xtensor/xtensor.hpp>
12 
14 
17 namespace basix::math
18 {
19 
24 template <typename U, typename V>
25 xt::xtensor<typename U::value_type, 2> outer(const U& u, const V& v)
26 {
27  xt::xtensor<typename U::value_type, 2> results({u.size(), v.size()});
28  for (std::size_t i = 0; i < u.size(); i++)
29  for (std::size_t j = 0; j < u.size(); j++)
30  results(i, j) = u(i) * v(j);
31 
32  return results;
33 }
34 
39 template <typename U, typename V>
40 xt::xtensor_fixed<typename U::value_type, xt::xshape<3>> cross(const U& u,
41  const V& v)
42 {
43  assert(u.size() == 3);
44  assert(v.size() == 3);
45  return {u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2],
46  u[0] * v[1] - u[1] * v[0]};
47 }
48 
53 template <typename U, typename V>
54 xt::xtensor<typename U::value_type, 2> dot(const U& A, const V& B)
55 {
56  xt::xtensor<typename U::value_type, 2> C
57  = xt::zeros<typename U::value_type>({A.shape(0), B.shape(1)});
58 
59  assert(A.shape(1) == B.shape(0));
60  for (std::size_t i = 0; i < A.shape(0); i++)
61  for (std::size_t j = 0; j < B.shape(1); j++)
62  for (std::size_t k = 0; k < A.shape(1); k++)
63  C(i, j) += A(i, k) * B(k, j);
64 
65  return C;
66 }
67 
71 std::pair<xt::xtensor<double, 1>,
72  xt::xtensor<double, 2, xt::layout_type::column_major>>
73 eigh(const xt::xtensor<double, 2>& A);
74 
79 xt::xarray<double, xt::layout_type::column_major>
80 solve(const xt::xtensor<double, 2>& A, const xt::xarray<double>& B);
81 
85 bool is_singular(const xt::xtensor<double, 2>& A);
86 
87 } // namespace basix::math
basix::math::eigh
std::pair< xt::xtensor< double, 1 >, xt::xtensor< double, 2, xt::layout_type::column_major > > eigh(const xt::xtensor< double, 2 > &A)
Definition: math.cpp:28
basix::math
Mathematical functions.
Definition: math.h:17
basix::math::solve
xt::xarray< double, xt::layout_type::column_major > solve(const xt::xtensor< double, 2 > &A, const xt::xarray< double > &B)
Definition: math.cpp:65
basix::math::is_singular
bool is_singular(const xt::xtensor< double, 2 > &A)
Definition: math.cpp:89
basix::math::dot
xt::xtensor< typename U::value_type, 2 > dot(const U &A, const V &B)
Definition: math.h:54
basix::math::outer
xt::xtensor< typename U::value_type, 2 > outer(const U &u, const V &v)
Definition: math.h:25
basix::math::cross
xt::xtensor_fixed< typename U::value_type, xt::xshape< 3 > > cross(const U &u, const V &v)
Definition: math.h:40