Forran format specifier for complex number

Is it possible to specify a format specifier for a complex number in fortran? I have a simple program.

program complx1
implicit none
complex :: var1
var1 = (10,20)
write (*,*) var1
write (*,'(F0.0)') var1
write (*,'(F0.0,A,F0.0)') real(var1), ' + i ' , aimag(var1)
end program complx1

      

Output:

 (  10.0000000    ,  20.0000000    )
10.
20.
10. + i 20.

      

I wanted to use inline format for a+bi

with some format specifier, not manually (second last line of the program). Obviously F0.0

not working. Any ideas?

EDIT: I don't think this is a duplicate of the post: A complex matrix entry in fortran that says to use the REAL

and functions AIMAG

. I have already used these functions and am wondering if there is a built-in format that can do the job.

+3


source to share


2 answers


Adding to @ francescalus' existing and mostly satisfactory answer. Format string such as

fmt = '(F0.0,SP,F0.0,"i")'

      



should result in displaying a complex number with the correct sign between the real and imaginary parts; no need to mess with strings to get a plus sign there.

+6


source


There is no separate complex edit descriptor. In Fortran 2008, 10.7.2.3.6, we see

A complex coordinate system consists of a pair of separate real data. Complex type scalar date editing is specified by two edit descriptors, each of which specifies real data editing.

In your second example, which you say doesn't work, you see this formatting in action. Since the format had only one descriptor without recounting, the values ​​are output in different records (formatting).

The first of your three cases is very special: it uses list-oriented inference. Withdrawal rules:

Complex constants are enclosed in parentheses with a separator between the real and imaginary parts

There is another useful part of the mentioned first rule:

Control and edit descriptors of a character string may be processed between the edit descriptor for the real part and the edit descriptor for the imaginary part.

You can happily accommodate your second try, as we note that your "not working" was not due to the use of the most complex variable (and not from real and imaginary components).

write (*, '(F0.0,"+i",F0.0)') var1

      



It is, however, wrong when you have a potentially negative tricky part. You will need to change the sign in the middle. This is possible by using a symbolic (rather than literal) variable format with a conditional, but it might not be worth the effort. See the other answer for information on a different approach, similar to your third option, but more reliable.

Another option is to write a function that returns a well-written symbolic representation of your complex variable. This is similar to your third option. This is also the least messy approach when you want to write a lot of complex variables.

Finally, if you need to worry about the negative tricky parts, but you want a simple specification of a list of variables, there is a really ugly one

write(*,'(F0.0,"+i*(",F0.0,")")') var1

      

or figurative

character(19) fmt
fmt = '(F0.0,"+",F0.0,"i")'
fmt(8:8) = MERGE('+',' ',var1%im.gt.0)
write(*,fmt) var1

      

or even more efficient use of a control edit descriptor SP

, as indicated by Mark's high performance answer , which temporarily (during an exit statement) sets the digit transfer mode to PLUS

, which causes an optional "+" to be printed. [Alternatively, this can be specified for the join itself open

using a qualifier sign='plus'

.]

This all has to do with the fact that the simple answer is no, there is no inline complex edit handle.

+3


source







All Articles