473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

sparse matrix class implementation

Hi,
I code class to storage sparse matrix row compressed and i have a
problem with implements method to setVal and addVal.
I will replace later this methods overloaded operator().
Please, can You help me?
Thanks in advance.

I write this, but it's not so good :(
#include <iostream>
#include <vector>

using namespace std;

template <class T>
class SparseMatrixCR {
public:
SparseMatrixCR() {}
SparseMatrixCR(size_t m, size_t n) : rows_(m),
cols_(n) {
row_ptr.resize(m+1);
}

// read
T operator()(size_t row, size_t col) const;
// write
T& operator()(size_t row, size_t col);

int setSize(size_t _size);
void setVal(size_t row, size_t col, T val);
void addVal(size_t row, size_t col, T val);

// -- friend func --
friend ostream& operator<< (ostream& os, const
SparseMatrixCR<T>& rha) {
// os << "[" << rha.GetRows() << "][" <<
rha.GetCols() << "]" << endl;

for(size_t i(0); i < rha.GetRows(); ++i) {
for(size_t j(0); j < rha.GetCols();
++j) {
os.setf(ios::scientific,
ios::floatfield);
os.width(14);
os << rha(i, j) << " ";
}
os << endl;
}
return os;
}
private:
vector< T > values;
vector< size_t > row_ptr;
vector< size_t > colidx;

size_t rows_;
size_t cols_;

}; // end of: class SparseMatrixCR

template <class T>
void SparseMatrixCR<T>::setVal(size_t row, size_t col, T val) {

if(val != 0 && values.size() == 0) {
colidx.push_back(col);
values.push_back(val);
for(size_t i(row+1); i < row_ptr.size(); ++i) {
row_ptr[i] += 1;
}
}
else {

bool exist(false);

for(size_t idx(row_ptr[row]); idx < row_ptr[row+1];
++idx) {
if(colidx[idx] == col) {
values[idx] = val;
exist = true;
break;
}

}
if(exist == false) {
if(val != 0) {
colidx.insert( colidx.begin() +
row_ptr[row], col );
values.insert( values.begin() +
row_ptr[row], val );
for(size_t i(row+1); i <
row_ptr.size(); ++i) {
row_ptr[i] += 1;
}
}
else {
colidx.erase( colidx.begin() +
row_ptr[row] );
values.erase( values.begin() +
row_ptr[row] );
for(size_t i(row+1); i <
row_ptr.size(); ++i) {
row_ptr[i] -= 1;
}
}
}
}

} // end of: setVal()


template <class T>
void SparseMatrixCR<T>::addVal(size_t row, size_t col, T val) {

if(values.size() == 0) {
colidx.push_back(col);
values.push_back(val);
for(size_t i(row+1); i < row_ptr.size(); ++i) {
row_ptr[i] += 1;
}
}
else {

bool exist(false);

for(size_t idx(row_ptr[row]); idx < row_ptr[row+1];
++idx) {
if(colidx[idx] == col) {
values[idx] += val;
exist = true;
break;
}
if(colidx[idx] > col) {
poz = idx;
}
}
if(exist == false) {
if(val != 0) {
colidx.insert( colidx.begin() +
row_ptr[row], col );
values.insert( values.begin() +
row_ptr[row], val );
for(size_t i(row+1); i <
row_ptr.size(); ++i) {
row_ptr[i] += 1;
}
}
}
}
} // end of: addVal()
Jun 8 '06 #1
7 14221

mariaczi wrote:
Hi,
I code class to storage sparse matrix row compressed and i have a
problem with implements method to setVal and addVal.
I will replace later this methods overloaded operator().
Please, can You help me?


Here's a simple sparse matrix class that you can use and is already
pretty efficient:

template < typename T >
class SparseMatrix {
std::map< std::pair< size_t, size_t >, T > rep;
public:
T operator()( size_t x, size_t y ) const {
return rep[ std::make_pair( x, y ) ];
}
T& operator()( size_t x, size_t y ) {
return rep[ std::make_pair( x, y ) ];
}
};

Add operations to taste. For example you might want to add a
member-function that tells wether a particular node exists.

Jun 8 '06 #2
> Here's a simple sparse matrix class that you can use and is already
pretty efficient:

Here is even more simple sparse matrix (not it is zero initialized)

