Tag Info

Hot answers tagged

19

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


16

I see no mention of the new-in-10 PositionIndex in the other answers, which takes a list (or association) of values and returns a 'reverse lookup' that maps from values in the list to the positions where they occur: In[1]:= index = PositionIndex[{a, b, c, a, c, a}] Out[1]= <|a -> {1, 4, 6}, b -> {2}, c -> {3, 5}|> It doesn't take a level ...


14

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


14

I personally like to use Thread for such things (bounds are e.g. easy to adjust), like: NMaximize[{a + b + c, Thread[{a, b, c} <= {5, 6, 7}]}, {a, b, c}] If it is all the same bound, we can directly write (as in Artes' comment below): NMaximize[{a + b + c, Thread[{a, b, c} <= 5]}, {a, b, c}] I think the syntax should be clear - see also Docu ...


13

I believe it does work, just not how you expect. :-) From the documentation for PutAppend: Note that there are no quotation marks around filename in the first line. It is not made particularly clear but you can use this syntax with >>>: Range[10] >>> file.txt Which outputs to a file named file.txt directly. This is a special and ...


13

You can use String "keys" for indexed variables, as I did for A combination of Set::setraw and Set::shape errors. The strings can have spaces or any other characters you want to use: var["Degree of the First Polynomial"] = (* stuff *); You also have a wide range of characters, many of which can be used in Symbol names. Go to menu Palettes > Special ...


13

Good News Everyone! Two-parameter syntax for Fold and FoldList has been (silently) implemented! Taliesin Beynon informs me that this was implemented in 2011, so check your older versions as well. Using version 10: Fold[f, a] FoldList[f, a] f[f[f[1, 2], 3], 4] {1, f[1, 2], f[f[1, 2], 3], f[f[f[1, 2], 3], 4]} And the held expression example: ...


10

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


10

First off, it's apparent that k needs to take some (positive integer) value, since it's the end-value of the iterator later on. So I add: k = 15; The next bit of the code sets up a recursive function where inttstar[i] depends on periods i-1 and i+1. (This looks a lot like some economic model to be solved.) Notice the inttstar[i_]:= inttstar[i] = (* etc *) ...


9

