教你一步一步编写javasci程序【java调用scilab】

如何基于javasci v2 编写java应用程序,下面就一步一步来实现

1 编码

javasci v2提供了java接口来操作scilab引擎
javasci v2提供了响应的对象和方法来执行对应的操作,主要的类是Scilab,这个对象支持:

  • 从scialb引擎 发送/接收数据
  • error管理
  • 执行scialb脚本

javasci v2的类只要集中在两个包中:

  • org.scilab.modules.javasci.*; // Scilab 类所在的包
  • org.scilab.modules.types.*; // 包含 Scilab <=> Java 数据类型映射

    下面是一个典型的例子

 /*
 *
 * This file is released under the 3-clause BSD license. See COPYING-BSD.
 *
 */

import org.scilab.modules.javasci.Scilab;
import org.scilab.modules.types.ScilabType;
import org.scilab.modules.types.ScilabDouble;

class Example1 {

    public static void main(String[] args) {
        try {
            Scilab sci = new Scilab();
            if (sci.open()) {
                /* Send a Scilab instruction */
                sci.exec("foo = [ 2, 4, 6; 4, 0, 10; 6, 10, 12 ];");

/* Retrieve the variable foo */
                ScilabType foo = sci.get("foo");

/* Display the variable */
                System.out.println("Representation of  : "+foo);

/* Get the data and retrieve the 2,2 value */
                double[][] aReal = ((ScilabDouble)foo).getRealPart();
                System.out.println("foo[1,1] = " + aReal[1][1]);

/* Change the value of 2,2 */
                aReal[1][1] = Math.PI;

/* Create a new variable */
                ScilabDouble bar = new ScilabDouble(aReal);

/* Send it to Scilab */
                sci.put("bar",bar);

/* Display it through Scilab */
                sci.exec("disp(bar)");

                sci.close();
            } else {
                System.out.println("Could not start Scilab ");
            }

/* Can be improved by other exceptions: AlreadyRunningException,
 * InitializationException, UndefinedVariableException,
 * UnknownTypeException, etc
 */
        } catch (org.scilab.modules.javasci.JavasciException e) {
            System.err.println("An exception occurred: " + e.getLocalizedMessage());
        }
    }
}

这个例子在D:\Program Files\scilab-5.5.2\modules\javasci\examples\v2路径下

运行结果为:

这里写图片描述
可以查看the documentation of Javasci v2.了解更多的方法和对象

Build

GNU/Linux and Mac OS X下,需要设置Scilab变量的路径(windows下会自动检测)

# Variable SCI
# Under GNU/Linux with the Scilab binary, the path is:
/path/to/scilab-xxx/share/scilab/
# Under GNU/Linux and Mac OS X with the source tree, the path is the base of the source tree
/path/to/scilab/sources/
# Under Mac OS X, the path is:
/Applications/scilab-xxx/Contents/MacOS/share/scilab/
# On Windows, the path is:
C:\Program Files\Scilab-5.3.0\

build 一个代码,需要在classpath中加入对应的两个jar文件:

# For example, create a variable CLASSPATH
$(SCI)/modules/javasci/jar/org.scilab.modules.javasci.jar
$(SCI)/modules/types/jar/org.scilab.modules.types.jar

3 执行

这一步中,javasciv2 依赖两个native 类库,所以路径需要设置一下,就是bin文件夹的路径

# For example, create a variable LIBPATH
# Under GNU/Linux with the Scilab binary, the path is:
/path/to/scilab-xxx/lib/scilab/
# Under GNU/Linux and Mac OS X with the source tree, the path is:
$(SCI)/modules/javasci/.libs/:$(SCI)/modules/.libs/
# Under Mac OS X, the path is:
/Applications/scilab-xxx/Contents/MacOS/lib/scilab/
# Under Windows, the path is:
set LIBPATH="C:\Program Files\Scilab-5.5.2\bin"

运行程序的命令行如下:

java -cp $CLASSPATH:. -DSCI=$SCI -Djava.library.path=$LIBPATH Example1

A sample Makefile

SCI = /path/to/scilab/share/scilab/
CLASSPATH = $(SCI)/modules/javasci/jar/org.scilab.modules.javasci.jar:$(SCI)/modules/types/jar/org.scilab.modules.types.jar
LIB_PATH = /path/to/scilab/lib/scilab
all:
javac -cp $(CLASSPATH) Example1.java
java -cp $(CLASSPATH):. -DSCI=$(SCI) -Djava.library.path=$(LIB_PATH) Example1

可以在路径 D:\Program Files\scilab-5.5.2\modules\javasci\examples\v2 下看到 build.bat

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值