290

What the @#%^&*?! do all those funny signs mean? Questions frequently arise about the meaning of the basic operators, and I hope it will prove useful to have a sort of index for them. It would be nice to have them organized by sign instead of topic, but they do not have a natural order. One can use the find/search feature of a browser to locate an ...


166

I. General I will first try to briefly answer the questions, and then illustrate this with a small but practical application. 1.Speed of insertion / deletion Associations are based on so called Hash Array Mapped Trie persistent data structure. One can think of this as a nested hash table, but it is more than that, because it has the following properties: ...


107

If you are new to Mathematica, and were directed to this post, first see if you can use Table to solve your problem. I have often told people, especially beginners, to avoid using For in favour of Do. The following is my personal opinion on why using For is harmful when learning Mathematica. If you are a seasoned Mathematica user, you won't find much to ...


84

User-defined functions, numerical approximation, and NumericQ Frequently there are questions, to which the answer is to use x_?NumericQ, about defining functions that call or sometimes are passed to FindRoot, NIntegrate, NMaximize, NMinimize, FindMaximum, FindMinimum, NDSolve, ParametricNDSolve, FindFit, LinearModelFit, NonlinearModelFit, and so on. ...


74

Prelude The items in this post are not generally regressions; they are simply changes and enhancements that may break code or cause problems in moving from one version to another. The work-arounds are offered as specific solutions to instances of incompatibility, not as recommendations of general practice. For example, Plot Themes are a powerful tool one ...


59

The displayed form may substantially differ from the internal form As soon as you discover replacement rules, you are bound to find that they mysteriously fail to replace subexpressions, or replace subexpressions you didn't expect to be replaced. For example, consider the definition foo = (a+b)(c+d)(e-f)/Sqrt[2] which will cause Mathematica output an ...


57

These three functions are similar (speaking commonly), and in some applications any of them could be used, yet they have very different special applications. Rudimentarily: Map wraps (sub)expressions in a given Head, and returns the modified input Apply replaces Heads in (sub)expressions, and returns the modified input Scan "visits" (sub)expressions, ...


56

As an Eterprise CDF user, I can say I have really tried, and my current opinion is that creating a standalone GUI program with the Wolfram Language is not an easy/commercial/deliverable task at the moment. Here are my points: All the interface controls are very limited. You will have a lot of difficulty to do basic things like make Tab jump between fields, ...


55

Don't leave the Suggestions Bar enabled The predictive interface (Suggestions Bar) is the source of many bugs reported on this site and surely many more that have yet to be reported. I strongly suggest that all new users turn off the Suggestions Bar to avoid unexpected problems such as massive memory usage([1], [2]), peculiar evaluation leaks ([1], [2]), ...


49

I helped design Association, and I designed and implemented Dataset, so I wanted to comment on question 3: Dataset is designed explicitly for hierarchical data. It supports any 'shape' of data, inferring the shape when the Dataset is first created. It also tracks the shape of the data as transformations are applied to the dataset, using a type-inference ...


46

