Implicit recursive array construction, F90

In summary: External functions are allowed, but their names must not match the names of the variables they are called from. In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,In summary,
  • #1
Boltzmann
7
2
Hello there,
I again have a problem with programming in Fortran 90.1. Homework Statement

I would like to construct an array with an implicit do loop.
I know how to get the array I want to with usual do-loop but I would like to do it without do loops.

So this is the array I want to construct:

a(0:f)
a(0)=4
DO i=1,f
a(i)=a(i-1)+4*(-1)**i*1/(2*i+1)
ENDDO

So, I want get this array, without using an explicit do-loop.
For example, I know, that the implicit do-loops work like this:

a=(/(i+1,i=1,5)/)

But it doesn't work with recursion. So what I've tried for my array is:

a(0)=4
a=(/(a(i-1)+4*(-1)**i*1/(2*i+1),i=1,f)/)

It doesn't work at all.

Does someone know how to use this in the right way or maybe any other solutions to construct the array without an explicit do-loop.Many thanks in advance.

greets Boltzmann
 
Physics news on Phys.org
  • #2
I don't see why this should work. The implied do on the right-hand side will be executed before the assignment, and hence can't be recursive.

Note also that as written, the code probably doesn't calculate what you want. 1/(2*i+1) will be calculated as an integer, and hence can only return 1 if i=0, 0 otherwise. You have to use 1./(2.*i+1.)
 
  • #3
Boltzmann said:
Hello there,
I again have a problem with programming in Fortran 90.1. Homework Statement

I would like to construct an array with an implicit do loop.
I know how to get the array I want to with usual do-loop but I would like to do it without do loops.

So this is the array I want to construct:

a(0:f)
a(0)=4
DO i=1,f
a(i)=a(i-1)+4*(-1)**i*1/(2*i+1)
ENDDO

So, I want get this array, without using an explicit do-loop.
For example, I know, that the implicit do-loops work like this:

a=(/(i+1,i=1,5)/)

But it doesn't work with recursion. So what I've tried for my array is:

a(0)=4
a=(/(a(i-1)+4*(-1)**i*1/(2*i+1),i=1,f)/)

It doesn't work at all.

Does someone know how to use this in the right way or maybe any other solutions to construct the array without an explicit do-loop.Many thanks in advance.

greets Boltzmann

The actual term of art in Fortran is "implied do-loops", not "implicit do-loops".

AFAIK, implied do-loops can only be used in certain situations for initializing array variables and for input/output involving arrays. I don't think your recursive use of the implied-do loop fits either of those scenarios. It's also not clear why just a plain old do-loop would not suffice.

Here are some examples of using implied do-loops:

http://www.personal.psu.edu/jhm/f90/lectures/19.html#1 {scroll down to the section entitled Array Initialization}
 
  • #4
First of all thanks for answering.

Thanks for your note Claude. I will change this but I also got the right values writing it like integers.
So is there any other way, I could use the recursive code with?

@SteamKing

I later recognized, that its implied do and not implicit after reading some more about it. I am sorry for this.

Usually a plain do-loop would suffice but its part of my homework, to realize it without explicit do-loops (do , enddo).

Right now I solved the problem by using two functions but I don't think that it's the way how my teacher wanted me to do.
1. Function
ho=4.*(-1)**r*1./(2.*r+1.)+ho1(x)
2. Function
ho1=ho(y-1)

a=(/(ho(i),i=0,f)/)

So this works but I think there has to be way a better way.Some other question. If a function returns an array. Who can I access to single values and not the whole array. I need it for a proper output.
So if a is an array I can just use a(i) but how does it work if for example add(x) is a function and an array. How can I get access to add(x)(i) ?
 
Last edited:
  • #5
Boltzmann said:
Some other question. If a function returns an array. Who can I access to single values and not the whole array. I need it for a proper output.
So if a is an array I can just use a(i) but how does it work if for example add(x) is a function and an array. How can I get access to add(x)(i) ?

It's not clear what you are describing here.

AFAIK, function names must not be identical to variable names in Fortran. You can, for example, declare a real array ADD (N), where N = some integer, or you can define a FUNCTION ADD ("argument list"), but you can't do both:

http://www.chem.ox.ac.uk/fortran/subprograms.html {See the section External Functions}
 
  • Like
Likes Boltzmann

Related to Implicit recursive array construction, F90

1. What is implicit recursive array construction in F90?

Implicit recursive array construction in F90 is a feature that allows for the creation of arrays without explicitly specifying the size or shape of the array. Instead, the size and shape are determined by the values assigned to the array during initialization.

2. How does implicit recursive array construction work in F90?

In F90, implicit recursive array construction works by using a set of rules to determine the size and shape of the array based on the values assigned to it. These rules take into account the number of elements and their dimensions to determine the final size and shape of the array.

3. What are the advantages of using implicit recursive array construction in F90?

One advantage of using implicit recursive array construction in F90 is that it allows for more concise and readable code, as the array size and shape do not need to be explicitly defined. This can also save time and effort in cases where the array size or shape may change frequently, as it does not need to be updated in multiple places.

4. Are there any limitations to using implicit recursive array construction in F90?

Yes, there are some limitations to using implicit recursive array construction in F90. This feature is only applicable to arrays with a known and consistent structure, and it cannot be used with arrays that have varying sizes or dimensions. It also cannot be used with arrays that have non-standard data types.

5. How is implicit recursive array construction different from explicit array construction in F90?

Explicit array construction in F90 involves explicitly defining the size and shape of an array before assigning values to it. This allows for more control over the array structure, but it can also be more time-consuming and less flexible compared to implicit recursive array construction, which determines the size and shape based on the assigned values.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Computing and Technology
Replies
2
Views
746
Back
Top