Class: GSLng::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/gslng/matrix.rb,
lib/gslng/plotter.rb,
lib/gslng/matrix_view.rb

Overview

A fixed-size MxN matrix.

Notes

See Vector notes. Everything applies with the following differences/additions:

  • The #* operator performs actual matrix-matrix and matrix-vector products. To perform element-by-element multiplication use the #^ operator (or #multiply method) instead. The rest of the operators work element-by-element.

  • Operators can handle matrix-matrix, matrix-vector and matrix-scalar (also in reversed order). See #coerce.

  • The #[] and #[]= operators can handle a “wildcard” value for any dimension, just like MATLAB’s colon (:).

Direct Known Subclasses

View

Defined Under Namespace

Classes: View

Instance Attribute Summary collapse

Constructors collapse

Setting/getting values collapse

Views collapse

Operators collapse

Row/column swapping collapse

Predicate methods collapse

Minimum/maximum collapse

High-order methods collapse

Type conversions collapse

Equality collapse

FFI collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, n, zero = false) ⇒ Matrix

Create a Matrix of m-by-n (rows and columns). If zero is true, the Matrix is initialized with zeros. Otherwise, the Matrix will contain garbage. You can optionally pass a block, in which case #map_index! will be called with it (i.e.: it works like Array.new).



28
29
30
31
32
33
34
35
# File 'lib/gslng/matrix.rb', line 28

def initialize(m, n, zero = false)
  @backend = GSLng.backend
  ptr = zero ? @backend.gsl_matrix_calloc(m, n) : @backend.gsl_matrix_alloc(m, n)
  @ptr = FFI::AutoPointer.new(ptr, Matrix.method(:release))
  @ptr_value = @ptr.to_i
  @m,@n = m,n
  if (block_given?) then self.map_index!(Proc.new) end
end

Instance Attribute Details

#mObject (readonly) Also known as: height, rows

Returns the value of attribute m.



11
12
13
# File 'lib/gslng/matrix.rb', line 11

def m
  @m
end

#nObject (readonly) Also known as: width, columns

Returns the value of attribute n.



11
12
13
# File 'lib/gslng/matrix.rb', line 11

def n
  @n
end

#ptrObject (readonly)



12
13
14
# File 'lib/gslng/matrix.rb', line 12

def ptr
  @ptr
end

#ptr_valueObject (readonly)



13
14
15
# File 'lib/gslng/matrix.rb', line 13

def ptr_value
  @ptr_value
end

Class Method Details

.[](*args) ⇒ Object

Create a Matrix from an Array/Array of Arrays/Range

Examples:

Matrix[[1,2],[3,4]] => [1.0 2.0; 3.0 4.0]:Matrix
Matrix[1,2,3] => [1.0 2.0 3.0]:Matrix
Matrix[[1..3],[5..7]] => [1.0 2.0 3.0; 5.0 6.0 7.0]:Matrix

See Also:

  • Matrix::from_array


78
79
80
# File 'lib/gslng/matrix.rb', line 78

def Matrix.[](*args)
  Matrix.from_array(args)
end

.from_array(array) ⇒ Object

Create a matrix from an Array

See Also:

  • Matrix::[]


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gslng/matrix.rb', line 56

def Matrix.from_array(array)
  if (array.empty?) then raise "Can't create empty matrix" end

  if (Numeric === array[0])
    m = Matrix.new(1, array.size)
    GSLng.backend.gsl_matrix_from_array(m.ptr_value, [ array ])
    return m
  elsif (Array === array[0])
    m = Matrix.new(array.size, array[0].size)
    GSLng.backend.gsl_matrix_from_array(m.ptr_value, array)
    return m
  else
    Matrix.new(array.size, array[0].to_a.size) {|i,j| array[i].to_a[j]}
  end
end

.random(m, n) ⇒ Object Also known as: rand

Generates a Matrix of m by n, of random numbers between 0 and 1. NOTE: This simply uses Kernel::rand



84
85
86
# File 'lib/gslng/matrix.rb', line 84

def Matrix.random(m, n)
  Matrix.new(m, n).map!{|x| Kernel::rand}
end

.release(ptr) ⇒ Object



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

def Matrix.release(ptr) # @private
  GSLng.backend.gsl_matrix_free(ptr)
end

.zero(m, n) ⇒ Object

Same as Matrix.new(m, n, true)



52
# File 'lib/gslng/matrix.rb', line 52

def Matrix.zero(m, n); Matrix.new(m, n, true) end

Instance Method Details

