Class: NArray

### Home Classes Methods

### In Files

- src/narray.c
- narray_ext.rb

### Parent

Object

### Methods

- ::[]
- ::byte
- ::cast
- ::complex
- ::dcomplex
- ::dfloat
- ::float
- ::int
- ::lint
- ::new
- ::object
- ::ref
- ::refer
- ::scomplex
- ::sfloat
- ::sint
- ::to_na
- ::to_narray
- #==
- #all?
- #any?
- #clone
- #coerce
- #collect
- #collect!
- #complex?
- #delete_at
- #dim
- #dimension
- #dup
- #each
- #element_size
- #empty?
- #eql?
- #fill
- #fill!
- #flatten
- #flatten!
- #hash
- #indgen
- #indgen!
- #inspect
- #integer?
- #length
- #map
- #map!
- #mean
- #median
- #newdim
- #newdim!
- #newdim=
- #newrank
- #newrank!
- #newrank=
- #none?
- #original
- #randomn
- #randomn!
- #rank
- #rank_total
- #refer
- #reshape
- #reshape!
- #reverse
- #rms
- #rmsdev
- #rot90
- #shape
- #shape=
- #size
- #sizes
- #stddev
- #to_binary
- #to_f
- #to_i
- #to_s
- #to_string
- #to_type
- #to_type_as_binary
- #total
- #typecode
- #where
- #where2

### Files

- ChangeLog
- MANIFEST
- SPEC.en.txt
- Makefile
- depend

### Class/Module Index

Quicksearch

- FFTW
- NArray
- NArrayScalar
- NMath
- NMatrix
- NMatrixLU
- NVector

No matching classes.

# NArray

initialization of NArray Class

Numerical Array Extention for Ruby

    (C) Copyright 2000-2008 by Masahiro TANAKA

This program is free software. You can distribute/modify this program under the same terms as Ruby itself. NO WARRANTY.

### Constants

BYTE
CLASS_DIMENSION
COMPLEX
DCOMPLEX
DFLOAT
ENDIAN
FLOAT
INT
LINT
NARRAY_VERSION
NONE
OBJECT
ROBJ
SCOMPLEX
SFLOAT
SINT

### Public Class Methods

[](*args) click to toggle source

singleton method: NArray

    static VALUE
     na_s_bracket(int argc, VALUE *argv, VALUE klass)
    {
      VALUE v = rb_ary_new4(argc, argv);
      return na_ary_to_nary( v, klass );
    }

byte(*args) click to toggle source