template <class T> class Matrix : public map<size_t, map<size_t, T> >
{};

int main ()
{
Matrix<double> mat;
mat[4238749][6234234] = 123.456;
cout << mat[4238749][6234234] << endl;
cout << mat[10000][2323436] << endl;
return 0;
}

Jun 8 '06 #3
On 8 Jun 2006 04:06:59 -0700, "Daniel T." <da******@earthlink.net>
wrote:
Here's a simple sparse matrix class that you can use and is already
pretty efficient: [cut]Add operations to taste. For example you might want to add a
member-function that tells wether a particular node exists.

it's ok, but i need my implementation to comparision with sparse
matrix implemented that You write
Jun 8 '06 #4
Daniel T. wrote:
template < typename T >
class SparseMatrix {
std::map< std::pair< size_t, size_t >, T > rep;
public:
T operator()( size_t x, size_t y ) const {
return rep[ std::make_pair( x, y ) ];
}
T& operator()( size_t x, size_t y ) {
return rep[ std::make_pair( x, y ) ];
}
};

Add operations to taste. For example you might want to add a
member-function that tells wether a particular node exists.


IIRC, operator[] on a std::map will allocate a new entry if one does
not already exist, no?

T operator()(size_t x, size_t y) const {
std::map<std::pair<size_t, size_t>, T>::const_iterator mapItr =
rep.find(std::make_pair<size_t, size_t>(x, y));
if (mapItr != rep.end()) return mapItr->second;
else return 0;
}

This is of course overkill if you can control calls to operator() such
that it will never be called on a nonexistent location. But, since I
believe [] reduces to a map lookup anyway, the performace overhead is
minimal. The advantage is that routines written for standard matrices
can work with the above with filling your map with a bunch of zeroes.

Ryan

Jun 8 '06 #5

mariaczi wrote:
On 8 Jun 2006 04:06:59 -0700, "Daniel T." <da******@earthlink.net>
wrote:
Here's a simple sparse matrix class that you can use and is already
pretty efficient:

[cut]
Add operations to taste. For example you might want to add a
member-function that tells wether a particular node exists.

it's ok, but i need my implementation to comparision with sparse
matrix implemented that You write


If I understand you... Just write an op== and it will.

Jun 8 '06 #6

rwf_20 wrote:
IIRC, operator[] on a std::map will allocate a new entry if one does
not already exist, no?


That is true.

Jun 8 '06 #7
On 8 Jun 2006 12:14:33 -0700, "Daniel T." <da******@earthlink.net>
wrote:
If I understand you... Just write an op== and it will.

Hmm... i use my sparse matrix implementation to solve linear
equations, and i need two assorted implementation of sparse matrix to
compare solving time with this and this and test usage memory for all

sorry for my english :(
regards
Jun 9 '06 #8

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

Similar topics

11
by: Michael Bader | last post by:
Hi, I'm currently working on a matrix multiplication code (matrix times matrix), and have come along some interesting/confusing results concerning the running time of the (apparently) same...
0
by: George Sakkis | last post by:
Is there any sparse matrix package compatible with Numeric/Numarray ? Ideally, the implementation of a matrix (dense/sparse) should be transparent to the application. However the APIs of the only...
4
by: deLenn | last post by:
Hi, Does scipy have an equivalent to Matlab's 'find' function, to list the indices of all nonzero elements in a sparse matrix? Cheers.
3
by: mediratta | last post by:
Hi, I want to allocate memory for a large matrix, whose size will be around 2.5 million x 17000. Three fourth of its rows will have all zeroes, but it is not known which will be those rows. If I...
6
by: hvmclrhu | last post by:
Hi I have a big problem. When we compile serial.c with gcc, I get this error program is generating the sparse matrix Segmentation fault I think ı have to use malloc() but I don't know how to...
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
5
by: Poseidon13 | last post by:
Hi everyone,
4
by: ishakteyran | last post by:
hello to all.. i have a realy tough assignment which requires me to add, substract, multiply, and get inverse of non-sparse and sparse matrixes.. in a more clear way it wants me to to the...
7
by: Mark | last post by:
I'm trying to figure out the best way to go about doing this. I have a "map" for a game, which I'd like to store in a matrix. Some cells will be empty (NULL), and some will hold objects. I need a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.