-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeasurementDimension.php
86 lines (74 loc) · 2.19 KB
/
MeasurementDimension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace ProcessWire;
/**
* @property $numerator
* @property $denominator
*/
class MeasurementDimension {
// Define dimensions using prime numbers
const TIME = 2;
const LENGTH = 3;
const MASS = 5;
const CURRENT = 7;
const TEMPERATURE = 11;
const SUBSTANCE_AMOUNT = 13;
const LUMINOSITY = 17;
public function __construct(int $numerator, int $denominator = 1) {
$this->numerator = $numerator;
$this->denominator = $denominator;
}
public function getQuantities() {
$allQuantities = Measurement::getQuantities();
$consistentQuantities = [];
foreach($allQuantities as $quantity) {
$quantityDimension = Measurement::getDimension($quantity);
/* @var $quantityDimension MeasurementDimension */
if($quantityDimension && $quantityDimension->simplify()->numerator == $this->simplify()->numerator &&
$quantityDimension->simplify()->denominator == $this->simplify()->denominator) $consistentQuantities[] = $quantity;
}
return $consistentQuantities;
}
public function simplify() {
$g = $this->gcd($this->numerator, $this->denominator);
$this->numerator = $this->numerator / $g;
$this->denominator = $this->denominator / $g;
return $this;
}
public function multiplyBy(MeasurementDimension $dim) {
$result = new MeasurementDimension($this->numerator * $dim->numerator, $this->denominator * $dim->denominator);
return $result->simplify();
}
public function divideBy(MeasurementDimension $dim) {
$result = new MeasurementDimension($this->numerator * $dim->denominator, $this->denominator * $dim->numerator);
return $result->simplify();
}
public function power(int $exponent) {
$result = new MeasurementDimension(1, 1);
if($exponent > 0) {
for($i = 1; $i <= $exponent; $i++) {
$result = $result->multiplyBy($this);
}
return $result->simplify();
} else if($exponent < 0) {
for($i = -1; $i >= $exponent; $i--) {
$result = $result->divideBy($this);
}
return $result->simplify();
} else {
return $result;
}
}
public function gcd($a, $b) {
$a = abs($a);
$b = abs($b);
if($a < $b) list($b, $a) = array($a, $b);
if($b == 0) return $a;
$r = $a % $b;
while($r > 0) {
$a = $b;
$b = $r;
$r = $a % $b;
}
return $b;
}
}