class method: byte(size1,size2,...,sizeN)

    static VALUE
     na_s_new_byte(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_BYTE, klass); }

    /* class method: sint(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_sint(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SINT, klass); }

    /* class method: int(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_int(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_LINT, klass); }

    /* class method: sfloat(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_sfloat(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SFLOAT, klass); }

    /* class method: float(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_float(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DFLOAT, klass); }

    /* class method: scomplex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SCOMPLEX, klass); }

    /* class method: complex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

cast(array,type=nil) click to toggle source

    # File narray_ext.rb, line 11
    def self.cast(array,type=nil)
      case array
      when NArray
      when Array
        array = NArray.to_na(array)
      else
        raise ArgumentError, "1st argument must be NArray or Array"
      end
      type = array.typecode if type.nil?
      shape = array.shape
      na = self.new(type,*shape)
      na[] = array
      na
    end

complex(*args) click to toggle source

class method: complex(size1,size2,...,sizeN)

    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

dcomplex(*args) click to toggle source

class method: complex(size1,size2,...,sizeN)

    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

dfloat(*args) click to toggle source

class method: float(size1,size2,...,sizeN)

    static VALUE
     na_s_new_float(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DFLOAT, klass); }

    /* class method: scomplex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SCOMPLEX, klass); }

    /* class method: complex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

float(*args) click to toggle source

class method: float(size1,size2,...,sizeN)

    static VALUE
     na_s_new_float(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DFLOAT, klass); }

    /* class method: scomplex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SCOMPLEX, klass); }

    /* class method: complex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

int(*args) click to toggle source

class method: int(size1,size2,...,sizeN)

    static VALUE
     na_s_new_int(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_LINT, klass); }

    /* class method: sfloat(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_sfloat(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SFLOAT, klass); }

    /* class method: float(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_float(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DFLOAT, klass); }

    /* class method: scomplex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SCOMPLEX, klass); }

    /* class method: complex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

lint(*args) click to toggle source

class method: int(size1,size2,...,sizeN)

    static VALUE
     na_s_new_int(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_LINT, klass); }

    /* class method: sfloat(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_sfloat(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SFLOAT, klass); }

    /* class method: float(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_float(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DFLOAT, klass); }

    /* class method: scomplex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SCOMPLEX, klass); }

    /* class method: complex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

new(*args) click to toggle source

class method: new(type, size1,size2,...,sizeN)

    static VALUE
     na_s_new(int argc, VALUE *argv, VALUE klass)
    {
      if (argc == 0)
        rb_raise(rb_eArgError, "Argument required");
      return na_new2(argc-1, argv+1, na_get_typecode(argv[0]), klass);
    }

object(*args) click to toggle source

class method: object(size1,size2,...,sizeN)

    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

ref(p1) click to toggle source

singleton method: NArray.refer( other )

    static VALUE
     na_s_refer(VALUE klass, VALUE self)
    {
      return na_wrap_struct_class(na_ref_alloc_struct(self), klass);
    }

refer(p1) click to toggle source

singleton method: NArray.refer( other )

    static VALUE
     na_s_refer(VALUE klass, VALUE self)
    {
      return na_wrap_struct_class(na_ref_alloc_struct(self), klass);
    }

scomplex(*args) click to toggle source

class method: scomplex(size1,size2,...,sizeN)

    static VALUE
     na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SCOMPLEX, klass); }

    /* class method: complex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

sfloat(*args) click to toggle source

class method: sfloat(size1,size2,...,sizeN)

    static VALUE
     na_s_new_sfloat(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SFLOAT, klass); }

    /* class method: float(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_float(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DFLOAT, klass); }

    /* class method: scomplex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SCOMPLEX, klass); }

    /* class method: complex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

sint(*args) click to toggle source

class method: sint(size1,size2,...,sizeN)

    static VALUE
     na_s_new_sint(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SINT, klass); }

    /* class method: int(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_int(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_LINT, klass); }

    /* class method: sfloat(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_sfloat(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SFLOAT, klass); }

    /* class method: float(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_float(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DFLOAT, klass); }

    /* class method: scomplex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_SCOMPLEX, klass); }

    /* class method: complex(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_complex(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_DCOMPLEX, klass); }

    /* class method: object(size1,size2,...,sizeN) */
    static VALUE
     na_s_new_object(int argc, VALUE *argv, VALUE klass)
    { return na_new2(argc, argv, NA_ROBJ, klass); }

    /* method: dup() */
    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

to_na(*args) click to toggle source

singleton method: NArray.to_na( string, type, size1,size2,...,sizeN ) NArray.to_na( array )

    static VALUE
     na_s_to_na(int argc, VALUE *argv, VALUE klass)
    {
      if (argc < 1) {
        rb_raise(rb_eArgError, "Argument is required");
      }
      if (TYPE(argv[0]) == T_STRING) {
        return na_str_to_na(argc-1,argv+1,argv[0]);
      }
      if (argc > 1) {
        rb_raise(rb_eArgError, "Only one array argument must be provided");
      }
      if (TYPE(argv[0]) == T_ARRAY) {
        return na_ary_to_nary( argv[0], klass );
      }
      if (NA_IsNArray(argv[0])) {
        return argv[0];
      }
      rb_raise(rb_eTypeError, "Argument must be Array or String (or NArray)");
      return Qnil;
    }

to_narray(*args) click to toggle source

