Have you tried to use *Idx* instead of *Symbol* for the index?

k = Idx("k", *4*)

4 is the range.

In this case, *k *ranges from 0 to 4-1 = 3.

On Thursday, 14 April 2016 09:31:38 UTC+2, Nico Schlömer wrote:
>
> Once again, atoms to the rescue.
> ```
> y.atoms(IndexedBase)
> ```
> will give you all IndexedBase object from an expression.
>
>
> On Thursday, April 14, 2016 at 9:25:09 AM UTC+2, Nico Schlömer wrote:
>>
>> It seems that, unfortunately, the information of whether or not a 
>> variable is an IndexedBase object is discarded when using it in an 
>> expression. Check
>> ```
>> from sympy import *
>>
>> u = IndexedBase('u')
>> k = Symbol('k')
>> y = u[k]
>>
>> print(isinstance(u, IndexedBase))
>>
>> for s in y.free_symbols:
>>     print(s, isinstance(s, IndexedBase))
>> ```
>> Hm...
>>
>>
>> On Thursday, April 14, 2016 at 8:59:46 AM UTC+2, Nico Schlömer wrote:
>>>
>>> From an object like `sin(u[k]) + u0[k]` I would like to get the 
>>> corresponding C code as a string. Since `k` is a variable, I cannot use 
>>> `MatrixSymbol`, but there's always `IndexedBase` of course. With
>>> ```
>>> u = IndexedBase('u')
>>> u0 = IndexedBase('u0')
>>> k = Symbol('k')
>>> y = sin(u[k]) + u0[k]
>>> ```
>>> things are looking good, but then
>>> ```
>>> from sympy.utilities.codegen import codegen
>>> [(c_name, c_code), (h_name, c_header)] = codegen(("f", y), "C")
>>> ```
>>> tells you
>>> ```
>>> sympy.tensor.indexed.IndexException: 
>>> Range is not defined for all indices in: u[k]
>>> ```
>>> I could perhaps go through all `IndexedBase` instances in `y` and 
>>> `.subs`titute them with a variable with the string `u[k]`, but that feels 
>>> hackish.
>>>
>>> Any hints?
>>>
>>

-- 
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/7813780f-4bc1-4b6c-915e-a5698b071088%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to