vector_number 0.4.2 → 0.5.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 +2 -2
- data/lib/vector_number/comparing.rb +110 -107
- data/lib/vector_number/converting.rb +152 -147
- data/lib/vector_number/enumerating.rb +107 -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 +22 -3
- data/lib/vector_number/querying.rb +143 -136
- data/lib/vector_number/stringifying.rb +79 -82
- data/lib/vector_number/version.rb +1 -1
- data/lib/vector_number.rb +125 -55
- data/sig/vector_number.rbs +128 -161
- metadata +6 -6
data/lib/vector_number.rb
CHANGED
|
@@ -1,23 +1,93 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
#
|
|
3
|
+
# VectorNumber provides a Numeric-like experience for doing arithmetics on heterogeneous objects,
|
|
4
|
+
# with more advanced operations based on real vector spaces available when needed.
|
|
5
|
+
#
|
|
6
|
+
# VectorNumber inherits from +Object+ and includes +Enumerable+ and +Comparable+.
|
|
7
|
+
# It implements mostly the same interface as +Numeric+ classes, but can {#coerce} any value.
|
|
8
|
+
# Its behavior follows +Complex+ when possible.
|
|
9
|
+
#
|
|
10
|
+
# All instances are frozen after creation.
|
|
11
|
+
#
|
|
12
|
+
# **Creation**
|
|
13
|
+
# - {.[]}: the simplest way to create a VectorNumber
|
|
14
|
+
# - {#initialize}: an alternative way, with some advanced features
|
|
15
|
+
#
|
|
16
|
+
# **Comparing**
|
|
17
|
+
# - {#==}: test for equal value
|
|
18
|
+
# - {#eql?}: test for equal value and type
|
|
19
|
+
# - {#<=>}: compare real values
|
|
20
|
+
# - {#hash}: calculate hash value for use in +Hash+es
|
|
21
|
+
#
|
|
22
|
+
# **Querying**
|
|
23
|
+
# - {#zero?}: check if all coefficients are zero
|
|
24
|
+
# - {#nonzero?}: check if any coefficient is non-zero
|
|
25
|
+
# - {#positive?}: check if all coefficients are positive
|
|
26
|
+
# - {#negative?}: check if all coefficients are negative
|
|
27
|
+
# - {#finite?}: check if all coefficients are finite
|
|
28
|
+
# - {#infinite?}: check if any coefficient is non-finite
|
|
29
|
+
# - {#numeric?}: test if value is real or complex
|
|
30
|
+
# - {#nonnumeric?}: test if value is not real or complex
|
|
31
|
+
# - {#integer?}: +false+
|
|
32
|
+
# - {#real?}: +false+
|
|
33
|
+
#
|
|
34
|
+
# **Unary** **math** **operations**
|
|
35
|
+
# - {#+}/{#dup}: return self
|
|
36
|
+
# - {#-@}/{#neg}: negate value
|
|
37
|
+
# - {#abs}/{#magnitude}: return absolute value (magnitude, length)
|
|
38
|
+
# - {#abs2}: return square of absolute value
|
|
39
|
+
#
|
|
40
|
+
# **Arithmetic** **operations**
|
|
41
|
+
# - {#coerce}: convert any object to a VectorNumber
|
|
42
|
+
# - {#+}/{#add}: add object
|
|
43
|
+
# - {#-}/{#sub}: subtract object
|
|
44
|
+
# - {#*}/{#mult}: multiply (scale) by a real number
|
|
45
|
+
# - {#/}/{#quo}: divide (scale) by a real number
|
|
46
|
+
# - {#fdiv}: divide (scale) by a real number, using +fdiv+
|
|
47
|
+
# - {#div}: perform integer division
|
|
48
|
+
# - {#%}/{#modulo}: return modulus from integer division
|
|
49
|
+
# - {#divmod}: combination of {#div} and {#modulo}
|
|
50
|
+
# - {#remainder}: return remainder from integer division
|
|
51
|
+
#
|
|
52
|
+
# **Rounding**
|
|
53
|
+
# - {#round}: round each coefficient
|
|
54
|
+
# - {#ceil}: round each coefficient up towards +∞
|
|
55
|
+
# - {#floor}: round each coefficient down towards -∞
|
|
56
|
+
# - {#truncate}: truncate each coefficient towards 0
|
|
57
|
+
#
|
|
58
|
+
# **Type** **conversion**
|
|
59
|
+
# - {#real}: return real part
|
|
60
|
+
# - {#imag}/{#imaginary}: return imaginary part
|
|
61
|
+
# - {#to_i}/{#to_int}: convert to +Integer+ if possible
|
|
62
|
+
# - {#to_f}: convert to +Float+ if possible
|
|
63
|
+
# - {#to_r}: convert to +Rational+ if possible
|
|
64
|
+
# - {#to_d}: convert to +BigDecimal+ if possible
|
|
65
|
+
# - {#to_c}: convert to +Complex+ if possible
|
|
66
|
+
#
|
|
67
|
+
# **Hash-like** **operations**
|
|
68
|
+
# - {#each}/{#each_pair}: iterate through every pair of unit and coefficient
|
|
69
|
+
# - {#[]}: get coefficient by unit
|
|
70
|
+
# - {#unit?}/{#key?}: check if a unit has a non-zero coefficient
|
|
71
|
+
# - {#units}/{#keys}: return an array of units
|
|
72
|
+
# - {#coefficients}/#{values}: return an array of coefficients
|
|
73
|
+
# - {#to_h}: convert to Hash
|
|
74
|
+
#
|
|
75
|
+
# **Miscellaneous** **methods**
|
|
76
|
+
# - {#size}: number of non-zero coefficients
|
|
77
|
+
# - {#options}: hash of options
|
|
78
|
+
# - {#dup}/{#+}: return self
|
|
79
|
+
# - {#clone}: return self
|
|
80
|
+
# - {#to_s}: return string representation suitable for printing
|
|
81
|
+
# - {#inspect}: return string representation suitable for display
|
|
13
82
|
class VectorNumber
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
83
|
+
require_relative "vector_number/comparing"
|
|
84
|
+
require_relative "vector_number/converting"
|
|
85
|
+
require_relative "vector_number/enumerating"
|
|
86
|
+
require_relative "vector_number/math_converting"
|
|
87
|
+
require_relative "vector_number/mathing"
|
|
88
|
+
require_relative "vector_number/querying"
|
|
89
|
+
require_relative "vector_number/stringifying"
|
|
90
|
+
require_relative "vector_number/version"
|
|
21
91
|
|
|
22
92
|
# Keys for possible options.
|
|
23
93
|
# Unknown options will be rejected when creating a vector.
|
|
@@ -47,6 +117,7 @@ class VectorNumber
|
|
|
47
117
|
# @since 0.1.0
|
|
48
118
|
I = UNIT[2]
|
|
49
119
|
|
|
120
|
+
# @group Creation
|
|
50
121
|
# Create new VectorNumber from a list of values, possibly specifying options.
|
|
51
122
|
#
|
|
52
123
|
# @example
|
|
@@ -67,6 +138,7 @@ class VectorNumber
|
|
|
67
138
|
def self.[](*values, **options)
|
|
68
139
|
new(values, options)
|
|
69
140
|
end
|
|
141
|
+
# @endgroup
|
|
70
142
|
|
|
71
143
|
# Number of non-zero dimensions.
|
|
72
144
|
#
|
|
@@ -84,9 +156,21 @@ class VectorNumber
|
|
|
84
156
|
# @since 0.1.0
|
|
85
157
|
attr_reader :options
|
|
86
158
|
|
|
159
|
+
# @group Creation
|
|
87
160
|
# Create new VectorNumber from +values+, possibly specifying +options+,
|
|
88
161
|
# possibly modifying coefficients with a block.
|
|
89
162
|
#
|
|
163
|
+
# +values+ can be:
|
|
164
|
+
# - an array of values (see {.[]});
|
|
165
|
+
# - a VectorNumber to copy;
|
|
166
|
+
# - a hash in the format returned by {#to_h};
|
|
167
|
+
# - +nil+ to specify a 0-dimensional vector (same as an empty array or hash).
|
|
168
|
+
#
|
|
169
|
+
# Using a hash as +values+ is an advanced technique which allows to quickly
|
|
170
|
+
# construct a VectorNumber with desired units and coefficients,
|
|
171
|
+
# but it can also lead to unexpected results if care is not taken
|
|
172
|
+
# to provide only valid keys and values.
|
|
173
|
+
#
|
|
90
174
|
# @example
|
|
91
175
|
# VectorNumber.new(1, 2, 3) # ArgumentError
|
|
92
176
|
# VectorNumber.new([1, 2, 3]) # => (6)
|
|
@@ -96,28 +180,32 @@ class VectorNumber
|
|
|
96
180
|
# VectorNumber.new(["a", "b", "c", 3]) { _1 * 2 } # => (2⋅'a' + 2⋅'b' + 2⋅'c' + 6)
|
|
97
181
|
# VectorNumber.new(["a", "b", "c", 3], &:-@) # => (-1⋅'a' - 1⋅'b' - 1⋅'c' - 3)
|
|
98
182
|
# VectorNumber.new(["a", "b", "c", 3], &:digits) # RangeError
|
|
183
|
+
# @example using hash for values
|
|
184
|
+
# v = VectorNumber.new({1 => 15, "a" => 3.4, nil => -3}) # => (15 + 3.4⋅'a' - 3⋅)
|
|
185
|
+
# v.to_h # => {1 => 15, "a" => 3.4, nil => -3}
|
|
99
186
|
#
|
|
100
|
-
# @param values [Array, VectorNumber, Hash{Object =>
|
|
101
|
-
# values for this number, hashes are treated like plain vector numbers
|
|
187
|
+
# @param values [Array, VectorNumber, Hash{Object => Numeric}, nil] values for this vector
|
|
102
188
|
# @param options [Hash{Symbol => Object}, nil]
|
|
103
|
-
# options for this
|
|
189
|
+
# options for this vector, if +values+ is a VectorNumber or contains it,
|
|
104
190
|
# these will be merged with options from its +options+
|
|
105
|
-
# @option options [Symbol, String] :mult
|
|
106
|
-
#
|
|
107
|
-
# @yieldparam coefficient [
|
|
108
|
-
# @yieldreturn [
|
|
109
|
-
# @raise [RangeError] if
|
|
110
|
-
def initialize(values = nil, options = nil, &)
|
|
191
|
+
# @option options [Symbol, String] :mult Multiplication symbol,
|
|
192
|
+
# either a key from {MULT_STRINGS} or a literal string to use
|
|
193
|
+
# @yieldparam coefficient [Numeric] a real number
|
|
194
|
+
# @yieldreturn [Numeric] new coefficient
|
|
195
|
+
# @raise [RangeError] if a block is used and it returns a non-number or non-real number
|
|
196
|
+
def initialize(values = nil, options = nil, &transform)
|
|
111
197
|
# @type var options: Hash[Symbol, Object]
|
|
112
198
|
initialize_from(values)
|
|
113
|
-
apply_transform(&)
|
|
199
|
+
apply_transform(&transform)
|
|
114
200
|
finalize_contents
|
|
115
|
-
save_options(options, values
|
|
201
|
+
save_options(options, values)
|
|
116
202
|
@options.freeze
|
|
117
203
|
@data.freeze
|
|
118
204
|
freeze
|
|
119
205
|
end
|
|
120
206
|
|
|
207
|
+
# @group Miscellaneous methods
|
|
208
|
+
|
|
121
209
|
# Return self.
|
|
122
210
|
#
|
|
123
211
|
# @return [VectorNumber]
|
|
@@ -149,11 +237,11 @@ class VectorNumber
|
|
|
149
237
|
# Create new VectorNumber from a value or self, optionally applying a transform.
|
|
150
238
|
#
|
|
151
239
|
# @param from [Object] self if not specified
|
|
152
|
-
# @yieldparam coefficient [
|
|
153
|
-
# @yieldreturn [
|
|
240
|
+
# @yieldparam coefficient [Numeric] a real number
|
|
241
|
+
# @yieldreturn [Numeric] new coefficient
|
|
154
242
|
# @return [VectorNumber]
|
|
155
|
-
def new(from = self, &)
|
|
156
|
-
self.class.new(from, options, &)
|
|
243
|
+
def new(from = self, &transform)
|
|
244
|
+
self.class.new(from, options, &transform)
|
|
157
245
|
end
|
|
158
246
|
|
|
159
247
|
# Check if +other+ is a real number.
|
|
@@ -162,16 +250,12 @@ class VectorNumber
|
|
|
162
250
|
#
|
|
163
251
|
# @param value [Object]
|
|
164
252
|
# @return [Boolean]
|
|
165
|
-
#
|
|
166
|
-
# @since 0.1.0
|
|
167
253
|
def real_number?(value)
|
|
168
254
|
(value.is_a?(Numeric) && value.real?) || (value.is_a?(self.class) && value.numeric?(1))
|
|
169
255
|
end
|
|
170
256
|
|
|
171
|
-
# @param values [Array, Hash{Object =>
|
|
257
|
+
# @param values [Array, Hash{Object => Numeric}, VectorNumber, nil]
|
|
172
258
|
# @return [void]
|
|
173
|
-
#
|
|
174
|
-
# @since 0.1.0
|
|
175
259
|
def initialize_from(values)
|
|
176
260
|
@data = values.to_h and return if values.is_a?(VectorNumber)
|
|
177
261
|
|
|
@@ -190,8 +274,6 @@ class VectorNumber
|
|
|
190
274
|
|
|
191
275
|
# @param value [VectorNumber, Numeric, Object]
|
|
192
276
|
# @return [void]
|
|
193
|
-
#
|
|
194
|
-
# @since 0.1.0
|
|
195
277
|
def add_value_to_data(value)
|
|
196
278
|
case value
|
|
197
279
|
when Numeric
|
|
@@ -205,8 +287,6 @@ class VectorNumber
|
|
|
205
287
|
|
|
206
288
|
# @param value [Numeric]
|
|
207
289
|
# @return [void]
|
|
208
|
-
#
|
|
209
|
-
# @since 0.1.0
|
|
210
290
|
def add_numeric_value_to_data(value)
|
|
211
291
|
@data[R] += value.real
|
|
212
292
|
# Most numbers will be real, and this extra condition appreciably speeds up addition,
|
|
@@ -214,10 +294,8 @@ class VectorNumber
|
|
|
214
294
|
@data[I] += value.imaginary unless value.real?
|
|
215
295
|
end
|
|
216
296
|
|
|
217
|
-
# @param vector [VectorNumber, Hash{Object =>
|
|
297
|
+
# @param vector [VectorNumber, Hash{Object => Numeric}]
|
|
218
298
|
# @return [void]
|
|
219
|
-
#
|
|
220
|
-
# @since 0.1.0
|
|
221
299
|
def add_vector_to_data(vector)
|
|
222
300
|
vector.each_pair do |unit, coefficient|
|
|
223
301
|
raise RangeError, "#{coefficient} is not a real number" unless real_number?(coefficient)
|
|
@@ -226,12 +304,10 @@ class VectorNumber
|
|
|
226
304
|
end
|
|
227
305
|
end
|
|
228
306
|
|
|
229
|
-
# @yieldparam coefficient [
|
|
230
|
-
# @yieldreturn [
|
|
307
|
+
# @yieldparam coefficient [Numeric] a real number
|
|
308
|
+
# @yieldreturn [Numeric] new coefficient
|
|
231
309
|
# @return [void]
|
|
232
310
|
# @raise [RangeError]
|
|
233
|
-
#
|
|
234
|
-
# @since 0.1.0
|
|
235
311
|
def apply_transform
|
|
236
312
|
return unless block_given?
|
|
237
313
|
|
|
@@ -246,9 +322,7 @@ class VectorNumber
|
|
|
246
322
|
# @param options [Hash{Symbol => Object}, nil]
|
|
247
323
|
# @param values [Object] initializing object
|
|
248
324
|
# @return [void]
|
|
249
|
-
|
|
250
|
-
# @since 0.1.0
|
|
251
|
-
def save_options(options, values:)
|
|
325
|
+
def save_options(options, values)
|
|
252
326
|
@options =
|
|
253
327
|
case values
|
|
254
328
|
in VectorNumber
|
|
@@ -263,8 +337,6 @@ class VectorNumber
|
|
|
263
337
|
# @param base_options [Hash{Symbol => Object}]
|
|
264
338
|
# @param added_options [Hash{Symbol => Object}, nil]
|
|
265
339
|
# @return [Hash{Symbol => Object}]
|
|
266
|
-
#
|
|
267
|
-
# @since 0.3.0
|
|
268
340
|
def merge_options(base_options, added_options)
|
|
269
341
|
return base_options if !added_options || added_options.empty?
|
|
270
342
|
# Optimization for the common case of passing options through #new.
|
|
@@ -275,8 +347,6 @@ class VectorNumber
|
|
|
275
347
|
|
|
276
348
|
# Compact coefficients, calculate size and freeze data.
|
|
277
349
|
# @return [void]
|
|
278
|
-
#
|
|
279
|
-
# @since 0.1.0
|
|
280
350
|
def finalize_contents
|
|
281
351
|
@data.delete_if { |_u, c| c.zero? }
|
|
282
352
|
@data.freeze
|
data/sig/vector_number.rbs
CHANGED
|
@@ -1,43 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# A class to add together anything.
|
|
2
|
+
class VectorNumber
|
|
3
|
+
# ---- Generic types ----
|
|
4
|
+
type real_number = Integer | Float | Rational | BigDecimal
|
|
3
5
|
|
|
4
|
-
type in_value_type = untyped
|
|
5
|
-
type unit_type = untyped
|
|
6
|
-
type coefficient_type = real_number
|
|
6
|
+
type in_value_type = untyped
|
|
7
|
+
type unit_type = untyped
|
|
8
|
+
type coefficient_type = real_number
|
|
7
9
|
|
|
8
|
-
type list[T] = Array[T]
|
|
10
|
+
type list[T] = Array[T]
|
|
9
11
|
|
|
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]
|
|
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]
|
|
14
16
|
|
|
15
|
-
type options_type = Hash[Symbol, untyped]
|
|
17
|
+
type options_type = Hash[Symbol, untyped]
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
def size: -> Integer
|
|
19
|
-
def options: -> options_type
|
|
20
|
-
|
|
21
|
-
# private
|
|
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
|
|
30
|
-
|
|
31
|
-
# A class to add together anything.
|
|
32
|
-
class VectorNumber
|
|
33
|
-
include _BaseMethods
|
|
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
|
|
19
|
+
# ---- Constants ----
|
|
41
20
|
|
|
42
21
|
VERSION: String
|
|
43
22
|
|
|
@@ -48,10 +27,15 @@ class VectorNumber
|
|
|
48
27
|
R: Complex
|
|
49
28
|
I: Complex
|
|
50
29
|
|
|
51
|
-
|
|
30
|
+
# ---- Public methods ----
|
|
31
|
+
|
|
32
|
+
def self.[]: (*unit_type values, **options_type options) -> instance
|
|
33
|
+
|
|
34
|
+
def size: -> Integer
|
|
35
|
+
def options: -> options_type
|
|
52
36
|
|
|
53
37
|
def initialize:
|
|
54
|
-
(?(units_list_type |
|
|
38
|
+
(?(units_list_type | plain_instance | instance)? values, ?options_type? options)
|
|
55
39
|
?{ (coefficient_type coefficient) -> coefficient_type }
|
|
56
40
|
-> void
|
|
57
41
|
|
|
@@ -64,196 +48,179 @@ class VectorNumber
|
|
|
64
48
|
|
|
65
49
|
@size: Integer
|
|
66
50
|
@options: options_type
|
|
67
|
-
@data:
|
|
51
|
+
@data: plain_instance
|
|
68
52
|
|
|
69
|
-
def
|
|
53
|
+
def new: () { (coefficient_type value) -> coefficient_type } -> instance
|
|
54
|
+
| (in_value_type) { (coefficient_type value) -> coefficient_type } -> instance
|
|
55
|
+
| () -> instance
|
|
56
|
+
| (in_value_type) -> instance
|
|
70
57
|
|
|
71
|
-
def
|
|
58
|
+
def real_number?: (real_number value) -> true
|
|
59
|
+
| (instance) -> bool
|
|
60
|
+
| (in_value_type value) -> false
|
|
61
|
+
|
|
62
|
+
def initialize_from: ((units_list_type | plain_instance | instance)? values) -> void
|
|
63
|
+
|
|
64
|
+
def add_value_to_data: ((instance | Numeric | unit_type) value) -> void
|
|
72
65
|
|
|
73
66
|
def add_numeric_value_to_data: (Numeric value) -> void
|
|
74
67
|
|
|
75
|
-
def add_vector_to_data: ((
|
|
68
|
+
def add_vector_to_data: ((instance | plain_instance) vector) -> void
|
|
76
69
|
|
|
77
70
|
def apply_transform: () ?{ (coefficient_type value) -> coefficient_type } -> void
|
|
78
71
|
|
|
79
|
-
def save_options: (options_type? options, values
|
|
72
|
+
def save_options: (options_type? options, in_value_type values) -> void
|
|
80
73
|
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
74
|
|
|
84
75
|
def finalize_contents: () -> void
|
|
76
|
+
end
|
|
85
77
|
|
|
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 *
|
|
103
|
-
|
|
104
|
-
def /: (real_number | vector_type) -> vector_type
|
|
105
|
-
alias quo /
|
|
106
|
-
|
|
107
|
-
def fdiv: (real_number | vector_type) -> vector_type
|
|
78
|
+
# Methods for performing actual math.
|
|
79
|
+
class VectorNumber
|
|
80
|
+
def coerce: (in_value_type) -> [instance, self]
|
|
108
81
|
|
|
109
|
-
|
|
82
|
+
def -@: () -> instance
|
|
83
|
+
alias neg -@
|
|
110
84
|
|
|
111
|
-
|
|
112
|
-
|
|
85
|
+
def +: (in_value_type) -> instance
|
|
86
|
+
alias add +
|
|
113
87
|
|
|
114
|
-
|
|
88
|
+
def -: (in_value_type) -> instance
|
|
89
|
+
alias sub -
|
|
115
90
|
|
|
116
|
-
|
|
91
|
+
def *: (real_number | instance) -> instance
|
|
92
|
+
alias mult *
|
|
117
93
|
|
|
118
|
-
|
|
94
|
+
def /: (real_number | instance) -> instance
|
|
95
|
+
alias quo /
|
|
119
96
|
|
|
120
|
-
|
|
121
|
-
end
|
|
97
|
+
def fdiv: (real_number | instance) -> instance
|
|
122
98
|
|
|
123
|
-
|
|
124
|
-
module MathConverting
|
|
125
|
-
include _BaseMethods
|
|
126
|
-
include Enumerating
|
|
99
|
+
def div: (real_number | instance) -> instance
|
|
127
100
|
|
|
128
|
-
|
|
101
|
+
def %: (real_number | instance) -> instance
|
|
102
|
+
alias modulo %
|
|
129
103
|
|
|
130
|
-
|
|
104
|
+
def divmod: (real_number | instance) -> [instance, instance]
|
|
131
105
|
|
|
132
|
-
|
|
106
|
+
def remainder: (real_number | instance) -> instance
|
|
133
107
|
|
|
134
|
-
|
|
108
|
+
private
|
|
135
109
|
|
|
136
|
-
|
|
110
|
+
def check_divisibility: (real_number | instance) -> void
|
|
111
|
+
end
|
|
137
112
|
|
|
138
|
-
|
|
139
|
-
|
|
113
|
+
# Various mathematical operations that are also conversions.
|
|
114
|
+
class VectorNumber
|
|
115
|
+
def abs: () -> Float
|
|
140
116
|
|
|
141
|
-
|
|
142
|
-
module Converting
|
|
143
|
-
include _BaseMethods
|
|
144
|
-
include Querying
|
|
117
|
+
def abs2: () -> Float
|
|
145
118
|
|
|
146
|
-
|
|
119
|
+
def truncate: (?Integer) -> instance
|
|
147
120
|
|
|
148
|
-
|
|
149
|
-
alias imag imaginary
|
|
121
|
+
def ceil: (?Integer) -> instance
|
|
150
122
|
|
|
151
|
-
|
|
152
|
-
| () -> void
|
|
153
|
-
alias to_int to_i
|
|
123
|
+
def floor: (?Integer) -> instance
|
|
154
124
|
|
|
155
|
-
|
|
156
|
-
|
|
125
|
+
def round: (?Integer, ?half: (:up | :down | :even)) -> instance
|
|
126
|
+
end
|
|
157
127
|
|
|
158
|
-
|
|
159
|
-
|
|
128
|
+
# Methods for converting to different number classes.
|
|
129
|
+
class VectorNumber
|
|
130
|
+
def real: () -> real_number
|
|
160
131
|
|
|
161
|
-
|
|
162
|
-
|
|
132
|
+
def imaginary: () -> real_number
|
|
133
|
+
alias imag imaginary
|
|
163
134
|
|
|
164
|
-
|
|
165
|
-
|
|
135
|
+
def to_i: () -> Integer
|
|
136
|
+
alias to_int to_i
|
|
166
137
|
|
|
167
|
-
|
|
138
|
+
def to_f: () -> Float
|
|
168
139
|
|
|
169
|
-
|
|
140
|
+
def to_r: () -> Rational
|
|
170
141
|
|
|
171
|
-
|
|
172
|
-
end
|
|
142
|
+
def to_d: (?Integer?) -> BigDecimal
|
|
173
143
|
|
|
174
|
-
|
|
175
|
-
module Enumerating
|
|
176
|
-
include _BaseMethods
|
|
177
|
-
include _Each[each_value_type]
|
|
178
|
-
include Enumerable[each_value_type]
|
|
144
|
+
def to_c: () -> Complex
|
|
179
145
|
|
|
180
|
-
|
|
181
|
-
| () -> Enumerator[each_value_type, Integer]
|
|
182
|
-
| ...
|
|
183
|
-
alias each_pair each
|
|
146
|
+
private
|
|
184
147
|
|
|
185
|
-
|
|
186
|
-
|
|
148
|
+
def raise_convert_error: (Class) -> void
|
|
149
|
+
end
|
|
187
150
|
|
|
188
|
-
|
|
189
|
-
|
|
151
|
+
class VectorNumber
|
|
152
|
+
include _Each[each_value_type]
|
|
153
|
+
include Enumerable[each_value_type]
|
|
190
154
|
|
|
191
|
-
|
|
192
|
-
|
|
155
|
+
def each: () { (unit_type unit, coefficient_type coefficient) -> void } -> self
|
|
156
|
+
| () -> Enumerator[each_value_type, Integer]
|
|
157
|
+
| ...
|
|
158
|
+
alias each_pair each
|
|
193
159
|
|
|
194
|
-
|
|
160
|
+
def units: () -> units_list_type
|
|
161
|
+
alias keys units
|
|
195
162
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
end
|
|
163
|
+
def coefficients: () -> coefficients_list_type
|
|
164
|
+
alias values coefficients
|
|
199
165
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
include _BaseMethods
|
|
203
|
-
include Enumerating
|
|
204
|
-
include Converting
|
|
205
|
-
include Querying
|
|
166
|
+
def to_h: () -> plain_instance
|
|
167
|
+
| () { (unit_type, coefficient_type) -> each_value_type } -> plain_instance
|
|
206
168
|
|
|
207
|
-
|
|
169
|
+
def []: (unit_type unit) -> coefficient_type
|
|
208
170
|
|
|
209
|
-
|
|
171
|
+
def unit?: (unit_type unit) -> bool
|
|
172
|
+
alias key? unit?
|
|
173
|
+
end
|
|
210
174
|
|
|
211
|
-
|
|
175
|
+
# Methods for comparing with other numbers.
|
|
176
|
+
class VectorNumber
|
|
177
|
+
def ==: (in_value_type other) -> bool
|
|
212
178
|
|
|
213
|
-
|
|
214
|
-
end
|
|
179
|
+
def eql?: (in_value_type other) -> bool
|
|
215
180
|
|
|
216
|
-
|
|
217
|
-
# Mostly modeled after {::Complex}.
|
|
218
|
-
module Querying
|
|
219
|
-
include Enumerating
|
|
181
|
+
def hash: () -> Integer
|
|
220
182
|
|
|
221
|
-
|
|
183
|
+
def <=>: (in_value_type other) -> Integer?
|
|
184
|
+
end
|
|
222
185
|
|
|
223
|
-
|
|
186
|
+
# Methods for querying state of the number.
|
|
187
|
+
# Mostly modeled after {::Complex}.
|
|
188
|
+
class VectorNumber
|
|
189
|
+
def numeric?: (?Integer) -> bool
|
|
224
190
|
|
|
225
|
-
|
|
191
|
+
def nonnumeric?: (?Integer) -> bool
|
|
226
192
|
|
|
227
|
-
|
|
193
|
+
def finite?: () -> bool
|
|
228
194
|
|
|
229
|
-
|
|
195
|
+
def infinite?: () -> Integer?
|
|
230
196
|
|
|
231
|
-
|
|
197
|
+
def zero?: () -> bool
|
|
232
198
|
|
|
233
|
-
|
|
199
|
+
def nonzero?: () -> self?
|
|
234
200
|
|
|
235
|
-
|
|
201
|
+
def positive?: () -> bool
|
|
236
202
|
|
|
237
|
-
|
|
203
|
+
def negative?: () -> bool
|
|
238
204
|
|
|
239
|
-
|
|
240
|
-
end
|
|
205
|
+
def real?: () -> false
|
|
241
206
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
include Querying
|
|
207
|
+
def integer?: () -> false
|
|
208
|
+
end
|
|
245
209
|
|
|
246
|
-
|
|
210
|
+
# Methods and options for string representation.
|
|
211
|
+
class VectorNumber
|
|
212
|
+
MULT_STRINGS: Hash[Symbol, String]
|
|
247
213
|
|
|
248
|
-
|
|
214
|
+
def to_s: (?mult: (Symbol | String)) -> String
|
|
249
215
|
|
|
250
|
-
|
|
216
|
+
def inspect: () -> String
|
|
251
217
|
|
|
252
|
-
|
|
218
|
+
private
|
|
253
219
|
|
|
254
|
-
|
|
255
|
-
|
|
220
|
+
def value_to_s: (unit_type unit, coefficient_type coefficient, mult: (Symbol | String)) -> String
|
|
221
|
+
end
|
|
256
222
|
|
|
223
|
+
class VectorNumber
|
|
257
224
|
# Refinements of Numeric classes to better work with VectorNumber and similar classes.
|
|
258
225
|
module NumericRefinements
|
|
259
226
|
module CommutativeShuttle
|