def constToAffine(obj,rows,cols): if isScalar(obj): new_affine = Affine(rows,cols) for j in range(cols): new_affine.vectors[j][Affine.CONST] = obj*sparse.csc_matrix(np.ones((rows,1))) else: shape = mutils.getShape(obj) new_affine = Affine(shape[0],shape[1]) for j in range(cols): new_affine.vectors[j][Affine.CONST] = sparse.csc_matrix(obj[:,j:j+1]) return new_affine
def __rmul__(self,other): """ Perform other*self """ if isScalar(other): return self.scale(other) mat_shape = mutils.getShape(other) if not mat_shape[1] == self.rows: raise RuntimeError('Multiplication failed due to size mismatch ' + str(mat_shape) + ' * ' + str(self.size())) new_affine = Affine(mat_shape[0],self.cols) other = sparse.csc_matrix(other) for j in range(self.cols): for key in self.vectors[j]: new_affine.vectors[j][key] = other.dot(self.vectors[j][key]) return new_affine
def __mul__(self,other): """ Perform self*other. This is done by individually evaluating each column of the new matrix as a weighted sum of the columns of self. This is not efficient and so it is recommended to avoid this if possible. """ if isScalar(other): return self.scale(other) mat_shape = mutils.getShape(other) if not mat_shape[0] == self.cols: raise RuntimeError('Multiplication failed due to size mismatch ' + str(self.size()) + ' * ' + str(mat_shape)) new_affine = Affine(self.rows,mat_shape[1]) for j in range(mat_shape[1]): new_affine_col = sum([float(other[i,j])*self[:,i] for i in range(self.cols)]) """ new_affine_col is always a vector of length self.rows """ for key in new_affine_col.vectors[0]: new_affine.vectors[j][key] = new_affine_col.vectors[0][key] return new_affine