【问题标题】:How to use FORTRAN subroutines or functions in Mathematica?如何在 Mathematica 中使用 FORTRAN 子程序或函数?
【发布时间】:2011-11-18 16:56:16
【问题描述】:

我有兴趣在 Mathematica 会话中调用 fortran 代码。我了解到 Mathlink 提供了一种方法来做到这一点。但是我对C知之甚少,对C++一无所知。 有人愿意给我一个详细的例子吗?

我正在使用 Mathematica 8、MS Visual Studio 2008 和 Intel Fortran 11。系统是 Windows 7 Home Premium。

非常感谢!

【问题讨论】:

  • @celtschk,这是一个非常有趣的链接。谢谢。
  • This can also be useful. 对于 MathLink,您只需要 C,根本不需要 C++,而且 C 很容易学习。但是为了将它与 Fortran 集成,你可能需要学习一些关于调用约定的知识,这会更高级一些(除非你对机器内部有一定的了解)
  • 搜索c and fortran 问题揭示了interesting answer 关于在gcc 下使用fortran 和c 的调用约定。
  • @rcollyer 链接的答案实际上更笼统。 Fortran ISO C 绑定提供了一种从 C 调用 Fortran 的方法,反之亦然,这是 Fortran 2003 语言标准的一部分,因此依赖于编译器和平台。除了 gfortran 和 gcc 之外,许多编译器都支持它。它绕过了必须了解内部调用约定和名称修改的老问题。因此,如果有办法从 Mathematica 调用 C,您可以通过使用 ISO C 绑定来调用 Fortran,以使 Fortran 编译器让您的 Fortran 过程使用 C 调用约定。

标签: wolfram-mathematica fortran mathlink


【解决方案1】:

以下是我在 Windows 系统上成功使用 gfortan 和 gcc 的一个显式示例

我找到了这个博客Adventures in Mathlink。 举一个具体的例子很有帮助。我安装了 MinGW 以使用 gfortran 和 gcc。安装后,必须设置 PATH 才能使用 gfortran 和 gcc,而无需每次都输入路径。不重启系统添加PATH的小技巧:添加PATH后,打开cmd,运行set PATH=C:,然后关闭cmd,再次打开时,使用echo %PATH%,会看到新的路径列表。我按照链接博客中的步骤进行操作,适用于 Windows,教程示例 addtwo:

编写 .bat 文件并运行它以生成可执行文件的 Mathematica 代码

(* Write a .bat file to compile the MathLink template *.tm, FORTRAN codes *.f and 
C codes *.c files, and run it to create an executable file. *)
CreateExeF[s_String] := 
Module[{dir, libdir, bindir, BatCode, bat}, dir = NotebookDirectory[];
{libdir, bindir} = StringJoin[
  "\"", $InstallationDirectory, 
 "\\SystemFiles\\Links\\MathLink\\DeveloperKit\\Windows\\CompilerAdditions\\mldev32\\",
  #] & /@ {"lib\\", "bin\\"};
BatCode = StringJoin[
 "gfortran -c ", #, ".f -o ", #, "f.o
 gcc -c ", #, ".c -o ", #, ".o
 ",
 bindir, "mprep.exe\" ", #, ".tm -o ", #, "tm.c
 gcc -c ", #, "tm.c -o ", #, "tm.o
 gcc ", #, "tm.o ", #, ".o ", #, "f.o ",
 libdir, "ml32i3m.lib\" ", 
 "-lm -lpthread -mwindows -lstdc++ -o ", #
 ] &;
 (* write the .bat file *)
 bat = Export[FileNameJoin[{dir, # <> ".bat"}], BatCode[dir <> #], 
  "string"] &[s];
 (* run the .bat file *)
 Run[bat]]

FORTRAN 代码 addtwo.f

   subroutine addtwof(i,j,k)
   integer i, j, k
   k = i + j
   end

C 包装器 addtwo.c

#include "mathlink.h"

int addtwo(int i, int j) 
{
  int res;
  addtwof_(&i, &j, &res);
  return res;
}

#if WINDOWS_MATHLINK

#if __BORLANDC__
#pragma argsused
#endif

int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine,     int nCmdShow)
{
char  buff[512];
char FAR * buff_start = buff;
char FAR * argv[32];
char FAR * FAR * argv_end = argv + 32;

hinstPrevious = hinstPrevious; /* suppress warning */

if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;
MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
return MLMain( (int)(argv_end - argv), argv);
}

#else

int main(int argc, char* argv[])
{
return MLMain(argc, argv);
}

#endif

模板文件 addtwo.tm 与 Todd Gayley 教程中的模板文件相同。为了完整起见,这里也给出:

:Begin:
:Function:       addtwo
:Pattern:        AddTwo[i_Integer, j_Integer]
:Arguments:      { i, j }
:ArgumentTypes:  { Integer, Integer }
:ReturnType:     Integer
:End:

:Evaluate:       AddTwo::usage = "AddTwo[i, j] gives the sum of two integer numbers i and j."

【讨论】:

  • 您在 Mathematica 中使用 Fortran 的动机是什么?
  • @Mertig,首先非常感谢您为 FeynCalc 付出的巨大努力!具体来说,我需要计算一些衰减幅度,其中包含相当多的四点循环积分。然后我需要绘制一些不变的质量分布和 Dalitz 图。仅使用 Mathematica,这将非常缓慢。但在这种情况下我想要 Mathematica,因为它为我提供了漂亮的图形。你有更好的建议吗?
  • @Mertig ,如果可以将表达式发送到FORTRAN(通过C),然后将结果返回给Mathematica,那就更好了...可行吗?
  • 是的。这是可能的,但涉及。前段时间我对此进行了一些实验。这太复杂了,无法在 Stackoverflow 继续。直接给我发电子邮件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多