Class: Classifier::Linalg::Matrix

Inherits:
Object
  • Object
show all
Defined in:
ext/classifier/matrix.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._load(str) ⇒ Object

Matrix._load for Marshal



357
358
359
360
361
362
363
# File 'ext/classifier/matrix.c', line 357

static VALUE rb_cmatrix_s_load(VALUE klass, VALUE str)
{
    VALUE ary = rb_marshal_load(str);
    long argc = RARRAY_LEN(ary);
    VALUE *argv = RARRAY_PTR(ary);
    return rb_cmatrix_s_alloc((int)argc, argv, klass);
}

.alloc(*args) ⇒ Object

Matrix.alloc(*rows) Create a new matrix from nested arrays



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/classifier/matrix.c', line 114

static VALUE rb_cmatrix_s_alloc(int argc, VALUE *argv, VALUE klass)
{
    CMatrix *m;
    VALUE result;

    if (argc == 0) {
        rb_raise(rb_eArgError, "Matrix.alloc requires at least one row");
    }

    /* Handle single array argument containing rows */
    VALUE rows_ary;
    if (argc == 1 && RB_TYPE_P(argv[0], T_ARRAY)) {
        VALUE first = rb_ary_entry(argv[0], 0);
        if (RB_TYPE_P(first, T_ARRAY)) {
            rows_ary = argv[0];
        } else {
            /* Single row */
            rows_ary = rb_ary_new_from_values(argc, argv);
        }
    } else {
        rows_ary = rb_ary_new_from_values(argc, argv);
    }

    long num_rows = RARRAY_LEN(rows_ary);
    if (num_rows == 0) {
        rb_raise(rb_eArgError, "Matrix cannot be empty");
    }

    VALUE first_row = rb_ary_entry(rows_ary, 0);
    long num_cols = RARRAY_LEN(first_row);

    m = cmatrix_alloc((size_t)num_rows, (size_t)num_cols);

    for (long i = 0; i < num_rows; i++) {
        VALUE row = rb_ary_entry(rows_ary, i);
        if (RARRAY_LEN(row) != num_cols) {
            cmatrix_free(m);
            rb_raise(rb_eArgError, "All rows must have the same length");
        }
        for (long j = 0; j < num_cols; j++) {
            MAT_AT(m, i, j) = NUM2DBL(rb_ary_entry(row, j));
        }
    }

    result = TypedData_Wrap_Struct(klass, &cmatrix_type, m);
    return result;
}

.diag(arg) ⇒ Object

Matrix.diag(vector_or_array) Create diagonal matrix from vector



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/classifier/matrix.c', line 166

static VALUE rb_cmatrix_s_diag(VALUE klass, VALUE arg)
{
    CVector *v;
    CMatrix *m;
    int free_v = 0;

    if (rb_obj_is_kind_of(arg, cClassifierVector)) {
        GET_CVECTOR(arg, v);
    } else if (RB_TYPE_P(arg, T_ARRAY)) {
        long len = RARRAY_LEN(arg);
        v = cvector_alloc((size_t)len);
        free_v = 1;
        for (long i = 0; i < len; i++) {
            v->data[i] = NUM2DBL(rb_ary_entry(arg, i));
        }
    } else {
        rb_raise(rb_eTypeError, "Expected Vector or Array");
        return Qnil;
    }

    m = cmatrix_diagonal(v);
    if (free_v) cvector_free(v);

    return TypedData_Wrap_Struct(klass, &cmatrix_type, m);
}

.diagonal(arg) ⇒ Object

Matrix.diag(vector_or_array) Create diagonal matrix from vector



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/classifier/matrix.c', line 166

static VALUE rb_cmatrix_s_diag(VALUE klass, VALUE arg)
{
    CVector *v;
    CMatrix *m;
    int free_v = 0;

    if (rb_obj_is_kind_of(arg, cClassifierVector)) {
        GET_CVECTOR(arg, v);
    } else if (RB_TYPE_P(arg, T_ARRAY)) {
        long len = RARRAY_LEN(arg);
        v = cvector_alloc((size_t)len);
        free_v = 1;
        for (long i = 0; i < len; i++) {
            v->data[i] = NUM2DBL(rb_ary_entry(arg, i));
        }
    } else {
        rb_raise(rb_eTypeError, "Expected Vector or Array");
        return Qnil;
    }

    m = cmatrix_diagonal(v);
    if (free_v) cvector_free(v);

    return TypedData_Wrap_Struct(klass, &cmatrix_type, m);
}

Instance Method Details

#*(other) ⇒ Object

Matrix#* - multiply with matrix or vector



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'ext/classifier/matrix.c', line 318

static VALUE rb_cmatrix_mul(VALUE self, VALUE other)
{
    CMatrix *m;
    GET_CMATRIX(self, m);

    if (rb_obj_is_kind_of(other, cClassifierMatrix)) {
        CMatrix *b;
        GET_CMATRIX(other, b);
        CMatrix *result = cmatrix_multiply(m, b);
        return TypedData_Wrap_Struct(cClassifierMatrix, &cmatrix_type, result);
    } else if (rb_obj_is_kind_of(other, cClassifierVector)) {
        CVector *v;
        GET_CVECTOR(other, v);
        CVector *result = cmatrix_multiply_vector(m, v);
        return TypedData_Wrap_Struct(cClassifierVector, &cvector_type, result);
    } else if (RB_TYPE_P(other, T_FLOAT) || RB_TYPE_P(other, T_FIXNUM)) {
        /* Scalar multiplication */
        double scalar = NUM2DBL(other);
        CMatrix *result = cmatrix_alloc(m->rows, m->cols);
        for (size_t i = 0; i < m->rows * m->cols; i++) {
            result->data[i] = m->data[i] * scalar;
        }
        return TypedData_Wrap_Struct(cClassifierMatrix, &cmatrix_type, result);
    }

    rb_raise(rb_eTypeError, "Cannot multiply Matrix with %s", rb_obj_classname(other));
    return Qnil;
}

