Why shouldn''t matrix class''s interface look like an array, C/C++ Programming

Why shouldn't Matrix class's interface look like an array-of-array?

A: Some people build a Matrix class that has an operator[] that returns a reference to an Array object (or possibly to a raw array, shudder), & that Array object contain an operator[] which returns an element of the Matrix (for example a reference to a double). Therefore they access elements of the matrix via syntax such as m[i][j] instead of syntax like m(i,j).


The array-of-array solution clearly works, however it is less flexible than the operator () approach. Exclusively, there are easy performance tuning tricks which can be done along with the operator () approach which are more complicated in the [][] approach, and thus the [][] approach is more likely to lead to bad performance, at least in some of the cases.

For instance, the easiest way to implement the [][] approach is to employ a physical layout of the matrix as a dense matrix which is stored in row-major form. By contrast, the operator () approach entirely hides the physical layout of the matrix, and which can lead to better performance in some of the cases.

Put it this way: the operator() approach is never bad than, and sometimes better than, the [][] approach.

The operator() approach is never bad since it is simple to implement the dense, row-major physical layout utilizing the operator() approach, so while that configuration happens to be the optimal layout through a performance standpoint, the operator() approach is only as simple as the [][] approach (possibly the operator() approach is a tiny bit simpler, The operator() approach is at times better since whenever the optimal layout for a given application happens to be something other than row-major, dense the implementation is frequently significantly easier by the operator() approach compared to the [][] approach.

As an instance of when a physical layout makes a important difference, a recent project happened to access matrix elements in columns (i.e., the algorithm accesses all the elements in one column, then the elements in another, etc.), & if the physical layout is row- major, the accesses may "stride the cache". For instance, if the rows happen to be approximately as big as the processor's cache size, machine might end up along with "cache miss" for almost all element access. In this specific project, we got a 20% development in performance by altering the mapping from the logical layout (row, column) to the physical layout (row , column).

Certainly there are several examples of this kind of thing from numerical methods, & sparse matrices are an entire other dimension on this issue. As it is, generally, easier to implement a sparse matrix or swap row/column ordering via the operator() approach, the operator() approach nothing lose and might achieve something it has no down-side & a potential up-side. Use the operator() approach.


Posted Date: 3/16/2013 3:18:43 AM | Location : United States

Related Discussions:- Why shouldn''t matrix class''s interface look like an array, Assignment Help, Ask Question on Why shouldn''t matrix class''s interface look like an array, Get Answer, Expert's Help, Why shouldn''t matrix class''s interface look like an array Discussions

Write discussion on Why shouldn''t matrix class''s interface look like an array
Your posts are moderated
Related Questions
A: Using the extern "C" linkage specification around the C function declarations. Programmers must know about mangled function names and type-safe linkages. After that they must

Prepare a MT4 EA to clone MT4 Trades to a Binary Options Platform Project Description: I want an EA that clones MT4 Trades to Globaltrader365, GT Options and if possible othe

Write a program called plus that will display asterisks in the shape of a plus sign which has the height and width specified by the user. You must only allow entry of an odd numb

Ask queCreate an object oriented application with C# that computes the area of a rectangle, and the area and the volume of a cuboid. Based on the inheritance concept, create a bas

Why many companies are switching their current business language to PHP? Where PHP basically used? PHP is rapidly gaining popularity and numerous companies are switching their

d county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Necklaces

#include #include #include using namespace std; #define MAX 5 #define INF (1 #define DEBUG if(0) #define pii pair #define pb(x) push_back(x) class

Project Description: collect / modify person records / photo images/ lookup / facebook iphone and ipad App basic function : 1 user will configure facebook connection

Ravi is a newbie to the programming and while learning the programming language he came to know the following rules: · Each program must start with ''{'' and end with '

Destructors A destructor function gets implemented whenever an instance of the class to which it belongs goes out of existence. The primary usage of a destructor function is