Skip to content

Commit

Permalink
simple codegen example
Browse files Browse the repository at this point in the history
  • Loading branch information
Eelco Hoogendoorn committed Jan 28, 2024
1 parent e147c98 commit 9751724
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions numga/backend/python/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@


class PythonContext(AbstractContext):
def __init__(self, algebra, dtype=float):
def __init__(self, algebra, dtype=float, otype=PythonSparseOperator):
super(PythonContext, self).__init__(
algebra=algebra,
dtype=dtype,
otype=PythonSparseOperator,
otype=otype,
mvtype=PythonMultiVector
)
21 changes: 20 additions & 1 deletion numga/backend/python/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,28 @@ def __call__(self, *inputs: Tuple[PythonMultiVector]) -> PythonMultiVector:
math.prod((inp.values[ii] for inp, ii in zip(inputs, idx)), start=scalar)
for (idx, scalar) in term
)

return output

# def partial(self, inputs: Dict[int, PythonMultiVector]) -> "PythonSparseOperator":

class PythonCodegenOperator(AbstractConcreteOperator):
"""quick and dirty python codegen example"""

def __call__(self, *inputs: Tuple[PythonMultiVector]) -> PythonMultiVector:
terms = self.precompute_sparse
inputs = [f'i{i}' for i in range(self.operator.arity)]
inp = ','.join(inputs)
text = f'def foo({inp}):\n'
def make_term(idx, scalar):
q = [inp + f'[{ii}]' for inp, ii in zip(inputs, idx)]
return '*'.join([str(scalar)] + q)

def make_line(ci, term):
return f'\to[{ci}] = ' + '+'.join(make_term(idx, scalar) for idx, scalar in term)

return text + '\n'.join(make_line(ci, term) for ci, term in terms)

# def partial(self, inputs: Dict[int, PythonMultiVector]) -> "PythonSparseOperator":
# expr = self.precompute_einsum_partial(tuple(inputs.keys()))
# return PythonSparseOperator(
# self.context,
Expand Down
9 changes: 9 additions & 0 deletions numga/backend/test/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ def test_sparse_operator():

output = q.sandwich(v)
print(output)


def test_codegen():
from numga.backend.python.operator import PythonCodegenOperator
ga = PythonContext('x+y+z+', otype=PythonCodegenOperator)
even = ga.subspace.even_grade()
op = ga.operator.product(even, even)
print()
print(op())

0 comments on commit 9751724

Please sign in to comment.