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}

Friday, December 27, 2013

Customizing Input in Mathematica Notebook

Here we customize the Mathematica Notebook frontend by the following example: bind key ctrl + ] into [[]] (which is array label in Mathematica), and let the cursor stay in the middle of the double brackets.

The way is to modify KeyEventTranslations.tr. This file is located at

Linux:
$MATHEMATICA_HOME/SystemFiles/FrontEnd/TextResources/X

Windows:
$MATHEMATICA_HOME/SystemFiles/FrontEnd/TextResources/Windows

Mac OS:
$MATHEMATICA_HOME/SystemFiles/FrontEnd/TextResources/OSX

One can directly modify this file. But the recommended method (at least under Linux) is to copy this file to user directory. For example, in Linux, this file should be copied to (create the directory before copying)

.Mathematica/SystemFiles/FrontEnd/TextResources/X/

The above copying step can be omitted if one prefers to edit directly system-wide.

After this, insert the following code before the final closing "}]" near the end of the file. Note that one may also need to add a comma in front of this code block, to make this entry an element of the whole list.

Item[KeyEvent["[", Modifiers -> {Control}], FrontEndExecute[{
    FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], "[", After],
    FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], "]", Before]
}]]



Friday, December 20, 2013

Some Benchmarks of Mathematica vs Compiled Mathematica vs C vs ...

I was looking into some details of Compiling Mathematica code. And here are some benchmarks.

Summary

As the post is growing in length, it may be helpful to provide a summary here -- we benchmark Mathematica, Mathematica compiled code vs C and a few other languages. The test code is a do-loop with a few triangle function computation in the loop.



Direct benchmark of the following code

(1) The uncompiled Mathematica code; with fFun[10.5, 1*^8]: 2320s
      (This is inferred value. I actually runned 1*^5 times with 2.320s. The normalization is for the convenience of comparing with compiled code.)
fFun = Function[ {x, n},
    Module[ {sum, inc}, sum = 1.0; inc = 1.0; 
    Do[inc = inc*Sin[x]/i; sum = sum + Tan[inc], {i, n}]; sum]];

A note is in order: in this example, inc is finally as small as 10^{-462146}. Mathematica is so clever that it catches underflow of machine precision. Thus this small number is kept (but none of the examples below do this). To be fair, one can set (thank Rojo for pointing this out)

SetSystemOptions["CatchMachineUnderflow" -> False]

After  that, the above code takes 180s to run fFun[10.5, 1*^8].

(2) The code compiled to Mathematica VM; with mFun[10.5, 1*^8]: 6.80s
mFun = Compile[ {{x, _Real}, {n, _Integer}},
    Module[ {sum, inc}, sum = 1.0; inc = 1.0; 
    Do[inc = inc*Sin[x]/i; sum = sum + Tan[inc], {i, n}]; sum]];

(3) The code compiled to C; with cFun[10.5, 1*^8]: 1.04s
cFun = Compile[ {{x, _Real}, {n, _Integer}},
    Module[ {sum, inc}, sum = 1.0; inc = 1.0; 
    Do[inc = inc*Sin[x]/i; sum = sum + Tan[inc], {i, n}]; sum], 
   CompilationOptions -> {"InlineExternalDefinitions" -> True, 
     "InlineCompiledFunctions" -> True, 
     "ExpressionOptimization" -> True}, 
   RuntimeAttributes -> {Listable}, Parallelization -> True, 
   CompilationTarget -> "C"];

(4) The native C code; with -O0 3.14s; with -O3 0.66s
# include <stdio.h>
# include <math.h>
int main(void) {
  double x=10.5;
  double inc=1.0;
  double sum=1.0;
  for(int i = 1; i <= 100000000; i++) {
    inc = inc * sin (x) / (double) i;
    sum = sum + tan(inc);
  }
  printf("%f, %f\n", inc, sum);
}

Parallel performance of code (3)

Here is performance as a function of length of list (in variable x). Considering the turbo boost feature of the CPU, the code can be considered well parallelized.

The test is done on a 4800MQ CPU. The CPU has 4 physical cores with HT (thus 8 logical cores). It is interesting (and surprising) to see that the evolution time starts to double after list length 8, instead of 4.


Why the native C code is still faster?

