Commit b094a7a2 authored by Christoph Richter's avatar Christoph Richter
Browse files

Enhanced sum command (fixes #15)

parent 0e5aa961
......@@ -9,6 +9,7 @@ import de.monticore.lang.monticar.generator.cpp.MathFunctionFixer;
import de.monticore.lang.monticar.generator.cpp.OctaveHelper;
import de.monticore.lang.monticar.generator.cpp.converter.ExecuteMethodGenerator;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.converter.StringIndexHelper;
import de.monticore.lang.monticar.generator.cpp.symbols.MathStringExpression;
import de.se_rwth.commons.logging.Log;
......@@ -76,6 +77,8 @@ public class MathSumCommand extends MathCommand {
MathFunctionFixer.fixMathFunctions(accessSymbol, bluePrintCPP);
if (mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().size() == 1) {
convertAccuSumImplementationArmadillo(mathMatrixNameExpressionSymbol, bluePrintCPP);
} else if (mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().size() == 2) {
convertSumImplementationArmadillo(mathMatrixNameExpressionSymbol, bluePrintCPP);
} else if (mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().size() == 4) {
MathMatrixAccessSymbol func = mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().get(0);
MathMatrixAccessSymbol sumVar = mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().get(1);
......@@ -83,10 +86,20 @@ public class MathSumCommand extends MathCommand {
MathMatrixAccessSymbol sumEnd = mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().get(3);
convertExtendedSumImplementationArmadillo(mathMatrixNameExpressionSymbol, func, sumVar, sumStart, sumEnd, bluePrintCPP);
} else {
Log.error(String.format("No implementation found for sum operation: \"sum(%s)\". Possible syntax is \"sum( X )\" or \"%s\"", mathExpressionSymbol.getTextualRepresentation(), SUM_SYNTAX_EXTENDED));
Log.error(String.format("No implementation found for sum operation: \"sum(%s)\". Possible syntax is \"sum( X )\", \"sum(X,dim)\" or \"%s\"", mathExpressionSymbol.getTextualRepresentation(), SUM_SYNTAX_EXTENDED));
}
}
private void convertSumImplementationArmadillo(MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol, BluePrintCPP bluePrintCPP) {
String valueListString = ExecuteMethodGenerator.generateExecuteCode(mathMatrixNameExpressionSymbol, new ArrayList<>());
// correct index for armadillo
valueListString = valueListString.substring(0, valueListString.length() - 1) + "-1)";
MathStringExpression stringExpression = new MathStringExpression("sum" + valueListString, mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
newMatrixAccessSymbols.add(new MathMatrixAccessSymbol(stringExpression));
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setMathMatrixAccessSymbols(newMatrixAccessSymbols);
}
/**
* Implements the sum command using Armadillos accu command
*
......@@ -192,7 +205,7 @@ public class MathSumCommand extends MathCommand {
return new Instruction() {
@Override
public String getTargetLanguageInstruction() {
return String.format(" for (int %s = %s - 1; %s <= %s - 1; %s++)\n", sumVar, sumStart, sumVar, sumEnd, sumVar);
return String.format(" for (int %s = %s; %s <= %s; %s++)\n", sumVar, sumStart, sumVar, sumEnd, sumVar);
}
@Override
......
......@@ -18,42 +18,42 @@ CONSTANTCONSTANTVECTOR0(2,0) = 3;
double calcSum1(colvec A)
{
double res = 0;
for (int i = 1 - 1; i <= 3 - 1; i++)
res += A(i);
for (int i = 1; i <= 3; i++)
res += A(i-1);
return res;
}
double calcSum2(colvec A, double x, int j)
{
double res = 0;
for (int i = 1 - 1; i <= 3 - 1; i++)
res += A(i)*A(j);
for (int i = 1; i <= 3; i++)
res += A(i-1)*A(j-1);
return res;
}
double calcSum3(colvec A, double x)
{
double res = 0;
for (int j = 1 - 1; j <= 2 - 1; j++)
for (int j = 1; j <= 2; j++)
res += calcSum2(A, x, j);
return res;
}
double calcSum4(colvec A, double x, double y, int j, int k)
{
double res = 0;
for (int i = 1 - 1; i <= 3 - 1; i++)
res += A(i)*A(j)*A(k);
for (int i = 1; i <= 3; i++)
res += A(i-1)*A(j-1)*A(k-1);
return res;
}
double calcSum5(colvec A, double x, double y, int k)
{
double res = 0;
for (int j = 1 - 1; j <= 2 - 1; j++)
for (int j = 1; j <= 2; j++)
res += calcSum4(A, x, y, j, k);
return res;
}
double calcSum6(colvec A, double x, double y)
{
double res = 0;
for (int k = 1 - 1; k <= 1 - 1; k++)
for (int k = 1; k <= 1; k++)
res += calcSum5(A, x, y, k);
return res;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment