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.util.DecompositionOps;
028    import org.ujmp.core.util.UJMPSettings;
029    
030    public interface Solve<T> {
031    
032            public static int SQUARETHRESHOLD = 100;
033    
034            public static int TALLTHRESHOLD = 100;
035    
036            public T calc(T a, T b);
037    
038            public static final Solve<Matrix> MATRIX = new Solve<Matrix>() {
039    
040                    public final Matrix calc(Matrix a, Matrix b) {
041                            if (a.isSquare()) {
042                                    if (UJMPSettings.getNumberOfThreads() == 1) {
043                                            if (a.getRowCount() >= SQUARETHRESHOLD && a.getColumnCount() >= SQUARETHRESHOLD) {
044                                                    return MATRIXSQUARELARGESINGLETHREADED.calc(a, b);
045                                            } else {
046                                                    return MATRIXSQUARESMALLSINGLETHREADED.calc(a, b);
047                                            }
048                                    } else {
049                                            if (a.getRowCount() >= SQUARETHRESHOLD && a.getColumnCount() >= SQUARETHRESHOLD) {
050                                                    return MATRIXSQUARELARGEMULTITHREADED.calc(a, b);
051                                            } else {
052                                                    return MATRIXSQUARESMALLMULTITHREADED.calc(a, b);
053                                            }
054                                    }
055                            } else {
056                                    if (UJMPSettings.getNumberOfThreads() == 1) {
057                                            if (a.getRowCount() >= TALLTHRESHOLD && a.getColumnCount() >= TALLTHRESHOLD) {
058                                                    return MATRIXTALLLARGESINGLETHREADED.calc(a, b);
059                                            } else {
060                                                    return MATRIXTALLSMALLSINGLETHREADED.calc(a, b);
061                                            }
062                                    } else {
063                                            if (a.getRowCount() >= TALLTHRESHOLD && a.getColumnCount() >= TALLTHRESHOLD) {
064                                                    return MATRIXTALLLARGEMULTITHREADED.calc(a, b);
065                                            } else {
066                                                    return MATRIXTALLSMALLMULTITHREADED.calc(a, b);
067                                            }
068                                    }
069                            }
070                    }
071            };
072    
073            public static final Solve<Matrix> INSTANCE = MATRIX;
074    
075            public static final Solve<Matrix> UJMPSQUARE = new Solve<Matrix>() {
076    
077                    public final Matrix calc(Matrix a, Matrix b) {
078                            return LU.INSTANCE.solve(a, b);
079                    }
080            };
081    
082            public static final Solve<Matrix> UJMPTALL = new Solve<Matrix>() {
083    
084                    public final Matrix calc(Matrix a, Matrix b) {
085                            return QR.INSTANCE.solve(a, b);
086                    }
087            };
088    
089            public static final Solve<Matrix> MATRIXSQUARESMALLSINGLETHREADED = UJMPSQUARE;
090    
091            public static final Solve<Matrix> MATRIXTALLSMALLSINGLETHREADED = UJMPTALL;
092    
093            public static final Solve<Matrix> MATRIXSQUARELARGESINGLETHREADED = new Solve<Matrix>() {
094                    public final Matrix calc(Matrix a, Matrix b) {
095                            Solve<Matrix> solve = null;
096                            if (UJMPSettings.isUseJBlas()) {
097                                    solve = DecompositionOps.SOLVE_JBLAS;
098                            }
099                            if (solve == null && UJMPSettings.isUseOjalgo()) {
100                                    solve = DecompositionOps.SOLVE_OJALGO;
101                            }
102                            if (solve == null && UJMPSettings.isUseEJML()) {
103                                    solve = DecompositionOps.SOLVE_EJML;
104                            }
105                            if (solve == null && UJMPSettings.isUseMTJ()) {
106                                    solve = DecompositionOps.SOLVE_MTJ;
107                            }
108                            if (solve == null) {
109                                    solve = UJMPSQUARE;
110                            }
111                            return solve.calc(a, b);
112                    }
113            };
114    
115            public static final Solve<Matrix> MATRIXTALLLARGESINGLETHREADED = new Solve<Matrix>() {
116                    public final Matrix calc(Matrix a, Matrix b) {
117                            Solve<Matrix> solve = null;
118                            if (UJMPSettings.isUseOjalgo()) {
119                                    solve = DecompositionOps.SOLVE_OJALGO;
120                            }
121                            if (solve == null && UJMPSettings.isUseEJML()) {
122                                    solve = DecompositionOps.SOLVE_EJML;
123                            }
124                            if (solve == null && UJMPSettings.isUseMTJ()) {
125                                    solve = DecompositionOps.SOLVE_MTJ;
126                            }
127                            if (solve == null) {
128                                    solve = UJMPTALL;
129                            }
130                            return solve.calc(a, b);
131                    }
132            };
133    
134            public static final Solve<Matrix> MATRIXSQUARELARGEMULTITHREADED = new Solve<Matrix>() {
135                    public Matrix calc(Matrix a, Matrix b) {
136                            Solve<Matrix> solve = null;
137                            if (UJMPSettings.isUseJBlas()) {
138                                    solve = DecompositionOps.SOLVE_JBLAS;
139                            }
140                            if (solve == null && UJMPSettings.isUseOjalgo()) {
141                                    solve = DecompositionOps.SOLVE_OJALGO;
142                            }
143                            if (solve == null && UJMPSettings.isUseEJML()) {
144                                    solve = DecompositionOps.SOLVE_EJML;
145                            }
146                            if (solve == null) {
147                                    solve = UJMPSQUARE;
148                            }
149                            return solve.calc(a, b);
150                    }
151            };
152    
153            public static final Solve<Matrix> MATRIXTALLLARGEMULTITHREADED = new Solve<Matrix>() {
154                    public Matrix calc(Matrix a, Matrix b) {
155                            Solve<Matrix> solve = null;
156                            if (UJMPSettings.isUseParallelColt()) {
157                                    solve = DecompositionOps.SOLVE_PARALLELCOLT;
158                            }
159                            if (UJMPSettings.isUseOjalgo()) {
160                                    solve = DecompositionOps.SOLVE_OJALGO;
161                            }
162                            if (solve == null && UJMPSettings.isUseEJML()) {
163                                    solve = DecompositionOps.SOLVE_EJML;
164                            }
165                            if (solve == null) {
166                                    solve = UJMPTALL;
167                            }
168                            return solve.calc(a, b);
169                    }
170            };
171    
172            public static final Solve<Matrix> MATRIXSQUARESMALLMULTITHREADED = UJMPSQUARE;
173    
174            public static final Solve<Matrix> MATRIXTALLSMALLMULTITHREADED = UJMPTALL;
175    }