# ReplaceAll Elements in an Indexed Vector

Following up on how to define symbolic vectors

Let x be an n-dimensional symbolic vector, where n is a positive integer large enough to be a massive pain in the butt.

n = 42;
xs = Array[Indexed[x, #] &, n]


If I were to evaluate a function on x with some value, I would replace each element by hand using the shorthand notation for ReplaceAll.

For example, if I take the sum of squares on x at the zero vector, I would type

Transpose[xs] . xs /. { \
xs[[1]]  -> 0,  xs[[2]] -> 0,  xs[[3]] -> 0,  xs[[4]] -> 0,  xs[[5]] -> 0, \
xs[[6]]  -> 0,  xs[[7]] -> 0,  xs[[8]] -> 0,  xs[[9]] -> 0, xs[[10]] -> 0, \
xs[[11]] -> 0, xs[[12]] -> 0, xs[[13]] -> 0, xs[[14]] -> 0, xs[[15]] -> 0, \
xs[[16]] -> 0, xs[[17]] -> 0, xs[[18]] -> 0, xs[[19]] -> 0, xs[[20]] -> 0, \
xs[[21]] -> 0, xs[[22]] -> 0, xs[[23]] -> 0, xs[[24]] -> 0, xs[[25]] -> 0, \
xs[[26]] -> 0, xs[[27]] -> 0, xs[[28]] -> 0, xs[[29]] -> 0, xs[[30]] -> 0, \
xs[[31]] -> 0, xs[[32]] -> 0, xs[[33]] -> 0, xs[[34]] -> 0, xs[[35]] -> 0, \
xs[[36]] -> 0, xs[[37]] -> 0, xs[[38]] -> 0, xs[[39]] -> 0, xs[[40]] -> 0, \
xs[[41]] -> 0, xs[[42]] -> 0}


Is there a shortcut to ReplaceAll the elements in the vector?

• Transpose[xs] . xs /. Thread[xs -> 0] or Transpose[xs] . xs /. _Indexed ->0?
– kglr
Mar 11 at 8:02
• Try: xs /. Indexed[x, _] -> 0 Mar 11 at 8:25

The FullForm of

is

Hence you can do

n = 10;
xs = Array[Indexed[x, #] &, n];
res = Transpose[xs] . xs


Now to replaced each $$x_{i}^{n}$$ just do

res /. Indexed[x, any1_]^any2_. :> 1
(*10*)


The above replaces each entry by 1. You can replace each element by zero also.

 res /. Indexed[x, any1_]^any2_. :> 0
(*0*)


You can replace the above using any other way you want using the named patterns any1_ and any2_ also.

For example to replace each element by the power it has

res /. Indexed[x, any1_]^any2_. :> any2
(*20*)


To replace each by its index

res /. Indexed[x, any1_]^any2_. :> any1
(* 55 *)


and so on.

I am not sure if this is what you meant or not.

• +1 There is no distinction between row and column vectors. Consequently, use the simpler res = xs . xs And if a pattern name on the LHS of a rule is not used on the RHS of the rule, then the pattern does not need a name; e.g., res /. Indexed[x, _]^_. :> 1 or res /. Indexed[x, _]^any2_. :> any2 suffice. Mar 11 at 2:42