简体   繁体   English

在C ++ API中使用Octave内置函数mldivide

[英]Using Octave built-in function mldivide in C++ API

I am programming in C++ and want to use the powerful built-in functions in Octave. 我正在用C ++编程,并且想在Octave中使用强大的内置函数。 I followed the standalone program guideline on Octave website. 我遵循了Octave网站上的独立程序指南。 I can run the function norm (which is called as Fnorm in C++) with the sample code successfully. 我可以使用示例代码成功运行函数norm(在C ++中称为Fnorm)。 Now I want to use the function mldivide to solve a linear equation. 现在,我想使用函数mldivide求解线性方程。

#include <iostream>
#include <octave/oct.h>
#include <octave/builtin-defun-decls.h>

octave_value_list input;
octave_value_list retval;

int main (void) {

Matrix A(4,4);

for (octave_idx_type i = 0; i < 4; i++)
   for (octave_idx_type j = 0; j < 4; j++)
    A(i,j) = 1.0 / (static_cast<double> (i) +static_cast<double> ( j ) + 1.0 ) ;

    ColumnVector b(4,1.0);
    input.append(A);
    input.append(b);

    retval=Fmldivide(input);
    ColumnVector x =retval(0).column_vector_value();

    std::cout << "A = " << std::endl << A << std::endl
              << "b = " << std::endl << b << std::endl
              << "x = " << std::endl << x << std::endl;
    return 0;
 }

But there are errors as follow. 但是有以下错误。

main.cpp: In function 'int main()':

main.cpp:23:26: error: invalid initialization of reference of type 'const octave_value_list&' from expression of type 'Matrix'

In file included from main.cpp:3:0:

/usr/local/octave/3.8.0/include/octave-3.8.0/octave/../octave/builtin-defun-decls.h:198:1: error: in passing argument 1 of 'octave_value_list Fmldivide(const octave_value_list&, int)'

Any thoughts? 有什么想法吗?

There are two reasons why this is not working: 不能正常工作的原因有两个:

  1. Fmldivide takes an octave_value_list as input Fmldivideoctave_value_list作为输入

  2. Fmldivide returns an octave_value_list but you are declaring a ColumnVector Fmldivide返回一个octave_value_list但是您正在声明ColumnVector

You can fix the first by: 您可以通过以下方法解决第一个问题:

  1. converting your input into a single octave_value and leaving the conversion to octave_value_list to the compiler: 将您的输入转换为一个octave_value ,并将转换为octave_value_list编译器:

    Fmldivide (octave_value (b), 1); Fmldivide(octave_value(b),1);

  2. use do_binary_op with the left division operator: do_binary_op与左除法运算符一起使用:

    do_binary_op (octave_value::op_ldiv, b, 1); do_binary_op(octave_value :: op_ldiv,b,1);

You can fix the second by using the column_vector_value method: 您可以使用column_vector_value方法修复第二个问题:

  1. if you are using Fmldivide you get a list, so you must index the first element first: 如果使用Fmldivide则会得到一个列表,因此必须首先索引第一个元素:

    ColumnVector x = Fmldivide (b, 1)(0).column_vector_value (); ColumnVector x = Fmldivide(b,1)(0).column_vector_value();

  2. if you use do_binary_op you will get an octave_value so indexing is unecessary: 如果使用do_binary_op您将获得一个octave_value因此索引是不必要的:

    ColumnVector x = do_binary_op (octave_value::op_ldiv, b, 1).column_vector_value (); ColumnVector x = do_binary_op(octave_value :: op_ldiv,b,1).column_vector_value();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM