Wolfram EngineをVisualBasicで使う(5:グラフィックスの対応範囲を広げる)
前回のGraphics3D関数に対応せるとと同時に、他のグラフ系の関数の対応を増やすことにしました。
関数名に共通した文字列を判断するようにIf文を拡張します。
Wlfram Researchのドキュメントセンターからめぼしい関数名をピックアップすると…
「データの可視化」セクション
ListPlot |
ListLinePlot |
ListStepPlot |
StackedListPlot |
ListPlot3D |
ListPointPlot3D |
ListDensityPlot |
ListLogLinerPlot |
ListLogLogPlot |
ListPolarPlt |
MatrixPlot |
ParallelAxisPlot |
などなど。共通部分は「Plot」なので、”*Plot*”とすれば、「Histgram」以外はほぼ拾えそう。
もちろん、今回は関数のプロットを想定しているので、棒グラフ「BarChart」や円グラフ「PieChart」などは含まれない。
「関数の可視化」セクション
Plot |
LogPlot |
Plot3D |
ContourPlot |
ContourPlot3D |
DensityPlot |
DensityPlot3D |
ParametricPlot |
PolarPlot |
RegionPlot |
ComplexPlot |
DiscretePlot |
などなど。これらも”*Plot*”で吸収できる。
前回のような”Sphere”のようなものは少し特殊なケースだが、”*2D*”と”*3D*”も判断すればかなり範囲が広がりそう。
ということで、
- If文に条件を追加します。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ml As IKernelLink = MathLinkFactory.CreateKernelLink("-linkmode launch -linkname 'C:\Program Files\Wolfram Research\Wolfram Engine\13.3\WolframKernel.exe'")
ml.WaitAndDiscardAnswer()
Dim strarg As String = TextBox2.Text
If strarg Like "*Plot*" Or strarg Like "*2D*" Or strarg Like "*3D*" Then
Dim result As Image = ml.EvaluateToImage(strarg, 200, 200)
PictureBox1.Image = result
Else
Dim result As String = ml.EvaluateToOutputForm(strarg, 0)
TextBox1.Text = result
End If
End Sub
のようにします。
-
実行して、
Graphics3D[Sphere[{0, 0, 0}]]
を入力すると、「Graphics3D」の「3D」の部分が拾われて、次のような結果となりました。OK!
Sphereが表示された Mathematicaでは、1つの関数の記述でグラフを横に並べて表示したりできるので、今回のプログラムでも実行できるか試してみました。
-
ドキュメントセンターの「VectorPlot」のページの
VectorPlot[{{x + y, y - x}, {y, x + y}}, {x, -3, 3}, {y, -3, 3}, PlotLayout -> "Row"]
を入力してみます。
ベクタープロットの表示(一部)
画像領域に収まらなかった。 -
GUIのデザインでPictureBoxを横に広げて、
Dim result As Image = ml.EvaluateToImage(strarg, 400, 200)
のように横長にして、再実行。
ベクタープロットを横に並べて表示
まだまだ見栄えの調整の余地はありますが、Wolfram言語内で複数並べて実行できることは確認できました。