Пример #1
0
        public void Negative_WrongIndecesForSubMatrix2()
        {
            int ib = 1, ie = 2, jb = 1, je = 3;             /* index ranges for sub GeneralMatrix */

            double[][]    avals = { new double[] { 1.0, 4.0, 7.0, 10.0 }, new double[] { 2.0, 5.0, 8.0, 11.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
            GeneralMatrix B     = new GeneralMatrix(avals);

            M = B.GetMatrix(ib, ie, jb, je + B.ColumnDimension + 1);
        }
Пример #2
0
        public void Negative_BadColumnIndexSetGoodRowIndexSet()
        {
            double[][] avals             = { new double[] { 1.0, 4.0, 7.0, 10.0 }, new double[] { 2.0, 5.0, 8.0, 11.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
            int[]      rowindexset       = new int[] { 1, 2 };
            int[]      badcolumnindexset = new int[] { 1, 2, 4 };

            GeneralMatrix B = new GeneralMatrix(avals);

            M = B.GetMatrix(badrowindexset, columnindexset);
        }
Пример #3
0
        public void SolveLinear()
        {
            double[][]    subavals   = { new double[] { 5.0, 8.0, 11.0 }, new double[] { 6.0, 9.0, 12.0 } };
            double[][]    sqSolution = { new double[] { 13.0 }, new double[] { 15.0 } };
            GeneralMatrix sub        = new GeneralMatrix(subavals);
            GeneralMatrix o          = new GeneralMatrix(sub.RowDimension, 1, 1.0);
            GeneralMatrix sol        = new GeneralMatrix(sqSolution);
            GeneralMatrix sq         = sub.GetMatrix(0, sub.RowDimension - 1, 0, sub.RowDimension - 1);

            Assert.IsTrue(GeneralTests.Check(sq.Solve(sol), o));
        }
Пример #4
0
        public void LUDecomposition()
        {
            GeneralMatrix A = new GeneralMatrix(columnwise, 4);
            int           n = A.ColumnDimension;

            A = A.GetMatrix(0, n - 1, 0, n - 1);
            A.SetElement(0, 0, 0.0);
            LUDecomposition LU = A.LUD();

            Assert.IsTrue(GeneralTests.Check(A.GetMatrix(LU.Pivot, 0, n - 1), LU.L.Multiply(LU.U)));
        }
Пример #5
0
        public void Negative_BadColumnIndexSet2()
        {
            int ib = 1, ie = 2;             /* index ranges for sub GeneralMatrix */

            double[][] avals          = { new double[] { 1.0, 4.0, 7.0, 10.0 }, new double[] { 2.0, 5.0, 8.0, 11.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
            int[]      badrowindexset = new int[] { 1, 3 };

            GeneralMatrix B = new GeneralMatrix(avals);

            M = B.GetMatrix(ib, ie + B.RowDimension + 1, columnindexset);
        }
Пример #6
0
        public void Negative_BadSubIndecesData()
        {
            int jb = 1, je = 3;             /* index ranges for sub GeneralMatrix */

            double[][] avals       = { new double[] { 1.0, 4.0, 7.0, 10.0 }, new double[] { 2.0, 5.0, 8.0, 11.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
            int[]      rowindexset = new int[] { 1, 2 };

            GeneralMatrix B = new GeneralMatrix(avals);

            M = B.GetMatrix(rowindexset, jb, je + B.ColumnDimension + 1);
        }
Пример #7
0
        public void Negative_BadRowIndexSet()
        {
            int jb = 1, je = 3;             /* index ranges for sub GeneralMatrix */

            double[][] avals          = { new double[] { 1.0, 4.0, 7.0, 10.0 }, new double[] { 2.0, 5.0, 8.0, 11.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
            int[]      badrowindexset = new int[] { 1, 3 };

            GeneralMatrix B = new GeneralMatrix(avals);

            M = B.GetMatrix(badrowindexset, jb, je);
        }
Пример #8
0
        public void SubMatrixWithColumnIndex()
        {
            int ib = 1, ie = 2;             /* index ranges for sub GeneralMatrix */

            double[][] avals    = { new double[] { 1.0, 4.0, 7.0, 10.0 }, new double[] { 2.0, 5.0, 8.0, 11.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
            double[][] subavals = { new double[] { 5.0, 8.0, 11.0 }, new double[] { 6.0, 9.0, 12.0 } };

            GeneralMatrix B = new GeneralMatrix(avals);

            M   = B.GetMatrix(ib, ie, columnindexset);
            SUB = new GeneralMatrix(subavals);

            Assert.IsTrue(GeneralTests.Check(SUB, M));
        }
Пример #9
0
        public void SubMatrixWithRowIndexSetColumnIndexSet()
        {
            double[][] avals          = { new double[] { 1.0, 4.0, 7.0, 10.0 }, new double[] { 2.0, 5.0, 8.0, 11.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
            double[][] subavals       = { new double[] { 5.0, 8.0, 11.0 }, new double[] { 6.0, 9.0, 12.0 } };
            int[]      rowindexset    = new int[] { 1, 2 };
            int[]      columnindexset = new int[] { 1, 2, 3 };

            GeneralMatrix B = new GeneralMatrix(avals);

            M   = B.GetMatrix(rowindexset, columnindexset);
            SUB = new GeneralMatrix(subavals);

            Assert.IsTrue(GeneralTests.Check(SUB, M));
        }
Пример #10
0
        public void SubMatrixWithRowIndexSet()
        {
            int jb = 1, je = 3;             /* index ranges for sub GeneralMatrix */

            double[][] avals       = { new double[] { 1.0, 4.0, 7.0, 10.0 }, new double[] { 2.0, 5.0, 8.0, 11.0 }, new double[] { 3.0, 6.0, 9.0, 12.0 } };
            double[][] subavals    = { new double[] { 5.0, 8.0, 11.0 }, new double[] { 6.0, 9.0, 12.0 } };
            int[]      rowindexset = new int[] { 1, 2 };

            GeneralMatrix B = new GeneralMatrix(avals);

            M   = B.GetMatrix(rowindexset, jb, je);
            SUB = new GeneralMatrix(subavals);

            Assert.IsTrue(GeneralTests.Check(SUB, M));
        }
Пример #11
0
        /// <summary>
        ///
        /// </summary>
        private void CalculateChoices()
        {
            GeneralMatrix tempMatrix = null;
            int           i, col0, colMax;

            PrioritiesSelector selector = new PrioritiesSelector();

            for (i = 0; i < this._nCriteria; i++)
            {
                col0       = i * _mChoices;
                colMax     = col0 + _mChoices - 1;
                tempMatrix = _choiceMatrix.GetMatrix(0, _mChoices - 1, col0, colMax);
                selector.ComputePriorities(tempMatrix);
                //first element of the matrix is consistency ratio
                //for the criteria
                _lambdas.SetElement(i + 1, 0, selector.ConsistencyRatio);

                _modelResult.SetMatrix(0, _mChoices - 1, i, i, selector.CalculatedMatrix);
            }
        }
Пример #12
0
        public static void  Main(System.String[] argv)
        {
            /*
             | Tests LU, QR, SVD and symmetric Eig decompositions.
             |
             |   n       = order of magic square.
             |   trace   = diagonal sum, should be the magic sum, (n^3 + n)/2.
             |   max_eig = maximum eigenvalue of (A + A')/2, should equal trace.
             |   rank    = linear algebraic rank,
             |             should equal n if n is odd, be less than n if n is even.
             |   cond    = L_2 condition number, ratio of singular values.
             |   lu_res  = test of LU factorization, norm1(L*U-A(p,:))/(n*eps).
             |   qr_res  = test of QR factorization, norm1(Q*R-A)/(n*eps).
             */

            print("\n    Test of GeneralMatrix Class, using magic squares.\n");
            print("    See MagicSquareExample.main() for an explanation.\n");
            print("\n      n     trace       max_eig   rank        cond      lu_res      qr_res\n\n");

            System.DateTime start_time = System.DateTime.Now;
            double          eps        = System.Math.Pow(2.0, -52.0);

            for (int n = 3; n <= 32; n++)
            {
                print(fixedWidthIntegertoString(n, 7));

                GeneralMatrix M = magic(n);

                //UPGRADE_WARNING: Narrowing conversions may produce unexpected results in C#. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
                int t = (int)M.Trace();
                print(fixedWidthIntegertoString(t, 10));

                EigenvalueDecomposition E = new EigenvalueDecomposition(M.Add(M.Transpose()).Multiply(0.5));
                double[] d = E.RealEigenvalues;
                print(fixedWidthDoubletoString(d[n - 1], 14, 3));

                int r = M.Rank();
                print(fixedWidthIntegertoString(r, 7));

                double c = M.Condition();
                print(c < 1 / eps ? fixedWidthDoubletoString(c, 12, 3):"         Inf");

                LUDecomposition LU  = new LUDecomposition(M);
                GeneralMatrix   L   = LU.L;
                GeneralMatrix   U   = LU.U;
                int[]           p   = LU.Pivot;
                GeneralMatrix   R   = L.Multiply(U).Subtract(M.GetMatrix(p, 0, n - 1));
                double          res = R.Norm1() / (n * eps);
                print(fixedWidthDoubletoString(res, 12, 3));

                QRDecomposition QR = new QRDecomposition(M);
                GeneralMatrix   Q  = QR.Q;
                R   = QR.R;
                R   = Q.Multiply(R).Subtract(M);
                res = R.Norm1() / (n * eps);
                print(fixedWidthDoubletoString(res, 12, 3));

                print("\n");
            }

            System.DateTime stop_time = System.DateTime.Now;
            double          etime     = (stop_time.Ticks - start_time.Ticks) / 1000.0;

            print("\nElapsed Time = " + fixedWidthDoubletoString(etime, 12, 3) + " seconds\n");
            print("Adios\n");
        }
Пример #13
0
 public void SetMatrix1()
 {
     B.SetMatrix(ib, ie, jb, je, M);
     Assert.IsTrue(GeneralTests.Check(M.Subtract(B.GetMatrix(ib, ie, jb, je)), M));
 }
Пример #14
0
        /// <summary>Solve A*X = B</summary>
        /// <param name="B">  A Matrix with as many rows as A and any number of columns.
        /// </param>
        /// <returns>     X so that L*U*X = B(piv,:)
        /// </returns>
        /// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
        /// </exception>
        /// <exception cref="System.SystemException"> Matrix is singular.
        /// </exception>
        public virtual GeneralMatrix Solve(GeneralMatrix B)
        {
            if (B.RowDimension != m)
            {
                throw new System.ArgumentException("Matrix row dimensions must agree.");
            }
            if (!this.IsNonSingular)
            {
                throw new System.SystemException("Matrix is singular.");
            }

            // Copy right hand side with pivoting
            int nx = B.ColumnDimension;
            GeneralMatrix Xmat = B.GetMatrix(piv, 0, nx - 1);
            double[][] X = Xmat.Array;

            // 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 Xmat;
        }
Пример #15
0
        public void Transpose6()
        {
            A.Transpose();

            Assert.IsTrue(GeneralTests.Check(A.GetMatrix(0, A.RowDimension - 1, 0, A.RowDimension - 1).Determinant(), 0.0));
        }
Пример #16
0
        public void TestChoiceMatrix()
        {
            double[][] mat1 = new double[][]
            {
                new double[] { 1, 2, 3, 4 },
                new double[] { 0, 1, 2, 5 },
                new double[] { 0, 0, 1, 5 },
                new double[] { 0, 0, 0, 1 }
            };

            GeneralMatrix gmat1 = new GeneralMatrix(mat1);

            gmat1 = AHPModel.ExpandUtility(gmat1);

            double[][] mat2 = new double[][]
            {
                new double[] { 1, 2, 3, 1 },
                new double[] { 0, 1, 2, 3 },
                new double[] { 0, 0, 1, 9 },
                new double[] { 0, 0, 0, 1 }
            };
            GeneralMatrix gmat2 = new GeneralMatrix(mat2);

            gmat2 = AHPModel.ExpandUtility(gmat2);

            double[][] mat3 = new double[][]
            {
                new double[] { 1, 4, 2, 2 },
                new double[] { 0, 1, 2, 4 },
                new double[] { 0, 0, 1, 4 },
                new double[] { 0, 0, 0, 1 }
            };

            GeneralMatrix gmat3 = new GeneralMatrix(mat3);

            gmat3 = AHPModel.ExpandUtility(gmat3);


            //3 criteria 4 choices
            AHPModel model = new AHPModel(3, 4);

            model.AddCriterionRatedChoices(0, mat1);
            model.AddCriterionRatedChoices(1, mat2);
            model.AddCriterionRatedChoices(2, mat3);

            GeneralMatrix matrix = model.ChoiceMatrix;

            GeneralMatrix nmat1 = matrix.GetMatrix(0, 3, 0, 3);
            GeneralMatrix nmat2 = matrix.GetMatrix(0, 3, 4, 7);
            GeneralMatrix nmat3 = matrix.GetMatrix(0, 3, 8, 11);

            bool alarm = false;

            int i, j;

            for (i = 0; i < nmat1.ColumnDimension; i++)
            {
                for (j = 0; j < nmat1.RowDimension; j++)
                {
                    if (nmat1.GetElement(i, j) != gmat1.GetElement(i, j))
                    {
                        alarm = true;
                    }
                }
            }

            for (i = 0; i < nmat2.ColumnDimension; i++)
            {
                for (j = 0; j < nmat2.RowDimension; j++)
                {
                    if (nmat2.GetElement(i, j) != gmat2.GetElement(i, j))
                    {
                        alarm = true;
                    }
                }
            }

            for (i = 0; i < nmat3.ColumnDimension; i++)
            {
                for (j = 0; j < nmat3.RowDimension; j++)
                {
                    if (nmat3.GetElement(i, j) != gmat3.GetElement(i, j))
                    {
                        alarm = true;
                    }
                }
            }


            Assert.IsFalse(alarm);
        }