Hot answers tagged

48

Operator Precedence Table Unless one wishes to write in FullForm a competent Mathematica user must be familiar with at least the majority of syntax precedence rules, which are described in the Operator Precedence table. Clarification: I do not mean that one must memorize (most of) this entire table be competent, but rather that one should know it well ...


42

For me the operator forms of Map and Apply will probably provide the most important benefits in terms of code readability. Often I need to apply a sequence of transformations to some data, and I am fond of infix notation for this purpose. For example I find a ~Position~ 0 ~SortBy~ Last more readable than the "conventional" SortBy[Position[a, 0], Last] ...


34

I would have liked to have more experience with the operator forms before this question was asked as I am short on examples, and I'm sure my opinion will evolve over time. Nevertheless I think I have enough familiarity with similar syntax to provide some useful comments. Taliesin Beynon provided some background for this functionality in Chat: Operator ...


30

Edit: version 10.4 update. Now it is superset of TypeSystem`$OperatorForms. It now also includes operator forms that have different names than the functions they're currying, like EqualTo. One can find these functions in the documentation by keywords "operator form" Select[Names@"*", StringMatchQ[ToString@ToExpression[# <> "::usage"], "*operator ...


25

Maybe I miss the point here, but FullForm[x ↗ y] gives UpperRightArrow[x,y]. This is described in the documentation to UpperRightArrow and since this symbol is not protected and has not built-in meaning, you can just define it the way you like: UpperRightArrow[x_, y_] := FooBar[x, y] and this instantly gives you Update: As answer to Jacobs ...


23

I find the value of the new operator forms becomes critical when working with datasets. Consider titanic = ExampleData[{"Dataset", "Titanic"}]; titanic[Count[#], "survived"] & /@ {True, False, _Missing} {500, 809, 0} Derive a data set for analyzing the survival of very young passengers. cutoff = 8; youngest = titanic[All, {"age", "survived"}][...


20

There is a global variable that purports to contain a list of operator forms, TypeSystem`$OperatorForms. I say "purports", because the list is missing some of the forms found by @ybeltukov's method. But to its credit, it correctly identifies the operator form of ReplaceAll which is mentioned in neither a usage message nor the documentation, and also ...


19

There is no need to play around with ReplaceAll, Rule, Block, Module or whatever using D, since you have an oparator Derivative really fulfilling your needs while you need not bother if the arguments were defined, so I recommend it to find symbolic derivatives of your function. Remember of shorthands f', f'' to represent first and second derivatives of one-...


19

Actually, // is not a postfix operator, itself; it would be considered an infix operator, akin to + or -. It operates by turning x // f into f[x]. You can string several of them together, e.g. x // f // g which is equivalent to g[f[x]], and think of them as successive transformations. This can be very useful for crafting complex transformations, but it does ...


18

Here's a way to do it: Begin["NonStandardAlgebra`"]; ClearAll /@ {plus, times}; SetAttributes[#, Orderless] & /@ {plus, times}; plus[x : 0 | 1, y : 0 | 1] := Unitize[x + y] plus[Infinity, x : 0 | 1 | Infinity] := Infinity times[0, Infinity] := 1 times[x_, y_] := System`Times[x, y] End[]; A couple of examples: NonStandardAlgebra`times[Infinity, 0] (* ...


18

Perhaps use the full names for the operators: a * b /. Times -> Plus a + b


18

I can't answer how the association is made for the built-in operators, but I can show how to add your own. If your symbol is already an operator you can do this simply as halirutan showed. This question may be a duplicate of How can one define an infix operator with an arbitrary unicode character? but since it admits a simpler interpretation I shall not ...


17

A general idea as to how this can be done in a consistent way is explained in the help documents under NonCommutativeMultiply. The thing is that you want to use your operators in an algebraic notation, and that's what that page discusses. If, on the other hand, you're happy with a more formal Mathematica notation, then you would have the easier task of ...


17

From version 10.3 you can use WolframLanguageData. WolframLanguageData[EntityClass["WolframLanguageSymbol", "Curryable"]] Hope this helps.


17

When using Set rather than SetDelayed you will need to hang the Condition on the left-hand-side: ClearAll[f] f[x_] /; x > 0 = Sqrt[x] f[2] f[-2] Sqrt[x] Sqrt[2] f[-2] There are other reasons to prefer this placement; see: Placement of Condition /; expressions However be aware that the use of Set results in "pre-evaluation" of the RHS which ...


16

x/## & // FullForm Function[Times[x,Power[SlotSequence[1],-1]]] and Power[a,b,c...] == Power[a, Power[b, c...]] so now it should be clear. This syntax is mentioned in the last bullet point in details of Power documentation.


