Skip to content

Commit ad64618

Browse files
committed
Features:
- Matrix Util Graham Schmidt #1 (1, 2, 3) - Matrix Util Graham Schmidt #2 (4, 5, 6) - Matrix Util Graham Schmidt #3 (7, 8, 9) - Matrix Util Modulus Internal Unchecked (10, 11, 12) - Matrix Util Graham Schmidt #4 (13, 14, 15) Bug Fixes/Re-organization: - Matrix Graham Schmidt Process Run (16, 17) - Matrix Util QR Decomposition Fix (37, 38, 39) Samples: - Bartels Stewart Sylvester Solver #1 (18, 19, 20) - Bartels Stewart Sylvester Solver #2 (21, 22) - Matrix Graham Schmidt Process #1 (23, 24) - Matrix Graham Schmidt Process #2 (25, 26) - Matrix Graham Schmidt Process #3 (27, 28) - Bartels Stewart Sylvester Solver #3 (29, 30) - Matrix Graham Schmidt Process #4 (31, 32) - Matrix Graham Schmidt Process #5 (33, 34) - Bartels Stewart Sylvester Solver #4 (35, 36) - Matrix Graham Schmidt Process #6 (40, 41, 42) - Matrix Graham Schmidt Process #7 (43, 44, 45) - Matrix Graham Schmidt Process #8 (46, 47, 48) - Matrix Graham Schmidt Process #9 (49, 50, 51) - Matrix Graham Schmidt Process #10 (52, 53, 54) - Matrix Graham Schmidt Process #11 (55, 56, 57) - Matrix Graham Schmidt Process #12 (58, 59, 60) - Matrix Graham Schmidt Process #13 (61, 62, 63) - Matrix Graham Schmidt Process #14 (64, 65, 66) - Matrix Graham Schmidt Process #15 (67, 68, 69) - Matrix Graham Schmidt Process #16 (70, 71, 72) - Matrix Graham Schmidt Process #17 (73, 74, 75) - Matrix Graham Schmidt Process #18 (76, 77, 78) - Matrix Graham Schmidt Process #19 (79, 80, 81) - Bartels Stewart Sylvester Solver #5 (85, 86, 87) - Bartels Stewart Sylvester Solver #6 (88, 89, 90) IdeaDRIP: - Bartels-Stewart Algorithm (91-97) - Bartels-Stewart Algorithm The (98-117) - Bartels-Stewart Algorithm The - Computational Cost (118) - Bartels-Stewart Algorithm The - Simplifications and Special Cases (119, 120)
1 parent 094488c commit ad64618

File tree

3 files changed

+144
-96
lines changed

3 files changed

+144
-96
lines changed

