LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2012, 08:19 AM   #1
bldcerealkiller
LQ Newbie
 
Registered: Aug 2011
Posts: 16

Rep: Reputation: Disabled
Question FORTRAN90 - Update of arrays in a loop


Hi everybody, I'm struggling to find a way out hope you can help me, or at least tell me that is possible to do it. Anyway any suggestion is appreciated.

I'm trying to update a code written by someone else. In this portion of the code I will obtain one final array which is updated with values obtained from a series of calculations
nested in a do loop.

The question is: is it possible to obtain the separate individual values obtained from the calculations
for each step of the loop instead of having the final array, which is actually the result of sum of the individual calculations after every cycle of the loop?

Otherwise, is it possible to create different separate arrays with every cycle of a loop?


Sorry for being unclear, and thank you in advance for every suggestion/advice.
 
Old 06-20-2012, 08:50 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
The general form for doing something like this is, if I understand the question, to use an index value to see an individual element of an array (single- or multidimensional).

So, if you had an array of 1,000 elements and wanted to substitute or view the 565th element you wound refer to it as
Code:
x = array (565)
or
Code:
array(565) = x
(No, FORTRAN does not have to be upper case.)

In a loop you'd do the same thing, just looping until you hit the right element.

If you're working with a multidimensional array, you'd want to look at (or replace) the 565th row and second column
Code:
array (i, j) = x
where i = 565 and j = 2, and
Code:
x = array (i, j)
to use the value.

Keep in mind that FORTRAN arrays start at 1 versus C arrays that start at 0.

That what you're trying to do?

Hope this helps some.
 
Old 06-20-2012, 10:10 AM   #3
bldcerealkiller
LQ Newbie
 
Registered: Aug 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks for your reply tronayne but is not what I'm trying to do. I'll try to be more clear.
I have an array (f_array) which is updated at every cycle of a loop, briefly the code looks like this:

Code:
...

do i =1,20

[...operations to obtain "number1", "number2" etc.]

f_array(i)        = f_array(i)     + number1
f_array(i+x)      = f_array(i+x)   + number2
f_array(i+2*x)    = f_array(i+2*x) + number3

end do
So as you can see, in the end i'll get f_array which includes the result of all of the iterations of the loop.
But, I'd like to be able to separate and look at each single contribution for each cycle of the loop.
Is it possible to do that, for example, creating every time a separate array (different from f_array,
let's say from 1 to 20) for each cycle of the loop?

Thanks again
 
Old 06-21-2012, 08:28 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Sure. Just create another array, same size, same type. Call it, say, s_array.

After the end of your existing loop, add another loop
Code:
do i = 1, 20
     s_array(i) = f_array(i)
end do
Then print it, write it to a file or something so you can look at it.

There's probably some function to do an array copy but I haven't done FORTRAN for... well, a long time and I don't recall but it's simple enough to just copy.

Hope this helps some.
 
Old 06-24-2012, 06:01 AM   #5
bldcerealkiller
LQ Newbie
 
Registered: Aug 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Yes thank you I was thinking about something like that.
Cheers
 
Old 06-24-2012, 04:07 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Fortran supports array slicing, so if you have n values for this iteration in s_array, you can add them to corresponding values in f_array using a single statement:
Code:
f_array(1:n) = f_array(1:n) + s_array(1:n)
Note that you can use a slicing to initialize an array, too:
Code:
f_array(1:n) = 0.0
If your calculation is done using only PURE functions, for example using mycalculation(), then you can use
Code:
FORALL (i = 1:n)
    s_array(i) = mycalculation(arguments...)
END FORALL

f_array(1:n) = f_array(1:n) + s_array(1:n)
which turns out to compile to quite efficient code.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - Creating Arrays using loop counter Mixiul Programming 4 02-08-2012 09:05 AM
Bash - Arrays and the for-loop Orangutanklaus Linux - General 2 10-23-2009 01:04 PM
[SOLVED] vectors in fortran90 aihaike Programming 2 04-25-2009 06:53 AM
[SOLVED] How to get Fortran90 compiler nelufarup Linux - Software 17 02-19-2009 03:46 AM
fortran90 kostis Programming 3 11-06-2006 02:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration