www.digitalmars.com         C & C++   DMDScript  

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

reply Charles D Hixson <charleshixsn earthlink.net> writes:
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
parent reply Mike Parker <aldacron71 yahoo.com> writes:
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
parent reply Charles D Hixson <charleshixsn earthlink.net> writes:
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. I was certain that wouldn't work, so I was asking about something else...but if that works it does precisely what I wanted better than custom properties would. Although... well, I was asking about something that 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; return val; // Not sure about this line } } 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
next sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
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. I was certain that wouldn't work, so I was asking about something else...but if that works it does precisely what I wanted better than custom properties would. Although... well, I was asking about something that 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; return val; // Not sure about this line } } 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
parent reply Charles D Hixson <charleshixsn earthlink.net> writes:
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
next sibling parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
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
prev sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
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
parent Charles D Hixson <charleshixsn earthlink.net> writes:
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
Jul 29 2006
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Charles D Hixson wrote:
<snip>
 Although... well, I was asking about something that 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;
       return val;  //  Not sure about this line
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