help-octave
[Top][All Lists]
Advanced

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

Re: Octave segfaults on .oct file + more troubles


From: Paul Kienzle
Subject: Re: Octave segfaults on .oct file + more troubles
Date: Mon, 3 Nov 2003 10:03:24 -0500
User-agent: Mutt/1.2.5.1i

On Sun, Nov 02, 2003 at 12:23:01AM +0100, Martijn Brouwer wrote:
> I have problems compiling and running an .oct file with the following code:
> 
> #include <oct.h>
> #include <complex>
> #include <cmath>
> using namespace std;
> 
> #define pi 3.141592654
> 
> DEFUN_DLD(lgm, args, ,"Calculates layer matrix from layer thickness, 
> wavelength, refractive index and cos(th)") {
>       complex<double> n(args(0).complex_value());
>       double d=args(1).double_value();
>       double l=args(2).double_value();
>       complex<double> cosan(args(3).complex_value());
> //    complex<double> expbeta=exp(-2*pi*n*d*cosan/l);
> //    complex<double> expbeta=2
>       double expbeta=2;
> 
>       ComplexMatrix LGM;
>       LGM(0,0)=expbeta;
> 19    LGM(1,1)=1/expbeta;
> 
>       return octave_value(LGM);
> }

The main problem you are experiencing is that 
        1/std::complex<double>
is not defined.  This is easily addressed using
        1./std::complex<double>

The second thing is that you should preallocate
your arrays:

        ComplexMatrix LGM(2,2);

Then there are a number of stylistic issues
such as error checking use of octave types.

The following code compiles and runs for me
(octave-2.1.50 on Debian with gcc 3.3.2):

#include <oct.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

DEFUN_DLD(lgm, args, ,"Calculates layer matrix from layer thickness, 
wavelength, refractive index and cos(th)") 
{
  octave_value_list retval;
  if (args.length() != 4) 
    { 
      print_usage("lgm"); 
      return retval; 
    }
  Complex n(args(0).complex_value());
  double d=args(1).double_value();  
  double l=args(2).double_value();
  Complex cosan(args(3).complex_value());
  if (error_state) return retval;

  Complex expbeta=exp(-2.*M_PI*n*d*cosan/l);

  ComplexMatrix LGM(2,2);
  LGM(0,0)=expbeta;
  LGM(1,1)=1./expbeta;

  return octave_value(LGM);
}

By the way, you won't see much speed improvement
building the matrices in an oct-file.  Instead
you will want to build the layer matrices on
the fly as you are calculating the reflectivity
from your layer model so that the nested for loops
are in C rather than octave.

Paul Kienzle
address@hidden



-------------------------------------------------------------
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]