rawmath 0.1.0__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.
rawmath-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 PeagazinhoAI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
rawmath-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,60 @@
1
+ Metadata-Version: 2.4
2
+ Name: rawmath
3
+ Version: 0.1.0
4
+ Summary: A from-scratch mathematics library for Python.
5
+ Author: PeagazinhoAI
6
+ License-Expression: MIT
7
+ Classifier: Development Status :: 2 - Pre-Alpha
8
+ Classifier: Intended Audience :: Developers
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: license-file
16
+
17
+ # RawMath
18
+
19
+ A from-scratch mathematics library for Python.
20
+
21
+ RawMath implements mathematical structures and operations directly from their
22
+ definitions, with a focus on clarity, independence, and understanding the
23
+ mathematics behind the implementation.
24
+
25
+ ## Requirements
26
+
27
+ - Python 3.10+
28
+
29
+ ## Installation
30
+
31
+ RawMath is currently under development.
32
+
33
+ For local development:
34
+
35
+ python -m pip install -e .
36
+
37
+ ## Quick Start
38
+
39
+ from rawmath import Matrix
40
+
41
+ A = Matrix([
42
+ [1, 2],
43
+ [3, 4],
44
+ ])
45
+
46
+ B = Matrix([
47
+ [5, 6],
48
+ [7, 8],
49
+ ])
50
+
51
+ print(A + B)
52
+
53
+ ## Status
54
+
55
+ RawMath is currently in pre-alpha development. The API may change between
56
+ versions.
57
+
58
+ ## License
59
+
60
+ RawMath is licensed under the MIT License.
@@ -0,0 +1,44 @@
1
+ # RawMath
2
+
3
+ A from-scratch mathematics library for Python.
4
+
5
+ RawMath implements mathematical structures and operations directly from their
6
+ definitions, with a focus on clarity, independence, and understanding the
7
+ mathematics behind the implementation.
8
+
9
+ ## Requirements
10
+
11
+ - Python 3.10+
12
+
13
+ ## Installation
14
+
15
+ RawMath is currently under development.
16
+
17
+ For local development:
18
+
19
+ python -m pip install -e .
20
+
21
+ ## Quick Start
22
+
23
+ from rawmath import Matrix
24
+
25
+ A = Matrix([
26
+ [1, 2],
27
+ [3, 4],
28
+ ])
29
+
30
+ B = Matrix([
31
+ [5, 6],
32
+ [7, 8],
33
+ ])
34
+
35
+ print(A + B)
36
+
37
+ ## Status
38
+
39
+ RawMath is currently in pre-alpha development. The API may change between
40
+ versions.
41
+
42
+ ## License
43
+
44
+ RawMath is licensed under the MIT License.
@@ -0,0 +1,28 @@
1
+ [build-system]
2
+ requires = ['setuptools>=77']
3
+ build-backend = 'setuptools.build_meta'
4
+
5
+ [project]
6
+ name = 'rawmath'
7
+ version = '0.1.0'
8
+ description = 'A from-scratch mathematics library for Python.'
9
+ readme = 'README.md'
10
+ requires-python = '>=3.10'
11
+ license = 'MIT'
12
+
13
+ authors = [
14
+ { name = 'PeagazinhoAI' }
15
+ ]
16
+
17
+ dependencies = []
18
+
19
+ classifiers = [
20
+ 'Development Status :: 2 - Pre-Alpha',
21
+ 'Intended Audience :: Developers',
22
+ 'Intended Audience :: Science/Research',
23
+ 'Programming Language :: Python :: 3',
24
+ 'Topic :: Scientific/Engineering :: Mathematics',
25
+ ]
26
+
27
+ [tool.setuptools.packages.find]
28
+ where = ['src']
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ from .models.matrix import Matrix
2
+
3
+ __all__ = ['Matrix']
File without changes
@@ -0,0 +1,251 @@
1
+ from __future__ import annotations
2
+ from typing import Any
3
+
4
+ from ..types.functions import OrderedPair
5
+ from ..types.matrix import MatrixData
6
+
7
+
8
+ class Matrix:
9
+ def __init__(
10
+ self,
11
+ data: MatrixData | OrderedPair,
12
+ edgeitems: int = 3,
13
+ threshold: int = 1000
14
+ ) -> None:
15
+ self.edgeitems = edgeitems
16
+ self.threshold = threshold
17
+
18
+ self._ignite(data=data)
19
+ self._class_name = type(self).__name__
20
+
21
+ def __str__(self) -> str:
22
+ return self._format_matrix()
23
+
24
+ def __repr__(self) -> str:
25
+ return self._format_matrix(debug=True)
26
+
27
+ def __setitem__(
28
+ self, key: tuple[int, int], value: Any
29
+ ) -> None:
30
+ i, j = key
31
+
32
+ self._matrix[i][j] = value
33
+
34
+ def __getitem__(self, key: int) -> list[Any]:
35
+ return self._matrix[key]
36
+
37
+ def __eq__(self, value: Matrix) -> bool:
38
+ return self._matrix == value._matrix
39
+
40
+ def __ne__(self, value: Matrix) -> bool:
41
+ return self._matrix != value._matrix
42
+
43
+ def __neg__(self) -> Matrix:
44
+ result = [
45
+ [-element for element in row] for row in self._matrix
46
+ ]
47
+
48
+ return Matrix(result)
49
+
50
+ def __add__(self, other: Matrix) -> Matrix:
51
+ if self.m != other.m or self.n != other.n:
52
+ raise ValueError('matrices must have the same dimensions')
53
+
54
+ result = [
55
+ [x + y for x, y in zip(row_a, row_b)]
56
+ for row_a, row_b in zip(self._matrix, other._matrix)
57
+ ]
58
+
59
+ return Matrix(result)
60
+
61
+ def __sub__(self, other: Matrix) -> Matrix:
62
+ return self + (-other)
63
+
64
+ def _ignite(self, data: MatrixData | OrderedPair) -> None:
65
+ if isinstance(data, tuple):
66
+ self.m, self.n = data
67
+
68
+ self._matrix = [
69
+ [0.0 for _ in range(self.n)]
70
+ for _ in range(self.m)
71
+ ]
72
+
73
+ return
74
+
75
+ rows_length = []
76
+ for row in data:
77
+ for element in row:
78
+ if not isinstance(element, (int, float)):
79
+ raise TypeError('matrix elements must be numeric')
80
+
81
+ rows_length.append(len(row))
82
+
83
+ if len(set(rows_length)) > 1:
84
+ raise ValueError('all matrix rows must have the same length')
85
+
86
+ if len(rows_length) == 0 or rows_length[0] == 0:
87
+ raise ValueError('matrix data cannot be empty')
88
+
89
+ self._matrix = data
90
+ self.m = len(self._matrix)
91
+ self.n = rows_length[0]
92
+
93
+ def _format_matrix(self, debug: bool = False) -> str:
94
+ name = self._class_name
95
+ padding = '\n' + ' '*(len(name) + 1)
96
+
97
+ summarize = self.elements >= self.threshold
98
+ rows = self._matrix
99
+
100
+ if summarize:
101
+ rows = (
102
+ rows[:self.edgeitems] + rows[-self.edgeitems:]
103
+ )
104
+
105
+ visible_rows = []
106
+ for row in rows:
107
+ if summarize:
108
+ row = (
109
+ row[:self.edgeitems] + ['...'] + row[-self.edgeitems:]
110
+ )
111
+
112
+ visible_rows.append(row)
113
+
114
+ widths = [0]*len(visible_rows[0])
115
+ for row in visible_rows:
116
+ for column, value in enumerate(row):
117
+ value_width = len(str(value))
118
+
119
+ if value_width > widths[column]:
120
+ widths[column] = value_width
121
+
122
+ formatted_rows = []
123
+ for index, row in enumerate(visible_rows):
124
+ formatted_values = []
125
+
126
+ for column, value in enumerate(row):
127
+ formatted_values.append(
128
+ f'{value:>{widths[column]}}'
129
+ )
130
+
131
+ clean_row = ' '.join(formatted_values)
132
+ clean_row = '[ ' + clean_row + ' ]'
133
+
134
+ formatted_rows.append(clean_row)
135
+
136
+ if summarize and index == self.edgeitems - 1:
137
+ formatted_rows.append('...'.center(len(clean_row)))
138
+
139
+ final_str = padding.join(formatted_rows)
140
+ shape = f', shape={self.shape}' if debug else ''
141
+
142
+ return f'{name}({final_str}{shape})'
143
+
144
+ @property
145
+ def shape(self) -> OrderedPair:
146
+ return self.m, self.n
147
+
148
+ @property
149
+ def elements(self) -> int:
150
+ return self.m*self.n
151
+
152
+ @property
153
+ def is_square(self) -> bool:
154
+ return self.shape[0] == self.shape[1]
155
+
156
+ @property
157
+ def order(self) -> int | None:
158
+ return self.shape[0] if self.is_square else None
159
+
160
+ @property
161
+ def main_diagonal(self) -> list[Any] | None:
162
+ result = None
163
+
164
+ if self.is_square:
165
+ result = [
166
+ self._matrix[n][n] for n in range(self.order)
167
+ ]
168
+
169
+ return result
170
+
171
+ @property
172
+ def anti_diagonal(self) -> list[Any] | None:
173
+ result = None
174
+
175
+ if self.is_square:
176
+ order = self.order
177
+
178
+ result = [
179
+ self._matrix[order - n][n - 1]
180
+ for n in range(order, 0, -1)
181
+ ]
182
+
183
+ return result
184
+
185
+ @property
186
+ def secondary_diagonal(self) -> list[Any] | None:
187
+ return self.anti_diagonal
188
+
189
+ @property
190
+ def counter_diagonal(self) -> list[Any] | None:
191
+ return self.anti_diagonal
192
+
193
+ @property
194
+ def reverse_diagonal(self) -> list[Any] | None:
195
+ return self.anti_diagonal
196
+
197
+ @property
198
+ def is_diagonal(self) -> bool:
199
+ result = False
200
+
201
+ if self.is_square:
202
+ _sum = 0
203
+
204
+ for i_index, row in enumerate(self._matrix):
205
+ for j_index, element in enumerate(row):
206
+ if i_index != j_index:
207
+ _sum += abs(element)
208
+
209
+ result = not bool(_sum)
210
+
211
+ return result
212
+
213
+ @property
214
+ def is_identity(self) -> bool:
215
+ result = False
216
+
217
+ if self.is_square and self.is_diagonal:
218
+ _count = 0
219
+
220
+ for element in self.main_diagonal:
221
+ if element == 1:
222
+ _count += 1
223
+
224
+ result = _count == self.order
225
+
226
+ return result
227
+
228
+ @property
229
+ def is_unit(self) -> bool:
230
+ return self.is_identity
231
+
232
+ @property
233
+ def is_row(self) -> bool:
234
+ return self.shape[0] == 1
235
+
236
+ @property
237
+ def is_column(self) -> bool:
238
+ return self.shape[1] == 1
239
+
240
+ @property
241
+ def is_null(self) -> bool:
242
+ _sum = 0
243
+
244
+ for row in self._matrix:
245
+ for element in row:
246
+ _sum += abs(element)
247
+
248
+ return not bool(_sum)
249
+
250
+ def is_opposite(self, target: Matrix) -> bool:
251
+ return (self + target).is_null
File without changes
@@ -0,0 +1 @@
1
+ type OrderedPair = tuple[int, int]
@@ -0,0 +1,3 @@
1
+ from typing import Any
2
+
3
+ type MatrixData = list[list[Any, Any]]
@@ -0,0 +1,60 @@
1
+ Metadata-Version: 2.4
2
+ Name: rawmath
3
+ Version: 0.1.0
4
+ Summary: A from-scratch mathematics library for Python.
5
+ Author: PeagazinhoAI
6
+ License-Expression: MIT
7
+ Classifier: Development Status :: 2 - Pre-Alpha
8
+ Classifier: Intended Audience :: Developers
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: license-file
16
+
17
+ # RawMath
18
+
19
+ A from-scratch mathematics library for Python.
20
+
21
+ RawMath implements mathematical structures and operations directly from their
22
+ definitions, with a focus on clarity, independence, and understanding the
23
+ mathematics behind the implementation.
24
+
25
+ ## Requirements
26
+
27
+ - Python 3.10+
28
+
29
+ ## Installation
30
+
31
+ RawMath is currently under development.
32
+
33
+ For local development:
34
+
35
+ python -m pip install -e .
36
+
37
+ ## Quick Start
38
+
39
+ from rawmath import Matrix
40
+
41
+ A = Matrix([
42
+ [1, 2],
43
+ [3, 4],
44
+ ])
45
+
46
+ B = Matrix([
47
+ [5, 6],
48
+ [7, 8],
49
+ ])
50
+
51
+ print(A + B)
52
+
53
+ ## Status
54
+
55
+ RawMath is currently in pre-alpha development. The API may change between
56
+ versions.
57
+
58
+ ## License
59
+
60
+ RawMath is licensed under the MIT License.
@@ -0,0 +1,14 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/rawmath/__init__.py
5
+ src/rawmath.egg-info/PKG-INFO
6
+ src/rawmath.egg-info/SOURCES.txt
7
+ src/rawmath.egg-info/dependency_links.txt
8
+ src/rawmath.egg-info/top_level.txt
9
+ src/rawmath/models/__init__.py
10
+ src/rawmath/models/matrix.py
11
+ src/rawmath/types/__init__.py
12
+ src/rawmath/types/functions.py
13
+ src/rawmath/types/matrix.py
14
+ tests/test_matrix.py
@@ -0,0 +1 @@
1
+ rawmath
@@ -0,0 +1,765 @@
1
+ import pytest
2
+
3
+ from rawmath import Matrix
4
+
5
+
6
+ def test_create_matrix_shape() -> None:
7
+ matrix = Matrix([
8
+ [1, 2],
9
+ [3, 4],
10
+ ])
11
+
12
+ assert matrix.shape == (2, 2)
13
+
14
+
15
+ def test_create_matrix_elements() -> None:
16
+ matrix = Matrix([
17
+ [1, 2],
18
+ [3, 4],
19
+ ])
20
+
21
+ assert matrix.elements == 4
22
+
23
+
24
+ def test_create_null_matrix_from_dimensions_shape() -> None:
25
+ matrix = Matrix((2, 3))
26
+
27
+ assert matrix.shape == (2, 3)
28
+
29
+
30
+ def test_create_null_matrix_from_dimensions_is_null() -> None:
31
+ matrix = Matrix((2, 3))
32
+
33
+ assert matrix.is_null
34
+
35
+
36
+ def test_reject_non_numeric_element() -> None:
37
+ with pytest.raises(TypeError):
38
+ Matrix([
39
+ [1, 2],
40
+ [3, 'hello'],
41
+ ])
42
+
43
+
44
+ def test_reject_irregular_rows() -> None:
45
+ with pytest.raises(ValueError):
46
+ Matrix([
47
+ [1, 2],
48
+ [3],
49
+ ])
50
+
51
+
52
+ def test_reject_empty_matrix() -> None:
53
+ with pytest.raises(ValueError):
54
+ Matrix([])
55
+
56
+
57
+ def test_reject_single_empty_row() -> None:
58
+ with pytest.raises(ValueError):
59
+ Matrix([
60
+ [],
61
+ ])
62
+
63
+
64
+ def test_reject_multiple_empty_rows() -> None:
65
+ with pytest.raises(ValueError):
66
+ Matrix([
67
+ [],
68
+ [],
69
+ ])
70
+
71
+
72
+ def test_shape_rectangular_matrix() -> None:
73
+ matrix = Matrix([
74
+ [1, 2, 3],
75
+ [4, 5, 6],
76
+ ])
77
+
78
+ assert matrix.shape == (2, 3)
79
+
80
+
81
+ def test_elements_rectangular_matrix() -> None:
82
+ matrix = Matrix([
83
+ [1, 2, 3],
84
+ [4, 5, 6],
85
+ ])
86
+
87
+ assert matrix.elements == 6
88
+
89
+
90
+ def test_square_matrix() -> None:
91
+ matrix = Matrix([
92
+ [1, 2],
93
+ [3, 4],
94
+ ])
95
+
96
+ assert matrix.is_square
97
+
98
+
99
+ def test_non_square_matrix() -> None:
100
+ matrix = Matrix([
101
+ [1, 2, 3],
102
+ [4, 5, 6],
103
+ ])
104
+
105
+ assert not matrix.is_square
106
+
107
+
108
+ def test_square_matrix_order() -> None:
109
+ matrix = Matrix([
110
+ [1, 2],
111
+ [3, 4],
112
+ ])
113
+
114
+ assert matrix.order == 2
115
+
116
+
117
+ def test_non_square_matrix_order() -> None:
118
+ matrix = Matrix([
119
+ [1, 2, 3],
120
+ [4, 5, 6],
121
+ ])
122
+
123
+ assert matrix.order is None
124
+
125
+
126
+ def test_row_matrix() -> None:
127
+ matrix = Matrix([
128
+ [1, 2, 3],
129
+ ])
130
+
131
+ assert matrix.is_row
132
+
133
+
134
+ def test_non_row_matrix() -> None:
135
+ matrix = Matrix([
136
+ [1, 2],
137
+ [3, 4],
138
+ ])
139
+
140
+ assert not matrix.is_row
141
+
142
+
143
+ def test_column_matrix() -> None:
144
+ matrix = Matrix([
145
+ [1],
146
+ [2],
147
+ [3],
148
+ ])
149
+
150
+ assert matrix.is_column
151
+
152
+
153
+ def test_non_column_matrix() -> None:
154
+ matrix = Matrix([
155
+ [1, 2],
156
+ [3, 4],
157
+ ])
158
+
159
+ assert not matrix.is_column
160
+
161
+
162
+ def test_one_by_one_is_row() -> None:
163
+ matrix = Matrix([
164
+ [7],
165
+ ])
166
+
167
+ assert matrix.is_row
168
+
169
+
170
+ def test_one_by_one_is_column() -> None:
171
+ matrix = Matrix([
172
+ [7],
173
+ ])
174
+
175
+ assert matrix.is_column
176
+
177
+
178
+ def test_one_by_one_is_square() -> None:
179
+ matrix = Matrix([
180
+ [7],
181
+ ])
182
+
183
+ assert matrix.is_square
184
+
185
+
186
+ def test_one_by_one_order() -> None:
187
+ matrix = Matrix([
188
+ [7],
189
+ ])
190
+
191
+ assert matrix.order == 1
192
+
193
+
194
+ def test_main_diagonal() -> None:
195
+ matrix = Matrix([
196
+ [1, 2, 3],
197
+ [4, 5, 6],
198
+ [7, 8, 9],
199
+ ])
200
+
201
+ assert matrix.main_diagonal == [1, 5, 9]
202
+
203
+
204
+ def test_main_diagonal_one_by_one() -> None:
205
+ matrix = Matrix([
206
+ [7],
207
+ ])
208
+
209
+ assert matrix.main_diagonal == [7]
210
+
211
+
212
+ def test_main_diagonal_non_square() -> None:
213
+ matrix = Matrix([
214
+ [1, 2, 3],
215
+ [4, 5, 6],
216
+ ])
217
+
218
+ assert matrix.main_diagonal is None
219
+
220
+
221
+ def test_anti_diagonal() -> None:
222
+ matrix = Matrix([
223
+ [1, 2, 3],
224
+ [4, 5, 6],
225
+ [7, 8, 9],
226
+ ])
227
+
228
+ assert matrix.anti_diagonal == [3, 5, 7]
229
+
230
+
231
+ def test_anti_diagonal_one_by_one() -> None:
232
+ matrix = Matrix([
233
+ [7],
234
+ ])
235
+
236
+ assert matrix.anti_diagonal == [7]
237
+
238
+
239
+ def test_anti_diagonal_non_square() -> None:
240
+ matrix = Matrix([
241
+ [1, 2, 3],
242
+ [4, 5, 6],
243
+ ])
244
+
245
+ assert matrix.anti_diagonal is None
246
+
247
+
248
+ def test_secondary_diagonal_alias() -> None:
249
+ matrix = Matrix([
250
+ [1, 2],
251
+ [3, 4],
252
+ ])
253
+
254
+ assert matrix.secondary_diagonal == matrix.anti_diagonal
255
+
256
+
257
+ def test_counter_diagonal_alias() -> None:
258
+ matrix = Matrix([
259
+ [1, 2],
260
+ [3, 4],
261
+ ])
262
+
263
+ assert matrix.counter_diagonal == matrix.anti_diagonal
264
+
265
+
266
+ def test_reverse_diagonal_alias() -> None:
267
+ matrix = Matrix([
268
+ [1, 2],
269
+ [3, 4],
270
+ ])
271
+
272
+ assert matrix.reverse_diagonal == matrix.anti_diagonal
273
+
274
+
275
+ def test_diagonal_matrix() -> None:
276
+ matrix = Matrix([
277
+ [2, 0, 0],
278
+ [0, 5, 0],
279
+ [0, 0, -3],
280
+ ])
281
+
282
+ assert matrix.is_diagonal
283
+
284
+
285
+ def test_zero_diagonal_matrix() -> None:
286
+ matrix = Matrix([
287
+ [0, 0],
288
+ [0, 0],
289
+ ])
290
+
291
+ assert matrix.is_diagonal
292
+
293
+
294
+ def test_non_diagonal_matrix() -> None:
295
+ matrix = Matrix([
296
+ [1, 2],
297
+ [0, 1],
298
+ ])
299
+
300
+ assert not matrix.is_diagonal
301
+
302
+
303
+ def test_non_square_is_not_diagonal() -> None:
304
+ matrix = Matrix([
305
+ [1, 0, 0],
306
+ [0, 1, 0],
307
+ ])
308
+
309
+ assert not matrix.is_diagonal
310
+
311
+
312
+ def test_identity_matrix() -> None:
313
+ matrix = Matrix([
314
+ [1, 0, 0],
315
+ [0, 1, 0],
316
+ [0, 0, 1],
317
+ ])
318
+
319
+ assert matrix.is_identity
320
+
321
+
322
+ def test_non_identity_diagonal_matrix() -> None:
323
+ matrix = Matrix([
324
+ [2, 0],
325
+ [0, 2],
326
+ ])
327
+
328
+ assert not matrix.is_identity
329
+
330
+
331
+ def test_non_diagonal_is_not_identity() -> None:
332
+ matrix = Matrix([
333
+ [1, 1],
334
+ [0, 1],
335
+ ])
336
+
337
+ assert not matrix.is_identity
338
+
339
+
340
+ def test_non_square_is_not_identity() -> None:
341
+ matrix = Matrix([
342
+ [1, 0, 0],
343
+ [0, 1, 0],
344
+ ])
345
+
346
+ assert not matrix.is_identity
347
+
348
+
349
+ def test_one_by_one_identity() -> None:
350
+ matrix = Matrix([
351
+ [1],
352
+ ])
353
+
354
+ assert matrix.is_identity
355
+
356
+
357
+ def test_unit_alias() -> None:
358
+ matrix = Matrix([
359
+ [1, 0],
360
+ [0, 1],
361
+ ])
362
+
363
+ assert matrix.is_unit
364
+
365
+
366
+ def test_null_matrix() -> None:
367
+ matrix = Matrix([
368
+ [0, 0],
369
+ [0, 0],
370
+ ])
371
+
372
+ assert matrix.is_null
373
+
374
+
375
+ def test_non_null_matrix() -> None:
376
+ matrix = Matrix([
377
+ [0, 0],
378
+ [0, 1],
379
+ ])
380
+
381
+ assert not matrix.is_null
382
+
383
+
384
+ def test_negative_matrix_is_not_null() -> None:
385
+ matrix = Matrix([
386
+ [0, -1],
387
+ [0, 0],
388
+ ])
389
+
390
+ assert not matrix.is_null
391
+
392
+
393
+ def test_get_first_row() -> None:
394
+ matrix = Matrix([
395
+ [1, 2],
396
+ [3, 4],
397
+ ])
398
+
399
+ assert matrix[0] == [1, 2]
400
+
401
+
402
+ def test_get_second_row() -> None:
403
+ matrix = Matrix([
404
+ [1, 2],
405
+ [3, 4],
406
+ ])
407
+
408
+ assert matrix[1] == [3, 4]
409
+
410
+
411
+ def test_setitem() -> None:
412
+ matrix = Matrix([
413
+ [1, 2],
414
+ [3, 4],
415
+ ])
416
+
417
+ matrix[0, 1] = 10
418
+
419
+ assert matrix[0][1] == 10
420
+
421
+
422
+ def test_setitem_negative_value() -> None:
423
+ matrix = Matrix([
424
+ [1, 2],
425
+ [3, 4],
426
+ ])
427
+
428
+ matrix[1, 0] = -20
429
+
430
+ assert matrix[1][0] == -20
431
+
432
+
433
+ def test_setitem_float() -> None:
434
+ matrix = Matrix([
435
+ [1, 2],
436
+ [3, 4],
437
+ ])
438
+
439
+ matrix[0, 0] = 1.5
440
+
441
+ assert matrix[0][0] == 1.5
442
+
443
+
444
+ def test_equal_matrices() -> None:
445
+ a = Matrix([
446
+ [1, 2],
447
+ [3, 4],
448
+ ])
449
+
450
+ b = Matrix([
451
+ [1, 2],
452
+ [3, 4],
453
+ ])
454
+
455
+ assert a == b
456
+
457
+
458
+ def test_different_matrices_are_not_equal() -> None:
459
+ a = Matrix([
460
+ [1, 2],
461
+ [3, 4],
462
+ ])
463
+
464
+ b = Matrix([
465
+ [1, 2],
466
+ [3, 5],
467
+ ])
468
+
469
+ assert not (a == b)
470
+
471
+
472
+ def test_different_matrices() -> None:
473
+ a = Matrix([
474
+ [1, 2],
475
+ [3, 4],
476
+ ])
477
+
478
+ b = Matrix([
479
+ [1, 2],
480
+ [3, 5],
481
+ ])
482
+
483
+ assert a != b
484
+
485
+
486
+ def test_equal_matrices_are_not_different() -> None:
487
+ a = Matrix([
488
+ [1, 2],
489
+ [3, 4],
490
+ ])
491
+
492
+ b = Matrix([
493
+ [1, 2],
494
+ [3, 4],
495
+ ])
496
+
497
+ assert not (a != b)
498
+
499
+
500
+ def test_negative_matrix() -> None:
501
+ matrix = Matrix([
502
+ [1, -2],
503
+ [3, 4],
504
+ ])
505
+
506
+ assert -matrix == Matrix([
507
+ [-1, 2],
508
+ [-3, -4],
509
+ ])
510
+
511
+
512
+ def test_double_negative_matrix() -> None:
513
+ matrix = Matrix([
514
+ [1, -2],
515
+ [3, 4],
516
+ ])
517
+
518
+ assert -(-matrix) == matrix
519
+
520
+
521
+ def test_negative_null_matrix() -> None:
522
+ matrix = Matrix([
523
+ [0, 0],
524
+ [0, 0],
525
+ ])
526
+
527
+ assert -matrix == matrix
528
+
529
+
530
+ def test_opposite_matrices() -> None:
531
+ a = Matrix([
532
+ [1, -2],
533
+ [3, 4],
534
+ ])
535
+
536
+ b = Matrix([
537
+ [-1, 2],
538
+ [-3, -4],
539
+ ])
540
+
541
+ assert a.is_opposite(b)
542
+
543
+
544
+ def test_non_opposite_matrices() -> None:
545
+ a = Matrix([
546
+ [1, 2],
547
+ [3, 4],
548
+ ])
549
+
550
+ b = Matrix([
551
+ [-1, -2],
552
+ [-3, -5],
553
+ ])
554
+
555
+ assert not a.is_opposite(b)
556
+
557
+
558
+ def test_null_matrix_is_opposite_to_itself() -> None:
559
+ matrix = Matrix([
560
+ [0, 0],
561
+ [0, 0],
562
+ ])
563
+
564
+ assert matrix.is_opposite(matrix)
565
+
566
+
567
+ def test_matrix_addition() -> None:
568
+ a = Matrix([
569
+ [1, 2],
570
+ [3, 4],
571
+ ])
572
+
573
+ b = Matrix([
574
+ [5, 6],
575
+ [7, 8],
576
+ ])
577
+
578
+ assert a + b == Matrix([
579
+ [6, 8],
580
+ [10, 12],
581
+ ])
582
+
583
+
584
+ def test_matrix_addition_with_negative_values() -> None:
585
+ a = Matrix([
586
+ [-1, 2],
587
+ [3, -4],
588
+ ])
589
+
590
+ b = Matrix([
591
+ [5, -6],
592
+ [-7, 8],
593
+ ])
594
+
595
+ assert a + b == Matrix([
596
+ [4, -4],
597
+ [-4, 4],
598
+ ])
599
+
600
+
601
+ def test_matrix_addition_with_null_matrix() -> None:
602
+ a = Matrix([
603
+ [1, 2],
604
+ [3, 4],
605
+ ])
606
+
607
+ null = Matrix((2, 2))
608
+
609
+ assert a + null == a
610
+
611
+
612
+ def test_matrix_addition_commutative() -> None:
613
+ a = Matrix([
614
+ [1, 2],
615
+ [3, 4],
616
+ ])
617
+
618
+ b = Matrix([
619
+ [5, 6],
620
+ [7, 8],
621
+ ])
622
+
623
+ assert a + b == b + a
624
+
625
+
626
+ def test_addition_different_row_count() -> None:
627
+ a = Matrix([
628
+ [1, 2],
629
+ ])
630
+
631
+ b = Matrix([
632
+ [1, 2],
633
+ [3, 4],
634
+ ])
635
+
636
+ with pytest.raises(ValueError):
637
+ a + b
638
+
639
+
640
+ def test_addition_different_column_count() -> None:
641
+ a = Matrix([
642
+ [1, 2],
643
+ [3, 4],
644
+ ])
645
+
646
+ b = Matrix([
647
+ [1, 2, 3],
648
+ [4, 5, 6],
649
+ ])
650
+
651
+ with pytest.raises(ValueError):
652
+ a + b
653
+
654
+
655
+ def test_matrix_subtraction() -> None:
656
+ a = Matrix([
657
+ [10, -4, 7],
658
+ [3, 8, -2],
659
+ ])
660
+
661
+ b = Matrix([
662
+ [6, 2, -3],
663
+ [-5, 1, 4],
664
+ ])
665
+
666
+ assert a - b == Matrix([
667
+ [4, -6, 10],
668
+ [8, 7, -6],
669
+ ])
670
+
671
+
672
+ def test_matrix_subtraction_from_itself() -> None:
673
+ matrix = Matrix([
674
+ [1, 2],
675
+ [3, 4],
676
+ ])
677
+
678
+ assert (matrix - matrix).is_null
679
+
680
+
681
+ def test_matrix_subtraction_null_matrix() -> None:
682
+ matrix = Matrix([
683
+ [1, 2],
684
+ [3, 4],
685
+ ])
686
+
687
+ null = Matrix((2, 2))
688
+
689
+ assert matrix - null == matrix
690
+
691
+
692
+ def test_subtraction_different_row_count() -> None:
693
+ a = Matrix([
694
+ [1, 2],
695
+ ])
696
+
697
+ b = Matrix([
698
+ [1, 2],
699
+ [3, 4],
700
+ ])
701
+
702
+ with pytest.raises(ValueError):
703
+ a - b
704
+
705
+
706
+ def test_subtraction_different_column_count() -> None:
707
+ a = Matrix([
708
+ [1, 2],
709
+ [3, 4],
710
+ ])
711
+
712
+ b = Matrix([
713
+ [1, 2, 3],
714
+ [4, 5, 6],
715
+ ])
716
+
717
+ with pytest.raises(ValueError):
718
+ a - b
719
+
720
+
721
+ def test_str_matrix() -> None:
722
+ matrix = Matrix([
723
+ [1, 2],
724
+ [3, 4],
725
+ ])
726
+
727
+ assert str(matrix) == (
728
+ 'Matrix([ 1 2 ]\n'
729
+ ' [ 3 4 ])'
730
+ )
731
+
732
+
733
+ def test_repr_contains_shape() -> None:
734
+ matrix = Matrix([
735
+ [1, 2],
736
+ [3, 4],
737
+ ])
738
+
739
+ assert 'shape=(2, 2)' in repr(matrix)
740
+
741
+
742
+ def test_str_does_not_contain_shape() -> None:
743
+ matrix = Matrix([
744
+ [1, 2],
745
+ [3, 4],
746
+ ])
747
+
748
+ assert 'shape=' not in str(matrix)
749
+
750
+
751
+ def test_large_matrix_is_summarized() -> None:
752
+ matrix = Matrix(
753
+ [[0 for _ in range(40)] for _ in range(40)]
754
+ )
755
+
756
+ assert '...' in str(matrix)
757
+
758
+
759
+ def test_small_matrix_is_not_summarized() -> None:
760
+ matrix = Matrix([
761
+ [1, 2],
762
+ [3, 4],
763
+ ])
764
+
765
+ assert '...' not in str(matrix)