#*(other) ⇒ Object

TODO:

some cases could be optimized when doing Matrix-Matrix, by using dgemv

Matrix Product. self.n should equal other.m (or other.size, if a Vector).

Examples:

Matrix[[1,2],[2,3]] * 2 => [2.0 4.0; 4.0 6.0]:Matrix


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/gslng/matrix.rb', line 281

def *(other)
  case other
  when Numeric
    self.multiply(other)
  when Vector
    matrix = Matrix.new(self.m, other.size)
    @backend.gsl_blas_dgemm(:no_transpose, :no_transpose, 1, @ptr, other.to_matrix.ptr, 0, matrix.ptr)
    return matrix
  when Matrix
    matrix = Matrix.new(self.m, other.n)
    @backend.gsl_blas_dgemm(:no_transpose, :no_transpose, 1, @ptr, other.ptr, 0, matrix.ptr)
    return matrix
  else
    x,y = other.coerce(self)
    x * y
  end
end

#+(other) ⇒ Object

Element-by-element addition



264
# File 'lib/gslng/matrix.rb', line 264

def +(other); self.dup.add!(other) end

#-(other) ⇒ Object

Element-by-element substraction



267
# File 'lib/gslng/matrix.rb', line 267

def -(other); self.dup.substract!(other) end

#/(other) ⇒ Object

Element-by-element division



270
# File 'lib/gslng/matrix.rb', line 270

def /(other); self.dup.divide!(other) end

#==(other) ⇒ Object

Element-by-element comparison.



507
508
509
510
511
512
513
514
515
516
517
# File 'lib/gslng/matrix.rb', line 507

def ==(other)
  if (self.m != other.m || self.n != other.n) then return false end

  @m.times do |i|
    @n.times do |j|
      if (self[i,j] != other[i,j]) then return false end
    end
  end
  
  return true
end

#[](i, j = :*) ⇒ Numeric, Matrix

Access the element (i,j), which means (row,column). Symbols :* or :all can be used as wildcards for both dimensions.

Examples:

If m = Matrix[,[3,4]]

m[0,0] => 1.0
m[0,:*] => [1.0, 2.0]:Matrix
m[:*,0] => [1.0, 3.0]:Matrix
m[:*,:*] => [1.0, 2.0; 3.0, 4.0]:Matrix

Returns:

  • (Numeric, Matrix)

    the element/sub-matrix

Raises:

  • (RuntimeError)

    if out-of-bounds



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gslng/matrix.rb', line 100

def [](i, j = :*)
  if (Integer === i && Integer === j)
    @backend.gsl_matrix_get_operator(@ptr_value, i, j)
  else
    if (Symbol === i && Symbol === j) then return self
    elsif (Symbol === i)
      col = Vector.new(@m)
      @backend.gsl_matrix_get_col(col.ptr, @ptr, j)
      return col.to_matrix
    elsif (Symbol === j)
      row = Vector.new(@n)
      @backend.gsl_matrix_get_row(row.ptr, @ptr, i)
      return row.to_matrix
    end
  end
end

#[]=(i, j, value) ⇒ Object

Set the element (i,j), which means (row,column).

Parameters:

Raises:

  • (RuntimeError)

    if out-of-bounds

See Also:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gslng/matrix.rb', line 121

def []=(i, j, value)
  if (Symbol === i && Symbol === j) then
    if (Numeric === value) then self.fill!(value)
    else
      x,y = self.coerce(value)
      @backend.gsl_matrix_memcpy(@ptr, x.ptr)
    end
  elsif (Symbol === i)
    col = Vector.new(@m)
    x,y = col.coerce(value)
    @backend.gsl_matrix_set_col(@ptr, j, x.ptr)
    return col
  elsif (Symbol === j)
    row = Vector.new(@n)
    x,y = row.coerce(value)
    @backend.gsl_matrix_set_row(@ptr, i, x.ptr)
    return row
  else
    @backend.gsl_matrix_set_operator(@ptr_value, i, j, value)
  end

  return self
end

#^(other) ⇒ Object Also known as: multiply, mul

Element-by-element product. Both matrices should have same dimensions.



273
# File 'lib/gslng/matrix.rb', line 273

def ^(other); self.dup.multiply!(other) end

#add!(other) ⇒ Matrix

Add other to self

Returns:



210
211
212
213
214
215
216
217
218
219
# File 'lib/gslng/matrix.rb', line 210

