Exemplo n.º 1
0
    internal static Algebraic[] pqsolve(Algebraic p, Algebraic q)
    {
        var r = p * Symbolic.MINUS / Symbolic.TWO;

        var s = FunctionVariable.Create("sqrt", r * r - q);

        var result = new[] { r + s, r - s };

        return(result);
    }
Exemplo n.º 2
0
    internal static Algebraic[] pqsolve(Algebraic p, Algebraic q)
    {
        var r = p.mult(Zahl.MINUS).div(Zahl.TWO);

        var s = FunctionVariable.create("sqrt", r.mult(r).sub(q));

        var result = new[] { r.add(s), r.sub(s) };

        return(result);
    }
Exemplo n.º 3
0
    internal override Algebraic f_exakt(Algebraic x1)
    {
        if (v.Count == 0)
        {
            return(x1);
        }

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

        Exponential e = (Exponential)x1;

        int exp = 1;

        Algebraic exp_b = e.exp_b;

        if (exp_b is Zahl && ((Zahl)exp_b).smaller(Zahl.ZERO))
        {
            exp  *= -1;
            exp_b = exp_b.mult(Zahl.MINUS);
        }

        Variable x = e.expvar;

        for (int i = 0; i < v.Count; i++)
        {
            Polynomial y = (Polynomial)v[i];

            if (y.v.Equals(x))
            {
                Algebraic rat = exp_b.div(y.a[1]);

                if (rat is Zahl && !((Zahl)rat).komplexq())
                {
                    int _cfs = cfs((( Zahl )rat).unexakt().real);

                    if (_cfs != 0 && _cfs != 1)
                    {
                        exp  *= _cfs;
                        exp_b = exp_b.div(new Unexakt((double)_cfs));
                    }
                }
            }
        }

        var p = (new Polynomial(x)).mult(exp_b);

        p = FunctionVariable.create("exp", p).pow_n(exp);

        return(p.mult(f_exakt(e.a[1])).add(f_exakt(e.a[0])));
    }
Exemplo n.º 4
0
    internal override Algebraic SymEval(Algebraic x1)
    {
        if (v.Count == 0)
        {
            return(x1);
        }

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

        Exponential e = (Exponential)x1;

        int exp = 1;

        Algebraic exp_b = e.exp_b;

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

        Variable x = e.expvar;

        for (int i = 0; i < v.Count; i++)
        {
            Polynomial y = (Polynomial)v[i];

            if (y._v.Equals(x))
            {
                Algebraic rat = exp_b / y[1];

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

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

        var p = (new Polynomial(x)) * exp_b;

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

        return(p * SymEval(e[1]) + SymEval(e[0]));
    }
Exemplo n.º 5
0
 internal override Algebraic f_exakt(Algebraic x, Algebraic y)
 {
     if (y is Unexakt && !y.komplexq() && x is Unexakt && !x.komplexq())
     {
         return(new Unexakt(JMath.atan2((( Unexakt )y).real, (( Unexakt )x).real)));
     }
     if (!Zahl.ZERO.Equals(x))
     {
         return(FunctionVariable.create("atan", y.div(x)).add((FunctionVariable.create("sign", y).mult(Zahl.ONE.sub((FunctionVariable.create("sign", x)))).mult(Zahl.PI).div(Zahl.TWO))));
     }
     else
     {
         return(FunctionVariable.create("sign", y).mult(Zahl.PI).div(Zahl.TWO));
     }
 }
Exemplo n.º 6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public Algebraic map(LambdaAlgebraic f) throws JasymcaException
    public override Algebraic map(LambdaAlgebraic f)
    {
        Algebraic x = v is SimpleVariable ? new Polynomial(v): FunctionVariable.create(((FunctionVariable)v).fname, f.f_exakt(((FunctionVariable)v).arg));
        Algebraic r = Zahl.ZERO;

        for (int i = a.Length - 1; i > 0; i--)
        {
            r = r.add(f.f_exakt(a[i])).mult(x);
        }
        if (a.Length > 0)
        {
            r = r.add(f.f_exakt(a[0]));
        }
        return(r);
    }
Exemplo n.º 7
0
    public override int lambda(Stack st)
    {
        int narg = getNarg(st);
        var arg  = getAlgebraic(st);

        if (arg is Zahl)
        {
            st.Push(f(( Zahl )arg));
        }
        else
        {
            st.Push(FunctionVariable.create("factorial", arg));
        }

        return(0);
    }
Exemplo n.º 8
0
    public override int Eval(Stack st)
    {
        int narg = GetNarg(st);
        var arg  = GetAlgebraic(st);

        if (arg is Symbolic)
        {
            st.Push(PreEval(( Symbolic )arg));
        }
        else
        {
            st.Push(FunctionVariable.Create("factorial", arg));
        }

        return(0);
    }
Exemplo n.º 9
0
    internal override Algebraic SymEval(Algebraic x, Algebraic y)
    {
        if (y is Complex && !y.IsComplex() && x is Complex && !x.IsComplex())
        {
            return(new Complex(JMath.atan2((( Complex )y).Re, (( Complex )x).Re)));
        }

        if (Symbolic.ZERO != x)
        {
            return(FunctionVariable.Create("atan", y / x) + FunctionVariable.Create("sign", y) * (Symbolic.ONE - FunctionVariable.Create("sign", x)) * Symbolic.PI / Symbolic.TWO);
        }
        else
        {
            return((FunctionVariable.Create("sign", y) * Symbolic.PI) / Symbolic.TWO);
        }
    }
Exemplo n.º 10
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.º 11
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)));
    }
