Class: NMatrix
- Inherits:
-
Object
- Object
- NMatrix
- Defined in:
- lib/numruby/nmatrix.rb,
lib/numruby/version.rb,
ext/ruby_nmatrix.c
Defined Under Namespace
Modules: VERSION
Instance Method Summary collapse
- #* ⇒ Object
- #+ ⇒ Object
- #- ⇒ Object
- #/ ⇒ Object
- #< ⇒ Object
- #<= ⇒ Object
-
#== ⇒ Object
VALUE nm_get_column(VALUE self, VALUE column_number);.
- #> ⇒ Object
- #>= ⇒ Object
- #[] ⇒ Object
- #[]= ⇒ Object
- #acos ⇒ Object
- #acosh ⇒ Object
- #asin ⇒ Object
- #asinh ⇒ Object
- #atan ⇒ Object
- #atanh ⇒ Object
- #cbrt ⇒ Object
- #ceil ⇒ Object
- #cholesky ⇒ Object
- #cholesky_solve ⇒ Object
- #cos ⇒ Object
- #cosh ⇒ Object
- #det ⇒ Object
- #diagsvd ⇒ Object
- #dim ⇒ Object
-
#dot(another) ⇒ Object
Calculates the dot product of two matrices.
- #dtype ⇒ Object
-
#each ⇒ Object
Iterators Methods.
- #each_column ⇒ Object
- #each_layer ⇒ Object
-
#each_rank ⇒ Object
VALUE nm_map_stored(VALUE self);.
- #each_row ⇒ Object
- #each_with_indices ⇒ Object
- #eig ⇒ Object
- #eigh ⇒ Object
- #eigvalsh ⇒ Object
- #elements ⇒ Object
- #erf ⇒ Object
- #erfc ⇒ Object
- #exp ⇒ Object
- #floor ⇒ Object
-
#initialize ⇒ Object
constructor
VALUE nm_broadcast_arrays(int argc, VALUE* argv).
-
#inspect ⇒ Object
:nodoc:.
- #invert ⇒ Object
- #kronecker_prod ⇒ Object
- #least_square ⇒ Object
- #lgamma ⇒ Object
- #log10 ⇒ Object
- #log1p ⇒ Object
- #log2 ⇒ Object
- #lu ⇒ Object
- #lu_factor ⇒ Object
- #lu_solve ⇒ Object
- #norm ⇒ Object
- #orth ⇒ Object
- #pinv ⇒ Object
-
#pretty_print(q) ⇒ String
Printing the NMatrix object.
- #rank ⇒ Object
- #shape ⇒ Object
- #sin ⇒ Object
- #sinh ⇒ Object
- #solve ⇒ Object
- #sqrt ⇒ Object
- #stype ⇒ Object
- #svd ⇒ Object
- #svdvals ⇒ Object
- #tan ⇒ Object
- #tanh ⇒ Object
- #tgamma ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ Object
VALUE nm_broadcast_arrays(int argc, VALUE* argv)
352 |
# File 'ext/ruby_nmatrix.c', line 352
VALUE nmatrix_init(int argc, VALUE* argv, VALUE self);
|
Instance Method Details
#* ⇒ Object
#+ ⇒ Object
377 |
# File 'ext/ruby_nmatrix.c', line 377
VALUE nm_add( VALUE self, VALUE another);
|
#- ⇒ Object
#/ ⇒ Object
#< ⇒ Object
375 |
# File 'ext/ruby_nmatrix.c', line 375
VALUE nm_lt( VALUE self, VALUE another);
|
#<= ⇒ Object
376 |
# File 'ext/ruby_nmatrix.c', line 376
VALUE nm_lteq(VALUE self, VALUE another);
|
#== ⇒ Object
VALUE nm_get_column(VALUE self, VALUE column_number);
372 |
# File 'ext/ruby_nmatrix.c', line 372
VALUE nm_eqeq(VALUE self, VALUE another);
|
#> ⇒ Object
373 |
# File 'ext/ruby_nmatrix.c', line 373
VALUE nm_gt( VALUE self, VALUE another);
|
#>= ⇒ Object
374 |
# File 'ext/ruby_nmatrix.c', line 374
VALUE nm_gteq(VALUE self, VALUE another);
|
#[] ⇒ Object
465 |
# File 'ext/ruby_nmatrix.c', line 465
VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self);
|
#[]= ⇒ Object
466 |
# File 'ext/ruby_nmatrix.c', line 466
VALUE nm_accessor_set(int argc, VALUE* argv, VALUE self);
|
#acos ⇒ Object
#acosh ⇒ Object
#asin ⇒ Object
#asinh ⇒ Object
#atan ⇒ Object
#atanh ⇒ Object
#cbrt ⇒ Object
#ceil ⇒ Object
#cholesky ⇒ Object
462 |
# File 'ext/ruby_nmatrix.c', line 462 VALUE nm_cholesky(VALUE self); |
#cholesky_solve ⇒ Object
463 |
# File 'ext/ruby_nmatrix.c', line 463 VALUE nm_cholesky_solve(VALUE self); |
#cos ⇒ Object
#cosh ⇒ Object
#det ⇒ Object
421 |
# File 'ext/ruby_nmatrix.c', line 421 VALUE nm_det(VALUE self); |
#diagsvd ⇒ Object
433 |
# File 'ext/ruby_nmatrix.c', line 433 VALUE nm_diagsvd(VALUE self); |
#dim ⇒ Object
353 |
# File 'ext/ruby_nmatrix.c', line 353 VALUE nm_get_dim(VALUE self); |
#dot(another) ⇒ Object
Calculates the dot product of two matrices. Args:
- self matrix, type: NMatrix
- another matrix, type: NMatrix
returns the resultant matrix of type NMatrix
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'ext/blas.c', line 9
VALUE nm_dot(VALUE self, VALUE another){
nmatrix* left;
nmatrix* right;
Data_Get_Struct(self, nmatrix, left);
Data_Get_Struct(another, nmatrix, right);
nmatrix* result = ALLOC(nmatrix);
result->dtype = left->dtype;
result->stype = left->stype;
result->ndims = left->ndims;
result->shape = ALLOC_N(size_t, result->ndims);
result->shape[0] = left->shape[0];
result->shape[1] = right->shape[1];
result->count = result->shape[0] * result->shape[1];
switch (left->dtype) {
case nm_bool:
{
// Not supported message and casting to double
break;
}
case nm_int:
{
// Not supported message and casting to double
break;
}
case nm_float64:
{
result->elements = ALLOC_N(double, result->shape[0] * result->shape[1]);
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, (int)left->shape[0], (int)right->shape[1], (int)left->shape[1], /*no scaling*/
1, left->elements, (int)left->shape[1], right->elements, (int)right->shape[1], /*no addition*/0, result->elements, (int)right->shape[1]);
break;
}
case nm_float32:
{
result->elements = ALLOC_N(float, result->shape[0] * result->shape[1]);
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, (int)left->shape[0], (int)right->shape[1], (int)left->shape[1], /*no scaling*/
1, left->elements, (int)left->shape[1], right->elements, (int)right->shape[1], /*no addition*/0, result->elements, (int)right->shape[1]);
break;
}
case nm_complex32:
{
float alpha[2] = {1, 1};
float beta[2] = {0, 0};
result->elements = ALLOC_N(complex float, result->shape[0] * result->shape[1]);
cblas_cgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, (int)left->shape[0], (int)right->shape[1], (int)left->shape[1], /*no scaling*/
alpha, left->elements, (int)left->shape[1], right->elements, (int)right->shape[1], /*no addition*/beta, result->elements, (int)right->shape[1]);
break;
}
case nm_complex64:
{
double alpha[2] = {1, 1};
double beta[2] = {0, 0};
result->elements = ALLOC_N(complex double, result->shape[0] * result->shape[1]);
cblas_zgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, (int)left->shape[0], (int)right->shape[1], (int)left->shape[1], /*no scaling*/
alpha, left->elements, (int)left->shape[1], right->elements, (int)right->shape[1], /*no addition*/beta, result->elements, (int)right->shape[1]);
break;
}
}
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result);
}
|
#dtype ⇒ Object
468 |
# File 'ext/ruby_nmatrix.c', line 468 VALUE nm_get_dtype(VALUE self); |
#each ⇒ Object
Iterators Methods
359 |
# File 'ext/ruby_nmatrix.c', line 359 VALUE nm_each(VALUE self); |
#each_column ⇒ Object
366 |
# File 'ext/ruby_nmatrix.c', line 366 VALUE nm_each_column(VALUE self); |
#each_layer ⇒ Object
367 |
# File 'ext/ruby_nmatrix.c', line 367 VALUE nm_each_layer(VALUE self); |
#each_rank ⇒ Object
VALUE nm_map_stored(VALUE self);
364 |
# File 'ext/ruby_nmatrix.c', line 364
VALUE nm_each_rank(VALUE self, VALUE dimension_idx);
|
#each_row ⇒ Object
365 |
# File 'ext/ruby_nmatrix.c', line 365 VALUE nm_each_row(VALUE self); |
#each_with_indices ⇒ Object
360 |
# File 'ext/ruby_nmatrix.c', line 360 VALUE nm_each_with_indices(VALUE self); |
#eig ⇒ Object
425 |
# File 'ext/ruby_nmatrix.c', line 425 VALUE nm_eig(VALUE self); |
#eigh ⇒ Object
426 |
# File 'ext/ruby_nmatrix.c', line 426 VALUE nm_eigh(VALUE self); |
#eigvalsh ⇒ Object
427 |
# File 'ext/ruby_nmatrix.c', line 427 VALUE nm_eigvalsh(VALUE self); |
#elements ⇒ Object
354 |
# File 'ext/ruby_nmatrix.c', line 354 VALUE nm_get_elements(VALUE self); |
#erf ⇒ Object
#erfc ⇒ Object
#exp ⇒ Object
#floor ⇒ Object
#inspect ⇒ Object
:nodoc:
92 93 94 95 96 |
# File 'lib/numruby/nmatrix.rb', line 92 def inspect #:nodoc: original_inspect = super() original_inspect = original_inspect[0...original_inspect.size-1] original_inspect + " " + inspect_helper.join(" ") + ">" end |
#invert ⇒ Object
419 |
# File 'ext/ruby_nmatrix.c', line 419 VALUE nm_invert(VALUE self); |
#kronecker_prod ⇒ Object
424 |
# File 'ext/ruby_nmatrix.c', line 424 VALUE nm_kronecker_prod(VALUE self); |
#least_square ⇒ Object
422 |
# File 'ext/ruby_nmatrix.c', line 422
VALUE nm_least_square(VALUE self, VALUE rhs_val);
|
#lgamma ⇒ Object
#log10 ⇒ Object
#log1p ⇒ Object
#log2 ⇒ Object
#lu ⇒ Object
428 |
# File 'ext/ruby_nmatrix.c', line 428 VALUE nm_lu(VALUE self); |
#lu_factor ⇒ Object
429 |
# File 'ext/ruby_nmatrix.c', line 429 VALUE nm_lu_factor(VALUE self); |
#lu_solve ⇒ Object
430 |
# File 'ext/ruby_nmatrix.c', line 430
VALUE nm_lu_solve(VALUE self, VALUE rhs_val);
|
#norm ⇒ Object
414 |
# File 'ext/ruby_nmatrix.c', line 414 VALUE nm_norm2(VALUE self); |
#orth ⇒ Object
461 |
# File 'ext/ruby_nmatrix.c', line 461 VALUE nm_orth(VALUE self); |
#pinv ⇒ Object
423 |
# File 'ext/ruby_nmatrix.c', line 423 VALUE nm_pinv(VALUE self); |
#pretty_print(q) ⇒ String
Printing the NMatrix object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/numruby/nmatrix.rb', line 10 def pretty_print(q) if self.dim == 1 q.group(0, "\n[", "]") do q.seplist(self.elements, -> { q.text ", " }, :each) do |v| q.text v.inspect end end elsif self.dim == 2 # iterate through the whole matrix and find the longest number for each column longest = Array.new(self.shape[1], 0) for col_index in 0...self.shape[1] do j = col_index self.shape[0].times do elem_len = self.elements[j].inspect.size longest[col_index] = elem_len if longest[col_index] < elem_len j += self.shape[1] end end q.group(0, "\n[\n", "]") do for row_index in 0...self.shape[0] do i = (row_index * self.shape[1]) current_row = Array.new self.shape[1].times do current_row.push(self.elements[i]) i += 1 end q.group(1, " [", "]") do q.seplist(current_row, -> { q.text ", " }, :each_with_index) do |v,j| q.text v.inspect.rjust(longest[j]) end end q.comma_breakable unless row_index + 1 == self.shape[0] q.text "\n" end end elsif self.dim == 3 q.group(0, "\n[\n", "]") do for layer_index in 0...self.shape[2] do # iterate through the whole matrix and find the longest number for each column longest = Array.new(self.shape[1], 0) for col_index in 0...self.shape[1] do j = (col_index * self.shape[2]) + layer_index self.shape[0].times do elem_len = self.elements[j].inspect.size longest[col_index] = elem_len if longest[col_index] < elem_len j += (self.shape[1] * self.shape[2]) end end q.group(1, " [\n", " ]") do for row_index in 0...self.shape[0] do i = (row_index * self.shape[1] * self.shape[2]) + layer_index current_row = Array.new self.shape[1].times do current_row.push(self.elements[i]) i += self.shape[2] end q.group(2, " [", "]") do q.seplist(current_row, -> { q.text ", " }, :each_with_index) do |v,j| q.text v.inspect.rjust(longest[j]) end end q.text "," unless row_index + 1 == self.shape[0] q.text "\n" end end q.text "," unless layer_index + 1 == self.shape[2] q.text "\n" end end else self.inspect.pretty_print(q) end end |
#rank ⇒ Object
467 |
# File 'ext/ruby_nmatrix.c', line 467
VALUE nm_get_rank(VALUE self, VALUE dim);
|
#shape ⇒ Object
355 |
# File 'ext/ruby_nmatrix.c', line 355 VALUE nm_get_shape(VALUE self); |
#sin ⇒ Object
#sinh ⇒ Object
#solve ⇒ Object
420 |
# File 'ext/ruby_nmatrix.c', line 420
VALUE nm_solve(VALUE self, VALUE rhs_val);
|
#sqrt ⇒ Object
#stype ⇒ Object
469 |
# File 'ext/ruby_nmatrix.c', line 469 VALUE nm_get_stype(VALUE self); |
#svd ⇒ Object
431 |
# File 'ext/ruby_nmatrix.c', line 431 VALUE nm_svd(VALUE self); |
#svdvals ⇒ Object
432 |
# File 'ext/ruby_nmatrix.c', line 432 VALUE nm_svdvals(VALUE self); |
#tan ⇒ Object
#tanh ⇒ Object
#tgamma ⇒ Object
#to_a ⇒ Object
88 89 90 |
# File 'lib/numruby/nmatrix.rb', line 88 def to_a return self.elements end |