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.missingvalues; 025 026 import java.util.Arrays; 027 028 import org.ujmp.core.Coordinates; 029 import org.ujmp.core.Matrix; 030 import org.ujmp.core.doublematrix.calculation.AbstractDoubleCalculation; 031 import org.ujmp.core.exceptions.MatrixException; 032 import org.ujmp.core.matrix.SparseMatrix; 033 import org.ujmp.core.util.MathUtil; 034 035 public class AddMissing extends AbstractDoubleCalculation { 036 private static final long serialVersionUID = -8211102877475985639L; 037 038 private double[] percentMissing = null; 039 040 private Matrix missingValues = null; 041 042 public AddMissing(int dimension, Matrix source, double... percentMissing) { 043 super(dimension, source); 044 if (percentMissing.length == 1 && dimension != ALL) { 045 this.percentMissing = new double[(int) getSource().getSize(dimension)]; 046 Arrays.fill(this.percentMissing, percentMissing[0]); 047 } else { 048 this.percentMissing = percentMissing; 049 } 050 } 051 052 public double getDouble(long... coordinates) throws MatrixException { 053 if (missingValues == null) { 054 missingValues = SparseMatrix.factory.zeros(getSource().getSize()); 055 056 switch (getDimension()) { 057 case ALL: 058 int count = (int) (percentMissing[0] * Coordinates.product(getSource().getSize())); 059 for (int i = 0; i < count; i++) { 060 double v = 0.0; 061 int r = 0; 062 int c = 0; 063 do { 064 r = MathUtil.nextInteger(0, (int) getSource().getRowCount() - 1); 065 c = MathUtil.nextInteger(0, (int) getSource().getColumnCount() - 1); 066 v = missingValues.getAsDouble(r, c); 067 } while (MathUtil.isNaNOrInfinite(v)); 068 missingValues.setAsDouble(Double.NaN, r, c); 069 } 070 break; 071 case COLUMN: 072 int missingCount = (int) (getSource().getColumnCount() * percentMissing[0]); 073 for (long r = getSource().getRowCount() - 1; r != -1; r--) { 074 for (int i = 0; i < missingCount; i++) { 075 double v = 0.0; 076 int c = 0; 077 do { 078 c = MathUtil.nextInteger(0, (int) getSource().getColumnCount() - 1); 079 v = missingValues.getAsDouble(r, c); 080 } while (MathUtil.isNaNOrInfinite(v)); 081 missingValues.setAsDouble(Double.NaN, r, c); 082 } 083 } 084 } 085 } 086 if (MathUtil.isNaNOrInfinite(missingValues.getAsDouble(coordinates))) { 087 return Double.NaN; 088 } else { 089 return getSource().getAsDouble(coordinates); 090 } 091 } 092 093 }