Class: FastMatrix::Matrix

Inherits:
Data
  • Object
show all
Defined in:
lib/matrix/matrix.rb,
lib/scalar.rb,
lib/matrix/constructors.rb,
lib/lup_decomposition/lup_decomposition.rb,
ext/fast_matrix/Matrix/matrix.c

Overview

Constructors as in the standard matrix

Defined Under Namespace

Classes: LUPDecomposition

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows_count, columns_count) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'ext/fast_matrix/Matrix/matrix.c', line 48

VALUE matrix_initialize(VALUE self, VALUE rows_count, VALUE columns_count)
{
    int m = raise_rb_value_to_int(columns_count);
    int n = raise_rb_value_to_int(rows_count);
    
    if(m <= 0 || n <= 0)
        rb_raise(fm_eIndexError, "Size cannot be negative or zero");

	struct matrix* data = get_matrix_from_rb_value(self);
    c_matrix_init(data, m, n);
	return self;
}

Class Method Details

.[](*rows) ⇒ Object

Creates a matrix where each argument is a row.

Matrix[ [25, 93], [-1, 66] ]
   =>  25 93
       -1 66


57
58
59
# File 'lib/matrix/constructors.rb', line 57

def self.[](*rows)
  self.rows(rows)
end

.build(row_count, column_count = row_count, &block) ⇒ Object

Creates a matrix of size row_count x column_count. It fills the values by calling the given block, passing the current row and column. Returns random matrix if no block is given.

m = Matrix.build(2, 4) {|row, col| col - row }
  => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
m = Matrix.build(3) { rand }
  => a 3x3 matrix with random elements

Raises:

  • (NotImplementedError)


20
21
22
23
24
25
# File 'lib/matrix/constructors.rb', line 20

def self.build(row_count, column_count = row_count, &block)
  matrix = create_with_check(row_count, column_count)
  raise NotImplementedError, 'Issue#17' unless block_given?

  matrix.each_with_index! { |_, i, j| block.call(i, j) }
end

.column_vector(column) ⇒ Object

Creates a single-column matrix where the values of that column are as given in column.

Matrix.column_vector([4,5,6])
  => 4
     5
     6


69
70
71
72
73
# File 'lib/matrix/constructors.rb', line 69

def self.column_vector(column)
  matrix = create_with_check(column.size, 1)
  column.each_with_index { |elem, i| matrix[i, 0] = elem }
  matrix
end

.columns(columns) ⇒ Object

Creates a matrix using columns as an array of column vectors.

Matrix.columns([[25, 93], [-1, 66]])
   =>  25 -1
       93 66


47
48
49
# File 'lib/matrix/constructors.rb', line 47

def self.columns(columns)
  lines(columns, false)
end

.combine(*matrices) ⇒ Object

Create a matrix by combining matrices entrywise, using the given block

x = Matrix[[6, 6], [4, 4]]
y = Matrix[[1, 2], [3, 4]]
Matrix.combine(x, y) {|a, b| a - b} # => Matrix[[5, 4], [1, 0]]

TODO: optimize in C



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/matrix/constructors.rb', line 146

def self.combine(*matrices)
  return empty if matrices.empty?

  result = convert(matrices.first)
  matrices[1..matrices.size].each do |m|
    raise IndexError unless result.row_count == m.row_count &&
                            result.column_count == m.column_count

    result.each_with_index! { |elem, i, j| yield elem, m[i, j] }
  end
  result
end

.convert(matrix) ⇒ Object

Create fast matrix from standard matrix



137
138
139
140
141
142
143
144
145
# File 'lib/matrix/matrix.rb', line 137

def self.convert(matrix)
  fast_matrix = Matrix.new(matrix.row_count, matrix.column_count)
  (0...matrix.row_count).each do |i|
    (0...matrix.column_count).each do |j|
      fast_matrix[i, j] = matrix[i, j]
    end
  end
  fast_matrix
end

.diagonal(*values) ⇒ Object

Creates a matrix where the diagonal elements are composed of values.

Matrix.diagonal(9, 5, -3)
  =>  9  0  0
      0  5  0
      0  0 -3


94
95
96
# File 'lib/matrix/constructors.rb', line 94

def self.diagonal(*values)
  build(values.size, values.size) { |i, j| i == j ? values[i] : 0 }
end

.empty(_ = 0, _ = 0) ⇒ Object

Empty matrices does not supported

Raises:



135
136
137
# File 'lib/matrix/constructors.rb', line 135

def self.empty(_ = 0, _ = 0)
  raise NotSupportedError, 'Empty matrices does not supported'
end

.fill(value, row_count, column_count = row_count) ⇒ Object

Creates a filled matrix Matrix.fill(42, 2, 4)

=>  42 42 42 42
    42 42 42 42


118
119
120
# File 'lib/matrix/constructors.rb', line 118

def self.fill(value, row_count, column_count = row_count)
  create_with_check(row_count, column_count).fill!(value)
end

.hstack(*args) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'ext/fast_matrix/Matrix/matrix.c', line 418

VALUE matrix_hstack(int argc, VALUE *argv, VALUE obj)
{
    raise_check_no_arguments(argc);
    
    struct matrix** mtrs;
    convert_matrix_array(argc, argv, &mtrs);

    if(!c_matrix_equal_by_n(argc, mtrs))
    {
        free(mtrs);
        rb_raise(fm_eIndexError, "Columns of different size");
    }

    int m = c_matrix_sum_by_m(argc, mtrs);
    int n = mtrs[0]->n;

    MAKE_MATRIX_AND_RB_VALUE(C, result, m, n);
    c_matrix_hstack(argc, mtrs, C->data, m);

    free(mtrs);
    return result;
}

.identity(n) ⇒ Object Also known as: unit, I

Creates an n by n identity matrix.

Matrix.identity(2)
  => 1 0
     0 1


104
105
106
# File 'lib/matrix/constructors.rb', line 104

def self.identity(n)
  scalar(n, 1)
end

.row_vector(row) ⇒ Object

Creates a single-row matrix where the values of that row are as given in row.

Matrix.row_vector([4,5,6])
  => 4 5 6


81
82
83
84
85
# File 'lib/matrix/constructors.rb', line 81

def self.row_vector(row)
  matrix = create_with_check(1, row.size)
  row.each_with_index { |elem, j| matrix[0, j] = elem }
  matrix
end

.rows(rows, copy = true) ⇒ Object

Creates a matrix where rows is an array of arrays, each of which is a row of the matrix. The optional argument copy exists only for compatibility with standard. The optional argument copy cannot be false, unlike standard.

Matrix.rows([[25, 93], [-1, 66]])
   =>  25 93
       -1 66


36
37
38
39
# File 'lib/matrix/constructors.rb', line 36

def self.rows(rows, copy = true)
  check_flag_copy(copy)
  lines(rows, true)
end

.scalar(size, value) ⇒ Object



441
442
443
444
445
446
447
448
449
# File 'ext/fast_matrix/Matrix/matrix.c', line 441

VALUE matrix_scalar(VALUE obj, VALUE size, VALUE value)
{
    int n = raise_rb_value_to_int(size);
    double v = raise_rb_value_to_double(value);

    MAKE_MATRIX_AND_RB_VALUE(C, result, n, n);
    c_matrix_scalar(n, C->data, v);
    return result;
}

.vstack(*args) ⇒ Object



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'ext/fast_matrix/Matrix/matrix.c', line 396

VALUE matrix_vstack(int argc, VALUE *argv, VALUE obj)
{
    raise_check_no_arguments(argc);
    
    struct matrix** mtrs;
    convert_matrix_array(argc, argv, &mtrs);

    if(!c_matrix_equal_by_m(argc, mtrs))
    {
        free(mtrs);
        rb_raise(fm_eIndexError, "Rows of different size");
    }

    int m = mtrs[0]->m;
    int n = c_matrix_sum_by_n(argc, mtrs);

    MAKE_MATRIX_AND_RB_VALUE(C, result, m, n);
    c_matrix_vstack(argc, mtrs, C->data);
    free(mtrs);
    return result;
}

.zero(row_count, column_count = row_count) ⇒ Object

Creates a zero matrix row_count by column_count.

Matrix.zero(2, 3)
  => 0 0 0
     0 0 0


128
129
130
# File 'lib/matrix/constructors.rb', line 128

def self.zero(row_count, column_count = row_count)
  fill(0, row_count, column_count)
end

Instance Method Details

#*(v) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'ext/fast_matrix/Matrix/matrix.c', line 159

