Class: GSLng::Vector

Inherits:
Object
  • Object
show all
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

View

Defined Under Namespace

Classes: View

Instance Attribute Summary collapse

Constructors collapse

Operators collapse

Other mathematical operations collapse

Miscelaneous methods collapse

Setting/getting values collapse

Views collapse

2D/3D/4D utility vectors collapse

Predicate methods collapse

Minimum/Maximum collapse

Statistics collapse

High-order methods collapse

Type conversions collapse

Equality test collapse

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

#ptrObject (readonly)

Internal FFI::Pointer that wraps the internal gsl_vector*


18
19
20
# File 'lib/gslng/vector.rb', line 18

def ptr
  @ptr
end

#ptr_valueObject (readonly)


19
20
21
# File 'lib/gslng/vector.rb', line 19

def ptr_value
  @ptr_value
end

#sizeObject (readonly)

Returns the value of attribute size


17
18
19
# File 'lib/gslng/vector.rb', line 17

def size
  @size
end

#strideObject (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

Examples:

Vector[1,2,3]
Vector[1..3]

See Also:

  • Vector::from_array

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

.release(ptr) ⇒ Object


46
47
48
# File 'lib/gslng/vector.rb', line 46

def Vector.release(ptr) # @private
  GSLng.backend.gsl_vector_free(ptr)
end

.zero(n) ⇒ Object

Same as Vector.new(n, true)


51
# File 'lib/gslng/vector.rb', line 51

def Vector.zero(n); Vector.new(n, true) end

Instance Method Details

#*(other) ⇒ Object

Element-by-element product

Examples:

Vector[1,2,3] * 2 => [2.0, 4.0, 6.0]:Vector
Vector[1,2,3] * Vector[0,1,2] => [0.0, 2.0, 6.0]:Vector    

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

#[email protected]Object

Invert sign on all elements


165
# File 'lib/gslng/vector.rb', line 165

def [email protected]; self.map!(&:[email protected]) 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


313
# File 'lib/gslng/vector.rb', line 313

def <(other); (other - self).positive? end

#<=(other) ⇒ Object

If each element of self is less-or-equal than other's elements


319
# File 'lib/gslng/vector.rb', line 319

def <=(other); (other - self).nonnegative? end

#==(other) ⇒ Object

Element-by-element comparison. Admits comparing to Array.


545
546
547
548
549
550
551
# File 'lib/gslng/vector.rb', line 545

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


316
# File 'lib/gslng/vector.rb', line 316

def >(other); (other - self).negative? end

#>=(other) ⇒ Object

If each element of self is less-or-equal than other's elements


322
# File 'lib/gslng/vector.rb', line 322

def >=(other); (self - other).nonnegative? end

#[](index) ⇒ Object

TODO:

support ranges

Access the i-th element. If index is negative, it counts from the end (-1 is the last element).

Raises:

  • (RuntimeError)

    if out-of-bounds


233
234
235
# File 'lib/gslng/vector.rb', line 233

def [](index)
  @backend.gsl_vector_get_operator(@ptr_value, index)
end

#[]=(index, value) ⇒ Object

TODO:

support ranges

Set the i-th element. If index is negative, it counts from the end (-1 is the last element).

Raises:

  • (RuntimeError)

    if out-of-bounds


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

See Also:


399
400
401
402
# File 'lib/gslng/vector.rb', line 399

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

Returns:


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


247
# File 'lib/gslng/vector.rb', line 247

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

#as_arrayFFI::Pointer Also known as: data_ptr

Exposes underlying double* pointer, as a FFI::Pointer object, which can be passed to other libraries which use FFI. If you use a C Ruby extension, you can convert this using #to_i to a number which can be casted to a pointer in C.

Returns:

  • (FFI::Pointer)

519
520
521
# File 'lib/gslng/vector.rb', line 519

def as_array
  @backend.gsl_vector_as_array(@ptr)
end

#autocorrelation(mean = nil) ⇒ Object

Compute the autocorrelation of the vector

See Also:


419
420
421
422
# File 'lib/gslng/vector.rb', line 419

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


255
# File 'lib/gslng/vector.rb', line 255

