Example #1
0
        internal override Algebraic SymEval(Algebraic x1)
        {
            if (v.Count == 0)
            {
                return(x1);
            }

            if (!(x1 is Exponential))
            {
                return(x1.Map(this));
            }

            var e = ( Exponential )x1;

            int exp = 1;

            var exp_b = e.exp_b;

            if (exp_b is Symbol && ( Symbol )exp_b < Symbol.ZERO)
            {
                exp  *= -1;
                exp_b = -exp_b;
            }

            var x = e.expvar;

            foreach (Polynomial y in v)
            {
                if (y.Var.Equals(x))
                {
                    var rat = exp_b / y[1];

                    if (rat is Symbol && !(( Symbol )rat).IsComplex())
                    {
                        int _cfs = cfs((( Symbol )rat).ToComplex().Re);

                        if (_cfs != 0 && _cfs != 1)
                        {
                            exp  *= _cfs;
                            exp_b = exp_b / new Complex(_cfs);
                        }
                    }
                }
            }

            var p = new Polynomial(x) * exp_b;

            p = FunctionVariable.Create("exp", p).Pow(exp);

            return(p * SymEval(e[1]) + SymEval(e[0]));
        }
Example #2
0
        internal override Algebraic SymEval(Algebraic f)
        {
            if (f is Exponential)
            {
                var x  = ( Exponential )f;
                var cn = new Algebraic[2];

                cn[0] = SymEval(x[0]);
                cn[1] = SymEval(x[1]);

                return(new Polynomial(x.Var, cn));
            }

            return(f.Map(this));
        }
Example #3
0
        internal override Algebraic SymEval(Algebraic f)
        {
            if (gcd.Equals(Symbol.ZERO))
            {
                return(f);
            }
            if (f is Polynomial)
            {
                Polynomial p = ( Polynomial )f;

                if (p.Var is FunctionVariable && (( FunctionVariable )p.Var).Name.Equals("exp") && Poly.Degree((( FunctionVariable )p.Var).Var, vr) == 1)
                {
                    var arg = (( FunctionVariable )p.Var).Var;

                    var new_coef = new Algebraic[2];

                    new_coef[1] = gcd.ToComplex();
                    new_coef[0] = Symbol.ZERO;

                    Algebraic new_arg = new Polynomial(vr, new_coef);

                    var subst = FunctionVariable.Create("exp", new_arg);
                    var exp   = Poly.Coefficient(arg, vr, 1) / gcd;

                    if (!(exp is Symbol) && !(( Symbol )exp).IsInteger())
                    {
                        throw new SymbolicException("Not integer exponent in exponential simplification.");
                    }

                    subst = subst ^ (( Symbol )exp).ToInt();

                    subst = subst * FunctionVariable.Create("exp", Poly.Coefficient(arg, vr, 0));

                    var len = p.Coeffs.Length;

                    var r = SymEval(p[len - 1]);

                    for (var n = len - 2; n >= 0; n--)
                    {
                        r = r * subst + SymEval(p[n]);
                    }

                    return(r);
                }
            }

            return(f.Map(this));
        }
Example #4
0
        internal override Algebraic SymEval(Algebraic f)
        {
            if (f is Exponential)
            {
                Algebraic x = new Polynomial((( Exponential )f).expvar);

                x = x * (( Exponential )f).exp_b;

                v.Add(x);

                // TODO: Check this
                SymEval((( Exponential )f)[1]);
                SymEval((( Exponential )f)[0]);

                return(Symbol.ONE);
            }

            return(f.Map(this));
        }
Example #5
0
        internal override Algebraic SymEval(Algebraic x)
        {
            if (x is Polynomial && (( Polynomial )x).Var is FunctionVariable)
            {
                var xp = ( Polynomial )x;

                var f = ( FunctionVariable )xp.Var;

                var la = Session.Proc.Store.GetValue(f.Name);

                if (la is LambdaAlgebraic && (( LambdaAlgebraic )la).trigrule != null)
                {
                    try
                    {
                        var rule = (( LambdaAlgebraic )la).trigrule;

                        var fexp = evalx(rule, f.Var);

                        Algebraic r = Symbol.ZERO;

                        for (int i = xp.Coeffs.Length - 1; i > 0; i--)
                        {
                            r = (r + SymEval(xp[i])) * fexp;
                        }

                        if (xp.Coeffs.Length > 0)
                        {
                            r = r + SymEval(xp[0]);
                        }

                        return(r);
                    }
                    catch (Exception e)
                    {
                        throw new SymbolicException(e.ToString());
                    }
                }
            }

            return(x.Map(this));
        }
Example #6
0
        internal override Algebraic SymEval(Algebraic f)
        {
            if (f is Polynomial)
            {
                var p = ( Polynomial )f;

                if (p.Var is FunctionVariable && (( FunctionVariable )p.Var).Name.Equals("exp"))
                {
                    v.Add((( FunctionVariable )p.Var).Var);
                }

                for (var n = 0; n < p.Coeffs.Length; n++)
                {
                    SymEval(p[n]);
                }

                return(Symbol.ONE);
            }

            return(f.Map(this));
        }
Example #7
0
        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));
        }
Example #8
0
        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));
        }
Example #9
0
        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));
        }