View Javadoc
1   /*
2    * Copyright 2007, 2008 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;
17  
18  import eu.simuline.octave.type.Octave;
19  import eu.simuline.octave.type.OctaveDouble;
20  
21  /**
22   * @author Kim Hansen
23   */
24  public class RunOctave {
25  
26      /**
27       * @param args
28       */
29      public static void main(final String[] args) {
30          System.out.println("BEGIN");
31          test1();
32          test2();
33          System.out.println("END.");
34      }
35  
36      private static void test1() {
37          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
38  
39          octave.put("a", Octave.scalar(42));
40          octave.eval("a");
41          System.out.println("Java: a = " + octave.get("a"));
42          octave.eval("a=a+10");
43          System.out.println("Java: a = " + octave.get("a"));
44  
45          octave.close();
46      }
47  
48      private static void test2() {
49          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
50          final OctaveDouble a = new OctaveDouble(new double[] { 1, 2, 3, 4 }, 2, 2);
51          octave.put("a", a);
52          final String func = "" //
53                  + "function res = my_func(a)\n" //
54                  + "  res = 2 * a;\n" //
55                  + "endfunction\n" //
56                  + "";
57          octave.eval(func);
58          octave.eval("b = my_func(a);");
59          final OctaveDouble b = octave.get(OctaveDouble.class, "b");
60          octave.close();
61  
62          System.out.println("Java: b(1,1) = " + b.get(1, 1));
63          System.out.println("Java: b(1,2) = " + b.get(1, 2));
64          System.out.println("Java: b(2,1) = " + b.get(2, 1));
65          System.out.println("Java: b(2,2) = " + b.get(2, 2));
66      }
67  
68  }