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); }
/// <summary>Check norm of difference of Matrices. /// </summary> public static bool Check(GeneralMatrix X, GeneralMatrix Y) { bool result = false; double eps = System.Math.Pow(2.0, -52.0); if (X.Norm1() == 0.0 & Y.Norm1() < 10 * eps) { result = true; } else if (Y.Norm1() == 0.0 & X.Norm1() < 10 * eps) { result = true; } else if (X.Subtract(Y).Norm1() > 1000 * eps * System.Math.Max(X.Norm1(), Y.Norm1())) { throw new System.SystemException("The norm of (X-Y) is too large: " + X.Subtract(Y).Norm1().ToString()); } else { result = true; } return(result); }
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)); }
public void Substraction() { double[] columnwise = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 }; 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 validld = 3; /* leading dimension of intended test Matrices */ //one dimensional array of doubles packed by columns ala Fortran //is passed to the constructor //the integer value tell the constructor how to //breakup one domensional value into mulidimensional column GeneralMatrix A = new GeneralMatrix(columnwise, validld); GeneralMatrix B = new GeneralMatrix(avals); double tmp = B.GetElement(0, 0); avals[0][0] = 0.0; GeneralMatrix C = B.Subtract(A); avals[0][0] = tmp; B = GeneralMatrix.Create(avals); tmp = B.GetElement(0, 0); avals[0][0] = 0.0; Assert.IsTrue(tmp == B.GetElement(0, 0)); }
public void Substract() { A = R; Assert.AreEqual(0.0, A.Subtract(R).Norm1()); }
public void Negative_Substract() { S = new GeneralMatrix(columnwise, nonconformld); S = A.Subtract(S); }