View Javadoc
1   /*
2    * Copyright 2007, 2008, 2009 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.io.impl;
17  
18  import java.io.StringWriter;// for nan and inf 
19  import java.util.Map;
20  import java.util.TreeMap;
21  
22  import eu.simuline.octave.OctaveEngine;
23  import eu.simuline.octave.OctaveEngineFactory;
24  import eu.simuline.octave.io.OctaveIO;
25  import eu.simuline.octave.type.Octave;
26  import eu.simuline.octave.type.OctaveDouble;
27  import eu.simuline.octave.type.OctaveObject;
28  
29  import static org.junit.Assert.assertEquals;
30  
31  import org.junit.Ignore;
32  import org.junit.Test;
33  
34  /**
35   * Test read/write of {@link OctaveDouble}
36   */
37  public class TestIoOctaveDouble {
38  
39      /** */
40      @Test public void testScalarToText() {
41          final OctaveObject integer42 = Octave.scalar(42);
42          assertEquals("# name: ans\n" + // 
43  		     "# type: scalar\n" + // 
44  		     "42.0\n", 
45  		     OctaveIO.toText(integer42));
46          final OctaveObject integer43 = Octave.scalar(43);
47          assertEquals("# name: integer43\n" +
48  		     "# type: scalar\n" +
49  		     "43.0\n", 
50  		     OctaveIO.toText("integer43", integer43));
51      }
52  
53      /**
54       * Test
55       */
56      @Test public void test3dToText() {
57          final OctaveDouble matrix = new OctaveDouble(3, 4, 2);
58          assertEquals("# name: matrix3d\n" + //
59  		     "# type: matrix\n" + //
60  		     "# ndims: 3\n" + //
61  		     " 3 4 2\n" + //
62  		     " 0.0\n 0.0\n 0.0\n" + //
63  		     " 0.0\n 0.0\n 0.0\n" + //
64  		     " 0.0\n 0.0\n 0.0\n" + //
65  		     " 0.0\n 0.0\n 0.0\n" + //
66  		     " 0.0\n 0.0\n 0.0\n" + //
67  		     " 0.0\n 0.0\n 0.0\n" + //
68  		     " 0.0\n 0.0\n 0.0\n" + //
69  		     " 0.0\n 0.0\n 0.0\n", 
70  		     OctaveIO.toText("matrix3d", matrix));
71          matrix.set(42.0, 1, 3, 2);
72          assertEquals("# name: matrix3d\n" + //
73  		     "# type: matrix\n" + //
74  		     "# ndims: 3\n" + //
75  		     " 3 4 2\n" + //
76  		     " 0.0\n 0.0\n 0.0\n" + // matrix(x,x,1)
77  		     " 0.0\n 0.0\n 0.0\n" + //
78  		     " 0.0\n 0.0\n 0.0\n" + //
79  		     " 0.0\n 0.0\n 0.0\n" + //
80  
81  		     " 0.0\n 0.0\n 0.0\n" + // matrix(x,x,2)
82  		     " 0.0\n 0.0\n 0.0\n" + //
83  		     " 42.0\n 0.0\n 0.0\n" + // matrix(1,3,2)
84  		     " 0.0\n 0.0\n 0.0\n", 
85  		     OctaveIO.toText("matrix3d", matrix));
86      }
87  
88      /**
89       * Test
90       */
91      @Test public void testEmpty3dToText() {
92          final OctaveDouble matrix = new OctaveDouble(0, 0, 0);
93          assertEquals("# name: matrix3d\n" + 
94  		     "# type: matrix\n" + 
95  		     "# ndims: 3\n 0 0 0\n", 
96  		     OctaveIO.toText("matrix3d", matrix));
97      }
98  
99      /**
100      * @throws Exception
101      */
102     @Test public void testEmpty2dToText() throws Exception {
103         final OctaveDouble matrix = new OctaveDouble(0, 0);
104         assertEquals(0, matrix.getSize(1));
105         assertEquals(0, matrix.getSize(2));
106         assertEquals("# name: matrix\n" + //
107 		     "# type: matrix\n" + //
108 		     "# rows: 0\n" + //
109 		     "# columns: 0\n", 
110 		     OctaveIO.toText("matrix", matrix));
111     }
112 
113     /**
114      * @throws Exception
115      */
116     @Test public void test2dToText() throws Exception {
117         final double[] numbers = { 1, 2, 3, 4, 5, 6 };
118         final OctaveDouble matrix = new OctaveDouble(numbers, 2, 3);
119         assertEquals(2, matrix.getSize(1));
120         assertEquals(3, matrix.getSize(2));
121         assertEquals("# name: mymatrix\n" + //
122 		     "# type: matrix\n" + //
123 		     "# rows: 2\n" + //
124 		     "# columns: 3\n" + //
125 		     " 1.0 3.0 5.0\n" + //
126 		     " 2.0 4.0 6.0\n", 
127 		     OctaveIO.toText("mymatrix", matrix));
128     }
129 
130     /**
131      * @throws Exception
132      */
133     @Test public void test2dToTextB() throws Exception {
134         final OctaveDouble matrix = new OctaveDouble(2, 3);
135         assertEquals(2, matrix.getSize(1));
136         assertEquals(3, matrix.getSize(2));
137         assertEquals("# name: matrix\n" + //
138 		     "# type: matrix\n" + //
139 		     "# rows: 2\n" + //
140 		     "# columns: 3\n" + //
141 		     " 0.0 0.0 0.0\n" + //
142 		     " 0.0 0.0 0.0\n", 
143 		     OctaveIO.toText("matrix", matrix));
144         matrix.set(42, 1, 2);
145         assertEquals("# name: myother\n" + //
146 		     "# type: matrix\n" + //
147 		     "# rows: 2\n" + //
148 		     "# columns: 3\n" + //
149 		     " 0.0 42.0 0.0\n" + //
150 		     " 0.0 0.0 0.0\n", 
151 		     OctaveIO.toText("myother", matrix));
152         matrix.set(2, 2, 1);
153         assertEquals("# name: myother\n" + //
154 		     "# type: matrix\n" + //
155 		     "# rows: 2\n" + //
156 		     "# columns: 3\n" + //
157 		     " 0.0 42.0 0.0\n" + //
158 		     " 2.0 0.0 0.0\n", 
159 		     OctaveIO.toText("myother", matrix));
160         matrix.set(4.0, 2, 2);
161         assertEquals("# name: myother\n" + //
162 		     "# type: matrix\n" + //
163 		     "# rows: 2\n" + //
164 		     "# columns: 3\n" + //
165 		     " 0.0 42.0 0.0\n" + //
166 		     " 2.0 4.0 0.0\n", 
167 		     OctaveIO.toText("myother", matrix));
168     }
169 
170     /**
171      * @throws Exception
172      */
173     @Test public void testOctaveScalar() throws Exception {
174         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
175         final OctaveObject i1 = Octave.scalar(42);
176         octave.put("i", i1);
177         final OctaveDouble i2 = octave.get(OctaveDouble.class, "i");
178         assertEquals(i1, i2);
179         octave.close();
180     }
181 
182     /**
183      * @throws Exception
184      */
185     @Test public void testOctaveGet() throws Exception {
186         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
187         octave.eval("m=[1 2;3 4];");
188         final OctaveDouble m = octave.get(OctaveDouble.class, "m");
189         assertEquals("# name: m\n" + //
190 		     "# type: matrix\n" + //
191 		     "# rows: 2\n" + //
192 		     "# columns: 2\n" + //
193 		     " 1.0 2.0\n" + //
194 		     " 3.0 4.0\n",  //
195 		     OctaveIO.toText("m", m));
196         octave.close();
197     }
198 
199     /**
200      * @throws Exception
201      */
202     @Test public void testOctaveSetExecGet() throws Exception {
203         final double[] numbers = { 1, 2, 3, 4, 5, 6 };
204         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
205         final OctaveDouble in = new OctaveDouble(numbers, 2, 3);
206         octave.put("in", in);
207         octave.eval("out=in;");
208         final OctaveDouble out = octave.get(OctaveDouble.class, "out");
209         assertEquals(OctaveIO.toText(in), OctaveIO.toText(out));
210         octave.eval("slicerow=in(2,:); slicecol=in(:,2);");
211         OctaveDouble slicerow = octave.get(OctaveDouble.class, "slicerow");
212         OctaveDouble slicecol = octave.get(OctaveDouble.class, "slicecol");
213         assertEquals(2.0, slicerow.get(1, 1), 0.0);
214         assertEquals(4.0, slicerow.get(1, 2), 0.0);
215         assertEquals(6.0, slicerow.get(1, 3), 0.0);
216         assertEquals(3.0, slicecol.get(1, 1), 0.0);
217         assertEquals(4.0, slicecol.get(2, 1), 0.0);
218         octave.close();
219     }
220 
221     /**
222      * Test how the system handles save of Inf and NaN
223      * 
224      * @throws Exception
225      */
226     @Test public void testOctaveSaveNanInf() throws Exception {
227         final StringWriter stderr = new StringWriter();
228         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
229         octave.setErrorWriter(stderr);
230 
231         octave.eval("ok=1;");
232         final OctaveDouble okOne = Octave.scalar(1);
233         OctaveDouble ok;
234 
235         octave.eval("xnan=[NaN 0];");
236         ok = octave.get(OctaveDouble.class, "ok");
237         assertEquals(okOne, ok);
238         final OctaveDouble xnan = octave.get(OctaveDouble.class, "xnan");
239         assertEquals(Double.NaN, xnan.get(1, 1), 0.0);
240         assertEquals(Double.valueOf(0), xnan.get(1, 2), 0.0);
241         ok = octave.get(OctaveDouble.class, "ok");
242         assertEquals(okOne, ok);
243 
244         octave.eval("xinf=[Inf -Inf];");
245         ok = octave.get(OctaveDouble.class, "ok");
246         assertEquals(okOne, ok);
247         final OctaveDouble xinf = octave.get(OctaveDouble.class, "xinf");
248         assertEquals(Double.POSITIVE_INFINITY, xinf.get(1, 1), 0.0);
249         assertEquals(Double.NEGATIVE_INFINITY, xinf.get(1, 2), 0.0);
250         ok = octave.get(OctaveDouble.class, "ok");
251         assertEquals(okOne, ok);
252 
253         octave.close();
254         stderr.close();
255 	// No warning when saving matrix with NaN/Inf
256         assertEquals("", stderr.toString()); 
257     }
258 
259     /**
260      * Test
261      */
262     @Test public void testOctave3dMatrix() {
263         final OctaveDouble matrix3d = new OctaveDouble(3, 4, 2);
264         matrix3d.set(42.0, 1, 3, 2);
265         matrix3d.set(-1.0, 3, 1, 1);
266         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
267         octave.put("matrix3d", matrix3d);
268         octave.eval("x1 = matrix3d(:,:,1);");
269         octave.eval("x2 = matrix3d(:,:,2);");
270         octave.eval("x3 = matrix3d(:,3,:);");
271         octave.eval("x4 = matrix3d(3,:,:);");
272         final OctaveDouble x1 = octave.get(OctaveDouble.class, "x1");
273         final OctaveDouble x2 = octave.get(OctaveDouble.class, "x2");
274         final OctaveDouble x3 = octave.get(OctaveDouble.class, "x3");
275         final OctaveDouble x4 = octave.get(OctaveDouble.class, "x4");
276         octave.close();
277         assertEquals( 0.0, x1.get(1, 3), 0.0);
278         assertEquals(-1.0, x1.get(3, 1), 0.0);
279         assertEquals(42.0, x2.get(1, 3), 0.0);
280         assertEquals( 0.0, x2.get(3, 1), 0.0);
281         assertEquals(42.0, x3.get(1, 1, 2), 0.0);
282         assertEquals(-1.0, x4.get(1, 1, 1), 0.0);
283     }
284 
285     /** Test */
286     @Test public void testOctaveNdMatrix() {
287         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
288         final Map<String, OctaveObject> vars = 
289 	    new TreeMap<String, OctaveObject>();
290         final double[] bigdata = new double[2 * 3 * 4];
291         for (int idx = 0; idx < bigdata.length; idx++) {
292             bigdata[idx] = idx + 1.0;
293         }
294         final double[] data2d = {1.0, 2.0, 3.0, 5.0, 8.0, 13.0};
295         final double[] datascalar = {42.0};
296         vars.put("bigmatrix",    new OctaveDouble(bigdata, 1, 2, 3, 4));
297         vars.put("matrix2d",     new OctaveDouble(data2d, 2, 3));
298         vars.put("matrixscalar", new OctaveDouble(datascalar, 1, 1));
299         vars.put("matrixzero",   new OctaveDouble(0, 0, 0, 0));
300         vars.put("matrixzero2d", new OctaveDouble(0, 0));
301         octave.putAll(vars);
302         OctaveDouble matrixzero = octave.get(OctaveDouble.class, "matrixzero");
303         OctaveDouble matrix2d = octave.get(OctaveDouble.class, "matrix2d");
304         OctaveDouble bigmatrix = octave.get(OctaveDouble.class, "bigmatrix");
305         OctaveDouble matrixzero2d = octave.get(OctaveDouble.class, "matrixzero2d");
306         OctaveDouble matrixscalar = octave.get(OctaveDouble.class, "matrixscalar");
307         assertEquals(matrixzero,   vars.get("matrixzero"));
308         assertEquals(matrixzero2d, vars.get("matrixzero2d"));
309         assertEquals(matrixscalar, vars.get("matrixscalar"));
310         assertEquals(matrix2d,     vars.get("matrix2d"));
311         assertEquals(bigmatrix,    vars.get("bigmatrix"));
312         octave.close();
313 
314         assertEquals("# name: matrixzero2d\n" + //
315 		     "# type: matrix\n" + //
316 		     "# rows: 0\n" + //
317 		     "# columns: 0\n", //
318 		     OctaveIO.toText("matrixzero2d", matrixzero2d));
319 
320         assertEquals("# name: matrixzero\n" + //
321 		     "# type: matrix\n" + //
322 		     "# ndims: 4\n" + //
323 		     " 0 0 0 0\n",  // 
324 		     OctaveIO.toText("matrixzero", matrixzero));
325 
326         assertEquals("# name: matrixscalar\n" + //
327 		     "# type: scalar\n" + //
328 		     "42.0\n",  //
329 		     OctaveIO.toText("matrixscalar", matrixscalar));
330 
331         assertEquals("# name: matrix2d\n" + //
332 		     "# type: matrix\n" + //
333 		     "# rows: 2\n" + //
334 		     "# columns: 3\n" + //
335 		     " 1.0 3.0 8.0\n" + //
336 		     " 2.0 5.0 13.0\n",  //
337 		     OctaveIO.toText("matrix2d", matrix2d));
338 
339         assertEquals("# name: bigmatrix\n" + //
340 		     "# type: matrix\n" + //
341 		     "# ndims: 4\n" + //
342 		     " 1 2 3 4\n" + //
343 		     " 1.0\n" + //
344 		     " 2.0\n" + //
345 		     " 3.0\n" + //
346 		     " 4.0\n" + //
347 		     " 5.0\n" + //
348 		     " 6.0\n" + //
349 		     " 7.0\n" + //
350 		     " 8.0\n" + //
351 		     " 9.0\n" + //
352 		     " 10.0\n" + //
353 		     " 11.0\n" + //
354 		     " 12.0\n" + //
355 		     " 13.0\n" + //
356 		     " 14.0\n" + //
357 		     " 15.0\n" + //
358 		     " 16.0\n" + //
359 		     " 17.0\n" + //
360 		     " 18.0\n" + //
361 		     " 19.0\n" + //
362 		     " 20.0\n" + //
363 		     " 21.0\n" + //
364 		     " 22.0\n" + //
365 		     " 23.0\n" + //
366 		     " 24.0\n",  //
367 		     OctaveIO.toText("bigmatrix", bigmatrix));
368     }
369 
370     /** Test that we can get and set globals */
371     @Test public void testOctaveGlobal() {
372         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
373 
374         octave.eval("global x");
375         octave.put("x", Octave.scalar(42.0));
376 
377         final OctaveDouble x = octave.get(OctaveDouble.class, "x");
378         assertEquals(42.0, x.get(1), 0.0);
379 
380         octave.close();
381     }
382 
383 }