[Jprogramming] programming with arrays?

Brian Schott schott.brian at gmail.com
Tue May 30 18:35:57 HKT 2006


Michael,

	I believe the verb avg1d will work for any size
array. The trial array a has 5 rows and columns filled with
randomly generated integers; the trial array b has 3 planes,
each of which contains 5 rows and columns randomly filled.

   avg1d =: {.,(3(+/%3:)\]),{:
   ]a =. ?. 5 5 $ 100
46 55 79 52 54
39 60 57 60 94
46 78 13 18 51
92 78 60 90 62
31 16 60 64 64
   (avg1d"1;]) a
+-----------------------------+--------------+
|46      60      62 61.6667 54|46 55 79 52 54|
|39      52      59 70.3333 94|39 60 57 60 94|
|46 45.6667 36.3333 27.3333 51|46 78 13 18 51|
|92 76.6667      76 70.6667 62|92 78 60 90 62|
|31 35.6667 46.6667 62.6667 64|31 16 60 64 64|
+-----------------------------+--------------+
   ]b =. ?. 3 5 5 $ 100
46 55 79 52 54
39 60 57 60 94
46 78 13 18 51
92 78 60 90 62
31 16 60 64 64

71 13  3 76 26
25 77 68 48 42
91 99 97 99  9
81 64 13 89 57
52  4 73 57 95

93 28 33 16  5
27 37 45 46 43
17  9 74 47 25
 8 89  5 81  9
43 91 94 55 38
   (avg1d"1;]) b
+-----------------------------+--------------+
|46      60      62 61.6667 54|46 55 79 52 54|
|39      52      59 70.3333 94|39 60 57 60 94|
|46 45.6667 36.3333 27.3333 51|46 78 13 18 51|
|92 76.6667      76 70.6667 62|92 78 60 90 62|
|31 35.6667 46.6667 62.6667 64|31 16 60 64 64|
|                             |              |
|71      29 30.6667      35 26|71 13  3 76 26|
|25 56.6667 64.3333 52.6667 42|25 77 68 48 42|
|91 95.6667 98.3333 68.3333  9|91 99 97 99  9|
|81 52.6667 55.3333      53 57|81 64 13 89 57|
|52      43 44.6667      75 95|52  4 73 57 95|
|                             |              |
|93 51.3333 25.6667      18  5|93 28 33 16  5|
|27 36.3333 42.6667 44.6667 43|27 37 45 46 43|
|17 33.3333 43.3333 48.6667 25|17  9 74 47 25|
| 8      34 58.3333 31.6667  9| 8 89  5 81  9|
|43      76      80 62.3333 38|43 91 94 55 38|
+-----------------------------+--------------+

On Tue, 30 May 2006, hktyz2 wrote:

+ 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
+

(B=) <----------my "sig"

Brian Schott
Atlanta, GA, USA
schott DOT bee are eye eh en AT gee em ae eye el DOT com
http://schott.selfip.net/~brian/


More information about the Programming mailing list