singleton method: NArray.to_na( string, type, size1,size2,...,sizeN ) NArray.to_na( array )

    static VALUE
     na_s_to_na(int argc, VALUE *argv, VALUE klass)
    {
      if (argc < 1) {
        rb_raise(rb_eArgError, "Argument is required");
      }
      if (TYPE(argv[0]) == T_STRING) {
        return na_str_to_na(argc-1,argv+1,argv[0]);
      }
      if (argc > 1) {
        rb_raise(rb_eArgError, "Only one array argument must be provided");
      }
      if (TYPE(argv[0]) == T_ARRAY) {
        return na_ary_to_nary( argv[0], klass );
      }
      if (NA_IsNArray(argv[0])) {
        return argv[0];
      }
      rb_raise(rb_eTypeError, "Argument must be Array or String (or NArray)");
      return Qnil;
    }

### Public Instance Methods

==(other) click to toggle source

    # File narray_ext.rb, line 48
    def ==(other)
      other.kind_of?(NArray) &&
        shape == other.shape &&
        eq(other).all?
    end

all?() click to toggle source

    # File narray_ext.rb, line 36
    def all?
      where.size == size
    end

any?() click to toggle source

    # File narray_ext.rb, line 40
    def any?
      where.size > 0
    end

clone() click to toggle source

method: dup()

    VALUE
     na_clone(VALUE self)
    {
      struct NARRAY *org, *cpy;

      GetNArray(self,org);
      cpy = na_alloc_struct(org->type,org->rank,org->shape);
      memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
      return na_wrap_struct(cpy,self);
    }

Also aliased as: dup
coerce(p1) click to toggle source

method: coerce(other)

    static VALUE na_coerce(VALUE self, VALUE other)
    {
      struct NARRAY *a1;

      GetNArray(self,a1);
      return rb_assoc_new( na_cast_object(other,a1->type), self );
    }

collect() click to toggle source

iterator: collect()

    static VALUE
     na_collect(VALUE obj1)
    {
      int i, sz;
      VALUE v, obj2;
      struct NARRAY *a1, *a2;
      char *p1, *p2;
      void (*get)(), (*set)();

      GetNArray(obj1,a1);
      obj2 = na_make_object(a1->type, a1->rank, a1->shape, CLASS_OF(obj1));
      GetNArray(obj2,a2);

      p1  = a1->ptr;
      p2  = a2->ptr;
      sz  = na_sizeof[a1->type];
      get = SetFuncs[NA_ROBJ][a1->type];
      set = SetFuncs[a1->type][NA_ROBJ];

      for ( i=a1->total; i-->0; ) {
        (*get)( 1, &v, 0, p1, 0 );
        v = rb_yield(v);
        (*set)( 1, p2, 0, &v, 0 );
        p1 += sz;
        p2 += sz;
      }
      return obj2;
    }

Also aliased as: map
collect!() click to toggle source

iterator: collect!()

    static VALUE
     na_collect_bang(VALUE self)
    {
      int i, sz;
      VALUE v;
      struct NARRAY *a1;
      char *p1;
      void (*get)(), (*set)();

      GetNArray(self,a1);

      p1  = a1->ptr;
      sz  = na_sizeof[a1->type];
      get = SetFuncs[NA_ROBJ][a1->type];
      set = SetFuncs[a1->type][NA_ROBJ];

      for ( i=a1->total; i-->0; ) {
        (*get)( 1, &v, 0, p1, 0 );
        v = rb_yield(v);
        (*set)( 1, p1, 0, &v, 0 );
        p1 += sz;
      }
      return self;
    }

Also aliased as: map!
complex?() click to toggle source

    # File narray_ext.rb, line 31
    def complex?
      self.typecode==NArray::DCOMPLEX ||
      self.typecode==NArray::SCOMPLEX
    end

delete_at(*args) click to toggle source

delete rows/columns

    # File narray_ext.rb, line 90
    def delete_at(*args)
      if args.size > self.rank
        raise ArgumentError, "too many arguments"
      end
      shp = self.shape
      ind = []
      self.rank.times do |i|
        n = shp[i]
        case a=args[i]
        when Integer
          a = n+a if a<0
          raise IndexError, "index(%d) out of range"%[a] if a<0
          x = [0...a,a+1...n]
        when Range
          b = a.first
          b = n+b if b<0
          raise IndexError, "index(%s) out of range"%[a] if b<0
          e = a.last
          e = n+e if e<0
          e -= 1 if a.exclude_end?
          raise IndexError, "index(%s) out of range"%[a] if e<0
          x = [0...b,e+1...n]
        when Array
          x = (0...n).to_a
          x -= a.map do |j|
            raise IndexError, "contains non-integer" unless Integer===j
            (j<0) ? n+j : j
          end
        else
          if a
            raise ArgumentError, "invalid argument"
          else
            x = true
          end
        end
        ind << x
      end
      self[*ind]
    end