VALUE matrix_multiply(VALUE self, VALUE v)
{
    if(RB_FLOAT_TYPE_P(v) || FIXNUM_P(v)
        || RB_TYPE_P(v, T_BIGNUM))
        return matrix_multiply_mn(self, v);
    if(RBASIC_CLASS(v) == cMatrix)
        return matrix_strassen(self, v);
    if(RBASIC_CLASS(v) == cVector)
        return matrix_multiply_mv(self, v);
    rb_raise(fm_eTypeError, "Invalid klass for multiply");
}

#**(value) ⇒ Object



686
687
688
689
690
691
692
693
694
695
# File 'ext/fast_matrix/Matrix/matrix.c', line 686

VALUE matrix_exponentiation(VALUE self, VALUE value)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    int d = raise_rb_value_to_int(value);
    
    MAKE_MATRIX_AND_RB_VALUE(C, result, A->m, A->n);
    if(!c_matrix_exponentiation(A->m, A->n, A->data, C->data, d))
        rb_raise(fm_eIndexError, "Invalid exponentiation");
    return result;
}

#+(other) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/fast_matrix/Matrix/matrix.c', line 241

VALUE matrix_add_with(VALUE self, VALUE other)
{
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* A = get_matrix_from_rb_value(self);
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    MAKE_MATRIX_AND_RB_VALUE(C, result, A->m, A->n);
    add_d_arrays_to_result(A->n * A->m, A->data, B->data, C->data);

    return result;
}

#+@Object



478
479
480
481
# File 'ext/fast_matrix/Matrix/matrix.c', line 478

VALUE matrix_plus(VALUE self)
{
    return self;
}

#-(other) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'ext/fast_matrix/Matrix/matrix.c', line 268

VALUE matrix_sub_with(VALUE self, VALUE other)
{
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* A = get_matrix_from_rb_value(self);
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    MAKE_MATRIX_AND_RB_VALUE(C, result, A->m, A->n);
    sub_d_arrays_to_result(A->n * A->m, A->data, B->data, C->data);
    return result;
}

#-@Object



470
471
472
473
474
475
476
# File 'ext/fast_matrix/Matrix/matrix.c', line 470

VALUE matrix_minus(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    MAKE_MATRIX_AND_RB_VALUE(C, result, A->m, A->n);
    multiply_d_array_to_result(A->n * A->m, A->data, -1, C->data);
    return result;
}

#/(v) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'ext/fast_matrix/Matrix/matrix.c', line 203

VALUE matrix_division(VALUE self, VALUE v)
{
    if(RB_FLOAT_TYPE_P(v) || FIXNUM_P(v)
        || RB_TYPE_P(v, T_BIGNUM))
        return matrix_division_mn(self, v);
    if(RBASIC_CLASS(v) == cMatrix)
        return matrix_division_mm(self, v);
    rb_raise(fm_eTypeError, "Invalid klass for division");
}

#<(other) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
# File 'ext/fast_matrix/Matrix/matrix.c', line 372

VALUE matrix_less(VALUE self, VALUE other)
{
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* A = get_matrix_from_rb_value(self);
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    if(less_d_array(A->n * A->m, A->data, B->data))
        return Qtrue;
    return Qfalse;
}

#<=(other) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
# File 'ext/fast_matrix/Matrix/matrix.c', line 346

VALUE matrix_less_or_equal(VALUE self, VALUE other)
{
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* A = get_matrix_from_rb_value(self);
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    if(less_or_equal_d_array(A->n * A->m, A->data, B->data))
        return Qtrue;
    return Qfalse;
}

#==(other) ⇒ Object

FIXME: for compare with standard matrix



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/matrix/matrix.rb', line 243

def ==(other)
  return eql?(other) if other.class == Matrix
  return false unless %i[row_size column_size \[\]].all? { |x| other.respond_to? x }
  return false unless self.row_size == other.row_size && self.column_size == other.column_size

  result = true
  each_with_index do |elem, i, j|
    result &&= elem == other[i, j].to_f
  end
  result
end

#>(other) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
# File 'ext/fast_matrix/Matrix/matrix.c', line 359

VALUE matrix_greater(VALUE self, VALUE other)
{
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* A = get_matrix_from_rb_value(self);
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    if(greater_d_array(A->n * A->m, A->data, B->data))
        return Qtrue;
    return Qfalse;
}

