public void testScalarToText() {
   final OctaveObject integer42 = Octave.scalar(42);
   assertEquals(
       "" //
           + "# name: ans\n" //
           + "# type: scalar\n" //
           + "42.0\n" //
       ,
       OctaveIO.toText(integer42));
   final OctaveObject integer43 = Octave.scalar(43);
   assertEquals(
       "# name: integer43\n# type: scalar\n43.0\n", OctaveIO.toText(integer43, "integer43"));
 }
 /** @throws Exception */
 public void testOctaveSetExecGet() throws Exception {
   final double[] numbers = {1, 2, 3, 4, 5, 6};
   final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
   final OctaveDouble in = new OctaveDouble(numbers, 2, 3);
   octave.put("in", in);
   octave.eval("out=in;");
   final OctaveDouble out = octave.get(OctaveDouble.class, "out");
   assertEquals(OctaveIO.toText(in), OctaveIO.toText(out));
   octave.eval("slicerow=in(2,:); slicecol=in(:,2);");
   final OctaveDouble slicerow = octave.get(OctaveDouble.class, "slicerow");
   final OctaveDouble slicecol = octave.get(OctaveDouble.class, "slicecol");
   assertEquals(2.0, slicerow.get(1, 1));
   assertEquals(4.0, slicerow.get(1, 2));
   assertEquals(6.0, slicerow.get(1, 3));
   assertEquals(3.0, slicecol.get(1, 1));
   assertEquals(4.0, slicecol.get(2, 1));
   octave.close();
 }
Esempio n. 3
0
  @Override
  public OctaveCell read(final BufferedReader reader) {
    String line;
    String token;

    line = OctaveIO.readerReadLine(reader);
    token = "# rows: ";
    if (!line.startsWith(token)) {
      throw new OctaveParseException("Expected <" + token + ">, but got <" + line + ">");
    }
    final int nrows = Integer.parseInt(line.substring(token.length()));

    line = OctaveIO.readerReadLine(reader);
    token = "# columns: ";
    if (!line.startsWith(token)) {
      throw new OctaveParseException("Expected <" + token + ">, but got <" + line + ">");
    }
    final int ncols = Integer.parseInt(line.substring(token.length()));

    final OctaveCell octaveCell = new OctaveCell(nrows, ncols);

    for (int col = 1; col <= ncols; col++) {
      for (int row = 1; row <= nrows; row++) {
        line = OctaveIO.readerReadLine(reader);
        token = "# name: <cell-element>";
        if (!line.equals(token)) {
          throw new OctaveParseException("Expected <" + token + ">, but got <" + line + ">");
        }
        final OctaveType octaveType = OctaveIO.read(reader);
        octaveCell.set(row, col, octaveType);
      }
      line = OctaveIO.readerReadLine(reader);
      token = "";
      if (!line.equals(token)) {
        throw new OctaveParseException("Expected <" + token + ">, but got <" + line + ">");
      }
    }

    return octaveCell;
  }
 /** Test */
 public void test3dToText() {
   final OctaveDouble matrix = new OctaveDouble(3, 4, 2);
   assertEquals(
       "" //
           + "# name: matrix3d\n" //
           + "# type: matrix\n" //
           + "# ndims: 3\n" //
           + " 3 4 2\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + "",
       OctaveIO.toText(matrix, "matrix3d"));
   matrix.set(42.0, 1, 3, 2);
   assertEquals(
       "" //
           + "# name: matrix3d\n" //
           + "# type: matrix\n" //
           + "# ndims: 3\n" //
           + " 3 4 2\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + " 42.0\n 0.0\n 0.0\n" //
           + " 0.0\n 0.0\n 0.0\n" //
           + "",
       OctaveIO.toText(matrix, "matrix3d"));
 }
 /** @throws Exception */
 public void testEmpty2dToText() throws Exception {
   final OctaveDouble matrix = new OctaveDouble(0, 0);
   assertEquals(0, matrix.size(1));
   assertEquals(0, matrix.size(2));
   assertEquals(
       ""
           + //
           "# name: matrix\n"
           + //
           "# type: matrix\n"
           + //
           "# rows: 0\n"
           + //
           "# columns: 0\n"
           + //
           "" //
       ,
       OctaveIO.toText(matrix, "matrix"));
 }
 /** @throws Exception */
 public void testOctaveGet() throws Exception {
   final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
   octave.eval("m=[1 2;3 4];");
   final OctaveDouble m = octave.get(OctaveDouble.class, "m");
   assertEquals(
       ""
           + //
           "# name: m\n"
           + //
           "# type: matrix\n"
           + //
           "# rows: 2\n"
           + //
           "# columns: 2\n"
           + //
           " 1.0 2.0\n"
           + //
           " 3.0 4.0\n" //
       ,
       OctaveIO.toText(m, "m"));
   octave.close();
 }
 /** @throws Exception */
 public void test2dToText() throws Exception {
   final double[] numbers = {1, 2, 3, 4, 5, 6};
   final OctaveDouble matrix = new OctaveDouble(numbers, 2, 3);
   assertEquals(2, matrix.size(1));
   assertEquals(3, matrix.size(2));
   assertEquals(
       ""
           + //
           "# name: mymatrix\n"
           + //
           "# type: matrix\n"
           + //
           "# rows: 2\n"
           + //
           "# columns: 3\n"
           + //
           " 1.0 3.0 5.0\n"
           + //
           " 2.0 4.0 6.0\n" //
       ,
       OctaveIO.toText(matrix, "mymatrix"));
 }
