Class: Tonal::Ratio
- Inherits:
-
Object
- Object
- Tonal::Ratio
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/tonal/approximation.rb,
lib/tonal/ratio.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Approximation
Constant Summary collapse
- PRECISION =
2- DEFAULT_EQUAVE =
2
Instance Attribute Summary collapse
-
#antecedent ⇒ Object
(also: #numerator)
readonly
Returns the value of attribute antecedent.
-
#consequent ⇒ Object
(also: #denominator)
readonly
Returns the value of attribute consequent.
-
#equave ⇒ Object
readonly
Returns the value of attribute equave.
-
#label ⇒ String
Symbolic representation of Tonal::Ratio.
-
#reduced_antecedent ⇒ Object
readonly
Returns the value of attribute reduced_antecedent.
-
#reduced_consequent ⇒ Object
readonly
Returns the value of attribute reduced_consequent.
Class Method Summary collapse
-
.ed(step, modulo, equave: DEFAULT_EQUAVE) ⇒ Tonal::Ratio
The ratio of step in the modulo.
-
.random_ratio(number_of_factors = 2, within: 100, reduced: (self == Tonal::ReducedRatio) ? true : false) ⇒ Tonal::Ratio
A randomly generated ratio.
-
.superparticular(n, factor: 1, superpart: :upper) ⇒ Tonal::Ratio
Ratio who’s numerator and denominator are seperated by a difference of 1.
-
.superpartient(n, summand:, factor: 1, superpart: :upper) ⇒ Tonal::Ratio
Ratio who’s numerator and denominator are separated by a summand difference.
-
.within_cents?(cents1, cents2, within) ⇒ Boolean
If pair of ratios are within the given cents limit.
Instance Method Summary collapse
-
#*(rhs) ⇒ Tonal::Ratio
Result of multiplying self and rhs.
-
#**(rhs) ⇒ Tonal::Ratio
Result of raising self to the rhs power.
-
#+(rhs) ⇒ Tonal::Ratio
Result of adding self and rhs.
-
#-(rhs) ⇒ Tonal::Ratio
Result of subtracting rhs from self.
-
#/(rhs) ⇒ Tonal::Ratio
Result of dividing self by rhs.
-
#<=>(rhs) ⇒ Integer
Comparison of self to rhs.
-
#approximate ⇒ Tonal::Ratio::Approximation
Self’s approximation instance.
-
#benedetti_height ⇒ Integer
(also: #product_complexity)
Measures of complexity ==================================.
-
#cent_diff ⇒ Tonal::Cents
Cent difference between self and other ratio.
-
#cents_difference_with(other_ratio, is_lower: true) ⇒ Tonal::Cents
Difference between ratio (upper) and self (lower).
-
#combination ⇒ Integer
(also: #comb)
The sum of antecedent and consequent.
-
#difference ⇒ Integer
(also: #diff)
The difference between antecedent and consequent.
-
#div_times(other_ratio) ⇒ Array
The results of ratio dividing and multiplying self.
-
#efficiency(modulo, is_step_efficiency: false) ⇒ Tonal::Cents
The cents difference between self and its step in the given modulo.
-
#eql?(other) ⇒ Boolean
Set uses Hash as storage and equality of elements is determined according to Object#eql? and Object#hash.
-
#equave_reduce(equave = DEFAULT_EQUAVE) ⇒ Tonal::Ratio
(also: #reduce, #reduced)
Copy of self reduced to the given equave.
-
#equave_reduce!(equave = DEFAULT_EQUAVE) ⇒ Tonal::Ratio
(also: #reduce!, #reduced!)
Self reduced to the given equave.
-
#fraction_reduce ⇒ Tonal::Ratio
Copy of self rationally reduced.
- #hash ⇒ Object
- #initialize(antecedent, consequent = nil, label: nil, equave: DEFAULT_EQUAVE) ⇒ Tonal::Ratio constructor
-
#inspect ⇒ String
(also: #to_s)
The string representation of Tonal::Ratio.
-
#interval_with(other_ratio, is_lower: true) ⇒ Tonal::Interval
Between self and another ratio.
-
#invert ⇒ Tonal::Ratio
(also: #reflect, #reciprocal)
Copy of self with the antecedent and precedent switched.
-
#invert! ⇒ Tonal::Ratio
With antecedent and precedent switched.
-
#lcm(lhs) ⇒ Integer
The least common multiple with self’s denominator and the given number’s denominator.
-
#log_weil_height ⇒ Tonal::Log2
The log of Weil height.
-
#max_prime ⇒ Integer
The maximum prime factor of self.
-
#mediant_sum(number) ⇒ Tonal::Ratio
(also: #mediant, #farey_sum)
The mediant (Farey) sum of self and another number.
-
#min_prime ⇒ Integer
The minimum prime factor of self.
-
#mirror(axis = 1) ⇒ Tonal::Ratio
The mirror of self along the axis (default 1/1).
-
#negative ⇒ Tonal::ReducedRatio
The Ernst Levy negative of self.
-
#period_degrees(round: PRECISION) ⇒ Float
Degrees.
-
#period_radians(round: PRECISION) ⇒ Float
Radians.
-
#planar_degrees(round: PRECISION) ⇒ Float
Degrees of antecedent (x) and consequent (y) on a 2D plane.
-
#planar_radians(round: PRECISION) ⇒ Float
Radians.
-
#plus_minus(other_ratio) ⇒ Array
(also: #min_plus)
The results of ratio subtracted and added to self.
-
#power(power, root = nil, approximant: 0, by_method: :continued_fraction, from: :lower_ratio) ⇒ Tonal::Ratio
Self raised to the given power and root.
-
#prime_divisions ⇒ Array
, self decomposed into its prime factors.
-
#prime_vector ⇒ Vector
(also: #monzo, #prime_exponent_vector)
, self represented as a prime vector.
-
#ratio ⇒ Tonal::Ratio
(also: #to_ratio)
Convenience method returning self.
-
#scale(a, b = a) ⇒ Tonal::Ratio
Self scaled by given arguments.
-
#shear(a, b = a) ⇒ Tonal::Ratio
Self sheared by given arguments.
-
#step(modulo = 12) ⇒ Integer
The step of self in the given modulo.
-
#tenney_height ⇒ Tonal::Log2
(also: #log_product_complexity, #harmonic_distance)
The log product complexity of self.
-
#to_a ⇒ Array
Antecedent and consequent as elements of Array.
-
#to_cents ⇒ Tonal::Cents
(also: #cents)
Cents value of self.
-
#to_f ⇒ Float
(also: #number)
Self as a Float.
-
#to_interval ⇒ Tonal::Interval
Between 1/1 and self.
-
#to_log(base = 2) ⇒ Tonal::Log
(also: #log)
Math.log of self in given base.
-
#to_log2 ⇒ Tonal::Log2
(also: #log2)
Math.log2 of self.
-
#to_r ⇒ Rational
Self as a Rational.
-
#to_reduced_ratio ⇒ Tonal::ReducedRatio
(also: #reduced_ratio)
Of self.
-
#to_v ⇒ Vector
Antecedent and consequent as elements of Vector.
-
#translate(x = 1, y = 0) ⇒ Tonal::Ratio
Ratio grid transformations numerator mapped on x-axis, denominator mapped on y-axis ==================================.
-
#weil_height ⇒ Integer
The Weil height.
-
#wilson_height(prime_rejects: [2]) ⇒ Integer
The Wilson height.
Constructor Details
#initialize(antecedent, consequent = nil, label: nil, equave: DEFAULT_EQUAVE) ⇒ Tonal::Ratio
20 21 22 23 24 25 |
# File 'lib/tonal/ratio.rb', line 20 def initialize(antecedent, consequent=nil, label: nil, equave: DEFAULT_EQUAVE) raise ArgumentError, "Antecedent must be Numeric" unless antecedent.kind_of?(Numeric) raise ArgumentError, "Consequent must be Numeric or nil" unless (consequent.kind_of?(Numeric) || consequent.nil?) _initialize(antecedent, consequent, label:, equave:) end |
Instance Attribute Details
#antecedent ⇒ Object (readonly) Also known as: numerator
Returns the value of attribute antecedent.
10 11 12 |
# File 'lib/tonal/ratio.rb', line 10 def antecedent @antecedent end |
#consequent ⇒ Object (readonly) Also known as: denominator
Returns the value of attribute consequent.
10 11 12 |
# File 'lib/tonal/ratio.rb', line 10 def consequent @consequent end |
#equave ⇒ Object (readonly)
Returns the value of attribute equave.
10 11 12 |
# File 'lib/tonal/ratio.rb', line 10 def equave @equave end |
#label ⇒ String
Returns symbolic representation of Tonal::Ratio.
553 554 555 |
# File 'lib/tonal/ratio.rb', line 553 def label @label end |
#reduced_antecedent ⇒ Object (readonly)
Returns the value of attribute reduced_antecedent.
10 11 12 |
# File 'lib/tonal/ratio.rb', line 10 def reduced_antecedent @reduced_antecedent end |
#reduced_consequent ⇒ Object (readonly)
Returns the value of attribute reduced_consequent.
10 11 12 |
# File 'lib/tonal/ratio.rb', line 10 def reduced_consequent @reduced_consequent end |
Class Method Details
.ed(step, modulo, equave: DEFAULT_EQUAVE) ⇒ Tonal::Ratio
Returns the ratio of step in the modulo.
83 84 85 |
# File 'lib/tonal/ratio.rb', line 83 def self.ed(step, modulo, equave: DEFAULT_EQUAVE) self.new(2**(step.to_f/modulo), equave:) end |
.random_ratio(number_of_factors = 2, within: 100, reduced: (self == Tonal::ReducedRatio) ? true : false) ⇒ Tonal::Ratio
Returns a randomly generated ratio.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/tonal/ratio.rb', line 65 def self.random_ratio(number_of_factors = 2, within: 100, reduced: (self == Tonal::ReducedRatio) ? true : false) primes = Prime.each(within).to_a nums = [] dens = [] 1.upto(number_of_factors) do nums << [primes[rand(10)], rand(3)] dens << [primes[rand(10)], rand(3)] end [nums, dens].ratio_from_prime_divisions(reduced:) end |
.superparticular(n, factor: 1, superpart: :upper) ⇒ Tonal::Ratio
Returns ratio who’s numerator and denominator are seperated by a difference of 1.
37 38 39 |
# File 'lib/tonal/ratio.rb', line 37 def self.superparticular(n, factor: 1/1r, superpart: :upper) superpartient(n, summand: 1, factor:, superpart:) end |
.superpartient(n, summand:, factor: 1, superpart: :upper) ⇒ Tonal::Ratio
Returns ratio who’s numerator and denominator are separated by a summand difference.
49 50 51 52 53 54 55 56 |
# File 'lib/tonal/ratio.rb', line 49 def self.superpartient(n, summand:, factor: 1/1r, superpart: :upper) case superpart.to_sym.downcase when :lower, :consequent, :denominator self.new(n*factor.numerator, (n+summand)*factor.denominator) else self.new((n+summand)*factor.numerator, n*factor.denominator) end end |
.within_cents?(cents1, cents2, within) ⇒ Boolean
Returns if pair of ratios are within the given cents limit.
94 95 96 |
# File 'lib/tonal/ratio.rb', line 94 def self.within_cents?(cents1, cents2, within) (cents1 - cents2).abs <= within end |
Instance Method Details
#*(rhs) ⇒ Tonal::Ratio
Returns result of multiplying self and rhs.
597 598 599 |
# File 'lib/tonal/ratio.rb', line 597 def *(rhs) operate(rhs, :*) end |
#**(rhs) ⇒ Tonal::Ratio
Returns result of raising self to the rhs power.
615 616 617 |
# File 'lib/tonal/ratio.rb', line 615 def **(rhs) operate(rhs, :**) end |
#+(rhs) ⇒ Tonal::Ratio
Returns result of adding self and rhs.
579 580 581 |
# File 'lib/tonal/ratio.rb', line 579 def +(rhs) operate(rhs, :+) end |
#-(rhs) ⇒ Tonal::Ratio
Returns result of subtracting rhs from self.
588 589 590 |
# File 'lib/tonal/ratio.rb', line 588 def -(rhs) operate(rhs, :-) end |
#/(rhs) ⇒ Tonal::Ratio
Returns result of dividing self by rhs.
606 607 608 |
# File 'lib/tonal/ratio.rb', line 606 def /(rhs) operate(rhs, :/) end |
#<=>(rhs) ⇒ Integer
Returns comparison of self to rhs.
635 636 637 638 639 |
# File 'lib/tonal/ratio.rb', line 635 def <=>(rhs) left = consequent == 0 ? Float::INFINITY : Rational(antecedent, consequent) right = rhs.denominator == 0 ? Float::INFINITY : Rational(rhs.numerator, rhs.denominator) left <=> right end |
#approximate ⇒ Tonal::Ratio::Approximation
Returns self’s approximation instance.
107 108 109 |
# File 'lib/tonal/ratio.rb', line 107 def approximate @approximation end |
#benedetti_height ⇒ Integer Also known as: product_complexity
Measures of complexity
389 390 391 |
# File 'lib/tonal/ratio.rb', line 389 def benedetti_height reduced_antecedent * reduced_consequent end |
#cent_diff ⇒ Tonal::Cents
Returns cent difference between self and other ratio.
469 470 471 |
# File 'lib/tonal/ratio.rb', line 469 def cents_difference_with(other_ratio) to_cents - other_ratio.ratio.to_cents end |
#cents_difference_with(other_ratio, is_lower: true) ⇒ Tonal::Cents
Returns difference between ratio (upper) and self (lower).
466 467 468 |
# File 'lib/tonal/ratio.rb', line 466 def cents_difference_with(other_ratio) to_cents - other_ratio.ratio.to_cents end |
#combination ⇒ Integer Also known as: comb
Returns the sum of antecedent and consequent.
529 530 531 |
# File 'lib/tonal/ratio.rb', line 529 def combination antecedent + consequent end |
#difference ⇒ Integer Also known as: diff
Returns the difference between antecedent and consequent.
520 521 522 |
# File 'lib/tonal/ratio.rb', line 520 def difference antecedent - consequent end |
#div_times(other_ratio) ⇒ Array
Returns the results of ratio dividing and multiplying self.
445 446 447 448 |
# File 'lib/tonal/ratio.rb', line 445 def div_times(other_ratio) other_ratio = self.class.new(other_ratio) [self / other_ratio, self * other_ratio] end |
#efficiency(modulo, is_step_efficiency: false) ⇒ Tonal::Cents
Returns the cents difference between self and its step in the given modulo.
434 435 436 437 438 |
# File 'lib/tonal/ratio.rb', line 434 def efficiency(modulo, is_step_efficiency: false) # For single ratios, we normally want the efficiency from the ratio (self). # If the step efficiency is X cents, then the ratio efficiency is -X cents. step(modulo).efficiency * (is_step_efficiency ? 1.0 : -1.0) end |
#eql?(other) ⇒ Boolean
Set uses Hash as storage and equality of elements is determined according to Object#eql? and Object#hash.
643 644 645 |
# File 'lib/tonal/ratio.rb', line 643 def eql?(other) other.instance_of?(self.class) && to_r == other.to_r end |
#equave_reduce(equave = DEFAULT_EQUAVE) ⇒ Tonal::Ratio Also known as: reduce, reduced
Returns copy of self reduced to the given equave.
217 218 219 |
# File 'lib/tonal/ratio.rb', line 217 def equave_reduce(equave=DEFAULT_EQUAVE) self.class.new(*_equave_reduce(equave)) end |
#equave_reduce!(equave = DEFAULT_EQUAVE) ⇒ Tonal::Ratio Also known as: reduce!, reduced!
Returns self reduced to the given equave.
228 229 230 231 |
# File 'lib/tonal/ratio.rb', line 228 def equave_reduce!(equave=DEFAULT_EQUAVE) @antecedent, @consequent = _equave_reduce(equave) self end |
#fraction_reduce ⇒ Tonal::Ratio
Returns copy of self rationally reduced.
208 209 210 |
# File 'lib/tonal/ratio.rb', line 208 def fraction_reduce self.class.new(to_r) end |
#hash ⇒ Object
647 648 649 650 651 |
# File 'lib/tonal/ratio.rb', line 647 def hash p, q = 17, 37 p = q * @id.hash p = q * @name.hash end |
#inspect ⇒ String Also known as: to_s
Returns the string representation of Tonal::Ratio.
562 563 564 565 566 567 |
# File 'lib/tonal/ratio.rb', line 562 def inspect # Return the "antecedent/consequent", if antecedent is less than 7 digits long; or # Return the floating point representation rounded to PRECISION digits func = -> (n) { n.zero? ? n : Math.log10(n).to_i + 1 } ((func.call(antecedent)) <= 6 ? "#{antecedent}/#{consequent}" : "#{to_f.round(PRECISION)}") end |
#interval_with(other_ratio, is_lower: true) ⇒ Tonal::Interval
Returns between self and another ratio.
490 491 492 493 494 |
# File 'lib/tonal/ratio.rb', line 490 def interval_with(other_ratio, is_lower: true) other_ratio = self.class.new(other_ratio) args = is_lower ? [self, other_ratio] : [other_ratio, self] Tonal::Interval.new(*args) end |
#invert ⇒ Tonal::Ratio Also known as: reflect, reciprocal
Returns copy of self with the antecedent and precedent switched.
248 249 250 |
# File 'lib/tonal/ratio.rb', line 248 def invert self.class.new(consequent, antecedent) end |
#invert! ⇒ Tonal::Ratio
Returns with antecedent and precedent switched.
258 259 260 261 |
# File 'lib/tonal/ratio.rb', line 258 def invert! _initialize(consequent, antecedent, label: label, equave: equave) self end |
#lcm(lhs) ⇒ Integer
Returns the least common multiple with self’s denominator and the given number’s denominator.
476 477 478 |
# File 'lib/tonal/ratio.rb', line 476 def lcm(lhs) [self.denominator, lhs.denominator].lcm end |
#log_weil_height ⇒ Tonal::Log2
Returns the log of Weil height.
417 418 419 |
# File 'lib/tonal/ratio.rb', line 417 def log_weil_height Tonal::Log2.new(logarithmand: weil_height) end |
#max_prime ⇒ Integer
Returns the maximum prime factor of self.
369 370 371 |
# File 'lib/tonal/ratio.rb', line 369 def max_prime prime_divisions.flatten(1).map(&:first).max end |
#mediant_sum(number) ⇒ Tonal::Ratio Also known as: mediant, farey_sum
Returns the mediant (Farey) sum of self and another number.
624 625 626 |
# File 'lib/tonal/ratio.rb', line 624 def mediant_sum(number) self.class.new(antecedent + number.numerator, consequent + number.denominator) end |
#min_prime ⇒ Integer
Returns the minimum prime factor of self.
377 378 379 |
# File 'lib/tonal/ratio.rb', line 377 def min_prime prime_divisions.flatten(1).map(&:first).min end |
#mirror(axis = 1) ⇒ Tonal::Ratio
Returns the mirror of self along the axis (default 1/1).
268 269 270 |
# File 'lib/tonal/ratio.rb', line 268 def mirror(axis=1/1r) (self.class.new(axis) ** 2) / self end |
#negative ⇒ Tonal::ReducedRatio
Returns the Ernst Levy negative of self.
276 277 278 |
# File 'lib/tonal/ratio.rb', line 276 def negative self.class.new(3/2r) / self end |
#period_degrees(round: PRECISION) ⇒ Float
Returns degrees.
190 191 192 |
# File 'lib/tonal/ratio.rb', line 190 def period_degrees(round: PRECISION) (360.0 * Math.log(to_f, equave)).round(round) end |
#period_radians(round: PRECISION) ⇒ Float
Returns radians.
199 200 201 |
# File 'lib/tonal/ratio.rb', line 199 def period_radians(round: PRECISION) (2 * Math::PI * Math.log(to_f, equave)).round(round) end |
#planar_degrees(round: PRECISION) ⇒ Float
Returns degrees of antecedent (x) and consequent (y) on a 2D plane.
324 325 326 |
# File 'lib/tonal/ratio.rb', line 324 def planar_degrees(round: PRECISION) (Math.atan2(consequent, antecedent) * 180/Math::PI).round(round) end |
#planar_radians(round: PRECISION) ⇒ Float
Returns radians.
333 334 335 |
# File 'lib/tonal/ratio.rb', line 333 def planar_radians(round: PRECISION) Math.atan2(consequent, antecedent).round(round) end |
#plus_minus(other_ratio) ⇒ Array Also known as: min_plus
Returns the results of ratio subtracted and added to self.
455 456 457 458 |
# File 'lib/tonal/ratio.rb', line 455 def plus_minus(other_ratio) other_ratio = self.class.new(other_ratio) [self - other_ratio, self + other_ratio] end |
#power(power, root = nil, approximant: 0, by_method: :continued_fraction, from: :lower_ratio) ⇒ Tonal::Ratio
Returns self raised to the given power and root.
543 544 545 |
# File 'lib/tonal/ratio.rb', line 543 def power(power, root=nil, approximant: 0, by_method: :continued_fraction, from: :lower_ratio) to_interval.root_interval(power:, root:, approximant:, by_method:, from:).intervalic_ratio end |
#prime_divisions ⇒ Array
Returns , self decomposed into its prime factors.
341 342 343 |
# File 'lib/tonal/ratio.rb', line 341 def prime_divisions [antecedent.prime_division, consequent.prime_division] end |
#prime_vector ⇒ Vector Also known as: monzo, prime_exponent_vector
Returns , self represented as a prime vector.
349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/tonal/ratio.rb', line 349 def prime_vector pds = prime_divisions return nil if pds.all?(&:empty?) max = [pds.first.max{|p| p.first}, pds.last.max{|p| p.first}].compact.max.first pds.last.collect!{|i| [i.first, -i.last]} p_arr = Prime.each(max).to_a Array.new(p_arr.count, 0).tap do |arr| pds.flatten(1).each{|e| arr[p_arr.find_index(e.first)] = e.last} end.to_vector end |
#ratio ⇒ Tonal::Ratio Also known as: to_ratio
Returns convenience method returning self.
100 101 102 |
# File 'lib/tonal/ratio.rb', line 100 def ratio self end |
#scale(a, b = a) ⇒ Tonal::Ratio
Returns self scaled by given arguments.
303 304 305 306 |
# File 'lib/tonal/ratio.rb', line 303 def scale(a, b=a) raise_if_negative(a,b) self.class.new(*(Matrix[[a, 0],[0, b]] * Vector[antecedent, consequent])) end |
#shear(a, b = a) ⇒ Tonal::Ratio
Returns self sheared by given arguments.
314 315 316 317 |
# File 'lib/tonal/ratio.rb', line 314 def shear(a, b=a) raise_if_negative(a,b) self.class.new(*((Matrix[[1,a],[0,1]] * Matrix[[1,0], [b,1]]) * Vector[antecedent, consequent])) end |
#step(modulo = 12) ⇒ Integer
Returns the step of self in the given modulo.
181 182 183 |
# File 'lib/tonal/ratio.rb', line 181 def step(modulo=12) Tonal::Scale::Step.new(ratio: to_r, modulo: modulo) end |
#tenney_height ⇒ Tonal::Log2 Also known as: log_product_complexity, harmonic_distance
Returns the log product complexity of self.
398 399 400 |
# File 'lib/tonal/ratio.rb', line 398 def tenney_height Tonal::Log2.new(logarithmand: benedetti_height) end |
#to_a ⇒ Array
Returns antecedent and consequent as elements of Array.
119 120 121 |
# File 'lib/tonal/ratio.rb', line 119 def to_a [antecedent, consequent] end |
#to_cents ⇒ Tonal::Cents Also known as: cents
Returns cents value of self.
172 173 174 |
# File 'lib/tonal/ratio.rb', line 172 def to_cents Tonal::Cents.new(ratio: to_r) end |
#to_f ⇒ Float Also known as: number
Returns self as a Float.
144 145 146 |
# File 'lib/tonal/ratio.rb', line 144 def to_f antecedent.to_f / consequent.to_f end |
#to_interval ⇒ Tonal::Interval
Returns between 1/1 and self.
501 502 503 |
# File 'lib/tonal/ratio.rb', line 501 def to_interval interval_with(1/1r) end |
#to_log(base = 2) ⇒ Tonal::Log Also known as: log
Returns Math.log of self in given base.
154 155 156 |
# File 'lib/tonal/ratio.rb', line 154 def to_log(base=2) Tonal::Log.new(logarithmand: to_r, base: base) end |
#to_log2 ⇒ Tonal::Log2 Also known as: log2
Returns Math.log2 of self.
163 164 165 |
# File 'lib/tonal/ratio.rb', line 163 def to_log2 Tonal::Log2.new(logarithmand: to_r) end |
#to_r ⇒ Rational
Returns self as a Rational.
135 136 137 138 |
# File 'lib/tonal/ratio.rb', line 135 def to_r return Float::INFINITY if consequent.zero? || !antecedent.finite? Rational(antecedent, consequent) end |
#to_reduced_ratio ⇒ Tonal::ReducedRatio Also known as: reduced_ratio
Returns of self.
239 240 241 |
# File 'lib/tonal/ratio.rb', line 239 def to_reduced_ratio Tonal::ReducedRatio.new(reduced_antecedent, reduced_consequent, equave: equave) end |
#to_v ⇒ Vector
Returns antecedent and consequent as elements of Vector.
127 128 129 |
# File 'lib/tonal/ratio.rb', line 127 def to_v Vector[antecedent, consequent] end |
#translate(x = 1, y = 0) ⇒ Tonal::Ratio
Ratio grid transformations numerator mapped on x-axis, denominator mapped on y-axis
292 293 294 295 |
# File 'lib/tonal/ratio.rb', line 292 def translate(x=1, y=0) raise_if_negative(x,y) self.class.new(*(Vector[antecedent, consequent] + Vector[x, y])) end |
#weil_height ⇒ Integer
Returns the Weil height.
408 409 410 |
# File 'lib/tonal/ratio.rb', line 408 def weil_height [reduced_antecedent, reduced_consequent].max end |
#wilson_height(prime_rejects: [2]) ⇒ Integer
Returns the Wilson height. The sum of self’s prime factors (greater than 2) times the absolute values of their exponents.
425 426 427 |
# File 'lib/tonal/ratio.rb', line 425 def wilson_height(prime_rejects: [2]) benedetti_height.prime_division.reject{|p| prime_rejects.include?(p.first) }.sum{|p| p.first * p.last } end |