MathGroup Archive 1999

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

Search the Archive

Re: Complex numbers and MathLink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21269] Re: Complex numbers and MathLink
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Tue, 21 Dec 1999 03:46:50 -0500 (EST)
  • Organization: Universitaet Leipzig
  • References: <83kleb$1ec@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

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

MLPutFunction(stdlink,"Apply",2);
  MLPutFunction(stdlink,"Partition",2);
    MLPutRealList(stdlink,(double *) yourcomplexarray, clen);
    MLPutInteger(stdlink,2);
  MLPutFunction(stdlink,"List",1);
    MLPutInteger(stdlink,1);

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
  Jens
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