File: set_all.rb

package info (click to toggle)
ruby-gsl 2.1.0.3%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,604 kB
  • sloc: ansic: 62,050; ruby: 15,845; sh: 19; makefile: 10
file content (121 lines) | stat: -rwxr-xr-x 3,254 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env ruby
# Turn on warnings
$-w = true

require 'irb/xmp'
require 'gsl'

# Apparently, IRB::Frame has a bug that prevents the defaults from working, so
# an XMP instance must be created explicitly this way instead of using the
# otherwise convenient xmp method.
XMP.new(IRB::Frame.top(-1)).puts <<END
# These examples show some of the ways that Vector#set or its alias Vector#[]=
# can be invoked.  For a single argument, this is equivalent to Vector#set_all.
# For two arguments with the first a Fixnum i, this sets the i'th element (or
# the (size-i)'th element to the value of the second argument.  All other forms
# treat all but the last argument as with Vector#subvector and set the
# specified elements based on the last argument, which can be a Vector (or
# Vector::View), an Array, a Range, or a Numeric.  Vector, Array, and Range
# rvalues must have the same number of elements as the specified subvector.
# For a Numeric rvalue, all elements of the subvector are set to that value.
#
# Note the different return values of Vector#set and Vector#[]=.  Vector#set
# return self, but Vector[]= return the value to the right of the = sign.  This
# must be standard Ruby behavior since the underlying code returns the same
# value to Ruby regardless of whether it is invoked as #set or #[]=.
#
# Also be careful is setting part of a Vector from another part of the same
# vector.  The GSL method that performs this operation uses memcpy, which does
# not handle overlapping memory regions in a well defined way.  See the last
# two examples.
#
# See examples/vector/view_all.rb for additional examples of how to specify
# subvectors.

# Create test vector v
v = GSL::Vector.indgen(9)

# Vector#set and Vector#[]= with one arg sets all elements
v.set(1.2)
v[] = 3.4
v

# Vector#[i]= Numeric sets the i'th element if i is
# positive or the (size+i)'th element if i is negative.
v[3] = 5.6
v[-8] = 7.8
v

# Specifying subvector using Range with various rvalue types
v[1..4] = GSL::Vector[2, 3, 5, 7] # rvalue is Vector
v

v[1..4] = [11, 13, 17, 19] # rvalue is Array
v

v[1..4] = 24..27 # rvalue is Range
v

v[1..4] = 1.0 # rvalue is Numeric
v

# Specifying subvector using Range and stride with various rvalue types
v[0..4, 2] = GSL::Vector[2, 3, 5] # rvalue is Vector
v

v[0..4, 2] = [7, 11, 13] # rvalue is Array
v

v[0..4, 2] = 8..10 # rvalue is Range
v

v[0..4, 2] = 1.0 # rvalue is Numeric
v

# Specifying subvector using two Fixnums (offset, length) with various rvalue
# types
v[2, 4] = GSL::Vector[2, 3, 5, 7] # rvalue is Vector
v

v[2, 4] = [11, 13, 17, 19] # rvalue is Array
v

v[2, 4] = 24..27 # rvalue is Range
v

v[2, 4] = 1.0 # rvalue is Numeric
v

# Specifying subvector using three Fixnum arguments (offset, stride, length)
# with various rvalue types
v[1, 2, 3] = GSL::Vector[2, 3, 5] # rvalue is Vector
v

v[1, 2, 3] = [7, 11, 13] # rvalue is Array
v

v[1, 2, 3] = 8..10 # rvalue is Range
v

v[1, 2, 3] = 1.0 # rvalue is Numeric
v

# Copying part of a Vector to another part of the same Vector can potentially
# be problematic if the regions overlap.
v.indgen!
v[0, 3] = v[2, 3]
v

v.indgen!
v[2, 3] = v[0, 3]
v

# But it's OK if the regions do not overlap
v.indgen!
v[0, 3] = v[3, 3]
v

v.indgen!
v[3, 3] = v[0, 3]
v
END