3

我正在寻找创建 Mathematica Notebook 文件的原型“Hello World”程序。

我有这个工作程序。

 package graphica;

 import com.wolfram.jlink.*;

 /**
  *
  * @author Nilo
  */
public class MathematicaTester {

public static void main(String[] args) {

    KernelLink ml = null; 
    String jLinkDir = "C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\\SystemFiles\\Links\\JLink";
    System.setProperty("com.wolfram.jlink.libdir", jLinkDir);

    try { 
        ml = MathLinkFactory.createKernelLink("-linkmode launch -linkname 'C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\\MathKernel.exe'");

        ml.discardAnswer();
        String expr = "Sum[k^2,{k,1,11}]";
        ml.evaluate(expr);
        ml.waitForAnswer();
        String x = ml.getString();
        System.out.println("Result = " + x);

    } catch (MathLinkException e) { 
        System.out.println("Fatal error opening link: " + 
        e.getMessage()); 
        return; 
    }
}
}

当我运行它时,我得到以下 -expected- 输出。

运行:
Result = 506
BUILD SUCCESSFUL(总时间:2 秒)

问题:

我想更改这个程序,以便创建 Mathematica Notebook。该程序将(最终)逐行添加 mma 命令字符串。如果同时启动 Mathematica 前端并且 mma 代码由 Java 程序的请求进行评估,那就太好了。本质是创建一个笔记本,以后可以由 mma 前端打开。

4

3 回答 3

6

创建格式化笔记本文件的方法如下所示:

如何创建具有正确格式表达式的笔记本

您可以使用内核调用框格式化 Mathematica 代码(mathCommand),例如

String mathCommand = "Plot[Sin[x], {x, 0, 6}]";
mathCommand = "FullForm[ToBoxes[Defer[" + mathCommand + "]]]";
MathKernel kernel = new MathKernel();
kernel.Compute(mathCommand);
mathCommand = kernel.Result.ToString();

然后像这样封装它,并以 .nb 扩展名保存它。

Notebook[{Cell[BoxData[
... ( inserted box-formatted output ) ...
], "Input"]
},
WindowSize->{615, 750},
WindowMargins->{{328, Automatic}, {Automatic, 76}},
StyleDefinitions->"Default.nb"
]
于 2011-11-07T16:17:05.270 回答
3

Mathematica 笔记本是纯文本文件,其结构如下

Notebook[{Cell[],Cell[]}]

您可以通过使用文本编辑器查看它们来制定所需的结构。假设您可以让 Java 创建一个文本文件,以.nb文件名结尾保存它,并调用 Mathematica 的命令行版本,那么您想要的应该是可行的。您可能希望将输入单元格设置为初始化类型。

于 2011-11-07T15:54:18.160 回答
2

花了一些研究,但我设法自己回答了这个问题。

 package graphica;

 import com.wolfram.jlink.*;

 /**
  *
  * @author Nilo
  */
 public class MathematicaTester {

     public static void main(String[] args) {

         KernelLink ml = null; 
         String jLinkDir = "C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\    \SystemFiles\\Links\\JLink";
         System.setProperty("com.wolfram.jlink.libdir", jLinkDir);

         try { 
             ml = MathLinkFactory.createKernelLink("-linkmode launch -linkname 'C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\\MathKernel.exe'");
        //test-1
             ml.discardAnswer();
             String expr = "Sum[k,{k,1,11}]";
             ml.evaluate(expr);
             ml.waitForAnswer();
             String x = ml.getString();
             System.out.println("Result = " + x);
       //test-2
             expr = "UsingFrontEnd[nb=NotebookPut[Notebook[{Cell[\"Graphics3D[Cuboid[]]\", \"Input\"]}]]]";
             System.out.println("Result = " + ml.evaluateToOutputForm(expr, 40) );
             expr = "UsingFrontEnd[NotebookSave[nb,\"TERRANOVA1\"]]";
             System.out.println("Result = " + ml.evaluateToOutputForm(expr, 40) );

         } catch (MathLinkException e) { 
             System.out.println("Fatal error opening link: " + 
             e.getMessage()); 
             return; 
         }
     }
 }
于 2011-11-26T16:38:58.257 回答