www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array-wise operations

reply Bob Cowdery <bob bobcowdery.plus.com> writes:
 I'm trying to implement some array-wise operations.

// I have an array of float values that need to be averaged over a 10
cycle period.
float x_points[600];
float x_average[600][10];
int ptr = 0;

// I accumulate one cycle in x_points and add that into a circular array
of the last 10 cycles
x_average[ptr] = x_points;
if (++ptr > 9) {ptr = 0;}
// Now I want to average the last 10 cycles and put the result back into
x_points
x_points[]
=(x_average[0]+x_average[1]+x_average[2]+x_average[3]+x_average[4]+x_average[5]+
x_average[6]+x_average[7]+x_average[8]+x_average[9])/10;

The average gives me a compile error of incompatible types.

bob
Oct 12 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Bob Cowdery <bob bobcowdery.plus.com> wrote:

 x_points[]
 =(x_average[0]+x_average[1]+x_average[2]+x_average[3]+x_average[4]+x_average[5]+
 x_average[6]+x_average[7]+x_average[8]+x_average[9])/10;

 The average gives me a compile error of incompatible types.
You need to append [] to x_average[index]. -- Simen
Oct 12 2010
parent reply Bob Cowdery <bob bobcowdery.plus.com> writes:
 On 12/10/2010 20:29, Simen kjaeraas wrote:
 Bob Cowdery <bob bobcowdery.plus.com> wrote:

 x_points[]
 =(x_average[0]+x_average[1]+x_average[2]+x_average[3]+x_average[4]+x_average[5]+

 x_average[6]+x_average[7]+x_average[8]+x_average[9])/10;

 The average gives me a compile error of incompatible types.
You need to append [] to x_average[index].
Thanks. I tried it the other way round [][0]. To put [0][] seems to me the wrong way round. Probably just not understanding the syntax properly. bob
Oct 12 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Bob Cowdery <bob bobcowdery.plus.com> wrote:

  On 12/10/2010 20:29, Simen kjaeraas wrote:
 Bob Cowdery <bob bobcowdery.plus.com> wrote:

 x_points[]
 =(x_average[0]+x_average[1]+x_average[2]+x_average[3]+x_average[4]+x_average[5]+

 x_average[6]+x_average[7]+x_average[8]+x_average[9])/10;

 The average gives me a compile error of incompatible types.
You need to append [] to x_average[index].
Thanks. I tried it the other way round [][0]. To put [0][] seems to me the wrong way round. Probably just not understanding the syntax properly.
Yeah, it's a bit the wrong way around, for those coming from C, at least. The idea is that for any T[] t, t[0] should behave as if a function returning a T. So, for char[][] c, c[0] returns char[], which would then be indexed by the next brackets. If index was the function: c[0][1] => index( index( c, 0 ), 1 ); -- Simen
Oct 12 2010
next sibling parent reply Bob Cowdery <bob bobcowdery.plus.com> writes:
 On 12/10/2010 21:11, Simen kjaeraas wrote:
 Bob Cowdery <bob bobcowdery.plus.com> wrote:

  On 12/10/2010 20:29, Simen kjaeraas wrote:
 Bob Cowdery <bob bobcowdery.plus.com> wrote:

 x_points[]
 =(x_average[0]+x_average[1]+x_average[2]+x_average[3]+x_average[4]+x_average[5]+


 x_average[6]+x_average[7]+x_average[8]+x_average[9])/10;

 The average gives me a compile error of incompatible types.