dim() click to toggle source
Alias for: rank
dimension() click to toggle source
Alias for: rank
dup() click to toggle source
Alias for: clone
each() click to toggle source

iterator: each()

    static VALUE
     na_each(VALUE obj)
    {
      int i, sz;
      VALUE v;
      struct NARRAY *ary;
      char *p;
      void (*func)();

      if (rb_block_given_p()) {
        GetNArray(obj,ary);

        p  = ary->ptr;
        sz = na_sizeof[ary->type];
        func = SetFuncs[NA_ROBJ][ary->type];

        for ( i=ary->total; i-->0; ) {
          (*func)( 1, &v, 0, p, 0 );
          rb_yield(v);
          p += sz;
        }
        return Qnil;
      } else {
        return rb_funcall(obj, rb_intern("to_enum"), 0);
      }
    }

element_size() click to toggle source

method: element_size -- returns the element size of the array type

    static VALUE
     na_element_size(VALUE self)
    {
      struct NARRAY *ary;
      GetNArray(self,ary);
      return INT2FIX(na_sizeof[ary->type]);
    }

empty?() click to toggle source

method: empty? -- returns true if empty array

    static VALUE
     na_is_empty(VALUE self)
    {
      struct NARRAY *ary;
      GetNArray(self,ary);
      if (ary->total==0) return Qtrue;
      return Qfalse;
    }

eql?(other) click to toggle source

    # File narray_ext.rb, line 54
    def eql?(other)
      self.class == other.class &&
        typecode == other.typecode &&
        shape == other.shape &&
        case typecode
        when NArray::OBJECT
          to_a.eql? other.to_a
        else
          to_s.eql? other.to_s
        end
    end

fill(p1) click to toggle source
Alias for: fill!
fill!(p1) click to toggle source

method: fill!(val)

    VALUE na_fill(VALUE self, volatile VALUE val)
    {
      struct NARRAY *a1, *a2;

      GetNArray(self,a1);
      val = na_cast_unless_narray(val,a1->type);
      GetNArray(val,a2);

      if (a2->total != 1)
        rb_raise(rb_eArgError, "single-element argument required");

      SetFuncs[a1->type][a2->type]( a1->total,
                                    a1->ptr, na_sizeof[a1->type],
                                    a2->ptr, 0 );
      return self;
    }

Also aliased as: fill
flatten() click to toggle source

method: flatten

    static VALUE
     na_flatten_ref(VALUE self)
    {
      return na_flatten_bang( na_wrap_struct( na_ref_alloc_struct(self), self ));
    }

flatten!() click to toggle source

method: flatten!

    static VALUE
     na_flatten_bang(VALUE self)
    {
      struct NARRAY *ary;

      GetNArray(self,ary);
      if (ary->total==0 || ary->rank==0)
        rb_raise(rb_eRuntimeError, "cannot reshape empty array");
      ary->shape[0] = ary->total;
      ary->rank = 1;
      return self;
    }

hash() click to toggle source

    # File narray_ext.rb, line 66
    def hash
      case typecode
      when NArray::OBJECT
        [self.class, to_a].hash
      else
        [self.class, typecode, shape, to_s].hash
      end
    end

indgen(*args) click to toggle source
Alias for: indgen!
indgen!(*args) click to toggle source

method: indgen!([start,])

    VALUE
     na_indgen(int argc, VALUE *argv, VALUE self)
    {
      int start=0, step=1;
      struct NARRAY *ary;

      if (argc>0) {
        start = NUM2INT(argv[0]);
        if (argc==2)
          step = NUM2INT(argv[1]);
        else
          if (argc>2)
            rb_raise(rb_eArgError, "wrong # of arguments (%d for <= 2)", argc);
      }

      GetNArray(self,ary);
      IndGenFuncs[ary->type]( ary->total,
                              ary->ptr, na_sizeof[ary->type],
                              start, step );
      return self;
    }

