On Oct 12, 2010, at 10:53 AM, Nicholas Kinar wrote:

> 
>> 
>> The easiest workaround that I can think of is to use subs() to substitute 
>> variables p_1_1 and p_1_2 into the expressions so that the Jacobian is not 
>> computed using indexed variables p[1,1] and p[1,2].  However, I can't seem 
>> to use subs() to substitute the variables either!  What am I doing wrong 
>> here?
>> 
>> The output of the modified example code is:
>> 
>> 0.5 - p[1, 2] - 2*p[1, 1] + p[1, 1]**2
>> -4 + p[1, 1]**2 + 4*p[1, 2]**2
>> [0, 0]
>> [0, 0]
>> 
> 
> The output of subs must be assigned to a variable.  The following test 
> program (at the bottom of this e-mail) seems to produce correct behavior, as 
> shown by the output:
> 
> [-2 + 2*p_1_1,      -1]
> [     2*p_1_1, 8*p_1_2]
> 
> 
> I have to admit that the documentation 
> (http://docs.sympy.org/modules/core.html?highlight=subs#sympy.core.basic.Basic.subs)
>  misled me into thinking that subs() works directly on the variable itself, 
> and there is no need to assign the output to another variable.

All expressions in SymPy are always immutable, so no operation will ever be 
done in place.  This should go in the gotchas, I think.  

> 
> If I would have seen something similar to the following in the documentation, 
> then I think I would have immediately realized that the output can be placed 
> into a variable.
> 
> expr = (1+x*y).subs([(x,pi), (y,2)])

Can you submit a patch changing this?  I agree that it would be clearer.  Also, 
if you want to add a bit to the gotchas (http://docs.sympy.org/gotchas.html) 
noting that expressions are immutable, that would be great too (or if you don't 
have time to submit such a patch, can you at least create an issue for it?).  
The docs are in the docs/src directory of the sympy repository, and you can 
test build them by typing "cd docs; make html" (requires Sphinx), and they will 
be build into _build.

Aaron Meurer

> 
> Of course, the output at the prompt shows that something is being returned, 
> but IMHO it would have been easier to understand when attempting to write a 
> script.
> 
> Nicholas
> 
> 
> #begin sample code
> from sympy import *
> from sympy.tensor import Indexed, Idx, IndexedBase
> from sympy.matrices import *
> 
> p = IndexedBase('p')
> 
> def test_function():
> 
>    var('p_1_1 p_1_2')
> 
>    expr1 =  p[1,1]**2 - 2*p[1,1] - p[1,2] + 0.5
>    expr2 = p[1,1]**2 + 4*p[1,2]**2 - 4
> 
>    expr1 = expr1.subs(p[1,1], p_1_1)
>    expr1 = expr1.subs(p[1,2], p_1_2)
> 
>    expr2 = expr2.subs(p[1,1], p_1_1)
>    expr2 = expr2.subs(p[1,2], p_1_2)
> 
>    vec = Matrix([expr1, expr2])
>    v = Matrix([p_1_1, p_1_2])
>    J = vec.jacobian(v)
>    print J
> 
> 
> def main():
>    test_function()
> 
> if __name__ == "__main__":
>    main()
> #end sample code

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

Reply via email to