#>=(other) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
# File 'ext/fast_matrix/Matrix/matrix.c', line 333

VALUE matrix_greater_or_equal(VALUE self, VALUE other)
{
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* A = get_matrix_from_rb_value(self);
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    if(greater_or_equal_d_array(A->n * A->m, A->data, B->data))
        return Qtrue;
    return Qfalse;
}

#[](row, column) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/fast_matrix/Matrix/matrix.c', line 82

VALUE matrix_get(VALUE self, VALUE row, VALUE column)
{
    int m = raise_rb_value_to_int(column);
    int n = raise_rb_value_to_int(row);
	struct matrix* data = get_matrix_from_rb_value(self);
    
    m = (m < 0) ? data->m + m : m;
    n = (n < 0) ? data->n + n : n;
    
    if(m < 0 || n < 0 || n >= data->n || m >= data->m)
        return Qnil;

    return DBL2NUM(data->data[m + data->m * n]);
}

#[]=(row, column, v) ⇒ Object

[]=



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'ext/fast_matrix/Matrix/matrix.c', line 62

VALUE matrix_set(VALUE self, VALUE row, VALUE column, VALUE v)
{
	struct matrix* data = get_matrix_from_rb_value(self);
    raise_check_frozen_matrix(data);

    int m = raise_rb_value_to_int(column);
    int n = raise_rb_value_to_int(row);
    double x = raise_rb_value_to_double(v);

    m = (m < 0) ? data->m + m : m;
    n = (n < 0) ? data->n + n : n;

    raise_check_range(m, 0, data->m);
    raise_check_range(n, 0, data->n);

    data->data[m + data->m * n] = x;
    return v;
}

#absObject



325
326
327
328
329
330
331
# File 'ext/fast_matrix/Matrix/matrix.c', line 325

VALUE matrix_abs(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    MAKE_MATRIX_AND_RB_VALUE(R, result, A->m, A->n);
    abs_d_array(A->n * A->m, A->data, R->data);
    return result;
}

#add!(other) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
# File 'ext/fast_matrix/Matrix/matrix.c', line 255

VALUE matrix_add_from(VALUE self, VALUE other)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_frozen_matrix(A);
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    add_d_arrays_to_first(A->n * B->m, A->data, B->data);
    return self;
}

#adjugateObject



676
677
678
679
680
681
682
683
684
# File 'ext/fast_matrix/Matrix/matrix.c', line 676

VALUE matrix_adjugate(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);
    MAKE_MATRIX_AND_RB_VALUE(R, result, A->n, A->n);
    if(!c_matrix_adjugate(R->n, A->data, R->data))
        rb_raise(fm_eIndexError, "The discriminant is zero");
    return result;
}

#antisymmetric?Boolean Also known as: skew_symmetric?

Returns:

  • (Boolean)


451
452
453
454
455
456
457
458
459
# File 'ext/fast_matrix/Matrix/matrix.c', line 451

VALUE matrix_antisymmetric(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);

    if(c_matrix_antisymmetric(A->n, A->data))
        return Qtrue;
    return Qfalse;
}

#cloneObject



213
214
215
216
217
218
219
# File 'ext/fast_matrix/Matrix/matrix.c', line 213

VALUE matrix_copy(VALUE self)
{
	struct matrix* M = get_matrix_from_rb_value(self);
    MAKE_MATRIX_AND_RB_VALUE(R, result, M->m, M->n);
    copy_d_array(M->m * M->n, M->data, R->data);
    return result;
}

#coerce(other) ⇒ Object

The coerce method provides support for Ruby type coercion. This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator. See also Numeric#coerce.



14
15
16
17
18
19
20
21
# File 'lib/scalar.rb', line 14

def coerce(other)
  case other
  when Numeric
    [Scalar.new(other), self]
  else
    raise TypeError, "#{self.class} can't be coerced into #{other.class}"
  end
end

#cofactor(row, column) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'ext/fast_matrix/Matrix/matrix.c', line 563