16

The documentation for Minus states that -x is converted to Times[-1,x] on input. So -Sequence[a,b] == Times[-1,Sequence[a,b]] == Times[-1,a,b] by this definition. Similarly the documentation for Divide states that x/y is converted to x y^-1 on input. and therefore x / Sequence[a,b] == x Sequence[a,b]^-1. Sequence[a,b]^c == Power[a, Power[b,c]]. ...


15

Here is the Mathematica proof. I'll leave out the prefactor $\hbar/i$ for simplicity. Also, in case this is a homework problem, I decided not to add too many comments to the code. Instead I'll let you figure it out. The basic idea is to do cross products and gradients in spherical coordinates. The calculation shown here actually gives you a way to calculate ...


15

You can use any built in operator modified with subscripts, superscripts, etc, and retain its precedence, for your own purposes. For example, say you want a general Apply operator like @@ that could work at any level. One could use create the operator @@ with a number subscripted for the level of Apply seems appropriate MakeExpression[RowBox[{fun_, ...


14

While the answer of Szabolcs is clearly the best alternative, if you have already assigned values to the variables and clearing them for some reason is no viable option, you can use Block[{s, L0, L1, a}, Hold@Evaluate@D[L[s, L0, L1, a], s]] Unlike Module, Block doesn't introduce new variable names but temporarily removes the values of those given. Hold ...


14

Distribute @ Sum[-2 Subscript[x, i] (-a Subscript[x, i] - b + Subscript[y, i]) // Expand, {i, n}] == 0


13

If you need to work with a set of variables symbolically, but you also need to substitute in values for them occasionally, a good approach is to use a rule list: values = {a -> 0.04, L1 = 1, L0 -> 1} If the symbols have no values assigned, you can use them normally in symbolic calculations: L[s_, L0_, L1_, a_] := L1 + L0/(1 + s/a) D[L[s, L0, L1, a]...


13

If you are looking for complete answer, take a look at Mr. Wizard's :) Also, see the comments of @JacobAkkerboom below, who proved I was too hasty. :) I was right that the function OP is asking about at the end is Precedence but I was wrong in my interpretation of what is happening. I will leave this for future visitors as it is not so obvious. Also, ...


13

The Notation package is the most convenient way to define new notation(s). <<Notation` Define an infix notation. You can use the palette that the 'Notation` package pops up to do this. InfixNotation[ParsedBoxWrapper["\[UpperRightArrow]"], FooBar] Check that the infix notation maps to the correct FullForm expression. x \[UpperRightArrow] y // ...


13

It's not a bug if you consider this behavior as a logical continuation of the following permissible syntax: D[a + b x^3, x, x, x] (* ==> 6 b *) D[a + b x^3, x, x] (* ==> 6 b x *) D[a + b x^3, x] (* ==> 3 b x^2 *) D[a + b x^3] (* ==> a + b x^3 *) The point is that a Sequence of variables is allowed following the first argument of D. And ...


13

Intermingling Operator Forms & Linguistic Connections This answer attempts to draw out linguistic connections in understanding why operator forms seem so useful, a process that can perhaps point to further utility. Operator Forms are a type of modularization with the standard re-use and combinatory advantages but IMO the biggest benefit is cognitive - ...


13

To my knowledge, there aren't built-in versions for comparison operators that would be automatically threaded over lists. One reason for that is that Mathematica is a symbolic system, and every auto-simplification has a cost, because there may be cases when this isn't desirable. It is relatively easy however to construct the behavior you want: ClearAll[l]; ...


12

Something like this f = D[#, x] + D[#, y] + z # & seems to work. Use as follows: f[x ψ[x, y, z]] to give $x \psi ^{(0,1,0)}(x,y,z)+x \psi ^{(1,0,0)}(x,y,z)+x z \psi (x,y,z)+\psi (x,y,z)$


12

Very nice answers. I wanted to add something else. One typical "Mathematica way" of coding involves overloading a function with several definitions, that do different things according to what arguments are passed (I actually abuse this). You can pattern match by head with things like f[x_Integer]:=... and f[x_Real]:=.... I see the Dataset/Query ...


12

If they changed it, I'm pretty sure it would break some code somewhere, possibly in an obscure and hard to debug way. So it's a good thing that they didn't change it. Why does the one argument behaviour make sense? Take a look at the 4th signature in the documentation: Take[list, seq1, seq2, ...] gives a nested list in which elements specified by ...



Only top voted, non community-wiki answers of a minimum length are eligible