Unfortunately its not possible to make a gsl_matrix_complex_real
function since the matrix rows are expected to be contiguous in memory
(so you can't skip elements like with vectors). Here is a function I
defined to print matrices in a format compatible with matlab/octave:
void
printc_octave(const gsl_matrix_complex *m, const char *str)
{
FILE *fp;
size_t i, j;
const size_t N = m->size1;
const size_t M = m->size2;
fp = fopen(str, "w");
if (!fp)
return;
fprintf(fp, "%% Created by Octave 2.1.73, Tue Aug 01 15:00:27 2006 MDT
<blah@blah>\n");
fprintf(fp, "%% name: %s\n", str);
fprintf(fp, "%% type: complex matrix\n");
fprintf(fp, "%% rows: %zu\n", N);
fprintf(fp, "%% columns: %zu\n", M);
for (i = 0; i < N; ++i)
{
for (j = 0; j < M; ++j)
{
gsl_complex z = gsl_matrix_complex_get(m, i, j);
fprintf(fp,
"(%.12e,%.12e)%s",
GSL_REAL(z),
GSL_IMAG(z),
(j < M - 1) ? " " : "\n");
}
}
fclose(fp);
}
On 09/28/2016 10:58 AM, Dimitrova, Maria wrote:
> Hello,
>
> What is the right way to print a complex matrix? I tried defining a function,
> however, separating the real and the imaginary components turned out
> impossible. Is there a way to define an analog of
> gsl_vector_complex_real(gsl_vector_complex *v) for matrices?
>
> void printArrC(gsl_matrix_complex *matrix, int nrows)
> {
> int i;
> printf("\n");
> for (i=0;i<nrows;i++)
> {
> gsl_vector_view row = gsl_matrix_row (matrix, i);
> gsl_vector_fprintf(stdout,&row.vector,"%f");
> printf("\n\n");
> }
> printf("\n\n");
> }
>
> Best regards,
> Maria