从 Mathematica 导出自定义格式的表达式
如何让 Mathematica 导出/保存/写入具有正确 Fortan77 格式的文本文件,即 72 列和第六列上的继续标记?
我正在使用 Mathematica 生成大型且复杂的分析表达式,然后需要将其插入到预先存在的 Fortran77 代码中。我在 Mathematica 前端使用 FortranForm[]
和
SetOptions[$Output, PageWidth ->; 使一切正常工作。 72]
但是,我不知道如何让 Mathematica 正确输出到文本文件。我想要这样的东西:
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.
...
但是要么得到这个:
MM11 = FortranForm[mH1^2 + (g2^2*v1^2)/2 - ...
要么得到这个:
MM11 = mH1**2 + (g2**2*v1**2)/2. - (g2**2*
(v1**2/2. - ((v2*Cos(phi2) - (0,1)*v2*Sin(phi2))*
...
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))*
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是鲜为人知的
Splice
函数的工作。首先,创建一个模板文件,扩展名为".mf"
,如下所示:现在,当您使用
Splice
时,Mathematica 将自动替换< 之间的所有内容;*
和*>
分隔符及其计算形式。因此,如果您设置并调用
它将自动推断您想要从文件扩展名输出
FortranForm
,并且允许您将PageWidth
设置为选项,您将获得相当不错的结果结果自动生成的文件"test.f"
(注意新扩展名):查看文档
Splice
了解更多选项(更改输出文件的名称等)。This is a job for the surprisingly little-known
Splice
function. First, you make a template file, with the extension".mf"
, like so:Now when you use
Splice
, Mathematica will automatically replace everything between the<*
and*>
delimiters with its evaluated form. So if you setand call
which will automatically infer you want
FortranForm
output from the file extension, and which allows you to setPageWidth
as an option, you will get a pretty decent result in the automatically generated file"test.f"
(note the new extension):Look at the docs for
Splice
for more options (changing the name of the output file and the like).