7 #ifndef __solve_SparseMatrix_h__ 8 #define __solve_SparseMatrix_h__ 35 T operator()(U32 i, U32 j)
const;
36 T& operator()(U32 i, U32 j);
38 void reset(U32 rows,U32 columns);
40 U32 rows(
void)
const {
return m_rows; }
41 U32 columns(
void)
const {
return m_columns; }
50 void scale(
const F32 scalar,
59 BWORD seek(U32 i, U32 j)
const;
73 class Row:
public List<Entry*>
80 mutable Row* m_pCurrent;
90 m_pRow=
new Row[m_rows];
99 m_pRow=
new Row[m_rows];
105 m_columns(rhs.m_columns),
125 m_pRow=
new Row[m_rows];
132 m_pRow=
new Row[m_rows];
139 if(i >= m_rows || j >= m_columns) { feX(e_invalidRange); }
146 row.toHead(m_context);
151 if(row.isAtHeadNull(m_context))
154 row.postIncrement(m_context);
156 while(!row.isAtHeadNull(m_context) && !row.isAtTailNull(m_context) &&
157 (column=I32(row.current(m_context)->m_column))<I32(j))
160 row.postIncrement(m_context);
162 while(!row.isAtHeadNull(m_context) && (row.isAtTailNull(m_context) ||
163 (column=I32(row.current(m_context)->m_column))>I32(j)))
166 row.postDecrement(m_context);
169 if(!row.isAtHeadNull(m_context) && row.isAtTailNull(m_context))
171 column=I32(row.current(m_context)->m_column);
174 return column==I32(j);
182 return m_pRow[i].current(m_context)->m_value;
193 return m_pRow[i].current(m_context)->m_value;
196 Entry** ppEntry=m_pRow[i].insertAfter(m_context,
new Entry(j));
197 return (*ppEntry)->m_value;
209 m_columns=rhs.m_columns;
213 for(U32 i = 0; i<m_rows; i++)
215 Row& row=rhs.m_pRow[i];
217 row.toHead(rhs.m_context);
220 while((pNode=row.postIncrement(rhs.m_context)) != NULL)
223 operator()(i,pNode->m_column)=pNode->m_value;
239 for(U32 i = 0; i<m_rows; i++)
241 Row& row=rhs.m_pRow[i];
243 row.toHead(rhs.m_context);
246 while((pNode=row.postIncrement(rhs.m_context)) != NULL)
249 operator()(pNode->m_column,i)=pNode->m_value;
261 for(U32 i = 0; i<m_rows; i++)
263 Row& row=rhs.m_pRow[i];
265 row.toHead(rhs.m_context);
268 while((pNode=row.postIncrement(rhs.m_context)) != NULL)
271 operator()(i,pNode->m_column)+=pNode->m_value;
282 for(U32 i = 0; i<m_rows; i++)
284 Row& row=rhs.m_pRow[i];
286 row.toHead(rhs.m_context);
289 while((pNode=row.postIncrement(rhs.m_context)) != NULL)
292 operator()(i,pNode->m_column)-=pNode->m_value;
303 for(U32 i = 0; i<m_rows; i++)
307 row.toHead(m_context);
310 while((pNode=row.postIncrement(m_context)) != NULL)
313 result(i,pNode->m_column)=pNode->m_value*scalar;
324 for(U32 i = 0; i<m_rows; i++)
328 row.toHead(m_context);
332 while((pNode=row.postIncrement(m_context)) != NULL)
335 sum+=pNode->m_value * x[pNode->m_column];
350 for(U32 i = 0; i<m_rows; i++)
354 row.toHead(m_context);
357 while((pNode=row.postIncrement(m_context)) != NULL)
360 b[pNode->m_column]+=pNode->m_value * x[i];
374 for(U32 i = 0; i<m_rows; i++)
378 row.toHead(m_context);
383 while((pNode=row.postIncrement(m_context)) != NULL)
386 result(i,pNode->m_column)=pNode->m_value*d;
406 return(matrix.rows()==matrix.columns());
415 U32 limit=minimum(matrix.columns(),matrix.rows());
417 for(U32 i = 0;i < limit;i++)
429 A.setTranspose(matrix);
454 result.setDifference(rhs);
466 rhs.premultiplyDiagonal(lhs,result);
477 lhs.transform(rhs,b);
489 lhs.transposeTransform(rhs,b);
500 matrix.scale(scalar,result);
511 matrix.scale(scalar,result);
524 for(U32 i = 0; i<matrix.rows(); i++)
526 for(U32 j = 0; j<matrix.columns(); j++)
528 s.
catf(
"%6.3f ", matrix(i,j));
538 #endif // __solve_SparseMatrix_h__ Fully Bidirectional Doubly-Linked List.
Definition: List.h:496
BWORD isSquare(const SparseMatrix< T > &matrix)
Return true is matrix is square, otherwise return false.
Definition: ListSparseMatrix.h:404
Dense vector - size fixed at construction or reset.
Definition: DenseVector.h:21
SparseMatrix< T > operator*(const F32 scalar, const SparseMatrix< T > &matrix)
SparseMatrix-Scalar pre-multiply.
Definition: ListSparseMatrix.h:508
kernel
Definition: namespace.dox:3
String print(const SparseMatrix< T > &matrix)
SparseMatrix print.
Definition: ListSparseMatrix.h:521
DenseVector< T > transposeMultiply(const SparseMatrix< T > &lhs, const DenseVector< T > &rhs)
SparseMatrix-Vector multiply with matrix transposed.
Definition: ListSparseMatrix.h:485
A view state into an fe::List <>
Definition: List.h:101
String & cat(const char *operand)
Append the current String with the given text.
Definition: String.cc:545
void setAutoDestruct(BWORD set)
Set the AutoDestruct flag.
Definition: List.h:52
DenseVector< T > operator*(const SparseMatrix< T > &lhs, const DenseVector< T > &rhs)
SparseMatrix-Vector multiply.
Definition: ListSparseMatrix.h:474
Automatically reference-counted string container.
Definition: String.h:128
String & catf(const char *fmt,...)
Populate the string as with sPrintf(), but by concatenating the results to the existing string...
Definition: String.cc:554
Dense array of sparse rows.
Definition: ListSparseMatrix.h:25
SparseMatrix< T > premultiplyDiagonal(const SparseMatrix< T > &lhs, const SparseMatrix< T > &rhs)
SparseMatrix-SparseMatrix multiply where first matrix is a diagonal.
Definition: ListSparseMatrix.h:462
SparseMatrix< T > operator*(const SparseMatrix< T > &matrix, const F32 scalar)
SparseMatrix-Scalar post-multiply.
Definition: ListSparseMatrix.h:497
SparseMatrix< T > operator-(const SparseMatrix< T > &lhs, const SparseMatrix< T > &rhs)
SparseMatrix subtract.
Definition: ListSparseMatrix.h:450
SparseMatrix< T > & setIdentity(SparseMatrix< T > &matrix)
Set matrix to identity matrix.
Definition: ListSparseMatrix.h:413
SparseMatrix< T > operator+(const SparseMatrix< T > &lhs, const SparseMatrix< T > &rhs)
SparseMatrix add.
Definition: ListSparseMatrix.h:438
SparseMatrix< T > transpose(const SparseMatrix< T > &matrix)
Return transpose of matrix.
Definition: ListSparseMatrix.h:426