- la4j-0.2.0.zip
- la4j
- bin
- la4j-0.2.0.jar
- LICENSE
- README
- src
- main
- java
- la4j
- decomposition
- err
- factory
- inversion
- io
- linear
- matrix
- util
- vector
- test
- java
- la4j
- decomposition
- inversion
- linear
- matrix
- vector
|
/*
* Copyright 2011, Vladimir Kostyukov
*
* This file is part of la4j project (http://la4j.googlecode.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package la4j.decomposition;
import la4j.err.MatrixDecompositionException;
import la4j.factory.Factory;
import la4j.matrix.Matrix;
import la4j.vector.Vector;
public class LUDecompositor implements MatrixDecompositor {
@Override
public Matrix[] decompose(Matrix matrix, Factory factory) throws MatrixDecompositionException {
if (matrix.rows() != matrix.columns()) throw new MatrixDecompositionException();
Matrix lu = matrix.clone();
for (int j = 0; j < lu.columns(); j++) {
Vector jcolumn = lu.getColumn(j);
for (int i = 0; i < lu.rows(); i++) {
int kmax = Math.min(i, j);
double s = 0.0;
for (int k = 0; k < kmax; k++) {
s += lu.get(i, k) * jcolumn.get(k);
}
jcolumn.set(i, jcolumn.get(i) - s);
lu.set(i, j, jcolumn.get(i));
}
int p = j;
for (int i = j + 1; i < lu.rows(); i++) {
if (Math.abs(jcolumn.get(i)) > Math.abs(jcolumn.get(p))) p = i;
}
if (p != j) {
for (int k = 0; k < lu.columns(); k++) {
double t = lu.get(p, k); lu.set(p, k, lu.get(j, k)); lu.set(j, k, t);
}
}
if (j < lu.rows() & lu.get(j, j) != 0.0) {
for (int i = j + 1; i < lu.rows(); i++) {
lu.set(i, j, lu.get(i, j) / lu.get(j, j));
}
}
}
Matrix l = factory.createMatrix(lu.rows(), lu.columns());
for (int i = 0; i < l.rows(); i++) {
for (int j = 0; j <= i; j++) {
if (i > j) l.set(i, j, lu.get(i, j));
else if (i == j) l.set(i, j, 1.0);
}
}
Matrix u = factory.createMatrix(lu.columns(), lu.columns());
for (int i = 0; i < u.columns(); i++) {
for (int j = i; j < u.columns(); j++) {
if (i <= j) u.set(i, j, lu.get(i, j));
}
}
return new Matrix[] { l, u };
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've received a Master's Degree in Computer Science at the IT Faculty of Altai State Technical University, Russia.
Currently, I am Software Engineer at Intel Corporation.