A J program might be: upd =: (<<<0 _1)}"1~ 3 (3 %~ +/)\"1 ]
The computation of the moving average is done by 3 (3 %~ +/)\"1 ] which averages successive 3-element sections. Then there is the question of what to do with the unchanged ends of the array; this program decides to leave them as is, and inserts the result of the computation into the array, using (<<<0 _1)}"1~ This program should work on arrays of any dimension. a =. ?5 6$100 a 90 47 58 29 22 32 55 5 55 73 58 50 40 5 69 46 34 40 46 84 29 8 75 97 24 40 21 82 77 9 upd a 90 65 44.6667 36.3333 27.6667 32 55 38.3333 44.3333 62 60.3333 50 40 38 40 49.6667 40 40 46 53 40.3333 37.3333 60 97 24 28.3333 47.6667 60 56 9 This program does not exactly reproduce the results of your FORTRAN program. The FORTRAN program calculates a(t,i1) and then uses the result of that computation to produce the result for the next value of t. I am assuming that was unintended and you really wanted each result value to depend only on the 2 nearest neighbors. Henry Rich > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of hktyz2 > Sent: Tuesday, May 30, 2006 4:17 AM > To: [email protected] > Subject: [Jprogramming] programming with arrays? > > 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 > ---------------------------------------------------------------------- > For information about J forums see > http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
