在 C++ 中嵌入八度音程 - 如何传递可变大小的向量

问题描述

我正在尝试在 C++ 中嵌入 Octave 解释器,并且将被调用的 m 文件将始终是这种类型...

function out = myFunction(pars,val1,val2)

% pars will always be a variable sized row vector of doubles
% val1 will always be a 1 x 1 double
% val2 will also be a  1 x 1 double

% out will always be a [n x 3] array e.g

out = [1 2 3 ; 4 5 6 ; 7 8 9];

endfunction

我已经开始工作了,但前提是我为 pars 传递了一个值。所以,如果我有一个八度值列表,那么



double pars = 10;
double bulkIn = 20;
double bulkOut = 30;

octave_value_list in;
in(0) = pars;
in(1) = bulkIn;
in(2) = bulkOut;

octave_value_list out = octave::feval("myFunction",in);

...这有效。我想不通的是如何将数组放入 in(0) 中。我已经尝试了下面的方法,但如果失败,因为“没有已知的参数 1 从 'std::vector

最终,pars 将作为 std::vector 的参数从 'main' 进入 OctaveCallerFunction。所以我的问题是如何正确地将可变大小的行向量放入 in(0) 中?

#include <iostream>
#include <oct.h>
#include <octave.h>
#include <parse.h>
#include <interpreter.h>
#include <vector>

using namespace std;

int octaveCallerFunction() {

    static int started = 0;
    static octave::interpreter interpreter;

    // check to see if the interpreter has started
    // and initialise it if not.
    if (started == 0) {
        interpreter.initialize_history(false);
        interpreter.initialize();
        interpreter.execute();
        string path = "<the relevant path goes here>";
        octave_value_list p;
        p(0) = path;
        octave_value_list o1 = octave::feval ("addpath",p,1);
        cout << "In interpreter initialise loop" << endl;
        started = 1;
    }

    octave_value_list in;

    vector<double> pars = {1,2,3,4};

    double bulkIn = 2.073e-6;
    double bulkOut = 6.35e-6;

    in(0) = pars;
    in(1) = octave_value(bulkIn);
    in(2) = octave_value(bulkOut);

    octave_value_list out = octave::feval ("myFunction",in,1);

    if (out.length () > 0)
      std::cout << "Output is "
                << out(0).matrix_value(0)
                << std::endl;
    else
      std::cout << "invalid\n";

    return 0;
}

int main(void) {


    octaveCallerFunction();
    return 0;
}

解决方法

我仍然不是 100% 你想要达到的目标(你的评论听起来与原始问题相反),但我希望这个例子无论如何都能有所帮助:)

%% in file myFunction.m
function Out = myFunction( pars,val1,val2 )
    Out = (val1 + val2) .* pars;
endfunction

// In file octtest.cpp
#include <iostream>
#include <vector>
#include <oct.h>
#include <octave.h>
#include <parse.h>
#include <interpreter.h>

void octaveCallerFunction() {
    octave::interpreter interpreter;
    Matrix P(1,3),A(1,B(1,3);
    octave_value_list in,out;

    P(0,0)= 1; P(0,1) = 2; P(0,2) = 3;
    A(0,0)= 4; A(0,1) = 5; A(0,2) = 6;
    B(0,0)= 7; B(0,1) = 8; B(0,2) = 9;
    in(0) = P; in(1) = A; in(2) = B;

    interpreter.execute();
    out = octave::feval ("myFunction",in,1);
    interpreter.shutdown ();

 // Use normal octave facilities to print
    std::cout << "Output directly from Matrix type =" << out(0).matrix_value(); // std::endl implied by Matrix

 // Collect into std::vector first and print using that
    std::vector<double>  outvector = { out(0).matrix_value()(0),out(0).matrix_value()(1),out(0).matrix_value()(2)  };
    std::cout << "Output from standard std::vector =";
    for( int i = 0; i < outvector.size(); i++ ) { std::cout << ' ' << outvector[i]; }
    std::cout << std::endl;
}

int main(void) { octaveCallerFunction(); }

编译:

mkoctfile --link-stand-alone octtest.cpp -o octtest

输出:

Output directly from Matrix type = 11 26 45
Output from standard std::vector = 11 26 45