Example #1
0
    internal override Algebraic f_exakt(Algebraic x)
    {
        if (x.Equals(Zahl.ZERO))
        {
            return(Zahl.ONE);
        }

        if (x is Polynomial &&
            ((Polynomial)x).degree() == 1 &&
            ((Polynomial)x).a[0].Equals(Zahl.ZERO))
        {
            Polynomial xp = (Polynomial)x;

            if (xp.v is SimpleVariable && ((SimpleVariable)xp.v).name.Equals("pi"))
            {
                Algebraic q = xp.a[1].div(Zahl.IONE);
                if (q is Zahl)
                {
                    return(fzexakt((Zahl)q));
                }
            }
            if (xp.a[1] is Zahl &&
                xp.v is FunctionVariable &&
                ((FunctionVariable)xp.v).fname.Equals("log"))
            {
                if (((Zahl)xp.a[1]).integerq())
                {
                    int n = ((Zahl)xp.a[1]).intval();

                    return(((FunctionVariable)xp.v).arg.pow_n(n));
                }
            }
        }
        return(null);
    }
Example #2
0
    public virtual Matrix invert()
    {
        Algebraic _det = det();

        if (_det.Equals(Zahl.ZERO))
        {
            throw new JasymcaException("Matrix not invertible.");
        }
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: Algebraic[][] b = new Algebraic[a.Length][a.Length];
        Algebraic[][] b = RectangularArrays.ReturnRectangularAlgebraicArray(a.Length, a.Length);
        if (a.Length == 1)
        {
            b[0][0] = Zahl.ONE.div(_det);
        }
        else
        {
            for (int i = 0; i < a.Length; i++)
            {
                for (int k = 0; k < a[0].Length; k++)
                {
                    b[i][k] = unterdet(k, i).div(_det);
                }
            }
        }
        return(new Matrix(b));
    }
Example #3
0
 public override Algebraic mult(Algebraic x)
 {
     if (x.Equals(Zahl.ZERO))
     {
         return(x);
     }
     if (x is Zahl)
     {
         return(new Exponential(a[1].mult(x), a[0].mult(x), expvar, exp_b));
     }
     if (x is Exponential && expvar.Equals(((Exponential)x).expvar))
     {
         Exponential xp  = (Exponential)x;
         Algebraic   r   = Zahl.ZERO;
         Algebraic   nex = exp_b.add(xp.exp_b);
         if (nex.Equals(Zahl.ZERO))
         {
             r = a[1].mult(xp.a[1]);
         }
         else
         {
             r = new Exponential(a[1].mult(xp.a[1]), Zahl.ZERO, expvar, nex);
         }
         r = r.add(a[0].mult(xp));
         r = r.add(mult(xp.a[0]));
         r = r.reduce();
         return(r);
     }
     return(poly2exp(base.mult(x)));
 }
Example #4
0
 internal override Algebraic SymEval(Algebraic x)
 {
     if (x.Equals(Symbolic.ZERO))
     {
         return(Symbolic.ZERO);
     }
     return(null);
 }
Example #5
0
        public override Algebraic Reduce()
        {
            if (nom is Symbol)
            {
                if (nom.Equals(Symbol.ZERO))
                {
                    return(Symbol.ZERO);
                }
                return(this);
            }

            var pq = new[] { nom, den };

            pq = Exponential.reduce_exp(pq);

            if (!nom.Equals(pq[0]) || !den.Equals(pq[1]))
            {
                return((pq[0] / pq[1]).Reduce());
            }

            if (IsNumber())
            {
                var gcd = Poly.poly_gcd(den, nom);

                if (!gcd.Equals(Symbol.ONE))
                {
                    var n = Poly.polydiv(nom, gcd);
                    var d = Poly.polydiv(den, gcd);

                    if (d.Equals(Symbol.ONE))
                    {
                        return(n);
                    }
                    else if (d is Symbol)
                    {
                        return(n / d);
                    }
                    else
                    {
                        return(new Rational(n, ( Polynomial )d));
                    }
                }
            }

            return(this);
        }
Example #6
0
 internal override Algebraic f_exakt(Algebraic x)
 {
     if (x.Equals(Zahl.ZERO))
     {
         return(Zahl.ZERO);
     }
     return(null);
 }
