DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlITKSpams.h
Go to the documentation of this file.
1 
18 #ifndef __utlITKSpams_h
19 #define __utlITKSpams_h
20 
21 
22 
23 #include "linalg.h"
24 #include <vnl/vnl_matrix.h>
25 #include <vnl/vnl_vector.h>
26 
27 namespace spams
28 {
29 
30 template <class T>
31 void
32 VnlMatrixToMatrix ( const vnl_matrix<T>& matVnl, Matrix<T>& matSpams )
33 {
34  matSpams.resize(matVnl.rows(), matVnl.columns());
35  // for ( int i = 0; i < matVnl.rows(); i += 1 )
36  // for ( int j = 0; j < matVnl.columns(); j += 1 )
37  // matSpams(i,j) = matVnl(i,j);
38  T const* matVnl_data = matVnl.data_block();
39  T* matSpams_data = matSpams.rawX();
40  if (matVnl.rows() >= matVnl.columns())
41  {
42  for ( int j = 0; j < matVnl.columns(); j += 1 )
43  cblas_copy<T>(matVnl.rows(), (T*)matVnl_data+j, matVnl.columns(), matSpams_data+j*matVnl.rows(),1);
44  }
45  else
46  {
47  for ( int i = 0; i < matVnl.rows(); i += 1 )
48  cblas_copy<T>(matVnl.columns(), (T*)matVnl_data+i*matVnl.columns(), 1, matSpams_data+i,matVnl.rows());
49  }
50 }
51 
52 template <class T>
53 void
54 MatrixToVnlMatrix ( const Matrix<T>& matSpams, vnl_matrix<T>& matVnl )
55 {
56  matVnl.set_size(matSpams.m(), matSpams.n());
57  // for ( int i = 0; i < mat.m(); i += 1 )
58  // for ( int j = 0; j < mat.n(); j += 1 )
59  // matVnl(i,j) = matSpams(i,j);
60  T* matVnl_data = matVnl.data_block();
61  const T * matSpams_data = matSpams.X();
62  if (matVnl.rows() >= matVnl.columns())
63  {
64  for ( int j = 0; j < matVnl.columns(); j += 1 )
65  cblas_copy<T>(matVnl.rows(), (T*)matSpams_data+j*matVnl.rows(),1, matVnl_data+j, matVnl.columns());
66  }
67  else
68  {
69  for ( int i = 0; i < matVnl.rows(); i += 1 )
70  cblas_copy<T>(matVnl.columns(), (T*)matSpams_data+i,matVnl.rows(), matVnl_data+i*matVnl.columns(), 1);
71  }
72 }
73 
74 template <class T>
75 void
76 VnlVectorToVector ( const vnl_vector<T>& v, Vector<T>& vec )
77 {
78  vec.resize(v.size());
79  // for ( int i = 0; i < v.size(); i += 1 )
80  // vec[i] = v[i];
81  cblas_copy<T>(v.size(), v.data_block(),1, vec.rawX(), 1);
82 }
83 
84 template <class T>
85 void
86 VectorToVnlVector ( const Vector<T>& v, vnl_vector<T>& vec )
87 {
88  vec.set_size(v.n());
89  // for ( int i = 0; i < v.n(); i += 1 )
90  // vec[i] = v[i];
91  cblas_copy<T>(vec.size(), v.rawX(),1, vec.data_block(), 1);
92 }
93 
94 template <class T>
95 void
96 SpMatrixToVnlMatrix ( const SpMatrix<T>& mat, vnl_matrix<T>& result )
97 {
98  result.set_size(mat.m(), mat.n());
99  result.fill(0.0);
100  int* pB = mat.pB();
101  int* pE = mat.pE();
102  int* r = mat.r();
103  for ( int i = 0; i < mat.n(); i += 1 )
104  for (int j = pB[i]; j<pE[i]; ++j)
105  result(r[j], i) = mat.v(j);
106 }
107 
108 
109 }
110 
111 #endif
112 
Sparse Matrix class.
Definition: linalg.h:63
Definition: dag.h:26
void VnlVectorToVector(const vnl_vector< T > &v, Vector< T > &vec)
Definition: utlITKSpams.h:76
int pB(const int i) const
returns pB[i]
Definition: linalg.h:779
void VectorToVnlVector(const Vector< T > &v, vnl_vector< T > &vec)
Definition: utlITKSpams.h:86
int * pE() const
Direct access to _pE.
Definition: linalg.h:806
T * rawX() const
returns a modifiable reference of the data, DANGEROUS
Definition: linalg.h:593
int m() const
Definition: linalg.h:222
int n() const
returns the size of the vector
Definition: linalg.h:591
T v(const int i) const
returns v[i]
Definition: linalg.h:783
Dense Vector class.
Definition: linalg.h:65
int m() const
returns the number of columns
Definition: linalg.h:789
void SpMatrixToVnlMatrix(const SpMatrix< T > &mat, vnl_matrix< T > &result)
Definition: utlITKSpams.h:96
int n() const
Number of columns.
Definition: linalg.h:224
void resize(const int n)
resize the vector
Definition: linalg.h:2876
int r(const int i) const
returns r[i]
Definition: linalg.h:781
int n() const
returns the number of rows
Definition: linalg.h:787
void MatrixToVnlMatrix(const Matrix< T > &matSpams, vnl_matrix< T > &matVnl)
Definition: utlITKSpams.h:54
void resize(int m, int n)
Resize the matrix.
Definition: linalg.h:1217
void VnlMatrixToMatrix(const vnl_matrix< T > &matVnl, Matrix< T > &matSpams)
Definition: utlITKSpams.h:32
const T * X() const
return a non-modifiable reference to the data
Definition: linalg.h:256
Dense Matrix class.
Definition: linalg.h:61
T * rawX() const
reference a modifiable reference to the data, DANGEROUS
Definition: linalg.h:254