Class: GSLng::Matrix
- Inherits:
-
Object
- Object
- GSLng::Matrix
- 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
Defined Under Namespace
Classes: View
Instance Attribute Summary collapse
-
#m ⇒ Object
(also: #height, #rows)
readonly
Returns the value of attribute m.
-
#n ⇒ Object
(also: #width, #columns)
readonly
Returns the value of attribute n.
- #ptr ⇒ Object readonly
- #ptr_value ⇒ Object readonly
Constructors collapse
-
.[](*args) ⇒ Object
Create a Matrix from an Array/Array of Arrays/Range.
-
.from_array(array) ⇒ Object
Create a matrix from an Array.
-
.random(m, n) ⇒ Object
(also: rand)
Generates a Matrix of m by n, of random numbers between 0 and 1.
- .release(ptr) ⇒ Object
-
.zero(m, n) ⇒ Object
Same as Matrix.new(m, n, true).
-
#initialize(m, n, zero = false) ⇒ Matrix
constructor
Create a Matrix of m-by-n (rows and columns).
- #initialize_copy(other) ⇒ Object
Setting/getting values collapse
-
#[](i, j = :*) ⇒ Numeric, Matrix
Access the element (i,j), which means (row,column).
-
#[]=(i, j, value) ⇒ Object
Set the element (i,j), which means (row,column).
-
#all!(v) ⇒ Object
(also: #set!, #fill!)
Set all values to v.
-
#identity ⇒ Object
Set the identity matrix values.
-
#set(other) ⇒ Object
Copy matrix values from
other
toself
. -
#zero! ⇒ Object
Set all values to zero.
Views collapse
-
#column(*args) ⇒ Matrix
Analogous to #submatrix.
-
#column_vecview(i, offset = 0, size = nil) ⇒ Vector::View
Same as #column_view, but returns a Vector::View.
-
#column_view(i, offset = 0, size = nil) ⇒ Matrix::View
Creates a View for the i-th column.
-
#row(*args) ⇒ Matrix
Analogous to #submatrix.
-
#row_vecview(i, offset = 0, size = nil) ⇒ Vector::View
Same as #row_view, but returns a Vector::View.
-
#row_view(i, offset = 0, size = nil) ⇒ Matrix::View
Creates a View for the i-th row.
-
#submatrix(*args) ⇒ Matrix
Shorthand for #submatrix_view(..).to_matrix.
-
#view(x = 0, y = 0, m = nil, n = nil) ⇒ Matrix::View
(also: #submatrix_view)
Create a View from this Matrix.
Operators collapse
-
#*(other) ⇒ Object
Matrix Product.
-
#+(other) ⇒ Object
Element-by-element addition.
-
#-(other) ⇒ Object
Element-by-element substraction.
-
#/(other) ⇒ Object
Element-by-element division.
-
#^(other) ⇒ Object
(also: #multiply, #mul)
Element-by-element product.
-
#add!(other) ⇒ Matrix
Add other to self.
-
#divide!(other) ⇒ Matrix
(also: #div!)
Divide (element-by-element) self by other.
-
#multiply!(other) ⇒ Matrix
(also: #mul!)
Multiply (element-by-element) other with self.
-
#substract!(other) ⇒ Matrix
(also: #sub!)
Substract other from self.
Row/column swapping collapse
-
#slide(i, j) ⇒ Object
Discards rows and columns as necessary (fill them with zero), to “slide” the values of the matrix.
-
#swap_columns(i, j) ⇒ Object
Swap the i-th and j-th columnos.
-
#swap_rowcol(i, j) ⇒ Object
Swap the i-th row with the j-th column.
-
#swap_rows(i, j) ⇒ Object
Swap the i-th and j-th rows.
-
#transpose ⇒ Object
Returns the transpose of self, in a new matrix.
-
#transpose! ⇒ Object
Transposes in-place.
Predicate methods collapse
-
#column? ⇒ Boolean
If this is a column Matrix.
-
#negative? ⇒ Boolean
if all elements are strictly negative (<0).
-
#nonnegative? ⇒ Boolean
if all elements are non-negative (>=0).
-
#positive? ⇒ Boolean
if all elements are strictly positive (>0).
-
#zero? ⇒ Boolean
if all elements are zero.
Minimum/maximum collapse
-
#max ⇒ Object
Maximum element of the Matrix.
-
#max_index ⇒ Object
Same as #max, but returns the indices to the i-th and j-th maximum elements.
-
#min ⇒ Object
Minimum element of the Matrix.
-
#min_index ⇒ Object
Same as #min, but returns the indices to the i-th and j-th minimum elements.
-
#minmax ⇒ Object
Same as Array#minmax.
-
#minmax_index ⇒ Object
Same as #minmax, but returns the indices to the i-th and j-th min, and i-th and j-th max.
High-order methods collapse
-
#each(block = Proc.new) {|elem| ... } ⇒ void
Calls the block on each element of the matrix.
-
#each_column {|view| ... } ⇒ Object
Yields the block for each column view (View).
-
#each_row {|view| ... } ⇒ Object
Yields the block for each row view (View).
-
#each_vec_column {|vector_view| ... } ⇒ Object
Same as #each_column, but yields Vector::View‘s.
-
#each_vec_row {|vector_view| ... } ⇒ Object
Same as #each_row, but yields Vector::View‘s.
- #each_with_index(block = Proc.new) {|elem, i, j| ... } ⇒ Object
- #map(block = Proc.new) {|elem| ... } ⇒ Matrix
-
#map!(block = Proc.new) {|elem| ... } ⇒ Object
Efficient #map! implementation.
- #map_array(block = Proc.new) {|elem| ... } ⇒ Array
-
#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.
-
#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.
Type conversions collapse
-
#coerce(other) ⇒ Object
Coerces other to be of Matrix class.
- #inspect ⇒ Object
-
#join(sep = $,) ⇒ Object
Same as Array#join.
-
#to_a ⇒ Object
Converts the matrix to an Array (of Arrays).
-
#to_s ⇒ Object
Converts the matrix to a String, separating each element with a space and each row with a ‘;’ and a newline.
Equality collapse
-
#==(other) ⇒ Object
Element-by-element comparison.
FFI collapse
-
#data_ptr ⇒ Object
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.
Instance Method Summary collapse
-
#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.
-
#plot(with, extra_cmds = '') ⇒ Object
Create a single plot out of the data contained in this Matrix.
- #size ⇒ Object
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
#m ⇒ Object (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 |
#n ⇒ Object (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 |
#ptr ⇒ Object (readonly)
12 13 14 |
# File 'lib/gslng/matrix.rb', line 12 def ptr @ptr end |
#ptr_value ⇒ Object (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
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
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 |
Instance Method Details
#*(other) ⇒ Object
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).
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.
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).
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
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
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
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
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
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_ptr ⇒ Object
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
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
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
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).
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).
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
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
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
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 |
#identity ⇒ Object
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 |
#inspect ⇒ Object
484 485 486 |
# File 'lib/gslng/matrix.rb', line 484 def inspect # @private "#{self}:Matrix" end |
#join(sep = $,) ⇒ Object
Same as Array#join
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
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
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
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.
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
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 |
#max ⇒ Object
Maximum element of the Matrix
343 |
# File 'lib/gslng/matrix.rb', line 343 def max; @backend.gsl_matrix_max(@ptr) end |
#max_index ⇒ Object
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 |
#min ⇒ Object
Minimum element of the Matrix
346 |
# File 'lib/gslng/matrix.rb', line 346 def min; @backend.gsl_matrix_min(@ptr) end |
#min_index ⇒ Object
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 |
#minmax ⇒ Object
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_index ⇒ Object
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
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)
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)
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
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)
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
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
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
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 |
#size ⇒ Object
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
321 |
# File 'lib/gslng/matrix.rb', line 321 def (i, j); @backend.(@ptr, i, j); return self end |
#submatrix(*args) ⇒ Matrix
Shorthand for #submatrix_view(..).to_matrix.
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
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_a ⇒ Object
Converts the matrix to an Array (of Arrays).
480 481 482 |
# File 'lib/gslng/matrix.rb', line 480 def to_a @backend.gsl_matrix_to_a(@ptr_value) end |
#to_s ⇒ Object
Converts the matrix to a String, separating each element with a space and each row with a ‘;’ and a newline.
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 |
#transpose ⇒ Object
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
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
326 |
# File 'lib/gslng/matrix.rb', line 326 def zero?; @backend.gsl_matrix_isnull(@ptr) == 1 ? true : false end |