Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ExternalEvaluation Scientific/DotNetScilab/cs_example/cs_example.cs @ 10122

Last change on this file since 10122 was 10122, checked in by mkommend, 10 years ago

#2082: Added Scilab .NET coupling to external evaluation branch.

File size: 11.4 KB
Line 
1/*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
4 *
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at   
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12//=============================================================================
13using System;
14using cs_example;
15using DotNetScilab;
16//=============================================================================
17namespace example {
18  class cs_example {
19    //=============================================================================
20    /*
21     * A small example to call scilab from C#
22     * read & write matrix of double, string, boolean, int(32)
23     */
24    static void example_readwriteMatrixOfDouble(Scilab _objScilab) {
25      //=============================================================================
26      // Send a command to scilab
27      // Here , we want to display SCI variable
28      _objScilab.SendScilabJob("disp(\'SCI = \');");
29      _objScilab.SendScilabJob("disp(SCI);");
30      //=============================================================================
31      double[] A = new double[] { 1, 2, 3, 4, 5, 6 };
32      int mA = 2, nA = 3;
33
34      // Write a matrix of double named in scilab
35      _objScilab.createNamedMatrixOfDouble("A", mA, nA, A);
36
37      // display matrix of double by scilab
38      _objScilab.SendScilabJob("disp(\'A =\');");
39      _objScilab.SendScilabJob("disp(A);");
40      //=============================================================================
41      if (_objScilab.getNamedVarType("A") == (int)DotNetScilab.ScilabType.sci_matrix) {
42        Console.WriteLine("A is a matrix of double");
43      }
44      //=============================================================================
45      _objScilab.SendScilabJob("B = A + 1;");
46
47      // get dimensions of a named matrix of double
48      int[] DimB = _objScilab.getNamedVarDimension("B");
49
50      // get named matrix of double
51      double[] B = _objScilab.readNamedMatrixOfDouble("B");
52
53      // display matrix of double from C#
54      Console.WriteLine("");
55      Console.WriteLine("(C#) B =");
56      for (int i = 0; i < DimB[0]; i++) {
57        for (int j = 0; j < DimB[1]; j++) {
58          Console.Write(B[j * DimB[0] + i] + " ");
59        }
60
61        Console.WriteLine("");
62      }
63
64      // display matrix of double by scilab
65      _objScilab.SendScilabJob("disp(\'B =\');");
66      _objScilab.SendScilabJob("disp(B);");
67      //=============================================================================
68    }
69    //=============================================================================
70    static void example_readwriteMatrixOfString(Scilab _objScilab) {
71      //=============================================================================
72      string[] strA = new string[] { "Scilab", "The", "open",
73                                        "source", "for", "numerical",
74                                        "computation" , ":", ")"};
75      int mstrA = 3, nstrA = 3;
76
77      // Write a matrix of string named in scilab
78      _objScilab.createNamedMatrixOfString("string_A", mstrA, nstrA, strA);
79
80      // display matrix of string by scilab
81      _objScilab.SendScilabJob("disp(\'string_A =\');");
82      _objScilab.SendScilabJob("disp(string_A);");
83      //=============================================================================
84      if (_objScilab.getNamedVarType("string_A") == (int)DotNetScilab.ScilabType.sci_strings) {
85        Console.WriteLine("string_A is a matrix of strings");
86      }
87      //=============================================================================
88      _objScilab.SendScilabJob("string_B = convstr(string_A,\'u\');");
89
90      // get dimensions of a named matrix of string
91      int[] DimstrB = _objScilab.getNamedVarDimension("string_B");
92
93      // get named matrix of string
94      string[] strB = _objScilab.readNamedMatrixOfString("string_B");
95
96      Console.WriteLine("");
97      Console.WriteLine("(C#) strB =");
98      for (int i = 0; i < DimstrB[0]; i++) {
99        for (int j = 0; j < DimstrB[1]; j++) {
100          Console.Write(strB[j * DimstrB[0] + i] + " ");
101        }
102
103        Console.WriteLine("");
104      }
105
106      // display matrix of string by scilab
107      _objScilab.SendScilabJob("disp(\'string_B =\');");
108      _objScilab.SendScilabJob("disp(string_B);");
109      //=============================================================================
110    }
111    //=============================================================================
112    static void example_readwriteMatrixOfBoolean(Scilab _objScilab) {
113      //=============================================================================
114      Boolean[] bA = new Boolean[] { false, true, false,
115                                           true, false, true};
116      int mbA = 2, nbA = 3;
117
118      // Write a matrix of string named in scilab
119      _objScilab.createNamedMatrixOfBoolean("boolean_A", mbA, nbA, bA);
120
121      // display matrix of string by scilab
122      _objScilab.SendScilabJob("disp(\'boolean_A =\');");
123      _objScilab.SendScilabJob("disp(boolean_A);");
124      //=============================================================================
125      // check if av
126      if (_objScilab.existNamedVariable("boolean_A") == true) {
127        Console.WriteLine("boolean_A exists in scilab");
128      }
129
130      if (_objScilab.existNamedVariable("boolean_B") == false) {
131        Console.WriteLine("boolean_B does not exist in scilab");
132      }
133      //=============================================================================
134      if (_objScilab.getNamedVarType("boolean_A") == (int)DotNetScilab.ScilabType.sci_boolean) {
135        Console.WriteLine("boolean_A is a matrix of boolean");
136      }
137      //=============================================================================
138      _objScilab.SendScilabJob("boolean_B = ~boolean_A;");
139      // get dimensions of a named matrix of boolean
140      int[] DimbB = _objScilab.getNamedVarDimension("boolean_B");
141
142      // get named matrix of boolean
143      Boolean[] bB = _objScilab.getNamedMatrixOfBoolean("boolean_B");
144
145      Console.WriteLine("");
146      Console.WriteLine("(C#) bB =");
147      for (int i = 0; i < DimbB[0]; i++) {
148        for (int j = 0; j < DimbB[1]; j++) {
149          Console.Write(bB[j * DimbB[0] + i] + " ");
150        }
151
152        Console.WriteLine("");
153      }
154
155      // display matrix of string by scilab
156      _objScilab.SendScilabJob("disp(\'boolean_B =\');");
157      _objScilab.SendScilabJob("disp(boolean_B);");
158      //=============================================================================
159    }
160    //=============================================================================
161    static void example_doplot3d(Scilab _objScilab) {
162      _objScilab.SendScilabJob("plot3d()");
163      while (_objScilab.HaveAGraph()) {
164        _objScilab.doEvent();
165      }
166    }
167    //=============================================================================
168    static void example_readwriteMatrixOfInt(Scilab _objScilab) {
169      //=============================================================================
170      int[] A = new int[] { 1, 2, 3, 4, 5, 6 };
171      int mA = 2, nA = 3;
172
173      // Write a matrix of double named in scilab
174      _objScilab.createNamedMatrixOfInt32("int32_A", mA, nA, A);
175
176      // display matrix of double by scilab
177      _objScilab.SendScilabJob("disp(\'int32_A =\');");
178      _objScilab.SendScilabJob("disp(int32_A);");
179      //=============================================================================
180      if (_objScilab.getNamedVarType("int32_A") == (int)DotNetScilab.ScilabType.sci_ints) {
181        Console.WriteLine("int32_A is a matrix of int(32)");
182      }
183      //=============================================================================
184      _objScilab.SendScilabJob("int32_B = int32_A + 1;");
185
186      // get dimensions of a named matrix of double
187      int[] DimB = _objScilab.getNamedVarDimension("int32_B");
188
189      // get named matrix of double
190      int[] B = _objScilab.readNamedMatrixOfInt32("int32_B");
191
192      // display matrix of double from C#
193      Console.WriteLine("");
194      Console.WriteLine("(C#) int32_B =");
195      for (int i = 0; i < DimB[0]; i++) {
196        for (int j = 0; j < DimB[1]; j++) {
197          Console.Write(B[j * DimB[0] + i] + " ");
198        }
199
200        Console.WriteLine("");
201      }
202
203      // display matrix of double by scilab
204      _objScilab.SendScilabJob("disp(\'int32_B =\');");
205      _objScilab.SendScilabJob("disp(int32_B);");
206      //=============================================================================
207    }
208    //=============================================================================
209    static void example_readwriteComplexMatrixOfDouble(Scilab _objScilab) {
210      //=============================================================================
211      double[] realPartA = new double[] { 1, 2, 3, 4, 5, 6 };
212      double[] imagPartA = new double[] { 6, 5, 4, 3, 2, 1 };
213      int mA = 2, nA = 3;
214
215      // Write a matrix of double named in scilab
216      _objScilab.createNamedComplexMatrixOfDouble("cplx_A", mA, nA, realPartA, imagPartA);
217
218      // display matrix of double by scilab
219      _objScilab.SendScilabJob("disp(\'cplx_A =\');");
220      _objScilab.SendScilabJob("disp(cplx_A);");
221      //=============================================================================
222      _objScilab.SendScilabJob("cplx_B = cplx_A * 2;");
223
224      // get dimensions of a named matrix of double
225      int[] DimB = _objScilab.getNamedVarDimension("cplx_B");
226
227      // get named matrix of double
228      double[] realPartB = _objScilab.readNamedComplexMatrixOfDoubleRealPart("cplx_B");
229      double[] imagPartB = _objScilab.readNamedComplexMatrixOfDoubleImgPart("cplx_B");
230
231      // display matrix of double from C#
232      Console.WriteLine("");
233      Console.WriteLine("(C#) cplx_B =");
234      for (int i = 0; i < DimB[0]; i++) {
235        for (int j = 0; j < DimB[1]; j++) {
236          Console.Write(realPartB[j * DimB[0] + i] + " + i *" + imagPartB[j * DimB[0] + i] + " ");
237        }
238
239        Console.WriteLine("");
240      }
241
242      // display matrix of double by scilab
243      _objScilab.SendScilabJob("disp(\'cplx_B =\');");
244      _objScilab.SendScilabJob("disp(cplx_B);");
245      //=============================================================================
246    }
247    //=============================================================================
248    static void Main(string[] args) {
249      Util.AddScilabInstalltionPathToEnvironment();
250      // start scilab engine with graphics
251      Scilab m_oSCilab = new Scilab(true);
252
253      example_readwriteMatrixOfDouble(m_oSCilab);
254
255      example_readwriteMatrixOfString(m_oSCilab);
256
257      example_readwriteMatrixOfBoolean(m_oSCilab);
258
259      example_readwriteMatrixOfInt(m_oSCilab);
260
261      example_readwriteComplexMatrixOfDouble(m_oSCilab);
262
263      //mkommend 11.11.2013: commented out as this functionality is not completely working
264      //chart is displayed, but hasFigures throws an exception
265      //example_doplot3d(m_oSCilab);
266    }
267    //=============================================================================
268  }
269}
270//=============================================================================
Note: See TracBrowser for help on using the repository browser.