I am using Maxima (aka Macsyma) to generate some coefficients and Maxima has to write the fortran representation of the expression in an external file for a python program to read out of.
I am using the following piece of code to write the coefficients down:
with_stdout ("An.txt",
for n : 0 thru N do
fortran(A[n]));
But unfortunately they get written down in the file An.txt
exactly like this:
exp(beta*t)*(-alpha*(beta**2*t**2+2*beta*t+2)*exp(-beta*t)*D/beta*
1 *3+alpha*(beta*t+1)*exp(-beta*t)*D/beta**2-alpha**2*(beta**4*t*
2 *4+4*beta**3*t**3+12*beta**2*t**2+24*beta*t+24)*exp(-beta*t)/be
3 ta**5+2*alpha**2*(beta**3*t**3+3*beta**2*t**2+6*beta*t+6)*exp(-
4 beta*t)/beta**4-alpha**2*(beta**2*t**2+2*beta*t+2)*exp(-beta*t)
5 /beta**3+%c)
alpha*(t-1)*t
beta
(2*(beta*D+3*alpha*beta*(t-1)*t)+alpha*t+alpha*(t-1))/A_0(t)/6.0E+
1 0
yes, that's with a five(or six)-space indentation before every entry and with the numbers indicating a continuation of expression.
Any chance I can get a easier-to-read version of this? Every entry in one full line would be ideal. I have already tried the function f90
but it prints out nothing and I have also tried to use fortindent
and fortspaces
but nothing changes (I might be using the wrong syntax for them, though).
Thanks in advance.
I think you can use f90 after telling it to allow very long lines. Note that f90 is not loaded by default.
(%i1) load (f90) $
(%i2) :lisp (setq *f90-output-line-length-max* 1000000000)
1000000000
(%i2) A : makelist (expand ((foo + exp(bar*t))^n), n, [5, 10, 20]) $
(%i3) with_stdout ("A.txt", for e in A do f90(e));
(%o3) done
Note that you have to set the line length via Lisp; that's a wart. Here is the output I get:
$ cat A.txt
exp(5*bar*t)+5*foo*exp(4*bar*t)+10*foo**2*exp(3*bar*t)+10*foo**3*exp(2*bar*t)+5*foo**4*exp(bar*t)+foo**5
exp(10*bar*t)+10*foo*exp(9*bar*t)+45*foo**2*exp(8*bar*t)+120*foo**3*exp(7*bar*t)+210*foo**4*exp(6*bar*t)+252*foo**5*exp(5*bar*t)+210*foo**6*exp(4*bar*t)+120*foo**7*exp(3*bar*t)+45*foo**8*exp(2*bar*t)+10*foo**9*exp(bar*t)+foo**10
exp(20*bar*t)+20*foo*exp(19*bar*t)+190*foo**2*exp(18*bar*t)+1140*foo**3*exp(17*bar*t)+4845*foo**4*exp(16*bar*t)+15504*foo**5*exp(15*bar*t)+38760*foo**6*exp(14*bar*t)+77520*foo**7*exp(13*bar*t)+125970*foo**8*exp(12*bar*t)+167960*foo**9*exp(11*bar*t)+184756*foo**10*exp(10*bar*t)+167960*foo**11*exp(9*bar*t)+125970*foo**12*exp(8*bar*t)+77520*foo**13*exp(7*bar*t)+38760*foo**14*exp(6*bar*t)+15504*foo**15*exp(5*bar*t)+4845*foo**16*exp(4*bar*t)+1140*foo**17*exp(3*bar*t)+190*foo**18*exp(2*bar*t)+20*foo**19*exp(bar*t)+foo**20
$ wc A.txt
3 3 856 A.txt
I see you have a %c
in your output; maybe you need to substitute something for that in order to make the output acceptable to Python?
By the way, the fixed width with continuation characters is an artifact left over from the days of punch cards ....