Class: GSLng::Vector
- Inherits:
-
Object
- Object
- GSLng::Vector
- Includes:
- Enumerable
- Defined in:
- lib/gslng/vector.rb,
lib/gslng/vector_view.rb
Overview
A fixed-size n-dimensional vector.
Notes
-
#each, #map and similar methods are implemented with C versions which should be fast.
-
#map returns a Vector, not an Array. Use #map_array for that.
-
While this class includes Enumerable, certain methods are redefined (like #max and #min) so they use internal GSL methods.
-
Some functions (like #sum, #dot, and others) use BLAS functions (through GSLng’s CBLAS interface).
-
In contrary to Array, operators #[] and #[]= will raise an exception when accessing out-of-bounds elements.
-
Operator #* multiplies two vectors element-by-element. To perform a dot product use the #^ operator instead (or the #dot alias).
-
Operands are coerced to vectors so you can do vector + scalar, etc. (see #coerce)
Direct Known Subclasses
Defined Under Namespace
Classes: View
Instance Attribute Summary collapse
- #ptr ⇒ Object readonly
- #ptr_value ⇒ Object readonly
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#stride ⇒ Object
readonly
Returns the value of attribute stride.
Constructors collapse
-
.[](*args) ⇒ Object
Creates a Vector from an Array or a Range.
-
.from_array(array) ⇒ Object
Create a vector from an Array.
-
.linspace(start, stop, delta) ⇒ Object
Creates a Vector with linearly distributed values between
start
andstop
, separated bydelta
. -
.random(n) ⇒ Object
(also: rand)
Generates a Vector of n random numbers between 0 and 1.
- .release(ptr) ⇒ Object
-
.zero(n) ⇒ Object
Same as Vector.new(n, true).
-
#initialize(n, zero = false) ⇒ Vector
constructor
Create a Vector of size n.
- #initialize_copy(other) ⇒ Object
Operators collapse
-
#*(other) ⇒ Object
Element-by-element product.
-
#+(other) ⇒ Object
Element-by-element addition.
-
#-(other) ⇒ Object
Element-by-element substraction.
-
#-@ ⇒ Object
Invert sign on all elements.
-
#/(other) ⇒ Object
Element-by-element division.
-
#add!(other) ⇒ Vector
Add (element-by-element) other to self.
-
#divide!(other) ⇒ Vector
(also: #div!)
Divide (element-by-element) self by other.
-
#multiply!(other) ⇒ Vector
(also: #mul!)
Multiply (element-by-element) other with self.
-
#substract!(other) ⇒ Vector
(also: #sub!)
Substract (element-by-element) other from self.
Other mathematical operations collapse
-
#dot(other) ⇒ Float
(also: #^)
Dot product between self and other (uses BLAS’s ddot).
-
#mul_add(other, alpha) ⇒ Object
Optimized version of: self += other * alpha (where alpha is a Numeric).
-
#norm ⇒ Object
(also: #length)
Norm 2 of the vector (uses BLAS’s dnrm2).
-
#sum ⇒ Object
Returns the sum of all elements (uses BLAS’s dasum).
Miscelaneous methods collapse
-
#copy(other) ⇒ Object
Copy other’s values into self.
-
#hash ⇒ Object
Compute hash value for this Vector.
-
#reverse! ⇒ Object
Reverse the order of elements.
- #sort ⇒ Object
- #sort! ⇒ Object
-
#swap(i, j) ⇒ Object
Swap the i-th element with the j-th element.
-
#wrap!(up_to) ⇒ Vector
Wraps self into the interval [0,up_to).
Setting/getting values collapse
-
#[](index) ⇒ Object
Access the i-th element.
-
#[]=(index, value) ⇒ Object
Set the i-th element.
Views collapse
-
#all!(v) ⇒ Object
(also: #set!, #fill!)
Set all values to v.
-
#basis!(i) ⇒ Object
Set all values to zero, except the i-th element, which is set to 1.
-
#subvector(*args) ⇒ Object
Shorthand for #subvector_view(..).to_vector.
-
#view(offset = 0, size = nil, stride = 1) ⇒ Object
(also: #subvector_view)
Create a View from this Vector.
-
#zero! ⇒ Object
Set all values to zero.
2D/3D/4D utility vectors collapse
-
#w ⇒ Object
Same as Vector#.
-
#w=(v) ⇒ Object
Same as Vector#=.
-
#x ⇒ Object
Same as Vector#.
-
#x=(v) ⇒ Object
Same as Vector#=.
-
#y ⇒ Object
Same as Vector#.
-
#y=(v) ⇒ Object
Same as Vector#=.
-
#z ⇒ Object
Same as Vector#.
-
#z=(v) ⇒ Object
Same as Vector#=.
Predicate methods collapse
-
#<(other) ⇒ Object
If each element of self is less than other’s elements.
-
#<=(other) ⇒ Object
If each element of self is less-or-equal than other’s elements.
-
#>(other) ⇒ Object
If each element of self is greater than other’s elements.
-
#>=(other) ⇒ Object
If each element of self is less-or-equal than other’s elements.
-
#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
Return maximum element of vector.
-
#max_index ⇒ Object
Same as #max, but returns the index to the element.
-
#min ⇒ Object
Return minimum element of vector.
-
#min_index ⇒ Object
Same as #min, but returns the index to the element.
-
#minmax ⇒ Object
Same as Array#minmax.
-
#minmax_index ⇒ Object
Same as #minmax, but returns the indices to the elements.
Statistics collapse
-
#absolute_deviation(mean = nil) ⇒ Object
Compute the absolute deviation of the vector.
-
#autocorrelation(mean = nil) ⇒ Object
Compute the autocorrelation of the vector.
-
#correlation(other) ⇒ Object
Compute the correlation between self and other.
-
#covariance(other, mean1 = nil, mean2 = nil) ⇒ Object
Compute the covariance between self and other.
-
#kurtosis(mean = nil, sd = nil) ⇒ Object
Compute the kurtosis of the vector.
-
#mean ⇒ Object
Compute the mean of the vector.
-
#median ⇒ Object
Compute the median of the vector Note it assumes sorted data!.
-
#quantile(f) ⇒ Object
Compute the median of the vector Note it assumes sorted data!.
-
#skew(mean = nil, sd = nil) ⇒ Object
Compute the skewness of the vector.
-
#standard_deviation(mean = nil, fixed_mean = false) ⇒ Object
Compute the standard deviation of the vector.
-
#total_sum_squares(mean = nil) ⇒ Object
Compute the total sum of squares of the vector.
-
#variance(mean = nil, fixed_mean = false) ⇒ Object
Compute the variance of the vector.
High-order methods collapse
- #each(block = Proc.new) {|elem| ... } ⇒ Object
- #each_with_index(block = Proc.new) {|elem, i| ... } ⇒ Object
- #map(block = Proc.new) {|elem| ... } ⇒ Vector
- #map!(block = Proc.new) ⇒ Object
-
#map_array(block = Proc.new) {|i| ... } ⇒ Array
Acts like the normal ‘map’ method from Enumerator.
- #map_index(block = Proc.new) {|i| ... } ⇒ Vector
-
#map_index!(block = Proc.new) {|i| ... } ⇒ Object
Similar to #map!, but passes the index to the element instead.
- #map_old ⇒ Object
Type conversions collapse
- #as_array ⇒ Object
-
#coerce(other) ⇒ Object
Coerces other to be a Vector.
- #inspect ⇒ Object
- #join(sep = $,) ⇒ String
- #to_a ⇒ Array
-
#to_matrix ⇒ Matrix
(also: #to_row)
Create a row matrix from this vector.
-
#to_s ⇒ String
Same format as Array#to_s.
-
#transpose ⇒ Matrix
(also: #to_column)
Create a column matrix from this vector.
Equality test collapse
-
#==(other) ⇒ Object
Element-by-element comparison.
- #eql?(other) ⇒ Boolean
Constructor Details
#initialize(n, zero = false) ⇒ Vector
Create a Vector of size n. If zero is true, the vector is initialized with zeros. Otherwise, the vector 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).
26 27 28 29 30 31 32 33 34 |
# File 'lib/gslng/vector.rb', line 26 def initialize(n, zero = false) @backend = GSLng.backend ptr = (zero ? @backend.gsl_vector_calloc(n) : @backend.gsl_vector_alloc(n)) @ptr = FFI::AutoPointer.new(ptr, Vector.method(:release)) @ptr_value = @ptr.to_i @size = n @stride = 1 if (block_given?) then self.map_index!(Proc.new) end end |
Instance Attribute Details
#ptr ⇒ Object (readonly)
18 19 20 |
# File 'lib/gslng/vector.rb', line 18 def ptr @ptr end |
#ptr_value ⇒ Object (readonly)
19 20 21 |
# File 'lib/gslng/vector.rb', line 19 def ptr_value @ptr_value end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
17 18 19 |
# File 'lib/gslng/vector.rb', line 17 def size @size end |
#stride ⇒ Object (readonly)
Returns the value of attribute stride.
17 18 19 |
# File 'lib/gslng/vector.rb', line 17 def stride @stride end |
Class Method Details
.[](*args) ⇒ Object
Creates a Vector from an Array or a Range
72 73 74 75 |
# File 'lib/gslng/vector.rb', line 72 def Vector.[](*args) array = (args.size == 1 && Range === args[0] ? args[0].to_a : args) Vector.from_array(array) end |
.from_array(array) ⇒ Object
Create a vector from an Array.
54 55 56 57 58 59 |
# File 'lib/gslng/vector.rb', line 54 def Vector.from_array(array) if (array.empty?) then raise "Can't create empty vector" end v = Vector.new(array.size) GSLng.backend.gsl_vector_from_array(v.ptr_value, array) return v end |
.linspace(start, stop, delta) ⇒ Object
Creates a Vector with linearly distributed values between start
and stop
, separated by delta
.
62 63 64 65 |
# File 'lib/gslng/vector.rb', line 62 def Vector.linspace(start, stop, delta) if (start > stop || delta <= 0) then raise 'Invalid values' end Vector.new(((stop - start) / delta).floor.to_i + 1) {|i| start + delta * i} end |
.random(n) ⇒ Object Also known as: rand
Generates a Vector of n random numbers between 0 and 1. NOTE: This simply uses Kernel::rand
79 80 81 |
# File 'lib/gslng/vector.rb', line 79 def Vector.random(n) Vector.new(n).map!{|x| Kernel::rand} end |
Instance Method Details
#*(other) ⇒ Object
Element-by-element product
151 152 153 154 155 156 157 158 159 |
# File 'lib/gslng/vector.rb', line 151 def *(other) case other when Numeric; self.dup.mul!(other) when Vector; self.dup.mul!(other) else x,y = other.coerce(self) x * y end end |
#+(other) ⇒ Object
Element-by-element addition
142 |
# File 'lib/gslng/vector.rb', line 142 def +(other); self.dup.add!(other) end |
#-(other) ⇒ Object
Element-by-element substraction
145 |
# File 'lib/gslng/vector.rb', line 145 def -(other); self.dup.sub!(other) end |
#-@ ⇒ Object
Invert sign on all elements
165 |
# File 'lib/gslng/vector.rb', line 165 def -@; self.map!(&:-@) end |
#/(other) ⇒ Object
Element-by-element division
162 |
# File 'lib/gslng/vector.rb', line 162 def /(other); self.dup.div!(other) end |
#<(other) ⇒ Object
If each element of self is less than other’s elements
314 |
# File 'lib/gslng/vector.rb', line 314 def <(other); (other - self).positive? end |
#<=(other) ⇒ Object
If each element of self is less-or-equal than other’s elements
320 |
# File 'lib/gslng/vector.rb', line 320 def <=(other); (other - self).nonnegative? end |
#==(other) ⇒ Object
Element-by-element comparison. Admits comparing to Array.
541 542 543 544 545 546 547 |
# File 'lib/gslng/vector.rb', line 541 def ==(other) if (self.size != other.size) then return false end self.each_with_index do |elem,i| if (elem != other[i]) then return false end end return true end |
#>(other) ⇒ Object
If each element of self is greater than other’s elements
317 |
# File 'lib/gslng/vector.rb', line 317 def >(other); (other - self).negative? end |
#>=(other) ⇒ Object
If each element of self is less-or-equal than other’s elements
323 |
# File 'lib/gslng/vector.rb', line 323 def >=(other); (self - other).nonnegative? end |
#[](index) ⇒ Object
support ranges
Access the i-th element. If index is negative, it counts from the end (-1 is the last element).
233 234 235 |
# File 'lib/gslng/vector.rb', line 233 def [](index) @backend.gsl_vector_get_operator(@ptr_value, index) end |
#[]=(index, value) ⇒ Object
support ranges
Set the i-th element. If index is negative, it counts from the end (-1 is the last element).
241 242 243 244 |
# File 'lib/gslng/vector.rb', line 241 def []=(index, value) @backend.gsl_vector_set_operator(@ptr_value, index, value.to_f) #@backend.gsl_vector_set(@ptr, (index < 0 ? @size + index : index), value.to_f) end |
#absolute_deviation(mean = nil) ⇒ Object
Compute the absolute deviation of the vector
400 401 402 403 |
# File 'lib/gslng/vector.rb', line 400 def absolute_deviation(mean = nil) if (mean.nil?) then @backend.gsl_stats_absdev(self.as_array, self.stride, self.size) else @backend.gsl_stats_absdev_m(self.as_array, self.stride, self.size, mean) end end |
#add!(other) ⇒ Vector
Add (element-by-element) other to self
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/gslng/vector.rb', line 88 def add!(other) case other when Numeric; @backend.gsl_vector_add_constant(@ptr, other.to_f) when Vector; @backend.gsl_vector_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
269 |
# File 'lib/gslng/vector.rb', line 269 def all!(v); @backend.gsl_vector_set_all(@ptr, v); return self end |
#as_array ⇒ Object
516 517 518 |
# File 'lib/gslng/vector.rb', line 516 def as_array # @private @backend.gsl_vector_as_array(@ptr) end |
#autocorrelation(mean = nil) ⇒ Object
Compute the autocorrelation of the vector
420 421 422 423 |
# File 'lib/gslng/vector.rb', line 420 def autocorrelation(mean = nil) if (mean.nil?) then @backend.gsl_stats_lag1_autocorrelation(self.as_array, self.stride, self.size) else @backend.gsl_stats_lag1_autocorrelation(self.as_array, self.stride, self.size, mean) end end |
#basis!(i) ⇒ Object
Set all values to zero, except the i-th element, which is set to 1
277 |
# File 'lib/gslng/vector.rb', line 277 def basis!(i); @backend.gsl_vector_set_basis(@ptr, i); return self end |
#coerce(other) ⇒ Object
Coerces other to be a Vector.
489 490 491 492 493 494 495 496 497 498 |
# File 'lib/gslng/vector.rb', line 489 def coerce(other) case other when Vector [ other, self ] when Numeric [ Vector.new(@size).set!(other), self ] else raise TypeError, "Can't coerce #{other.class} into #{self.class}" end end |
#copy(other) ⇒ Object
Copy other’s values into self
202 |
# File 'lib/gslng/vector.rb', line 202 def copy(other); @backend.gsl_vector_memcpy(@ptr, other.ptr); return self end |
#correlation(other) ⇒ Object
Compute the correlation between self and other
433 434 435 |
# File 'lib/gslng/vector.rb', line 433 def correlation(other) @backend.gsl_stats_correlation(self.as_array, self.stride, other.as_array, other.stride, self.size) end |
#covariance(other, mean1 = nil, mean2 = nil) ⇒ Object
Compute the covariance between self and other. You can optionally pass the mean of both vectors if you already computed them
427 428 429 430 |
# File 'lib/gslng/vector.rb', line 427 def covariance(other, mean1 = nil, mean2 = nil) if (mean1.nil? || mean2.nil?) then @backend.gsl_stats_covariance(self.as_array, self.stride, other.as_array, other.stride, self.size) else @backend.gsl_stats_covariance(self.as_array, self.stride, other.as_array, other.stride, self.size, mean1, mean2) end end |
#divide!(other) ⇒ Vector Also known as: div!
Divide (element-by-element) self by other
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gslng/vector.rb', line 129 def divide!(other) case other when Numeric; @backend.gsl_blas_dscal(1.0 / other, @ptr) when Vector; @backend.gsl_vector_div(@ptr, other.ptr) else x,y = other.coerce(self) x.div!(y) end return self end |
#dot(other) ⇒ Float Also known as: ^
Dot product between self and other (uses BLAS’s ddot)
173 174 175 176 177 |
# File 'lib/gslng/vector.rb', line 173 def dot(other) out = FFI::Buffer.new(:double) @backend.gsl_blas_ddot(@ptr, other.ptr, out) return out[0].get_double(0) end |
#each(block = Proc.new) {|elem| ... } ⇒ Object
440 441 442 |
# File 'lib/gslng/vector.rb', line 440 def each(block = Proc.new) @backend.gsl_vector_each(@ptr_value, &block) end |
#each_with_index(block = Proc.new) {|elem, i| ... } ⇒ Object
446 447 448 |
# File 'lib/gslng/vector.rb', line 446 def each_with_index(block = Proc.new) @backend.gsl_vector_each_with_index(@ptr_value, &block) end |
#eql?(other) ⇒ Boolean
549 550 551 |
# File 'lib/gslng/vector.rb', line 549 def eql?(other) @backend.gsl_vector_eql?(@ptr_value, other.ptr_value) end |
#hash ⇒ Object
Compute hash value for this Vector. Note: this may be a bit inefficient for now
223 224 225 |
# File 'lib/gslng/vector.rb', line 223 def hash self.to_a.hash end |
#initialize_copy(other) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/gslng/vector.rb', line 36 def initialize_copy(other) # @private @backend = GSLng.backend ptr = @backend.gsl_vector_alloc(other.size) @ptr = FFI::AutoPointer.new(ptr, Vector.method(:release)) @ptr_value = @ptr.to_i @size = other.size @stride = 1 @backend.gsl_vector_memcpy(@ptr, other.ptr) end |
#inspect ⇒ Object
507 508 509 |
# File 'lib/gslng/vector.rb', line 507 def inspect # @private "#{self}:Vector" end |
#join(sep = $,) ⇒ String
478 479 480 481 482 483 484 |
# File 'lib/gslng/vector.rb', line 478 def join(sep = $,) s = '' self.each do |e| s += (s.empty?() ? e.to_s : "#{sep}#{e}") end return s end |
#kurtosis(mean = nil, sd = nil) ⇒ Object
Compute the kurtosis of the vector
413 414 415 416 |
# File 'lib/gslng/vector.rb', line 413 def kurtosis(mean = nil, sd = nil) if (mean.nil? || sd.nil?) then @backend.gsl_stats_kurtosis(self.as_array, self.stride, self.size) else @backend.gsl_stats_kurtosis_sd_m(self.as_array, self.stride, self.size, mean, sd) end end |
#map(block = Proc.new) {|elem| ... } ⇒ Vector
472 |
# File 'lib/gslng/vector.rb', line 472 def map(block = Proc.new); self.dup.map!(block) end |
#map!(block = Proc.new) ⇒ Object
451 |
# File 'lib/gslng/vector.rb', line 451 def map!(block = Proc.new); @backend.gsl_vector_map!(@ptr_value, &block); return self end |
#map_array(block = Proc.new) {|i| ... } ⇒ Array
Acts like the normal ‘map’ method from Enumerator
468 |
# File 'lib/gslng/vector.rb', line 468 def map_array(block = Proc.new); self.map_old(&block); end |
#map_index(block = Proc.new) {|i| ... } ⇒ Vector
460 |
# File 'lib/gslng/vector.rb', line 460 def map_index(block = Proc.new); self.dup.map_index!(block) end |
#map_index!(block = Proc.new) {|i| ... } ⇒ Object
Similar to #map!, but passes the index to the element instead.
455 |
# File 'lib/gslng/vector.rb', line 455 def map_index!(block = Proc.new); @backend.gsl_vector_map_index!(@ptr_value, &block); return self end |
#map_old ⇒ Object
462 |
# File 'lib/gslng/vector.rb', line 462 alias_method :map_old, :map |
#max ⇒ Object
Return maximum element of vector
328 |
# File 'lib/gslng/vector.rb', line 328 def max; @backend.gsl_vector_max(@ptr) end |
#max_index ⇒ Object
Same as #max, but returns the index to the element
354 |
# File 'lib/gslng/vector.rb', line 354 def max_index; @backend.gsl_vector_max_index(@ptr) end |
#mean ⇒ Object
Compute the mean of the vector
359 |
# File 'lib/gslng/vector.rb', line 359 def mean; @backend.gsl_stats_mean(self.as_array, self.stride, self.size) end |
#median ⇒ Object
Compute the median of the vector Note it assumes sorted data!
363 |
# File 'lib/gslng/vector.rb', line 363 def median; @backend.gsl_stats_median_from_sorted_data(self.as_array, self.stride, self.size) end |
#min ⇒ Object
Return minimum element of vector
331 |
# File 'lib/gslng/vector.rb', line 331 def min; @backend.gsl_vector_min(@ptr) end |
#min_index ⇒ Object
Same as #min, but returns the index to the element
351 |
# File 'lib/gslng/vector.rb', line 351 def min_index; @backend.gsl_vector_min_index(@ptr) end |
#minmax ⇒ Object
Same as Array#minmax
334 335 336 337 338 339 |
# File 'lib/gslng/vector.rb', line 334 def minmax min = FFI::Buffer.new(:double) max = FFI::Buffer.new(:double) @backend.gsl_vector_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 elements
342 343 344 345 346 347 348 |
# File 'lib/gslng/vector.rb', line 342 def minmax_index min = FFI::Buffer.new(:size_t) max = FFI::Buffer.new(:size_t) @backend.gsl_vector_minmax_index(@ptr, min, max) #return [min[0].get_size_t(0),max[0].get_size_t(0)] return [min[0].get_ulong(0),max[0].get_ulong(0)] end |
#mul_add(other, alpha) ⇒ Object
Optimized version of: self += other * alpha (where alpha is a Numeric). Uses BLAS’s daxpy.
188 |
# File 'lib/gslng/vector.rb', line 188 def mul_add(other, alpha); @backend.gsl_blas_daxpy(alpha, other.ptr, @ptr); return self end |
#multiply!(other) ⇒ Vector Also known as: mul!
Multiply (element-by-element) other with self
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/gslng/vector.rb', line 115 def multiply!(other) case other when Numeric; @backend.gsl_blas_dscal(other.to_f, @ptr) when Vector; @backend.gsl_vector_mul(@ptr, other.ptr) else x,y = other.coerce(self) x.mul!(y) end return self end |
#negative? ⇒ Boolean
if all elements are strictly negative (<0)
308 |
# File 'lib/gslng/vector.rb', line 308 def negative?; @backend.gsl_vector_isneg(@ptr) == 1 ? true : false end |
#nonnegative? ⇒ Boolean
if all elements are non-negative (>=0)
311 |
# File 'lib/gslng/vector.rb', line 311 def nonnegative?; @backend.gsl_vector_isnonneg(@ptr) == 1 ? true : false end |
#norm ⇒ Object Also known as: length
Norm 2 of the vector (uses BLAS’s dnrm2)
181 |
# File 'lib/gslng/vector.rb', line 181 def norm; @backend.gsl_blas_dnrm2(@ptr) end |
#positive? ⇒ Boolean
if all elements are strictly positive (>0)
305 |
# File 'lib/gslng/vector.rb', line 305 def positive?; @backend.gsl_vector_ispos(@ptr) == 1 ? true : false end |
#quantile(f) ⇒ Object
Compute the median of the vector Note it assumes sorted data!
368 |
# File 'lib/gslng/vector.rb', line 368 def quantile(f); @backend.gsl_stats_quantile_from_sorted_data(self.as_array, self.stride, self.size, f) end |
#reverse! ⇒ Object
Reverse the order of elements
193 |
# File 'lib/gslng/vector.rb', line 193 def reverse!; @backend.gsl_vector_reverse(@ptr); return self end |
#skew(mean = nil, sd = nil) ⇒ Object
Compute the skewness of the vector. You can optionally provide the mean and the standard deviation if you already computed them
406 407 408 409 |
# File 'lib/gslng/vector.rb', line 406 def skew(mean = nil, sd = nil) if (mean.nil? || sd.nil?) then @backend.gsl_stats_skew(self.as_array, self.stride, self.size) else @backend.gsl_stats_skew_sd_m(self.as_array, self.stride, self.size, mean, sd) end end |
#sort ⇒ Object
199 |
# File 'lib/gslng/vector.rb', line 199 def sort; self.dup.sort! end |
#sort! ⇒ Object
198 |
# File 'lib/gslng/vector.rb', line 198 def sort!; @backend.gsl_sort_vector(@ptr); return self end |
#standard_deviation(mean = nil, fixed_mean = false) ⇒ Object
Compute the standard deviation of the vector
383 384 385 386 387 388 389 |
# File 'lib/gslng/vector.rb', line 383 def standard_deviation(mean = nil, fixed_mean = false) if (mean.nil?) then @backend.gsl_stats_sd(self.as_array, self.stride, self.size) else if (fixed_mean) then @backend.gsl_stats_sd_with_fixed_mean(self.as_array, self.stride, self.size, mean) else @backend.gsl_stats_sd_m(self.as_array, self.stride, self.size, mean) end end end |
#substract!(other) ⇒ Vector Also known as: sub!
Substract (element-by-element) other from self
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/gslng/vector.rb', line 101 def substract!(other) case other when Numeric; @backend.gsl_vector_add_constant(@ptr, -other.to_f) when Vector; @backend.gsl_vector_sub(@ptr, other.ptr) else x,y = other.coerce(self) x.sub!(y) end return self end |
#subvector(*args) ⇒ Object
Shorthand for #subvector_view(..).to_vector.
266 |
# File 'lib/gslng/vector.rb', line 266 def subvector(*args); subvector_view(*args).to_vector end |
#sum ⇒ Object
Returns the sum of all elements (uses BLAS’s dasum)
185 |
# File 'lib/gslng/vector.rb', line 185 def sum; @backend.gsl_blas_dasum(@ptr) end |
#swap(i, j) ⇒ Object
Swap the i-th element with the j-th element
196 |
# File 'lib/gslng/vector.rb', line 196 def swap(i,j); @backend.gsl_vector_swap_elements(@ptr, i, j); return self end |
#to_a ⇒ Array
512 513 514 |
# File 'lib/gslng/vector.rb', line 512 def to_a @backend.gsl_vector_to_a(@ptr_value) end |
#to_matrix ⇒ Matrix Also known as: to_row
Create a row matrix from this vector
522 523 524 525 526 |
# File 'lib/gslng/vector.rb', line 522 def to_matrix m = Matrix.new(1, @size) @backend.gsl_matrix_set_row(m.ptr, 0, @ptr) return m end |
#to_s ⇒ String
Returns same format as Array#to_s.
503 504 505 |
# File 'lib/gslng/vector.rb', line 503 def to_s "[" + self.join(', ') + "]" end |
#total_sum_squares(mean = nil) ⇒ Object
Compute the total sum of squares of the vector
393 394 395 396 |
# File 'lib/gslng/vector.rb', line 393 def total_sum_squares(mean = nil) if (mean.nil?) then @backend.gsl_stats_tss(self.as_array, self.stride, self.size) else @backend.gsl_stats_tss_m(self.as_array, self.stride, self.size, mean) end end |
#transpose ⇒ Matrix Also known as: to_column
Create a column matrix from this vector
531 532 533 534 535 |
# File 'lib/gslng/vector.rb', line 531 def transpose m = Matrix.new(@size, 1) @backend.gsl_matrix_set_col(m.ptr, 0, @ptr) return m end |
#variance(mean = nil, fixed_mean = false) ⇒ Object
Compute the variance of the vector
373 374 375 376 377 378 379 |
# File 'lib/gslng/vector.rb', line 373 def variance(mean = nil, fixed_mean = false) if (mean.nil?) then @backend.gsl_stats_variance(self.as_array, self.stride, self.size) else if (fixed_mean) then @backend.gsl_stats_variance_with_fixed_mean(self.as_array, self.stride, self.size, mean) else @backend.gsl_stats_variance_m(self.as_array, self.stride, self.size, mean) end end end |
#view(offset = 0, size = nil, stride = 1) ⇒ Object Also known as: subvector_view
Create a View from this Vector. If size is nil, it is computed automatically from offset and stride
250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/gslng/vector.rb', line 250 def view(offset = 0, size = nil, stride = 1) if (stride <= 0) then raise 'stride must be positive' end if (size.nil?) size = @size - offset k,m = size.divmod(stride) size = k + (m == 0 ? 0 : 1) end if (stride == 1) then ptr = @backend.gsl_vector_subvector2(@ptr, offset, size) else ptr = @backend.gsl_vector_subvector_with_stride2(@ptr, offset, stride, size) end View.new(ptr, self, size, stride) end |
#w ⇒ Object
Same as Vector#
288 |
# File 'lib/gslng/vector.rb', line 288 def w; @backend.gsl_vector_get(@ptr, 3) end |
#w=(v) ⇒ Object
Same as Vector#=
297 |
# File 'lib/gslng/vector.rb', line 297 def w=(v); @backend.gsl_vector_set(@ptr, 3, v.to_f) end |
#wrap!(up_to) ⇒ Vector
Wraps self into the interval [0,up_to). NOTE: this value must be > 0 or not modified, respectively.
211 212 213 214 215 216 217 218 219 |
# File 'lib/gslng/vector.rb', line 211 def wrap!(up_to) delta = Vector.new(self.size) self.map_index! do |i| a,b = self[i].divmod(up_to) delta[i] = -a b end return delta end |
#x ⇒ Object
Same as Vector#
282 |
# File 'lib/gslng/vector.rb', line 282 def x; @backend.gsl_vector_get(@ptr, 0) end |
#x=(v) ⇒ Object
Same as Vector#=
291 |
# File 'lib/gslng/vector.rb', line 291 def x=(v); @backend.gsl_vector_set(@ptr, 0, v.to_f) end |
#y ⇒ Object
Same as Vector#
284 |
# File 'lib/gslng/vector.rb', line 284 def y; @backend.gsl_vector_get(@ptr, 1) end |
#y=(v) ⇒ Object
Same as Vector#=
293 |
# File 'lib/gslng/vector.rb', line 293 def y=(v); @backend.gsl_vector_set(@ptr, 1, v.to_f) end |
#z ⇒ Object
Same as Vector#
286 |
# File 'lib/gslng/vector.rb', line 286 def z; @backend.gsl_vector_get(@ptr, 2) end |
#z=(v) ⇒ Object
Same as Vector#=
295 |
# File 'lib/gslng/vector.rb', line 295 def z=(v); @backend.gsl_vector_set(@ptr, 2, v.to_f) end |
#zero! ⇒ Object
Set all values to zero
274 |
# File 'lib/gslng/vector.rb', line 274 def zero!; @backend.gsl_vector_set_zero(@ptr); return self end |
#zero? ⇒ Boolean
if all elements are zero
302 |
# File 'lib/gslng/vector.rb', line 302 def zero?; @backend.gsl_vector_isnull(@ptr) == 1 ? true : false end |