MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problem: Passing Variables within Expressions To Mathematica through JLink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg26216] Re: Problem: Passing Variables within Expressions To Mathematica through JLink
  • From: tgayley at wolfram.com (Todd Gayley)
  • Date: Sat, 2 Dec 2000 02:10:42 -0500 (EST)
  • Organization: Wolfram Research, Inc.
  • References: <9074kr$b0o@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 30 Nov 2000 22:07:07 -0500, "rani olam" <okayr9 at cool.com.jo> wrote:

>
>Hi
>The following program is a visual basic program to send request to 
>Mathematica Kernel to calculate Pi to arbitrary decimal digits throught 
>JLink using the expression
>   N[Pi, NumOfDigits]  and sending it Packet by Packet, the problem is 
>that i could not send this expression as a whole using the following 
>line :
>Text1.Text=link.evaluateToOutputForm("N[Pi, NumOfDigits]", 0)
>i could not pass the variable NumOfDigits within the expression to 
>Kernel.
>it work if we replaced the variable with a number such as 10000:
>Text1.Text=link.evaluateToOutputForm("N[Pi, 10000]", 0)
>unfortunately the user's guide give us only examples about calculating 
>2+2, an the packet by packet method.
>any help?
>rani olam
>okayr9 at cool.com.jo
>
>'remember to adjust the mathematica kernel path as in your system
>'Text1.Text is for displaying Pi digits in the Text window
>'Text2.Text is for entering the number of digits you want to calculate
>'The main method is from the User's Manual of JLink which could be 
>obtained from:
>'http://www.wolfram.com/solutions/mathlink/jlink
>Private Sub Command1_Click()
> NumOfDigits= Val(Text2.Text)
>
>Set link = GetObject("java:com.wolfram.jlink.KernelLinkCOM")
>    link.setLink ("-linkname 'C:\Program Files\Wolfram 
>Research\Mathematica\4.0\\mathkernel' -linkmode launch")
>
>    link.discardAnswer
>    link.putFunction "EvaluatePacket", 1
>    link.putFunction "ToString", 1
>    link.putFunction "N", 2
>    link.putSymbol ("Pi")
>    link.put (NumOfDigits)
>    link.waitForAnswer
>Text1.Text =link.getString
>link.Close
>End Sub
>
>

Rani,

I'm glad you posted this question, since many readers are probably not aware that J/Link
can be used to call Mathematica from COM-aware scripting languages like Visual Basic,
VBScript, Active Server Pages, etc. Because the Microsoft Java virtual machine is tightly
integrated with COM, Java classes are automatically COM objects that expose a so-called
"Dispatch" interface, meaning they can be easily scripted. J/Link has a special
KernelLinkCOM class that exposes virtually the entire J/Link KernelLink interface to
non-Java scripting languages. Your code is a good example of all that you need to write to
use J/Link's high-level facilities for calling Mathematica without being the least bit
concerned that Java is providing the "glue" layer.

The answer to your question about getting this line to work:

    Text1.Text=link.evaluateToOutputForm("N[Pi, NumOfDigits]", 0)

is that NumOfDigits is a variable in VB, so you need to create a string of Mathematica
input that contains its value, not its name:

    Text1.Text=link.evaluateToOutputForm("N[Pi, " & NumOfDigits & "]", 0)

(For people not familiar with VB, & is a string-join operator). Thus, your subroutine
becomes just:

Private Sub Command1_Click()
   NumOfDigits= Val(Text2.Text)
   Set link = GetObject("java:com.wolfram.jlink.KernelLinkCOM")
   link.setLink ("-linkname 'C:\Program Files\Wolfram
Research\Mathematica\4.0\\mathkernel' -linkmode launch")
   Text1.Text = link.evaluateToOutputForm("N[Pi, " & NumOfDigits & "]", 0)
   link.Close
End Sub

This question examplifies a classic decision in all flavors of MathLink programming: Do
you create a string giving a complete command and send that, or do you build the input
expression by sending it piece-by-piece with a sequence of MathLink "put" calls? Often, as
here, it is more convenient to use the facilities of your programming language to
construct a complete string. In rarer cases it is easier to send the epxression piecemeal.


Todd Gayley
Wolfram Research


  • Prev by Date: Re: solve f(x)=0, where f:Rn+1 -> Rn
  • Next by Date: using the result from previous calculation
  • Previous by thread: Re: possible bug in Mathematica?
  • Next by thread: using the result from previous calculation