NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
tmatrix.ixx
Go to the documentation of this file.
1 #include "tsolver-lapacklu.hxx"
7 namespace numcxx
8 {
9 
10 
11  extern "C"
12  {
13  void dgemm_(char *TRANSA, char *TRANSB, int *M, int * N, int *K, double *ALPHA, const double *A, int *LDA, double *B, int *LDB, double *BETA,double * C, int *LDC);
14  void sgemm_(char *TRANSA, char *TRANSB, int *M, int * N, int *K, float *ALPHA, const float *A, int *LDA, float *B, int *LDB, float *BETA,float * C, int *LDC);
15  }
16 
17  template <>
18  inline void TMatrix<double>::apply(const TArray<double> &u, TArray<double> &v) const
19  {
20  v.resize(u.size());
21  char transmat[2]={'T','\0'};
22  char transvec[2]={'N','\0'};
23  int n=shape(0);
24  int ione=1;
25  double done=1.0;
26  double dzero=0.0;
27  dgemm_(transmat,
28  transvec,
29  &n,&ione,&n,
30  &done,
31  _data,&n,
32  u.data(),&n,
33  &dzero,
34  v.data(),&n);
35  }
36 
37 
38  template <>
39  inline void TMatrix<float>::apply(const TArray<float> &u, TArray<float> &v) const
40  {
41  v.resize(u.size());
42  char transmat[2]={'T','\0'};
43  char transvec[2]={'N','\0'};
44  int n=shape(0);
45  int ione=1;
46  float done=1.0;
47  float dzero=0.0;
48  sgemm_(transmat,
49  transvec,
50  &n,&ione,&n,
51  &done,
52  _data,&n,
53  u.data(),&n,
54  &dzero,
55  v.data(),&n);
56  }
57 
58 
59  template <typename T>
60  inline void TMatrix<T>::apply(const TArray<T> &u, TArray<T> &v) const
61  {
62  for (index i=0;i<shape(1);i++)
63  { v[i]=0.0;
64  for (index j=0;j<shape(0);j++)
65  v[i]+=_data[_idx(i,j)]*u[j];
66  }
67  }
68 
69  template <typename T>
70  inline std::shared_ptr<TMatrix<T>> TMatrix<T>::calculate_inverse()
71  {
72  auto pA=copy(); // essentially superfluous, but we seldomly use this
73  auto pLU=TSolverLapackLU<T>::create(pA);
74  return pLU->calculate_inverse();
75  }
76 
77 
78 }
79 
void sgemm_(char *TRANSA, char *TRANSB, int *M, int *N, int *K, float *ALPHA, const float *A, int *LDA, float *B, int *LDB, float *BETA, float *C, int *LDC)
static std::shared_ptr< TSolverLapackLU< T > > create(const std::shared_ptr< TMatrix< T >> pMatrix)
Static wrapper around constructor.
TArray is the common template base class for arrays and dense matrices of the numcxx project...
Definition: tarray.hxx:17
size_t size() const
Obtain size of array.
Definition: tarray.hxx:58
Header for numcxx::TSolverLapackLU.
unsigned int index
Definition: numcxx.hxx:21
void dgemm_(char *TRANSA, char *TRANSB, int *M, int *N, int *K, double *ALPHA, const double *A, int *LDA, double *B, int *LDB, double *BETA, double *C, int *LDC)
std::shared_ptr< TMatrix< T > > calculate_inverse()
Calculate inverse of matrix.
Definition: tmatrix.ixx:70
void resize(size_t n)
Resize array.
Definition: tarray.ixx:282
double B(double x)
Numcxx template library.
Definition: expression.ixx:41
void apply(const TArray< T > &u, TArray< T > &v) const
Apply matrix to vector.
Definition: tmatrix.ixx:60
T * data() const
Obtain C-pointer of data array.
Definition: tarray.hxx:128