| 1 | |
|---|
| 2 | package com.hcarralero.toljava; |
|---|
| 3 | |
|---|
| 4 | import java.util.Arrays; |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * Representa a un objeto TOL de tipo Matrix. |
|---|
| 8 | * |
|---|
| 9 | * @author Humberto Andrés Carralero López (hcarralero@gmail.com) |
|---|
| 10 | * @version 1.0 |
|---|
| 11 | * @see com.hcarralero.toljava.TOLMatrixException |
|---|
| 12 | */ |
|---|
| 13 | public class TOLMatrix extends TOLLObject { |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * Construye un objeto <code>TOLMatrix</code> vacÃo (UnknownMatrix). |
|---|
| 17 | * |
|---|
| 18 | */ |
|---|
| 19 | public TOLMatrix() { |
|---|
| 20 | this.rows= 0; |
|---|
| 21 | this.columns= 0; |
|---|
| 22 | this.values= null; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Construye un objeto <code>TOLMatrix</code> con el número especificado de filas y columnas, |
|---|
| 27 | * todos los elementos son inicializados a <code>0.0</code>. |
|---|
| 28 | * |
|---|
| 29 | * @param rows número de filas |
|---|
| 30 | * @param columns número de columnas |
|---|
| 31 | * @throws com.hcarralero.toljava.TOLMatrixException cuando no se puede crear la matriz por alguna de |
|---|
| 32 | * las razones siguientes: número de filas o columnas no válidos o insuficicnte memoria. |
|---|
| 33 | * @see com.hcarralero.toljava.TOLMatrixException |
|---|
| 34 | */ |
|---|
| 35 | public TOLMatrix(int rows, int columns) throws TOLMatrixException { |
|---|
| 36 | if (rows<=0) { |
|---|
| 37 | throw new TOLMatrixException(TOLMatrixException.ERROR_INVALID_ROWS, "Número de filas de la matriz NO ES VÃLIDO (<=0)"); |
|---|
| 38 | } |
|---|
| 39 | this.rows = rows; |
|---|
| 40 | if (columns<=0) { |
|---|
| 41 | throw new TOLMatrixException(TOLMatrixException.ERROR_INVALID_COLUMNS, "Número de columnas de la matriz NO ES VÃLIDO (<=0)"); |
|---|
| 42 | } |
|---|
| 43 | this.columns = columns; |
|---|
| 44 | this.values= new double[rows][]; |
|---|
| 45 | if (values==null) { |
|---|
| 46 | throw new TOLMatrixException(TOLMatrixException.ERROR_NOT_ENOUGH_MEMORY, "No hay memoria suficiente para crear la matriz"); |
|---|
| 47 | } |
|---|
| 48 | for (int i= 0; i<rows; ++i) { |
|---|
| 49 | values[i]= new double[columns]; |
|---|
| 50 | if (values[i]==null) { |
|---|
| 51 | throw new TOLMatrixException(TOLMatrixException.ERROR_NOT_ENOUGH_MEMORY, "No hay memoria suficiente para crear la matriz"); |
|---|
| 52 | } |
|---|
| 53 | for (int j= 0; j<columns; ++j) { |
|---|
| 54 | values[i][j]= 0.0; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Construye un objeto <code>TOLMatrix</code>na partie de un arreglo doble (matriz) de números reales. |
|---|
| 61 | * |
|---|
| 62 | * @param values arreglo doble (matriz) de números reales. |
|---|
| 63 | * @throws com.hcarralero.toljava.TOLMatrixException cuando no se puede crear la matriz por alguna de |
|---|
| 64 | * las razones siguientes: número de filas o columnas no válidos en el arreglo doble (matriz) pasado como parámetro |
|---|
| 65 | * o insuficicnte memoria. |
|---|
| 66 | * @see com.hcarralero.toljava.TOLMatrixException |
|---|
| 67 | */ |
|---|
| 68 | public TOLMatrix(double values[][]) throws TOLMatrixException { |
|---|
| 69 | this.rows = values.length; |
|---|
| 70 | if (rows<=0) { |
|---|
| 71 | throw new TOLMatrixException(TOLMatrixException.ERROR_INVALID_ROWS, "Número de filas de la matriz NO ES VÃLIDO (<=0)"); |
|---|
| 72 | } |
|---|
| 73 | this.columns= values[0].length; |
|---|
| 74 | if (this.columns<=0) { |
|---|
| 75 | throw new TOLMatrixException(TOLMatrixException.ERROR_INVALID_COLUMNS, "Número de columnas de la matriz NO ES VÃLIDO (<=0)"); |
|---|
| 76 | } |
|---|
| 77 | this.values= new double[this.rows][]; |
|---|
| 78 | if (this.values==null) { |
|---|
| 79 | throw new TOLMatrixException(TOLMatrixException.ERROR_NOT_ENOUGH_MEMORY, "No hay memoria suficiente para crear la matriz"); |
|---|
| 80 | } |
|---|
| 81 | for (int i= 0; i<this.rows; ++i) { |
|---|
| 82 | if (values[i].length!=this.columns) { |
|---|
| 83 | throw new TOLMatrixException(TOLMatrixException.ERROR_NOT_EQUALS_ROWS, "No todas las filas tienen igual cantidad de columnas"); |
|---|
| 84 | } |
|---|
| 85 | this.values[i]= new double[this.columns]; |
|---|
| 86 | if (this.values[i]==null) { |
|---|
| 87 | throw new TOLMatrixException(TOLMatrixException.ERROR_NOT_ENOUGH_MEMORY, "No hay memoria suficiente para crear la matriz"); |
|---|
| 88 | } |
|---|
| 89 | for (int j= 0; j<this.columns; ++j) { |
|---|
| 90 | this.values[i][j]= values[i][j]; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | private int rows= 0; |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Obtiene el número de filas de la matriz. |
|---|
| 99 | * |
|---|
| 100 | * @return el número de filas de la matriz. |
|---|
| 101 | */ |
|---|
| 102 | public int getRows() { |
|---|
| 103 | return rows; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | private int columns= 0; |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * Obtiene el número de columnas de la matriz. |
|---|
| 110 | * |
|---|
| 111 | * @return el número de columnas de la matriz. |
|---|
| 112 | */ |
|---|
| 113 | public int getColumns() { |
|---|
| 114 | return columns; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * |
|---|
| 119 | */ |
|---|
| 120 | protected double[][] values = null; |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * Obtiene como arreglo doble la matriz contenida en el objeto. |
|---|
| 124 | * |
|---|
| 125 | * @return arreglo doble la con matriz contenida. |
|---|
| 126 | */ |
|---|
| 127 | public double[][] getValues() { |
|---|
| 128 | return values; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Obtiene el valor numérico real correspondiente a la celda cuyas fila y columna se indican. |
|---|
| 133 | * |
|---|
| 134 | * @param row fila |
|---|
| 135 | * @param column columna |
|---|
| 136 | * |
|---|
| 137 | * @return el valor numérico real correspondiente a la celda indicada. |
|---|
| 138 | * |
|---|
| 139 | * @throws com.hcarralero.toljava.TOLMatrixException si se intenta acceder a una celda inexistente. |
|---|
| 140 | */ |
|---|
| 141 | public double getCellValue(int row, int column) throws TOLMatrixException { |
|---|
| 142 | if (row<0 || row>=this.rows) { |
|---|
| 143 | throw new TOLMatrixException(TOLMatrixException.ERROR_INVALID_ROW_NUMBER, "Se intenta acceder a un NÃMERO DE FILA NO VÃLIDO (<0 o >número de filas de la matriz)"); |
|---|
| 144 | } |
|---|
| 145 | if (column<0 || column>=this.columns) { |
|---|
| 146 | throw new TOLMatrixException(TOLMatrixException.ERROR_INVALID_COLUMN_NUMBER, "Se intenta acceder a un NÃMERO DE COLUMNA NO VÃLIDO (<0 o >número de columnas de la matriz)"); |
|---|
| 147 | } |
|---|
| 148 | return this.values[row][column]; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * Cambia el valor de la celda cuyas fila y columna se indican por el nuevo valor pasado como parámetro. |
|---|
| 153 | * |
|---|
| 154 | * @param row fila |
|---|
| 155 | * @param column columna |
|---|
| 156 | * @param value nuevo valor |
|---|
| 157 | * @throws com.hcarralero.toljava.TOLMatrixException cuando la fila o columna están fuera del rango de la matriz. |
|---|
| 158 | */ |
|---|
| 159 | public void setCellValue(int row, int column, double value) throws TOLMatrixException { |
|---|
| 160 | if (row<0 || row>=this.rows) { |
|---|
| 161 | throw new TOLMatrixException(TOLMatrixException.ERROR_INVALID_ROW_NUMBER, "Se intenta acceder a un NÃMERO DE FILA NO VÃLIDO (<0 o >número de filas de la matriz)"); |
|---|
| 162 | } |
|---|
| 163 | if (column<0 || column>=this.columns) { |
|---|
| 164 | throw new TOLMatrixException(TOLMatrixException.ERROR_INVALID_COLUMN_NUMBER, "Se intenta acceder a un NÃMERO DE COLUMNA NO VÃLIDO (<0 o >número de columnas de la matriz)"); |
|---|
| 165 | } |
|---|
| 166 | this.values[row][column]= value; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Determina si el objeto corresponde al tipo Matrix de TOL. |
|---|
| 171 | * |
|---|
| 172 | * @return <code>true</code> ya que el objeto corresponde al tipo Matrix de TOL. |
|---|
| 173 | */ |
|---|
| 174 | @Override |
|---|
| 175 | public boolean isMatrix() { |
|---|
| 176 | return true; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | * Compara a este objeto con otro. |
|---|
| 181 | * |
|---|
| 182 | * @param obj el objeto con el que se comparará. |
|---|
| 183 | * @return <code>true</code> si el objeto pasado como parámetro es de tipo |
|---|
| 184 | * <code>TOLMatrix</code> y representa una matriz con igual cantidad de filas |
|---|
| 185 | * y columnas, asà como todos los elementos que contienen. |
|---|
| 186 | */ |
|---|
| 187 | @Override |
|---|
| 188 | public boolean equals(Object obj) { |
|---|
| 189 | if (obj == null) { |
|---|
| 190 | return false; |
|---|
| 191 | } |
|---|
| 192 | if (getClass() != obj.getClass()) { |
|---|
| 193 | return false; |
|---|
| 194 | } |
|---|
| 195 | final TOLMatrix other = (TOLMatrix) obj; |
|---|
| 196 | if (this.rows != other.rows) { |
|---|
| 197 | return false; |
|---|
| 198 | } |
|---|
| 199 | if (this.columns != other.columns) { |
|---|
| 200 | return false; |
|---|
| 201 | } |
|---|
| 202 | if (this.values != other.values && (this.values == null || !Arrays.deepEquals(this.values, other.values))) { |
|---|
| 203 | return false; |
|---|
| 204 | } |
|---|
| 205 | return true; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /** |
|---|
| 209 | * Calcula el código hash asignado al objeto. |
|---|
| 210 | * |
|---|
| 211 | * @return el código hash asignado al objeto. |
|---|
| 212 | */ |
|---|
| 213 | @Override |
|---|
| 214 | public int hashCode() { |
|---|
| 215 | int hash = 7; |
|---|
| 216 | hash = 89 * hash + this.rows; |
|---|
| 217 | hash = 89 * hash + this.columns; |
|---|
| 218 | hash = 89 * hash + (this.values != null ? this.values.hashCode() : 0); |
|---|
| 219 | return hash; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /** |
|---|
| 223 | * Obtiene una representación como cadena de texto de la matriz en el formato |
|---|
| 224 | * {{a11, a12, ..., a1n}, {a21, a22, ..., a2n}, ..., {am1, am2, ..., amn}}, donde |
|---|
| 225 | * m es el número de filas y m el de columnas. |
|---|
| 226 | * |
|---|
| 227 | * @return la representación como cadena de texto de la matriz. |
|---|
| 228 | */ |
|---|
| 229 | @Override |
|---|
| 230 | public String toString() { |
|---|
| 231 | if (this.rows<=0||this.columns<=0) { |
|---|
| 232 | return "UnknownMatrix"; |
|---|
| 233 | } |
|---|
| 234 | String res= "{"; |
|---|
| 235 | for (int i= 0; i<this.rows; ++i) { |
|---|
| 236 | res+= "{"; |
|---|
| 237 | boolean coma= false; |
|---|
| 238 | for (int j= 0; j<this.columns; ++j) { |
|---|
| 239 | if (coma) { |
|---|
| 240 | res+= ", "; |
|---|
| 241 | } else { |
|---|
| 242 | coma= true; |
|---|
| 243 | } |
|---|
| 244 | res+= this.values[i][j]; |
|---|
| 245 | } |
|---|
| 246 | res+= "}"; |
|---|
| 247 | } |
|---|
| 248 | res+= "}"; |
|---|
| 249 | return res; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | } |
|---|