Skip to content

Commit

Permalink
Features:
Browse files Browse the repository at this point in the history
	- Matrix Util Graham Schmidt Orthogonalization (1, 2, 3)
	- Matrix Util Graham Schmidt Orthonormalization #1 (4, 5)
	- Matrix Util QR Decomposition Vectors in Columns (6, 7)
	- QR Eigen-component Extractor Vectors in Columns (9)
	- Linear Solver Bartels Stewart Scheme (10)
	- Matrix Util Graham Schmidt Orthonormalization #2 (21, 22)
	- Matrix Util Graham Schmidt Orthonormalization #3 (23, 24)


Bug Fixes/Re-organization:

Samples:

	- Matrix Graham Schmidt Process #1 (8)
	- Matrix Graham Schmidt Process #2 (11, 12)
	- Matrix Graham Schmidt Process #3 (13, 14, 15)
	- Matrix Graham Schmidt Process #4 (16, 17, 18)
	- Matrix Graham Schmidt Process #5 (19, 20)
	- Matrix Graham Schmidt Process #6 (25, 26, 27)
	- Matrix Graham Schmidt Process #7 (28, 29, 30)
	- Matrix Graham Schmidt Process #8 (31, 32, 33)


IdeaDRIP:

	- QR Decomposition Computing the - Using Householder Reflections (34-82)
	- QR Decomposition Computing the - Using Householder Reflections - Example (83-108)
	- QR Decomposition Computing the - Using Householder Reflections - Advantages and Disadvantages (109, 110)
	- QR Decomposition Computing the - Using Givens Rotations (111-116)
	- QR Decomposition Computing the - Using Givens Rotations - Example (117-120)
  • Loading branch information
Lakshmik committed Jul 14, 2024
1 parent 2bab0b1 commit 90ab316
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 77 deletions.
33 changes: 33 additions & 0 deletions ReleaseNotes/02_19_2024.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

Features:

- Matrix Util Graham Schmidt Orthogonalization (1, 2, 3)
- Matrix Util Graham Schmidt Orthonormalization #1 (4, 5)
- Matrix Util QR Decomposition Vectors in Columns (6, 7)
- QR Eigen-component Extractor Vectors in Columns (9)
- Linear Solver Bartels Stewart Scheme (10)
- Matrix Util Graham Schmidt Orthonormalization #2 (21, 22)
- Matrix Util Graham Schmidt Orthonormalization #3 (23, 24)


Bug Fixes/Re-organization:

Samples:

- Matrix Graham Schmidt Process #1 (8)
- Matrix Graham Schmidt Process #2 (11, 12)
- Matrix Graham Schmidt Process #3 (13, 14, 15)
- Matrix Graham Schmidt Process #4 (16, 17, 18)
- Matrix Graham Schmidt Process #5 (19, 20)
- Matrix Graham Schmidt Process #6 (25, 26, 27)
- Matrix Graham Schmidt Process #7 (28, 29, 30)
- Matrix Graham Schmidt Process #8 (31, 32, 33)


IdeaDRIP:

