help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ using Octave headers, mkoctfile


From: Steve C. Thompson
Subject: Re: C++ using Octave headers, mkoctfile
Date: Fri, 18 Feb 2005 11:36:10 -0800
User-agent: Mutt/1.3.28i

> Perhaps, before trying to produce C++ code, you should try to learn how
> to use Octave's vector abilities at their best.  For example, the code
> you wrote can be vectorialised as:
> 
> N=30; W=exp(j*2*pi*[0:N-1]'*[1:N]/N);
> 
> and it runs faster.
You are absolutely correct.  But this code snippet was to serve as an
example only.  Let me simplify the question:

How do I return a complex number from a custom .oct file?

For example, the file sct_complex_return.cc,

#include <octave/oct.h>
#include <octave/oct-cmplx.h>
DEFUN_DLD (sct_complex_return, args, ,
 "Returns a complex number") {
 octave_value retval;
 Complex tmp = exp(1);
 return retval = tmp;
}

compiles to sct_complex_return.oct with the command ``mkoctfile
sct_complex_return.cc''.  Running ``sct_complex_return'' at the octave
prompt yields

octave:1> sct_complex_return
ans = 2.7183

But, I want to return exp(j), which is

octave:2> exp(j)
ans = 0.54030 + 0.84147i

Looking at octave/oct-cmplx.h:

inline Complex
exp (const Complex& z)
{
          return std::exp (z);
}

I don't know how to take this definition and apply it to real code.
Several stabes in the dark:

Complex tmp = exp(1); \\ thinking the argument was assumed imaginary
Complex tmp = exp(Complex& 1); \\ mimicing the expression in the header
Complex tmp = exp(I); \\ this is they way you do it in standard C (I think)

etc.

Concerning optimizing my Octave code.  I've vectorized as much as
possible, but the simulation simply must perform a while loop until a
certain condition is met, and this takes days.  Vectorizing the code
improves performance, but the algorithm can't sidestep the loop.

Thanks,
Steve

On Feb 18 18:40PM, Francesco Potorti` wrote:
> >I have learned how to generate random variables -- a big step.  But I'm
> >currently stuck on building a complex matrix (or returning a complex
> >number).  For example, what would the C++ code be to generate:
> >
> >n=[0:29]';
> >W=zeros(30,30);
> >for k=1:30
> > W(:,k)=exp(j*2*pi*k*n/30);
> >end
> 
> Perhaps, before trying to produce C++ code, you should try to learn how
> to use Octave's vector abilities at their best.  For example, the code
> you wrote can be vectorialised as:
> 
> N=30; W=exp(j*2*pi*[0:N-1]'*[1:N]/N);
> 
> and it runs faster.
> 
> -- 
> Francesco Potort? (ricercatore)        Voice: +39 050 315 3058 (op.2111)
> ISTI - Area della ricerca CNR          Fax:   +39 050 313 8091
> via G. Moruzzi 1, I-56124 Pisa         Email: address@hidden
> Web: http://fly.isti.cnr.it/           Key:   fly.isti.cnr.it/public.key



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]