Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Complex numbers in fortran language

1,406 views
Skip to first unread message

jza...@gmail.com

unread,
Jun 10, 2015, 3:14:40 PM6/10/15
to
If I wanted to multiply a number by i=(-1)^1/2, how would I do this using a Fortran 95 compiler?

Gordon Sande

unread,
Jun 10, 2015, 3:30:00 PM6/10/15
to
On 2015-06-10 19:14:37 +0000, jza...@gmail.com said:

> If I wanted to multiply a number by i=(-1)^1/2, how would I do this
> using a Fortran 95 compiler?

The literal answer to your question is by use of the multiplication operator.

However that is probably not really your question! Be warned that use of
the multiplication operator has some overhead. You will have to write a
program.
You will need to declare some variables where the intrinsic Fortran data type
COMPLEX may well be of use. You will either have to define some data
using literal
constants or read some data. And you will also have to provide some
output which
would include the results of your use of the multipliction operator.
Many Fortran
tutorials are available which will help with this overhead.

You may well need fragments of program like

COMPLEX :: eye, sam, joe

eye = ( 0.0, 1.0 )

joe = eye * sam


Please be sure to give credit to the newsgroup so they can benefit from
your homework.






Richard Maine

unread,
Jun 10, 2015, 3:33:59 PM6/10/15
to
<jza...@gmail.com> wrote:

> If I wanted to multiply a number by i=(-1)^1/2, how would I do this using
> a Fortran 95 compiler?

I'm thinking there must be more to this question than is obvious, but I
can't guess exactly what. Perhaps a general tutorial on complex type
might be in order, but that seems a bit much for a newsgroup post. The
question seems strangely specific if it is asking how to use complex
type in general.

I suppose I could give a literal answer to the exact question asked, but
that's unlikely to be of much use. For the nothing that it is probably
worth (and none of this has changed since at least Fortran 66, I might
add):

Multiplication in Fortran is done with the * operator and the complex
number i is written (in single precision) as (0.0,1.0). So to multiply
number by i, write

number*(0.0,1.0)

This has obvious optimizations in special cases. For example, if number
is real instead of complex, you could just write the result as

(0.0, number)

Somehow I doubt these answers are going to be of much use, however,
because someone who didn't already know this is unlikely to be prepared
to do much useful with the resulting complex value.

--
Richard Maine
email: last name at domain . net
dimnain: summer-triangle

Jos Bergervoet

unread,
Jun 11, 2015, 2:52:01 AM6/11/15
to
On 6/10/2015 9:33 PM, Richard Maine wrote:
> <jza...@gmail.com> wrote:
>
>> If I wanted to multiply a number by i=(-1)^1/2, how would I do this using
>> a Fortran 95 compiler?
>
> I'm thinking there must be more to this question than is obvious, but I
> can't guess exactly what.

I remember that when I was a student I had the same question.
We were using Algol68 in our course, and the complex numbers
were not part of the course and therefore not explained in
the material we were given.

So I just tried to use print(( sqrt(-1) )) in a program. From
there, some more experimenting and looking at the error messages
from the compiler I was eventually able to figure out that the
type was called "COMPL" and how the literals had to be written.

But this approach might not work in Fortran. The A68 compiler
apparently "promoted" the -1 to COMPL (because it considered
the sqrt as "strong context", or some A68 rule like that). But
f95 would not accept sqrt(-1), and also sqrt(-1.0) would not
give a complex result.

So in f95 you don't get it by just asking for it. Perhaps that
was the background of the question? (After all, just asking
does work in a lot of other languages, like Mathematica, matlab,
etc.)

--
Jos


robin....@gmail.com

