# Working with complex numbers in LibraryLink

**URL:** <https://community.wolfram.com/t/working-with-complex-numbers-in-librarylink/1348>\
**Category:** Ideas\
**Tags:** external-interfaces\
**Created:** [January 23, 2014, 8:46pm UTC](https://community.wolfram.com/t/working-with-complex-numbers-in-librarylink/1348 "2014-01-23T20:46:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnoudb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/arnoudb/32/41177_2.png) [@arnoudb](https://community.wolfram.com/u/arnoudb)\
**Post date:** [January 23, 2014, 8:46pm UTC](https://community.wolfram.com/t/working-with-complex-numbers-in-librarylink/1348/1 "2014-01-23T20:46:25Z")

</div>

In a [previous post](https://wolfram.discourse.group/t/1345/1) I explained how to create a matrix with integer values when passing in a row and column argument.  
In this post we are going to look at a data type we have not previously used: complex numbers (mcomplex). Unlike reals,  
integers and strings this data type is a compound type with both a real and imaginary part which can be extracted using  
mcreal(z) and mcimag(z). In this example we will extract both parts and return them back as a list to the calling Wolfram  
Language function.

Here is the C code (ReIm.c):

```auto
#include "WolframLibrary.h"

DLLEXPORT int ReIm(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {

        mcomplex z;
        mreal re,im;

        MTensor out;
        mint out_type;
        mint out_rank;
        mint* out_dims;
        mint* out_pos;
        int err;

        z = MArgument_getComplex(Args[0]);
        re = mcreal(z);
        im = mcimag(z);

        out_type = MType_Real;
        out_rank = 1;
        out_dims[0] = 2;
        err = libData->MTensor_new(out_type, out_rank, out_dims, &out);
        out_pos[0] = 1;
        err = libData->MTensor_setReal(out, out_pos, re);
        out_pos[0] = 2;
        err = libData->MTensor_setReal(out, out_pos, im);
        MArgument_setInteger(Res,out);
        return LIBRARY_NO_ERROR;
}

```

Here we get the complex number by calling MArgument\_getComplex on the first argument and immediately split it up into  
its real and imaginary part using mcreal(z) and mcimag(z). To return these parts as a list we need to create a tensor  
with one dimension (a list) and length 2. After creating this tensor we need to set the elements of the list with libData-\>MTensor\_setReal  
and finally return the result.

The following Wolfram Language code (ReIm.m) creates the library and loads the LibraryLink function. Note that the function expect a  
single complex number as its input and will return a list of real numbers (of length two in this case):

```auto
Needs["CCompilerDriver`"];
lib = CreateLibrary[{"ReIm.c"},"ReIm"];
ReIm = LibraryFunctionLoad[lib,"ReIm",{Complex},{Real,1}];

```

Now we can try running this code in the Wolfram Language:

```auto
Mathematica 10.0 for Microsoft Windows (64-bit)
Copyright 1988-2013 Wolfram Research, Inc.

In[1]:= Get["ReIm.m"]

In[2]:= ReIm[0]

Out[2]= {0., 0.}

In[3]:= ReIm[I]

Out[3]= {0., 1.}

In[4]:= ReIm[Log[-1]]

Out[4]= {0., 3.14159}

In[5]:= ReIm[2.3+6.5*I]

Out[5]= {2.3, 6.5}

```

Note how Log[-1] gets evaluated to I\*Pi before it is sent to the LibraryLink function. Integers and constants with a numerical value  
are automatically converted to their real number approximation.

Next, let’s take a look at [how to work with multiple functions in LibraryLink](http://community.wolfram.com/groups/-/m/t/189744) .

---

<div class="post-metadata">

**Author:** ![ivansavytskyy](https://avatars.discourse-cdn.com/v4/letter/i/a9adbd/32.png) [@ivansavytskyy](https://community.wolfram.com/u/ivansavytskyy)\
**Post date:** [June 11, 2019, 12:31am UTC](https://community.wolfram.com/t/working-with-complex-numbers-in-librarylink/1348/2 "2019-06-11T00:31:53Z")

</div>

Hello!

It seems as though MMA already has a function called “ReIm” ([ReIm—Wolfram Documentation](https://reference.wolfram.com/language/ref/ReIm.html)) which means that you are calling that function in your MMA evaluation (as opposed to using your custom ReIm function).

Also, I tried evaluating your code and MMA crashes regardless of the input. Will keep looking for the error and get back to you if I find it!

All the best,

Ivan S.

---

<div class="post-metadata">

**Author:** ![garyanderson](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@garyanderson](https://community.wolfram.com/u/garyanderson)\
**Post date:** [May 23, 2021, 10:42pm UTC](https://community.wolfram.com/t/working-with-complex-numbers-in-librarylink/1348/3 "2021-05-23T22:42:23Z")

</div>

Here’s a version that avoids the name clash, the kernel crash and seems to work:  
#include “WolframLibrary.h”

DLLEXPORT int notReIm(WolframLibraryData libData, mint Argc, MArgument \*Args, MArgument Res) {

```
   mcomplex z;
     mreal re,im;

     z = MArgument_getComplex(Args[0]);
    re = mcreal(z);
    im = mcimag(z);

     mint row,col,rows,cols,count;

     MTensor out;
     mint out_type = MType_Integer;
    mint out_rank = 1 ;
    mint out_dims[1];
    mint* out_data;
    int err;
    mint out_pos[2];

    out_type = MType_Real;
    out_rank = 1;
    out_dims[0] = 2;
    err = libData->MTensor_new(out_type, out_rank, out_dims, &out);
    out_pos[0] = 1;
    err = libData->MTensor_setReal(out, out_pos, re);
    out_pos[0] = 2;
    err = libData->MTensor_setReal(out, out_pos, im);
    MArgument_setMTensor(Res,out);
    return LIBRARY_NO_ERROR;

```

}
