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 13 16729
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
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
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
"Charulatha Kalluri" <ck******@mathw orks.com> wrote in message news:csfdc0 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();
There's no need to write a copy constructor, operator=, or destructor as the
compiler generated one will suffice.
But I'd opt for one private member variable std::vector<int > m_Data that
would store the data by rows or columns. I find that on my implementation
(Windows or Linux on PCs, g++ or Borland or other compilers) you get faster
performance because memory allocation is faster. Then you'd implement
operator() like this
int Matrix::operato r()(int row, int col) const {
return m_Data[row*numcols+col];
}
You can also easily write row and column iterators that don't do the
arithmetic, so will be very fast.
Charulatha Kalluri wrote: 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
First of all, you should make your Matrix class a template. Often
enough, you don't want matrices of integral values, but of double or
boolean.
Second, I can't see that part of the interface which gets you access to
the matrix data. You should implement operator[][] for example.
Third, I would consider not to hold a vector< vector<T> >, but separate
vectors, one for rows, one for columns (I think that was already said).
I can tell from my personal experience because I am currently
implementing a solver for linear optimization problems for a math
project at the university, and I found it most annoying to work with
vectors of vectors of T as matrix representations , because you can only
get the data in form of rows or values. However, by-column access is
needed quite often. I wish I would have known from the beginning.
As to the memory thing, I think std::vector<T> handles it as follows:
It will acquire space for several entries on construction (how much by
default is implementation defined I think, but you can explicitly pass
an initial size on construction). Now each time the vecor becomes too
small, it will -again- allocate as much space as it did on construction.
This makes sure the vector won't resize with each and every insertion.
In your case, size m for the row vector and size n for the column vector
seem to make sense (consider though, is it appropriate to resize a
matrix which was constructed as mxn? I don't think so. You would rather
discard it and create a new m'xn' matrix).
Just my 2 euro cents.
"Charulatha Kalluri" <ck******@mathw orks.com> wrote in message
news:cs******** **@fred.mathwor ks.com... 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.
Yes, in fact a vector of vectors is a very bad way to implement a Matrix (at
least a mathematical one), because there is nothing that says the internal
vectors all have to be the same length.
The way to do it is to use a vector<T>, or perhaps a valarray<T> to provide
the internal representation of your data. You then have to build various
ways to view the data, keeping in mind the appropriate striding for the
matrix. There are tons of references about how to do this on the web .. try
a google search. A couple that I found useful in the past are:
Object oriented numerics: http://www.oonumerics.org/oon/
and the POOMA library: http://acts.nersc.gov/pooma/
Also, in chapter 22 of TC++PL 3rd ed., Stroustrup shows an example of how to
implement a Matrix using a std::valarray, along with some associated STL
helper classes for building different kinds of views. For me, the
non-aliasing restrictions placed on std::valarray were too strict, and I
ended up rolling my own using std::vector as an exercise, but the general
approach and examples he laid out were certainly useful.
HTH,
Dave Moore
Charulatha Kalluri wrote: 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(); ............... ............... ......
}
g++ -Wall -ansi -pedantic -c Matrix.cc
Matrix.cc:1: error: expected unqualified-id before ')' token
Matrix.cc:3: error: expected `,' or `;' before '{' token
It doesn't even compile.
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.
Have you considered valarray?
Bjarne Stroustrup, "The C++ Programming Language: Third Edition",
Chapter 22: Numerics, Section 4: Vector Arithmetic,
Subsection 6: Slice_array, page 672.
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?
Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library http://www.netwood.net/~edwin/svmtl/
then take a look at
The Object-Oriented Numerics Page http://www.oonumerics.org/oon/
This is great - thanks for the responses!!
I've decided to go with a vector<T>, rather than valarray. (I don't have the
Stroustroup book, and there are time constraints on the project)
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"?
Thanks in advance
--CK
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cs******** **@nntp1.jpl.na sa.gov... Charulatha Kalluri wrote:
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(); ............... ............... ......
}
> g++ -Wall -ansi -pedantic -c Matrix.cc Matrix.cc:1: error: expected unqualified-id before ')' token Matrix.cc:3: error: expected `,' or `;' before '{' token
It doesn't even compile.
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.
Have you considered valarray? Bjarne Stroustrup, "The C++ Programming Language: Third Edition", Chapter 22: Numerics, Section 4: Vector Arithmetic, Subsection 6: Slice_array, page 672.
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?
Take a look at The C++ Scalar, Vector, Matrix and Tensor class Library
http://www.netwood.net/~edwin/svmtl/
then take a look at The Object-Oriented Numerics Page
http://www.oonumerics.org/oon/
"Charulatha Kalluri" <ck******@mathw orks.com> wrote in message
news:csjfjt$bb8 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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
{
|
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
...
|
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)
{
| |
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...
|
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...
|
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...
|
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
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |