3

我正在使用 C# 将数据传递给 Mathematica 并返回 Graphs:

MathKernel k = new MathKernel();
k.CaptureGraphics = true;
k.GraphicsFormat = "JPEG";
k.Compute("Show[Graphics[{Thick, Blue, Circle[{#, 0}] & /@ Range[4], Black, Dashed, Line[{{0, 0}, {5, 0}}]}]]");
pictureBox1.Image = k.Graphics[0];

在此处输入图像描述

这一直有效,直到我需要使用一个包。我看到返回的是原始数据而不是图像:

StringBuilder command = new StringBuilder();
command.Append("fakedata01 = With[{n = DayCount[{2008, 01, 01}, {2011, 3, 27}]}, Transpose[{Array[DatePlus[{2008, 01, 01}, #] &, n, 0], #}] & /@ (100. + (Accumulate /@ RandomVariate[NormalDistribution[0, 1], {8, n}]))];");
command.Append("Dimensions[%];");
command.Append("XYZLineGraph[fakedata01, Title -> \"Banks\\[CloseCurlyQuote] Share Prices\", Subtitle -> \"1 January 2008 = 100\", ScaleUnits -> \"Index\", DateLabelFormat -> \"Quarter1\", PartialLastYear -> 2.95/12, Footnotes -> {{\"*\", \"MSCI financials index\"}}, Sourcenotes -> {\"Bloomberg\"}, SpecialGridValues -> 100, PlotStyle -> {Red, XYZDarkBlue, XYZPink, XYZMauve, XYZPaleOrange, XYZTurquoise, Green, Gray}, Epilog -> {Red, Arrow[{{{2009, 3}, 30}, {{2009, 8}, 48}}], Text[\"Label\", {{2009, 3}, 25}]}]");

MathKernel k = new MathKernel();
k.CaptureGraphics = true;
k.GraphicsFormat = "JPEG";
k.Compute("Get[\"XYZ`XYZGraphs`\"];");
k.Compute("Get[\"XYZ`XYZUtilities`\"]");
k.Compute("Show[" + command.ToString() + "]");
pictureBox1.Image = k.Graphics[0];

错误是:

Get::noopen: 无法打开 XYZ XYZGraphs

我已经在 Mathematica 中安装了包,所以在启动时它们就可用了。当我在 Mathematica 中运行命令时,它会给我预期的输出。

有谁知道如何加载包以便通过 .Net 调用获得它们?

这两行不起作用:

k.Compute("Get[\"XYZ`XYZGraphs`\"];");
k.Compute("Get[\"XYZ`XYZUtilities`\"]");

我也尝试了以下方法,它有同样的问题:

k.Compute("Get[\"XYZ`XYZGraphs`\"];Get[\"XYZ`XYZUtilities`\"];Show[" + command.ToString() + "]");

我已经看到了这些线程,但它们在 Mathematica 中而不是在 C# 中: Load a mathematica package from inside a package Building Application Packages with multple packages and references in Mathematica

4

1 回答 1

1

I worked it out, the full answer is over at https://mathematica.stackexchange.com/questions/19516/load-a-mathematica-package-via-net-code

I evaluated $UserBaseDirectory in a Mathematica session. In that directory is the pre-established Applications folder where you are supposed to put your work. This is your private Applications folder.

Say you are doing extended work on TopicX. Create a TopicX folder in your private Applications folder. You can create subsidiary folders that contain your work and organize your notebooks on this topic.

Now make the BeginPackage statement in your package (which has the name XYZGraphs.m and is placed in theXYZ folder):

BeginPackage["XYZ`XYZGraphs`"]

Now you can load the package from anywhere by using:

<< XYZ`XYZGraphs`
于 2013-02-14T04:03:43.457 回答