Exemplo n.º 12
0
    public override Algebraic Map(LambdaAlgebraic f)
    {
        var x = _v is SimpleVariable ? new Polynomial(_v): FunctionVariable.Create(((FunctionVariable)_v).Name, f.SymEval(((FunctionVariable)_v).Var));

        Algebraic r = Symbolic.ZERO;

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

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

        return(r);
    }
Exemplo n.º 13
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));
    }
Exemplo n.º 14
0
        public override Algebraic Integrate(Variable v)
        {
            if (!den.Depends(v))
            {
                return(nom.Integrate(v) / den);
            }

            var quot = den.Derive(v) / nom;

            if (quot.Derive(v).Equals(Symbol.ZERO))
            {
                return(FunctionVariable.Create("log", den) / quot);
            }

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

            Poly.polydiv(q, v);

            if (q[0] != Symbol.ZERO && nom.IsRat(v) && den.IsRat(v))
            {
                return(q[0].Integrate(v) + (q[1] / den).Integrate(v));
            }

            if (IsRat(v))
            {
                Algebraic r = Symbol.ZERO;

                var h = Horowitz(nom, den, v);

                if (h[0] is Rational)
                {
                    r = r + h[0];
                }

                if (h[1] is Rational)
                {
                    r = r + new TrigInverseExpand().SymEval((( Rational )h[1]).intrat(v));
                }

                return(r);
            }

            throw new SymbolicException("Could not integrate Function " + this);
        }
Exemplo n.º 15
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);
 }
Exemplo n.º 16
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);
 }
Exemplo n.º 17
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public int lambda(Stack st) throws ParseException, JasymcaException
    public override int lambda(Stack st)
    {
        int       narg = getNarg(st);
        Algebraic dgl  = getAlgebraic(st);
        Variable  y    = getVariable(st);
        Variable  x    = getVariable(st);
        Algebraic p    = Poly.coefficient(dgl, y, 1);
        Algebraic q    = Poly.coefficient(dgl, y, 0);
        Algebraic pi   = LambdaINTEGRATE.integrate(p, x);

        if (pi is Rational && !pi.exaktq())
        {
            pi = (new LambdaRAT()).f_exakt(pi);
        }
        Variable  vexp = new FunctionVariable("exp", pi, new LambdaEXP());
        Algebraic dn   = new Polynomial(vexp);
        Algebraic qi   = LambdaINTEGRATE.integrate(q.div(dn), x);

        if (qi is Rational && !qi.exaktq())
        {
            qi = (new LambdaRAT()).f_exakt(qi);
        }
        Algebraic cn  = new Polynomial(new SimpleVariable("C"));
        Algebraic res = qi.add(cn).mult(dn);

        res = (new ExpandUser()).f_exakt(res);
        debug("User Function expand: " + res);
        res = (new TrigExpand()).f_exakt(res);
        debug("Trigexpand: " + res);
        res = (new NormExp()).f_exakt(res);
        debug("Norm: " + res);
        if (res is Rational)
        {
            res = (new LambdaRAT()).f_exakt(res);
        }
        res = (new TrigInverseExpand()).f_exakt(res);
        debug("Triginverse: " + res);
        res = (new SqrtExpand()).f_exakt(res);
        st.Push(res);
        return(0);
    }
