MathGroup Archive 2001

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

Search the Archive

Re: Differential equations error with MathLink/JLink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27941] Re: Differential equations error with MathLink/JLink
  • From: tgayley at wolfram.com (Todd Gayley)
  • Date: Sat, 24 Mar 2001 15:15:45 -0500 (EST)
  • Organization: Wolfram Research, Inc.
  • References: <98vh54$8sr@smc.vnet.net> <99chhr$8bi@smc.vnet.net> <99hcmk$eb8@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Ralph,

You cannot necessarily use MLGetFunction() to read what's on the link just because
MLGetNext() returned MLTKFUNC. MLGetFunction() only works for functions with symbols as
their head. You can write a function that does not have a symbol as its head, and a
classic case is

     Derivative[1][x][t]

which is, coincidentally, exactly the return value of your computation. This expression is
a function of one argument (t), with Derivative[1][x] as its head. (Note that this head is
itself a function with a function, not a symbol, as _its_ head.)

If you want to be able to read arbitrary expressions off a link, your handling of MLTKFUNC
has to look like this:

    if (MLGetNext(link) == MLTKFUNC) {
        long argCount;
        MLGetArgCount(link, &argCount);
        // Read the head, first by checking MLGetNext() and then the correct get call
        for (int i = 0; i < argCount; i++) {
            // Read the args
        }
    }
        
Take a look at the read_and_print_expression() function in the MathLink example program
factor2.c to see general-purpose expression-reading code.

This complexity is one reason why the Expr class in J/Link is so useful.

One more thing. MLGetNext() is a destructive call--it advances the link position. You got
MLTKFUNC twice in your example because the expression is a function, and its head also
happens to be a function. In other words, the second time you called MLGetNext(), you were
reading a new part of the result, not just reaffirming the first call.


--Todd Gayley
Wolfram Research


On 24 Mar 2001 00:52:20 -0500, Ralph Gauges <ralph.gauges at eml.villa-bosch.de> wrote:
>Hi again,
>
>I looked into this getFunction error a little more closely, but I still can not find out what
>the problem seems to be.
>Since it works in Java, there has to be a way to get it to work in python as well.
>I added some print statement to the python wrapper around MLGetFunction. I think, the rest of
>the code is not that important. So what basically happens is that python finds out that it
>will get a RETURNPKT and that the MLGetNext wrapper finds out that the next thing will be a
>function (MLTKFUNC=70="F"). Just to be sure, I call MLGetNext again right before
>MLGetFunction is called (see below) and I still get MLTKFUNC as the return value. (See
>output.) But then the actual MLGetFunction call just fails and returns this "MLGet out of
>sequence" error. When I do the same thing in JLink (GetNext and then getFunction), it seems
>to work.
>Does anyone see, where the error in this function might be.
>I can get around this by assembling the whole expression that solves a set of differential
>equations and getting the result from that, but I would still be interested in finding out
>why this does not work.
>
>Thanks a lot
>
>
>Ralph
>
>
>
>
>
>static PyObject *
>GetFunction(PyObject *self, PyObject *args)
>{
>  long int len;
>
>  const char *Function;
>  PyObject *PyFunction;
>  long int x;
>  printf("Checking next again.\n");
>  x=MLGetNext(lp);
>  printf("%d\n",x);
>  printf("getting Function.\n");
>  MLGetFunction(lp, &Function, &len);
>  printf("Got %s %d\n",Function,len);
>  if (MLError( lp)) {
>    printf("Error occured.\n");
>    error( lp);
>  }
>  PyFunction = Py_BuildValue("si", Function, len);
>  MLDisownSymbol(lp, Function);
>
>  return PyFunction;
>}
>
>
>
>Testoutput for D[x[t],t]:
>
>Next packet.
>INPUTNAMEPKT
>Getting string.
>InputName: text= In[1]:=
>Next packet.
>RETURNPKT
>Python GetNext.
>Get next.
>MLTKFUNC
>NEXT is PYMLTKFUNC
>Checking next again.
>70
>getting Function.
>Got (null) 1
>Error occured.
>Error detected by MathLink: MLGet out of sequence.
>
>
>
>



  • Prev by Date: Re: DSolve bug in Mathematica version 4.1?,
  • Next by Date: RE: ListDensityPlot[] : frame ticks help !
  • Previous by thread: Re: Differential equations error with MathLink/JLink
  • Next by thread: Re: Differential equations error with MathLink/JLink