From help-octave-request at bevo dot che dot wisc dot edu Sat Nov 1 22:06:22 2003 Subject: Re: Octave segfaults on .oct file + more troubles From: Glenn Golden To: Martijn Brouwer Cc: octave Date: Sat, 01 Nov 2003 21:05:27 -0700 This is a multipart MIME message. --==_Exmh_-7016708160 Content-Type: text/plain Martijn, Martijn Brouwer writes: > I have problems compiling and running an .oct file with the following code: > . > . > . > One way to avoid some these problems is to stay in the Octave DLD object regime, rather than trying to cross over between Octave and C++ library objects like complex. At least, this approach has worked for me so far, I'm pretty new to DLDing myself. I'm in the midst right now of writing about a dozen small functions similar to yours, i.e., brief, simple computations. So I put your semantics into one of my DLDs, and added some explanatory comments for you. Here's how it came out. Seems to work. I'm curious about a few things in what I've been doing too, some of which are shown here. Perhaps someone on the list experienced with DLDs can comment. For example, are there any hidden gotchas when calling feval() from DLDs like this (aside from the obvious efficiency issue)? Also, is there a pre-existing way to refer to sqrt(-1) within a DLD, without having to resort to what I did here, i.e. Complex j(0, 1); I did look thru src/DLD-FUNCTIONS and the complex class definitions for anything like this, but if it's there, I missed it. Any other critiques/comments also appreciated. Glenn --==_Exmh_-7016708160 Content-Type: text/plain ; name="lgm.cc" Content-Description: lgm.cc Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="lgm.cc" #include // Most high-level Octave stuff #include // Octave feval() #include // M_PI typedef octave_value_list OVL; // Convenience typedef octave_value OV; // Conveneince DEFUN_DLD(lgm, args, , "Usage: result =3D lgm(n, d, l, cosan)\n" "\n" " n mumble (scalar)\n" " d mumble (real scalar)\n" " l mumble (real scalar)\n" " cosan mumble (scalar)\n" " return 2x2 complex matrix\n") { OVL retval; // Return OVL OVL t_ovl; // Scratch OV t_ov; // Scratch Complex j(0, 1); // Sqrt(-1), for convenience. // Your args. For simplicity, stick with all Octave datatypes, or // native types (like 'double' here) which Octave can manipulate dire= ctly. // = Complex n; double d; double l; Complex cosan; Complex expbeta; // Your intermediate result. ComplexMatrix LGM(2,2); // Dimension it if you know beforehand. retval(0) =3D OV(-1); // Assume error return in what follows. // // Check the number and types of all args before going further. Takes= // only a few minutes to write, avoids time-consuming mysteries later= =2E // print_usage() is an Octave library function which reports the doc // string from the DEFUN_DLD(). // if (args.length() !=3D 4) { print_usage("lgm"); return retval; } if (!args(0).is_scalar_type() || !args(1).is_real_scalar() = || !args(2).is_real_scalar() || !args(3).is_scalar_type()) { print_usage("lgm"); return retval; } // // Now it's safe(er) to assign the DLD args. // n =3D args(0).complex_value(); d =3D args(1).double_value(); l =3D args(2).double_value(); cosan =3D args(3).complex_value(); = // #define BARF_ARGS // Uncomment to verify args. #ifdef BARF_ARGS // Like Dirk said, "test, test, test" cout << "n =3D " << n << "\n"; cout << "d =3D " << d << "\n"; cout << "l =3D " << l << "\n"; cout << "cosan =3D " << cosan << "\n"; #endif // // Compute expbeta using Octave's exp() function, via feval(). Just // like DLD functions, feval() takes its args and returns its results= // in OVLs. Intermediate assignments written out explicitly here // with temps, just to clarify what's going on. // t_ovl(0) =3D OV(-j * 2 * M_PI * n * d * cosan / l); // Arg -> t_ovl(0= ) t_ovl =3D feval("exp", t_ovl, 1); // Re-use t_ovl for result also. t_ov =3D t_ovl(0); // Extract the OV from t_ovl(0) expbeta =3D t_ov.complex_value(); // Extract the complex_value from t= _ov. // // Same as above, sans temps. = // #ifdef OPAQUE expbeta =3D feval("exp", OV(-j*2*M_PI*n*d*cosan/l))(0).complex_value(= ); #endif LGM(0, 0) =3D expbeta; LGM(1, 1) =3D 1 / expbeta; retval(0) =3D OV(LGM); // Return OGM as first elt of our OVL. return retval; } --==_Exmh_-7016708160-- ------------------------------------------------------------- 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 -------------------------------------------------------------