Example #7
0
    public static Algebraic polydiv(Algebraic p1, Algebraic q1)
    {
        if (q1 is Symbolic)
        {
            return(p1 / q1);
        }

        if (p1.Equals(Symbolic.ZERO))
        {
            return(Symbolic.ZERO);
        }

        if (!(p1 is Polynomial) || !(q1 is Polynomial))
        {
            throw new JasymcaException("Polydiv is implemented for polynomials only.Got " + p1 + " / " + q1);
        }

        var p = ( Polynomial )p1;
        var q = ( Polynomial )q1;

        if (p._v.Equals(q._v))
        {
            int len = p.Degree() - q.Degree();

            if (len < 0)
            {
                throw new JasymcaException("Polydiv requires zero rest.");
            }

            var cdiv = new Algebraic[len + 1];
            var nom  = Clone(p.Coeffs);
            var den  = q[q.Coeffs.Length - 1];

            for (int i = len, k = nom.Length - 1; i >= 0; i--, k--)
            {
                cdiv[i] = polydiv(nom[k], den);
                nom[k]  = Symbolic.ZERO;

                for (int j = k - 1, l = q.Coeffs.Length - 2; j > k - q.Coeffs.Length; j--, l--)
                {
                    nom[j] = nom[j] - cdiv[i] * q[l];
                }
            }

            return(horner(p._v, cdiv));
        }
        else
        {
            var cn = new Algebraic[p.Coeffs.Length];

            for (int i = 0; i < p.Coeffs.Length; i++)
            {
                cn[i] = polydiv(p[i], q1);
            }

            return(horner(p._v, cn));
        }
    }
Example #8
0
    public static Algebraic polydiv(Algebraic p1, Algebraic q1)
    {
        if (q1 is Zahl)
        {
            return(p1.div(q1));
        }

        if (p1.Equals(Zahl.ZERO))
        {
            return(Zahl.ZERO);
        }

        if (!(p1 is Polynomial) || !(q1 is Polynomial))
        {
            throw new JasymcaException("Polydiv is implemented for polynomials only.Got " + p1 + " / " + q1);
        }

        var p = ( Polynomial )p1;
        var q = ( Polynomial )q1;

        if (p.v.Equals(q.v))
        {
            int len = p.degree() - q.degree();

            if (len < 0)
            {
                throw new JasymcaException("Polydiv requires zero rest.");
            }

            var cdiv = new Algebraic[len + 1];
            var nom  = clone(p.a);
            var den  = q.a[q.a.Length - 1];

            for (int i = len, k = nom.Length - 1; i >= 0; i--, k--)
            {
                cdiv[i] = polydiv(nom[k], den);
                nom[k]  = Zahl.ZERO;

                for (int j = k - 1, l = q.a.Length - 2; j > k - q.a.Length; j--, l--)
                {
                    nom[j] = nom[j].sub(cdiv[i].mult(q.a[l]));
                }
            }

            return(horner(p.v, cdiv));
        }
        else
        {
            var cn = new Algebraic[p.a.Length];

            for (int i = 0; i < p.a.Length; i++)
            {
                cn[i] = polydiv(p.a[i], q1);
            }

            return(horner(p.v, cn));
        }
    }
Example #9
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static void elim(Vektor expr, Vektor vars, int n) throws JasymcaException
    private static void elim(Vektor expr, Vektor vars, int n)
    {
        if (n >= expr.length())
        {
            return;
        }
        double     maxc = 0.0;
        int        iv = 0, ie = 0;
        Variable   vp = null;
        Algebraic  f  = Zahl.ONE;
        Polynomial pm = null;

        for (int i = 0; i < vars.length(); i++)
        {
            Variable v = ((Polynomial)vars.get(i)).v;
            for (int k = n; k < expr.length(); k++)
            {
                Algebraic pa = expr.get(k);
                if (pa is Polynomial)
                {
                    Polynomial p  = (Polynomial)pa;
                    Algebraic  c  = p.coefficient(v, 1);
                    double     nm = c.norm();
                    if (nm > maxc)
                    {
                        maxc = nm;
                        vp   = v;
                        ie   = k;
                        iv   = i;
                        f    = c;
                        pm   = p;
                    }
                }
            }
        }
        if (maxc == 0.0)
        {
            return;
        }
        expr.set(ie, expr.get(n));
        expr.set(n, pm);
        for (int i = n + 1; i < expr.length(); i++)
        {
            Algebraic p = expr.get(i);
            if (p is Polynomial)
            {
                Algebraic fc = ((Polynomial)p).coefficient(vp, 1);
                if (!fc.Equals(Zahl.ZERO))
                {
                    p = p.sub(pm.mult(fc.div(f)));
                }
            }
            expr.set(i, p);
        }
        elim(expr, vars, n + 1);
    }
