MathGroup Archive 1999

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

Search the Archive

Re: Complex numbers and MathLink

  • To: mathgroup at
  • Subject: [mg21269] Re: Complex numbers and MathLink
  • From: Jens-Peer Kuska <kuska at>
  • Date: Tue, 21 Dec 1999 03:46:50 -0500 (EST)
  • Organization: Universitaet Leipzig
  • References: <83kleb$>
  • Sender: owner-wri-mathgroup at


since C has no complex data type you must deal with Real 
and imaginary parts. I assume that you have real and imaginary
parts like

typedef struct complex { double re,im; } complex;

The best way to send an array of complex numbers to
Mathematica is

    MLPutRealList(stdlink,(double *) yourcomplexarray, clen);

it is just Apply[Complex,Partition[{re1,im1,re2,im2,..},2],{1}]

In Matheamtica you should prepare the values by

N[Flatten[{Re[#],Im[#]} & /@ complexarray]]

when you send it to the client.

The explanation is easy. When you send Complex[re,im].. the
protocol has to transmit always the Complex head for every pair.
That is a huge overhead compared with the transmission of a block
of doubles and some speedy operations in Mathematica. 

Hope that helps
Dirk Kraaijpoel wrote:
> What is the best way to deal with Complex numbers
> and Lists of Complex numbers in MathLink?
> Dirk Kraaijpoel

  • Prev by Date: Re: Partial evaluation
  • Next by Date: Re: Plotting a step function with the origin indicated
  • Previous by thread: Complex numbers and MathLink
  • Next by thread: modular exponantiation