Exemple #1
0
        public override int Eval(Stack st)
        {
            int       narg = GetNarg(st);
            Algebraic x    = GetAlgebraic(st);

            if (x.IsScalar() && !x.IsConstant())
            {
                throw new SymbolicException("Unknown variable dimension: " + x);
            }
            Matrix m = new Matrix(x);

            st.Push(new Complex(( double )Math.max(m.Cols(), m.Rows())));
            return(0);
        }
Exemple #2
0
        protected virtual Algebraic Div(Algebraic x)
        {
            if (x is Polynomial)
            {
                return(new Rational(this, ( Polynomial )x).Reduce());
            }

            if (x is Rational)
            {
                return((( Rational )x).den.Mul(this).Div((( Rational )x).nom));
            }

            if (!x.IsScalar())
            {
                return(new Matrix(this).Div(x));
            }

            throw new SymbolicException("Can not divide " + this + " through " + x);
        }
Exemple #3
0
        protected override Algebraic Div(Algebraic x)
        {
            if (x.IsScalar())
            {
                var b = CreateRectangularArray <Algebraic>(a.Length, a[0].Length);

                for (int i = 0; i < a.Length; i++)
                {
                    for (int k = 0; k < a[0].Length; k++)
                    {
                        b[i][k] = a[i][k] / x;
                    }
                }

                return(new Matrix(b));
            }

            return(Mul(new Matrix(x).pseudoinverse()));
        }
Exemple #4
0
        public override int Eval(Stack st)
        {
            int       narg = GetNarg(st);
            Algebraic x    = GetAlgebraic(st);

            if (x.IsScalar() && !x.IsConstant())
            {
                throw new SymbolicException("Unknown variable dimension: " + x);
            }
            Matrix    mx = new Matrix(x);
            Algebraic s  = mx.col(1);

            for (int i = 2; i <= mx.Cols(); i++)
            {
                s = s * mx.col(i);
            }
            st.Push(s);
            return(0);
        }
Exemple #5
0
        protected override Algebraic Mul(Algebraic x)
        {
            if (x.IsScalar())
            {
                var b = CreateRectangularArray <Algebraic>(a.Length, a[0].Length);

                for (int i = 0; i < a.Length; i++)
                {
                    for (int k = 0; k < a[0].Length; k++)
                    {
                        b[i][k] = a[i][k] * x;
                    }
                }

                return(new Matrix(b));
            }

            var xm = new Matrix(x);

            if (Cols() != xm.Rows())
            {
                throw new SymbolicException("Matrix dimensions wrong.");
            }

            var b1 = CreateRectangularArray <Algebraic>(a.Length, xm.a[0].Length);

            for (int i = 0; i < a.Length; i++)
            {
                for (int k = 0; k < xm.a[0].Length; k++)
                {
                    b1[i][k] = a[i][0] * xm.a[0][k];

                    for (int l = 1; l < xm.a.Length; l++)
                    {
                        b1[i][k] = b1[i][k] + a[i][l] * xm.a[l][k];
                    }
                }
            }

            return(new Matrix(b1));
        }
Exemple #6
0
        public virtual Algebraic Promote(Algebraic a)
        {
            if (a.IsScalar())
            {
                return(this);
            }

            if (a is Vector)
            {
                var bv = ( Vector )a;

                if (this is Vector && (( Vector )this).Length() == bv.Length())
                {
                    return(this);
                }

                if (IsScalar())
                {
                    return(new Vector(this, bv.Length()));
                }
            }

            if (a is Matrix)
            {
                var bm = ( Matrix )a;

                if (this is Matrix && bm.Equalsized(( Matrix )this))
                {
                    return(this);
                }

                if (IsScalar())
                {
                    return(new Matrix(this, bm.Rows(), bm.Cols()));
                }
            }

            throw new SymbolicException("Wrong argument type.");
        }
Exemple #7
0
        protected override Algebraic Add(Algebraic x)
        {
            if (x.IsScalar())
            {
                x = x.Promote(this);
            }

            if (x is Matrix && Equalsized(( Matrix )x))
            {
                var b = CreateRectangularArray <Algebraic>(a.Length, a[0].Length);

                for (int i = 0; i < a.Length; i++)
                {
                    for (int k = 0; k < a[0].Length; k++)
                    {
                        b[i][k] = a[i][k] + (( Matrix )x).a[i][k];
                    }
                }

                return(new Matrix(b));
            }

            throw new SymbolicException("Wrong arguments for add:" + this + "," + x);
        }
Exemple #8
0
        public override int Eval(Stack st)
        {
            int       narg = GetNarg(st);
            Algebraic x    = GetAlgebraic(st);

            if (x.IsScalar() && !x.IsConstant())
            {
                throw new SymbolicException("Unknown variable dimension: " + x);
            }
            Matrix  mx = new Matrix(x);
            Complex nr = new Complex(( double )mx.Rows()), nc = new Complex(( double )mx.Cols());

            if (length == 2)
            {
                st.Push(nr);
                st.Push(nc);
                length = 1;
            }
            else
            {
                st.Push(new Vector(new Algebraic[] { nr, nc }));
            }
            return(0);
        }