Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/tonal/extensions.rb

Instance Method Summary collapse

Instance Method Details

#benedetti_heightFloat Also known as: product_complexity

Returns the log product complexity of self.

Examples:

(3/2r).benedetti_height => 6

Returns:

  • (Float)

    the log product complexity of self



112
# File 'lib/tonal/extensions.rb', line 112

def benedetti_height = ratio.benedetti_height

#centsTonal::Cents

Returns of self interpreted as a cents quantity.

Examples:

700.0.cents => 700.0

Returns:

  • (Tonal::Cents)

    of self interpreted as a cents quantity



82
# File 'lib/tonal/extensions.rb', line 82

def cents = Tonal::Cents.new(cents: self)

#cents_difference_with(other_ratio) ⇒ Tonal::Cents

Returns difference between ratio (upper) and self (lower).

Examples:

(133).cents_difference_with(3/2r)
=> 635.62

Parameters:

  • other_ratio

Returns:

  • (Tonal::Cents)

    difference between ratio (upper) and self (lower)



163
# File 'lib/tonal/extensions.rb', line 163

def cents_difference_with(other_ratio) = interval_with(other_ratio).to_cents

#div_times(factor) ⇒ Array

Returns a tuple of self divided and multiplied by factor.

Examples:

Math::PI.div_times(3) => [1.0471975511965976, 9.42477796076938]

Parameters:

Returns:

  • (Array)

    a tuple of self divided and multiplied by factor



40
# File 'lib/tonal/extensions.rb', line 40

def div_times(factor) = [self / factor, self * factor]

#efficiency(modulo, reduced: false) ⇒ Float

We want the efficiency from the ratio (self)

Examples:

(3/2r).efficiency(12) => -1.96

Parameters:

  • modulo

Returns:

  • (Float)

    the cents difference between self and its step in the given modulo



148
# File 'lib/tonal/extensions.rb', line 148

def efficiency(modulo, reduced: false) = ratio(reduced: reduced).efficiency(modulo)

#hzTonal::Hertz Also known as: to_hz

Returns of self.

Returns:



98
# File 'lib/tonal/extensions.rb', line 98

def hz = Tonal::Hertz.new(self)

#interval_with(other_ratio) ⇒ Tonal::Interval

Returns beween self (upper) and ratio (lower).

Examples:

(133).interval_with(3/2r) => 133/96 (133/128 / 3/2)

Parameters:

  • other_ratio

Returns:



155
# File 'lib/tonal/extensions.rb', line 155

def interval_with(other_ratio) = Tonal::Interval.new(ratio, other_ratio)

#log(base) ⇒ Tonal::Log

Returns the log of self to the given base.

Examples:

(3/2r).log(10) => 0.17609125905568124

Returns:

  • (Tonal::Log)

    the log of self to the given base



68
# File 'lib/tonal/extensions.rb', line 68

def log(base) = Tonal::Log.new(logarithmand: self, base: base)

#log2Tonal::Log2 Also known as: to_log2, span

Returns the log2 of self.

Examples:

(3/2r).log2 => 0.5849625007211562

Returns:



74
# File 'lib/tonal/extensions.rb', line 74

def log2 = Tonal::Log2.new(logarithmand: self)

#log_floor(base = 10) ⇒ Integer

Returns the floor of the log (to the given base) of self.

Examples:

Math::PI.log_floor(2) => 1

Parameters:

  • base (defaults to: 10)

    of the log

Returns:

  • (Integer)

    the floor of the log (to the given base) of self



217
# File 'lib/tonal/extensions.rb', line 217

def log_floor(base=10) = Math.log(self, base).floor

#log_weil_heightTonal::Log2

Returns the log of Weil height.

Examples:

(3/2r).log_weil_height => 1.58

Returns:



132
# File 'lib/tonal/extensions.rb', line 132

def log_weil_height = ratio.log_weil_height

#max_primeInteger

Returns the maximum prime factor of self.

Examples:

(31/30r).max_prime => 31

Returns:

  • (Integer)

    the maximum prime factor of self



184
# File 'lib/tonal/extensions.rb', line 184

def max_prime = prime_divisions.flatten(1).map(&:first).max

#min_primeInteger

Returns the minimum prime factor of self.

Examples:

(31/30r).min_prime => 2

Returns:

  • (Integer)

    the minimum prime factor of self



190
# File 'lib/tonal/extensions.rb', line 190

def min_prime = prime_divisions.flatten(1).map(&:first).min

#mirror(axis = 1) ⇒ Tonal::Ratio

Returns , the ratio rotated on the given axis, default 1/1.

Examples:

(3/2r).mirror => (4/3)

Parameters:

  • axis (defaults to: 1)

    around which self is mirrored

Returns:

  • (Tonal::Ratio)

    , the ratio rotated on the given axis, default 1/1



210
# File 'lib/tonal/extensions.rb', line 210

def mirror(axis=1/1r) = ratio.mirror(axis)

#modulo_translate(lower = 0, upper) ⇒ Numeric

Returns translated modularly.

Examples:

Math::PI.modulo_translate(-3, 3) => -2.858407346410207

Parameters:

  • lower (defaults to: 0)

    the lower bound of the modulo range

  • upper

    the upper bound of the modulo range

Returns:

  • (Numeric)

    translated modularly



