-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_module.h
More file actions
executable file
·539 lines (478 loc) · 17 KB
/
diff_module.h
File metadata and controls
executable file
·539 lines (478 loc) · 17 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#ifndef DIFF_MODULE_H
#define DIFF_MODULE_H
/*
* diff_module.h
*
* Symbolic differentiation module (clean, non-debug).
* Provides:
* - diff_expr_ctx(ctx, expr, var): returns heap-allocated derivative string (caller frees)
* - diff_expr(expr, var): same without user-function expansion
* - diff_func(ctx, src_name, wrt, dst_name): creates derivative function in ctx
*
* This header expects to be included into tinymath.c (single TU). It uses
* parser_api.h types and tinymath's helpers: nd_* and DLex parse functions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "parser_api.h"
// AST
typedef enum { N_NUM, N_VAR, N_OP, N_FUNC } NodeType;
typedef struct Node {
NodeType type;
double num; /* N_NUM */
char *name; /* N_VAR or N_FUNC (heap-allocated; may be NULL) */
char op; /* N_OP: '+','-','*','/' */
struct Node *a, *b; /* binary for N_OP */
struct Node **args; /* heap-allocated array of Node* for N_FUNC */
int n_args;
} Node;
// Node constructors / helpers (allocate and initialize)
static Node* nd_alloc(void) {
Node *n = (Node*)calloc(1, sizeof(Node));
return n;
}
static Node* nd_num(double v) {
Node* n = nd_alloc();
n->type = N_NUM;
n->num = v;
return n;
}
static Node* nd_var(const char* s) {
Node* n = nd_alloc();
n->type = N_VAR;
n->name = strdup(s ? s : "");
return n;
}
static Node* nd_op(char op, Node* a, Node* b) {
Node* n = nd_alloc();
n->type = N_OP;
n->op = op;
n->a = a;
n->b = b;
return n;
}
// Create a function node with given name and argument list
static Node* nd_func_n(const char* f, Node** argv, int n_args) {
Node* n = nd_alloc();
n->type = N_FUNC;
n->name = strdup(f ? f : "");
if (n_args > 0) {
n->args = (Node**)malloc(sizeof(Node*) * n_args);
for (int i = 0; i < n_args; ++i) n->args[i] = argv[i];
} else {
n->args = NULL;
}
n->n_args = n_args;
return n;
}
// Convenience single-arg and two-arg factories
static Node* nd_func(const char* f, Node* x) {
Node* argv[1] = { x };
return nd_func_n(f, argv, 1);
}
static Node* nd_pow(Node* u, Node* v) {
Node* argv[2] = { u, v };
return nd_func_n("pow", argv, 2);
}
// Deep copy and free helpers
static Node* nd_copy(Node* n) {
if (!n) return NULL;
Node* r = nd_alloc();
r->type = n->type;
r->num = n->num;
r->op = n->op;
r->a = NULL; r->b = NULL;
r->name = n->name ? strdup(n->name) : NULL;
r->n_args = n->n_args;
r->args = NULL;
if (n->type == N_OP) {
r->a = nd_copy(n->a);
r->b = nd_copy(n->b);
} else if (n->type == N_FUNC) {
if (n->n_args > 0) {
r->args = (Node**)malloc(sizeof(Node*) * n->n_args);
for (int i = 0; i < n->n_args; ++i)
r->args[i] = nd_copy(n->args[i]);
}
}
return r;
}
static void nd_free(Node* n) {
if (!n) return;
if (n->type == N_OP) {
nd_free(n->a);
nd_free(n->b);
} else if (n->type == N_FUNC) {
for (int i = 0; i < n->n_args; ++i) nd_free(n->args[i]);
free(n->args);
}
free(n->name);
free(n);
}
// Tiny parser to AST (expr subset)
typedef struct { const char* s; size_t i, len; } DLex;
static void d_skip(DLex* lx){ while(lx->i<lx->len && isspace((unsigned char)lx->s[lx->i])) lx->i++; }
static int d_peek(DLex* lx){ d_skip(lx); return (lx->i<lx->len)? lx->s[lx->i] : 0; }
static int d_accept(DLex* lx, char c){ d_skip(lx); if(d_peek(lx)==c){ lx->i++; return 1; } return 0; }
static Node* d_parse_expr(DLex* lx); /* fwd */
// Fixed numeric scanner for the tiny AST parser (handles exponents correctly)
static Node* d_parse_number(DLex* lx){
d_skip(lx);
int seen_digit=0;
int seen_dot=0;
int seen_exp=0;
char buf[128];
size_t j=0;
while(lx->i<lx->len){
char c=lx->s[lx->i];
if (isdigit((unsigned char)c)) {
seen_digit = 1;
if (j < sizeof(buf)-1) buf[j++] = c;
lx->i++;
continue;
}
if ((c=='.') && !seen_dot && !seen_exp) {
seen_dot = 1;
if (j < sizeof(buf)-1) buf[j++] = c;
lx->i++;
continue;
}
if ((c=='e' || c=='E') && !seen_exp) {
seen_exp = 1;
if (j < sizeof(buf)-1) buf[j++] = c;
lx->i++;
if (lx->i < lx->len && (lx->s[lx->i] == '+' || lx->s[lx->i] == '-')) {
if (j < sizeof(buf)-1) buf[j++] = lx->s[lx->i];
lx->i++;
}
continue;
}
break;
}
if (!seen_digit) return NULL;
buf[j] = '\0';
return nd_num(strtod(buf,NULL));
}
static int d_is_ident_char(int c){ return isalnum((unsigned char)c) || c=='_'; }
static Node* d_parse_primary(DLex* lx){
d_skip(lx);
if(d_accept(lx,'(')){ Node* e=d_parse_expr(lx); d_accept(lx,')'); return e; }
if(d_accept(lx,'+')) return d_parse_primary(lx);
if(d_accept(lx,'-')) return nd_op('-', nd_num(0), d_parse_primary(lx));
Node* num=d_parse_number(lx);
if(num) return num;
if(lx->i<lx->len && (isalpha((unsigned char)lx->s[lx->i]) || lx->s[lx->i]=='_')){
char id[MAX_IDENT_LEN]; size_t j=0;
while(lx->i<lx->len && d_is_ident_char(lx->s[lx->i]) && j<sizeof(id)-1) id[j++]=lx->s[lx->i++];
id[j]='\0';
if(d_accept(lx,'(')){
Node **args = NULL;
int n = 0;
if(!d_accept(lx,')')){
do {
Node *arg = d_parse_expr(lx);
Node **tmp = realloc(args, sizeof(Node*) * (n + 1));
if (!tmp) {
for (int k = 0; k < n; ++k) nd_free(args[k]);
free(args);
return NULL;
}
args = tmp;
args[n++] = arg;
} while (d_accept(lx,','));
d_accept(lx,')');
}
if(strcmp(id,"pow")==0 && n==2) {
Node* r = nd_pow(args[0], args[1]);
free(args);
return r;
}
if(n==1) {
Node* r = nd_func(id, args[0]);
free(args);
return r;
}
if (n > 1) {
Node *r = nd_func_n(id, args, n);
free(args);
return r;
}
return nd_var(id);
}
return nd_var(id);
}
return nd_num(0);
}
static Node* d_parse_factor(DLex* lx){
Node* u=d_parse_primary(lx);
if(d_accept(lx,'^')){
Node* v=d_parse_factor(lx);
return nd_pow(u,v);
}
return u;
}
static Node* d_parse_term(DLex* lx){
Node* n=d_parse_factor(lx);
while(1){
if(d_accept(lx,'*')) n=nd_op('*', n, d_parse_factor(lx));
else if(d_accept(lx,'/')) n=nd_op('/', n, d_parse_factor(lx));
else break;
}
return n;
}
static Node* d_parse_expr(DLex* lx){
Node *n=d_parse_term(lx);
while(1){
if(d_accept(lx,'+')) n=nd_op('+', n, d_parse_term(lx));
else if(d_accept(lx,'-')) n=nd_op('-', n, d_parse_term(lx));
else break;
}
return n;
}
static int is_var(Node* n, const char* x){ return n->type==N_VAR && strcmp(n->name,x)==0; }
static int is_num(Node* n, double v){ return n->type==N_NUM && fabs(n->num - v) < 1e-15; }
// Differentiator and simplifier (uses Node API)
static Node* d_diff(Node* n, const char* x){
if(!n) return nd_num(0);
switch(n->type){
case N_NUM: return nd_num(0);
case N_VAR: return nd_num(is_var(n,x)? 1 : 0);
case N_OP: {
Node *u=n->a, *v=n->b;
switch(n->op){
case '+': return nd_op('+', d_diff(u,x), d_diff(v,x));
case '-': return nd_op('-', d_diff(u,x), d_diff(v,x));
case '*': return nd_op('+',
nd_op('*', d_diff(u,x), nd_copy(v)),
nd_op('*', nd_copy(u), d_diff(v,x)));
case '/': return nd_op('/',
nd_op('-', nd_op('*', d_diff(u,x), nd_copy(v)),
nd_op('*', nd_copy(u), d_diff(v,x))),
nd_pow(nd_copy(v), nd_num(2)));
default: return nd_num(0);
}
}
case N_FUNC: {
const char* f=n->name;
Node* u=n->args[0];
if(strcmp(f,"sin")==0) return nd_op('*', nd_func("cos", nd_copy(u)), d_diff(u,x));
if(strcmp(f,"cos")==0) return nd_op('*', nd_num(-1), nd_op('*', nd_func("sin", nd_copy(u)), d_diff(u,x)));
if(strcmp(f,"exp")==0) return nd_op('*', nd_func("exp", nd_copy(u)), d_diff(u,x));
if(strcmp(f,"log")==0 || strcmp(f,"ln")==0) return nd_op('*', nd_op('/', nd_num(1), nd_copy(u)), d_diff(u,x));
if(strcmp(f,"pow")==0){
Node* a=n->args[0]; Node* b=n->args[1];
if(b->type==N_NUM){
double k=b->num;
return nd_op('*',
nd_num(k),
nd_op('*', nd_pow(nd_copy(a), nd_num(k-1)), d_diff(a,x)));
} else {
return nd_op('*',
nd_pow(nd_copy(a), nd_copy(b)),
nd_op('+',
nd_op('*', d_diff(b,x), nd_func("log", nd_copy(a))),
nd_op('*', nd_copy(b),
nd_op('/', d_diff(a,x), nd_copy(a)))
));
}
}
return nd_num(0);
}
}
return nd_num(0);
}
// Simplifier (light)
static Node* s_simpl(Node* n){
if(!n) return n;
if(n->type==N_OP){
n->a=s_simpl(n->a); n->b=s_simpl(n->b);
Node *u=n->a, *v=n->b;
if(n->op=='+'){
if(is_num(u,0)) return v;
if(is_num(v,0)) return u;
} else if(n->op=='-'){
if(is_num(v,0)) return u;
} else if(n->op=='*'){
if(is_num(u,0) || is_num(v,0)) return nd_num(0);
if(is_num(u,1)) return v;
if(is_num(v,1)) return u;
} else if(n->op=='/'){
if(is_num(u,0)) return nd_num(0);
if(is_num(v,1)) return u;
}
} else if(n->type==N_FUNC){
for(int i=0;i<n->n_args;i++) n->args[i]=s_simpl(n->args[i]);
}
return n;
}
// Pretty-print AST -> string
static void p_buf(char** out, size_t* cap, const char* s){
size_t need=strlen(s);
size_t cur=strlen(*out);
if(cur+need+1 >= *cap){
*cap = (cur+need+64);
*out = (char*)realloc(*out, *cap);
}
memcpy(*out+cur, s, need+1);
}
static void p_node_rec(Node* n, char** out, size_t* cap){
char tmp[64];
if(!n){ p_buf(out,cap,"0"); return; }
switch(n->type){
case N_NUM:
snprintf(tmp,sizeof(tmp),"%.15g", n->num);
p_buf(out,cap,tmp);
break;
case N_VAR:
p_buf(out,cap,n->name ? n->name : "");
break;
case N_OP:
if(n->op=='+'||n->op=='-'){
p_node_rec(n->a,out,cap);
snprintf(tmp,sizeof(tmp)," %c ", n->op); p_buf(out,cap,tmp);
p_node_rec(n->b,out,cap);
} else if(n->op=='*'){
p_buf(out,cap,"("); p_node_rec(n->a,out,cap); p_buf(out,cap,")");
p_buf(out,cap,"*");
p_buf(out,cap,"("); p_node_rec(n->b,out,cap); p_buf(out,cap,")");
} else if(n->op=='/'){
p_buf(out,cap,"("); p_node_rec(n->a,out,cap); p_buf(out,cap,")");
p_buf(out,cap,"/");
p_buf(out,cap,"("); p_node_rec(n->b,out,cap); p_buf(out,cap,")");
}
break;
case N_FUNC:
if(n->name && strcmp(n->name,"pow")==0 && n->n_args==2){
p_buf(out,cap,"("); p_node_rec(n->args[0],out,cap); p_buf(out,cap,")^("); p_node_rec(n->args[1],out,cap); p_buf(out,cap,")");
} else {
p_buf(out,cap,n->name ? n->name : "");
p_buf(out,cap,"(");
for(int i=0;i<n->n_args;i++){
if(i) p_buf(out,cap,",");
p_node_rec(n->args[i],out,cap);
}
p_buf(out,cap,")");
}
break;
}
}
static char* ast_to_string(Node* n){
char* out=(char*)calloc(1,256);
size_t cap=256; out[0]='\0';
p_node_rec(n,&out,&cap);
return out;
}
/* ---------- Helpers for expanding user functions ---------- */
/* substitute_vars: walk AST n, replace any N_VAR equal to 'param' with a deep copy of repl.
Returns (possibly new) Node* for the subtree.
*/
static Node* substitute_vars(Node* n, const char* param, Node* repl) {
if (!n) return NULL;
if (n->type == N_VAR) {
if (n->name && strcmp(n->name, param) == 0) {
nd_free(n);
return nd_copy(repl);
}
return n;
} else if (n->type == N_OP) {
n->a = substitute_vars(n->a, param, repl);
n->b = substitute_vars(n->b, param, repl);
return n;
} else if (n->type == N_FUNC) {
for (int i = 0; i < n->n_args; ++i)
n->args[i] = substitute_vars(n->args[i], param, repl);
return n;
}
return n;
}
/* expand_node: recursively expand user-defined function calls using ctx.
If a function call name matches a user-defined function, parse its body,
substitute actual args for parameters, expand recursively and return the
expanded subtree (freeing the original node).
*/
static Node* expand_node(Node* n, mp_context* ctx) {
if (!n) return NULL;
if (n->type == N_OP) {
n->a = expand_node(n->a, ctx);
n->b = expand_node(n->b, ctx);
return n;
} else if (n->type == N_FUNC) {
for (int i = 0; i < n->n_args; ++i) n->args[i] = expand_node(n->args[i], ctx);
mp_func* f = lookup_func(ctx, n->name);
if (!f) return n;
DLex lx = { f->body, 0, strlen(f->body) };
Node* body_ast = d_parse_expr(&lx);
if (!body_ast) return n;
for (int i = 0; i < f->n_params; ++i) {
Node* replacement = (i < n->n_args) ? n->args[i] : nd_num(0);
body_ast = substitute_vars(body_ast, f->params[i], replacement);
}
Node* expanded = expand_node(body_ast, ctx);
for (int i = 0; i < n->n_args; ++i) nd_free(n->args[i]);
free(n->args);
free(n->name);
free(n);
return expanded;
}
return n;
}
/* ---------- Public API for diff module ---------- */
// diff_expr_ctx: Parses expr, expands user functions using ctx, differentiates, simplifies, returns string
static char* diff_expr_ctx(mp_context* ctx, const char* expr, const char* var) {
if (!expr || !var) {
return strdup("NaN");
}
DLex lx = { expr, 0, strlen(expr) };
Node* ast = d_parse_expr(&lx);
if (!ast) {
return strdup("NaN");
}
Node* expanded = expand_node(ast, ctx);
if (!expanded) {
nd_free(ast);
return strdup("NaN");
}
Node* d = d_diff(expanded, var);
if (!d) {
nd_free(expanded);
return strdup("NaN");
}
Node* simplified = s_simpl(d);
char* result = ast_to_string(simplified);
nd_free(simplified);
nd_free(expanded);
return result;
}
// Backwards-compatible wrapper: diff_expr (no ctx) behaves as before
static char* diff_expr(const char* expr, const char* var) {
if (!expr || !var) return strdup("NaN");
DLex lx = { expr, 0, strlen(expr) };
Node* ast = d_parse_expr(&lx);
if (!ast) return strdup("NaN");
Node* d = d_diff(ast, var);
if (!d) { nd_free(ast); return strdup("NaN"); }
Node* simplified = s_simpl(d);
char* result = ast_to_string(simplified);
nd_free(simplified);
nd_free(ast);
return result;
}
// Create a derivative function: dst_name(params...) = d/d(wrt) src_name(body)
static int diff_func(mp_context* ctx, const char* src_name, const char* wrt, const char* dst_name){
if (!ctx) return 0;
mp_func* f = lookup_func(ctx, src_name);
if(!f) return 0;
char* body_d = diff_expr_ctx(ctx, f->body, wrt);
if (!body_d) return 0;
char params[MAX_FUNC_PARAMS][MAX_IDENT_LEN];
for(int i=0;i<f->n_params;i++){
snprintf(params[i], MAX_IDENT_LEN, "%s", f->params[i]);
}
int ok = define_func(ctx, dst_name, params, f->n_params, body_d, strlen(body_d));
free(body_d);
return ok;
}
#endif /* DIFF_MODULE_H */