You need to append [] to x_average[index].
Thanks. I tried it the other way round [][0]. To put [0][] seems to me the wrong way round. Probably just not understanding the syntax properly.
Yeah, it's a bit the wrong way around, for those coming from C, at least. The idea is that for any T[] t, t[0] should behave as if a function returning a T. So, for char[][] c, c[0] returns char[], which would then be indexed by the next brackets. If index was the function: c[0][1] => index( index( c, 0 ), 1 );
Ok, kind of understand that. Now I have another problem. x_average[ptr] = x_points; // tells me array length don't match. When I print x_average[ptr] or x_average[][ptr] or x_average[ptr][] they all tell me the length is 10. What do I have to do to get to the row which is 600?
Oct 12 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Bob Cowdery <bob bobcowdery.plus.com> wrote:

 x_average[ptr] = x_points; // tells me array length don't match.

 When I print x_average[ptr] or x_average[][ptr] or x_average[ptr][] they
 all tell me the length is 10. What do I have to do to get to the row
 which is 600?
Oh, yes. Sorry, I didn't see that at first. D (for some reason) supports both C style definition of arrays (int a[3][];), and D style (int[][3] a;). As you can see, the order of indices are reversed. If you print typeof( x_average ), you should get float[10][600], meaning an array of 600 elements, each holding 10 floats. I believe what you want is float[600][10], which is an array of 10 arrays of 600 floats. If I'm wrong, you likely want a strided array, which D noes not currently support. -- Simen
Oct 12 2010
parent reply Bob Cowdery <bob bobcowdery.plus.com> writes:
 On 12/10/2010 21:43, Simen kjaeraas wrote:
 Bob Cowdery <bob bobcowdery.plus.com> wrote:

 x_average[ptr] = x_points; // tells me array length don't match.

 When I print x_average[ptr] or x_average[][ptr] or x_average[ptr][] they
 all tell me the length is 10. What do I have to do to get to the row
 which is 600?
Oh, yes. Sorry, I didn't see that at first. D (for some reason) supports both C style definition of arrays (int a[3][];), and D style (int[][3] a;). As you can see, the order of indices are reversed. If you print typeof( x_average ), you should get float[10][600], meaning an array of 600 elements, each holding 10 floats. I believe what you want is float[600][10], which is an array of 10 arrays of 600 floats. If I'm wrong, you likely want a strided array, which D noes not currently support.
How confusing to support two ways of doing something which require the indices in a different order. I now have a C style defined which works so I will change it to a D style which should also work. Thanks for the help.
Oct 12 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Bob Cowdery:

 How confusing to support two ways of doing something which require the
 indices in a different order.
Yes, it's a mess. I have proposed to accept that C syntax only when the -d compiler switch is used (it also enables C-style function literals and maybe C function pointers too). Bye, bearophile
Oct 12 2010
prev sibling parent Bob Cowdery <bob bobcowdery.plus.com> writes:
 On 12/10/2010 21:25, Bob Cowdery wrote:
  On 12/10/2010 21:11, Simen kjaeraas wrote:
 Bob Cowdery <bob bobcowdery.plus.com> wrote:

  On 12/10/2010 20:29, Simen kjaeraas wrote:
 Bob Cowdery <bob bobcowdery.plus.com> wrote:

 x_points[]
 =(x_average[0]+x_average[1]+x_average[2]+x_average[3]+x_average[4]+x_average[5]+


 x_average[6]+x_average[7]+x_average[8]+x_average[9])/10;

 The average gives me a compile error of incompatible types.
You need to append [] to x_average[index].
Thanks. I tried it the other way round [][0]. To put [0][] seems to me the wrong way round. Probably just not understanding the syntax properly.
Yeah, it's a bit the wrong way around, for those coming from C, at least. The idea is that for any T[] t, t[0] should behave as if a function returning a T. So, for char[][] c, c[0] returns char[], which would then be indexed by the next brackets. If index was the function: c[0][1] => index( index( c, 0 ), 1 );
Ok, kind of understand that. Now I have another problem. x_average[ptr] = x_points; // tells me array length don't match. When I print x_average[ptr] or x_average[][ptr] or x_average[ptr][] they all tell me the length is 10. What do I have to do to get to the row which is 600?
Got it now. The array was defined back to front as well. Should have been x_average[10][600] (I think, seems to work anyway).
Oct 12 2010