from sympy import Eq, IndexedBase, Idx, fcode
len_y = 5
y = IndexedBase('y', shape=(len_y,))
t = IndexedBase('t', shape=(len_y,))
Dy = IndexedBase('Dy', shape=(len_y-1,))
i = Idx('i', len_y-1)
e=Eq(Dy[i], (y[i+1]-y[i])/(t[i+1]-t[i]))
print fcode(e.rhs, assign_to=e.lhs, contract=False)
print fcode(e.rhs, assign_to=e.lhs, contract=True)
./testSympyIndexed.py
Dy(i) = (y(i + 1) - y(i))/(t(i + 1) - t(i))
Traceback (most recent call last):
File "./testSympyIndexed.py", line 49, in <module>
print fcode(e.rhs, assign_to=e.lhs, contract=True)
File "~/.local/lib/python2.7/site-packages/sympy/printing/fcode.py", line 550, in fcode
return FCodePrinter(settings).doprint(expr, assign_to)
File "~/.local/lib/python2.7/site-packages/sympy/printing/codeprinter.py", line 81, in doprint
lines = self._print(expr).splitlines()
File "~/.local/lib/python2.7/site-packages/sympy/printing/printer.py", line 257, in _print
return getattr(self, printmethod)(expr, *args, **kwargs)
File "~/.local/lib/python2.7/site-packages/sympy/printing/codeprinter.py", line 291, in _print_Assignment
return self._doprint_loops(rhs, lhs)
File "~/.local/lib/python2.7/site-packages/sympy/printing/codeprinter.py", line 113, in _doprint_loops
indices = self._get_expression_indices(expr, assign_to)
File "~/.local/lib/python2.7/site-packages/sympy/printing/codeprinter.py", line 185, in _get_expression_indices
rinds, junk = get_indices(expr)
File "~/.local/lib/python2.7/site-packages/sympy/tensor/index_methods.py", line 247, in get_indices
return _get_indices_Mul(expr)
File "~/.local/lib/python2.7/site-packages/sympy/tensor/index_methods.py", line 64, in _get_indices_Mul
inds = list(map(get_indices, expr.args))
File "~/.local/lib/python2.7/site-packages/sympy/tensor/index_methods.py", line 251, in get_indices
return _get_indices_Pow(expr)
File "~/.local/lib/python2.7/site-packages/sympy/tensor/index_methods.py", line 123, in _get_indices_Pow
binds, bsyms = get_indices(base)
File "~/.local/lib/python2.7/site-packages/sympy/tensor/index_methods.py", line 249, in get_indices
return _get_indices_Add(expr)
File "~/.local/lib/python2.7/site-packages/sympy/tensor/index_methods.py", line 167, in _get_indices_Add
raise IndexConformanceException("Indices are not consistent: %s" % expr)
sympy.tensor.index_methods.IndexConformanceException: Indices are not consistent: t[i + 1] - t[i]
>>> sympy.__version__
'1.1.1'
.
I added the last line in accordance with the comments, this should yield a version with for loops.
e=Eq(Dy[i], y[i])
print(fcode(e.rhs, assign_to=e.lhs, contract=True))
e=Eq(Dy[i], y[i+1])
print(fcode(e.rhs, assign_to=e.lhs, contract=True))
Is not
>>> e=Eq(Dy[i], y[i+1])
>>> print(fcode(e.rhs, assign_to=e.lhs, contract=False))
Dy(i) = y(i + 1)
For the contents in the issue: just a (minimal) self-contained failing example (like your last example),
together with what you had expected to be the output (and a few words describing the rationale).