Also aliased as: indgen
inspect() click to toggle source

method: inspect() -- returns the inspect of the array

    static VALUE
     na_inspect(VALUE self)
    {
      VALUE str;
      struct NARRAY *ary;
      int i;
      char buf[256];
      const char *classname;
      const char *ref = "%s(ref).%s(%i";
      const char *org = "%s.%s(%i";

      GetNArray(self,ary);
      classname = rb_class2name(CLASS_OF(self));

      str = rb_str_new(0,0);
      if (ary->rank < 1) {
        sprintf(buf, "%s.%s(): []", classname, na_typestring[ary->type]);
        rb_str_cat(str,buf,strlen(buf));
      }
      else {
        sprintf(buf, (ary->ref==Qnil) ? org:ref,
                classname, na_typestring[ary->type], ary->shape[0]);
        rb_str_cat(str,buf,strlen(buf));
        for (i=1; i<ary->rank; ++i) {
          sprintf(buf,",%i",ary->shape[i]);
          rb_str_cat(str,buf,strlen(buf));
        }
        rb_str_cat(str,")",1);
        rb_str_cat(str,": \n",3);
        rb_str_concat(str, na_make_inspect(self));
      }
      return str;
    }

integer?() click to toggle source

    # File narray_ext.rb, line 26
    def integer?
      self.typecode==NArray::BYTE ||
      self.typecode==NArray::SINT ||
      self.typecode==NArray::LINT
    end

length() click to toggle source
Alias for: size
map() click to toggle source
Alias for: collect
map!() click to toggle source
Alias for: collect!
mean(*ranks) click to toggle source

Statistics

    # File narray_ext.rb, line 131
    def mean(*ranks)
      if integer?
        a = self.to_type(NArray::DFLOAT)
      else
        a = self
      end
      a = NArray.ref(a)
      a.sum(*ranks) / (rank_total(*ranks))
    end

median(rank=nil) click to toggle source

    # File narray_ext.rb, line 186
    def median(rank=nil)
      shape = self.shape
      rank = shape.size-1 if rank==nil
      s = sort(rank).reshape!(true,*shape[rank+1..-1])
      n = s.shape[0]
      if n%2==1
        s[n/2,false]
      else
        s[n/2-1..n/2,false].sum(0)/2
      end
    end

newdim(*args) click to toggle source

method: newdim(size1,size2,...,sizeN)

    VALUE
     na_newdim_ref(int argc, VALUE *argv, VALUE self)
    {
      struct NARRAY *ary;

      GetNArray(self,ary);
      ary = na_ref_alloc_struct(self);
      na_newdim(argc, argv, ary);
      return na_wrap_struct(ary,self);
    }

Also aliased as: newrank
newdim!(*args) click to toggle source

method: newdim!(size1,size2,...,sizeN)

    static VALUE
     na_newdim_bang(int argc, VALUE *argv, VALUE self)
    {
      struct NARRAY *ary;

      GetNArray(self,ary);
      na_newdim(argc, argv, ary);
      return self;
    }

Also aliased as: newdim=, newrank!, newrank=
newdim=(*args) click to toggle source
Alias for: newdim!
newrank(*args) click to toggle source
Alias for: newdim
newrank!(*args) click to toggle source
Alias for: newdim!
newrank=(*args) click to toggle source
Alias for: newdim!
none?() click to toggle source

    # File narray_ext.rb, line 44
    def none?
      where.size == 0
    end

original() click to toggle source

method: self.original

    static VALUE
     na_original(VALUE self)
    {
      struct NARRAY *ary;

      GetNArray(self,ary);
      return ary->ref;
    }

randomn() click to toggle source

