vector_number 0.4.3 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +47 -22
- data/doc/vector_space.svg +94 -0
- data/lib/vector_number/comparing.rb +103 -107
- data/lib/vector_number/converting.rb +137 -147
- data/lib/vector_number/enumerating.rb +94 -108
- data/lib/vector_number/math_converting.rb +113 -109
- data/lib/vector_number/mathing.rb +305 -312
- data/lib/vector_number/numeric_refinements.rb +7 -0
- data/lib/vector_number/querying.rb +136 -136
- data/lib/vector_number/special_unit.rb +36 -0
- data/lib/vector_number/stringifying.rb +89 -80
- data/lib/vector_number/version.rb +1 -1
- data/lib/vector_number.rb +155 -126
- data/sig/vector_number.rbs +141 -168
- metadata +14 -11
data/sig/vector_number.rbs
CHANGED
|
@@ -1,57 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
type unit_type = untyped
|
|
6
|
-
type coefficient_type = real_number
|
|
1
|
+
# A class to add together anything.
|
|
2
|
+
class VectorNumber
|
|
3
|
+
# ---- Generic types ----
|
|
4
|
+
type real_number = Integer | Float | Rational | BigDecimal
|
|
7
5
|
|
|
8
|
-
type
|
|
6
|
+
type in_value_type = untyped
|
|
7
|
+
type unit_type = untyped
|
|
8
|
+
type coefficient_type = real_number
|
|
9
9
|
|
|
10
|
-
type
|
|
11
|
-
type units_list_type = list[unit_type]
|
|
12
|
-
type coefficients_list_type = list[coefficient_type]
|
|
13
|
-
type each_value_type = [unit_type, coefficient_type]
|
|
10
|
+
type list[T] = Array[T]
|
|
14
11
|
|
|
15
|
-
type
|
|
12
|
+
type plain_instance = Hash[unit_type, coefficient_type]
|
|
13
|
+
type units_list_type = list[unit_type]
|
|
14
|
+
type coefficients_list_type = list[coefficient_type]
|
|
15
|
+
type each_value_type = [unit_type, coefficient_type]
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
def size: -> Integer
|
|
19
|
-
def options: -> options_type
|
|
17
|
+
# ---- Constants ----
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
def new: () { (coefficient_type value) -> coefficient_type } -> vector_type
|
|
23
|
-
| (in_value_type) { (coefficient_type value) -> coefficient_type } -> vector_type
|
|
24
|
-
| () -> vector_type
|
|
25
|
-
| (in_value_type) -> vector_type
|
|
26
|
-
def real_number?: (real_number value) -> true
|
|
27
|
-
| (vector_type) -> bool
|
|
28
|
-
| (in_value_type value) -> false
|
|
29
|
-
end
|
|
19
|
+
VERSION: String
|
|
30
20
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
include VectorNumber::Mathing
|
|
35
|
-
include VectorNumber::MathConverting
|
|
36
|
-
include VectorNumber::Converting
|
|
37
|
-
include VectorNumber::Enumerating
|
|
38
|
-
include VectorNumber::Comparing
|
|
39
|
-
include VectorNumber::Querying
|
|
40
|
-
include VectorNumber::Stringifying
|
|
21
|
+
NUMERIC_UNITS: list[SpecialUnit]
|
|
22
|
+
R: SpecialUnit
|
|
23
|
+
I: SpecialUnit
|
|
41
24
|
|
|
42
|
-
|
|
25
|
+
# ---- Public methods ----
|
|
43
26
|
|
|
44
|
-
|
|
45
|
-
DEFAULT_OPTIONS: options_type
|
|
27
|
+
def self.[]: (*unit_type values) -> instance
|
|
46
28
|
|
|
47
|
-
|
|
48
|
-
R: Complex
|
|
49
|
-
I: Complex
|
|
29
|
+
def self.numeric_unit?: (unit_type unit) -> bool
|
|
50
30
|
|
|
51
|
-
def
|
|
31
|
+
def size: -> Integer
|
|
52
32
|
|
|
53
33
|
def initialize:
|
|
54
|
-
(?(units_list_type |
|
|
34
|
+
(?(units_list_type | plain_instance | instance)? values)
|
|
55
35
|
?{ (coefficient_type coefficient) -> coefficient_type }
|
|
56
36
|
-> void
|
|
57
37
|
|
|
@@ -63,197 +43,179 @@ class VectorNumber
|
|
|
63
43
|
private
|
|
64
44
|
|
|
65
45
|
@size: Integer
|
|
66
|
-
@
|
|
67
|
-
|
|
46
|
+
@data: plain_instance
|
|
47
|
+
|
|
48
|
+
def new: () { (coefficient_type value) -> coefficient_type } -> instance
|
|
49
|
+
| (in_value_type) { (coefficient_type value) -> coefficient_type } -> instance
|
|
50
|
+
| () -> instance
|
|
51
|
+
| (in_value_type) -> instance
|
|
52
|
+
|
|
53
|
+
def real_number?: (real_number value) -> true
|
|
54
|
+
| (instance) -> bool
|
|
55
|
+
| (in_value_type value) -> false
|
|
68
56
|
|
|
69
|
-
def initialize_from: ((units_list_type |
|
|
57
|
+
def initialize_from: ((units_list_type | plain_instance | instance)? values) -> void
|
|
70
58
|
|
|
71
|
-
def add_value_to_data: ((
|
|
59
|
+
def add_value_to_data: ((instance | Numeric | unit_type) value) -> void
|
|
72
60
|
|
|
73
61
|
def add_numeric_value_to_data: (Numeric value) -> void
|
|
74
62
|
|
|
75
|
-
def add_vector_to_data: ((
|
|
63
|
+
def add_vector_to_data: ((instance | plain_instance) vector) -> void
|
|
76
64
|
|
|
77
65
|
def apply_transform: () ?{ (coefficient_type value) -> coefficient_type } -> void
|
|
78
66
|
|
|
79
|
-
def save_options: (options_type? options, values: in_value_type) -> void
|
|
80
|
-
def merge_options: (options_type base_options, options_type added_options) -> options_type
|
|
81
|
-
def default_options: () -> options_type
|
|
82
|
-
def known_options: () -> list[Symbol]
|
|
83
|
-
|
|
84
67
|
def finalize_contents: () -> void
|
|
68
|
+
end
|
|
85
69
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
def coerce: (in_value_type) -> [vector_type, self]
|
|
91
|
-
|
|
92
|
-
def -@: () -> vector_type
|
|
93
|
-
alias neg -@
|
|
94
|
-
|
|
95
|
-
def +: (in_value_type) -> vector_type
|
|
96
|
-
alias add +
|
|
97
|
-
|
|
98
|
-
def -: (in_value_type) -> vector_type
|
|
99
|
-
alias sub -
|
|
100
|
-
|
|
101
|
-
def *: (real_number | vector_type) -> vector_type
|
|
102
|
-
alias mult *
|
|
70
|
+
# Methods for performing actual math.
|
|
71
|
+
class VectorNumber
|
|
72
|
+
def coerce: (in_value_type) -> [instance, self]
|
|
103
73
|
|
|
104
|
-
|
|
105
|
-
|
|
74
|
+
def -@: () -> instance
|
|
75
|
+
alias neg -@
|
|
106
76
|
|
|
107
|
-
|
|
77
|
+
def +: (in_value_type) -> instance
|
|
78
|
+
alias add +
|
|
108
79
|
|
|
109
|
-
|
|
80
|
+
def -: (in_value_type) -> instance
|
|
81
|
+
alias sub -
|
|
110
82
|
|
|
111
|
-
|
|
112
|
-
|
|
83
|
+
def *: (real_number | instance) -> instance
|
|
84
|
+
alias mult *
|
|
113
85
|
|
|
114
|
-
|
|
86
|
+
def /: (real_number | instance) -> instance
|
|
87
|
+
alias quo /
|
|
115
88
|
|
|
116
|
-
|
|
89
|
+
def fdiv: (real_number | instance) -> instance
|
|
117
90
|
|
|
118
|
-
|
|
91
|
+
def div: (real_number | instance) -> instance
|
|
119
92
|
|
|
120
|
-
|
|
121
|
-
|
|
93
|
+
def %: (real_number | instance) -> instance
|
|
94
|
+
alias modulo %
|
|
122
95
|
|
|
123
|
-
|
|
124
|
-
module MathConverting
|
|
125
|
-
include _BaseMethods
|
|
126
|
-
include Enumerating
|
|
96
|
+
def divmod: (real_number | instance) -> [instance, instance]
|
|
127
97
|
|
|
128
|
-
|
|
98
|
+
def remainder: (real_number | instance) -> instance
|
|
129
99
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
def truncate: (Integer) -> vector_type
|
|
100
|
+
private
|
|
133
101
|
|
|
134
|
-
|
|
102
|
+
def check_divisibility: (real_number | instance) -> void
|
|
103
|
+
end
|
|
135
104
|
|
|
136
|
-
|
|
105
|
+
# Various mathematical operations that are also conversions.
|
|
106
|
+
class VectorNumber
|
|
107
|
+
def abs: () -> Float
|
|
137
108
|
|
|
138
|
-
|
|
139
|
-
end
|
|
109
|
+
def abs2: () -> Float
|
|
140
110
|
|
|
141
|
-
|
|
142
|
-
module Converting
|
|
143
|
-
include _BaseMethods
|
|
144
|
-
include Querying
|
|
111
|
+
def truncate: (?Integer) -> instance
|
|
145
112
|
|
|
146
|
-
|
|
113
|
+
def ceil: (?Integer) -> instance
|
|
147
114
|
|
|
148
|
-
|
|
149
|
-
alias imag imaginary
|
|
115
|
+
def floor: (?Integer) -> instance
|
|
150
116
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
alias to_int to_i
|
|
117
|
+
def round: (?Integer, ?half: (:up | :down | :even)) -> instance
|
|
118
|
+
end
|
|
154
119
|
|
|
155
|
-
|
|
156
|
-
|
|
120
|
+
# Methods for converting to different number classes.
|
|
121
|
+
class VectorNumber
|
|
122
|
+
def real: () -> real_number
|
|
157
123
|
|
|
158
|
-
|
|
159
|
-
|
|
124
|
+
def imaginary: () -> real_number
|
|
125
|
+
alias imag imaginary
|
|
160
126
|
|
|
161
|
-
|
|
162
|
-
|
|
127
|
+
def to_i: () -> Integer
|
|
128
|
+
alias to_int to_i
|
|
163
129
|
|
|
164
|
-
|
|
165
|
-
| () -> void
|
|
130
|
+
def to_f: () -> Float
|
|
166
131
|
|
|
167
|
-
|
|
132
|
+
def to_r: () -> Rational
|
|
168
133
|
|
|
169
|
-
|
|
134
|
+
def to_d: (?Integer?) -> BigDecimal
|
|
170
135
|
|
|
171
|
-
|
|
172
|
-
end
|
|
136
|
+
def to_c: () -> Complex
|
|
173
137
|
|
|
174
|
-
|
|
175
|
-
module Enumerating
|
|
176
|
-
include _BaseMethods
|
|
177
|
-
include _Each[each_value_type]
|
|
178
|
-
include Enumerable[each_value_type]
|
|
138
|
+
private
|
|
179
139
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
| ...
|
|
183
|
-
alias each_pair each
|
|
140
|
+
def raise_convert_error: (Class) -> void
|
|
141
|
+
end
|
|
184
142
|
|
|
185
|
-
|
|
186
|
-
|
|
143
|
+
class VectorNumber
|
|
144
|
+
include _Each[each_value_type]
|
|
145
|
+
include Enumerable[each_value_type]
|
|
187
146
|
|
|
188
|
-
|
|
189
|
-
|
|
147
|
+
def each: () { (unit_type unit, coefficient_type coefficient) -> void } -> self
|
|
148
|
+
| () -> Enumerator[each_value_type, Integer]
|
|
149
|
+
| ...
|
|
150
|
+
alias each_pair each
|
|
190
151
|
|
|
191
|
-
|
|
192
|
-
|
|
152
|
+
def units: () -> units_list_type
|
|
153
|
+
alias keys units
|
|
193
154
|
|
|
194
|
-
|
|
155
|
+
def coefficients: () -> coefficients_list_type
|
|
156
|
+
alias values coefficients
|
|
195
157
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
end
|
|
158
|
+
def to_h: () -> plain_instance
|
|
159
|
+
| () { (unit_type, coefficient_type) -> each_value_type } -> plain_instance
|
|
199
160
|
|
|
200
|
-
|
|
201
|
-
module Comparing
|
|
202
|
-
include _BaseMethods
|
|
203
|
-
include Enumerating
|
|
204
|
-
include Converting
|
|
205
|
-
include Querying
|
|
161
|
+
def []: (unit_type unit) -> coefficient_type
|
|
206
162
|
|
|
207
|
-
|
|
163
|
+
def unit?: (unit_type unit) -> bool
|
|
164
|
+
alias key? unit?
|
|
165
|
+
end
|
|
208
166
|
|
|
209
|
-
|
|
167
|
+
# Methods for comparing with other numbers.
|
|
168
|
+
class VectorNumber
|
|
169
|
+
def ==: (in_value_type other) -> bool
|
|
210
170
|
|
|
211
|
-
|
|
171
|
+
def eql?: (in_value_type other) -> bool
|
|
212
172
|
|
|
213
|
-
|
|
214
|
-
end
|
|
173
|
+
def hash: () -> Integer
|
|
215
174
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
module Querying
|
|
219
|
-
include Enumerating
|
|
175
|
+
def <=>: (in_value_type other) -> Integer?
|
|
176
|
+
end
|
|
220
177
|
|
|
221
|
-
|
|
178
|
+
# Methods for querying state of the number.
|
|
179
|
+
# Mostly modeled after {::Complex}.
|
|
180
|
+
class VectorNumber
|
|
181
|
+
def numeric?: (?Integer) -> bool
|
|
222
182
|
|
|
223
|
-
|
|
183
|
+
def nonnumeric?: (?Integer) -> bool
|
|
224
184
|
|
|
225
|
-
|
|
185
|
+
def finite?: () -> bool
|
|
226
186
|
|
|
227
|
-
|
|
187
|
+
def infinite?: () -> Integer?
|
|
228
188
|
|
|
229
|
-
|
|
189
|
+
def zero?: () -> bool
|
|
230
190
|
|
|
231
|
-
|
|
191
|
+
def nonzero?: () -> self?
|
|
232
192
|
|
|
233
|
-
|
|
193
|
+
def positive?: () -> bool
|
|
234
194
|
|
|
235
|
-
|
|
195
|
+
def negative?: () -> bool
|
|
236
196
|
|
|
237
|
-
|
|
197
|
+
def real?: () -> false
|
|
238
198
|
|
|
239
|
-
|
|
240
|
-
|
|
199
|
+
def integer?: () -> false
|
|
200
|
+
end
|
|
241
201
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
202
|
+
# Methods and options for string representation.
|
|
203
|
+
class VectorNumber
|
|
204
|
+
MULT_STRINGS: Hash[Symbol, String]
|
|
245
205
|
|
|
246
|
-
|
|
206
|
+
def to_s: (?mult: (Symbol | String)) { (unit_type, coefficient_type, ?Integer index, ?String operator) -> String } -> String
|
|
247
207
|
|
|
248
|
-
|
|
208
|
+
def inspect: () -> String
|
|
249
209
|
|
|
250
|
-
|
|
210
|
+
private
|
|
251
211
|
|
|
252
|
-
|
|
212
|
+
def build_string: (String operator) -> String
|
|
213
|
+
| (String operator) { (unit_type, coefficient_type, ?Integer index, ?String operator) -> String } -> String
|
|
253
214
|
|
|
254
|
-
|
|
255
|
-
|
|
215
|
+
def value_to_s: (unit_type unit, coefficient_type coefficient, String operator) -> String
|
|
216
|
+
end
|
|
256
217
|
|
|
218
|
+
class VectorNumber
|
|
257
219
|
# Refinements of Numeric classes to better work with VectorNumber and similar classes.
|
|
258
220
|
module NumericRefinements
|
|
259
221
|
module CommutativeShuttle
|
|
@@ -266,3 +228,14 @@ class VectorNumber
|
|
|
266
228
|
end
|
|
267
229
|
end
|
|
268
230
|
end
|
|
231
|
+
|
|
232
|
+
class VectorNumber
|
|
233
|
+
# Class for representing special numerical units.
|
|
234
|
+
class SpecialUnit
|
|
235
|
+
def initialize: (Object unit, String text) -> void
|
|
236
|
+
|
|
237
|
+
def to_s: () -> String
|
|
238
|
+
|
|
239
|
+
def inspect: () -> String
|
|
240
|
+
end
|
|
241
|
+
end
|
metadata
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: vector_number
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- Alexander Bulancov
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
@@ -13,18 +13,20 @@ description: |
|
|
|
13
13
|
VectorNumber provides a Numeric-like experience for doing arithmetics on heterogeneous objects, with more advanced operations based on real vector spaces available when needed.
|
|
14
14
|
|
|
15
15
|
Features:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
* Add and subtract (almost) any object, with no setup or declaration.
|
|
17
|
+
* Multiply and divide vectors by any real number to create 1.35 of an array and -2 of a string. What does that mean? Only you know!
|
|
18
|
+
* Use vectors instead of inbuilt numbers in most situtations with no difference in behavior. Or, use familiar methods from numerics with sane semantics!
|
|
19
|
+
* Enumerate vectors in a hash-like fashion, or transform to an array or hash as needed.
|
|
20
|
+
* Enjoy a mix of vector-, complex- and polynomial-like behavior at appropriate times.
|
|
21
|
+
* No dependencies, no extensions. It just works!
|
|
22
22
|
executables: []
|
|
23
23
|
extensions: []
|
|
24
24
|
extra_rdoc_files:
|
|
25
25
|
- README.md
|
|
26
|
+
- doc/vector_space.svg
|
|
26
27
|
files:
|
|
27
28
|
- README.md
|
|
29
|
+
- doc/vector_space.svg
|
|
28
30
|
- lib/vector_number.rb
|
|
29
31
|
- lib/vector_number/comparing.rb
|
|
30
32
|
- lib/vector_number/converting.rb
|
|
@@ -33,6 +35,7 @@ files:
|
|
|
33
35
|
- lib/vector_number/mathing.rb
|
|
34
36
|
- lib/vector_number/numeric_refinements.rb
|
|
35
37
|
- lib/vector_number/querying.rb
|
|
38
|
+
- lib/vector_number/special_unit.rb
|
|
36
39
|
- lib/vector_number/stringifying.rb
|
|
37
40
|
- lib/vector_number/version.rb
|
|
38
41
|
- sig/vector_number.rbs
|
|
@@ -42,9 +45,9 @@ licenses:
|
|
|
42
45
|
metadata:
|
|
43
46
|
homepage_uri: https://github.com/trinistr/vector_number
|
|
44
47
|
bug_tracker_uri: https://github.com/trinistr/vector_number/issues
|
|
45
|
-
documentation_uri: https://rubydoc.info/gems/vector_number/0.
|
|
46
|
-
source_code_uri: https://github.com/trinistr/vector_number/tree/v0.
|
|
47
|
-
changelog_uri: https://github.com/trinistr/vector_number/blob/v0.
|
|
48
|
+
documentation_uri: https://rubydoc.info/gems/vector_number/0.6.0
|
|
49
|
+
source_code_uri: https://github.com/trinistr/vector_number/tree/v0.6.0
|
|
50
|
+
changelog_uri: https://github.com/trinistr/vector_number/blob/v0.6.0/CHANGELOG.md
|
|
48
51
|
rubygems_mfa_required: 'true'
|
|
49
52
|
rdoc_options:
|
|
50
53
|
- "--main"
|