Example #10
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public Polynomial monic() throws JasymcaException
    public virtual Polynomial monic()
    {
        Algebraic cm = a[a.Length - 1];

        if (cm.Equals(Zahl.ONE))
        {
            return(this);
        }
        if (cm.Equals(Zahl.ZERO) || (cm.depends(v)))
        {
            throw new JasymcaException("Ill conditioned polynomial: main coefficient Zero or not number");
        }
        Algebraic[] b = new Algebraic[a.Length];
        b[a.Length - 1] = Zahl.ONE;
        for (int i = 0; i < a.Length - 1; i++)
        {
            b[i] = a[i].div(cm);
        }
        return(new Polynomial(v, b));
    }
Example #11
0
 internal override Algebraic f_exakt(Algebraic x)
 {
     if (x.Equals(Zahl.ONE))
     {
         return(Zahl.ZERO);
     }
     if (x.Equals(Zahl.MINUS))
     {
         return(Zahl.PI.mult(Zahl.IONE));
     }
     if (x is Polynomial &&
         ((Polynomial)x).degree() == 1 &&
         ((Polynomial)x).a[0].Equals(Zahl.ZERO) &&
         ((Polynomial)x).v is FunctionVariable &&
         ((FunctionVariable)((Polynomial)x).v).fname.Equals("exp"))
     {
         return(((FunctionVariable)((Polynomial)x).v).arg.add(FunctionVariable.create("log", ((Polynomial)x).a[1])));
     }
     return(null);
 }
Example #12
0
 internal override Algebraic SymEval(Algebraic x)
 {
     if (x.Equals(Symbolic.ONE))
     {
         return(Symbolic.ZERO);
     }
     if (x.Equals(Symbolic.MINUS))
     {
         return(Symbolic.PI * Symbolic.IONE);
     }
     if (x is Polynomial &&
         ((Polynomial)x).Degree() == 1 &&
         ((Polynomial)x)[0].Equals(Symbolic.ZERO) &&
         ((Polynomial)x)._v is FunctionVariable &&
         ((FunctionVariable)((Polynomial)x)._v).Name.Equals("exp"))
     {
         return(((FunctionVariable)((Polynomial)x)._v).Var + FunctionVariable.Create("log", ((Polynomial)x)[1]));
     }
     return(null);
 }
Example #13
0
 public override Algebraic reduce()
 {
     if (a[1].reduce().Equals(Zahl.ZERO))
     {
         return(a[0].reduce());
     }
     if (exp_b.Equals(Zahl.ZERO))
     {
         return(a[0].add(a[1]).reduce());
     }
     return(this);
 }
Example #14
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public Algebraic reduce() throws JasymcaException
    public override Algebraic reduce()
    {
        if (nom is Zahl)
        {
            if (nom.Equals(Zahl.ZERO))
            {
                return(Zahl.ZERO);
            }
            return(this);
        }
        Algebraic[] pq = new Algebraic[] { nom, den };
        pq = Exponential.reduce_exp(pq);
        if (!nom.Equals(pq[0]) || !den.Equals(pq[1]))
        {
            return(pq[0].div(pq[1]).reduce());
        }
        if (exaktq())
        {
            Algebraic gcd = Poly.poly_gcd(den, nom);
            if (!gcd.Equals(Zahl.ONE))
            {
                Algebraic n = Poly.polydiv(nom, gcd);
                Algebraic d = Poly.polydiv(den, gcd);
                if (d.Equals(Zahl.ONE))
                {
                    return(n);
                }
                else if (d is Zahl)
                {
                    return(n.div(d));
                }
                else
                {
                    return(new Rational(n, (Polynomial)d));
                }
            }
        }
        return(this);
    }
Example #15
0
    internal override Algebraic SymEval(Algebraic x, Algebraic y)
    {
        if (x.Equals(Symbolic.ZERO))
        {
            if (y.Equals(Symbolic.ZERO))
            {
                return(Symbolic.ONE);
            }

            return(Symbolic.ZERO);
        }
        if (y is Symbolic && (( Symbolic )y).IsInteger())
        {
            return(x.Pow((( Symbolic )y).ToInt()));
        }

        return(FunctionVariable.Create("exp", FunctionVariable.Create("log", x) * y));
    }
Example #16
0
    internal override Algebraic f_exakt(Algebraic x, Algebraic y)
    {
        if (x.Equals(Zahl.ZERO))
        {
            if (y.Equals(Zahl.ZERO))
            {
                return(Zahl.ONE);
            }

            return(Zahl.ZERO);
        }
        if (y is Zahl && (( Zahl )y).integerq())
        {
            return(x.pow_n((( Zahl )y).intval()));
        }

        return(FunctionVariable.create("exp", FunctionVariable.create("log", x).mult(y)));
    }
