Wednesday, June 16, 2010

hyperbolic function in awk

Since:

sinh(x)=[exp(x)-exp(-x)]/2
cosh(x)=[exp(x)+exp(-x)]/2
tanh(x)=sinh(x)/cosh(x)

Therefore:

/usr/bin/awk '{print (exp($1)-exp(-$1))/2}' in.dat
/usr/bin/awk '{print (exp($1)+exp(-$1))/2}' in.dat
/usr/bin/awk '{print (exp($1)-exp(-$1))/(exp($1)+exp(-$1))}' in.dat

will give sinh, cosh, and tanh for the data inside in.dat.

An example is below, the first column is input, the rest 3 columns are output of sinh, cosh, and tanh using the above awk commands.
-9.0 -4051.5400 4051.5400 -1.0000
-8.0 -1490.4800 1490.4800 -1.0000
-7.0 -548.3160 548.3170 -1.0000
-6.0 -201.7130 201.7160 -1.0000
-5.0 -74.2032 74.2099 -0.9999
-4.0 -27.2899 27.3082 -0.9993
-3.0 -10.0179 10.0677 -0.9951
-1.0 -1.1752 1.5431 -0.7616
-0.6 -0.6367 1.1855 -0.5371
-0.3 -0.3045 1.0453 -0.2913
0.0 0.0000 1.0000 0.0000
0.3 0.3045 1.0453 0.2913
0.6 0.6367 1.1855 0.5371
1.0 1.1752 1.5431 0.7616
2.0 3.6269 3.7622 0.9640
3.0 10.0179 10.0677 0.9951
4.0 27.2899 27.3082 0.9993
5.0 74.2032 74.2099 0.9999
6.0 201.7130 201.7160 1.0000
7.0 548.3160 548.3170 1.0000
8.0 1490.4800 1490.4800 1.0000
9.0 4051.5400 4051.5400 1.0000

Friday, June 11, 2010

Multiple pages in one postscript file

The following is one postscript file with 3 pages. (I don't know why page 4 doesn't show up).
And the file doesn't always give expected result.
--------------------------
%!PS-Adobe-3.0
%%BoundingBox: 0 0 600 792
%%Page: 1 1
newpath
144 72 moveto
144 232 lineto
stroke
/Times-Roman findfont 30 scalefont setfont
200 200 moveto (page 1)show
showpage
%%Page: 2 2
newpath
144 72 moveto
164 262 lineto
stroke
200 200 moveto (page 2)show
showpage
%%Page: 3 3
newpath
144 72 moveto
184 292 lineto
stroke
200 200 moveto (page 3)show
showpage
%%Page: 4 4
newpath
144 72 moveto
204 322 lineto
stroke
200 200 moveto (page 4)show
showpage
----------------------------------