private void Start() { Debug.Log("Launch Wolfram Kernel."); if (KernelPath == "") { kernelLink = MathLinkFactory.CreateKernelLink(); } else { kernelLink = MathLinkFactory.CreateKernelLink(new string[] { "-linkmode", "launch", "-linkname", KernelPath + " -wstp" }); } if (kernelLink != null) { kernelLink.WaitAndDiscardAnswer(); if (!TestKernel()) { goto Fail; } else { goto Success; } } Fail: Debug.LogError("Fail to launch Wolfam Kernel."); return; Success: DontDestroyOnLoad(gameObject); return; }
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`\"];"); }
private void startToolStripMenuItem_Click(object sender, EventArgs e) { if (count != 0 || firstIndicator == true) { t.Start(); } else { frmWorking fw2 = new frmWorking(); fw2.Show(); fw2.Refresh(); ml = MathLinkFactory.CreateKernelLink(); ml.WaitAndDiscardAnswer(); //load the prepared package ml.Evaluate(@"<<C:\ChaosWheel\ChaosWheel2.m"); //ml.Evaluate(path); ml.WaitAndDiscardAnswer(); string input = string.Format("ChaosWheel2[{0},{1},{2},{3},{4},{5},{6},{7},{8}]", paras.R, paras.Sigma, paras.Rho, paras.K, paras.QInflow, paras.Qc, paras.Numb, paras.Tlength, paras.Omega); ml.Evaluate(input); // went for 45 seconds on time interval of 10 ml.WaitForAnswer(); datalist = (double[, ])ml.GetArray(typeof(double), 2); numrows = datalist.GetLength(0); numcolumns = datalist.GetLength(1); fw2.Close(); t.Start(); } }
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, }; }
static void getChemistry3dModel(string nameOfModel, string moleculeName) { IKernelLink ml = MathLinkFactory.CreateKernelLink(); ml.WaitAndDiscardAnswer(); //ml.Evaluate("gr1 = Plot3D[-(cos(x) + cos(y) + cos(z))=0, {x, -5, 5}, {y, -5, 5}, Mesh -> None]"); ml.Evaluate("gr1 = ChemicalData[\"Caffeine\", \"MoleculePlot\"]"); ml.WaitAndDiscardAnswer(); //ml.Evaluate("Export[\"3DPlot.obj\", gr1]"); if (!Directory.Exists("C:\\hackNY\\")) { Directory.CreateDirectory("C:\\hackNY\\"); } ml.Evaluate(string.Format("Export[\"C:\\hackNY\\{0}.obj\", gr1]", nameOfModel)); ml.WaitAndDiscardAnswer(); Console.WriteLine("hope it worked"); string keyName = string.Format("C:\\hackNY\\{0}", nameOfModel); string modelWithExtension = nameOfModel + ".obj"; Upload_object(modelWithExtension, "C:\\hackNY\\" + modelWithExtension); Upload_object(modelWithExtension + ".mtl", "C:\\hackNY\\" + modelWithExtension + ".mtl"); }
public static void Main(String[] args) { String[] mathArgs = { @"-linkname", @"C:\Program Files\Wolfram Research\Mathematica\11.0\MathKernel.exe -mathlink" }; Console.WriteLine(mathArgs.ToString()); // This launches the Mathematica kernel: IKernelLink ml = MathLinkFactory.CreateKernelLink(mathArgs); // 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 link when done: ml.Close(); // Wait for user to close window. Console.WriteLine("Press Return to exit..."); Console.Read(); }
public void Initialize() { mathematicaLink = MathLinkFactory. CreateKernelLink(); mathematicaLink.WaitAndDiscardAnswer(); var netPath = Path.Combine( TemporaryPath, "InstrumentRecognizer", "net.wlnet"); if (!File.Exists(netPath)) { Directory.CreateDirectory(Path.GetDirectoryName(netPath)); File.Copy("net.wlnet", netPath, true); } mathematicaLink.Evaluate($"net = Import[\"{netPath.Replace('\\', '/')}\"];"); mathematicaLink.WaitAndDiscardAnswer(); }
public static IUnityContainer CreateContainer() { IKernelLink mathEngine = null; try { MathLinkFactory.CreateKernelLink(); } catch (Exception e) { throw new Exception("WolframMathematica wasn't found on machine.", e); } IUnityContainer container = new UnityContainer() .RegisterInstance(mathEngine) .RegisterType <IMinimizationStrategy, X2MinimizationStrategy>("X2") .RegisterType <IMinimizationStrategy, LeastSquaresMinimizationStrategy>("LeastSquares"); return(container); }
private void CreateKernel() { try { if (KernelArgs is null || KernelArgs.Length == 0) //Optional param KernelArgs default launch settings { KernelArgs = new[] { @"-linkmode", @"launch", @"-linkname", KernelPath }; } Console.WriteLine(Resources.Application_CreateKernel_Creating_Kernel); Kernel = MathLinkFactory.CreateKernelLink(KernelArgs); Kernel.WaitAndDiscardAnswer(); SetupTypeDict(); if (EnableObjectReferences) { Console.WriteLine(Resources.Application_CreateKernel_Enabling_Object_References_); var needNetLinkExpr = new Expr(Wolfram.NETLink.ExpressionType.Function, @"Needs"); needNetLinkExpr = new Expr(needNetLinkExpr, @"NETLink`"); Kernel.Evaluate(needNetLinkExpr); Kernel.WaitAndDiscardAnswer(); Kernel.EnableObjectReferences(); Kernel.WaitAndDiscardAnswer(); Kernel.Evaluate(@"InstallNET[];"); Kernel.WaitAndDiscardAnswer(); if (Kernel.LastError != null) { throw Kernel.LastError; } } DoneInitializing = true; Console.WriteLine(Resources.Application_CreateKernel_Kernel_created); } catch (Exception eq) { throw new CustomException(Resources.Application_CreateKernel_FailException, eq); } }
public static IKernelLink GetKernel() { if (kernelLink == null) { String args = "-linkmode launch -linkname \"" + Environs.FileSystem.Paths["mathPath"] + "\""; if ((String)Environs.Info["mlArgs"] != null) { args = (String)Environs.Info["mlArgs"]; } try { kernelLink = MathLinkFactory.CreateKernelLink(args); kernelLink.WaitAndDiscardAnswer(); } catch (MathLinkException) { Console.WriteLine("Failed to open the link to Mathematica"); } } return(kernelLink); }
public List <Vector> MathDraw(string kappa, string range) { IKernelLink ml = null; List <Vector> outputCurve = new List <Vector>(); try { ml = MathLinkFactory.CreateKernelLink(mlArgs); ml.WaitAndDiscardAnswer(); ml.Evaluate("k[t_]=" + kappa); ml.WaitAndDiscardAnswer(); ml.Evaluate("range = " + range); ml.WaitAndDiscardAnswer(); ml.Evaluate("Needs[\"DrawCurve`\"]"); ml.WaitAndDiscardAnswer(); ml.Evaluate("DrawCurve[k,range]"); ml.WaitForAnswer(); double[,] CurveArray = (double[, ])ml.GetArray(typeof(double), 2); int rows = CurveArray.GetLength(0); int cols = CurveArray.GetLength(1); for (int i = 0; i < rows; i++) { outputCurve.Add(new Vector(CurveArray[i, 0], CurveArray[i, 1], 0)); } } catch (Exception ee) { MessageBox.Show("Error: " + ee.Message); } finally { //tidy everything up ml.Close(); } return(outputCurve); }
private void FrmMain_Load(object sender, EventArgs e) { SetupTransform(); //receive user input // some initial values paras.R = 7; paras.QInflow = 10; paras.Qc = 10; paras.Sigma = 10; paras.Rho = 28; paras.Numb = 8; paras.Tlength = 5; paras.K = 1; paras.Omega = 0.1f; frmParameterDialog pd = new frmParameterDialog(); pd.Text = "Enter the parameter values for the system"; // just some default values pd.Data.R_ = paras.R; pd.Data.QInflow_ = paras.QInflow; pd.Data.Qc_ = paras.Qc; pd.Data.K_ = paras.K; pd.Data.Sigma_ = paras.Sigma; pd.Data.Rho_ = paras.Rho; pd.Data.Numb_ = paras.Numb; pd.Data.Tlength_ = paras.Tlength; pd.Data.Omega_ = paras.Omega; DialogResult d = pd.ShowDialog(); if (d == DialogResult.OK) { paras.R = pd.Data.R_; paras.QInflow = pd.Data.QInflow_; paras.Qc = pd.Data.Qc_; paras.K = pd.Data.K_; paras.Sigma = pd.Data.Sigma_; paras.Rho = pd.Data.Rho_; paras.Numb = pd.Data.Numb_; paras.Tlength = pd.Data.Tlength_; paras.Omega = pd.Data.Omega_; } // set the initial positions of the buckets for (i = 0; i < paras.Numb; i++) { bucketlist.Add(new Bucket()); } for (j = 0; j < bucketlist.Count; j++) { bucketlist[j].Width = 3.5f * (float)Math.Pow(0.9, paras.Numb - 8); // when there are 8 buckets, looks best if bucket width and height is 3.5 bucketlist[j].Height = 3.5f * (float)Math.Pow(0.9, paras.Numb - 8); // adjust depending on number of buckets according to this formula: width = 3.5*(0.9)^(n-8) bucketlist[j].X = (float)(paras.R * Math.Sin(-theta - j * 2 * Math.PI / bucketlist.Count)); bucketlist[j].Y = (float)(-paras.R * Math.Cos(-theta - j * 2 * Math.PI / bucketlist.Count)); } frmWorking fw1 = new frmWorking(); fw1.Show(); fw1.Refresh(); //System.Threading.Thread.Sleep(100); ml = MathLinkFactory.CreateKernelLink(); ml.WaitAndDiscardAnswer(); //load the prepared package ml.Evaluate(@"<<C:\ChaosWheel\ChaosWheel2.m"); //dont need the result ml.WaitAndDiscardAnswer(); //call method for new theta string input = string.Format("ChaosWheel2[{0},{1},{2},{3},{4},{5},{6},{7},{8}]", paras.R, paras.Sigma, paras.Rho, paras.K, paras.QInflow, paras.Qc, paras.Numb, paras.Tlength, paras.Omega); ml.Evaluate(input); // went for 45 seconds on time interval of 10 ml.WaitForAnswer(); //move the system according to theta //thetalist = (double[])ml.GetArray(typeof(double),1); datalist = (double[, ])ml.GetArray(typeof(double), 2); numrows = datalist.GetLength(0); numcolumns = datalist.GetLength(1); firstIndicator = true; fw1.Close(); }
public static List <Note> NotesFromFile(string[] args) { if (args.Length < 3) { throw new ArgumentException("You need three arguments dawg "); } var path = string.Empty; if (args[0].Length != 0) { path = args[0]; SAMPLE_COUNT = int.Parse(args[1]); LOWER_THRESHOLD = double.Parse(args[2]); } else { path = @"C:\Users\zhadowoflight\Desktop\Studies\COEN_9\y-hack\lotr.wav"; //var path = @"C:\Users\zhadowoflight\Desktop\Studies\COEN_9\y-hack\fureliseshort.wav"; //var path = @"C:\Users\zhadowoflight\Desktop\Studies\COEN_9\y-hack\furelise2.wav"; //var path = @"C:\Users\zhadowoflight\Desktop\Studies\COEN_9\y-hack\furs.wav"; //toats shit //var path = @"C:\Users\zhadowoflight\Desktop\Studies\COEN_9\y-hack\all.wav"; } //double[] arr = { 0, 10, 100, 1000, 10000, 400, 440, 32.703 }; //foreach (double d in arr) //{ // string actual = FrequencyToNote(d).ToString(); //} // This launches the Mathematica kernel: IKernelLink ml = MathLinkFactory.CreateKernelLink(); var myComplexType = new Complex(1, 2); ml.ComplexType = myComplexType.GetType(); ml.WaitAndDiscardAnswer(); // Discard the initial InputNamePacket the kernel will send when launched. var data = GetDataFromWav(ml, path); data = MovingAverageSmooth(data); var rate = GetSamplingRateFromWav(ml, path); List <double[]> MajorFrequencies = new List <double[]> (); for (var ii = 0; ii < data.Length + SAMPLE_COUNT; ii += SAMPLE_COUNT) { var temp_data = new double[SAMPLE_COUNT]; try { Array.Copy(data, ii, temp_data, 0, SAMPLE_COUNT); } catch (ArgumentException) { if (ii + SAMPLE_COUNT < data.Length) { Console.WriteLine("Not supposed to happen!!"); } break; } var fft_data = GetMagnitudeOfComplexArray(GetFourierTransform(ml, temp_data)); fft_data = MovingAverageSmooth(fft_data); MajorFrequencies.Add(GetMajorFrequenciesFromTransform(fft_data, rate)); #if DEBUG // File.WriteAllText(string.Format("test{0}.txt",ii),string.Join("\n",fft_data)); #endif } List <Note[]> MajorNotes = GetMajorNotesFromFrequencies(MajorFrequencies); WriteResults(MajorNotes); WriteResults(MajorFrequencies); var a = MakeMusicSheet(MajorNotes); Console.WriteLine("All done"); MusicSheetGenerator.generateMusicSheet(a); return(a); // 'a' is the most descriptive variable name.... EVER }