vector_number 0.2.5 → 0.2.6

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: b8fc99d66449b0cbc1ac4f2871e540ff921dee5d291b487221193edaefca1b2f
4
- data.tar.gz: 38f15f406bff5348debb50b5468bf416e2fd3bca4be69257e3b59b99fbebd6ed
3
+ metadata.gz: 60948b232b4ab095c808a54d3ba32748893db4c660a2ab553ad216d677906814
4
+ data.tar.gz: b3ff8e996c8617c7c6a5c0592f18480d78cd5f7c520ab47194ae965eb21406a5
5
5
  SHA512:
6
- metadata.gz: 23f819566a189655bcefaa7d22005e7de4e7349ad99718497e13faf97a748d6c9c63788146379473d28caed9bc7063a13787074f0c87241a1a36859a30a126a7
7
- data.tar.gz: 33c4ce966c7ce9d3d1a486acd6119af50d9458c8048e2a6f07348625bb50bcb11d1db50c8dbee75ba81be996afe53bcb25af2b356ab9be0e1a6689a67d3a14dd
6
+ metadata.gz: d40eb5b5c6de69bd5db2579e9f6eb9f872abd67b68b49983c7d5c23d4a2726fbbaad128819631baf8e21e094d77986a4c69d53bcc408e2f838faffed9875c2bc
7
+ data.tar.gz: 4656e1b5a136191bede67f3c3203b3a43110e4c9700328b61d3802abfca3805ff39d7718d774a70eb0e0c20ea0a9e687fbdf6ce19594171148998b95ffcda9bd
data/CHANGELOG.md CHANGED
@@ -6,6 +6,16 @@ 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.2.6] — 2025-04-30
10
+
11
+ **Added**
12
+ - Add `#div`, `#%` (aliased as `#modulo`), `#divmod` and `#remainder` methods.
13
+ - Add `#quo` alias to `#/`.
14
+
15
+ **Fixed**
16
+ - `#/`, `#fdiv` as well as new division methods now properly check for division by zero.
17
+ VectorNumber does not support this as not all Numeric classes do.
18
+
9
19
  ## [v0.2.5] — 2025-02-26
10
20
 
11
21
  Technical update after release to rubygems.org.
@@ -22,7 +32,7 @@ README was updated to reflect this change.
22
32
  - Make `VectorNumber.new` accept options when initializing from a VectorNumber
23
33
  instead of only copying. Options will be merged.
24
34
  - Remove `Initializing` module, move its methods to the actual class.
25
- - Updated development gem versions.
35
+ - Update development gems' versions.
26
36
 
27
37
  **Fixed**
28
38
  - `#dup` and `#clone` now behave exactly like Numeric versions, preventing unfreezing.
@@ -3,10 +3,10 @@
3
3
  class VectorNumber
4
4
  # Various mathematical operations that are also conversions.
5
5
  module MathConverting
6
- # Return the absolute value of a vector, i.e. its length.
6
+ # Return the absolute value of the vector, i.e. its length.
7
7
  # @return [Float]
8
8
  def abs
9
- Math.sqrt(coefficients.reduce(0.0) { |result, coefficient| result + coefficient.abs2 })
9
+ Math.sqrt(coefficients.sum(&:abs2)) # rubocop:disable Naming/VariableNumber
10
10
  end
11
11
 
12
12
  alias magnitude abs
@@ -64,12 +64,13 @@ class VectorNumber
64
64
  end
65
65
 
66
66
  # Divide all coefficients by a real number, returning new vector.
67
- # This effectively multiplies magnitude by reciprocal of the specified factor.
67
+ # This effectively multiplies magnitude by reciprocal of +other+.
68
68
  # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
69
69
  # @return [VectorNumber]
70
70
  # @raise [RangeError] if +other+ is not a number or is not a real number
71
+ # @raise [ZeroDivisionError] if +other+ is zero
71
72
  def /(other)
72
- raise RangeError, "can't divide #{self} by #{other}" unless real_number?(other)
73
+ check_divisibility(other)
73
74
 
74
75
  other = other.real
75
76
  # Prevent integer division, but without loss of accuracy.
@@ -78,16 +79,87 @@ class VectorNumber
78
79
  new { _1 / other }
79
80
  end
80
81
 
82
+ alias quo /
83
+
81
84
  # Divide all coefficients by a real number using +fdiv+, returning new vector
82
85
  # with Float coefficients.
83
86
  # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
84
87
  # @return [VectorNumber]
85
88
  # @raise [RangeError] if +other+ is not a number or is not a real number
89
+ # @raise [ZeroDivisionError] if +other+ is zero
86
90
  def fdiv(other)
87
- raise RangeError, "can't divide #{self} by #{other}" unless real_number?(other)
91
+ check_divisibility(other)
88
92
 
89
93
  other = other.real
90
94
  new { _1.fdiv(other) }
91
95
  end