21
22
23
24
# File 'lib/tonal/extensions.rb', line 21

def modulo_translate(lower=0, upper)
  range = (upper - lower) == 0 ? 1 : upper - lower
  (self - lower) % range + lower
end

#negativeTonal::Ratio

Returns , the Ernst Levy negative of self.

Examples:

(7/4r).negative => (12/7)

Returns:



203
# File 'lib/tonal/extensions.rb', line 203

def negative = ratio.negative

#period_degreesFloat

Returns , the degrees on a circle of self.

Examples:

(2**(6.0/12)).period_degrees => 180.0

Returns:

  • (Float)

    , the degrees on a circle of self



62
# File 'lib/tonal/extensions.rb', line 62

def period_degrees = ratio.period_degrees

#plus_minus(offset) ⇒ Array Also known as: min_plus

Returns a tuple of self offset positively/negatively.

Examples:

Math::PI.plus_minus(3)
  => [6.141592653589793, 0.14159265358979312]

Parameters:

  • offset

    plus and minus distance from self

Returns:

  • (Array)

    a tuple of self offset positively/negatively



32
# File 'lib/tonal/extensions.rb', line 32

def plus_minus(offset) = [self + offset, self - offset]

#prime_divisionsArray

Returns , self decomposed into its prime factors.

Examples:

(31/30r).prime_divisions => [[[31, 1]], [[2, 1], [3, 1], [5, 1]]]

Returns:

  • (Array)

    , self decomposed into its prime factors



178
# File 'lib/tonal/extensions.rb', line 178

def prime_divisions = [self.numerator.prime_division, self.denominator.prime_division]

#prime_vector(reduced: false) ⇒ Vector Also known as: monzo, prime_exponent_vector

Returns , self represented as a prime vector.

Examples:

(3/2r).prime_vector => Vector[-1, 1]

Parameters:

  • reduced (defaults to: false)

Returns:

  • (Vector)

    , self represented as a prime vector



170
# File 'lib/tonal/extensions.rb', line 170

def prime_vector(reduced: false) = ratio(reduced: reduced).prime_vector

#step(modulo = 12) ⇒ Tonal::Step

Returns the step of self in the given modulo.

Examples:

(5/4r).step(12) => 4\12

Parameters:

  • modulo (defaults to: 12)

Returns:

  • (Tonal::Step)

    the step of self in the given modulo



106
# File 'lib/tonal/extensions.rb', line 106

def step(modulo=12) = Tonal::Step.new(ratio: self, modulo: modulo)

#tenney_heightInteger Also known as: log_product_complexity

Returns the product complexity of self.

Examples:

(3/2r).tenney_height => 2.58

Returns:

  • (Integer)

    the product complexity of self



119
# File 'lib/tonal/extensions.rb', line 119

def tenney_height = ratio.tenney_height

#to_centsTonal::Cents

Returns of self interpreted as a ratio.

Examples:

(3/2r).to_cents => 701.96

Returns:



94
# File 'lib/tonal/extensions.rb', line 94

def to_cents = Tonal::Cents.new(ratio: self)

#to_ratio(reduced: false, equave: 2) ⇒ Tonal::Ratio Also known as: ratio

Returns the octave reduced ratio of self.

Examples:

(4/5r).to_ratio => 4/5

Parameters:

  • reduced (defaults to: false)
  • equave (defaults to: 2)

Returns:



48
# File 'lib/tonal/extensions.rb', line 48

def to_ratio(reduced: false, equave: 2/1r) = reduced ? Tonal::ReducedRatio.new(self, equave: equave) : Tonal::Ratio.new(self, equave: equave)

#to_reduced_ratio(equave: 2) ⇒ Tonal::ReducedRatio

Examples:

{4/5r}.to_reduced_ratio => 8/5

Parameters:

  • equave (defaults to: 2)

Returns:



56
# File 'lib/tonal/extensions.rb', line 56

def to_reduced_ratio(equave: 2/1r) = to_ratio(reduced: true, equave: equave)

#to_vectorVector Also known as: vector

Returns , self expressed as a Vector.

Examples:

(3/2r).to_vector => Vector[3, 2]

Returns:

  • (Vector)

    , self expressed as a Vector



196
# File 'lib/tonal/extensions.rb', line 196

def to_vector = Vector[self.numerator, self.denominator]

#weil_heightInteger

Returns the Weil height.

Examples:

(3/2r).weil_height => 3

Returns:



126
# File 'lib/tonal/extensions.rb', line 126

def weil_height = ratio.weil_height

#wilson_height(reduced: false, equave: 2, prime_rejects: [2]) ⇒ Integer

Returns the Wilson height.

Examples:

(14/9r).wilson_height => 13

Parameters:

  • reduced (defaults to: false)
  • equave (defaults to: 2)
  • prime_rejects (defaults to: [2])

Returns:



140
# File 'lib/tonal/extensions.rb', line 140

def wilson_height(reduced: false, equave: 2/1r, prime_rejects: [2]) = ratio(reduced: reduced, equave: equave).wilson_height(prime_rejects: prime_rejects)

#¢Tonal::Cents

Returns of self interpreted as a cents quantity.

Examples:

700.0.¢ => 700.0

Returns:

  • (Tonal::Cents)

    of self interpreted as a cents quantity



88
# File 'lib/tonal/extensions.rb', line 88

def ¢ = cents