def basis!(i); @backend.gsl_vector_set_basis(@ptr, i); return self end

#coerce(other) ⇒ Object

Coerces other to be a Vector.

Examples:

Vector[1,2].coerce(5) => [[5.0, 5.0]:Vector, [1.0, 2.0]:Vector]

488
489
490
491
492
493
494
495
496
497
# File 'lib/gslng/vector.rb', line 488

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


432
433
434
# File 'lib/gslng/vector.rb', line 432

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

See Also:


426
427
428
429
# File 'lib/gslng/vector.rb', line 426

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

Returns:


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)

Examples:

Vector[1,2,3] ^ Vector[0,1,2] => 8.0

Returns:

  • (Float)

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

Yields:

  • (elem)

439
440
441
# File 'lib/gslng/vector.rb', line 439

def each(block = Proc.new)
  @backend.gsl_vector_each(@ptr_value, &block)
end

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

Yields:

  • (elem, i)

See Also:


445
446
447
# File 'lib/gslng/vector.rb', line 445

def each_with_index(block = Proc.new)
  @backend.gsl_vector_each_with_index(@ptr_value, &block)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

553
554
555
# File 'lib/gslng/vector.rb', line 553

def eql?(other)
  @backend.gsl_vector_eql?(@ptr_value, other.ptr_value)
end

#hashObject

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

#inspectObject


506
507
508
# File 'lib/gslng/vector.rb', line 506

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

#join(sep = $,) ⇒ String

Returns:

  • (String)

See Also:

  • Array#join

477
478
479
480
481
482
483
# File 'lib/gslng/vector.rb', line 477

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

See Also:


412
413
414
415
# File 'lib/gslng/vector.rb', line 412

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

Yields:

  • (elem)

Returns:


471
# File 'lib/gslng/vector.rb', line 471

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

#map!(block = Proc.new) ⇒ Object

See Also:


450
# File 'lib/gslng/vector.rb', line 450

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

Yields:

  • (i)

Returns:

See Also:


467
# File 'lib/gslng/vector.rb', line 467

def map_array(block = Proc.new); self.map_old(&block); end

#map_index(block = Proc.new) {|i| ... } ⇒ Vector

Yields:

  • (i)

Returns:

See Also:


459
# File 'lib/gslng/vector.rb', line 459

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.

Yields:

  • (i)

454
# File 'lib/gslng/vector.rb', line 454

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

#map_oldObject


461
# File 'lib/gslng/vector.rb', line 461

alias_method :map_old, :map

#maxObject

Return maximum element of vector


327
# File 'lib/gslng/vector.rb', line 327

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

#max_indexObject

Same as #max, but returns the index to the element


353
# File 'lib/gslng/vector.rb', line 353

def max_index; @backend.gsl_vector_max_index(@ptr) end

#meanObject

Compute the mean of the vector


358
# File 'lib/gslng/vector.rb', line 358

def mean; @backend.gsl_stats_mean(self.as_array, self.stride, self.size) end

#medianObject

Compute the median of the vector Note it assumes sorted data!


362
# File 'lib/gslng/vector.rb', line 362

def median; @backend.gsl_stats_median_from_sorted_data(self.as_array, self.stride, self.size) end

#minObject

Return minimum element of vector


330
# File 'lib/gslng/vector.rb', line 330

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

#min_indexObject

Same as #min, but returns the index to the element


350
# File 'lib/gslng/vector.rb', line 350

def min_index; @backend.gsl_vector_min_index(@ptr) end

#minmaxObject

Same as Array#minmax


333
334
335
336
337
338
# File 'lib/gslng/vector.rb', line 333

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_indexObject

Same as #minmax, but returns the indices to the elements


341
342
343
344
345
346
347
# File 'lib/gslng/vector.rb', line 341

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

Returns:


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)

Returns:

  • (Boolean)

307
# File 'lib/gslng/vector.rb', line 307

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

#nonnegative?Boolean

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

Returns:

  • (Boolean)

310
# File 'lib/gslng/vector.rb', line 310

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

#normObject 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)

Returns:

  • (Boolean)

