Module: Cumo::NMath

Defined in:
ext/cumo/narray/math.c

Constant Summary collapse

DISPATCH =
hCast

Class Method Summary collapse

Class Method Details

.method_missing(name, x, ...) ⇒ NArray

Dispatches method to Math module of upcasted type, eg, Cumo::DFloat::Math.

Parameters:

  • name (Symbol)

    method name.

  • x (NArray, Numeric)

    input array.

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'ext/cumo/narray/math.c', line 84

static VALUE cumo_na_math_method_missing(int argc, VALUE *argv, VALUE mod)
{
    VALUE type, ans, typemod, hash;
    if (argc>1) {
  type = cumo_na_mathcast(argc-1,argv+1);

  hash = rb_const_get(mod, cumo_id_DISPATCH);
  typemod = rb_hash_aref( hash, type );
  if (NIL_P(typemod)) {
      rb_raise(rb_eTypeError,"%s is unknown for Cumo::NMath",
         rb_class2name(type));
  }

  ans = rb_funcall2(typemod,cumo_id_send,argc,argv);

  if (!RTEST(rb_class_inherited_p(type,cNArray)) &&
      CumoIsNArray(ans) ) {
      ans = rb_funcall(ans,cumo_id_extract,0);
  }
  return ans;
    }
    rb_raise(rb_eArgError,"argument or method missing");
    return Qnil;
}

.respond_to_missing?(name, include_private) ⇒ Boolean

Tells whether method_missing would find the method, so that respond_to? answers for sqrt and gelu the way it does for a defined method. The dtype decides which Math module a call lands in and respond_to? is not given one, so this asks the module that a call can reach with the widest set of methods. Every other module a call can reach defines a subset of those.

Parameters:

  • name (Symbol)

    method name.

  • include_private (Boolean)

    whether to look at private methods too.

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/cumo/narray/math.c', line 121

static VALUE
cumo_na_math_respond_to_missing_p(VALUE mod, VALUE name, VALUE include_private)
{
    VALUE typemod;
    ID id = rb_check_id(&name);

    if (!id) {  // a name nothing has interned cannot be a method anywhere
        return Qfalse;
    }
    typemod = rb_hash_aref(rb_const_get(mod, cumo_id_DISPATCH), cumo_cDFloat);
    if (NIL_P(typemod)) {
        return Qfalse;
    }
    return rb_obj_respond_to(typemod, id, RTEST(include_private)) ? Qtrue : Qfalse;
}