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