VALUE matrix_cofactor(VALUE self, VALUE row, VALUE column)
{
    int i = raise_rb_value_to_int(column);
    int j = raise_rb_value_to_int(row);
	struct matrix* A = get_matrix_from_rb_value(self);

    int m = A->m;
    int n = A->n;
    if(i < 0 || i >= m || j < 0 || j >= n)
        rb_raise(fm_eIndexError, "Index out of range");
    raise_check_square_matrix(A);

    double* D = malloc(sizeof(double) * (n - 1) * (n - 1));
    c_matrix_minor(n, n, A->data, D, i, j);

    int coefficient = ((i + j) % 2 == 1) ? -1 : 1;
    double det = c_matrix_determinant(n - 1, D);

    free(D);
    return DBL2NUM(coefficient * det);
}

#collectObject



110
111
112
113
114
115
116
# File 'lib/matrix/matrix.rb', line 110

def collect
  collected_rows = []
  rows.each do |i|
    collected_rows.push(yield i)
  end
  collected_rows
end

#column(v) ⇒ Object



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'ext/fast_matrix/Matrix/matrix.c', line 500

VALUE matrix_column_vector(VALUE self, VALUE v)
{
    int idx = raise_rb_value_to_int(v);
	struct matrix* A = get_matrix_from_rb_value(self);

    int m = A->m;
    int n = A->n;
    idx = (idx < 0) ? n + idx : idx;
    
    if(idx < 0 || idx >= n)
        return Qnil;

    MAKE_VECTOR_AND_RB_VALUE(C, result, n);
    c_matrix_column_vector(m, n, A->data, C->data, idx);
    return result;
}

#column_countObject Also known as: column_size



221
222
223
224
225
# File 'ext/fast_matrix/Matrix/matrix.c', line 221

VALUE matrix_row_size(VALUE self)
{
	struct matrix* data = get_matrix_from_rb_value(self);
    return INT2NUM(data->m);
}

#componentObject



25
# File 'lib/matrix/matrix.rb', line 25

alias component []

#convertObject

Convert to standard ruby matrix.



238
239
240
# File 'lib/matrix/matrix.rb', line 238

def convert
  ::Matrix.build(row_size, column_size) { |i, j| self[i, j] }
end

#determinantObject



294
295
296
297
298
299
# File 'ext/fast_matrix/Matrix/matrix.c', line 294

VALUE matrix_determinant(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);
    return DBL2NUM(c_matrix_determinant(A->n, A->data));
}

#diagonal?Boolean

Returns:

  • (Boolean)


517
518
519
520
521
522
523
524
525
# File 'ext/fast_matrix/Matrix/matrix.c', line 517

VALUE matrix_diagonal(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);
    
    if(c_matrix_diagonal(A->n, A->data))
        return Qtrue;
    return Qfalse;
}

#each(which = :all) ⇒ Object

Yields all elements of the matrix, starting with those of the first row

Matrix[ [1,2], [3,4] ].each { |e| puts e }
  # => prints the numbers 1 to 4


152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/matrix/matrix.rb', line 152

def each(which = :all) # :yield: e
  return to_enum :each, which unless block_given?
  case which
  when :all
    (0...row_count).each do |i|
      (0...column_count).each do |j|
        yield self[i, j]
      end
    end
  when :diagonal
    (0...[row_count, column_count].min).each do |i|
          yield self[i, i]
    end
  when :off_diagonal
    (0...row_count).each do |i|
      (0...column_count).each do |j|
        if i != j
          yield self[i, j]
        end
      end
    end
  when :lower
    (0...row_count).each do |i|
      (0..[i,column_count-1].min).each do |j|
        yield self[i, j]
      end
    end
  when :strict_lower
    (1...row_count).each do |i|
      (0...[i,column_count].min).each do |j|
          yield self[i, j]
      end
    end
  when :strict_upper
    (0...row_count).each do |i|
      (i+1...column_count).each do |j|
        yield self[i, j]
      end
    end
  when :upper
    (0...row_count).each do |i|
      (i...column_count).each do |j|
        yield self[i, j]
      end
    end
  else
    raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper"
  end
  self
end

#each_with_indexObject

Same as #each, but the row index and column index in addition to the element

Matrix[ [1,2], [3,4] ].each_with_index do |e, row, col|
  puts "#{e} at #{row}, #{col}"
end
  # => Prints:
  #    1 at 0, 0
  #    2 at 0, 1
  #    3 at 1, 0
  #    4 at 1, 1

Raises:



214
215
216
217
218
219
220
221
222
223
# File 'lib/matrix/matrix.rb', line 214

