is it possible to define a property to access (read/write) a matrix? (I.e., more dimensions than a vector)
Hasan Aljudy
hasan.aljudy at gmail.com
Thu Jul 27 22:56:36 PDT 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.
>
> 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.
More information about the Digitalmars-d-learn
mailing list