如何格式化Maxima中的fortran输出

时间:2015-04-21 14:46:17

标签: formatting output indentation maxima

我正在使用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,但它没有打印出任何内容,我也尝试使用fortindentfortspaces,但没有任何变化(我可能会使用错误的语法)

提前致谢。

1 个答案:

答案 0 :(得分:2)

我认为你可以使用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的输出可以接受?

顺便说一句,带有延续字符的固定宽度是打卡时代遗留下来的神器....