Exemplo n.º 18
0
    internal override Algebraic f_exakt(Algebraic f)
    {
        if (gcd.Equals(Zahl.ZERO))
        {
            return(f);
        }
        if (f is Polynomial)
        {
            Polynomial p = (Polynomial)f;
            if (p.v is FunctionVariable && ((FunctionVariable)p.v).fname.Equals("exp") && Poly.degree(((FunctionVariable)p.v).arg, @var) == 1)
            {
                Algebraic   arg      = ((FunctionVariable)p.v).arg;
                Algebraic[] new_coef = new Algebraic[2];
                new_coef[1] = gcd.unexakt();
                new_coef[0] = Zahl.ZERO;
                Algebraic new_arg = new Polynomial(@var, new_coef);
                Algebraic subst   = FunctionVariable.create("exp", new_arg);
                Algebraic exp     = Poly.coefficient(arg, @var, 1).div(gcd);
                if (!(exp is Zahl) && !((Zahl)exp).integerq())
                {
                    throw new JasymcaException("Not integer exponent in exponential simplification.");
                }
                subst = subst.pow_n(((Zahl)exp).intval());
                subst = subst.mult(FunctionVariable.create("exp", Poly.coefficient(arg, @var, 0)));
                int       n = p.a.Length;
                Algebraic r = f_exakt(p.a[n - 1]);
                for (int i = n - 2; i >= 0; i--)
                {
                    r = r.mult(subst).add(f_exakt(p.a[i]));
                }
                return(r);
            }
        }

        return(f.map(this));
    }
Exemplo n.º 19
0
        public override void Serialize(TextWriter writer)
        {
            ScriptVariable returnValue = new FunctionVariable(this.Parameters, this.Body, this.IsParams);

            writer.Write(returnValue.Serialize());
        }
Exemplo n.º 20
0
    internal override Algebraic f_exakt(Algebraic f)
    {
        if (f is Rational)
        {
            Algebraic nom = f_exakt(((Rational)f).nom);
            Algebraic den = f_exakt(((Rational)f).den);
            if (den is Zahl)
            {
                return(f_exakt(nom.div(den)));
            }
            if (den is Exponential && ((Polynomial)den).a[0].Equals(Zahl.ZERO) && ((Polynomial)den).a[1] is Zahl)
            {
                if (nom is Zahl || nom is Polynomial)
                {
                    Exponential denx    = (Exponential)den;
                    Exponential den_inv = new Exponential(Zahl.ONE.div(denx.a[1]), Zahl.ZERO, denx.expvar, denx.exp_b.mult(Zahl.MINUS));
                    return(nom.mult(den_inv));
                }
            }
            f = nom.div(den);
            return(f);
        }
        if (f is Exponential)
        {
            return(f.map(this));
        }
        if (!(f is Polynomial))
        {
            return(f.map(this));
        }
        Polynomial fp = (Polynomial)f;

        if (!(fp.v is FunctionVariable) || !((FunctionVariable)fp.v).fname.Equals("exp"))
        {
            return(f.map(this));
        }
        Algebraic arg = ((FunctionVariable)fp.v).arg.reduce();

        if (arg is Zahl)
        {
            return(fp.value(FunctionVariable.create("exp", arg)).map(this));
        }
        if (!(arg is Polynomial) || !(((Polynomial)arg).degree() == 1))
        {
            return(f.map(this));
        }
        Algebraic r = Zahl.ZERO;
        Algebraic a = ((Polynomial)arg).a[1];

        for (int i = 1; i < fp.a.Length; i++)
        {
            Algebraic b   = ((Polynomial)arg).a[0];
            Zahl      I   = new Unexakt((double)i);
            Algebraic ebi = Zahl.ONE;
            while (b is Polynomial && ((Polynomial)b).degree() == 1)
            {
                Algebraic f1 = FunctionVariable.create("exp", (new Polynomial(((Polynomial)b).v)).mult(((Polynomial)b).a[1].mult(I)));
                f1  = Exponential.poly2exp(f1);
                ebi = ebi.mult(f1);
                b   = ((Polynomial)b).a[0];
            }
            ebi = ebi.mult(FunctionVariable.create("exp", b.mult(I)));
            Algebraic cf = f_exakt(fp.a[i].mult(ebi));
            Algebraic f2 = FunctionVariable.create("exp", (new Polynomial(((Polynomial)arg).v)).mult(a.mult(I)));
            f2 = Exponential.poly2exp(f2);
            r  = r.add(cf.mult(f2));
        }
        if (fp.a.Length > 0)
        {
            r = r.add(f_exakt(fp.a[0]));
        }
        return(Exponential.poly2exp(r));
    }
Exemplo n.º 21
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));
    }