Normal distributed random number; valid for floating point types

    # File narray_ext.rb, line 200
    def randomn
      size = self.size
      case type = self.typecode
      when COMPLEX; type=FLOAT
      when SCOMPLEX; type=SFLOAT
      when FLOAT
      when SFLOAT
      else
        raise TypeError, "NArray type must be (S)FLOAT or (S)COMPLEX."
      end
      rr = NArray.new(type,size)
      xx = NArray.new(type,size)
      i = 0
      while i < size
        n = size-i
        m = ((n+Math::sqrt(n))*1.27).to_i
        x = NArray.new(type,m).random!(1) * 2 - 1
        y = NArray.new(type,m).random!(1) * 2 - 1
        r = x**2 + y**2
        idx = (r<1).where
        idx = idx[0...n] if idx.size > n
        if idx.size>0
          rr[i] = r[idx]
          xx[i] = x[idx]
          i += idx.size
        end
      end
      # Box-Muller transform
      rr = ( xx * NMath::sqrt( -2 * NMath::log(rr) / rr ) )
      # finish
      rr.reshape!(*self.shape) if self.rank > 1
      rr = rr.to_type(self.typecode) if type!=self.typecode
      if RUBY_VERSION < "1.8.0"
        self.type.refer(rr)
      else
        self.class.refer(rr)
      end
    end

randomn!() click to toggle source

alias randomn! randomn

    # File narray_ext.rb, line 240
    def randomn!
      self[]= randomn
      self
    end

rank() click to toggle source

method: rank() -- returns the rank of the array

    static VALUE
     na_rank(VALUE self)
    {
      struct NARRAY *ary;
      GetNArray(self,ary);
      return INT2FIX(ary->rank);
    }

Also aliased as: dim, dimension
rank_total(*ranks) click to toggle source

    # File narray_ext.rb, line 75
    def rank_total(*ranks)
      if ranks.size>0
        idx = []
        ranks.each{|i| idx.push(*i)}
        # ranks is expected to be, e.g., [1, 3..5, 7]
        a = self.shape
        n = 1
        idx.each{|i| n *= a[i]}
        n
      else
        self.total
      end
    end

refer() click to toggle source

method: self.refer

    static VALUE
     na_refer(VALUE self)
    {
      return na_wrap_struct(na_ref_alloc_struct(self), self);
    }

reshape(*args) click to toggle source

method: reshape(size1,size2,...,sizeN)

    static VALUE
     na_reshape_ref(int argc, VALUE *argv, VALUE self)
    {
      struct NARRAY *ary;

      GetNArray(self,ary);
      ary = na_ref_alloc_struct(self);
      na_reshape(argc, argv, ary, self);
      return na_wrap_struct(ary,self);
    }

reshape!(*args) click to toggle source

method: reshape!(size1,size2,...,sizeN)

    static VALUE
     na_reshape_bang(int argc, VALUE *argv, VALUE self)
    {
      struct NARRAY *ary;

      GetNArray(self,ary);
      na_reshape(argc, argv, ary, self);
      return self;
    }

Also aliased as: shape=
reverse(*ranks) click to toggle source

    # File narray_ext.rb, line 245
    def reverse(*ranks)
      if self.rank==0
        return self.dup
      elsif ranks.size==0
        idx = (0...self.rank).map{-1..0}
      else
        idx = [true]*self.rank
        ranks.each do |i|
          if !i.kind_of?(Integer)
            raise ArgumentError, "Argument must be Integer"
          end
          if i >= self.rank
            raise ArgumentError, "dimension(%s) out of range"%[i]
          end
          idx[i] = -1..0
        end
      end
      self[*idx]
    end

rms(*ranks) click to toggle source

    # File narray_ext.rb, line 156
    def rms(*ranks)
      if integer?
        a = self.to_type(NArray::DFLOAT)
      else
        a = self
      end
      a = NArray.ref(a)
      n = rank_total(*ranks)
      if complex?
        NMath::sqrt( (a.abs**2).sum(*ranks)/n )
      else
        NMath::sqrt( (a**2).sum(*ranks)/n )
      end
    end

rmsdev(*ranks) click to toggle source

    # File narray_ext.rb, line 171
    def rmsdev(*ranks)
      if integer?
        a = self.to_type(NArray::DFLOAT)
      else
        a = self
      end
      a = NArray.ref(a)
      n = rank_total(*ranks)
      if complex?
        NMath::sqrt( (( a-a.accum(*ranks).div!(n) ).abs**2).sum(*ranks)/n )
      else
        NMath::sqrt( (( a-a.accum(*ranks).div!(n) )**2).sum(*ranks)/n )
      end
    end

