TSparseVec operator * (const TSparseVec &v, const TSparseMat &m) // v * m { Assert(v.Elts() == m.Rows(), "(Mat::v*m) vector/matrix sizes don't match"); TMSparseVec temp(m.Cols()); TSparseVec result(m.Cols()); Int i; TSVIter j(v); TMSVIter k; temp = vl_zero; // v * M = v[0] * m[0] + v[1] * m[1] + ... for (i = 0; i < m.Rows(); i++) { j.Inc(i); if (j.Exists(i)) temp += m[i] * j.Data(); } result.SetNumElts(temp.Elts()); result.Begin(); for (k.Begin(temp.Elts()); !k.AtEnd(); k.Inc()) result.AddNZElt(k.Index(), k.Data()); result.End(); return(result); }
TSparseMat operator * (const TSparseMat &m, const TSparseMat &n) { Assert(m.Cols() == n.Rows(), "(SparseMat::*m) matrix cols don't match"); TSparseMat result(m.Rows(), n.Cols()); Int i; for (i = 0; i < m.Rows(); i++) result[i] = m[i] * n; return(result); }
TSparseMat operator / (const TSparseMat &m, TMReal s) { Int i; TSparseMat result(m.Rows(), m.Cols()); for (i = 0; i < m.Rows(); i++) result[i] = m[i] / s; return(result); }
TSparseMat operator - (const TSparseMat &m) { TSparseMat result(m.Rows(), m.Cols()); Int i; for (i = 0; i < m.Rows(); i++) result[i] = -m[i]; return(result); }
TMVec operator * (const TSparseMat &m, const TMVec &v) { Assert(m.Cols() == v.Elts(), "(SparseMat::*v) matrix and vector sizes don't match"); Int i; TMVec result(m.Rows()); for (i = 0; i < m.Rows(); i++) result[i] = dot(m[i], v); return(result); }
TSparseMat operator - (const TSparseMat &m, const TSparseMat &n) { Assert(n.Rows() == m.Rows(), "(SparseMat::-) matrix rows don't match"); TSparseMat result(m.Rows(), m.Cols()); Int i; for (i = 0; i < m.Rows(); i++) result[i] = m[i] - n[i]; return(result); }
TSparseVec operator * (const TSparseMat &m, const TSparseVec &v) { Assert(m.Cols() == v.Elts(), "(SparseMat::m*v) matrix and vector sizes don't match"); Int i; TSparseVec result(m.Rows()); result.Begin(); for (i = 0; i < m.Rows(); i++) result.AddElt(i, dot(m[i], v)); result.End(); return(result); }
TMVec operator * (const TMVec &v, const TSparseMat &m) // v * m { Assert(v.Elts() == m.Rows(), "(Mat::v*m) vector/matrix sizes don't match"); TMVec result(m.Cols()); Int i, j; result = vl_zero; // v * M = v[0] * m[0] + v[1] * m[1] + ... for (i = 0; i < m.Rows(); i++) if (len(v[i]) > 0) for (j = 0; j < m[i].NumItems() - 1; j++) result[m[i][j].index] += v[i] * m[i][j].elt; return(result); }
TMSparseVec operator * (const TSparseVec &v, const TSparseMat &m) // v * m { Assert(v.Elts() == m.Rows(), "(Mat::v*m) vector/matrix sizes don't match"); TMSparseVec result(m.Cols()); Int i; TSVIter j(v); result = vl_zero; // v * M = v[0] * m[0] + v[1] * m[1] + ... for (i = 0; i < m.Rows(); i++) { j.Inc(i); if (j.Exists(i)) result += m[i] * j.Data(); } return(result); }
TSparseMat trans(const TSparseMat &m) { Int i, j; TSparseMat result(m.Cols(), m.Rows()); for (i = 0; i < result.Rows(); i++) result[i].Begin(); for (i = 0; i < m.Rows(); i++) { // For row i of 'm', add its elements to the ends of the // appropriate row of 'result'. for (j = 0; j < m[i].NumItems() - 1; j++) result[m[i][j].index].AddNZElt(i, m[i][j].elt); } for (i = 0; i < result.Rows(); i++) result[i].End(); return(result); }