unitforge 0.5.3

A library for unit and quantity consistent computations in Rust
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import pytest
import numpy as np
from unitforge import *


def test_vector_missmatch():
    with pytest.raises(ValueError, match="The passed values must be of the same quantity."):
        _ = Vector3(1.4, 1.3, Force(9, ForceUnit.N))

def test_vector_zero():
    vec = Vector3.zero()
    assert vec[0] == 0.
    assert vec[1] == 0.
    assert vec[2] == 0.

def test_vector_x():
    vec = Vector3.x()
    assert vec[0] == 1.
    assert vec[1] == 0.
    assert vec[2] == 0.

def test_vector_y():
    vec = Vector3.y()
    assert vec[0] == 0.
    assert vec[1] == 1.
    assert vec[2] == 0.

def test_vector_y():
    vec = Vector3.z()
    assert vec[0] == 0.
    assert vec[1] == 0.
    assert vec[2] == 1.

def test_vector_set():
    vec = Vector3.from_list([0, 0, 0], DistanceUnit.m)
    vec[1] = Distance(12., DistanceUnit.m)
    assert isinstance(vec[0], Distance)
    assert vec[1] == Distance(12., DistanceUnit.m)
    assert isinstance(vec[2], Distance)

def test_vector_set_error():
    vec = Vector3.from_list([0, 0, 0])
    with pytest.raises(ValueError, match="The passed values must be of the same quantity."):
        vec[1] = Distance(12., DistanceUnit.m)

class TestVectorAdd():
    def test_vector_add_ok(self):
        a = Vector3(1., 12., 3.)
        b = Vector3(2., 1., 5.)
        c = a + b
        assert c[0] == 3.
        assert c[1] == 13.
        assert c[2] == 8.

    def test_vector_add_failing(self):
        with pytest.raises(TypeError, match="The passed values must be of the same quantity."):
            a = Vector3(1., 12., 3.)
            b = Vector3(Distance(2., DistanceUnit.mm), Distance(2., DistanceUnit.mm), Distance(2., DistanceUnit.mm))
            _ = a + b

def test_vector_sub():
    a = Vector3(1., 12., 3.)
    b = Vector3(2., 1., 5.)
    c = a - b
    assert c[0] == -1.
    assert c[1] == 11.
    assert c[2] == -2.

def test_vector_neg():
    a = Vector3(1., 12., 3.)
    b = -a
    assert b[0] == -1.
    assert b[1] == -12.
    assert b[2] == -3.

def test_vector_shared_unit():
    vec = Vector3(1., 12., 3., ForceUnit.kN)
    assert vec[0].to(ForceUnit.kN) == 1.
    assert vec[1].to(ForceUnit.kN) == 12.
    assert vec[2].to(ForceUnit.kN) == 3.

def test_vector_sub_failing():
    with pytest.raises(TypeError, match="The passed values must be of the same quantity."):
        a = Vector3(1., 12., 3.)
        b = Vector3(Distance(2., DistanceUnit.mm), Distance(2., DistanceUnit.mm), Distance(2., DistanceUnit.mm))
        _ = a - b

def test_vector_str():
    vec = Vector3(Distance(2., DistanceUnit.mm), Distance(2., DistanceUnit.mm), Distance(2., DistanceUnit.mm))
    assert str(vec) == 'Vector3: [DistanceQuantity(2 mm), DistanceQuantity(2 mm), DistanceQuantity(2 mm)]'

