## digitalmars.D.learn - is it possible to define a property to access (read/write) a matrix?

• Charles D Hixson (3/3) Jul 25 2006 If it is possible, then could someone point me to where it's
• Mike Parker (16/20) Jul 27 2006 Do you mean like this?
• Charles D Hixson (29/53) Jul 27 2006 Thank you, yes.
• Hasan Aljudy (4/66) Jul 27 2006 Note that float vals[][] in D is actually a jagged array.
• Charles D Hixson (6/18) Jul 28 2006 I'm sure you're right. I'd planned to allocate a static
• Chris Nicholson-Sauls (4/28) Jul 28 2006 I'm not sure I understand the limitation, but if you mean what I think y...
• Hasan Aljudy (2/26) Jul 28 2006 uhh .. static array means array allocated at compile time ^_^'
• Charles D Hixson (6/35) Jul 29 2006 Which is probably where I went wrong. I was thinking of
• Stewart Gordon (35/63) Jul 30 2006 What is there to be not sure about about it? Whether you can shorten
```If it is possible, then could someone point me to where it's
documented?  Or show me an example of how?

Thank you.
```
Jul 25 2006
```Charles D Hixson wrote:
If it is possible, then could someone point me to where it's
documented?  Or show me an example of how?

Thank you.

Do you mean like this?

class MyMatrix
{
float opIndex(uint i, uint j)
{
return values[i][j];
}

float opIndexAssign(float val, uint i, uint j)
{
values[i][j] = val;
}
}

MyMatrix mat = new mat;
mat[0,0] = 0.0f;
float f = mat[0,0];
```
Jul 27 2006
```Mike Parker wrote:
Charles D Hixson wrote:
If it is possible, then could someone point me to where it's
documented?  Or show me an example of how?

Thank you.

Do you mean like this?

class MyMatrix
{
float opIndex(uint i, uint j)
{
return values[i][j];
}

float opIndexAssign(float val, uint i, uint j)
{
values[i][j] = val;
}
}

MyMatrix mat = new mat;
mat[0,0] = 0.0f;
float f = mat[0,0];

Thank you, yes.

something else...but if that works it does precisely what I
wanted better than custom properties would.

look more like:

class MyMatrix
{
float at(uint i, uint j)
{
return values[i][j];
}

float at(float val, uint i, uint j)
{
values[i][j] = val;
}
}

MyMatrix mat = new mat;
mat.at(0,0) = 0.0f;
float f = mat.at(0,0);

where the property is "at".  Knowing this would still be
useful, as I might some time want to access things via more
than one method (imagine I had sorted indexes, and was
retrieving the same thing sometimes by name and sometimes by
date, and occasionally by record number).
Still, if this works for opIndexAssign, it probably works
for all properties.
```
Jul 27 2006
```Charles D Hixson wrote:
Mike Parker wrote:

Charles D Hixson wrote:

If it is possible, then could someone point me to where it's
documented?  Or show me an example of how?

Thank you.

Do you mean like this?

class MyMatrix
{
float opIndex(uint i, uint j)
{
return values[i][j];
}

float opIndexAssign(float val, uint i, uint j)
{
values[i][j] = val;
}
}

MyMatrix mat = new mat;
mat[0,0] = 0.0f;
float f = mat[0,0];

Thank you, yes.

something else...but if that works it does precisely what I
wanted better than custom properties would.

look more like:

class MyMatrix
{
float at(uint i, uint j)
{
return values[i][j];
}

float at(float val, uint i, uint j)
{
values[i][j] = val;
}
}

MyMatrix mat = new mat;
mat.at(0,0) = 0.0f;
float f = mat.at(0,0);

where the property is "at".  Knowing this would still be
useful, as I might some time want to access things via more
than one method (imagine I had sorted indexes, and was
retrieving the same thing sometimes by name and sometimes by
date, and occasionally by record number).
Still, if this works for opIndexAssign, it probably works
for all properties.

Note that float vals[][] in D is actually a jagged array.
I think it would be a better idea to store the matrix internally as a
one-dimensional array.
```
Jul 27 2006
```Hasan Aljudy wrote:

Charles D Hixson wrote:
Mike Parker wrote:

Charles D Hixson wrote:

...

Note that float vals[][] in D is actually a jagged array.
I think it would be a better idea to store the matrix internally as a
one-dimensional array.

I'm sure you're right.  I'd planned to allocate a static
array at run time, but apparently that only works for one
dimensional arrays...in which case (I think) I do just as
well by using a dynamic array and setting the length before
I use it.
```
Jul 28 2006
```Charles D Hixson wrote:
Hasan Aljudy wrote:

Charles D Hixson wrote:

Mike Parker wrote:

Charles D Hixson wrote:

...

Note that float vals[][] in D is actually a jagged array.
I think it would be a better idea to store the matrix internally as a
one-dimensional array.

I'm sure you're right.  I'd planned to allocate a static
array at run time, but apparently that only works for one
dimensional arrays...in which case (I think) I do just as
well by using a dynamic array and setting the length before
I use it.

I'm not sure I understand the limitation, but if you mean what I think you do,
might a
template solve the situation?

-- Chris Nicholson-Sauls
```
Jul 28 2006
```Charles D Hixson wrote:
Hasan Aljudy wrote:

Charles D Hixson wrote:

Mike Parker wrote:

Charles D Hixson wrote:

...

Note that float vals[][] in D is actually a jagged array.
I think it would be a better idea to store the matrix internally as a
one-dimensional array.

I'm sure you're right.  I'd planned to allocate a static
array at run time, but apparently that only works for one
dimensional arrays...in which case (I think) I do just as
well by using a dynamic array and setting the length before
I use it.

uhh .. static array means array allocated at compile time ^_^'
```
Jul 28 2006
```Hasan Aljudy wrote:

Charles D Hixson wrote:
Hasan Aljudy wrote:

Charles D Hixson wrote:

Mike Parker wrote:

Charles D Hixson wrote:

...

Note that float vals[][] in D is actually a jagged array.
I think it would be a better idea to store the matrix internally as a
one-dimensional array.

I'm sure you're right.  I'd planned to allocate a static
array at run time, but apparently that only works for one
dimensional arrays...in which case (I think) I do just as
well by using a dynamic array and setting the length before
I use it.

uhh .. static array means array allocated at compile time ^_^'

Which is probably where I went wrong.  I was thinking of
"fixed size" as opposed to "dynamic", so I was figuring that
I could allocate a "static" (i.e. fixed size) doubly indexed
array on the stack.  It's no big deal...it just means that I
need to code in an index method (ndx = row + col*(# rows) ).
```
Jul 29 2006    Stewart Gordon <smjg_1998 yahoo.com> writes:
```Charles D Hixson wrote:
<snip>
look more like:

class MyMatrix
{
float at(uint i, uint j)
{
return values[i][j];
}

float at(float val, uint i, uint j)
{
values[i][j] = val;

What is there to be not sure about about it?  Whether you can shorten
the two statements to

return values[i][j] = val;

?  Well, of course you can.

}
}

MyMatrix mat = new mat;
mat.at(0,0) = 0.0f;
float f = mat.at(0,0);

where the property is "at".  Knowing this would still be
useful, as I might some time want to access things via more
than one method (imagine I had sorted indexes, and was
retrieving the same thing sometimes by name and sometimes by
date, and occasionally by record number).
Still, if this works for opIndexAssign, it probably works
for all properties.

You could define a property that returns a wrapper object, and then use
opIndex(assign) on that.  For example:

class MyMatrix {
struct At {    // I don't think there are "inner" structs as such
MyMatrix m;

float opIndex(uint i, uint j) {
return m.values[i][j];
}

float opIndexAssign(float val, uint i, uint j) {
return m.values[i][j] = val;
}
}

At at() {
return * cast(At*) cast(void*) &this;
}
}

MyMatrix mat = new mat;
mat.at[0,0] = 0.0f;
float f = mat.at[0,0];

Except that values is undeclared....

Stewart.

--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-  C++  a->--- UB  P+ L E  W++  N+++ o K-  w++  O? M V? PS-
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on
the 'group where everyone may benefit.
```
Jul 30 2006