def add!(other)
  case other
  when Numeric; @backend.gsl_matrix_add_constant(@ptr, other.to_f)
  when Matrix; @backend.gsl_matrix_add(@ptr, other.ptr)
  else
    x,y = other.coerce(self)
    x.add!(y)
  end
  return self
end

#all!(v) ⇒ Object Also known as: set!, fill!

Set all values to v



146
# File 'lib/gslng/matrix.rb', line 146

def all!(v); @backend.gsl_matrix_set_all(@ptr, v); return self end

#coerce(other) ⇒ Object

Coerces other to be of Matrix class. If other is a scalar (Numeric) a Matrix filled with other values is created. Vectors are coerced using Vector#to_matrix (which results in a row matrix).



491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/gslng/matrix.rb', line 491

def coerce(other)
  case other
  when Matrix
    [ other, self ]
  when Numeric
    [ Matrix.new(@m, @n).fill!(other), self ]
  when Vector
    [ other.to_matrix, self ]
  else
    raise TypeError, "Can't coerce #{other.class} into #{self.class}"
  end
end

#column(*args) ⇒ Matrix

Analogous to #submatrix

Returns:



179
# File 'lib/gslng/matrix.rb', line 179

def column(*args); self.column_view(*args).to_matrix end

#column?Boolean

If this is a column Matrix

Returns:

  • (Boolean)


338
# File 'lib/gslng/matrix.rb', line 338

def column?; self.columns == 1 end

#column_vecview(i, offset = 0, size = nil) ⇒ Vector::View

Same as #column_view, but returns a Vector::View

Returns:



199
200
201
202
203
# File 'lib/gslng/matrix.rb', line 199

def column_vecview(i, offset = 0, size = nil)
  size = (@m - offset) if size.nil?
  ptr = @backend.gsl_matrix_column_view(@ptr, i, offset, size)
  Vector::View.new(ptr, self, size, self.columns)
end

#column_view(i, offset = 0, size = nil) ⇒ Matrix::View

Creates a View for the i-th column

Returns:



175
# File 'lib/gslng/matrix.rb', line 175

def column_view(i, offset = 0, size = nil); self.view(offset, i, (size or (@m - offset)), 1) end

#data_ptrObject

Returns the FFI::Pointer to the underlying C data memory; can be used to pass the data directly to/from other FFI libraries, without needing to go through Ruby conversion



524
525
526
# File 'lib/gslng/matrix.rb', line 524

def data_ptr
  GSLmatrix.new(ptr)[:data]
end

#define_plot(with, extra_cmds = '') ⇒ Plot

Works the same as #plot but returns a Plotter::Plot object you can store and then pass (along with other plot objects) to Plotter#plot and create a single plot out of them

Returns:



79
80
81
82
83
84
85
86
# File 'lib/gslng/plotter.rb', line 79

def define_plot(with, extra_cmds = '')
  if (with == 'image')
    cmd = "'-' binary array=(#{self.m},#{self.n}) format='%double' #{extra_cmds} with #{with}"
  else
    cmd = "'-' binary record=(#{self.m}) format=\"#{Array.new(self.n,'%double').join('')}\" #{extra_cmds} with #{with}"
  end
  Plotter::Plot.new(cmd, self)
end

#divide!(other) ⇒ Matrix Also known as: div!

Divide (element-by-element) self by other

Returns:



251
252
253
254
255
256
257
258
259
260
# File 'lib/gslng/matrix.rb', line 251

def divide!(other)
  case other
  when Numeric; @backend.gsl_matrix_scale(@ptr, 1.0 / other)
  when Matrix;  @backend.gsl_matrix_div_elements(@ptr, other.ptr)
  else
    x,y = other.coerce(self)
    x.divide!(y)
  end
  return self
end

#each(block = Proc.new) {|elem| ... } ⇒ void

This method returns an undefined value.

Calls the block on each element of the matrix

Yields:

  • (elem)


387
388
389
# File 'lib/gslng/matrix.rb', line 387

def each 
  @m.times {|i| @n.times {|j| yield(self[i,j]) } }
end

#each_column {|view| ... } ⇒ Object

Yields the block for each column view (View).

Yields:



424
# File 'lib/gslng/matrix.rb', line 424

def each_column; self.columns.times {|i| yield(column_view(i))} end

#each_row {|view| ... } ⇒ Object

Yields the block for each row view (View).

Yields:



412
# File 'lib/gslng/matrix.rb', line 412

def each_row; self.rows.times {|i| yield(row_view(i))} end

#each_vec_column {|vector_view| ... } ⇒ Object

