473,769 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Designing a Matrix class

Hi,

I'm implementing a Matrix class, as part of a project. This is the interface
I've designed:

class Matrix( )

{

private:

vector< vector<int> > m_Data;
pair<int, int> m_size;
public:

Matrix(unsigned int n);
Matrix(unsigned int m, unsigned int n);

~Matrix();
............... ............... ......

}

I have a couple of questions:

1. Is there a better way to model the data structure other than a vector of
vectors? I realize I can write up the Matrix class from scratch, but I want
to use STL containers.

2. If I use a vector of vectors, how would dynamic memory allocation work? I
don't fully understand how STL's vector handles memory. For example, to
resize a matrix, how would I "delete" the memory for the old matrix? Would
the 'resize' and 'clear' functions guarantee proper memory management,
without leaks?

Thanks in advance!!

-CK


Jul 22 '05
13 16735
Siemel Naran wrote:
Charulatha Kalluri wrote:
Also, I'm deriving Symmetric, Diagonal matrices, etc.
from the main Matrix class
and plan to have a 'minimize( )' function that uses up less memory.
But this would also involve
overloading almost all operators (arithmetic and otherwise).
Does this approach seem "natural",
or does the inheritance seem "forced"?
It seems forced to me because if Symmetric matrix inherits from Matrix,
it inherits all the functionality of the base class,
inculding its data structure.
Sure, you could modify the base class data structure in the derived class,
but there are easier ways if you are designing from scratch.

You can make class Matrix abstract.
It will just define an interface of pure virtual functions,
including a virtual destructor. For example

template <class T>
class Matrix {
public:
virtual ~Matrix();
virtual const T& operator()(size _type row, size_type col) const = 0;
virtual T& operator()(size _type row, size_type col) = 0;
};

The derived classes will be RegularMatrix, SymmetricMatrix , etc.


I'm not sure what applications Charulatha Kalluri has in mind.
Most C++ programmers avoid this approach
for high performance numerical computing
because, for example, neither

operator()(size _type, size_type) const or
operator()(size _type, size_type)

can be inline'd.

Jul 22 '05 #11
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
Siemel Naran wrote:
You can make class Matrix abstract.
It will just define an interface of pure virtual functions,
including a virtual destructor. For example

I'm not sure what applications Charulatha Kalluri has in mind.
Most C++ programmers avoid this approach
for high performance numerical computing
because, for example, neither

operator()(size _type, size_type) const or
operator()(size _type, size_type)

can be inline'd.


Good point. However, if one has numerical intensive functions like
FindEigenVector s or RotateMatrix, then one can write specialized functions
for each matrix type, possibly using templates

template <class SpecialMatrix>
void Rotate(SpecialM atrix&, double angle);

Then Rotate(Abstract Matrix&) calls one of the specialized functions. Not
saying this is the best approach or the worst, but it's at least something
to consider for our particular design.
Jul 22 '05 #12
Siemel Naran wrote:
E. Robert Tisdale wrote:
Siemel Naran wrote:

You can make class Matrix abstract.
It will just define an interface of pure virtual functions,
including a virtual destructor. For example

I'm not sure what applications Charulatha Kalluri has in mind.
Most C++ programmers avoid this approach
for high performance numerical computing
because, for example, neither

operator()(size _type, size_type) const or
operator()(size _type, size_type)

can be inline'd.


Good point. However, if one has numerical intensive functions
like FindEigenVector s or RotateMatrix,
then one can write specialized functions
for each matrix type, possibly using templates

template <class SpecialMatrix>
void Rotate(SpecialM atrix&, double angle);

Then Rotate(Abstract Matrix&) calls one of the specialized functions.
Not saying this is the best approach or the worst,
but it's at least something to consider for our particular design.


Our particular design?

I'm sorry that I didn't notice that
both you an Charulatha Kalluri work for The MathWorks

http://www.mathworks.com/

I have written *lots* of MATLAB code.
For MATLAB programmers, the priority stack looks like this:

0. convenience
1. reliability
2. performance

For high performance numerical computing
this priority stack is inverted:
0. performance
1. reliability
2. convenience

MATLAB works best if programmers can confine themselves
to the heavy weight functions that MATLAB provides but,
if programmers need to implement their own algorithms
and access matrix elements individually,
they will need to write Fortran, C or C++ subprograms
and call them from MATLAB.

I'm not sure why you want to implement a C++ class library
if you already have MATLAB.
One of the problems with MATLAB is that
there is just one type -- a double precision matrix.
This feature simplifies small MATLAB programs
but it makes it very difficult to write large reliable programs.
For example, there is no way for MATLAB to determine
that a vector or scalar function argument is required
until run time.
If you are going to implement a C++ matrix class,
then I *strongly* suggest that you implement a vector class as well.
Jul 22 '05 #13
Hello,
You can make class Matrix abstract.
It will just define an interface of pure virtual functions,
including a virtual destructor. For example

Thanks, this is what I've decided to do eventually.
I'm sorry that I didn't notice that
both you an Charulatha Kalluri work for The MathWorks

http://www.mathworks.com/
I'm not sure why you want to implement a C++ class library
if you already have MATLAB.
If you are going to implement a C++ matrix class,
then I *strongly* suggest that you implement a vector class as well.


Yes, I work there. However, this is a school project, not a work-related
one. The goal of the project is to demonstrate various OO concepts -
inheritance, STL, etc.. High performance is a plus, of course, but not the
main goal.

That said, thanks again for all your input :)
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3332
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
5
3807
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
3
1488
by: Huibuh | last post by:
In one of my header-files I have a class named "matrix" with the function to construct a matrix. It works properly but at the time of destruction of the class the program stops. What have I done wrong? Thanks in advance Dieter class matrix {
15
13277
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of matricies at compile-time? Any help would be appreciated. Hopefully this code comes in useful for someone, let me know if you find it useful, or if you have suggestions on how to improve it. // Public Domain by Christopher Diggins, 2005 ...
7
2731
by: check.checkta | last post by:
Hi, I'd like to implement a simple matrix class. I'd like to overload operator so that it returns as a vector (either the stl vector or some other Vector class of my own). The reason I want to do this is because it enables me to apply some functions of a vector to a row of of matrix, e.g., I have a function to compute the sum of vector: double my_sum(vector<double> x) {
20
5245
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've used various techniques ( loop unrolling, loop jamming...) and tried some matrix libraries : newmat (slow for large matrix) , STL (fast but ..not usefull) , hand coding (brain consuming...), and recently Meschach...
10
1907
by: andrea | last post by:
I'm studying some graphs algorithm (minumum spanning tree, breath search first topological sort etc etc...) and to understand better the theory I'm implementing them with python... I made my own graph class, the constructor is simply this: class graph: "in forma di matrice e' una matrice normale, in forma di lista uso un dizionario" def __init__(self,nodes,edges,dir=False,weight=): # inizializzatore dell'oggetto, di default in forma di...
2
8565
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and numeric because this is a personal programming exercise for me in creating an operational class in pure python for some *basic* matrix operations). 1. Please take a look at the __init__ function and comment on the initialization of the list data...
8
1971
by: slizorn | last post by:
Hi guys, well i am learning c++ on my own.. i am studying it by refering to the book and doin its exercises. well i need help sorting out the errors.. and i am not sure wat destructors and constructors are.. plus i am coding using Visual C++ 2008 express edition.. this program needs me to input a matrix from a file.. the file called matrix1.txt is as follows: <matrix> rows = 2 cols = 2
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.