79 SetSize(m.m_nRows, m.m_nColumns);
81 for (
int i = 0; i < m_nRows; i++)
82 for (
int j = 0; j < m_nColumns; j++)
83 m_ppElements[i][j] = m.m_ppElements[i][j];
90 for (
int i = 0; i < m_nRows; i++)
91 delete [] m_ppElements[i];
93 delete [] m_ppElements;
106 return m_ppElements[nRow][nColumn];
111 SetSize(m.m_nRows, m.m_nColumns);
113 for (
int i = 0; i < m_nRows; i++)
114 for (
int j = 0; j < m_nColumns; j++)
115 m_ppElements[i][j] = m.m_ppElements[i][j];
122 for (
int i = 0; i < m_nRows; i++)
123 for (
int j = 0; j < m_nColumns; j++)
124 m_ppElements[i][j] *= s;
131 int i, j, k, newColumns = m.m_nColumns;
132 double constantElement;
133 CMatd result(m_nRows, newColumns);
136 for (i = 0; i < m_nRows; i++)
138 constantElement = m_ppElements[i][0];
140 for (j = 0; j < newColumns; j++)
142 result.m_ppElements[i][j] = constantElement * m.m_ppElements[0][j];
145 for (i = 0; i < m_nRows; i++)
147 for (k = 1; k < m_nColumns; k++)
149 constantElement = m_ppElements[i][k];
151 for (j = 0; j < newColumns; j++)
153 result.m_ppElements[i][j] += constantElement * m.m_ppElements[k][j];
163 if (m_nColumns != m.m_nColumns || m_nRows != m.m_nRows)
164 printf(
"error: incompatible input in CMatd::operator+(const CMatd&)\n");
166 CMatd result(m_nRows, m_nColumns);
169 for (
int i = 0; i < m_nRows; i++)
170 for (
int j = 0; j < m_nColumns; j++)
171 result.m_ppElements[i][j] = m_ppElements[i][j] + m.m_ppElements[i][j];
178 if (m_nColumns != m.m_nColumns || m_nRows != m.m_nRows)
179 printf(
"error: incompatible input in CMatd::operator-(const CMatd&)\n");
181 CMatd result(m_nRows, m_nColumns);
184 for (
int i = 0; i < m_nRows; i++)
185 for (
int j = 0; j < m_nColumns; j++)
186 result.m_ppElements[i][j] = m_ppElements[i][j] - m.m_ppElements[i][j];
193 CMatd result(m_nRows, m_nColumns);
196 for (
int i = 0; i < m_nRows; i++)
197 for (
int j = 0; j < m_nColumns; j++)
198 result.m_ppElements[i][j] = s * m_ppElements[i][j];
207 CVecd result(m_nRows);
209 for (
int i = 0; i < m_nRows; i++)
213 for (
int j = 0; j < m_nColumns; j++)
215 result[i] += m_ppElements[i][j] * v[j];
229 for (
int i = 0; i < m_nRows; i++)
230 for (
int j = 0; j < m_nColumns; j++)
231 m_ppElements[i][j] = 0.0;
236 if (m_nRows != m_nColumns)
241 for (
int i = 0; i < m_nRows; i++)
242 m_ppElements[i][i] = 1.0;
250 if (m_nRows != m_nColumns)
252 printf(
"error: input matrix must be square matrix for CMatd::Invert\n");
257 const int n = m_nColumns;
260 CMatd copiedMatrix(*
this);
261 CMatd resultMatrix(n, n);
265 int *pPivotRows =
new int[n];
266 for (i = 0; i < n; i++)
270 for (i = 0; i < n; i++)
272 int j, nPivotColumn = 0;
274 double *helper1 = copiedMatrix.m_ppElements[i];
275 double *helper2 = resultMatrix.m_ppElements[i];
279 for (j = 0; j < n; j++)
280 if (fabs(helper1[j]) > max)
282 max = fabs(helper1[j]);
286 pPivotRows[nPivotColumn] = i;
288 const double dPivotElement = copiedMatrix.m_ppElements[i][nPivotColumn];
290 if (fabs(dPivotElement) < 0.00001)
292 printf(
"error: input matrix is not regular for CMatd::Invert\n");
293 delete [] pPivotRows;
298 const double dFactor = 1.0 / dPivotElement;
300 for (j = 0; j < n; j++)
302 helper1[j] *= dFactor;
303 helper2[j] *= dFactor;
306 for (j = 0; j < n; j++)
310 const double v = copiedMatrix.m_ppElements[j][nPivotColumn];
313 helper1 = copiedMatrix.m_ppElements[j];
314 helper2 = copiedMatrix.m_ppElements[i];
315 for (k = 0; k < n; k++)
316 helper1[k] -= v * helper2[k];
317 helper1[nPivotColumn] = 0;
319 helper1 = resultMatrix.m_ppElements[j];
320 helper2 = resultMatrix.m_ppElements[i];
321 for (k = 0; k < n; k++)
322 helper1[k] -= v * helper2[k];
328 double **ppTemp =
new double*[n];
329 for (i = 0; i < n; i++)
330 ppTemp[i] = resultMatrix.m_ppElements[i];
332 for (i = 0; i < n; i++)
333 resultMatrix.m_ppElements[i] = ppTemp[pPivotRows[i]];
335 delete [] pPivotRows;
347 for (
int i = 0; i < m_nRows; i++)
348 delete [] m_ppElements[i];
349 delete [] m_ppElements;
353 m_ppElements =
new double*[nRows];
354 for (
int i = 0; i < nRows; i++)
356 m_ppElements[i] =
new double[nColumns];
358 for (
int j = 0; j < nColumns; j++)
359 m_ppElements[i][j] = 0.0;
364 m_nColumns = nColumns;
369 CMatd result(m_nColumns, m_nRows);
371 for (
int i = 0; i < m_nRows; i++)
372 for (
int j = 0; j < m_nColumns; j++)
373 result.m_ppElements[j][i] = m_ppElements[i][j];
double & operator()(int nRow, int nColumn) const
void operator*=(const double s)
Data structure and operations for calculating with matrices of arbitrary dimension.
CMatd & operator=(const CMatd &v)
CMatd operator-(const CMatd &m)
CMatd operator*(const double s)
void SetSize(int nRows, int nColumns)
Data structure and operations for calculating with vectors of arbitrary dimension.
CMatd operator+(const CMatd &m)