GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
gmVectorUtils.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_VECTOR_UTILS_H_
25#define _GEMA_VECTOR_UTILS_H_
26
27#ifndef GM_VECTOR_DEFINED
28// Copy of the definition in gmVector.h to break a dependency cycle since gmVector.h must also
29// include gmVectorUtils.h
30#define GM_VECTOR_DEFINED
31#include <armadillo>
32typedef arma::vec GmVector;
33#endif
34
35#include <math.h>
36#include "gmLog.h"
37
38#include "gmDoubleCmp.h"
39#include <assert.h>
40
41#if ARMA_VERSION_MAJOR != 14 || ARMA_VERSION_MINOR != 0
42#error Unexpected Armadillo version. Please check that our internal fiddling done below is still valid
43#endif
44
45class LuaTable;
46
50namespace GmVectorUtils
51{
52 /*
54 inline double sqrDistance(const GmVector& a, const GmVector& b)
55 {
56 assert(a.n_elem == b.n_elem);
57
58 double sum = 0.0;
59 for(unsigned int i = 0; i<a.n_elem; i++)
60 {
61 double d = a[i] - b[i];
62 sum += d*d;
63 }
64 return sum;
65 }
66
68 inline double distance(const GmVector& a, const GmVector& b)
69 {
70 return sqrt(sqrDistance(a, b));
71 }
72
74 inline double sqrNorm(const GmVector& v)
75 {
76 double sum = 0.0;
77 for(unsigned int i = 0; i<v.n_elem; i++)
78 sum += v[i]*v[i];
79 return sum;
80 }*/
81
83 inline double sqrNorm(const double* v, unsigned s)
84 {
85 double sum = 0.0;
86 for(unsigned int i = 0; i < s; i++)
87 sum += v[i] * v[i];
88 return sum;
89 }
90
91
93 inline double equal(const GmVector& a, const GmVector& b, double relTol = GM_DOUBLECMP_RELTOL, double absTol = GM_DOUBLECMP_ABSTOL)
94 {
95 if(a.n_elem != b.n_elem)
96 return false;
97 for(unsigned i = 0; i<a.n_elem; i++)
98 {
99 if(!GmDoubleCmp::equal(a[i], b[i], relTol, absTol))
100 return false;
101 }
102 return true;
103 }
104
106 inline double isZero(const GmVector& a, double absTol)
107 {
108 for(unsigned i = 0; i<a.n_elem; i++)
109 {
110 if(!GmDoubleCmp::isZero(a[i], absTol))
111 return false;
112 }
113 return true;
114 }
115
116
124 inline void setVectorMemory(GmVector& v, double* data, int nlin)
125 {
126 // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
127 // implementation and is done only because there is really no other way (short of
128 // changing the library itself).
129 assert(v.vec_state == 1);
130 assert(v.mem_state == 2 || (v.mem_state == 0 && v.n_elem == 0 && v.mem == NULL));
131 assert(v.n_alloc == 0);
132 arma::access::rw(v.mem) = data;
133 arma::access::rw(v.mem_state) = 2;
134 arma::access::rw(v.n_rows) = nlin;
135 arma::access::rw(v.n_cols) = 1;
136 arma::access::rw(v.n_elem) = nlin;
137 }
138
145 {
146 // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
147 // implementation and is done only because there is really no other way (short of
148 // changing the library itself).
149 assert(v.vec_state == 1);
150 if(v.mem_state == 2)
151 {
152 arma::access::rw(v.mem) = NULL;
153 arma::access::rw(v.mem_state) = 0;
154 arma::access::rw(v.n_rows) = 0;
155 arma::access::rw(v.n_cols) = 1;
156 arma::access::rw(v.n_elem) = 0;
157 }
158 else if(v.mem_state == 0)
159 {
160 v.reset();
161 arma::access::rw(v.mem) = NULL;
162 }
163 else
164 assert(0);
165 assert(v.n_alloc == 0);
166 }
167
169 inline bool hasSharedMemory(GmVector& v) { return v.mem_state == 2; }
170
177 inline void setVectorSize(GmVector& v, int nlin)
178 {
179 // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
180 // implementation and is done only because there is really no other way (short of
181 // changing the library itself).
182 assert(v.vec_state == 1);
183 assert(v.mem_state == 2);
184 assert(v.n_alloc == 0);
185 assert(v.mem);
186 arma::access::rw(v.n_rows) = nlin;
187 arma::access::rw(v.n_cols) = 1;
188 arma::access::rw(v.n_elem) = nlin;
189 }
190
191 GMC_API_EXPORT void print(const GmVector& v, const GmLogCategory& logger, GmLogLevel level, int fieldWidth = 0,
192 char format = 'g', int precision = -1);
193
194 GMC_API_EXPORT QString toString(const GmVector& v, int fieldWidth = 0, char format = 'g', int precision = -1, bool noBraces = false);
195
198}
199
200#endif
201
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
Functions for comparing double values.
#define GM_DOUBLECMP_ABSTOL
Tolerância absoluta entre valores para comparar valores próximos de zero.
Definition gmDoubleCmp.h:64
#define GM_DOUBLECMP_RELTOL
Tolerância relativa entre valores = 0.000001%.
Definition gmDoubleCmp.h:61
Declaration of support functions and macros for information logging.
GmLogLevel
Available log levels list.
Definition gmLog.h:36
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition gmVector.h:34
bool equal(double a, double b, double relTol=GM_DOUBLECMP_RELTOL, double absTol=GM_DOUBLECMP_ABSTOL)
Funcao para comparar se dois números reais são iguais usando uma tolerância recebida como parâmetro.
Definition gmDoubleCmp.h:71
bool isZero(double a, double absTol)
Funcao para comparar se o valor a é ou não igual a zero dada uma tolerância absoluta.
Definition gmDoubleCmp.h:93
Groups utilitary routines for working with vectors.
Definition gmVectorUtils.cpp:31
double isZero(const GmVector &a, double absTol)
Check to see if a vector is zero using GmDoubleCmp::isZero over each component.
Definition gmVectorUtils.h:106
void print(const GmVector &v, const GmLogCategory &logger, GmLogLevel level, int fieldWidth, char format, int precision)
Prints the vector using the specified logger, level and precision fields. Values are separated by spa...
Definition gmVectorUtils.cpp:34
QString toString(const GmVector &v, int fieldWidth, char format, int precision, bool noBraces)
Serializes the vector to a string using the specified precision fields. Coordinates are surrounded by...
Definition gmVectorUtils.cpp:48
void setVectorMemory(GmVector &v, double *data, int nlin)
Updates the memory area used internally by a vector. DANGEROUS. Should be used only by the bold ones ...
Definition gmVectorUtils.h:124
double equal(const GmVector &a, const GmVector &b, double relTol=GM_DOUBLECMP_RELTOL, double absTol=GM_DOUBLECMP_ABSTOL)
Check to see if two vectors are equal comparing values using GmDoubleCmp::equal.
Definition gmVectorUtils.h:93
double sqrNorm(const double *v, unsigned s)
Returns the squared norm of vector v, with size s.
Definition gmVectorUtils.h:83
bool hasSharedMemory(GmVector &v)
Is this vector operating over a shared memory (set by setVectorMemory())?
Definition gmVectorUtils.h:169
void fillFromLuaTable(GmVector &v, LuaTable &t)
Resizes and fills the vector v to receive the data stored in a lua table.
Definition gmVectorUtils.cpp:60
void resetVectorMemory(GmVector &v)
Updates a vector that was prepared with setVectorMemory() to a common empty column vector....
Definition gmVectorUtils.h:144
void setVectorSize(GmVector &v, int nlin)
Updates the logical vector size WITHOUT changin the used memory area. DANGEROUS. Should be used only ...
Definition gmVectorUtils.h:177