in the documentation of sympy's fcode the following example is presented:
http://docs.sympy.org/latest/_modules/sympy/printing/fcode.html
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)
I added the last line in accordance with the comments, this should yield a
version with for loops.
Instead an error is thrown, and the contract=False case does not seem to
give compilable code:
./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]
I have sympy version 1.1.1
>>> sympy.__version__
'1.1.1'
.
Is this a bug or a misinterpretation on my side?
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/9997ecb8-99fd-49a5-96ca-d09f8177c84f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.