Symbolic operators in Mathematica
Posted by David Zaslavsky on — CommentsThis originally arose as a post I was going to make on Mathematica Stack Exchange, but the question wasn’t well defined enough for Stack Exchange’s Q&A format. So I’m reposting it here. The list is incomplete right now but I’ll keep adding to it as I have time.
Each of the operators below is a shortened notation for some piece of Mathematica syntax or for a Mathematica function. After a short description of each operator, I list one or two usage example along with a way of representing the same thing using different syntax.
-
@is the prefix notation for function application. This applies the single-argument function whose name precedes it to the expression that follows it.func @ expr func[expr](remember, that’s a usage example and an equivalent way of writing the same thing using different notation)
-
//is the postfix notation for function application. This applies the single-argument function whose name follows it to the expression that precedes it.expr // func func[expr] -
~is the infix notation for function application. This has a slightly unusual usage pattern: you have to use it twice, in the formarg1 ~ func ~ arg2, and it applies the two-argument function whose name is contained in the middle to the expressions on either side of it.expr1 ~ func ~ expr2 func[expr1, expr2] -
@@is the symbolic expression for the Mathematica functionApply, acting at level 0. This replaces the head of the expression given on its right with the function named on its left. Essentially, it swaps out one function for another.func1@@func2[a, b, c] func1[a, b, c]This is frequently used to convert a list of arguments into a function call,
func1@@{a, b, c} func1@@List[a, b, c] func1[a, b, c] -
@@@is the symbolic expression forApplyacting at level 1. This replaces the head of each first-level subexpression of the expression given on its right with the function named on its left. It’s kind of like taking@@and mapping it across a list or other structure.func@@@{f[x], g[x], h[x]} {func[x], func[x], func[x]}Or another example (and equivalent syntax):
func1@@@func2[f[x], g[x], h[x]] func2[func1[x], func1[x], func1[x]] -
/@is the symbolic expression forMap. This applies the function named on its left to each first-level subexpression of the expression given on its right.func/@{a, b, c} {func[a], func[b], func[c]}This is very frequently used in functional programming.
-
//@is the symbolic expression forMapAllThis applies the function named on its left to each subexpression at any level of the expression given on its right.func//@{f[a], g[b], h[c]} func[{func[f[func[a]]],func[g[func[b]]],func[h[func[c]]]}] -
=is the symbolic expression forSet, typically known as the assignment operator. This assigns the value of the expression given on its right to the symbol given on its left.a = b Set[a, b] -
:=is the symbolic expression forSetDelayed. This assigns a delayed version of the expression given on its right to the symbol given on its left: when the symbol is referenced, the expression will be evaluated and it will use the value given by that evaluation.a := b SetDelayed[a, b] -
->is the symbolic expression forRule. This is used in combination withReplaceAllor a similar function to perform a pattern substitution. It doesn’t do anything on its own.a -> b Rule[a, b] -
:>is the symbolic expression forRuleDelayed. This is also used in combination withReplaceAllor similar to perform a pattern substitution. It also doesn’t do anything on its own. The difference between this andRuleis that whereas the second argument ofRuleis evaluated right when you write theRule, the second argument ofRuleDelayedis evaluated later, when the rule is used inReplaceAllor similar.a :> b RuleDelayed[a, b] -
/.is the symbolic expression forReplaceAll. This alters the expression given on its left by converting each occurrence of a “key” in the list of transformation rules given on its right to the corresponding value. Perhaps best illustrated with an example:{f[a], g[b]}/.{a->c, g->h} {f[c], h[b]} -
/;is the symbolic expression forCondition. This limits the pattern, rule, or assignment on its left to only apply in cases where the expression on the right evaluates toTrue.a := b /; b > 4 a := Condition[b, b > 4]or
a :> b /; b > 4 a :> Condition[b, b > 4]or
f[b_ /; b > 4] := Sqrt[b - 4] f[Condition[b_, b > 4]] := Sqrt[b - 4] -
_is the symbolic expression forBlank. This is a pattern which matches any single expression._ Blank[]The pattern can be limited by specifying a head:
_h Blank[h]This pattern will only match expressions with head
h. -
__is the symbolic expression forBlankSequence. This is a pattern which matches one or more expressions.__ BlankSequence[]The pattern can be limited by specifying a head:
__h BlankSequence[h]This pattern will only match sequences of expressions each of which has head
h. -
___is the symbolic expression forBlankNullSequence. This is a pattern which matches zero or more expressions.___ BlankNullSequence[]The pattern can be limited by specifying a head:
__h == BlankNullSequence[h]This pattern will only match sequences of expressions each of which has head
h. -
:if followed by a pattern is the symbolic expression forPattern. This will match the pattern on its right and give the matched expression the name on its left.p:{__} Pattern[p, {__}] -
:if preceded by a pattern is the symbolic expression forOptional. This will match the pattern on its left if possible, and if not, will produce the value of the expression on its right._:x Optional[_, x] -
?is the symbolic expression forPatternTest. This limits the pattern on its left to only match in cases where the function on the right, applied to the expression matched by the pattern, evaluates toTrue.p_?NumberQThis only matches expressions on which
NumberQreturnsTrue. -
[[ ... ]]is the symbolic expression forPart. This extracts the subexpression of the expression given on its left corresponding to the set of indices given within the double brackets.{f[a], g[b]}[[2]] g[b]Or
{f[a], g[b]}[[2,1]] bThe index 0 can be used to refer to the
Headof a subexpression.{f[a], g[b]}[[0]] List[f[a], g[b]][[0]] Listor
{f[a], g[b]}[[2,0]] gor
{f[a], g[b]}[[2,1,0]] Symbol -
;;is the symbolic expression forSpan. When used as an index toPart, it allows you to specify a range in place of a single index.{a, b, c, d}[[1;;3]] {a, b, c}You can also use it twice to specify a skip.
{a, b, c, d}[[1;;3;;2]] {a, c}Aside: this is basically just like the slice operator in Python.
-
&is the symbolic expression forFunction. It allows you to define a (pure) function “on-the-fly”, so to speak. -
#is the symbolic expression for an argument within a pure function.Exp[-# + 2]& Function[{x}, Exp[-x + 2]]This is roughly equivalent to the definition of
fafter executingf[x_] := Exp[-x + 2]You can use
#followed by a number to represent multiple argumentsExp[-#1 - #2]& Function[{x, y}, Exp[-x - y]] -
~~is the symbolic expression forStringExpression. It’s really just a string concatenation operator."abc"~~"def" "abcdef"
And more to come…