float eval_branch(struct node *node, float x, float y, float z, float a) { switch (node->token) { case token_x: return x; case token_y: return y; case token_z: return z; case token_a: return a; case token_num: return node->value; case token_pow: return func_pow(eval_branch(node->left, x, y, z, a), node->value); case token_mul: return func_mul(eval_branch(node->left, x, y, z, a), eval_branch(node->right, x, y, z, a)); case token_div: return func_div(eval_branch(node->left, x, y, z, a), eval_branch(node->right, x, y, z, a)); case token_add: return func_add(eval_branch(node->left, x, y, z, a), eval_branch(node->right, x, y, z, a)); case token_sub: return func_sub(eval_branch(node->left, x, y, z, a), eval_branch(node->right, x, y, z, a)); case token_neg: return func_neg(0, eval_branch(node->right, x, y, z, a)); case token_sqrt: return func_sqrt(0, eval_branch(node->right, x, y, z, a)); case token_sin: return func_sin(0, eval_branch(node->right, x, y, z, a)); case token_cos: return func_cos(0, eval_branch(node->right, x, y, z, a)); default: return 0; } }
func_t *func_diff_sqrt_pow1(func_t *f, int var) { func_t *fx=NULL,*g=NULL,*gx=NULL; if(!func_is(f,"sqrt") || func_power(f)!=1){ FUNC_ERROR_ARG1("func_diff_sqrt",f); } g=f->a[0]; gx=func_diff(FR(g),func_var1(var,1)); fx=func_mul(func_div(gx,func_sqrt(FR(g))),func_bigint_int(1,2)); f=func_del(f); return fx; }
func_t *func_diff_log_pow1(func_t *f, int var) { func_t *fx=NULL,*g=NULL,*gx=NULL; if(!func_is(f,"log") || func_power(f)!=1){ FUNC_ERROR_ARG1("func_diff_log",f); } g=f->a[0]; gx=func_diff(FR(g),func_var1(var,1)); fx=func_div(gx,FR(g)); f=func_del(f); return fx; }
func_t *func_diff_atanh_pow1(func_t *f, int var) { func_t *fx=NULL,*g=NULL,*gx=NULL,*h=NULL; if(!func_is(f,"atanh") || func_power(f)!=1){ FUNC_ERROR_ARG1("func_diff_atanh",f); } g=f->a[0]; gx=func_diff(FR(g),func_var1(var,1)); h=func_pow_n(FR(g),2); h=func_sub(func_one(),h); fx=func_div(gx,h); f=func_del(f); return fx; }
func_t *func_diff_acosh_pow1(func_t *f, int var) { func_t *fx=NULL,*g=NULL,*gx=NULL,*h=NULL; if(!func_is(f,"acosh") || func_power(f)!=1){ FUNC_ERROR_ARG1("func_diff_acosh",f); } g=f->a[0]; gx=func_diff(FR(g),func_var1(var,1)); h=func_pow_n(FR(g),2); h=func_add(h,func_bigint_int(-1,1)); h=func_sqrt(h); fx=func_div(gx,h); f=func_del(f); return fx; }
func_t *func_diff_pow(func_t *f, int var) { func_t *fx=NULL,*g=NULL,*h=NULL,*gx=NULL,*hx=NULL; if(!func_is(f,"pow")){ FUNC_ERROR_ARG1("func_diff_pow",f); } g=f->a[0]; h=f->a[1]; gx=func_diff(FR(g),func_var1(var,1)); hx=func_diff(FR(h),func_var1(var,1)); hx=func_mul(hx,func_log(FR(g))); gx=func_mul(gx,FR(h)); gx=func_div(gx,FR(g)); gx=func_add(gx,hx); fx=func_mul(FR(f),gx); f=func_del(f); return fx; }
// r=mod(f,g), where f=g*q+r func_t *func_poly_div_r(func_t *f, func_t *g) { func_t *r=NULL,*neg=NULL,*a; if(func_is_poly(f) && func_is_poly(g)){ neg=func_bigint_int(-1,1); r=FR(f); // r=f while(func_is_poly(r) && func_poly_can_div(r,g)) { a=func_div(FR(func_poly_get_lt(r)), FR(func_poly_get_lt(g))); // a=LT(r)/LT(g) a=func_mul(FR(neg),a); // a=(-1)*LT(r)/LT(g) a=func_expand(func_mul(a,FR(g))); // a=((-1)*LT(r)/LT(g))*g r=func_add(r,a); // r-=(LT(r)/LT(g))*g } } f=func_del(f); g=func_del(g); neg=func_del(neg); return r; }
func_t *func_poly_list_div_r(func_t *f, func_t *g) { int i,flag; func_t *p=NULL,*r=NULL,*neg=NULL,*a=NULL; if(!(func_is_poly(f) && func_is_poly_list(g))){ FUNC_ERROR_ARG2("func_poly_list_div_r",f,g); } neg=func_bigint_int(-1,1); p=func_clone(FR(f)); r=func_zero(); while(!func_is_zero(p)){ i=0; flag=0; while(i<func_asize(g) && flag==0){ if(func_poly_can_div(p,g->a[i])){ a=func_div(FR(func_poly_get_lt(p)),FR(func_poly_get_lt(g->a[i]))); // a:=LT(p)/LT(g[i]) a=func_mul(FR(neg),a); // a:=-LT(p)/LT(g[i]) a=func_expand(func_mul(a,func_poly_rm_lt(FR(g->a[i])))); // a:=expand((-LT(p)/LT(g[i]))*(g[i]-LT(g[i]))) p=func_poly_rm_lt(p); // p:=p-LT(p) p=func_add(p,FR(a)); // p:=(p-LT(p))-(LT(p)/LT(g[i]))*(g[i]-LT(g[i])))=p-(LT(p)/LT(g[i]))*g[i] a=func_del(a); flag=1; }else{ i=i+1; } } if(flag==0){ r=func_add(r,FR(func_poly_get_lt(p))); p=func_poly_rm_lt(p); } } p=func_del(p); f=func_del(f); g=func_del(g); neg=func_del(neg); a=func_del(a); return r; }