comparison scripts/java/org/octave/Matrix.java @ 15753:6e39fe7992d9

style fixes and copyright update for java files * ClassHelper.java, DlgListener.java, JDialogBox.java, Matrix.java, OctClassLoader.java, Octave.java, OctaveReference.java, TeXcode.java, TeXtranslator.java: Use Octave copyright text. Style fixes.
author John W. Eaton <jwe@octave.org>
date Sat, 08 Dec 2012 00:24:45 -0500
parents acf0addfc610
children d63878346099
comparison
equal deleted inserted replaced
15752:f96faf028d90 15753:6e39fe7992d9
1 /* Copyright (C) 2007 Michael Goffioul 1 /*
2 ** 2
3 ** This program is free software; you can redistribute it and/or modify 3 Copyright (C) 2007 Michael Goffioul
4 ** it under the terms of the GNU General Public License as published by 4
5 ** the Free Software Foundation; either version 2 of the License, or 5 This file is part of Octave.
6 ** (at your option) any later version. 6
7 ** 7 Octave is free software; you can redistribute it and/or modify it
8 ** This program is distributed in the hope that it will be useful, 8 under the terms of the GNU General Public License as published by
9 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 9 the Free Software Foundation; either version 3 of the License, or (at
10 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 your option) any later version.
11 ** GNU General Public License for more details. 11
12 ** 12 Octave is distributed in the hope that it will be useful, but
13 ** You should have received a copy of the GNU General Public License 13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** along with this program; If not, see <http://www.gnu.org/licenses/>. 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20
15 */ 21 */
16 22
17 package org.octave; 23 package org.octave;
18 24
19 import java.nio.*; 25 import java.nio.*;
20 import java.text.DecimalFormat; 26 import java.text.DecimalFormat;
21 27
22 public class Matrix 28 public class Matrix
23 { 29 {
24 private int[] dims; 30 private int[] dims;
25 private Buffer data; 31 private Buffer data;
26 private Object cache = null; 32 private Object cache = null;
27 33
28 public Matrix() 34 public Matrix ()
29 { 35 {
30 this(new double[0], new int[] {0, 0}); 36 this (new double[0], new int[] {0, 0});
31 } 37 }
32 38
33 public Matrix(double[] data) 39 public Matrix (double[] data)
34 { 40 {
35 this(data, new int[] {1, data.length}); 41 this (data, new int[] {1, data.length});
36 } 42 }
37 43
38 public Matrix(double[][] data) 44 public Matrix (double[][] data)
39 { 45 {
40 int m = data.length; 46 int m = data.length;
41 int n = (m > 0 ? data[0].length : 0); 47 int n = (m > 0 ? data[0].length : 0);
42 int idx = 0; 48 int idx = 0;
43 double[] buf = new double[m*n]; 49 double[] buf = new double[m*n];
44 50
45 for (int j=0; j<n; j++) 51 for (int j = 0; j < n; j++)
46 for (int i=0; i<m; i++) 52 for (int i = 0; i < m; i++)
47 buf[idx++] = data[i][j]; 53 buf[idx++] = data[i][j];
48 this.data = DoubleBuffer.wrap(buf); 54 this.data = DoubleBuffer.wrap(buf);
49 this.dims = new int[] {m, n}; 55 this.dims = new int[] {m, n};
50 this.cache = data; 56 this.cache = data;
51 } 57 }
52 58
53 public Matrix(double[][][] data) 59 public Matrix (double[][][] data)
54 { 60 {
55 int m = data.length; 61 int m = data.length;
56 int n = (m > 0 ? data[0].length : 0); 62 int n = (m > 0 ? data[0].length : 0);
57 int p = (n > 0 ? data[0][0].length : 0); 63 int p = (n > 0 ? data[0][0].length : 0);
58 int idx = 0; 64 int idx = 0;
59 double[] buf = new double[m*n*p]; 65 double[] buf = new double[m*n*p];
60 66
61 for (int k=0; k<p; k++) 67 for (int k = 0; k < p; k++)
62 for (int j=0; j<n; j++) 68 for (int j = 0; j < n; j++)
63 for (int i=0; i<m; i++) 69 for (int i = 0; i < m; i++)
64 buf[idx++] = data[i][j][k]; 70 buf[idx++] = data[i][j][k];
65 this.data = DoubleBuffer.wrap(buf); 71 this.data = DoubleBuffer.wrap(buf);
66 this.dims = new int[] {m, n, p}; 72 this.dims = new int[] {m, n, p};
67 this.cache = data; 73 this.cache = data;
68 } 74 }
69 75
70 public Matrix(double[] data, int[] dims) 76 public Matrix (double[] data, int[] dims)
71 { 77 {
72 this.dims = dims; 78 this.dims = dims;
73 this.data = DoubleBuffer.wrap(data); 79 this.data = DoubleBuffer.wrap(data);
74 } 80 }
75 81
76 public Matrix(byte[] data, int[] dims) 82 public Matrix (byte[] data, int[] dims)
77 { 83 {
78 this.dims = dims; 84 this.dims = dims;
79 this.data = ByteBuffer.wrap(data); 85 this.data = ByteBuffer.wrap(data);
80 } 86 }
81 87
82 public Matrix(int[] data, int[] dims) 88 public Matrix (int[] data, int[] dims)
83 { 89 {
84 this.dims = dims; 90 this.dims = dims;
85 this.data = IntBuffer.wrap(data); 91 this.data = IntBuffer.wrap(data);
86 } 92 }
87 93
88 public double[] toDouble() 94 public double[] toDouble ()
89 { 95 {
90 if (data instanceof DoubleBuffer) 96 if (data instanceof DoubleBuffer)
91 return ((DoubleBuffer)data).array(); 97 return ((DoubleBuffer)data).array ();
92 else 98 else
93 throw new ClassCastException("matrix is not of type `double'"); 99 throw new ClassCastException ("matrix is not of type `double'");
94 } 100 }
95 101
96 public byte[] toByte() 102 public byte[] toByte ()
97 { 103 {
98 if (data instanceof ByteBuffer) 104 if (data instanceof ByteBuffer)
99 return ((ByteBuffer)data).array(); 105 return ((ByteBuffer)data).array ();
100 else 106 else
101 throw new ClassCastException("matrix is not of type `byte'"); 107 throw new ClassCastException ("matrix is not of type `byte'");
102 } 108 }
103 109
104 public int[] toInt() 110 public int[] toInt ()
105 { 111 {
106 if (data instanceof IntBuffer) 112 if (data instanceof IntBuffer)
107 return ((IntBuffer)data).array(); 113 return ((IntBuffer)data).array ();
108 else 114 else
109 throw new ClassCastException("matrix is not of type `integer'"); 115 throw new ClassCastException ("matrix is not of type `integer'");
110 } 116 }
111 117
112 public int getNDims() 118 public int getNDims ()
113 { 119 {
114 return (dims == null ? 0 : dims.length); 120 return (dims == null ? 0 : dims.length);
115 } 121 }
116 122
117 public int getDim(int index) 123 public int getDim (int index)
118 { 124 {
119 return (dims == null || index < 0 || index >= dims.length ? -1 : dims[index]); 125 return (dims == null || index < 0 || index >= dims.length ? -1 : dims[index]);
120 } 126 }
121 127
122 public int[] getDims() 128 public int[] getDims ()
123 { 129 {
124 return dims; 130 return dims;
125 } 131 }
126 132
127 public String getClassName() 133 public String getClassName ()
128 { 134 {
129 if (data instanceof DoubleBuffer) 135 if (data instanceof DoubleBuffer)
130 return "double"; 136 return "double";
131 else if (data instanceof IntBuffer) 137 else if (data instanceof IntBuffer)
132 return "integer"; 138 return "integer";
133 else if (data instanceof ByteBuffer) 139 else if (data instanceof ByteBuffer)
134 return "byte"; 140 return "byte";
135 else 141 else
136 return "unknown"; 142 return "unknown";
137 } 143 }
138 144
139 public String toString() 145 public String toString ()
140 { 146 {
141 if (dims == null || data == null) 147 if (dims == null || data == null)
142 return "null"; 148 return "null";
143 149
144 String s = ""; 150 String s = "";
145 151
146 if (dims.length == 2 && dims[0] == 1 && dims[1] <= 5) 152 if (dims.length == 2 && dims[0] == 1 && dims[1] <= 5)
147 { 153 {
148 if (data instanceof DoubleBuffer) 154 if (data instanceof DoubleBuffer)
149 { 155 {
150 DoubleBuffer b = (DoubleBuffer)data; 156 DoubleBuffer b = (DoubleBuffer)data;
151 DecimalFormat fmt = new DecimalFormat("0.0000 "); 157 DecimalFormat fmt = new DecimalFormat ("0.0000 ");
152 for (int i=0; i<b.capacity(); i++) 158 for (int i = 0; i < b.capacity (); i++)
153 s += fmt.format(b.get(i)); 159 s += fmt.format (b.get (i));
154 } 160 }
155 else if (data instanceof IntBuffer) 161 else if (data instanceof IntBuffer)
156 { 162 {
157 IntBuffer b = (IntBuffer)data; 163 IntBuffer b = (IntBuffer)data;
158 for (int i=0; i<b.capacity(); i++) 164 for (int i = 0; i < b.capacity (); i++)
159 s += (Integer.toString(b.get(i)) + " "); 165 s += (Integer.toString (b.get (i)) + " ");
160 } 166 }
161 else if (data instanceof ByteBuffer) 167 else if (data instanceof ByteBuffer)
162 { 168 {
163 ByteBuffer b = (ByteBuffer)data; 169 ByteBuffer b = (ByteBuffer)data;
164 for (int i=0; i<b.capacity(); i++) 170 for (int i = 0; i < b.capacity (); i++)
165 s += (Byte.toString(b.get(i)) + " "); 171 s += (Byte.toString (b.get (i)) + " ");
166 } 172 }
167 s = ("[ " + s + "]"); 173 s = ("[ " + s + "]");
168 } 174 }
169 else if (dims.length == 2 && dims[0] == 0 && dims[1] == 0) 175 else if (dims.length == 2 && dims[0] == 0 && dims[1] == 0)
170 s = "[ ]"; 176 s = "[ ]";
171 else 177 else
172 { 178 {
173 for (int i=0; i<dims.length; i++) 179 for (int i = 0; i < dims.length; i++)
174 if (i == 0) 180 if (i == 0)
175 s = Integer.toString(dims[i]); 181 s = Integer.toString (dims[i]);
176 else 182 else
177 s += (" by " + Integer.toString(dims[i])); 183 s += (" by " + Integer.toString (dims[i]));
178 s = ("[ (" + s + ") array of " + getClassName() + " ]"); 184 s = ("[ (" + s + ") array of " + getClassName () + " ]");
179 } 185 }
180 186
181 return s; 187 return s;
182 } 188 }
183 189
184 public static Object ident(Object o) 190 public static Object ident (Object o)
185 { 191 {
186 System.out.println(o); 192 System.out.println (o);
187 return o; 193 return o;
188 } 194 }
189 195
190 public boolean equals(Object value) 196 public boolean equals (Object value)
191 { 197 {
192 if (value instanceof Matrix) 198 if (value instanceof Matrix)
193 { 199 {
194 Matrix m = (Matrix)value; 200 Matrix m = (Matrix)value;
195 if (!java.util.Arrays.equals(dims, m.dims)) 201 if (!java.util.Arrays.equals (dims, m.dims))
196 return false; 202 return false;
197 return data.equals(m.data); 203 return data.equals (m.data);
198 } 204 }
199 else 205 else
200 return false; 206 return false;
201 } 207 }
202 208
203 public boolean isEmpty() 209 public boolean isEmpty ()
204 { 210 {
205 return (data == null || dims == null || data.capacity() == 0); 211 return (data == null || dims == null || data.capacity () == 0);
206 } 212 }
207 213
208 public boolean isVector() 214 public boolean isVector ()
209 { 215 {
210 return (dims.length == 1 || 216 return (dims.length == 1 ||
211 (dims.length == 2 && (dims[0] == 1 || dims[1] == 1 || 217 (dims.length == 2 && (dims[0] == 1 || dims[1] == 1 ||
212 (dims[0] == 0 && dims[1] == 0)))); 218 (dims[0] == 0 && dims[1] == 0))));
213 } 219 }
214 220
215 public int getLength() 221 public int getLength ()
216 { 222 {
217 return data.capacity(); 223 return data.capacity ();
218 } 224 }
219 225
220 public double[] asDoubleVector() 226 public double[] asDoubleVector ()
221 { 227 {
222 if (data instanceof DoubleBuffer) 228 if (data instanceof DoubleBuffer)
223 return toDouble(); 229 return toDouble ();
224 else 230 else
225 System.out.println("Warning: invalid conversion to double vector"); 231 System.out.println ("Warning: invalid conversion to double vector");
226 return null; 232 return null;
227 } 233 }
228 234
229 public double[][] asDoubleMatrix() 235 public double[][] asDoubleMatrix ()
230 { 236 {
231 if (cache != null) 237 if (cache != null)
232 { 238 {
233 try { return (double[][])cache; } 239 try { return (double[][])cache; }
234 catch (ClassCastException e) { } 240 catch (ClassCastException e) { }
235 } 241 }
236 242
237 if (data instanceof DoubleBuffer && dims.length == 2) 243 if (data instanceof DoubleBuffer && dims.length == 2)
238 { 244 {
239 double[][] m = new double[dims[0]][dims[1]]; 245 double[][] m = new double[dims[0]][dims[1]];
240 double[] data = ((DoubleBuffer)this.data).array(); 246 double[] data = ((DoubleBuffer)this.data).array ();
241 int idx = 0; 247 int idx = 0;
242 if (data.length > 0) 248 if (data.length > 0)
243 for (int j=0; j<m[0].length; j++) 249 for (int j = 0; j < m[0].length; j++)
244 for (int i=0; i<m.length; i++) 250 for (int i = 0; i < m.length; i++)
245 m[i][j] = data[idx++]; 251 m[i][j] = data[idx++];
246 cache = m; 252 cache = m;
247 return m; 253 return m;
248 } 254 }
249 else 255 else
250 System.out.println("Warning: invalid conversion to double matrix"); 256 System.out.println ("Warning: invalid conversion to double matrix");
251 257
252 return null; 258 return null;
253 } 259 }
254 260
255 public double[][][] asDoubleMatrix3() 261 public double[][][] asDoubleMatrix3 ()
256 { 262 {
257 if (cache != null) 263 if (cache != null)
258 { 264 {
259 try { return (double[][][])cache; } 265 try { return (double[][][])cache; }
260 catch (ClassCastException e) { } 266 catch (ClassCastException e) { }
261 } 267 }
262 268
263 if (data instanceof DoubleBuffer && dims.length == 3) 269 if (data instanceof DoubleBuffer && dims.length == 3)
264 { 270 {
265 double[][][] m = new double[dims[0]][dims[1]][dims[2]]; 271 double[][][] m = new double[dims[0]][dims[1]][dims[2]];
266 double[] data = ((DoubleBuffer)this.data).array(); 272 double[] data = ((DoubleBuffer)this.data).array ();
267 int idx = 0; 273 int idx = 0;
268 if (data.length > 0) 274 if (data.length > 0)
269 for (int k=0; k<dims[2]; k++) 275 for (int k = 0; k < dims[2]; k++)
270 for (int j=0; j<dims[1]; j++) 276 for (int j = 0; j < dims[1]; j++)
271 for (int i=0; i<dims[0]; i++) 277 for (int i = 0; i < dims[0]; i++)
272 m[i][j][k] = data[idx++]; 278 m[i][j][k] = data[idx++];
273 cache = m; 279 cache = m;
274 return m; 280 return m;
275 } 281 }
276 else 282 else
277 System.out.println("Warning: invalid conversion to double array"); 283 System.out.println ("Warning: invalid conversion to double array");
278 284
279 return null; 285 return null;
280 } 286 }
281 287
282 public int[][] asIntMatrix() 288 public int[][] asIntMatrix ()
283 { 289 {
284 if (cache != null) 290 if (cache != null)
285 { 291 {
286 try { return (int[][])cache; } 292 try { return (int[][])cache; }
287 catch (ClassCastException e) { } 293 catch (ClassCastException e) { }
288 } 294 }
289 295
290 if (data instanceof IntBuffer && dims.length == 2) 296 if (data instanceof IntBuffer && dims.length == 2)
291 { 297 {
292 int[][] m = new int[dims[0]][dims[1]]; 298 int[][] m = new int[dims[0]][dims[1]];
293 int[] data = ((IntBuffer)this.data).array(); 299 int[] data = ((IntBuffer)this.data).array ();
294 int idx = 0; 300 int idx = 0;
295 if (data.length > 0) 301 if (data.length > 0)
296 for (int j=0; j<m[0].length; j++) 302 for (int j = 0; j < m[0].length; j++)
297 for (int i=0; i<m.length; i++) 303 for (int i = 0; i < m.length; i++)
298 m[i][j] = data[idx++]; 304 m[i][j] = data[idx++];
299 cache = m; 305 cache = m;
300 return m; 306 return m;
301 } 307 }
302 else 308 else
303 System.out.println("Warning: invalid conversion to integer matrix"); 309 System.out.println ("Warning: invalid conversion to integer matrix");
304 310
305 return null; 311 return null;
306 } 312 }
307 313
308 public double minValue() 314 public double minValue ()
309 { 315 {
310 double val = Double.POSITIVE_INFINITY; 316 double val = Double.POSITIVE_INFINITY;
311 317
312 if (data instanceof DoubleBuffer) 318 if (data instanceof DoubleBuffer)
313 { 319 {
314 double[] buf = ((DoubleBuffer)data).array(); 320 double[] buf = ((DoubleBuffer)data).array ();
315 for (int i=0; i<buf.length; i++) 321 for (int i = 0; i < buf.length; i++)
316 if (buf[i] < val) 322 if (buf[i] < val)
317 val = buf[i]; 323 val = buf[i];
318 } 324 }
319 else if (data instanceof ByteBuffer) 325 else if (data instanceof ByteBuffer)
320 { 326 {
321 byte[] buf = ((ByteBuffer)data).array(); 327 byte[] buf = ((ByteBuffer)data).array ();
322 for (int i=0; i<buf.length; i++) 328 for (int i = 0; i < buf.length; i++)
323 if (buf[i] < val) 329 if (buf[i] < val)
324 val = buf[i]; 330 val = buf[i];
325 } 331 }
326 else 332 else
327 System.out.println("Warning: cannot compute min value for array of type `" + getClassName() + "'"); 333 System.out.println ("Warning: cannot compute min value for array of type `" + getClassName () + "'");
328 334
329 return val; 335 return val;
330 } 336 }
331 337
332 public double maxValue() 338 public double maxValue ()
333 { 339 {
334 double val = Double.NEGATIVE_INFINITY; 340 double val = Double.NEGATIVE_INFINITY;
335 341
336 if (data instanceof DoubleBuffer) 342 if (data instanceof DoubleBuffer)
337 { 343 {
338 double[] buf = ((DoubleBuffer)data).array(); 344 double[] buf = ((DoubleBuffer)data).array ();
339 for (int i=0; i<buf.length; i++) 345 for (int i = 0; i < buf.length; i++)
340 if (buf[i] > val) 346 if (buf[i] > val)
341 val = buf[i]; 347 val = buf[i];
342 } 348 }
343 else if (data instanceof ByteBuffer) 349 else if (data instanceof ByteBuffer)
344 { 350 {
345 byte[] buf = ((ByteBuffer)data).array(); 351 byte[] buf = ((ByteBuffer)data).array ();
346 for (int i=0; i<buf.length; i++) 352 for (int i = 0; i <buf.length; i++)
347 if (buf[i] > val) 353 if (buf[i] > val)
348 val = buf[i]; 354 val = buf[i];
349 } 355 }
350 else 356 else
351 System.out.println("Warning: cannot compute max value for array of type `" + getClassName() + "'"); 357 System.out.println ("Warning: cannot compute max value for array of type `" + getClassName () + "'");
352 358
353 return val; 359 return val;
354 } 360 }
355 } 361 }