Same as #each_column, but yields Vector::View‘s

Yields:

  • (vector_view)


420
# File 'lib/gslng/matrix.rb', line 420

def each_vec_column; self.columns.times {|i| yield(column_vecview(i))} end

#each_vec_row {|vector_view| ... } ⇒ Object

Same as #each_row, but yields Vector::View‘s

Yields:

  • (vector_view)


416
# File 'lib/gslng/matrix.rb', line 416

def each_vec_row; self.rows.times {|i| yield(row_vecview(i))} end

#each_with_index(block = Proc.new) {|elem, i, j| ... } ⇒ Object

Yields:

  • (elem, i, j)

See Also:



393
394
395
# File 'lib/gslng/matrix.rb', line 393

def each_with_index 
  @m.times {|i| @n.times {|j| yield(self[i,j], i, j) } }
end

#identityObject

Set the identity matrix values



154
# File 'lib/gslng/matrix.rb', line 154

def identity; @backend.gsl_matrix_set_identity(@ptr); return self end

#initialize_copy(other) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/gslng/matrix.rb', line 37

def initialize_copy(other) # @private
  @backend = GSLng.backend
  ptr = @backend.gsl_matrix_alloc(other.m, other.n)
  @ptr = FFI::AutoPointer.new(ptr, Matrix.method(:release))
  @ptr_value = @ptr.to_i

  @m,@n = other.size
  @backend.gsl_matrix_memcpy(@ptr, other.ptr)
end

#inspectObject



484
485
486
# File 'lib/gslng/matrix.rb', line 484

def inspect # @private
  "#{self}:Matrix"
end

#join(sep = $,) ⇒ Object

Same as Array#join

Examples:

Matrix[[1,2],[2,3]].join => "1.0 2.0 2.0 3.0"


453
454
455
456
457
458
459
# File 'lib/gslng/matrix.rb', line 453

def join(sep = $,)
  s = ''
  self.each do |e|
    s += (s.empty?() ? e.to_s : "#{sep}#{e}")
  end
  return s
end

#map(block = Proc.new) {|elem| ... } ⇒ Matrix

Yields:

  • (elem)

Returns:

See Also:



441
# File 'lib/gslng/matrix.rb', line 441

def map(block = Proc.new); self.dup.map!(block) end

#map!(block = Proc.new) {|elem| ... } ⇒ Object

Efficient #map! implementation

Yields:

  • (elem)


428
# File 'lib/gslng/matrix.rb', line 428

def map!(block = Proc.new); @backend.gsl_matrix_map!(@ptr_value, &block); return self end

#map_array(block = Proc.new) {|elem| ... } ⇒ Array

Yields:

  • (elem)

Returns:

See Also:



446
# File 'lib/gslng/matrix.rb', line 446

def map_array(block = Proc.new); @backend.gsl_matrix_map_array(@ptr_value, &block) end

#map_index!(block = Proc.new) {|i, j| ... } ⇒ Object

Alternate version of #map!, in this case the block receives the index (row, column) as a parameter.

Yields:

  • (i, j)


432
# File 'lib/gslng/matrix.rb', line 432

def map_index!(block = Proc.new); @backend.gsl_matrix_map_index!(@ptr_value, &block); return self end

#map_with_index!(block = Proc.new) {|elem, i, j| ... } ⇒ Object

Similar to #map_index!, in this case it receives both the element and the index to it

Yields:

  • (elem, i, j)


436
# File 'lib/gslng/matrix.rb', line 436

def map_with_index!(block = Proc.new); @backend.gsl_matrix_map_with_index!(@ptr_value, &block); return self end

#maxObject

Maximum element of the Matrix



343
# File 'lib/gslng/matrix.rb', line 343

def max; @backend.gsl_matrix_max(@ptr) end

#max_indexObject

Same as #max, but returns the indices to the i-th and j-th maximum elements



376
377
378
379
380
381
# File 'lib/gslng/matrix.rb', line 376

def max_index
  i_max = FFI::Buffer.new(:size_t)
  j_max = FFI::Buffer.new(:size_t)
  @backend.gsl_matrix_max_index(@ptr, i_max, j_max)
  return [i_max[0].get_ulong(0), j_max[0].get_ulong(0)]
end

#minObject

Minimum element of the Matrix



346
# File 'lib/gslng/matrix.rb', line 346

def min; @backend.gsl_matrix_min(@ptr) end

#min_indexObject

Same as #min, but returns the indices to the i-th and j-th minimum elements



368
369
370
371
372
373
# File 'lib/gslng/matrix.rb', line 368

