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)



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

#-@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

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:



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

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



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

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

#as_arrayObject



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

See Also:



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.

Examples:

Vector[1,2].coerce(5) => [[5.0, 5.0]:Vector, [1.0, 2.0]: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

See Also:



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

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)


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

Yields:

  • (elem, i)

See Also:



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

Returns:

  • (Boolean)


549
550
551
# File 'lib/gslng/vector.rb', line 549

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



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

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

#join(sep = $,) ⇒ String

Returns:

  • (String)

See Also:

  • Array#join


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

See Also:



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

Yields:

  • (elem)

Returns:



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

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

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

See Also:



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

Yields:

  • (i)

Returns:

See Also:



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

Yields:

  • (i)

Returns:

See Also:



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.

Yields:

  • (i)


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_oldObject



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

alias_method :map_old, :map

#maxObject

Return maximum element of vector



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

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

#max_indexObject

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

#meanObject

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

#medianObject

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

#minObject

Return minimum element of vector



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

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

#min_indexObject

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

#minmaxObject

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_indexObject

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

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)


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)

Returns:

  • (Boolean)


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

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)


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!

Parameters:

  • f (Float)

    A number between 0 and 1 indicating the percentile



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

#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:



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

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.



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

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:



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

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:



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_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



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

See Also:



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

#transposeMatrix Also known as: to_column

Create a column matrix from this vector

Returns:



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

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)



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

#wObject

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.

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#



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

#yObject

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

#zObject

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

Returns:

  • (Boolean)


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

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