class TestListInterface:
    def test_vector_to_list(self):
        vec = Vector3(Distance(2., DistanceUnit.mm), Distance(3., DistanceUnit.mm), Distance(4., DistanceUnit.mm))
        lst = vec.to_list()
        assert lst[0] == Distance(2, DistanceUnit.mm)
        assert lst[1] == Distance(3, DistanceUnit.mm)
        assert lst[2] == Distance(4, DistanceUnit.mm)

    def test_vector_from_list_individual_unit(self):
        lst = [Distance(2, DistanceUnit.mm), Distance(3, DistanceUnit.mm), Distance(4, DistanceUnit.mm)]
        vec = Vector3.from_list(lst)
        assert vec[0] == Distance(2, DistanceUnit.mm)
        assert vec[1] == Distance(3, DistanceUnit.mm)
        assert vec[2] == Distance(4, DistanceUnit.mm)

    def test_vector_from_list_shared_unit(self):
        vec = Vector3.from_list([2, 3, 4], DistanceUnit.mm)
        assert vec[0] == Distance(2, DistanceUnit.mm)
        assert vec[1] == Distance(3, DistanceUnit.mm)
        assert vec[2] == Distance(4, DistanceUnit.mm)

    def test_vector_from_list_failing_size(self):
        with pytest.raises(ValueError, match="List must contain exactly 3 elements"):
            lst = [Distance(2, DistanceUnit.mm), Distance(4, DistanceUnit.mm)]
            vec = Vector3.from_list(lst)

    def test_vector_from_list_failing_quantities(self):
        with pytest.raises(ValueError, match="The passed values must be of the same quantity."):
            lst = [Distance(2, DistanceUnit.mm), Force(3, ForceUnit.N), Distance(4, DistanceUnit.mm)]
            vec = Vector3.from_list(lst)

def test_vector_norm():
    from math import isclose
    vec = Vector3(Distance(3., DistanceUnit.mm), Distance(4., DistanceUnit.mm), Distance(12., DistanceUnit.mm))
    norm = vec.norm()
    assert isclose(norm.to(DistanceUnit.mm), 13.)

def test_vector_norm_force():
    from math import isclose
    vec = Vector3(Force(3., ForceUnit.N), Force(4., ForceUnit.N), Force(12., ForceUnit.N))
    norm = vec.norm()
    assert isclose(norm.to(ForceUnit.N), 13.)

def test_vector_dot():
    a = Vector3(Distance(3., DistanceUnit.mm), Distance(4., DistanceUnit.mm), Distance(12., DistanceUnit.mm))
    b = Vector3(2., 1., 5.)
    c = a.dot_vec(b)
    assert c == Distance(70., DistanceUnit.mm)

def test_vector_to_unit_vector():
    vec = Vector3(Distance(3., DistanceUnit.mm), Distance(4., DistanceUnit.mm), Distance(12., DistanceUnit.mm))
    uv = vec.to_unit_vector()
    assert uv.norm() - 1. < 1E-15
    assert abs(uv.dot_vec(vec) / vec.norm() - 1) < 1E-10

def test_vector_cross():
    vec_a = Vector3(Distance(10., DistanceUnit.mm), Distance.zero(), Distance.zero())
    vec_b = Vector3(Force.zero(), Force(50., ForceUnit.N), Force.zero())
    res = vec_a.cross(vec_b)
    assert res[0].to(ForceDistanceUnit.Nm) == 0.
    assert res[1].to(ForceDistanceUnit.Nm) == 0.
    assert res[2].to(ForceDistanceUnit.Nm) == 0.5

def test_vector_cross_self_same_quantity_zero():
    vec = Vector3(Force(3., ForceUnit.N), Force(4., ForceUnit.N), Force(12., ForceUnit.N))
    res = vec.cross(vec)
    assert res[0].to(ForceUnit.N) == 0.
    assert res[1].to(ForceUnit.N) == 0.
    assert res[2].to(ForceUnit.N) == 0.

class TestNumpyInterface:
    def test_vector_from_array(self):
        arr = np.array([1., 2., 3.])
        vec = Vector3.from_array(arr, ForceUnit.N)
        assert vec[0].to(ForceUnit.N) == 1.
        assert vec[1].to(ForceUnit.N) == 2.
        assert vec[2].to(ForceUnit.N) == 3.

    def test_vector_to_array(self):
        vec = Vector3(Distance(3., DistanceUnit.mm), Distance(4., DistanceUnit.mm), Distance(12., DistanceUnit.mm))
        arr = vec.to_array(DistanceUnit.mm)
        assert (arr == np.array([3., 4., 12.])).all()

    def test_vector_3_array_interface_no_units(self):
        a = np.array([1., 2., 3.])
        vec = Vector3.from_array(a)
        b = vec.to_array()
        assert np.linalg.norm(a - b) < 1E-10

