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 import org.ujmp.core.util.MathUtil; 032 033 public class Prod extends AbstractDoubleCalculation { 034 private static final long serialVersionUID = 932987805882530211L; 035 036 boolean ignoreNaN = false; 037 038 public Prod(int dimension, boolean ignoreNaN, Matrix matrix) { 039 super(dimension, matrix); 040 this.ignoreNaN = ignoreNaN; 041 Annotation aold = matrix.getAnnotation(); 042 if (aold != null) { 043 Annotation a = new DefaultAnnotation(getSize()); 044 a.setMatrixAnnotation(aold.getMatrixAnnotation()); 045 if (dimension == ROW) { 046 a.setDimensionMatrix(ROW, aold.getDimensionMatrix(ROW)); 047 } else if (dimension == COLUMN) { 048 a.setDimensionMatrix(COLUMN, aold.getDimensionMatrix(COLUMN)); 049 } 050 setAnnotation(a); 051 } 052 } 053 054 public double getDouble(long... coordinates) throws MatrixException { 055 double prod = 1; 056 057 if (ignoreNaN) { 058 059 switch (getDimension()) { 060 case ROW: 061 for (long r = getSource().getSize()[ROW] - 1; r != -1; r--) { 062 double v = getSource().getAsDouble(r, coordinates[COLUMN]); 063 if (!MathUtil.isNaNOrInfinite(v)) { 064 prod *= v; 065 } 066 } 067 return prod; 068 case COLUMN: 069 for (long c = getSource().getSize()[COLUMN] - 1; c != -1; c--) { 070 double v = getSource().getAsDouble(coordinates[ROW], c); 071 if (!MathUtil.isNaNOrInfinite(v)) { 072 prod *= v; 073 } 074 } 075 return prod; 076 case ALL: 077 for (long r = getSource().getSize()[ROW] - 1; r != -1; r--) { 078 for (long c = getSource().getSize()[COLUMN] - 1; c != -1; c--) { 079 double v = getSource().getAsDouble(r, c); 080 if (!MathUtil.isNaNOrInfinite(v)) { 081 prod *= v; 082 } 083 } 084 } 085 return prod; 086 default: 087 throw new MatrixException("dimension not allowed"); 088 } 089 } else { 090 switch (getDimension()) { 091 case ROW: 092 for (long r = getSource().getSize()[ROW] - 1; r != -1; r--) { 093 prod *= getSource().getAsDouble(r, coordinates[COLUMN]); 094 } 095 return prod; 096 case COLUMN: 097 for (long c = getSource().getSize()[COLUMN] - 1; c != -1; c--) { 098 prod *= getSource().getAsDouble(coordinates[ROW], c); 099 } 100 return prod; 101 case ALL: 102 for (long r = getSource().getSize()[ROW] - 1; r != -1; r--) { 103 for (long c = getSource().getSize()[COLUMN] - 1; c != -1; c--) { 104 prod *= getSource().getAsDouble(r, c); 105 } 106 } 107 return prod; 108 default: 109 throw new MatrixException("dimension not allowed"); 110 } 111 } 112 113 } 114 115 public long[] getSize() { 116 switch (getDimension()) { 117 case ROW: 118 return new long[] { 1, getSource().getSize()[COLUMN] }; 119 case COLUMN: 120 return new long[] { getSource().getSize()[ROW], 1 }; 121 case ALL: 122 return new long[] { 1, 1 }; 123 } 124 return null; 125 } 126 127 }