Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decide on high level looping constructs #3

Open
jcrist opened this issue Sep 18, 2014 · 4 comments
Open

Decide on high level looping constructs #3

jcrist opened this issue Sep 18, 2014 · 4 comments

Comments

@jcrist
Copy link
Owner

jcrist commented Sep 18, 2014

So far what I'd like to implement is:

Functional Looping

  • Map
  • Filter
  • Reduce

Mathematical Looping

  • Sum (not like sympy's sum, more like python's sum)
  • Prod (like Sum, but with *, not +)

Ideally, sympy's Sum could also work, so maybe we should have sum instead, that returns a sympy.Sum object, with the iteration setup properly.

@bjodah
Copy link

bjodah commented Sep 23, 2014

Not sure if this is of any value for you, but I managed (very hackishly) make this work:

x[i] = (a[i]/3-1)**i + c
y[j] = a[j] - j

here:

https://github.com/bjodah/pycodeexport/blob/master/examples/loops_main.py

(uses mako template's to recursivesly print loops)

@jcrist
Copy link
Owner Author

jcrist commented Sep 24, 2014

mmmboy, that's complicated. I'm actually planning on having as little magic in this as possible. I'm looking more for expressive ways to define iterations in SymPy. These will then be converted to instances of For in the first pass of the compiler, and then printed just as any other node would be. The question is then really what is a syntactically clear way of expressing loops. I'm not a fan of the tensor notation expressing loops inherently on it's own.

Enclosed in a higher looping construct though, the indices are nice. Something like:

i, n = symbols('i, n', integer=True)
x = Vector('x', n)
# Iterate i from 0 to n, performing the assignment along the way
Iter(Assign(y[i], (x[i]/3 - 1)**i + c), (i, 0, n))

isn't too bad. I'd like to really get these designed in the most expressive and usable way possible though, so we don't have to redo them again...

@bjodah
Copy link

bjodah commented Sep 24, 2014

I'm all for less magic! (that example of mine was a horrible hack...)

No, I'm not a fan of the tensor contraction either - to me it's an exception and should be handled by the tensor module, not by code printers.

I really like your approach with a proper tree structure for this with appropriate nodes!

@asmeurer
Copy link

My advice here would be to use non-code generation specific things whenever possible. That is, they should be done in a mathematically general way whenever it makes sense. For example, "sum of an array" makes mathematical sense outside of code generation. So I would implement a way to do that in SymPy in general, and then write code printers for it. Raw loop constructs, like For or While probably don't fit into this category. Functional looping constructs may be representable in generic mathematical ways (we already have Lambda).

I agree that tensor looping is not the way to go. Not all loops are tensor loops, and not everyone thinks in terms of tensors, or has even been taught that way (it's not a trivial way of thinking to learn if you haven't done it before).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants