def __init__(self, i=0, j=0, dir=0, mag=0): if dir!=0 or mag!=0: if hasattr(dir, "to_rad"): dir = dir.to_rad() self.i = math.cos(dir) * mag self.j = math.sin(dir) * mag elif isiterable(i): self.i,self.j = i else: self.i = i self.j = j
def __rmul__(self,oth): """ Can apply another matrix or similar nested iterable (where cols in lhs equals rows in rhs), or a scalar value, to this matrix. The result is a matrix. """ if isinstance(oth,Matrix) or isiterable(oth): # matrix oth_m = oth if not isinstance(oth_m,Matrix): oth_m = Matrix(oth_m) return oth_m._mat_mul(self) else: # scalar return Matrix._make_new(lambda i,j: oth * self.data[i][j], self.rows, self.cols)
def __init__(self,data): """ Can construct from singly or doubly nested iterable. Each inner item forms a row in the matrix. E.g: (1,2,3) => | 1 | | 2 | | 3 | ( (1,2,3), => | 1 2 3 | (4,5,6) | 4 5 6 | ) """ self.data = tuple([tuple(x) if isiterable(x) else (x,) for x in data]) self.rows = len(self.data) self.cols = len(self.data[0]) if len(self.data)>0 else 0
def __mul__(self, oth): """ Can apply this matrix to another matrix or any similar nested iterable (where cols in lhs equals rows in rhs), resulting in the same type as the operand, or can apply to scalar, resulting in a matrix. """ if isinstance(oth, Matrix) or isiterable(oth): # matrix oth_m = oth if not isinstance(oth_m, Matrix): oth_m = Matrix(oth_m) res_m = self._mat_mul(oth_m) if isinstance(oth, Matrix): return res_m else: return type(oth)(res_m._unnest()) else: # scalar return Matrix._make_new(lambda i,j: self.data[i][j] * oth, self.rows, self.cols)
def __init__(self, i=0, j=0, k=0, dir=0, mag=0): """ Can construct from i,j,k parameters, dir and mag parameters or with a single (i,j,k) tuple. """ if dir!=0 or mag!=0: if hasattr(dir, "to_tuple"): dir = dir.to_tuple() roll = dir[0] pitch = dir[1] yaw = dir[2] fwd = math.cos(pitch) * mag up = math.sin(pitch) * mag self.i = math.cos(yaw) * fwd self.j = math.sin(yaw) * fwd self.k = up elif isiterable(i): self.i,self.j,self.k = i else: self.i = i self.j = j self.k = k