Пример #1
0
        protected override GeneralMatrix CalculateNextHessianApproximation(GeneralMatrix previousH,
                                                                           double[] prevX, double[] curX, double[] prevGrad, double[] curGrad)
        {
            GeneralMatrix currentH = new GeneralMatrix(_nDim, _nDim);
            GeneralMatrix cX       = new GeneralMatrix(curX, _nDim);
            GeneralMatrix pX       = new GeneralMatrix(prevX, _nDim);
            GeneralMatrix cG       = new GeneralMatrix(curGrad, _nDim);
            GeneralMatrix pG       = new GeneralMatrix(prevGrad, _nDim);

            GeneralMatrix dX = cX.Subtract(pX);
            GeneralMatrix dG = cG.Subtract(pG);

            double        aK1 = 1 / (dX.Transpose().Multiply(dG).GetElement(0, 0));
            GeneralMatrix aK2 = dX.Multiply(dX.Transpose());

            GeneralMatrix aK = aK2.Multiply(aK1);

            double        bK1 = -1 / (dG.Transpose().Multiply(previousH).Multiply(dG).GetElement(0, 0));
            GeneralMatrix bK2 = previousH.Multiply(dG).Multiply(dG.Transpose()).Multiply(previousH.Transpose());

            GeneralMatrix bK = bK2.Multiply(bK1);

            currentH = previousH.Add(aK).Add(bK);

            return(currentH);
        }
Пример #2
0
        /// <summary>
        /// (
        /// </summary>
        /// <param name="matrix"></param>
        /// <returns></returns>
        public static GeneralMatrix ExpandUtility(GeneralMatrix matrix)
        {
            double val = 0.0;
            int    n   = matrix.RowDimension;
            int    m   = matrix.ColumnDimension;

            if (n != m)
            {
                throw new ArgumentException("Criteria matrix must be symmetrical");
            }

            GeneralMatrix newMatrix = matrix.Transpose();

            //for all transposed elements calculate their inverse values
            //set diagonal elements to 0
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    val = newMatrix.GetElement(i, j);
                    if (val == 0.0)
                    {
                        throw new ArgumentException("Criteria comparison values van't be 0");
                    }
                    newMatrix.SetElement(i, j, 1 / val);
                    if (i == j)
                    {
                        newMatrix.SetElement(i, j, 0);
                    }
                }
            }
            //add transposed, inverse matrix to the original one
            //create fully expanded matrix
            return(newMatrix.Add(matrix));
        }
Пример #3
0
        public void SubstractAndAdd()
        {
            GeneralMatrix B = GeneralMatrix.Random(A.RowDimension, A.ColumnDimension);
            GeneralMatrix C = A.Subtract(B);

            Assert.IsTrue(GeneralTests.Check(C.Add(B), A));
        }
Пример #4
0
        protected override GeneralMatrix CalculateNextHessianApproximation(GeneralMatrix pH,
                                                                           double[] prevX, double[] curX, double[] prevGrad, double[] curGrad)
        {
            GeneralMatrix cH = new GeneralMatrix(_nDim, _nDim);
            GeneralMatrix cX = new GeneralMatrix(curX, _nDim);
            GeneralMatrix pX = new GeneralMatrix(prevX, _nDim);
            GeneralMatrix cG = new GeneralMatrix(curGrad, _nDim);
            GeneralMatrix pG = new GeneralMatrix(prevGrad, _nDim);

            GeneralMatrix sigma = cX.Subtract(pX);
            GeneralMatrix gamma = cG.Subtract(pG);

            double sigmaTGamma = sigma.Transpose().Multiply(gamma).GetElement(0, 0);

            GeneralMatrix hGammaSigmaT = pH.Multiply(gamma.Multiply(sigma.Transpose()));
            GeneralMatrix sigmaGammaTH = sigma.Multiply(gamma.Transpose().Multiply(pH));
            double        gammaTHGamma = (gamma.Transpose().Multiply(pH.Multiply(gamma))).GetElement(0, 0);
            GeneralMatrix sigmaSigmaT  = sigma.Multiply(sigma.Transpose());

            GeneralMatrix term1 = (hGammaSigmaT.Add(sigmaGammaTH)).Multiply(1 / sigmaTGamma);
            GeneralMatrix term2 = (sigmaSigmaT.Multiply(1 / sigmaTGamma)).Multiply(1 + gammaTHGamma / sigmaTGamma);

            return(pH.Subtract(term1).Add(term2));
        }
Пример #5
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");
        }
Пример #6
0
 public void Negative_AddMatrix()
 {
     A.Add(S);
 }
Пример #7
0
 public void UnaryMinus()
 {
     A = R.UnaryMinus();
     Z = new GeneralMatrix(A.RowDimension, A.ColumnDimension);
     Assert.IsTrue(GeneralTests.Check(A.Add(R), Z));
 }