# Saving an array of complex numbers

**URL:** <https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653>\
**Category:** General Usage\
**Tags:** question, complex-numbers\
**Created:** [April 29, 2019, 3:19pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653 "2019-04-29T15:19:35Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![M.Zahirul\_ALAM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m.zahirul_alam/32/8221_2.png) [@M.Zahirul\_ALAM](https://discourse.julialang.org/u/M.Zahirul_ALAM)\
**Post date:** [April 29, 2019, 3:19pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/1 "2019-04-29T15:19:35Z")

</div>

I haven’t been using Julia for a year now. And this morning I am struggling to save a two-dimensional array of complex numbers in a file in Julia so that I can open it up in Mathematica. What is the easiest way of doing this?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 29, 2019, 4:11pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/2 "2019-04-29T16:11:21Z")

</div>

It looks like Mathematica supports reading from CSV files, so perhaps you can just do:

```julia
julia> using DelimitedFiles

julia> M = rand(ComplexF64, 3, 5)
3×5 Array{Complex{Float64},2}:
  0.27002+0.970939im 0.153381+0.512542im 0.67822+0.371012im 0.272543+0.45637im 0.313847+0.138268im
 0.104664+0.140601im 0.525328+0.617633im 0.712583+0.473099im 0.321276+0.854511im 0.883507+0.41321im 
 0.732629+0.0214171im 0.822763+0.360661im 0.791205+0.94645im 0.954784+0.0411716im 0.0109759+0.3415im  

julia> writedlm("test.csv", M)

```

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 29, 2019, 4:57pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/3 "2019-04-29T16:57:42Z")

</div>

It’s not elegant, but for imperoperability’s sake saving the real and imaginary parts separately is pretty simple. You can get them with `real.(A)` and `imag.(A)` respectively. If you already have a way of getting real matrices into mathematica, you can use whatever that is on the real and imaginary parts separately, then simply add them in mathematica.

---

<div class="post-metadata">

**Author:** ![M.Zahirul\_ALAM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m.zahirul_alam/32/8221_2.png) [@M.Zahirul\_ALAM](https://discourse.julialang.org/u/M.Zahirul_ALAM)\
**Post date:** [April 29, 2019, 6:19pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/4 "2019-04-29T18:19:51Z")

</div>

The problem is in Mathematica complex numbers are denoted with “i” not im.

---

<div class="post-metadata">

**Author:** ![M.Zahirul\_ALAM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m.zahirul_alam/32/8221_2.png) [@M.Zahirul\_ALAM](https://discourse.julialang.org/u/M.Zahirul_ALAM)\
**Post date:** [April 29, 2019, 6:20pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/5 "2019-04-29T18:20:13Z")

</div>

that could do. But seems very clumsy. Why would there not be a simpler solution. It bothers me. I don’t know about other scientific disciplines, but physics is filled with complex numbers (and so is EEE). So it seems odd that complex numbers get pushed to the side.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 29, 2019, 6:29pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/6 "2019-04-29T18:29:25Z")

</div>

If anything, complex numbers are “pushed aside” by the IEEE standards on which computers operate, not by either of Julia or mathematica which both handle complex numbers quite well. Because there isn’t a standard binary (or unicode, for that matter) representation of a complex number, there isn’t really a standard format which allows Julia and mathematica to communicate with each other.

Therefore, unless you know of a standard data format that can be output and read by both, your only options are to have a custom output from Julia, a custom parser in mathematica, or both.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 29, 2019, 9:44pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/7 "2019-04-29T21:44:16Z")

</div>

If that’s the only problem, you can postprocess the file and remove all m’s:

```julia
cat test.csv | tr -d 'm' > mathematica.csv

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [April 30, 2019, 10:52am UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/8 "2019-04-30T10:52:03Z")

</div>

I originally thought you’d either have to teach Julia “how to Mathematica”, or teach Mathematica “how to Julia”, since there’s no obvious common format. But perhaps they can meet halfway. You could do something like this in Julia:

```julia
using DelimitedFiles
M = rand(ComplexF64, 42, 42)
mcomplex(c) = string(real(c), ((imag(c) > 0) ? "+" : ""), imag(c), "*I")
writedlm("/tmp/test.csv", mcomplex.(M), ',')

julia-1.1> M[1, 1]
0.1625286359541478 + 0.0675447712375079im
julia-1.1> mcomplex(M[1, 1])
"0.1625286359541478+0.0675447712375079*I" 

