|
Prev: Call Array valued Fortran function from C
Next: Advice on how to keep track of allocated memory
From: Ron Shepard on 20 Apr 2008 10:44 In article <u0jm049f123f0c3ntnvi9dqbgo162dtvac(a)4ax.com>, Luka Djigas <ldigas@@gmail.com> wrote: > Anyway, I read the help. But didn't manage to figure it out. > As far as I can see, sum can be used to summ an entire array in one or > more dimensions. How can one sum just some elements from, for example > one dimensional array ? Here is a description with examples: http://gcc.gnu.org/onlinedocs/gfortran/SUM.html If you want a subset of an array, or you want a nonunit stride, then use the colon notation. $.02 -Ron Shepard
From: dpb on 20 Apr 2008 10:48 Luka Djigas wrote: .... > As far as I have gone with the sum intristic is that it can summ all > elements in specified dimensions of an array. > > I didn't understand (no examples in help either) how can one sum just > some elements from an array ... My online doc w/ CVF has an example with the SUM() intrinsic documentation. I'm not sure what you're wanting the sum of specifically enough to attempt a specific example. The optional mask argument can be used it the elements you wish to sum can be selected via some logical operation. Look up "Mask expressions" and keyword in the help to read about how they work. If there are specific entries in the array to be excluded by position, perhaps the use of array sections would be of some benefit. Look in the help on array subscripts, sections, etc., and read up on them some. A more precise problem definition could lead to a specific solution no doubt... --
From: David Frank on 20 Apr 2008 11:05 "Luka Djigas" <ldigas@@gmail.com> wrote in message news:pu4l04dnt3apg1hf1a1jvesp0gf3jttiol(a)4ax.com... > What would be the shortest way to sum some elements of an array? For > example from 5 to 50. > Gee whiz, all these replies and no-one shows him what he asked for. asum = SUM(a(5:50)) It already been determined that compilers allowed to optimize will implement SUM inline resulting in just as fast and maybe faster than your do loop. IMO a shorter syntax is clearer (but some disagree about that)..
From: dpb on 20 Apr 2008 11:21 David Frank wrote: > "Luka Djigas" <ldigas@@gmail.com> wrote in message > news:pu4l04dnt3apg1hf1a1jvesp0gf3jttiol(a)4ax.com... >> What would be the shortest way to sum some elements of an array? For >> example from 5 to 50. >> > > Gee whiz, all these replies and no-one shows him what he asked for. > > asum = SUM(a(5:50)) > I missed that... :) I saw a response that used 5,50 in a loop but didn't realize it was the actual question. The reply to look at array sections was spot on, though, wasn't it?? :) --
From: jamesgiles on 20 Apr 2008 17:10
On Apr 20, 12:29 am, "Gerry Ford" <ge...(a)nowhere.ford> wrote: > real :: a(50) ... > a(j) = 1.0 * j Just out of curiosity, why the multiply? Surely you don't think mixed mode multiply is more legible than direct mixed mode assignment. Or is it that you want to remind yourself that A is a REAL? In the latter case though, wouldn't a(j)=real(j) be better? -- J. Giles "I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies." -- C. A. R. Hoare |