Dear all,
I'm playing around with generating some C code for numerical analysis and simulation. The library is good and easy to work with in general, only thing I'm not sure so far is how sympy is treating multi-dimensional arrays.
Simple example of what I'm looking for:
sympy code: U[i, j] = U[i, j-1] + U[i, j-2]
C code: U[i][j] = U[i][j-1] + U[i][j-2]
As
you see it's pretty much direct substitution of symbols. However in
order to handle >2D array, I need to replace
printing.ccode._print_Indexed() with something like below:
def _print_Indexed(self, expr):
# Array base and append indices
output = self._print(expr.base.label) + ''.join([ '[' + self._print(x) + ']' for x in expr.indices ])
return output
The
current implementation seems to try to flatten the multi-dimensional
array into 1D vector, so I will get a 1D array back and I need to supply
the IndexedBase with its shape:
for i in reversed(range(expr.rank)):
elem += expr.indices[i]*offset
offset *= dims[i]
return "%s[%s]" % (self._print(expr.base.label), self._print(elem))
Can someone shed some light on why it is done this way? Thanks vm.