Exemplo n.º 22
0
    internal virtual Algebraic fzexakt(Zahl x)
    {
        if (x.smaller(Zahl.ZERO))
        {
            Algebraic r = fzexakt((Zahl)x.mult(Zahl.MINUS));
            if (r != null)
            {
                return(r.cc());
            }
            return(r);
        }

        if (x.integerq())
        {
            if (x.intval() % 2 == 0)
            {
                return(Zahl.ONE);
            }
            else
            {
                return(Zahl.MINUS);
            }
        }

        Algebraic qs = x.add(new Unexakt(.5));

        if (((Zahl)qs).integerq())
        {
            if (((Zahl)qs).intval() % 2 == 0)
            {
                return(Zahl.IMINUS);
            }
            else
            {
                return(Zahl.IONE);
            }
        }

        qs = x.mult(new Unexakt(4));

        if (((Zahl)qs).integerq())
        {
            Algebraic sq2 = FunctionVariable.create("sqrt", new Unexakt(0.5));
            switch (((Zahl)qs).intval() % 8)
            {
            case 1:
                return(Zahl.ONE.add(Zahl.IONE).div(Zahl.SQRT2));

            case 3:
                return(Zahl.MINUS.add(Zahl.IONE).div(Zahl.SQRT2));

            case 5:
                return(Zahl.MINUS.add(Zahl.IMINUS).div(Zahl.SQRT2));

            case 7:
                return(Zahl.ONE.add(Zahl.IMINUS).div(Zahl.SQRT2));
            }
        }
        qs = x.mult(new Unexakt(6));
        if (((Zahl)qs).integerq())
        {
            switch (((Zahl)qs).intval() % 12)
            {
            case 1:
                return(Zahl.SQRT3.add(Zahl.IONE).div(Zahl.TWO));

            case 2:
                return(Zahl.ONE.add(Zahl.SQRT3.mult(Zahl.IONE)).div(Zahl.TWO));

            case 4:
                return(Zahl.SQRT3.mult(Zahl.IONE).add(Zahl.MINUS).div(Zahl.TWO));

            case 5:
                return(Zahl.IONE.sub(Zahl.SQRT3).div(Zahl.TWO));

            case 7:
                return(Zahl.IMINUS.sub(Zahl.SQRT3).div(Zahl.TWO));

            case 8:
                return(Zahl.SQRT3.mult(Zahl.IMINUS).sub(Zahl.ONE).div(Zahl.TWO));

            case 10:
                return(Zahl.SQRT3.mult(Zahl.IMINUS).add(Zahl.ONE).div(Zahl.TWO));

            case 11:
                return(Zahl.IMINUS.add(Zahl.SQRT3).div(Zahl.TWO));
            }
        }
        return(null);
    }
Exemplo n.º 23
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public Algebraic integrate(Variable var) throws JasymcaException
    public override Algebraic integrate(Variable item)
    {
        Algebraic tmp = Zahl.ZERO;

        for (int i = 1; i < a.Length; i++)
        {
            if (!a[i].depends(item))
            {
                if (item.Equals(this.v))
                {
                    tmp = tmp.add(a[i].mult((new Polynomial(item)).pow_n(i + 1).div(new Unexakt(i + 1))));
                }
                else if (this.v is FunctionVariable && ((FunctionVariable)this.v).arg.depends(item))
                {
                    if (i == 1)
                    {
                        tmp = tmp.add(((FunctionVariable)this.v).integrate(item).mult(a[1]));
                    }
                    else
                    {
                        throw new JasymcaException("Integral not supported.");
                    }
                }
                else
                {
                    tmp = tmp.add(a[i].mult((new Polynomial(item)).mult((new Polynomial(this.v)).pow_n(i))));
                }
            }
            else if (item.Equals(this.v))
            {
                throw new JasymcaException("Integral not supported.");
            }
            else if (this.v is FunctionVariable && ((FunctionVariable)this.v).arg.depends(item))
            {
                if (i == 1 && a[i] is Polynomial && ((Polynomial)a[i]).v.Equals(item))
                {
                    debug("Trying to isolate inner derivative " + this);
                    try
                    {
                        FunctionVariable f = (FunctionVariable)this.v;
                        Algebraic        w = f.arg;
                        Algebraic        q = a[i].div(w.deriv(item));
                        if (q.deriv(item).Equals(Zahl.ZERO))
                        {
                            SimpleVariable v = new SimpleVariable("v");
                            Algebraic      p = FunctionVariable.create(f.fname, new Polynomial(v));
                            Algebraic      r = p.integrate(v).value(v, w).mult(q);
                            tmp = tmp.add(r);
                            continue;
                        }
                    }
                    catch (JasymcaException)
                    {
                    }
                    debug("Failed.");
                    for (int k = 0; k < ((Polynomial)a[i]).a.Length; k++)
                    {
                        if (((Polynomial)a[i]).a[k].depends(item))
                        {
                            throw new JasymcaException("Function not supported by this method");
                        }
                    }
                    if (loopPartial)
                    {
                        loopPartial = false;
                        debug("Partial Integration Loop detected.");
                        throw new JasymcaException("Partial Integration Loop: " + this);
                    }
                    debug("Trying partial integration: x^n*f(x) , n-times diff " + this);
                    try
                    {
                        loopPartial = true;
                        Algebraic _p = a[i];
                        Algebraic f  = ((FunctionVariable)this.v).integrate(item);
                        Algebraic r  = f.mult(_p);
                        while (!(_p = _p.deriv(item)).Equals(Zahl.ZERO))
                        {
                            f = f.integrate(item).mult(Zahl.MINUS);
                            r = r.add(f.mult(_p));
                        }
                        loopPartial = false;
                        tmp         = tmp.add(r);
                        continue;
                    }
                    catch (JasymcaException)
                    {
                        loopPartial = false;
                    }
                    debug("Failed.");
                    debug("Trying partial integration: x^n*f(x) , 1-times int " + this);
                    try
                    {
                        loopPartial = true;
                        Algebraic p1 = a[i].integrate(item);
                        Algebraic f  = new Polynomial((FunctionVariable)this.v);
                        Algebraic r  = p1.mult(f).sub(p1.mult(f.deriv(item)).integrate(item));
                        loopPartial = false;
                        tmp         = tmp.add(r);
                        continue;
                    }
                    catch (JasymcaException)
                    {
                        loopPartial = false;
                    }
                    debug("Failed");
                    throw new JasymcaException("Function not supported by this method");
                }
                else
                {
                    throw new JasymcaException("Integral not supported.");
                }
            }
            else
            {
                tmp = tmp.add(a[i].integrate(item).mult((new Polynomial(this.v)).pow_n(i)));
            }
        }
        if (a.Length > 0)
        {
            tmp = tmp.add(a[0].integrate(item));
        }
        return(tmp);
    }
