如何用Java创建Mathematica Notebook? [英] How to create a Mathematica Notebook in Java?
问题描述
我正在寻找创建Mathematica Notebook文件的原型"Hello World"程序.
I am looking for the prototypical 'Hello World' program that creates a Mathematica Notebook file.
我有这个工作程序.
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-输出.
When I run this I get the following -expected- output.
运行:
结果= 506
建立成功(总时间:2秒)
run:
Result = 506
BUILD SUCCESSFUL (total time: 2 seconds)
问题:
我想更改此程序,以便创建Mathematica笔记本.该程序将(最终)在一行mma命令字符串之后添加一行.如果同时启动Mathematica前端,并且可以通过Java程序的请求对mma代码进行评估,那就太好了.至关重要的是创建一个笔记本,以后可以通过mma前端打开.
I want to change this program so that a Mathematica Notebook is created. The program will ( eventually ) add line after line of mma command strings. It would be nice if a Mathematica frontend is started at the same time and that the mma code is evaluated by request from the Java program. Essential is the creation of a Notebook that can be opened later by the mma front-end.
推荐答案
进行了一些研究,但我自己回答了这个问题.
It took some research but I managed to answer the question myself.
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;
}
}
}
这篇关于如何用Java创建Mathematica Notebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!