/
symbolic_codegen_test.cc
419 lines (354 loc) · 12.8 KB
/
symbolic_codegen_test.cc
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <iostream>
#include <sstream>
#include <vector>
#include <fmt/format.h>
#include <gtest/gtest.h>
#include "drake/common/symbolic.h"
namespace drake {
namespace symbolic {
namespace {
using std::ostringstream;
using std::string;
using std::vector;
// Helper function to combine the function name @p function_name and an
// expression @p e with the proper function header and footer.
string MakeScalarFunctionCode(const string& function_name, const int n,
const string& e) {
// Note that fmtlib requires to escape "{'" and "}" using "{{" and "}}".
return fmt::format(
R"""(double {0}(const double* p) {{
return {1};
}}
typedef struct {{
/* p: input, vector */
struct {{ int size; }} p;
}} {0}_meta_t;
{0}_meta_t {0}_meta() {{ return {{{{{2}}}}}; }}
)""",
function_name, e, n);
}
// Helper function to generate expected codegen outcome for a dense matrix @p
// M. It combines the function name @p function_name, the number of input
// parameters @p in, the number of rows in M, @p rows, the number of columns in
// M, @p cols, and the expected code for the entries in M, @p expressions.
string MakeDenseMatrixFunctionCode(const string& function_name, const int in,
const int rows, const int cols,
const vector<string>& expressions) {
ostringstream oss;
// Main function f.
oss << fmt::format("void {0}(const double* p, double* m) {{\n",
function_name);
for (size_t i{0}; i < expressions.size(); ++i) {
oss << fmt::format(" m[{0}] = {1};\n", i, expressions[i]);
}
oss << "}\n";
// f_meta_t.
oss << fmt::format(
R"""(typedef struct {{
/* p: input, vector */
struct {{ int size; }} p;
/* m: output, matrix */
struct {{ int rows; int cols; }} m;
}} {0}_meta_t;
{0}_meta_t {0}_meta() {{ return {{{{{1}}}, {{{2}, {3}}}}}; }}
)""",
function_name, in, rows, cols);
return oss.str();
}
class SymbolicCodeGenTest : public ::testing::Test {
protected:
const Variable x_{"x"};
const Variable y_{"y"};
const Variable z_{"z"};
const Variable w_{"w"};
};
TEST_F(SymbolicCodeGenTest, Variable) {
EXPECT_EQ(CodeGen("f", {x_, y_}, x_), MakeScalarFunctionCode("f", 2, "p[0]"));
EXPECT_EQ(CodeGen("f", {x_, y_, z_}, z_),
MakeScalarFunctionCode("f", 3, "p[2]"));
}
TEST_F(SymbolicCodeGenTest, Constant) {
EXPECT_EQ(CodeGen("f", {}, 3.141592),
MakeScalarFunctionCode("f", 0, "3.141592"));
}
TEST_F(SymbolicCodeGenTest, Addition) {
EXPECT_EQ(CodeGen("f", {x_, y_}, 2.0 + 3.0 * x_ - 7.0 * y_),
MakeScalarFunctionCode("f", 2, "(2 + (3 * p[0]) + (-7 * p[1]))"));
}
TEST_F(SymbolicCodeGenTest, Multiplication) {
EXPECT_EQ(CodeGen("f", {x_, y_}, 2.0 * 3.0 * x_ * x_ * -7.0 * y_ * y_ * y_),
MakeScalarFunctionCode(
"f", 2, "(-42 * pow(p[0], 2.000000) * pow(p[1], 3.000000))"));
}
TEST_F(SymbolicCodeGenTest, Pow) {
EXPECT_EQ(CodeGen("f", {x_, y_}, pow(2 + x_, 3 * y_)),
MakeScalarFunctionCode("f", 2, "pow((2 + p[0]), (3 * p[1]))"));
}
TEST_F(SymbolicCodeGenTest, Division) {
EXPECT_EQ(CodeGen("f", {x_, y_}, (2 + x_) / (3 * y_)),
MakeScalarFunctionCode("f", 2, "((2 + p[0]) / (3 * p[1]))"));
}
TEST_F(SymbolicCodeGenTest, Abs) {
EXPECT_EQ(CodeGen("f", {x_, y_}, abs(2 + x_)),
MakeScalarFunctionCode("f", 2, "fabs((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Log) {
EXPECT_EQ(CodeGen("f", {x_, y_}, log(2 + x_)),
MakeScalarFunctionCode("f", 2, "log((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Exp) {
EXPECT_EQ(CodeGen("f", {x_, y_}, exp(2 + x_)),
MakeScalarFunctionCode("f", 2, "exp((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Sqrt) {
EXPECT_EQ(CodeGen("f", {x_, y_}, sqrt(2 + x_)),
MakeScalarFunctionCode("f", 2, "sqrt((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Sin) {
EXPECT_EQ(CodeGen("f", {x_, y_}, sin(2 + x_)),
MakeScalarFunctionCode("f", 2, "sin((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Cos) {
EXPECT_EQ(CodeGen("f", {x_, y_}, cos(2 + x_)),
MakeScalarFunctionCode("f", 2, "cos((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Tan) {
EXPECT_EQ(CodeGen("f", {x_, y_}, tan(2 + x_)),
MakeScalarFunctionCode("f", 2, "tan((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Asin) {
EXPECT_EQ(CodeGen("f", {x_, y_}, asin(2 + x_)),
MakeScalarFunctionCode("f", 2, "asin((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Acos) {
EXPECT_EQ(CodeGen("f", {x_, y_}, acos(2 + x_)),
MakeScalarFunctionCode("f", 2, "acos((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Atan) {
EXPECT_EQ(CodeGen("f", {x_, y_}, atan(2 + x_)),
MakeScalarFunctionCode("f", 2, "atan((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Atan2) {
EXPECT_EQ(CodeGen("f", {x_, y_}, atan2(2 + x_, 3 + y_)),
MakeScalarFunctionCode("f", 2, "atan2((2 + p[0]), (3 + p[1]))"));
}
TEST_F(SymbolicCodeGenTest, Sinh) {
EXPECT_EQ(CodeGen("f", {x_, y_}, sinh(2 + x_)),
MakeScalarFunctionCode("f", 2, "sinh((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Cosh) {
EXPECT_EQ(CodeGen("f", {x_, y_}, cosh(2 + x_)),
MakeScalarFunctionCode("f", 2, "cosh((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Tanh) {
EXPECT_EQ(CodeGen("f", {x_, y_}, tanh(2 + x_)),
MakeScalarFunctionCode("f", 2, "tanh((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Min) {
EXPECT_EQ(CodeGen("f", {x_, y_}, min(2 + x_, 3 + y_)),
MakeScalarFunctionCode("f", 2, "fmin((2 + p[0]), (3 + p[1]))"));
}
TEST_F(SymbolicCodeGenTest, Max) {
EXPECT_EQ(CodeGen("f", {x_, y_}, max(2 + x_, 3 + y_)),
MakeScalarFunctionCode("f", 2, "fmax((2 + p[0]), (3 + p[1]))"));
}
TEST_F(SymbolicCodeGenTest, Ceil) {
EXPECT_EQ(CodeGen("f", {x_, y_}, ceil(2 + x_)),
MakeScalarFunctionCode("f", 2, "ceil((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, Floor) {
EXPECT_EQ(CodeGen("f", {x_, y_}, floor(2 + x_)),
MakeScalarFunctionCode("f", 2, "floor((2 + p[0]))"));
}
TEST_F(SymbolicCodeGenTest, IfThenElse) {
const Expression e{if_then_else(x_ > y_, x_, y_)};
EXPECT_THROW(CodeGen("f", {x_, y_}, e), std::runtime_error);
}
TEST_F(SymbolicCodeGenTest, UninterpretedFunction) {
const Expression e{uninterpreted_function("uf", {x_, y_})};
EXPECT_THROW(CodeGen("f", {x_, y_}, e), std::runtime_error);
}
TEST_F(SymbolicCodeGenTest, ScalarExampleInDocumentation) {
EXPECT_EQ(CodeGen("f", {x_, y_}, 1 + sin(x_) + cos(y_)),
MakeScalarFunctionCode("f", 2, "(1 + sin(p[0]) + cos(p[1]))"));
}
TEST_F(SymbolicCodeGenTest, DenseMatrixRowMajor) {
Eigen::Matrix<symbolic::Expression, 3, 2, Eigen::RowMajor> M;
vector<string> expected;
M(0, 0) = 3 + 2 * x_ + y_;
expected.push_back("(3 + (2 * p[0]) + p[1])");
M(0, 1) = 2 * pow(x_, 2) * pow(y_, 3);
expected.push_back("(2 * pow(p[0], 2.000000) * pow(p[1], 3.000000))");
M(1, 0) = 5 + sin(x_) + cos(z_);
expected.push_back("(5 + sin(p[0]) + cos(p[2]))");
M(1, 1) = 3 * min(x_, w_);
expected.push_back("(3 * fmin(p[0], p[3]))");
M(2, 0) = (x_ + 2) / (y_ - 2);
expected.push_back("((2 + p[0]) / (-2 + p[1]))");
M(2, 1) = atan2(w_ + 3, y_ + 4);
expected.push_back("atan2((3 + p[3]), (4 + p[1]))");
EXPECT_EQ(CodeGen("f", {x_, y_, z_, w_}, M),
MakeDenseMatrixFunctionCode("f", 4 /* number of input parameters */,
3 /* number of rows */,
2 /* number of columns */, expected));
}
TEST_F(SymbolicCodeGenTest, DenseMatrixColMajor) {
Eigen::Matrix<symbolic::Expression, 3, 2, Eigen::ColMajor> M;
vector<string> expected;
M(0, 0) = 3 + 2 * x_ + y_;
expected.push_back("(3 + (2 * p[0]) + p[1])");
M(1, 0) = 5 + sin(x_) + cos(z_);
expected.push_back("(5 + sin(p[0]) + cos(p[2]))");
M(2, 0) = (x_ + 2) / (y_ - 2);
expected.push_back("((2 + p[0]) / (-2 + p[1]))");
M(0, 1) = 2 * pow(x_, 2) * pow(y_, 3);
expected.push_back("(2 * pow(p[0], 2.000000) * pow(p[1], 3.000000))");
M(1, 1) = 3 * min(x_, w_);
expected.push_back("(3 * fmin(p[0], p[3]))");
M(2, 1) = atan2(w_ + 3, y_ + 4);
expected.push_back("atan2((3 + p[3]), (4 + p[1]))");
EXPECT_EQ(CodeGen("f", {x_, y_, z_, w_}, M),
MakeDenseMatrixFunctionCode("f", 4 /* number of input parameters */,
3 /* number of rows */,
2 /* number of columns */, expected));
}
TEST_F(SymbolicCodeGenTest, DenseMatrixExampleInDocumentation) {
Eigen::Matrix<symbolic::Expression, 2, 2, Eigen::ColMajor> M;
vector<string> expected;
M(0, 0) = 1.0;
expected.push_back("1.000000");
M(1, 0) = 3 + x_ + y_;
expected.push_back("(3 + p[0] + p[1])");
M(0, 1) = 4 * y_;
expected.push_back("(4 * p[1])");
M(1, 1) = sin(x_);
expected.push_back("sin(p[0])");
EXPECT_EQ(CodeGen("f", {x_, y_}, M),
MakeDenseMatrixFunctionCode("f", 2 /* number of input parameters */,
2 /* number of rows */,
2 /* number of columns */, expected));
}
TEST_F(SymbolicCodeGenTest, SparseMatrixColMajor) {
const Variable x{"x"};
const Variable y{"y"};
const Variable z{"z"};
// | x 0 0 0 z 0|
// | 0 0 y 0 0 0|
// | 0 0 0 y 0 y|
Eigen::SparseMatrix<Expression, Eigen::ColMajor> m(3, 6);
m.insert(0, 0) = x;
m.insert(0, 4) = z;
m.insert(1, 2) = y;
m.insert(2, 3) = y;
m.insert(2, 5) = y;
m.makeCompressed();
const string generated{CodeGen("f", {x, y, z}, m)};
const string expected{
R"""(void f(const double* p, int* outer_indices, int* inner_indices, double* values) {
outer_indices[0] = 0;
outer_indices[1] = 1;
outer_indices[2] = 1;
outer_indices[3] = 2;
outer_indices[4] = 3;
outer_indices[5] = 4;
outer_indices[6] = 5;
inner_indices[0] = 0;
inner_indices[1] = 1;
inner_indices[2] = 2;
inner_indices[3] = 0;
inner_indices[4] = 2;
values[0] = p[0];
values[1] = p[1];
values[2] = p[1];
values[3] = p[2];
values[4] = p[1];
}
typedef struct {
/* p: input, vector */
struct { int size; } p;
/* m: output, matrix */
struct {
int rows;
int cols;
int non_zeros;
int outer_indices;
int inner_indices;
} m;
} f_meta_t;
f_meta_t f_meta() { return {{3}, {3, 6, 5, 7, 5}}; }
)"""};
EXPECT_EQ(generated, expected);
}
// This is the generated code (string expected) from the above
// SparseMatrixColMajor testcase. We use it in the following
// SparseMatrixColMajorExampleUsingEigenMap testcase.
void f(const double* p, int* outer_indices, int* inner_indices,
double* values) {
outer_indices[0] = 0;
outer_indices[1] = 1;
outer_indices[2] = 1;
outer_indices[3] = 2;
outer_indices[4] = 3;
outer_indices[5] = 4;
outer_indices[6] = 5;
inner_indices[0] = 0;
inner_indices[1] = 1;
inner_indices[2] = 2;
inner_indices[3] = 0;
inner_indices[4] = 2;
values[0] = p[0];
values[1] = p[1];
values[2] = p[1];
values[3] = p[2];
values[4] = p[1];
}
typedef struct {
/* p: input, vector */
struct {
int size;
} p;
/* m: output, matrix */
struct {
int rows;
int cols;
int non_zeros;
int outer_indices;
int inner_indices;
} m;
} f_meta_t;
f_meta_t f_meta() { return {{3}, {3, 6, 5, 7, 5}}; }
TEST_F(SymbolicCodeGenTest, SparseMatrixColMajorExampleUsingEigenMap) {
f_meta_t meta = f_meta();
// Checks that the meta information is correct.
EXPECT_EQ(meta.p.size, 3);
EXPECT_EQ(meta.m.rows, 3);
EXPECT_EQ(meta.m.cols, 6);
EXPECT_EQ(meta.m.non_zeros, 5);
// Prepares param, outer_indices, inner_indices, and values to call the
// generated function f.
const Eigen::Vector3d param{1 /* x */, 2 /* y */, 3 /* z */};
vector<int> outer_indices(meta.m.outer_indices);
vector<int> inner_indices(meta.m.inner_indices);
vector<double> values(meta.m.non_zeros);
// Calls f to fill the output parameters, `outer_indices`, `inner_indices`,
// and `values`.
f(param.data(), outer_indices.data(), inner_indices.data(), values.data());
// Uses `Eigen::Map` to construct a sparse matrix of double from
// `outer_indices`, `inner_indices`, and `values`.
Eigen::Map<Eigen::SparseMatrix<double, Eigen::ColMajor>> map_sp(
meta.m.rows, meta.m.cols, meta.m.non_zeros, outer_indices.data(),
inner_indices.data(), values.data());
const Eigen::SparseMatrix<double> m_double{map_sp.eval()};
// Checks that the constructed m_double is the expected one.
EXPECT_EQ(m_double.rows(), 3);
EXPECT_EQ(m_double.cols(), 6);
EXPECT_EQ(m_double.nonZeros(), 5);
EXPECT_EQ(m_double.coeff(0, 0), 1.0 /* x */);
EXPECT_EQ(m_double.coeff(0, 4), 3.0 /* z */);
EXPECT_EQ(m_double.coeff(1, 2), 2.0 /* y */);
EXPECT_EQ(m_double.coeff(2, 3), 2.0 /* y */);
EXPECT_EQ(m_double.coeff(2, 5), 2.0 /* y */);
}
} // namespace
} // namespace symbolic
} // namespace drake