From: "Brian Schröder" Date: 2005-01-26T21:29:50+09:00 Subject: Re: Hacking NArray On Wed, 26 Jan 2005 21:14:09 +0900 Yoshiki Tsunesada wrote: > Brian, > > Adding methods to NArray object is rather simple: > If an object is of double float, > > static VALUE my_narray_abs_in_place(VALUE self) > { > struct NARRAY *na; > double *ptr; > int i, n; > GetNArray(self, na); > n = na->total; > ptr = (double*) na->ptr; > for (i = 0; i < n; i++) ptr[i] = fabs(ptr[i]); > return self; > } > > and, > > rb_define_method(cNArray, "abs!", my_narray_abs_in_place, 0); > Thank you for this information. It is very helpfull. Maybe it should be included onto the website? best regards, Brian Schr�der > On Wed, 26 Jan 2005 18:33:39 +0900, Brian Schr��der wrote: > > On Wed, 26 Jan 2005 09:26:50 +0900 > > Yoshiki Tsunesada wrote: > > > >>> than I thought. I would really appreciate an example on how to extend > >>> narray. That would be: > >>> - How to create a function that works on one or more arrays (in > >>> place|with copy), one element at a time > >>> - How to create a function that works on one or more arrays with random > >>> access to the entries. > >> Could you show Ruby code you expect to work with it ? > >> > >> Yoshiki > >> > > > > Well: > > > > a.abs! > > > > for example. > > > > And something like: > > > > define_inplace_operator 'abs!', {[[:float, :float], [:double, > > :double], [:int, :int]] => '*p = *p > 0 ? p : -p;'} > > > > in the mkop.rb skript to create those functions automatically in the > > c source. > > > > That would be a nice dream... > > > > But at the moment I'll try to go with the other advice and implement > > my own purely double based volume class. (And with code generation > > maybe also float, int etc, but I have to do some exploration first, > > as I've never written a c extension. > > > > Regards, > > > > Brian >