Class: Array

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

Instance Method Summary collapse

Instance Method Details

#denominatorsArray Also known as: consequents

Returns of denominators of elements of self.

Examples:

[Tonal::Ratio.new(3,2), Tonal::Ratio.new(5,4)].denominators => [2, 4]

Returns:

  • (Array)

    of denominators of elements of self



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

def denominators = self.map(&:denominator)

#denominizeArray

Returns an array of ratios with equalized denominators.

Examples:

[4/3r, 3/2r].denominize => [(8/6), (9/6)]

Returns:

  • (Array)

    an array of ratios with equalized denominators



320
321
322
323
# File 'lib/tonal/extensions.rb', line 320

def denominize
  l = denominators.lcm
  map{|r| Tonal::Ratio.new(l / r.denominator * r.numerator, l)}
end

#lcmInteger

Returns least common multiple of integer elements of self.

Examples:

[3, 2, 7].lcm => 42

Returns:

  • (Integer)

    least common multiple of integer elements of self



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

def lcm = self.reduce(1, :lcm)

#meanFloat

Returns the mean of the elements of self.

Examples:

[1, 2].mean => 1.5

Returns:

  • (Float)

    the mean of the elements of self



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

def mean = self.sum / self.count.to_f

#modulo_translate(lower = 0, upper) ⇒ Array

Returns translated modularly.

Examples:

[-6.617469071022061, 4.755369851099594, 7.588140911919945, -6.49706614430203].modulo_translate(-3, 5)
=> [1.382530928977939, 4.755369851099594,-0.411859088080055,  1.50293385569797]

Parameters:

  • lower (defaults to: 0)

    the lower bound of the modulo range

  • upper

    the upper bound of the modulo range

Returns:

  • (Array)

    translated modularly



376
377
378
379
380
381
# File 'lib/tonal/extensions.rb', line 376

def modulo_translate(lower=0, upper)
  range = (upper - lower) == 0 ? 1 : upper - lower
  map do |value|
    (value - lower) % range + lower
  end
end

#numeratorsArray Also known as: antecedents

Returns of numerators of elements of self.

Examples:

[3/2r, 5/4r].numerators => [3, 5]

Returns:

  • (Array)

    of numerators of elements of self



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

def numerators = self.map(&:numerator)

#ratio_from_prime_divisions(reduced: false) ⇒ Tonal::ReducedRatio

Returns ratio reconstructed from the result of a prime factor decomposition.

Examples:

[[[3, 1]], [[2, 1]]].ratio_from_prime_divisions => (3/2)

Parameters:

  • reduced (Boolean) (defaults to: false)

    if a reduced or unreduced ratio is returned

Returns:

  • (Tonal::ReducedRatio)

    ratio reconstructed from the result of a prime factor decomposition



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

def ratio_from_prime_divisions(reduced: false) = reduced ? Tonal::ReducedRatio.new(Prime.int_from_prime_division(self.first), Prime.int_from_prime_division(self.last)) : Tonal::Ratio.new(Prime.int_from_prime_division(self.first), Prime.int_from_prime_division(self.last))

#rescale(new_min = 0, new_max) ⇒ Array

Returns rescaled by new minimum and new maximum.

Examples:

[0.47943068514319154, 0.7161083818132802, 0.19855867360591783].rescale(0,3)
=> [1.6280871600341376, 3.0, 0.0]

Parameters:

  • new_min (defaults to: 0)
  • new_max

Returns:

  • (Array)

    rescaled by new minimum and new maximum



360
361
362
363
364
365
366
367
# File 'lib/tonal/extensions.rb', line 360

def rescale(new_min=0, new_max)
  old_min = min
  old_max = max

  self.map do |x|
    new_min + ((x - old_min) * (new_max - new_min)) / (old_max - old_min)
  end
end

#rpad(min_size, value = nil) ⇒ Array

Returns padded to the right up to n, with value. value default is nil.

Examples:

[3,2].rpad(3, 12) => [3, 2, 12]

Parameters:

  • min_size
  • value (defaults to: nil)

Returns:

  • (Array)

    padded to the right up to n, with value. value default is nil



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

def rpad(min_size, value = nil) = self.dup.rpad!(min_size, value)

#rpad!(min_size, value = nil) ⇒ Array

Returns self replaced by array padded to the right up to n, with value. value default is nil.

Examples:

[3,2].rpad!(3, 12) => [3, 2, 12]

Parameters:

  • min_size
  • value (defaults to: nil)

Returns:

  • (Array)

    self replaced by array padded to the right up to n, with value. value default is nil



276
277
278
279
# File 'lib/tonal/extensions.rb', line 276

def rpad!(min_size, value = nil)
  self.length > min_size ? self : (min_size - self.length).times { self << value }
  self
end

#to_centsArray Also known as: cents

Returns of cent values for ratio or rational elements of self.

Examples:

[3/2r, 4/3r].to_cents => [701.96, 498.04]

Returns:

  • (Array)

    of cent values for ratio or rational elements of self



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

def to_cents = self.map{|r| r.to_cents}

#to_vectorVector Also known as: vector

Returns self converted to a vector.

Examples:

[3,2].to_vector => Vector[3, 2]

Returns:

  • (Vector)

    self converted to a vector



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

def to_vector = Vector[*self]

#translate(value) ⇒ Array

Returns translated by value.

Examples:

[0.24184760813024642, 0.49344034900361244, 0.07231824070126536].translate(-0.07231824070126536) = [0.16952936742898106, 0.4211221083023471, 0.0]

Parameters:

  • value (Numeric)

    the value that is translating self

Returns:

  • (Array)

    translated by value



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

def translate(value) = self.map{|e| e + value}