Wednesday, January 9, 2013

Python Script for Transposing a Table in LaTeX

Sometimes you gotta try transposing a table in LaTeX, just to see if it looks better. It probably doesn't, but you gotta try. And it's a nasty job to be doing that by hand.

Try this:
(Download the .py file to keep that indentation right!)


#LaTeX table transpose
#Peter Raffensperger
#9 Jan 2012

import numpy

x = r"""
\hline
a & b & c & d & e\\
\hline
f & g & h & i & j \\
k & l & m & n & o \\
"""

x = x.replace(r'\hline', '')
tableRows = x.split(r'\\')
table = None
for i, t in enumerate(tableRows):
if t != '\n':
if table is None:
table = numpy.array(t.split('&'))
else:
table = numpy.vstack((table, numpy.array(t.split('&'))))

tableTrans = table.T.tolist()

tNew = ''
for row in tableTrans:
for col in row:
tNew += col.strip() + ' & '
tNew = tNew[:-2] + r' \\' + '\n'
print tNew

Saturday, January 5, 2013

Archimedes' War Machine


Archimedes entered the darkened room where the sibyl of the Oracle at Delphi sat on her tripod seat. He heard the woman raving as the priests translated the words of the prophetic god Apollo:
...
To wit, every single one.

If you count successive
Letters in Russian prose
Observe the likelihood
Of this one after that
Now then you find indeed
That the law of large sums
Generalises to
Those sequences that are
History dependent
...

"Which great future thinker sayeth these fell things?" Archimedes asked the chief priest.

"One Andrey Andreyevich Markov of Ryazan. But seek you now occulted things, things other than the deep knowledge of statistics, probability and history-dependent sequences?"

"Forsooth, I do seek! I must devise a war machine to defend Syracuse. Summon me one yet born, who knows things of strategy, of intrigue and complexity, and of those black things yet to be named that teach a nave the ways of bloodshed, revenge and violence."

The chief priest's eyes darkened. The smoke rising from the crack in the floor thinned. The sibyl of oracle abruptly stopped her babbling. The translating priests stopped on syllable four of the hexameter, interrupting their poetic description of the foundation of 20th century statistics.
This implies that ...

The smoke, raving, and translations then began anew:
Here you are, a spirit
Of trouble and deceit
Wars are fought on his guile
And nobles fall without
His counsel; So now heed:

I am John Forbes Nash two
The son of John Nash one
A mathematician,
And a man of great vice
But I have what you seek!

Calculate you must, the
Von Neumann-Morgenstern
Utility functions
For each of the players
To know his value for
Each possible outcome!

Then, we define a point
In the space of player
Strategies and vices:
The equilibrium!
At this magical point
No player can ever
Improve his lonely lot
By changing his vile plan
Without another's grace!

Find ye this fabled point
Of assured destruction
And act, fight, now defend!
You can do nothing more
If your enemy knows
What truly is in store

But evil, evil woe!
Beware exam marking!
Bad undergraduates
Always make undue haste
When writing their answers,
Filling reams upon reams
With poorly reasoned crap
And demonic scribbles
... 

Another brave man came to the oracle to make an inquiry about x-ray computed tomography, so the chief priest shuffled Archimedes off on his way. "But how?" inquired Archimedes, "How can I calculate your equilibrium point?" But the sibyl of the oracle silenced the soul of John F. Nash Jr and started the processing of summoning the soul of Professor R. H. T. Bates.