2 using System.Collections.Generic;
5 using System.Threading.Tasks;
16 private static readonly Lazy<MSingle> Lazy =
new Lazy<MSingle>(() =>
new MSingle());
23 get {
return Lazy.Value; }
29 public static IKernelLink
Kernel
31 get {
return Instance.KernelLink; }
39 get {
return _kernelLink; }
43 private readonly IKernelLink _kernelLink;
48 if (((
int) System.Environment.OSVersion.Platform).EqualsAny(4,6,128))
50 _kernelLink = MathLinkFactory.CreateKernelLink(
51 "-linkmode launch -linkname '\"/Applications/Mathematica.app/Contents/MacOS/MathKernel\" -mathlink'");
55 _kernelLink = MathLinkFactory.CreateKernelLink();
57 _kernelLink.WaitAndDiscardAnswer();
66 public static Expr
Eval(String s, params
object[] args)
69 if (args ==null || !args.Any())
75 sf = String.Format(s, args);
78 Kernel.WaitForAnswer();
79 var e = Kernel.GetExpr();
89 public static Expr
EvalFunction(String functionName, IEnumerable<Expr> args)
91 return Eval(
"{0}[{1}]", functionName, String.Join(
",", args));
94 public static bool EvalBool(String s, params
object[] args)
97 if (args == null || !args.Any())
103 sf = String.Format(s, args);
106 Kernel.WaitForAnswer();
107 var e = Kernel.GetBoolean();
112 public static class MSingleExtensions
114 public static int CompareTo(
this Expr e0, Expr e1 = null)
118 e1 =
"0".MsEvalWith();
120 if (MSingle.EvalBool(
"{0} > {1}", e0, e1))
124 if (MSingle.EvalBool(
"{0} < {1}", e0, e1))
128 if (MSingle.EvalBool(
"{0} == {1}", e0, e1))
132 throw new Exception(
"These two expressions are not comparable");
141 public static Expr MsEvalWith(
this String s, params
object[] args)
143 return MSingle.Eval(s, args);
152 public static Expr MsBracket(
this String s, IEnumerable<Expr> args)
154 return MSingle.EvalFunction(s, args);
163 public static Expr MsBracket(
this String s, Expr arg)
165 return s.MsBracket(
new[]{arg});
static Expr Eval(String s, params object[] args)
Has the Mathematica Kernel evaluate a string, formatted to include the provided arguments ...
static Expr EvalFunction(String functionName, IEnumerable< Expr > args)
Computes a function with a sequence of arguments
static bool EvalBool(String s, params object[] args)
IKernelLink KernelLink
The Mathematica kernel link
A singleton object that communicates with the Mathematica kernel
static IKernelLink Kernel
The Mathematica kernel link
static MSingle Instance
Gives the instance of the Mathematica kernel