One can have a look at the generated C code from Mathematica (it can be found when setting Compiler`$CCompilerOptions = {"CleanIntermediate" -> False};)

DLLEXPORT int compiledFunction1(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res)
{
mint I0_ 0;
mint I0_ 1;
mint I0_ 3;
mreal R0_ 0;
mreal R0_ 2;
mreal R0_ 3;
mreal R0_ 4;
mreal R0_ 5;
mreal R0_ 6;
R0_ 0 = MArgument_getReal(Args[0]);
I0_ 0 = MArgument_getInteger(Args[1]);
R0_ 2 = R0_ 1;
R0_ 4 = R0_ 1;
I0_ 1 = I0_ 0;
I0_ 3 = I0_ 2;
goto lab15;
lab6:
R0_ 3 = sin(R0_ 0);
R0_ 5 = (mreal) I0_ 3;
R0_ 6 = 1 / R0_5;
R0_ 3 = R0_ 3 * R0_ 6;
R0_ 6 = R0_ 4 * R0_ 3;
R0_ 4 = R0_ 6;
R0_ 6 = tan(R0_ 4);
R0_ 3 = R0_ 2 + R0_ 6;
R0_ 2 = R0_ 3;
lab15:
if( ++I0_3 <= I0_ 1)
{
goto lab6;
}
MArgument_setReal(Res, R0_ 2);
return 0;
}

The for loop is written in a goto way. I think the compiler is confused and don't know how to optimize the loop (perhaps in branch prediction and register optimization) in this case. This is supported in that, if one make the calculation more complicated inside the loop (tested for nesting the sin functions a few times), the performance of the compiled Mathematica code approaches the C code.

Performance drop for external calls

Once there exists external calls to user defined non-compiled functions, the performance of the compiled code drops a lot.

This takes 25.6 seconds to run  1*^8 loops (note: if writing g=Identity, there is no performance drop. Defining g[x_]:=x, 1*^8 loops takes 29.8 seconds.)
g = # &
cFun = Compile[ {{x, _Real}, {n, _Integer}},
    Module[ {sum, inc}, sum = 1.0; inc = 1.0; 
    Do[inc = g[inc]*Sin[x]/i; sum = sum + Tan[inc], {i, n}]; sum], 
   CompilationOptions -> {"InlineExternalDefinitions" -> False, 
     "InlineCompiledFunctions" -> True, 
     "ExpressionOptimization" -> True}, 
   RuntimeAttributes -> {Listable}, Parallelization -> True, 
   CompilationTarget -> "C"];

For this case, one had better turn on "InlineExternalDefinitions" option (useful for pure function g=#&, but not the rule based g[x_]:=x). Then performance returns.

If one insert some Mathematica functions, which has no counter part in C or pure MVM, e.g. a BesselJ, 1*^8 runs take 1187s (again inferred value, and MVM version takes 1193s, uncompiled version takes 8975s). To compare, pure C code with bessel function from GSL takes only 71s. But the comparison is not completely fair, considering that the Mathematica BesselJ is more powerful, in potentially arbitrary precision and complex arguments for both variables.

Nevertheless, in this BesselJ case for performance one had better to write a C code and link it back to Mathematica.

Another lesson from this section is that, if there are complicated external calls, compile to C doesn't make much benefit compared to compile to MVM. But to compile is still useful.

Comparison with other languages

As Urjit suggests, here is the corresponding code run on Maxima:
x : 10.5;
inc : 1.0;
sum : 1.0;
for i thru 100000000 do (
  inc : inc * sin(x) / i,
  sum : sum + tan(inc)
);
print(sum);

To my surprise (in the delight sense), this code (10^8 loops as above) needs 702 seconds to run. This is 1/3 the time for the Mathematica non-compiled code.

Note that Both Maxima and Mathematica are symbolic systems, which are not primarily designed for numerical calculation. Thus the comparison cannot be over-interpreted as a benchmark of Mathematica vs Maxima :)

For the same purpose, Python is tested -- I believe the math software Sage should have similar performance. The following takes 59.6 seconds:
import math
import time
x = 10.5
inc = 1.0
sum = 1.0
for i in range(1,100000000):
    inc = inc * math.sin(x) / float(i)
    sum = sum + math.tan(inc)

time.clock()

Just for fun, I did the same test on emacs lisp. Interestingly, the performance is much better than I had expected (considering elisp is never designed to do numerical calculation as such). Here is the code. It takes 26s to run -- the fasted among the tested interpretation based languages:

(let ((x 10.5) (inc 1.0) (sum 1.0) (i 1.0))
  (insert (format-time-string "%s"))
  (insert "\n")
  (while (< i 10000001) 
    (setq inc (/ (* inc (sin x)) i))
    (setq sum (+ sum (tan inc)))
    (incf i))
  (insert (format-time-string "%s"))
  sum)

Mathematica packed list vs C code

As another test, we check the performance of Mathematica packed list. We use a different example because I don't know how to use the same technique on the above example (tell me how if you know).

Mathematica code: 0.73s

AbsoluteTiming@Total@Sin@Range[1.0, 10^8]

C code (single thread, gcc -O3): 3.36s

#include 
#include 
int main(){
  int i;
  double sum;
  for(i=1; i<=100000000; i++){
    sum += sin(i);
  }
  printf("%f\n", sum);
}

Tuesday, December 17, 2013

Run mathematica commands in emacs -- updated

Sometime ago I posted how to run Mathematica code in Emacs. Now this is improved -- faster and cleaner version:


(defun wy-rl-beginning () ; "rl" denotes "region or line"
  (if mark-active (region-beginning) (line-beginning-position))) 
(defun wy-rl-end () ; "rl" denotes "region or line"
  (if mark-active (region-end) (line-end-position)))
(defun wy-get-rl ()
  (buffer-substring-no-properties (wy-rl-beginning) (wy-rl-end)) )
(defun wy-remove-comment (s)
  "Remove comment symbol (on the left only)."
  (if (string-match (concat "\\`[ \t\n\r]*" comment-start "+") s)
      (replace-match "" t t s)
    s))