def min_index
  i_min = FFI::Buffer.new(:size_t)
  j_min = FFI::Buffer.new(:size_t)
  @backend.gsl_matrix_min_index(@ptr, i_min, j_min)
  return [i_min[0].get_ulong(0), j_min[0].get_ulong(0)]
end

#minmaxObject

Same as Array#minmax



349
350
351
352
353
354
# File 'lib/gslng/matrix.rb', line 349

def minmax
  min = FFI::Buffer.new(:double)
  max = FFI::Buffer.new(:double)
  @backend.gsl_matrix_minmax(@ptr, min, max)
  return [min[0].get_float64(0),max[0].get_float64(0)]
end

#minmax_indexObject

Same as #minmax, but returns the indices to the i-th and j-th min, and i-th and j-th max.



357
358
359
360
361
362
363
364
365
# File 'lib/gslng/matrix.rb', line 357

def minmax_index
  i_min = FFI::Buffer.new(:size_t)
  j_min = FFI::Buffer.new(:size_t)
  i_max = FFI::Buffer.new(:size_t)
  j_max = FFI::Buffer.new(:size_t)
  @backend.gsl_matrix_minmax_index(@ptr, i_min, j_min, i_max, j_max)
  #return [min[0].get_size_t(0),max[0].get_size_t(0)]
  return [i_min[0].get_ulong(0),j_min[0].get_ulong(0),i_max[0].get_ulong(0),j_max[0].get_ulong(0)]
end

#multiply!(other) ⇒ Matrix Also known as: mul!

Multiply (element-by-element) other with self

Returns:



237
238
239
240
241
242
243
244
245
246
# File 'lib/gslng/matrix.rb', line 237

def multiply!(other)
  case other
  when Numeric; @backend.gsl_matrix_scale(@ptr, other.to_f)
  when Matrix; @backend.gsl_matrix_mul_elements(@ptr, other.ptr)
  else
    x,y = other.coerce(self)
    x.multiply!(y)
  end
  return self
end

#negative?Boolean

if all elements are strictly negative (<0)

Returns:

  • (Boolean)


332
# File 'lib/gslng/matrix.rb', line 332

def negative?; @backend.gsl_matrix_isneg(@ptr) == 1 ? true : false end

#nonnegative?Boolean

if all elements are non-negative (>=0)

Returns:

  • (Boolean)


335
# File 'lib/gslng/matrix.rb', line 335

def nonnegative?; @backend.gsl_matrix_isnonneg(@ptr) == 1 ? true : false end

#plot(with, extra_cmds = '') ⇒ Object

Create a single plot out of the data contained in this Matrix

Parameters:

  • with (String)

    The value of the ‘with’ option in gnuplot’s plot command (i.e.: lines, linespoints, image, etc.)

  • extra_cmds (String) (defaults to: '')

    Other variables/modifiers you can optionally pass to the “plot” command



72
73
74
# File 'lib/gslng/plotter.rb', line 72

def plot(with, extra_cmds = '')
  Plotter.instance.plot(define_plot(with, extra_cmds))
end

#positive?Boolean

if all elements are strictly positive (>0)

Returns:

  • (Boolean)


329
# File 'lib/gslng/matrix.rb', line 329

def positive?; @backend.gsl_matrix_ispos(@ptr) == 1 ? true : false end

#row(*args) ⇒ Matrix

Analogous to #submatrix

Returns:



187
# File 'lib/gslng/matrix.rb', line 187

def row(*args); self.row_view(*args).to_matrix end

#row_vecview(i, offset = 0, size = nil) ⇒ Vector::View

Same as #row_view, but returns a Vector::View

Returns:



191
192
193
194
195
# File 'lib/gslng/matrix.rb', line 191

def row_vecview(i, offset = 0, size = nil)
  size = (@n - offset) if size.nil?
  ptr = @backend.gsl_matrix_row_view(@ptr, i, offset, size)
  Vector::View.new(ptr, self, size)
end

#row_view(i, offset = 0, size = nil) ⇒ Matrix::View

Creates a View for the i-th row

Returns:



183
# File 'lib/gslng/matrix.rb', line 183

def row_view(i, offset = 0, size = nil); self.view(i, offset, 1, (size or (@n - offset))) end

#set(other) ⇒ Object

Copy matrix values from other to self



157
# File 'lib/gslng/matrix.rb', line 157

def set(other); @backend.gsl_matrix_memcpy(@ptr, other.ptr); return self end

#sizeObject

