source: tol/TolJava/TOLJava/src/com/hcarralero/toljava/TOLVMatrix.java @ e946b884

1941-docker-image-to-testdevel
Last change on this file since e946b884 was 9887b93, checked in by Humberto Carralero <hacarralero@…>, 15 years ago

Importing TolJava

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@3900 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 8.8 KB
Line 
1
2package com.hcarralero.toljava;
3
4import java.util.Arrays;
5
6/**
7 * Representa a un objeto TOL de tipo VMatrix
8 *
9 * @author Humberto Andrés Carralero López (hcarralero@gmail.com)
10 * @version 1.0
11 * @see com.hcarralero.toljava.TOLMatrixException
12 */
13public class TOLVMatrix extends TOLLObject {
14
15    /**
16     * Construye un objeto <code>TOLMatrix</code> vacío (UnknownMatrix)
17     *
18     */
19    public TOLVMatrix() {
20        this.rows= 0;
21        this.columns= 0;
22        this.values= null;
23    }
24
25    /**
26     * Construye un objeto <code>TOLVMatrix</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 TOLVMatrix(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>TOLVMatrix</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 TOLVMatrix(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    protected double[][] values = null;
118
119    /**
120     * Obtiene como arreglo doble la matriz contenida en el objeto
121     *
122     * @return arreglo doble la con matriz contenida
123     */
124    public double[][] getValues() {
125        return values;
126    }
127
128    /**
129     * Obtiene el valor numérico real correspondiente a la celda cuyas fila y columna se indican
130     *
131     * @param row fila
132     * @param column columna
133     *
134     * @return el valor numérico real correspondiente a la celda indicada
135     *
136     * @throws com.hcarralero.toljava.TOLMatrixException si se intenta acceder a una celda inexistente
137     */
138    public double getCellValue(int row, int column) throws TOLMatrixException {
139        if (row<0 || row>=this.rows) {
140            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)");
141        }
142        if (column<0 || column>=this.columns) {
143            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)");
144        }
145        return this.values[row][column];
146    }
147
148    /**
149     * Cambia el valor de la celda cuyas fila y columna se indican por el nuevo valor pasado como parámetro
150     *
151     * @param row fila
152     * @param column columna
153     * @param value nuevo valor
154     * @throws com.hcarralero.toljava.TOLMatrixException cuando la fila o columna están fuera del rango de la matriz
155     */
156    public void setCellValue(int row, int column, double value) throws TOLMatrixException {
157        if (row<0 || row>=this.rows) {
158            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)");
159        }
160        if (column<0 || column>=this.columns) {
161            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)");
162        }
163        this.values[row][column]= value;
164    }
165
166    /**
167     * Determina si el objeto corresponde al tipo VMatrix de TOL
168     *
169     * @return <code>true</code> ya que el objeto corresponde al tipo VMatrix de TOL
170     */
171    @Override
172    public boolean isVMatrix() {
173        return true;
174    }
175    /**
176     * Compara a este objeto con otro
177     *
178     * @param obj el objeto con el que se comparará
179     * @return <code>true</code> si el objeto pasado como parámetro es de tipo
180     * <code>TOLVMatrix</code> y representa una matriz con igual cantidad de filas
181     * y columnas, así como todos los elementos que contienen
182     */
183    @Override
184    public boolean equals(Object obj) {
185        if (obj == null) {
186            return false;
187        }
188        if (getClass() != obj.getClass()) {
189            return false;
190        }
191        final TOLVMatrix other = (TOLVMatrix) obj;
192        if (this.rows != other.rows) {
193            return false;
194        }
195        if (this.columns != other.columns) {
196            return false;
197        }
198        if (this.values != other.values && (this.values == null || !Arrays.deepEquals(this.values, other.values))) {
199            return false;
200        }
201        return true;
202    }
203
204    /**
205     * Calcula el código hash asignado al objeto
206     *
207     * @return el código hash asignado al objeto
208     */
209    @Override
210    public int hashCode() {
211        int hash = 7;
212        hash = 89 * hash + this.rows;
213        hash = 89 * hash + this.columns;
214        hash = 89 * hash + (this.values != null ? this.values.hashCode() : 0);
215        return hash;
216    }
217
218    /**
219     * Obtiene una representación como cadena de texto de la matriz en el formato
220     * {{a11, a12, ..., a1n}, {a21, a22, ..., a2n}, ..., {am1, am2, ..., amn}}, donde
221     * m es el número de filas y m el de columnas
222     *
223     * @return la representación como cadena de texto de la matriz
224     */
225    @Override
226    public String toString() {
227        if (this.rows<=0||this.columns<=0) {
228            return "UnknownVMatrix";
229        }
230        String res= "{";
231        for (int i= 0; i<this.rows; ++i) {
232            res+= "{";
233            boolean coma= false;
234            for (int j= 0; j<this.columns; ++j) {
235                if (coma) {
236                    res+= ", ";
237                } else {
238                    coma= true;
239                }
240                res+= this.values[i][j];
241            }
242            res+= "}";
243        }
244        res+= "}";
245        return res;
246    }
247
248}
Note: See TracBrowser for help on using the repository browser.