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 = 3^2+1 |
| The expression is a script variable. | e := 3t^2 - t |
f = t -> 3t^2-tr = 3*2^2-2 |
| The variable is chosen at random. | v := RandVars[1] |
for example f = a -> a^2r = 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] |
f = {x,y} -> x yr = 2*5 |
Note
Writing the mapping directly does not work when parts of it are script variables:
- With
e := 3t^2 - t, the mappingf := t -> edoes not substitute values:f[2]gives3t^2-t. - With
v := RandVars[1], the mappingv -> v^2uses the lettervitself, not the randomly chosen variable.
CreateMapping creates the mapping correctly in both cases.
Details
- The result is not calculated: use
SimplifyorCalccommands 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