symengine 0.14.0__cp313-cp313t-win_amd64.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.
Files changed (50) hide show
  1. symengine/lib/flint-19.dll +0 -0
  2. symengine/lib/libgcc_s_seh-1.dll +0 -0
  3. symengine/lib/libgmp-10.dll +0 -0
  4. symengine/lib/libmpc-3.dll +0 -0
  5. symengine/lib/libmpfr-6.dll +0 -0
  6. symengine/lib/libwinpthread-1.dll +0 -0
  7. symengine/lib/pywrapper.h +220 -0
  8. symengine/lib/symengine.pxd +955 -0
  9. symengine/lib/symengine_wrapper.cp313t-win_amd64.lib +0 -0
  10. symengine/lib/symengine_wrapper.cp313t-win_amd64.pyd +0 -0
  11. symengine/lib/symengine_wrapper.pxd +78 -0
  12. symengine/lib/zlib.dll +0 -0
  13. symengine/lib/zstd.dll +0 -0
  14. symengine-0.14.0.data/purelib/symengine/__init__.py +79 -0
  15. symengine-0.14.0.data/purelib/symengine/functions.py +10 -0
  16. symengine-0.14.0.data/purelib/symengine/lib/__init__.py +0 -0
  17. symengine-0.14.0.data/purelib/symengine/printing.py +33 -0
  18. symengine-0.14.0.data/purelib/symengine/sympy_compat.py +4 -0
  19. symengine-0.14.0.data/purelib/symengine/test_utilities.py +95 -0
  20. symengine-0.14.0.data/purelib/symengine/tests/__init__.py +0 -0
  21. symengine-0.14.0.data/purelib/symengine/tests/test_arit.py +261 -0
  22. symengine-0.14.0.data/purelib/symengine/tests/test_cse.py +17 -0
  23. symengine-0.14.0.data/purelib/symengine/tests/test_dict_basic.py +28 -0
  24. symengine-0.14.0.data/purelib/symengine/tests/test_eval.py +67 -0
  25. symengine-0.14.0.data/purelib/symengine/tests/test_expr.py +28 -0
  26. symengine-0.14.0.data/purelib/symengine/tests/test_functions.py +432 -0
  27. symengine-0.14.0.data/purelib/symengine/tests/test_lambdify.py +863 -0
  28. symengine-0.14.0.data/purelib/symengine/tests/test_logic.py +124 -0
  29. symengine-0.14.0.data/purelib/symengine/tests/test_matrices.py +757 -0
  30. symengine-0.14.0.data/purelib/symengine/tests/test_ntheory.py +254 -0
  31. symengine-0.14.0.data/purelib/symengine/tests/test_number.py +186 -0
  32. symengine-0.14.0.data/purelib/symengine/tests/test_pickling.py +59 -0
  33. symengine-0.14.0.data/purelib/symengine/tests/test_printing.py +38 -0
  34. symengine-0.14.0.data/purelib/symengine/tests/test_sage.py +175 -0
  35. symengine-0.14.0.data/purelib/symengine/tests/test_series_expansion.py +22 -0
  36. symengine-0.14.0.data/purelib/symengine/tests/test_sets.py +118 -0
  37. symengine-0.14.0.data/purelib/symengine/tests/test_solve.py +25 -0
  38. symengine-0.14.0.data/purelib/symengine/tests/test_subs.py +82 -0
  39. symengine-0.14.0.data/purelib/symengine/tests/test_symbol.py +179 -0
  40. symengine-0.14.0.data/purelib/symengine/tests/test_sympify.py +63 -0
  41. symengine-0.14.0.data/purelib/symengine/tests/test_sympy_compat.py +200 -0
  42. symengine-0.14.0.data/purelib/symengine/tests/test_sympy_conv.py +835 -0
  43. symengine-0.14.0.data/purelib/symengine/tests/test_var.py +68 -0
  44. symengine-0.14.0.data/purelib/symengine/utilities.py +280 -0
  45. symengine-0.14.0.dist-info/AUTHORS +40 -0
  46. symengine-0.14.0.dist-info/LICENSE +430 -0
  47. symengine-0.14.0.dist-info/METADATA +39 -0
  48. symengine-0.14.0.dist-info/RECORD +50 -0
  49. symengine-0.14.0.dist-info/WHEEL +5 -0
  50. symengine-0.14.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,757 @@
