cmplx在C语言,在C/C++中使用Octave库函数 | 翟鸣的技术博客/Tech Tootoogo

以前用Matcom把matlab的程序转换成C代码,用来运行一些对速度要求较高的程序,matcom被收购后,就没有新版本出来了,而matlab越出越新,于是乎,求助于Octave了。Octave功能一样强大,不但兼容matlab语法和程序,而且原生的C函数库,很容易在C/C++下调用。在网上搜到了一个使用Octave C/C++库函数编程的教程,贴上来,以备不时之需。

Howto Use Octave Functions in

C/C++ Programs

1 Getting Started

Octave is a very nice free Matlab clone and provides many helpful mathematical func-

tions. Especially linear algebra calculations are rather easy, but Octave comes also

with lot more. For more information and to download octave see the homepage of the

project at www.gnu.org/software/octave/. To use the very powerful Octave functions

in your C/C++ programs include the main octave header file

#include

which is mostly located in \usr\local\include\octave-version-number Mostly, it

will not be necessary to include further header files, since this file already includes most

of the octave functions. Nevertheless we will refer to the respective special headers in

the following to give you the chance to gain some further information.

To compile your C/C++ code, e.g. with g++, set the -I option of your compiler to

the location of the octave include files. For the linking process octave provides you with

a very useful make file mkoctfile. Standardly this make file is also used for translating

octave code into .oct files. It comes with the option --link-stand-alone which

produces an executable file of your C/C++ program containing octave subroutines.

The following Makefile represents a simple example to compile and link your program

called test at the end:

Makefile

makefile:

all: test

clean:

-rm test.o test

test: test.o

mkoctfile --link-stand-alone -o test test.o

test.o: main.cpp

g++ -c -I$(OCTAVE_INCLUDE)

-I$(OCTAVE_INCLUDE)octave -o test.o main.cpp

Please note that the spaces before the commands must be a tabulator character accord-

ing to standard rules of Makefiles. The environment variable $OCTAVE_INCLUDE should

be set to your octave include path, which could be, e.g., /usr/include/octave-2.1.73.

By using the command make all your program will be compiled and linked.

2 Complex Numbers

Octave defines its own complex data structure in the header oct-cmplx.h. The pro-

grammers propose: “By using this file instead of ‘complex.h’, we can easily avoid

buggy implementations of the standard complex data type (if needed).” Thus the

data structure is defined as

typedef std::complex Complex

To define a complex number, e.g. 0.7 + 0.3i, within your program use

Complex number = Complex (0.7, 0.3);

The following useful functions are available to manipulate the above given complex

data structure.

// real part

double real (const Complex& z)

// imaginary part

double imag (const Complex& z)

// absolute value

double abs (const Complex& z)

// argument of the complex number

double arg (const Complex& z)

// complex conjugate complex number

Complex conj (const Complex& z)

// complex exponential function

Complex exp (const Complex& z)

// power n of a complex number

Complex pow (const Complex& z, int n)

// square root of a complex number

Complex sqrt (const Complex& z)

3 The Matrix Classes

There are several classes providing functianallity for matrices. The most important

classes are defined in dMatrix.h (real matrices) and in CMatrix.h (complex matrices).

The class for normal matrices is called Matrix and for complex ones ComplexMatrix.

Furthermore one can use two classes for diagonal matrices DiagMatrix and the in case

of complex matrieces ComplexDiagMatrix.

Since all classes contain a great amount of different functions, we concentrate on the

most important ones in the following. All further information

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值