public override int Eval(Stack st) { int narg = GetNarg(st); var dgl = GetAlgebraic(st); var y = GetVariable(st); var x = GetVariable(st); var p = Poly.Coefficient(dgl, y, 1); var q = Poly.Coefficient(dgl, y, 0); var pi = LambdaINTEGRATE.Integrate(p, x); if (pi is Rational && !pi.IsNumber()) { pi = (new LambdaRAT()).SymEval(pi); } Variable vexp = new FunctionVariable("exp", pi, new LambdaEXP()); Algebraic dn = new Polynomial(vexp); var qi = LambdaINTEGRATE.Integrate(q / dn, x); if (qi is Rational && !qi.IsNumber()) { qi = (new LambdaRAT()).SymEval(qi); } Algebraic cn = new Polynomial(new SimpleVariable("C")); var res = (qi + cn) * dn; res = (new ExpandUser()).SymEval(res); Debug("User Function expand: " + res); res = (new TrigExpand()).SymEval(res); Debug("Trigexpand: " + res); res = (new NormExp()).SymEval(res); Debug("Norm: " + res); if (res is Rational) { res = (new LambdaRAT()).SymEval(res); } res = (new TrigInverseExpand()).SymEval(res); Debug("Triginverse: " + res); res = (new SqrtExpand()).SymEval(res); st.Push(res); return(0); }
internal override Algebraic SymEval(Algebraic f) { if (gcd.Equals(Symbolic.ZERO)) { return(f); } if (f is Polynomial) { Polynomial p = (Polynomial)f; if (p._v is FunctionVariable && ((FunctionVariable)p._v).Name.Equals("exp") && Poly.Degree(((FunctionVariable)p._v).Var, @var) == 1) { Algebraic arg = ((FunctionVariable)p._v).Var; Algebraic[] new_coef = new Algebraic[2]; new_coef[1] = gcd.ToComplex(); new_coef[0] = Symbolic.ZERO; Algebraic new_arg = new Polynomial(@var, new_coef); Algebraic subst = FunctionVariable.Create("exp", new_arg); Algebraic exp = Poly.Coefficient(arg, @var, 1) / gcd; if (!(exp is Symbolic) && !((Symbolic)exp).IsInteger()) { throw new JasymcaException("Not integer exponent in exponential simplification."); } subst = subst ^ (( Symbolic )exp).ToInt(); subst = subst * FunctionVariable.Create("exp", Poly.Coefficient(arg, @var, 0)); int n = p.Coeffs.Length; Algebraic r = SymEval(p[n - 1]); for (int i = n - 2; i >= 0; i--) { r = r * subst + SymEval(p[i]); } return(r); } } return(f.Map(this)); }
public static Symbolic exp_gcd(ArrayList v, Variable x) { var gcd = Symbolic.ZERO; int k = 0; foreach (var t in v) { var a = (Algebraic)t; Algebraic c; if (Poly.Degree(a, x) == 1 && (c = Poly.Coefficient(a, x, 1)) is Symbolic) { k++; gcd = gcd.gcd(( Symbolic )c); } } return(k > 0 ? gcd : Symbolic.ONE); }
public static Vector Horowitz(Algebraic p, Polynomial q, Variable x) { if (Poly.Degree(p, x) >= Poly.Degree(q, x)) { throw new SymbolicException("Degree of p must be smaller than degree of q"); } p = p.Rat(); q = ( Polynomial )q.Rat(); var d = Poly.poly_gcd(q, q.Derive(x)); var b = Poly.polydiv(q, d); var m = b is Polynomial ? (( Polynomial )b).Degree() : 0; var n = d is Polynomial ? (( Polynomial )d).Degree() : 0; var a = new SimpleVariable[m]; var X = new Polynomial(x); Algebraic A = Symbol.ZERO; for (var i = a.Length - 1; i >= 0; i--) { a[i] = new SimpleVariable("a" + i); A = A + new Polynomial(a[i]); if (i > 0) { A = A * X; } } var c = new SimpleVariable[n]; Algebraic C = Symbol.ZERO; for (var i = c.Length - 1; i >= 0; i--) { c[i] = new SimpleVariable("c" + i); C = C + new Polynomial(c[i]); if (i > 0) { C = C * X; } } var r = Poly.polydiv(C * b * d.Derive(x), d); r = b * C.Derive(x) - r + d * A; var aik = Matrix.CreateRectangularArray <Algebraic>(m + n, m + n); Algebraic cf; var co = new Algebraic[m + n]; for (var i = 0; i < m + n; i++) { co[i] = Poly.Coefficient(p, x, i); cf = Poly.Coefficient(r, x, i); for (var k = 0; k < m; k++) { aik[i][k] = cf.Derive(a[k]); } for (var k = 0; k < n; k++) { aik[i][k + m] = cf.Derive(c[k]); } } var s = LambdaLINSOLVE.Gauss(new Matrix(aik), new Vector(co)); A = Symbol.ZERO; for (var i = m - 1; i >= 0; i--) { A = A + s[i]; if (i > 0) { A = A * X; } } C = Symbol.ZERO; for (var i = n - 1; i >= 0; i--) { C = C + s[i + m]; if (i > 0) { C = C * X; } } return(new Vector(new[] { C / d, A / b })); }