## Installing ESS (Emacs Speaks Statistics)

**Problem**

You have a non-standard directory under which you install software. You have installed Emacs under this directory. Now you want to install ESS under this directory as well so that Emacs could interact with R.

**Solution**

Suppose your non-standard directory is

/scratch/username/usr

where username is your actual username. I assume that you have installed both Emacs and R under this directory. Download ESS and extract the downloaded tarball to

/scratch/username/usr/share/emacs/site-lisp

Edit your /home/username/.emacs by inserting the following lines:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ESS: Emacs Speaks Statistics, mainly for R ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (load "/scratch/username/usr/share/emacs/site-lisp/ess-x.y.z/lisp/ess-site") (setq inferior-R-program-name "/scratch/username/usr/bin/R")

Remember to replace “x.y.z” with the actual version number of ESS that you use. Now load Emacs and to use R from Emacs, enter the command

M-x R

This should bring up an R session from within Emacs. Here’s a screenshot for your viewing pleasure:

## Typeset trees using TikZ/PGF

Some combinatorial trees typeset using TikZ/PGF. The Linux filesystem hierarchy:

\begin{tikzpicture} [-,thick] \footnotesize \node {\texttt{/}} [edge from parent fork down] child {node {\texttt{bin}}} child {node {\texttt{etc}}} child {node {\texttt{home}} child {node {\texttt{anne}}} child {node {\texttt{sam}}} child {node {$\dots$}} } child {node {\texttt{lib}}} child {node {\texttt{opt}}} child {node {\texttt{proc}}} child {node {\texttt{tmp}}} child {node {\texttt{usr}} child {node {\texttt{bin}} child {node {\texttt{acyclic}}} child {node {\texttt{diff}}} child {node {\texttt{dot}}} child {node {\texttt{gc}}} child {node {\texttt{neato}}} child {node {$\dots$}} } child {node {\texttt{include}}} child {node {\texttt{local}}} child {node {\texttt{share}}} child {node {\texttt{src}}} child {node {$\dots$}} } child {node {$\dots$}}; \end{tikzpicture}

Classification tree of organisms:

\begin{tikzpicture} [sibling distance=6cm,-,thick] \footnotesize \node {organism} child {node {plant} [sibling distance=2cm] child {node {tree} child {node {deciduous}} child {node {evergreen}} } child {node {flower}} } child {node {animal} [sibling distance=2.5cm] child {node {invertebrate}} child {node {vetebrate} [sibling distance=4.7cm] child {node {bird} [sibling distance=1.5cm] child {node {finch}} child {node {rosella}} child {node {sparrow}} } child {node {mammal} [sibling distance=1.5cm] child {node {dolphin}} child {node {human}} child {node {whale}} } } }; \end{tikzpicture}

The Bernoulli family tree of mathematicians:

\begin{tikzpicture} [-,thick,% every node/.style={shape=rectangle,inner sep=3pt,draw,thick}] \footnotesize \node {Nikolaus senior} [edge from parent fork down] [sibling distance=4cm] child {node {Jacob}} child {node {Nicolaus} child {node {Nicolaus I}} } child {node {Johann} [sibling distance=2cm] child {node {Nicolaus II}} child {node {Daniel}} child {node {Johann II} child {node {Johann III}} child {node {Daniel II}} child {node {Jakob II}} } }; \end{tikzpicture}

An expression tree:

\begin{tikzpicture} [-,thick] \node {$+$} [sibling distance=2.5cm] child {node {$\times$} [sibling distance=1cm] child {node {$a$}} child {node {$a$}} } child {node {$\times$} [sibling distance=1cm] child {node {$2$}} child {node {$a$}} child {node {$b$}} } child {node {$\times$} [sibling distance=1cm] child {node {$b$}} child {node {$b$}} }; \end{tikzpicture}

## Simple graphs, bridges of Konigsberg and directed graphs