To prevent your second invocation of SetOptions from resetting the value of sub-option "SymbolContextStyles", you need to set both "System" and "Global" sub-sub-option values at once: SetOptions[EvaluationNotebook[], "AutoStyleOptions" -> {"SymbolContextStyles" -> {"System`" -> RGBColor[211./255, 54./255, ...


8

I can see the source of your confusion: If you use Head[f[x]] and Head[5] you get f and Integer respectively. Then, you read the documentation Apply[f,expr] or f@@expr replaces the head of expr by f. and you expect Cos@@5 to replace the Integer head by Cos. The way I explain it to myself is by saying Mathematica has two (types of) heads ;-) One type is ...


8

Perhaps you want something like this: f[x_] := x Log[x] N[area[1, 2, 100, f]] (* -> 0.6363 *)


8

You could use the Notation package: << Notation` Symbolize[ParsedBoxWrapper[FractionBox["d\[Sigma]", "d\[CapitalOmega]"]]] (d\[Sigma]/d\[CapitalOmega])[\[CapitalEpsilon]_, r_, \[Theta]_] := f[\[CapitalEpsilon], r, \[Theta]]


8

CirclePlus is a built-in symbol already with no meaning for the kernel, but meaning in the front-end. The second definition tried to use the first definition (with head 'Function'), which is protected. (Note the pattern [a_,b_] appearing in the error message, which tells you the left-hand-side is the issue.) Just one line is enough as @rm-rf said, like ...


8

This is in fact tricky. But j is not what it looks. TensorRank[i] gives 2 and its dimensions are {3,1}. j is different: TensorRank[j] gives 1 and its dimensions are {3} instead of {3,1}. A fix. j = {{1,2,3}} and you get i.j {{1, 2, 3}, {2, 4, 6}, {3, 6, 9}} j.i gives {{14}}. The reason it apparently works with j.i is that in this case ...


8

ArrayFlatten[Outer[Times, mat, Rmat]]


8

From the docs for Label Label must appear as an explicit element of a CompoundExpression object. So this works: p[1] = .9; i = 1; Label[begin]; i++; p[i] = p[i - 1] + 1; Print[i]; If[i < 5, Goto[begin], Goto[end]]; Label[end]; But this doesn't: p[1] = .9; i = 1; Label[begin]; i++; p[i] = p[i - 1] + 1; Print[i]; If[i < 5, Goto[begin], ...


8

I'm the one inside the company who suggested RightComposition (and pushed for syntax for Composition and RightComposition). I'm sympathetic to your need, and have wanted the same thing once or twice myself. Given that not much /* and @* code has been written yet, I think it is certainly possible we could have /* parse to LeftComposition. I'm not sure what ...


7

If you do these things a lot you may consider building your own syntax to be able to write constraints in a more concise manner, e.g.: constrAnd[list_, func_] := And @@ (func /@ list) lt[list_,n_] := constrAnd[{a, b, c}, # <= n &] lt[{a, b, c},5] a <= 5 && b <= 5 && c <= 5 So that you may now write NMaximize[{a + b + ...


7

If you want a nicer layout for Range you could try the Notation package: Notation is a bit picky about the definition code of your notation. It has to go manually via its templates. That's why I used a picture above. The following code should work when copied: << Notation` CellPrint@Cell[BoxData[ RowBox[{"InfixNotation", "[", RowBox[{ ...


7

The FE only looks at the structure of your code for colouring. It doesn't evaluate anything. This means two things: (1) it can only guess that there might be a problem, because by looking at the structure, it doesn't know whether your code really evaluates to something you might not want. (2) You can easily trick the FE by changing the structure into ...


7

They return different answers because they're actually different expressions that have the same value, but of course that equality isn't going to be preserved when working with inexact numbers like 4.3. In particular, the first four expressions are all interpreted by Mathematica as Sqrt[(-4.3)^2] while the last expression is interpreted as Sqrt[-4.3]^2 ...


7

The two definitions are not the same... it changes what kind of definition is associated with the symbols. Consider the following: Syntax 1: ClearAll@Foo Foo[x_, y_] := Format[MatrixForm[{{x},{y}}]] DownValues@Foo FormatValues@Foo (* {} *) Syntax 2: ClearAll@Bar Format[Bar[x_, y_]] := MatrixForm[{{x}, {y}}] DownValues@Bar (* {} *) FormatValues@Bar ...


7

Mathematica does not have the concept of row or column vectors like you may be used to. The concept isn't really necessary either and is just a convention to visualize the dot product (although I know there are people that vehemently object to this statement). In dot products like $M\cdot\vec{x}$ and $\vec{x}^{^\top}\cdot M$ Mathematica uses $\vec{x}$ as ...


7

A little bit tricky because it is not supposed to work: Plot[x^2, {x, 0, 3}, ScalingFunctions -> {Identity, "Reverse"}, PlotRange -> {{0, 4}, {-10, 0}}, AxesStyle -> {Arrowheads@.05, Arrowheads[{-.05, 0}]}] How can one find undocumented options


7

s = {x, y} /. Solve[a x + y == 7 && b x - y == 1, {x, y}][[1]] {8/(a + b), -((a - 7 b)/(a + b))} lsa = LinearSolve[{{a, 1}, {b, -1}}, {7, 1}] {8/(a + b), (-a + 7 b)/(a + b)} f = LinearSolve[{{a, 1}, {b, -1}}]; lsb = f[{7, 1}] // Simplify {8/(a + b), -((a - 7 b)/(a + b))} s == lsa == lsb // Simplify True Solve can handle a ...


6

I like to think about @@ as a Frankstein decapitation operator. It take out the Head of the old expression and replace by the new one. And @@@ as a mass Frankstein decapitation operator. It get inside each list element and apply @@ to each element inside the list. To understand what Head means, use FullForm. For example, in the list l={1,2,3} if you apply ...


6

What about this (because NMinimize also accepts a list of boundary conditions): NMaximize[{a + b + c, # <= 5 & /@ {a, b, c}}, {a, b, c}] or (if you are after the very same expression) NMaximize[{a + b + c, And @@ (# <= 5 & /@ {a, b, c})}, {a, b, c}] Both are obviously not more compact as such, but very easily adapted to larger number of ...


6

Sorry, I sometimes write rather opaque code in my own quest for brevity and a certain style. You seem to have a pretty good understanding of my code, though I'm sorry it took as much effort as it did. I believe in Mathematica documentation the term "pure function" is used in the manner than "anonymous function" is used elsewhere. I agree this is confusing ...


6

Your code is far from optimal. Perhaps a tutorial that presents a better implementation would be helpful. Despite my code being very different from yours, it is still a straight-forward implementation of the textbook formula for the trapezoid rule. Because it takes advantage of just a few of Mathematica's built-in functions, it is much more compact than your ...



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