def each_with_index
  raise NotSupportedError unless block_given?

  (0...row_count).each do |i|
    (0...column_count).each do |j|
      yield self[i, j], i, j
    end
  end
  self
end

#each_with_index!Object

don’t use (Issue#1)



226
227
228
229
230
231
232
233
# File 'lib/matrix/matrix.rb', line 226

def each_with_index!
  (0...row_count).each do |i|
    (0...column_count).each do |j|
      self[i, j] = yield self[i, j], i, j
    end
  end
  self
end

#elementObject

Returns element (i,j) of the matrix. That is: row i, column j.



24
# File 'lib/matrix/matrix.rb', line 24

alias element []

#empty?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/matrix/matrix.rb', line 44

def empty?
  row_count * column_count == 0
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/fast_matrix/Matrix/matrix.c', line 310

VALUE matrix_equal(VALUE self, VALUE other)
{
    if(RBASIC_CLASS(other) != cMatrix)
        return Qfalse;
	struct matrix* A = get_matrix_from_rb_value(self);
	struct matrix* B = get_matrix_from_rb_value(other);

    if(A->n != B->n || A->m != B->m)
        return Qfalse;
        
    if(equal_d_arrays(A->n * A->m, A->data, B->data))
		return Qtrue;
	return Qfalse;
}

#fill!(value) ⇒ Object



301
302
303
304
305
306
307
308
# File 'ext/fast_matrix/Matrix/matrix.c', line 301

VALUE matrix_fill(VALUE self, VALUE value)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_frozen_matrix(A);
    double d = raise_rb_value_to_double(value);
    fill_d_array(A->m * A->n, A->data, d);
    return self;
}

#first_minor(row, column) ⇒ Object



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'ext/fast_matrix/Matrix/matrix.c', line 547

VALUE matrix_first_minor(VALUE self, VALUE row, VALUE column)
{
    int i = raise_rb_value_to_int(column);
    int j = raise_rb_value_to_int(row);
	struct matrix* A = get_matrix_from_rb_value(self);

    int m = A->m;
    int n = A->n;
    if(i < 0 || i >= m || j < 0 || j >= n)
        rb_raise(fm_eIndexError, "Index out of range");

    MAKE_MATRIX_AND_RB_VALUE(C, result, m - 1, n - 1);
    c_matrix_minor(m, n, A->data, C->data, i, j);
    return result;
}

#freezeObject



744
745
746
747
748
749
# File 'ext/fast_matrix/Matrix/matrix.c', line 744

VALUE matrix_freeze(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    A->frozen = true;
    return self;
}

#hadamard_product(other) ⇒ Object Also known as: entrywise_product



527
528
529
530
531
532
533
534
535
536
537
538
# File 'ext/fast_matrix/Matrix/matrix.c', line 527

VALUE matrix_hadamard_product(VALUE self, VALUE other)
{
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* A = get_matrix_from_rb_value(self);
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    MAKE_MATRIX_AND_RB_VALUE(C, result, A->m, A->n);
    multiply_elems_d_array_to_result(A->n * A->m, A->data, B->data, C->data);
    return result;
}

#imaginaryObject Also known as: imag

Returns the imaginary part of the matrix. Always returns a zero matrix



73
74
75
# File 'lib/matrix/matrix.rb', line 73

def imaginary()
  Matrix.zero(row_count, column_count)
end

#inverseObject Also known as: inv



666
667
668
669
670
671
672
673
674
# File 'ext/fast_matrix/Matrix/matrix.c', line 666

VALUE matrix_inverse(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);
    MAKE_MATRIX_AND_RB_VALUE(R, result, A->n, A->n);
    if(!c_matrix_inverse(R->n, A->data, R->data))
        rb_raise(fm_eIndexError, "The discriminant is zero");
    return result;
}

#laplace_expansion(row: nil, column: nil) ⇒ Object Also known as: cofactor_expansion

Laplace expansion is equal to the determinant in the real numbers



82
83
84
# File 'lib/matrix/matrix.rb', line 82

def laplace_expansion(row: nil, column: nil) 
  determinant
end

#lower_triangular?Boolean

Returns:

  • (Boolean)


615
616
617
618
619
620
621
622
623
# File 'ext/fast_matrix/Matrix/matrix.c', line 615

VALUE matrix_lower_triangular(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);

    if(c_matrix_lower_triangular(A->n, A->data))
        return Qtrue;
    return Qfalse;
}

