sympy/sympy — pull request #2780 by bjodah (2014-01-16 14:27:34+00:00)
Title: Added 'look_for_contraction' option to CodePrinter

This PR allows to print Indexed instances with non-tensor compliant indices.

Example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sympy import *

def main(len_y=5):
y = IndexedBase('y', shape=(len_y,))
x = IndexedBase('x', shape=(len_y,))
Dy = IndexedBase('Dy', shape=(len_y-1,))
i = Idx('i', len_y-1)

a,b,c = symbols('a b c')
print(ccode(a*b+c**2))

# Forward difference
e=Eq(Dy[i], (y[i+1]-y[i])/(x[i+1]-x[i]))
print(ccode(e.rhs, assign_to=e.lhs, look_for_contraction=False))

if __name__ == '__main__':
main()

Outputs:

a*b + pow(c, 2)
// Not C:
// i
Dy[i] = (y[i + 1] - y[i])*1.0/(x[i + 1] - x[i]);

How to avoid "i" to end up in CodePriner._not_supported set in unclear to me.

Critisism is welcome. It would be great if we could allow something along these lines since it currently is a real show stopper for doing e.g. finite differences meta programming using SymPy..