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 SolveSPD<T> extends Solve<T> {
031    
032            public static int SQUARETHRESHOLD = 100;
033    
034            public T calc(T a, T b);
035    
036            public static final SolveSPD<Matrix> MATRIX = new SolveSPD<Matrix>() {
037    
038                    public final Matrix calc(Matrix a, Matrix b) {
039                            if (UJMPSettings.getNumberOfThreads() == 1) {
040                                    if (a.getRowCount() >= SQUARETHRESHOLD && a.getColumnCount() >= SQUARETHRESHOLD) {
041                                            return MATRIXSQUARELARGESINGLETHREADED.calc(a, b);
042                                    } else {
043                                            return MATRIXSQUARESMALLSINGLETHREADED.calc(a, b);
044                                    }
045                            } else {
046                                    if (a.getRowCount() >= SQUARETHRESHOLD && a.getColumnCount() >= SQUARETHRESHOLD) {
047                                            return MATRIXSQUARELARGEMULTITHREADED.calc(a, b);
048                                    } else {
049                                            return MATRIXSQUARESMALLMULTITHREADED.calc(a, b);
050                                    }
051                            }
052                    }
053            };
054    
055            public static final SolveSPD<Matrix> INSTANCE = MATRIX;
056    
057            public static final SolveSPD<Matrix> UJMPSQUARE = new SolveSPD<Matrix>() {
058    
059                    public final Matrix calc(Matrix a, Matrix b) {
060                            return Chol.INSTANCE.solve(a, b);
061                    }
062            };
063    
064            public static final SolveSPD<Matrix> MATRIXSQUARESMALLSINGLETHREADED = UJMPSQUARE;
065    
066            public static final SolveSPD<Matrix> MATRIXSQUARELARGESINGLETHREADED = new SolveSPD<Matrix>() {
067                    public final Matrix calc(Matrix a, Matrix b) {
068                            SolveSPD<Matrix> solve = null;
069                            if (UJMPSettings.isUseJBlas()) {
070                                    solve = DecompositionOps.SOLVESPD_JBLAS;
071                            }
072                            if (solve == null && UJMPSettings.isUseOjalgo()) {
073                                    solve = DecompositionOps.SOLVESPD_OJALGO;
074                            }
075                            if (solve == null && UJMPSettings.isUseEJML()) {
076                                    solve = DecompositionOps.SOLVESPD_EJML;
077                            }
078                            if (solve == null && UJMPSettings.isUseMTJ()) {
079                                    solve = DecompositionOps.SOLVESPD_MTJ;
080                            }
081                            if (solve == null) {
082                                    solve = UJMPSQUARE;
083                            }
084                            return solve.calc(a, b);
085                    }
086            };
087    
088            public static final SolveSPD<Matrix> MATRIXSQUARELARGEMULTITHREADED = new SolveSPD<Matrix>() {
089                    public Matrix calc(Matrix a, Matrix b) {
090                            SolveSPD<Matrix> solve = null;
091                            if (UJMPSettings.isUseJBlas()) {
092                                    solve = DecompositionOps.SOLVESPD_JBLAS;
093                            }
094                            if (solve == null && UJMPSettings.isUseOjalgo()) {
095                                    solve = DecompositionOps.SOLVESPD_OJALGO;
096                            }
097                            if (solve == null && UJMPSettings.isUseEJML()) {
098                                    solve = DecompositionOps.SOLVESPD_EJML;
099                            }
100                            if (solve == null) {
101                                    solve = UJMPSQUARE;
102                            }
103                            return solve.calc(a, b);
104                    }
105            };
106    
107            public static final SolveSPD<Matrix> MATRIXSQUARESMALLMULTITHREADED = UJMPSQUARE;
108    
109    }