304
# File 'lib/gslng/vector.rb', line 304

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!

Parameters:

  • f (Float)

    A number between 0 and 1 indicating the percentile


367
# File 'lib/gslng/vector.rb', line 367

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


405
406
407
408
# File 'lib/gslng/vector.rb', line 405

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

#sortObject


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

See Also:


382
383
384
385
386
387
388
# File 'lib/gslng/vector.rb', line 382

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

Returns:


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.


276
# File 'lib/gslng/vector.rb', line 276

def subvector(*args); subvector_view(*args).to_vector end

#sumObject

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_aArray

Returns:


511
512
513
# File 'lib/gslng/vector.rb', line 511

def to_a
  @backend.gsl_vector_to_a(@ptr_value)
end

#to_matrixMatrix Also known as: to_row

Create a row matrix from this vector

Returns:


526
527
528
529
530
# File 'lib/gslng/vector.rb', line 526

def to_matrix
  m = Matrix.new(1, @size)
  @backend.gsl_matrix_set_row(m.ptr, 0, @ptr)
  return m
end

#to_sString

Returns same format as Array#to_s.

Examples:

Vector[1,2,3].to_s => "[1.0, 2.0, 3.0]"

Returns:

  • (String)

    same format as Array#to_s


502
503
504
# File 'lib/gslng/vector.rb', line 502

def to_s
  "[" + self.join(', ') + "]"
end

#total_sum_squares(mean = nil) ⇒ Object

Compute the total sum of squares of the vector

See Also:


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

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

#transposeMatrix Also known as: to_column

Create a column matrix from this vector

Returns:


535
536
537
538
539
# File 'lib/gslng/vector.rb', line 535

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

Parameters:

  • mean (Float) (defaults to: nil)

    Optionally supply the mean if you already computed it previously with GSLng::Vector.selfself#mean

  • fixed_mean (Boolean) (defaults to: false)

    If true, the passed mean is taken to be known a priori (see GSL documentation)


372
373
374
375
376
377
378
# File 'lib/gslng/vector.rb', line 372

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


261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/gslng/vector.rb', line 261

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

  view_ptr = @backend.gsl_vector_subvector2(@ptr, offset, stride, size)
  View.new(self, view_ptr, size, stride)
end

#wObject

Same as Vector#


287
# File 'lib/gslng/vector.rb', line 287

def w; @backend.gsl_vector_get(@ptr, 3) end

#w=(v) ⇒ Object

Same as Vector#=


296
# File 'lib/gslng/vector.rb', line 296

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.

Examples:

Assuming that v = Vector

v.wrap(5) => [1.0 0.0 -1.0]:Vector
v => [-3.0 2.0 3.0]:Vector

Parameters:

Returns:

  • (Vector)

    a vector of values -1, 1 or 0, if (max-min) was substracted, added to the coordinate,


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

#xObject

Same as Vector#


281
# File 'lib/gslng/vector.rb', line 281

def x; @backend.gsl_vector_get(@ptr, 0) end

#x=(v) ⇒ Object

Same as Vector#=


290
# File 'lib/gslng/vector.rb', line 290

def x=(v); @backend.gsl_vector_set(@ptr, 0, v.to_f) end

#yObject

Same as Vector#


283
# File 'lib/gslng/vector.rb', line 283

def y; @backend.gsl_vector_get(@ptr, 1) end

#y=(v) ⇒ Object

Same as Vector#=


292
# File 'lib/gslng/vector.rb', line 292

def y=(v); @backend.gsl_vector_set(@ptr, 1, v.to_f) end

#zObject

Same as Vector#


285
# File 'lib/gslng/vector.rb', line 285

def z; @backend.gsl_vector_get(@ptr, 2) end

#z=(v) ⇒ Object

Same as Vector#=


294
# File 'lib/gslng/vector.rb', line 294

def z=(v); @backend.gsl_vector_set(@ptr, 2, v.to_f) end

#zero!Object

Set all values to zero


252
# File 'lib/gslng/vector.rb', line 252

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

#zero?Boolean

if all elements are zero

Returns:

  • (Boolean)

301
# File 'lib/gslng/vector.rb', line 301

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