|
| 1 | +import math |
| 2 | + |
| 3 | +class Unit(): |
| 4 | + numerator : list |
| 5 | + denumerator : list |
| 6 | + ratio : float |
| 7 | + |
| 8 | + |
| 9 | + def getKey(self): |
| 10 | + key = {"num" : {}, "denum" : {}} |
| 11 | + for unit in self.numerator: |
| 12 | + if unit.abrev in key["num"]: |
| 13 | + key["num"][unit.abrev] += 1 |
| 14 | + else: |
| 15 | + key["num"][unit.abrev] = 1 |
| 16 | + for unit in self.denumerator: |
| 17 | + if unit.abrev in key["denum"]: |
| 18 | + key["denum"][unit.abrev] += 1 |
| 19 | + else: |
| 20 | + key["denum"][unit.abrev] = 1 |
| 21 | + return key |
| 22 | + |
| 23 | + def __eq__ (self, other): |
| 24 | + if not isinstance(other, Unit): |
| 25 | + return NotImplemented |
| 26 | + |
| 27 | + if int(math.log10(self.ratio)) != int(math.log10(other.ratio)) : |
| 28 | + return False |
| 29 | + |
| 30 | + return self.getKey() == other.getKey() |
| 31 | + |
| 32 | + |
| 33 | + def __mul__(self, other): |
| 34 | + if isinstance(other, Unit): |
| 35 | + return DerivedUnit(numerator=self.numerator + other.numerator, denumerator= self.denumerator + other.denumerator, ratio = self.ratio * other.ratio) |
| 36 | + else: |
| 37 | + return DimensionnedValue(other,self ) |
| 38 | + |
| 39 | + |
| 40 | + def __rmul__(self, other ): |
| 41 | + return self.__mul__(other) |
| 42 | + |
| 43 | + def __pow__(self, other : int): |
| 44 | + if not isinstance(other, int): |
| 45 | + raise ValueError |
| 46 | + |
| 47 | + targetNum = [] |
| 48 | + targetDenum = [] |
| 49 | + targetRatio = 1.0 |
| 50 | + |
| 51 | + for i in range(abs(other)): |
| 52 | + targetNum += self.numerator |
| 53 | + targetDenum += self.denumerator |
| 54 | + targetRatio *= self.ratio |
| 55 | + |
| 56 | + if other < 0 : |
| 57 | + return DerivedUnit(numerator=targetDenum, denumerator= targetNum, ratio = 1.0/targetRatio) |
| 58 | + elif other > 0 : |
| 59 | + return DerivedUnit(numerator=targetNum, denumerator= targetDenum, ratio = targetRatio) |
| 60 | + else: |
| 61 | + return NeutralUnit() |
| 62 | + |
| 63 | + def __truediv__(self, other ): |
| 64 | + if not isinstance(other, Unit): |
| 65 | + return NotImplemented |
| 66 | + |
| 67 | + return DerivedUnit(numerator=self.numerator + other.denumerator, denumerator= self.denumerator + other.numerator, ratio = self.ratio / other.ratio) |
| 68 | + |
| 69 | + def toString(self, addRatio : bool = True): |
| 70 | + self_key = self.getKey() |
| 71 | + |
| 72 | + def side(units: dict) -> str: |
| 73 | + return " * ".join( |
| 74 | + k if exp == 1 else f"{k}^{exp}" |
| 75 | + for k, exp in units.items() |
| 76 | + ) |
| 77 | + |
| 78 | + num = side(self_key["num"]) |
| 79 | + denum = side(self_key["denum"]) |
| 80 | + |
| 81 | + num_s = f"( {num} ) " if num else "1" |
| 82 | + denum_s = f"/ ( {denum} )" if denum else "" |
| 83 | + |
| 84 | + prefix = f"{self.ratio} * " if addRatio else "" |
| 85 | + return prefix + num_s + denum_s |
| 86 | + |
| 87 | + |
| 88 | + def __str__(self): |
| 89 | + return self.toString() |
| 90 | + |
| 91 | + def __hash__(self): |
| 92 | + key = self.getKey() |
| 93 | + return hash(( |
| 94 | + frozenset(key["num"].items()), |
| 95 | + frozenset(key["denum"].items()), |
| 96 | + int(math.log10(self.ratio)), |
| 97 | + )) |
| 98 | + |
| 99 | +class NeutralUnit(Unit): |
| 100 | + def __init__(self): |
| 101 | + self.numerator = [] |
| 102 | + self.denumerator = [] |
| 103 | + self.ratio = 1.0 |
| 104 | + |
| 105 | + def __str__(self): |
| 106 | + return "1" |
| 107 | + |
| 108 | + |
| 109 | +class PrimaryUnit(Unit): |
| 110 | + |
| 111 | + abrev = str |
| 112 | + |
| 113 | + def __init__(self, abrev : str): |
| 114 | + self.abrev = abrev |
| 115 | + self.numerator = [self] |
| 116 | + self.denumerator = [] |
| 117 | + self.ratio = 1.0 |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +class DerivedUnit(Unit): |
| 122 | + |
| 123 | + def __init__(self, numerator : list[PrimaryUnit], denumerator : list[PrimaryUnit], ratio : float): |
| 124 | + self.numerator = numerator |
| 125 | + self.denumerator = denumerator |
| 126 | + self.ratio = ratio |
| 127 | + |
| 128 | + self.simplify() |
| 129 | + |
| 130 | + |
| 131 | + def simplify(self): |
| 132 | + futNum = [] |
| 133 | + for unit in self.numerator: |
| 134 | + simplified = False |
| 135 | + for i in range(len(self.denumerator)): |
| 136 | + if self.denumerator[i].abrev == unit.abrev: |
| 137 | + simplified = True |
| 138 | + self.denumerator.pop(i) |
| 139 | + break |
| 140 | + if not(simplified): |
| 141 | + futNum.append(unit) |
| 142 | + self.numerator = futNum |
| 143 | + |
| 144 | + |
| 145 | +class ScaledUnit(Unit): |
| 146 | + |
| 147 | + def __init__(self, unit : Unit, ratio : float): |
| 148 | + self.numerator = unit.numerator.copy() |
| 149 | + self.denumerator = unit.denumerator.copy() |
| 150 | + self.ratio = ratio |
| 151 | + |
| 152 | + |
| 153 | +class DimensionnedValue(): |
| 154 | + |
| 155 | + value : float |
| 156 | + unit : Unit |
| 157 | + |
| 158 | + def __init__(self, value : float, unit : Unit): |
| 159 | + self.value = value |
| 160 | + self.unit = unit |
| 161 | + |
| 162 | + def __eq__ (self, other): |
| 163 | + if not isinstance(other, DimensionnedValue): |
| 164 | + raise TypeError("Dimensionned values can only be compared to other dimensionned values") |
| 165 | + |
| 166 | + |
| 167 | + if self.unit.getKey() != other.unit.getKey(): |
| 168 | + raise TypeError("Only values that share the same units can be compared") |
| 169 | + |
| 170 | + return math.isclose(self.value * self.unit.ratio, other.value * other.unit.ratio) |
| 171 | + |
| 172 | + |
| 173 | + def __mul__(self, other): |
| 174 | + if isinstance(other, DimensionnedValue): |
| 175 | + return DimensionnedValue(self.value * other.value,self.unit * other.unit) |
| 176 | + elif isinstance(other, Unit) : |
| 177 | + return DimensionnedValue(self.value ,self.unit * other) |
| 178 | + else : |
| 179 | + return DimensionnedValue(self.value * other,self.unit) |
| 180 | + |
| 181 | + def __rmul__(self, other ): |
| 182 | + return self.__mul__(other) |
| 183 | + |
| 184 | + def __pow__(self, other : int): |
| 185 | + if not isinstance(other, int): |
| 186 | + raise ValueError |
| 187 | + |
| 188 | + return DimensionnedValue(self.value ** other, self.unit**other) |
| 189 | + |
| 190 | + def __truediv__(self, other): |
| 191 | + if isinstance(other, DimensionnedValue): |
| 192 | + return DimensionnedValue(self.value / other.value, self.unit / other.unit) |
| 193 | + elif isinstance(other, Unit) : |
| 194 | + return DimensionnedValue(self.value, self.unit / other) |
| 195 | + else: |
| 196 | + return DimensionnedValue(self.value / other, self.unit) |
| 197 | + |
| 198 | + def __rtruediv__(self, other): |
| 199 | + if isinstance(other, DimensionnedValue): |
| 200 | + return DimensionnedValue(other.value / self.value, other.unit / self.unit) |
| 201 | + elif isinstance(other, Unit): |
| 202 | + return DimensionnedValue(1.0 / self.value, other / self.unit) |
| 203 | + else: |
| 204 | + return DimensionnedValue(other / self.value, self.unit ** -1) |
| 205 | + |
| 206 | + def __str__(self): |
| 207 | + return f"{self.value * self.unit.ratio} * " + self.unit.toString(False) |
| 208 | + |
| 209 | + def __hash__(self): |
| 210 | + key = self.unit.getKey() |
| 211 | + return hash(( |
| 212 | + frozenset(key["num"].items()), |
| 213 | + frozenset(key["denum"].items()), |
| 214 | + round(self.value * self.unit.ratio, 9) # normalized magnitude |
| 215 | + )) |
| 216 | + |
0 commit comments