Example #17
0
    private static void subst(Vector expr, Vector vars, int n)
    {
        if (n < 0)
        {
            return;
        }
        var pa = expr[n];

        if (pa is Polynomial)
        {
            var       p = (Polynomial)pa;
            Variable  v = null;
            Algebraic c1 = null, c0;
            for (int k = 0; k < vars.Length(); k++)
            {
                var va = ((Polynomial)vars[k])._v;
                c1 = p.coefficient(va, 1);
                if (!c1.Equals(Symbolic.ZERO))
                {
                    v = va;
                    break;
                }
            }
            if (v != null)
            {
                expr[n] = p / c1;

                var val = -p.coefficient(v, 0) / c1;

                for (int k = 0; k < n; k++)
                {
                    var ps = expr[k];
                    if (ps is Polynomial)
                    {
                        expr.set(k, ((Polynomial)ps).Value(v, val));
                    }
                }
            }
        }
        subst(expr, vars, n - 1);
    }
Example #18
0
    public static Algebraic poly_gcd(Algebraic p, Algebraic q)
    {
        if (p.Equals(Symbolic.ZERO))
        {
            return(q);
        }

        if (q.Equals(Symbolic.ZERO))
        {
            return(p);
        }

        if (p is Symbolic || q is Symbolic)
        {
            return(Symbolic.ONE);
        }

        var r = (( Polynomial )q)._v.Smaller((( Polynomial )p)._v) ? (( Polynomial )p)._v : (( Polynomial )q)._v;

        Algebraic pc = Content(p, r), qc = Content(q, r);

        var eu = Euclid(polydiv(p, pc), polydiv(q, qc), r);

        var re = polydiv(eu, Content(eu, r)) * poly_gcd(pc, qc);

        if (re is Symbolic)
        {
            return(Symbolic.ONE);
        }

        var       rp  = ( Polynomial )re;
        Algebraic res = rp;

        if (rp[rp.Degree()] is Symbolic)
        {
            res = rp / rp[rp.Degree()];
        }

        return(res);
    }
Example #19
0
    protected override Algebraic Mul(Algebraic x)
    {
        if (x.Equals(Symbolic.ZERO))
        {
            return(x);
        }

        if (x is Symbolic)
        {
            return(new Exponential(this[1] * x, this[0] * x, expvar, exp_b));
        }

        if (x is Exponential && expvar.Equals((( Exponential )x).expvar))
        {
            var xp = (Exponential)x;

            Algebraic r = Symbolic.ZERO;

            var nex = exp_b + xp.exp_b;

            if (Equals(nex, Symbolic.ZERO))
            {
                r = this[1] * xp[1];
            }
            else
            {
                r = new Exponential(this[1] * xp[1], Symbolic.ZERO, expvar, nex);
            }

            r = r + this[0] * xp;

            r = r + this * xp[0];

            r = r.Reduce();

            return(r);
        }

        return(poly2exp(this * x));
    }
Example #20
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static void subst(Vektor expr, Vektor vars, int n) throws JasymcaException
    private static void subst(Vektor expr, Vektor vars, int n)
    {
        if (n < 0)
        {
            return;
        }
        Algebraic pa = expr.get(n);

        if (pa is Polynomial)
        {
            Polynomial p = (Polynomial)pa;
            Variable   v = null;
            Algebraic  c1 = null, c0;
            for (int k = 0; k < vars.length(); k++)
            {
                Variable va = ((Polynomial)vars.get(k)).v;
                c1 = p.coefficient(va, 1);
                if (!c1.Equals(Zahl.ZERO))
                {
                    v = va;
                    break;
                }
            }
            if (v != null)
            {
                expr.set(n, p.div(c1));
                Algebraic val = p.coefficient(v, 0).mult(Zahl.MINUS).div(c1);
                for (int k = 0; k < n; k++)
                {
                    Algebraic ps = expr.get(k);
                    if (ps is Polynomial)
                    {
                        expr.set(k, ((Polynomial)ps).value(v, val));
                    }
                }
            }
        }
        subst(expr, vars, n - 1);
    }
