Matt, I initially had in mind to output the real and imaginary matrices separately, instead of the equivalent real matrix. Pierre, if you think there is value on it, I can contribute an implementation of "Mat[Real,Imaginary]PartLoad()" as you suggested, instead of solving the problem on my side with Python etc.. Regards, Frederico. From: "Matthew Knepley" <[email protected]> To: "Pierre Jolivet" <[email protected]> Cc: "Frederico Teixeira" <[email protected]>, "petsc-users" <[email protected]> Sent: Wednesday, May 12, 2021 1:35:55 PM Subject: Re: [petsc-users] Binary format in real vs. complex scalar type configurations On Wed, May 12, 2021 at 2:17 AM Pierre Jolivet < [ mailto:[email protected] | [email protected] ] > wrote: BQ_BEGIN On 11 May 2021, at 6:26 PM, Matthew Knepley < [ mailto:[email protected] | [email protected] ] > wrote: On Tue, May 11, 2021 at 12:03 PM Pierre Jolivet < [ mailto:[email protected] | [email protected] ] > wrote: BQ_BEGIN Hello Frederico, I’m not sure that’s possible. Here is what I do, it makes me sick, but mixing precisions/scalar types with PETSc is difficult (crossing my fingers this will be better with future). In MATLAB (after putting petsc/share/petsc/matlab in the path): A = PetscBinaryRead('your_binary_mat_with_re+im.dat','complex',true); % scalar-type=complex PetscBinaryWrite('re.dat',real(A)); % scalar-type=real PetscBinaryWrite('im.dat',imag(A)); % scalar-type=real So what you want to happen is that MatLoad() looks at the datatype, sees that it is complex and PetscScalar is real, and returns two matrices with the real and imaginary parts? The hard part is that the MatLoad interface returns a single matrix. BQ_END There is Mat[Real,Imaginary]Part(), maybe there could be Mat[Real,Imaginary]PartLoad()? It will be inefficient (complex Mat read twice in a very naive implementation), but functional and would not require tinkering with the options (but maybe you had something clear in mind). Also, just for reference, [ https://gitlab.com/petsc/petsc/-/issues/901 | https://gitlab.com/petsc/petsc/-/issues/901 ] . Frederico, as Barry wrote, it will work with Octave or Python. In fact, it will work in any code that does not include petsc.h or link to libpetsc. If you want to stick to C, I think you could simply copy/paste the MatLoad_SeqAIJ_Binary() implementation [ https://www.mcs.anl.gov/petsc/petsc-current/src/mat/impls/aij/seq/aij.c.html... | https://www.mcs.anl.gov/petsc/petsc-3.15.0/src/mat/impls/aij/seq/aij.c.html#... ] and replace PETSC_SCALAR line 4861 by PETSC_COMPLEX. Then, as Matt wrote, instead of assembling a single Mat, assemble two Mats by splitting a->a in two PetscScalar arrays (remember that this is for your scalar-type=real configuration). BQ_END Should this make two matrices, or the equivalent real matrix? / R Q \ \ -Q R / THanks, Matt BQ_BEGIN Thanks, Pierre BQ_BEGIN I guess we could have a flag that says what to do with complex numbers (read real, read imaginary, read norm, etc.) and you could read it twice. Would that work? Thanks, Matt BQ_BEGIN Thanks, Pierre BQ_BEGIN On 11 May 2021, at 3:30 PM, Frederico Teixeira < [ mailto:[email protected] | [email protected] ] > wrote: Dear fellows, I hope this message finds you safe and well. I have a complex-valued matrix and its real/imaginary components in binary format . They were extracted from a solver that only works with "scalar-type=complex" configuration. I am getting weird results when I load them into a small test program that's configured with "scalar-type=real", but I believe this is expected. At the end of the day, I would like to have both real and imaginary components as real-valued matrices. Is it possible to do it? I want to test preconditioners that are tailored for this sort of problem. Regards, Frederico. BQ_END BQ_END -- What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead. -- Norbert Wiener [ http://www.cse.buffalo.edu/~knepley/ | https://www.cse.buffalo.edu/~knepley/ ] BQ_END BQ_END -- What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead. -- Norbert Wiener [ http://www.cse.buffalo.edu/~knepley/ | https://www.cse.buffalo.edu/~knepley/ ]