Do we need Mat, Vec, TMmat, Diag, Sym and other matrix types?

jmh530 john.michael.hall at gmail.com
Tue Mar 13 20:07:01 UTC 2018


On Tuesday, 13 March 2018 at 15:47:36 UTC, Martin Tschierschke 
wrote:
>
> I think for mathematics it is more important for easy handling,
> to be able to get the element of a matrix a_ij by a(i,j) and 
> not only by a[i-1,j-1].
> [snip]

I didn't really address this in my other post.

What you're talking about is 0-based or 1-based indexing. Most 
languages force you to choose, though there are a few languages 
that let you specify the type of index you want (Pascal, Chapel, 
and Ada come to mind).

While I've thought that the way Chapel does domains is cool, I 
guess I never gave much thought into implementing the optional 0 
or 1 based indexing in D. Now that I'm thinking about it, I don't 
see why it couldn't be implemented. For instance, there's nothing 
stopping you from writing a function like below that has the same 
behavior.

auto access(T)(T x, size_t i, size_t j)
{
      return(x.opIndex(i - 1, j - 1));
}

What you really care about is the nice syntax. In that case, you 
could write an opIndex function that has different behavior based 
on a template parameter in Slice. Even something simple like 
below might work.

auto ref opIndex(Indexes...)(Indexes indexes) @safe
     if (isIndexSlice!Indexes)
{
     static if (defaultIndexingBehavior)
     {
         return this.opIndex!(indexes.length)([indexes]);
     }
     else
     {
         Indexes newIndexes;
         foreach(i, e; indexes)
         {
             newIndexes[i] = e - 1;
         }
         return this.opIndex!(indexes.length)([newIndexes]);
     }
}

The time-consuming part is that you'd have to go through all of 
mir where it relies on opIndex and ensure that both sets of 
behavior work.





More information about the Digitalmars-d mailing list