Class: NMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/numruby/nmatrix.rb,
lib/numruby/version.rb,
ext/ruby_nmatrix.c

Defined Under Namespace

Modules: VERSION

Instance Method Summary collapse

Constructor Details

#initializeObject

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);

#acosObject

#acoshObject

#asinObject

#asinhObject

#atanObject

#atanhObject

#cbrtObject

#ceilObject

#choleskyObject



462
# File 'ext/ruby_nmatrix.c', line 462

VALUE nm_cholesky(VALUE self);

#cholesky_solveObject



463
# File 'ext/ruby_nmatrix.c', line 463

VALUE nm_cholesky_solve(VALUE self);

#cosObject

#coshObject

#detObject



421
# File 'ext/ruby_nmatrix.c', line 421

VALUE nm_det(VALUE self);

#diagsvdObject



433
# File 'ext/ruby_nmatrix.c', line 433

VALUE nm_diagsvd(VALUE self);

#dimObject



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);
}

#dtypeObject



468
# File 'ext/ruby_nmatrix.c', line 468

VALUE nm_get_dtype(VALUE self);

#eachObject

Iterators Methods



359
# File 'ext/ruby_nmatrix.c', line 359

VALUE nm_each(VALUE self);

#each_columnObject



366
# File 'ext/ruby_nmatrix.c', line 366

VALUE nm_each_column(VALUE self);

#each_layerObject



367
# File 'ext/ruby_nmatrix.c', line 367

VALUE nm_each_layer(VALUE self);

#each_rankObject

VALUE nm_map_stored(VALUE self);



364
# File 'ext/ruby_nmatrix.c', line 364

VALUE nm_each_rank(VALUE self, VALUE dimension_idx);

#each_rowObject



365
# File 'ext/ruby_nmatrix.c', line 365

VALUE nm_each_row(VALUE self);

#each_with_indicesObject



360
# File 'ext/ruby_nmatrix.c', line 360

VALUE nm_each_with_indices(VALUE self);

#eigObject



425
# File 'ext/ruby_nmatrix.c', line 425

VALUE nm_eig(VALUE self);

#eighObject



426
# File 'ext/ruby_nmatrix.c', line 426

VALUE nm_eigh(VALUE self);

#eigvalshObject



427
# File 'ext/ruby_nmatrix.c', line 427

VALUE nm_eigvalsh(VALUE self);

#elementsObject



354
# File 'ext/ruby_nmatrix.c', line 354

VALUE nm_get_elements(VALUE self);

#erfObject

#erfcObject

#expObject

#floorObject

#inspectObject

: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

#invertObject



419
# File 'ext/ruby_nmatrix.c', line 419

VALUE nm_invert(VALUE self);

#kronecker_prodObject



424
# File 'ext/ruby_nmatrix.c', line 424

VALUE nm_kronecker_prod(VALUE self);

#least_squareObject



422
# File 'ext/ruby_nmatrix.c', line 422

VALUE nm_least_square(VALUE self, VALUE rhs_val);

#lgammaObject

#log10Object

#log1pObject

#log2Object

#luObject



428
# File 'ext/ruby_nmatrix.c', line 428

VALUE nm_lu(VALUE self);

#lu_factorObject



429
# File 'ext/ruby_nmatrix.c', line 429

VALUE nm_lu_factor(VALUE self);

#lu_solveObject



430
# File 'ext/ruby_nmatrix.c', line 430

VALUE nm_lu_solve(VALUE self, VALUE rhs_val);

#normObject



414
# File 'ext/ruby_nmatrix.c', line 414

VALUE nm_norm2(VALUE self);

#orthObject



461
# File 'ext/ruby_nmatrix.c', line 461

VALUE nm_orth(VALUE self);

#pinvObject



423
# File 'ext/ruby_nmatrix.c', line 423

VALUE nm_pinv(VALUE self);

#pretty_print(q) ⇒ String

Printing the NMatrix object

Examples:

TODO

Parameters:

  • q

Returns:

  • (String)

    inspect



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

#rankObject



467
# File 'ext/ruby_nmatrix.c', line 467

VALUE nm_get_rank(VALUE self, VALUE dim);

#shapeObject



355
# File 'ext/ruby_nmatrix.c', line 355

VALUE nm_get_shape(VALUE self);

#sinObject

#sinhObject

#solveObject



420
# File 'ext/ruby_nmatrix.c', line 420

VALUE nm_solve(VALUE self, VALUE rhs_val);

#sqrtObject

#stypeObject



469
# File 'ext/ruby_nmatrix.c', line 469

VALUE nm_get_stype(VALUE self);

#svdObject



431
# File 'ext/ruby_nmatrix.c', line 431

VALUE nm_svd(VALUE self);

#svdvalsObject



432
# File 'ext/ruby_nmatrix.c', line 432

VALUE nm_svdvals(VALUE self);

#tanObject

#tanhObject

#tgammaObject

#to_aObject



88
89
90
# File 'lib/numruby/nmatrix.rb', line 88

def to_a
  return self.elements
end