9
#include "simodo/variable/Module_interface.h"
10
#include "simodo/variable/VariableSetWrapper.h"
11
#include "simodo/variable/matrix_operations.h"
12
#include "simodo/inout/convert/functions.h"
13
#include "simodo/bormental/DrBormental.h"
20
#define BOOST_DLL_FORCE_ALIAS_INSTANTIATION
23
#include <boost/dll/alias.hpp>
26
using namespace simodo;
27
using namespace simodo::variable;
28
using namespace simodo::inout;
32
Value _sin(Module_interface * , const VariableSetWrapper & args)
34
const Variable & ori = args[0].origin();
35
assert(ori.value().isFloat());
36
return sin(ori.value().getFloat());
39
Value _cos(Module_interface * , const VariableSetWrapper & args)
41
const Variable & ori = args[0].origin();
42
assert(ori.value().isFloat());
43
return cos(ori.value().getFloat());
46
Value _tan(Module_interface * , const VariableSetWrapper & args)
48
const Variable & ori = args[0].origin();
49
assert(ori.value().isFloat());
50
return tan(ori.value().getFloat());
53
Value _asin(Module_interface * , const VariableSetWrapper & args)
55
const Variable & ori = args[0].origin();
56
assert(ori.value().isFloat());
57
return asin(ori.value().getFloat());
60
Value _acos(Module_interface * , const VariableSetWrapper & args)
62
const Variable & ori = args[0].origin();
63
assert(ori.value().isFloat());
64
return acos(ori.value().getFloat());
67
Value _atan(Module_interface * , const VariableSetWrapper & args)
69
const Variable & ori = args[0].origin();
70
assert(ori.value().isFloat());
71
return atan(ori.value().getFloat());
74
Value _sqrt(Module_interface * , const VariableSetWrapper & args)
76
const Variable & ori = args[0].origin();
77
assert(ori.value().isFloat());
78
return sqrt(ori.value().getFloat());
81
Value _exp(Module_interface * , const VariableSetWrapper & args)
83
const Variable & ori = args[0].origin();
84
assert(ori.value().isFloat());
85
return exp(ori.value().getFloat());
88
Value _ln(Module_interface * , const VariableSetWrapper & args)
90
const Variable & ori = args[0].origin();
91
assert(ori.value().isFloat());
92
return log(ori.value().getFloat());
95
bool isZero(double d) {
99
return d <= 0.0 && 0.0 <= d;
102
Value _atan2ND(Module_interface * , const VariableSetWrapper & args)
104
const Variable & oriN = args[0].origin();
105
assert(oriN.type() == ValueType::Float);
106
double num = std::get<double>(oriN.variant());
108
const Variable & oriD = args[1].origin();
109
assert(oriD.type() == ValueType::Float);
110
double denom = std::get<double>(oriD.variant());
113
throw bormental::DrBormental("Module math atan2ND", "Denominator is zero");
116
return atan2(num, denom);
119
Value _signFloat(Module_interface * , const VariableSetWrapper & args)
121
const Variable & ori = args[0].origin();
122
assert(ori.type() == ValueType::Float);
123
return double (std::signbit(std::get<double>(ori.variant())) ? -1.0 : 1.0);
126
Value _signInt(Module_interface * , const VariableSetWrapper & args)
128
const Variable & ori = args[0].origin();
129
assert(ori.type() == ValueType::Int);
130
return int64_t (std::signbit(std::get<int64_t>(ori.variant())) ? -1 : 1);
133
Value _asinND(Module_interface * , const VariableSetWrapper & args)
135
const Variable & oriN = args[0].origin();
136
assert(oriN.type() == ValueType::Float);
137
double num = std::get<double>(oriN.variant());
139
const Variable & oriD = args[1].origin();
140
assert(oriD.type() == ValueType::Float);
141
double denom = std::get<double>(oriD.variant());
144
throw bormental::DrBormental("Module math asinND", "Denominator is zero");
147
return asin(num / denom);
150
Value _acosND(Module_interface * , const VariableSetWrapper & args)
152
const Variable & oriN = args[0].origin();
153
assert(oriN.type() == ValueType::Float);
154
double num = std::get<double>(oriN.variant());
156
const Variable & oriD = args[1].origin();
157
assert(oriD.type() == ValueType::Float);
158
double denom = std::get<double>(oriD.variant());
161
throw bormental::DrBormental("Module math acosND", "Denominator is zero");
164
return acos(num / denom);
167
Value _atanND(Module_interface * , const VariableSetWrapper & args)
169
const Variable & oriN = args[0].origin();
170
assert(oriN.type() == ValueType::Float);
171
double num = std::get<double>(oriN.variant());
173
const Variable & oriD = args[1].origin();
174
assert(oriD.type() == ValueType::Float);
175
double denom = std::get<double>(oriD.variant());
178
throw bormental::DrBormental("Module math atanND", "Denominator is zero");
181
return atan(num / denom);
184
Value _clampReal(Module_interface * , const VariableSetWrapper & args)
186
const Variable & oriL = args[0].origin();
187
assert(oriL.type() == ValueType::Float);
188
double left = std::get<double>(oriL.variant());
190
const Variable & oriV = args[1].origin();
191
assert(oriV.type() == ValueType::Float);
192
double value = std::get<double>(oriV.variant());
194
const Variable & oriR = args[2].origin();
195
assert(oriR.type() == ValueType::Float);
196
double right = std::get<double>(oriR.variant());
199
throw bormental::DrBormental("Module math clampReal", "Left boundary is greater than right one");
202
return std::clamp(value, left, right);
205
Value _clampInt(Module_interface * , const VariableSetWrapper & args)
207
const Variable & oriL = args[0].origin();
208
assert(oriL.type() == ValueType::Int);
209
int64_t left = std::get<int64_t>(oriL.variant());
211
const Variable & oriV = args[1].origin();
212
assert(oriV.type() == ValueType::Int);
213
int64_t value = std::get<int64_t>(oriV.variant());
215
const Variable & oriR = args[2].origin();
216
assert(oriR.type() == ValueType::Int);
217
int64_t right = std::get<int64_t>(oriR.variant());
220
throw bormental::DrBormental("Module math clampReal", "Left boundary is greater than right one");
223
return std::clamp(value, left, right);
226
Value _absReal(Module_interface * , const VariableSetWrapper & args)
228
const Variable & ori = args[0].origin();
229
assert(ori.type() == ValueType::Float);
230
return abs(std::get<double>(ori.variant()));
233
Value _absInt(Module_interface * , const VariableSetWrapper & args)
235
const Variable & ori = args[0].origin();
236
assert(ori.type() == ValueType::Int);
237
return abs(std::get<int64_t>(ori.variant()));
240
Value _length(Module_interface * , const VariableSetWrapper & args)
242
const Variable & vector = args[0].origin();
244
if (vector.type() == ValueType::Array)
245
return lengthVector(vector.value());
247
throw bormental::DrBormental("Module math length", "Input argument must be Array type");
254
class ModuleHost_math : public Module_interface
257
virtual version_t version() const override { return lib_version(); }
259
virtual Value instantiate(std::shared_ptr<Module_interface> module_host) override {
262
{u"sin", {ValueType::Function, Object {{
263
{u"@", ExternalFunction {module_host, _sin}},
264
{{}, ValueType::Float},
265
{u"_value_", ValueType::Float},
267
{u"cos", {ValueType::Function, Object {{
268
{u"@", ExternalFunction {{module_host}, _cos}},
269
{{}, ValueType::Float},
270
{u"_value_", ValueType::Float},
272
{u"tan", {ValueType::Function, Object {{
273
{u"@", ExternalFunction {{module_host}, _tan}},
274
{{}, ValueType::Float},
275
{u"_value_", ValueType::Float},
277
{u"asin", {ValueType::Function, Object {{
278
{u"@", ExternalFunction {{module_host}, _asin}},
279
{{}, ValueType::Float},
280
{u"_value_", ValueType::Float},
282
{u"acos", {ValueType::Function, Object {{
283
{u"@", ExternalFunction {{module_host}, _acos}},
284
{{}, ValueType::Float},
285
{u"_value_", ValueType::Float},
287
{u"atan", {ValueType::Function, Object {{
288
{u"@", ExternalFunction {{module_host}, _atan}},
289
{{}, ValueType::Float},
290
{u"_value_", ValueType::Float},
292
{u"sqrt", {ValueType::Function, Object {{
293
{u"@", ExternalFunction {{module_host}, _sqrt}},
294
{{}, ValueType::Float},
295
{u"_value_", ValueType::Float},
297
{u"exp", {ValueType::Function, Object {{
298
{u"@", ExternalFunction {{module_host}, _exp}},
299
{{}, ValueType::Float},
300
{u"_value_", ValueType::Float},
302
{u"ln", {ValueType::Function, Object {{
303
{u"@", ExternalFunction {{module_host}, _ln}},
304
{{}, ValueType::Float},
305
{u"_value_", ValueType::Float},
307
{u"atan2ND", {ValueType::Function, Object {{
308
{u"@", ExternalFunction {{module_host}, _atan2ND}},
309
{{}, ValueType::Float},
310
{u"_y_", ValueType::Float},
311
{u"_x_", ValueType::Float},
313
{u"signFloat", {ValueType::Function, Object {{
314
{u"@", ExternalFunction {{module_host}, _signFloat}},
315
{{}, ValueType::Float},
316
{u"_value_", ValueType::Float},
318
{u"signInt", {ValueType::Function, Object {{
319
{u"@", ExternalFunction {{module_host}, _signInt}},
320
{{}, ValueType::Int},
321
{u"_value_", ValueType::Int},
323
{u"asinND", {ValueType::Function, Object {{
324
{u"@", ExternalFunction {{module_host}, _asinND}},
325
{{}, ValueType::Float},
326
{u"_n_", ValueType::Float},
327
{u"_d_", ValueType::Float},
329
{u"acosND", {ValueType::Function, Object {{
330
{u"@", ExternalFunction {{module_host}, _acosND}},
331
{{}, ValueType::Float},
332
{u"_n_", ValueType::Float},
333
{u"_d_", ValueType::Float},
335
{u"atanND", {ValueType::Function, Object {{
336
{u"@", ExternalFunction {{module_host}, _atanND}},
337
{{}, ValueType::Float},
338
{u"_n_", ValueType::Float},
339
{u"_d_", ValueType::Float},
341
{u"clampReal", {ValueType::Function, Object {{
342
{u"@", ExternalFunction {{module_host}, _clampReal}},
343
{{}, ValueType::Float},
344
{u"_left_", ValueType::Float},
345
{u"_value_", ValueType::Float},
346
{u"_right_", ValueType::Float},
348
{u"clampInt", {ValueType::Function, Object {{
349
{u"@", ExternalFunction {{module_host}, _clampInt}},
350
{{}, ValueType::Int},
351
{u"_left_", ValueType::Int},
352
{u"_value_", ValueType::Int},
353
{u"_right_", ValueType::Int},
355
{u"absReal", {ValueType::Function, Object {{
356
{u"@", ExternalFunction {{module_host}, _absReal}},
357
{{}, ValueType::Float},
358
{u"_value_", ValueType::Float},
360
{u"absInt", {ValueType::Function, Object {{
361
{u"@", ExternalFunction {{module_host}, _absInt}},
362
{{}, ValueType::Int},
363
{u"_value_", ValueType::Int},
365
{u"length", {ValueType::Function, Object {{
366
{u"@", ExternalFunction {{module_host}, _length}},
368
{u"vector", ValueType::Null},
374
static std::shared_ptr<Module_interface> create() {
375
return std::make_shared<ModuleHost_math>();
380
ModuleHost_math::create,