Showing posts with label matchfix. Show all posts
Showing posts with label matchfix. Show all posts

Saturday, November 24, 2012

Mathematica Keyboard Shortcuts

Keyboard Shortcuts and Handy Short Commands

Here are some useful and lesser-known keyboard shortcuts. In terms of saving time, rather than using brackets for single-argument functions, use Prefix: function@argument.

Likewise, nesting functions with increasingly distal matched brackets is difficult to read, increases programming time, and obstructs you highlighting what you feel is important in the function. Use Postfix-style with pure Functions like this:

dataFiles[[1]]//Rest//Flatten//Length;

dataFilesDifferences//Chop[#,10^-4]&//Union/@#&//ListPlot/@#&//Partition[#,3]&//Grid

As advocated in my 2007 User Conference talk (also here), a "Functional-Procedural Fusion" elegantly represents deeply nested functions in a readable manner. If you do use deeply nested functions, multiple-clicking the Head of an Expression will select its brackets.

Ctrl+F6          Switch between open Mathematica notebooks
F12                Toggle current notebook to and from full screen

Ctrl+Shift+K  After a function name gives you a template for the simplest form of the function
Ctrl+K            After a function name gives a drop-down listbox with autocompletions of the function
?*name*         As in "?*Plot*" lists all Mathematica functions with Plot in their name. You can then click on them to get quick help text.

?Global`*       Lists all Names created in current Mathematica session
F1                  Search Help for expression to left of cursor
Shift + F1      Open new instance of Help browser. Do scratchpad calcs in Help rather than opening a new Notebook. They're automatically cleaned up when you go to a new Help topic.

Alt+.              Interrupt a computation. If it doesn't work, do Evaluation/Quit Kernel.
Shift+Alt+.    Remove a selected computation from the Evaluation queue. This is handy when you have a long computation running, start another one, and want to abort that one without aborting the one that is running.

Rather than using palettes, just learn keyboard shortcuts for the symbols you use frequently, for example:

esc+[character}+esc  E.g. to give the Greek letter corresponding to the keyboard character
Ctrl+6             Superscript
Ctrl+-              Subscript
Alt+7              Format cell as plain text
Alt+6              Format cell as Subsubsection
Alt+5              Format cell as Subsection

%+Out line    As: %127. Refers to output line #127. Same as Out@127 or Out[127] but more concise. I use this frequently for a temporary variable name but if it turns out I need a more permanent mnemonic name I just create one: newName = %127. I use %, %%, or %2 only for very temporary "scratchpad" calcs.

Ctrl+L            Copy input cell from the one just above
Ctrl+Shift+L   Copy output cell from the one just above, into new input cell. An alternative is to just start typing in the Output cell and Mathematica automatically creates a new Input cell with those contents.

Clear@%127  Removes from memory some huge expression you created or imported in In[127].
ClearAll["Global`*"] and Remove["Global`*"] Clears or Removes all values from variables you created in the current session. If it comes to that, I usually kill the kernel though (Evaluation/Quit Kernel).

filePath="C:\\directory1\\directory2"; FileNameSetter@Dynamic@filePath






Gives you a in-Notebook button to click to navigate and select a filename or directory to which to Set filePath. Giving filePath an initial value before evaluating FileNameSetter takes you to that value (e.g. a directory) when you click the button. Using Dynamic lets you click the button over and over to re-Set filePath.

CreateDocument@expression Opens a new Mathematica notebook with expression in it. Expression can be an imported document.

Saturday, November 12, 2011

Introduction to my Mathematica guide: The Way of Mathematica

Greetings! Several years ago I began creating a guide to Mathematica for my own use--as a reference to what I wanted to learn and remember, especially about Mathematica programming. I also wanted to capture a philosophy about Mathematica, like the Tao--as in the Tao Teh Ching--or Way of Mathematica. How does one 'go with the flow' when programming in Mathematica? It evolved into a book and here I'm posting drafts from the book. Please keep in mind these are drafts and may contain errors or omissions. Of course I'm very interested in your comments and corrections.

One device I use that you may not like is capitalizing Mathematica functions, such as Map or Import, as proper nouns. The idea is to highlight them and impress them on our minds. However I can also see that this usage can be irritating--if so let me know.

More significantly, I use a new programming dialect that I first presented at the Wolfram Technology Conference in 2007--you can download the presentation here:


This dialect, which features heavy usage of Prefix (f@x) and Postfix (x//f) syntax with pure Function (x//f [#, {parameters}]&, offers a number of significant advantages over the traditional Matchfix style. Specifically, these benefits include:

  1. Code is more readable
  2. We have less typing because we finally depart from the confusing matching brackets f[[[parameter1, g[parameter2], h[parameter3]]]] syntax that goes at least as far back as Lisp
  3. Functions are listed in the order in which they are applied as in procedural programming
  4. We have more options to organize our code so as to highlight what's important and bury what is not.

We can think of this dialect as a "functional-procedural fusion" and I haven't come up with a better name for it than that. Accordingly another benefit is that the syntax renders Mathematica easier to learn for procedural programmers. Schematically it looks like this:

Traditional Matchfixh [ g [ f [ x, y ], z ], j ]
Postfix-Pure Functionf [ x, y ] // g [ #, z ]& // h [#, j ]&
Prefixf @ g @ h @ i

Using Prefix and Postfix with abbreviated operators (such as +, -, x, /, ^, as in f@x+5) necessitates knowing Mathematica's Precedence table, included in the 2007 presentation and I will post the code and table for Mathematica 8 here. You can always determine the Precedence of any operators using that undocumented function:

In[68]:= Precedence /@ {Plus, Subtract, Times, Divide, Power}

Out[68]= {310., 310., 400., 470., 590.}

And you can always just use Matchfix, or group with parentheses, if you don't want to deal with Precedence. Of course we all deal with Precedence, and few would advise abolishing all abbreviated operator usage, in mathematics and Mathematica. So think of Prefix in Mathematica as a greatly extended usage of the power of abbreviated operators that harks to the origins of math.

http://reference.wolfram.com/mathematica/ref/Prefix.html
http://reference.wolfram.com/mathematica/ref/Postfix.html