Group Abstract Group Abstract

Message Boards Message Boards

30
|
131K Views
|
22 Replies
|
121 Total Likes
View groups...
Share
Share this post:

[CALL] Most common pitfalls for beginners of Wolfram Language

POSTED BY: Vitaliy Kaurov
22 Replies
Posted 4 years ago

Keyboard combinations and keyboard mistakes. I'm a longtime Mathematica user but am surprised by the recent inadvertent results when trying to add comments to input. The second line below, the red square, is particularly problematic. It appears at the beginning of the input line and will change the properties of what is usually a variable and assignment operator (set). If it occurs with a variable that is subscripted, I have to reboot Mathematica; undo, Clear[], ClearAll[] will not fix whatever that red square does to my subscripted variables. The freeform input symbol shown is easy to search for (orange box with equals sign), but not so with the red box and other mistakes shown....

Wolfram Notebook

POSTED BY: Mark Bourland
Posted 4 years ago
POSTED BY: Pedro Cabral

Don't perform operations on whole arrays by indexing individual elements....

Horrible:
output = ConstantArray[0, Length[data]]; 
For[j = 1, j <= Length[data], j++, output[[j]] = Sin[data[[j]]]];
output

Bad:
Table[Sin[data[[j]]], {j, 1, Length[data]}]

Nice:
Map[Sin, data]

And if the operation is Listable... Best: Sin[data]

POSTED BY: Jon McLoone

If i would get a euro for every time I see horrible/bad approach…

POSTED BY: Sander Huisman

Subscripted variables are a pain...use [i] indexing instead.

POSTED BY: Yaroslav Bulatov

Properties & Relations of constraining a locator

Since the example of constraining a (interactive, or, editable) point or locator to the edge of the unit circle appears over and over again in different yet similar teaching contexts, it can become confusing for the beginner to discern all the coding variations by heart and know which one to use in a given active coding situation. In order to constrain a locator (or point or plot tracker/tracer or alike) key functions can be Locator, LocatorPane, Dynamic, DynamicModule, Manipulate, TrackingFunction, Slider2D, but not all of them at the same time. It depends on what you want and what you start with.

In the end it all boils down to the exploitation of the optional 2nd argument of Dynamic which must be a function (or a list of functions); typically the function is a pure function func[val, expr] processing the "mouse position" (val) and the dynamic expression expr (usually just a single dynamic variable u).

The attached short notebook tries to give a helpful overview of all seven (common) variations for reference and easy comparison.

Also check out the Monday morning quiz testing your beginner's Wolfram L vocabulary!

Attachments:
POSTED BY: Raspi Rascal
Posted 9 years ago

Import and "CurrencyTokens"

Perhaps not 'common' but I was bitten by it twice until I made it 'my' default for data import. I assume for some legacy reason the stripping of currency symbols is the default behavior of the Import[] function. Seems an odd default behavior to me and worse the default behavior is not pointed out as boldly as I think it should be in the documentation.

Import[   , "CurrencyTokens" -> None]

Incorrect Import

Correct Import

POSTED BY: David Proffer
POSTED BY: Neil Singer
POSTED BY: Aeyoss Antelope

Consider Reap/Sow Instead of AppendTo

When using the output of a program to build large lists, a combination of Reap and Sow is often cited as the most efficient (computationally speaking) approach. Put simply, AppendTo trades speed in favour of flexibility by creating a copy of the original list when called. Indeed, Reap/Sow is also point 7 of 10 Tips For Writing Fast Mathematica Code.

I remember first seeing Reap/Sow as a beginner and feeling like it was an 'advanced' feature for other users. Instead I fell into the pattern of defining an empty list and using AppendTo which in hindsight was a beginner's mistake I frequently made by the justification given above.

The example from the link above says it best:

In[7]:= data = {}; 
 Do[AppendTo[data, RandomReal[x]], {x, 0, 40000}]; // AbsoluteTiming

