Commit c54e58c9 authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Fix: remove REQUIRE macro use from HelperA.h if no tests are generated

parent fef3de38
Pipeline #99560 passed with stages
in 40 minutes and 6 seconds
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
<groupId>de.monticore.lang.monticar</groupId> <groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-generator</artifactId> <artifactId>embedded-montiarc-math-generator</artifactId>
<version>0.1.4-SNAPSHOT</version> <version>0.1.5-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= --> <!-- == PROJECT DEPENDENCIES ============================================= -->
......
...@@ -28,10 +28,10 @@ import de.monticore.lang.monticar.generator.FileContent; ...@@ -28,10 +28,10 @@ import de.monticore.lang.monticar.generator.FileContent;
public class ArmadilloHelper { public class ArmadilloHelper {
public static String fileName = "HelperA"; public static String fileName = "HelperA";
public static FileContent getArmadilloHelperFileContent() { public static FileContent getArmadilloHelperFileContent(boolean generateTests) {
FileContent fileContent = new FileContent(); FileContent fileContent = new FileContent();
fileContent.setFileName(fileName + ".h"); fileContent.setFileName(fileName + ".h");
String fileContentString = ArmadilloHelperSource.armadilloHelperSourceCode; String fileContentString = generateTests ? ArmadilloHelperSource.armadilloHelperSourceCodeWithTests : ArmadilloHelperSource.armadilloHelperSourceCode;
fileContent.setFileContent(fileContentString); fileContent.setFileContent(fileContentString);
return fileContent; return fileContent;
......
...@@ -24,7 +24,7 @@ package de.monticore.lang.monticar.generator.cpp; ...@@ -24,7 +24,7 @@ package de.monticore.lang.monticar.generator.cpp;
* @author Sascha Schneiders * @author Sascha Schneiders
*/ */
public class ArmadilloHelperSource { public class ArmadilloHelperSource {
public static String armadilloHelperSourceCode = "#ifndef HELPERA_H\n" + public static String armadilloHelperSourceCodeWithTests = "#ifndef HELPERA_H\n" +
"#define HELPERA_H\n" + "#define HELPERA_H\n" +
"#include <iostream>\n" + "#include <iostream>\n" +
"#include \"armadillo.h\"\n" + "#include \"armadillo.h\"\n" +
...@@ -180,4 +180,146 @@ public class ArmadilloHelperSource { ...@@ -180,4 +180,146 @@ public class ArmadilloHelperSource {
"}\n" + "}\n" +
"};\n" + "};\n" +
"#endif\n"; "#endif\n";
public static String armadilloHelperSourceCode = "#ifndef HELPERA_H\n" +
"#define HELPERA_H\n" +
"#include <iostream>\n" +
"#include \"armadillo.h\"\n" +
"#include <stdarg.h>\n" +
"#include <initializer_list>\n" +
"#include <fstream>\n" +
"using namespace arma;\n" +
"#ifndef _FILESTRING_CONVERSION___A\n" +
"#define _FILESTRING_CONVERSION___A\n" +
"void toFileString(std::ofstream& myfile, mat A){\n" +
" myfile << \"[\";\n" +
" for (int i = 0; i < A.n_rows; i++){\n" +
" for (int j = 0; j < A.n_cols; j++){\n" +
" myfile << A(i,j);\n" +
" if(j + 1 < A.n_cols){\n" +
" myfile << \", \";\n" +
" }\n" +
" }\n" +
" if(i + 1 < A.n_rows){\n" +
" myfile << \";\";\n" +
" }\n" +
" }\n" +
" myfile << \"]\";\n" +
"}\n" +
"void toFileString(std::ofstream& myfile, double A){\n" +
" myfile << A;\n" +
"}\n" +
"void toFileString(std::ofstream& myfile, float A){\n" +
" myfile << A;\n" +
"}\n" +
"void toFileString(std::ofstream& myfile, int A){\n" +
" myfile << A;\n" +
"}\n" +
"void toFileString(std::ofstream& myfile, bool A){\n" +
" myfile << A;\n" +
"}\n" +
"bool Is_close(mat& X, mat& Y, double tol)\n" +
"{\n" +
" // abs returns a mat type then max checks columns and returns a row_vec\n" +
" // max used again will return the biggest element in the row_vec\n" +
" bool close(false);\n" +
" if(arma::max(arma::max(arma::abs(X-Y))) < tol)\n" +
" {\n" +
" close = true;\n" +
" }\n" +
" return close;\n" +
"}\n" +
"#endif\n" +
"class HelperA{\n" +
"public:\n" +
"static mat getEigenVectors(mat A){\n" +
"vec eigenValues;\n" +
"mat eigenVectors;\n" +
"eig_sym(eigenValues,eigenVectors,A);\n" +
"return eigenVectors;\n" +
"}\n" +
"static vec getEigenValues(mat A){\n" +
"vec eigenValues;\n" +
"mat eigenVectors;\n" +
"eig_sym(eigenValues,eigenVectors,A);\n" +
"return eigenValues;\n" +
"}\n" +
"\n" +
"static mat getKMeansClusters(mat A, int k){\n" +
"mat clusters;\n" +
"kmeans(clusters,A.t(),k,random_subset,20,true);\n" +
"/*printf(\"cluster centroid calculation done\\n\");\n" +
"std::ofstream myfile;\n" +
" myfile.open(\"data after cluster.txt\");\n" +
" myfile << A;\n" +
" myfile.close();\n" +
"\t \n" +
"\t std::ofstream myfile2;\n" +
" myfile2.open(\"cluster centroids.txt\");\n" +
" myfile2 << clusters;\n" +
" myfile2.close();*/\n" +
"mat indexedData=getKMeansClustersIndexData(A.t(), clusters);\n" +
"\n" +
"/*std::ofstream myfile3;\n" +
" myfile3.open(\"data after index.txt\");\n" +
" myfile3 << indexedData;\n" +
" myfile3.close();\n" +
"\t */\n" +
"return indexedData;\n" +
"}\n" +
"\n" +
"static mat getKMeansClustersIndexData(mat A, mat centroids){\n" +
"\tmat result=mat(A.n_cols, 1);\n" +
"\tfor(int i=0;i<A.n_cols;++i){\n" +
"\t\tresult(i, 0) = getIndexForClusterCentroids(A, i, centroids);\n" +
"\t}\n" +
"\treturn result;\n" +
"}\n" +
"\n" +
"static int getIndexForClusterCentroids(mat A, int colIndex, mat centroids){\n" +
"\tint index=1;\n" +
"\tdouble lowestDistance=getEuclideanDistance(A, colIndex, centroids, 0);\n" +
"\tfor(int i=1;i<centroids.n_cols;++i){\n" +
"\t\tdouble curDistance=getEuclideanDistance(A, colIndex, centroids, i);\n" +
"\t\tif(curDistance<lowestDistance){\n" +
"\t\t\tlowestDistance=curDistance;\n" +
"\t\t\tindex=i+1;\n" +
"\t\t}\n" +
"\t}\n" +
"\treturn index;\n" +
"}\n" +
"\n" +
"static double getEuclideanDistance(mat A, int colIndexA, mat B, int colIndexB){\n" +
"\tdouble distance=0;\n" +
"\tfor(int i=0;i<A.n_rows;++i){\n" +
"\t\tdouble elementA=A(i,colIndexA);\n" +
"\t\tdouble elementB=B(i,colIndexB);\n" +
"\t\tdouble diff=elementA-elementB;\n" +
"\t\tdistance+=diff*diff;\n" +
"\t}\n" +
"\treturn sqrt(distance);\n" +
"}\n" +
"\n" +
"static mat getSqrtMat(mat A){\n" +
" cx_mat result=sqrtmat(A);\n" +
" return real(result);\n" +
"}\n" +
"\n" +
"static mat getSqrtMatDiag(mat A){\n" +
"for(int i=0;i<A.n_rows;++i){\n" +
" double curVal = A(i,i);\n" +
" A(i,i) = sqrt(curVal);\n" +
"}\n" +
"return A;\n" +
"}\n" +
"\n" +
"static mat invertDiagMatrix(mat A){\n" +
"for(int i=0;i<A.n_rows;++i){\n" +
" double curVal = A(i,i);\n" +
" A(i,i) = 1/curVal;\n" +
"}\n" +
"return A;\n" +
"}\n" +
"};\n" +
"#endif\n";
} }
...@@ -224,7 +224,7 @@ public class GeneratorCPP implements Generator { ...@@ -224,7 +224,7 @@ public class GeneratorCPP implements Generator {
if (MathConverter.curBackend.getBackendName().equals("OctaveBackend")) if (MathConverter.curBackend.getBackendName().equals("OctaveBackend"))
fileContents.add(OctaveHelper.getOctaveHelperFileContent()); fileContents.add(OctaveHelper.getOctaveHelperFileContent());
if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend")) if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend"))
fileContents.add(ArmadilloHelper.getArmadilloHelperFileContent()); fileContents.add(ArmadilloHelper.getArmadilloHelperFileContent(isGenerateTests));
if (shouldGenerateMainClass()) { if (shouldGenerateMainClass()) {
//fileContents.add(getMainClassFileContent(componentInstanceSymbol, fileContents.get(0))); //fileContents.add(getMainClassFileContent(componentInstanceSymbol, fileContents.get(0)));
...@@ -235,7 +235,7 @@ public class GeneratorCPP implements Generator { ...@@ -235,7 +235,7 @@ public class GeneratorCPP implements Generator {
if (MathConverter.curBackend.getBackendName().equals("OctaveBackend")) if (MathConverter.curBackend.getBackendName().equals("OctaveBackend"))
fileContents.add(OctaveHelper.getOctaveHelperFileContent()); fileContents.add(OctaveHelper.getOctaveHelperFileContent());
if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend")) if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend"))
fileContents.add(ArmadilloHelper.getArmadilloHelperFileContent()); fileContents.add(ArmadilloHelper.getArmadilloHelperFileContent(isGenerateTests));
if(componentInstanceSymbol instanceof EMADynamicComponentInstanceSymbol){ if(componentInstanceSymbol instanceof EMADynamicComponentInstanceSymbol){
//TODO: add Events Value Helper //TODO: add Events Value Helper
......
Markdown is supported
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