Open Dynamics Engine

mat.h

00001 /*************************************************************************
00002  *                                                                       *
00003  * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
00004  * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
00005  *                                                                       *
00006  * This library is free software; you can redistribute it and/or         *
00007  * modify it under the terms of EITHER:                                  *
00008  *   (1) The GNU Lesser General Public License as published by the Free  *
00009  *       Software Foundation; either version 2.1 of the License, or (at  *
00010  *       your option) any later version. The text of the GNU Lesser      *
00011  *       General Public License is included with this library in the     *
00012  *       file LICENSE.TXT.                                               *
00013  *   (2) The BSD-style license that is included with this library in     *
00014  *       the file LICENSE-BSD.TXT.                                       *
00015  *                                                                       *
00016  * This library is distributed in the hope that it will be useful,       *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00019  * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
00020  *                                                                       *
00021  *************************************************************************/
00022 
00023 // matrix class. this is mostly for convenience in the testing code, it is
00024 // not optimized at all. correctness is much more importance here.
00025 
00026 #ifndef _ODE_MAT_H_
00027 #define _ODE_MAT_H_
00028 
00029 #include <ode/common.h>
00030 
00031 
00032 class dMatrix {
00033   int n,m;     // matrix dimension, n,m >= 0
00034   dReal *data;    // if nonzero, n*m elements allocated on the heap
00035 
00036 public:
00037   // constructors, destructors
00038   dMatrix();            // make default 0x0 matrix
00039   dMatrix (int rows, int cols);     // construct zero matrix of given size
00040   dMatrix (const dMatrix &);     // construct copy of given matrix
00041   // create copy of given data - element (i,j) is data[i*rowskip+j*colskip]
00042   dMatrix (int rows, int cols, dReal *_data, int rowskip, int colskip);
00043   ~dMatrix();           // destructor
00044 
00045   // data movement
00046   dReal & operator () (int i, int j);  // reference an element
00047   void operator= (const dMatrix &); // matrix = matrix
00048   void operator= (dReal);     // matrix = scalar
00049   dMatrix transpose();        // return transposed matrix
00050   // return a permuted submatrix of this matrix, made up of the rows in p
00051   // and the columns in q. p has np elements, q has nq elements.
00052   dMatrix select (int np, int *p, int nq, int *q);
00053 
00054   // operators
00055   dMatrix operator + (const dMatrix &);
00056   dMatrix operator - (const dMatrix &);
00057   dMatrix operator - ();
00058   dMatrix operator * (const dMatrix &);
00059   void operator += (const dMatrix &);
00060   void operator -= (const dMatrix &);
00061 
00062   // utility
00063   void clearUpperTriangle();
00064   void clearLowerTriangle();
00065   void makeRandom (dReal range);
00066   void print (char *fmt = "%10.4f ", FILE *f=stdout);
00067   dReal maxDifference (const dMatrix &);
00068 };
00069 
00070 
00071 #endif