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.misc; 025 026 import java.util.ArrayList; 027 import java.util.HashSet; 028 import java.util.List; 029 import java.util.Set; 030 031 import org.ujmp.core.Matrix; 032 import org.ujmp.core.doublematrix.calculation.AbstractDoubleCalculation; 033 import org.ujmp.core.exceptions.MatrixException; 034 import org.ujmp.core.util.MathUtil; 035 036 public class DiscretizeToColumns extends AbstractDoubleCalculation { 037 private static final long serialVersionUID = -3606534079672701424L; 038 039 private long column = 0; 040 041 private List<Object> values = null; 042 043 private boolean ignoreNaN = false; 044 045 public DiscretizeToColumns(Matrix matrix, boolean ignoreNaN, long column) { 046 super(matrix); 047 this.column = column; 048 this.ignoreNaN = ignoreNaN; 049 } 050 051 052 public long[] getSize() { 053 try { 054 countValues(); 055 } catch (MatrixException e) { 056 // TODO Auto-generated catch block 057 e.printStackTrace(); 058 } 059 long[] size = getSource().getSize(); 060 size[COLUMN] += values.size() - 1; 061 return size; 062 } 063 064 065 public double getDouble(long... coordinates) throws MatrixException { 066 countValues(); 067 if (coordinates[COLUMN] < column) { 068 return getSource().getAsDouble(coordinates); 069 } else if (coordinates[COLUMN] >= column + values.size()) { 070 long col = coordinates[COLUMN] - values.size() + 1; 071 return getSource().getAsDouble(coordinates[ROW], col); 072 } else { 073 Object o = getSource().getAsObject(coordinates[ROW], column); 074 if (ignoreNaN) { 075 if (MathUtil.isNaNOrInfinite(o)) { 076 return 0.0; 077 } else { 078 int index = values.indexOf(o); 079 long col = coordinates[COLUMN] - column; 080 if (index == col) { 081 return 1.0; 082 } else { 083 return 0.0; 084 } 085 } 086 } else { 087 if (MathUtil.isNaNOrInfinite(o)) { 088 return Double.NaN; 089 } else { 090 int index = values.indexOf(o); 091 long col = coordinates[COLUMN] - column; 092 if (index == col) { 093 return 1.0; 094 } else { 095 return 0.0; 096 } 097 } 098 } 099 100 } 101 } 102 103 private void countValues() throws MatrixException { 104 if (values == null) { 105 Set<Object> set = new HashSet<Object>(); 106 for (long row = getSource().getRowCount(); --row >= 0;) { 107 Object o = getSource().getAsObject(row, column); 108 if (ignoreNaN) { 109 if (MathUtil.isNaNOrInfinite(o)) { 110 set.add(Double.valueOf(0.0)); 111 } else { 112 set.add(o); 113 } 114 } else { 115 if (!MathUtil.isNaNOrInfinite(o)) { 116 set.add(o); 117 } 118 } 119 120 } 121 values = new ArrayList<Object>(set); 122 } 123 } 124 125 }