00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <iostream>
00013 #include <iomanip>
00014
00015 #include "RefMatrix.h"
00016
00017 using namespace Mx;
00018 using namespace std;
00019
00020
00021 template <class E>
00022 bool isSquare( const RefMatrix<E>& M ) {
00023 return M.rows() == M.cols();
00024 }
00025
00026
00027 template <class E>
00028 bool isSymmetric( const RefMatrix<E>& M ) {
00029 assert( "This code has not been tested" );
00030 if (M.rows() != M.cols()) {
00031 return false;
00032 }
00033 for (unsigned i=1; i < M.rows(); i++) {
00034 for (unsigned j=0; j < i; j++) {
00035 if (M(i,j) != M(j,i)) {
00036 return false;
00037 }
00038 }
00039 }
00040 return true;
00041 }
00042
00043
00044 template <class E>
00045 void print( const char* name, RefMatrix<E> & V ) {
00046 cout << endl << name << '[' << V.rows() << ',' << V.cols() << ']' << endl;
00047 for (unsigned i=0; i < V.rows(); ++i) {
00048 for (unsigned j=0; j < V.cols(); ++j) {
00049 cout << " " << setw(4) << V(i,j);
00050 }
00051 cout << endl;
00052 }
00053 }
00054
00055
00056 void use_lkptr( unsigned M, unsigned N ) {
00057 lkptr< RefMatrix<unsigned> > A ( new RefMatrix<unsigned>(M,N) );
00058 unsigned k = 0;
00059 for (unsigned i=0; i < A->rows(); ++i) {
00060 for (unsigned j=0; j < A->cols(); ++j) {
00061 A->at(i,j) = k++;
00062 }
00063 }
00064
00065 lkptr< RefMatrix<unsigned> > B, C ( A );
00066 assert( B == (void*)0 );
00067 B = C;
00068 B->reSize( B->cols(), B->rows() );
00069 assert( B == A );
00070 assert( *B == *A );
00071 B.reset( A->clone() );
00072 B->reSize(N,M);
00073 C = (C - C);
00074 assert( A->at(0,0) == 0 );
00075 C = B + B - B;
00076 B->reSize( B->cols(), B->rows() );
00077 A = C * B;
00078 assert( *A != *B && *B != *C && *C != *A );
00079 }
00080
00081
00082 int main() {
00083 const unsigned M = 5;
00084 const unsigned N = 8;
00085 use_lkptr( M, N );
00086 unsigned i,j,k;
00087 char * above = new char [10];
00088 typedef RefMatrix<unsigned> RefMatrix_unsigned;
00089 lkptr< RefMatrix<unsigned> > V ( new RefMatrix<unsigned>(M,N) );
00090 char * below = new char [10];
00091
00092 k = 0;
00093 for (i=0; i < V->rows(); ++i) {
00094 for (j=0; j < V->cols(); ++j) {
00095 V->at(i,j) = k++;
00096 }
00097 }
00098
00099 lkptr< RefMatrix<unsigned> > A;
00100 A = V;
00101 assert( A == V );
00102 print("A", *A);
00103
00104 V->reShape(N,M);
00105 print("V", *V);
00106
00107
00108 for (i=0; i < V->rows(); ++i) {
00109 for (j=0; j < V->cols(); ++j) {
00110 V->operator()(i,j) = k++;
00111 }
00112 }
00113
00114 lkptr< RefMatrix<unsigned> > B;
00115 B = V;
00116 assert( B == V );
00117 print("B", *B);
00118
00119 lkptr<RefMatrix_unsigned> C = V;
00120 C = C + C;
00121 print("C", *C);
00122
00123 C = A - A;
00124 print( "C", *C );
00125 assert( ! isSquare( *C ) && ! isSymmetric( *C ) );
00126
00127 B->reShape( B->cols(), B->rows() );
00128 print("A", *A);
00129 assert( A->rows() == B->rows() && A->cols() == B->cols() );
00130 A.reset( B->clone() );
00131 B->reShape( B->cols(), B->rows() );
00132 assert( V->rows() == B->rows() && V->cols() == B->cols() );
00133 print("B", *B);
00134 C = A * B;
00135 print("C", *C);
00136 assert( isSquare( *C ) && ! isSymmetric( *C ) );
00137
00138 C = (A - A) * (B + B) - C;
00139 print( "C", *C );
00140 assert( isSquare( *C ) && ! isSymmetric( *C ) );
00141
00142 print("A", *A);
00143 print("B", *B);
00144 C = B * A;
00145 print("C", *C);
00146 assert( isSquare( *C ) && ! isSymmetric( *C ) );
00147
00148 return 0;
00149 }
00150
00151
00152