#lupObject Also known as: lup_decomposition



751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# File 'ext/fast_matrix/Matrix/matrix.c', line 751

VALUE matrix_lup(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);

    int n = A->n;

    struct lupdecomposition lp;
    lp.n = n;
    lp.data = malloc(n * n * sizeof(double));
    lp.permutation = malloc(n * sizeof(double));
    c_matrix_lup(n, A->data, lp.data, lp.permutation, &(lp.pivot_sign), &(lp.singular));
    
    struct lupdecomposition* p_lp;
    VALUE result = TypedData_Make_Struct(cLUPDecomposition, struct lupdecomposition, &lup_type, p_lp); 
    *p_lp = lp;

    return result;
}

#normal?Boolean

Returns:

  • (Boolean)


697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'ext/fast_matrix/Matrix/matrix.c', line 697

VALUE matrix_normal(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    if(A->m != A-> n)
        return Qfalse;
    
    int n = A->n;
    double* B = malloc(n * n * sizeof(double));
    double* C = malloc(n * n * sizeof(double));
    double* D = malloc(n * n * sizeof(double));
    
    c_matrix_transpose(n, n, A->data, B);
    c_matrix_strassen(n, n, n, A->data, B, C);
    c_matrix_strassen(n, n, n, B, A->data, D);
    
    VALUE res = Qfalse;
    if(equal_d_arrays(n * n, C, D))
        res = Qtrue;
    
    free(B);
    free(C);
    free(D);
    return res;
}

#orthogonal?Boolean

Returns:

  • (Boolean)


645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'ext/fast_matrix/Matrix/matrix.c', line 645

VALUE matrix_orthogonal(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);
    
    int n = A->n;
    double* B = malloc(sizeof(double) * n * n);
    double* C = malloc(sizeof(double) * n * n);

    c_matrix_transpose(n, n, A->data, B);
    fill_d_array(n * n, C, 0);
    c_matrix_strassen(n, n, n, A->data, B, C);
    bool result = c_matrix_identity(n, C);

    free(B);
    free(C);
    if(result)
        return Qtrue;
    return Qfalse;
}

#permutation?Boolean

Returns:

  • (Boolean)


635
636
637
638
639
640
641
642
643
# File 'ext/fast_matrix/Matrix/matrix.c', line 635

VALUE matrix_permutation(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);

    if(c_matrix_permutation(A->n, A->data))
        return Qtrue;
    return Qfalse;
}

#rankObject



593
594
595
596
597
# File 'ext/fast_matrix/Matrix/matrix.c', line 593

VALUE matrix_rank(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    return INT2NUM(c_matrix_rank(A->m, A->n, A->data));
}

#real?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/matrix/matrix.rb', line 106

def real?
  true
end

#rectObject Also known as: rectangular

Returns an array containing matrices corresponding to the real and imaginary parts of the matrix



90
91
92
# File 'lib/matrix/matrix.rb', line 90

def rect
  [real, imag]
end

#regular?Boolean

Returns true if this is a regular (i.e. non-singular) matrix.

Returns:

  • (Boolean)


58
59
60
# File 'lib/matrix/matrix.rb', line 58

def regular?
  not singular?
end

#round(*args) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'ext/fast_matrix/Matrix/matrix.c', line 599

VALUE matrix_round(int argc, VALUE *argv, VALUE self)
{
    if(argc > 1)
        rb_raise(fm_eTypeError, "Wrong number of arguments");
    int d;
    if(argc == 1)
        d = raise_rb_value_to_int(argv[0]);
    else
        d = 0;

    struct matrix* A = get_matrix_from_rb_value(self);
    MAKE_MATRIX_AND_RB_VALUE(R, result, A->m, A->n);
    round_d_array(A->m * A->n, A->data, R->data, d);
    return result;
}

#row(v) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'ext/fast_matrix/Matrix/matrix.c', line 483

VALUE matrix_row_vector(VALUE self, VALUE v)
{
    int idx = raise_rb_value_to_int(v);
	struct matrix* A = get_matrix_from_rb_value(self);

    int m = A->m;
    int n = A->n;
    idx = (idx < 0) ? m + idx : idx;
    
    if(idx < 0 || idx >= m)
        return Qnil;
    
    MAKE_VECTOR_AND_RB_VALUE(C, result, n);
    copy_d_array(m, A->data + idx * m, C->data);
    return result;
}

