def matmul(self, other: 'Matrix') -> 'Matrix': """Multiply two matrices.""" if self._cols != other._rows: raise YovecError('cannot mulitply matrices with mismatching sizes') vecs = [] for i in range(self._rows): nums = [] for j in range(other._cols): n = Number(0) for k in range(other._rows): n = n.binary( 'add', self.vecs[i].nums[k].binary('mul', other.vecs[k].nums[j])) nums.append(n) vecs.append(Vector(nums)) return Matrix(vecs)
def dot(self, other: 'Vector') -> Number: """Calculate the dot product of two vectors.""" n = Number(0) for ln, rn in zip(self.nums, other.nums): n = n.binary('add', ln.binary('mul', rn)) return n
def postmap(self, other: Number, op: str) -> 'Vector': """Postmap a binary operation to a vector.""" return Vector([other.binary(op, n) for n in self.nums])
def postmap(self, other: Number, op: str) -> 'Matrix': """Postmap a binary operation to a matrix.""" vecs = [] for v in self.vecs: vecs.append(Vector([other.binary(op, n) for n in v.nums])) return Matrix(vecs)