Skip to content

Commit 30a061f

Browse files
committed
Fix index mechanism
1 parent e100c42 commit 30a061f

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

LUFactorization.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
A = mats.Matrix(A)
1414
P, L, U = LUFactorization()(A)
1515

16+
print('The second column of A:\n', A[:, 2], sep='')
1617
print('\n', '*'*80, sep='')
1718
print('Execution Path:')
1819
result = A.forward(display=display_execution)
1920

2021
print('\n', '*'*80, sep='')
2122
print('The LU Factorization result:')
22-
print('P:\n', P)
23-
print('L:\n', L)
24-
print('U:\n', U)
23+
print('P:\n', P, sep='')
24+
print('L:\n', L, sep='')
25+
print('U:\n', U, sep='')
2526

2627
print('\n', '*'*80, sep='')
2728
print('Proof P * A = L * U:')
28-
print('P * A:\n', P * A)
29-
print('L * U:\n', L * U)
29+
print('P * A:\n', P * A, sep='')
30+
print('L * U:\n', L * U, sep='')

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ A = mats.Matrix(A) # get the matrix object from MatrixStitcher
2222
P, L, U = LUFactorization()(A) # apply LU Factorization on matrix A and get the factorization results
2323

2424
# show the execution state of the matrix A
25+
print('The second column of A:\n', A[:, 2], sep='')
2526
print('Execution Path:')
2627
result = A.forward(display=True)
2728
```
2829
Here is the execution result:
2930
```
31+
The second column of A:
32+
array([[ 2.],
33+
[ 8.],
34+
[ 3.]])
3035
Execution Path:
3136
-> Origin matrix:
3237
array([[ 1., 2., -3.],

matrixstitcher/backend.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,20 @@ def __repr__(self):
7474
return self.matrix.__repr__()
7575

7676
def __getitem__(self, key):
77-
key = index_mechanism(*key)
77+
if isinstance(key, (list, tuple)):
78+
key = index_mechanism(*key)
79+
else:
80+
key = index_mechanism(*[key])
7881
data = self.matrix.__getitem__(key)
7982
# if not isinstance(data, np.ndarray):
8083
# data = np.array(data).reshape(1)
8184
return Matrix(data, dtype=self.matrix.dtype)
8285

8386
def __setitem__(self, key, value):
84-
key = index_mechanism(*key)
87+
if isinstance(key, (list, tuple)):
88+
key = index_mechanism(*key)
89+
else:
90+
key = index_mechanism(*[key])
8591
self.matrix.__setitem__(key, value)
8692

8793
def __add__(self, other):
@@ -220,13 +226,24 @@ def numpy(self):
220226

221227

222228
def index_mechanism(*key):
223-
key = tuple(i - 1 if not isinstance(i, slice) else slice_mechanism(i) for i in key)
224-
return key
229+
new_key = []
230+
# key = tuple(i - 1 if not isinstance(i, slice) else slice_mechanism(i) for i in key)
231+
for i in key:
232+
if isinstance(i, slice):
233+
new_key.append(slice_mechanism(i))
234+
else:
235+
if i > 0:
236+
new_key.append(i - 1)
237+
elif i == 0:
238+
raise Exception('Index from 0 is not vaild')
239+
else:
240+
new_key.append(i)
241+
return tuple(new_key)
225242

226243

227244
def slice_mechanism(key: slice):
228-
start = key.start - 1 if key.start is not None else None
229-
stop = key.stop - 1 if key.stop is not None else None
245+
start = index_mechanism(*[key.start])[0] if key.start is not None else None
246+
stop = index_mechanism(*[key.stop])[0] if key.stop is not None else None
230247
step = key.step
231248
return slice(start, stop, step)
232249

0 commit comments

Comments
 (0)