Exemplo n.º 1
0
    public virtual int Degree(Variable v)
    {
        if (this._v == v)
        {
            return(Coeffs.Length - 1);
        }

        int degree = 0;

        foreach (var t in Coeffs)
        {
            int d = Poly.Degree(t, v);

            if (d > degree)
            {
                degree = d;
            }
        }

        return(degree);
    }
Exemplo n.º 2
0
    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);
    }
Exemplo n.º 3
0
        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 }));
        }
Exemplo n.º 4
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));
    }
Exemplo n.º 5
0
    internal override Algebraic SymEval(Algebraic x)
    {
        if (x is Rational)
        {
            var xr = ( Rational )x;

            if (xr.den._v is FunctionVariable &&
                (( FunctionVariable )xr.den._v).Name.Equals("exp") &&
                (( FunctionVariable )xr.den._v).Var.IsComplex())
            {
                var fv = ( FunctionVariable )xr.den._v;

                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._v).Var / Symbolic.TWO, (( FunctionVariable )xr.den._v).AlgLambda);

                    Algebraic ex = new Polynomial(fv2, new Algebraic[] { Symbolic.ZERO, Symbolic.ZERO, Symbolic.ONE });

                    var xr1 = xr.nom.Value(xr.den._v, ex) / xr.den.Value(xr.den._v, ex);

                    return(SymEval(xr1));
                }
            }
        }

        if (x is Polynomial && (( Polynomial )x)._v is FunctionVariable)
        {
            var xp = ( Polynomial )x;

            Algebraic xf = null;

            var fvar = ( FunctionVariable )xp._v;

            if (fvar.Name.Equals("exp"))
            {
                var re = fvar.Var.RealPart();
                var im = fvar.Var.ImagPart();

                if (im != Symbolic.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) * Symbolic.IONE;

                    xf = a * (_minus ? b - c : b + c);
                }
            }

            if (fvar.Name.Equals("log"))
            {
                var arg = fvar.Var;

                Algebraic factor = Symbolic.ONE, sum = Symbolic.ZERO;

                if (arg is Polynomial &&
                    (( Polynomial )arg).Degree() == 1 &&
                    (( Polynomial )arg)._v is FunctionVariable &&
                    (( Polynomial )arg)[0].Equals(Symbolic.ZERO) &&
                    (( FunctionVariable )(( Polynomial )arg)._v).Name.Equals("sqrt"))
                {
                    sum = FunctionVariable.Create("log", (( Polynomial )arg)[1]);

                    factor = new Complex(0.5);

                    arg = (( FunctionVariable )(( Polynomial )arg)._v).Var;

                    xf = FunctionVariable.Create("log", arg);
                }

                try
                {
                    var re = arg.RealPart();
                    var im = arg.ImagPart();

                    if (im != Symbolic.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) / Symbolic.TWO;

                        var b1 = SymEval(re / im);

                        var b = FunctionVariable.Create("atan", b1) * Symbolic.IONE;

                        xf = min_im ? a + b : a - b;

                        var pi2 = Symbolic.PI * Symbolic.IONE / Symbolic.TWO;

                        xf = min_im ? xf - pi2 : xf + pi2;
                    }
                }
                catch (JasymcaException)
                {
                }

                if (xf != null)
                {
                    xf = xf * factor + sum;
                }
            }

            if (xf == null)
            {
                return(x.Map(this));
            }

            Algebraic r = Symbolic.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));
    }