-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_series.cpp
220 lines (173 loc) · 4.48 KB
/
parallel_series.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
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
#include <iostream>
#include <cstring>
#include <cstdint>
#include <cmath>
#include "parallel_series.hpp"
static bool isChar(uint8_t key) {
return (key >= 'a' && key <= 'z' || key >= 'A' && key <= 'Z');
}
static bool isNumber(uint8_t key) {
return (key >= '0' && key <= '9');
}
ParallelSeries::ParallelSeries() : expr(""), onSeries(nullptr), onParallel(nullptr) {
reset();
}
ParallelSeries::~ParallelSeries() {}
double ParallelSeries::primary() {
getToken();
switch (lexer.token.type) {
case LP: {
double parens = solve();
if (lexer.token.type != RP)
error(") expected");
getToken();
return parens;
}
case NUMBER: {
Lexer save = lexer;
double result = strtod(lexer.token.value.c_str(), nullptr);
getToken();
if (lexer.token.type == CHARS) {
int8_t exponent = 0;
for (uint8_t i = 0; i < (sizeof(PREFIXES) / sizeof(*PREFIXES)); ++i) {
METRIC_PREFIX prefix = PREFIXES[i];
if (
strcmp(prefix.symbol, lexer.token.value.c_str()) == 0
||
strcmp(prefix.name, lexer.token.value.c_str()) == 0
) {
exponent = prefix.exponent;
break;
}
}
if (exponent != 0)
result *= pow(10, exponent);
else
error("unknown metric");
} else
lexer = save;
return result;
}
case '-': { // unary
return -primary();
}
case '+': { // unary
return primary();
}
default: {
lexer.busy = true;
error("primary expected");
return 0;
}
}
}
double ParallelSeries::solve() {
double left = primary();
getToken();
for(;!lexer.fail;) {
switch(lexer.token.type) {
case SERIES: {
if (onSeries == nullptr) {
error("onSeries = nullptr");
return 0;
}
left = onSeries(left, solve());
getToken();
break;
}
case PARALLEL: {
if (onParallel == nullptr) {
error("onParallel = nullptr");
return 0;
}
left = onParallel(left, primary());
getToken();
break;
}
default: {
lexer.busy = true;
return left;
}
}
}
return 0;
}
Star ParallelSeries::deltaStarTransform(Delta delta) {
/* also known as
mesh -> star
pi -> star
pi -> T
delta -> wye
*/
Star output = {0.0, 0.0, 0.0};
if (onSeries == nullptr || onParallel == nullptr) {
error("[deltaStarTransform] onSeries or onParallel or both are nullptr");
return output;
}
double deltaSeries = onSeries(delta.a, onSeries(delta.b, delta.c));
output.a = (delta.a * delta.c) / deltaSeries;
output.b = (delta.b * delta.c) / deltaSeries;
output.c = (delta.a * delta.b) / deltaSeries;
/* CHECK
// (a + b) = (c || (a + b))
double a_series_b = onParallel(delta.c, onSeries(delta.a, delta.b));
// (b + c) = (a || (b + c))
double b_series_c = onParallel(delta.a, onSeries(delta.b, delta.c));
// (c + a) = (b || (c + a));
double c_series_a = onParallel(delta.b, onSeries(delta.a, delta.c));
*/
return output;
}
void ParallelSeries::reset() {
lexer.index = 0;
lexer.token.type = -1;
lexer.token.value = "";
lexer.busy = false;
lexer.fail = false;
}
void ParallelSeries::getToken() {
if (lexer.busy || lexer.fail) {
lexer.busy = false;
return;
}
lexer.token.value.clear();
lexer.token.type = -1;
for (uint8_t c = expr[lexer.index]; lexer.index < expr.size(); c = expr[++lexer.index]) {
if (c == '\0' || c == '\n' || c == '\t' || c == ' ') continue;
else if(isChar(c)) {
lexer.token.type = CHARS;
for (; isChar(c); c = expr[++lexer.index])
lexer.token.value += c;
}
else if (isNumber(c)) {
lexer.token.type = NUMBER;
for (; isNumber(c); c = expr[++lexer.index])
lexer.token.value += c;
if ((c == '.' || c ==',') && isNumber(expr[lexer.index + 1])) {
// the strtod function can ignore the comma
lexer.token.value += '.'; // '.' || ','
c = expr[++lexer.index]; // next number
for (; isNumber(c); c = expr[++lexer.index])
lexer.token.value += c;
}
}
else if (c == '-' || c == SERIES || c == LP || c == RP) {
lexer.token.type = c;
++lexer.index;
}
else if ((c == '|' || c == '/') && expr[lexer.index + 1] == c) {
lexer.token.type = PARALLEL;
lexer.index += 2;
}
else {
std::string msg = "bad token: ";
msg += c;
error(msg);
}
return;
}
}
void ParallelSeries::error(std::string msg) {
lexer.fail = true;
std::cerr << "Error: " << msg << "" << std::endl;
}