Exemplo n.º 24
0
    public virtual Vector solvepoly()
    {
        var s = new ArrayList();

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

        case 1:
            s.Add(-Coeffs[0] / Coeffs[1]);
            break;

        case 2:
            var p = Coeffs[1] / Coeffs[2];
            var q = Coeffs[0] / Coeffs[2];

            p = -p / Symbolic.TWO;

            q = p * p - q;

            if (Equals(q, Symbolic.ZERO))
            {
                s.Add(p);
                break;
            }

            q = FunctionVariable.Create("sqrt", q);

            s.Add(p + q);
            s.Add(p - q);
            break;

        default:
            int gcd = -1;

            for (int i = 1; i < Coeffs.Length; i++)
            {
                if (Coeffs[i] != Symbolic.ZERO)
                {
                    gcd = gcd < 0 ? i : Poly.gcd(i, gcd);
                }
            }

            int deg = Degree() / gcd;

            if (deg < 3)
            {
                var cn = new Algebraic[deg + 1];

                for (int i = 0; i < cn.Length; i++)
                {
                    cn[i] = Coeffs[i * gcd];
                }

                var pr = new Polynomial(_v, cn);

                var 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[i]);

                        cn[2 * i + 1] = -cn[2 * i];
                    }
                }
                else
                {
                    cn = new Algebraic[sn.Length()];

                    Symbolic wx = new Complex(1.0 / gcd);

                    for (int i = 0; i < sn.Length(); i++)
                    {
                        var exp = FunctionVariable.Create("log", sn[i]);

                        cn[i] = FunctionVariable.Create("exp", exp * wx);
                    }
                }
                return(new Vector(cn));
            }
            throw new JasymcaException("Can't solve expression " + this);
        }

        return(Vector.Create(s));
    }
