如何通过对 Scilab 函数的引用传递变量

How pass variables by reference to a Scilab function

我想要一个能够改变其输入变量的 Scilab 函数,例如在 C 我可以

void double(int* x){
    *x *= 2;
    return;
}

Scilab中有intpptyfunptraddinteristksadrstk似乎是相关的,但是我找不到任何工作示例。 Scilab 确实有一个 pointer 类型(即 128)。如果你能帮我解决这个问题,我将不胜感激。

P.S.1. 我也反映了这个问题here on Reddit

P.S.2. Scilab 也有 intersci, SWIG, fort, external, callAPI_Scilab/gateway 可以连接 C/C++ 函数或 Fortran 子程序。不幸的是,intersci 已被弃用,SWIG 似乎仅适用于 Linux,C++ 兼容性有限。

P.S.3. scilab 有 function overloading 可以用 deff 定义的函数和 %,<...>,_... 语法。

P.S.4. API_Scilab/gateway 的工作方式,基本上是您使用 [=] 提供的功能开发代码=79=] 文件 api_scilab.h,用 ilib_build 编译它,写一个 loader*.sce 脚本然后用 exec.

加载它

P.S.5. 应该可以用

安装 mingw 编译器
atomsInstall('mingw'); atomsLoad('mingw');

但是我无法按照我所解释的那样让它工作

根据我的理解,这是不可能的,在 scilab 中,输入参数位于函数的右侧,输出位于左侧。参见 https://help.scilab.org/docs/6.0.2/en_US/function.html

[output,...] = function(input,...)

因此,如果您想要一个 input/output 参数,您必须在函数内将输入参数分配给输出参数。

[c] = f1(a, b)
     c = a + b    
endfunction

并且您使用与输入和输出参数相同的变量来调用它:

d = 10;
d = f1(d, 1);

这可以通过使用例如一个 C++ Scilab 6 网关(示例需要机器上的编译器,对于 Linux 和 OSX 用户来说应该不是问题):

gw=[
"#include ""double.hxx"""
"#include ""function.hxx"""
"types::Function::ReturnValue sci_incr(types::typed_list &in, int _iRetCount,"
"                                      types::typed_list &out)"
"{"    
"    if (in.size() != 1 || in[0]->isDouble() == false) {"
"        throw ast::InternalError(""Wrong type/number of input argument(s)"");"
"    }"
"    types::Double *pDbl = in[0]->getAs<types::Double>();"
"    double *pdbl = pDbl->get();"
""    
"    for (int i=0; i < pDbl->getSize(); i++) (*pdbl) += 1.0;"
""
"    return types::Function::OK;"
"}"];
cd TMPDIR;
mputl(gw,TMPDIR+"/sci_incr.cpp");
ulink
ilib_build("incr", ["incr" "sci_incr" "cppsci"],"sci_incr.cpp", [])
exec loader.sce

接口compilation/link后,可以有如下行为:

--> x=1
 x  = 

   1.

--> incr(x)

--> x
 x  = 

   2.

但是,不要将此视为一项功能,因为 Scilab 语言并未设计为使用它!