How to apply a rule involving a hundred variables in mathematics

I have an expression that includes x1, x2, ..., x100, I also have an lst list with 100 elements, how to apply a rule to this expression to achieve something like the following:

 exp/.{x1->lst[[1]],x2->lst[[2]],...,x100->lst[[100]]} 

Thanks!

+2
source share
3 answers
  exp /. Table[Symbol["x" <> ToString[i]] -> lst[[i]], {i, 1, 100}] 

Therefore you do not need to write X1, X2, ... X100

+6
source

You can use Thread to apply rules to each pair of expressions:

 Thread[{a, b, c} -> {1, 2, 3}] 
+3
source

It is much simpler and more convenient to solve such problems using indexed variables instead of generating a list of different Symbol s. In this way:

 listOfRules = Array[ f@ # :> list[[#]] &, {100}]; Short@ % => {f[1]:>list[[1]],f[2]:>list[[2]],f[3]:>list[[3]],f[4]:>list[[4]], <<92>>,f[97]:>list[[97]],f[98]:>list[[98]],f[99]:>list[[99]],f[100]:>list[[100]]} 

If you plan to make such a replacement many times, Dispatch large list of rules:

 listOfRules = Dispatch@listOfRules ; 

Replacement can be performed as usual:

 expr /. listOfRules 
+2
source

All Articles