#row_countObject Also known as: row_size



227
228
229
230
231
# File 'ext/fast_matrix/Matrix/matrix.c', line 227

VALUE matrix_column_size(VALUE self)
{
	struct matrix* data = get_matrix_from_rb_value(self);
    return INT2NUM(data->n);
}

#singular?Boolean

Returns true if this is a singular matrix.

Returns:

  • (Boolean)


51
52
53
# File 'lib/matrix/matrix.rb', line 51

def singular?
  determinant == 0
end

#square?Boolean

Returns true if this is a square matrix.

Returns:

  • (Boolean)


65
66
67
# File 'lib/matrix/matrix.rb', line 65

def square?
  column_count == row_count
end

#sub!(other) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
# File 'ext/fast_matrix/Matrix/matrix.c', line 281

VALUE matrix_sub_from(VALUE self, VALUE other)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_frozen_matrix(A);
    raise_check_rbasic(other, cMatrix, "matrix");
	struct matrix* B = get_matrix_from_rb_value(other);

    raise_check_equal_size_matrix(A, B);

    sub_d_arrays_to_first(A->n * B->m, A->data, B->data);
    return self;
}

#symmetric?Boolean Also known as: hermitian?

Returns:

  • (Boolean)


461
462
463
464
465
466
467
468
# File 'ext/fast_matrix/Matrix/matrix.c', line 461

VALUE matrix_symmetric(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);
    if(c_matrix_symmetric(A->n, A->data))
        return Qtrue;
    return Qfalse;
}

#to_aObject



130
131
132
# File 'lib/matrix/matrix.rb', line 130

def to_a
  rows.collect(&:dup)
end

#to_matrixObject Also known as: conjugate, real

Explicit conversion to a Matrix. Returns self



40
41
42
# File 'lib/matrix/matrix.rb', line 40

def to_matrix
  self
end

#to_sObject Also known as: to_str

Overrides Object#to_s



121
122
123
124
125
# File 'lib/matrix/matrix.rb', line 121

def to_s
  "#{self.class}[#{collect do |row|
    '[' + row.join(', ') + ']'
  end.join(', ')}]"
end

#traceObject Also known as: tr



540
541
542
543
544
545
# File 'ext/fast_matrix/Matrix/matrix.c', line 540

VALUE matrix_trace(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);
    return DBL2NUM(c_matrix_trace(A->n, A->data));
}

#transposeObject Also known as: t



233
234
235
236
237
238
239
# File 'ext/fast_matrix/Matrix/matrix.c', line 233

VALUE matrix_transpose(VALUE self)
{
	struct matrix* M = get_matrix_from_rb_value(self);
    MAKE_MATRIX_AND_RB_VALUE(R, result, M->n, M->m);
    c_matrix_transpose(M->m, M->n, M->data, R->data);
    return result;
}

#unitary?Boolean

Returns:

  • (Boolean)


722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'ext/fast_matrix/Matrix/matrix.c', line 722

VALUE matrix_unitary(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    if(A->m != A-> n)
        return Qfalse;
    
    int n = A->n;
    double* B = malloc(n * n * sizeof(double));
    double* C = malloc(n * n * sizeof(double));
    
    c_matrix_transpose(n, n, A->data, B);
    c_matrix_strassen(n, n, n, A->data, B, C);
    
    VALUE res = Qfalse;
    if(c_matrix_identity(n, C))
        res = Qtrue;
    
    free(B);
    free(C);
    return res;
}

#upper_triangular?Boolean

Returns:

  • (Boolean)


625
626
627
628
629
630
631
632
633
# File 'ext/fast_matrix/Matrix/matrix.c', line 625

VALUE matrix_upper_triangular(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    raise_check_square_matrix(A);

    if(c_matrix_upper_triangular(A->n, A->data))
        return Qtrue;
    return Qfalse;
}

#zero?Boolean

Returns:

  • (Boolean)


585
586
587
588
589
590
591
# File 'ext/fast_matrix/Matrix/matrix.c', line 585

VALUE matrix_zero(VALUE self)
{
	struct matrix* A = get_matrix_from_rb_value(self);
    if(zero_d_array(A->m * A->n, A->data))
            return Qtrue;
    return Qfalse;
}