where and non-conformable arrays - Fortran

This is a discussion on where and non-conformable arrays - Fortran ; Dear experts, is the following where-construct allowed ? real, dimension(045) :: A real, dimension(100) :: B ! fill A with numbers - invalid numbers are indicated by -9999. ! now fill B where( A /= -9999. ) B = A ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

where and non-conformable arrays

  1. Default where and non-conformable arrays

    Dear experts,
    is the following where-construct allowed ?

    real, dimension(045) :: A
    real, dimension(100) :: B

    ! fill A with numbers - invalid numbers are indicated by -9999.

    ! now fill B
    where( A /= -9999. )
    B = A
    else where
    B = -9999.
    end where

    For this construct it is always Size(B) > Size(A) with shown
    dimensions.

    The Absoft Compiler does not complain (v9.0)

    Thanks
    -Ralf

  2. Default Re: where and non-conformable arrays

    On 12 sep, 10:06, "ralf.schaa" <Ralf.Sc...@gmail.com> wrote:
    > Dear experts,
    > is the following where-construct allowed ?
    >
    > real, dimension(045) :: A
    > real, dimension(100) :: B
    >
    > ! fill A with numbers - invalid numbers are indicated by -9999.
    >
    > ! now fill B
    > where( A /= -9999. )
    >    B = A
    > else where
    >   B = -9999.
    > end where
    >
    > For this construct it is always Size(B) > Size(A) with shown
    > dimensions.
    >
    > The Absoft Compiler does not complain (v9.0)
    >
    > Thanks
    > -Ralf


    I do not think so. What happens if you turn on array bound checking?

    (gfortran refuses to compile it, by the way, clearly indicating
    the problem)

    Regards,

    Arjen

  3. Default Re: where and non-conformable arrays

    On Sep 12, 7:06 pm, "ralf.schaa" <Ralf.Sc...@gmail.com> wrote:
    [snip]

    > The Absoft Compiler does not complain (v9.0)


    and an update from Gfortran 4.3.1 and CVF 6.6C: both complain about
    the different array sizes



  4. Default Re: where and non-conformable arrays

    On Sep 12, 7:40 pm, Arjen Markus <arjen.mar...@wldelft.nl> wrote:
    > On 12 sep, 10:06, "ralf.schaa" <Ralf.Sc...@gmail.com> wrote:

    [snip]

    > I do not think so. What happens if you turn on array bound checking?


    > (gfortran refuses to compile it, by the way, clearly indicating
    > the problem)

    yeah, I tested it just a minute ago ...

    thanks Arjen - I give up the where-construct. It's a pity since it is
    fancy and short ...

    -Ralf

  5. Default Re: where and non-conformable arrays

    program test
    implicit none
    INTEGER n
    real, dimension(045) :: A
    real, dimension(100) :: B

    print *, 'Hello World'
    ! fill A with numbers - invalid numbers are indicated by -9999.
    do n=1,45
    A(n) = n
    enddo
    A(5) = -9999.
    ! now fill B
    B = -9999.
    where( A /= -9999. )
    B(1:45) = A
    elsewhere
    B(1:45) = -9999.
    end where
    do n=1,100
    print *, n, ',', B(n)
    enddo

    end program test

  6. Default Re: where and non-conformable arrays

    ralf.schaa <Ralf.Schaa@gmail.com> wrote:

    > is the following where-construct allowed ?

    [where with non-conformable arrays]
    > The Absoft Compiler does not complain (v9.0)


    While the answer was implied by other replies, nobody explicitly
    answered this from a standard perspective other than for Arjen's "I do
    not think so."

    I'll verify Arjen's answer. No, it is not allowed. (And it wouldn't be
    consistent to allow it any more than lots of other whole array things
    with nonconformable arrays; one could imagine definitions that used just
    part of the arrays in some cases, but that's not how the language does
    it anywhere.)

    It isn't the kind of error that compilers are required to catch, but of
    course, doing so where practical is a desirable feature in a compiler.

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

  7. Default Re: where and non-conformable arrays

    You can still use the WHERE statement to do what you want cleanly and
    simply (2 short lines).

    ! Fill all elements of B() with invalid numbers
    ! Then reset values in B() that correspond to valid elements in A()

    B = -9999.
    WHERE( A /= -9999. ) B(1:size(A)) = A

    Dan

  8. Default Re: where and non-conformable arrays

    On Sep 13, 2:48 am, Dan <dant...@aol.com> wrote:
    > You can still use the WHERE statement to do what you want cleanly and
    > simply (2 short lines).
    >
    > ! Fill all elements of B() with invalid numbers
    > ! Then reset values in B() that correspond to valid elements in A()
    >
    >   B = -9999.
    >   WHERE(  A /= -9999. )  B(1:size(A)) = A
    >
    > Dan


    And ineffiently: many elements of B will be set twice. That's what the
    elsewhere clause is for.

    Regards,

    Mike Metcalf

  9. Default Re: where and non-conformable arrays


    "Dan" <dantex1@aol.com> wrote in message
    news:615961ed-04e1-4d20-927d-1d31891d620f@a3g2000prm.googlegroups.com...
    > You can still use the WHERE statement to do what you want
    > cleanly and
    > simply (2 short lines).
    >
    > ! Fill all elements of B() with invalid numbers
    > ! Then reset values in B() that correspond to valid elements in
    > A()
    >
    > B = -9999.
    > WHERE( A /= -9999. ) B(1:size(A)) = A
    >
    > Dan


    Way use where at all?


    B(1:min(size(A),size(B))) = A(1:min(size(A),size(B)))
    B(size(A)+1= -9999.

    will do the work even if size(A) > size(B).

    For the OP's dimensions its enough with:

    B(1:45) = A
    B(46 = -9999.

    Kurt


  10. Default Re: where and non-conformable arrays

    michaelmetcalf@compuserve.com wrote:

    > On Sep 13, 2:48 am, Dan <dant...@aol.com> wrote:


    >> B = -9999.
    >> WHERE( A /= -9999. ) B(1:size(A)) = A


    > And ineffiently: many elements of B will be set twice.
    > That's what the elsewhere clause is for.


    It is hard to make generalizations on efficiency, but
    it is likely that testing a value is slower than
    assigning a value. One possible exception would be
    avoiding the write to the swap file if you are
    thrashing virtual storage. There are better fixes
    in that case, though.

    -- glen


+ Reply to Thread
Page 1 of 2 1 2 LastLast