# Substitution of an indexed variable in an expression

Until a certain stage of my computation, I do not need indices in my work. Then I want to substitute indexed variables

x,y,p,q=SR.var('x, y, p, q')
A=matrix(SR,2,2,[x,-y,-x,0])
B=matrix(SR,2,2,[-x,y,x,0])
pp = vector(SR,[p, 1-p])
qq = vector(SR,[q, 1-q])
EGe0 = (pp*A*qq).collect(p)
EGp0 = (pp*B*qq).collect(q)
p = var("p", n=10, latex_name='\\overline{p}')
EGe0 = (pp*A*qq).collect(p).substitute_expression(p==p)
show(EGe0)


But I certainly have made an error. (I also tried .subs())

edit retag close merge delete

Sort by » oldest newest most voted Indeed, (pp*A*qq).collect(p) does not work because p now refers to the tuple containing the indexed variables, rather than the original p. So, use a different name for the tuple of indexed variables, like P:

P = var("p", n=10, latex_name='\\overline{p}')
EGe0 = (pp*A*qq).collect(p).subs(p==P)
show(EGe0)


Output: $${\left(2 q x + {\left(q - 1\right)} y\right)} {\overline{p}}_{0} - q x$$

more