making even and odd index parameter? 
Author Message
 making even and odd index parameter?

Hi,

Is something like this (in Matlab) possible in fortran?

integer, parameter, dimension(4) :: odd(1,3,5,7)
integer, parameter, dimension(4) :: even(2,4,6,8)

Or simply:

even_index = [2 4 6 8 10]
odd_index = [1 3 5 7 9]

And then index every second number in somearrays:

somearray(even_index) = somearray(even_index) * 2.0
somearray(odd_index) = somearray(odd_index) * 2.0

Is it possible? I get compiler error when I try it...

Regards,
Martin



Mon, 10 May 2010 09:38:58 GMT  
 making even and odd index parameter?

Quote:

> integer, parameter, dimension(4) :: odd(1,3,5,7)
> integer, parameter, dimension(4) :: even(2,4,6,8)

This is almost valid, but doesn't mean what you think. The (1,3,5,7) is
not an array of values. It is the dimensions, overriding the
dimension(4). And you don't specify a value at all, which is what makes
it invalid. What you want is more like

  integer, parameter, dimension(4) :: odd = [1,3,5,7]

where I'm using the f2003 square bracket array constructor syntax. If
your compiler doesn't accept that, substitute (/1,3,5,7/), but do note
that you cannot use just (1,3,5,7).

Quote:
> even_index = [2 4 6 8 10]
> odd_index = [1 3 5 7 9]

That is fine if you declared even_index and odd_index appropriately. You
do, however, need declarations.

Quote:
> somearray(even_index) = somearray(even_index) * 2.0
> somearray(odd_index) = somearray(odd_index) * 2.0

This is ok, once you defined even_index and odd_index appropriately. But
there is a lot simpler way. Just do

  somearray(2::2) = somearray(2::2) * 2.0  !-- For the even ones
  somearray(1::2) = somearray(1::2) * 2.0  !-- For the odd ones

Quote:
> Is it possible? I get compiler error when I try it...

It helps to say what the compiler error is... and what code it goes
with, as you show multiple alternatives above.

--
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain



Mon, 10 May 2010 10:50:16 GMT  
 making even and odd index parameter?

Quote:


>> integer, parameter, dimension(4) :: odd(1,3,5,7)
>> integer, parameter, dimension(4) :: even(2,4,6,8)

> This is almost valid, but doesn't mean what you think. The (1,3,5,7) is
> not an array of values. It is the dimensions, overriding the
> dimension(4). And you don't specify a value at all, which is what makes
> it invalid. What you want is more like

>   integer, parameter, dimension(4) :: odd = [1,3,5,7]

Aha. Thanks.

Quote:
> where I'm using the f2003 square bracket array constructor syntax. If
> your compiler doesn't accept that, substitute (/1,3,5,7/), but do note
> that you cannot use just (1,3,5,7).

>> even_index = [2 4 6 8 10]
>> odd_index = [1 3 5 7 9]

> That is fine if you declared even_index and odd_index appropriately. You
> do, however, need declarations.

Ok.

Quote:
>> somearray(even_index) = somearray(even_index) * 2.0
>> somearray(odd_index) = somearray(odd_index) * 2.0

> This is ok, once you defined even_index and odd_index appropriately. But
> there is a lot simpler way. Just do

>   somearray(2::2) = somearray(2::2) * 2.0  !-- For the even ones
>   somearray(1::2) = somearray(1::2) * 2.0  !-- For the odd ones

That's much better, thanks.

Quote:
>> Is it possible? I get compiler error when I try it...

> It helps to say what the compiler error is... and what code it goes
> with, as you show multiple alternatives above.

Well, I see you figured it out anyway. If you want to see the specific
error messages, which I personally find irrelevant, you could just make
a test-file and try what I suggested (which ofcourse wasn't very good
suggestions because I didn't knew any better)...

Even though I didn't try your suggestions yet, I would like to thank you
for your help because AFAICS it looks like you know what you're talking
about and I don't see any reason why your suggestions shouldn't work...

Thanks.

Regards,
Martin



Tue, 11 May 2010 06:23:45 GMT  
 making even and odd index parameter?

Quote:


> > It helps to say what the compiler error is... and what code it goes
> > with, as you show multiple alternatives above.

> Well, I see you figured it out anyway. If you want to see the specific
> error messages, which I personally find irrelevant, you could just make
> a test-file and try what I suggested

One could do a lot of things, but

1. We didn't have exactly whatever you tried. We just had isolated lines
out of context. The declaration stood pretty well alone, but for the
other lines, context was pretty much everything. Those lines would have
worked in the right context, namely with appropriate declarations. In
fact a *VERY* frequent reply to requests for help here is to see the
declarations that the poster failed to show, insomuch as the problems
are about as often in the declarations as in the code shown.

2. The messages might not mean much to you, but they *DO* generally mean
something to the people you are asking for help. Even if the messages
are completely off-base, an experienced progarmmer can sometimes
recognize what the compiler appears to be confused about and thsu reason
back to where the likely problem is.

3. When asking for help, it works a lot better if you do the work to
make things simpler for the person helping you. Saying that someone else
could just go generate a test case himself in order to help you is not
likely to be the most fruitful way to get help. Specifically, if I
hadn't been able to guess the problem without it, no I would not have
bothered.

All this is just for future reference, of course.

--
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain



Tue, 11 May 2010 06:48:58 GMT  
 making even and odd index parameter?
Quote:

> Well, I see you figured it out anyway. If you want to see the specific
> error messages, which I personally find irrelevant,

[...snip..]

You think error messages generated by compilers are irrelevant? I guess we'll be hearing a
lot more from you in this newsgroup. :o)

Just kidding, of course. I'm assuming by "irrelevant" you meant the messages are difficult
to interpret (usually true), rather than "unrelated to the matter being considered."
(typically not true. :o)

cheers,

paulv



Fri, 14 May 2010 23:25:27 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. J: Even and Odd

2. Odd and Even conjunctions

3. Odd and Even functions missing in RB

4. Even/odd week number

5. Printing Odd or Even page only

6. Odd or Even

7. determine whether a number is odd or even

8. Even/Odd rounding

9. Even/Odd rounding

10. odd /even addressing?

11. even vs. odd numbers

12. number representations (was: even vs. odd numbers)

 

 
Powered by phpBB® Forum Software