001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.math.matrix;
031
032import no.uib.cipr.matrix.NotConvergedException;
033import Jama.Matrix;
034
035/**
036 * Methods for calculating the Moore-Penrose Pseudo-Inverse
037 * 
038 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
039 * 
040 */
041public class PseudoInverse {
042        /**
043         * Compute the Moore-Penrose Pseudo-Inverse.
044         * 
045         * @param matrix
046         *            The matrix to invert.
047         * @return the pseudo-inverse.
048         */
049        public static Matrix pseudoInverse(Matrix matrix) {
050                final no.uib.cipr.matrix.DenseMatrix mjtA = new no.uib.cipr.matrix.DenseMatrix(matrix.getArray());
051                no.uib.cipr.matrix.SVD svd;
052
053                try {
054                        svd = no.uib.cipr.matrix.SVD.factorize(mjtA);
055                } catch (final NotConvergedException e) {
056                        throw new RuntimeException(e);
057                }
058
059                final Matrix Sinv = new Matrix(matrix.getColumnDimension(), matrix.getRowDimension());
060
061                final double[] Sarr = svd.getS();
062                for (int i = 0; i < svd.getS().length; i++) {
063                        if (Sarr[i] != 0)
064                                Sinv.set(i, i, 1.0 / Sarr[i]);
065                }
066
067                final Matrix Vt = new Matrix(svd.getVt().numRows(), svd.getVt().numColumns());
068                for (int r = 0; r < svd.getVt().numRows(); r++) {
069                        for (int c = 0; c < svd.getVt().numColumns(); c++) {
070                                Vt.set(r, c, svd.getVt().get(r, c));
071                        }
072                }
073
074                final Matrix U = new Matrix(svd.getU().numRows(), svd.getU().numColumns());
075                for (int r = 0; r < svd.getU().numRows(); r++) {
076                        for (int c = 0; c < svd.getU().numColumns(); c++) {
077                                U.set(r, c, svd.getU().get(r, c));
078                        }
079                }
080
081                final Matrix pinv = Vt.transpose().times(Sinv).times(U.transpose());
082
083                return pinv;
084        }
085
086        /**
087         * Compute the lower-rank approximation of the Moore-Penrose Pseudo-Inverse.
088         * 
089         * @param matrix
090         *            The matrix to invert.
091         * @param rank
092         *            the desired rank.
093         * @return the pseudo-inverse.
094         */
095        public static Matrix pseudoInverse(Matrix matrix, int rank) {
096                return pseudoInverse(new JamaDenseMatrix(matrix), rank);
097        }
098
099        /**
100         * Compute the lower-rank approximation of the Moore-Penrose Pseudo-Inverse.
101         * 
102         * @param matrix
103         *            The matrix to invert.
104         * @param rank
105         *            the desired rank.
106         * @return the pseudo-inverse.
107         */
108        public static Matrix pseudoInverse(ch.akuhn.matrix.Matrix matrix, int rank) {
109                final ThinSingularValueDecomposition tsvd = new ThinSingularValueDecomposition(matrix, rank);
110
111                final Matrix Sinv = new Matrix(tsvd.S.length, tsvd.S.length);
112                for (int i = 0; i < tsvd.S.length; i++) {
113                        if (tsvd.S[i] != 0)
114                                Sinv.set(i, i, 1.0 / tsvd.S[i]);
115                }
116
117                final Matrix pinv = tsvd.Vt.transpose().times(Sinv).times(tsvd.U.transpose());
118
119                return pinv;
120        }
121}