Mathematica Tricks

This note records some useful tricks I learnt in the past years of using Mathematica (MMA). MMA provides a very powerful language system that realizes almost any functionality one would image. However, such a huge library means large learning cost.

Some exmaples are available in my MMA Repository and you can check there for more fun stuffs!

Cracking word puzzles

Recently, when I was playing some word puzzles, I suddenly came to realize that I can take advantages of using programs to look up the dictionary with some given patterns. MMA does support this feature with a built-in function DictionaryLookup[].

First, you can check if it supports a certain language by typing DictionaryLookup[All].

Now, to search for word with specific pattern, e.g., English words starting with ‘a’ and ending with ‘k’ and have only 5 letters, you can just type

1
2
Select[DictionaryLookup[{"English", "a" ~~ ___ ~~ "k"}], 
StringLength[#] == 5 &]

Next, if you want to get a word using given letters and restrict its length to be 4, you could use

1
2
3
4
StringJoin /@ 
Select[Tuples[{"e", "a", "r", "t", "h"}, 4],
DeleteDuplicates[#] == # &&
Length[DictionaryLookup[StringJoin[#]]] != 0 &]

Besides these, this function can be used for a lot fun thins! You can count the number of the words starting with ‘a’ to ‘z’

Length[DictionaryLookup[# ~~ ___]] & /@ CharacterRange["a", "z"]

or find all the palindromic words

DictionaryLookup[x__ /; x === StringReverse[x]]

Reference

Numerical derivative of a list

In MMA, you may easily take partial derivative using D[f(x), x] or even the total derivative Dt[f(x), x]. However, most models I encountered in researches are not analytic so that the functions are usually stored as an array $f(x_i)$, where $x_i$ is an array of discrete points. I’ve searched on stackoverflow, stackExchange and Wolfram Community, but I never found a satisfactory solution. Thus, I have to write another subroutine myself to realize this functionality for the past years. Recently, I came across to the function DerivativeFilter and finally, it works! To take $n$/$m$-th order derivative with respect to $x$/$y$, you just type

DerivativeFilter[array, {n,m}],

where the array should be 2D here. The function applies to any array or even image!

Reference

fftshift in MMA

As I have to deal with wave functions, it is common for me to work between real/momentum spaces, which are connected through Fourier transform. I was so spoiled by fftshift in MatLab so I didn’t know what to do when I cannot find a similar function in MMA. In fact, this can be easily realized through

1
2
3
(*wfx is an array representing the wave function in real space*)
wfk = Fourier[wfx, FourierParameters->1];
wfk = RotateRight[wfx, Floor[(Length@wfk-1)/2]]

Now, the zero momentum component has been shifted to the center! Why this works? Check out DFT and FFT on wiki (or the last two reference if you read Chinese).

Reference

Author

Ryan Hou

Posted on

2016-10-12

Updated on

2023-04-16

Licensed under