Module: VectorNumber::Mathing
- Included in:
- VectorNumber
- Defined in:
- lib/vector_number/mathing.rb
Overview
Methods for performing actual math.
All operators (like *) have aliases (like mult
) to make method chaining easier and more natural.
Instance Method Summary collapse
-
#%(other) ⇒ VectorNumber
(also: #modulo)
Return the modulus of dividing self by a real
other
as a vector. -
#*(other) ⇒ VectorNumber
(also: #mult)
Multiply all coefficients by a real
other
, returning new vector. -
#+(other) ⇒ VectorNumber
(also: #add)
Return new vector as a sum of this and
other
value. -
#-(other) ⇒ VectorNumber
(also: #sub)
Return new vector as a sum of this and additive inverse of
other
value. -
#-@ ⇒ VectorNumber
(also: #neg)
Return new vector with negated coefficients (additive inverse).
-
#/(other) ⇒ VectorNumber
(also: #quo)
Divide all coefficients by a real
other
, returning new vector. -
#coerce(other) ⇒ Array(VectorNumber, VectorNumber)
The coerce method provides support for Ruby type coercion.
-
#div(other) ⇒ VectorNumber
Divide all coefficients by a real
other
, rounding results with#floor
. -
#divmod(other) ⇒ Array(VectorNumber, VectorNumber)
Return the quotient and modulus of dividing self by a real
other
. -
#fdiv(other) ⇒ VectorNumber
Divide all coefficients by a real
other
usingfdiv
, returning new vector with Float (or BigDecimal) coefficients. -
#remainder(other) ⇒ VectorNumber
Return the remainder of dividing self by a real
other
as a vector.
Instance Method Details
#%(other) ⇒ VectorNumber Also known as: modulo
Return the modulus of dividing self by a real other
as a vector.
This is equal to self - other * (self/other).floor, or, alternatively, self - other * self.div(other).
257 258 259 260 261 262 |
# File 'lib/vector_number/mathing.rb', line 257 def %(other) check_divisibility(other) other = other.real new { _1 % other } end |
#*(other) ⇒ VectorNumber Also known as: mult
Multiply all coefficients by a real other
, returning new vector.
This effectively multiplies VectorNumber::MathConverting#magnitude by other
.
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/vector_number/mathing.rb', line 115 def *(other) if real_number?(other) other = other.real # @type var other: Float new { _1 * other } elsif real_number?(self) && other.is_a?(self.class) # @type var other: untyped other * self else raise RangeError, "can't multiply #{self} and #{other}" end end |
#+(other) ⇒ VectorNumber Also known as: add
Return new vector as a sum of this and other
value. This is analogous to VectorNumber.[].
66 67 68 |
# File 'lib/vector_number/mathing.rb', line 66 def +(other) new([self, other]) end |
#-(other) ⇒ VectorNumber Also known as: sub
89 90 91 |
# File 'lib/vector_number/mathing.rb', line 89 def -(other) self + new([other], &:-@) end |
#-@ ⇒ VectorNumber Also known as: neg
Return new vector with negated coefficients (additive inverse).
44 45 46 |
# File 'lib/vector_number/mathing.rb', line 44 def -@ new(&:-@) end |
#/(other) ⇒ VectorNumber Also known as: quo
This method never does integer division.
Divide all coefficients by a real other
, returning new vector.
This effectively multiplies VectorNumber::MathConverting#magnitude by reciprocal of other
.
153 154 155 156 157 158 159 160 161 |
# File 'lib/vector_number/mathing.rb', line 153 def /(other) check_divisibility(other) other = other.real # Prevent integer division, but without loss of accuracy. other = Rational(other) if other.integer? # @type var other: Float new { _1 / other } end |
#coerce(other) ⇒ Array(VectorNumber, VectorNumber)
The coerce method provides support for Ruby type coercion.
Unlike other numeric types, VectorNumber can coerce anything.
25 26 27 28 29 30 31 32 |
# File 'lib/vector_number/mathing.rb', line 25 def coerce(other) case other when VectorNumber [other, self] else [new([other]), self] end end |
#div(other) ⇒ VectorNumber
Divide all coefficients by a real other
, rounding results with #floor
.
This is requal to (self / other).floor.
220 221 222 223 224 225 |
# File 'lib/vector_number/mathing.rb', line 220 def div(other) check_divisibility(other) other = other.real new { _1.div(other) } end |
#divmod(other) ⇒ Array(VectorNumber, VectorNumber)
290 291 292 |
# File 'lib/vector_number/mathing.rb', line 290 def divmod(other) [div(other), modulo(other)] end |
#fdiv(other) ⇒ VectorNumber
Divide all coefficients by a real other
using fdiv
, returning new vector with Float (or BigDecimal) coefficients.
There isn’t much benefit to this method, as #/ doesn’t do integer division, but it is provided for consistency.
189 190 191 192 193 194 |
# File 'lib/vector_number/mathing.rb', line 189 def fdiv(other) check_divisibility(other) other = other.real new { _1.fdiv(other) } end |
#remainder(other) ⇒ VectorNumber
Return the remainder of dividing self by a real other
as a vector.
This is equal to self - other * (self/other).truncate.
321 322 323 324 325 326 |
# File 'lib/vector_number/mathing.rb', line 321 def remainder(other) check_divisibility(other) other = other.real new { _1.remainder(other) } end |