QR decomposition: Difference between revisions
Content added Content deleted
m (Minor code improvement.) |
m (Minor code improvements.) |
||
Line 3,315: | Line 3,315: | ||
for ( int k = 0; k < columnCount && k < rowCount - 1; k++ ) { |
for ( int k = 0; k < columnCount && k < rowCount - 1; k++ ) { |
||
Matrix vectorE = new Matrix( |
Matrix vectorE = new Matrix(rowCount, 1); |
||
z1 = z.minor(k); |
z1 = z.minor(k); |
||
Matrix vectorX = z1.column(k); |
Matrix vectorX = z1.column(k); |
||
Line 3,326: | Line 3,326: | ||
vectorE.setEntry(i, 0, ( i == k ) ? 1 : 0); |
vectorE.setEntry(i, 0, ( i == k ) ? 1 : 0); |
||
} |
} |
||
vectorE = vectorE.scalarMultiply(magnitudeX).add(vectorX); |
vectorE = vectorE.scalarMultiply(magnitudeX).add(vectorX).unit(); |
||
vectorE = vectorE.unit(); |
|||
versionsOfQ.add(householderFactor(vectorE)); |
versionsOfQ.add(householderFactor(vectorE)); |
||
z = versionsOfQ.get(k).multiply(z1); |
z = versionsOfQ.get(k).multiply(z1); |
||
Line 3,348: | Line 3,347: | ||
final int size = aVector.size(); |
final int size = aVector.size(); |
||
Matrix result = new Matrix(size, size); |
|||
for ( int i = 0; i < size; i++ ) { |
for ( int i = 0; i < size; i++ ) { |
||
for ( int j = 0; j < size; j++ ) { |
for ( int j = 0; j < size; j++ ) { |
||
result.setEntry(i, j, -2 * aVector.getEntry(i, 0) * aVector.getEntry(j, 0)); |
|||
} |
} |
||
} |
} |
||
for ( int i = 0; i < size; i++ ) { |
for ( int i = 0; i < size; i++ ) { |
||
result.setEntry(i, i, result.getEntry(i, i) + 1.0); |
|||
} |
} |
||
return |
return result; |
||
} |
} |
||
Line 3,411: | Line 3,410: | ||
} |
} |
||
public Matrix(int |
public Matrix(int aRowCount, int aColumnCount) { |
||
this( new double[ |
this( new double[aRowCount][aColumnCount] ); |
||
} |
} |
||
Line 3,469: | Line 3,468: | ||
} |
} |
||
public Matrix column(int |
public Matrix column(int aIndex) { |
||
Matrix result = new Matrix(rowCount, 1); |
Matrix result = new Matrix(rowCount, 1); |
||
for ( int i = 0; i < rowCount; i++ ) { |
for ( int i = 0; i < rowCount; i++ ) { |
||
result.setEntry(i, 0, data[i][ |
result.setEntry(i, 0, data[i][aIndex]); |
||
} |
} |
||
return result; |
return result; |
||
Line 3,483: | Line 3,482: | ||
Matrix result = new Matrix(rowCount, columnCount); |
Matrix result = new Matrix(rowCount, columnCount); |
||
for ( int i = 0; i < |
for ( int i = 0; i < rowCount; i++ ) { |
||
result.data[i][0] = data[i][0] * aValue; |
result.data[i][0] = data[i][0] * aValue; |
||
} |
} |
||
Line 3,496: | Line 3,495: | ||
final double magnitude = magnitude(); |
final double magnitude = magnitude(); |
||
Matrix result = new Matrix(rowCount, columnCount); |
Matrix result = new Matrix(rowCount, columnCount); |
||
for ( int i = 0; i < |
for ( int i = 0; i < rowCount; i++ ) { |
||
result.data[i][0] = data[i][0] / magnitude; |
result.data[i][0] = data[i][0] / magnitude; |
||
} |
} |
||
Line 3,518: | Line 3,517: | ||
throw new IllegalArgumentException("Incompatible matrix dimensions."); |
throw new IllegalArgumentException("Incompatible matrix dimensions."); |
||
} |
} |
||
return |
return rowCount; |
||
} |
} |
||
Line 3,532: | Line 3,531: | ||
} |
} |
||
public double getEntry(int aRow, int |
public double getEntry(int aRow, int aColumn) { |
||
return data[aRow][ |
return data[aRow][aColumn]; |
||
} |
} |
||
public void setEntry(int aRow, int |
public void setEntry(int aRow, int aColumn, double aValue) { |
||
data[aRow][ |
data[aRow][aColumn] = aValue; |
||
} |
} |
||