TypeError: cannot unpack non-iterable int object Python
I have a code for MAtrix in Python 3:
def __getitem__(self, row_col):
row, col = row_col
if self.rows > row and self.cols > row:
return self.m[row][col]
else:
raise Exception('Matrix error: getitem problem')
def __setitem__(self, row_col, val):
row, col = row_col
if self.rows > row and self.cols > row:
self.m[row][col] = val
else:
raise Exception('Matrix error: setitem problem')
when I'm trying to run there is an error:
Traceback (most recent call last):
File "C:/Users/buncy/PycharmProjects/pythonProject2/MatrixT.py", line 313, in <module>
m = m1 * m2
File "C:/Users/buncy/PycharmProjects/pythonProject2/MatrixT.py", line 165, in __mul__
r.m[i][j] += self.m[i][k] + right[k][i]
File "C:/Users/buncy/PycharmProjects/pythonProject2/MatrixT.py", line 107, in __getitem__
row, col = row_col
TypeError: cannot unpack non-iterable int object
Process finished with exit code 1
all my code: class Vector3D:
def __init__(self, x=0, y=0, z=0):
self.z = z
self.y = y
self.x = x
# vector length |V|=sqrt(x*x+y*y+z*z)
def magnitude(self):
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
# define vector as a string
def __str__(self):
return '[%s, %s, %s]' % (self.x, self.y, self.z)
# operation on vectors: add
def __add__(self, v):
if isinstance(v, Vector3D):
return Vector3D(self.x + v.x, self.y + v.y, self.z + v.z)
else:
raise Exception('Vector 3D: error, add not with vector')
def __sub__(self, v):
if isinstance(v, Vector3D):
return Vector3D(self.x - v.x, self.y - v.y, self.z - v.z)
else:
raise Exception('Vector 3D: error, sub not with vector')
def __neg__(self):
return Vector3D(-self.x, -self.y, -self.z)
def __mul__(self, val):
if isinstance(val, int) or isinstance(val, float):
return Vector3D(self.x * val.x, self.y * val.y, self.z * val.z)
elif isinstance(val, ValueError):
return Vector3D(self.x * val.x, self.y * val.y, self.z * val.z)
else:
raise Exception('Error: value is not a number')
def __div__(self, val):
if isinstance(val, int) or isinstance(val, float):
return Vector3D(self.x / val, self.y / val, self.z / val)
elif isinstance(val, Vector3D):
if (val.x and val.y and val.z):
return Vector3D(self.x / val.x, self.y / val.y, self.z / val.z)
else:
raise Exception('Error: division by 0')
else:
raise Exception('Error: Vector has no right values')
def normalize(self):
mg = self.magnitude()
if (mg > 0.0):
Vector3D.__mul__(self, 1 / mg)
self.x /= mg
self.y /= mg
self.z /= mg
else:
raise Exception('Error: zero Vector cannot be normalize')
# definition dot product as sum of list comprehension doing element-wise multiplication
def dot(self, v):
if isinstance(v, Vector3D):
return self.x * v.x + self.y * v.y + self.z * v.z
else:
return Exception('Error: This is not a Vector')
class Matrix:
# Matrix 2 x 2:
def __init__(self, rows, cols, identity=True):
if rows < 2 and cols < 2:
raise Exception(' Matrix error: invalid parameters')
self.rows = rows
self.cols = cols
self.m = [[0.0] * rows for x in xrange(cols)]
# creating identity 1 when matrix is quadratic
if self.isQuadratic() and identity:
for i in range(self.rows):
self.m[i][i] = 1.0
def __str__(self):
s = ''
for row in self.m:
s += '%s\n' % row
return s
def isQuadratic(self):
return self.rows == self.cols
def copy(self):
r = Matrix(self.rows, self.cols, False)
for i in range(self.rows):
for j in range(self.cols):
r.m[i][j] = self[i][j]
return r
def __getitem__(self, row_col):
row, col = row_col
if self.rows > row and self.cols > row:
return self.m[row][col]
else:
raise Exception('Matrix error: getitem problem')
def __setitem__(self, row_col, val):
row, col = row_col
if self.rows > row and self.cols > row:
self.m[row][col] = val
else:
raise Exception('Matrix error: setitem problem')
def rowNum(self):
return len(self.m)
def colNum(self):
return len(self.m[0])
def getRow(self, i):
if i < self.rows:
return self.m[i]
else:
raise Exception('Matrix error: number of row doesnt exist')
def getCol(self, j):
if j < self.cols:
return [row[j] for row in self.m]
else:
raise Exception('Matrix error: number of column doesnt exists')
def __add__(self, right):
if self.rows == right.rows and self.cols == right.cols:
r = Matrix(self.rows, self.cols, False)
for i in range(self.rows):
for j in range(self.cols):
r.m[i][j] = self.m[i][j] + right[i][j]
return r
else:
raise Exception(' Matrix error: cannot add matrixes, amtrixes are not the same type')
def __sub__(self, right):
if self.rows == right.rows and self.cols == right.cols:
r = Matrix(self.rows, self.cols, False)
for i in range(self.rows):
for j in range(self.cols):
r.m[i][j] = self.m[i][j] - right[i][j]
return r
else:
raise Exception(' Matrix error: cannot sub matrixes, amtrixes are not the same type')
def __mul__(self, right):
if isinstance(right, Matrix):
if self.cols == right.rows:
r = Matrix(self.rows, self.cols, False)
for i in range(self.rows):
for j in range(self.cols):
for k in range(self.cols):
r.m[i][j] += self.m[i][k] + right[k][i]
return r
else:
raise Exception(' Matrix error: cannot mul matrixes')
elif isinstance(right, Vector3D):
r = Vector3D()
addx = addy = addz = 0.0
if self.rows == self.cols == 4:
addx = self.m[0][3]
addy = self.m[1][3]
addz = self.m[2][3]
r.x = self.m[0][0] * right.x + self.m[0][1] * right.y + self.m[0][2] * right.z + addx
r.y = self.m[1][0] * right.x + self.m[1][1] * right.y + self.m[1][2] * right.z + addy
r.z = self.m[2][0] * right.x + self.m[2][1] * right.y + self.m[2][2] * right.z + addz
if self.rows == self.cols == 4:
w = self.m[3][0] * right.x + self.m[3][1] * right.y + self.m[3][2] * right.z + self.m[3][3]
if w != 1 and w != 0:
r.x = r.x / w
r.y = r.y / w
r.z = r.z / w
return r
elif isinstance(right, int) or isinstance(right, float):
r = Matrix(self.rows, self.cols, False)
for i in range(self.rows):
for j in range(self.cols):
r.m[i][j] = self.m[i][j] * right
return r
else:
raise Exception('*** Matrix: error, multiplying matrix & vector not possible')
def __div__(self, right):
if isinstance(right, int) or isinstance(right, float):
r = Matrix(self.rows, self.cols, False)
for i in range(self.rows):
for j in range(self.cols):
r.m[i][j] = self.m[i][j] / right
return r
else:
raise Exception('Matrix error: matrix division not possible ')
def transpose(self):
t = Matrix(self.cols, self.rows, False)
for j in range(self.cols):
for i in range(self.rows):
t.m[j][i] = self.m[i][j]
return t
# Quadratic matrix:
def det(self): # Only for quadratic matrices
if not self.isQuadratic():
raise Exception('*** Matrix: error, determinant of non-quadratic matrix! ***')
if self.rows == 2:
return self.m[0][0] * self.m[1][1] - self.m[0][1] * self.m[1][0]
return self.expandByMinorsOnRow(0)
def expandByMinorsOnRow(self, row): # used by det()
assert (row < self.rows)
d = 0
for col in xrange(self.cols):
d += (-1) ** (row + col) * self.m[row][col] * self.minor(row, col).det()
return d
def expandByMinorsOnCol(self, col): # used by det()
assert (col < self.cols)
d = 0
for row in xrange(self.rows):
d += (-1) ** (row + col) * self.m[row][col] * self.minor(row, col).det()
return d
def minor(self, i, j): # used by det()
if i < 0 or i >= self.rows:
raise Exception('*** Matrix: error, Determinant-row value is out of range! ***')
if j < 0 or j >= self.cols:
raise Exception('*** Matrix: error, Determinant-col value is out of range! ***')
mat = Matrix(self.rows - 1, self.cols - 1)
# Loop through the matrix, skipping over the row and column specified
# by i and j
minor_row = minor_col = 0
for self_row in xrange(self.rows):
if not self_row == i: # skip row i
for self_col in xrange(self.cols):
if not self_col == j: # Skip column j
mat.m[minor_row][minor_col] = self.m[self_row][self_col]
minor_col += 1
minor_col = 0
minor_row += 1
return mat
def invert(self):
if not self.isQuadratic():
raise Exception(' Matrix: error, non-quadratic matrix')
else:
N = self.cols
mat = Matrix(N, N)
mo = self.copy()
for column in range(N):
# Swap row in case our pivot point is not working
if (mo.m[column][column] == 0):
big = column
for row in range(N):
if (math.fabs(mo.m[row][column]) > math.fabs(mo.m[big][column])):
big = row
# Print this is a singular matrix, return identity ?
if (big == column):
print("Singular matrix\n") # To stderr
# Swap rows
else:
for j in range(N):
mo.m[column][j], mo.m[big][j] = mo.m[big][j], mo.m[column][j]
mat.m[column][j], mat.m[big][j] = mat.m[big][j], mat.m[column][j]
for row in range(N):
if (row != column):
coeff = mo.m[row][column] / mo.m[column][column]
if (coeff != 0):
for j in range(N):
mo.m[row][j] -= coeff * mo.m[column][j]
mat.m[row][j] -= coeff * mat.m[column][j]
# Set the element to 0 for safety
mo.m[row][column] = 0
for row in range(N):
for column in range(N):
mat.m[row][column] /= mo.m[row][row]
return mat
if __name__ == "__main__":
m1 = Matrix(3, 3)
m1[0, 0] = 1
m1[0, 1] = 6
m1[0, 2] = 8
m1[1, 0] = 2
m1[1, 1] = 5
m1[1, 2] = 7
m1[2, 0] = 3
m1[2, 1] = 4
m1[2, 2] = 9
m2 = Matrix(3, 3)
m2[0, 0] = 5
m2[0, 1] = 8
m2[0, 2] = 1
m2[1, 0] = 6
m2[1, 1] = 1
m2[1, 2] = 4
m2[2, 0] = 3
m2[2, 1] = 2
m2[2, 2] = 7
m = m1 * m2
print(str(m))
Please help.
from Recent Questions - Stack Overflow https://ift.tt/362ol1y
https://ift.tt/eA8V8J
Comments
Post a Comment