private MathematicaKernel() { string path = ConfigurationManager.AppSettings["MathKernelPath"]; if (string.IsNullOrEmpty(path)) { m_kernel = MathLinkFactory.CreateKernelLink(); } else { string[] args = new string[] { "-linkmode", "launch", "-linkname", "" }; args[3] = "\"" + path + "\""; m_kernel = MathLinkFactory.CreateKernelLink(args); } m_math = new MathKernel(m_kernel); m_math.AutoCloseLink = true; m_math.CaptureGraphics = false; m_math.CaptureMessages = true; m_math.CapturePrint = false; m_math.HandleEvents = true; m_math.Input = null; m_math.LinkArguments = null; m_math.PageWidth = byte.MaxValue; m_math.ResultFormat = MathKernel.ResultFormatType.OutputForm; m_math.UseFrontEnd = true; m_math.Compute("Needs[\"HypothesisTesting`\"];"); }
public WolframLink() { string mlArgs = ""; #if WIN mlArgs = "-linkmode launch -linkname \"C:\\\\Program Files\\\\Wolfram Research\\\\Mathematica\\\\12.0\\\\MathKernel.exe\""; #elif LINUX mlArgs = "-linkmode launch -linkname \"/opt/Wolfram/WolframEngine/12.1/Executables/MathKernel\""; #endif IKernelLink ml = MathLinkFactory.CreateKernelLink(mlArgs); ml.WaitAndDiscardAnswer(); _mathKernel = new MathKernel { AutoCloseLink = true, CaptureGraphics = true, CaptureMessages = true, CapturePrint = true, GraphicsFormat = "Metafile", GraphicsHeight = 0, GraphicsResolution = 80, GraphicsWidth = 0, HandleEvents = true, Input = null, Link = ml, LinkArguments = null, PageWidth = 0, ResultFormat = MathKernel.ResultFormatType.OutputForm, UseFrontEnd = false, }; }
/// <summary> /// Calculates characteristic values of matrices /// (using Wolfram Mathematica). /// </summary> /// <param name="m"></param> /// <returns></returns> private Image GetEigenvalues(Matrix m) { MathKernel mk = new MathKernel(); mk.ResultFormat = MathKernel.ResultFormatType.StandardForm; mk.Compute($"Tally[Round[Eigenvalues[{m.ToWMFormat()}],10^(-10)]]"); Image image = new Image(); image.Source = ToBitmapImage((System.Drawing.Bitmap)mk.Result); return(image); }
public static string CreateNotebook(string mathCommand, string fileLocation, MathKernel kernel) { //Add common functions here; string commonFuncs = "GetScan[index_] := Transpose[{data[index, ScanParams[\"ScanParameterName\"]], data[index, #]}] & /@ DAQmx[\"AINames\"];\n"; commonFuncs += "PlotAll[] := Show[ListPlot[GetScan[#], ImageSize -> 600], ListPlot[GetScan[#], ImageSize -> 600, Joined -> True]] & /@ Range[1, ScanParams[\"NumberOfScans\"]];\n"; mathCommand += commonFuncs; mathCommand = string.Format("{0}{1}{2}", "FullForm[ToBoxes[Defer[", mathCommand, "]]]"); // mathCommand = ComputeMathCommand(mathCommand, kernel); mathCommand = string.Format("{0}{1}{2}", "Notebook[{Cell[\"Import\", \"Section\"], Cell[BoxData[", mathCommand, "], \"Input\"], Cell[\"Analysis\", \"Section\"]}, WindowSize->{615, 750}, WindowMargins->{{328, Automatic}, {Automatic, 76}}]"); File.WriteAllText(fileLocation, mathCommand); kernel.Dispose(); return(fileLocation); }
private static string ComputeMathCommand(string command, MathKernel kernel) { kernel.Compute(command); return(kernel.Result.ToString()); }