ReleaseNotes/02_13_2024.txt

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
Features:
3+
4+
- Matrix Util Graham Schmidt #1 (1, 2, 3)
5+
- Matrix Util Graham Schmidt #2 (4, 5, 6)
6+
- Matrix Util Graham Schmidt #3 (7, 8, 9)
7+
- Matrix Util Modulus Internal Unchecked (10, 11, 12)
8+
- Matrix Util Graham Schmidt #4 (13, 14, 15)
9+
10+
11+
Bug Fixes/Re-organization:
12+
13+
- Matrix Graham Schmidt Process Run (16, 17)
14+
- Matrix Util QR Decomposition Fix (37, 38, 39)
15+
16+
17+
Samples:
18+
19+
- Bartels Stewart Sylvester Solver #1 (18, 19, 20)
20+
- Bartels Stewart Sylvester Solver #2 (21, 22)
21+
- Matrix Graham Schmidt Process #1 (23, 24)
22+
- Matrix Graham Schmidt Process #2 (25, 26)
23+
- Matrix Graham Schmidt Process #3 (27, 28)
24+
- Bartels Stewart Sylvester Solver #3 (29, 30)
25+
- Matrix Graham Schmidt Process #4 (31, 32)
26+
- Matrix Graham Schmidt Process #5 (33, 34)
27+
- Bartels Stewart Sylvester Solver #4 (35, 36)
28+
- Matrix Graham Schmidt Process #6 (40, 41, 42)
29+
- Matrix Graham Schmidt Process #7 (43, 44, 45)
30+
- Matrix Graham Schmidt Process #8 (46, 47, 48)
31+
- Matrix Graham Schmidt Process #9 (49, 50, 51)
32+
- Matrix Graham Schmidt Process #10 (52, 53, 54)
33+
- Matrix Graham Schmidt Process #11 (55, 56, 57)
34+
- Matrix Graham Schmidt Process #12 (58, 59, 60)
35+
- Matrix Graham Schmidt Process #13 (61, 62, 63)
36+
- Matrix Graham Schmidt Process #14 (64, 65, 66)
37+
- Matrix Graham Schmidt Process #15 (67, 68, 69)
38+
- Matrix Graham Schmidt Process #16 (70, 71, 72)
39+
- Matrix Graham Schmidt Process #17 (73, 74, 75)
40+
- Matrix Graham Schmidt Process #18 (76, 77, 78)
41+
- Matrix Graham Schmidt Process #19 (79, 80, 81)
42+
- Bartels Stewart Sylvester Solver #5 (85, 86, 87)
43+
- Bartels Stewart Sylvester Solver #6 (88, 89, 90)
44+
45+
46+
IdeaDRIP:
47+
48+
- Bartels-Stewart Algorithm (91-97)
49+
- Bartels-Stewart Algorithm The (98-117)
50+
- Bartels-Stewart Algorithm The - Computational Cost (118)
51+
- Bartels-Stewart Algorithm The - Simplifications and Special Cases (119, 120)

src/main/java/org/drip/numerical/linearalgebra/MatrixUtil.java

+66-96
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,33 @@ private static final double DotProductInternal (
128128
return dotProductInternal;
129129
}
130130

131+
private static final double[] ProjectVOnUInternal (
132+
final double[] u,
133+
final double[] v)
134+
{
135+
double vDotUOverUDotU = DotProductInternal (u, v) / DotProductInternal (u, u);
136+
137+
double[] projectVOnU = new double[u.length];
138+
139+
for (int i = 0; i < u.length; ++i) {
140+
projectVOnU[i] = vDotUOverUDotU * u[i];
141+
}
142+
143+
return projectVOnU;
144+
}
145+
146+
private static final double ModulusInternal (
147+
final double[] v)
148+
{
149+
double modulus = 0.;
150+
151+
for (int i = 0; i < v.length; ++i) {
152+
modulus += v[i] * v[i];
153+
}
154+
155+
return Math.sqrt (modulus);
156+
}
157+
131158
/**
132159
* Indicate if the Cell corresponds to Bottom Left Location in the Matrix
133160
*
@@ -888,29 +915,22 @@ public static final double Sum (
888915
/**
889916
* Compute the Modulus of the Input Vector
890917
*
891-
* @param adbl The Input Vector
918+
* @param v The Input Vector
892919
*
893-
* @return TRUE - The Modulus of the Input Vector
920+
* @return The Modulus of the Input Vector
894921
*
895-
* @throws java.lang.Exception Thrown if the Inputs are Invalid
922+
* @throws Exception Thrown if the Inputs are Invalid
896923
*/
897924

898925
public static final double Modulus (
899-
final double[] adbl)
900-
throws java.lang.Exception
926+
final double[] v)
927+
throws Exception
901928
{
902-
if (null == adbl || !org.drip.numerical.common.NumberUtil.IsValid (adbl))
903-
throw new java.lang.Exception ("MatrixUtil::Modulus => Invalid Inputs");
904-
905-
double dblModulus = 0.;
906-
int iSize = adbl.length;
907-
908-
if (0 == iSize) throw new java.lang.Exception ("MatrixUtil::Modulus => Invalid Inputs");
909-
910-
for (int i = 0; i < iSize; ++i)
911-
dblModulus += adbl[i] * adbl[i];
929+
if (null == v || 0 == v.length || !NumberUtil.IsValid (v)) {
930+
throw new Exception ("MatrixUtil::Modulus => Invalid Inputs");
931+
}
912932

913-
return java.lang.Math.sqrt (dblModulus);
933+
return ModulusInternal (v);
914934
}
915935

