PolyLink
A library to allow manipulation of geometry from within Mathematica
 All Classes Namespaces Files Functions Variables Properties
MSingle.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using Wolfram.NETLink;
7 
8 namespace PolyLink
9 {
13  public sealed class MSingle
14  {
15  //Singleton code
16  private static readonly Lazy<MSingle> Lazy = new Lazy<MSingle>(() => new MSingle());
17 
21  public static MSingle Instance
22  {
23  get { return Lazy.Value; }
24  }
25 
29  public static IKernelLink Kernel
30  {
31  get { return Instance.KernelLink; }
32  }
33 
37  public IKernelLink KernelLink
38  {
39  get { return _kernelLink; }
40  }
41 
42  //Other stuff
43  private readonly IKernelLink _kernelLink;
44 
45  private MSingle()
46  {
47 
48  if (((int) System.Environment.OSVersion.Platform).EqualsAny(4,6,128))
49  {
50  _kernelLink = MathLinkFactory.CreateKernelLink(
51  "-linkmode launch -linkname '\"/Applications/Mathematica.app/Contents/MacOS/MathKernel\" -mathlink'");
52  }
53  else
54  {
55  _kernelLink = MathLinkFactory.CreateKernelLink();
56  }
57  _kernelLink.WaitAndDiscardAnswer();
58  }
59 
66  public static Expr Eval(String s, params object[] args)
67  {
68  string sf;
69  if (args ==null || !args.Any())
70  {
71  sf = s;
72  }
73  else
74  {
75  sf = String.Format(s, args);
76  }
77  Kernel.Evaluate(sf);
78  Kernel.WaitForAnswer();
79  var e = Kernel.GetExpr();
80  return e;
81  }
82 
89  public static Expr EvalFunction(String functionName, IEnumerable<Expr> args)
90  {
91  return Eval("{0}[{1}]", functionName, String.Join(",", args));
92  }
93 
94  public static bool EvalBool(String s, params object[] args)
95  {
96  string sf;
97  if (args == null || !args.Any())
98  {
99  sf = s;
100  }
101  else
102  {
103  sf = String.Format(s, args);
104  }
105  Kernel.Evaluate(sf);
106  Kernel.WaitForAnswer();
107  var e = Kernel.GetBoolean();
108  return e;
109  }
110  }
111 
112  public static class MSingleExtensions
113  {
114  public static int CompareTo(this Expr e0, Expr e1 = null)
115  {
116  if (e1 == null)
117  {
118  e1 = "0".MsEvalWith();
119  }
120  if (MSingle.EvalBool("{0} > {1}", e0, e1))
121  {
122  return 1;
123  }
124  if (MSingle.EvalBool("{0} < {1}", e0, e1))
125  {
126  return -1;
127  }
128  if (MSingle.EvalBool("{0} == {1}", e0, e1))
129  {
130  return 0;
131  }
132  throw new Exception("These two expressions are not comparable");
133  }
134 
141  public static Expr MsEvalWith(this String s, params object[] args)
142  {
143  return MSingle.Eval(s, args);
144  }
145 
152  public static Expr MsBracket(this String s, IEnumerable<Expr> args)
153  {
154  return MSingle.EvalFunction(s, args);
155  }
156 
163  public static Expr MsBracket(this String s, Expr arg)
164  {
165  return s.MsBracket(new[]{arg});
166  }
167 
168  /* This was creating a strange bug!!
169  *
170  *
171  *
172  public static Expr MsBracket(this String s, params Expr[] args)
173  {
174  return s.MsBracket(args);
175  }
176  */
177  }
178 }