40 #ifndef SPARSE_MATRIX_INCLUDE
41 #define SPARSE_MATRIX_INCLUDE
142 : mySM(sm), irow(i), jcol(j)
150 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
151 it = mySM.rowsMap.find(irow);
152 if (it != mySM.rowsMap.end())
154 return it->second[jcol];
164 typename std::map<unsigned int, SparseVector<T>>::iterator it;
165 it = mySM.rowsMap.find(irow);
168 if (T(rhs) != T(0) && it == mySM.rowsMap.end())
171 mySM.rowsMap[irow] = SVrow;
172 it = mySM.rowsMap.find(irow);
175 else if (it == mySM.rowsMap.end())
182 if (it->second.size() < jcol + 1)
184 it->second.
resize(jcol + 1);
186 it->second[jcol] = rhs;
189 if (it->second.datasize() == 0)
191 mySM.rowsMap.erase(it);
199 typename std::map<unsigned int, SparseVector<T>>::iterator it;
200 it = mySM.rowsMap.find(irow);
201 if (it == mySM.rowsMap.end())
207 return it->second[jcol];
325 const unsigned int M);
331 const unsigned int M);
368 friend std::ostream& operator<<<T>(std::ostream& os,
417 T *ptrSmall, T *ptrBig);
431 const unsigned int M);
448 const unsigned int& cind,
const unsigned int& rnum,
449 const unsigned int& cnum);
472 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
474 for (; it !=
rowsMap.end(); ++it)
475 n += it->second.datasize();
492 void resize(
const unsigned int newrows,
const unsigned int newcols)
494 typename std::map<unsigned int, SparseVector<T>>::iterator it;
498 it =
rowsMap.lower_bound(newrows);
505 it->second.resize(newcols);
528 inline bool isFilled(
const unsigned int i,
const unsigned int j)
530 typename std::map<unsigned int, SparseVector<T>>::iterator it;
532 return (it !=
rowsMap.end() && it->second.isFilled(j));
570 std::string
dump(
const int p = 3,
bool dosci =
false)
const
572 std::ostringstream oss;
575 <<
", datasize " <<
datasize() <<
" :";
576 oss << (dosci ? std::scientific : std::fixed) << std::setprecision(p);
579 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
587 for (; it !=
rowsMap.end(); ++it)
589 oss <<
"\n row " << it->first <<
": " << it->second.dump(p, dosci);
597 std::vector<unsigned int>&
cols,
598 std::vector<T>& values)
const
605 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
606 typename std::map<unsigned int, T>::const_iterator jt;
609 unsigned int row(it->first);
610 for (jt = it->second.vecMap.begin(); jt != it->second.vecMap.end();
614 cols.push_back(jt->first);
615 values.push_back(jt->second);
636 typename std::map<unsigned int, SparseVector<T>>::iterator it;
637 typename std::map<unsigned int, T>::iterator jt;
640 for (jt = it->second.vecMap.begin(); jt != it->second.vecMap.end();
642 jt->second = -jt->second;
657 void swapRows(
const unsigned int& ii,
const unsigned int& jj);
660 void swapCols(
const unsigned int ii,
const unsigned int jj);
667 std::map<unsigned int, SparseVector<T>>
rowsMap;
676 std::map<unsigned int, std::vector<unsigned int>>
columnMap()
const
679 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
680 typename std::map<unsigned int, std::vector<unsigned int>> colMap;
681 typename std::map<unsigned int, std::vector<unsigned int>>::iterator
689 std::vector<unsigned int> colIndexes = it->second.getIndexes();
692 for (k = 0; k < colIndexes.size(); k++)
696 if (jt == colMap.end())
698 std::vector<unsigned int> jvec;
702 jt->second.push_back(
727 const unsigned int& rind,
728 const unsigned int& cind,
729 const unsigned int& rnum,
730 const unsigned int& cnum)
732 if (rnum == 0 || cnum == 0 || rind + rnum > SM.
nrows ||
733 cind + cnum > SM.
ncols)
740 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
743 if (it->first < rind)
747 if (it->first > rind + rnum)
754 rowsMap[it->first] = SV;
765 for (i = 0; i < nrows; i++)
768 for (j = 0; j < ncols; j++)
782 rowsMap[i].
vecMap[j] = M(i, j);
791 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
792 typename std::map<unsigned int, T>::const_iterator jt;
793 for (it = rowsMap.begin(); it != rowsMap.end(); ++it)
795 for (jt = it->second.vecMap.begin(); jt != it->second.vecMap.end();
798 toRet(it->first, jt->first) = jt->second;
808 std::vector<unsigned int> toDelete;
809 typename std::map<unsigned int, SparseVector<T>>::iterator it;
810 for (it = rowsMap.begin(); it != rowsMap.end(); ++it)
812 it->second.zeroize(tol);
815 if (it->second.datasize() == 0)
817 toDelete.push_back(it->first);
822 for (
unsigned int i = 0; i < toDelete.size(); i++)
824 rowsMap.erase(toDelete[i]);
834 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
835 typename std::map<unsigned int, SparseVector<T>>::iterator jt;
838 for (
unsigned int j = 0; j < M.
cols(); j++)
840 if (it->second.isFilled(j))
849 jt->second.vecMap[it->first] = it->second[j];
860 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
867 T value(
min(it->second));
868 for (++it; it != SM.
rowsMap.end(); ++it)
870 T rowval(
min(it->second));
883 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
890 T value(
max(it->second));
891 for (++it; it != SM.
rowsMap.end(); ++it)
893 T rowval(
max(it->second));
906 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
913 T value(
minabs(it->second));
914 for (++it; it != SM.
rowsMap.end(); ++it)
916 T rowval(
minabs(it->second));
929 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
936 T value(
maxabs(it->second));
937 for (++it; it != SM.
rowsMap.end(); ++it)
939 T rowval(
maxabs(it->second));
954 std::ofstream savefmt;
958 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
963 for (i = 0; i < SM.
nrows; i++)
965 if (it != SM.
rowsMap.end() && i == it->first)
967 os << std::setw(1) <<
' ';
974 for (j = 0; j < SM.
ncols; j++)
976 os << std::setw(1) <<
' ';
981 if (i < SM.
nrows - 1)
1006 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
1010 retSV.
vecMap[it->first] =
dot(it->second, V);
1035 for (
unsigned int i = 0; i < L.
rows(); i++)
1039 typename std::map<unsigned int, T>::const_iterator it;
1040 for (it = V.
vecMap.begin(); it != V.
vecMap.end(); ++it)
1041 sum += L(i, it->first) * it->second;
1066 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
1070 retSV.
vecMap[it->first] =
dot(it->second, V);
1095 std::map<unsigned int, std::vector<unsigned int>>::iterator jt;
1096 std::map<unsigned int, std::vector<unsigned int>> colMap = R.
columnMap();
1099 for (jt = colMap.begin(); jt != colMap.end(); ++jt)
1103 for (
unsigned int k = 0; k < jt->second.size(); k++)
1106 sum += V[jt->second[k]] * R(k, jt->first);
1132 for (
unsigned int j = 0; j < R.
cols(); j++)
1165 std::map<unsigned int, std::vector<unsigned int>>::iterator jt;
1166 std::map<unsigned int, std::vector<unsigned int>> colMap = R.
columnMap();
1169 for (jt = colMap.begin(); jt != colMap.end(); ++jt)
1173 for (
unsigned int k = 0; k < jt->second.size(); k++)
1174 sum += V[jt->second[k]] * R(jt->second[k], jt->first);
1193 const unsigned int nr(L.
rows()), nc(R.
cols());
1196 typename std::map<unsigned int, SparseVector<T>>::const_iterator it, jt;
1201 bool haveRow(
false);
1206 T d(
dot(it->second, jt->second));
1212 retSM.
rowsMap[it->first] = row;
1215 retSM.
rowsMap[it->first].vecMap[jt->first] = d;
1234 const unsigned int nr(L.
rows()), nc(R.
cols());
1236 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
1241 bool haveRow(
false);
1243 for (
unsigned int j = 0; j < R.
cols(); j++)
1246 T d(
dot(it->second, colR));
1252 retSM.
rowsMap[it->first] = row;
1255 retSM.
rowsMap[it->first].vecMap[j] = d;
1277 const unsigned int nr(L.
rows()), nc(R.
cols());
1280 typename std::map<unsigned int, SparseVector<T>>::const_iterator jt;
1283 for (
unsigned int i = 0; i < nr; i++)
1285 bool haveRow(
false);
1292 T d(
dot(rowL, jt->second));
1301 retSM.
rowsMap[i].vecMap[jt->first] = d;
1323 unsigned int i, n(toRet.
ncols - 1);
1324 typename std::map<unsigned int, SparseVector<T>>::iterator jt;
1325 for (i = 0; i < V.
size(); i++)
1330 if (jt == toRet.
rowsMap.end())
1335 toRet.
rowsMap[i].vecMap[n] = V(i);
1354 unsigned int i, N(L.
ncols);
1355 typename std::map<unsigned int, SparseVector<T>>::iterator it;
1356 typename std::map<unsigned int, SparseVector<T>>::const_iterator jt;
1361 if (it->first < jt->first)
1363 it->second.len += N;
1366 else if (it->first > jt->first)
1368 toRet.
rowsMap[jt->first] = jt->second;
1369 toRet.
rowsMap[jt->first].len += N;
1374 it->second.len += N;
1375 typename std::map<unsigned int, T>::const_iterator vt;
1376 for (vt = jt->second.vecMap.begin(); vt != jt->second.vecMap.end();
1379 toRet.
rowsMap[it->first].vecMap[N + vt->first] = vt->second;
1395 if (SM.
cols() != cols() || SM.
rows() != rows())
1402 typename std::map<unsigned int, SparseVector<T>>::const_iterator sit;
1406 if (rowsMap.find(sit->first) == rowsMap.end())
1408 rowsMap[sit->first] = -sit->second;
1412 rowsMap[sit->first] -= sit->second;
1424 if (M.
cols() != cols() || M.
rows() != rows())
1431 for (
unsigned int i = 0; i < M.
rows(); i++)
1434 if (rowsMap.find(i) == rowsMap.end())
1503 if (SM.
cols() != cols() || SM.
rows() != rows())
1510 typename std::map<unsigned int, SparseVector<T>>::const_iterator sit;
1514 if (rowsMap.find(sit->first) == rowsMap.end())
1516 rowsMap[sit->first] = sit->second;
1520 rowsMap[sit->first] += sit->second;
1532 if (M.
cols() != cols() || M.
rows() != rows())
1539 for (
unsigned int i = 0; i < M.
rows(); i++)
1542 if (rowsMap.find(i) == rowsMap.end())
1569 typename std::map<unsigned int, SparseVector<T>>::iterator it;
1570 typename std::map<unsigned int, T>::iterator vt;
1571 for (it = rowsMap.begin(); it != rowsMap.end(); ++it)
1573 for (vt = it->second.vecMap.begin(); vt != it->second.vecMap.end();
1575 vt->second *= value;
1594 typename std::map<unsigned int, SparseVector<T>>::iterator it;
1595 typename std::map<unsigned int, T>::iterator vt;
1596 for (it = rowsMap.begin(); it != rowsMap.end(); ++it)
1598 for (vt = it->second.vecMap.begin(); vt != it->second.vecMap.end();
1600 vt->second /= value;
1665 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
1666 it = rowsMap.find(i);
1667 if (it != rowsMap.end())
1680 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
1681 for (it = rowsMap.begin(); it != rowsMap.end(); ++it)
1683 if (it->second.isFilled(j))
1685 retSV.
vecMap[it->first] = it->second[j];
1698 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
1699 for (it = rowsMap.begin(); it != rowsMap.end(); ++it)
1701 if (it->second.isFilled(it->first))
1703 retSV.
vecMap[it->first] = it->second[it->first];
1714 const unsigned int& jj)
1716 if (ii >= nrows || jj >= nrows)
1721 typename std::map<unsigned int, SparseVector<T>>::iterator it, jt;
1722 it = rowsMap.find(ii);
1723 jt = rowsMap.find(jj);
1724 if (it == rowsMap.end())
1726 if (jt == rowsMap.end())
1732 rowsMap[ii] = rowsMap[jj];
1738 if (jt == rowsMap.end())
1740 rowsMap[jj] = rowsMap[ii];
1746 rowsMap[ii] = rowsMap[jj];
1756 if (ii >= ncols || jj >= ncols)
1784 for (
unsigned int i = 0; i < dim; i++)
1808 typename std::map<unsigned int, SparseVector<T>>::const_iterator it,
1813 toRet.
rowsMap[it->first] = Vrow;
1816 T d(
dot(it->second, jt->second));
1819 toRet.
rowsMap[it->first][jt->first] = d;
1845 const unsigned int n(
P.cols());
1846 typename std::map<unsigned int, SparseVector<T>>::const_iterator jt;
1847 typename std::map<unsigned int, T>::const_iterator vt;
1853 for (jt =
P.rowsMap.begin(); jt !=
P.rowsMap.end(); ++jt)
1855 unsigned int j = jt->first;
1860 for (
unsigned int k = 0; k < n; k++)
1863 for (vt = jt->second.vecMap.begin();
1864 vt != jt->second.vecMap.end(); ++vt)
1865 sum += vt->second * C(vt->first, k);
1871 toRet(j) =
dot(jt->second, prod);
1896 std::ostringstream oss;
1897 oss <<
"Invalid input dimensions: " << A.
rows() <<
"x" << A.
cols();
1904 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
1907 for (it = A.
rowsMap.begin(), i = 0; it != A.
rowsMap.end(); i++, ++it)
1911 std::ostringstream oss;
1912 oss <<
"Singular matrix - zero row at index " << i <<
")";
1917 const unsigned int N(A.
rows());
1918 typename std::map<unsigned int, SparseVector<T>>::iterator jt, kt;
1919 typename std::map<unsigned int, T>::iterator vt;
1932 vt = jt->second.vecMap.find(jt->first);
1933 if (vt == jt->second.vecMap.end() || vt->second == T(0))
1937 for ((kt = jt)++; kt != GJ.
rowsMap.end(); ++kt)
1939 vt = kt->second.vecMap.find(jt->first);
1940 if (vt == kt->second.vecMap.end() || vt->second == T(0))
1946 jt->second += kt->second;
1965 jt->second *= T(1) / dtmp;
1974 for ((kt = jt)++; kt != GJ.
rowsMap.end(); ++kt)
1976 vt = kt->second.vecMap.find(jt->first);
1977 if (vt == kt->second.vecMap.end() || vt->second == T(0))
1982 kt->second.addScaledSparseVector(-vt->second, jt->second);
1992 typename std::map<unsigned int, SparseVector<T>>::reverse_iterator rjt,
1997 for ((rkt = rjt)++; rkt != GJ.
rowsMap.rend(); ++rkt)
1999 vt = rkt->second.vecMap.find(rjt->first);
2000 if (vt == rkt->second.vecMap.end() || vt->second == T(0))
2004 rkt->second.addScaledSparseVector(-vt->second, rjt->second);
2069 std::ostringstream oss;
2070 oss <<
"Invalid input dimensions: " << A.
rows() <<
"x" << A.
cols();
2074 const unsigned int n = A.
rows();
2075 unsigned int i, j, k;
2078 std::vector<T> rowSums;
2079 typename std::map<unsigned int, SparseVector<T>>::const_iterator it, jt;
2080 typename std::map<unsigned int, SparseVector<T>>::iterator Lit, Ljt;
2085 for (it = A.
rowsMap.begin(), i = 0; it != A.
rowsMap.end(); i++, ++it)
2089 std::ostringstream oss;
2090 oss <<
"lowerCholesky() requires positive-definite input:"
2091 <<
" (zero rows at index " << i <<
")";
2098 rowSums.push_back(T(0));
2118 std::ostringstream oss;
2119 oss <<
"Non-positive eigenvalue " << std::scientific << d
2121 <<
": lowerCholesky() requires positive-definite input";
2131 for (++Lit, ++it; Lit != L.
rowsMap.end(); ++Lit, ++it)
2134 d = (it->second.isFilled(j) ? it->second[j] : T(0));
2135 d -=
dot_lim(Lit->second, Ljt->second, 0, j);
2140 Lit->second.vecMap[j] = d;
2141 rowSums[i] += d * d;
2158 std::ostringstream oss;
2159 oss <<
"Invalid input dimensions: " << L.
rows() <<
"x" << L.
cols();
2163 const unsigned int n(L.
rows());
2164 unsigned int i, j, k;
2165 T big(0), small(0), dum,
sum;
2169 typename std::map<unsigned int, SparseVector<T>>::const_iterator it;
2170 typename std::map<unsigned int, SparseVector<T>>::iterator jt;
2174 for (i = 0, it = L.
rowsMap.begin(); i < n; ++it, ++i)
2176 if (it == L.
rowsMap.end() || it->first != i ||
2177 !it->second.isFilled(i) || it->second[i] == T(0))
2179 std::ostringstream oss;
2180 oss <<
"Singular matrix - zero diagonal at row " << i;
2184 dum = it->second[i];
2191 if (
ABS(dum) < small)
2208 for (++it; it != L.
rowsMap.end(); ++it)
2210 dum = T(1) / it->second[it->first];
2213 std::map<unsigned int, T> tempMap;
2215 for (jt = invLT.
rowsMap.begin(); jt != invLT.
rowsMap.end(); ++jt)
2217 if (jt->first >= it->first)
2222 sum =
dot_lim(it->second, jt->second, jt->first, it->first);
2226 tempMap[jt->first] = -dum *
sum;
2231 typename std::map<unsigned int, T>::iterator tt = tempMap.begin();
2232 for (; tt != tempMap.end(); ++tt)
2233 invLT.
rowsMap[tt->first].vecMap[it->first] =
2289 me.
addText(
"Called by inverseViaCholesky()");
2299 unsigned int i, j, k;
2300 typename std::map<unsigned int, SparseVector<T>>::iterator jt, kt, it;
2301 typename std::map<unsigned int, T>::iterator vt;
2307 for (j = 0; (j < A.
cols() - 1 && j < A.
rows() - 1); j++)
2319 for (vt = V.
vecMap.begin(); vt != V.
vecMap.end(); ++vt)
2325 sum += vt->second * vt->second;
2327 if (
sum < T(1.e-20))
2333 vt = jt->second.vecMap.lower_bound(jt->first);
2334 if (vt != jt->second.vecMap.end())
2336 jt->second.vecMap.erase(vt, jt->second.vecMap.end());
2341 if (vt != V.
vecMap.end())
2343 if (vt->second > T(0))
2347 jt->second[j] =
sum;
2349 sum = T(1) / (
sum * vt->second);
2353 jt->second[j] =
sum;
2360 for (++kt; kt != AT.
rowsMap.end(); ++kt)
2363 for (vt = kt->second.vecMap.begin(); vt != kt->second.vecMap.end();
2373 alpha += vt->second * V.
vecMap[i];
2382 for (i = jt->first; i < AT.
cols(); i++)
2388 vt = kt->second.vecMap.find(i);
2389 if (vt == kt->second.vecMap.end())
2391 kt->second.vecMap[i] = alpha * V.
vecMap[i];
2395 kt->second.vecMap[i] += alpha * V.
vecMap[i];
2468 const unsigned int M)
2479 std::ostringstream oss;
2480 oss <<
"Invalid input dimensions:\n R has dimension " << R.
rows()
2481 <<
"x" << R.
cols() <<
",\n Z has length " << Z.
size()
2482 <<
",\n and A has dimension " << A.
rows() <<
"x" << A.
cols();
2486 const T EPS = T(1.e-20);
2487 const unsigned int m(M == 0 || M > A.
rows() ? A.
rows() : M), n(R.
rows());
2488 const unsigned int np1(n +
2490 unsigned int i, j, k;
2492 typename std::map<unsigned int, SparseVector<T>>::iterator jt, kt, it;
2493 typename std::map<unsigned int, T>::iterator vt;
2497 for (j = 0; j < n; j++)
2520 sum = (dum > T(0) ? -T(1) : T(1)) *
SQRT(
sum);
2534 for (k = j + 1; k < np1; k++)
2538 if (kt->first == k - 1)
2552 sum = delta * (k == n ? Z(j) : R(j, k));
2564 Z(j) +=
sum * delta;
2568 R(j, k) +=
sum * delta;
2571 vt = kt->second.vecMap.begin();
2572 for (i = 0; i < m; i++)
2575 if (vt != kt->second.vecMap.end() && vt->first == i)
2582 kt->second.vecMap[i] =
sum * Vj.
vecMap[i];
2601 vt = jt->second.vecMap.begin();
2602 while (it != A.
rowsMap.end() && vt != jt->second.vecMap.end())
2604 if (it->first > vt->first)
2607 SV.
vecMap[j] = vt->second;
2611 else if (vt->first > it->first)
2617 A.
rowsMap[vt->first].vecMap[j] = vt->second;
2628 const unsigned int M)
2648 #endif // define SPARSE_MATRIX_INCLUDE