public static void WriteEvalToImageExpression(IMathLink ml, object obj, int width, int height, string graphicsFmt, int dpi, bool useFE) { ml.PutFunction("EvaluatePacket", 1); int numArgs = 2 + (useFE ? 1 : 0) + (dpi > 0 ? 1 : 0) + (width > 0 || height > 0 ? 1 : 0); ml.PutFunction("EvaluateToImage", numArgs); ml.Put(obj); if (useFE) { ml.Put(true); } ml.Put(graphicsFmt); if (dpi > 0) { ml.PutFunction("Rule", 2); ml.PutSymbol("ImageResolution"); ml.Put(dpi); } if (width > 0 || height > 0) { ml.PutFunction("Rule", 2); ml.PutSymbol("ImageSize"); ml.PutFunction("List", 2); if (width > 0) { ml.Put(width); } else { ml.PutSymbol("Automatic"); } if (height > 0) { ml.Put(height); } else { ml.PutSymbol("Automatic"); } } ml.EndPacket(); }
// These next methods are called by implementors of the KernelLink "evaluateTo" methods. It is useful to separate the // code to create the appropriate expression to send (which is what these methods do) and the code that runs the // reading loop. In these methods, obj must be a string or Expr. // This is ready to accommodate an evaluateToMathML() function, but I have not added such a function to KernelLink yet. public static void WriteEvalToStringExpression(IMathLink ml, Object obj, int pageWidth, string format) { ml.PutFunction("EvaluatePacket", 1); ml.PutFunction("ToString", 3); if (obj is string) { ml.PutFunction("ToExpression", 1); } ml.Put(obj); ml.PutFunction("Rule", 2); ml.PutSymbol("FormatType"); ml.PutSymbol(format); ml.PutFunction("Rule", 2); ml.PutSymbol("PageWidth"); if (pageWidth > 0) { ml.Put(pageWidth); } else { ml.PutSymbol("Infinity"); } ml.EndPacket(); }
public static void WriteEvalToTypesetExpression(IMathLink ml, object obj, int pageWidth, string graphicsFmt, bool useStdForm) { ml.PutFunction("EvaluatePacket", 1); int numArgs = 2 + (useStdForm ? 0 : 1) + (pageWidth > 0 ? 1 : 0); ml.PutFunction("EvaluateToTypeset", numArgs); ml.Put(obj); if (!useStdForm) { ml.PutSymbol("TraditionalForm"); } if (pageWidth > 0) { ml.Put(pageWidth); } ml.Put(graphicsFmt); ml.EndPacket(); }
/// <summary> /// Writes an instance of the class specified as the <see cref="IMathLink.ComplexType">ComplexType</see> on the link. /// </summary> /// public void PutComplex(IMathLink ml, object obj) { if (ComplexType == null) { throw new MathLinkException(MathLinkException.MLE_NO_COMPLEX); } double re = 0; double im = 0; try { re = getRealPart(obj); im = getImaginaryPart(obj); } catch (Exception e) { // Shouldn't get here. ml.PutSymbol("$Failed"); throw e; } ml.PutFunction("Complex", 2); // Use Put(double), because it has code to handle NaN, infinity. ml.Put(re); ml.Put(im); }
public override void PutSymbol(string s) { impl.PutSymbol(s); }
/// <summary> /// Analogous to the MLInstall function in mprep-generated C programs. /// </summary> /// <param name="ml"></param> /// <returns>Whether it succeeded or not.</returns> /// public static bool install(IMathLink ml) { // Adding nXX functions here requires also adding the symbols to the NETLink`NET.m file. try { ml.Connect(); ml.Put("Begin[\"" + KernelLinkImpl.PACKAGE_INTERNAL_CONTEXT + "\"]"); definePattern(ml, "nCall[typeName_String, obj_Symbol, callType_Integer, isByVal_, memberName_String, argCount_Integer, typesAndArgs___]", "{typeName, obj, callType, isByVal, memberName, argCount, typesAndArgs}", CALL); definePattern(ml, "nLoadType1[type_String, assemblyName_]", "{type, assemblyName}", LOADTYPE1); definePattern(ml, "nLoadType2[type_String, assemblyObj_]", "{type, assemblyObj}", LOADTYPE2); definePattern(ml, "nLoadExistingType[typeObject_?NETObjectQ]", "{typeObject}", LOADEXISTINGTYPE); definePattern(ml, "nLoadAssembly[assemblyNameOrPath_String, suppressErrors_]", "{assemblyNameOrPath, suppressErrors}", LOADASSEMBLY); definePattern(ml, "nLoadAssemblyFromDir[assemblyName_String, dir_String]", "{assemblyName, dir}", LOADASSEMBLYFROMDIR); definePattern(ml, "nGetAssemblyObject[asmName_String]", "{asmName}", GETASSEMBLYOBJ); definePattern(ml, "nGetTypeObject[aqTypeName_String]", "{aqTypeName}", GETTYPEOBJ); definePattern(ml, "nReleaseObject[instances:{__Symbol}]", "{instances}", RELEASEOBJECT); definePattern(ml, "nMakeObject[typeName_String, argType_Integer, val_]", "{typeName, argType, val}", MAKEOBJECT); definePattern(ml, "nVal[obj_?NETObjectQ]", "{obj}", VAL); definePattern(ml, "nReflectType[typeName_String]", "{typeName}", REFLECTTYPE); definePattern(ml, "nReflectAsm[asmName_String]", "{asmName}", REFLECTASM); definePattern(ml, "nSetComplex[typeName_String]", "{typeName}", SETCOMPLEX); definePattern(ml, "nSameQ[obj1_?NETObjectQ, obj2_?NETObjectQ]", "{obj1, obj2}", SAMEQ); definePattern(ml, "nInstanceOf[obj_?NETObjectQ, aqTypeName_String]", "{obj, aqTypeName}", INSTANCEOF); definePattern(ml, "nCast[obj_?NETObjectQ, aqTypeName_String]", "{obj, aqTypeName}", CAST); definePattern(ml, "nPeekTypes[]", "{}", PEEKTYPES); definePattern(ml, "nPeekObjects[]", "{}", PEEKOBJECTS); definePattern(ml, "nPeekAssemblies[]", "{}", PEEKASSEMBLIES); definePattern(ml, "nCreateDelegate[typeName_String, mFunc_String, sendTheseArgs_Integer, callsUnshare:(True | False), wrapInNETBlock:(True | False)]", "{typeName, mFunc, sendTheseArgs, callsUnshare, wrapInNETBlock}", CREATEDELEGATE); definePattern(ml, "nDefineDelegate[name_String, retTypeName_String, paramTypeNames_List]", "{name, retTypeName, paramTypeNames}", DEFINEDELEGATE); definePattern(ml, "nDlgTypeName[eventObject_?NETObjectQ, aqTypeName_String, evtName_String]", "{eventObject, aqTypeName, evtName}", DLGTYPENAME); definePattern(ml, "nAddHandler[eventObject_?NETObjectQ, aqTypeName_String, evtName_String, delegate_?NETObjectQ]", "{eventObject, aqTypeName, evtName, delegate}", ADDHANDLER); definePattern(ml, "nRemoveHandler[eventObject_?NETObjectQ, aqTypeName_String, evtName_String, delegate_?NETObjectQ]", "{eventObject, aqTypeName, evtName, delegate}", REMOVEHANDLER); definePattern(ml, "nCreateDLL1[funcName_String, dllName_String, callConv_String, retTypeName_String, argTypeNames_, areOutParams_, strFormat_String]", "{funcName, dllName, callConv, retTypeName, argTypeNames, areOutParams, strFormat}", CREATEDLL1); definePattern(ml, "nCreateDLL2[decl_String, refAsms_, lang_String]", "{decl, refAsms, lang}", CREATEDLL2); definePattern(ml, "nModal[modal:(True | False), formToActivate_?NETObjectQ]", "{modal, formToActivate}", MODAL); definePattern(ml, "nShow[formToActivate_?NETObjectQ]", "{formToActivate}", SHOW); definePattern(ml, "nShareKernel[sharing:(True | False)]", "{sharing}", SHAREKERNEL); definePattern(ml, "nAllowUIComputations[allow:(True | False)]", "{allow}", ALLOWUICOMPS); definePattern(ml, "nUILink[name_String, prot_String]", "{name, prot}", UILINK); definePattern(ml, "nIsCOMProp[obj_?NETObjectQ, memberName_String]", "{obj, memberName}", ISCOMPROP); definePattern(ml, "nCreateCOM[clsIDOrProgID_String]", "{clsIDOrProgID}", CREATECOM); definePattern(ml, "nGetActiveCOM[clsIDOrProgID_String]", "{clsIDOrProgID}", GETACTIVECOM); definePattern(ml, "nReleaseCOM[obj_?NETObjectQ]", "{obj}", RELEASECOM); definePattern(ml, "nLoadTypeLibrary[tlbPath_String, safeArrayAsArray_, assemblyFile_String]", "{tlbPath, safeArrayAsArray, assemblyFile}", LOADTYPELIBRARY); definePattern(ml, "nGetException[]", "{}", GETEXCEPTION); definePattern(ml, "nConnectToFEServer[linkName_String]", "{linkName}", CONNECTTOFE); definePattern(ml, "nDisconnectToFEServer[]", "{}", DISCONNECTTOFE); // For speed testing: definePattern(ml, "noop[]", "{}", NOOP); definePattern(ml, "noop2[argc_Integer, args___]", "{argc, args}", NOOP2); // Here we define the argTypeToInteger function by sending // MapThread[(argTypeToInteger[#1] = #2)&, {ToExpression /@ argTypePatterns, {... ARGTYPE_ constants ...}}] ml.PutFunction("MapThread", 2); ml.PutFunction("Function", 1); ml.PutFunction("Set", 2); ml.PutFunction("argTypeToInteger", 1); ml.PutFunction("Slot", 1); ml.Put(1); ml.PutFunction("Slot", 1); ml.Put(2); ml.PutFunction("List", 2); ml.PutFunction("Map", 2); ml.PutSymbol("ToExpression"); ml.Put(argTypePatterns); ml.PutFunction("List", 13); ml.Put(ARGTYPE_INTEGER); ml.Put(ARGTYPE_REAL); ml.Put(ARGTYPE_STRING); ml.Put(ARGTYPE_BOOLEAN); ml.Put(ARGTYPE_NULL); ml.Put(ARGTYPE_MISSING); ml.Put(ARGTYPE_OBJECTREF); ml.Put(ARGTYPE_VECTOR); ml.Put(ARGTYPE_MATRIX); ml.Put(ARGTYPE_TENSOR3); ml.Put(ARGTYPE_LIST); ml.Put(ARGTYPE_COMPLEX); ml.Put(ARGTYPE_OTHER); ml.Put("End[]"); ml.PutSymbol("End"); ml.Flush(); return(true); } catch (MathLinkException) { return(false); } }