Beliavsky wrote:
>> AFAIK if f(x) is an array-valued function one can't access an element
>> by saying (for example)
>> PRINT *,f(x)(1)
>> but must be more long-winded, by declaring an array g of the same shape
>> as f, with elements of the same type, and then saying
>> g = f(x)
>> PRINT *,g(1)
>
> To slice an expression one could define a function
>
> function slice(ivec,i1,i2) result(jvec)
> ! return the slice ivec(i1:i2), avoiding out-of-bounds
> ! array access
> integer, intent(in) :: ivec(:)
> integer, intent(in) :: i1,i2
> integer, allocatable :: jvec(:)
> integer :: j1,j2,nj
> j1 = max(lbound(ivec,dim=1),i1)
This sort of works, but it's not very convenient.
The lower bound of an assumed shape array, like IVEC(:), is
always 1.
So, if f(x) doesn't return a 1-based subscripted array, the
caller will have to pass in the lower bound, or do arithmetic on
i1 and j1 on the call side.
Dick Hendrickson
> j2 = min(ubound(ivec,dim=1),i2)
> nj = j2 - j1 + 1
PS: I'd worry a little here about trying to get a zero sized slice.
I think everything works, but I'd probably stick in an
nj = max(nj,0)
just to be sure.
> allocate (jvec(nj))
> jvec = ivec(j1:j2)
> end function slice
>
> and write
>
> slice(f(x),i1,i2)
>
> One can also write a function that gets a component of a derived type,
> to which an expression could be passed. It would be convenient to make
> such a function ELEMENTAL.
>
> An advantage of writing a function is that one can check that the
> slice is within the bounds of the array.
>
>> If f is of type character one could also imagine 3 pairs of parentheses:
>> PRINT *,f(x)(1:2)(3:4)
>> to print characters 3 and 4 of elements 1 and 2 of the array f(x)
>>
>> Does some possible ambiguity I haven't thought of prevent allowing
>> that sort of thing?
>
> Octave (and I assume Matlab) allow this, as does R, with slightly
> different syntax. Here is an example.
>
> octave:1> x = [10,20,30]
> x =
>
> 10 20 30
>
> octave:2> y = x(2:3)(2)
> y = 30
>
> I agree it would be a convenient feature to have in Fortran if it does
> not break something, but I don't deem it essential.
>
>> -- John Harper, School of Mathematics, Statistics and Computer Science,
>> Victoria University, PO Box 600, Wellington 6140, New Zealand
>> e-mail john.har...@
vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
>
>