array solution to iteration/recursion? problem?
Author Message
array solution to iteration/recursion? problem?

I saw this chunk of C code in a book I want to find an array solution
for (as practice in array thinking since I am a beginner).

/* <---- C code starts here ----> */

#define NMAX 10001

#define MMAX   500

double t[NMAX], x[NMAX], v[NMAX];

t[0] = x[0] = 0.0;  v[0] = 1.0;

ft = 12.566;

/* Recursion for position and velocity */

for (i = 0; i < NMAX; i++)

{

t[i+1] = dt * (i+1);

x[i+1] = x[i] + v[i] * ft;     /* trouble line 1 */

v[i+1] = v[i] - x[i] * ft;     /* trouble line 2 */

if ((i+MMAX) % MMAX == 0)

printf("%16.8f %16.8f %16.8f\n", t[i], x[i], v[i]);

}

Quote:
}

/* <----- C code ends here --------> */

So far, I have been able to translate everything into j (in terms of
arrays, including the 'if' part and the  t[i+1] part ) except for the

x[i+1] = x[i] + v[i] * ft;

v[i+1] = v[i] - x[i] * ft;

part.

My questions are:

a. What will a pure array based solution in j look like for

x[i+1] = x[i] + v[i] * ft;
v[i+1] = v[i] - x[i] * ft;

b. Are control structures unavoidable for this problem?

I suspect nested arrays might be involved but I don't know.

I am a novice novice novice j learner so please don't flame me.

Gilbert

Tue, 20 Apr 2004 04:05:25 GMT
array solution to iteration/recursion? problem?
0. I assume that dt is initialized somewhere.
I just arbitrarily assign it to 0.34.

1. The C program as stated actually overwrites
the ends of t,x,v (index errors if C were to check
index bounds).  I just arbitrarily do one less iteration.

2. t is clearly just dt*i.n

3. A straightforward translation of the C program gives
the following:

dt  =: 0.34
ft  =:  12.566

NB.  Recursion for position and velocity
foo=: 3 : 0
t=: y.\$ 0
x=: y.\$ 0
v=: y.{.1
for_i. i. y.-1 do.
t=: (dt*1+i)       (1+i)}t
x=: ((i{x)+ft*i{v) (1+i)}x
v=: ((i{v)-ft*i{x) (1+i)}v
end.
''
)
foo 10

x ,. v
0          1
12.566          1
25.132   _156.904
_1946.53   _472.713
_7886.64    23987.4
293539     123091
1.8403e6 _3.56551e6
_4.2964e7 _2.66907e7
_3.78359e8  5.13194e8
6.07044e9  5.26766e9

4. The key to removing the explicit looping, is to treat successive
elements of x and v as 2-element vectors, rather than elements
in separate vectors.  Then the recursion:
x[i+1] = x[i] + v[i] * ft;     /* trouble line 1 */
v[i+1] = v[i] - x[i] * ft;     /* trouble line 2 */
can be written as the matrix equation:
[x(i+1)] = [1   ft] [x(i)]
[v(i+1)]   [-ft  1] [v(i)]

Now it is easy to see a non-looping J solution:
M=: 2 2\$1,ft,(-ft),1
M&(+/ .*)^:(i.10) 0 1
0          1
12.566          1
25.132   _156.904
_1946.53   _472.713
_7886.64    23987.4
293539     123091
1.8403e6 _3.56551e6
_4.2964e7 _2.66907e7
_3.78359e8  5.13194e8
6.07044e9  5.26766e9

Quote:
----- Original Message -----

Sent: Thursday, November 01, 2001 14:35 PM
Subject: array solution to iteration/recursion? problem?

> I saw this chunk of C code in a book I want to find an array solution
> for (as practice in array thinking since I am a beginner).

> /* <---- C code starts here ----> */

> #define NMAX 10001
> #define MMAX   500

>   double t[NMAX], x[NMAX], v[NMAX];
>   t[0] = x[0] = 0.0;  v[0] = 1.0;
>   ft = 12.566;

> /* Recursion for position and velocity */

>   for (i = 0; i < NMAX; i++)
>   {
>     t[i+1] = dt * (i+1);
>     x[i+1] = x[i] + v[i] * ft;     /* trouble line 1 */
>     v[i+1] = v[i] - x[i] * ft;     /* trouble line 2 */
>     if ((i+MMAX) % MMAX == 0)
>       printf("%16.8f %16.8f %16.8f\n", t[i], x[i], v[i]);
>   }
> }

> /* <----- C code ends here --------> */

> So far, I have been able to translate everything into j (in terms of
> arrays, including the 'if' part and the  t[i+1] part ) except for the
>         x[i+1] = x[i] + v[i] * ft;
>         v[i+1] = v[i] - x[i] * ft;
> part.

> My questions are:

> a. What will a pure array based solution in j look like for

>         x[i+1] = x[i] + v[i] * ft;
>         v[i+1] = v[i] - x[i] * ft;

> b. Are control structures unavoidable for this problem?

> I suspect nested arrays might be involved but I don't know.
> I am a novice novice novice j learner so please don't flame me.

> Gilbert

