// Rascal, the Advanced Scientific CALculator // Copyright (C) 2001, Sebastian Ritterbusch (Rascal@Ritterbusch.de) // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more detauls. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // #ifndef MATRIX_HPP_INCLUDED #define MATRIX_HPP_INCLUDED // #define INDCHECK #include template class mmatrix { public: T **a; int N,M; public: mmatrix(int aN,int aM) : N(aN),M(aM) { a=new T *[N]; int i; for(i=0;i &b) : N(b.N),M(b.M) { int i,j; a=new T *[N]; for(i=0;i & operator =(const mmatrix & b) { int i,j; if(N!=b.N || M!=b.M) { for(i=0;i & operator +=(const mmatrix & b) { int i,j; int iN(b.N & operator -=(const mmatrix & b) { int i,j; int iN(b.N & foreach(T (*f)(const T &)) { int i,j; for(i=0;i foreach(T (*f)(const T &)) const { int i,j; mmatrix temp(N,M); for(i=0;i & foreach(T (*f)(T)) { int i,j; for(i=0;i & foreach(T (*f)(T)) const { int i,j; mmatrix temp(N,M); for(i=0;i operator +(mmatrix a,const mmatrix & b) { return a+=b; } friend inline mmatrix operator -(mmatrix a,const mmatrix & b) { return a-=b; } T & operator ()(int i,int j) { #ifndef INDCHECK return a[i][j]; #else if(i>=0 && i=0 && j=0 && i=0 && j=0 && i=0 && j=0 && i=0 && j row(int i) const { if(i>=0 && i r(1,M); for(int j=0;j(0,M); } friend ostream & operator <<(ostream & o,const mmatrix & x) { int i,j; for(i=0;i inline mmatrix operator *(const mmatrix & x,const mmatrix & y) { int i,j,k; int iB(x.M z(x.N,y.M); for(i=0;i inline int operator ==(const mmatrix & x,const mmatrix & y) { int i,j,k; if(x.M!=y.M || x.N!=y.N) return 0; for(i=0;i inline int operator !=(const mmatrix & x,const mmatrix & y) { int i,j,k; if(x.M!=y.M || x.N!=y.N) return 1; for(i=0;i inline mmatrix operator *(const S & x,const mmatrix & y) { int i,j,k; mmatrix z(y.N,y.M); for(i=0;i inline mmatrix operator -(const mmatrix & y) { int i,j,k; mmatrix z(y.N,y.M); for(i=0;i mmatrix trans(const mmatrix & A) { mmatrix B(A.M,A.N); int i,j; for(i=0;i mmatrix appendcol(const mmatrix & A,const mmatrix & c) // append col { int maxN(A.N>c.N?A.N:c.N); mmatrix B(maxN,A.M+c.M); int i,j; for(i=0;i mmatrix appendline(const mmatrix & A,const mmatrix & l) // append line { int maxM(A.M>l.M?A.M:l.M); mmatrix B(A.N+l.N,maxM); int i,j; for(i=0;i mmatrix inv(mmatrix A) { int N(A.N B(N,N); int i,j,k; for(i=0;i(0,0); // Sorry, inversion not possible. } } for(j=0;j T det(mmatrix A) { if((A.N==1 && A.M>0) || (A.M==1 && A.N>0)) return A(0,0); int N(A.N B(N-1,N-1); T res; int i,j,k; for(i=0;i mmatrix pow(mmatrix A,int p) { mmatrix b(A.N,A.M,1,0); if(p<0) { A=inv(A); p=-p; } while(p!=0) { if(p&1) b=b*A; p=p/2; if(p) A=A*A; } return b; } #endif