### One simple way to do simple floating point arithmetics in shell

#!/bin/tcsh set n = 128 set beta = echo "(-0.6)*($n)*($n)" | bc -l set ppn = 16 set np = 31 set nodes = echo "if ($np%$ppn==0) {$np/$ppn;} else {$np/$ppn+1}" | bc

### Useful Linux command—paste

I have for long ignored blogging, but today I came across the useful linux command “paste” and I thought I should write it down. In the past I have used quite a few smart tricks to handle data or output files, but I failed to document these tricks. I really should do it this time.

So here is the story. I have an output file test.o1551739 which contains lines such as “iter = 125” scattering everywhere. I need to get the sum of all the numbers in these lines. How can I achieve this without writing a program? The following is a little thought process.

First, I am so used to grep that I can grab all such lines by doing:

grep iter test.o1551739

Then, use sed to get rid of the suffix “iter = “:

sed s/"iter = "//

Now that I have a bunch of numbers, one in a line. How can I sum them up? Yah, here comes the usage of paste. The command paste allows me to merge the lines and put a delimiter in between. I chose to use the plus sign, because then it gives me an arithmetic expression. Try this:

paste -sd+

Once I have the arithmetic expression, I’ll just use bc to calculate the result. So piping all these steps, here is the one line command:

grep iter test.o1551739 | sed s/"iter = "// | paste -sd+ | bc

Nice. I realize that there must be tons of ways to do the same job. But I really love my solution.

### The cases environment in latex

The cases environment defined by the amsmath package sets each case to be inline math style. This will look ugly when the formulas in the cases are complicated, such as fractions or integrals. Consider the following sample code:

\usepackage{amsmath} $f(x)= \begin{cases} \frac{1}{2(x-1)}, & x>1 \\ \frac{\Gamma(x)}{2^{x-1}}, & 0<x<1 \end{cases}$ 

The result looks like this:

The solution is to use the dcases environment provided by the mathtools package instead. The prefix d’ means display’. It will set the cases in displayed math style (exactly the same as \frac versus \dfrac). So if we write the following code

\usepackage{amsmath,mathtools} $f(x)= \begin{dcases} \frac{1}{2(x-1)}, & x>1 \\ \frac{\Gamma(x)}{2^{x-1}}, & 0<x<1 \end{dcases}$ 

then we will see the result which looks like this:

### Syracuse PSTricks Page

http://melusine.eu.org/syracuse/pstricks/

Many fantastic PSTricks examples.

### Bocher’s formula

A colleague of mine mentioned to me today the Bocher’s formula for computing the coefficients of the characteristic polynomial of a matrix. It seems that this formula does not appear too often in textbooks or literature. I’ll just write down the formula and the idea of a simple proof here.

Let the characteristic polynomial of a matrix $A$ be

$\displaystyle{p(\lambda)=\lambda^n+a_1\lambda^{n-1}+\cdots+a_n.}$

Then the coefficients can be computed by
$a_1=-tr(A),$
$a_2=-\frac{1}{2}\left(a_1tr(A)+tr(A^2)\right),$
$a_3=-\frac{1}{3}\left(a_2tr(A)+a_1tr(A^2)+tr(A^3)\right),$
$\vdots$
$a_n=-\frac{1}{n}\left(a_{n-1}tr(A)+\cdots+a_1tr(A^{n-1})+tr(A^n)\right).$

To prove the formula, note that the coefficient $a_j$ is the summation of all possible products of j eigenvalues, i.e.,

$\displaystyle{a_j=(-1)^j\sum_{\{t_1\cdots t_j\}\in C_n^j}\lambda_{t_1}\cdots\lambda_{t_j},}$

where $C_n^j$ denotes the j-combination of numbers from 1 to n, and the trace of $A^i$ is the sum of the $j$th power of the eigenvalues, i.e.,

$\displaystyle{tr(A^i)=\sum_{t=1}^n\lambda_t^i.}$

In addition, we have

$\displaystyle{\left(\sum_{\{t_1\cdots t_j\}\in C_n^j}\lambda_{t_1}\cdots\lambda_{t_j}\right)\left(\sum_{t_{j+1}=1}^n\lambda_{t_{j+1}}^i\right)=\sum_{\{t_1\cdots t_{j+1}\}\in C_n^{j+1}}\lambda_{t_1}\cdots\lambda_{t_j}\lambda_{t_{j+1}}^i+\sum_{\{t_1\cdots t_j\}\in C_n^j}\lambda_{t_1}\cdots\lambda_{t_{j-1}}\lambda_{t_j}^{i+1}.}$

The above indicates that the first part of $(-1)^ja_jtr(A^i)$ cancels the second part of $(-1)^{j-1}a_{j-1}tr(A^{i+1})$, whereas the second part of $(-1)^ja_jtr(A^i)$ cancels the first part of $(-1)^{j+1}a_{j+1}tr(A^{i-1}).$ The rest of proof becomes obvious now.

• 246,216 hits