Why I like Fortran (reason # 324)
Mar. 2nd, 2006 03:24 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Consider the following simplified piece of a program that I was recently writing:
The part that's particularly nice about Fortran is the last line, which illustrates what I can do with this -- and, in particular, how array indexing works in combination with data structures of this sort. The index notation (:,:,2) represents the portion of the array where the first two indices can take on any value, but the third index is limited to 2 -- that is, a two-dimensional slice through the overall array. Thus, my_field%data(:,:,2)%z is exactly what it looks like -- a reference to all of the z components of the vectors that make up that slice. Fortran treats this as equivalent to any other two-dimensional array; in this case, I've assigned it to a constant value of 3, which simply sets the z components of all the elements in the slice to 3. That's a rather trivial example; there are far more interesting ones, like passing it as an argument to a function that expects a two-dimensional array of real numbers.
Most of that's just setup of user-defined variable types, and is nothing different than one would do in C++. It sets up a type called vector with real-number variables x, y, and z; sets up a field type containing a component data, which is a three-dimensional array of vectors (I've used fixed values for the dimensions to make things simple); and defines a variable my_field of this type. Also, Fortran has a slightly different notation for accessing components; one writes my_field%data rather than the my_field.data that most other languages use.type vector real :: x, y, z end type type field type(vector) :: data(5,5,5) end type type(field) my_field my_field%data(:,:,2)%z = 3
The part that's particularly nice about Fortran is the last line, which illustrates what I can do with this -- and, in particular, how array indexing works in combination with data structures of this sort. The index notation (:,:,2) represents the portion of the array where the first two indices can take on any value, but the third index is limited to 2 -- that is, a two-dimensional slice through the overall array. Thus, my_field%data(:,:,2)%z is exactly what it looks like -- a reference to all of the z components of the vectors that make up that slice. Fortran treats this as equivalent to any other two-dimensional array; in this case, I've assigned it to a constant value of 3, which simply sets the z components of all the elements in the slice to 3. That's a rather trivial example; there are far more interesting ones, like passing it as an argument to a function that expects a two-dimensional array of real numbers.
no subject
Date: 2006-03-03 06:33 am (UTC)no subject
Date: 2006-03-03 06:52 am (UTC)It's still backwards compatible in the sense that a Fortran-95 compiler will correctly compile nearly any Fortran program from the Fortran you remember, though. And God is still real unless declared integer or otherwise.
no subject
Date: 2006-03-03 07:32 pm (UTC)(wave)
Date: 2006-03-28 01:18 am (UTC)I don't think we've ever met IRL, but I just happened to check my snail mail, and discover something addressed to you sitting on top of the mailbox. Are you, indeed, my neighbor?
Les in 112
Re: (wave)
Date: 2006-03-28 01:37 am (UTC)However, I was just now visiting
This is terribly, terribly amusing. (And we're certainly still close enough to count as neighbors, I think.)
Re: (wave)
Date: 2006-03-28 01:44 am (UTC)