916936
/**
@@ -1019,58 +1039,39 @@ public static final double[] Normalize (
10191039
/**
10201040
* Orthogonalize the Specified Matrix Using the Graham-Schmidt Method
10211041
*
1022-
* @param aadblV The Input Matrix
1042+
* @param v The Input Matrix
10231043
*
10241044
* @return The Orthogonalized Matrix
10251045
*/
10261046

10271047
public static final double[][] GrahamSchmidtOrthogonalization (
1028-
final double[][] aadblV)
1048+
final double[][] v)
10291049
{
1030-
if (null == aadblV) return null;
1050+
if (null == v || 0 == v.length || v.length != v[0].length) {
1051+
return null;
1052+
}
10311053

1032-
int iSize = aadblV.length;
1033-
double[][] aadblU = new double[iSize][iSize];
1054+
double[][] vTranspose = Transpose (v);
10341055

1035-
if (0 == iSize || null == aadblV[0] || iSize != aadblV[0].length) return null;
1056+
double[][] u = new double[vTranspose.length][vTranspose.length];
10361057

1037-
for (int i = 0; i < iSize; ++i) {
1038-
for (int j = 0; j < iSize; ++j)
1039-
aadblU[i][j] = aadblV[i][j];
1058+
for (int i = 0; i < vTranspose.length; ++i) {
1059+
for (int j = 0; j < vTranspose.length; ++j) {
1060+
u[i][j] = vTranspose[i][j];
1061+
}
1062+
}
10401063

1064+
for (int i = 1; i < vTranspose.length; ++i) {
10411065
for (int j = 0; j < i; ++j) {
1042-
double dblProjectionAmplitude = java.lang.Double.NaN;
1066+
double[] projectionTrimOff = ProjectVOnUInternal (u[j], vTranspose[i]);
10431067

1044-
try {
1045-
dblProjectionAmplitude = DotProduct (aadblV[i], aadblU[j]) / DotProduct (aadblU[j],
1046-
aadblU[j]);
1047-
} catch (java.lang.Exception e) {
1048-
e.printStackTrace();
1049-
1050-
return null;
1068+
for (int k = 0; k < projectionTrimOff.length; ++k) {
1069+
u[i][k] -= projectionTrimOff[k];
10511070
}
1052-
1053-
for (int k = 0; k < iSize; ++k)
1054-
aadblU[i][k] -= dblProjectionAmplitude * aadblU[j][k];
10551071
}
10561072
}
10571073

1058-
return aadblU;
1059-
}
1060-
1061-
private static final double[] ProjectVOnUInternal (
1062-
final double[] u,
1063-
final double[] v)
1064-
{
1065-
double vDotUOverUDotU = DotProductInternal (u, v) / DotProductInternal (u, u);
1066-
1067-
double[] projectVOnU = new double[u.length];
1068-
1069-
for (int i = 0; i < u.length; ++i) {
1070-
projectVOnU[i] = vDotUOverUDotU * u[i];
1071-
}
1072-
1073-
return projectVOnU;
1074+
return u;
10741075
}
10751076

10761077
/**
@@ -1084,30 +1085,17 @@ private static final double[] ProjectVOnUInternal (
10841085
public static final double[][] GrahamSchmidtOrthonormalization (
10851086
final double[][] v)
10861087
{
1087-
if (null == v || 0 == v.length || v.length != v[0].length) {
1088-
return null;
1089-
}
1090-
1091-
double[] uDotProduct = new double[v.length];
1092-
double[][] u = new double[v.length][v.length];
1093-
1094-
for (int i = 0; i < v.length; ++i) {
1095-
uDotProduct[0] += u[0][i] * u[0][i];
1096-
}
1088+
double[][] u = GrahamSchmidtOrthogonalization (v);
10971089

1098-
for (int i = 0; i < v.length; ++i) {
1099-
for (int j = 0; j < v.length; ++j) {
1100-
u[i][j] = v[i][j];
1101-
}
1090+
if (null == u) {
1091+
return null;
11021092
}
11031093

1104-
for (int i = 1; i < v.length; ++i) {
1105-
for (int j = 0; j < i; ++j) {
1106-
double[] projectionTrimOff = ProjectVOnUInternal (u[j], v[i]);
1094+
for (int i = 0; i < u.length; ++i) {
1095+
double modulusReciprocal = 1. / ModulusInternal (u[i]);
11071096

1108-
for (int k = 0; k < projectionTrimOff.length; ++k) {
1109-
u[i][j] -= projectionTrimOff[k];
1110-
}
1097+
for (int j = 0; j < u.length; ++j) {
1098+
u[i][j] *= modulusReciprocal;
11111099
}
11121100
}
11131101

@@ -1117,37 +1105,19 @@ public static final double[][] GrahamSchmidtOrthonormalization (
11171105
/**
11181106
* Perform a QR Decomposition on the Input Matrix
11191107
*
1120-
* @param aadblA The Input Matrix
1108+
* @param a The Input Matrix
11211109
*
11221110
* @return The Output of QR Decomposition
11231111
*/
11241112

1125-
public static final org.drip.numerical.linearalgebra.QR QRDecomposition (
1126-
final double[][] aadblA)
1113+
public static final QR QRDecomposition (
1114+
final double[][] a)
11271115
{
1128-
double[][] aadblQ = GrahamSchmidtOrthonormalization (aadblA);
1129-
1130-
if (null == aadblQ) return null;
1131-
1132-
for (int i = 0; i < aadblQ.length; ++i) {
1133-
System.out.println (
1134-
"\t| Matrix GSOQ => [" + NumberUtil.ArrayRow (aadblQ[i], 2, 4, false) + " ]||"
1135-
);
1136-
}
1137-
1138-
int iSize = aadblQ.length;
1139-
double[][] aadblR = new double[iSize][iSize];
1116+
double[][] q = GrahamSchmidtOrthonormalization (a);
11401117

11411118
try {
1142-
/* for (int i = 0; i < iSize; ++i) {
1143-
for (int j = 0; j < iSize; ++j)
1144-
aadblR[i][j] = i > j ? DotProduct (aadblQ[i], aadblA[j]) : 0.;
1145-
} */
1146-
1147-
aadblR = Product (Transpose (aadblQ), aadblA);
1148-
1149-
return new org.drip.numerical.linearalgebra.QR (aadblQ, aadblR);
1150-
} catch (java.lang.Exception e) {
1119+
return null == q ? null : new QR (q, Product (q, a));
1120+
} catch (Exception e) {
11511121
e.printStackTrace();
11521122
}
11531123

src/main/java/org/drip/sample/matrix/GrahamSchmidtProcess.java

+27
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ public static final void main (
123123
{7, 0, 4, 5}
124124
}; */
125125

126+
/* double[][] aadblV = new double[][] {
127+
{ 12, 6, -4},
128+
{-51, 167, 24},
129+
{ 4, -68, -41}
130+
}; */
131+
126132
double[][] aadblV = new double[][] {
127133
{12, -51, 4},
128134
{ 6, 167, -68},
@@ -165,6 +171,27 @@ public static final void main (
165171
)
166172
);
167173

174+
System.out.println();
175+
176+
double[][] uvTranspose = MatrixUtil.Product (aadblUOrthonormal, aadblV);
177+
178+
NumberUtil.PrintMatrix (
179+
"R CHECK #1.1",
180+
uvTranspose
181+
);
182+
183+
System.out.println();
184+
185+
NumberUtil.PrintMatrix (
186+
"R CHECK #1.2",
187+
MatrixUtil.Product (MatrixUtil.Transpose (aadblUOrthonormal), uvTranspose)
188+
);
189+
190+
/* NumberUtil.PrintMatrix (
191+
"R CHECK #2",
192+
MatrixUtil.Product (aadblV, MatrixUtil.Transpose (aadblUOrthonormal))
193+
); */
194+
168195
EnvManager.TerminateEnv();
169196
}
170197
}

0 commit comments

Comments
 (0)