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

Assignment Help:

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.

 


Related Discussions:- Why shouldn''t matrix class''s interface look like an array

Visual basic, a program to accept average, if average is 75 to 85 print it...

a program to accept average, if average is 75 to 85 print it out else bypass using counter. enter and process 5 records

Compiler design, Compiler Design - Limit In The Method Instructions

Compiler Design - Limit In The Method Instructions

Simple object-oriented program, This assignment is to be undertaken individ...

This assignment is to be undertaken individually - no group work is permitted. Background information This assignment is an exercise in simple object-oriented programming and, acco

#minimum shelf, #At a shop of marbles, packs of marbles are prepared. Packe...

#At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with the

Basic input -output library of subroutines, GetChar - get a character fro...

GetChar - get a character from the keyboard, echo to the display and return it in reg al. - Note: if a linefeed (LF) is received (Enter key), a carriage return (CR) will als

Subrotine assembly language, You have to write a subroutine (assembly langu...

You have to write a subroutine (assembly language code using NASM) for the following equation.

Should i employ null or 0?, A: In C++, the definition of NULL is 0, thus th...

A: In C++, the definition of NULL is 0, thus there is only an aesthetic difference. I prefer to ignore macros, so I employ 0. Another difficulty with NULL is that people sometimes

Working with files in cpp, Operation Files 2.1. Your program should open an...

Operation Files 2.1. Your program should open and read the file specified by the first command line argument of the program. 2.2. The first line of the file contains an integer

I need app design team wanted to new check-in app, New App Production compa...

New App Production company is seeking an organized and highly accomplished design team to prepare the first (and potentially more) of my companies' App concepts, intended completel

Write a recursive function to generate anagrams, An anagram is a type of wo...

An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase. For example the anagram of tea is tea, tae, eat, eta,

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd