Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #4875 > unrolled thread

Re: passing Indeterminate and Infinity to C via MathLink

Started byJohn Fultz <jfultz@wolfram.com>
First post2011-09-12 08:23 +0000
Last post2011-09-15 08:49 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: passing Indeterminate and Infinity to C via MathLink John Fultz <jfultz@wolfram.com> - 2011-09-12 08:23 +0000
    Re: passing Indeterminate and Infinity to C via MathLink Roman <rschmied@gmail.com> - 2011-09-15 08:49 +0000

#4875 — Re: passing Indeterminate and Infinity to C via MathLink

FromJohn Fultz <jfultz@wolfram.com>
Date2011-09-12 08:23 +0000
SubjectRe: passing Indeterminate and Infinity to C via MathLink
Message-ID<j4kfii$kes$1@smc.vnet.net>
On Sat, 10 Sep 2011 07:29:23 -0400 (EDT), Roman wrote:
> Hello all,
> I am setting up a C function which accepts real numbers from MathLink.
> The behavior I would like to achieve is that whenever the number is
> "Infinity" then the C function receives "inf" (which is a valid double-
> precision-format number); and whenever the number is "Indeterminate"
> then the C function receives "nan" (which is also a valid double-
> precision-format number).
> Unfortunately MathLink (Mathematica 7.0 for Mac OS X x86 (64-bit))
> crashes whenever I am trying to pass either Infinity or Indeterminate
> to a MathLink function expecting a double-precision number.
> Would you know how to solve this without going into If[] statements on
> the Mathematica side of MathLink?
> Thanks!
> Roman

Mathematica represents Indeterminate and Infinity as symbols in its expression 
tree, and MathLink is always very faithful about transmitting the expression 
tree precisely.  Note that it's not very difficult to deal with this in your C 
code, though.  You can just declare the function as having a Manual MathLink 
type and then, in the C function determine using MLGetNext() whether the next 
thing is a symbol or a real.  If it's a symbol, then you can just synthesizes 
the IEEE version of the indeterminate value in your C program.

About two thirds of the way down this help page:

tutorial/HandlingListsArraysAndOtherExpressions

there's an example that illustrates how to use Manual as an argument type.

Sincerely,

John Fultz
jfultz@wolfram.com
User Interface Group
Wolfram Research, Inc.

[toc] | [next] | [standalone]


#4974

FromRoman <rschmied@gmail.com>
Date2011-09-15 08:49 +0000
Message-ID<j4se69$snn$1@smc.vnet.net>
In reply to#4875
Thanks John. The entire reason why I am communicating with external C
procedures is in order to speed up computation; if much time is spent
in the communication interface then this defeats the point. In
particular, when passing large arrays of real numbers (containing NaN
and/or inf) then receiving the array element by element via
MLGetNext() seems a very inefficient thing to do.

I appreciate how faithful Mathematica is in the transmission process,
but when I pass "NaN" from C to Mathematica via MLPutReal64() then
Mathematica does in fact receive the "Indeterminate" symbol, not an
IEEE "NaN". So the conversion capability is built into Mathematica in
one direction but not in the other. What I was hoping for is a trick
which allows me to use such an automatism in the Mathematica->C
direction via MLGetReal64Array(). For instance, is there a way to
convert a matrix in Mathematica into a pure numerical representation
(where every element must be an IEEE number) which could then be
forwarded immediately (no conversions) to MLGetReal64Array()?

Cheers!
Roman



On Sep 12, 10:23 am, John Fultz <jfu...@wolfram.com> wrote:
> On Sat, 10 Sep 2011 07:29:23 -0400 (EDT), Roman wrote:
> > Hello all,
> > I am setting up a C function which accepts real numbers from MathLink.
> > The behavior I would like to achieve is that whenever the number is
> > "Infinity" then the C function receives "inf" (which is a valid double-
> > precision-format number); and whenever the number is "Indeterminate"
> > then the C function receives "nan" (which is also a valid double-
> > precision-format number).
> > Unfortunately MathLink (Mathematica 7.0 for Mac OS X x86 (64-bit))
> > crashes whenever I am trying to pass either Infinity or Indeterminate
> > to a MathLink function expecting a double-precision number.
> > Would you know how to solve this without going into If[] statements on
> > the Mathematica side of MathLink?
> > Thanks!
> > Roman
>
> Mathematica represents Indeterminate and Infinity as symbols in its expre=
ssion
> tree, and MathLink is always very faithful about transmitting the express=
ion
> tree precisely.  Note that it's not very difficult to deal with this in=
 your C
> code, though.  You can just declare the function as having a Manual Mat=
hLink
> type and then, in the C function determine using MLGetNext() whether the =
next
> thing is a symbol or a real.  If it's a symbol, then you can just synth=
esizes
> the IEEE version of the indeterminate value in your C program.
>
> About two thirds of the way down this help page:
>
> tutorial/HandlingListsArraysAndOtherExpressions
>
> there's an example that illustrates how to use Manual as an argument type=
.
>
> Sincerely,
>
> John Fultz
> jfu...@wolfram.com
> User Interface Group
> Wolfram Research, Inc.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web