Szabolcs summed it up pretty well but there is also another popular convention to store complex numbers in HDF5 as compound type with two members being real numbers.
So starting from M11.1 you can import it like this:
In[1]:= Import["ExampleData/sample1.h5", "Complex/Complex64"]
Out[1]= <|"re" -> 0.58215809, "im" -> -0.92684859|>
[UPDATE for M12.0]
Both Import
and Export
take an option called "ComplexKeys"
that can trigger automatic conversion of such compound types to complex numbers. With "ComplexKeys" -> {X, Y}
, where X
and Y
are different Strings, Import
will convert all values of the form <|X -> a, Y -> b|>
to complex numbers (assuming a
and b
are real numbers). So
In[1]:= Import["ExampleData/sample1.h5", "Complex/Complex64",
"ComplexKeys" -> {"re", "im"}]
Out[1]= 0.58215809 - 0.92684859 I
And similarly, when given "ComplexKeys" -> {X, Y}
, Export
will save complex numbers a+bI
into <|X -> a, Y -> b|>
:
In[3]:= Export["test.h5", {1 + 3 I, Pi - 2.6*I}, "ComplexKeys" -> {"Re", "Im"}]
Out[3]= "test.h5"
In[4]:= Import["test.h5", "Dataset1"]
Out[4]= {<|"Re" -> 1., "Im" -> 3.|>, <|"Re" -> 3.1415927, "Im" -> -2.6|>}
In[5]:= Import["test.h5", "Dataset1", "ComplexKeys" -> {"Re", "Im"}]
Out[5]= {1. + 3. I, 3.1415927 - 2.6 I}
"DataFormat"
. $\endgroup$ – b.gates.you.know.what Oct 24 '14 at 12:29