rot90(n_times=1) click to toggle source

    # File narray_ext.rb, line 265
    def rot90(n_times=1)
      if self.rank < 2
        raise "must be >= 2 dimensional array"
      end
      case n_times%4
      when 0
        self.dup
      when 1
        self.transpose(1,0).reverse(0)
      when 2
        self.reverse(0,1)
      when 3
        self.transpose(1,0).reverse(1)
      end
    end

shape() click to toggle source

method: shape() -- returns an array of shape of each rank

    static VALUE
     na_shape(VALUE self)
    {
      struct NARRAY *ary;
      VALUE *shape;
      int i;

      GetNArray(self,ary);
      shape = ALLOCA_N(VALUE,ary->rank);
      for (i = 0; i < ary->rank; ++i)
        shape[i] = INT2FIX(ary->shape[i]);
      return rb_ary_new4(ary->rank,shape);
    }

Also aliased as: sizes
shape=(*args) click to toggle source
Alias for: reshape!
size() click to toggle source

method: size() -- returns the total number of elements

    static VALUE
     na_size(VALUE self)
    {
      struct NARRAY *ary;
      GetNArray(self,ary);
      return INT2FIX(ary->total);
    }

Also aliased as: total, length
sizes() click to toggle source
Alias for: shape
stddev(*ranks) click to toggle source

    # File narray_ext.rb, line 141
    def stddev(*ranks)
      if integer?
        a = self.to_type(NArray::DFLOAT)
      else
        a = self
      end
      a = NArray.ref(a)
      n = rank_total(*ranks)
      if complex?
        NMath::sqrt( (( a-a.accum(*ranks).div!(n) ).abs**2).sum(*ranks)/(n-1) )
      else
        NMath::sqrt( (( a-a.accum(*ranks).div!(n) )**2).sum(*ranks)/(n-1) )
      end
    end

to_binary() click to toggle source

method: to_binary -- convert the data contents to a BYTE type NArray

    static VALUE
     na_to_binary(VALUE self)
    {
      struct NARRAY *a1, *a2;
      int i, *shape, rank;
      VALUE v;

      GetNArray(self,a1);

      rank = a1->rank+1;
      shape = ALLOCA_N(int,rank);
      shape[0] = na_sizeof[a1->type];
      for (i=1; i<rank; ++i)
        shape[i] = a1->shape[i-1];

      v = na_make_object( NA_BYTE, rank, shape, cNArray );
      GetNArray(v,a2);
      MEMCPY(a2->ptr,a1->ptr,char,a2->total);

      return v;
    }

to_f() click to toggle source

method: to_f()

    static VALUE
     na_to_float(VALUE self)
    {
      struct NARRAY *a1, *a2;
      VALUE v;

      GetNArray(self,a1);

      v = na_make_object(na_upcast[NA_SFLOAT][a1->type], a1->rank, a1->shape,
                         CLASS_OF(self));
      GetNArray(v,a2);
      na_copy_nary(a2,a1);
      return v;
    }

to_i() click to toggle source

method: to_i()

    static VALUE
     na_to_integer(VALUE self)
    {
      int type;
      struct NARRAY *a1, *a2;
      VALUE v;

      GetNArray(self,a1);
      if (!NA_IsINTEGER(a1))
        type = NA_LINT;
      else
        type = a1->type;
      v = na_make_object(type, a1->rank, a1->shape, CLASS_OF(self));
      GetNArray(v,a2);
      na_copy_nary(a2,a1);
      return v;
    }

to_s() click to toggle source

method: to_s -- convert the data contents to a binary string

    static VALUE
     na_to_s(VALUE self)
    {
      struct NARRAY *ary;
      GetNArray(self,ary);
      if (NA_IsROBJ(ary))
        rb_raise(rb_eTypeError,"cannot convert object-type NArray");
      return rb_str_new(ary->ptr,ary->total*na_sizeof[ary->type]);
    }

to_string() click to toggle source