unread,
Jun 11, 2015, 7:43:35 AM6/11/15
to
On Thursday, June 11, 2015 at 4:52:01 PM UTC+10, Jos Bergervoet wrote:
> On 6/10/2015 9:33 PM, Richard Maine wrote:
> > <j.no...@gmail.com> wrote:
> >
> >> If I wanted to multiply a number by i=(-1)^1/2, how would I do this using
> >> a Fortran 95 compiler?
> >
> > I'm thinking there must be more to this question than is obvious, but I
> > can't guess exactly what.
>
> I remember that when I was a student I had the same question.
> We were using Algol68 in our course, and the complex numbers
> were not part of the course and therefore not explained in
> the material we were given.
>
> So I just tried to use print(( sqrt(-1) )) in a program. From
> there, some more experimenting and looking at the error messages
> from the compiler I was eventually able to figure out that the
> type was called "COMPL" and how the literals had to be written.
>
> But this approach might not work in Fortran. The A68 compiler
> apparently "promoted" the -1 to COMPL (because it considered
> the sqrt as "strong context", or some A68 rule like that). But
> f95 would not accept sqrt(-1), and also sqrt(-1.0) would not
> give a complex result.
>
> So in f95 you don't get it by just asking for it.

print *, CSQRT(-1)

or

complex :: c
c = -1
print *, sqrt (c)

Beliavsky

unread,
Jun 11, 2015, 8:13:51 AM6/11/15
to
On Thursday, June 11, 2015 at 7:43:35 AM UTC-4, robin....@gmail.com wrote:
> On Thursday, June 11, 2015 at 4:52:01 PM UTC+10, Jos Bergervoet wrote:
> > On 6/10/2015 9:33 PM, Richard Maine wrote:
> > > <j.no...@gmail.com> wrote:
> > >
> > >> If I wanted to multiply a number by i=(-1)^1/2, how would I do this using
> > >> a Fortran 95 compiler?
> > >
> > > I'm thinking there must be more to this question than is obvious, but I
> > > can't guess exactly what.
> >
> > I remember that when I was a student I had the same question.
> > We were using Algol68 in our course, and the complex numbers
> > were not part of the course and therefore not explained in
> > the material we were given.
> >
> > So I just tried to use print(( sqrt(-1) )) in a program. From
> > there, some more experimenting and looking at the error messages
> > from the compiler I was eventually able to figure out that the
> > type was called "COMPL" and how the literals had to be written.
> >
> > But this approach might not work in Fortran. The A68 compiler
> > apparently "promoted" the -1 to COMPL (because it considered
> > the sqrt as "strong context", or some A68 rule like that). But
> > f95 would not accept sqrt(-1), and also sqrt(-1.0) would not
> > give a complex result.
> >
> > So in f95 you don't get it by just asking for it.
>
> print *, CSQRT(-1)

gfortran says

print*,csqrt(-1.0)
1
Error: Type of argument 'x' in call to 'csqrt' at (1) should be COMPLEX(4), not REAL(4)

and g95 says something similar.

robin....@gmail.com

unread,
Jun 11, 2015, 12:01:22 PM6/11/15
to
On Thursday, June 11, 2015 at 10:13:51 PM UTC+10, Beliavsky wrote:
I suppose that that is expecting too much, that
it might actually be generic.

Of course, with SQRT, the argument MUST be COMPLEX,
as the generic mechanism cannot resolve to complex,
given only a non-complex argument.

glen herrmannsfeldt

unread,
Jun 11, 2015, 1:58:28 PM6/11/15
to
Jos Bergervoet <jos.ber...@xs4all.nl> wrote:

(snip)
> I remember that when I was a student I had the same question.
> We were using Algol68 in our course, and the complex numbers
> were not part of the course and therefore not explained in
> the material we were given.

(snip)

> But this approach might not work in Fortran. The A68 compiler
> apparently "promoted" the -1 to COMPL (because it considered
> the sqrt as "strong context", or some A68 rule like that). But
> f95 would not accept sqrt(-1), and also sqrt(-1.0) would not
> give a complex result.

> So in f95 you don't get it by just asking for it. Perhaps that
> was the background of the question? (After all, just asking
> does work in a lot of other languages, like Mathematica, matlab,
> etc.)

Many interpreted languages use dynamic typing, where the type of
a variable changes depending on the value assigned to it.

In that case, sqrt() can return a different type depending on
it arugument. That is rare in compiled languages.

-- glen

John Harper

unread,
Jun 11, 2015, 9:20:49 PM6/11/15
to
But Fortran can do that because sqrt is generic. The following program:

program testsqrt
implicit none
real:: x = 1
complex:: z = -1
print *,sqrt(x),sqrt(z)
end program testsqrt

when compiled with g95 prints

