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.statistical;
025    
026    import org.ujmp.core.Matrix;
027    import org.ujmp.core.annotation.Annotation;
028    import org.ujmp.core.annotation.DefaultAnnotation;
029    import org.ujmp.core.doublematrix.calculation.AbstractDoubleCalculation;
030    import org.ujmp.core.exceptions.MatrixException;
031    
032    public class Max extends AbstractDoubleCalculation {
033            private static final long serialVersionUID = -132801357103800951L;
034    
035            public Max(int dimension, Matrix matrix) {
036                    super(dimension, matrix);
037                    Annotation aold = matrix.getAnnotation();
038                    if (aold != null) {
039                            Annotation a = new DefaultAnnotation(getSize());
040                            a.setMatrixAnnotation(aold.getMatrixAnnotation());
041                            if (dimension == ROW) {
042                                    a.setDimensionMatrix(ROW, aold.getDimensionMatrix(ROW));
043                            } else if (dimension == COLUMN) {
044                                    a.setDimensionMatrix(COLUMN, aold.getDimensionMatrix(COLUMN));
045                            }
046                            setAnnotation(a);
047                    }
048            }
049    
050            public double getDouble(long... coordinates) throws MatrixException {
051                    double max = -Double.MAX_VALUE;
052                    switch (getDimension()) {
053                    case ROW:
054                            for (long r = getSource().getSize()[ROW] - 1; r != -1; r--) {
055                                    double v = getSource().getAsDouble(r, coordinates[COLUMN]);
056                                    if (v > max) {
057                                            max = v;
058                                    }
059                            }
060                            max = max == -Double.MAX_VALUE ? Double.NaN : max;
061                            return max;
062                    case COLUMN:
063                            for (long c = getSource().getSize()[COLUMN] - 1; c != -1; c--) {
064                                    double v = getSource().getAsDouble(coordinates[ROW], c);
065                                    if (v > max) {
066                                            max = v;
067                                    }
068                            }
069                            max = max == -Double.MAX_VALUE ? Double.NaN : max;
070                            return max;
071                    case ALL:
072                            for (long r = getSource().getSize()[ROW] - 1; r != -1; r--) {
073                                    for (long c = getSource().getSize()[COLUMN] - 1; c != -1; c--) {
074                                            double v = getSource().getAsDouble(r, c);
075                                            if (v > max) {
076                                                    max = v;
077                                            }
078                                    }
079                            }
080                            max = max == -Double.MAX_VALUE ? Double.NaN : max;
081                            return max;
082                    }
083                    return 0.0;
084            }
085    
086            public long[] getSize() {
087                    switch (getDimension()) {
088                    case ROW:
089                            return new long[] { 1, getSource().getSize()[COLUMN] };
090                    case COLUMN:
091                            return new long[] { getSource().getSize()[ROW], 1 };
092                    case ALL:
093                            return new long[] { 1, 1 };
094                    default:
095                            throw new MatrixException("dimension not supported: " + getDimension());
096                    }
097            }
098    
099            public static double calc(Matrix m) throws MatrixException {
100                    double max = -Double.MAX_VALUE;
101                    double v = 0.0;
102                    for (long[] c : m.availableCoordinates()) {
103                            max = (v = m.getAsDouble(c)) > max ? v : max;
104                    }
105                    max = max == -Double.MAX_VALUE ? Double.NaN : max;
106                    return max;
107            }
108    }