(defun wy-math2 ()
  (interactive)
  (let ((expr) (code) (res))
    (setq expr (wy-remove-comment (wy-get-rl)))
    (setq code (concat "echo 'Print[" expr "]'>/tmp/Meval2.tmp.m;"
      "MathematicaScript -script /tmp/Meval2.tmp.m"))
    (setq res (shell-command-to-string code))
    (kill-new res)
    (princ res)))


A first and naive look at Mathematica 10.0

I am quite curious about what's new in Mathematica 10. Fortunately now some of them are no longer secret. In the last blog post I introduced how to install Mathematica 10 in a virtual machine. Here let me share some findings.

The findings are based on a list of new functions, as pointed out in http://www.walkingrandomly.com/?p=5220.

Sometime in the future there will be an official summary for the new features. But as of writing, the links inside here are not available yet.
http://reference.wolfram.com/search/?q=new%20in%2010.0

The on-going manual for Mathematica 10, or rather they uplift it to the Wolfram Language (interestingly, the non-GUI way to run Mathematica is now no longer "math", but "wolfram"), can be found on Wolfram's official site:
http://reference.wolfram.com/language/

Association Data Structure

Now there is a set of new functions -- Association, Lookup, Keys, Values, KeyExistQ, CountBy, ..., which works with a new type of data structure, similar to dictionary in Python and Map in C++ 11.

Previously, I use function rules to mimic association in Mathematica. This works -- however now there are a whole package of operations acting on associations, either on its values or keys. This is powerful and make the language more expressive. For example,
appleData=<| "name"->"apple","color"->"red","weight"->100gram |> ;
{appleData["name"],appleData["weight"]}
[output] {apple,100 gram}
Associations can be generated by CountBy, GroupBy or PositionIndex (note that SortBy is another meaning. This is confusing). For example,
CountBy[RandomInteger[{0,1},10]]
[output] <|1->6,0->4|> 
PositionIndex[RandomInteger[{0,1},10]]
[output] <|0->{1,3,7,9,10},1->{2,4,5,6,8}|>
This new data structure works well with some previous functions
Map[f,appleData]
[output] <|name->f[apple],color->f[red],weight->f[100 gram]|>
But not for the rest, even other Listable ones.
Sin[appleData]
[output] Sin[<|name->apple,color->red,weight->100 gram|>]
It's easy to convert an association into a list of rules
"name"/.Normal[appleData]
[output] apple
Device Operations

There are lots of new functions added to operate device directly. For example,         Mathematica can now directly read a photo from a camera and operation on the result directly. My virtual machine does not allow me to do such tests though.

Cloud Integration

There are interesting cloud related new functions such as CloudConnect, CloudGet, CloudEvaluate, CloudPut, CloudSave,... The documentation for those functions are not ready though.

Logging into social media, Mathematica can now interact with them directly:
SendMessage["Twitter", "I sent this tweet from Mathematica."]
Or process on a list of Facebook friends
SocialMediaData["Facebook", "Friends"] // Short
Knowledge Based Computing

