[Top] [All Lists]

Re: Some matlab -> fortran translation

Subject: Re: Some matlab -> fortran translation
From: Richard Maine
Date: Sat, 26 Dec 2009 12:17:43 -0800
Newsgroups: comp.lang.fortran

Luka Djigas <[email protected]> wrote:

> The left side is what interests me. As I understand it lengthens the
> array by one element, and gives it the value of the result of right
> side.
> Now I've never done anything similar in fortran. What would be (can it
> be done?) the easiest way to do it (since I have half a page of those
> to translate) ?

In f2003, that could be written, with array allocatable, as

  array = [array, new_element]

I'm basing this just on your description; I don't know enough Matlab to
tell you how accurately this mimics the actual Matlab.

The RHS is an array constuctor, with the elements of array, followed by
new_element. That part you can do in f90 (though you have to use the
ugly (/... /) instead of the [...] form). F2003 allows the
auto-reallocation of array with the assignment.

Without f2003, you pretty have to have the allocation as a separate
step, which also means that you have to have 2 arrays because the
allocation will destroy the original. It is most easily done with
pointers instead of allocatables. Doing it with allocatables ends up
requiring twice as many data copies and allocations, in addition to more
lines of code. F2003 adds the move_alloc intrinsic, which facilitates
doing it wil allocatables, but if you have f2003, the auto-reallocation
form seems simpler.

A pure f90 form with pointers looks something like

   tmp_array = (/ array, new_element /)
   array -> tmp_array

Be aware that if you are building a large array by concatenating a lot
of elements like this, it is likely to be *FAR* more efficient to
reallocate in bigger chunks than each element.

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

<Prev in Thread] Current Thread [Next in Thread>