Exemplo n.º 25
0
    internal override Algebraic f_exakt(Algebraic x)
    {
        if (x is Rational)
        {
            var xr = ( Rational )x;

            if (xr.den.v is FunctionVariable &&
                (( FunctionVariable )xr.den.v).fname.Equals("exp") &&
                (( FunctionVariable )xr.den.v).arg.komplexq())
            {
                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).div(divExponential(xr.den, fv, maxdeg / 2)));
                }
                else
                {
                    var fv2 = new FunctionVariable("exp", (( FunctionVariable )xr.den.v).arg.div(Zahl.TWO), (( FunctionVariable )xr.den.v).la);

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

                    var xr1 = xr.nom.value(xr.den.v, ex).div(xr.den.value(xr.den.v, ex));

                    return(f_exakt(xr1));
                }
            }
        }

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

            Algebraic xf = null;

            var fvar = ( FunctionVariable )xp.v;

            if (fvar.fname.Equals("exp"))
            {
                var re = fvar.arg.realpart();
                var im = fvar.arg.imagpart();

                if (!im.Equals(Zahl.ZERO))

                {
                    bool _minus = minus(im);

                    if (_minus)
                    {
                        im = im.mult(Zahl.MINUS);
                    }

                    var a = FunctionVariable.create("exp", re);
                    var b = FunctionVariable.create("cos", im);
                    var c = FunctionVariable.create("sin", im).mult(Zahl.IONE);

                    xf = a.mult(_minus ? (b.sub(c)) : b.add(c));
                }
            }

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

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

                if (arg is Polynomial &&
                    (( Polynomial )arg).degree() == 1 &&
                    (( Polynomial )arg).v is FunctionVariable &&
                    (( Polynomial )arg).a[0].Equals(Zahl.ZERO) &&
                    (( FunctionVariable )(( Polynomial )arg).v).fname.Equals("sqrt"))
                {
                    sum = FunctionVariable.create("log", (( Polynomial )arg).a[1]);

                    factor = new Unexakt(0.5);

                    arg = (( FunctionVariable )(( Polynomial )arg).v).arg;

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

                try
                {
                    var re = arg.realpart();
                    var im = arg.imagpart();

                    if (!im.Equals(Zahl.ZERO))
                    {
                        bool min_im = minus(im);

                        if (min_im)
                        {
                            im = im.mult(Zahl.MINUS);
                        }

                        var a1 = (new SqrtExpand()).f_exakt(arg.mult(arg.cc()));
                        var a  = FunctionVariable.create("log", a1).div(Zahl.TWO);

                        var b1 = f_exakt(re.div(im));
                        var b  = FunctionVariable.create("atan", b1).mult(Zahl.IONE);

                        xf = min_im ? a.add(b) : a.sub(b);

                        var pi2 = Zahl.PI.mult(Zahl.IONE).div(Zahl.TWO);

                        xf = min_im ? xf.sub(pi2) : xf.add(pi2);
                    }
                }
                catch (JasymcaException)
                {
                }

                if (xf != null)
                {
                    xf = xf.mult(factor).add(sum);
                }
            }

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

            Algebraic r = Zahl.ZERO;

            for (int i = xp.a.Length - 1; i > 0; i--)
            {
                r = r.add(f_exakt(xp.a[i])).mult(xf);
            }

            if (xp.a.Length > 0)
            {
                r = r.add(f_exakt(xp.a[0]));
            }

            return(r);
        }

        return(x.map(this));
    }
Exemplo n.º 26
0
        internal virtual Algebraic MakeLog(Algebraic c, Variable x, Algebraic a)
        {
            var arg = new Polynomial(x) - a;

            return(FunctionVariable.Create("log", arg) * c);
        }
