Module: VectorSpace::Arithmetic
- Defined in:
- lib/vector_space/arithmetic.rb
Overview
These operations depend only upon addition and multiplication of the underlying values, so work in a vector space
Instance Method Summary collapse
-
#*(n) ⇒ Object
Scalar multiplication.
-
#+(vector) ⇒ Object
Vector addition.
-
#-(vector) ⇒ Object
Vector subtraction (by addition).
-
#-@ ⇒ Object
Additive inverse.
-
#/(n) ⇒ Object
Scalar division (by multiplication).
Instance Method Details
#*(n) ⇒ Object
Scalar multiplication
14 15 16 |
# File 'lib/vector_space/arithmetic.rb', line 14 def *(n) operate_on_values { |value| value * n } end |
#+(vector) ⇒ Object
Vector addition
5 6 7 8 9 10 11 |
# File 'lib/vector_space/arithmetic.rb', line 5 def +(vector) if compatible_with?(vector) operate_on_values(vector) { |a, b| a + b } else raise ArgumentError, "can't add #{vector.inspect} to #{self.inspect}" end end |
#-(vector) ⇒ Object
Vector subtraction (by addition)
24 25 26 |
# File 'lib/vector_space/arithmetic.rb', line 24 def -(vector) self + (-vector) end |
#-@ ⇒ Object
Additive inverse
19 20 21 |
# File 'lib/vector_space/arithmetic.rb', line 19 def -@ operate_on_values { |value| -value } end |
#/(n) ⇒ Object
Scalar division (by multiplication)
29 30 31 |
# File 'lib/vector_space/arithmetic.rb', line 29 def /(n) self * (1.0 / n) end |