def test_vector_mul():
    vec = Vector3(Distance(3., DistanceUnit.mm), Distance(4., DistanceUnit.mm), Distance(12., DistanceUnit.mm))
    res = vec * Force(2., ForceUnit.N)
    assert res[0].to(ForceDistanceUnit.Nm) == 0.006
    assert res[1].to(ForceDistanceUnit.Nm) == 0.008
    assert res[2].to(ForceDistanceUnit.Nm) == 0.024

def test_vector_float_mul_quantity():
    res = Vector3.x() * Force(1., ForceUnit.N)
    assert res[0] == Force(1., ForceUnit.N)
    assert res[1] == Force(0., ForceUnit.N)
    assert res[2] == Force(0., ForceUnit.N)

def test_vector_mul_unsupported_rhs_raises_type_error():
    vec = Vector3(Distance(3., DistanceUnit.mm), Distance(4., DistanceUnit.mm), Distance(12., DistanceUnit.mm))
    with pytest.raises(TypeError):
        _ = vec * object()

def test_vector_mul_unsupported_quantity_relation_raises_type_error():
    vec = Vector3(Force(3., ForceUnit.N), Force(4., ForceUnit.N), Force(12., ForceUnit.N))
    with pytest.raises(TypeError):
        _ = vec * Time(1., TimeUnit.s)

def test_vector_div():
    vec = Vector3(Force(3., ForceUnit.N), Force(4., ForceUnit.N), Force(12., ForceUnit.N))
    res = vec / Area(2., AreaUnit.mmsq)
    assert res[0].to(StressUnit.MPa) == 1.5
    assert res[1].to(StressUnit.MPa) == 2.
    assert res[2].to(StressUnit.MPa) == 6.

def test_vector_div_unsupported_rhs_raises_type_error():
    vec = Vector3(Force(3., ForceUnit.N), Force(4., ForceUnit.N), Force(12., ForceUnit.N))
    with pytest.raises(TypeError):
        _ = vec / object()

def test_vector_imul_quantity_mutates_in_place():
    vec = Vector3.x()
    original_id = id(vec)
    vec *= Force(2., ForceUnit.N)
    assert id(vec) == original_id
    assert vec[0] == Force(2., ForceUnit.N)
    assert vec[1] == Force(0., ForceUnit.N)
    assert vec[2] == Force(0., ForceUnit.N)

def test_vector_itruediv_quantity_mutates_in_place():
    vec = Vector3(Force(3., ForceUnit.N), Force(4., ForceUnit.N), Force(12., ForceUnit.N))
    original_id = id(vec)
    vec /= Area(2., AreaUnit.mmsq)
    assert id(vec) == original_id
    assert vec[0].to(StressUnit.MPa) == 1.5
    assert vec[1].to(StressUnit.MPa) == 2.
    assert vec[2].to(StressUnit.MPa) == 6.

def test_vector_imul_unsupported_quantity_leaves_vector_unchanged():
    vec = Vector3(Force(3., ForceUnit.N), Force(4., ForceUnit.N), Force(12., ForceUnit.N))
    before = vec.to_list()
    with pytest.raises(TypeError):
        vec *= Time(1., TimeUnit.s)
    assert vec.to_list() == before

def test_vector_matrix_zero():
    mat = Matrix3.zero()
    for i in range(3):
        for j in range(3):
            assert mat[0, 0] == 0.

