2011-11-07 76 views
3

我正在尋找創建Mathematica Notebook文件的原型「Hello World」程序。如何在Java中創建Mathematica Notebook?

我有這個工作計劃。

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-輸出。

運行:
結果= 506
生成成功(總時間:2秒)

問題:

我想,從而創建一個Mathematica筆記本改變這種程序。程序將(最終)在mma命令字符串的後面添加行。如果一個Mathematica前端同時啓動,並且通過Java程序的請求來評估mma代碼,那將會很不錯。基本的是創建一個可以在後期由mma前端打開的Notebook。

+0

我不知道Mathematica,但如果筆記本就像一個項目文件,你必須知道它的結構。如果它是二進制的,那麼你必須對它進行反向工程......官方網站(或任何非官方的網站)是否對此有任何說明? – zeller

+0

@david - Mathematica - Java集成基於J/Link。我瀏覽過文檔,但沒有找到我需要的內容。 - 我想確保我沒有錯過任何東西,或者在我開始黑客攻擊之前可以在這裏獲得HelloWorld。 –

+2

@niloderoock Mathematica筆記本實際上是純文本,因此您可以通過使用文本編輯器查看底層結構來獲取底層結構。我無法幫助你的Java位。 – Verbeia

回答

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; 
     } 
    } 
} 
3

Mathematica筆記本與結構的純文本文件一樣

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

您可以用文本編輯器查看他們的工作進行必要的結構。假設你可以讓Java創建一個文本文件,用.nb文件名結尾保存它,然後調用Mathematica的命令行版本,那麼你想要的應該是可行的。您可能需要將輸入單元格設置爲初始化類型。

6

在此處顯示用於創建一個格式化的筆記本文件的方法:

How to create a notebook with a properly formatted expression

可以框格式使用內核調用您的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" 
] 
+0

看起來很有希望,我會試試這個。 –

+0

你有沒有用過 - 這個?顯然,這個答案不是自成一體的。 –

+0

@ nilo,我已經添加了'MathKernel kernel = new MathKernel();'除此之外,鏈接中的版本在C#上工作。 –