Friday, July 11, 2014

Mathematica 10 is making good progress

Mathematica 10 is released a few days ago. It's a quite exciting release. As mentioned in the official website, there are lots of improvements. Among those, here are what I found interesting:

A developing environment

Mathematica Notebook has always been fun to work with. However, when developing a package, I would rather use Intellij or Eclipse. Now it may be time to change my mind, because of a few improvements:

* Multiple undo (and redo). If you have been annoyed about undo in previous versions, go ahead and upgrade.

* The testing framework. This is very important for writing packages. Previously I use Eclipse for unit test. Now this can be done within Mathematica.

* Easier access to function definitions. Hover on a function and there is usage. This is minor but quite useful for unfamiliar functions.

Language features

* Association. This is on the one hand very convenient (something like a struct), and on the other hand really an interesting data structure because both lookup (access) and insertion/deletion is O(log N). Leonid Shifrin wrote an excellent explanation about its usage.

* Dataset. This is built on Association. It's interesting because it is structured data, and the SQL/noSQL operations that can easily be operated on it. I am planning to build up my personal research database with Dataset, and see how it works.

* More convenient functional form. For example, now we can define a function Select[EvenQ], which can act on a list and select even elements. Previously we have to write Select[#, EvenQ]&, which is messy. Composition also gained an operator form (@*), for the same purpose.

* String template (so as file, notebook and xml) is introduced. This helps a lot to create output with clean code.

Algorithm

* Machine learning. The integration of some machine learning algorithms is definitely interesting. One can, for example, predict Facebook topics or language. Inclusion of such definitely makes Mathematica more clever. Neural network is said to be arriving in the next minor release.

* Finite element method in PDE. This would be very helpful for people working a lot with PDEs with nontrivial boundary conditions.

* Geometry. Now lots of calculation, like range of integration, can work on geometry objects.

Cloud Deployment

This is an interesting but expensive feature. It's fun to try but we need to pay for realistic usage. For example, I put GR tensor calculation online, powered by MathGR package. In principle, anybody can now use Mathematica and MathGR to do tensor calculation without having Mathematica and/or MathGR.

https://www.wolframcloud.com/objects/67e0e184-39e0-41fc-bae6-77624a3fe383

However, I only have very limited free credits for running this on cloud, and thus a small number of runs per month (so the above link may stop working if lots of people try it).

Friday, January 10, 2014

Processing org-mode -> latex-beamer -> pdf in the background

I have enjoyed making slides using org-mode greatly. However, it could be very slow. I currently have 30-page slides (but complicated code blocks and frames on them) and it takes me 10 seconds to compile.

Even worse, Emacs is not concurrent. Thus I have to wait those 10 seconds without being able to continue typing. This is annoying.

The fix is actually simple: launch another Emacs process to compile the file. Now everything works in the background in a non-blocking way. The code is very simple. Bind it with a key and that's it.

  (setq wy-obc-eval-str 
 "--eval=(progn (find-file \"%s\") (org-beamer-export-to-pdf) (kill-emacs))")
   
  (defun wy-org-beamer-concurrent ()
    (interactive)
    (save-buffer)
    (let ((eval-str (format wy-obc-eval-str buffer-file-name)))
      (call-process "emacs" nil 0 nil eval-str "--geometry" "60x5")))

PS: legoscia told me that there is emacs-async package (see question), which can free me from launching another emacs explicitly. This may be a better solution but I haven't tried that out.

Thursday, January 9, 2014

Colored code in org-mode export (Minted way)

In the emacs configuration file, add

(setq org-latex-listings 'minted)

And in the org file, add

#+LATEX_HEADER: \usepackage{minted}
#+LATEX_HEADER: \usepackage{color}

Then minted should work (in principle).

Fixes:

(1) I have to replace the pdflatex command to pdflatex --shell-escape to make it work (as I can read from the org code, seems org tries to add --shell-escape automatically. But on my machine it doesn't work out of the box.)

(2) Add support for Mathematica: Here is the plugin:

https://github.com/benjamin-hodgson/pygments-mathematica

It almost works. But there are a few special operators missing: $, `, !. I added them here and made a pull request.

https://github.com/tririver/pygments-mathematica

The looking is considerably better than Listings way:


Wednesday, January 8, 2014

Colored code in org-mode export

To export org-mode code with more style controls, use (note the var name has changed recently and the old one doesn't work any more!):

(setq org-latex-listings t)

One can then tweak the styles. For example, mine for exporting to beamer is:

\definecolor{dkgreen}{rgb}{0,0.5,0}
\definecolor{dkred}{rgb}{0.5,0,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{blue(pigment)}{rgb}{0.2, 0.2, 0.6}
\lstset{
  basicstyle=\ttfamily\bfseries\footnotesize\color{blue(pigment)},
  morekeywords={virtualinvoke},
  keywordstyle=\color{blue}\bf,
  ndkeywordstyle=\color{red},
  commentstyle=\color{dkred},
  stringstyle=\color{dkgreen},
}

\makeatletter
\def\verbatim{\vspace{-0.3cm}\footnotesize\@verbatim \@vobeyspaces \@xverbatim}
\makeatother

Save the above code to a file, and then input this file to the org-beamer export:

#+LATEX: \input{the_above_file.tex}

And This is how it looks like:


Tuesday, January 7, 2014

Evaluate Mathematica code in emacs org-mode

This is yet another attempt of running Mathematica in emacs. For the previous one, see
http://cosmosimple.blogspot.co.uk/2013/12/run-mathematica-commands-in-emacs.html

However, in org-mode, there are greater expectations -- we would like emacs not only to be able to run the Mathematica code, but also "knows" which are code blocks, which are results. Such knowledge enables clever things like calling a Mathematica function from emacs lisp, or other org-supported language. Also, one can easily export the org output to pdf or html, with code and result in the desired styles.

I didn't find org support for Mathematica. Thus I created it myself:

https://github.com/tririver/wy-els/blob/master/ob-mathematica.el

In principle, I could do better by using sessions. However, it's beyond my current need thus I leave it for (possible) future work.

Note that the rather complicated structure of org code blocks can be simplified by yasnippet of emacs.

Example: working on a org-table
(Note that the lines above "#+RESULTS:" are inputs. After pressing C-c C-c inside the SRC block, the result emerges.)

#+NAME: example-table
          | 1 | 4 |
          | 2 | 4 |
          | 3 | 6 |
          | 4 | 8 |
          | 7 | 0 |

#+BEGIN_SRC mathematica :var x=example-table :exports both
1+Transpose@x
#+END_SRC

#+RESULTS:
| 2 | 3 | 4 | 5 | 8 |
| 5 | 5 | 7 | 9 | 1 |

Example: call Mathematica function from org-mode

#+NAME: add-one
#+BEGIN_SRC mathematica :var x=1
  x+1
#+END_SRC

#+RESULTS: add-one
: 2

#+CALL: add-one(x='(1 2 3 4))

#+RESULTS:
| 2 | 3 | 4 | 5 |

Wednesday, January 1, 2014

Change default stylesheet in Mathematica

The whole thing starts when I wanted to change the default font in Mathematica (the default is buggy on my Linux box if ms-fonts are not installed). Later I also modified some other defaults. It's not very trivial thus let me note it down the steps:

(1) Open a notebook, in the menu choose "Format" -> "Edit Stylesheet".

(2) Choose a style to change. For example, "Text", in the menu at the top left.

(3) Modify fonts, etc.

(4) Save this stylesheet to a .nb file. Optionally one can also click Install StyleSheet so that the stylesheet can be found at "Format" -> "Stylesheet..." menu.

(5) Open menu "Format" -> "Option Inspector", make sure "Global Preference" is selected (where the default is "Selection") in the popup menu.

(6) Search for DefaultStyleDefinitions, change it to the saved stylesheet file. Click apply.

Close Mathematica and launch it again. The default font is changed.

Another change I made is added an opener for a group of cells. This can be done at steps (4). Here, search ShowGroupOpener and set it to True, and save. I also changed the color so that it does not look annoying.

Saturday, December 28, 2013

Better copy - paste in LaTeX generated PDF files

Research papers are often read in pdf format, with a lot of equations. One annoying fact is that copying the pdf equation and pasting it elsewhere makes almost no sense.

For example, it would be better to copy equations in the pdf file directly in the LaTeX source form, and then it can be pasted into presentations, using beamer, or via plugins to PowerPoint or LibreOffice.

As another example, as I am updating a manual for Mathematica code, it would be very useful for the user to copy the equations (which are Mathematica code) into Mathematica directly.

The solution is mentioned here and tested to work well.

\usepackage{accsupp}

\begin{equation}
\BeginAccSupp{ActualText={The text to paste}}
The text to display
\EndAccSupp{}
\end{equation}

Here \begin{equation} and \end{equation} are not necessary -- if advanced copy - paste is needed  outside the math environment, this can and should be omitted.

As pointed out by Oberdiek, if the paste text is the LaTex source code (to be used, for example, in making presentations), one can further ease the above process by

\documentclass{article}
\usepackage{accsupp}

\newcommand*{\copyable}[1]{%
  \BeginAccSupp{%
    ActualText=\detokenize{#1},%
    method=escape,
  }%
  #1%
  \EndAccSupp{}%
}

\begin{document}
\[
  \copyable{\int x\sin ax\;\mathrm{d}x = \frac{\sin ax}{a^2}-\frac{x\cos ax}{a}+C}
\]
\end{document}