Assignments
Assignments
MatrixMNTC.inl
Go to the documentation of this file.
1
2//
3// MatrixMNTC.inl
4//
5// Copyright Microsoft Corporation
6//
7// Microsoft Research Graphics Group
8// Kineform Project
9//
10// Abstract: An mxn matrix class for luciform / kineform
11//
12// Revision History Abstract:
13// 03Apr1999 ChuckR Initial code start
15
16template <class Coord>
17 GXMatrixMNTC<Coord>::GXMatrixMNTC (void) : m_pElements (NULL),
18 m_cRows (0),
19 m_cCols (0)
20{
21}
22
23template <class Coord>
24 GXMatrixMNTC<Coord>::GXMatrixMNTC (unsigned int cRows, unsigned int cCols)
25{
26 unsigned int cTotalSize = cRows * cCols;
27
28 if (cTotalSize > 0)
29 {
30 m_pElements = new Coord[cTotalSize];
31
32 if (!m_pElements)
33#ifdef USE_MK_EXCEPTIONS
34 throw MatrixMNTCException( "out of memory" );
35#else // !USE_MK_EXCEPTIONS
36 throw _com_error(E_OUTOFMEMORY);
37#endif // USE_MK_EXCEPTIONS
38 }
39
40 m_cRows = cRows;
41 m_cCols = cCols;
42}
43
44template <class Coord>
45 GXMatrixMNTC<Coord>::GXMatrixMNTC (unsigned int cRows, unsigned int cCols, const Coord& initializeTo)
46{
47 unsigned int cTotalSize = cRows * cCols;
48
49 if (cTotalSize > 0)
50 {
51 m_pElements = new Coord[cTotalSize];
52
53 if (!m_pElements)
54#ifdef USE_MK_EXCEPTIONS
55 throw MatrixMNTCException( "out of memory" );
56#else // !USE_MK_EXCEPTIONS
57 throw _com_error(E_OUTOFMEMORY);
58#endif // USE_MK_EXCEPTIONS
59 }
60
61 m_cRows = cRows;
62 m_cCols = cCols;
63
64 for (unsigned int i = 0 ; i < (cRows * cCols) ; i++)
65 m_pElements[i] = initializeTo;
66}
67
68template <class Coord>
70{
71 unsigned int cTotalSize = toCopy.m_cRows * toCopy.m_cCols;
72
73 if (cTotalSize > 0)
74 {
75 m_pElements = new Coord[cTotalSize];
76
77 if (!m_pElements)
78#ifdef USE_MK_EXCEPTIONS
79 throw MatrixMNTCException( "out of memory" );
80#else // !USE_MK_EXCEPTIONS
81 throw _com_error(E_OUTOFMEMORY);
82#endif // USE_MK_EXCEPTIONS
83 }
84
85 m_cRows = toCopy.m_cRows;
86 m_cCols = toCopy.m_cCols;
87
88 for (unsigned int i = 0 ; i < cTotalSize ; i++)
89 m_pElements[i] = toCopy.m_pElements[i];
90}
91
92template <class Coord>
94{
95 if (m_pElements)
96 delete[] m_pElements;
97}
98
99template <class Coord>
100 inline unsigned int GXMatrixMNTC<Coord>::Cols (void) const
101{
102 return m_cCols;
103}
104
105template <class Coord>
106 inline Coord* GXMatrixMNTC<Coord>::Data (void)
107{
108 return m_pElements;
109}
110
111template <class Coord>
112 inline const Coord& GXMatrixMNTC<Coord>::Get (unsigned int i, unsigned int j) const
113{
114 return m_pElements [j + (i * m_cCols)];
115}
116
117template <class Coord>
118 void GXMatrixMNTC<Coord>::Init (const Coord& initializeTo)
119{
120 for (unsigned int i = 0, cCount = m_cRows * m_cCols ; i < cCount ; i++)
121 m_pElements[i] = initializeTo;
122}
123
124template <class Coord>
126{
127 if ((m_cRows != 0) || (m_cRows != m_cCols))
128 return false;
129
130 for (unsigned int i = 0 ; i < m_cRows ; i++)
131 for (unsigned int j = 0 ; j <= i ; j++)
132 if ((i != j) && (Get(i,j) != -Get(j,i)))
133 return false;
134 else if ((i == j) && (Get(i,i) != 0))
135 return false;
136
137 return true;
138}
139
140template <class Coord>
142{
143 if ((m_cRows == 0) || (m_cCols != m_cRows))
144 return false;
145
146 for (unsigned int i = 0 ; i < m_cRows ; i++)
147 for (unsigned int j = 0 ; j < m_cCols ; j++)
148 if ((i != j) && (Get(i,j) != 0))
149 return false;
150
151 return true;
152}
153
154template <class Coord>
156{
157 if ((m_cRows == 0) || (m_cCols != m_cRows))
158 return false;
159
160 Coord val = Get(0,0);
161
162 for (unsigned int i = 0 ; i < m_cRows ; i++)
163 for (unsigned int j = 0 ; j < m_cCols ; j++)
164 if (((i != j) && (Get(i,j) != 0))
165 ||
166 ((i == j) && (Get(i,j) != val)))
167 return false;
168
169 return true;
170}
171
172template <class Coord>
174{
175 return ((m_cRows != 0) && (m_cRows == m_cCols));
176}
177
178template <class Coord>
180{
181 if ((m_cRows == 0) || (m_cRows != m_cCols))
182 return false;
183
184 for (unsigned int i = 0 ; i < m_cRows ; i++)
185 for (unsigned int j = 0 ; j <= i ; j++)
186 if (Get(i,j) != Get(j,i))
187 return false;
188
189 return true;
190}
191
192template <class Coord>
193 inline unsigned int GXMatrixMNTC<Coord>::Rows (void) const
194{
195 return m_cRows;
196}
197
198template <class Coord>
199 void GXMatrixMNTC<Coord>::SetDim (unsigned int cRows, unsigned int cCols)
200{
201 unsigned int cTotalSize = cRows * cCols;
202
203 if (m_pElements)
204 {
205 delete[] m_pElements;
206 }
207
208 if (cTotalSize > 0)
209 {
210 m_pElements = new Coord[cTotalSize];
211
212 if (!m_pElements)
213#ifdef USE_MK_EXCEPTIONS
214 throw MatrixMNTCException( "out of memory" );
215#else // !USE_MK_EXCEPTIONS
216 throw _com_error(E_OUTOFMEMORY);
217#endif // USE_MK_EXCEPTIONS
218 }
219 else
220 m_pElements = NULL;
221
222 m_cRows = cRows;
223 m_cCols = cCols;
224}
225
226template <class Coord>
227 void GXMatrixMNTC<Coord>::SetDim (unsigned int cRows, unsigned int cCols, const Coord& initializeTo)
228{
229 unsigned int cTotalSize = cRows * cCols;
230
231 if (m_pElements)
232 {
233 delete[] m_pElements;
234 }
235
236 if (cTotalSize > 0)
237 {
238 m_pElements = new Coord[cTotalSize];
239
240 if (!m_pElements)
241#ifdef USE_MK_EXCEPTIONS
242 throw MatrixMNTCException( "out of memory" );
243#else // !USE_MK_EXCEPTIONS
244 throw _com_error(E_OUTOFMEMORY);
245#endif // USE_MK_EXCEPTIONS
246 }
247 else
248 m_pElements = NULL;
249
250 m_cRows = cRows;
251 m_cCols = cCols;
252
253 for (unsigned int i = 0 ; i < cTotalSize ; i++)
254 m_pElements[i] = initializeTo;
255}
256
258
259template <class Coord>
261{
262 unsigned int cTotalSize = toCopy.m_cRows * toCopy.m_cCols;
263
264 if (m_pElements)
265 {
266 delete[] m_pElements;
267 }
268
269 if (cTotalSize > 0)
270 {
271 m_pElements = new Coord[cTotalSize];
272
273 if (!m_pElements)
274#ifdef USE_MK_EXCEPTIONS
275 throw MatrixMNTCException( "out of memory" );
276#else // !USE_MK_EXCEPTIONS
277 throw _com_error(E_OUTOFMEMORY);
278#endif // USE_MK_EXCEPTIONS
279 }
280 else
281 m_pElements = NULL;
282
283 m_cRows = toCopy.m_cRows;
284 m_cCols = toCopy.m_cCols;
285
286 for (unsigned int i = 0 ; i < cTotalSize ; i++)
287 m_pElements[i] = toCopy.m_pElements[i];
288
289 return *this;
290}
291
292
293template <class Coord>
295{
296 if ((m_cRows != toAdd.m_cRows) || (m_cCols != toAdd.m_cCols))
297#ifdef USE_MK_EXCEPTIONS
298 throw MatrixMNTCException( "invalid argument" );
299#else // !USE_MK_EXCEPTIONS
300 throw _com_error(E_INVALIDARG);
301#endif // USE_MK_EXCEPTIONS
302
303 for (unsigned int i = 0, cCount = m_cRows * m_cCols ; i < cCount ; i++)
304 m_pElements[i] += toAdd.m_pElements[i];
305
306 return *this;
307}
308
309template <class Coord>
311{
312 if ((m_cRows != toAdd.m_cRows) || (m_cCols != toAdd.m_cCols))
313#ifdef USE_MK_EXCEPTIONS
314 throw MatrixMNTCException( "invalid argument" );
315#else // !USE_MK_EXCEPTIONS
316 throw _com_error(E_INVALIDARG);
317#endif // USE_MK_EXCEPTIONS
318
319 for (unsigned int i = 0, cCount = m_cRows * m_cCols ; i < cCount ; i++)
320 m_pElements[i] -= toAdd.m_pElements[i];
321
322 return *this;
323}
324
325template <class Coord>
327{
328 for (unsigned int i = 0, cCount = m_cRows * m_cCols ; i < cCount ; i++)
329 m_pElements[i] *= s;
330
331 return *this;
332}
333
334template <class Coord>
336{
337 if (s == ((Coord) 0.0f))
338#ifdef USE_MK_EXCEPTIONS
339 throw MatrixMNTCException( "invalid argument" );
340#else // !USE_MK_EXCEPTIONS
341 throw _com_error(E_INVALIDARG);
342#endif // USE_MK_EXCEPTIONS
343
344 for (unsigned int i = 0, cCount = m_cRows * m_cCols ; i < cCount ; i++)
345 m_pElements[i] /= s;
346
347 return *this;
348}
349
350template <class Coord>
351 inline const Coord& GXMatrixMNTC<Coord>::operator() (unsigned int iRow, unsigned int iCol) const
352{
353 if ((iRow >= m_cRows) || (iCol >= m_cCols))
354#ifdef USE_MK_EXCEPTIONS
355 throw MatrixMNTCException( "invalid argument" );
356#else // !USE_MK_EXCEPTIONS
357 throw _com_error(E_INVALIDARG);
358#endif // USE_MK_EXCEPTIONS
359
360 return m_pElements[iCol + (m_cCols * iRow)];
361}
362
363template <class Coord>
364 inline Coord& GXMatrixMNTC<Coord>::operator() (unsigned int iRow, unsigned int iCol)
365{
366 if ((iRow >= m_cRows) || (iCol >= m_cCols))
367#ifdef USE_MK_EXCEPTIONS
368 throw MatrixMNTCException( "invalid argument" );
369#else // !USE_MK_EXCEPTIONS
370 throw _com_error(E_INVALIDARG);
371#endif // USE_MK_EXCEPTIONS
372
373 return m_pElements[iCol + (m_cCols * iRow)];
374}
375
376// non-member operators ////////////////////////////////////////////////////////////////
377
378template <class Coord>
380 const GXMatrixMNTC<Coord>& B)
381{
382 if ((A.m_cRows != B.m_cRows) ||
383 (A.m_cCols != B.m_cCols))
384 return false;
385
386 for (unsigned int i = 0, cCount = A.m_cRows * A.m_cCols ; i < cCount ; i++)
387 if (A.m_pElements[i] != B.m_pElements[i])
388 return false;
389
390 return true;
391}
392
393template <class Coord>
395 const GXMatrixMNTC<Coord>& B)
396{
397 if ((A.m_cRows != B.m_cRows) ||
398 (A.m_cCols != B.m_cCols))
399 return true;
400
401 for (unsigned int i = 0, cCount = A.m_cRows * A.m_cCols ; i < cCount ; i++)
402 if (A.m_pElements[i] != B.m_pElements[i])
403 return true;
404
405 return false;
406}
407
408template <class Coord>
410{
411 unsigned int cRows = A.m_cRows,
412 cCols = A.m_cCols;
413
414 if ((cRows != B.m_cRows) || (cCols != B.m_cCols))
415#ifdef USE_MK_EXCEPTIONS
416 throw MatrixMNTCException( "invalid argument" );
417#else // !USE_MK_EXCEPTIONS
418 throw _com_error(E_INVALIDARG);
419#endif // USE_MK_EXCEPTIONS
420
421 GXMatrixMNTC<Coord> ApB (A);
422
423 ApB += B;
424
425 return ApB;
426}
427
428template <class Coord>
430{
431 unsigned int cRows = A.m_cRows,
432 cCols = A.m_cCols;
433
434 if ((cRows != B.m_cRows) || (cCols != B.m_cCols))
435#ifdef USE_MK_EXCEPTIONS
436 throw MatrixMNTCException( "invalid argument" );
437#else // !USE_MK_EXCEPTIONS
438 throw _com_error(E_INVALIDARG);
439#endif // USE_MK_EXCEPTIONS
440
441 GXMatrixMNTC<Coord> AmB (A);
442
443 AmB -= B;
444
445 return AmB;
446}
447
448template <class Coord>
450{
451 unsigned int m (A.Rows()),
452 p (A.Cols()),
453 n (B.Cols()),
454 i,
455 j,
456 k;
457 Coord fTotal;
458
459 if (p != B.Rows())
460#ifdef USE_MK_EXCEPTIONS
461 throw MatrixMNTCException( "invalid argument" );
462#else // !USE_MK_EXCEPTIONS
463 throw _com_error(E_INVALIDARG);
464#endif // USE_MK_EXCEPTIONS
465
466 GXMatrixMNTC<Coord> AB (m,n);
467
468 for (i = 0 ; i < m ; i++)
469 for (j = 0 ; j < n ; j++)
470 {
471 fTotal = (Coord) 0.0f;
472
473 for (k = 0 ; k < p ; k++)
474 fTotal += A(i,k) * B(k,j);
475
476 AB(i,j) = fTotal;
477 }
478
479 return AB;
480}
481
482template <class Coord>
484{
486
487 for (unsigned int i = 0, cCount = M.m_cRows * M.m_cCols ; i < cCount ; i++)
488 Ms.m_pElements[i] *= s;
489
490 return Ms;
491}
492
493template <class Coord>
495{
497
498 for (unsigned int i = 0, cCount = M.m_cRows * M.m_cCols ; i < cCount ; i++)
499 Ms.m_pElements[i] *= s;
500
501 return Ms;
502}
503
504template <class Coord>
506{
507 if (s == ((Coord) 0.0f))
508#ifdef USE_MK_EXCEPTIONS
509 throw MatrixMNTCException( "invalid argument" );
510#else // !USE_MK_EXCEPTIONS
511 throw _com_error(E_INVALIDARG);
512#endif // USE_MK_EXCEPTIONS
513
514 GXMatrixMNTC<Coord> Mds(M);
515
516 for (unsigned int i = 0, cCount = M.m_cRows * M.m_cCols ; i < cCount ; i++)
517 Mds.m_pElements[i] /= s;
518
519 return Mds;
520}
521
522template <class Coord>
524{
525 GXMatrixMNTC<Coord> Mneg (M);
526
527 Mneg *= -1;
528
529 return Mneg;
530}
531
532template <class Coord>
534{
535 return M;
536}
537
539
540template <class Coord>
541#if OS == OS_UNIX
542 const GXMatrixMNTC<Coord> Identity (Coord cOrder)
543#else
544 const GXMatrixMNTC<Coord> Identity (unsigned int cOrder)
545#endif
546{
547 if (cOrder == 0)
548#ifdef USE_MK_EXCEPTIONS
549 throw MatrixMNTCException( "invalid argument" );
550#else // !USE_MK_EXCEPTIONS
551 throw _com_error(E_INVALIDARG);
552#endif // USE_MK_EXCEPTIONS
553
554 GXMatrixMNTC<Coord> I(cOrder,cOrder,(Coord) 0.0f);
555
556 for (unsigned int i = 0 ; i < cOrder ; i++)
557 I(i,i) = (Coord) 1.0f;
558
559 return I;
560}
561
562template <class Coord>
563 const GXMatrixMNTC<Coord> ScalarMatrix (unsigned int cOrder, Coord s)
564{
565 if (cOrder == 0)
566#ifdef USE_MK_EXCEPTIONS
567 throw MatrixMNTCException( "invalid argument" );
568#else // !USE_MK_EXCEPTIONS
569 throw _com_error(E_INVALIDARG);
570#endif // USE_MK_EXCEPTIONS
571
572 GXMatrixMNTC<Coord> S(cOrder,cOrder,(Coord) 0.0f);
573
574 for (unsigned int i = 0 ; i < cOrder ; i++)
575 S(i,i) = s;
576
577 return S;
578}
579
580template <class Coord>
582{
584
585 for (unsigned int i = 0 ; i < M.m_cRows ; i++)
586 for (unsigned int j = 0 ; j < M.m_cCols ; j++)
587 Mt(j,i) = M(i,j);
588
589 return Mt;
590}
bool operator==(const GXMatrixMNTC< Coord > &A, const GXMatrixMNTC< Coord > &B)
Definition MatrixMNTC.inl:379
GXMatrixMNTC< Coord > operator-(const GXMatrixMNTC< Coord > &A, const GXMatrixMNTC< Coord > &B)
Definition MatrixMNTC.inl:429
const GXMatrixMNTC< Coord > Identity(Coord cOrder)
Definition MatrixMNTC.inl:542
const GXMatrixMNTC< Coord > ScalarMatrix(unsigned int cOrder, Coord s)
Definition MatrixMNTC.inl:563
GXMatrixMNTC< Coord > operator+(const GXMatrixMNTC< Coord > &A, const GXMatrixMNTC< Coord > &B)
Definition MatrixMNTC.inl:409
bool operator!=(const GXMatrixMNTC< Coord > &A, const GXMatrixMNTC< Coord > &B)
Definition MatrixMNTC.inl:394
GXMatrixMNTC< Coord > operator/(const GXMatrixMNTC< Coord > &M, Coord s)
Definition MatrixMNTC.inl:505
const GXMatrixMNTC< Coord > Transpose(const GXMatrixMNTC< Coord > &M)
Definition MatrixMNTC.inl:581
GXMatrixMNTC< Coord > operator*(const GXMatrixMNTC< Coord > &A, const GXMatrixMNTC< Coord > &B)
Definition MatrixMNTC.inl:449
Definition MatrixMNTC.h:48
const Coord & Get(unsigned int i, unsigned int j) const
Definition MatrixMNTC.inl:112
unsigned int m_cRows
Definition MatrixMNTC.h:112
bool IsDiagonal(void) const
Definition MatrixMNTC.inl:141
void SetDim(unsigned int cRows, unsigned int cCols)
Definition MatrixMNTC.inl:199
unsigned int m_cCols
Definition MatrixMNTC.h:113
unsigned int Cols(void) const
Definition MatrixMNTC.inl:100
const Coord & operator()(unsigned int iRow, unsigned int iCol) const
Definition MatrixMNTC.inl:351
const GXMatrixMNTC< Coord > & operator=(const GXMatrixMNTC< Coord > &toCopy)
Definition MatrixMNTC.inl:260
Coord * m_pElements
Definition MatrixMNTC.h:111
const GXMatrixMNTC< Coord > & operator/=(Coord s)
Definition MatrixMNTC.inl:335
bool IsSquare(void) const
Definition MatrixMNTC.inl:173
const GXMatrixMNTC< Coord > & operator+=(const GXMatrixMNTC< Coord > &toAdd)
Definition MatrixMNTC.inl:294
void Init(const Coord &initializeTo)
Definition MatrixMNTC.inl:118
Coord * Data(void)
Definition MatrixMNTC.inl:106
~GXMatrixMNTC(void)
Definition MatrixMNTC.inl:93
unsigned int Rows(void) const
Definition MatrixMNTC.inl:193
bool IsAntiSymmetric(void) const
Definition MatrixMNTC.inl:125
GXMatrixMNTC(void)
Definition MatrixMNTC.inl:17
const GXMatrixMNTC< Coord > & operator-=(const GXMatrixMNTC< Coord > &toAdd)
Definition MatrixMNTC.inl:310
const GXMatrixMNTC< Coord > & operator*=(Coord s)
Definition MatrixMNTC.inl:326
bool IsSymmetric(void) const
Definition MatrixMNTC.inl:179
bool IsScalar(void) const
Definition MatrixMNTC.inl:155
Definition MatrixMNTC.h:34
int val
Definition jpeglib.h:956