GemaCoreLib
The GeMA Core library
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>
32 typedef arma::vec GmVector;
33 #endif
34 
35 #include <math.h>
36 #include "gmLog.h"
37 #include "gmDoubleCmp.h"
38 #include <assert.h>
39 
40 #if ARMA_VERSION_MAJOR != 5 || ARMA_VERSION_MINOR != 200
41 #error Unexpected Armadillo version. Please check that our internal fiddling done bellow is still valid
42 #endif
43 
44 class LuaTable;
45 
49 namespace GmVectorUtils
50 {
51 
53  inline double sqrDistance(const GmVector& a, const GmVector& b)
54  {
55  assert(a.n_elem == b.n_elem);
56 
57  double sum = 0.0;
58  for(unsigned int i = 0; i<a.n_elem; i++)
59  {
60  double d = a[i] - b[i];
61  sum += d*d;
62  }
63  return sum;
64  }
65 
67  inline double distance(const GmVector& a, const GmVector& b)
68  {
69  return sqrt(sqrDistance(a, b));
70  }
71 
73  inline double sqrNorm(const GmVector& v)
74  {
75  double sum = 0.0;
76  for(unsigned int i = 0; i<v.n_elem; i++)
77  sum += v[i]*v[i];
78  return sum;
79  }
80 
82  inline double equal(const GmVector& a, const GmVector& b, double relTol = GM_DOUBLECMP_RELTOL, double absTol = GM_DOUBLECMP_ABSTOL)
83  {
84  if(a.n_elem != b.n_elem)
85  return false;
86  for(unsigned i = 0; i<a.n_elem; i++)
87  {
88  if(!GmDoubleCmp::equal(a[i], b[i], relTol, absTol))
89  return false;
90  }
91  return true;
92  }
93 
95  inline double isZero(const GmVector& a, double absTol)
96  {
97  for(unsigned i = 0; i<a.n_elem; i++)
98  {
99  if(!GmDoubleCmp::isZero(a[i], absTol))
100  return false;
101  }
102  return true;
103  }
104 
105 
113  inline void setVectorMemory(GmVector& v, double* data, int nlin)
114  {
115  // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
116  // implementation and is done only because there is really no other way (short of
117  // changing the library itself).
118  assert(v.vec_state == 1);
119  assert(v.mem_state == 2 || (v.mem_state == 0 && v.n_elem == 0 && v.mem == NULL));
120  arma::access::rw(v.mem) = data;
121  arma::access::rw(v.mem_state) = 2;
122  arma::access::rw(v.n_rows) = nlin;
123  arma::access::rw(v.n_cols) = 1;
124  arma::access::rw(v.n_elem) = nlin;
125  }
126 
132  inline void resetVectorMemory(GmVector& v)
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).
137  assert(v.vec_state == 1);
138  if(v.mem_state == 2)
139  {
140  arma::access::rw(v.mem) = NULL;
141  arma::access::rw(v.mem_state) = 0;
142  arma::access::rw(v.n_rows) = 0;
143  arma::access::rw(v.n_cols) = 1;
144  arma::access::rw(v.n_elem) = 0;
145  }
146  else if(v.mem_state == 0)
147  {
148  v.reset();
149  arma::access::rw(v.mem) = NULL;
150  }
151  else
152  assert(0);
153  }
154 
156  inline bool hasSharedMemory(GmVector& v) { return v.mem_state == 2; }
157 
164  inline void setVectorSize(GmVector& v, int nlin)
165  {
166  // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
167  // implementation and is done only because there is really no other way (short of
168  // changing the library itself).
169  assert(v.vec_state == 1);
170  assert(v.mem_state == 2);
171  assert(v.mem);
172  arma::access::rw(v.n_rows) = nlin;
173  arma::access::rw(v.n_cols) = 1;
174  arma::access::rw(v.n_elem) = nlin;
175  }
176 
177  GMC_API_EXPORT void print(const GmVector& v, const GmLogCategory& logger, GmLogLevel level, int fieldWidth = 0,
178  char format = 'g', int precision = -1);
179 
180  GMC_API_EXPORT QString toString(const GmVector& v, int fieldWidth = 0, char format = 'g', int precision = -1, bool noBraces = false);
181 
183  GMC_API_EXPORT bool fillFromLuaTable(GmVector& v, LuaTable& t, int n);
184 }
185 
186 #endif
187 
#define GM_DOUBLECMP_ABSTOL
Tolerância absoluta entre valores para comparar valores próximos de zero.
Definition: gmDoubleCmp.h:64
double distance(const GmVector &a, const GmVector &b)
Returns the distance between vectors a and b.
Definition: gmVectorUtils.h:67
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:82
void resetVectorMemory(GmVector &v)
Updates a vector that was prepared with setVectorMemory() to a common empty column vector....
Definition: gmVectorUtils.h:132
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
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
double sqrDistance(const GmVector &a, const GmVector &b)
Returns the squared distance between vectors a and b.
Definition: gmVectorUtils.h:53
Functions for comparing double values.
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition: gmVectorUtils.h:32
double isZero(const GmVector &a, double absTol)
Check to see if a vector is zero using GmDoubleCmp::isZero over each component.
Definition: gmVectorUtils.h:95
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
Groups utilitary routines for working with vectors.
Definition: gmVectorUtils.cpp:30
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
#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
GmLogLevel
Available log levels list.
Definition: gmLog.h:36
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
#define GM_DOUBLECMP_RELTOL
Tolerância relativa entre valores = 0.000001%.
Definition: gmDoubleCmp.h:61
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
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:164
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition: gmVector.h:34
bool hasSharedMemory(GmVector &v)
Is this vector operating over a shared memory (set by setVectorMemory())?
Definition: gmVectorUtils.h:156
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:113
double sqrNorm(const GmVector &v)
Returns the squared norm of vector v.
Definition: gmVectorUtils.h:73
Declaration of support functions and macros for information logging.