## Printing a complex matrix Fortran

The complex matrix is declared this way:

complex(8) :: matrix(:,:)

How can I print this matrix with each element as: (a, b) or a+ib, and in a nxn format? (by that I mean as a square matrix, with a row per line, so there will be n rows and n columns)

This is the way I would print a real matrix with the format I want:

do i=1,n do j=1,n write(*, fmt="(f0.2, tr2)", advance="no") matrix(i,j) end do write(*, fmt="(a)") " " end do

But I'm not sure how to translate this to a complex matrix

How can I print this matrix with each element as: (a, b)

Supposing you already know that `(a b)`

is the default printing fotmat for `complex`

type, Why isn't this just enough?

do j=1,n write(*, *) matrix(:,j) end do

The output would be something like:

(10.000000000000000,-20.000000000000000) (10.000000000000000,-20.000000000000000) (10.000000000000000,-20.000000000000000) (10.000000000000000, 20.000000000000000) (10.000000000000000, 20.000000000000000) (10.000000000000000, 20.000000000000000)

If you want something more customized, you could try something like this (adjusting the field width and precision):

do j=1,n write(*, "(*('('sf6.2xspf6.2x'i)':x))") matrix(:,j) end do

That produces something like this:

( 10.00 -20.00 i) ( 10.00 -20.00 i) ( 10.00 -20.00 i) ( 10.00 +20.00 i) ( 10.00 +20.00 i) ( 10.00 +20.00 i)

**Printing a Fortran Array with write,** How to print an array on a single line in Fortran 77. Printing a Fortran Array with write. February 6, 2006. Fortran 77, by default, includes a newline after every write statement. This can be a problem if you want to print a number of elements on the same line, but you don’t know how many elements there will be at compile time.

Here is something that has worked in the past, it could be fine tuned quite a bit

!! compile and link with gfortran -I/usr/include -o PrintComplex PrintComplex.f90 Program PrintComplex use, intrinsic :: iso_c_binding implicit none integer, parameter :: N=16 integer :: k real (kind=c_double) :: val(N) complex (kind=c_double_complex) :: in(N) character(19) fmt fmt = '(F7.2,"+",F7.2,"i")' val=(/(sin(3.14159d0*float(k)/3.d0),k=1,N)/) in=cmplx(val,-val/2) print *,"in" do k=1,N fmt(8:8) = MERGE('+',' ',imag(in(k)).gt.0) write(*,fmt)in(k) end do End Program PrintComplex

The output is:

in 0.87 -0.43i 0.87 -0.43i 0.00 -0.00i -0.87+ 0.43i -0.87+ 0.43i -0.00+ 0.00i 0.87 -0.43i 0.87 -0.43i 0.00 -0.00i -0.87+ 0.43i -0.87+ 0.43i -0.00+ 0.00i 0.87 -0.43i 0.87 -0.43i 0.00 -0.00i -0.87+ 0.43i

**[PDF] Pretty-printing matrices in standard Fortran 95 using Dispmodule,** The module can display variables of default kind of all intrinsic types (integer, real , complex, logical and character), and add- on modules are provided for data of� A standard Fortran 95 module for printing scalars, vectors and matrices to external files is described. The module can display variables of default kind of all intrinsic types (integer, real, complex, logical and character), and add-

So far this is what has worked for me. Taking into account Clinton's advice:

character(19) fmt fmt = '(F7.2,"+",F7.2,"i")' do i=1,n do j=1,n fmt(8:8) = MERGE('+',' ',imag(a(i,j)).gt.0) write(*,fmt, advance="no") a(i,j) end do write(*, fmt="(a)") " " end do

And the output is:

-0.26 -0.00i -0.00 -0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00+ 0.00i -0.25 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i -0.05 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i -0.05 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00+ 0.00i -0.46 -0.00i -0.00+ 0.00i -0.00+ 0.00i -0.50 0.00i 0.00 -0.00i 0.00 0.00i -0.00 0.00i 0.32+ 0.00i -0.00+ 0.00i 0.00 -0.00i -0.00 -0.00i 0.00+ 0.00i -0.27 0.00i 0.00 0.00i 0.00 0.00i -0.00+ 0.00i 0.00 -0.00i -0.00 -0.00i 0.24+ 0.00i 0.00 0.00i 0.00 0.00i -0.18 0.00i 0.00 0.00i 0.00 0.00i -0.00 -0.00i 0.24+ 0.00i 0.00 -0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i -0.18 0.00i

If someone has a better suggestion I'll be glad to hear it :)

**[PDF] FORTRAN 90,** Write a fortran 90 program to addition and multiplication two complex numbers x & y, also find the conjugate of x , i*x*y, and print the real and imaginary part of y. Output from an exception handler is unpredictable. If you make your own exception handler, do not do any FORTRAN output from it. If you must do some, then call abort right after the output. Doing so reduces the relative risk of a program freeze. FORTRAN I/O from an exception handler amounts to recursive I/O. See the next point.

**COMPLEX,** The GNU Fortran Compiler: COMPLEX. program test_complex integer :: i = 42 real :: x = 3.14 print *, complex(i, x) end program test_complex. See also:. To create a 5 x 5 two-dimensional array of integers named matrix, you write − integer, dimension (5,5) :: matrix You can also declare an array with some explicit lower bound, for example − real, dimension(2:6) :: numbers integer, dimension (-3:2,0:4) :: matrix Assigning Values. You can either assign values to individual members, like,

**[PDF] fortran 90,** Write a fortran 90 program to addition and multiplication two complex numbers x & y, also find the conjugate of x , i*x*y, and print the real and imaginary part of y. m is the number of character spaces reserved for printing (including the sign if there is one) If the actual width is less than m, blanks are printed; n is the number of integers to output per line. If omitted, one number is output per line. Floating point Specification. General form : nfm.d. Right justified

Fortran - Basic Input Output - We have so far seen that we can read data from keyboard using the read * statement, and display output to the screen using the print* statement, respectively.

##### Comments

- Please ask one question per post. Or at least keep one specific topic. The conjg() is completely unrelated to printing (and yes, it is elemental). What is the nxn format? Hav you tried anything?
- @VladimirF Ok I took off the other question and explained what I meant by that format
- What did you try? Please read "How to ask" and "Minimum Viable Example" under the Help section, then
`edit`

your question so we may efficiently help. - @SherylHohman Np, already made the changes you suggested