From help-octave-request at bevo dot che dot wisc dot edu Mon Nov 3 09:03:31 2003 Subject: Re: Octave segfaults on .oct file + more troubles From: Paul Kienzle To: Martijn Brouwer Cc: octave Date: Mon, 3 Nov 2003 10:03:24 -0500 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 > #include > #include > using namespace std; > > #define pi 3.141592654 > > DEFUN_DLD(lgm, args, ,"Calculates layer matrix from layer thickness, wavelength, refractive index and cos(th)") { > complex n(args(0).complex_value()); > double d=args(1).double_value(); > double l=args(2).double_value(); > complex cosan(args(3).complex_value()); > // complex expbeta=exp(-2*pi*n*d*cosan/l); > // complex 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 is not defined. This is easily addressed using 1./std::complex 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 #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 pkienzle at nist dot gov ------------------------------------------------------------- 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 -------------------------------------------------------------