Module: VectorNumber::MathConverting
- Included in:
- VectorNumber
- Defined in:
- lib/vector_number/math_converting.rb
Overview
Various mathematical operations that are also conversions.
Instance Method Summary collapse
-
#abs ⇒ Float
(also: #magnitude)
Calculate the absolute value of the vector, i.e.
-
#abs2 ⇒ Float
Calculate the square of absolute value.
-
#ceil(digits = 0) ⇒ VectorNumber
Return a new vector with every coefficient rounded using their
#ceil
. -
#floor(digits = 0) ⇒ VectorNumber
Return a new vector with every coefficient rounded using their
#floor
. -
#round(digits = 0, half: :up) ⇒ VectorNumber
Return a new vector with every coefficient rounded using their
#round
. -
#truncate(digits = 0) ⇒ VectorNumber
Return a new vector with every coefficient truncated using their
#truncate
.
Instance Method Details
#abs ⇒ Float Also known as: magnitude
Calculate the absolute value of the vector, i.e. its length.
16 17 18 |
# File 'lib/vector_number/math_converting.rb', line 16 def abs Math.sqrt(abs2) end |
#abs2 ⇒ Float
Calculate the square of absolute value.
32 33 34 |
# File 'lib/vector_number/math_converting.rb', line 32 def abs2 coefficients.sum(&:abs2) end |
#ceil(digits = 0) ⇒ VectorNumber
Return a new vector with every coefficient rounded using their #ceil
.
66 67 68 |
# File 'lib/vector_number/math_converting.rb', line 66 def ceil(digits = 0) new { _1.ceil(digits) } end |
#floor(digits = 0) ⇒ VectorNumber
Return a new vector with every coefficient rounded using their #floor
.
83 84 85 |
# File 'lib/vector_number/math_converting.rb', line 83 def floor(digits = 0) new { _1.floor(digits) } end |
#round(digits = 0, half: :up) ⇒ VectorNumber
Return a new vector with every coefficient rounded using their #round
.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/vector_number/math_converting.rb', line 105 def round(digits = 0, half: :up) if defined?(BigDecimal) bd_mode = case half when :down then :half_down when :even then :half_even else :half_up end new { _1.is_a?(BigDecimal) ? _1.round(digits, bd_mode) : _1.round(digits, half: half) } # :nocov: else new { _1.round(digits, half: half) } end # :nocov: end |
#truncate(digits = 0) ⇒ VectorNumber
Return a new vector with every coefficient truncated using their #truncate
.
49 50 51 |
# File 'lib/vector_number/math_converting.rb', line 49 def truncate(digits = 0) new { _1.truncate(digits) } end |