1 /*
2  * testIntegrationHyperrectanglesDriver.cpp
3  *
4  *  Created on: Jul 21, 2010
5  *      Author: bedutra
6  *
7  *  usage: exe correct-answer [p or l] [polynomial-file or linear-form-file] latte-file
8  *  checks the result from the valuation class with the passed-in correct value.
9  *  This exe is used by a maple script.
10  */
11 
12 #include <iostream>
13 #include <cstring>
14 #include "../valuation.h"
15 #include "rational.h"
16 
17 using namespace std;
18 
19 
checkPolynomial(const char * correctAns,const char * integrandFile,const char * latteFile)20 int checkPolynomial(const char *correctAns, const char * integrandFile, const char * latteFile)
21 {
22 
23 	RationalNTL correctAnswer(correctAns);
24 	Valuation::ValuationContainer valuationContainerLaw, valuationContainerTri;
25 
26 	char * options[4];
27 
28 	options[0] = (char*)"./exe";
29 	options[1] = (char*)"--valuation-alg=poly-lf-cone";
30 	options[2] = new char[strlen(integrandFile) + 13];
31 	strcpy(options[2], "--monomials=");
32 	strcat(options[2], integrandFile);
33 	options[3] = (char*)latteFile;
34 
35 
36 	cout << "Running main valuation Driver with Lawrence" << endl;
37 	valuationContainerLaw = Valuation::mainValuationDriver((const char **)options, 4);
38 
39 	options[1] = (char*)"--valuation-alg=poly-lf-triangulation";
40 	cout << "Running main valuation Driver with Triangulate" << endl;
41 	valuationContainerTri = Valuation::mainValuationDriver((const char **)options, 4);
42 
43 	delete options[2];
44 
45 	RationalNTL triAns, lawAns;
46 	for(int i = 0; i < valuationContainerLaw.answers.size(); ++i)
47 	{
48 		if (valuationContainerLaw.answers[i].valuationType == PolytopeValuation::integratePolynomialAsLinearFormCone)
49 			lawAns = valuationContainerLaw.answers[i].answer;
50 	}
51 
52 
53 	for(int i = 0; i < valuationContainerTri.answers.size(); ++i)
54 	{
55 		if (valuationContainerTri.answers[i].valuationType == PolytopeValuation::integratePolynomialAsLinearFormTriangulation)
56 			triAns = valuationContainerTri.answers[i].answer;
57 	}
58 
59 
60 
61 	if ( triAns != correctAnswer || lawAns != correctAnswer)
62 	{
63 		cout << "******ERROR*******" << endl;
64 		cout << "correct answer    = " << correctAnswer << endl;
65 		cout << "Lawrence answer   = " << lawAns << endl;
66 		cout << "Triangulate anser = " << triAns << endl;
67 		cout << "Input options:" << endl;
68 		for(int i = 0; i < 4; ++i)
69 			cout << "     " << options[i] << endl;
70 			return 2;
71 	}
72 
73 
74 	return 0;
75 }
76 
checkLinearForm(const char * correctAns,const char * integrandFile,const char * latteFile)77 int checkLinearForm(const char *correctAns, const char * integrandFile, const char * latteFile)
78 {
79 
80 	RationalNTL correctAnswer(correctAns);
81 	Valuation::ValuationContainer valuationContainerLaw, valuationContainerTri;
82 
83 	char * options[4];
84 
85 	options[0] = (char*)"./exe";
86 	options[1] = (char*)"--valuation-alg=lf-cone";
87 	options[2] = new char[strlen(integrandFile) + 16];
88 	strcpy(options[2], "--linear-forms=");
89 	strcat(options[2], integrandFile);
90 	options[3] = (char*)latteFile;
91 
92 
93 	cout << "Running main valuation Driver with Lawrence" << endl;
94 	valuationContainerLaw = Valuation::mainValuationDriver((const char **)options, 4);
95 
96 	options[1] = (char*)"--valuation-alg=lf-triangulation";
97 	cout << "Running main valuation Driver with Triangulate" << endl;
98 	valuationContainerTri = Valuation::mainValuationDriver((const char **)options, 4);
99 
100 	delete options[2];
101 
102 	RationalNTL triAns, lawAns;
103 	for(int i = 0; i < valuationContainerLaw.answers.size(); ++i)
104 	{
105 		if (valuationContainerLaw.answers[i].valuationType == PolytopeValuation::integrateLinearFormCone)
106 			lawAns = valuationContainerLaw.answers[i].answer;
107 	}
108 
109 
110 	for(int i = 0; i < valuationContainerTri.answers.size(); ++i)
111 	{
112 		if (valuationContainerTri.answers[i].valuationType == PolytopeValuation::integrateLinearFormTriangulation)
113 			triAns = valuationContainerTri.answers[i].answer;
114 	}
115 
116 
117 
118 	if ( triAns != correctAnswer || lawAns != correctAnswer)
119 	{
120 		cout << "******ERROR*******" << endl;
121 		cout << "correct answer    = " << correctAnswer << endl;
122 		cout << "Lawrence answer   = " << lawAns << endl;
123 		cout << "Triangulate anser = " << triAns << endl;
124 		cout << "Input options:" << endl;
125 		for(int i = 0; i < 4; ++i)
126 			cout << "     " << options[i] << endl;
127 			return 2;
128 	}
129 	return 0;
130 }
131 
checkProductLinearForm(const char * correctAns,const char * integrandFile,const char * latteFile)132 int checkProductLinearForm(const char *correctAns, const char * integrandFile, const char * latteFile)
133 {
134 
135 	RationalNTL correctAnswer(correctAns);
136 	Valuation::ValuationContainer valuationContainerTri;
137 
138 	char * options[4];
139 
140 	options[0] = (char*)"./exe";
141 	options[1] = (char*)"--valuation-alg=plf-triangulation";
142 	options[2] = new char[strlen(integrandFile) + 25];
143 	strcpy(options[2], "--product-linear-forms=");
144 	strcat(options[2], integrandFile);
145 	options[3] = (char*)latteFile;
146 
147 	cout << "Running main valuation Driver with Triangulate" << endl;
148 	valuationContainerTri = Valuation::mainValuationDriver((const char **)options, 4);
149 
150 	delete options[2];
151 
152 	RationalNTL triAns;
153 
154 	for(int i = 0; i < valuationContainerTri.answers.size(); ++i)
155 	{
156 		if (valuationContainerTri.answers[i].valuationType == PolytopeValuation::integrateProductLinearFormsTriangulation)
157 			triAns = valuationContainerTri.answers[i].answer;
158 	}
159 
160 
161 
162 	if ( triAns != correctAnswer)
163 	{
164 		cout << "******ERROR*******" << endl;
165 		cout << "correct answer    = " << correctAnswer << endl;
166 		cout << "Triangulate anser = " << triAns << endl;
167 		cout << "Input options:" << endl;
168 		for(int i = 0; i < 4; ++i)
169 			cout << "     " << options[i] << endl;
170 			return 2;
171 	}
172 	return 0;
173 }
174 
main(int argc,char * argv[])175 int main(int argc, char *argv[])
176 {
177 	if ( argc != 5)
178 	{
179 		cout << "Usage: " << argv[0] << " correct-answer [p or l or d] [polynomial-file or linear-form-file or product-linear-form-file] latte-file\n";
180 		cout << "the correct-answer could be a fraction, but should not have spaces.\n\n";
181 		cout << "Description: Tests that integrating a polynomial or power of a linear form or a product of linear forms over the polytope gives the same answer as what is passed in." << endl;
182 		return 1;
183 	}
184 
185 
186 	if ( argv[2][0] == 'p')
187 		return checkPolynomial(argv[1], argv[3], argv[4]); //checkPolynomial(const char *correctAns, const char * integrandFile, const char * latteFile);
188 	else if (argv[2][0] == 'l')
189 		return checkLinearForm(argv[1], argv[3], argv[4]);
190 	else if ( argv[2][0] == 'd')
191 		return checkProductLinearForm(argv[1], argv[3], argv[4]);
192 	else
193 		THROW_LATTE(LattException::ue_BadCommandLineOption);
194 
195 }
196 //main()
197