newbie constant matrix assignment question
Author Message
newbie constant matrix assignment question

Greetings,

I need to supply a 1-D constant array of doubles
to a function. I can do it in f90 this way:

double precizion x(5)

x(1)=111
x(2)=222
...
x(5)=555

call myfunc(x)

where myfunc is the function of 1-d array of length 5.

I'm a newbie in fortran.
In C/C++ and Pascal i used to write something like:

x = (111, 222, 333, ..., 555).

Is something similar available in f90?

br,
Anton.

Mon, 13 Mar 2006 21:02:41 GMT
newbie constant matrix assignment question

Quote:

> Greetings,

> I need to supply a 1-D constant array of doubles to a function. I can do
> it in f90 this way:

> double precizion x(5)

> x(1)=111
> x(2)=222
> ...
> x(5)=555

> call myfunc(x)

> where myfunc is the function of 1-d array of length 5.

> I'm a newbie in fortran.
> In C/C++ and Pascal i used to write something like:

> x = (111, 222, 333, ..., 555).

> Is something similar available in f90?

> thanks a lot in advance,

> br,
> Anton.

i mean that is there any shorter syntax to do the
matrix assigment like in this example, in f90?

I would like to avoid this

x(1)=...
...
x(N)=N.

notation. Is it possible?

br,
Anton.

Mon, 13 Mar 2006 21:04:54 GMT
newbie constant matrix assignment question

Quote:

> > Greetings,

> > I need to supply a 1-D constant array of doubles to a function. I
can do
> > it in f90 this way:

> > double precizion x(5)

> > x(1)=111
> > x(2)=222
> > ...
> > x(5)=555

> > call myfunc(x)

> > where myfunc is the function of 1-d array of length 5.

> > I'm a newbie in fortran.
> > In C/C++ and Pascal i used to write something like:

> > x = (111, 222, 333, ..., 555).

> > Is something similar available in f90?

> > thanks a lot in advance,

> > br,
> > Anton.

> i mean that is there any shorter syntax to do the
> matrix assigment like in this example, in f90?

> I would like to avoid this

> x(1)=...
> ...
> x(N)=N.

> notation. Is it possible?

> br,
> Anton.

In current standard you can use:

x = (/ 111,222,...,555/)

in current CVF (and apparently in F2003 standard)
you can substitute   [ 111,222,,,,]  for the stupid (/ /)  syntax.

Mon, 13 Mar 2006 21:36:19 GMT
newbie constant matrix assignment question
|| Greetings,
||
|| I need to supply a 1-D constant array of doubles to a function. I can do
|| it in f90 this way:
||
|| double precizion x(5)
||
|| I'm a newbie in fortran.
|| In C/C++ and Pascal i used to write something like:
||
|| x = (111, 222, 333, ..., 555).
||
|| Is something similar available in f90?

x = (/ 111.d0, 222.d0, 333.d0, ..., 555.d0 /)

Automatic conversion doesn't take place, so you have to use
double-precision constants as above. The preferred way in F90
is actually to use kind-parameters:

//Usually you'll put something like this in a module:
integer, parameter:: dp = SELECTED_REAL_KIND(13) !>=13 digits precision
...
real(dp):: x(5)
...
x = (/ 111._dp, 222._dp, 333._dp, ..., 555._dp /)

--
Jugoslav
___________
www.geocities.com/jdujic

Mon, 13 Mar 2006 21:42:33 GMT
newbie constant matrix assignment question

Quote:

> In current standard you can use:

> x = (/ 111,222,...,555/)

> in current CVF (and apparently in F2003 standard)
> you can substitute   [ 111,222,,,,]  for the stupid (/ /)  syntax.

And CVF provides automatic integer to double precision in above
context,
as long as ALL are integer,
(no mixed data specifications [ 111, 222.22 ]  )

Mon, 13 Mar 2006 22:04:20 GMT
newbie constant matrix assignment question

Quote:

>> Greetings,

>> I need to supply a 1-D constant array of doubles to a function. I can do
>> it in f90 this way:

>> double precizion x(5)

>> x(1)=111
>> x(2)=222
>> ...
>> x(5)=555

>> call myfunc(x)

>> where myfunc is the function of 1-d array of length 5.

>> I'm a newbie in fortran.
>> In C/C++ and Pascal i used to write something like:

