ruby-gr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GRCommons
4
+ module AttachFunction
5
+ def attach_function(name, *args)
6
+ @ffi_methods ||= []
7
+ @ffi_methods << name
8
+ super(name, *args)
9
+ end
10
+
11
+ attr_reader :ffi_methods
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GRCommons
4
+ module DefineMethods
5
+ private
6
+
7
+ def define_ffi_methods(ffi_class, prefix: '', default_type: :double)
8
+ ffi_class.ffi_methods.each do |method|
9
+ # delete_prefix (Ruby >= 2.5)
10
+ method_name = method.to_s.sub(/^#{prefix}/, '')
11
+
12
+ define_method(method_name) do |*args|
13
+ args.map! do |arg|
14
+ case arg
15
+ when Array
16
+ send(default_type, arg)
17
+ when ->(x) { narray? x }
18
+ send(default_type, arg)
19
+ else
20
+ arg
21
+ end
22
+ end
23
+ ffi_class.send(method, *args)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GRCommons
4
+ module GRCommonUtils
5
+ private
6
+
7
+ def equal_length(*args)
8
+ lengths = args.map { |arg| length(arg) }
9
+ raise ArgumentError, 'Sequences must have same length.' unless lengths.all? { |l| l == lengths[0] }
10
+
11
+ lengths[0]
12
+ end
13
+
14
+ def length(pt, dtype = :double)
15
+ case pt
16
+ when Array
17
+ pt.size
18
+ when ->(x) { narray? x }
19
+ pt.size
20
+ when ::FFI::MemoryPointer
21
+ case dtype
22
+ when :int
23
+ pt.size / ::FFI::Type::INT.size
24
+ when :double
25
+ pt.size / ::FFI::Type::DOUBLE.size
26
+ else
27
+ raise "Unknown type: #{dtype}"
28
+ end
29
+ else
30
+ raise
31
+ end
32
+ end
33
+
34
+ def uint8(data)
35
+ data = data.to_a.flatten
36
+ pt = ::FFI::MemoryPointer.new(:uint8, data.size)
37
+ pt.write_array_of_uint8 data
38
+ end
39
+
40
+ def int(data)
41
+ data = data.to_a.flatten
42
+ pt = ::FFI::MemoryPointer.new(:int, data.size)
43
+ pt.write_array_of_int data
44
+ end
45
+
46
+ def double(data)
47
+ data = data.to_a.flatten
48
+ pt = ::FFI::MemoryPointer.new(:double, data.size)
49
+ pt.write_array_of_double data
50
+ end
51
+
52
+ def float(data)
53
+ data = data.to_a.flatten
54
+ pt = ::FFI::MemoryPointer.new(:float, data.size)
55
+ pt.write_array_of_float data
56
+ end
57
+
58
+ def narray?(data)
59
+ defined?(Numo::NArray) && data.is_a?(Numo::NArray)
60
+ end
61
+
62
+ def inquiry_int(&block)
63
+ inquiry(:int, &block)
64
+ end
65
+
66
+ def inquiry_double(&block)
67
+ inquiry(:double, &block)
68
+ end
69
+
70
+ def inquiry(types)
71
+ case types
72
+ when Hash, Symbol
73
+ pt = create_ffi_pointer(types)
74
+ yield(pt)
75
+ read_ffi_pointer(pt, types)
76
+ when Array
77
+ pts = types.map { |type| create_ffi_pointer(type) }
78
+ yield(*pts)
79
+ pts.zip(types).map { |pt, type| read_ffi_pointer(pt, type) }
80
+ else
81
+ raise ArgumentError
82
+ end
83
+ end
84
+
85
+ def create_ffi_pointer(type)
86
+ case type
87
+ when Hash
88
+ typ = type.keys[0]
89
+ len = type.values[0]
90
+ ::FFI::MemoryPointer.new(typ, len)
91
+ else
92
+ ::FFI::MemoryPointer.new(type)
93
+ end
94
+ end
95
+
96
+ def read_ffi_pointer(pt, type)
97
+ case type
98
+ when Hash
99
+ typ = type.keys[0]
100
+ len = type.values[0]
101
+ pt.send("read_array_of_#{typ}", len)
102
+ else
103
+ pt.send("read_#{type}")
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GRCommons
4
+ module JupyterSupport
5
+ # For IRuby Notebook
6
+
7
+ if defined? IRuby
8
+ def self.extended(_obj)
9
+ require 'tempfile'
10
+ ENV['GKSwstype'] = 'svg'
11
+ # May be extended to both GR3 and GR
12
+ ENV['GKS_FILEPATH'] = Tempfile.open(['plot', '.svg']).path
13
+ end
14
+
15
+ def show
16
+ emergencyclosegks
17
+ sleep 0.5
18
+ svg = File.read(ENV['GKS_FILEPATH'])
19
+ IRuby.display(svg, mime: 'image/svg+xml')
20
+ nil
21
+ end
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-gr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kojix2
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: numo-narray
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: GR framework - the graphics library for visualisation - for Ruby
112
+ email: 2xijok@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - LICENSE.txt
118
+ - README.md
119
+ - lib/gr.rb
120
+ - lib/gr/ffi.rb
121
+ - lib/gr/grbase.rb
122
+ - lib/gr3.rb
123
+ - lib/gr3/ffi.rb
124
+ - lib/gr3/gr3base.rb
125
+ - lib/gr_commons.rb
126
+ - lib/gr_commons/attach_function.rb
127
+ - lib/gr_commons/define_methods.rb
128
+ - lib/gr_commons/gr_common_utils.rb
129
+ - lib/gr_commons/jupyter_support.rb
130
+ homepage: https://github.com/kojix2/GR.rb
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '2.3'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubygems_version: 3.0.3
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: GR for Ruby
153
+ test_files: []