Shorthand for [#rows,#columns]



21
# File 'lib/gslng/matrix.rb', line 21

def size; [ @m, @n ] end

#slide(i, j) ⇒ Object

Discards rows and columns as necessary (fill them with zero), to “slide” the values of the matrix

Parameters:

  • i (Integer)

    If > 0, slides all values to the bottom (adds i rows of zeros at the top). If < 0, slides all values to the top and adds zeros in the bottom.

  • j (Integer)

    Analogous to parameter i, in this case a value < 0 adds zeros to the right (slides to the left), and a value > 0 adds zeros to the left (slides to the right).



321
# File 'lib/gslng/matrix.rb', line 321

def slide(i, j); @backend.gsl_matrix_slide(@ptr, i, j); return self end

#submatrix(*args) ⇒ Matrix

Shorthand for #submatrix_view(..).to_matrix.

Returns:



171
# File 'lib/gslng/matrix.rb', line 171

def submatrix(*args); self.submatrix_view(*args).to_matrix end

#substract!(other) ⇒ Matrix Also known as: sub!

Substract other from self

Returns:



223
224
225
226
227
228
229
230
231
232
# File 'lib/gslng/matrix.rb', line 223

def substract!(other)
  case other
  when Numeric; @backend.gsl_matrix_add_constant(@ptr, -other.to_f)
  when Matrix; @backend.gsl_matrix_sub(@ptr, other.ptr)
  else
    x,y = other.coerce(self)
    x.substract!(y)
  end
  return self
end

#swap_columns(i, j) ⇒ Object

Swap the i-th and j-th columnos



308
# File 'lib/gslng/matrix.rb', line 308

def swap_columns(i, j); @backend.gsl_matrix_swap_columns(@ptr, i, j); return self end

#swap_rowcol(i, j) ⇒ Object

Swap the i-th row with the j-th column. The Matrix must be square.



314
# File 'lib/gslng/matrix.rb', line 314

def swap_rowcol(i, j); @backend.gsl_matrix_swap_rowcol(@ptr, i, j); return self end

#swap_rows(i, j) ⇒ Object

Swap the i-th and j-th rows



311
# File 'lib/gslng/matrix.rb', line 311

def swap_rows(i, j); @backend.gsl_matrix_swap_rows(@ptr, i, j); return self end

#to_aObject

Converts the matrix to an Array (of Arrays).

Examples:

Matrix[[1,2],[2,3]] => [[1.0,2.0],[2.0,3.0]]


480
481
482
# File 'lib/gslng/matrix.rb', line 480

def to_a
  @backend.gsl_matrix_to_a(@ptr_value)
end

#to_sObject

Converts the matrix to a String, separating each element with a space and each row with a ‘;’ and a newline.

Examples:

Matrix[[1,2],[2,3]] => "[1.0 2.0;\n 2.0 3.0]"


464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/gslng/matrix.rb', line 464

def to_s
  s = '['
  @m.times do |i|
    s += ' ' unless i == 0
    @n.times do |j|
      s += (j == 0 ? self[i,j].to_s : ' ' + self[i,j].to_s)
    end
    s += (i == (@m-1) ? ']' : ";\n")
  end

  return s
end

#transposeObject

Returns the transpose of self, in a new matrix



305
# File 'lib/gslng/matrix.rb', line 305

def transpose; matrix = Matrix.new(@n, @m); @backend.gsl_matrix_transpose_memcpy(matrix.ptr, @ptr); return matrix end

#transpose!Object

Transposes in-place. Only for square matrices



302
# File 'lib/gslng/matrix.rb', line 302

def transpose!; @backend.gsl_matrix_transpose(@ptr); return self end

#view(x = 0, y = 0, m = nil, n = nil) ⇒ Matrix::View Also known as: submatrix_view

Create a View from this Matrix. If either m or n are nil, they’re computed from x, y and the Matrix’s #size

Returns:



164
165
166
# File 'lib/gslng/matrix.rb', line 164

def view(x = 0, y = 0, m = nil, n = nil)
  View.new(self, x, y, (m or @m - x), (n or @n - y))
end

#zero!Object

Set all values to zero



151
# File 'lib/gslng/matrix.rb', line 151

def zero!; @backend.gsl_matrix_set_zero(@ptr); return self end

#zero?Boolean

if all elements are zero

Returns:

  • (Boolean)


326
# File 'lib/gslng/matrix.rb', line 326

def zero?; @backend.gsl_matrix_isnull(@ptr) == 1 ? true : false end