Tag Info

Hot answers tagged

38

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 ...


35

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] ...


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 ...


25

One can find these functions in built-in documentation by keywords "an operator form of" Select[Names@"*", StringMatchQ[ToString@ToExpression[# <> "::usage"], "*an operator form of*"] &] {"AllTrue", "AnyTrue", "Append", "Apply", "AssociationMap", "Cases", \ "Count", "CountDistinctBy", "CountsBy", "Delete", "DeleteCases", \ ...


24

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 ...


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

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

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 ...


17

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", ...


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

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 ...


14

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 ...


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, ...


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

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_, ...


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 ...


11

I propose using Interpolation. list = Prime~Array~3000; intf = Interpolation[ {list, Range@Length@list}\[Transpose], InterpolationOrder -> 0 ]; Then, for point x: x = 12225.4; Which[ x < First@list , {-\[Infinity], First@list}, x > Last@list , {Last@list, \[Infinity]}, True , list[[#-1 ;; #]]& @ intf @ ...


11

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, ...


11

From prior comments I know that you are interested in forms such as: a - b - c - d a / b / c / d There is no simple short form for these as there is for Plus. To understand this you must understand how Mathematica parses and displays these expressions. Let's look at the first one: Subtract HoldForm[a - b - c - d] a - b - c - d No surprises. But ...


11

This perhaps: Function[{a, b}, a[#]/b[#] &] @@@ {{a, b}, {c, d}, {e, f}} (* Out: {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *) Mr.Wizard's way of writing it (see comment) looks like this in the frontend:


11

Use upvalues. You don't want || to change its behavior except when it's operating on impedances. So, use a wrapper (z[ ], say) around the quantities that represent impedances, and associate upvalues with the wrapper. This lets you redefine how standard operators work on the wrapped values: z[a_] || z[b_] ^= z[1/(1/a + 1/b)]; z[a_] + z[b_] ^= z[a + b]; a_ ...


11

I don't like the idea of redefining Or (||). Rather, I would suggest defining a function with the name DoubleVerticalBar. There is a special double vertical bar character which will be interpreted as the infix operator for DoubleVerticalBar and can be input with Esc+Space+|+|+Esc. SetAttributes[ DoubleVerticalBar, {NumericFunction, Orderless, Flat, ...


10

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)$


10

The behaviour we see is due to the precedence of &, which is much lower than the precedence of /@. As a consequence, the expression Line /@ (Print[#]; #) is bound tightly together by the high precedence /@ infix operator, yielding the single argument to the low precedence & postfix operator. This means that the second expression is interpreted as ...


10

The definition of the scalar product in your question assumes that all your kets are orthogonal unit vectors. In that case, the most natural approach would be to use the built-in Bra and Ket as follows: Ket /: Dot[Bra[x__], Ket[y__]] := Times @@ MapThread[KroneckerDelta, {{x}, {y}}] BraKet[x_, y_] := Bra[x].Ket[y] Bra[2, 4].Ket[2, 4] (* ==> 1 *) ...



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