vector_number 0.3.1 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b935cacefb185478f7f923046b28589c6da2763574c315f19c6b8050cd5835e1
4
- data.tar.gz: a93dd71a4de2d07255962e13a81a357c92f5a1653d916e95d5a54a99afa984a6
3
+ metadata.gz: 3958070d42eb34b2a76467cda30a4c33e90838a17211d06bf9088311f6cf82ee
4
+ data.tar.gz: 5308af739c073e2193ee8199a1118eef011e0aa552cbcceb6a8bae63f8c2da5d
5
5
  SHA512:
6
- metadata.gz: 9d6ea64b09d12434470d65c7ff808b63b9ccc59cb1e479bc7659ed973a8f03a3fb137dfe3641163dc56b06163b7d0c472ed74af2f49f6623b866eff90961aa7c
7
- data.tar.gz: 85a927c88c99533e434a185a239f2d129567adf8d2e8c4d8d62e6239560d889e24fc7d3e478039b6d8c4213596e9ea7a61da5d0ab867162f9fbe7bee4edf05e5
6
+ metadata.gz: 515c092ebe57230a92fff99868ccf7fd929fcf9a2aa6d81b10918c68108010b55214bc88fef0112659b513623959c82fd6e8431fbaf16e1917e25d1b07a797a7
7
+ data.tar.gz: 9738e48084961b2ac7a6e02853ca30c37d736692ac14b3e49ac72f8adea3a31ebc72ea1751e474c57007ef1aa9d3663aa61a2de3ac3b937192c7ae7bd680a6e4
data/CHANGELOG.md CHANGED
@@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
8
  ## [Next]
9
+ ## [v0.4.0] — 2025-07-07
10
+
11
+ **Changed**
12
+ - [🍄 BREAKING] Change `R` and `I` constants to be `1` and `2` instead of `0i` and `1i` respectively.
13
+ Their values are still semi-private and should not be relied on.
14
+ - [🍄 BREAKING] Calling `new` with an unsupported value now raises `ArgumentError` instead of treating it like air.
15
+ - Optimize various initialization paths. It is now 1.5-2.5 times faster, depending on arguments.
16
+ - [🚀 CI] Disable JRuby testing on CI.
17
+
18
+ [Compare v0.3.1...main](https://github.com/trinistr/vector_number/compare/v0.3.1...main)
19
+
9
20
  ## [v0.3.1] — 2025-06-21
10
21
 
11
22
  This is mostly a documentation update with a side of improved gemspec.
@@ -23,7 +34,7 @@ This is mostly a documentation update with a side of improved gemspec.
23
34
  **Fixed**
24
35
  - Correct broken changelog link in gem metadata.
25
36
 
26
- [Compare v0.3.0...main](https://github.com/trinistr/vector_number/compare/v0.3.0...main)
37
+ [Compare v0.3.0...v0.3.1](https://github.com/trinistr/vector_number/compare/v0.3.0...v0.3.1)
27
38
 
28
39
  ## [v0.3.0] — 2025-05-12
29
40
 
@@ -33,7 +44,7 @@ This is mostly a documentation update with a side of improved gemspec.
33
44
  `#+@` was already practically aliased by `#dup`.
34
45
 
35
46
  **Changed**
36
- - [Breaking] Long-existing but broken options feature is now fixed.
47
+ - [🍄 BREAKING] Long-existing but broken options feature is now fixed.
37
48
  When creating new vector through any operation, participating vector's options
38
49
  are copied to the new one. When several vectors are present, only first one matters.
39
50
  - Both `#+@` and `#dup` are now aliases of `#itself` instead of full methods.
@@ -70,7 +81,7 @@ README was updated to reflect this change.
70
81
  - Add hash-like methods `#[]` and `#unit?` (aliased as `#key?`).
71
82
 
72
83
  **Changed**
73
- - [Breaking] Change `positive?` and `negative?` to no longer return `nil`
84
+ - [🍄 BREAKING] Change `positive?` and `negative?` to no longer return `nil`
74
85
  when number is neither strictly positive or negative,
75
86
  these cases will now return `false`.
76
87
  - Make `VectorNumber.new` accept options when initializing from a VectorNumber
@@ -138,6 +149,7 @@ README was updated to reflect this change.
138
149
  - Initial work.
139
150
 
140
151
  [Next]: https://github.com/trinistr/vector_number/tree/main
152
+ [v0.3.1]: https://github.com/trinistr/vector_number/tree/v0.3.1
141
153
  [v0.3.0]: https://github.com/trinistr/vector_number/tree/v0.3.0
142
154
  [v0.2.6]: https://github.com/trinistr/vector_number/tree/v0.2.6
143
155
  [v0.2.5]: https://github.com/trinistr/vector_number/tree/v0.2.5
data/README.md CHANGED
@@ -52,8 +52,8 @@ VectorNumbers are mostly useful for summing up heterogeneous objects:
52
52
  sum = VectorNumber[]
53
53
  [4, "death", "death", 13, nil].each { sum = sum + _1 }
54
54
  sum # => (17 + 2⋅'death' + 1⋅)
55
- sum.to_a # => [[(0+0i), 17], ["death", 2], [nil, 1]]
56
- sum.to_h # => {(0+0i)=>17, "death"=>2, nil=>1}
55
+ sum.to_a # => [[1, 17], ["death", 2], [nil, 1]]
56
+ sum.to_h # => {1=>17, "death"=>2, nil=>1}
57
57
  ```
58
58
 
59
59
  Alternatively, the same result can be equivalently (and more efficiently) achieved by
@@ -67,7 +67,7 @@ VectorNumber.new([4, "death", "death", 13, nil])
67
67
 
68
68
  VectorNumber is developed on MRI (CRuby) but should work on other engines too.
69
69
  - TruffleRuby: works as expected, but there are differences in core Ruby code, so some tests fail.
70
- - JRuby: significant problems (at least on CI), but may work.
70
+ - JRuby: significant problems, but may work, currently not tested.
71
71
  - Other engines: untested, but should work, depending on compatibility.
72
72
 
73
73
  ## Development
@@ -85,7 +85,7 @@ class VectorNumber
85
85
  # VectorNumber["a"] <=> 2 # => nil
86
86
  #
87
87
  # @param other [Object]
88
- # @return [-1, 0, 1]
88
+ # @return [Integer]
89
89
  # @return [nil] if +self+ or +other+ isn't a real number.
90
90
  #
91
91
  # @see Comparable
@@ -21,7 +21,7 @@ class VectorNumber
21
21
  # units # => ["a", "b"]
22
22
  # @example Enumerator
23
23
  # v.each.size # => 3
24
- # (v.each + [["d", 0]]).map(&:first) # => ["a", "b", (0+0i), "d"]
24
+ # (v.each + [["d", 0]]).map(&:first) # => ["a", "b", 1, "d"]
25
25
  # v.each_pair.peek # => ["a", 1]
26
26
  #
27
27
  # @overload each
@@ -49,7 +49,7 @@ class VectorNumber
49
49
  # Get a list of units with non-zero coefficients.
50
50
  #
51
51
  # @example
52
- # VectorNumber["a", "b", 6].units # => ["a", "b", (0+0i)]
52
+ # VectorNumber["a", "b", 6].units # => ["a", "b", 1]
53
53
  # VectorNumber.new.keys # => []
54
54
  #
55
55
  # @return [Array<Object>]
@@ -79,7 +79,7 @@ class VectorNumber
79
79
  # Returned hash has a default value of 0.
80
80
  #
81
81
  # @example
82
- # VectorNumber["a", "b", 6].to_h # => {"a"=>1, "b"=>1, (0+0i)=>6}
82
+ # VectorNumber["a", "b", 6].to_h # => {"a"=>1, "b"=>1, 1=>6}
83
83
  # VectorNumber["a", "b", 6].to_h["c"] # => 0
84
84
  #
85
85
  # @return [Hash{Object => Integer, Float, Rational, BigDecimal}]
@@ -2,5 +2,5 @@
2
2
 
3
3
  class VectorNumber
4
4
  # @return [String]
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.0"
6
6
  end
data/lib/vector_number.rb CHANGED
@@ -37,7 +37,7 @@ class VectorNumber
37
37
  # Get a unit for +n+th numeric dimension, where 1 is real, 2 is imaginary.
38
38
  #
39
39
  # @since 0.2.0
40
- UNIT = ->(n) { (n - 1).i }.freeze
40
+ UNIT = ->(n) { n }.freeze
41
41
  # Constant for real unit.
42
42
  #
43
43
  # @since 0.2.0
@@ -97,9 +97,9 @@ class VectorNumber
97
97
  # VectorNumber.new(["a", "b", "c", 3], &:-@) # => (-1⋅'a' - 1⋅'b' - 1⋅'c' - 3)
98
98
  # VectorNumber.new(["a", "b", "c", 3], &:digits) # RangeError
99
99
  #
100
- # @param values [Array, Hash{Object => Integer, Float, Rational, BigDecimal}, VectorNumber]
100
+ # @param values [Array, VectorNumber, Hash{Object => Integer, Float, Rational, BigDecimal}, nil]
101
101
  # values for this number, hashes are treated like plain vector numbers
102
- # @param options [Hash{Symbol => Object}]
102
+ # @param options [Hash{Symbol => Object}, nil]
103
103
  # options for this number, if +values+ is a VectorNumber or contains it,
104
104
  # these will be merged with options from its +options+
105
105
  # @option options [Symbol, String] :mult
@@ -173,15 +173,18 @@ class VectorNumber
173
173
  #
174
174
  # @since 0.1.0
175
175
  def initialize_from(values)
176
- @data = Hash.new(0)
176
+ @data = values.to_h and return if values.is_a?(VectorNumber)
177
177
 
178
+ @data = Hash.new(0)
178
179
  case values
179
- when VectorNumber, Hash
180
- add_vector_to_data(values)
181
180
  when Array
182
181
  values.each { |value| add_value_to_data(value) }
182
+ when Hash
183
+ add_vector_to_data(values)
184
+ when nil
185
+ # Do nothing, as there are no values.
183
186
  else
184
- # Don't add anything.
187
+ raise ArgumentError, "unsupported type for values: #{values.class}"
185
188
  end
186
189
  end
187
190
 
@@ -206,7 +209,9 @@ class VectorNumber
206
209
  # @since 0.1.0
207
210
  def add_numeric_value_to_data(value)
208
211
  @data[R] += value.real
209
- @data[I] += value.imaginary
212
+ # Most numbers will be real, and this extra condition appreciably speeds up addition,
213
+ # while having no noticeable impact on complex numbers.
214
+ @data[I] += value.imaginary unless value.real?
210
215
  end
211
216
 
212
217
  # @param vector [VectorNumber, Hash{Object => Integer, Float, Rational, BigDecimal}]
@@ -245,32 +250,27 @@ class VectorNumber
245
250
  # @since 0.1.0
246
251
  def save_options(options, values:)
247
252
  @options =
248
- case [options, values]
249
- in [{} | nil, VectorNumber]
250
- values.options
251
- in [{} | nil, [*, VectorNumber => vector, *]]
252
- vector.options
253
- in Hash, VectorNumber
253
+ case values
254
+ in VectorNumber
254
255
  merge_options(values.options, options)
255
- in Hash, [*, VectorNumber => vector, *]
256
+ in Array[*, VectorNumber => vector, *]
256
257
  merge_options(vector.options, options)
257
- in Hash, _ unless options.empty?
258
- merge_options(default_options, options)
259
258
  else
260
- default_options
259
+ merge_options(DEFAULT_OPTIONS, options)
261
260
  end
262
261
  end
263
262
 
264
263
  # @param base_options [Hash{Symbol => Object}]
265
- # @param added_options [Hash{Symbol => Object}]
264
+ # @param added_options [Hash{Symbol => Object}, nil]
266
265
  # @return [Hash{Symbol => Object}]
267
266
  #
268
267
  # @since 0.3.0
269
268
  def merge_options(base_options, added_options)
269
+ return base_options if !added_options || added_options.empty?
270
270
  # Optimization for the common case of passing options through #new.
271
271
  return base_options if added_options.equal?(base_options)
272
272
 
273
- base_options.merge(added_options).slice(*known_options)
273
+ base_options.merge(added_options).slice(*KNOWN_OPTIONS)
274
274
  end
275
275
 
276
276
  # Compact coefficients, calculate size and freeze data.
@@ -282,14 +282,4 @@ class VectorNumber
282
282
  @data.freeze
283
283
  @size = @data.size
284
284
  end
285
-
286
- # @since 0.2.0
287
- def default_options
288
- DEFAULT_OPTIONS
289
- end
290
-
291
- # @since 0.2.0
292
- def known_options
293
- KNOWN_OPTIONS
294
- end
295
285
  end
metadata CHANGED
@@ -1,20 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vector_number
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Bulancov
8
+ autorequire:
8
9
  bindir: exe
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2025-07-07 00:00:00.000000000 Z
11
12
  dependencies: []
13
+ description:
14
+ email:
12
15
  executables: []
13
16
  extensions: []
14
17
  extra_rdoc_files:
18
+ - README.md
15
19
  - CHANGELOG.md
16
20
  - LICENSE.txt
17
- - README.md
18
21
  files:
19
22
  - CHANGELOG.md
20
23
  - LICENSE.txt
@@ -36,10 +39,11 @@ licenses:
36
39
  metadata:
37
40
  homepage_uri: https://github.com/trinistr/vector_number
38
41
  bug_tracker_uri: https://github.com/trinistr/vector_number/issues
39
- documentation_uri: https://rubydoc.info/gems/vector_number/0.3.1
40
- source_code_uri: https://github.com/trinistr/vector_number/tree/v0.3.1
41
- changelog_uri: https://github.com/trinistr/vector_number/blob/v0.3.1/CHANGELOG.md
42
+ documentation_uri: https://rubydoc.info/gems/vector_number/0.4.0
43
+ source_code_uri: https://github.com/trinistr/vector_number/tree/v0.4.0
44
+ changelog_uri: https://github.com/trinistr/vector_number/blob/v0.4.0/CHANGELOG.md
42
45
  rubygems_mfa_required: 'true'
46
+ post_install_message:
43
47
  rdoc_options:
44
48
  - "--main"
45
49
  - README.md
@@ -58,7 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
62
  - !ruby/object:Gem::Version
59
63
  version: '0'
60
64
  requirements: []
61
- rubygems_version: 3.6.8
65
+ rubygems_version: 3.4.19
66
+ signing_key:
62
67
  specification_version: 4
63
68
  summary: A library to add together anything.
64
69
  test_files: []