File: complex.rb

package info (click to toggle)
ruby-gsl 2.1.0.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,988 kB
  • sloc: ansic: 62,105; ruby: 15,859; sh: 19; makefile: 10
file content (51 lines) | stat: -rwxr-xr-x 901 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
#!/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
# Create a Vector::Complex of 5 elements, all 0+0i
v = GSL::Vector::Complex.alloc(5)

# Set element 2 to 3+4i
v[2] = [3, 4]

# Show vector
v

# Show element 2
v[2]

# Use #map! to modify each element of vector in-place
i = 0
v.map! do |elm|
  i += 1
  elm += i
end

# Show vector
v

# Show element 3
v[3]

# Set all elements to 2+4.7i
v.set_all([2, 4.7])

# Create subvector starting at element 1 and spanning 3 elements
v2 = v.subvector(1, 3)

# Show size of subvector
v2.size

# Get a Vector::View of the real components of Vector::Complex v
p v.real

# Convert v to an Array
v.to_a
END