method: to_string

    static VALUE
     na_to_string(VALUE self)
    {
      VALUE v;
      struct NARRAY *a1, *a2;

      GetNArray(self,a1);

      if (a1->total==0)
        v = na_make_empty(NA_ROBJ, CLASS_OF(self));
      else
      if (a1->type==NA_BYTE) {
        if (a1->rank==1)
          return rb_str_new(a1->ptr,a1->shape[0]);
        v  = na_make_object(NA_ROBJ, a1->rank-1, a1->shape+1, cNArray);
        GetNArray(v,a2);
        na_to_string_binary( a2->total,
                             a2->ptr, sizeof(VALUE),
                             a1->ptr, a1->shape[0] );
      } else {
        v = na_make_object(NA_ROBJ, a1->rank, a1->shape, CLASS_OF(self));
        GetNArray(v,a2);
        ToStrFuncs[a1->type]( a2->total,
                              a2->ptr, sizeof(VALUE),
                              a1->ptr, na_sizeof[a1->type] );
      }
      return v;
    }

to_type(p1) click to toggle source

method: to_type(type)

    static VALUE
     na_to_type(VALUE self, VALUE vtype)
    {
      struct NARRAY *a1, *a2;
      VALUE v;

      GetNArray(self,a1);

      v = na_make_object(na_get_typecode(vtype), a1->rank, a1->shape,
                         CLASS_OF(self));
      GetNArray(v,a2);
      na_copy_nary(a2,a1);
      return v;
    }

to_type_as_binary(p1) click to toggle source

method: to_type_as_binary(type)

    static VALUE
     na_to_type_as_binary(VALUE self, VALUE vtype)
    {
      struct NARRAY *a1, *a2;
      int size, total, type;
      VALUE v;

      type = na_get_typecode(vtype);
      GetNArray(self,a1);

      size = a1->total * na_sizeof[a1->type];
      if ( size % na_sizeof[type] != 0 )
        rb_raise(rb_eRuntimeError, "bina1 size mismatch");
      total = size / na_sizeof[type];

      v = na_make_object( type, 1, &total, cNArray );
      GetNArray(v,a2);
      MEMCPY(a2->ptr,a1->ptr,char,size);

      return v;
    }

total() click to toggle source
Alias for: size
typecode() click to toggle source

method: typecode -- returns the type of the array

    static VALUE
     na_typecode(VALUE self)
    {
      struct NARRAY *ary;
      GetNArray(self,ary);
      return INT2FIX(ary->type);
    }

where() click to toggle source

method: where idx_true = narray.where

    static VALUE
     na_where(VALUE self)
    {
      return RARRAY_PTR( na_where2(self) )[0];
    }

where2() click to toggle source

method: where2 idx_true, idx_false = narray.where2

    static VALUE
     na_where2(volatile VALUE obj)
    {
      VALUE v1, v0;
      int  n, i, n1, n0;
      char *c;
      int32_t *idx1, *idx0;
      struct NARRAY *ary, *a1, *a0; /* a1=true, a0=false */

      GetNArray(obj,ary);
      /* Convert to NA_BYTE by calling "obj.ne(0)", if needed */
      if(ary->type != NA_BYTE) {
        obj = rb_funcall(obj, na_id_ne, 1, INT2FIX(0));
        GetNArray(obj,ary);
      }
      n = ary->total;

      /* Count true */
      c = ary->ptr;
      n1 = 0;
      for (i=0; i<n; ++i)
        if (*(c++)) ++n1;

      n0 = n-n1;

      /* partially true and false */
      v1 = na_make_object( NA_LINT, 1, &n1, cNArray );
      GetNArray(v1,a1);
      idx1 = (int32_t*) a1->ptr;
      v0 = na_make_object( NA_LINT, 1, &n0, cNArray );
      GetNArray(v0,a0);
      idx0 = (int32_t*) a0->ptr;

      /* Get Indices */
      c = ary->ptr;
      for ( i=0; i<n; ++i ) {
        if (*(c++))
          *(idx1++) = i;
        else
          *(idx0++) = i;
      }

      return rb_assoc_new( v1, v0 );
    }

[Validate]

Generated with the Darkfish Rdoc Generator 2.