>> x = (111, 222, 333, ..., 555).

>> Is something similar available in f90?

>> thanks a lot in advance,

>> br,
>> Anton.

>i mean that is there any shorter syntax to do the
>matrix assigment like in this example, in f90?

>I would like to avoid this

>x(1)=...
>...
>x(N)=N.

>notation. Is it possible?

>br,
>Anton.

I haven't tried this ... but  if they are constants does

work ? (final comma a precaution .. maybe not necessary.)

Chris

Mon, 13 Mar 2006 16:33:29 GMT
newbie constant matrix assignment question

Quote:

>  work ? (final comma a precaution .. maybe not necessary.)

ooops .... should have been ... maybe

Mon, 13 Mar 2006 16:36:40 GMT
newbie constant matrix assignment question

Quote:

> > In current standard you can use:

> > x = (/ 111,222,...,555/)

> > in current CVF (and apparently in F2003 standard)
> > you can substitute   [ 111,222,,,,]  for the stupid (/ /)  syntax.

> And CVF provides automatic integer to double precision in above
> context,
> as long as ALL are integer,
> (no mixed data specifications [ 111, 222.22 ]  )

<BUT>, to OP, note <carefully> the reference to CVF here--to rely on
that as a standard behavior will likely bite...

Mon, 13 Mar 2006 21:45:47 GMT
newbie constant matrix assignment question

Quote:

> x = (/ 111.d0, 222.d0, 333.d0, ..., 555.d0 /)

> Automatic conversion doesn't take place, so you have to use
> double-precision constants as above.  The preferred way in F90
> is actually to use kind-parameters:

Eh?  Where do you get the impression that automatic conversion doesn't
take place?  Yes, it does.  If you prefer to avoid it for reasons
of style, clarity, etc, I can well understand that, but that's a
very different matter from the conversion not being specified by
the language.

I suspect that what is confusing you is that you are not realizing
exactly where the conversion takes place.  The individual array
elements are not converted, thus

(/ 111, 222, 333, 444, 555 /)

is an integer array rather than a double precision one.  The
conversion takes place when the assignment is done.

What you can't do (until f2003) is have mixed types inside of
the array constructor.  Each element of the constructor has to
be of the same type (and type parameters).  This, you can't
write

(/ 111, 222d0, 333 /)

The compiler wouldn't know what type this array ought to be.  And
while you can eyeball this one and give the obvious answwer, and a
compiler probably could also, it is possible to write cases that are
*FAR* more complicated, subtle, and downright {*filter*}.  The standard
disallows anything along this line; all the elements just have to be
of the same type.

The F2003 draft does provide another conversion-related option here.
You can explicitly specify the type and type parameters of an
array constructor.  This does allow you the conversion to be done
element by element so that you could write something like (using
the nicer square brackets, since this is f2003 anyway)

[double precision:: 111, 222d0, 333]

I'll agree if you comment that this looks sort of pointless; why bother?
It is more interesting in the case of character array constructors,
allowing you to write, for example

[character*16:: 'tom', 'dick', 'harry']

which may be a little wordy, but is sure a lot better than

(/ 'tom             ','dick            ','harry           ' /)

which is what you have to write now (if I counted correctly).

--
Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain

Mon, 13 Mar 2006 23:32:44 GMT
newbie constant matrix assignment question

Quote:

> || Greetings,
> ||
> || I need to supply a 1-D constant array of doubles to a function. I can do
> || it in f90 this way:
> ||
> || double precizion x(5)
> ||
> || I'm a newbie in fortran.
> || In C/C++ and Pascal i used to write something like:
> ||
> || x = (111, 222, 333, ..., 555).
> ||
> || Is something similar available in f90?

> x = (/ 111.d0, 222.d0, 333.d0, ..., 555.d0 /)

> Automatic conversion doesn't take place, so you have to use
> double-precision constants as above. The preferred way in F90
> is actually to use kind-parameters:

Sure it does.  You can always do DOUBLE = INTEGER, whether or
not the left hand side is an array doesn't matter.  The conversion
takes place for any compatible types.  What won't happen is
conversion within the array constructor.  You can't do
(/1,2.0,3.0D0.../)

Dick Hendrickson

- Show quoted text -

Quote:

