## digitalmars.D - Array expression for multi dimensional arrays

• Madhav (13/13) Mar 23 2011 Hi,
• spir (18/31) Mar 23 2011 IIUC, you can do it level by level only:
• Madhav (7/38) Mar 23 2011 Yes i guessed that. My question was more to do with extending array expr...
• Don (6/43) Mar 23 2011 The problem is, that with the multi-dimensional case, you generally want...
• Jonathan M Davis (10/28) Mar 23 2011 It wouldn't really help in this case, but I would point out that you pro...
• Daniel Green (8/21) Mar 23 2011 I'm not sure this applies to your topic, but it gave me something to
```Hi,

I tried to do the following:
auto arr = new int[2][2];
arr[] = 1; // using array expressions
// The above gives - Error: cannot implicitly convert expression (1) of type
int  to const(int[2u][])

This was the first step to try out if array arithmetic that worked with single
dimensional arrays worked with multi-dimensional. I guess it does not.

The immediate utility of this would be to cleanly do matrix algebra. Any ideas
why powerful array expressions were not extended to multi dimensional arrays
as well?

Regards,
```
Mar 23 2011
spir <denis.spir gmail.com> writes:
```On 03/23/2011 12:01 PM, Madhav wrote:
Hi,

I tried to do the following:
auto arr = new int[2][2];
arr[] = 1; // using array expressions
// The above gives - Error: cannot implicitly convert expression (1) of type
int  to const(int[2u][])

This was the first step to try out if array arithmetic that worked with single
dimensional arrays worked with multi-dimensional. I guess it does not.

The immediate utility of this would be to cleanly do matrix algebra. Any ideas
why powerful array expressions were not extended to multi dimensional arrays
as well?

Regards,

IIUC, you can do it level by level only:
unittest {
int[3] a3;
int[3][2] a32;
a3[] = 1;
a32[] = a3;
assert(a32 == [[1,1,1], [1,1,1]]);
}

Note: in those assignments, [] is optional, but I consider this as a bug. Even
more since
int[3] a3 = 1;	// works
int[3][2] a32 = a3	// works not

denis
--
_________________
vita es estrany
spir.wikidot.com
```
Mar 23 2011
```== Quote from spir (denis.spir gmail.com)'s article
On 03/23/2011 12:01 PM, Madhav wrote:
Hi,

I tried to do the following:
auto arr = new int[2][2];
arr[] = 1; // using array expressions
// The above gives - Error: cannot implicitly convert expression (1) of type
int  to const(int[2u][])

This was the first step to try out if array arithmetic that worked with single
dimensional arrays worked with multi-dimensional. I guess it does not.

The immediate utility of this would be to cleanly do matrix algebra. Any ideas
why powerful array expressions were not extended to multi dimensional arrays
as well?

Regards,

IIUC, you can do it level by level only:
unittest {
int[3] a3;
int[3][2] a32;
a3[] = 1;
a32[] = a3;
assert(a32 == [[1,1,1], [1,1,1]]);
}
Note: in those assignments, [] is optional, but I consider this as a bug. Even
more since
int[3] a3 = 1;	// works
int[3][2] a32 = a3	// works not
denis

Yes i guessed that. My question was more to do with extending array expressions
for multi dimensional arrays as well.
Don't you think this should be provided by the language? It will ease out a lot
of
mathematical implementations

Regards,
```
Mar 23 2011
Don <nospam nospam.com> writes:
```Madhav wrote:
== Quote from spir (denis.spir gmail.com)'s article
On 03/23/2011 12:01 PM, Madhav wrote:
Hi,

I tried to do the following:
auto arr = new int[2][2];
arr[] = 1; // using array expressions
// The above gives - Error: cannot implicitly convert expression (1) of type
int  to const(int[2u][])

This was the first step to try out if array arithmetic that worked with single
dimensional arrays worked with multi-dimensional. I guess it does not.

The immediate utility of this would be to cleanly do matrix algebra. Any ideas
why powerful array expressions were not extended to multi dimensional arrays
as well?

Regards,

IIUC, you can do it level by level only:
unittest {
int[3] a3;
int[3][2] a32;
a3[] = 1;
a32[] = a3;
assert(a32 == [[1,1,1], [1,1,1]]);
}
Note: in those assignments, [] is optional, but I consider this as a bug. Even
more since
int[3] a3 = 1;	// works
int[3][2] a32 = a3	// works not
denis

Yes i guessed that. My question was more to do with extending array expressions
for multi dimensional arrays as well.
Don't you think this should be provided by the language? It will ease out a
lot of
mathematical implementations

The problem is, that with the multi-dimensional case, you generally want
STRIDED operations. These are much more complicated, and are much easier
to implement in a library rather than in the core language.

Belongs in Phobos, no doubt about it. Currently waiting for the
enhancement patch in bug 3474 to be applied.
```
Mar 23 2011
Jonathan M Davis <jmdavisProg gmx.com> writes:
``` Hi,

I tried to do the following:
auto arr = new int[2][2];
arr[] = 1; // using array expressions
// The above gives - Error: cannot implicitly convert expression (1) of
type int  to const(int[2u][])

This was the first step to try out if array arithmetic that worked with
single dimensional arrays worked with multi-dimensional. I guess it does
not.

The immediate utility of this would be to cleanly do matrix algebra. Any
ideas why powerful array expressions were not extended to multi
dimensional arrays as well?

Regards,

It wouldn't really help in this case, but I would point out that you probably
didn't declare the type of array that you meant to (you might have, but most
people wouldn't really want the type of array that you declared but _would_
mistakenly use the syntax that you used).

You declare an int[2u][], whereas what you probably wanted was an int[][]. You
have a dynamic array of static arrays where each static array has a fixed
length of 2. If you want a dynamic array of dynamic arrays, you need to do
this:

auto arr = new int[][](2, 2);

- Jonathan M Davis
```
Mar 23 2011
Daniel Green <venix1 gmail.com> writes:
```On 3/23/2011 7:01 AM, Madhav wrote:
Hi,

I tried to do the following:
auto arr = new int[2][2];
arr[] = 1; // using array expressions
// The above gives - Error: cannot implicitly convert expression (1) of type
int  to const(int[2u][])

This was the first step to try out if array arithmetic that worked with single
dimensional arrays worked with multi-dimensional. I guess it does not.

The immediate utility of this would be to cleanly do matrix algebra. Any ideas
why powerful array expressions were not extended to multi dimensional arrays
as well?

Regards,