1
+ from symengine import symbols, init_printing
2
+ from symengine.lib.symengine_wrapper import (DenseMatrix, Symbol, Integer,
3
+ Rational, function_symbol, I, NonSquareMatrixError, ShapeError, zeros,
4
+ ones, eye, ImmutableMatrix)
5
+ from symengine.test_utilities import raises
6
+ import unittest
7
+
8
+
9
+ try:
10
+ import numpy as np
11
+ have_numpy = True
12
+ except ImportError:
13
+ have_numpy = False
14
+
15
+ try:
16
+ import sympy
17
+ from sympy.core.cache import clear_cache
18
+ import atexit
19
+ atexit.register(clear_cache)
20
+ have_sympy = True
21
+ except ImportError:
22
+ have_sympy = False
23
+
24
+
25
+ def test_init():
26
+ raises(ValueError, lambda: DenseMatrix(2, 1, [0]*4))
27
+
28
+
29
+ def test_get():
30
+ A = DenseMatrix([[1, 2], [3, 4]])
31
+
32
+ assert A.get(0, 0) == 1
33
+ assert A.get(0, 1) == 2
34
+ assert A.get(1, 1) == 4
35
+
36
+ a = Symbol("a")
37
+ b = Symbol("b")
38
+ c = Symbol("c")
39
+ d = Symbol("d")
40
+ A = DenseMatrix(2, 2, [a, b, c, d])
41
+
42
+ assert A.get(0, 0) == a
43
+ assert A.get(1, 0) == c
44
+ assert A.get(1, 1) == d
45
+
46
+ assert A.get(-1, 0) == c
47
+ assert A.get(-1, -1) == d
48
+
49
+ raises(IndexError, lambda: A.get(2, 0))
50
+ raises(IndexError, lambda: A.get(0, 2))
51
+ raises(IndexError, lambda: A.get(-3, 0))
52
+
53
+
54
+ def test_tolist():
55
+ A = DenseMatrix([2, 3])
56
+ assert A.shape == (2, 1)
57
+ assert A.tolist() == [[2], [3]]
58
+
59
+
60
+ def test_get_item():
61
+ A = DenseMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
62
+
63
+ assert A[5] == 6
64
+ assert A[-1] == 9
65
+ assert A[2, 2] == 9
66
+ assert A[-2, 2] == 6
67
+
68
+ assert A[1:2, 0] == DenseMatrix(1, 1, [4])
69
+ assert A[1:3, 0] == DenseMatrix(2, 1, [4, 7])
70
+ assert A[1:3, 0] == DenseMatrix(2, 1, [4, 7])
71
+ assert A[1:3, 1:] == DenseMatrix(2, 2, [5, 6, 8, 9])
72
+ assert A[1:3, :1] == DenseMatrix(2, 1, [4, 7])
73
+ assert A[0, 0:] == DenseMatrix(1, 3, [1, 2, 3])
74
+ assert A[2, :] == DenseMatrix(1, 3, [7, 8, 9])
75
+ assert A[:2, -2:] == DenseMatrix(2, 2, [2, 3, 5, 6])
76
+ assert A[1:, :3] == DenseMatrix(2, 3, [4, 5, 6, 7, 8, 9])
77
+ assert A[1:] == [2, 3, 4, 5, 6, 7, 8, 9]
78
+ assert A[-2:] == [8, 9]
79
+ assert A[[0, 2], 0] == DenseMatrix(2, 1, [1, 7])
80
+ assert A[[0, 2], [0]] == DenseMatrix(2, 1, [1, 7])
81
+ assert A[0, [0, 2]] == DenseMatrix(1, 2, [1, 3])
82
+ assert A[[0], [0, 2]] == DenseMatrix(1, 2, [1, 3])
83
+
84
+ raises(IndexError, lambda: A[-10])
85
+ raises(IndexError, lambda: A[9])
86
+
87
+ raises(IndexError, lambda: A[1:3, 3])
88
+ raises(IndexError, lambda: A[1:3, -4])
89
+
90
+ A = zeros(3, 4)
91
+ assert list(A[0, :]) == [0, 0, 0, 0]
92
+ assert list(A[:, 0]) == [0, 0, 0]
93
+
94
+ A = zeros(4, 3)
95
+ assert list(A[:, 0]) == [0, 0, 0, 0]
96
+ assert list(A[0, :]) == [0, 0, 0]
97
+
98
+
99
+ def test_set_item():
100
+ A = DenseMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
101
+
102
+ A[2] = 7
103
+ A[2, 2] = 8
104
+ A[-2] = 3
105
+ A[-2, -1] = 1
106
+
107
+ assert A == DenseMatrix(3, 3, [1, 2, 7, 4, 5, 1, 7, 3, 8])
108
+
109
+ A[0, :] = [10, 11, 12]
110
+ assert A == DenseMatrix(3, 3, [10, 11, 12, 4, 5, 1, 7, 3, 8])
111
+
112
+ A[:, 1] = [13, 14, 15]
113
+ assert A == DenseMatrix(3, 3, [10, 13, 12, 4, 14, 1, 7, 15, 8])
114
+
115
+ A[0::2, :] = [[1, 2, 3], [4, 5, 6]]
116
+ assert A == DenseMatrix(3, 3, [1, 2, 3, 4, 14, 1, 4, 5, 6])
117
+
118
+ B = DenseMatrix(A)
119
+ B[[0, 2], 0] = -1
120
+ assert B == DenseMatrix(3, 3, [-1, 2, 3, 4, 14, 1, -1, 5, 6])
121
+
122
+ B = DenseMatrix(A)
123
+ B[[0, 2], 0] = [-1, -2]
124
+ assert B == DenseMatrix(3, 3, [-1, 2, 3, 4, 14, 1, -2, 5, 6])
125
+
126
+ B = DenseMatrix(A)
127
+ B[[0, 2], 0] = [[-1], [-2]]
128
+ assert B == DenseMatrix(3, 3, [-1, 2, 3, 4, 14, 1, -2, 5, 6])
129
+
130
+ B = DenseMatrix(A)
131
+ B[[0, 2], [0]] = [-1, -2]
132
+ assert B == DenseMatrix(3, 3, [-1, 2, 3, 4, 14, 1, -2, 5, 6])
133
+
134
+ B = DenseMatrix(A)
135
+ B[[0, 2], [0]] = [[-1], [-2]]
136
+ assert B == DenseMatrix(3, 3, [-1, 2, 3, 4, 14, 1, -2, 5, 6])
137
+
138
+ B = DenseMatrix(A)
139
+ B[0, [0, 2]] = [-1, -2]
140
+ assert B == DenseMatrix(3, 3, [-1, 2, -2, 4, 14, 1, 4, 5, 6])
141
+
142
+ B = DenseMatrix(A)
143
+ B[0, [0, 2]] = -1
144
+ assert B == DenseMatrix(3, 3, [-1, 2, -1, 4, 14, 1, 4, 5, 6])
145
+
146
+ B = DenseMatrix(A)
147
+ B[:, [0, 2]] = -1
148
+ assert B == DenseMatrix(3, 3, [-1, 2, -1, -1, 14, -1, -1, 5, -1])
149
+
150
+ B = DenseMatrix(A)
151
+ B[[0, 1], [0, 2]] = -1
152
+ assert B == DenseMatrix(3, 3, [-1, 2, -1, -1, 14, -1, 4, 5, 6])
153
+
154
+ A = zeros(3, 4)
155
+ B = ones(1, 4)
156
+ A[0, :] = B
157
+ assert A[0, :] == B
158
+
159
+ A = zeros(3, 4)
160
+ B = ones(3, 1)
161
+ A[:, 0] = B
162
+ assert A[:, 0] == B
163
+
164
+
165
+ def test_set():
166
+ i7 = Integer(7)
167
+ y = Symbol("y")
168
+ g = function_symbol("g", y)
169
+ c = 2*I + 3
170
+ A = DenseMatrix(2, 2,
171
+ [Integer(5), Symbol("x"),
172
+ function_symbol("f", Symbol("x")), 1 + I])
173
+
174
+ A.set(0, 0, i7)
175
+ assert A.get(0, 0) == i7
176
+ A.set(0, 1, y)
177
+ assert A.get(0, 1) == y
178
+ A.set(1, 0, g)
179
+ assert A.get(1, 0) == g
180
+ A.set(1, 1, c)
181
+ assert A.get(1, 1) == c
182
+
183
+
184
+ def test_det():
185
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
186
+ assert A.det() == -2
187
+
188
+ a = Symbol("a")
189
+ b = Symbol("b")
190
+ c = Symbol("c")
191
+ d = Symbol("d")
192
+ A = DenseMatrix(2, 2, [a, b, c, d])
193
+ assert A.det() == a*d - b*c
194
+
195
+ A = DenseMatrix(3, 2, [1, 2, 3, 4, 5, 6])
196
+ raises(NonSquareMatrixError, lambda: A.det())
197
+
198
+
199
+ def test_inv():
200
+ A = DenseMatrix(2, 2, [1, 0, 0, 1])
201
+ assert A.inv() == A
202
+
203
+ A = DenseMatrix(2, 2, [1, 2, 2, 3])
204
+ B = DenseMatrix(2, 2, [-3, 2, 2, -1])
205
+
206
+ assert A.inv('LU') == B
207
+ assert A.inv('FFLU') == B
208
+ assert A.inv('GJ') == B
209
+
210
+
211
+ def test_add_matrix():
212
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
213
+ B = DenseMatrix(2, 2, [1, 0, 0, 1])
214
+
215
+ assert A.add_matrix(B) == DenseMatrix(2, 2, [2, 2, 3, 5])
216
+
217
+ a = Symbol("a")
218
+ b = Symbol("b")
219
+ c = Symbol("c")
220
+ d = Symbol("d")
221
+ A = DenseMatrix(2, 2, [a + b, a - b, a, b])
222
+ B = DenseMatrix(2, 2, [a - b, a + b, -a, b])
223
+
224
+ assert A.add_matrix(B) == DenseMatrix(2, 2, [2*a, 2*a, 0, 2*b])
225
+ assert A + B == DenseMatrix(2, 2, [2*a, 2*a, 0, 2*b])
226
+
227
+ C = DenseMatrix(1, 2, [a, b])
228
+ raises(ShapeError, lambda: A + C)
229
+
230
+
231
+ def test_mul_matrix():
232
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
233
+ B = DenseMatrix(2, 2, [1, 0, 0, 1])
234
+
235
+ assert A.mul_matrix(B) == A
236
+
237
+ a = Symbol("a")
238
+ b = Symbol("b")
239
+ c = Symbol("c")
240
+ d = Symbol("d")
241
+ A = DenseMatrix(2, 2, [a, b, c, d])
242
+ B = DenseMatrix(2, 2, [1, 0, 1, 0])
243
+
244
+ assert A.mul_matrix(B) == DenseMatrix(2, 2, [a + b, 0, c + d, 0])
245
+ assert A * B == DenseMatrix(2, 2, [a + b, 0, c + d, 0])
246
+ assert A @ B == DenseMatrix(2, 2, [a + b, 0, c + d, 0])
247
+ assert (A @ DenseMatrix(2, 1, [0]*2)).shape == (2, 1)
248
+
249
+ C = DenseMatrix(2, 3, [1, 2, 3, 2, 3, 4])
250
+ D = DenseMatrix(3, 2, [3, 4, 4, 5, 5, 6])
251
+
252
+ assert C.mul_matrix(D) == DenseMatrix(2, 2, [26, 32, 38, 47])
253
+
254
+ raises(ShapeError, lambda: A*D)
255
+
256
+
257
+ def test_multiply_elementwise():
258
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
259
+ B = DenseMatrix(2, 2, [1, 0, 0, 1])
260
+
261
+ assert A.multiply_elementwise(B) == DenseMatrix(2, 2, [1, 0, 0, 4])
262
+
263
+
264
+ def test_add_scalar():
265
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
266
+
267
+ a = Symbol("a")
268
+ assert A.add_scalar(a) == DenseMatrix(2, 2, [1 + a, 2 + a, 3 + a, 4 + a])
269
+
270
+ i5 = Integer(5)
271
+ assert A.add_scalar(i5) == DenseMatrix(2, 2, [6, 7, 8, 9])
272
+ raises(TypeError, lambda: A + 5)
273
+ raises(TypeError, lambda: 5 + A)
274
+
275
+
276
+ def test_mul_scalar():
277
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
278
+
279
+ a = Symbol("a")
280
+ assert A.mul_scalar(a) == DenseMatrix(2, 2, [a, 2*a, 3*a, 4*a])
281
+
282
+ i5 = Integer(5)
283
+ assert A.mul_scalar(i5) == DenseMatrix(2, 2, [5, 10, 15, 20])
284
+ assert A * 5 == DenseMatrix(2, 2, [5, 10, 15, 20])
285
+ assert 5 * A == DenseMatrix(2, 2, [5, 10, 15, 20])
286
+ assert a * A == DenseMatrix(2, 2, [a, 2*a, 3*a, 4*a])
287
+
288
+
289
+ def test_neg_abs():
290
+ A = DenseMatrix(2, 3, [1, 2, 3, 4, 5, 6])
291
+ B = DenseMatrix(2, 3, [-1, -2, -3, -4, -5, -6])
292
+ assert -A == B
293
+
294
+ assert A == abs(B)
295
+
296
+
297
+ def test_sub():
298
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
299
+ B = DenseMatrix(2, 2, [0, -1, -2, -3])
300
+ a = Symbol("a")
301
+ assert A - B == DenseMatrix(2, 2, [1, 3, 5, 7])
302
+
303
+ C = DenseMatrix(2, 1, [1, 2])
304
+ raises(ShapeError, lambda: A - C)
305
+ raises(TypeError, lambda: A - 5)
306
+ raises(TypeError, lambda: 5 - A)
307
+
308
+
309
+ def test_div():
310
+ w, x, y, z = symbols("w, x, y, z")
311
+ A = DenseMatrix([[w, x], [y, z]])
312
+ B = DenseMatrix([[1, 1], [1, 0]])
313
+ C = DenseMatrix([[x, w - x], [z, y - z]])
314
+
315
+ assert A / 2 == DenseMatrix([[w/2, x/2], [y/2, z/2]])
316
+ assert C * B == A
317
+ assert A / B == C
318
+
319
+ raises(TypeError, lambda: 2/A)
320
+
321
+
322
+ def test_transpose():
323
+ A = DenseMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
324
+
325
+ assert A.transpose() == DenseMatrix(3, 3, [1, 4, 7, 2, 5, 8, 3, 6, 9])
326
+
327
+ A = DenseMatrix(2, 2, [1, 2, 2, 1])
328
+
329
+ assert A.transpose() == A
330
+
331
+
332
+ def test_conjugate():
333
+ A = DenseMatrix(2, 2, [1, 2, 3, I])
334
+ assert A.conjugate() == DenseMatrix(2, 2, [1, 2, 3, -I])
335
+
336
+
337
+ def test_conjugate_transpose():
338
+ A = DenseMatrix(2, 2, [1, 2, 3, I])
339
+ assert A.conjugate_transpose() == DenseMatrix(2, 2, [1, 3, 2, -I])
340
+
341
+
342
+ def test_trace():
343
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
344
+ assert A.trace() == 5
345
+
346
+
347
+ def test_is_zero_matrix():
348
+ A = DenseMatrix(2, 2, [1, 2, 3, I])
349
+ assert not A.is_zero_matrix
350
+ B = DenseMatrix(1, 1, [Symbol('x')])
351
+ assert B.is_zero_matrix is None
352
+ C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 0, 0])
353
+ assert C.is_zero_matrix
354
+
355
+
356
+ def test_is_real_matrix():
357
+ A = DenseMatrix(2, 2, [1, 2, 3, I])
358
+ assert not A.is_real_matrix
359
+ B = DenseMatrix(1, 1, [Symbol('x')])
360
+ assert B.is_real_matrix is None
361
+ C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 0, 0])
362
+ assert C.is_real_matrix
363
+
364
+
365
+ def test_is_diagonal():
366
+ A = DenseMatrix(2, 2, [1, 0, 0, I])
367
+ assert A.is_diagonal
368
+ B = DenseMatrix(1, 1, [Symbol('x')])
369
+ assert B.is_diagonal
370
+ C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 2, 0])
371
+ assert not C.is_diagonal
372
+
373
+
374
+ def test_is_symmetric():
375
+ A = DenseMatrix(2, 2, [1, 3, 2, I])
376
+ assert not A.is_symmetric
377
+ B = DenseMatrix(1, 1, [Symbol('x')])
378
+ assert B.is_symmetric
379
+ C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 2, 0])
380
+ assert not C.is_symmetric
381
+
382
+
383
+ def test_is_hermitian():
384
+ A = DenseMatrix(2, 2, [1, 3, 2, I])
385
+ assert not A.is_hermitian
386
+ B = DenseMatrix(1, 1, [Symbol('x')])
387
+ assert B.is_hermitian is None
388
+ C = DenseMatrix(3, 3, [0, I, 0, 0, 0, 0, 0, 2, 0])
389
+ assert not C.is_hermitian
390
+
391
+
392
+ def test_is_weakly_diagonally_dominant():
393
+ A = DenseMatrix(2, 2, [2, 1, 1, 2])
394
+ assert A.is_weakly_diagonally_dominant
395
+ C = DenseMatrix(3, 3, [Symbol('x'), 0, 0, 0, 3, 0, 0, 0, 4])
396
+ assert C.is_weakly_diagonally_dominant is None
397
+
398
+
399
+ def test_is_strongly_diagonally_dominant():
400
+ A = DenseMatrix(2, 2, [2, 1, 1, 2])
401
+ assert A.is_strongly_diagonally_dominant
402
+ C = DenseMatrix(3, 3, [Symbol('x'), 2, 0, 0, 4, 0, 0, 0, 4])
403
+ assert C.is_strongly_diagonally_dominant is None
404
+
405
+
406
+ def test_is_positive_definite():
407
+ A = DenseMatrix(2, 2, [2, 1, 1, 2])
408
+ assert A.is_positive_definite
409
+ C = DenseMatrix(3, 3, [Symbol('x'), 2, 0, 0, 4, 0, 0, 0, 4])
410
+ assert C.is_positive_definite is None
411
+
412
+
413
+ def test_is_negative_definite():
414
+ A = DenseMatrix(2, 2, [-2, -1, -1, -2])
415
+ assert A.is_negative_definite
416
+ C = DenseMatrix(3, 3, [Symbol('x'), -2, 0, 0, -4, 0, 0, 0, -4])
417
+ assert C.is_negative_definite is None
418
+
419
+
420
+ def test_LU():
421
+ A = DenseMatrix(3, 3, [1, 3, 5, 2, 5, 6, 8, 3, 1])
422
+ L, U = A.LU()
423
+
424
+ assert L == DenseMatrix(3, 3, [1, 0, 0, 2, 1, 0, 8, 21, 1])
425
+ assert U == DenseMatrix(3, 3, [1, 3, 5, 0, -1, -4, 0, 0, 45])
426
+
427
+
428
+ def test_LDL():
429
+ A = DenseMatrix(3, 3, [4, 12, -16, 12, 37, -43, -16, -43, 98])
430
+
431
+ L, D = A.LDL()
432
+
433
+ assert L == DenseMatrix(3, 3, [1, 0, 0, 3, 1, 0, -4, 5, 1])
434
+ assert D == DenseMatrix(3, 3, [4, 0, 0, 0, 1, 0, 0, 0, 9])
435
+
436
+
437
+ def test_solve():
438
+ A = DenseMatrix(4, 4, [1, 2, 3, 4, 2, 2, 3, 4, 3, 3, 3, 4, 9, 8, 7, 6])
439
+ b = DenseMatrix(4, 1, [10, 11, 13, 30])
440
+ y = DenseMatrix(4, 1, [1, 1, 1, 1])
441
+
442
+ x = A.solve(b, 'LU')
443
+ assert x == y
444
+ x = A.solve(b, 'FFLU')
445
+ assert x == y
446
+ x = A.solve(b, 'FFGJ')
447
+ assert x == y
448
+
449
+
450
+ def test_FFLU():
451
+ A = DenseMatrix(4, 4, [1, 2, 3, 4, 2, 2, 3, 4, 3, 3, 3, 4, 9, 8, 7, 6])
452
+
453
+ L, U = A.FFLU()
454
+ assert L == DenseMatrix(4, 4, [1, 0, 0, 0, 2, -2, 0, -0, 3,
455
+ -3, 3, 0, 9, -10, 10, -10])
456
+ assert U == DenseMatrix(4, 4, [1, 2, 3, 4, 0, -2, -3, -4,
457
+ 0, 0, 3, 4, 0, 0, 0, -10])
458
+
459
+
460
+ def test_FFLDU():
461
+ A = DenseMatrix(3, 3, [1, 2, 3, 5, -3, 2, 6, 2, 1])
462
+ L, D, U = A.FFLDU()
463
+
464
+ assert L == DenseMatrix(3, 3, [1, 0, 0, 5, -13, 0, 6, -10, 1])
465
+ assert D == DenseMatrix(3, 3, [1, 0, 0, 0, -13, 0, 0, 0, -13])
466
+ assert U == DenseMatrix(3, 3, [1, 2, 3, 0, -13, -13, 0, 0, 91])
467
+
468
+
469
+ def test_QR():
470
+ A = DenseMatrix(3, 3, [12, -51, 4, 6, 167, -68, -4, 24, -41])
471
+ Q, R = A.QR()
472
+
473
+ assert Q == DenseMatrix(3, 3, [Rational(6, 7), Rational(-69, 175),
474
+ Rational(-58, 175), Rational(3, 7),
475
+ Rational(158, 175), Rational(6, 175),
476
+ Rational(-2, 7), Rational(6, 35),
477
+ Rational(-33, 35)])
478
+ assert R == DenseMatrix(3, 3, [14, 21, -14, 0, 175, -70, 0, 0, 35])
479
+
480
+
481
+ def test_cholesky():
482
+ A = DenseMatrix(3, 3, [4, 12, -16, 12, 37, -43, -16, -43, 98])
483
+ L = A.cholesky()
484
+
485
+ assert L == DenseMatrix(3, 3, [2, 0, 0, 6, 1, 0, -8, 5, 3])
486
+
487
+
488
+ def test_str_repr():
489
+ d = DenseMatrix(3, 2, [1, 2, 3, 4, 5, 6])
490
+ assert str(d) == '[1, 2]\n[3, 4]\n[5, 6]\n'
491
+ assert str(d) == repr(d)
492
+
493
+
494
+ def test_DenseMatrix_symbols():
495
+ x, y, z = symbols("x y z")
496
+ D = DenseMatrix(4, 4,
497
+ [1, 0, 1, 0,
498
+ 0, z, y, 0,
499
+ z, 1, x, 1,
500
+ 1, 1, 0, 0])
501
+ assert D.get(1, 2) == y
502
+
503
+
504
+ def test_jacobian():
505
+ x, y, z, t = symbols("x y z t")
506
+ J_correct = DenseMatrix(4, 4,
507
+ [1, 0, 1, 0,
508
+ 0, z, y, 0,
509
+ z, 1, x, 1,
510
+ 1, 1, 0, 0])
511
+ D = DenseMatrix(4, 1, [x+z, y*z, z*x+y+t, x+y])
512
+ x = DenseMatrix(4, 1, [x, y, z, t])
513
+ J = D.jacobian(x)
514
+ assert J == J_correct
515
+
516
+
517
+ def test_size():
518
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
519
+ assert A.size == 4
520
+
521
+
522
+ def test_shape():
523
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
524
+ assert A.shape == (2, 2)
525
+
526
+
527
+ def test_reshape():
528
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
529
+ B = DenseMatrix(4, 1, [1, 2, 3, 4])
530
+ C = A.reshape(4, 1)
531
+ assert C == B
532
+ assert C != A
533
+
534
+
535
+ @unittest.skipIf(not have_numpy, 'requires numpy')
536
+ def test_dump_real():
537
+ ref = [1, 2, 3, 4]
538
+ A = DenseMatrix(2, 2, ref)
539
+ out = np.empty(4)
540
+ A.dump_real(out)
541
+ assert np.allclose(out, ref)
542
+
543
+
544
+
545
+ @unittest.skipIf(not have_numpy, 'requires numpy')
546
+ def test_dump_complex():
547
+ ref = [1j, 2j, 3j, 4j]
548
+ A = DenseMatrix(2, 2, ref)
549
+ out = np.empty(4, dtype=np.complex128)
550
+ A.dump_complex(out)
551
+ assert np.allclose(out, ref)
552
+
553
+
554
+ def test_col_swap():
555
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
556
+ B = DenseMatrix(2, 2, [2, 1, 4, 3])
557
+ A.col_swap(0, 1)
558
+ assert A == B
559
+
560
+
561
+ def test_fill():
562
+ A = zeros(4, 4)
563
+ A.fill(1)
564
+ B = ones(4, 4)
565
+ assert A == B
566
+ assert A.rows == B.rows
567
+ assert A.cols == B.cols
568
+ assert A.shape == B.shape == (4, 4)
569
+
570
+
571
+ def test_row_swap():
572
+ A = DenseMatrix(2, 2, [1, 2, 3, 4])
573
+ B = DenseMatrix(2, 2, [3, 4, 1, 2])
574
+ A.row_swap(0, 1)
575
+ assert A == B
576
+
577
+
578
+ def test_row_col_del():
579
+ e = DenseMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
580
+ raises(IndexError, lambda: e.row_del(5))
581
+ raises(IndexError, lambda: e.row_del(-5))
582
+ raises(IndexError, lambda: e.col_del(5))
583
+ raises(IndexError, lambda: e.col_del(-5))
584
+
585
+ assert e.row_del(-1) == DenseMatrix([[1, 2, 3], [4, 5, 6]])
586
+ assert e.col_del(-1) == DenseMatrix([[1, 2], [4, 5]])
587
+
588
+ e = DenseMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
589
+ assert e.row_del(1) == DenseMatrix([[1, 2, 3], [7, 8, 9]])
590
+ assert e.col_del(1) == DenseMatrix([[1, 3], [7, 9]])
591
+
592
+
593
+ def test_row_join():
594
+ assert eye(3).row_join(DenseMatrix([7, 7, 7])) == \
595
+ DenseMatrix([[1, 0, 0, 7],
596
+ [0, 1, 0, 7],
597
+ [0, 0, 1, 7]])
598
+
599
+
600
+ def test_col_join():
601
+ assert eye(3).col_join(DenseMatrix([[7, 7, 7]])) == \
602
+ DenseMatrix([[1, 0, 0],
603
+ [0, 1, 0],
604
+ [0, 0, 1],
605
+ [7, 7, 7]])
606
+
607
+
608
+ def test_row_insert():
609
+ M = zeros(3)
610
+ V = ones(1, 3)
611
+ assert M.row_insert(1, V) == DenseMatrix([[0, 0, 0],
612
+ [1, 1, 1],
613
+ [0, 0, 0],
614
+ [0, 0, 0]])
615
+
616
+
617
+ def test_col_insert():
618
+ M = zeros(3)
619
+ V = ones(3, 1)
620
+ assert M.col_insert(1, V) == DenseMatrix([[0, 1, 0, 0],
621
+ [0, 1, 0, 0],
622
+ [0, 1, 0, 0]])
623
+
624
+
625
+ def test_rowmul():
626
+ M = ones(3)
627
+ assert M.rowmul(2, 2) == DenseMatrix([[1, 1, 1],
628
+ [1, 1, 1],
629
+ [2, 2, 2]])
630
+
631
+
632
+ def test_rowadd():
633
+ M = ones(3)
634
+ assert M.rowadd(2, 1, 1) == DenseMatrix([[1, 1, 1],
635
+ [1, 1, 1],
636
+ [2, 2, 2]])
637
+
638
+
639
+ def test_row_col():
640
+ m = DenseMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
641
+ assert m.row(0) == DenseMatrix(1, 3, [1, 2, 3])
642
+ assert m.col(0) == DenseMatrix(3, 1, [1, 4, 7])
643
+
644
+
645
+ def test_is_square():
646
+ m = DenseMatrix([[1],[1]])
647
+ m2 = DenseMatrix([[2, 2], [2, 2]])
648
+ assert not m.is_square
649
+ assert m2.is_square
650
+
651
+
652
+ def test_dot():
653
+ A = DenseMatrix(2, 3, [1, 2, 3, 4, 5, 6])
654
+ B = DenseMatrix(2, 1, [7, 8])
655
+ assert A.dot(B) == DenseMatrix(1, 3, [39, 54, 69])
656
+ assert ones(1, 3).dot(ones(3, 1)) == 3
657
+
658
+
659
+ def test_cross():
660
+ M = DenseMatrix(1, 3, [1, 2, 3])
661
+ V = DenseMatrix(1, 3, [3, 4, 5])
662
+ assert M.cross(V) == DenseMatrix(1, 3, [-2, 4, -2])
663
+ raises(ShapeError, lambda:
664
+ DenseMatrix(1, 2, [1, 1]).cross(DenseMatrix(1, 2, [1, 1])))
665
+
666
+
667
+ def test_diff():
668
+ x = symbols("x")
669
+ M = DenseMatrix(1, 2, [x**2, x])
670
+ result = M.diff(x)
671
+ assert isinstance(result, DenseMatrix)
672
+ assert result == DenseMatrix(1, 2, [2*x, 1])
673
+
674
+
675
+ def test_immutablematrix():
676
+ A = ImmutableMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
677
+ assert A.shape == (3, 3)
678
+ assert A[1, 2] == 6
679
+ assert A[2, 2] == 9
680
+
681
+ assert A[1, :] == ImmutableMatrix([[4, 5, 6]])
682
+ assert A[:2, :2] == ImmutableMatrix([[1, 2], [4, 5]])
683
+
684
+ with raises(TypeError):
685
+ A[2, 2] = 5
686
+
687
+ X = DenseMatrix([[1, 2], [3, 4]])
688
+ assert X.as_immutable() == ImmutableMatrix([[1, 2], [3, 4]])
689
+
690
+ assert X.det() == -2
691
+
692
+ X = ImmutableMatrix(eye(3))
693
+ assert isinstance(X + A, ImmutableMatrix)
694
+ assert isinstance(X * A, ImmutableMatrix)
695
+ assert isinstance(X * 2, ImmutableMatrix)
696
+ assert isinstance(2 * X, ImmutableMatrix)
697
+
698
+ X = ImmutableMatrix([[1, 2], [3, 4]])
699
+ Y = ImmutableMatrix([[1], [0]])
700
+ assert type(X.LUsolve(Y)) == ImmutableMatrix
701
+
702
+ x = Symbol("x")
703
+ X = ImmutableMatrix([[1, 2], [3, 4]])
704
+ Y = ImmutableMatrix([[1, 2], [x, 4]])
705
+ assert Y.subs(x, 3) == X
706
+ assert Y.xreplace(x, 3) == X
707
+
708
+ X = ImmutableMatrix([[1, 2], [3, 4]])
709
+ Y = ImmutableMatrix([[5], [6]])
710
+ Z = X.row_join(Y)
711
+ assert isinstance(Z, ImmutableMatrix)
712
+ assert Z == ImmutableMatrix([[1, 2, 5], [3, 4, 6]])
713
+
714
+ X = ImmutableMatrix([[1, 2], [3, 4]])
715
+ Y = ImmutableMatrix([[5, 6]])
716
+ Z = X.col_join(Y)
717
+ assert isinstance(Z, ImmutableMatrix)
718
+ assert Z == ImmutableMatrix([[1, 2], [3, 4], [5, 6]])
719
+
720
+ # Operations of one immutable and one mutable matrix should give immutable result
721
+ X = ImmutableMatrix([1])
722
+ Y = DenseMatrix([1])
723
+ assert type(X + Y) == ImmutableMatrix
724
+ assert type(Y + X) == ImmutableMatrix
725
+ assert type(X * Y) == ImmutableMatrix
726
+ assert type(Y * X) == ImmutableMatrix
727
+
728
+
729
+ def test_atoms():
730
+ a = Symbol("a")
731
+ b = Symbol("b")
732
+ X = DenseMatrix([[a, 2], [b, 4]])
733
+ assert X.atoms(Symbol) == {a, b}
734
+
735
+
736
+ def test_LUdecomp():
737
+ testmat = DenseMatrix([[0, 2, 5, 3],
738
+ [3, 3, 7, 4],
739
+ [8, 4, 0, 2],
740
+ [-2, 6, 3, 4]])
741
+ L, U, p = testmat.LUdecomposition()
742
+ res = L*U
743
+ for orig, new in p:
744
+ res.row_swap(orig, new)
745
+ assert res - testmat == zeros(4)
746
+
747
+ def test_repr_latex():
748
+ testmat = DenseMatrix([[0, 2]])
749
+ init_printing(True)
750
+ latex_string = testmat._repr_latex_()
751
+ assert isinstance(latex_string, str)
752
+ init_printing(False)
753
+
754
+ @unittest.skipIf(not have_sympy, "SymPy not installed")
755
+ def test_simplify():
756
+ A = ImmutableMatrix([1])
757
+ assert type(A.simplify()) == type(A)