View Javadoc
1   /*
2    * Copyright 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.examples;
17  
18  import eu.simuline.octave.OctaveEngine;
19  import eu.simuline.octave.OctaveEngineFactory;
20  import eu.simuline.octave.type.Octave;
21  import eu.simuline.octave.type.OctaveDouble;
22  import eu.simuline.octave.type.OctaveFunctionHandle;
23  import eu.simuline.octave.type.OctaveString;
24  
25  import static org.junit.Assert.assertEquals;
26  
27  import org.junit.Ignore;
28  import org.junit.Test;
29  
30  /**
31   * http://kenai.com/projects/javaoctave/pages/Home
32   */
33  public class HomeExampleTest {
34  
35      /** Test used on web page */
36      @Test public void test() {
37          // Begin web text
38          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
39          octave.eval("warning off"); // not web text: needed to silence warnings from lsode
40          octave.put("t1", Octave.scalar(0));
41          octave.put("t2", Octave.scalar(1));
42          octave.eval("result = lsode(@(x, t) sqrt(1 - t^2), 0, [t1 t2])(2);");
43          final OctaveDouble result = octave.get(OctaveDouble.class, "result");
44          octave.close();
45          final double integral = result.get(1);
46          assertEquals(Math.PI / 4, integral, 1e-5);
47          // End web text
48      }
49  
50      /**
51       * Test that is too complicated because of bugs in both 3.0 and 3.2
52       */
53      // **** this does not seem to terminate. 
54      @Ignore 
55  @Test public void testWithFunctionInVariable() {
56          // Begin web text
57  System.out.println("1testWithFunctionInVariable");
58      
59          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
60  //        octave.eval("warning off"); // not web text: needed to silence warnings from lsode
61          octave.put("t1", Octave.scalar(0));
62          octave.put("t2", Octave.scalar(1));
63  System.out.println("2testWithFunctionInVariable");
64          if (octave.getVersion().startsWith("3.0.")) {
65              octave.put("fun", new OctaveString("sqrt(1-t**2)"));
66          } else {
67              octave.put("fun", new OctaveFunctionHandle("@(x, t) sqrt (1 - t ^ 2)"));
68          }
69  System.out.println("3testWithFunctionInVariable");
70  octave.eval("fun");
71  octave.eval("t1");
72  octave.eval("t2");
73  octave.eval("fun");
74  // this is what does not terminate 
75  octave.eval("lsode(fun, 0, [t1 t2])");
76          octave.eval("result = lsode(fun, 0, [t1 t2])(2);");
77  System.out.println("3atestWithFunctionInVariable");
78          final OctaveDouble result = octave.get(OctaveDouble.class, "result");
79  System.out.println("4testWithFunctionInVariable");
80          octave.close();
81  System.out.println("5testWithFunctionInVariable");
82          final double integral = result.get(1);
83          assertEquals(Math.PI / 4, integral, 1e-5);
84          // End web text
85  System.out.println("6testWithFunctionInVariable");
86      }
87  
88  }