Matrix.h
Go to the documentation of this file.
00001 
00002 #ifndef __WRAPPED_MATRIX_H__
00003 #define __WRAPPED_MATRIX_H__
00004 
00005 #include <string.h>
00006 
00007 #include <iostream>
00008 #include <sstream>
00009 
00010 #include <NewMat/Matrix.h>
00011 #include <NewMat/Temporary.h>
00012 
00013 namespace jlo
00014 {
00015 
00016     class Matrix
00017     {
00018         public:
00019             Matrix() : w(0), h(0), data(0), printed(0) {}
00020             Matrix(int _w, int _h)
00021                 : w(_w), h(_h), data( (_w * _h > 0) ? new double[_w * _h] : 0 ), printed(0)
00022             { memset( data, 0, w * h * sizeof( *data ) ); }
00023             Matrix( const jlo::Matrix &src )
00024                 : w(src.w), h(src.h), data( (w * h > 0) ? new double[ src.w * src.h ] : 0 ), printed(0)
00025             { memcpy( data, src.data, w*h ); }
00026             Matrix( const ::BaseMatrix &src )
00027                 : h(0), w(0), data(0), printed(0)
00028             { init( src ); }
00029 
00030             ~Matrix() { delete[] data; delete[] printed; }
00031             int width() const { return w; }
00032             int height() const { return h; }
00033             double get( int x, int y ) const { return data[ y*w + x ]; }
00034             void set( int x, int y, double newval ) { data[ y*w + x ] = newval; }
00035 
00036             operator ::Matrix() const
00037             {
00038                 ::Matrix result( toReturnMatrix() );
00039                 return result;
00040             }
00041             operator ReturnMatrix() const
00042             {
00043                 return toReturnMatrix();
00044             }
00045             Matrix &operator=( const Matrix &src )
00046             {
00047                 w = src.w;
00048                 h = src.h;
00049                 delete[] data;
00050                 data = w * h ? new double[w * h] : 0;
00051                 memcpy( data, src.data, w * h * sizeof( *data ) );
00052                 return *this;
00053             }
00054             const char *print()
00055             {
00056                 std::stringstream str;
00057 
00058                 str.str("");
00059                 for( int y=0; y<height(); y++ )
00060                 {
00061                     str << "| ";
00062                     for( int x=0; x<width(); x++ )
00063                         str << get(x, y) << " ";
00064                     str << "|" << std::endl;
00065                 }
00066                 delete[] printed;
00067                 printed = new char[ str.str().size()+1 ];
00068                 strcpy( printed, str.str().c_str() );
00069                 return printed;
00070             }
00071 
00072         private:
00073             int w;
00074             int h;
00075             double *data;
00076             char *printed;
00077 
00078             void init( const ::Matrix &src )
00079             {
00080                 if( w!=src.ncols() || h!=src.nrows() )
00081                 {
00082                     delete[] data;
00083                     w = src.ncols();
00084                     h = src.nrows();
00085                     data = (w * h > 0) ? new double[ w * h ] : 0;
00086                 }
00087                 for( int y=0; y<h; y++ )
00088                 {
00089                     for( int x=0; x<w; x++ )
00090                         set( x, y, src.element( y, x ) );
00091                 }
00092             }
00093             ::ReturnMatrix toReturnMatrix() const
00094             {
00095                 ::Matrix result( w, h );
00096 
00097                 for( int y=0; y<h; y++ )
00098                 {
00099                     for( int x=0; x<w; x++ )
00100                         result.element( y, x ) = get( x, y );
00101                 }
00102                 return ::ReturnMatrix( result );
00103             }
00104     };
00105 }
00106 
00107 #endif
00108 


lo
Author(s): U. Klank
autogenerated on Mon Oct 6 2014 10:44:13