Exemple #1
0
        public virtual Algebraic ratsubst(Algebraic expr)
        {
            if (gcd.Equals(Symbol.ZERO))
            {
                return(expr);
            }

            if (!expr.Depends(vr))
            {
                return(expr);
            }

            if (expr is Rational)
            {
                return(ratsubst((( Rational )expr).nom) / ratsubst((( Rational )expr).den));
            }

            if (expr is Polynomial && (( Polynomial )expr).Var is FunctionVariable)
            {
                var pex = ( Polynomial )expr;
                var vex = ( FunctionVariable )pex.Var;

                if (vex.Name.Equals("exp") && vex.Var is Polynomial &&
                    (( Polynomial )vex.Var).Var.Equals(vr) &&
                    (( Polynomial )vex.Var).Degree() == 1 &&
                    (( Polynomial )vex.Var)[0].Equals(Symbol.ZERO))
                {
                    int degree = pex.Degree();

                    var a = new Algebraic[degree + 1];

                    for (var n = 0; n <= degree; n++)
                    {
                        var cf = pex[n];

                        if (cf.Depends(vr))
                        {
                            throw new SymbolicException("Rationalize failed: 2");
                        }

                        a[n] = cf;
                    }

                    return(new Polynomial(t, a));
                }
            }

            throw new SymbolicException("Could not rationalize " + expr);
        }
Exemple #2
0
        internal static Algebraic remove_constant(Algebraic expr, Variable x)
        {
            if (!expr.Depends(x))
            {
                return(Symbol.ZERO);
            }

            if (expr is Polynomial)
            {
                (( Polynomial )expr)[0] = remove_constant((( Polynomial )expr)[0], x);

                return(expr);
            }

            if (expr is Rational)
            {
                var den = (( Rational )expr).den;
                var nom = (( Rational )expr).nom;

                if (!den.Depends(x))
                {
                    return(remove_constant(nom, x) / den);
                }

                if (nom is Polynomial)
                {
                    var a = new[] { nom, den };

                    Poly.polydiv(a, den.Var);

                    if (!a[0].Depends(x))
                    {
                        return(a[1] / den);
                    }
                }
            }

            return(expr);
        }
Exemple #3
0
        public virtual Algebraic Integrate(Algebraic va, Variable vx)
        {
            if (!va.Depends(vx))
            {
                throw new SymbolicException("Expression in function does not depend on Variable.");
            }

            if (!(va is Polynomial) || (( Polynomial )va).Degree() != 1 || !(( Polynomial )va).IsRat(vx) || intrule == null)
            {
                throw new SymbolicException("Can not integrate function ");
            }

            try
            {
                var y = evalx(intrule, va);

                return(y / (( Polynomial )va)[1]);
            }
            catch (Exception)
            {
                throw new SymbolicException("Error integrating function");
            }
        }
Exemple #4
0
        internal virtual Vector solve(ArrayList expr, ArrayList x, int n)
        {
            Algebraic equ = null;
            Variable  v = null;
            int       i, k, iv = 0, ke = 0;

            for (i = 0; i < n && equ == null; i++)
            {
                v = ( Variable )x[i];
                double norm = -1.0;
                for (k = 0; k < n; k++)
                {
                    Algebraic exp = ( Algebraic )expr[k];
                    if (exp is Rational)
                    {
                        exp = (( Rational )exp).nom;
                    }
                    Algebraic slope = exp.Derive(v);
                    if (!slope.Equals(Symbol.ZERO) && slope is Symbol)
                    {
                        double nm = slope.Norm() / exp.Norm();
                        if (nm > norm)
                        {
                            norm = nm;
                            equ  = exp;
                            ke   = k;
                            iv   = i;
                        }
                    }
                }
            }
            if (equ == null)
            {
                for (i = 0; i < n && equ == null; i++)
                {
                    v = ( Variable )x[i];
                    for (k = 0; k < n; k++)
                    {
                        Algebraic exp = ( Algebraic )expr[k];
                        if (exp is Rational)
                        {
                            exp = (( Rational )exp).nom;
                        }
                        if (exp.Depends(v))
                        {
                            equ = exp;
                            ke  = k;
                            iv  = i;
                            break;
                        }
                    }
                }
            }
            if (equ == null)
            {
                throw new SymbolicException("Expressions do not depend of Variables.");
            }
            Vector sol = LambdaSOLVE.solve(equ, v);

            expr.RemoveAt(ke);
            expr.Insert(n - 1, equ);
            x.RemoveAt(iv);
            x.Insert(n - 1, v);
            return(sol);
        }