def postmap(self, other: SimpleNumber, op: str) -> 'SimpleMatrix': """Postmap a binary operation to a simple matrix.""" svecs = [] for sv in self.svecs: svecs.append( SimpleVector([other.binary(op, sn) for sn in sv.snums])) return SimpleMatrix(svecs)
def matmul(self, other: 'SimpleMatrix') -> 'SimpleMatrix': """Multiply two simple matrices.""" if self._cols != other._rows: raise YovecError('cannot mulitply matrices with mismatching sizes') svecs = [] for i in range(self._rows): snums = [] for j in range(other._cols): sn = SimpleNumber(0) for k in range(other._rows): sn = sn.binary( 'add', self.svecs[i].snums[k].binary('mul', other.svecs[k].snums[j])) snums.append(sn) svecs.append(SimpleVector(snums)) return SimpleMatrix(svecs)
def assign(self, vec_index: int) -> Tuple[List[Node], 'SimpleVector']: """Generate YOLOL assignment statements.""" assignments = [] snums = [] expressions = [sn.evaluate() for sn in self.snums] for i, expr in enumerate(expressions): ident = 'v{}e{}'.format(vec_index, i) var = Node(kind='variable', value=ident) asn = Node(kind='assignment', children=[var, expr]) assignments.append(asn) snums.append(SimpleNumber(ident)) return assignments, SimpleVector(snums)
def assign(self, mat_index: int) -> Tuple[List[Node], 'SimpleMatrix']: """Generate YOLOL assignment statements.""" assignments = [] svecs = [] for i, sv in enumerate(self.svecs): snums = [] for j, sn in enumerate(sv.snums): expr = sn.evaluate() ident = 'm{}r{}c{}'.format(mat_index, i, j) var = Node(kind='variable', value=ident) asn = Node(kind='assignment', children=[var, expr]) assignments.append(asn) snums.append(SimpleNumber(ident)) svecs.append(SimpleVector(snums)) return assignments, SimpleMatrix(svecs)
def len(self) -> SimpleNumber: """Return the length of the simple vector.""" return SimpleNumber(self.length)
def dot(self, other: 'SimpleVector') -> SimpleNumber: """Calculate the dot product of two simple vectors.""" sn = SimpleNumber(0) for lsn, rsn in zip(self.snums, other.snums): sn = sn.binary('add', lsn.binary('mul', rsn)) return sn
def postmap(self, other: SimpleNumber, op: str) -> 'SimpleVector': """Postmap a binary operation to a simple vector.""" return SimpleVector([other.binary(op, sn) for sn in self.snums])
def cols(self) -> SimpleNumber: """Return the number of columns in the simple matrix.""" return SimpleNumber(self._cols)
def rows(self) -> SimpleNumber: """Return the number of rows in the simple matrix.""" return SimpleNumber(self._rows)