From: Nikolaus Rath on
Hello,

I have just started to learn Fortran (mainly from the "Fortran 95/2003
Explained" book by Metcalf et al) and there are a couple of things
that seem really odd to me. It'd be great if someone could clarify the
following issues for me:

In case it matters: I am using the gfortran compiler and I am willing
to use whatever nonstandard extensions it may have.

1. Are there really no ++, +=, -= operators as shortcuts for i = i + 1
and i = i + n?

2. Is there really no loop control statement except "stop"? I am
missing statements do redo the loop (jump to the beginning and do
not update the loop variable) and to start the next iteration
(update loop variable and jump to beginning of loop).

3. Is there no command to simply print things like

"Position of particle 1 at t=23s: x_x=23, x_y=11"? It seems that

the only way to write this in Fortran seems to be using something like

print "(a,1x,i0,1x,a,f0,a,f0,a,f0)", "Pos..", i, "at t=", t, ....

(since the * format looks just awful). I would really like
something like the C printf statement so that I can just write

printf "Position of particle %d at t=%d s: x_x=%f, x_y=%f", &
i, t, x(0), x(1)

4. Are there any (free) modules that implement basic vector algebra,
like cross product, length etc in different coordinate systems?


Thanks in advance!

-Nikolaus

--
Nikolaus(a)rath.org | College Ring 6, 28759 Bremen, Germany
Class of 2008 - Physics | Jacobs University Bremen

»My opinions may have changed, but not the fact that I am right.«
From: Ian Bush on

Hi Nikolaus,

As if by magic, Nikolaus Rath appeared !

> Hello,
>
> I have just started to learn Fortran (mainly from the "Fortran 95/2003
> Explained" book by Metcalf et al) and there are a couple of things
> that seem really odd to me. It'd be great if someone could clarify the
> following issues for me:
>
> In case it matters: I am using the gfortran compiler and I am willing
> to use whatever nonstandard extensions it may have.
>

Personally I would avoid extensions, you are trying to learn the
language and it's much better to avoid being locked into "one true
compiler"

> 1. Are there really no ++, +=, -= operators as shortcuts for i = i + 1
> and i = i + n?
>

Nope.

> 2. Is there really no loop control statement except "stop"? I am
> missing statements do redo the loop (jump to the beginning and do
> not update the loop variable) and to start the next iteration
> (update loop variable and jump to beginning of loop).
>

Cycle will do the second of these, in fact in a more powerful
way than the C equivalent. You also want to look at Exit.

The first suggests that you don't quite understand fortran do loops,
at least those of the form

Do i = 1, n

End Do

Unlike C for loops you can always say in Fortran a priori the (maximum)
number of times the loop is executed (maximum because of the possibility
of exit or a go to). To do something like you are suggesting, and to be
honest
I can't think where I would want to do it, I would do something like

i = 0
Do
...
If( some_condition ) Then
Cycle
End If
...
i = i + 1
If( i > i_max ) Then
Exit
End If
End Do

> 3. Is there no command to simply print things like
>
> "Position of particle 1 at t=23s: x_x=23, x_y=11"?

Yes - use free format ( i.e. the * format )

> It seems that
>
> the only way to write this in Fortran seems to be using something like
>
> print "(a,1x,i0,1x,a,f0,a,f0,a,f0)", "Pos..", i, "at t=", t, ....
>
> (since the * format looks just awful).

Oh.

> I would really like
> something like the C printf statement so that I can just write
>
> printf "Position of particle %d at t=%d s: x_x=%f, x_y=%f", &
> i, t, x(0), x(1)
>

To be honest ultimately I don't really see the difference, formatting
by its very nature is fiddly. However you might prefer

Write( *, "( 'Position of particle ', i0, ' at t =', f0, &
&'s: x_x=', f0, 'x_y=', f0 )" ) i, t, x( 0 ), x( 1 )

( untested - but I hope you get the drift )

> 4. Are there any (free) modules that implement basic vector algebra,
> like cross product, length etc in different coordinate systems?
>

Don't know about this, nothing show up on google ?

Hope this helps,

Ian

From: Arjen Markus on
On 3 apr, 13:01, Ian Bush <I.J.B...(a)ku.ca.ld> wrote:
>
> > 3. Is there no command to simply print things like
>
> >    "Position of particle 1 at t=23s: x_x=23, x_y=11"?
>
> Yes - use free format ( i.e. the * format )
>

That should be: list-directed.

It is a trifle unruly way of producing output,
but it works (you have no control over the formatting).

Note that Fortran's input and output statements are
actually statements, not functions/subroutines as in C.
As a consequence they are far more robust (in my opinion)
than C's printf() and scanf().

Regards,

Arjen
From: Nikolaus Rath on
Ian Bush <I.J.Bush(a)ku.ca.ld> writes:
>> 2. Is there really no loop control statement except "stop"? I am
>> missing statements do redo the loop (jump to the beginning and do
>> not update the loop variable) and to start the next iteration
>> (update loop variable and jump to beginning of loop).
>>
>
> Cycle will do the second of these, in fact in a more powerful way
> than the C equivalent. You also want to look at Exit.

Actually, "exit" is what I wanted to write above - sorry for the
confusion. Cycle does indeed what I need and as it turns out it is
also described in my book. No idea how I overlooked it - thanks.

>> I would really like
>> something like the C printf statement so that I can just write
>>
>> printf "Position of particle %d at t=%d s: x_x=%f, x_y=%f", &
>> i, t, x(0), x(1)
>>
>
> To be honest ultimately I don't really see the difference, formatting
> by its very nature is fiddly. However you might prefer
>
> Write( *, "( 'Position of particle ', i0, ' at t =', f0, &
> &'s: x_x=', f0, 'x_y=', f0 )" ) i, t, x( 0 ), x( 1 )
>
> ( untested - but I hope you get the drift )

Yes, that already looks better.

>> 4. Are there any (free) modules that implement basic vector algebra,
>> like cross product, length etc in different coordinate systems?
>
> Don't know about this, nothing show up on google ?

Not really. I only found some really basic module on
http://www.nag.co.uk/nagware/Examples.asp, but it only works in
cartesian coordinates.


Best,

-Nikolaus

--
Nikolaus(a)rath.org | College Ring 6, 28759 Bremen, Germany
Class of 2008 - Physics | Jacobs University Bremen

»My opinions may have changed, but not the fact that I am right.«
From: John Harper on
In article <0f11e4fc-0eae-4afe-819e-51e60be1ddb9(a)i36g2000prf.googlegroups.com>,
Arjen Markus <arjen.markus(a)wldelft.nl> wrote:
>On 3 apr, 13:01, Ian Bush <I.J.B...(a)ku.ca.ld> wrote:
>>
>> Yes - use free format ( i.e. the * format )
>
>That should be: list-directed.

What's evil about "* format"? F95 section 9.4.1.1 and F2003 section
9.5.1.1 both say "If format is *, the statement is a list-directed
input/output statement." IMHO the benefit of "* format" is that it
should prevent the error, seen all too often here, of confusing
list-directed with unformatted input/output.

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington 6140, New Zealand
e-mail john.harper(a)vuw.ac.nz phone (+64)(4)463 6780 fax (+64)(4)463 5045