Exemplo n.º 27
0
    public override Algebraic Integrate(Variable v)
    {
        Algebraic tmp = Symbolic.ZERO;

        for (int i = 1; i < Coeffs.Length; i++)
        {
            if (!Coeffs[i].Depends(v))
            {
                if (v.Equals(_v))
                {
                    tmp = tmp + Coeffs[i] * (new Polynomial(v) ^ (i + 1)) / new Complex(i + 1);
                }
                else if (_v is FunctionVariable && (( FunctionVariable )_v).Var.Depends(v))
                {
                    if (i == 1)
                    {
                        tmp = tmp + (( FunctionVariable )_v).Integrate(v) * Coeffs[1];
                    }
                    else
                    {
                        throw new JasymcaException("Integral not supported.");
                    }
                }
                else
                {
                    tmp = tmp + Coeffs[i] * (new Polynomial(v) * new Polynomial(_v) ^ i);
                }
            }
            else if (v.Equals(this._v))
            {
                throw new JasymcaException("Integral not supported.");
            }
            else if (this._v is FunctionVariable && ((FunctionVariable)this._v).Var.Depends(v))
            {
                if (i == 1 && Coeffs[i] is Polynomial && ((Polynomial)Coeffs[i])._v.Equals(v))
                {
                    Debug("Trying to isolate inner derivative " + this);

                    try
                    {
                        var f = ( FunctionVariable )_v;

                        var w = f.Var;

                        var q = Coeffs[i] / w.Derive(v);

                        if (Equals(q.Derive(v), Symbolic.ZERO))
                        {
                            var sv = new SimpleVariable("v");

                            var p = FunctionVariable.Create(f.Name, new Polynomial(sv));

                            var r = p.Integrate(sv).Value(sv, w) * q;

                            tmp = tmp + r;

                            continue;
                        }
                    }
                    catch (JasymcaException)
                    {
                    }

                    Debug("Failed.");

                    if ((( Polynomial )Coeffs[i]).Coeffs.Any(t => t.Depends(v)))
                    {
                        throw new JasymcaException("Function not supported by this method");
                    }

                    if (loopPartial)
                    {
                        loopPartial = false;

                        Debug("Partial Integration Loop detected.");

                        throw new JasymcaException("Partial Integration Loop: " + this);
                    }

                    Debug("Trying partial integration: x^n*f(x) , n-times diff " + this);

                    try
                    {
                        loopPartial = true;

                        var c = Coeffs[i];

                        var fv = (( FunctionVariable )_v).Integrate(v);

                        var r = fv * c;

                        while ((c = c.Derive(v)) != Symbolic.ZERO)
                        {
                            r = r - fv.Integrate(v) * c;
                        }

                        loopPartial = false;
                        tmp         = tmp + r;

                        continue;
                    }
                    catch (JasymcaException)
                    {
                        loopPartial = false;
                    }

                    Debug("Failed.");
                    Debug("Trying partial integration: x^n*f(x) , 1-times int " + this);

                    try
                    {
                        loopPartial = true;

                        var p1 = Coeffs[i].Integrate(v);

                        Algebraic f = new Polynomial(( FunctionVariable )_v);

                        var r = p1 * f - (p1 * f.Derive(v)).Integrate(v);

                        loopPartial = false;

                        tmp = tmp + r;

                        continue;
                    }
                    catch (JasymcaException)
                    {
                        loopPartial = false;
                    }

                    Debug("Failed");

                    throw new JasymcaException("Function not supported by this method");
                }
                else
                {
                    throw new JasymcaException("Integral not supported.");
                }
            }
            else
            {
                tmp = tmp + Coeffs[i].Integrate(v) * new Polynomial(_v) ^ i;
            }
        }

        if (Coeffs.Length > 0)
        {
            tmp = tmp + Coeffs[0].Integrate(v);
        }

        return(tmp);
    }
Exemplo n.º 28
0
    internal virtual Algebraic fzexakt(Symbolic x)
    {
        if (x < Symbolic.ZERO)
        {
            var r = fzexakt(( Symbolic )(-x));

            if (r != null)
            {
                return(r.Conj());
            }
            return(r);
        }

        if (x.IsInteger())
        {
            if (x.ToInt() % 2 == 0)
            {
                return(Symbolic.ONE);
            }
            else
            {
                return(Symbolic.MINUS);
            }
        }

        var qs = x + new Complex(0.5);

        if (((Symbolic)qs).IsInteger())
        {
            if (((Symbolic)qs).ToInt() % 2 == 0)
            {
                return(Symbolic.IMINUS);
            }
            else
            {
                return(Symbolic.IONE);
            }
        }

        qs = x * new Complex(4);

        if (((Symbolic)qs).IsInteger())
        {
            Algebraic sq2 = FunctionVariable.Create("sqrt", new Complex(0.5));
            switch (((Symbolic)qs).ToInt() % 8)
            {
            case 1:
                return((Symbolic.ONE + Symbolic.IONE) / Symbolic.SQRT2);

            case 3:
                return((Symbolic.MINUS + Symbolic.IONE) / Symbolic.SQRT2);

            case 5:
                return((Symbolic.MINUS + Symbolic.IMINUS) / Symbolic.SQRT2);

            case 7:
                return((Symbolic.ONE + Symbolic.IMINUS) / Symbolic.SQRT2);
            }
        }

        qs = x * new Complex(6);

        if (((Symbolic)qs).IsInteger())
        {
            switch (((Symbolic)qs).ToInt() % 12)
            {
            case 1:
                return((Symbolic.SQRT3 + Symbolic.IONE) / Symbolic.TWO);

            case 2:
                return((Symbolic.ONE + Symbolic.SQRT3) * Symbolic.IONE / Symbolic.TWO);

            case 4:
                return((Symbolic.SQRT3 * Symbolic.IONE + Symbolic.MINUS) / Symbolic.TWO);

            case 5:
                return((Symbolic.IONE - Symbolic.SQRT3) / Symbolic.TWO);

            case 7:
                return((Symbolic.IMINUS - Symbolic.SQRT3) / Symbolic.TWO);

            case 8:
                return((Symbolic.SQRT3 * Symbolic.IMINUS - Symbolic.ONE) / Symbolic.TWO);

            case 10:
                return((Symbolic.SQRT3 * Symbolic.IMINUS + Symbolic.ONE) / Symbolic.TWO);

            case 11:
                return((Symbolic.IMINUS + Symbolic.SQRT3) / Symbolic.TWO);
            }
        }
        return(null);
    }