1. (0.,1.)

Of course sqrt(integer) is not a standard intrinsic but you can always write
your own function to calculate it, with a suitable generic interface block.

glen herrmannsfeldt

unread,
Jun 12, 2015, 12:27:27 AM6/12/15
to
John Harper <john....@vuw.ac.nz> wrote:

(snip, someone wrote)
>>> So in f95 you don't get it by just asking for it. Perhaps that
>>> was the background of the question? (After all, just asking
>>> does work in a lot of other languages, like Mathematica, matlab,
>>> etc.)

>> Many interpreted languages use dynamic typing, where the type of
>> a variable changes depending on the value assigned to it.

>> In that case, sqrt() can return a different type depending on
>> it arugument. That is rare in compiled languages.

> But Fortran can do that because sqrt is generic. The following program:

> program testsqrt
> implicit none
> real:: x = 1
> complex:: z = -1
> print *,sqrt(x),sqrt(z)
> end program testsqrt

Yes, but Fortran can't do generics that depend on the value of
the argument, like sqrt(1.) is real, but sqrt(-1.) should be complex.
Many interpreted languages, and also hand calculators can do that.

It is interesting what calculators won't do, too.

The T -84 plus will generate complex values for sqrt, logs, and
exponentials, but won't do asin(2.)

> when compiled with g95 prints

> 1. (0.,1.)

> Of course sqrt(integer) is not a standard intrinsic but you can
> always write your own function to calculate it, with a suitable
> generic interface block.

I believe that generic functions should figure out what to do
with integers.

PL/I generics know what to do with fixed point (integer or not)
arguments. For the usual transcendental functions, it converts
the argument to floating point and calls the floating point routine.

But partly that is needed as fixed point doesn't have to be integer.
The value 1.1 is fixed decimal with one digit after the decimal point
in PL/I, and there is no reason not to use it for SQRT or SIN.

C and Java will convert arguments as appropriate, such that one can
use sqrt() or Math.sin(), respectively, on integers.

-- glen

Jos Bergervoet

unread,
Jun 12, 2015, 2:58:56 AM6/12/15
to
On 6/12/2015 6:27 AM, glen herrmannsfeldt wrote:
> John Harper <john....@vuw.ac.nz> wrote:
>
> (snip, someone wrote)
>>>> So in f95 you don't get it by just asking for it. Perhaps that
>>>> was the background of the question? (After all, just asking
>>>> does work in a lot of other languages, like Mathematica, matlab,
>>>> etc.)
>
>>> Many interpreted languages use dynamic typing, where the type of
>>> a variable changes depending on the value assigned to it.
>
>>> In that case, sqrt() can return a different type depending on
>>> it arugument. That is rare in compiled languages.
>
>> But Fortran can do that because sqrt is generic. The following program:
>
>> program testsqrt
>> implicit none
>> real:: x = 1
>> complex:: z = -1
>> print *,sqrt(x),sqrt(z)
>> end program testsqrt
>
> Yes, but Fortran can't do generics that depend on the value of
> the argument, like sqrt(1.) is real, but sqrt(-1.) should be complex.

And I should add that on second thought Algol68 most likely
could not do this either. (Context analysis, although it was
elaborate, would not go as far as dealing with values occurring
for the argument.)

So probably in my old experiment I had to guess the name of
the type first (not too difficult if INT and REAL are known
to exist) and then cast -1 to that type and take the root:
print(( sqrt(COMPL -1) ))
An when that worked I could define a constant
COMPL Ima = sqrt(COMPL -1);
which was enough to work with complex numbers by writing
them as a+b*Ima and using the existing operators (since in
A68 they all were generics from the start).

At that point I did not know that instead of 3+4*Ima, one
can write 3 I 4 (or something similar, it was) to denote a
number. Somehow I found that out later but it is not essential.

So if you can guess the name of the type, and the language
has all operators defined as generics already, that's all
you need. This works in f90 as well!

complex :: test = -1 ! <-- this you have to guess
print *, sqrt(test)
end

Of course this my sound strange, in an era where you just
can Google "Fortran complex numbers" or "Algol68 complex
numbers" to see how they are used if you don't know it,
but in 1977 the procedure was different.

--
Jos
0 new messages