Completed
Push — master ( 3298fc...e8ff27 )
by Patrick
01:57
created

Harmonic::getStringLengthsFromNumber()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ExtendedStrings\Strings;
6
7
class Harmonic
8
{
9
    private $halfStop;
10
    private $baseStop;
11
12
    /**
13
     * @param float $halfStop
14
     * @param float $baseStop
15
     */
16
    public function __construct(float $halfStop, float $baseStop = 1.0)
17
    {
18
        if ($halfStop > $baseStop) {
19
            throw new \InvalidArgumentException("The half-stop's string length cannot be longer than the base stop's.");
20
        }
21
22
        $this->baseStop = $baseStop;
23
        $this->halfStop = $halfStop;
24
    }
25
26
    /**
27
     * @param \ExtendedStrings\Strings\VibratingString $string
28
     *
29
     * @return float
30
     */
31
    public function getSoundingPitch(VibratingString $string): float
32
    {
33
        // Transpose the half-stop onto the new string length, which was formed
34
        // by the stop.
35
        $pseudoString = new VibratingString($string->getStoppedFrequency($this->baseStop));
36
        $pseudoHalfStop = $this->halfStop / $this->baseStop;
37
38
        return $pseudoString->getHarmonicSoundingFrequency($pseudoHalfStop);
39
    }
40
41
    /**
42
     * @param int  $number
43
     * @param bool $exclusive
44
     *
45
     * @return float[]
46
     */
47
    public static function getStringLengthsFromNumber(int $number, bool $exclusive = false): array
48
    {
49
        if ($number === 1) {
50
            return [1.0];
51
        }
52
53
        $harmonics = [];
54
        for ($numerator = 1; $numerator < $number; $numerator++) {
55
            if (!$exclusive || (int) Math::gcd($numerator, $number) === 1) {
56
                $harmonics[] = $numerator / $number;
57
            }
58
        }
59
60
        return $harmonics;
61
    }
62
63
    /**
64
     * @param int $limit
65
     *
66
     * @return float[]
67
     */
68
    public static function getSeries(int $limit): array
69
    {
70
        $series = [];
71
        $base = 0;
72
        for ($denominator = 1; $denominator <= $limit; $denominator++) {
73
            $base = $series[$denominator] = $base + 1 / $denominator;
74
        }
75
76
        return $series;
77
    }
78
}
79