Exemplo n.º 29
0
    public void PopulateContainer()
    {
        TriggerVariables.Clear();
        BoolVariables.Clear();
        IntVariables.Clear();
        FloatVariables.Clear();
        Vector2Variables.Clear();
        Vector3Variables.Clear();
        QuaternionVariables.Clear();
        TimerVariables.Clear();

        foreach (var propertyPath in AssetDatabaseUtils.GetAssetRelativePaths(FolderPath, IncludeSubdirectories))
        {
            TriggerVariable assetAsTrigger =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(TriggerVariable)) as TriggerVariable;
            if (assetAsTrigger != null)
            {
                TriggerVariables.Add(assetAsTrigger);
                continue;
            }

            BoolVariable assetAsBool =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(BoolVariable)) as BoolVariable;
            if (assetAsBool != null)
            {
                BoolVariables.Add(assetAsBool);
                continue;
            }

            IntVariable assetAsInt =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(IntVariable)) as IntVariable;
            if (assetAsInt != null)
            {
                IntVariables.Add(assetAsInt);
                continue;
            }

            FloatVariable assetAsFloat =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(FloatVariable)) as FloatVariable;
            if (assetAsFloat != null)
            {
                FloatVariables.Add(assetAsFloat);
                continue;
            }

            Vector2Variable assetAsVector2 =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(Vector2Variable)) as Vector2Variable;
            if (assetAsVector2 != null)
            {
                Vector2Variables.Add(assetAsVector2);
                continue;
            }

            Vector3Variable assetAsVector3 =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(Vector3Variable)) as Vector3Variable;
            if (assetAsVector3 != null)
            {
                Vector3Variables.Add(assetAsVector3);
                continue;
            }

            QuaternionVariable assetAsQuaternion =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(QuaternionVariable)) as QuaternionVariable;
            if (assetAsQuaternion != null)
            {
                QuaternionVariables.Add(assetAsQuaternion);
                continue;
            }

            TimerVariable assetAsTimer =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(TimerVariable)) as TimerVariable;
            if (assetAsTimer != null)
            {
                TimerVariables.Add(assetAsTimer);
                continue;
            }

            FunctionVariable assetAsFunction =
                AssetDatabase.LoadAssetAtPath(propertyPath, typeof(TimerVariable)) as FunctionVariable;
            if (assetAsFunction != null)
            {
                FunctionVariables.Add(assetAsFunction);
                continue;
            }
        }
        Debug.Log($"{TriggerVariables.Count} Triggers" +
                  $" | {BoolVariables.Count} Bools" +
                  $" | {IntVariables.Count} Ints" +
                  $" | {FloatVariables.Count} Floats" +
                  $" | {Vector2Variables.Count} Vector2s" +
                  $" | {Vector3Variables.Count} Vector3s" +
                  $" | {QuaternionVariables.Count} Quaternions" +
                  $" | {TimerVariables.Count} Timers" +
                  $" | {FunctionVariables.Count} Functions");
    }
Exemplo n.º 30
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 Symbolic)
            {
                return(SymEval(nom / den));
            }

            if (den is Exponential && (( Polynomial )den)[0].Equals(Symbolic.ZERO) && (( Polynomial )den)[1] is Symbolic)
            {
                if (nom is Symbolic || nom is Polynomial)
                {
                    var denx = ( Exponential )den;

                    var den_inv = new Exponential(Symbolic.ONE / denx[1], Symbolic.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._v is FunctionVariable) || !((FunctionVariable)fp._v).Name.Equals("exp"))
        {
            return(f.Map(this));
        }

        var arg = ((FunctionVariable)fp._v).Var.Reduce();

        if (arg is Symbolic)
        {
            return(fp.value(FunctionVariable.Create("exp", arg)).Map(this));
        }

        if (!(arg is Polynomial) || ((Polynomial)arg).Degree() != 1)
        {
            return(f.Map(this));
        }

        Algebraic z = Symbolic.ZERO;

        var a = ((Polynomial)arg)[1];

        for (int i = 1; i < fp.Coeffs.Length; i++)
        {
            var b = (( Polynomial )arg)[0];

            Symbolic I = new Complex(( double )i);

            Algebraic ebi = Symbolic.ONE;

            while (b is Polynomial && (( Polynomial )b).Degree() == 1)
            {
                var p = ( Polynomial )b;

                var f1 = FunctionVariable.Create("exp", new Polynomial(p._v) * 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)._v) * a * I);

            f2 = Exponential.poly2exp(f2);

            z = z + cf * f2;
        }

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

        return(Exponential.poly2exp(z));
    }