Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

la4j - Linear Algebra for Java

, 14 Nov 2011
Elegant and pure Java matrix library
/*
 * 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;

public class CholeskyDecompositor implements MatrixDecompositor {

	@Override
	public Matrix[] decompose(Matrix matrix, Factory factory) throws MatrixDecompositionException {
		
		// TOOD: add check symmetric & positive defined 
		
		if (matrix.rows() != matrix.columns()) throw new MatrixDecompositionException();
		
		Matrix u = factory.createMatrix(matrix.rows(), matrix.rows());  
		
		for (int j = 0; j < u.rows(); j++) {
			
			double d = 0.0;

			for (int k = 0; k < j; k++) {
				
				double s = 0.0;

				for (int i = 0; i < k; i++) {
					s += u.get(k, i) * u.get(j, i);
				}

				s = (matrix.get(j, k) - s) / u.get(k, k);

				u.set(j, k, s);
				
				d = d + s * s;
			}
			
			d = matrix.get(j, j) - d;
			
			u.set(j, j, Math.sqrt(Math.max(d, 0.0)));
			
			for (int k = j + 1; k < u.rows(); k++) {
				u.set(j, k, 0.0);
			}
		}

		return new Matrix[] { 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vladimir Kostyukov
Software Developer Intel
Russian Federation Russian Federation
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.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 14 Nov 2011
Article Copyright 2011 by Vladimir Kostyukov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid