Class: Classifier::Linalg::Vector
- Includes:
- Enumerable
- Defined in:
- ext/classifier/vector.c
Class Method Summary collapse
-
._load(str) ⇒ Object
Vector._load for Marshal.
-
.alloc(arg) ⇒ Object
Vector.alloc(size_or_array) Create a new vector from size (zero-filled) or array of values.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Vector#* - dot product with vector, or matrix multiplication.
-
#[](idx) ⇒ Object
Vector#[].
-
#[]=(idx, val) ⇒ Object
Vector#[]=.
-
#_dump(depth) ⇒ Object
Vector#_dump for Marshal.
-
#col ⇒ Object
Vector#col - return self as column vector.
-
#collect ⇒ Object
(also: #map)
Vector#collect (map).
-
#each ⇒ Object
Vector#each.
-
#normalize ⇒ Object
Vector#normalize.
-
#row ⇒ Object
Vector#row - return self as row vector.
-
#size ⇒ Object
Vector#size.
-
#sum ⇒ Object
Vector#sum.
-
#to_a ⇒ Object
Vector#to_a.
Class Method Details
._load(str) ⇒ Object
Vector._load for Marshal
285 286 287 288 289 290 291 292 293 294 |
# File 'ext/classifier/vector.c', line 285 static VALUE rb_cvector_s_load(VALUE klass, VALUE str) { VALUE ary = rb_marshal_load(str); VALUE is_col = rb_ary_pop(ary); VALUE result = rb_cvector_s_alloc(klass, ary); CVector *v; GET_CVECTOR(result, v); v->is_col = RTEST(is_col) ? 1 : 0; return result; } |
.alloc(arg) ⇒ Object
Vector.alloc(size_or_array) Create a new vector from size (zero-filled) or array of values
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'ext/classifier/vector.c', line 101 static VALUE rb_cvector_s_alloc(VALUE klass, VALUE arg) { CVector *v; VALUE result; if (RB_TYPE_P(arg, T_ARRAY)) { long len = RARRAY_LEN(arg); v = cvector_alloc((size_t)len); for (long i = 0; i < len; i++) { v->data[i] = NUM2DBL(rb_ary_entry(arg, i)); } } else { size_t size = NUM2SIZET(arg); v = cvector_alloc(size); } result = TypedData_Wrap_Struct(klass, &cvector_type, v); return result; } |
Instance Method Details
#*(other) ⇒ Object
Vector#* - dot product with vector, or matrix multiplication
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'ext/classifier/vector.c', line 250 static VALUE rb_cvector_mul(VALUE self, VALUE other) { CVector *v; GET_CVECTOR(self, v); if (rb_obj_is_kind_of(other, cClassifierVector)) { CVector *w; GET_CVECTOR(other, w); return DBL2NUM(cvector_dot(v, w)); } else if (RB_TYPE_P(other, T_FLOAT) || RB_TYPE_P(other, T_FIXNUM)) { /* Scalar multiplication */ double scalar = NUM2DBL(other); CVector *result = cvector_alloc(v->size); result->is_col = v->is_col; for (size_t i = 0; i < v->size; i++) { result->data[i] = v->data[i] * scalar; } return TypedData_Wrap_Struct(cClassifierVector, &cvector_type, result); } rb_raise(rb_eTypeError, "Cannot multiply Vector with %s", rb_obj_classname(other)); return Qnil; } |
#[](idx) ⇒ Object
Vector#[]
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'ext/classifier/vector.c', line 130 static VALUE rb_cvector_aref(VALUE self, VALUE idx) { CVector *v; GET_CVECTOR(self, v); long i = NUM2LONG(idx); if (i < 0) i += v->size; if (i < 0 || (size_t)i >= v->size) { rb_raise(rb_eIndexError, "index %ld out of bounds", i); } return DBL2NUM(v->data[i]); } |
#[]=(idx, val) ⇒ Object
Vector#[]=
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'ext/classifier/vector.c', line 145 static VALUE rb_cvector_aset(VALUE self, VALUE idx, VALUE val) { CVector *v; GET_CVECTOR(self, v); long i = NUM2LONG(idx); if (i < 0) i += v->size; if (i < 0 || (size_t)i >= v->size) { rb_raise(rb_eIndexError, "index %ld out of bounds", i); } v->data[i] = NUM2DBL(val); return val; } |
#_dump(depth) ⇒ Object
Vector#_dump for Marshal
275 276 277 278 279 280 281 282 |
# File 'ext/classifier/vector.c', line 275 static VALUE rb_cvector_dump(VALUE self, VALUE depth) { CVector *v; GET_CVECTOR(self, v); VALUE ary = rb_cvector_to_a(self); rb_ary_push(ary, v->is_col ? Qtrue : Qfalse); return rb_marshal_dump(ary, Qnil); } |
#col ⇒ Object
Vector#col - return self as column vector
237 238 239 240 241 242 243 244 245 246 247 |
# File 'ext/classifier/vector.c', line 237 static VALUE rb_cvector_col(VALUE self) { CVector *v; GET_CVECTOR(self, v); CVector *result = cvector_alloc(v->size); memcpy(result->data, v->data, v->size * sizeof(double)); result->is_col = 1; return TypedData_Wrap_Struct(cClassifierVector, &cvector_type, result); } |
#collect ⇒ Object Also known as: map
Vector#collect (map)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'ext/classifier/vector.c', line 196 static VALUE rb_cvector_collect(VALUE self) { CVector *v; GET_CVECTOR(self, v); RETURN_ENUMERATOR(self, 0, 0); CVector *result = cvector_alloc(v->size); result->is_col = v->is_col; for (size_t i = 0; i < v->size; i++) { VALUE val = rb_yield(DBL2NUM(v->data[i])); result->data[i] = NUM2DBL(val); } return TypedData_Wrap_Struct(cClassifierVector, &cvector_type, result); } |
#each ⇒ Object
Vector#each
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'ext/classifier/vector.c', line 182 static VALUE rb_cvector_each(VALUE self) { CVector *v; GET_CVECTOR(self, v); RETURN_ENUMERATOR(self, 0, 0); for (size_t i = 0; i < v->size; i++) { rb_yield(DBL2NUM(v->data[i])); } return self; } |
#normalize ⇒ Object
Vector#normalize
215 216 217 218 219 220 221 |
# File 'ext/classifier/vector.c', line 215 static VALUE rb_cvector_normalize(VALUE self) { CVector *v; GET_CVECTOR(self, v); CVector *result = cvector_normalize(v); return TypedData_Wrap_Struct(cClassifierVector, &cvector_type, result); } |
#row ⇒ Object
Vector#row - return self as row vector
224 225 226 227 228 229 230 231 232 233 234 |
# File 'ext/classifier/vector.c', line 224 static VALUE rb_cvector_row(VALUE self) { CVector *v; GET_CVECTOR(self, v); CVector *result = cvector_alloc(v->size); memcpy(result->data, v->data, v->size * sizeof(double)); result->is_col = 0; return TypedData_Wrap_Struct(cClassifierVector, &cvector_type, result); } |
#size ⇒ Object
Vector#size
122 123 124 125 126 127 |
# File 'ext/classifier/vector.c', line 122 static VALUE rb_cvector_size(VALUE self) { CVector *v; GET_CVECTOR(self, v); return SIZET2NUM(v->size); } |
#sum ⇒ Object
Vector#sum
174 175 176 177 178 179 |
# File 'ext/classifier/vector.c', line 174 static VALUE rb_cvector_sum(VALUE self) { CVector *v; GET_CVECTOR(self, v); return DBL2NUM(cvector_sum(v)); } |
#to_a ⇒ Object
Vector#to_a
161 162 163 164 165 166 167 168 169 170 171 |
# File 'ext/classifier/vector.c', line 161 static VALUE rb_cvector_to_a(VALUE self) { CVector *v; GET_CVECTOR(self, v); VALUE ary = rb_ary_new_capa((long)v->size); for (size_t i = 0; i < v->size; i++) { rb_ary_push(ary, DBL2NUM(v->data[i])); } return ary; } |