-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultidimvar.cpp
63 lines (45 loc) · 2.02 KB
/
multidimvar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "multidimvar.h"
// Receives a string with a value with multiple dimensions separated by space
// Example of input "5 4 6". Variable with dim1=5, dim2=4, dim3=6.
MultiDimVar::MultiDimVar(QString multiDimValue)
{
this->multiDimValue=UtilXmlTools::qListDoubleFromSpacedString(multiDimValue);
}
// Inicializes a new MultiDimVar with the dimensions specified with all zero values
MultiDimVar::MultiDimVar(int dim){
for(int i=0; i<dim; i++){
this->multiDimValue.append(0);
}
}
// Sums 2 MultiDimVar values
MultiDimVar MultiDimVar::sum(const MultiDimVar &val1, const MultiDimVar &val2){
MultiDimVar result(val1.multiDimValue.size());
if(val1.multiDimValue.size()!=val2.multiDimValue.size()){
UtilXmlTools::displayErrorMessage("MultiDimVar sum","Error summing MultiDimVar, variables don't have the same dimensions.");
}
for(int i=0; i<result.multiDimValue.size(); i++){
result.multiDimValue[i]=val1.multiDimValue[i]+val2.multiDimValue[i];
}
return result;
}
// Subtract 2 MultiDimVar values
MultiDimVar MultiDimVar::sub(const MultiDimVar &val1, const MultiDimVar &val2){
MultiDimVar result(val1.multiDimValue.size());
if(val1.multiDimValue.size()!=val2.multiDimValue.size()){
UtilXmlTools::displayErrorMessage("MultiDimVar subtraction","Error subtracting MultiDimVar, variables don't have the same dimensions.");
}
for(int i=0; i<result.multiDimValue.size(); i++){
result.multiDimValue[i]=val1.multiDimValue[i]-val2.multiDimValue[i];
}
return result;
}
// Returns a string representation of this multidimensional value as string.
// Each dimension is separated by a space.
QString MultiDimVar::toString(){
QString result;
for(int i=0; i<this->multiDimValue.size()-1; i++){
result += QString::number(this->multiDimValue[i],'G',9) + " "; // G is the option used by onisplit
}
result += QString::number(this->multiDimValue[this->multiDimValue.size()-1],'G',9); // last one does not have a space at the end
return result;
}