This may have already existed in earlier versions of Mathematica. However here lots of further convenience are made. For example, concept of Entity is raised and an entity can have different names (CanonicalName, CommonName, etc.) Other examples include FormulaData and FormulaLookup.

There are a few new functions which operates bar code. It is not hard to imagine we scan the bar code of goods in a supermarket and use Mathematica (yes, on arm cpus) to process the data.

Machine Learning

Now one can train Mathematica using existing data, and use the experience for future data sets. For example Classify and Predict. More can be found at
http://reference.wolfram.com/language/guide/MachineLearning.html

trainingData = {1 -> False, 2 -> True, 3 -> False, 4 -> False, 5 -> True, 6 -> True};
cfunc = Classify[trainingData]

cfunc[0] then gives False, which is inferred from the above set.

Activate / Inactive
testSin=Inactive[Sin][π/2]
[output] Sin[π/2] 
Activate[testSin]
[output] 1
I haven't got the point -- isn't it yet another HoldForm / ReleaseHold? If so, why not let them support tags instead of inventing another pair?
However, I highly trust the intellengence of the Mathematica  team believe there should be something that I missed. Here is how to do it the old way:

testCos=HoldForm[Cos][π]
[output] Cos[π] 
ReleaseHold[testCos]
[output] -1
Other Interesting new functions

Lots of functions are added in different areas, e.g.
URLDecode["Hello%2C+World%21"]
[output] Hello, World!
URL functions as such aims to make Mathematica more cloud-ready.

And there are a whole lot of new image processing functions. Also, lots of new functions for computational geometry (http://reference.wolfram.com/language/guide/ComputationalGeometry.html).

In the field of statistics, a number of functions are added to help process time series data, like heart rates, network traffic, etc. Also, now random processes can get transformed using TransformedProcess (similar to TransformedDistribution, which already existed in previous versions).

Among those new functions are also some additional math convenience. For example, FunctionDomain[x/(x^4 - 1), x] gives on which domain is the given function defined. Similarly one can also find the range on y-axis using FunctionRange[func[x], x, y].

There are improvements on matrix processing. For example, a lot of matrix properties added such as AntihermitianMatrixQ, AntisymmetricMatrixQ, DiagonalizableMatrixQ, IndefiniteMatrixQ, NegativeDefiniteMatrixQ, NegativeSemidefiniteMatrixQ, NormalMatrixQ, OrthogonalMatrixQ, PositiveSemidefiniteMatrixQ, SquareMatrixQ, UnitaryMatrixQ.

There is now an easier way to run external commands -- RunProcess. Compared with previous ways, now RunProcess returns an association <| ExitCode->0, Output->... |>. So extracting data from external command is easier then ever! A related new function is StartProcess, which starts an external process, but does not block Mathematica from doing other things. A started process can interact with Mathematica with ReadLine, WriteLine, ProcessStatus, ...

Get a (toy) Mathematica 10 now and for free

I noticed over one month ago that Mathematica is supproting Raspberry Pi (rpi). But What I haven't noticed is that the rpi version of Mathematica is a preview of version 10.0. Otherwise I could have tried the whole thing much earlier.

I don't have a rpi, neither motivation to get one. Nevertheless, it is easy to use an emulator -- qemu -- to emulate the hardware of rpi from a x86 pc. And here is how:

First follow the instructions here

http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/

Then resize the sd, to have more space (I resized to 8GB myself)

http://superuser.com/questions/24838/is-it-possible-to-resize-a-qemu-disk-image

Finally install Mathematica.

It's very annoying that qemu can only run rpi at 640x480. I managed to get it to 800x600 by modifying xorg.conf following this instructions:

http://www.soslug.org/wiki/raspberry_pi_emulation

But higher than 800x600 is not yet luck.

Wednesday, December 11, 2013

Some tests of Linux graphics for Dell Precision M6800

GpuTest 0.6.0 test results, run at 1920x1080. 

TestsIntel 4800MQOptirun K3100MPrimusrun * K3100M
Furmark393 (6 FPS)1492 (24 FPS)1545 (25 FPS)
Pixmark Volplosion289 (4 FPS)632 (10 FPS)641 (10 FPS)
Tessmark12651 (210 FPS)8495 (141 FPS)
Gimark3573 (59 FPS)3967 (66 FPS)
Plot3D3492 (65 FPS)14382 (239 FPS)8396 (139 FPS)

* Here "vblank_mode=0 primusrun command" is used.