> //Usually you'll put something like this in a module:
> integer, parameter:: dp = SELECTED_REAL_KIND(13) !>=13 digits precision
> ...
> real(dp):: x(5)
> ...
> x = (/ 111._dp, 222._dp, 333._dp, ..., 555._dp /)

> --
>  Jugoslav
> ___________
> www.geocities.com/jdujic

Mon, 13 Mar 2006 23:55:02 GMT
newbie constant matrix assignment question

Quote:

and asks whether it will work.

Actually, no....though it is slightly subtle.  Enough so that you may
get people mistaken;y answering yes.  You might think it would.  You
wouldn't be alone.  I've mistakenly tried to use constants as internal
files myself for one reason or another.  I've seen other people ask
here why it doesn't work.  I've probably even told them that it will
work (as I almost told you :-)) and then been corrected by others.  An
internal file is required to be a variable.

To the inevitable "why" question, the only think I can speculate
(and it is speculation) is that

1. It pretty clearly does need to be a variable for internal
writes.  Probably simplified at least the standard-speak
to just define a single kind of internal file instead of
giving separate requirements for internal input vs output
files.

2. Probably people didn't think it would be very useful if
they thought of it at all.  Add this to point 1 and it might
have been judged not worth the bother.

Some compilers will probably accept it, but I know that some
won't (having seen compilers reject my attempts).

Of course, you could make the idea work by copying the constant
into a string variable (or perhaps using it to initialize the
string variable).  But the whole approach seems pretty
complicated and roundabout compared to just writing the array
constructor, which is a direct parallel to the Pascal construct

I = nint(sqrt(real(j)**2))

to copy the value of J into I (subject to limitations), but why
would I?

--
Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain

Mon, 13 Mar 2006 23:50:37 GMT
newbie constant matrix assignment question

Quote:

> and asks whether it will work.

> Actually, no....though it is slightly subtle.  Enough so that you may
> get people mistaken;y answering yes.  You might think it would.  You
> wouldn't be alone.  I've mistakenly tried to use constants as internal
> files myself for one reason or another.  I've seen other people ask
> here why it doesn't work.  I've probably even told them that it will
> work (as I almost told you :-)) and then been corrected by others.  An
> internal file is required to be a variable.

Before Fortran 77 you couldn't put constants or expressions in the I/O list
for write statements.

-- glen

Tue, 14 Mar 2006 00:24:50 GMT
newbie constant matrix assignment question

Quote:
> Greetings,

> I need to supply a 1-D constant array of doubles
> to a function. I can do it in f90 this way:

> double precizion x(5)

> x(1)=111
> x(2)=222
> ...
> x(5)=555

> call myfunc(x)

> where myfunc is the function of 1-d array of length 5.

> I'm a newbie in fortran.
> In C/C++ and Pascal i used to write something like:

> x = (111, 222, 333, ..., 555).

In the olden days, I would have used the DATA statement for this.  That is
initialization values, not an assignment, but often that would work.

-- glen

Tue, 14 Mar 2006 00:28:36 GMT
newbie constant matrix assignment question

Quote:

> Before Fortran 77 you couldn't put constants or expressions in the I/O list
> for write statements.

I'd either forgotten or never knew that one, but I see you are correct.
Of course, before f77, there were lots of places where you couldn't
use expressions, or could use only very special-case forms of
expressions.

For example, you could use N+1 as an array index, but you couldn't
use 1+N.  You couldn't use either one as a DO loop limit.

I recall it as being a feature of f77 that I was really fond of at the
time - the ability to use an expression in most places where one would
make sense.  F66 code tended to have a lot of temporary variables that
served no purpose other than as workarounds for places where you
wanted an expression; you assigned the expression value to the
variable and then put the variable where you wanted the expression.
Variables with names like NP1 (N plus 1) were ubiquitous.

--
Richard Maine | Good judgment comes from experience; email: my
first.last at org.domain | experience comes from bad judgment.  org:
nasa, domain: gov | -- Mark Twain

Tue, 14 Mar 2006 00:41:57 GMT
newbie constant matrix assignment question

Quote:

>and asks whether it will work.

>Actually, no....though it is slightly subtle.  Enough so that you may

... you're right .. I just tried it ... (WATCOM F77) ... too bad.
Chris

Mon, 13 Mar 2006 19:21:56 GMT

 Page 1 of 2 [ 23 post ] Go to page: [1] [2]

Relevant Pages