[Jprogramming] programming with arrays?

hktyz2 hktyz2 at hotmail.com
Tue May 30 16:17:00 HKT 2006


Hello,

I am a scientist mainly writing programs in Fortran and C. Mostly, our programs are about computations on arrays. I heard that J is an array programming language so I am interested in that if our programs can be written in J easily.

For example, we write the following Fortran subroutine that updates an array with an averaging operation. We update the value of an array element with the average of its neighbors.

subroutine avg1d (T, a)
  integer :: T
  real, dimension (0:T, 1,100) :: a
  integer :: t
  integer :: i1
  do i1 = 1, 100
    do t = 1, T-1
      a(t,i1) = (a(t-1,i1)+a(t,i1)+a(t+1,i1))/3
    end do
  end do
end subroutine avg1d

I read some introductory examples about J. But I still cannot get the idea how this subroutine can be implemented in J. Can anyone give me some hints? Or maybe this cannot be done in J easily?

If this subroutine can be implemented in J. Then my question is can we make this subroutine generic with respect to the dimensionality of a? For example, we want to define the following function which is exactly the same except it works on arrays with one more dimension.

subroutine avg2d(T, a)
  integer :: T
  real, dimension (0:T, 1:100, 1:100) :: a
  integer :: t
  integer :: i1, i2
  do i2 = 1, 100
    do i1 = 1, 100
      do t = 1, T-1
        a(t,i1,i2) = (a(t-1,i1,i2)+a(t,i1,i2)+a(t+1,i1,i2))/3
      end do
    end do
  end do
end subroutine avg2d

I am wondering if we can define just one function in J to do the same computation on arrays that whose first dimension ranges over 0..T, and have 1 or more dimensions that all have the size 100. 

Can this be done in J?

Please help.

Thanks a lot,

Michael


More information about the Programming mailing list