internal override Algebraic SymEval(Algebraic x) { throw new SymbolicException("Usage: ATAN2(y,x)."); }
internal override Algebraic SymEval(Algebraic x) { if (x is Rational) { var xr = ( Rational )x; if (xr.den.Var is FunctionVariable && (( FunctionVariable )xr.den.Var).Name.Equals("exp") && (( FunctionVariable )xr.den.Var).Var.IsComplex()) { var fv = ( FunctionVariable )xr.den.Var; int maxdeg = Math.max(Poly.Degree(xr.nom, fv), Poly.Degree(xr.den, fv)); if (maxdeg % 2 == 0) { return(divExponential(xr.nom, fv, maxdeg / 2) / divExponential(xr.den, fv, maxdeg / 2)); } else { var fv2 = new FunctionVariable("exp", (( FunctionVariable )xr.den.Var).Var / Symbol.TWO, (( FunctionVariable )xr.den.Var).Body); Algebraic ex = new Polynomial(fv2, new Algebraic[] { Symbol.ZERO, Symbol.ZERO, Symbol.ONE }); var xr1 = xr.nom.Value(xr.den.Var, ex) / xr.den.Value(xr.den.Var, ex); return(SymEval(xr1)); } } } if (x is Polynomial && (( Polynomial )x).Var is FunctionVariable) { var xp = ( Polynomial )x; Algebraic xf = null; var fvar = ( FunctionVariable )xp.Var; if (fvar.Name.Equals("exp")) { var re = fvar.Var.RealPart(); var im = fvar.Var.ImagPart(); if (im != Symbol.ZERO) { bool _minus = minus(im); if (_minus) { im = -im; } var a = FunctionVariable.Create("exp", re); var b = FunctionVariable.Create("cos", im); var c = FunctionVariable.Create("sin", im) * Symbol.IONE; xf = a * (_minus ? b - c : b + c); } } if (fvar.Name.Equals("log")) { var arg = fvar.Var; Algebraic factor = Symbol.ONE, sum = Symbol.ZERO; if (arg is Polynomial && (( Polynomial )arg).Degree() == 1 && (( Polynomial )arg).Var is FunctionVariable && (( Polynomial )arg)[0].Equals(Symbol.ZERO) && (( FunctionVariable )(( Polynomial )arg).Var).Name.Equals("sqrt")) { sum = FunctionVariable.Create("log", (( Polynomial )arg)[1]); factor = new Complex(0.5); arg = (( FunctionVariable )(( Polynomial )arg).Var).Var; xf = FunctionVariable.Create("log", arg); } try { var re = arg.RealPart(); var im = arg.ImagPart(); if (im != Symbol.ZERO) { bool min_im = minus(im); if (min_im) { im = -im; } var a1 = new SqrtExpand().SymEval(arg * arg.Conj()); var a = FunctionVariable.Create("log", a1) / Symbol.TWO; var b1 = SymEval(re / im); var b = FunctionVariable.Create("atan", b1) * Symbol.IONE; xf = min_im ? a + b : a - b; var pi2 = Symbol.PI * Symbol.IONE / Symbol.TWO; xf = min_im ? xf - pi2 : xf + pi2; } } catch (SymbolicException) { } if (xf != null) { xf = xf * factor + sum; } } if (xf == null) { return(x.Map(this)); } Algebraic r = Symbol.ZERO; for (int i = xp.Coeffs.Length - 1; i > 0; i--) { r = (r + SymEval(xp[i])) * xf; } if (xp.Coeffs.Length > 0) { r = r + SymEval(xp[0]); } return(r); } return(x.Map(this)); }
internal override Algebraic SymEval(Algebraic x) { return(Equals(x, Symbol.ZERO) ? Symbol.ZERO : null); }
public FunctionVariable(string fname, Algebraic fvar, LambdaAlgebraic flambda) { Name = fname; Var = fvar; Body = flambda; }
internal override Algebraic SymEval(Algebraic x) { if (!(x is Polynomial)) { return(x.Map(this)); } var xp = ( Polynomial )x; var item = xp.Var; if (item is Root) { var cr = (( Root )item).poly; if (cr.Length() == xp.Degree() + 1) { var xr = new Algebraic[xp.Degree() + 1]; Algebraic ratio = null; for (int i = xr.Length - 1; i >= 0; i--) { xr[i] = xp[i].Map(this); if (i == xr.Length - 1) { ratio = xr[i]; } else if (i > 0 && ratio != null) { if (!Equals(cr[i] * ratio, xr[i])) { ratio = null; } } } if (ratio != null) { return(xr[0] - ratio * cr[0]); } else { return(new Polynomial(item, xr)); } } } Algebraic xf = null; if (item is FunctionVariable && (( FunctionVariable )item).Name.Equals("sqrt") && (( FunctionVariable )item).Var is Polynomial) { var arg = ( Polynomial )(( FunctionVariable )item).Var; var sqfr = arg.square_free_dec(arg.Var); var issquare = !(sqfr.Length > 0 && !sqfr[0].Equals(arg[arg.Coeffs.Length - 1])); for (int i = 2; i < sqfr.Length && issquare; i++) { if ((i + 1) % 2 == 1 && !sqfr[i].Equals(Symbol.ONE)) { issquare = false; } } if (issquare) { xf = Symbol.ONE; for (int i = 1; i < sqfr.Length; i += 2) { if (!sqfr[i].Equals(Symbol.ZERO)) { xf = xf * sqfr[i] ^ ((i + 1) / 2); } } Algebraic r = Symbol.ZERO; for (int i = xp.Coeffs.Length - 1; i > 0; i--) { r = (r + SymEval(xp[i])) * xf; } if (xp.Coeffs.Length > 0) { r = r + SymEval(xp[0]); } return(r); } } if (item is FunctionVariable && (( FunctionVariable )item).Name.Equals("sqrt") && xp.Degree() > 1) { xf = (( FunctionVariable )item).Var; var sq = new Polynomial(item); var r = SymEval(xp[0]); Algebraic xv = Symbol.ONE; for (int i = 1; i < xp.Coeffs.Length; i++) { if (i % 2 == 1) { r = r + SymEval(xp[i]) * xv * sq; } else { xv = xv * xf; r = r + SymEval(xp[i]) * xv; } } return(r); } return(x.Map(this)); }
public CollectExp(Algebraic f) { v = new ArrayList(); new GetExpVars(v).SymEval(f); }
internal override Algebraic SymEval(Algebraic f) { if (f is Rational) { var r = ( Rational )f; var nom = SymEval(r.nom); var den = SymEval(r.den); if (den is Symbol) { return(SymEval(nom / den)); } if (den is Exponential && (( Polynomial )den)[0].Equals(Symbol.ZERO) && (( Polynomial )den)[1] is Symbol) { if (nom is Symbol || nom is Polynomial) { var denx = ( Exponential )den; var den_inv = new Exponential(Symbol.ONE / denx[1], Symbol.ZERO, denx.expvar, -denx.exp_b); return(nom * den_inv); } } f = nom / den; return(f); } if (f is Exponential) { return(f.Map(this)); } if (!(f is Polynomial)) { return(f.Map(this)); } var fp = ( Polynomial )f; if (!(fp.Var is FunctionVariable) || !(( FunctionVariable )fp.Var).Name.Equals("exp")) { return(f.Map(this)); } var arg = (( FunctionVariable )fp.Var).Var.Reduce(); if (arg is Symbol) { return(fp.value(FunctionVariable.Create("exp", arg)).Map(this)); } if (!(arg is Polynomial) || (( Polynomial )arg).Degree() != 1) { return(f.Map(this)); } Algebraic z = Symbol.ZERO; var a = (( Polynomial )arg)[1]; for (int i = 1; i < fp.Coeffs.Length; i++) { var b = (( Polynomial )arg)[0]; Symbol I = new Complex(( double )i); Algebraic ebi = Symbol.ONE; while (b is Polynomial && (( Polynomial )b).Degree() == 1) { var p = ( Polynomial )b; var f1 = FunctionVariable.Create("exp", new Polynomial(p.Var) * p[1] * I); f1 = Exponential.poly2exp(f1); ebi = ebi * f1; b = (( Polynomial )b)[0]; } ebi = ebi * FunctionVariable.Create("exp", b * I); var cf = SymEval(fp[i] * ebi); var f2 = FunctionVariable.Create("exp", new Polynomial((( Polynomial )arg).Var) * a * I); f2 = Exponential.poly2exp(f2); z = z + cf * f2; } if (fp.Coeffs.Length > 0) { z = z + SymEval(fp[0]); } return(Exponential.poly2exp(z)); }
public virtual Algebraic rational(Algebraic expr) { return((ratsubst(expr) / gcd / new Polynomial(t)).Reduce()); }
public Exponential(Polynomial x) : base(x.Var, x.Coeffs) { expvar = (( Polynomial )(( FunctionVariable )Var).Var).Var; exp_b = (( Polynomial )(( FunctionVariable )Var).Var)[1]; }
internal virtual Algebraic SymEval(Algebraic ax, Algebraic ay) { return(null); }