Esempio n. 8
0
 /**
  * @param <T> the type of the return value
  * @param key the name of the variable
  * @return the value from octave or null if the variable does not exist
  * @throws OctaveClassCastException If the value can not be cast to T
  */
 @SuppressWarnings("unchecked")
 public <T extends OctaveType> T get(final String key) {
   return (T) octaveIO.get(key);
 }
Esempio n. 9
0
 /**
  * Sets all the mappings in the specified map as variables in octave. These mappings replace any
  * variable that octave had for any of the keys currently in the specified map.
  *
  * @param vars the variables to be stored in octave
  */
 public void putAll(final Map<String, OctaveType> vars) {
   octaveIO.set(vars);
 }
Esempio n. 10
0
 /**
  * Sets a value in octave.
  *
  * @param key the name of the variable
  * @param value the value to set
  */
 public void put(final String key, final OctaveType value) {
   octaveIO.set(Collections.singletonMap(key, value));
 }
Esempio n. 11
0
 /** Test */
 public void testEmpty3dToText() {
   final OctaveDouble matrix = new OctaveDouble(0, 0, 0);
   assertEquals(
       "# name: matrix3d\n# type: matrix\n# ndims: 3\n 0 0 0\n",
       OctaveIO.toText(matrix, "matrix3d"));
 }
