ucon 0.5.1__py3-none-any.whl → 0.6.0__py3-none-any.whl

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.
@@ -1,264 +0,0 @@
1
- # © 2026 The Radiativity Company
2
- # Licensed under the Apache License, Version 2.0
3
- # See the LICENSE file for details.
4
-
5
- """
6
- Tests for v0.5.x uncertainty propagation.
7
-
8
- Tests Number construction with uncertainty, display formatting,
9
- arithmetic propagation, and conversion propagation.
10
- """
11
-
12
- import math
13
- import unittest
14
-
15
- from ucon import units, Scale
16
- from ucon.core import Number, UnitProduct
17
-
18
-
19
- class TestUncertaintyConstruction(unittest.TestCase):
20
- """Test constructing Numbers with uncertainty."""
21
-
22
- def test_number_with_uncertainty(self):
23
- n = units.meter(1.234, uncertainty=0.005)
24
- self.assertEqual(n.value, 1.234)
25
- self.assertEqual(n.uncertainty, 0.005)
26
-
27
- def test_number_without_uncertainty(self):
28
- n = units.meter(1.234)
29
- self.assertEqual(n.value, 1.234)
30
- self.assertIsNone(n.uncertainty)
31
-
32
- def test_unit_product_callable_with_uncertainty(self):
33
- km = Scale.kilo * units.meter
34
- n = km(5.0, uncertainty=0.1)
35
- self.assertEqual(n.value, 5.0)
36
- self.assertEqual(n.uncertainty, 0.1)
37
-
38
- def test_composite_unit_with_uncertainty(self):
39
- mps = units.meter / units.second
40
- n = mps(10.0, uncertainty=0.5)
41
- self.assertEqual(n.value, 10.0)
42
- self.assertEqual(n.uncertainty, 0.5)
43
-
44
-
45
- class TestUncertaintyDisplay(unittest.TestCase):
46
- """Test display formatting with uncertainty."""
47
-
48
- def test_repr_with_uncertainty(self):
49
- n = units.meter(1.234, uncertainty=0.005)
50
- r = repr(n)
51
- self.assertIn("1.234", r)
52
- self.assertIn("±", r)
53
- self.assertIn("0.005", r)
54
- self.assertIn("m", r)
55
-
56
- def test_repr_without_uncertainty(self):
57
- n = units.meter(1.234)
58
- r = repr(n)
59
- self.assertIn("1.234", r)
60
- self.assertNotIn("±", r)
61
-
62
- def test_repr_dimensionless_with_uncertainty(self):
63
- n = Number(quantity=0.5, uncertainty=0.01)
64
- r = repr(n)
65
- self.assertIn("0.5", r)
66
- self.assertIn("±", r)
67
- self.assertIn("0.01", r)
68
-
69
-
70
- class TestAdditionSubtractionPropagation(unittest.TestCase):
71
- """Test uncertainty propagation through addition and subtraction."""
72
-
73
- def test_addition_both_uncertain(self):
74
- a = units.meter(10.0, uncertainty=0.3)
75
- b = units.meter(5.0, uncertainty=0.4)
76
- c = a + b
77
- self.assertAlmostEqual(c.value, 15.0, places=9)
78
- # sqrt(0.3² + 0.4²) = sqrt(0.09 + 0.16) = sqrt(0.25) = 0.5
79
- self.assertAlmostEqual(c.uncertainty, 0.5, places=9)
80
-
81
- def test_subtraction_both_uncertain(self):
82
- a = units.meter(10.0, uncertainty=0.3)
83
- b = units.meter(5.0, uncertainty=0.4)
84
- c = a - b
85
- self.assertAlmostEqual(c.value, 5.0, places=9)
86
- # sqrt(0.3² + 0.4²) = 0.5
87
- self.assertAlmostEqual(c.uncertainty, 0.5, places=9)
88
-
89
- def test_addition_one_uncertain(self):
90
- a = units.meter(10.0, uncertainty=0.3)
91
- b = units.meter(5.0) # no uncertainty
92
- c = a + b
93
- self.assertAlmostEqual(c.value, 15.0, places=9)
94
- self.assertAlmostEqual(c.uncertainty, 0.3, places=9)
95
-
96
- def test_addition_neither_uncertain(self):
97
- a = units.meter(10.0)
98
- b = units.meter(5.0)
99
- c = a + b
100
- self.assertAlmostEqual(c.value, 15.0, places=9)
101
- self.assertIsNone(c.uncertainty)
102
-
103
-
104
- class TestMultiplicationPropagation(unittest.TestCase):
105
- """Test uncertainty propagation through multiplication."""
106
-
107
- def test_multiplication_both_uncertain(self):
108
- a = units.meter(10.0, uncertainty=0.2) # 2% relative
109
- b = units.meter(5.0, uncertainty=0.15) # 3% relative
110
- c = a * b
111
- self.assertAlmostEqual(c.value, 50.0, places=9)
112
- # relative: sqrt((0.2/10)² + (0.15/5)²) = sqrt(0.0004 + 0.0009) = sqrt(0.0013) ≈ 0.0361
113
- # absolute: 50 * 0.0361 ≈ 1.803
114
- expected = 50.0 * math.sqrt((0.2/10)**2 + (0.15/5)**2)
115
- self.assertAlmostEqual(c.uncertainty, expected, places=6)
116
-
117
- def test_multiplication_one_uncertain(self):
118
- a = units.meter(10.0, uncertainty=0.2)
119
- b = units.meter(5.0) # no uncertainty
120
- c = a * b
121
- self.assertAlmostEqual(c.value, 50.0, places=9)
122
- # Only a contributes: |c| * (δa/a) = 50 * 0.02 = 1.0
123
- expected = 50.0 * (0.2/10)
124
- self.assertAlmostEqual(c.uncertainty, expected, places=9)
125
-
126
- def test_multiplication_neither_uncertain(self):
127
- a = units.meter(10.0)
128
- b = units.meter(5.0)
129
- c = a * b
130
- self.assertAlmostEqual(c.value, 50.0, places=9)
131
- self.assertIsNone(c.uncertainty)
132
-
133
-
134
- class TestDivisionPropagation(unittest.TestCase):
135
- """Test uncertainty propagation through division."""
136
-
137
- def test_division_both_uncertain(self):
138
- a = units.meter(10.0, uncertainty=0.2) # 2% relative
139
- b = units.second(2.0, uncertainty=0.04) # 2% relative
140
- c = a / b
141
- self.assertAlmostEqual(c.value, 5.0, places=9)
142
- # relative: sqrt((0.2/10)² + (0.04/2)²) = sqrt(0.0004 + 0.0004) = sqrt(0.0008) ≈ 0.0283
143
- # absolute: 5 * 0.0283 ≈ 0.1414
144
- expected = 5.0 * math.sqrt((0.2/10)**2 + (0.04/2)**2)
145
- self.assertAlmostEqual(c.uncertainty, expected, places=6)
146
-
147
- def test_division_one_uncertain(self):
148
- a = units.meter(10.0, uncertainty=0.2)
149
- b = units.second(2.0) # no uncertainty
150
- c = a / b
151
- self.assertAlmostEqual(c.value, 5.0, places=9)
152
- expected = 5.0 * (0.2/10)
153
- self.assertAlmostEqual(c.uncertainty, expected, places=9)
154
-
155
- def test_division_neither_uncertain(self):
156
- a = units.meter(10.0)
157
- b = units.second(2.0)
158
- c = a / b
159
- self.assertAlmostEqual(c.value, 5.0, places=9)
160
- self.assertIsNone(c.uncertainty)
161
-
162
-
163
- class TestScalarOperations(unittest.TestCase):
164
- """Test uncertainty propagation with scalar multiplication/division."""
165
-
166
- def test_scalar_multiply_number(self):
167
- a = units.meter(10.0, uncertainty=0.2)
168
- c = a * 3 # scalar multiplication
169
- self.assertAlmostEqual(c.value, 30.0, places=9)
170
- self.assertAlmostEqual(c.uncertainty, 0.6, places=9)
171
-
172
- def test_scalar_divide_number(self):
173
- a = units.meter(10.0, uncertainty=0.2)
174
- c = a / 2 # scalar division
175
- self.assertAlmostEqual(c.value, 5.0, places=9)
176
- self.assertAlmostEqual(c.uncertainty, 0.1, places=9)
177
-
178
-
179
- class TestConversionPropagation(unittest.TestCase):
180
- """Test uncertainty propagation through unit conversion."""
181
-
182
- def test_linear_conversion(self):
183
- # meter to foot: factor ≈ 3.28084
184
- length = units.meter(1.0, uncertainty=0.01)
185
- length_ft = length.to(units.foot)
186
- self.assertAlmostEqual(length_ft.value, 3.28084, places=4)
187
- # uncertainty scales by same factor
188
- self.assertAlmostEqual(length_ft.uncertainty, 0.01 * 3.28084, places=4)
189
-
190
- def test_affine_conversion(self):
191
- # Celsius to Kelvin: K = C + 273.15
192
- # Derivative is 1, so uncertainty unchanged
193
- temp = units.celsius(25.0, uncertainty=0.5)
194
- temp_k = temp.to(units.kelvin)
195
- self.assertAlmostEqual(temp_k.value, 298.15, places=9)
196
- self.assertAlmostEqual(temp_k.uncertainty, 0.5, places=9)
197
-
198
- def test_fahrenheit_to_celsius(self):
199
- # F to C: C = (F - 32) * 5/9
200
- # Derivative is 5/9 ≈ 0.5556
201
- temp = units.fahrenheit(100.0, uncertainty=1.0)
202
- temp_c = temp.to(units.celsius)
203
- self.assertAlmostEqual(temp_c.value, 37.7778, places=3)
204
- self.assertAlmostEqual(temp_c.uncertainty, 1.0 * (5/9), places=6)
205
-
206
- def test_conversion_without_uncertainty(self):
207
- length = units.meter(1.0)
208
- length_ft = length.to(units.foot)
209
- self.assertIsNone(length_ft.uncertainty)
210
-
211
-
212
- class TestEdgeCases(unittest.TestCase):
213
- """Test edge cases for uncertainty handling."""
214
-
215
- def test_zero_uncertainty(self):
216
- a = units.meter(10.0, uncertainty=0.0)
217
- b = units.meter(5.0, uncertainty=0.0)
218
- c = a + b
219
- self.assertAlmostEqual(c.uncertainty, 0.0, places=9)
220
-
221
- def test_very_small_uncertainty(self):
222
- a = units.meter(10.0, uncertainty=1e-15)
223
- b = units.meter(5.0, uncertainty=1e-15)
224
- c = a * b
225
- self.assertIsNotNone(c.uncertainty)
226
- self.assertGreater(c.uncertainty, 0)
227
-
228
- def test_uncertainty_preserved_through_simplify(self):
229
- km = Scale.kilo * units.meter
230
- n = km(5.0, uncertainty=0.1)
231
- simplified = n.simplify()
232
- self.assertAlmostEqual(simplified.value, 5000.0, places=9)
233
- # uncertainty also scales
234
- self.assertAlmostEqual(simplified.uncertainty, 100.0, places=9)
235
-
236
-
237
- class TestMapDerivative(unittest.TestCase):
238
- """Test Map.derivative() implementations."""
239
-
240
- def test_linear_map_derivative(self):
241
- from ucon.maps import LinearMap
242
- m = LinearMap(3.28084)
243
- self.assertAlmostEqual(m.derivative(0), 3.28084, places=9)
244
- self.assertAlmostEqual(m.derivative(100), 3.28084, places=9)
245
-
246
- def test_affine_map_derivative(self):
247
- from ucon.maps import AffineMap
248
- m = AffineMap(5/9, -32 * 5/9) # F to C
249
- self.assertAlmostEqual(m.derivative(0), 5/9, places=9)
250
- self.assertAlmostEqual(m.derivative(100), 5/9, places=9)
251
-
252
- def test_composed_map_derivative(self):
253
- from ucon.maps import LinearMap, AffineMap
254
- # Compose: first scale by 2, then add 10
255
- inner = LinearMap(2)
256
- outer = AffineMap(1, 10)
257
- composed = outer @ inner
258
- # d/dx [1*(2x) + 10] = 2
259
- self.assertAlmostEqual(composed.derivative(0), 2, places=9)
260
- self.assertAlmostEqual(composed.derivative(5), 2, places=9)
261
-
262
-
263
- if __name__ == "__main__":
264
- unittest.main()
tests/ucon/test_units.py DELETED
@@ -1,25 +0,0 @@
1
-
2
- # © 2025 The Radiativity Company
3
- # Licensed under the Apache License, Version 2.0
4
- # See the LICENSE file for details.
5
-
6
- from unittest import TestCase
7
-
8
- from ucon import units
9
- from ucon.core import Dimension
10
-
11
-
12
- class TestUnits(TestCase):
13
-
14
- def test_has_expected_basic_units(self):
15
- expected_basic_units = {'none', 'volt', 'liter', 'gram', 'second', 'kelvin', 'mole', 'coulomb'}
16
- missing = {name for name in expected_basic_units if not units.have(name)}
17
- assert not missing, f"Missing expected units: {missing}"
18
-
19
- def test___truediv__(self):
20
- self.assertEqual(units.none, units.gram / units.gram)
21
- self.assertEqual(units.gram, units.gram / units.none)
22
-
23
- composite_unit = units.gram / units.liter
24
- self.assertEqual("g/L", composite_unit.shorthand)
25
- self.assertEqual(Dimension.density, composite_unit.dimension)
@@ -1,24 +0,0 @@
1
- tests/ucon/__init__.py,sha256=9BAHYTs27Ed3VgqiMUH4XtVttAmOPgK0Zvj-dUNo7D8,119
2
- tests/ucon/test_algebra.py,sha256=NnoQOSMW8NJlOTnbr3M_5epvnDPXpVTgO21L2LcytRY,8503
3
- tests/ucon/test_core.py,sha256=bmwSRWPlhwossy5NJ9rcPWujFmzBBPOeZzPAzN1acFg,32631
4
- tests/ucon/test_default_graph_conversions.py,sha256=rkcDcSV1_kZeuPf4ModHDpgfkOPZS32xcKq7KPDRN-0,15760
5
- tests/ucon/test_dimensionless_units.py,sha256=K6BrIPOFL9IO_ksR8t_oJUXmjTgqBUzMdgaV-hZc52w,8410
6
- tests/ucon/test_quantity.py,sha256=md5nbmy0u2cFBdqNeu-ROhoj29vYrIlGm_AjlmCttgc,24519
7
- tests/ucon/test_uncertainty.py,sha256=KkJw2dJR0EToxPpBN24x735jr9fv6a2myxjvhOH4MPU,9649
8
- tests/ucon/test_units.py,sha256=SILymDtDNDyxEhkYQubrfkakKCMexwEwjyHfhrkDrMI,869
9
- tests/ucon/conversion/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
- tests/ucon/conversion/test_graph.py,sha256=fs0aP6qNf8eE1uI7SoGSCW2XAkHYb7T9aaI-kzmO02c,16955
11
- tests/ucon/conversion/test_map.py,sha256=DVFQ3xwp16Nuy9EtZRjKlWbkXfRUcM1mOzFrS4HhOaw,13886
12
- ucon/__init__.py,sha256=jaFcNFZC5gxHKzM8OkS1pLxltp6ToLRVpuuhJQY9FKQ,2000
13
- ucon/algebra.py,sha256=4JiT_SHHep86Sv3tVkgKsRY95lBRASMkyH4vOUA-gfM,7459
14
- ucon/core.py,sha256=nuDmSuGiG3xUW_kRpOWu9FDhNmKE0aJPwpj30lMdrgo,49464
15
- ucon/graph.py,sha256=lPoYSvHNGBZxeZ-4dyZIu2OS5R1JTo0qPZ9wd0vg-s4,15566
16
- ucon/maps.py,sha256=tWP4ayYCEazJzf81EP1_fmtADhg18D1eHldudAMEY0U,5460
17
- ucon/quantity.py,sha256=GBxZ_96nocx-8F-usNWGbPvWHRhRgdZzqfH9Sx69iC4,465
18
- ucon/units.py,sha256=u1ILwGllzNiwGLadlg5jguKPyFV1u-CZSUMgUDWTen4,7509
19
- ucon-0.5.1.dist-info/licenses/LICENSE,sha256=LtimSYBSw1L_X6n1-VEdZRdwuROzPumrMUNX21asFuI,11356
20
- ucon-0.5.1.dist-info/licenses/NOTICE,sha256=bh4fBOItio3kM4hSNYhqfFpcaAvOoixjD7Du8im-sYA,1079
21
- ucon-0.5.1.dist-info/METADATA,sha256=dkjXacaxxZbFq9vIBfJJ-2koy03T9Ozf_nimaNm2mwQ,13554
22
- ucon-0.5.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
23
- ucon-0.5.1.dist-info/top_level.txt,sha256=zZYRJiQrVUtN32ziJD2YEq7ClSvDmVYHYy5ArRAZGxI,11
24
- ucon-0.5.1.dist-info/RECORD,,
@@ -1,2 +0,0 @@
1
- tests
2
- ucon
File without changes