Out[7]= {11.6127, Null}

In[8]:= data = 
   Reap[Do[Sow[RandomReal[x]], {x, 0, 40000}]][[2]]; // AbsoluteTiming

Out[8]= {0.164533, Null}

This is one small change which will Reap large benefits for any beginner.

POSTED BY: Benjamin Goodman

Alternatives to Reap/Sow

1. I would like to mention Table[] in addition. In complicated situations, Reap and Sow maybe be a better alternative, but I've seen quite a bit of beginner's code that looks just like the Do-AppendTo loop above. In that particular case, Table[] is superior both in speed and legibility. It's about 20 times faster than the Reap version:

data = Table[RandomReal[x], {x, 0, 40000}];

(Asides: In some use cases, Map may be an even better choice. Also, many functions are "vectorized" or have special cases for efficiently constructing arrays; for instance, RandomReal[1, 40001] * Range[0., 40000.] produces nearly the same data 150 times faster than Reap, the maximum relative error being less than 10^-12 -- learning to think computationally in terms of arrays can speed up code significantly.)

2. Let me also add linked lists as an alternative (see also this answer by Leonid Shifrin). It is 5-10% faster than Reap & Sow.

data = {};
Do[data = {data, RandomReal[x]}, {x, 0, 40000}];
data = Flatten@data;

If the data is an multidimensional array, then we can structure the flattening operation by using a special head.

Block[{ll},    (* not necessary if  ll  is undefined *)
 data = ll[];
 Do[data = ll[data, RandomReal[x, 2]], {x, 0, 40000}];
 data = List @@ Flatten[data, Infinity, ll];
 ]

Example timing:

SeedRandom[0];    (* for reproducible randomness *)
data = Reap[Do[Sow[RandomReal[x, 2]], {x, 0, 40000}]][[2, 1]]; // AbsoluteTiming
(*  {0.097571, Null}  *)

SeedRandom[0];    (* for reproducible randomness *)
dataLL = ll[];
(Do[dataLL = ll[dataLL, RandomReal[x, 2]], {x, 0, 40000}]; 
   dataLL = List @@ Flatten[dataLL, Infinity, ll]); // AbsoluteTiming
(*  {0.08676, Null}  *)

data == dataLL
(*  True  *)
POSTED BY: Michael Rogers
POSTED BY: Sander Huisman

New in 12.2, ApplyTo x//=f

It's included on the Input Operator Forms webpage.

POSTED BY: Raspi Rascal

Thanks, had added a previous version of ApplyTo, now fixed, and also added [Application]. (https://reference.wolfram.com/language/ref/Application.html)

POSTED BY: Sander Huisman

see "x|->f — new syntax for Function with named variables"

btw your post is the only bookmark I am keeping from this community site.

POSTED BY: Raspi Rascal
POSTED BY: Sander Huisman
POSTED BY: Peter Burbery
Posted 4 years ago

Not sure how you are entering the left and right floor brackets but that is the problem,

\[LeftFloor]5.5\[RightFloor]
(* 5 *)

To enter from the keyboard esclfesc and escrfesc where esc is escape.

POSTED BY: Rohit Namjoshi
POSTED BY: Sander Huisman
POSTED BY: Sander Huisman
POSTED BY: Patrick Scheibe

Basic syntax of built-in functions

  • All built-in functions start from capital letters and are in CamelCase for compound names. Users of many other programming languages might miss this as they are used to different conventions. Examples: Plot, ListPlot, FindSpanningTree, etc.

  • Arguments of a function are inclosed in square brackets. Round parenthesis are used only for ordering of operations. Again, other languages use different conventions. Examples:

    • Cos[Pi] --- is a function with a single argument Pi.
    • BesselJ[1/2, 5] --- is a function with 2 arguments
    • (2-Cos[Pi])Sin[Pi/3] --- round parenthesis are used to order operations
POSTED BY: Marina Shchitova
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard