This page contains my notes for Mathematica 6. It’s mostly meant for my reference.
Links
Some useful links first:
- Mathematica tricks
- Supplementary Mathematica Help Browser
- comp . soft-sys . math . mathematica – discussion on Google Groups
Interface
Shortcuts for typing [[ and ]]
Key binding customization
Mathematica\version\\SystemFiles\FrontEnd\TextResources\Windows\KeyEventTranslations.tr
Menu customization
Mathematica\version\\SystemFiles\FrontEnd\TextResources\Windows\MenuSetup.tr Mathematica\version\\SystemFiles\FrontEnd\TextResources\Windows\PopupMenuSetup.tr
Mathematica 6.0.2 problems
Tally bug
Tally
with a user defined test function doesn’t work, as in
r = {{1, "b"}, {2, "a"}, {3, "b"}, {4, "b"}}; Tally[r, SameQ[#1[[2]], #2[[2]]] &] (* Reversing the elements helps *) Tally[r[[All, {2, 1}]], SameQ[#1[[1]], #2[[1]]] &]
produces:
{{{1, "b"}, 1}, {{2, "a"}, 1}, {{3, "b"}, 2}} {{{"b", 1}, 3}, {{"a", 2}, 1}}
Pick problem
Pick
works in a way that is surprising when used, for instance, with Except
pattern. See discussion at comp.soft-sys.math.mathematica. For example
Cases[{1, 0}, Except[0]] Pick[{1, 0}, {1, 0}, Except[0]]
returns
{1} {1, 0}
Adding 2nd argument to Except fixes the problem:
Pick[{1, 0}, {1, 0}, Except[0, 0 | 1]]
gives
{1}
The cause of this is that Pick
matches the whole list {1, 0}
with pattern Except[0]
and uses this to pick the whole list, instead of individual elements. Writing explicitly Except[0, _Integer]
allows matching only with _Integer
and pick has to proceed into level 1.
Code
Using Scan to make assigments
To define f[i] = i^2
for i
in some list (don’t forget the parentheses):
Scan[(f[#] = #^2)&, Range[10]]
Cases versus Select
Cases appears to be faster in cases such as
Select[r, # > 1. &]; Cases[r, x_ /; x > 1.];
Selecting elements of a list of lists
Use for example
Pick[list, list[[All, 2]], x_ /; x > 1];
It is faster then
Select[list, #[[2]] > 1 &];
which is even slower than
Cases[list, {_, x_ /; x > 1}];