96
+
97
+ # Divide all coefficients by +other+, rounding results with {#floor}.
98
+ # This is requal to +(self / other).floor+.
99
+ # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
100
+ # @return [VectorNumber]
101
+ # @raise [RangeError] if +other+ is not a number or is not a real number
102
+ # @raise [ZeroDivisionError] if +other+ is zero
103
+ # @see #divmod
104
+ # @see #%
105
+ def div(other)
106
+ check_divisibility(other)
107
+
108
+ other = other.real
109
+ new { _1.div(other) }
110
+ end
111
+
112
+ # Return the modulus of dividing self by +other+ as a vector.
113
+ # This is equal to +self - other * (self/other).floor+.
114
+ # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
115
+ # @return [VectorNumber]
116
+ # @raise [RangeError] if +other+ is not a number or is not a real number
117
+ # @raise [ZeroDivisionError] if +other+ is zero
118
+ # @see #divmod
119
+ # @see #div
120
+ # @see #remainder for alternative
121
+ # @see Numeric#% for examples
122
+ def %(other)
123
+ check_divisibility(other)
124
+
125
+ other = other.real
126
+ new { _1 % other }
127
+ end
128
+
129
+ alias modulo %
130
+
131
+ # Return the quotient and modulus of dividing self by +other+.
132
+ # There is no performance benefit compared to calling {#div} and {#%} separately.
133
+ # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
134
+ # @return [Array(VectorNumber, VectorNumber)]
135
+ # @raise [RangeError] if +other+ is not a number or is not a real number
136
+ # @raise [ZeroDivisionError] if +other+ is zero
137
+ # @see #div
138
+ # @see #%
139
+ def divmod(other)
140
+ [div(other), modulo(other)]
141
+ end
142
+
143
+ # Return the remainder of dividing self by +other+ as a vector.
144
+ # This is equal to +self - other * (self/other).truncate+.
145
+ # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
146
+ # @return [VectorNumber]
147
+ # @raise [RangeError] if +other+ is not a number or is not a real number
148
+ # @raise [ZeroDivisionError] if +other+ is zero
149
+ # @see #% for alternative
150
+ # @see Numeric#remainder for examples
151
+ def remainder(other)
152
+ check_divisibility(other)
153
+
154
+ other = other.real
155
+ new { _1.remainder(other) }
156
+ end
157
+
158
+ private
159
+
160
+ def check_divisibility(other)
161
+ raise RangeError, "can't divide #{self} by #{other}", caller unless real_number?(other)
162
+ raise ZeroDivisionError, "divided by 0", caller if other.zero?
163
+ end
92
164
  end
93
165
  end
@@ -65,14 +65,14 @@ class VectorNumber
65
65
  !zero? && all? { |_u, c| c.negative? }
66
66
  end
67
67
 
68
- # Always returns +false+.
68
+ # Always returns +false+, as vectors are never real numbers.
69
69
  # @see #numeric?
70
70
  # @return [false]
71
71
  def real?
72
72
  false
73
73
  end
74
74
 
75
- # Always returns +false+.
75
+ # Always returns +false+, as vectors are not +Integer+s.
76
76
  # @return [false]
77
77
  def integer?
78
78
  false
@@ -48,7 +48,7 @@ class VectorNumber
48
48
  # @raise [ArgumentError] if +mult+ is not in {MULT_STRINGS}'s keys
49
49
  def value_to_s(unit, coefficient, mult:)
50
50
  if !mult.is_a?(String) && !MULT_STRINGS.key?(mult)
51
- raise ArgumentError, "unknown key :#{mult}", caller
51
+ raise ArgumentError, "unknown key #{mult.inspect}", caller
52
52
  end
53
53
 
54
54
  case unit
@@ -2,5 +2,5 @@
2
2
 
3
3
  class VectorNumber # rubocop:disable Style/StaticClass
4
4
  # @return [String]
5
- VERSION = "0.2.5"
5
+ VERSION = "0.2.6"
6
6
  end
@@ -1,3 +1,19 @@
1
+ type vector_type = VectorNumber
2
+ type real_number = Integer | Float | Rational | BigDecimal
3
+
4
+ type in_value_type = untyped
5
+ type unit_type = untyped
6
+ type coefficient_type = real_number
7
+
8
+ type list[T] = Array[T]
9
+
10
+ type plain_vector_type = Hash[unit_type, coefficient_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]
14
+
15
+ type options_type = Hash[Symbol, untyped]
16
+
1
17
  interface _BaseMethods
2
18
  def size: -> Integer
3
19
  def options: -> options_type
@@ -82,8 +98,22 @@ class VectorNumber
82
98
  def *: (real_number | vector_type) -> vector_type
83
99
 
84
100
  def /: (real_number | vector_type) -> vector_type
101
+ alias quo /
85
102
 
86
103
  def fdiv: (real_number | vector_type) -> vector_type
104
+
105
+ def div: (real_number | vector_type) -> vector_type
106
+
107
+ def %: (real_number | vector_type) -> vector_type
108
+ alias modulo %
109
+
110
+ def divmod: (real_number | vector_type) -> [vector_type, vector_type]
111
+
112
+ def remainder: (real_number | vector_type) -> vector_type
113
+
114
+ private
115
+
116
+ def check_divisibility: (real_number | vector_type) -> void
87
117
  end
88
118
 
89
119
  # Various mathematical operations that are also conversions.
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vector_number
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Bulancov
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-25 00:00:00.000000000 Z
10
+ date: 2025-04-30 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  executables: []
13
13
  extensions: []
@@ -27,7 +27,6 @@ files:
27
27
  - lib/vector_number/querying.rb
28
28
  - lib/vector_number/stringifying.rb
29
29
  - lib/vector_number/version.rb
30
- - sig/definitions.rbs
31
30
  - sig/vector_number.rbs
32
31
  homepage: https://github.com/trinistr/vector_number
33
32
  licenses:
data/sig/definitions.rbs DELETED
@@ -1,15 +0,0 @@
1
- type vector_type = VectorNumber
2
- type real_number = Integer | Float | Rational | BigDecimal
3
-
4
- type in_value_type = untyped
5
- type unit_type = untyped
6
- type coefficient_type = real_number
7
-
8
- type list[T] = Array[T]
9
-
10
- type plain_vector_type = Hash[unit_type, coefficient_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]
14
-
15
- type options_type = Hash[Symbol, untyped]