-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.cpp
110 lines (102 loc) · 2.31 KB
/
Function.cpp
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
//
// Copyright © 2017 Lennart Oymanns. All rights reserved.
//
#include <algorithm>
#include <set>
#include "Function.hpp"
#include "Number.hpp"
#include "UserFunction.hpp"
using namespace Equation;
void Function::Eval(NodePtr *base, std::shared_ptr<State> state, bool numeric) {
for (auto &e : args) {
e->Eval(&e, state, false);
}
if (state && state->IsFunction(fname)) {
auto result = state->EvalFunction(fname, args, numeric);
if (result) {
*base = result;
return;
}
if (!numeric) {
return;
}
for (auto &e : args) {
e->Eval(&e, state, true);
}
result = state->EvalFunction(fname, args, true);
if (result) {
*base = result;
return;
}
}
}
void Function::writeTreeToStream(std::ostream &s, const std::string &name) {
std::string type = "fu";
s << name << "[label=<" << type << ", " << fname << ">]\n";
int i = 0;
for (auto it = args.begin(); it != args.end(); it++, i++) {
std::stringstream ss;
ss << name << "f" << i;
s << name << " -> " << ss.str() << ";\n";
}
i = 0;
for (auto it = args.begin(); it != args.end(); it++, i++) {
std::stringstream ss;
ss << name << "f" << i;
(*it)->writeTreeToStream(s, ss.str());
}
}
void Function::ToStream(std::ostream &s) {
s << fname;
s << "(";
int i = 0;
for (auto &a : args) {
if (i != 0) {
s << ", ";
}
a->ToStream(s);
i += 1;
}
s << ")";
}
void Function::ToLatex(std::ostream &s) {
static std::set<std::string> funcs = { "exp", "sin", "cos", "tan",
"log", "sinh", "cosh", "tanh" };
auto isl = funcs.find(fname);
if (isl != funcs.end()) {
s << "\\";
}
s << fname;
s << "\\left(";
int i = 0;
for (auto &a : args) {
if (i != 0) {
s << ", ";
}
a->ToLatex(s);
i += 1;
}
s << "\\right)";
}
bool Function::equals(NodePtr n) const {
if (n->Type() != Node::Type_t::Function) {
return false;
}
auto un = std::static_pointer_cast<Function>(n);
if (fname != un->fname) {
return false;
}
if (args.size() != un->args.size()) {
return false;
}
auto it1 = args.begin();
auto it2 = un->args.begin();
while (it1 != args.end()) {
if ((*it1)->equals(*it2) == false) {
return false;
}
it1++;
it2++;
}
return true;
}