- QR Decomposition Computing the - Using Householder Reflections (34-82)
- QR Decomposition Computing the - Using Householder Reflections - Example (83-108)
- QR Decomposition Computing the - Using Householder Reflections - Advantages and Disadvantages (109, 110)
- QR Decomposition Computing the - Using Givens Rotations (111-116)
- QR Decomposition Computing the - Using Givens Rotations - Example (117-120)
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public int maxIterations()
final double[][] a)
{
org.drip.numerical.linearalgebra.QR qr = org.drip.numerical.linearalgebra.MatrixUtil.QRDecomposition (
a
a,
false
);

if (null == qr)
Expand Down Expand Up @@ -214,7 +215,8 @@ public int maxIterations()
v,
q
)
)
),
false
)))
{
return null;
Expand Down
44 changes: 27 additions & 17 deletions src/main/java/org/drip/numerical/linearalgebra/MatrixUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1036,56 +1036,64 @@ public static final double[] Normalize (
/**
* Orthogonalize the Specified Matrix Using the Graham-Schmidt Method
*
* @param v The Input Matrix
* @param a The Input Matrix
* @param vectorsInColumns TRUE - Vectors in Columns
*
* @return The Orthogonalized Matrix
*/

public static final double[][] GrahamSchmidtOrthogonalization (
final double[][] v)
final double[][] a,
final boolean vectorsInColumns)
{
if (null == v || 0 == v.length || v.length != v[0].length) {
if (null == a || 0 == a.length || a.length != a[0].length) {
return null;
}

double[][] u = new double[v.length][v.length];
double[][] aUsingVectorsInRows = vectorsInColumns ? Transpose (a) : a;

for (int i = 0; i < v.length; ++i) {
for (int j = 0; j < v.length; ++j) {
u[i][j] = v[i][j];
double[][] u = new double[aUsingVectorsInRows.length][aUsingVectorsInRows.length];

for (int i = 0; i < aUsingVectorsInRows.length; ++i) {
for (int j = 0; j < aUsingVectorsInRows.length; ++j) {
u[i][j] = aUsingVectorsInRows[i][j];
}
}

for (int i = 1; i < v.length; ++i) {
for (int i = 1; i < aUsingVectorsInRows.length; ++i) {
for (int j = 0; j < i; ++j) {
double[] projectionTrimOff = ProjectVOnUInternal (u[j], v[i]);
double[] projectionTrimOff = ProjectVOnUInternal (u[j], aUsingVectorsInRows[i]);

for (int k = 0; k < projectionTrimOff.length; ++k) {
u[i][k] -= projectionTrimOff[k];
}
}
}

return u;
return vectorsInColumns ? Transpose (u) : u;
}

/**
* Orthonormalize the Specified Matrix Using the Graham-Schmidt Method
*
* @param v The Input Matrix
* @param a The Input Matrix
* @param vectorsInColumns TRUE - Vectors in Columns
*
* @return The Orthonormalized Matrix
*/

public static final double[][] GrahamSchmidtOrthonormalization (
final double[][] v)
final double[][] a,
final boolean vectorsInColumns)
{
double[][] u = GrahamSchmidtOrthogonalization (v);
double[][] uOrthogonal = GrahamSchmidtOrthogonalization (a, vectorsInColumns);

if (null == u) {
if (null == uOrthogonal) {
return null;
}

double[][] u = vectorsInColumns ? Transpose (uOrthogonal) : uOrthogonal;

for (int i = 0; i < u.length; ++i) {
double modulusReciprocal = 1. / ModulusInternal (u[i]);

Expand All @@ -1094,21 +1102,23 @@ public static final double[][] GrahamSchmidtOrthonormalization (
}
}

return u;
return vectorsInColumns ? Transpose (u) : u;
}

/**
* Perform a QR Decomposition on the Input Matrix
*
* @param a The Input Matrix
* @param vectorsInColumns TRUE - Vectors in Columns
*
* @return The Output of QR Decomposition
*/

public static final QR QRDecomposition (
final double[][] a)
final double[][] a,
final boolean vectorsInColumns)
{
double[][] q = GrahamSchmidtOrthonormalization (a);
double[][] q = GrahamSchmidtOrthonormalization (a, vectorsInColumns);

try {
return null == q ? null : new QR (q, Product (q, a));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public double[][] rhsMatrix()

public void solve()
{
QR qrA = MatrixUtil.QRDecomposition (_sylvesterEquation.squareMatrixA().r2Array());
QR qrA = MatrixUtil.QRDecomposition (_sylvesterEquation.squareMatrixA().r2Array(), false);

System.out.println();

Expand All @@ -214,7 +214,8 @@ public void solve()
System.out.println();

QR qrBTranspose = MatrixUtil.QRDecomposition (
_sylvesterEquation.squareMatrixB().transpose().r2Array()
_sylvesterEquation.squareMatrixB().transpose().r2Array(),
false
);

double[][] v = qrBTranspose.q();
Expand Down
70 changes: 15 additions & 55 deletions src/main/java/org/drip/sample/matrix/GrahamSchmidtProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,83 +116,43 @@ public static final void main (
{
EnvManager.InitEnv ("");

/* double[][] aadblV = new double[][] {
{3, 1, 4, 9},
{2, 2, 6, 0},
{1, 8, 3, 5},
{7, 0, 4, 5}
}; */

double[][] aadblV = new double[][] {
{ 12, 6, -4},
{-51, 167, 24},
{ 4, -68, -41}
};

/* double[][] aadblV = new double[][] {
double[][] a = new double[][] {
{12, -51, 4},
{ 6, 167, -68},
{-4, 24, -41}
}; */
};

double[][] aadblUOrthogonal = MatrixUtil.GrahamSchmidtOrthogonalization (aadblV);
double[][] u = MatrixUtil.GrahamSchmidtOrthogonalization (a, true);

NumberUtil.PrintMatrix (
"ORTHOGONAL",
aadblUOrthogonal
);
NumberUtil.PrintMatrix ("ORTHOGONAL", u);

double[][] uTranspose = MatrixUtil.Transpose (u);

System.out.println (
"ORTHOGONAL TEST: " +
FormatUtil.FormatDouble (
MatrixUtil.DotProduct (
aadblUOrthogonal[0],
aadblUOrthogonal[1]
),
1, 1, 1.
)
FormatUtil.FormatDouble ( MatrixUtil.DotProduct (uTranspose[0], uTranspose[1]), 1, 1, 1.)
);

double[][] aadblUOrthonormal = MatrixUtil.GrahamSchmidtOrthonormalization (aadblV);
double[][] q = MatrixUtil.GrahamSchmidtOrthonormalization (a, true);

NumberUtil.PrintMatrix (
"ORTHONORMAL",
aadblUOrthonormal
);
NumberUtil.PrintMatrix ("ORTHONORMAL", q);

double[][] qTranspose = MatrixUtil.Transpose (q);

System.out.println (
"ORTHONORMAL TEST: " +
FormatUtil.FormatDouble (
MatrixUtil.DotProduct (
aadblUOrthonormal[0],
aadblUOrthonormal[1]
),
1, 1, 1.
)
FormatUtil.FormatDouble (MatrixUtil.DotProduct (qTranspose[0], qTranspose[1]), 1, 1, 1.)
);

System.out.println();

double[][] uvTranspose = MatrixUtil.Product (MatrixUtil.Transpose (aadblUOrthonormal), aadblV);
double[][] uvTranspose = MatrixUtil.Product (MatrixUtil.Transpose (q), a);

NumberUtil.PrintMatrix (
"R CHECK #1.1",
uvTranspose
);

System.out.println();

NumberUtil.PrintMatrix (
"R CHECK #1.2",
MatrixUtil.Product (aadblUOrthonormal, uvTranspose)
);
NumberUtil.PrintMatrix ("R CHECK #1.1", uvTranspose);

System.out.println();

NumberUtil.PrintMatrix (
"R CHECK #1.3",
MatrixUtil.Product (MatrixUtil.Transpose (aadblV), aadblUOrthonormal)
);
NumberUtil.PrintMatrix ("R CHECK #1.2", MatrixUtil.Product (q, uvTranspose));

System.out.println();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/drip/sample/matrix/QRDecomposition.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static final void main (
{ 0.0, 0.4, 1.0}
};

QR qr = MatrixUtil.QRDecomposition (aadblA);
QR qr = MatrixUtil.QRDecomposition (aadblA, false);

double[][] aadblR = qr.r();

Expand Down

0 comments on commit 90ab316

Please sign in to comment.