Compose and Composition There is, but it is deprecated (in favor of Composition): Compose: MapThread[Compose, {{a, b, c}, {1, 2, 3}}] (* {a[1], b[2], c[3]} *) I still use Compose myself, but I would not take the responsibility to recommend this as a common practice. You can also use Composition[#1][#2] &, although this is hardly better than your ...


45

Mathematica's own programming model: functions and expressions There are many books about Mathematica programming, still one sees many people falling to understand Mathematica's programming model and usually misunderstand it as functional programming. This is, because one can pass a function as an argument, like plotZeroPi[f_] := Plot[f[x], {x,0,Pi}]; ...


42

The default $HistoryLength causes Mathematica to crash! By default $HistoryLength = Infinity, which is absurd. That ensures Mathematica will crash after making output with graphics or images for a few hours. Besides, who would do something like In[2634]:=Expand[Out[93]].... You can ensure a reasonable default setting by including ($HistoryLength=3), or ...


42

First let me note that I didn't write PositionIndex, so I can't speak to its internals without doing a bit of digging (which at the moment I do not have time to do). I agree performance could be improved in the case where there are many collisions. Let's quantify how bad the situation is, especially since complexity was mentioned! We'll use the ...


39

Using Sort incorrectly Sorting mathematical expressions without numeric conversion New users are often baffled by the behavior of Sort on lists of mathematical expressions. Though this is covered in the documentation of Sort itself they expect that expressions will be ordered by numeric value but they are not. Instead expressions are effectively ordered ...


39

Mathematica can be much more than a scratchpad My impression is that Mathematica is predominately used as a super graphical calculator, or as a programming language and sometimes as a mathematical word processor. Although it is in part all of these things, there is a more powerful usage paradigm for Mathematica. Mathematica stackexchange itself tends to be ...


39

The Wolfram Language is what we all know as Mathematica, but rebranded to help wider adoption to people, particularly for people who don't think of themselves as "math" people. As a Mathematica programmer, emphasis on the "programmer", I see this as a good thing.


38

Why is my picture upside-down? Sometimes, when moving from data-based representations into image-based representations, odd things happen. For example, the left-most leaf in the rose img = ColorConvert[Import["ExampleData/rose.gif"], "grayscale"] points downwards. Yet if we extract the data in the image and plot by another means imgData = ImageData[img]; ...


38

Updated Both Hold and Inactive block evaluation; the key difference is that Inactive is meant to be wrapped around heads rather than a whole expression. Inactivate does this. Inactivate[1 + 2 + 3 * 4 ^ 5 ] // FullForm Inactive[Plus][1, 2, Inactive[Times][3, Inactive[Power][4, 5]]] It is of course possible to use Inactive directly, and it will behave like ...


34

General usage Here is what I think Using strings and subsequently ToString - ToExpression just to generate variable names is pretty much unacceptable, or at the very least should be the last thing you try. I don't know of a single case where this couldn't be replaced with a better solution Using subscripts is also pretty bad and should be avoided, except ...


32

I believe Increment (more accurately PreIncrement as george2079 noted) is essentially this: SetAttributes[inc, HoldFirst] inc[a_] := a = (a + 1) This exhibits the same behavior, e.g. f[] := (Print[#]; #) &@RandomInteger[{1, 3}] v = {0, 0, 0}; inc @ v[[f[]]]; 2 1 Here f[] is evaluated twice because parameter a is used twice within Set. On the ...


31

The general case There are indeed some functions in Mathematica that are/were not performing nicely. The one I am most scared of is Total (the issue is addressed here) (update: apparently Total has been fixed in version 10.0.2). User C.E. provides some more examples in his comment. But I feel the case of Union is different, as it is simply specialised for a ...


30

Illustration of the timings required to compute the squares i^2 from i=1 to i=10^n for n=1, 2, ..., 7 with the use of For, While, Do, Table, and Range. for = Table[ Module[{i}, For[i = 1, i <= 10^n, i++, i^2] // AbsoluteTiming // First ] , {n, 1, 7}] while = Table[ Module[{i}, i = 1; While[i <= 10^n, i^2; i++] // AbsoluteTiming // ...


29

In addition to the previous answer... I designed and implemented Total 17 years ago. Its first version was named ListSum. The primary reason for ListSum's implementation was to encapsulate the functionalities dealing with error accumulation while summing a list of numbers. (A well known phenomena in, say, numerical solvers for ODE's.) Of course, I also ...


28

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. As Naitree notes this is now documented in 10.0.2: 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 ...


28

uh oh, another "work in progress" answer from me that may never be finished. Unevaluated Robby Villegas, Working with Unevaluated Expressions: Unevaluated is not meant to be a function or stable data type. It is to be used as a wrapper on an argument in stage 1, before argument evaluation. It is a signal to the evaluator to suppress the usual ...


27

You can actually Delete the head of the expression, which is part 0: Delete[#, 0] & /@ {Cos[a], Sin[b], Tan[c]} {a, b, c} With version 10 operator forms: Delete[0] /@ {Cos[a], Sin[b], Tan[c]} {a, b, c} One case of interest may be held expressions. If our expression is: expr = HoldComplete[2 + 2]; And the head we wish to remove is Plus, we cannot ...


26

Why do I get an empty plot? Often new Mathematica users (and some not-so-new users) post questions asking why their plot of some expression just shows axes, with no plotted curve appearing. The key thing to keep in mind is that this will almost never have to do with the Plot command itself. It invariably occurs because the expression is not evaluating to a ...


25

Let's start by taking a look at the compiled form of one of our queries: Dataset`CompileQuery[Query @ First @ spans] (* Dataset`WithOverrides@*Checked[Slice[205 ;; 313], Identity] *) We can see that the operation is not implemented directly in terms of part. Indeed, there are three components: Dataset`WithOverrides, GeneralUtilities`Checked and ...


25

This is just a long comment trying to shed light on where the problem may be coming from. Since version 10.2, the following is valid syntax: Table[x, 5] Before we could only use Table[x, {5}] or Table[x, {i, 5}] This implies that we should expect the following to work too: n=5; Table[x, n] the same way as Table[x, {n}] or Table[x, {i, 1, n}] work ...


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