Some combinatorial graphs drawn using TikZ/PGF. The seven bridges of Konigsberg:

\begin{tikzpicture} [lineDecorate/.style={-,thick},% nodeDecorate/.style={shape=circle,inner sep=2pt,draw,thick}] %% nodes or vertices \foreach \nodename/\x/\y/\direction/\navigate in { a/0/0/left/west, b/0/2/left/west, c/0/4/left/west, d/4/2/right/east} { \node (\nodename) at (\x,\y) [nodeDecorate] {}; \node [\direction] at (\nodename.\navigate) {\footnotesize$\nodename$}; } %% edges or lines \path \foreach \startnode/\endnode in {a/d, b/d, c/d} { (\startnode) edge[lineDecorate] node {} (\endnode) } \foreach \startnode/\endnode in {a/b, b/c, c/b, b/a} { (\startnode) edge[lineDecorate,bend left] node {} (\endnode) }; \end{tikzpicture}

A house graph:

\begin{tikzpicture} [lineDecorate/.style={-,thick},% nodeDecorate/.style={shape=circle,inner sep=2pt,draw,thick}] %% nodes or vertices \foreach \nodename/\x/\y/\direction/\navigate in { a/0/5/above/north, b/2/3/right/east, e/-2/3/left/west, c/2/0/right/east, d/-2/0/left/west} { \node (\nodename) at (\x,\y) [nodeDecorate] {}; \node [\direction] at (\nodename.\navigate) {\footnotesize$\nodename$}; } %% edges or lines \path \foreach \startnode/\endnode in {a/b, b/c, b/e, c/d, d/e, e/a} { (\startnode) edge[lineDecorate] node {} (\endnode) }; \end{tikzpicture}

\begin{tikzpicture} [nodeDecorate/.style={shape=circle,inner sep=1pt,draw,thick}] %% nodes or vertices \foreach \nodename/\x/\y in {v_1/4/0, v_2/0/0, v_3/0/3, v_4/4/3, v_5/7/1.5} { \node (\nodename) at (\x,\y) [nodeDecorate] {\scriptsize$\nodename$}; } %% edges or lines \tikzstyle{EdgeStyle}=[->,>=stealth,thick] \tikzstyle{LabelStyle}=[fill=white] \foreach \startnode/\endnode/\bend/\weight in { v_1/v_2/bend left/1, v_1/v_3/bend left/3, v_2/v_3/bend left/1, v_2/v_4/bend left/3, v_3/v_1/bend left/1, v_3/v_2/bend left=0/2, v_3/v_4/bend left/1, v_4/v_1/bend left=0/3, v_4/v_5/bend left/2, v_5/v_1/bend left=0/3, v_5/v_1/bend left/6, v_5/v_4/bend left/1} { \scriptsize \Edge[label=$\weight$,style=\bend](\startnode)(\endnode) } \end{tikzpicture}

## UTF-8 and byte order mark

**Problem**

You have a text file with non-ASCII characters that you want to view within a web browser. But the browser jumbles up all those character accents, etc. How do you fix this?

**Solution**

With Vim, you can use bomb. But I just use Bash to prepend a text file with the relevant hex characters:

$ echo -n -e "\0357\0273\0277" | cat - myfile.txt > /tmp/myfile.tmp && mv /tmp/myfile.tmp myfile.txt

I wish it could be as easy and dramatic as letting the Bomberman take care of the above problem ðŸ™‚

## QED symbol for end of proof

The amssymb package defines a default Halmos symbol that automatically appears when you use the proof environment. That default Halmos symbol is . But if you want that to be filled with black, you could redefine the Halmos symbol. Put the following in your preamble:

\renewcommand{\qed}{\hfill \mbox{\raggedright \rule{0.1in}{0.1in}}}

Better still, you could customize what symbol you want to use as your QED symbol. Just replace the macro \rule{0.1in}{0.1in} with the the symbol of your choice.

