Hot answers tagged

28

As explained by Michael Pilat you cannot create your own compound operators* with custom precedence. (You could conceivably write your own parser as Leonid has worked on, or attempt to coerce the Box form with CellEvaluationFunction.) You can however use an existing operator with the desired precedence. Looking at the table Colon appears to be a good ...


26

Yes you can, with limitations. You have at least three different ways to make an assignment to a subscripted symbol a0 : make a rule for Subscript make a rule for a "symbolize" a0 using the Notation package/palette In each case below, when I write e.g. Subscript[a, 1] this can also be entered as a1 by typing a then Ctrl+_ then 1. When you write: ...


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


21

UpArrow[a_, n_Integer] := Nest[a^# &, 1, n] then UpArrow[4, 3] or 4 \[UpArrow] 3 To complete this method you may wish to add an input alias: AppendTo[CurrentValue[$FrontEndSession, InputAliases], "up" -> "\[UpArrow]"]; Now EscupEsc will enter \[UpArrow]. Change $FrontEndSession to $FrontEnd and run it only once to make the change ...


18

You can get the syntax highlighting that you desire by modifying your UnicodeCharacters.tr file (path given by System`Dump`unicodeCharactersTR), though I don't know how advisable this practice is. For example, adding: 0x20B0 \[PennyOp] ($penny$) Infix 155 None 5 5 I can use EscpennyEsc to enter: I am not aware of ...


18

The short answer is don't do it. Really, it's just not a good idea. You can use other symbols, such as \[CapitalIota] which looks almost exactly like I and is entered with EscIEsc. If you're really determined you could substitute symbols using $PreRead and MakeBoxes but again I don't recommend it. For example: MakeBoxes[I, _] := "\[ImaginaryJ]" ...


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


16

Some people make good use of the Notation package but I have never been very successful with it. A good reason to learn a little about MakeBoxes and TemplateBox. The following MakeBoxes definition will format your expression. MyHead /: MakeBoxes[MyHead[a_, b_], form : (StandardForm | TraditionalForm) : StandardForm] := InterpretationBox[#1, #2] & ...


15

Currying I don't know if it is possible to make all functions work in the Currying form (h[x1][x2][..]) but it is at least possible to extend Hold behavior to all arguments which natively that pattern will not have. I will copy my favorite method which I learned from this post by Grisha Kirilin: SetAttributes[f, HoldAllComplete] f[a_, b_, c_] := Hold[a, ...


14

You can use Symbolize, from the Notation package following the tutorial as you did. Then, just take the precaution of writing the pattern with its head explicit, such as: Pattern[xr, _] The problem is that Mathematica can't interpret the short notation for patterns (xr_ for example) if it has a box structure before the "_"


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

Agree with other answers, this is a bad idea (why, precisely do you want to do this?), but in the spirit of encouraging unmaintainable write-once read-never code, here's my entry into the freak show: $NewSymbol = If[StringMatchQ[#, "f" ~~ NumberString], ToExpression[# <> "[x_]=x+" <> StringDrop[#, 1]]] &; Remove["f*"]; ...


13

You can use the Notation package. It requires a GUI palette though. Needs["Notation`"] Once you have this package loaded, you can use the template to define: Notation[+[x___] ==> Plus[x___]] and then +[1,2,3] (* 6 *) Similarly, Notation[*[x___] ==> Times[x___]] and so *[2,3,4] (* 24 *) Note: A * typed as the first character of a cell ...


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


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


10

Instead of using the Notation package, you can achieve the translation by doing the following: MakeExpression[RowBox[{x_, "⟗", y_}], StandardForm] := MakeExpression[ RowBox[{"FlatJoin", "[", x, ",", y, "]"}], StandardForm ] This takes care of the input translation. Now it's possible to enter expressions like 1 ⟗ (3 + 4 ⟗ 2) and have ...


10

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

Just to be clear, I think this is a terrible idea but nevertheless, a question has been posed for which there is a simple answer: ClearAll@fn SetAttributes[fn, HoldAll] fn[h_[x_]] /; StringMatchQ[SymbolName@h, "f" ~~ DigitCharacter ..] := First@StringCases[SymbolName@h, "f" ~~ d : DigitCharacter .. :> x + ToExpression@d] ...


9

You may wish to use the Notation package. It lets you do these things fairly easily. I'd copy and paste some examples but they don't really copy and paste well. Read through the tutorials and you'll see some examples of how to do this. You may also be interested in the Vector Analysis package.


9

You can use something like this dx /: MakeBoxes[dx[a_], fmt_] := RowBox[{FractionBox["\[PartialD]", "\[PartialD]x"], MakeBoxes[a, fmt], "=", MakeBoxes[#, fmt] &@D[a, x]}]; dx[Sin[x]] dx[Sin[x]] // TraditionalForm I prefer MakeBoxes but it also can be implemented with Format ClearAll[dx] Format[dx[a_]] := ...


9

Maybe this? OverDot[f_, n_Integer] := Derivative[n][f] It really only works for . and \[DoubleDot]. To keep the output from displaying as y'[x], etc., you could define MakeBoxes[Derivative[n_Integer][f_], form_] /; 1 <= n <= 2 := ToBoxes[HoldForm[OverDot[f, n]], form]


9

The Notation package is not necessary to use an infix form of \[Star] as that is handled automatically. Also I recommend PadRight for constructing your expression (reference Generating a matrix using sublists A and B n times). SetAttributes[Star, HoldFirst] Star[a_List, n_Integer] := PadRight[a, n*Length@a, a] {1, 2}⋆5 (* ⋆ is \[Star] *) {1, 2, ...


9

For Plus, there's this, from How would I add together any list of arguments as a pure function?: +Sequence[1, 2, 3] (* 6 *)


8

Perhaps: uu := u[x, y, z, t] uu[x_, y_, z_, t_] := u[x, y, z, t] So that D[uu, t] // InputForm (* - >Derivative[0, 0, 0, 1][u][x, y, z, t] *) and Dt[uu, t] // InputForm (* -> Derivative[0, 0, 0, 1][u][x, y, z, t] + Dt[z, t]*Derivative[0, 0, 1, 0][u][x, y, z, t] + Dt[y, t]*Derivative[0, 1, 0, 0][u][x, y, z, t] + Dt[x, ...


8

What I usually suggest for such cases is to use custom environments, inside which you can change the rules of the game. Here is a lexical one for your case: ClearAll[withNCTimes]; SetAttributes[withNCTimes, HoldAll]; withNCTimes[code_] := Unevaluated[code] /. Times -> NonCommutativeMultiply so that withNCTimes[a*b*c] (* a**b**c *) and here is ...


8

Unfortunately, this is not possible directly, at least as far as I know. The error message you are getting is just a manifestation of it. You are using UpValues, which have a fundamental limitation that they can only be attached to symbols on a level no deeper than 1. I discussed this more in my answer to this question. Redefining built-in functions, OTOH, ...


8

That's how I finally defined haskell operators: rapply[x_] := x rapply[x_, y__] := x[rapply[y]] InfixNotation[ParsedBoxWrapper["|"], rapply] lapply[x_] := x lapply[x__, y_] := lapply[x][y] InfixNotation[ParsedBoxWrapper["\[SmallCircle]"], lapply] InfixNotation[ParsedBoxWrapper["\[CenterDot]"], Composition] Now $\circ$, $\dot{}{}$ and | act exactly like ...


8

I'm not going into the well-coded part of your question (as this is rather subjective), but a package that I've (cursorily) examined and which looks nice is this quantum notation package, which has lots of custom notation and corresponding palettes.


8

Here's a more general variant a(↑...↑)b with any given number of up-arrows, as defined on MathWorld: (* Short-hand for single arrow. *) UpArrow[a_, b_] := UpArrow[1][a, b]; (* Trivial case of a(↑...↑)1. *) UpArrow[_][a_, 1] := a; (* Single arrow: exponentation. *) UpArrow[1][a_, b_] := a^b; (* Generic case: do a recursion. *) UpArrow[n_Integer][a_, ...



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