RemoveFunction
RemoveFunction
RemoveFunction[ expr, func ] removes all occurrences of a given function from an expression.
Usages
RemoveFunction[ expr, func ]
RemoveFunction[ expr, func ] removes the function func everywhere in expr, and keeps its first argument. For example, Quantity[3, 'm'] becomes 3: the unit is removed.
| Example | in | out |
|---|---|---|
| Remove the units from a solution. | RemoveFunction[ Solution[x=Quantity[3, 'm'], x], Quantity ] |
Solution[x=3, x] |
| Remove the units from a list of quantities. | RemoveFunction[ {Quantity[3,'m'], Quantity[5,'m']}, Quantity ] |
{3, 5} |
| Remove the square roots. | RemoveFunction[ Sqrt[2] + Sqrt[x], Sqrt ] |
2+x |
| Also inside a mapping. | RemoveFunction[ x -> Sqrt[x], Sqrt ] |
x -> x |
Options
RemoveChildren
If set to True, the whole sub-expression with func is removed, including its arguments. Default: False.
| Syntax | Example | out |
|---|---|---|
RemoveFunction[ expr, func, RemoveChildren=>True ] |
RemoveFunction[ {Quantity[3,'m'], Text[`abc`], 4}, Text, RemoveChildren=>True ] |
{Quantity[3, 'm'], 4} |
Skip
A function, or a list of functions, whose contents are left untouched: func is not removed inside these functions.
| Syntax | Example | out |
|---|---|---|
RemoveFunction[ expr, func, Skip=>f ] |
RemoveFunction[ {Sqrt[2], Hold[Sqrt[3]]}, Sqrt, Skip=>Hold ] |
{2, Hold[Sqrt[3]]} |
RemoveFunction[ expr, func, Skip=>{f1, f2, ...} ] |
RemoveFunction[ {Sqrt[2], Hold[Sqrt[3]], Abs[Sqrt[5]]}, Sqrt, Skip=>{Hold, Abs} ] |
{2, Hold[Sqrt[3]], Abs[Sqrt[5]]} |
Details
- If
funcdoes not occur inexpr,expris returned unchanged. - Only the first argument is kept. For a function with more arguments, the others are removed as well.
RemoveChildrenin a sum or product can leave an incomplete expression:RemoveFunction[ Sqrt[2] + Sqrt[x] + 1, Sqrt, RemoveChildren=>True ]givesPlus[1].
| Invalid input | Result |
|---|---|
RemoveFunction[ Quantity[3, 'm'] ] |
Error: the number of arguments should be 2 |
RemoveFunction[ Quantity[3, 'm'], Quantity, Foo=>1 ] |
Error: Invalid associative argument for RemoveFunction: Foo |
See also: Substitute, Apply, Quantity