mathematica | IKernelLink

http://reference.wolfram.com/mathematica/NETLink/ref/net/Wolfram.NETLink.IKernelLink.html

using System;
using Wolfram.NETLink;

public class LinkTest {
    public static void Main(String[] args) {
    
        // This launches the Mathematica kernel:
        IKernelLink ml = MathLinkFactory.CreateKernelLink();
        
        // Discard the initial InputNamePacket the kernel will send when launched.
        ml.WaitAndDiscardAnswer();
        
        // Now compute 2+2 in several different ways.
        
        // The easiest way. Send the computation as a string and get the result in a single call:
        string result = ml.EvaluateToOutputForm("2+2", 0);
        Console.WriteLine("2 + 2 = " + result);
        
        // Use Evaluate() instead of EvaluateToXXX() if you want to read the result as a native type
        // instead of a string.
        ml.Evaluate("2+2");
        ml.WaitForAnswer();
        int intResult = ml.GetInteger();
        Console.WriteLine("2 + 2 = " + intResult);
        
        // You can also get down to the metal by using methods from IMathLink:
        ml.PutFunction("EvaluatePacket", 1);
        ml.PutFunction("Plus", 2);
        ml.Put(2);
        ml.Put(2);
        ml.EndPacket();
        ml.WaitForAnswer();
        intResult = ml.GetInteger();
        Console.WriteLine("2 + 2 = " + intResult);
        
        // Always Close when done:
        ml.Close();
    }
}
  1. IKernelLink ml = MathLinkFactory.CreateKernelLink();
  2. ml.WaitAndDiscardAnswer();
  3. Image img = ml.EvaluateToTypeset("HoldForm[x Pi x]", 0);
  4. context.Response.ContentType = "image/gif";
  5. img.Save(context.Response.OutputStream, ImageFormat.Gif);
  6. ml.Close();
Comments