001/* Matlab engine interface test (demo) 002 003 Copyright (c) 1998-2014 The Regents of the University of California and 004 Research in Motion Limited. 005 All rights reserved. 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the above 009 copyright notice and the following two paragraphs appear in all copies 010 of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA OR RESEARCH IN MOTION 013 LIMITED BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, 014 INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS 015 SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA 016 OR RESEARCH IN MOTION LIMITED HAVE BEEN ADVISED OF THE POSSIBILITY OF 017 SUCH DAMAGE. 018 019 THE UNIVERSITY OF CALIFORNIA AND RESEARCH IN MOTION LIMITED 020 SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 022 PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" 023 BASIS, AND THE UNIVERSITY OF CALIFORNIA AND RESEARCH IN MOTION 024 LIMITED HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 025 ENHANCEMENTS, OR MODIFICATIONS. 026 PT_COPYRIGHT_VERSION_2 027 COPYRIGHTENDKEY 028 029 */ 030package ptolemy.matlab.test; 031 032import ptolemy.data.ArrayToken; 033import ptolemy.data.ComplexMatrixToken; 034import ptolemy.data.DoubleMatrixToken; 035import ptolemy.data.IntToken; 036import ptolemy.data.RecordToken; 037import ptolemy.data.StringToken; 038import ptolemy.data.Token; 039import ptolemy.kernel.util.IllegalActionException; 040import ptolemy.math.Complex; 041import ptolemy.matlab.Engine; 042 043/////////////////////////////////////////////////////////////////////////// 044//// TestEngine 045 046/** Provides a simple demo of capabilities.<p> 047 Use: "ptinvoke ptolemy.matlab.test.TestEngine" 048 in this directory to execute, output goes to stdout.<p> 049 TODO: automate regression test (python?, jtcl?) 050 @author Zoltan Kemenczy, Research in Motion Limited 051 @version $Id$ 052 @since Ptolemy II 2.0 053 @Pt.ProposedRating Red (cxh) 054 @Pt.AcceptedRating Red (cxh) 055 */ 056public class TestEngine { 057 /** Invoke the Matlab engine and run a few tests. 058 * @param args Not used. 059 * @exception IllegalActionException If there is a problem running the tests. 060 */ 061 public static void main(String[] args) throws IllegalActionException { 062 Engine eng = new Engine(); 063 eng.setDebugging((byte) 0); 064 065 long[] engineHandle = eng.open(); 066 eng.evalString(engineHandle, "clear"); 067 068 DoubleMatrixToken tx = new DoubleMatrixToken( 069 new double[][] { { 1, 2, 3 } }); 070 System.out.println("\nNote: All data output is via " 071 + "Token.toString() on tokens"); 072 System.out.println("that are put/get from the matlab engineHandle."); 073 System.out.println("\nCreate 1x3 double matrix x:"); 074 eng.put(engineHandle, "x", tx); 075 System.out.println("x = " + tx.toString()); 076 System.out.println("Eval: y = x.*x;"); 077 eng.evalString(engineHandle, "y = x.*x;"); 078 079 DoubleMatrixToken ty = (DoubleMatrixToken) eng.get(engineHandle, "y"); 080 System.out.println("y = " + ty.toString()); 081 082 System.out.println("\nCreate 2x3 double matrix x:"); 083 tx = new DoubleMatrixToken(new double[][] { { 1, 2, 3 }, { 4, 5, 6 } }); 084 eng.put(engineHandle, "x", tx); 085 System.out.println("x = " + tx.toString()); 086 System.out.println("Eval: y = x.*x;"); 087 eng.evalString(engineHandle, "y = x.*x;"); 088 ty = (DoubleMatrixToken) eng.get(engineHandle, "y"); 089 System.out.println("y = " + ty.toString()); 090 091 System.out.println("\nEval: z = exp(j*pi/2*x);"); 092 eng.evalString(engineHandle, "z = exp(j*pi/2*x);"); 093 094 ComplexMatrixToken tz = (ComplexMatrixToken) eng.get(engineHandle, "z"); 095 System.out.println("z = " + tz.toString()); 096 097 System.out.println("\nEval: w = z';"); 098 eng.evalString(engineHandle, "w = z';"); 099 100 ComplexMatrixToken tw = (ComplexMatrixToken) eng.get(engineHandle, "w"); 101 System.out.println("w = " + tw.toString()); 102 103 System.out.println("\nCreate 1xn string s:"); 104 105 StringToken ts = new StringToken("a string"); 106 System.out.println("s = " + ts.toString()); 107 eng.put(engineHandle, "s", ts); 108 System.out.println("\nEval: rc = [s;s];"); 109 eng.evalString(engineHandle, "rc = [s;s];"); 110 111 Token ta = eng.get(engineHandle, "rc"); 112 System.out.println("rc = " + ta.toString()); 113 114 System.out.println("\nCreate 2xn string s:"); 115 ta = new ArrayToken(new Token[] { new StringToken("str one"), 116 new StringToken("str two") }); 117 System.out.println("s = " + ta.toString()); 118 eng.put(engineHandle, "s", ta); 119 System.out.println("\nEval: rr = [s,s];"); 120 eng.evalString(engineHandle, "rr = [s,s];"); 121 ta = eng.get(engineHandle, "rr"); 122 System.out.println("rr = " + ta.toString()); 123 124 System.out.println("\nCreate 1x1 struct r (RecordToken):"); 125 126 RecordToken tr = new RecordToken(new String[] { "x", "r", "s" }, 127 new Token[] { tx, new RecordToken(new String[] { "a" }, 128 new Token[] { new IntToken() }), ts }); 129 System.out.println("r = " + tr.toString()); 130 eng.put(engineHandle, "r", tr); 131 132 Token t = eng.get(engineHandle, "r"); 133 System.out.println("\nRead back 1x1 struct r into RecordToken t:"); 134 System.out.println("t = " + t.toString()); 135 136 System.out.println("\nEval: ta = [r,r,r;r,r,r];"); 137 eng.evalString(engineHandle, "ta = [r,r,r;r,r,r];"); 138 t = eng.get(engineHandle, "ta"); 139 System.out.println("\nRead 2x3 struct ta into ArrayToken " 140 + "of ArrayToken of RecordTokens:"); 141 System.out.println("ta = " + t.toString()); 142 143 System.out.println("\nCreate 1x3 cell array from ta, " 144 + "an ArrayToken of RecordTokens:"); 145 146 RecordToken r1 = new RecordToken(new String[] { "a" }, 147 new Token[] { new ComplexMatrixToken(new Complex[][] { 148 { new Complex(1.0, 1.0), new Complex(2.0, 2.0) } }) }); 149 RecordToken r2 = new RecordToken(new String[] { "a" }, 150 new Token[] { new ComplexMatrixToken(new Complex[][] { 151 { new Complex(3.0, 3.0), new Complex(4.0, 4.0) } }) }); 152 RecordToken r3 = new RecordToken(new String[] { "a" }, 153 new Token[] { new ComplexMatrixToken(new Complex[][] { 154 { new Complex(5.0, 5.0), new Complex(6.0, 6.0) } }) }); 155 ta = new ArrayToken(new Token[] { r1, r2, r3 }); 156 System.out.println("ta = " + ta.toString()); 157 eng.put(engineHandle, "ta", ta); 158 eng.evalString(engineHandle, "tb = ta;"); 159 ta = eng.get(engineHandle, "tb"); 160 System.out.println("\nRead 1x3 cell array back into tb:"); 161 System.out.println("tb = " + ta.toString()); 162 163 eng.close(engineHandle); 164 } 165}