def test_matrix_set():
    mat = Matrix3.from_list([[0, 0, 0], [0, 0, 0], [0, 0, 0]], ForceUnit.N)
    mat[0, 1] = Force(12, ForceUnit.N)
    assert mat[0, 0].to(ForceUnit.N) == 0.
    assert mat[0, 1].to(ForceUnit.N) == 12.
    assert mat[0, 2].to(ForceUnit.N) == 0.
    assert mat[1, 0].to(ForceUnit.N) == 0.
    assert mat[1, 1].to(ForceUnit.N) == 0.
    assert mat[1, 2].to(ForceUnit.N) == 0.
    assert mat[2, 0].to(ForceUnit.N) == 0.
    assert mat[2, 1].to(ForceUnit.N) == 0.
    assert mat[2, 2].to(ForceUnit.N) == 0.

def test_matrix_set_error():
    mat = Matrix3.from_list([[0, 0, 0], [0, 0, 0], [0, 0, 0]], ForceUnit.N)
    with pytest.raises(ValueError, match="The passed values must be of the same quantity."):
        mat[1, 2] = Distance(12., DistanceUnit.m)

def test_matrix_identity():
    mat = Matrix3.identity()
    for i in range(3):
        for j in range(3):
            if i == j:
                assert mat[i, j] == 1.
            else:
                assert mat[i, j] == 0.

def test_matrix_str():
    mat = Matrix3.from_list([[1, 3, 4], [5, 7, 3], [7, 8, 9]], ForceUnit.N)
    assert str(mat) == 'Matrix3: [\n[ForceQuantity(1 N), ForceQuantity(3 N), ForceQuantity(4 N)]\n[ForceQuantity(5 N), ForceQuantity(7 N), ForceQuantity(3 N)]\n[ForceQuantity(7 N), ForceQuantity(8 N), ForceQuantity(9 N)]]'

def test_matrix_missing_binary_arithmetic_raises_type_error():
    mat = Matrix3.zero()
    with pytest.raises(TypeError):
        _ = mat + mat
    with pytest.raises(TypeError):
        _ = mat * 2.


class TestMatrixListInterface:
    def test_matrix_from_list(self):
        mat = Matrix3.from_list([[1, 3, 4], [5, 7, 3], [7, 8, 9]], ForceUnit.N)
        assert mat[0, 0].to(ForceUnit.N) == 1.
        assert mat[1, 0].to(ForceUnit.N) == 5.
        assert mat[0, 1].to(ForceUnit.N) == 3.
        assert mat[2, 2].to(ForceUnit.N) == 9.

    def test_matrix_to_list_with_unit(self):
        lst = [[1, 3, 4], [5, 7, 3], [7, 8, 9]]
        mat = Matrix3.from_list(lst, ForceUnit.N)
        assert lst == mat.to_list(ForceUnit.N)

    def test_matrix_to_list_without_unit(self):
        lst = [[1, 3, 4], [5, 7, 3], [7, 8, 9]]
        mat = Matrix3.from_list(lst, ForceUnit.N)
        read_lst = mat.to_list()
        for i in range(3):
            for j in range(3):
                assert read_lst[i][j].to(ForceUnit.N) == lst[i][j]

class TestMatrixArrayInterface:
    def test_matrix_from_array(self):
        mat = Matrix3.from_array(np.array([[1., 3., 4.], [5., 7., 3.], [7., 8., 9.]]), ForceUnit.N)
        assert mat[0, 0].to(ForceUnit.N) == 1.
        assert mat[1, 0].to(ForceUnit.N) == 5.
        assert mat[0, 1].to(ForceUnit.N) == 3.
        assert mat[2, 2].to(ForceUnit.N) == 9.

    def test_matrix_to_array(self):
        array_in = np.array([[1., 3., 4.], [5., 7., 3.], [7., 8., 9.]])
        mat = Matrix3.from_array(array_in, ForceUnit.N)
        array_out = mat.to_array(ForceUnit.N)
        assert (array_in == array_out).all()

    def test_matrix_array_interface_no_units(self):
        a = np.array([[1., 3., 4.], [5., 7., 3.], [7., 8., 9.]])
        mat = Matrix3.from_array(a)
        b = mat.to_array()
        assert np.linalg.norm(a - b) < 1E-10

