diff --git a/ReleaseNotes/12_28_2023.txt b/ReleaseNotes/12_28_2023.txt new file mode 100644 index 00000000000..91d62e27742 --- /dev/null +++ b/ReleaseNotes/12_28_2023.txt @@ -0,0 +1,36 @@ + +Features: + + - Numerical Linear Algebra SOR Standard (1, 2, 3) + - Numerical Linear Algebra SOR Omega (4, 5, 6) + - Numerical Linear Algebra SOR Square (7, 8, 9) + - Linear Algebra SOR RHS Array (10, 11, 12) + - SOR Projective Forward Substitution #1 (13, 14, 15) + - SOR Projective Forward Substitution #2 (16, 17, 18) + - Successive Over Relaxation Vector Match #1 (19, 20, 21) + - Successive Over Relaxation Vector Match #2 (22, 23, 24) + - Successive Over Relaxation Vector Match #3 (25, 26, 27) + - Privatize SOR Vector Comparison Match (28) + - Forward Substitution Major Element Identification (29, 30, 31) + - Updated Unknown Array Major Element (32, 33) + - Forward Substitution Vectors Match Exception (34) + - SOR Previous/Updated Array #1 (35, 36) + - SOR Previous/Updated Array #2 (37, 38) + - SOR Forward Substitution Run #1 (39) + - SOR Forward Substitution Run #2 (40, 41) + - SOR Forward Substitution Run #3 (42, 43) + - SOR Forward Substitution Run #4 (44, 45) + - SOR Forward Substitution Run #5 (46, 47) + - SOR Forward Substitution Run #6 (48, 49) + - SOR Forward Substitution Run #7 (50, 51) + - SOR Forward Substitution Run #8 (52, 53) + - SOR Forward Substitution Run #9 (54, 55) + - SOR Forward Substitution Run #10 (56, 57) + - SOR Forward Substitution Run #11 (58, 59, 60) + + +Bug Fixes/Re-organization: + +Samples: + +IdeaDRIP: diff --git a/ScheduleSheet.xlsx b/ScheduleSheet.xlsx index eb0ddbde6da..3ca23983426 100644 Binary files a/ScheduleSheet.xlsx and b/ScheduleSheet.xlsx differ diff --git a/src/main/java/org/drip/numerical/linearalgebra/SuccessiveOverRelaxation.java b/src/main/java/org/drip/numerical/linearalgebra/SuccessiveOverRelaxation.java index a1d636ee443..24b8fc36852 100644 --- a/src/main/java/org/drip/numerical/linearalgebra/SuccessiveOverRelaxation.java +++ b/src/main/java/org/drip/numerical/linearalgebra/SuccessiveOverRelaxation.java @@ -1,6 +1,9 @@ package org.drip.numerical.linearalgebra; +import org.drip.numerical.common.NumberUtil; +import org.drip.service.common.FormatUtil; + /* * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ @@ -120,20 +123,49 @@ public class SuccessiveOverRelaxation { + public static final double TOLERANCE = 1.0e-04; + + private double[] _rhsArray = null; + private double _omega = Double.NaN; + private double[][] _squareMatrix = null; private double[][] _diagonalMatrix = null; private double[][] _strictlyLowerTriangularMatrix = null; private double[][] _strictlyUpperTriangularMatrix = null; + private static final boolean VectorsMatch ( + final double[] array1, + final double[] array2) + throws Exception + { + for (int i = 0; i < array1.length; ++i) { + System.out.println ("Arrays => " + array1[i] + " | " + array2[i]); + + if (Math.abs (array1[i] - array2[i]) > TOLERANCE) { + System.out.println ("FALSE => " + Math.abs (array1[i] - array2[i])); + + return false; + } + } + + System.out.println ("TRUE"); + + return true; + } + /** * Construct a Standard SuccessiveOverRelaxation Instance from the Square Matrix * * @param squareMatrix Square Matrix + * @param rhsArray RHS Array + * @param omega SOR Omega Parameter * * @return SuccessiveOverRelaxation Instance */ public static final SuccessiveOverRelaxation Standard ( - final double[][] squareMatrix) + final double[][] squareMatrix, + final double[] rhsArray, + final double omega) { try { int size = squareMatrix.length; @@ -160,9 +192,12 @@ public static final SuccessiveOverRelaxation Standard ( } return new SuccessiveOverRelaxation ( + squareMatrix, diagonalMatrix, strictlyLowerTriangularMatrix, - strictlyUpperTriangularMatrix + strictlyUpperTriangularMatrix, + rhsArray, + omega ); } catch (Exception e) { e.printStackTrace(); @@ -174,27 +209,47 @@ public static final SuccessiveOverRelaxation Standard ( /** * SuccessiveOverRelaxation Constructor * + * @param squareMatrix Square Matrix * @param diagonalMatrix Diagonal Matrix * @param strictlyLowerTriangularMatrix Strictly Lower Triangular Matrix * @param strictlyUpperTriangularMatrix Strictly Upper Triangular Matrix + * @param rhsArray RHS Array + * @param omega SOR Omega Parameter * * @throws Exception Thrown if the Inputs are Invalid */ public SuccessiveOverRelaxation ( + final double[][] squareMatrix, final double[][] diagonalMatrix, final double[][] strictlyLowerTriangularMatrix, - final double[][] strictlyUpperTriangularMatrix) + final double[][] strictlyUpperTriangularMatrix, + final double[] rhsArray, + final double omega) throws Exception { - if (null == (_diagonalMatrix = diagonalMatrix) || + if (null == (_squareMatrix = squareMatrix) || + null == (_diagonalMatrix = diagonalMatrix) || null == (_strictlyLowerTriangularMatrix = strictlyLowerTriangularMatrix) || - null == (_strictlyUpperTriangularMatrix = strictlyUpperTriangularMatrix)) + null == (_strictlyUpperTriangularMatrix = strictlyUpperTriangularMatrix) || + null == (_rhsArray = rhsArray) || + !NumberUtil.IsValid (_omega = omega)) { throw new Exception ("SuccessiveOverRelaxation Construction => Invalid Inputs"); } } + /** + * Retrieve the Square Matrix + * + * @return Square Matrix + */ + + public double[][] squareMatrix() + { + return _squareMatrix; + } + /** * Retrieve the Diagonal Matrix * @@ -227,4 +282,112 @@ public double[][] strictlyUpperTriangularMatrix() { return _strictlyUpperTriangularMatrix; } + + /** + * Retrieve the RHS Array + * + * @return RHS Array + */ + + public double[] rhsArray() + { + return _rhsArray; + } + + /** + * Retrieve the SOR Omega + * + * @return SOR Omega + */ + + public double omega() + { + return _omega; + } + + public void forwardSubstitution ( + final double[] startingUnknownArray) + { + if (null == startingUnknownArray || _rhsArray.length != startingUnknownArray.length) { + return; + } + + // int iteration = 0; + double[] previousUnknownArray = startingUnknownArray; + double[] updatedUnknownArray = new double[previousUnknownArray.length]; + + for (int i = 0; i < updatedUnknownArray.length; ++i) { + updatedUnknownArray[i] = Math.random(); + } + + try { + do { + previousUnknownArray = updatedUnknownArray; + + for (int i = 0; i < previousUnknownArray.length; ++i) { + updatedUnknownArray[i] = _rhsArray[i]; + + for (int j = 0; j < previousUnknownArray.length; ++j) { + if (j < i) { + updatedUnknownArray[i] -= _squareMatrix[i][j] * updatedUnknownArray[j]; + } else if (j > i) { + updatedUnknownArray[i] -= _squareMatrix[i][j] * previousUnknownArray[j]; + } + } + + updatedUnknownArray[i] = (1. - _omega) * previousUnknownArray[i] + ( + _omega * updatedUnknownArray[i] / _squareMatrix[i][i] + ); + + System.out.println (i + " => " + previousUnknownArray[i] + " | " + updatedUnknownArray[i]); + } + + /* String dump = "[Iteration = " + iteration++ + "] {"; + + for (int i = 0; i < previousUnknownArray.length; ++i) { + dump += FormatUtil.FormatDouble (updatedUnknownArray[i], 2, 4, 1.) + " | " + + FormatUtil.FormatDouble (previousUnknownArray[i], 2, 4, 1.) + ", "; + } + + System.out.println (dump + "}"); */ + } while (!VectorsMatch (previousUnknownArray, updatedUnknownArray)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static final void main ( + final String[] argumentArray) + { + double[][] squareMatrix = new double[][] { + { 4., -1., -6., 0.}, + {-5., -4., 10., 8.}, + { 0., 9., 4., -2.}, + { 1., 0., -7., 5.}, + }; + + double[] rhsArray = new double[] { + 2., + 21., + -12., + -6. + }; + + double omega = 0.5; + + double[] startingUnknownArray = new double[] { + 0., + 0., + 0., + 0. + }; + + SuccessiveOverRelaxation successiveOverRelaxation = SuccessiveOverRelaxation.Standard ( + squareMatrix, + rhsArray, + omega + ); + + successiveOverRelaxation.forwardSubstitution (startingUnknownArray); + } }