#[](row, col) ⇒ Object

Matrix#[](i, j)



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/classifier/matrix.c', line 217

static VALUE rb_cmatrix_aref(VALUE self, VALUE row, VALUE col)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    long i = NUM2LONG(row);
    long j = NUM2LONG(col);

    if (i < 0) i += m->rows;
    if (j < 0) j += m->cols;
    if (i < 0 || (size_t)i >= m->rows || j < 0 || (size_t)j >= m->cols) {
        rb_raise(rb_eIndexError, "index out of bounds");
    }

    return DBL2NUM(MAT_AT(m, i, j));
}

#[]=(row, col, val) ⇒ Object

Matrix#[]=(i, j, val)



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'ext/classifier/matrix.c', line 234

static VALUE rb_cmatrix_aset(VALUE self, VALUE row, VALUE col, VALUE val)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    long i = NUM2LONG(row);
    long j = NUM2LONG(col);

    if (i < 0) i += m->rows;
    if (j < 0) j += m->cols;
    if (i < 0 || (size_t)i >= m->rows || j < 0 || (size_t)j >= m->cols) {
        rb_raise(rb_eIndexError, "index out of bounds");
    }

    MAT_AT(m, i, j) = NUM2DBL(val);
    return val;
}

#_dump(depth) ⇒ Object

Matrix#_dump for Marshal



348
349
350
351
352
353
354
# File 'ext/classifier/matrix.c', line 348

static VALUE rb_cmatrix_dump(VALUE self, VALUE depth)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    VALUE ary = rb_cmatrix_to_a(self);
    return rb_marshal_dump(ary, Qnil);
}

#column(col_idx) ⇒ Object

Matrix#column(n) -> Vector



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'ext/classifier/matrix.c', line 261

static VALUE rb_cmatrix_column(VALUE self, VALUE col_idx)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    long j = NUM2LONG(col_idx);

    if (j < 0) j += m->cols;
    if (j < 0 || (size_t)j >= m->cols) {
        rb_raise(rb_eIndexError, "column index out of bounds");
    }

    CVector *v = cvector_alloc(m->rows);
    v->is_col = 1;
    for (size_t i = 0; i < m->rows; i++) {
        v->data[i] = MAT_AT(m, i, j);
    }

    return TypedData_Wrap_Struct(cClassifierVector, &cvector_type, v);
}

#column_sizeObject

Matrix#column_size



209
210
211
212
213
214
# File 'ext/classifier/matrix.c', line 209

static VALUE rb_cmatrix_column_size(VALUE self)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    return SIZET2NUM(m->cols);
}

#row(row_idx) ⇒ Object

Matrix#row(n) -> Vector



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'ext/classifier/matrix.c', line 282

static VALUE rb_cmatrix_row(VALUE self, VALUE row_idx)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    long i = NUM2LONG(row_idx);

    if (i < 0) i += m->rows;
    if (i < 0 || (size_t)i >= m->rows) {
        rb_raise(rb_eIndexError, "row index out of bounds");
    }

    CVector *v = cvector_alloc(m->cols);
    v->is_col = 0;
    memcpy(v->data, &MAT_AT(m, i, 0), m->cols * sizeof(double));

    return TypedData_Wrap_Struct(cClassifierVector, &cvector_type, v);
}

#row_sizeObject

Matrix#row_size



201
202
203
204
205
206
# File 'ext/classifier/matrix.c', line 201

static VALUE rb_cmatrix_row_size(VALUE self)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    return SIZET2NUM(m->rows);
}

#sizeObject

Matrix#size -> [rows, cols]



193
194
195
196
197
198
# File 'ext/classifier/matrix.c', line 193

static VALUE rb_cmatrix_size(VALUE self)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    return rb_ary_new_from_args(2, SIZET2NUM(m->rows), SIZET2NUM(m->cols));
}

#to_aObject

Matrix#to_a



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'ext/classifier/matrix.c', line 301

static VALUE rb_cmatrix_to_a(VALUE self)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    VALUE ary = rb_ary_new_capa((long)m->rows);

    for (size_t i = 0; i < m->rows; i++) {
        VALUE row = rb_ary_new_capa((long)m->cols);
        for (size_t j = 0; j < m->cols; j++) {
            rb_ary_push(row, DBL2NUM(MAT_AT(m, i, j)));
        }
        rb_ary_push(ary, row);
    }
    return ary;
}

#transObject Also known as: transpose

Matrix#trans (transpose)



252
253
254
255
256
257
258
# File 'ext/classifier/matrix.c', line 252

static VALUE rb_cmatrix_trans(VALUE self)
{
    CMatrix *m;
    GET_CMATRIX(self, m);
    CMatrix *result = cmatrix_transpose(m);
    return TypedData_Wrap_Struct(cClassifierMatrix, &cmatrix_type, result);
}