-
Notifications
You must be signed in to change notification settings - Fork 0
/
cas.h
338 lines (319 loc) · 5.91 KB
/
cas.h
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <stdbool.h>
#include <signal.h>
#ifdef INF
#undef INF
#endif
#define INF 1.0 / 0.0
const char date[] = "18/12/2023";
typedef struct term_t term_t;
struct term_t
{
double coef;
double exponent;
double division;
double func_exponent;
char variable;
int function;
term_t *t_coef;
term_t *t_exponent;
term_t *t_division;
term_t **t_multiply;
size_t t_coef_sz;
size_t t_exponent_sz;
size_t t_division_sz;
size_t t_multiply_sz;
};
typedef struct domain_t
{
double *lowbound;
double *highbound;
int *is_open_low;
int *is_open_high;
int *operation;
size_t domcount;
int *is_periodic;
double *x0;
double *period;
double *holes;
size_t holecount;
} domain_t;
enum Functions
{
SIN = 1,
COS = 2,
TAN = 3,
ASIN = 4,
ACOS = 5,
ATAN = 6,
SEC = 7,
CSC = 8,
COT = 9,
ACSC = 10,
ASEC = 11,
ACOT = 12,
SINH = 13,
COSH = 14,
TANH = 15,
SECH = 16,
CSCH = 17,
COTH = 18,
ASINH = 19,
ACOSH = 20,
ATANH = 21,
ASECH = 22,
ACSCH = 23,
ACOTH = 24,
LN = 25,
LOG = 26,
LOG2 = 27,
EXP = 28,
SQRT = 29,
GAMMA = 30, // funció Γ(x) d'Euler
ERF = 31, // funció d'error
SGN = 32, // funció de signe, definida com: -1 si x<0, 0 si x=0 i 1 si x>0
ABS = 33, // valor absolut
DELTA = 34, // funció Dirac Delta, 0 si x != 0, +inf si x=0, int R = 1
FACT = 35, // factorial, funció gamma d'Euler pels naturals
LB = 36, // logaritme binari, igual que log2
LOG10 = 37, // log10
INV = 38, // 1/x
};
const size_t FUNC_COUNT = 38;
double sin_fn(double x)
{
return sin(x);
}
double cos_fn(double x)
{
return cos(x);
}
double tan_fn(double x)
{
return tan(x);
}
double asin_fn(double x)
{
return asin(x);
}
double acos_fn(double x)
{
return acos(x);
}
double atan_fn(double x)
{
return atan(x);
}
double sec_fn(double x)
{
return 1.0 / cos(x);
}
double csc_fn(double x)
{
return 1.0 / sin(x);
}
double cot_fn(double x)
{
return 1.0 / tan(x);
}
double acsc_fn(double x)
{
return asin(1.0 / x);
}
double asec_fn(double x)
{
return acos(1.0 / x);
}
double acot_fn(double x)
{
return atan(1.0 / x);
}
double sinh_fn(double x)
{
return sinh(x);
}
double cosh_fn(double x)
{
return cosh(x);
}
double tanh_fn(double x)
{
return tanh(x);
}
double sech_fn(double x)
{
return 1.0 / cosh(x);
}
double csch_fn(double x)
{
return 1.0 / sinh(x);
}
double coth_fn(double x)
{
return 1.0 / tanh(x);
}
double asinh_fn(double x)
{
return asinh(x);
}
double acosh_fn(double x)
{
return acosh(x);
}
double atanh_fn(double x)
{
return atanh(x);
}
double asech_fn(double x)
{
return acosh(1.0 / x);
}
double acsch_fn(double x)
{
return asinh(1.0 / x);
}
double acoth_fn(double x)
{
return atanh(1.0 / x);
}
double ln_fn(double x)
{
return log(x);
}
double log10_fn(double x)
{
return log10(x);
}
double log2_fn(double x)
{
return log2(x);
}
double exp_fn(double x)
{
return exp(x);
}
double sqrt_fn(double x)
{
return sqrt(x);
}
double gamma_fn(double x)
{
return tgamma(x);
}
double erf_fn(double x)
{
return erf(x);
}
double abs_fn(double x)
{
return fabs(x);
}
double sgn_fn(double x)
{
if (x < 0.0)
return -1.0;
else if (x > 0.0)
return 1.0;
else
return 0.0;
}
double num_fn(double x)
{
return x;
}
double delta_fn(double x)
{
return ((x == 0) ? 1.0 / 0.0 : 0);
}
double factorial_fn(double x)
{
if (floor(x) != x)
{
return NAN;
}
if (x < 0)
{
if ((int)fabs(x) % 2)
{
return +INF;
}
else
{
return -INF;
}
}
double fact = 1;
if (x >= 0)
{
for (unsigned int i = 1; i <= floor(x); i++)
{
fact *= i;
}
}
return fact;
}
double inv_fn(double x)
{
return 1.0 / x;
}
struct function_table
{
char *name;
double (*fn)(double num);
};
struct function_table func_list[] = {
// noms de les funcions matemàtiques
{"", num_fn},
{"sin", sin_fn},
{"cos", cos_fn},
{"tan", tan_fn},
{"arcsin", asin_fn},
{"arccos", acos_fn},
{"arctan", atan_fn},
{"sec", sec_fn},
{"csc", csc_fn},
{"cot", cot_fn},
{"arccsc", acsc_fn},
{"arcsec", asec_fn},
{"arccot", acot_fn},
{"sinh", sinh_fn},
{"cosh", cosh_fn},
{"tanh", tanh_fn},
{"sech", sech_fn},
{"csch", csch_fn},
{"coth", coth_fn},
{"arcsinh", asinh_fn},
{"arccosh", acosh_fn},
{"arctanh", atanh_fn},
{"arcsech", asech_fn},
{"arccsch", acsch_fn},
{"arccoth", acoth_fn},
{"ln", ln_fn},
{"log", log10_fn},
{"log2", log2_fn},
{"exp", exp_fn},
{"sqrt", sqrt_fn},
{"gamma", gamma_fn},
{"erf", erf_fn},
{"sgn", sgn_fn},
{"abs", abs_fn},
{"delta", delta_fn},
{"fact", factorial_fn},
{"lb", log2_fn},
{"log10", log10_fn},
{"inv", inv_fn},
};
const char cas_version[] = "1.0";
int print_func(term_t **func, size_t termcount);
double eval_func(term_t **term, double x, size_t termcount);
term_t **add_terms(term_t *f1, term_t *f2, size_t *finalsize);
term_t **multiply_terms(term_t *f1, term_t *f2);
int is_polynomial(term_t **func, size_t termcount, bool allow_func);
term_t *create_term();
term_t *parse_input(char *func, term_t *term);
int check_if_no_func(term_t **func, size_t termcount);
term_t **simp_func(term_t **func, size_t termcount, bool simplify_division);