GemaCoreLib
The GeMA Core library
gmMatrixUtils.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
24 #ifndef _GEMA_MATRIX_UTILS_H_
25 #define _GEMA_MATRIX_UTILS_H_
26 
27 #ifndef GM_MATRIX_DEFINED
28 // Copy of the definition in gmMatrix.h to break a dependency cycle since gmMatrix.h must also
29 // include gmMatrixUtils.h
30 #define GM_MATRIX_DEFINED
31 #include <armadillo>
32 typedef arma::mat GmMatrix;
33 #endif
34 
35 #include <math.h>
36 #include "gmLog.h"
37 #include <assert.h>
38 
39 #if ARMA_VERSION_MAJOR != 5 || ARMA_VERSION_MINOR != 200
40 #error Unexpected Armadillo version. Please check that our internal fiddling done bellow is still valid
41 #endif
42 
44 namespace GmMatrixUtils
45 {
46 
51  inline void fill(GmMatrix& m, const double* data, int nlin, int ncol)
52  {
53  m.set_size(nlin, ncol);
54  memcpy(m.memptr(), data, nlin * ncol * sizeof(double));
55  }
56 
58  inline void fillLine(GmMatrix& m, const double* data, int lin)
59  {
60  switch (m.n_cols)
61  {
62  // Optimize common case of small matrices
63  case 8: m(lin, 7) = data[7];
64  case 7: m(lin, 6) = data[6];
65  case 6: m(lin, 5) = data[5];
66  case 5: m(lin, 4) = data[4];
67  case 4: m(lin, 3) = data[3];
68  case 3: m(lin, 2) = data[2];
69  case 2: m(lin, 1) = data[1];
70  case 1: m(lin, 0) = data[0];
71  break;
72  default:
73  for (unsigned int i = 0; i < m.n_cols; i++)
74  m(lin, i) = data[i];
75  }
76  }
77 
79  inline void fillColumn(GmMatrix& m, const double* data, int col)
80  {
81  switch (m.n_rows)
82  {
83  // Optimize common case of small matrices
84  case 8: m(7, col) = data[7];
85  case 7: m(6, col) = data[6];
86  case 6: m(5, col) = data[5];
87  case 5: m(4, col) = data[4];
88  case 4: m(3, col) = data[3];
89  case 3: m(2, col) = data[2];
90  case 2: m(1, col) = data[1];
91  case 1: m(0, col) = data[0];
92  break;
93  default:
94  for (unsigned int i = 0; i < m.n_rows; i++)
95  m(i, col) = data[i];
96  }
97  }
98 
106  inline void setMatrixMemory(GmMatrix& m, double* data, int nlin, int ncol)
107  {
108  // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
109  // implementation and is done only because there is really no other way (short of
110  // changing the library itself).
111  assert(m.vec_state == 0);
112  assert(m.mem_state == 2 || (m.mem_state == 0 && m.n_elem == 0 && m.mem == NULL));
113  arma::access::rw(m.mem) = data;
114  arma::access::rw(m.mem_state) = 2;
115  arma::access::rw(m.n_rows) = nlin;
116  arma::access::rw(m.n_cols) = ncol;
117  arma::access::rw(m.n_elem) = nlin * ncol;
118  }
119 
125  inline void resetMatrixMemory(GmMatrix& m)
126  {
127  // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
128  // implementation and is done only because there is really no other way (short of
129  // changing the library itself).
130  assert(m.vec_state == 0);
131  if(m.mem_state == 2)
132  {
133  arma::access::rw(m.mem) = NULL;
134  arma::access::rw(m.mem_state) = 0;
135  arma::access::rw(m.n_rows) = 0;
136  arma::access::rw(m.n_cols) = 0;
137  arma::access::rw(m.n_elem) = 0;
138  }
139  else if(m.mem_state == 0)
140  {
141  m.reset();
142  arma::access::rw(m.mem) = NULL;
143  }
144  else
145  assert(0);
146  }
147 
149  inline bool hasSharedMemory(GmMatrix& m) { return m.mem_state == 2; }
150 
158  inline void setMatrixSize(GmMatrix& m, int nlin, int ncol)
159  {
160  // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
161  // implementation and is done only because there is really no other way (short of
162  // changing the library itself).
163  assert(m.vec_state == 0);
164  assert(m.mem_state == 2);
165  assert(m.mem);
166  arma::access::rw(m.n_rows) = nlin;
167  arma::access::rw(m.n_cols) = ncol;
168  arma::access::rw(m.n_elem) = nlin * ncol;
169  }
170 
171  GMC_API_EXPORT void print(const GmMatrix& m, const GmLogCategory& logger, GmLogLevel level, int fieldWidth = 0,
172  char format = 'g', int precision = -1);
173 
174  GMC_API_EXPORT QString toString (const GmMatrix& m, int fieldWidth = 0, char format = 'g', int precision = -1);
175  GMC_API_EXPORT QString toColMajorString(const GmMatrix& m, int fieldWidth = 0, char format = 'g', int precision = -1);
176 
178  GMC_API_EXPORT bool fillFromLuaTable (GmMatrix& m, LuaTable& t, int nlin, int ncol);
180 }
181 
182 #endif
Groups utilitary routines for working with matrices.
Definition: gmMatrixUtils.cpp:30
void setMatrixMemory(GmMatrix &m, double *data, int nlin, int ncol)
Updates the memory area used internally by a matrix. DANGEROUS. Should be used only by the bold ones ...
Definition: gmMatrixUtils.h:106
QString toString(const GmMatrix &m, int fieldWidth, char format, int precision)
Serializes the matrix to a string using the specified precision fields. Lines are surrounded by [] an...
Definition: gmMatrixUtils.cpp:51
void resetMatrixMemory(GmMatrix &m)
Updates a matrix that was prepared with setMatrixMemory() to a common empty matrix....
Definition: gmMatrixUtils.h:125
void fill(GmMatrix &m, const double *data, int nlin, int ncol)
Copy the contents of data to matrix m. If m size is different from nlin, ncol the matrix is resized....
Definition: gmMatrixUtils.h:51
void fillFromLuaTable(GmMatrix &m, LuaTable &t)
Resizes and fills the matrix m to receive the data stored in a lua table.
Definition: gmMatrixUtils.cpp:95
arma::mat GmMatrix
The basic type for a GeMA matrix object. Currently based on an Armadillo matrix.
Definition: gmMatrixUtils.h:32
void fillColumn(GmMatrix &m, const double *data, int col)
Fills the specified column of a matrix m with the specified data.
Definition: gmMatrixUtils.h:79
void fillLine(GmMatrix &m, const double *data, int lin)
Fills the specified line of a matrix m with the specified data.
Definition: gmMatrixUtils.h:58
bool updateFromLuaTable(GmMatrix &m, LuaTable &t)
Updates the matrix m with the data stored in a lua table. The matrix size is left unchanged.
Definition: gmMatrixUtils.cpp:284
void print(const GmMatrix &m, const GmLogCategory &logger, GmLogLevel level, int fieldWidth, char format, int precision)
Prints the matrix using the specified logger, level and precision fields.
Definition: gmMatrixUtils.cpp:34
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
void setMatrixSize(GmMatrix &m, int nlin, int ncol)
Updates the logical matrix size WITHOUT changin the used memory area. DANGEROUS. Should be used only ...
Definition: gmMatrixUtils.h:158
GmLogLevel
Available log levels list.
Definition: gmLog.h:36
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
arma::mat GmMatrix
The basic type for a GeMA matrix object. Currently based on an Armadillo matrix.
Definition: gmMatrix.h:38
bool hasSharedMemory(GmMatrix &m)
Is this matrix operating over a shared memory (set by setMatrixMemory())?
Definition: gmMatrixUtils.h:149
QString toColMajorString(const GmMatrix &m, int fieldWidth, char format, int precision)
Serializes the matrix to a "linear" string, using the specified precision fields, with values ordered...
Definition: gmMatrixUtils.cpp:72
Declaration of support functions and macros for information logging.