unit-propagation 0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,49 @@
1
+ Metadata-Version: 2.1
2
+ Name: unit_propagation
3
+ Version: 0.1
4
+ Summary: physical quantities (numbers with units)
5
+ Keywords: quantities,physical quantity,units,SI scale factors,engineering notation,mks,cgs
6
+ Author: Ken Kundert
7
+ Author-email: quantiphy@nurdletech.com
8
+ Requires-Python: >=3.6
9
+ Description-Content-Type: text/x-rst
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Natural Language :: English
14
+ Classifier: Operating System :: POSIX :: Linux
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Utilities
17
+ Classifier: Topic :: Scientific/Engineering
18
+ Requires-Dist: quantiphy
19
+
20
+ Simple-Minded Unit Propagation for QuantiPhy
21
+ ============================================
22
+
23
+ | Author: Ken Kundert
24
+ | Version: 0.1
25
+ | Released: 2024-03-01
26
+ |
27
+
28
+ This is a package used to experiment with adding unit propagation to QuantiPhy_.
29
+ It currently employs simple-minded simplification rules that are relatively easy
30
+ to fool. Also, there is a strong emphasis on simple electrical unit scenarios.
31
+ Even so, it shows promise for use in well controlled settings.
32
+
33
+ Here is simple example::
34
+
35
+ from unit_propagation import (
36
+ UnitPropagatingQuantity as Quantity, QuantiPhyError
37
+ )
38
+ try:
39
+ v = Quantity("2.5V")
40
+ i = Quantity("100nA")
41
+ print(v/i)
42
+ except QuantiPhyError as e:
43
+ print(f"error: {e!s}")
44
+
45
+ Included in the package is a simple RPN calculator that allows you to explore
46
+ the capabilities and limitation of the *unit propagation*.
47
+
48
+ .. _QuantiPhy: https://quantiphy.readthedocs.io
49
+
@@ -0,0 +1,29 @@
1
+ Simple-Minded Unit Propagation for QuantiPhy
2
+ ============================================
3
+
4
+ | Author: Ken Kundert
5
+ | Version: 0.1
6
+ | Released: 2024-03-01
7
+ |
8
+
9
+ This is a package used to experiment with adding unit propagation to QuantiPhy_.
10
+ It currently employs simple-minded simplification rules that are relatively easy
11
+ to fool. Also, there is a strong emphasis on simple electrical unit scenarios.
12
+ Even so, it shows promise for use in well controlled settings.
13
+
14
+ Here is simple example::
15
+
16
+ from unit_propagation import (
17
+ UnitPropagatingQuantity as Quantity, QuantiPhyError
18
+ )
19
+ try:
20
+ v = Quantity("2.5V")
21
+ i = Quantity("100nA")
22
+ print(v/i)
23
+ except QuantiPhyError as e:
24
+ print(f"error: {e!s}")
25
+
26
+ Included in the package is a simple RPN calculator that allows you to explore
27
+ the capabilities and limitation of the *unit propagation*.
28
+
29
+ .. _QuantiPhy: https://quantiphy.readthedocs.io
@@ -0,0 +1,43 @@
1
+ [project]
2
+ name = "unit_propagation"
3
+ version = "0.1"
4
+ description = "physical quantities (numbers with units)"
5
+ readme = "README.rst"
6
+ keywords = [
7
+ "quantities",
8
+ "physical quantity",
9
+ "units",
10
+ "SI scale factors",
11
+ "engineering notation",
12
+ "mks",
13
+ "cgs"
14
+ ]
15
+ authors = [
16
+ {name = "Ken Kundert"},
17
+ {email = "quantiphy@nurdletech.com"}
18
+ ]
19
+ classifiers = [
20
+ # "Development Status :: 5 - Production/Stable",
21
+ "Intended Audience :: Developers",
22
+ "Intended Audience :: Science/Research",
23
+ "License :: OSI Approved :: MIT License",
24
+ "Natural Language :: English",
25
+ "Operating System :: POSIX :: Linux",
26
+ "Programming Language :: Python :: 3",
27
+ "Topic :: Utilities",
28
+ "Topic :: Scientific/Engineering",
29
+ ]
30
+ requires-python = ">=3.6"
31
+ dependencies = [
32
+ "quantiphy"
33
+ ]
34
+
35
+ [project.urls]
36
+ # homepage = "https://quantiphy.readthedocs.io"
37
+ # documentation = "https://quantiphy.readthedocs.io"
38
+ # repository = "https://github.com/kenkundert/quantiphy"
39
+ # changelog = "https://github.com/KenKundert/quantiphy/blob/master/doc/releases.rst"
40
+
41
+ [build-system]
42
+ requires = ["flit_core >=2,<4"]
43
+ build-backend = "flit_core.buildapi"
@@ -0,0 +1,353 @@
1
+ # Unit Propagation — Simple Minded Unit Propagtaion for QuantiPhy
2
+ # encoding: utf8
3
+
4
+ # Description {{{1
5
+ """
6
+ Adds unit propagation to *QuantiPhy*.
7
+ """
8
+
9
+ # Issues
10
+ # The following examples represent challenges to unit propagation
11
+ # 2*pi*1.42GHz becomes rads/s
12
+ # In this case pi needs a unit of rads, and then evaluator must recognize
13
+ # that Hz is /s with the results becoming rads/s
14
+ # T₀ + 25
15
+ # T₀ is in Kelvin and 25 in in Celsius. These appear to have different
16
+ # units, but those units are compatible (where as Fahrenheit is not).
17
+ # But in addition, you cannot convert Celsius to Kelvin before doing the
18
+ # addition.
19
+
20
+ # MIT License {{{1
21
+ # Copyright (C) 2016-2024 Kenneth S. Kundert
22
+ #
23
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
24
+ # of this software and associated documentation files (the "Software"), to deal
25
+ # in the Software without restriction, including without limitation the rights
26
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27
+ # copies of the Software, and to permit persons to whom the Software is
28
+ # furnished to do so, subject to the following conditions:
29
+ #
30
+ # The above copyright notice and this permission notice shall be included in all
31
+ # copies or substantial portions of the Software.
32
+ #
33
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39
+ # SOFTWARE.
40
+
41
+ # Imports {{{1
42
+ from quantiphy import Quantity, IncompatibleUnits, QuantiPhyError, InvalidNumber
43
+ import numbers
44
+ import operator
45
+ import math
46
+
47
+
48
+ # Globals {{{1
49
+ # product_sep = self.narrow_non_breaking_space
50
+ # product_sep = '⋅'
51
+ product_sep = '-'
52
+ quotient_sep = '/'
53
+
54
+ __version__ = '0.1'
55
+ __released__ = '2024-03-01'
56
+
57
+ # Simplifications {{{2
58
+ SIMPLIFICATIONS = dict(
59
+ multiply = {
60
+ ('V', 'A'): 'W', # power
61
+ ('Ω', 'A'): 'V', # voltage (from Ohm symbol)
62
+ ('Ω', 'A'): 'V', # voltage (from Greek Omega symbol)
63
+ ('Ʊ', 'V'): 'A', # amperes
64
+ ('m', 'm'): 'm²', # area
65
+ ('rads', 'Hz'): 'rads/s', # radial frequency
66
+ ('(rads/s)', 's'): 'rads', # radians
67
+ ('(Hz/V)', 'V'): 'Hz', # frequency
68
+ ('(J/K)', 'K'): 'J', # joules
69
+ },
70
+ divide = {
71
+ ('V', 'A'): 'Ω' , # resistance (to Ohm symbol)
72
+ ('V', 'Ω'): 'A' , # current (from Ohm symbol)
73
+ ('V', 'Ω'): 'A' , # current (from Greek Omega symbol)
74
+ ('A', 'V'): 'Ʊ' , # conductance
75
+ ('', 's'): 'Hz', # frequency
76
+ ('', 'Hz'): 's' , # time
77
+ ('J', 'C'): 'V' , # volts
78
+ ('', 'Ω'): 'Ʊ', # conductance (from Ohm symbol)
79
+ ('', 'Ω'): 'Ʊ', # conductance (from Ohm symbol)
80
+ ('', 'Ʊ'): 'Ω', # resistance (to Ohm symbol)
81
+ ('(rads/s)', 'rads'): 'Hz', # hertz
82
+ },
83
+ )
84
+ # check for invalid units (a unit that contains an operator must be parenthesized)
85
+ for section, rules in SIMPLIFICATIONS.items():
86
+ for units in rules.keys():
87
+ for unit in units:
88
+ if unit and not unit.isidentifier():
89
+ assert unit[:1] == "(" and unit[-1:] == ")", f"{unit} must be parenthesized."
90
+
91
+
92
+ # def add_simplifications(multiply=None, divide=None):
93
+ # if multiply:
94
+ # SIMPLIFICATIONS['multiply'].update(multiply)
95
+ # if divide:
96
+ # SIMPLIFICATIONS['divide'].update(divide)
97
+ # Not ready for prime time. Need to recheck parentheses and resort
98
+ # commutative operators after adding new simplifications
99
+
100
+
101
+ # sort the multiply units to make it insensitive to order, also group as needed
102
+ SIMPLIFICATIONS["multiply"] = {
103
+ tuple(sorted(f)): t
104
+ for f, t in SIMPLIFICATIONS["multiply"].items()
105
+ }
106
+
107
+
108
+ # Utilities {{{1
109
+ # group() {{{2
110
+ def group(units, aggressive=False):
111
+ if '/' in units:
112
+ return f"({units})"
113
+ if aggressive and product_sep in units:
114
+ return f"({units})"
115
+ return units
116
+
117
+
118
+ # UnitPropagatingQuantity class {{{1
119
+ class UnitPropagatingQuantity(Quantity):
120
+ check_units = True
121
+
122
+ # operator overloads {{{2
123
+ # pos {{{3
124
+ def __pos__(self):
125
+ return self
126
+
127
+ # neg {{{3
128
+ def __neg__(self):
129
+ return self.__class__(-self.real, units=self.units)
130
+
131
+ # abs {{{3
132
+ def __abs__(self):
133
+ return self.__class__(abs(self.real), units=self.units)
134
+
135
+ # round {{{3
136
+ def __round__(self, ndigits=None):
137
+ return self.__class__(round(self.real, ndigits), units=self.units)
138
+
139
+ # trunc {{{3
140
+ def __trunc__(self):
141
+ return self.__class__(math.trunc(self.real), units=self.units)
142
+
143
+ # floor {{{3
144
+ def __floor__(self):
145
+ return self.__class__(math.floor(self.real), units=self.units)
146
+
147
+ # ceil {{{3
148
+ def __ceil__(self):
149
+ return self.__class__(math.ceil(self.real), units=self.units)
150
+
151
+ # generic binary operator {{{3
152
+ # handles simple cases where units must match
153
+ def _binary_operator(self, other, op):
154
+ if isinstance(other, str):
155
+ other = self.__class__(other)
156
+ if not isinstance(other, numbers.Number):
157
+ raise InvalidNumber(other)
158
+
159
+ try:
160
+ if self.check_units and self.units != other.units:
161
+ raise IncompatibleUnits(self, other)
162
+ except AttributeError:
163
+ if self.check_units == 'strict':
164
+ raise IncompatibleUnits(
165
+ getattr(self, 'units', None),
166
+ getattr(other, 'units', None)
167
+ )
168
+
169
+ new = self.__class__(op(self.real, other.real), units=self.units)
170
+ new._inherit_attributes(self)
171
+ return new
172
+
173
+ # handles simple cases where units must match
174
+ def _reflected_binary_operator(self, other, op):
175
+ if isinstance(other, str):
176
+ other = self.__class__(other)
177
+ if not isinstance(other, numbers.Number):
178
+ raise InvalidNumber(other)
179
+
180
+ try:
181
+ if self.check_units and self.units != other.units:
182
+ raise IncompatibleUnits(self, other)
183
+ except AttributeError:
184
+ if self.check_units == 'strict':
185
+ raise IncompatibleUnits(other, self)
186
+ new = self.__class__(op(other.real, self.real), units=self.units)
187
+ new._inherit_attributes(self)
188
+ return new
189
+
190
+ # add {{{3
191
+ def __add__(self, addend):
192
+ return self._binary_operator(addend, operator.add)
193
+
194
+ def __radd__(self, addend):
195
+ return self._reflected_binary_operator(addend, operator.add)
196
+
197
+ __iadd__ = __add__
198
+
199
+ # subtract {{{3
200
+ def __sub__(self, subtrahend):
201
+ return self._binary_operator(subtrahend, operator.sub)
202
+
203
+ def __rsub__(self, minuend):
204
+ return self._reflected_binary_operator(minuend, operator.sub)
205
+
206
+ __isub__ = __sub__
207
+
208
+ # multiply {{{3
209
+ def __mul__(self, multiplicand):
210
+ if isinstance(multiplicand, str):
211
+ multiplicand = self.__class__(multiplicand)
212
+ if not isinstance(multiplicand, numbers.Number):
213
+ raise InvalidNumber(multiplicand)
214
+
215
+ # units
216
+ try:
217
+ units = tuple(sorted([group(self.units), group(multiplicand.units)]))
218
+ except AttributeError:
219
+ units = (self.units,)
220
+ if self.check_units == 'strict':
221
+ raise IncompatibleUnits(self, multiplicand)
222
+ simplifications = SIMPLIFICATIONS['multiply']
223
+ if units in simplifications:
224
+ units = simplifications[units]
225
+ else:
226
+ units = product_sep.join(u for u in units if u)
227
+
228
+ # this is not quite right, perhaps when defining the simplifications I
229
+ # could also define new classes for the product
230
+ new = self.__class__(self.real * multiplicand.real, units=units)
231
+ new._inherit_attributes(self)
232
+ return new
233
+
234
+ __rmul__ = __mul__
235
+ __imul__ = __mul__
236
+
237
+ # divide {{{3
238
+ def __truediv__(self, divisor):
239
+ if isinstance(divisor, str):
240
+ divisor = self.__class__(divisor)
241
+ if not isinstance(divisor, numbers.Number):
242
+ raise InvalidNumber(divisor)
243
+
244
+ # units
245
+ try:
246
+ units = (group(self.units), group(divisor.units, True))
247
+ except AttributeError:
248
+ units = (self.units, '')
249
+ if self.check_units == 'strict':
250
+ raise IncompatibleUnits(self, divisor)
251
+ simplifications = SIMPLIFICATIONS['divide']
252
+ if units in simplifications:
253
+ units = simplifications[units]
254
+ elif units[0]:
255
+ units = quotient_sep.join(units) if units[1] else units[0]
256
+ elif units[1]:
257
+ units = units[1] + '⁻¹'
258
+ else:
259
+ units = ''
260
+
261
+ # this is not quite right, perhaps when defining the simplifications I
262
+ # could also define new classes for the product
263
+ new = self.__class__(self.real / divisor.real, units=units)
264
+ new._inherit_attributes(self)
265
+ return new
266
+
267
+ def __rtruediv__(self, dividend):
268
+ if isinstance(dividend, str):
269
+ dividend = self.__class__(dividend)
270
+ if not isinstance(dividend, numbers.Number):
271
+ raise InvalidNumber(dividend)
272
+
273
+ # units
274
+ try:
275
+ units = (dividend.units, self.units)
276
+ except AttributeError:
277
+ units = ('', self.units)
278
+ if self.check_units == 'strict':
279
+ raise IncompatibleUnits(dividend, self)
280
+ simplifications = SIMPLIFICATIONS['divide']
281
+ if units in simplifications:
282
+ units = simplifications[units]
283
+ elif units[0]:
284
+ units = quotient_sep.join(units) if units[1] else units[0]
285
+ elif units[1]:
286
+ units = units[1] + '⁻¹'
287
+ else:
288
+ units = ''
289
+
290
+ # this is not quite right, perhaps when defining the simplifications I
291
+ # could also define new classes for the product
292
+ new = self.__class__(dividend.real / self.real, units=units)
293
+ new._inherit_attributes(self)
294
+ return new
295
+
296
+ __itruediv__ = __truediv__
297
+
298
+ # comparison operations {{{3
299
+ def _compare(self, other, op):
300
+ if isinstance(other, str):
301
+ other = self.__class__(other)
302
+ if not isinstance(other, numbers.Number):
303
+ raise InvalidNumber(other)
304
+
305
+ try:
306
+ if self.check_units and self.units != other.units:
307
+ raise IncompatibleUnits(self, other)
308
+ except AttributeError:
309
+ if self.check_units == 'strict':
310
+ raise IncompatibleUnits(self, other)
311
+ return op(self.real, other)
312
+
313
+ # less than {{{3
314
+ def __lt__(self, other):
315
+ return self._compare(other, operator.lt)
316
+
317
+ # less than or equal {{{3
318
+ def __le__(self, other):
319
+ return self._compare(other, operator.le)
320
+
321
+ # greater than {{{3
322
+ def __gt__(self, other):
323
+ return self._compare(other, operator.gt)
324
+
325
+ # greater than or equal {{{3
326
+ def __ge__(self, other):
327
+ return self._compare(other, operator.ge)
328
+
329
+ # equality operations {{{3
330
+ def _equality(self, other, op, on_failure):
331
+ try:
332
+ if isinstance(other, str):
333
+ other = self.__class__(other)
334
+ if not isinstance(other, numbers.Number):
335
+ raise InvalidNumber(other)
336
+ except InvalidNumber:
337
+ return on_failure
338
+
339
+ try:
340
+ if self.check_units and self.units != other.units:
341
+ return on_failure
342
+ except AttributeError:
343
+ if self.check_units == 'strict':
344
+ return on_failure
345
+ return op(self.real, other)
346
+
347
+ # equal {{{3
348
+ def __eq__(self, other):
349
+ return self._equality(other, operator.eq, False)
350
+
351
+ # equal {{{3
352
+ def __ne__(self, other):
353
+ return self._equality(other, operator.ne, True)