Esempio n. 12
0
  /** Test */
  public void testOctaveNdMatrix() {
    final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
    final TreeMap<String, OctaveObject> vars = new TreeMap<String, OctaveObject>();
    final double[] bigdata = new double[2 * 3 * 4];
    for (int idx = 0; idx < bigdata.length; idx++) {
      bigdata[idx] = idx + 1.0;
    }
    final double[] data2d = {1.0, 2.0, 3.0, 5.0, 8.0, 13.0};
    final double[] datascalar = {42.0};
    vars.put("bigmatrix", new OctaveDouble(bigdata, 1, 2, 3, 4));
    vars.put("matrix2d", new OctaveDouble(data2d, 2, 3));
    vars.put("matrixscalar", new OctaveDouble(datascalar, 1, 1));
    vars.put("matrixzero", new OctaveDouble(0, 0, 0, 0));
    vars.put("matrixzero2d", new OctaveDouble(0, 0));
    octave.putAll(vars);
    final OctaveDouble matrixzero = octave.get(OctaveDouble.class, "matrixzero");
    final OctaveDouble matrix2d = octave.get(OctaveDouble.class, "matrix2d");
    final OctaveDouble bigmatrix = octave.get(OctaveDouble.class, "bigmatrix");
    final OctaveDouble matrixzero2d = octave.get(OctaveDouble.class, "matrixzero2d");
    final OctaveDouble matrixscalar = octave.get(OctaveDouble.class, "matrixscalar");
    assertEquals(matrixzero, vars.get("matrixzero"));
    assertEquals(matrixzero2d, vars.get("matrixzero2d"));
    assertEquals(matrixscalar, vars.get("matrixscalar"));
    assertEquals(matrix2d, vars.get("matrix2d"));
    assertEquals(bigmatrix, vars.get("bigmatrix"));
    octave.close();

    assertEquals(
        ""
            + //
            "# name: matrixzero2d\n"
            + //
            "# type: matrix\n"
            + //
            "# rows: 0\n"
            + //
            "# columns: 0\n" //
        ,
        OctaveIO.toText(matrixzero2d, "matrixzero2d"));

    assertEquals(
        ""
            + //
            "# name: matrixzero\n"
            + //
            "# type: matrix\n"
            + //
            "# ndims: 4\n"
            + //
            " 0 0 0 0\n" //
        ,
        OctaveIO.toText(matrixzero, "matrixzero"));

    assertEquals(
        ""
            + //
            "# name: matrixscalar\n"
            + //
            "# type: scalar\n"
            + //
            "42.0\n" //
        ,
        OctaveIO.toText(matrixscalar, "matrixscalar"));

    assertEquals(
        ""
            + //
            "# name: matrix2d\n"
            + //
            "# type: matrix\n"
            + //
            "# rows: 2\n"
            + //
            "# columns: 3\n"
            + //
            " 1.0 3.0 8.0\n"
            + //
            " 2.0 5.0 13.0\n" //
        ,
        OctaveIO.toText(matrix2d, "matrix2d"));

    assertEquals(
        ""
            + //
            "# name: bigmatrix\n"
            + //
            "# type: matrix\n"
            + //
            "# ndims: 4\n"
            + //
            " 1 2 3 4\n"
            + //
            " 1.0\n"
            + //
            " 2.0\n"
            + //
            " 3.0\n"
            + //
            " 4.0\n"
            + //
            " 5.0\n"
            + //
            " 6.0\n"
            + //
            " 7.0\n"
            + //
            " 8.0\n"
            + //
            " 9.0\n"
            + //
            " 10.0\n"
            + //
            " 11.0\n"
            + //
            " 12.0\n"
            + //
            " 13.0\n"
            + //
            " 14.0\n"
            + //
            " 15.0\n"
            + //
            " 16.0\n"
            + //
            " 17.0\n"
            + //
            " 18.0\n"
            + //
            " 19.0\n"
            + //
            " 20.0\n"
            + //
            " 21.0\n"
            + //
            " 22.0\n"
            + //
            " 23.0\n"
            + //
            " 24.0\n" //
        ,
        OctaveIO.toText(bigmatrix, "bigmatrix"));
  }
Esempio n. 13
0
 /** @throws Exception */
 public void test2dToTextB() throws Exception {
   final OctaveDouble matrix = new OctaveDouble(2, 3);
   assertEquals(2, matrix.size(1));
   assertEquals(3, matrix.size(2));
   assertEquals(
       ""
           + //
           "# name: matrix\n"
           + //
           "# type: matrix\n"
           + //
           "# rows: 2\n"
           + //
           "# columns: 3\n"
           + //
           " 0.0 0.0 0.0\n"
           + //
           " 0.0 0.0 0.0\n" //
       ,
       OctaveIO.toText(matrix, "matrix"));
   matrix.set(42, 1, 2);
   assertEquals(
       ""
           + //
           "# name: myother\n"
           + //
           "# type: matrix\n"
           + //
           "# rows: 2\n"
           + //
           "# columns: 3\n"
           + //
           " 0.0 42.0 0.0\n"
           + //
           " 0.0 0.0 0.0\n" //
       ,
       OctaveIO.toText(matrix, "myother"));
   matrix.set(2, 2, 1);
   assertEquals(
       ""
           + //
           "# name: myother\n"
           + //
           "# type: matrix\n"
           + //
           "# rows: 2\n"
           + //
           "# columns: 3\n"
           + //
           " 0.0 42.0 0.0\n"
           + //
           " 2.0 0.0 0.0\n" //
       ,
       OctaveIO.toText(matrix, "myother"));
   matrix.set(4.0, 2, 2);
   assertEquals(
       ""
           + //
           "# name: myother\n"
           + //
           "# type: matrix\n"
           + //
           "# rows: 2\n"
           + //
           "# columns: 3\n"
           + //
           " 0.0 42.0 0.0\n"
           + //
           " 2.0 4.0 0.0\n" //
       ,
       OctaveIO.toText(matrix, "myother"));
 }