コード例 #1
0
ファイル: Rational.cs プロジェクト: dcprojects/symbcs
    public static Vector Horowitz(Algebraic p, Polynomial q, Variable x)
    {
        if (Poly.Degree(p, x) >= Poly.Degree(q, x))
        {
            throw new JasymcaException("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);

        int m = b is Polynomial ? (( Polynomial )b).Degree() : 0;
        int n = d is Polynomial ? (( Polynomial )d).Degree() : 0;

        var a = new SimpleVariable[m];
        var X = new Polynomial(x);

        Algebraic A = Symbolic.ZERO;

        for (int 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 = Symbolic.ZERO;

        for (int 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 (int i = 0; i < m + n; i++)
        {
            co[i] = Poly.Coefficient(p, x, i);

            cf = Poly.Coefficient(r, x, i);

            for (int k = 0; k < m; k++)
            {
                aik[i][k] = cf.Derive(a[k]);
            }

            for (int k = 0; k < n; k++)
            {
                aik[i][k + m] = cf.Derive(c[k]);
            }
        }

        var s = LambdaLINSOLVE.Gauss(new Matrix(aik), new Vector(co));

        A = Symbolic.ZERO;

        for (int i = m - 1; i >= 0; i--)
        {
            A = A + s[i];

            if (i > 0)
            {
                A = A * X;
            }
        }

        C = Symbolic.ZERO;

        for (int 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 }));
    }
コード例 #2
0
 public override Algebraic Derive(Variable v)
 {
     return(((nom.Derive(v) * den - den.Derive(v) * nom) / (den * den)).Reduce());
 }
コード例 #3
0
ファイル: LambdaALGSYS.cs プロジェクト: fastrom/symbcs
//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 Vector solve(ArrayList expr, ArrayList x, int n)
    {
        Algebraic equ = null;
        Variable  v = null;
        int       i, k, iv = 0, ke = 0;

        for (i = 0; i < n && equ == null; i++)
        {
            v = (Variable)x[i];
            double norm = -1.0;
            for (k = 0; k < n; k++)
            {
                Algebraic exp = (Algebraic)expr[k];
                if (exp is Rational)
                {
                    exp = ((Rational)exp).nom;
                }
                Algebraic slope = exp.Derive(v);
                if (!slope.Equals(Symbolic.ZERO) && slope is Symbolic)
                {
                    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.");
        }
        Vector sol = LambdaSOLVE.solve(equ, v);

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