我正在使用 Maxima(又名 Macsyma)生成一些系数,Maxima 必须在外部文件中写入表达式的 Fortran 表示形式,以便 Python 程序读取。
我使用以下代码来写下系数:
with_stdout ("An.txt",
for n : 0 thru N do
fortran(A[n]));
但不幸的是,它们被完全写在文件 An.txt
中,如下所示:
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
是的,每个条目之前都有一个五(或六)个空格的缩进,并且数字表示表达式的延续。
我有机会获得此内容的更易于阅读的版本吗?完整一行中的每个条目都是理想的。我已经尝试过函数 f90
但它什么也没打印出来,我也尝试过使用 fortindent
和 fortspaces
但没有任何变化(我可能是但使用了错误的语法)。
提前致谢。
最佳答案
我认为您可以在告诉 f90 允许很长的行后使用它。请注意,默认情况下不加载 f90。
(%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
请注意,您必须通过 Lisp 设置行长度;那是一个疣。这是我得到的输出:
$ 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
我看到你的输出中有一个%c
;也许你需要用一些东西来替代它,以使输出可以被 Python 接受?
顺便说一句,带有连续字符的固定宽度是打洞卡时代遗留下来的产物......
关于formatting - 如何在 Maxima 中格式化 Fortran 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29775567/