# Tag Info

31

Between Versions 7 and 8 Hash now gives the hash of a raw sequence of characters when applied to Strings. In past versions the string characters (quotation marks) were included in the calculation of the hash. (Reference) Use "\"" <> string <> "\"" before hashing if you want output to match older versions. \[Dash], \[LongDash] and ...

29

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

17

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

17

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

14

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

12

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

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

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

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

9

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

9

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

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

8

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

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

One can use Sequence for such purposes Sequence[]~f~x x~f~Sequence[] f[x] f[x] x1~f~Sequence[x2, x3] f[x1, x2, x3]

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

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

8

ArrayFlatten[Outer[Times, mat, Rmat]]

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

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

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

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

Well, it turns out you are doing the computation with low numerical precision. And this error propagates. If you use high enough precision (infinite maybe), the results turns out fine. Also since you're using a series approximation, including more terms also helps. Here it is: Let's define A: A = {{0, 1}, {-1, -3}}; Then Sum[MatrixPower[20 A, s]/s!, {s, ...

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

7

It comes down to the DRY principle: The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." The content management system Wordpress doesn't use object oriented paradigms and so for that reason it looks exactly like your code. Tens of thousands of lines of code like this. ...

7

tree = Function[x, Defer @ FullForm @ x, HoldAll]; Now: 2 + 2 // tree Plus[2, 2] I used Defer to allow the output to be evaluated. If you do not prefer this replace it with HoldForm. For some explanation of the mechanics of this code see: Why doesn't "Defer" work with "TableForm"? See also my standard methods for ...

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