从Mathematica中导出自定义格式化的表达式 [英] Export custom formatted expressions from Mathematica
问题描述
我怎样才能让Mathematica输出/保存/写入一个适当的Fortan77格式的文本文件,即第二列的72列和一个连续的标记?
我正在使用Mathematica来生成大而复杂的分析表达式,然后我需要将其插入到预先存在的Fortran77代码中。在Mathematica的前端,所有的东西都可以正常工作,它们是 FortranForm []
和
SetOptions [$ Output,PageWidth - >不过,我不知道如何让Mathematica正确地输出到文本文件。我想要的是这样的:
$ b $ pre $ code MM11 = mH1 ** 2 +(g2 ** 2 * v1 ** 2)/ 2 。 -
- (g2 ** 2 *(v1 ** 2/2。 -
- ((v2 * Cos(phi2) - (0,1)* v2 * Sin(phi2))* $ (v2 * Cos(phi2)+(0,1)* v2 * Sin(ph2)))/ 2。))/ 2。
...
但是得到这个:
MM11 = FortranForm [mH1 ^ 2 +(g2 ^ 2 * v1 ^ 2)/ 2 - ...
或者这个:
$ p $ MM11 = mH1 ** 2 +(g2 ** 2 * v1 ** 2)/ 2。 - (g2 ** 2 *
(v1 ** 2/2。 - ((v2 * Cos(phi2) - (0,1)* v2 * Sin(phi2))*
...
这是一个令人吃惊的鲜为人知的 Splice
函数首先,创建一个扩展名为。mf
的模板文件,如下所示:
file =test.mf;
out = OpenWrite [file];
WriteString [out,MH1 =< * form *>];
关闭[out];
现在当您使用 Splice
时,Mathematica会自动替换< << $ c>和
*>>
分隔符与它的求值形式所以如果你设置
form = 4 + b9 ^ 2 + c1 ^ 5 + c4 ^ 5 + h10 ^ 4 + j2 + k10 ^ 4 + p10 ^ 4 + q5 ^ 5 +
q8 + s3 ^ 3 + s7 ^ 2 + t6 ^ 3 + u3 ^ 2 + u9 ^ 3 + x8 ^ 4 + z2 ^ 3;
和
Splice [test.mf,PageWidth - > 72];
哪会自动推断你想要 FortranForm
从文件扩展名输出,并允许您设置 PageWidth
作为选项,您将在自动生成的文件test.f
(注意新的扩展名)中获得相当不错的结果:
- z2 ** 3
查看 Splice
以获取更多选项(更改输出文件的名称等)。
How can I get Mathematica to export/save/write a text file with proper Fortan77 formatting, that is, 72 columns and a continuation marker on the sixth column?
I am using Mathematica to generate large and complex analytic expressions, which I then need to insert into pre-existing Fortran77 code. I have everything working correctly in the front end of Mathematica with FortranForm[]
and
SetOptions[$Output, PageWidth -> 72]
However, I can't figure out how to get Mathematica to output correctly to a text file. I want something like this:
MM11 = mH1**2 + (g2**2*v1**2)/2. -
- (g2**2*(v1**2/2. -
- ((v2*Cos(phi2) - (0,1)*v2*Sin(phi2))*
- (v2*Cos(phi2) + (0,1)*v2*Sin(phi2)))/2.))/2.
...
but get either this:
MM11 = FortranForm[mH1^2 + (g2^2*v1^2)/2 - ...
or this:
MM11 = mH1**2 + (g2**2*v1**2)/2. - (g2**2*
(v1**2/2. - ((v2*Cos(phi2) - (0,1)*v2*Sin(phi2))*
...
This is a job for the surprisingly little-known Splice
function. First, you make a template file, with the extension ".mf"
, like so:
file = "test.mf";
out = OpenWrite[file];
WriteString[out, "MH1 = <* form *>"];
Close[out];
Now when you use Splice
, Mathematica will automatically replace everything between the <*
and *>
delimiters with its evaluated form. So if you set
form = 4 + b9^2 + c1^5 + c4^5 + h10^4 + j2 + k10^4 + p10^4 + q5^5 +
q8 + s3^3 + s7^2 + t6^3 + u3^2 + u9^3 + x8^4 + z2^3;
and call
Splice["test.mf", PageWidth -> 72];
which will automatically infer you want FortranForm
output from the file extension, and which allows you to set PageWidth
as an option, you will get a pretty decent result in the automatically generated file "test.f"
(note the new extension):
MH1 = 4 + b9**2 + c1**5 + c4**5 + h10**4 + j2 + k10**4 + p10**4 +
- q5**5 + q8 + s3**3 + s7**2 + t6**3 + u3**2 + u9**3 + x8**4 +
- z2**3
Look at the docs for Splice
for more options (changing the name of the output file and the like).
这篇关于从Mathematica中导出自定义格式化的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!