36template <
class IT,
class NT>
37SpTuples<IT,NT>::SpTuples(
int64_t size, IT nRow, IT nCol)
38:m(nRow), n(nCol), nnz(
size)
42 tuples =
new std::tuple<IT, IT, NT>[nnz];
48 isOperatorNew =
false;
51template <
class IT,
class NT>
52SpTuples<IT,NT>::SpTuples (
int64_t size, IT nRow, IT nCol, std::tuple<IT, IT, NT> * mytuples,
bool sorted,
bool isOpNew)
53:tuples(mytuples), m(nRow), n(nCol), nnz(
size), isOperatorNew(isOpNew)
69template <
class IT,
class NT>
70SpTuples<IT,NT>::SpTuples (
int64_t maxnnz, IT nRow, IT nCol, std::vector<IT> & edges,
bool removeloops):m(nRow), n(nCol)
74 tuples =
new std::tuple<IT, IT, NT>[maxnnz];
78 rowindex(i) = edges[2*i+0];
79 colindex(i) = edges[2*i+1];
82 std::vector<IT>().swap(edges);
93 while(j < maxnnz && rowindex(cnz) == rowindex(j) && colindex(cnz) == colindex(j))
95 numvalue(cnz) += numvalue(j);
99 if(removeloops && rowindex(cnz) == colindex(cnz))
109 std::tuple<IT, IT, NT> * ntuples =
new std::tuple<IT,IT,NT>[nnz];
111 for(
int64_t i=0; i<maxnnz; ++i)
115 ntuples[j++] = tuples[i];
122 isOperatorNew =
false;
131template <
class IT,
class NT>
132SpTuples<IT,NT>::SpTuples (
int64_t size, IT nRow, IT nCol, StackEntry<NT, std::pair<IT,IT> > * & multstack)
133:m(nRow), n(nCol), nnz(
size)
135 isOperatorNew =
false;
138 tuples =
new std::tuple<IT, IT, NT>[nnz];
142 colindex(i) = multstack[i].key.first;
143 rowindex(i) = multstack[i].key.second;
144 numvalue(i) = multstack[i].value;
150template <
class IT,
class NT>
151SpTuples<IT,NT>::~SpTuples()
156 ::operator
delete(tuples);
167template <
class IT,
class NT>
168SpTuples<IT,NT>::SpTuples(
const SpTuples<IT,NT> & rhs): m(rhs.m), n(rhs.n), nnz(rhs.nnz)
170 tuples =
new std::tuple<IT, IT, NT>[nnz];
171 isOperatorNew =
false;
172 for(IT i=0; i< nnz; ++i)
174 tuples[i] = rhs.tuples[i];
179template <
class IT,
class NT>
180SpTuples<IT,NT>::SpTuples (
const SpDCCols<IT,NT> & rhs): m(rhs.m), n(rhs.n), nnz(rhs.nnz)
184 FillTuples(rhs.dcsc);
186 isOperatorNew =
false;
189template <
class IT,
class NT>
190inline void SpTuples<IT,NT>::FillTuples (Dcsc<IT,NT> * mydcsc)
192 tuples =
new std::tuple<IT, IT, NT>[nnz];
194 for(IT i = 0; i< mydcsc->nzc; ++i)
196 for(IT j = mydcsc->cp[i]; j< mydcsc->cp[i+1]; ++j)
198 colindex(k) = mydcsc->jc[i];
199 rowindex(k) = mydcsc->ir[j];
200 numvalue(k++) = mydcsc->numx[j];
209template <
class IT,
class NT>
210SpTuples<IT,NT> & SpTuples<IT,NT>::operator=(
const SpTuples<IT,NT> & rhs)
218 ::operator
delete(tuples);
225 isOperatorNew =
false;
229 tuples =
new std::tuple<IT, IT, NT>[nnz];
230 for(IT i=0; i< nnz; ++i)
232 tuples[i] = rhs.tuples[i];
242template <
class IT,
class NT>
243template <
typename BINFUNC>
244void SpTuples<IT,NT>::RemoveDuplicates(BINFUNC BinOp)
248 std::vector< std::tuple<IT, IT, NT> > summed;
249 summed.push_back(tuples[0]);
251 for(IT i=1; i< nnz; ++i)
253 if((joker::get<0>(summed.back()) == joker::get<0>(tuples[i])) && (joker::get<1>(summed.back()) == joker::get<1>(tuples[i])))
255 joker::get<2>(summed.back()) = BinOp(joker::get<2>(summed.back()), joker::get<2>(tuples[i]));
259 summed.push_back(tuples[i]);
264 ::operator
delete(tuples);
267 tuples =
new std::tuple<IT, IT, NT>[summed.size()];
268 isOperatorNew =
false;
269 std::copy(summed.begin(), summed.end(), tuples);
277template <
class IT,
class NT>
278std::ifstream& SpTuples<IT,NT>::getstream (std::ifstream& infile)
280 std::cout <<
"Getting... SpTuples" << std::endl;
282 if (infile.is_open())
284 while ( (!infile.eof()) && cnz < nnz)
286 infile >> rowindex(cnz) >> colindex(cnz) >> numvalue(cnz);
291 if((rowindex(cnz) > m) || (colindex(cnz) > n))
293 std::cerr <<
"supplied matrix indices are beyond specified boundaries, aborting..." << std::endl;
301 std::cerr <<
"input file is not open!" << std::endl;
308template <
class IT,
class NT>
309std::ofstream& SpTuples<IT,NT>::putstream(std::ofstream& outfile)
const
311 outfile << m <<
"\t"<< n <<
"\t"<< nnz<<std::endl;
312 for (IT i = 0; i < nnz; ++i)
314 outfile << rowindex(i)+1 <<
"\t"<< colindex(i)+1 <<
"\t"
315 << numvalue(i) << std::endl;
320template <
class IT,
class NT>
321void SpTuples<IT,NT>::PrintInfo()
323 std::cout <<
"This is a SpTuples class" << std::endl;
325 std::cout <<
"m: " << m ;
326 std::cout <<
", n: " << n ;
327 std::cout <<
", nnz: "<< nnz << std::endl;
329 for(IT i=0; i< nnz; ++i)
331 if(rowindex(i) < 0 || colindex(i) < 0)
333 std::cout <<
"Negative index at " << i << std::endl;
336 else if(rowindex(i) >= m || colindex(i) >= n)
338 std::cout <<
"Index " << i <<
" too big with values (" << rowindex(i) <<
","<< colindex(i) <<
")" << std::endl;
344 NT **
A = SpHelper::allocate2D<NT>(m,n);
345 for(IT i=0; i< m; ++i)
346 for(IT j=0; j<n; ++j)
349 for(IT i=0; i< nnz; ++i)
351 A[rowindex(i)][colindex(i)] = numvalue(i);
353 for(IT i=0; i< m; ++i)
355 for(IT j=0; j<n; ++j)
357 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(2) <<
A[i][j];
360 std::cout << std::endl;
362 SpHelper::deallocate2D(
A,m);