Пример #1
0
        private int _AssignID; // 0 == assign, 1 == increment, 2 == decrement, 3 == auto increment, 4 == auto decrement

        public TNodeMatrixUnitAssign(TNode Parent, CellMatrix Data, FNode Node, FNode RowID, FNode ColumnID, int AssignID)
            : base(Parent)
        {
            this._matrix = Data;
            this._Node = Node;
            this._AssignID = AssignID;
            this._row_id = RowID;
            this._col_id = ColumnID;
        }
Пример #2
0
        /// <summary>
        /// Create a cell matrix from another matrix, effectively cloning the matrix
        /// </summary>
        /// <param name="A"></param>
        public CellMatrix(CellMatrix A)
        {

            // Build Matrix //
            this._Rows = A._Rows;
            this._Columns = A._Columns;

            this._Data = new Cell[this._Rows, this._Columns];
            for (int i = 0; i < this._Rows; i++)
                for (int j = 0; j < this._Columns; j++)
                    this._Data[i, j] = A[i, j];
            this._Affinity = A.Affinity;

        }
Пример #3
0
            public CellMatrix getU()
            {

                CellMatrix X = new CellMatrix(n, n, LU.Affinity);
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        if (i <= j)
                        {
                            X[i, j] = LU[i, j];
                        }
                        else
                        {
                            X[i, j] = Cell.ZeroValue(LU.Affinity);
                        }
                    }
                }
                return X;

            }
Пример #4
0
            public LUDecomposition(CellMatrix A)
            {

                // Use a "left-looking", dot-product, Crout/Doolittle algorithm.

                this._zero = Cell.ZeroValue(A.Affinity);
                LU = new CellMatrix(A);
                m = A.RowCount;
                n = A.ColumnCount;
                piv = new int[m];
                for (int i = 0; i < m; i++)
                {
                    piv[i] = i;
                }
                pivsign = 1;
                Cell[] LUcolj = new Cell[m];

                // Outer loop.

                for (int j = 0; j < n; j++)
                {

                    // Make a copy of the j-th column to localize references.

                    for (int i = 0; i < m; i++)
                    {
                        LUcolj[i] = LU[i, j];
                    }

                    // Apply previous transformations.

                    for (int i = 0; i < m; i++)
                    {

                        // Most of the time is spent in the following dot product.

                        int kmax = Math.Min(i, j);
                        Cell s = this._zero;
                        for (int k = 0; k < kmax; k++)
                        {
                            s += LU[i, k] * LUcolj[k];
                        }

                        LU[i, j] = LUcolj[i] -= s;

                    }

                    // Find pivot and exchange if necessary.

                    int p = j;
                    for (int i = j + 1; i < m; i++)
                    {
                        if (Cell.Abs(LUcolj[i]) > Cell.Abs(LUcolj[p]))
                        {
                            p = i;
                        }
                    }
                    if (p != j)
                    {
                        for (int k = 0; k < n; k++)
                        {
                            Cell t = LU[p, k];
                            LU[p, k] = LU[j, k];
                            LU[j, k] = t;
                        }
                        int l = piv[p];
                        piv[p] = piv[j];
                        piv[j] = l;
                        pivsign = -pivsign;
                    }

                    // Compute multipliers.

                    if (j < m & LU[j, j] != this._zero)
                    {
                        for (int i = j + 1; i < m; i++)
                        {
                            LU[i, j] /= LU[j, j];
                        }
                    }
                }
            }
Пример #5
0
            public CellMatrix getL()
            {

                CellMatrix L = new CellMatrix(m, n, LU.Affinity);
                for (int i = 0; i < m; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        if (i > j)
                        {
                            L[i, j] = LU[i, j];
                        }
                        else if (i == j)
                        {
                            L[i, j] = Cell.OneValue(LU.Affinity);
                        }
                        else
                        {
                            L[i, j] = Cell.ZeroValue(LU.Affinity);
                        }
                    }
                }
                return L;

            }
Пример #6
0
        public static Cell SumSquare(CellMatrix A)
        {

            Cell d = Cell.ZeroValue(A.Affinity);
            for (int i = 0; i < A.RowCount; i++)
                for (int j = 0; j < A.ColumnCount; j++)
                    d += A[i, j] * A[i, j];
            return d;

        }
