GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
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>
32typedef arma::mat GmMatrix;
33#endif
34
35#include <math.h>
36#include "gmLog.h"
37#include <assert.h>
38
39#if ARMA_VERSION_MAJOR != 14 || ARMA_VERSION_MINOR != 0
40#error Unexpected Armadillo version. Please check that our internal fiddling done below is still valid
41#endif
42
44namespace 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]; [[fallthrough]];
64 case 7: m(lin, 6) = data[6]; [[fallthrough]];
65 case 6: m(lin, 5) = data[5]; [[fallthrough]];
66 case 5: m(lin, 4) = data[4]; [[fallthrough]];
67 case 4: m(lin, 3) = data[3]; [[fallthrough]];
68 case 3: m(lin, 2) = data[2]; [[fallthrough]];
69 case 2: m(lin, 1) = data[1]; [[fallthrough]];
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]; [[fallthrough]];
85 case 7: m(6, col) = data[6]; [[fallthrough]];
86 case 6: m(5, col) = data[5]; [[fallthrough]];
87 case 5: m(4, col) = data[4]; [[fallthrough]];
88 case 4: m(3, col) = data[3]; [[fallthrough]];
89 case 3: m(2, col) = data[2]; [[fallthrough]];
90 case 2: m(1, col) = data[1]; [[fallthrough]];
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 assert(m.n_alloc == 0);
114 arma::access::rw(m.mem) = data;
115 arma::access::rw(m.mem_state) = 2;
116 arma::access::rw(m.n_rows) = nlin;
117 arma::access::rw(m.n_cols) = ncol;
118 arma::access::rw(m.n_elem) = nlin * ncol;
119 }
120
127 {
128 // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
129 // implementation and is done only because there is really no other way (short of
130 // changing the library itself).
131 assert(m.vec_state == 0);
132 if(m.mem_state == 2)
133 {
134 arma::access::rw(m.mem) = NULL;
135 arma::access::rw(m.mem_state) = 0;
136 arma::access::rw(m.n_rows) = 0;
137 arma::access::rw(m.n_cols) = 0;
138 arma::access::rw(m.n_elem) = 0;
139 }
140 else if(m.mem_state == 0)
141 {
142 m.reset();
143 arma::access::rw(m.mem) = NULL;
144 }
145 else
146 assert(0);
147 assert(m.n_alloc == 0);
148 }
149
151 inline bool hasSharedMemory(GmMatrix& m) { return m.mem_state == 2; }
152
160 inline void setMatrixSize(GmMatrix& m, int nlin, int ncol)
161 {
162 // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
163 // implementation and is done only because there is really no other way (short of
164 // changing the library itself).
165 assert(m.vec_state == 0);
166 assert(m.mem_state == 2);
167 assert(m.n_alloc == 0);
168 assert(m.mem);
169 arma::access::rw(m.n_rows) = nlin;
170 arma::access::rw(m.n_cols) = ncol;
171 arma::access::rw(m.n_elem) = nlin * ncol;
172 }
173
174 GMC_API_EXPORT void print(const GmMatrix& m, const GmLogCategory& logger, GmLogLevel level, int fieldWidth = 0,
175 char format = 'g', int precision = -1);
176
177 GMC_API_EXPORT QString toString (const GmMatrix& m, int fieldWidth = 0, char format = 'g', int precision = -1);
178 GMC_API_EXPORT QString toColMajorString(const GmMatrix& m, int fieldWidth = 0, char format = 'g', int precision = -1);
179
181 GMC_API_EXPORT bool fillFromLuaTable (GmMatrix& m, LuaTable& t, int nlin, int ncol);
183}
184
185#endif
Class representing a category with multiple logging levels.
Definition gmLog.h:58
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
Declaration of support functions and macros for information logging.
GmLogLevel
Available log levels list.
Definition gmLog.h:36
arma::mat GmMatrix
The basic type for a GeMA matrix object. Currently based on an Armadillo matrix.
Definition gmMatrix.h:38
Groups utilitary routines for working with matrices.
Definition gmMatrixUtils.cpp:31
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 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
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:160
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
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 resetMatrixMemory(GmMatrix &m)
Updates a matrix that was prepared with setMatrixMemory() to a common empty matrix....
Definition gmMatrixUtils.h:126
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
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
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
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
bool hasSharedMemory(GmMatrix &m)
Is this matrix operating over a shared memory (set by setMatrixMemory())?
Definition gmMatrixUtils.h:151
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