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.intmatrix.impl; 025 026 import org.ujmp.core.Coordinates; 027 import org.ujmp.core.Matrix; 028 import org.ujmp.core.exceptions.MatrixException; 029 import org.ujmp.core.intmatrix.IntMatrix; 030 import org.ujmp.core.intmatrix.stub.AbstractDenseIntMatrix2D; 031 032 public class SimpleDenseIntMatrix2D extends AbstractDenseIntMatrix2D { 033 private static final long serialVersionUID = -3331102463077119410L; 034 035 private int[][] values = null; 036 037 public SimpleDenseIntMatrix2D(Matrix m) throws MatrixException { 038 if (m instanceof SimpleDenseIntMatrix2D) { 039 int[][] v = ((SimpleDenseIntMatrix2D) m).values; 040 this.values = new int[v.length][v[0].length]; 041 for (int r = v.length; --r >= 0;) { 042 for (int c = v[0].length; --c >= 0;) { 043 values[r][c] = v[r][c]; 044 } 045 } 046 } else { 047 values = new int[(int) m.getRowCount()][(int) m.getColumnCount()]; 048 for (long[] c : m.allCoordinates()) { 049 setAsDouble(m.getAsDouble(c), c); 050 } 051 } 052 } 053 054 public SimpleDenseIntMatrix2D(int[]... v) { 055 this.values = v; 056 } 057 058 public SimpleDenseIntMatrix2D(long... size) { 059 values = new int[(int) size[ROW]][(int) size[COLUMN]]; 060 } 061 062 public SimpleDenseIntMatrix2D(int[] v) { 063 this.values = new int[v.length][1]; 064 for (int r = v.length; --r >= 0;) { 065 values[r][0] = v[r]; 066 } 067 } 068 069 public long[] getSize() { 070 return new long[] { values.length, values.length == 0 ? 0 : values[0].length }; 071 } 072 073 074 public long getRowCount() { 075 return values.length; 076 } 077 078 079 public long getColumnCount() { 080 return values.length == 0 ? 0 : values[0].length; 081 } 082 083 public int getInt(long row, long column) { 084 return values[(int) row][(int) column]; 085 } 086 087 public void setInt(int value, long row, long column) { 088 values[(int) row][(int) column] = value; 089 } 090 091 public int getInt(int row, int column) { 092 return values[row][column]; 093 } 094 095 public void setInt(int value, int row, int column) { 096 values[row][column] = value; 097 } 098 099 100 public final IntMatrix transpose() { 101 int[][] result = new int[values[0].length][values.length]; 102 for (int r = result.length; --r >= 0;) { 103 for (int c = result[0].length; --c >= 0;) { 104 result[r][c] = values[c][r]; 105 } 106 } 107 return new SimpleDenseIntMatrix2D(result); 108 } 109 110 111 public final IntMatrix plus(double v) { 112 int vInt = (int) v; 113 int[][] result = new int[values.length][values[0].length]; 114 for (int r = result.length; --r >= 0;) { 115 for (int c = result[0].length; --c >= 0;) { 116 result[r][c] = values[r][c] + vInt; 117 } 118 } 119 return new SimpleDenseIntMatrix2D(result); 120 } 121 122 123 public final IntMatrix minus(double v) { 124 int vInt = (int) v; 125 int[][] result = new int[values.length][values[0].length]; 126 for (int r = result.length; --r >= 0;) { 127 for (int c = result[0].length; --c >= 0;) { 128 result[r][c] = values[r][c] - vInt; 129 } 130 } 131 return new SimpleDenseIntMatrix2D(result); 132 } 133 134 135 public final IntMatrix times(double v) { 136 int vInt = (int) v; 137 int[][] result = new int[values.length][values[0].length]; 138 for (int r = result.length; --r >= 0;) { 139 for (int c = result[0].length; --c >= 0;) { 140 result[r][c] = values[r][c] * vInt; 141 } 142 } 143 return new SimpleDenseIntMatrix2D(result); 144 } 145 146 147 public final IntMatrix divide(double v) { 148 int vInt = (int) v; 149 int[][] result = new int[values.length][values[0].length]; 150 for (int r = result.length; --r >= 0;) { 151 for (int c = result[0].length; --c >= 0;) { 152 result[r][c] = values[r][c] / vInt; 153 } 154 } 155 return new SimpleDenseIntMatrix2D(result); 156 } 157 158 public final IntMatrix plus(IntMatrix m2) throws MatrixException { 159 int[][] result = new int[values.length][values[0].length]; 160 for (int r = result.length; --r >= 0;) { 161 for (int c = result[0].length; --c >= 0;) { 162 result[r][c] = values[r][c] + m2.getAsInt(r, c); 163 } 164 } 165 return new SimpleDenseIntMatrix2D(result); 166 } 167 168 public final IntMatrix minus(IntMatrix m2) throws MatrixException { 169 int[][] result = new int[values.length][values[0].length]; 170 for (int r = result.length; --r >= 0;) { 171 for (int c = result[0].length; --c >= 0;) { 172 result[r][c] = values[r][c] - m2.getAsInt(r, c); 173 } 174 } 175 return new SimpleDenseIntMatrix2D(result); 176 } 177 178 public final IntMatrix times(IntMatrix m2) throws MatrixException { 179 int[][] result = new int[values.length][values[0].length]; 180 for (int r = result.length; --r >= 0;) { 181 for (int c = result[0].length; --c >= 0;) { 182 result[r][c] = values[r][c] * m2.getAsInt(r, c); 183 } 184 } 185 return new SimpleDenseIntMatrix2D(result); 186 } 187 188 public final IntMatrix divide(IntMatrix m2) throws MatrixException { 189 int[][] result = new int[values.length][values[0].length]; 190 for (int r = result.length; --r >= 0;) { 191 for (int c = result[0].length; --c >= 0;) { 192 result[r][c] = values[r][c] / m2.getAsInt(r, c); 193 } 194 } 195 return new SimpleDenseIntMatrix2D(result); 196 } 197 198 public IntMatrix mtimes(IntMatrix matrix) throws MatrixException { 199 if (values[0].length != matrix.getRowCount()) { 200 throw new MatrixException("matrices have wrong size: " 201 + Coordinates.toString(getSize()) + " and " 202 + Coordinates.toString(matrix.getSize())); 203 } 204 205 int i, j, k; 206 int sum; 207 int[][] ret = new int[values.length][(int) matrix.getColumnCount()]; 208 209 for (i = values.length; --i >= 0;) { 210 for (j = ret[0].length; --j >= 0;) { 211 sum = 0; 212 for (k = values[0].length; --k >= 0;) { 213 sum += values[i][k] * matrix.getAsDouble(k, j); 214 } 215 ret[i][j] = sum; 216 } 217 } 218 219 return new SimpleDenseIntMatrix2D(ret); 220 } 221 222 public boolean containsNaN() { 223 for (int r = values.length; --r >= 0;) { 224 for (int c = values[0].length; --c >= 0;) { 225 if (Double.isNaN(values[r][c])) { 226 return true; 227 } 228 } 229 } 230 return false; 231 } 232 233 }