Пример #7
0
        public static CellMatrix Trace(CellMatrix A)
        {

            if (!A.IsSquare)
                throw new Exception(string.Format("Cannot trace a non-square matrix : {0} x {1}", A.RowCount, A.ColumnCount));

            CellMatrix B = new CellMatrix(A.RowCount, A.RowCount, Cell.ZeroValue(A.Affinity));

            for (int i = 0; i < A.RowCount; i++)
                B[i, i] = A[i, i];

            return B;

        }
Пример #8
0
 /// <summary>
 /// Use for checking matrix multiplication
 /// </summary>
 /// <param name="A"></param>
 /// <param name="B"></param>
 /// <returns></returns>
 private static bool CheckDimensions2(CellMatrix A, CellMatrix B)
 {
     return (A.ColumnCount == B.RowCount);
 }
Пример #9
0
        public static CellMatrix Invert(CellMatrix A)
        {

            // New decomposition //
            LUDecomposition lu = new LUDecomposition(A);

            // New identity //
            CellMatrix I = CellMatrix.Identity(A.RowCount, A.Affinity);

            return lu.solve(I);

        }
Пример #10
0
            public CellMatrix solve(CellMatrix B)
            {

                if (B.RowCount != m)
                {
                    throw new Exception("Matrix row dimensions must agree.");
                }
                if (!this.isNonsingular())
                {
                    throw new Exception("Matrix is singular.");
                }

                // Copy right hand side with pivoting
                int nx = B.ColumnCount;
                CellMatrix X = this.getMatrix(B, piv, 0, nx - 1);

                // Solve L*Y = B(piv,:)
                for (int k = 0; k < n; k++)
                {

                    for (int i = k + 1; i < n; i++)
                    {

                        for (int j = 0; j < nx; j++)
                        {
                            X[i, j] -= X[k, j] * LU[i, k];
                        }

                    }

                }
                // Solve U*X = Y;
                for (int k = n - 1; k >= 0; k--)
                {

                    for (int j = 0; j < nx; j++)
                    {
                        X[k, j] /= LU[k, k];
                    }

                    for (int i = 0; i < k; i++)
                    {

                        for (int j = 0; j < nx; j++)
                        {
                            X[i, j] -= X[k, j] * LU[i, k];
                        }

                    }

                }

                return X;

            }
Пример #11
0
 public MNodeLiteral(MNode Parent, CellMatrix Value)
     : base(Parent)
 {
     this._value = Value;
 }
Пример #12
0
 public static CellMatrix CheckDivide(CellMatrix A, Cell B)
 {
     CellMatrix C = new CellMatrix(A.RowCount, A.ColumnCount, A.Affinity);
     for (int i = 0; i < A.RowCount; i++)
     {
         for (int j = 0; j < A.ColumnCount; j++)
         {
             C._Data[i, j] = Cell.CheckDivide(A._Data[i, j], B);
         }
     }
     return C;
 }
Пример #13
0
        /// <summary>
        /// Performs the true matrix multiplication between two matricies
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static CellMatrix operator ^(CellMatrix A, CellMatrix B)
        {

            if (CellMatrix.CheckDimensions2(A, B) == false)
            {
                throw new Exception(string.Format("Dimension mismatch A {0}x{1} B {2}x{3}", A.RowCount, A.ColumnCount, B.RowCount, B.ColumnCount));
            }

            CellMatrix C = new CellMatrix(A.RowCount, B.ColumnCount, Cell.ZeroValue(A.Affinity));

            // Main Loop //
            for (int i = 0; i < A.RowCount; i++)
            {

                // Sub Loop One //
                for (int j = 0; j < B.ColumnCount; j++)
                {

                    // Sub Loop Two //
                    for (int k = 0; k < A.ColumnCount; k++)
                    {

                        C[i, j] = C[i, j] + A[i, k] * B[k, j];

                    }

                }

            }

            // Return C //
            return C;

        }
Пример #14
0
 public static CellMatrix CheckDivide(Cell A, CellMatrix B)
 {
     CellMatrix C = new CellMatrix(B.RowCount, B.ColumnCount, A.AFFINITY);
     for (int i = 0; i < B.RowCount; i++)
     {
         for (int j = 0; j < B.ColumnCount; j++)
         {
             C._Data[i, j] = Cell.CheckDivide(A, B._Data[i, j]);
         }
     }
     return C;
 }
