22 #ifndef __HERMES_COMMON_DENSE_MATRIX_OPERATIONS_H
23 #define __HERMES_COMMON_DENSE_MATRIX_OPERATIONS_H
36 namespace DenseMatrixOperations
45 T **vec = (T**)calloc_with_check_direct_size<char>(
sizeof(T *)* m +
sizeof(T)* m * n);
46 T *row = (T *)(vec + m);
47 for (
unsigned int i = 0; i < m; i++, row += n)
53 T **new_matrix_malloc(
unsigned int m,
unsigned int n = 0)
56 T **vec = (T **)malloc(
sizeof(T *)* m +
sizeof(T)* m * n);
57 memset(vec, 0,
sizeof(T *)* m +
sizeof(T)* m * n);
58 T *row = (T *)(vec + m);
59 for (
unsigned int i = 0; i < m; i++, row += n) vec[i] = row;
66 void copy_matrix(T** dest, T** src,
unsigned int m,
unsigned int n = 0)
69 for (
unsigned int i = 0; i < m; i++)
71 memcpy(dest[i], src[i], n*
sizeof(T));
82 void save_matrix_octave(
const std::string& matrix_name, T** matrix,
unsigned int m,
unsigned int n = 0,
const std::string& filename = std::string())
87 std::string fname = filename;
89 fname = matrix_name +
".mat";
92 std::ofstream fout(fname.c_str());
100 fout << std::string(
"# name: ") << matrix_name << std::endl;
101 fout << std::string(
"# type: matrix") << std::endl;
102 fout << std::string(
"# rows: ") << m << std::endl;
103 fout << std::string(
"# columns: ") << n << std::endl;
106 for (
unsigned int i = 0; i < m; i++)
108 for (
unsigned int k = 0; k < n; k++)
109 fout <<
' ' << matrix[i][k];
120 unsigned int m,
const std::string& filename = std::string())
123 std::string fname = filename;
125 fname = matrix_name +
".mat";
128 std::ofstream fout(fname.c_str());
136 fout << std::string(
"# name: ") << matrix_name << std::endl;
137 fout << std::string(
"# type: sparse matrix") << std::endl;
138 fout << std::string(
"# nnz: ") << Ap[m] << std::endl;
139 fout << std::string(
"# rows: ") << m << std::endl;
140 fout << std::string(
"# columns: ") << m << std::endl;
143 for (
int j = 0; j < m; j++)
144 for (
int i = Ap[j]; i < Ap[j + 1]; i++)
145 fout << j + 1 <<
" " << Ai[i] + 1 <<
" " << Ax[i] << std::endl;
154 void transpose(T **matrix,
unsigned int m,
unsigned int n)
156 unsigned int min = std::min(m, n);
157 for (
unsigned int i = 0; i < min; i++)
158 for (
unsigned int j = i + 1; j < min; j++)
159 std::swap(matrix[i][j], matrix[j][i]);
162 for (
unsigned int i = 0; i < m; i++)
163 for (
unsigned int j = m; j < n; j++)
164 matrix[j][i] = matrix[i][j];
166 for (
unsigned int i = n; i < m; i++)
167 for (
unsigned int j = 0; j < n; j++)
168 matrix[j][i] = matrix[i][j];
174 void transpose(T *matrix,
unsigned int m,
unsigned int n,
unsigned int size)
176 unsigned int min = std::min(m, n);
177 for (
unsigned int i = 0; i < min; i++)
179 for (
unsigned int j = i + 1; j < min; j++)
181 std::swap(matrix[i * size + j], matrix[j * size + i]);
186 for (
unsigned int i = 0; i < m; i++)
187 for (
unsigned int j = m; j < n; j++)
188 matrix[j * size + i] = matrix[i * size + j];
191 for (
unsigned int i = n; i < m; i++)
192 for (
unsigned int j = 0; j < n; j++)
193 matrix[j * size + i] = matrix[i * size + j];
200 for (
unsigned int i = 0; i < m; i++)
201 for (
unsigned int j = 0; j < n; j++)
202 matrix[i][j] = -matrix[i][j];
206 void change_sign(T *matrix,
unsigned int m,
unsigned int n,
unsigned int size)
208 for (
unsigned int i = 0; i < m; i++)
209 for (
unsigned int j = 0; j < n; j++)
211 int local_matrix_index_array = i * size + j;
212 matrix[local_matrix_index_array] = -matrix[local_matrix_index_array];
222 template<
typename T,
typename Int>
223 HERMES_API
void ludcmp(T **a, Int n, Int *indx,
double *d);
232 template<
typename T,
typename S,
typename Int>
233 HERMES_API
void lubksb(T **a, Int n, Int *indx, S *b);
239 template<
typename T,
typename Int>
240 HERMES_API
void choldc(T **a, Int n, T p[]);
249 template<
typename T,
typename Int>
250 void cholsl(
double **a, Int n,
double p[], T b[], T x[])
255 for (i = 0; i < n; i++)
259 while (--k >= 0) sum -= a[i][k] * x[k];
263 for (i = n - 1; i >= 0; i--)
267 while (++k < n) sum -= a[k][i] * x[k];
HERMES_API void ludcmp(T **a, Int n, Int *indx, double *d)
void save_sparse_matrix_octave(const std::string &matrix_name, const T *Ax, const int *Ap, const int *Ai, unsigned int m, const std::string &filename=std::string())
Saves MxM sparse matrix to a octave file format.
General namespace for the Hermes library.
Exception interface Basically a std::exception, but with a constructor with string and with print_msg...
void transpose(T **matrix, unsigned int m, unsigned int n)
T ** new_matrix(unsigned int m, unsigned int n=0)
File containing common definitions, and basic global enums etc. for HermesCommon. ...
void cholsl(double **a, Int n, double p[], T b[], T x[])
void copy_matrix(T **dest, T **src, unsigned int m, unsigned int n=0)
File containing definition of exceptions classes.
File containing platform compatibility layer, especially for Win / MSVC.
void change_sign(T **matrix, unsigned int m, unsigned int n)
Changes the sign of a matrix.
HERMES_API void choldc(T **a, Int n, T p[])
File containing common definitions, and basic global enums etc. for HermesCommon. ...
HERMES_API void lubksb(T **a, Int n, Int *indx, S *b)
void save_matrix_octave(const std::string &matrix_name, T **matrix, unsigned int m, unsigned int n=0, const std::string &filename=std::string())
Saves a dense matrix to a octave file format.