# 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[]  -> 0,  xs[] -> 0,  xs[] -> 0,  xs[] -> 0,  xs[] -> 0, \
xs[]  -> 0,  xs[] -> 0,  xs[] -> 0,  xs[] -> 0, xs[] -> 0, \
xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, \
xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, \
xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, \
xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, \
xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, \
xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, xs[] -> 0, \
xs[] -> 0, xs[] -> 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