from Visual Basic To Mathematica through JLink: examples and problems

*To*: mathgroup at smc.vnet.net*Subject*: [mg26278] from Visual Basic To Mathematica through JLink: examples and problems*From*: "rani olam" <okayr9 at cool.com.jo>*Date*: Sun, 10 Dec 2000 00:19:50 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

Hi Suppose you are in Visual Basic and have big numbers of 42 digits size each and you want to find if any number is a prime or not from that (aNum) to (aNum+10) in a loop, this is not possible by Visual Basic, so while we are relaxing in the easy life in the Grand VB Yacht with all rich aspects, we need here to communicate with the supersonic JUMBO Airplane "Mathematica" using JLink from http://www.wolfram.com/solutions/mathlink/jlink to calculate our big numbers, the following are 2 solutions , the first one derived from Todd Gayley ideas about delivering the expressions as strings to mathematica, as in http://library.wolfram.com/mathgroup/archive/2000/Dec/msg00012.html we will not resort to the NextPrime Function in Mathematica , as my purpose is a demonstration of some plans, the concise solution , and the long one, you could draw two buttons on a Form then copy and paste . Private Sub Command1_Click() Dim aNum As String aNum="111111111111111111111111111111122345678916" Set link = GetObject("java:com.wolfram.jlink.KernelLinkCOM") link.setLink ("-linkname 'C:\Program = Files\Mathematica\4.0\\mathkernel' -linkmode launch") ' You must set the above directory to accommodate the mathematica kernel directory in your system link.discardAnswer Text1.Text = link.evaluateToOutputForm("Table[" & "PrimeQ[" & aNum & "+ i], {i,10}]",0) ' Text1.Text is a text box in the visual basic Form link.close End Sub ' ================ ' The long solution Private Sub Command2_Click() Dim aNum As String aNum="111111111111111111111111111111122345678917" newline=chr(13) + chr(10) Text1.Text="" ' you may construct a 900 digits number from the following code but you can not use it ' here as input to PrimeQ . ' dim aNum as string ' for j=1 to 100 ' for i=1 to 9 ' aNum=aNum + Right(Str(i),1) ' next i ' next j Set link = GetObject("java:com.wolfram.jlink.KernelLinkCOM") link.setLink ("-linkname 'C:\Program Files\Mathematica\4.0\\mathkernel' -linkmode launch") ' You must set the above directory to accommodate the mathematica kernel directory in your system link.discardAnswer For i = 1 to 10 link.putFunction "EvaluatePacket", 1 link.putFunction "PrimeQ", 1 link.putFunction "ToExpression", 1 link.put (aNum) link.waitForAnswer a = link.getString If a = "True" Then Text1.Text = Text1.Text & aNum & " Prime" & newline ' displayingthe Prime number in the Text ' Box In The VB Form Else Text1.Text = Text1.Text & aNum & " Not prime" & newline End If ' below we will add 1 to the number aNum a = link.endPacket() link.putFunction "EvaluatePacket" , 1 link.putFunction "Plus" , 2 link.putFunction "ToExpression", 1 link.put (aNum) link.put 1 ' add one to the aNum link.waitForAnswer aNum = link.getString Text1.Text = Text1.Text & newline & aNum & newline a=link.endPacket() Next i link.Close End Sub ' ======================= from the above example the number 111111111111111111111111111111122345678921 will be displayed in the Text1.Text as a Prime number, this example is just a demostration , i hope VB fans will utilise JLink in connecting to mathematica so they will benefit from the powerfull capabilities, and the other people think that they are geniuses while they are phoning to mathematica kernel for the answers which delivered to them through Jlink underground Cables. there is a problem with graphics : How we could plot the function Plot[x,{x,0,1}] in a Picture1.Picture or Image1.Image in Visual Basic Form ?? every time i get an error. Please Help rani olam