rawmath 0.1.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.
rawmath/__init__.py ADDED
@@ -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,11 @@
1
+ rawmath/__init__.py,sha256=HoJU4qOJodpb38gSD_CCKkn08BMxpvWnn5pa0ib-4bA,56
2
+ rawmath/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ rawmath/models/matrix.py,sha256=2fF48x9gFVVIR-Be2nT1EQyACaKznYxVCHNfKOwKz9g,6423
4
+ rawmath/types/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ rawmath/types/functions.py,sha256=kCedGSTwvxZsFlRF_N7X0lm27IgKHnN-aukVWugExHs,35
6
+ rawmath/types/matrix.py,sha256=eGVX0nJtreZyj1Jelwku29WwfAlq7x6lm8ZJmeSV4qc,63
7
+ rawmath-0.1.0.dist-info/licenses/LICENSE,sha256=9nAXoaKMAQqfeLuFXnfTEjHG5XLoXECo_LSuLOOQ_Yo,1069
8
+ rawmath-0.1.0.dist-info/METADATA,sha256=NRQA3kjIRDlnIHya1hGxMWafndtGvEBlhFjhVIcP-Lc,1239
9
+ rawmath-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
10
+ rawmath-0.1.0.dist-info/top_level.txt,sha256=veJZGF0-BzbWifJX2y69XVkvuiBScc19Vos8NyYsQK0,8
11
+ rawmath-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -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.
@@ -0,0 +1 @@
1
+ rawmath