## Is the Sage command line limiting?

The central issue I want to address in this post is: Does the Sage command line interface, which is text-based, limit what you could do with Sage?

My short answer is, “No”. With careful use and experience, the Sage command line interface can be a powerful tool. Say you want to step through a function to understand how it works. As of Sage 4.6, the Sage command line is the only place where you can interactively step through a calculation, via the function trace(). As another example, say you have a Sage script called

/home/username/myscript.sage

and you want to edit it and load it immediately after each edit. From the command line, do

sage: %edit /home/username/myscript.sage

to load your Sage script, and edit it. After you are done, quit your editor and you should be back at the Sage command line, with your updated script automatically loaded.

Or say you want to interact with your operating system without having to quit Sage. From the Sage command line, do

sage: !sh

and you should be presented with a shell for interacting with your system. When you are done, enter “exit” and you should be back to the Sage command line. From the Sage notebook interface, giving users a shell can open the door to many security issues. The issue becomes clear when you are managing a public Sage notebook server.

Or how do you view the Sage standard documentation from the command line? To view the documentation of function foo(), from the Sage command line do

sage: foo?

To view the source code of function bar(), from the Sage command line do

sage: bar??

To view the Sage tutorial, do

sage: tutorial()

To view the Sage reference manual, do

sage: reference()

How do you find that function you are looking for? Do

sage: search_src("the function name") # or sage: search_doc("the function name") # or sage: search_def("the function name")

## Converting from binary to integer

The following is an updated and edited version of my posts to this sage-support thread.

**Problem**

You have a bitstring as output by

sage.crypto.stream.blum_blum_shub

and you want to convert that bitstring to an integer. Or in general, you want to convert a bit vector to its integer representation.

**Solution**

Here are two ways, assuming that you want the bits in little-endian order, i.e. you read the bits from right to left in increasing order of powers of 2.

sage: version() 'Sage Version 4.5.3, Release Date: 2010-09-04' sage: from sage.crypto.stream import blum_blum_shub sage: b = blum_blum_shub(length=6, lbound=10**4, ubound=10**5); b 100110 sage: type(b) <class 'sage.monoids.string_monoid_element.StringMonoidElement'> sage: # read in little-endian order sage: # conversion using Python's built-in int() sage: int(str(b), base=2) 38 sage: # conversion using Sage's built-in Integer() sage: Integer(str(b), base=2) 38

Now assume you read the bitstring as output by blum_blum_shub in big-endian order, i.e. from left to right in increasing order of powers of 2. You simply convert the bitstring to a string, reverse that string, and apply any of the above two methods.

sage: # reversing a string sage: str(b) '100110' sage: str(b)[::-1] '011001' sage: # read in big-endian order sage: int(str(b)[::-1], base=2) 25 sage: Integer(str(b)[::-1], base=2) 25

Or you can do as follows:

sage: b = "100110" sage: sum(Integer(i) * (2^Integer(e)) for e, i in enumerate(b)) 25 sage: sum(Integer(i) * (2^Integer(e)) for e, i in enumerate(b[::-1])) 38

Another way is to use Horner’s method. Here’s a Sage function that computes the integer representation of a bit vector read using big-endian order. A usage example is also shown.

sage: def horner(A, x0): ... # Evaluate the polynomial P(x) at x = x_0. ... # ... # INPUT ... # ... # - A -- list of coefficients of P where A[i] is the coefficient of ... # x_i. ... # - x0 -- the value x_0 at which to evaluate P(x). ... # ... # OUTPUT ... # ... # An evaluation of P(x) using Horner's method. ... i = len(A) - 1 ... b = A[i] ... i -= 1 ... while i >= 0: ... b = b*x0 + A[i] ... i -= 1 ... return b sage: A = [1, 0, 0, 1, 1, 0] sage: horner(A, 2) 25

As an exercise, modify the function horner to output the integer representation of a bit vector that is read using little-endian order.