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