Tag Info

Hot answers tagged

141

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


82

Avoiding procedural loops People coming from other languages often translate directly from what they are used to into Mathematica. And that usually means lots of nested For loops and things like that. So "say no to loops" and get programming the Mathematica way! See also this excellent answer for some guidance on how Mathematica differs from more ...


72

Basic syntax issues Symbol names cannot contain underscore. _ is a reserved character used for pattern matching. Avoid using subscripted symbols in your code. While it can be done, it causes a lot of confusion and is harder to use than just sym[j] or whatever your symbol might be. The reason is that subscripted symbols are not plain symbols, so you can’t ...


66

Understand that semicolon (;) is not a delimiter Although it may look to newcomers that semicolons are used in Mathematica as statement terminators as in C or Java, or perhaps as statement separators as in Pascal and its derivatives, in fact, semicolons are the infix form of the function CompoundExpression, just as plus-signs (+) are the infix form of the ...


64

Using the result of functions that return replacement rules Most new Mathematica users will at some point encounter the seemingly odd formatting of the output given by functions such as Solve or Root. Let's start with the follwing simple example: Solve[x^2 == 4, x] {{x -> -2}, {x -> 2}} You might find this output strange for two reasons. We'll have ...


61

Understand the difference between Set (or =) and SetDelayed (or :=) A common misconception is that = is always used to define variables (such as x = 1) and := is used to define functions (such as f[x_] := x^2). However, there really is no explicit distinction in Mathematica as to what constitutes a "variable" and what constitutes a "function" — they're both ...


55

Learn how to use the Documentation Center effectively Mathematica comes with the most comprehensive documentation I have ever seen in a software product. This documentation contains reference pages for every Mathematica function tutorials for various topics, which show you step by step how to achieve something guide pages to give you an overview of ...


55

Understand what Set (=) really does Because WRI's tutorials and documentation encourage the use of =, the infix operator version of Set, in a manner that mimics assignment in other programming languages, newcomers to Mathematica are likely to presume that Set is the equivalent of whatever kind of assignment operator they have previously encountered. It is ...


49

Attempting to make an assignment to the argument of a function Quite frequently new users attempt something like this: foo[bar_, new_] := AppendTo[bar, new] x = {1}; foo[x, 2] To be met with: AppendTo::rvalue: {1} is not a variable with a value, so its value cannot be changed. >> Or: f[x_, y_] := (x = x + y; x) a = 1; b = 2; f[a, b] ...


47

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


46

LongestCommonSequencePositions and LongestCommonSubsequencePositions Their use is analogous to LongestCommon(Sub)sequence but they return the position of the first match instead. ClipboardNotebook[] can be used to access the clipboard. NotebookGet@ClipboardNotebook[] will give a Notebook expression with the current contents of the clipboard. I use this ...


46

Assuming commands will have side effects which they don't: Consider: In[97]:= list = {1, 2, 3} Out[97]= {1, 2, 3} In[98]:= Append[list, 4] Out[98]= {1, 2, 3, 4} In[99]:= list Out[99]= {1, 2, 3} When I was first learning Mathematica, I assumed that Append[list, 4] would take the list list and append the element 4 to it, overwriting the previous list. ...


42

Understand the difference between exact and approximate (Real) numbers Unlike many other computational software, Mathematica allows you to deal with exact integers and rational numbers (heads Integer and Rational), as well as normal floating-point (Real) numbers. While you can use both exact and floating-point numbers in a calculation, using exact ...


42

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 bullet points are offered as specific solutions to instances of incompatibility, and not as recommendations of general practice. For example, Plot Themes are a powerful tool ...


41

Lingering Definitions: when calculations go bad One aspect of Mathematica that sometimes confuses new users, and has confused me often enough, is the Lingering Definition Problem. Mathematica diligently accumulates all definitions (functions, variables, etc.) during a session, and they remain in effect in the memory until explicitly cleared/removed. Here's ...


30

Multiple front-end undo is not available in versions less than 10 As the title already claims, in versions less than 10, there is no overall option to undo certain steps in Mathematica files. Nevertheless, inside the boxes one can undo as long as one stays inside. Personal recommendations: 1. Never delete some code except if what you were doing was ...


28

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


27

Use Consistent Naming Conventions This is basic, and good practice in any programming language, but Mathematica's slow-to-fail nature makes it in a sense a less forgiving language than others, so those of us who have in the past gotten away with bad habits may run into trouble. Suppose I have a function loseMemoriesLikeTearsInRain[] which I later try to ...


26

Thinking about a recent answer made me wonder exactly which functions in Mathematica use Assumptions. You can find the list of System` functions that use that Option by running Reap[Do[Quiet[If[Options[Symbol[i], Assumptions]=!={}, Sow[i], Options::optnf]], {i, DeleteCases[Names["System`*"], _?(StringMatchQ[#, "$"~~__] &)]}]][[2, 1]] which (can be ...


26

One undocumented function I find useful is Precedence For example: {#, Precedence@#} & /@ {Plus, Minus, Times, Power, Apply, Map, Factor, Prefix, Postfix, Infix} // TableForm giving: Plus 310. Minus 480. Times 400. Power 590. Apply 620. Map 620. Factor 670. Prefix 640. Postfix 70. Infix 630. Precedence is described in a ...


22

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


21

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


19

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


19

Leaving 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 ranging from massive memory usage to peculiar evaluation leaks.


18

You can try Maxima. It is small, free and closest look alike to Mathematica and install easily in both Windows and Linux. But if you want to strictly impose "which produce equal or even better functionality" condition, I am afraid, I have to withdraw my answer. Honestly speaking, I think there would not be any answer at all. Big List You can find a big ...


18

Sage. It is a great project using a nice and clean programming language (Python). Many parts of its functionality comes from other free software. The usual stuff you are expecting (integration, ODEs, solving equations) is implemented using Maxima. For example for specialised group theory GAP is used.


17

I got a request to post here the undocumented tokens I already posted in an old answer on SO. For completion, I merged my list (which is also in the link provided by @Chris) with @Rojo's list. Later, the list was merged with Vladimir's list below and two more tokens were included, so as to have here a repository of all known FE tokens. Please feel free to ...


17

This method only returns a few of them, hopefully including some undocumented ones. It's not intended to be a complete answer. fnames = FileNames[ "*.nb" | "*.tr", {FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "StyleSheets"}], FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "TextResources"}], ...


16

A complete alternative to Mathematica will be pretty difficult to find. Not quite there but developing at an impressive speed is Sympy. It started out as a small library for python but today it can do quite a lot of symbolic mathematics. The feature list contains from basic arithmetic over calculus, solving differential equations to group theory, geometry ...


16

You may use Tally to finish the task as follows: Cases[Tally[list], {x_, 4} :> x] the result will be {a,b}.



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