Пример #15
0
        /// <summary>
        /// Divides a matrix by another matrix, but checks for N / 0 erros
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static CellMatrix CheckDivide(CellMatrix A, CellMatrix B)
        {

            // Check bounds are the same //
            if (CellMatrix.CheckDimensions(A, B) == false)
            {
                throw new Exception(string.Format("Dimension mismatch A {0}x{1} B {2}x{3}", A.RowCount, A.ColumnCount, B.RowCount, B.ColumnCount));
            }

            // Build a matrix //
            CellMatrix C = new CellMatrix(A.RowCount, A.ColumnCount, A.Affinity);

            // Main loop //
            for (int i = 0; i < A.RowCount; i++)
            {
                for (int j = 0; j < A.ColumnCount; j++)
                {
                    C[i, j] = B[i, j].IsZero ? Cell.ZeroValue(A[i, j].Affinity) : A[i, j] / B[i, j];
                }
            }

            // Return //
            return C;

        }
Пример #16
0
 public static CellMatrix operator /(CellMatrix A, Cell B)
 {
     CellMatrix C = new CellMatrix(A.RowCount, A.ColumnCount, A.Affinity);
     for (int i = 0; i < A.RowCount; i++)
     {
         for (int j = 0; j < A.ColumnCount; j++)
         {
             C._Data[i, j] = A._Data[i, j] / B;
         }
     }
     return C;
 }
Пример #17
0
 public static CellMatrix operator /(Cell A, CellMatrix B)
 {
     CellMatrix C = new CellMatrix(B.RowCount, B.ColumnCount, A.AFFINITY);
     for (int i = 0; i < B.RowCount; i++)
     {
         for (int j = 0; j < B.ColumnCount; j++)
         {
             C._Data[i, j] = A / B._Data[i, j];
         }
     }
     return C;
 }
Пример #18
0
        /// <summary>
        /// Negates a matrix
        /// </summary>
        /// <param name="A">Matrix to negate</param>
        /// <returns>0 - A</returns>
        public static CellMatrix operator -(CellMatrix A)
        {

            // Build a matrix //
            CellMatrix C = new CellMatrix(A.RowCount, A.ColumnCount, A.Affinity);

            // Main loop //
            for (int i = 0; i < A.RowCount; i++)
            {
                for (int j = 0; j < A.ColumnCount; j++)
                {
                    C[i, j] = -A[i, j];
                }
            }

            // Return //
            return C;

        }
Пример #19
0
        /// <summary>
        /// Transposes a matrix
        /// </summary>
        /// <param name="A"></param>
        /// <returns></returns>
        public static CellMatrix operator ~(CellMatrix A)
        {

            // Create Another Matrix //
            CellMatrix B = new CellMatrix(A.ColumnCount, A.RowCount, A.Affinity);

            // Loop through A and copy element to B //
            for (int i = 0; i < A.RowCount; i++)
                for (int j = 0; j < A.ColumnCount; j++)
                    B[j, i] = A[i, j];

            // Return //
            return B;

        }
Пример #20
0
        /// <summary>
        /// Returns the identity matrix given a dimension
        /// </summary>
        /// <param name="Dimension"></param>
        /// <returns></returns>
        public static CellMatrix Identity(int Dimension, CellAffinity Affinity)
        {

            // Check that a positive number was passed //
            if (Dimension < 1)
                throw new Exception("Dimension must be greater than or equal to 1");

            // Create a matrix //
            CellMatrix A = new CellMatrix(Dimension, Dimension, Affinity);

            for (int i = 0; i < Dimension; i++)
            {
                for (int j = 0; j < Dimension; j++)
                {
                    if (i != j)
                        A[i, j] = Cell.ZeroValue(A.Affinity);
                    else
                        A[i, j] = Cell.OneValue(A.Affinity);
                }
            }

            return A;

        }
Пример #21
0
        public static void AllocateMemory(Workspace Home, MemoryStruct Heap, ExpressionVisitor Evaluator, HScriptParser.DeclareMatrix2DContext context)
        {

            string name = context.IDENTIFIER().GetText();
            CellAffinity type = GetAffinity(context.type());
            int rows = (int)Evaluator.ToNode(context.expression()[0]).Evaluate().valueINT;
            int cols = (int)Evaluator.ToNode(context.expression()[1]).Evaluate().valueINT;
            CellMatrix mat = new CellMatrix(rows, cols, type);
            Heap.Arrays.Reallocate(name, mat);

        }
Пример #22
0
            public CellMatrix getMatrix(CellMatrix A, int[] r, int j0, int j1)
            {

                CellMatrix X = new CellMatrix(r.Length, j1 - j0 + 1, A.Affinity);

                for (int i = 0; i < r.Length; i++)
                {
                    for (int j = j0; j <= j1; j++)
                    {
                        X[i, j - j0] = A[r[i], j];
                    }
                }

                return X;

            }