Tue, 20 Apr 2004 08:58:04 GMT
array solution to iteration/recursion? problem?
Thank you for the prompt response, I would never have thought about it
in terms of matrices. For that, I shall blame my lack of experience.
The dt was a typo, it was meant to be ft.

I have another question... what should have prompted me to think of
this problem in terms of matrices? If I had had 3 terms thus:

z[0]= 2

x[i+1] = x[i] + v[i] + z[i] * ft;

v[i+1] = v[i] - x[i] + z[i] * ft;

z[i+1] = v[i] + x[i] - (z[i])^2 * ft;

a. Will the solution then be a 3 * 3 matrix?

b. Is the general case for equations as above with n elements always
an n*n    matrix?

Quote:
> 4. The key to removing the explicit looping, is to treat successive
> elements of x and v as 2-element vectors, rather than elements
> in separate vectors.  Then the recursion:
>    x[i+1] = x[i] + v[i] * ft;     /* trouble line 1 */
>    v[i+1] = v[i] - x[i] * ft;     /* trouble line 2 */
> can be written as the matrix equation:
>    [x(i+1)] = [1   ft] [x(i)]
>    [v(i+1)]   [-ft  1] [v(i)]
> Now it is easy to see a non-looping J solution:
>    M=: 2 2\$1,ft,(-ft),1
>    M&(+/ .*)^:(i.10) 0 1
>          0          1

<snip>

Tue, 20 Apr 2004 23:40:24 GMT
array solution to iteration/recursion? problem?

Quote:

> a. Will the solution then be a 3 * 3 matrix?

Yes.

Quote:
> b. Is the general case for equations as above with n elements always
> an n*n    matrix?

Any time you have n results expressed as a linear combination of three
arguments, the result may be expressed as a matrix expression of the
form
Res {is} M +.{times} Arg
the fact that Res is the next elements of a progression is irrelevent.

Ted
--
Please note changed e-mail address

Wed, 21 Apr 2004 01:25:32 GMT
array solution to iteration/recursion? problem?
0. In this case you can not write the equations as a
matrix equation, because it is not a linear system
(the z[i]^2 term louses it up).  However, you can still
write it in the form:

xvz[i+1]= f xvz[i]

where xvz is a 3-element vector of x[i], v[i], and z[i].
In J then:

f^:i xvz0      NB. the result of the i-th iteration
f^:(i.n) xvz0  NB. the first n iterations

1. Questions on J would get a more timely response,

2. What mail/news software are you using?  There are
a large number of extraneous blank lines in your msg
that makes it impossible to read without editing.

Quote:
----- Original Message -----

Sent: Friday, November 02, 2001 08:24 AM
Subject: Re: array solution to iteration/recursion? problem?

> Thank you for the prompt response, I would never have thought about it
> in terms of matrices. For that, I shall blame my lack of experience.
> The dt was a typo, it was meant to be ft.

> I have another question... what should have prompted me to think of
> this problem in terms of matrices? If I had had 3 terms thus:

> z[0]= 2
>     x[i+1] = x[i] + v[i] + z[i] * ft;
>     v[i+1] = v[i] - x[i] + z[i] * ft;
>     z[i+1] = v[i] + x[i] - (z[i])^2 * ft;

> a. Will the solution then be a 3 * 3 matrix?

> b. Is the general case for equations as above with n elements always
> an n*n    matrix?

> > 4. The key to removing the explicit looping, is to treat successive
> > elements of x and v as 2-element vectors, rather than elements
> > in separate vectors.  Then the recursion:
> >    x[i+1] = x[i] + v[i] * ft;     /* trouble line 1 */
> >    v[i+1] = v[i] - x[i] * ft;     /* trouble line 2 */
> > can be written as the matrix equation:
> >    [x(i+1)] = [1   ft] [x(i)]
> >    [v(i+1)]   [-ft  1] [v(i)]

> > Now it is easy to see a non-looping J solution:

> >    M=: 2 2\$1,ft,(-ft),1
> >    M&(+/ .*)^:(i.10) 0 1
> >          0          1
> <snip>

Wed, 21 Apr 2004 01:50:56 GMT
array solution to iteration/recursion? problem?
But it's not a linear combination.

The system is:

Quote:
>     x[i+1] = x[i] + v[i] + z[i] * ft;
>     v[i+1] = v[i] - x[i] + z[i] * ft;
>     z[i+1] = v[i] + x[i] - (z[i])^2 * ft;

If written as a matrix there will be a different
matrix for each iteration.
Quote:
----- Original Message -----

Sent: Friday, November 02, 2001 12:45 PM
Subject: Re: array solution to iteration/recursion? problem?

> > a. Will the solution then be a 3 * 3 matrix?

> Yes.

> > b. Is the general case for equations as above with n elements always
> > an n*n    matrix?

> Any time you have n results expressed as a linear combination of three
> arguments, the result may be expressed as a matrix expression of the
> form
>         Res {is} M +.{times} Arg
> the fact that Res is the next elements of a progression is irrelevent.

Wed, 21 Apr 2004 05:22:28 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages

Powered by phpBB® Forum Software