def test_matrix_transpose():
    mat = Matrix3.from_list([[1, 3, 4], [5, 7, 3], [7, 8, 9]])
    mat_t = mat.transpose()
    for i in range(3):
        for j in range(3):
            assert mat[i, j] == mat_t[j, i]

class TestMatrixFromRows:
    def test_from_rows_different_quantities(self):
        with pytest.raises(ValueError, match="All vectors must have the same Quantity."):
            Matrix3.from_rows([Vector3.from_list([1., 0., 1.], ForceUnit.N), Vector3.y(), Vector3.z()])

    def test_from_rows(self):
        mat = Matrix3.from_rows([Vector3.from_list([1., 0., 1.]), Vector3.y(), Vector3.z()])
        assert mat[0, 0] == 1.
        assert mat[0, 1] == 0.
        assert mat[0, 2] == 1.
        assert mat[1, 0] == 0.
        assert mat[1, 1] == 1.
        assert mat[1, 2] == 0.
        assert mat[2, 0] == 0.
        assert mat[2, 1] == 0.
        assert mat[2, 2] == 1.

def test_from_columns():
    mat = Matrix3.from_columns([Vector3.from_list([1., 0., 1.]), Vector3.y(), Vector3.z()]).transpose()
    assert mat[0, 0] == 1.
    assert mat[0, 1] == 0.
    assert mat[0, 2] == 1.
    assert mat[1, 0] == 0.
    assert mat[1, 1] == 1.
    assert mat[1, 2] == 0.
    assert mat[2, 0] == 0.
    assert mat[2, 1] == 0.
    assert mat[2, 2] == 1.

class TestMatrixRowInterface:
    def test_matrix_get_row(self):
        mat = Matrix3.from_columns([Vector3.from_list([1., 0., 1.]), Vector3.y(), Vector3.z()]).transpose()
        row = mat.get_row(2)
        assert row[0] == 0.
        assert row[1] == 0.
        assert row[2] == 1.

    def test_matrix_set_row(self):
        mat = Matrix3.from_columns([Vector3.x(), Vector3.y(), Vector3.z()]).transpose()
        mat.set_row(1, Vector3.from_list([1., 0., 1.]))
        assert mat[1, 0] == 1.
        assert mat[1, 1] == 0.
        assert mat[1, 2] == 1.

class TestMatrixColumnInterface:
    def test_matrix_get_column(self):
        mat = Matrix3.from_columns([Vector3.from_list([1., 0., 1.]), Vector3.y(), Vector3.z()]).transpose()
        column = mat.get_column(2)
        assert column[0] == 1.
        assert column[1] == 0.
        assert column[2] == 1.

    def test_matrix_set_column(self):
        mat = Matrix3.from_columns([Vector3.x(), Vector3.y(), Vector3.z()]).transpose()
        mat.set_column(1, Vector3.from_list([1., 0., 1.]))
        assert mat[0, 1] == 1.
        assert mat[1, 1] == 0.
        assert mat[2, 1] == 1.

class TestMatrixConstructor:
    def test_matrix_constructor_no_args(self):
        mat = Matrix3()
        assert mat == Matrix3.zero()

    def test_matrix_constructor_list(self):
        mat = Matrix3([[1, 3, 4], [5, 7, 3], [7, 8, 9]], ForceUnit.N)
        assert mat == Matrix3.from_list([[1, 3, 4], [5, 7, 3], [7, 8, 9]], ForceUnit.N)

    def test_matrix_constructor_array(self):
        mat = Matrix3(np.array([[1., 3., 4.], [5., 7., 3.], [7., 8., 9.]]), ForceUnit.N)
        assert mat == Matrix3.from_array(np.array([[1., 3., 4.], [5., 7., 3.], [7., 8., 9.]]), ForceUnit.N)