GemaCoreLib
The GeMA Core library
gmMatrix.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_H_
25 #define _GEMA_MATRIX_H_
26 
27 #include "gmCoreConfig.h"
28 
34 #ifndef GM_MATRIX_DEFINED
35 #define GM_MATRIX_DEFINED
36 
37 #include <armadillo>
38 typedef arma::mat GmMatrix;
39 
40 // Register type for storing them inside a QVariant
41 #include <QMetaType>
43 
44 #endif
45 
46 // A define that can be used by code that expects that the matrix is stored in
47 // column major format to check and mark that.
48 // Unfortuantely, this define was created late in the game, so do not assume
49 // that every (rare) place in GeMA that depends on a GmMatrix being column major
50 // (as an optimization) to have an assert :(
51 #define GM_MATRIX_IS_COLUMN_MAJOR
52 
53 #include "gmMatrixUtils.h"
54 #include <assert.h>
55 
56 
58 typedef arma::subview_col<double> GmMatrixCol;
59 
60 #if ARMA_VERSION_MAJOR != 5 || ARMA_VERSION_MINOR != 200
61 #error Unexpected Armadillo version. Please check that our internal fiddling done bellow is still valid
62 #endif
63 
77 #define DECLARE_REF_MATRIX(x, ptr, lin, col) GmMatrix x((ptr), (lin), (col), false, true)
78 
79 
101 {
102 public:
104  GmCRMatrix(const double* data, int nlin, int ncol)
105  : n_rows(nlin), n_cols(ncol), n_elem(nlin*ncol),
106  _mat(const_cast<double*>(data), nlin, ncol, false, true)
107  {
108  }
109 
112  : n_rows(0), n_cols(0), n_elem(0), _mat(NULL, 0, 0, false, true)
113  {
114  }
115 
116  typedef double elem_type;
117 
118  const unsigned int n_rows;
119  const unsigned int n_cols;
120  const unsigned int n_elem;
121 
123  operator const GmMatrix& () { return _mat; }
124 
126  const GmMatrix& m() { return _mat; }
127 
131  void setMemory(const double* data, int nlin, int ncol)
132  {
133  // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
134  // implementation and is done only because there is really no other way (short of
135  // changing the library itself). Not very different from this whole class, by the way.
136  arma::access::rw(n_rows) = nlin;
137  arma::access::rw(n_cols) = ncol;
138  arma::access::rw(n_elem) = nlin * ncol;
139 
140  GmMatrixUtils::setMatrixMemory(_mat, const_cast<double*>(data), nlin, ncol);
141  }
142 
143  const double& operator[] (unsigned int ii) const { return _mat[ii]; }
144  const double& at(unsigned int ii) const { return _mat.at(ii); }
145  const double& operator() (unsigned int ii) const { return _mat(ii); }
146 
147  const double& at(unsigned int in_row, unsigned int in_col) const { return _mat.at(in_row, in_col); }
148  const double& operator() (unsigned int in_row, unsigned int in_col) const { return _mat(in_row, in_col); }
149 
150  bool is_empty() const { return _mat.is_empty(); }
151  bool is_vec() const { return _mat.is_vec(); }
152  bool is_rowvec() const { return _mat.is_rowvec(); }
153  bool is_colvec() const { return _mat.is_colvec(); }
154  bool is_square() const { return _mat.is_square(); }
155 
156  bool in_range(unsigned int ii) const { return _mat.in_range(ii); }
157  bool in_range(unsigned int in_row, unsigned int in_col) const { return _mat.in_range(in_row, in_col); }
158 
159  const double* colptr(unsigned int in_col) const { return _mat.colptr(in_col); }
160  const double* memptr() const { return _mat.memptr(); }
161 
162  bool empty() const { return _mat.empty(); }
163  unsigned int size() const { return _mat.size(); }
164 
165 private:
166  Q_DISABLE_COPY(GmCRMatrix)
167 
168  GmMatrix _mat;
169 };
170 
171 #endif
172 
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
GmCRMatrix(const double *data, int nlin, int ncol)
Constructs the matrix pointing to a const memory area represented in COLUMN MAJOR ORDER.
Definition: gmMatrix.h:104
const unsigned int n_cols
number of columns in the matrix (read-only)
Definition: gmMatrix.h:119
GmCRMatrix()
Constructs an empty matrix that needs to be initialized later by a call to setMemory()
Definition: gmMatrix.h:111
double elem_type
the type of elements stored in the matrix
Definition: gmMatrix.h:116
Declaration of usefull configuration definitions for the Core library.
Utilitary functions for working with doubles.
const GmMatrix & m()
Returns the wrapped matrix as a CONST reference.
Definition: gmMatrix.h:126
arma::subview_col< double > GmMatrixCol
A subcolumn view of a matrix.
Definition: gmMatrix.h:58
const unsigned int n_elem
number of elements in the matrix (read-only)
Definition: gmMatrix.h:120
const unsigned int n_rows
number of rows in the matrix (read-only)
Definition: gmMatrix.h:118
#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
An auxiliary matrix WRAPPER that binds the matrix to a CONST memory area with data already inicialize...
Definition: gmMatrix.h:100
arma::mat GmMatrix
The basic type for a GeMA matrix object. Currently based on an Armadillo matrix.
Definition: gmMatrix.h:38
void setMemory(const double *data, int nlin, int ncol)
Exchanges the memory area used by the matrix. Same caveats explained in the class documentation apply...
Definition: gmMatrix.h:131
Q_DECLARE_METATYPE(LuaFunction)