units
Use physical dimensions at compile-time or run-time.
Classes | Typedefs | Functions
vnix::mv Namespace Reference

Classes

struct  mat
 Model of a matrix of quantities. More...
 
class  mref
 Reference to column or row in matrix. More...
 

Typedefs

template<typename T , size_t NR>
using col = mat< T, NR, 1 >
 Model of column of quantities. More...
 
template<typename T , size_t NC>
using row = mat< T, 1, NC >
 Model of row of quantities. More...
 

Functions

template<typename T , typename OT , size_t N>
constexpr T cnv_el (array< OT, N > const &a, size_t i)
 Convert element i of array a from type OT to type T. More...
 
template<typename T , typename OT , size_t... i>
constexpr auto _cnv (array< OT, sizeof...(i)> const &a, index_sequence< i... >)
 Convert array of elements of type OT to array of elements of type T. More...
 
template<typename T , typename OT , size_t N>
constexpr auto cnv_ar (array< OT, N > const &a)
 Convert array of elements of type OT to array of elements of type T. More...
 
template<typename T1 , typename T2 , size_t S1, size_t S2, size_t N>
constexpr auto dot (mref< T1, S1, N > const &mr1, mref< T2, S2, N > const &mr2)
 Dot-product of two mrefs. More...
 
template<typename T1 , typename T2 , size_t NR1, size_t NC2, size_t N>
constexpr auto operator* (mat< T1, NR1, N > const &m1, mat< T2, N, NC2 > const &m2)
 Multiply two matrices. More...
 
template<typename T1 , typename T2 , size_t NR2, size_t NC2>
constexpr auto operator* (T1 const &s1, mat< T2, NR2, NC2 > const &m2)
 Multiply matrix on left by scalar. More...
 
template<typename T , size_t NR, size_t NC>
ostream & operator<< (ostream &os, mat< T, NR, NC > const &m)
 Print matrix. More...
 

Typedef Documentation

template<typename T , size_t NR>
using vnix::mv::col = typedef mat<T, NR, 1>

Model of column of quantities.

Definition at line 124 of file example1.cpp.

template<typename T , size_t NC>
using vnix::mv::row = typedef mat<T, 1, NC>

Model of row of quantities.

Definition at line 127 of file example1.cpp.

Function Documentation

template<typename T , typename OT , size_t... i>
constexpr auto vnix::mv::_cnv ( array< OT, sizeof...(i)> const &  a,
index_sequence< i... >   
)

Convert array of elements of type OT to array of elements of type T.

Definition at line 31 of file example1.cpp.

31  {
32  return array<T, sizeof...(i)>{{cnv_el<T>(a, i)...}};
33 }
template<typename T , typename OT , size_t N>
constexpr auto vnix::mv::cnv_ar ( array< OT, N > const &  a)

Convert array of elements of type OT to array of elements of type T.

Definition at line 37 of file example1.cpp.

37  {
38  return _cnv<T>(a, make_index_sequence<N>{});
39 }
template<typename T , typename OT , size_t N>
constexpr T vnix::mv::cnv_el ( array< OT, N > const &  a,
size_t  i 
)

Convert element i of array a from type OT to type T.

Definition at line 25 of file example1.cpp.

25  {
26  return a[i];
27 }
template<typename T1 , typename T2 , size_t S1, size_t S2, size_t N>
constexpr auto vnix::mv::dot ( mref< T1, S1, N > const &  mr1,
mref< T2, S2, N > const &  mr2 
)

Dot-product of two mrefs.

Definition at line 77 of file example1.cpp.

77  {
78  auto sum = 0 * mr1(0) * mr2(0);
79  for (size_t i = 0; i < N; ++i) { sum += mr1(i) * mr2(i); }
80  return sum;
81 }
constexpr auto N
Constant-expression symbol for N.
Definition: units.hpp:2008
template<typename T1 , typename T2 , size_t NR1, size_t NC2, size_t N>
constexpr auto vnix::mv::operator* ( mat< T1, NR1, N > const &  m1,
mat< T2, N, NC2 > const &  m2 
)

Multiply two matrices.

Definition at line 131 of file example1.cpp.

132  {
133  using TT = decltype(dot(m1.row(0), m2.col(0)));
134  using TP = typename std::remove_const<TT>::type;
135  mat<TP, NR1, NC2> pr; // product
136  for (size_t i = 0; i < NR1; ++i) {
137  auto const m1_rowi = m1.row(i);
138  auto pr_rowi = pr.row(i);
139  for (size_t j = 0; j < NC2; ++j) { pr_rowi(j) = dot(m1_rowi, m2.col(j)); }
140  }
141  return pr;
142 }
constexpr auto dot(mref< T1, S1, N > const &mr1, mref< T2, S2, N > const &mr2)
Dot-product of two mrefs.
Definition: example1.cpp:77

Here is the caller graph for this function:

template<typename T1 , typename T2 , size_t NR2, size_t NC2>
constexpr auto vnix::mv::operator* ( T1 const &  s1,
mat< T2, NR2, NC2 > const &  m2 
)

Multiply matrix on left by scalar.

Definition at line 146 of file example1.cpp.

146  {
147  mat<decltype(s1 * m2.row(0)(0)), NR2, NC2> pr; // product
148  for (size_t i = 0; i < NR2; ++i) {
149  auto const m2_rowi = m2.row(i);
150  auto pr_rowi = pr.row(i);
151  for (size_t j = 0; j < NC2; ++j) { pr_rowi(j) = s1 * m2_rowi(j); }
152  }
153  return pr;
154 }
template<typename T , size_t NR, size_t NC>
ostream& vnix::mv::operator<< ( ostream &  os,
mat< T, NR, NC > const &  m 
)

Print matrix.

Definition at line 158 of file example1.cpp.

158  {
159  for (size_t i = 0; i < NR; ++i) {
160  auto const rowi = m.row(i);
161  os << endl;
162  for (size_t j = 0; j < NC; ++j) { os << rowi(j) << " "; }
163  }
164  return os;
165 }
constexpr auto m
Constant-expression symbol for m.
Definition: units.hpp:1675