@@ -128,6 +128,33 @@ private static final double DotProductInternal (
128
128
return dotProductInternal ;
129
129
}
130
130
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
+
131
158
/**
132
159
* Indicate if the Cell corresponds to Bottom Left Location in the Matrix
133
160
*
@@ -888,29 +915,22 @@ public static final double Sum (
888
915
/**
889
916
* Compute the Modulus of the Input Vector
890
917
*
891
- * @param adbl The Input Vector
918
+ * @param v The Input Vector
892
919
*
893
- * @return TRUE - The Modulus of the Input Vector
920
+ * @return The Modulus of the Input Vector
894
921
*
895
- * @throws java.lang. Exception Thrown if the Inputs are Invalid
922
+ * @throws Exception Thrown if the Inputs are Invalid
896
923
*/
897
924
898
925
public static final double Modulus (
899
- final double [] adbl )
900
- throws java . lang . Exception
926
+ final double [] v )
927
+ throws Exception
901
928
{
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
+ }
912
932
913
- return java . lang . Math . sqrt ( dblModulus );
933
+ return ModulusInternal ( v );
914
934
}
915
935
916
936
/**
@@ -1019,58 +1039,39 @@ public static final double[] Normalize (
1019
1039
/**
1020
1040
* Orthogonalize the Specified Matrix Using the Graham-Schmidt Method
1021
1041
*
1022
- * @param aadblV The Input Matrix
1042
+ * @param v The Input Matrix
1023
1043
*
1024
1044
* @return The Orthogonalized Matrix
1025
1045
*/
1026
1046
1027
1047
public static final double [][] GrahamSchmidtOrthogonalization (
1028
- final double [][] aadblV )
1048
+ final double [][] v )
1029
1049
{
1030
- if (null == aadblV ) return null ;
1050
+ if (null == v || 0 == v .length || v .length != v [0 ].length ) {
1051
+ return null ;
1052
+ }
1031
1053
1032
- int iSize = aadblV .length ;
1033
- double [][] aadblU = new double [iSize ][iSize ];
1054
+ double [][] vTranspose = Transpose (v );
1034
1055
1035
- if ( 0 == iSize || null == aadblV [ 0 ] || iSize != aadblV [ 0 ] .length ) return null ;
1056
+ double [][] u = new double [ vTranspose . length ][ vTranspose .length ] ;
1036
1057
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
+ }
1040
1063
1064
+ for (int i = 1 ; i < vTranspose .length ; ++i ) {
1041
1065
for (int j = 0 ; j < i ; ++j ) {
1042
- double dblProjectionAmplitude = java . lang . Double . NaN ;
1066
+ double [] projectionTrimOff = ProjectVOnUInternal ( u [ j ], vTranspose [ i ]) ;
1043
1067
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 ];
1051
1070
}
1052
-
1053
- for (int k = 0 ; k < iSize ; ++k )
1054
- aadblU [i ][k ] -= dblProjectionAmplitude * aadblU [j ][k ];
1055
1071
}
1056
1072
}
1057
1073
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 ;
1074
1075
}
1075
1076
1076
1077
/**
@@ -1084,30 +1085,17 @@ private static final double[] ProjectVOnUInternal (
1084
1085
public static final double [][] GrahamSchmidtOrthonormalization (
1085
1086
final double [][] v )
1086
1087
{
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 );
1097
1089
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 ;
1102
1092
}
1103
1093
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 ]);
1107
1096
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 ;
1111
1099
}
1112
1100
}
1113
1101
@@ -1117,37 +1105,19 @@ public static final double[][] GrahamSchmidtOrthonormalization (
1117
1105
/**
1118
1106
* Perform a QR Decomposition on the Input Matrix
1119
1107
*
1120
- * @param aadblA The Input Matrix
1108
+ * @param a The Input Matrix
1121
1109
*
1122
1110
* @return The Output of QR Decomposition
1123
1111
*/
1124
1112
1125
- public static final org . drip . numerical . linearalgebra . QR QRDecomposition (
1126
- final double [][] aadblA )
1113
+ public static final QR QRDecomposition (
1114
+ final double [][] a )
1127
1115
{
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 );
1140
1117
1141
1118
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 ) {
1151
1121
e .printStackTrace ();
1152
1122
}
1153
1123
0 commit comments