```

and then do something like this in Mathematica:

 ![31](https://global.discourse-cdn.com/julialang/original/3X/7/6/76576071ad61ff7afd6bfe78f45b19a29a5929db.png)

Using `ToExpression` was the only way I could convince Mathematica that the strings in the CSV file are to be interpreted as numbers. I’m hoping that this isn’t the easiest way to do it, though, because it does feel clumsy…

---

<div class="post-metadata">

**Author:** ![M.Zahirul\_ALAM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m.zahirul_alam/32/8221_2.png) [@M.Zahirul\_ALAM](https://discourse.julialang.org/u/M.Zahirul_ALAM)\
**Post date:** [April 30, 2019, 3:41pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/9 "2019-04-30T15:41:33Z")

</div>

I could wrap the Import command with ToExpression[Import[“test.csv”, “Data”]]. That’s not too difficult to do. Thanks very much. I think this solution will do the job for now.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 30, 2019, 3:58pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/10 "2019-04-30T15:58:39Z")

</div>

The root problem here is that there seems to be no standardized, widely used file format for interchange of complex-number data.

For CSV files, Matlab reads `b+bi`, Python reads `a+bj`, Julia reads both but also `a+bim` which is what it writes, Mathematica needs `a+b*I` followed by `ToExpression`, …

HDF5 files, inexplicably, don’t have a standard complex-number type, and neither does NetCDF.

`.mat` files _do_ have a documented complex number format, but it would be stretch to call these a “standard”. Mathematical only reads [version 4 and 5 `.mat` files](https://reference.wolfram.com/language/ref/format/MAT.html), whereas Julia can only _write_ [version 7.3 `.mat` files](https://github.com/JuliaIO/MAT.jl), so that’s no help here. Python also only [knows about earlier `.mat` formats](https://scipy-cookbook.readthedocs.io/items/Reading_mat_files.html). (Version 7.3 `.mat` files are actually HDF5, which is great except that the lack of a standard complex-number type in HDF5 means that generic HDF5 software like `h5py` won’t recognize the complex numbers as such.)

In the near term, it might be nice if `MAT.jl` could output Matlab v5 `.mat` files for interchange purposes. In the long term, I wish HDF5 would standardize a complex-number datatype, but it’s been 20 years and they haven’t gotten around to it yet so I wouldn’t hold my breath unless there is major external pressure (ideally accompanied by code PRs supplied to HDF5).

---

<div class="post-metadata">

**Author:** ![jkleinh](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jkleinh](https://discourse.julialang.org/u/jkleinh)\
**Post date:** [April 30, 2019, 7:33pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/11 "2019-04-30T19:33:49Z")

</div>

Has anyone thought about adding complex number support to [HDF5.jl](https://github.com/JuliaIO/HDF5.jl) along the same lines as is done in [h5py](https://github.com/h5py/h5py) or is that out of scope for the package? The h5py implementation is [here](https://github.com/h5py/h5py/blob/master/h5py/h5t.pyx#L1210) and [here](https://github.com/h5py/h5py/blob/master/h5py/h5t.pyx#L1517) and doesn’t look too heavy. Compatibility between HDF5.jl and h5py for complex types would be convenient.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [April 30, 2019, 9:47pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/12 "2019-04-30T21:47:47Z")

</div>

@jkleinh: I have written a h5py compatible version here:  
[https://github.com/MagneticParticleImaging/MPIFiles.jl/blob/master/src/Utils.jl#L5](https://github.com/MagneticParticleImaging/MPIFiles.jl/blob/master/src/Utils.jl#L5)

and

here:  
[https://github.com/MagneticParticleImaging/MPIFiles.jl/blob/master/src/Utils.jl#L42](https://github.com/MagneticParticleImaging/MPIFiles.jl/blob/master/src/Utils.jl#L42)

I have thought as well if we should just integrate this into HDF5.jl.

---

<div class="post-metadata">

**Author:** ![jkleinh](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jkleinh](https://discourse.julialang.org/u/jkleinh)\
**Post date:** [May 1, 2019, 12:03am UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/13 "2019-05-01T00:03:40Z")

</div>

I would be in favor of following h5py here and integrating this with HDF5.jl since it seems to be fairly basic and universally useful functionality that people coming from python would expect to just work. Also h5py has had this functionality for a while and doesn’t seem to have run into any major problems with their approach as far as I can tell.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [May 1, 2019, 8:51am UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/14 "2019-05-01T08:51:57Z")

</div>

Yes, we probably then should follow h5py as well in their way how the store boolean values. @jkleinh could you open an issue to HDF5.jl so that we can discuss this there? My read implementation should probably be adapted since `readmmap` is not type stable anymore but this is doable using the low-level interface (similar to what I do here [https://github.com/MagneticResonanceImaging/MRIReco.jl/blob/master/src/IO/ISMRMRD.jl#L9](https://github.com/MagneticResonanceImaging/MRIReco.jl/blob/master/src/IO/ISMRMRD.jl#L9))

---

<div class="post-metadata">

**Author:** ![jkleinh](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jkleinh](https://discourse.julialang.org/u/jkleinh)\
**Post date:** [May 1, 2019, 3:48pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/15 "2019-05-01T15:48:29Z")

</div>

@tobias.knopp it looks like there is already an [issue](https://github.com/JuliaIO/HDF5.jl/issues/180) open for this. I made a comment to gauge support for implementing this.

---

<div class="post-metadata">

**Author:** ![jkleinh](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jkleinh](https://discourse.julialang.org/u/jkleinh)\
**Post date:** [July 18, 2019, 7:09pm UTC](https://discourse.julialang.org/t/saving-an-array-of-complex-numbers/23653/16 "2019-07-18T19:09:00Z")

</div>

I’ve just now made a [pull request](https://github.com/JuliaIO/HDF5.jl/pull/558) to HDF5.jl implementing the h5py strategy for dealing with complex numbers. Interested in what people think. Hopefully this can be merged.
