001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.doublematrix.calculation.general.decomposition; 025 026 import org.ujmp.core.Matrix; 027 import org.ujmp.core.doublematrix.DenseDoubleMatrix2D; 028 import org.ujmp.core.exceptions.MatrixException; 029 import org.ujmp.core.util.DecompositionOps; 030 import org.ujmp.core.util.UJMPSettings; 031 032 public interface Inv<T> { 033 034 public static int THRESHOLD = 100; 035 036 public T calc(T source); 037 038 public static final Inv<Matrix> MATRIX = new Inv<Matrix>() { 039 040 public final Matrix calc(Matrix source) { 041 if (source.getDimensionCount() != 2 || source.getRowCount() != source.getColumnCount()) { 042 throw new MatrixException( 043 "inverse only possible for square matrices. use pinv or ginv instead"); 044 } 045 if (UJMPSettings.getNumberOfThreads() == 1) { 046 if (source.getRowCount() >= THRESHOLD && source.getColumnCount() >= THRESHOLD) { 047 return MATRIXLARGESINGLETHREADED.calc(source); 048 } else { 049 return MATRIXSMALLSINGLETHREADED.calc(source); 050 } 051 } else { 052 if (source.getRowCount() >= THRESHOLD && source.getColumnCount() >= THRESHOLD) { 053 return MATRIXLARGEMULTITHREADED.calc(source); 054 } else { 055 return MATRIXSMALLMULTITHREADED.calc(source); 056 } 057 } 058 } 059 }; 060 061 public final Inv<Matrix> UJMP = new Inv<Matrix>() { 062 public final Matrix calc(Matrix source) { 063 DenseDoubleMatrix2D b = DenseDoubleMatrix2D.factory.zeros(source.getRowCount(), source 064 .getRowCount()); 065 for (int i = (int) source.getRowCount(); --i >= 0;) { 066 b.setDouble(1.0, i, i); 067 } 068 return LU.INSTANCE.solve(source, b); 069 } 070 }; 071 072 public final Inv<Matrix> INSTANCE = MATRIX; 073 074 public static final Inv<Matrix> MATRIXSMALLSINGLETHREADED = UJMP; 075 076 public static final Inv<Matrix> MATRIXLARGESINGLETHREADED = new Inv<Matrix>() { 077 public final Matrix calc(Matrix source) { 078 Inv<Matrix> inv = null; 079 if (UJMPSettings.isUseJBlas()) { 080 inv = DecompositionOps.INV_JBLAS; 081 } 082 if (inv == null && UJMPSettings.isUseOjalgo()) { 083 inv = DecompositionOps.INV_OJALGO; 084 } 085 if (inv == null && UJMPSettings.isUseEJML()) { 086 inv = DecompositionOps.INV_EJML; 087 } 088 if (inv == null && UJMPSettings.isUseMTJ()) { 089 inv = DecompositionOps.INV_MTJ; 090 } 091 if (inv == null) { 092 inv = UJMP; 093 } 094 return inv.calc(source); 095 } 096 }; 097 098 public static final Inv<Matrix> MATRIXLARGEMULTITHREADED = new Inv<Matrix>() { 099 public Matrix calc(Matrix source) { 100 Inv<Matrix> inv = null; 101 if (UJMPSettings.isUseJBlas()) { 102 inv = DecompositionOps.INV_JBLAS; 103 } 104 if (inv == null && UJMPSettings.isUseOjalgo()) { 105 inv = DecompositionOps.INV_OJALGO; 106 } 107 if (inv == null && UJMPSettings.isUseEJML()) { 108 inv = DecompositionOps.INV_EJML; 109 } 110 if (inv == null && UJMPSettings.isUseMTJ()) { 111 inv = DecompositionOps.INV_MTJ; 112 } 113 if (inv == null) { 114 inv = UJMP; 115 } 116 return inv.calc(source); 117 } 118 }; 119 120 public static final Inv<Matrix> MATRIXSMALLMULTITHREADED = UJMP; 121 122 }