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



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

def benedetti_height = self.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



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

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

#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



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

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

#efficiency(modulo) ⇒ Float

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

Examples:

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

Parameters:

  • modulo

Returns:

  • (Float)

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



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

def efficiency(modulo) = (Tonal::Cents::CENT_SCALE * step(modulo).step / modulo.to_f) - to_cents

#hzTonal::Hertz Also known as: to_hz

Returns of self.

Returns:



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

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

#interval_with(ratio) ⇒ Tonal::Interval

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

Examples:

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

Parameters:

  • ratio

Returns:



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

def interval_with(ratio) = Tonal::Interval.new(self.ratio, 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



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

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:



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

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



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

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.5849625007211563

Returns:



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

def log_weil_height = self.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



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

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



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

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

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

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:



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

def mirror(axis=1/1r) = self.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



18
19
20
21
# File 'lib/tonal/extensions.rb', line 18

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

#negativeTonal::ReducedRatio

Returns , the Ernst Levy negative of self.

Examples:

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

Returns:



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

def negative = self.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



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

def period_degrees = self.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



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

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



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

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

#prime_vectorVector Also known as: monzo

Returns , self represented as a prime vector.

Examples:

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

Returns:

  • (Vector)

    , self represented as a prime vector



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

def prime_vector = self.ratio.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



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

def step(modulo=12) = to_log2.step(modulo)

#tenney_heightInteger Also known as: log_product_complexity

Returns the product complexity of self.

Examples:

(3/2r).tenney_height => 2.584962500721156

Returns:

  • (Integer)

    the product complexity of self



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

def tenney_height = self.ratio.tenney_height

#to_centsTonal::Cents

Returns of self interpreted as a ratio.

Examples:

(3/2r).to_cents => 701.96

Returns:



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

def to_cents = self.log2.to_cents

#to_ratio(reduced: true, equave: 2) ⇒ Tonal::ReducedRatio Also known as: ratio

Returns the octave reduced ratio of self.

Examples:

(2**(1.0/12)).ratio => (4771397596969315/4503599627370496)

Returns:



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

def to_ratio(reduced: true, equave: 2/1r) = reduced ? Tonal::ReducedRatio.new(self, equave: equave) : Tonal::Ratio.new(self, 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



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

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

#weil_heightInteger

Returns the Weil height.

Examples:

(3/2r).weil_height => 3

Returns:



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

def weil_height = self.ratio.weil_height

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

Returns the Wilson height.

Examples:

(14/9r).wilson_height => 13

Returns:



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

def wilson_height(reduced: true, equave: 2/1r, prime_rejects: [2]) = self.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



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

def ¢ = cents