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