CreateMapping

CreateMapping

CreateMapping[ x, expr ] creates the mapping x -> expr, also when the variable or the expression comes from a script variable.

Usages

CreateMapping[ x, expr ]

CreateMapping[ x, expr ] returns the mapping x -> expr. Use it instead of writing x -> expr directly when expr or x is stored in a script variable (see the note below).

Example in out
A simple mapping. CreateMapping[ x, x^2 + 1 ] x -> x^2+1
Evaluate the mapping. f := CreateMapping[x, x^2 + 1]
r := f[3]
r = 3^2+1
The expression is a script variable. e := 3t^2 - t
f := CreateMapping[t, e]
r := f[2]
f = t -> 3t^2-t
r = 3*2^2-2
The variable is chosen at random. v := RandVars[1]
f := CreateMapping[v, v^2]
r := f[3]
for example f = a -> a^2
r = 3^2

CreateMapping[ {x, y, ...}, expr ]

With a list of variables, the mapping takes several values.

Example in out
A mapping with two variables. f := CreateMapping[{x, y}, x*y]
r := FuncApplication[f, {2, 5}]
f = {x,y} -> x y
r = 2*5
Note

Writing the mapping directly does not work when parts of it are script variables:

  • With e := 3t^2 - t, the mapping f := t -> e does not substitute values: f[2] gives 3t^2-t.
  • With v := RandVars[1], the mapping v -> v^2 uses the letter v itself, not the randomly chosen variable.

CreateMapping creates the mapping correctly in both cases.

Details
  • The result is not calculated: use Simplify or Calc commands for that.
Invalid input Result
CreateMapping[ 2, x ] Error: the 1st argument should be a list of variables
CreateMapping[ x ] Error: the number of arguments should be 2

See also: FuncApplication, Map, RandVars, Hold