help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] Printing complex matrices


From: Patrick Alken
Subject: Re: [Help-gsl] Printing complex matrices
Date: Sun, 2 Oct 2016 11:45:21 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0

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
<address@hidden>\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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]