I did some digging in the code, and I didn't find a way to do it.  It
seems that it is pretty hardcoded that Indexed is not just an indexed
symbol, but a tensor.  What we really should do is create a subclass
IndexedTensor and only check for that in get_contraction_structure in
sympy/tensor/index_methods.py, and require the use of that if you want
automatic tensor contraction.

Somewhat related is
http://code.google.com/p/sympy/issues/detail?id=2659.
Indexed/IndexedBase should just be generic and additional, tensor-like
assumptions should be in subclasses.

Feel free to open an issue for this, or even give it a shot implementing it.

Aaron Meurer

On Fri, Jul 27, 2012 at 1:58 PM, Matthew Turk <[email protected]> wrote:
> Hi all,
>
> I'm trying to get some code generation working, and I was hoping to
> get some clarification about how IndexedBase objects work inside
> codegen.  For instance, with this code:
>
> import sympy
> from sympy.utilities.codegen import codegen
> A, B, C, D = map(sympy.IndexedBase, "ABCD")
> m = sympy.Symbol("m", integer=True)
> i = sympy.Idx("i", m)
> expr = sympy.Equality(D[i], A[i]*B[i]+C[i])
> rv = codegen( ("f1", expr), "C", "temp", header=False)
> print rv[0][1]
>
> I would naively expect each element in D to be assigned
> A[i]*B[i]+C[i].  However, what comes out of codegen iterates over the
> index twice:
>
> [snip]
>    for (int i=0; i<m; i++){
>       for (int i=0; i<m; i++){
>          D[i] = A[i]*B[i] + D[i];
>       }
>    }
> [/snip]
>
> Everything else looks right, including the assignment to the passed
> pointer.  If I change the expression to A[i]+C[i], so there's no
> multiplication, the loop looks like what I'd expect:
>
> [snip]
>    for (int i=0; i<m; i++){
>       D[i] = A[i] + C[i];
>    }
> [/snip]
>
> It looks as though the first expression is potentially trying to
> evaluate the components as tensors.  Is there a way to modify what I'm
> doing in the multiplicative expression to indicate I'm actually
> multiplying flat arrays, and get a single loop rather than the double
> nested one?
>
> Thanks very much,
>
> Matt
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to