Example #21
0
    public static Algebraic poly_gcd(Algebraic p, Algebraic q)
    {
        if (p.Equals(Zahl.ZERO))
        {
            return(q);
        }

        if (q.Equals(Zahl.ZERO))
        {
            return(p);
        }

        if (p is Zahl || q is Zahl)
        {
            return(Zahl.ONE);
        }

        var r = (( Polynomial )q).v.smaller((( Polynomial )p).v) ? (( Polynomial )p).v : (( Polynomial )q).v;

        Algebraic pc = content(p, r), qc = content(q, r);
        var       eu = euclid(polydiv(p, pc), polydiv(q, qc), r);
        var       re = polydiv(eu, content(eu, r)).mult(poly_gcd(pc, qc));

        if (re is Zahl)
        {
            return(Zahl.ONE);
        }

        var       rp  = ( Polynomial )re;
        Algebraic res = rp;

        if (rp.a[rp.degree()] is Zahl)
        {
            res = rp.div(rp.a[rp.degree()]);
        }

        return(res);
    }
Example #22
0
    internal override Algebraic SymEval(Algebraic x)
    {
        if (x.Equals(Symbolic.ZERO))
        {
            return(Symbolic.ONE);
        }

        if (x is Polynomial &&
            ((Polynomial)x).Degree() == 1 &&
            ((Polynomial)x)[0].Equals(Symbolic.ZERO))
        {
            Polynomial xp = (Polynomial)x;

            if (xp._v is SimpleVariable && ((SimpleVariable)xp._v).name.Equals("pi"))
            {
                Algebraic q = xp[1] / Symbolic.IONE;

                if (q is Symbolic)
                {
                    return(fzexakt((Symbolic)q));
                }
            }
            if (xp[1] is Symbolic &&
                xp._v is FunctionVariable &&
                ((FunctionVariable)xp._v).Name.Equals("log"))
            {
                if (((Symbolic)xp[1]).IsInteger())
                {
                    int n = ((Symbolic)xp[1]).ToInt();

                    return(((FunctionVariable)xp._v).Var.Pow(n));
                }
            }
        }
        return(null);
    }
Example #23
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public Vektor solvepoly() throws JasymcaException
    public virtual Vektor solvepoly()
    {
        ArrayList s = new ArrayList();

        switch (degree())
        {
        case 0:
            break;

        case 1:
            s.Add(Zahl.MINUS.mult(a[0].div(a[1])));
            break;

        case 2:
            Algebraic p = a[1].div(a[2]);
            Algebraic q = a[0].div(a[2]);
            p = Zahl.MINUS.mult(p).div(Zahl.TWO);
            q = p.mult(p).sub(q);
            if (q.Equals(Zahl.ZERO))
            {
                s.Add(p);
                break;
            }
            q = FunctionVariable.create("sqrt", q);
            s.Add(p.add(q));
            s.Add(p.sub(q));
            break;

        default:
            int gcd = -1;
            for (int i = 1; i < a.Length; i++)
            {
                if (!a[i].Equals(Zahl.ZERO))
                {
                    if (gcd < 0)
                    {
                        gcd = i;
                    }
                    else
                    {
                        gcd = Poly.gcd(i, gcd);
                    }
                }
            }
            int deg = degree() / gcd;
            if (deg < 3)
            {
                Algebraic[] cn = new Algebraic[deg + 1];
                for (int i = 0; i < cn.Length; i++)
                {
                    cn[i] = a[i * gcd];
                }
                Polynomial pr = new Polynomial(v, cn);
                Vektor     sn = pr.solvepoly();
                if (gcd == 2)
                {
                    cn = new Algebraic[sn.length() * 2];
                    for (int i = 0; i < sn.length(); i++)
                    {
                        cn[2 * i]     = FunctionVariable.create("sqrt", sn.get(i));
                        cn[2 * i + 1] = cn[2 * i].mult(Zahl.MINUS);
                    }
                }
                else
                {
                    cn = new Algebraic[sn.length()];
                    Zahl wx = new Unexakt(1.0 / gcd);
                    for (int i = 0; i < sn.length(); i++)
                    {
                        Algebraic exp = FunctionVariable.create("log", sn.get(i));
                        cn[i] = FunctionVariable.create("exp", exp.mult(wx));
                    }
                }
                return(new Vektor(cn));
            }
            throw new JasymcaException("Can't solve expression " + this);
        }
        return(Vektor.create(s));
    }
Example #24
0
 public override bool Equals(object x)
 {
     return(x is FunctionVariable && fname.Equals((( FunctionVariable )x).fname) && arg.Equals((( FunctionVariable )x).arg));
 }
Example #25
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: Vektor solve(Vector expr, Vector x, int n) throws JasymcaException
    internal virtual Vektor 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.deriv(v);
                if (!slope.Equals(Zahl.ZERO) && slope is Zahl)
                {
                    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 JasymcaException("Expressions do not depend of Variables.");
        }
        Vektor sol = LambdaSOLVE.solve(equ, v);

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