This project is read-only.

Faster access to DenseMatrix elements


The current indexer for DenseMatrix is slow to the point that it can take much longer to fill a matrix than multiply it. This really needs to be improved.
--Modified by Marcus:
1) Make the non-ranged checked internal method ValueAt public.
2) Add a Fill method, accepting a delegate like "double ProduceValue(int x, int y)".


cuda wrote Jul 9, 2009 at 5:55 AM

The indexer is ranged checked. There is an internal method ValueAt (not ranged check) that we've be discussing on whether to make it public. Users using ValueAt would be responsible to handle their own range checking.

marklam wrote Jul 9, 2009 at 9:33 AM

If the values for the DenseMatrix are stored in an array (jagged or 2D) the indexing could be done without the range check (because the language will do it anyway). If an IndexOutOfRangeException occurs, the catch block could then test each index and throw its own exception for backwards compatability (perhaps even wrapping the IndexOutOfRangeException if it was seen as adding some value).

cuda wrote Jul 9, 2009 at 2:45 PM

The values are stored in a 1-D array, so one index can be out of range but the indexer would return an incorrect value. One option is to have the ValueAt method ranged checked and not the indexer (and document the fact - this would mimic the the C++ vector class).

DonReba wrote Jul 9, 2009 at 4:30 PM

Reversing the behaviour of ValueAt and the indexer with respect to range checking might be a surprising change for other users, especially for anyone who has subclassed Matrix or DenseMatrix. Personally, I would prefer a Fill method, accepting a delegate like "double ProduceValue(int x, int y)". This could be implemented very efficiently.

cuda wrote Jul 9, 2009 at 8:08 PM

A Fill method is great idea.

jvangael wrote Jul 9, 2009 at 9:45 PM

Just for the record: the F# front-end provides this functionality through a method called Matrix.iteri. We've had the request to be supported in C# and now the Matrix.iteri request; maybe we should start thinking about making the F# API fully supported in C# as well (or everyone should become a functional programmer ;) )

DonReba wrote Jul 10, 2009 at 5:32 AM

I am using dnAnalytics from Nemerle, so already on board with the functional programming.

wrote Feb 14, 2013 at 3:00 AM