radioactiveshrimp 0.1.0__cp39-abi3-manylinux_2_28_x86_64.whl → 0.1.1__cp39-abi3-manylinux_2_28_x86_64.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.
- radioactiveshrimp/matrix/__init.py__ +5 -0
- radioactiveshrimp/matrix/elementary.py +201 -0
- {radioactiveshrimp-0.1.0.dist-info → radioactiveshrimp-0.1.1.dist-info}/METADATA +3 -1
- {radioactiveshrimp-0.1.0.dist-info → radioactiveshrimp-0.1.1.dist-info}/RECORD +5 -3
- {radioactiveshrimp-0.1.0.dist-info → radioactiveshrimp-0.1.1.dist-info}/WHEEL +1 -1
@@ -0,0 +1,201 @@
|
|
1
|
+
import torch
|
2
|
+
import math
|
3
|
+
|
4
|
+
def rowswap(M, idxSourceRow, idxTargetRow):
|
5
|
+
"""
|
6
|
+
Swap rows in a matrix.
|
7
|
+
|
8
|
+
Args:
|
9
|
+
M (matrix/pytorch tensor): Matrix of values
|
10
|
+
idxSourceRow (int): Index value of one of the rows to be swapped (indexing from 1)
|
11
|
+
idxTargetRow (int): Index value of one of the rows to be swapped (indexing from 1)
|
12
|
+
|
13
|
+
Returns:
|
14
|
+
mat: Matrix with the indicated rows swapped
|
15
|
+
|
16
|
+
Example:
|
17
|
+
>>> rowswap(torch.tensor([[1,2,3],[4,5,6]]), 1,2)
|
18
|
+
tensor([[4,5,6],[1,2,3]])
|
19
|
+
"""
|
20
|
+
# print("DOING ROW SWAP")
|
21
|
+
mat = M.clone()
|
22
|
+
# print('swap: ', idxSourceRow, idxTargetRow)
|
23
|
+
temptarget = M[idxTargetRow-1, :]
|
24
|
+
mat[idxTargetRow-1] = M[idxSourceRow-1]
|
25
|
+
mat[idxSourceRow-1] = temptarget
|
26
|
+
return mat
|
27
|
+
|
28
|
+
def rowscale(M, idxRow, scaleFactor):
|
29
|
+
"""
|
30
|
+
Scale one row of matrix by scalar value.
|
31
|
+
|
32
|
+
Args:
|
33
|
+
M (matrix/pytorch tensor): Matrix of values
|
34
|
+
idxRow (int): Index (starting from 1) of row to be scaled
|
35
|
+
scaleFactor (number int or float): Value to scale each element in the row by
|
36
|
+
|
37
|
+
Returns:
|
38
|
+
mat: Matrix with the indicated row scaled
|
39
|
+
|
40
|
+
Example:
|
41
|
+
>>> rowscale(torch.tensor([[1,2,3],[4,5,6]]), 1,5)
|
42
|
+
tensor([[5,10,15],[4,5,6]])
|
43
|
+
"""
|
44
|
+
# print('DOING ROW SCALE')
|
45
|
+
M[idxRow-1] = M[idxRow-1, :]*scaleFactor
|
46
|
+
return M
|
47
|
+
|
48
|
+
def rowreplacement(M, idxFirstRow, idxSecondRow, scaleJ, scaleK):
|
49
|
+
"""
|
50
|
+
Replace row in matrix by scaling and adding to other row.
|
51
|
+
|
52
|
+
Args:
|
53
|
+
M (matrix/pytorch tensor): Matrix of values
|
54
|
+
idxFirstRow (int): Index of the row to be updated
|
55
|
+
idxSecondRow (int): Index of the row to be added into the updated row
|
56
|
+
scaleJ (number int or float): Scale factor used for the firstRow elements
|
57
|
+
scaleK (number int or float): Scale factor used for the secondRow elements
|
58
|
+
|
59
|
+
Returns:
|
60
|
+
mat: Matrix with the indicated row scaled and summed with the scale of the second row
|
61
|
+
|
62
|
+
Example:
|
63
|
+
>>> rowreplacement(torch.tensor([[1,2,3],[4,5,6]]), 1,2,2,1)
|
64
|
+
tensor([[6,9,12],[4,5,6]])
|
65
|
+
"""
|
66
|
+
# print('DOING ROW REPLACE')
|
67
|
+
# print(M, idxFirstRow, idxSecondRow, scaleJ, scaleK)
|
68
|
+
mat = M.clone()
|
69
|
+
firstScaled = rowscale(mat, idxFirstRow, scaleJ)[idxFirstRow-1]
|
70
|
+
secondScaled = rowscale(mat, idxSecondRow, scaleK)[idxSecondRow-1]
|
71
|
+
firstScaled = firstScaled + secondScaled
|
72
|
+
M[idxFirstRow-1] = firstScaled
|
73
|
+
return M
|
74
|
+
|
75
|
+
def rref(M):
|
76
|
+
"""
|
77
|
+
Calculate reduced row echelon form of a matrix
|
78
|
+
|
79
|
+
Args:
|
80
|
+
M (matrix/pytorch tensor): Matrix of values
|
81
|
+
|
82
|
+
Returns:
|
83
|
+
mat: Matrix in rref
|
84
|
+
|
85
|
+
Example:
|
86
|
+
>>> rref(torch.tensor([[1,2,3],[4,5,6]]))
|
87
|
+
tensor([[ 1, 0, -1],[ 0, 1, 2]])
|
88
|
+
"""
|
89
|
+
mat = M.clone()
|
90
|
+
pivotFound = False
|
91
|
+
totalRows= mat.shape[0]
|
92
|
+
# print(totalRows)
|
93
|
+
manualRowidx = 0
|
94
|
+
for colidx in range(len(mat[0])):
|
95
|
+
if manualRowidx==totalRows:
|
96
|
+
break
|
97
|
+
for rowidx in range(manualRowidx, totalRows):
|
98
|
+
# print(rowidx, colidx)
|
99
|
+
# print(mat[rowidx, colidx].item())
|
100
|
+
if mat[rowidx, colidx].item() == 1 or mat[rowidx, colidx].item() == 1.0 :
|
101
|
+
if rowidx>manualRowidx:
|
102
|
+
mat = rowswap(mat, rowidx+1, manualRowidx+1)
|
103
|
+
# print(mat)
|
104
|
+
rowidx = manualRowidx
|
105
|
+
pivot = [rowidx,colidx]
|
106
|
+
# mat = reduceCol(mat, pivot)
|
107
|
+
pivotFound = True
|
108
|
+
break
|
109
|
+
if pivotFound == False: #no value in column = 1
|
110
|
+
if mat[manualRowidx, colidx].item()==0:
|
111
|
+
for rowidx in range(manualRowidx, totalRows):
|
112
|
+
if mat[rowidx,colidx].item() != 0:
|
113
|
+
mat = rowswap(mat, manualRowidx+1, rowidx+1)
|
114
|
+
# print(mat)
|
115
|
+
pivotFound=True
|
116
|
+
pivot = [manualRowidx, colidx]
|
117
|
+
break
|
118
|
+
else: #current pivot item not 0
|
119
|
+
pivotFound = True
|
120
|
+
pivot = [manualRowidx, colidx]
|
121
|
+
if pivotFound ==True:
|
122
|
+
mat = reduceCol(mat, pivot)
|
123
|
+
manualRowidx+=1
|
124
|
+
pivotFound = False
|
125
|
+
# print('*'*50)
|
126
|
+
# print(mat)
|
127
|
+
# print()
|
128
|
+
return mat
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
def reduceCol(M, pivot):
|
133
|
+
"""
|
134
|
+
Reduces the column of a matrix based on pivot value to acheive rref.
|
135
|
+
|
136
|
+
Args:
|
137
|
+
M (matrix/pytorch tensor): Matrix of values
|
138
|
+
pivot ([int, int]): List (len=2) of integer values representing the row, column index for the current pivot
|
139
|
+
|
140
|
+
Returns:
|
141
|
+
mat: Matrix with the column of focus reduced
|
142
|
+
|
143
|
+
Example:
|
144
|
+
>>> reduceCol(torch.tensor([[1,2],[3,4]]), [1,1])
|
145
|
+
tensor([[1,0],[1,0]])
|
146
|
+
"""
|
147
|
+
# print('doing reduction')
|
148
|
+
pivotRow = pivot[0]
|
149
|
+
pivotCol = pivot[1]
|
150
|
+
|
151
|
+
# if M[pivotRow,pivotCol].item() == 0:
|
152
|
+
# pivotCol+=1
|
153
|
+
|
154
|
+
if M[pivotRow, pivotCol].item() != 1:
|
155
|
+
try:
|
156
|
+
scale = 1/M[pivotRow, pivotCol].item()
|
157
|
+
# print(scale)
|
158
|
+
M = rowscale(M, pivotRow+1, scale)
|
159
|
+
except:
|
160
|
+
pass
|
161
|
+
|
162
|
+
|
163
|
+
for rowidx in range(len(M)):
|
164
|
+
if rowidx != pivotRow:
|
165
|
+
# print(M[rowidx, pivotCol].item())
|
166
|
+
if M[rowidx,pivotCol].item() ==0:
|
167
|
+
pass
|
168
|
+
else:
|
169
|
+
fractionCheck, _ = math.modf(M[rowidx, pivotCol].item())
|
170
|
+
if fractionCheck != 0: #check if its a fraction
|
171
|
+
scale = M[rowidx, pivotCol].item()
|
172
|
+
# print(scale)
|
173
|
+
M = rowreplacement(M, rowidx+1, pivotRow+1, 1, scale*-1)
|
174
|
+
else:
|
175
|
+
scale = M[rowidx, pivotCol].item()/1
|
176
|
+
# print(M, rowidx, pivotRow, 1, scale*-1)
|
177
|
+
M = rowreplacement(M, rowidx+1, pivotRow+1, 1, scale*-1)
|
178
|
+
return M
|
179
|
+
|
180
|
+
|
181
|
+
# Matrix = torch.tensor([[0,1,1],[1,1,6],[2,1,8]], dtype=torch.float16)
|
182
|
+
|
183
|
+
# print(rowswap(Matrix, 4,1))
|
184
|
+
# print(rowscale(Matrix, 1, 10))
|
185
|
+
# print(rowreplacement(Matrix, 1,6,16,-1))
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
# Matrix = torch.tensor([[1,3,0,0,3], [0,0,1,0,9], [0,0,0,1,-4]], dtype=torch.float16)
|
190
|
+
# print(Matrix)
|
191
|
+
# M = rowswap(Matrix, 1,2)
|
192
|
+
# print(M)
|
193
|
+
# M = rowscale(M, 1,(1/3))
|
194
|
+
# print(M)
|
195
|
+
# M = rowreplacement(M, 3, 1, 1, -3)
|
196
|
+
# print(M)
|
197
|
+
|
198
|
+
# Matrix = torch.tensor([[1,2,3],[4,5,6],[7,8,9]], dtype=torch.float16)
|
199
|
+
|
200
|
+
|
201
|
+
# print(rref(Matrix))
|
@@ -1,6 +1,8 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: radioactiveshrimp
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.1
|
4
|
+
Requires-Dist: numpy>=2.3.3
|
5
|
+
Requires-Dist: torch>=2.8.0
|
4
6
|
Requires-Dist: twine>=6.1.0
|
5
7
|
Summary: Add your description here
|
6
8
|
Author-email: radioactiveshrimp <radioactiveshrimpmail@gmail.com>
|
@@ -1,9 +1,11 @@
|
|
1
|
-
radioactiveshrimp-0.1.
|
2
|
-
radioactiveshrimp-0.1.
|
1
|
+
radioactiveshrimp-0.1.1.dist-info/METADATA,sha256=4miZ8FtVUImI28hoh1lyXSbo-IuHwLBUmaK838D378Y,378
|
2
|
+
radioactiveshrimp-0.1.1.dist-info/WHEEL,sha256=VCoUqQjnl5wExkC-2PG3I2s7-FBXYum-vf1jh8qiYg8,97
|
3
3
|
radioactiveshrimp/__init__.py,sha256=QY4ait-Lo2WQdyx7MU8N6a1v2ZYyAmYRqzYqdNbiavI,101
|
4
4
|
radioactiveshrimp/_core.abi3.so,sha256=3IMwsP7UrgNWdKdw2Pl11_VUQF8NqPy1xyyPuzkREdA,489672
|
5
5
|
radioactiveshrimp/_core.pyi,sha256=b6oJaUXUzEzqUE5rpqefV06hl8o_JCU8pgKgIIzQgmc,33
|
6
6
|
radioactiveshrimp/differential/__init.py__,sha256=jOhQKr6xM_7TVUt0GWRddtkgBvNn5I873WZcArT-GtE,101
|
7
7
|
radioactiveshrimp/differential/discrete.py,sha256=9-w6Avz4MZfu8gFhJU8kTvryn9oT2xS1TvvYQ_8pmPU,1371
|
8
|
+
radioactiveshrimp/matrix/__init.py__,sha256=EoerrXlfmd2lI9grb3jxqIhHn-bzLtYhRKyxI0RVgaE,166
|
9
|
+
radioactiveshrimp/matrix/elementary.py,sha256=zPiBG24F5zIncrrVWbiXEp1oFnHw2xNKy5quF0GBOuc,6333
|
8
10
|
radioactiveshrimp/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
9
|
-
radioactiveshrimp-0.1.
|
11
|
+
radioactiveshrimp-0.1.1.dist-info/RECORD,,
|