# matrix.h

    00001 //
    00002 // matrix.h
    00003 //
    00004 // Copyright (C) 1996 Limit Point Systems, Inc.
    00005 //
    00006 // Author: Curtis Janssen <cljanss@limitpt.com>
    00007 // Maintainer: LPS
    00008 //
    00009 // This file is part of the SC Toolkit.
    00010 //
    00011 // The SC Toolkit is free software; you can redistribute it and/or modify
    00012 // it under the terms of the GNU Library General Public License as published by
    00013 // the Free Software Foundation; either version 2, or (at your option)
    00014 // any later version.
    00015 //
    00016 // The SC Toolkit 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
    00019 // GNU Library General Public License for more details.
    00020 //
    00021 // You should have received a copy of the GNU Library General Public License
    00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
    00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
    00024 //
    00025 // The U.S. Government is granted a limited license as per AL 91-7.
    00026 //
    00027
    00028 #ifndef _math_scmat_matrix_h
    00029 #define _math_scmat_matrix_h
    00030 #ifdef __GNUC__
    00031 #pragma interface
    00032 #endif
    00033
    00034 #include <iostream>
    00035
    00036 #include <math/scmat/abstract.h>
    00037
    00038 namespace sc {
    00039
    00040 class SCVectordouble;
    00041 class SCMatrixdouble;
    00042 class SymmSCMatrixdouble;
    00043 class DiagSCMatrixdouble;
    00044
    00045 class SCMatrixBlockIter;
    00046 class SCMatrixRectBlock;
    00047 class SCMatrixLTriBlock;
    00048 class SCMatrixDiagBlock;
    00049 class SCVectorSimpleBlock;
    00050
    00051 class RefSCMatrix;
    00052 class RefSymmSCMatrix;
    00055 class RefSCVector: public Ref<SCVector> {
    00056     // standard overrides
    00057   public:
    00060     RefSCVector();
    00062     RefSCVector(const RefSCVector& v);
    00064     RefSCVector(SCVector *v);
    00065     // don't allow automatic conversion from any reference to a
    00066     // described class
    00067     ~RefSCVector();
    00069     RefSCVector& operator=(SCVector* v);
    00071     RefSCVector& operator=(const RefSCVector& v);
    00072
    00073     // vector specific members
    00074   public:
    00077     RefSCVector(const RefSCDimension& dim,const Ref<SCMatrixKit>&);
    00078
    00080     SCVectordouble operator()(int) const;
    00082     SCVectordouble operator[](int) const;
    00084     RefSCVector operator+(const RefSCVector&a) const;
    00086     RefSCVector operator-(const RefSCVector&a) const;
    00088     RefSCVector operator*(double) const;
    00090     RefSCMatrix outer_product(const RefSCVector& v) const;
    00092     RefSymmSCMatrix symmetric_outer_product() const;
    00093
    00094     void set_element(int i,double val) const;
    00095     void accumulate_element(int i,double val) const;
    00096     double get_element(int) const;
    00097     int n() const;
    00098     RefSCDimension dim() const;
    00099     Ref<SCMatrixKit> kit() const;
    00100     RefSCVector clone() const;
    00101     RefSCVector copy() const;
    00102     double maxabs() const;
    00103     double scalar_product(const RefSCVector&) const;
    00104     double dot(const RefSCVector&) const;
    00105     void normalize() const;
    00106     void randomize() const;
    00107     void assign(const RefSCVector& v) const;
    00108     void assign(double val) const;
    00109     void assign(const double* v) const;
    00110     void convert(double*) const;
    00111     void scale(double val) const;
    00112     void accumulate(const RefSCVector& v) const;
    00113     void accumulate_product(const RefSymmSCMatrix&, const RefSCVector&);
    00114     void accumulate_product(const RefSCMatrix&, const RefSCVector&);
    00115     void element_op(const Ref<SCElementOp>& op) const;
    00116     void element_op(const Ref<SCElementOp2>&,
    00117                     const RefSCVector&) const;
    00118     void element_op(const Ref<SCElementOp3>&,
    00119                     const RefSCVector&,
    00120                     const RefSCVector&) const;
    00121     void print(std::ostream&out) const;
    00122     void print(const char*title=0,
    00123                std::ostream&out=ExEnv::out0(), int precision=10) const;
    00124     void save(StateOut&);
    00126     void restore(StateIn&);
    00127 };
    00128 RefSCVector operator*(double,const RefSCVector&);
    00129
    00130 class RefSymmSCMatrix;
    00131 class RefDiagSCMatrix;
    00135 class RefSCMatrix: public Ref<SCMatrix> {
    00136     // standard overrides
    00137   public:
    00140     RefSCMatrix();
    00142     RefSCMatrix(const RefSCMatrix& m);
    00144      RefSCMatrix(SCMatrix* m);
    00145     ~RefSCMatrix();
    00147     RefSCMatrix& operator=(SCMatrix* m);
    00149     RefSCMatrix& operator=(const RefSCMatrix& m);
    00150
    00151     // matrix specific members
    00152   public:
    00155     RefSCMatrix(const RefSCDimension& d1,const RefSCDimension& d2,
    00156                 const Ref<SCMatrixKit>&);
    00157
    00159     RefSCVector operator*(const RefSCVector&) const;
    00160
    00162     RefSCMatrix operator*(const RefSCMatrix&) const;
    00163     RefSCMatrix operator*(const RefSymmSCMatrix&) const;
    00164     RefSCMatrix operator*(const RefDiagSCMatrix&) const;
    00165
    00167     RefSCMatrix operator*(double) const;
    00168
    00170     RefSCMatrix operator+(const RefSCMatrix&) const;
    00172     RefSCMatrix operator-(const RefSCMatrix&) const;
    00173
    00175     RefSCMatrix t() const;
    00177     RefSCMatrix i() const;
    00179     RefSCMatrix gi() const;
    00180
    00183     RefSCMatrix clone() const;
    00184     RefSCMatrix copy() const;
    00185
    00186     RefSCMatrix get_subblock(int br, int er, int bc, int ec);
    00187     void assign_subblock(const RefSCMatrix&, int br, int er, int bc, int ec,
    00188                          int source_br = 0, int source_bc = 0);
    00189     void accumulate_subblock(const RefSCMatrix&, int, int, int, int,
    00190                              int source_br = 0, int source_bc = 0);
    00191     RefSCVector get_row(int) const;
    00192     RefSCVector get_column(int) const;
    00193     void assign_row(const RefSCVector&, int) const;
    00194     void assign_column(const RefSCVector&, int) const;
    00195     void accumulate_row(const RefSCVector&, int) const;
    00196     void accumulate_column(const RefSCVector&, int) const;
    00197
    00198     void accumulate_outer_product(const RefSCVector&,const RefSCVector&) const;
    00199     void accumulate_product(const RefSCMatrix&,const RefSCMatrix&) const;
    00200     void assign(const RefSCMatrix&) const;
    00201     void scale(double) const;
    00202     void randomize() const;
    00203     void assign(double) const;
    00204     void assign(const double*) const;
    00205     void assign(const double**) const;
    00206     void convert(double*) const;
    00207     void convert(double**) const;
    00208     void accumulate(const RefSCMatrix&) const;
    00209     void accumulate(const RefSymmSCMatrix&) const;
    00210     void accumulate(const RefDiagSCMatrix&) const;
    00211     void element_op(const Ref<SCElementOp>&) const;
    00212     void element_op(const Ref<SCElementOp2>&,
    00213                     const RefSCMatrix&) const;
    00214     void element_op(const Ref<SCElementOp3>&,
    00215                     const RefSCMatrix&,
    00216                     const RefSCMatrix&) const;
    00217     int nrow() const;
    00218     int ncol() const;
    00219     RefSCDimension rowdim() const;
    00220     RefSCDimension coldim() const;
    00221     Ref<SCMatrixKit> kit() const;
    00222     void set_element(int,int,double) const;
    00223     void accumulate_element(int,int,double) const;
    00224     double get_element(int,int) const;
    00225     void print(std::ostream&) const;
    00226     void print(const char*title=0,
    00227                std::ostream&out=ExEnv::out0(), int =10) const;
    00228     double trace() const;
    00229     void save(StateOut&);
    00231     void restore(StateIn&);
    00232
    00237     void svd(const RefSCMatrix &U,
    00238              const RefDiagSCMatrix &sigma,
    00239              const RefSCMatrix &V);
    00241     double solve_lin(const RefSCVector& v) const;
    00243     double determ() const;
    00245     SCMatrixdouble operator()(int i,int j) const;
    00246
    00250     int nblock() const;
    00254     RefSCMatrix block(int i) const;
    00255 };
    00257 RefSCMatrix operator*(double,const RefSCMatrix&);
    00258
    00261 class RefSymmSCMatrix: public Ref<SymmSCMatrix> {
    00262     // standard overrides
    00263   public:
    00266     RefSymmSCMatrix();
    00268     RefSymmSCMatrix(const RefSymmSCMatrix& m);
    00270     RefSymmSCMatrix(SymmSCMatrix *m);
    00271     ~RefSymmSCMatrix();
    00273     RefSymmSCMatrix& operator=(SymmSCMatrix* m);
    00275     RefSymmSCMatrix& operator=(const RefSymmSCMatrix& m);
    00276
    00277     // matrix specific members
    00278   public:
    00281     RefSymmSCMatrix(const RefSCDimension& d,const Ref<SCMatrixKit>&);
    00283     RefSCMatrix operator*(const RefSCMatrix&) const;
    00284     RefSCMatrix operator*(const RefSymmSCMatrix&) const;
    00286     RefSCVector operator*(const RefSCVector&a) const;
    00287     RefSymmSCMatrix operator*(double) const;
    00289     RefSymmSCMatrix operator+(const RefSymmSCMatrix&) const;
    00290     RefSymmSCMatrix operator-(const RefSymmSCMatrix&) const;
    00292     RefSymmSCMatrix i() const;
    00294     RefSymmSCMatrix gi() const;
    00297     RefSymmSCMatrix clone() const;
    00298     RefSymmSCMatrix copy() const;
    00299     void set_element(int,int,double) const;
    00300     void accumulate_element(int,int,double) const;
    00301     double get_element(int,int) const;
    00302
    00303     RefSCMatrix get_subblock(int br, int er, int bc, int ec);
    00304     RefSymmSCMatrix get_subblock(int br, int er);
    00305     void assign_subblock(const RefSCMatrix&, int br, int er, int bc, int ec);
    00306     void assign_subblock(const RefSymmSCMatrix&, int br, int er);
    00307     void accumulate_subblock(const RefSCMatrix&, int, int, int, int);
    00308     void accumulate_subblock(const RefSymmSCMatrix&, int, int);
    00309     RefSCVector get_row(int);
    00310     void assign_row(const RefSCVector&, int);
    00311     void accumulate_row(const RefSCVector&, int);
    00312
    00313     void accumulate_symmetric_outer_product(const RefSCVector&) const;
    00314     double scalar_product(const RefSCVector&) const;
    00315     void accumulate_symmetric_product(const RefSCMatrix&) const;
    00316     void accumulate_symmetric_sum(const RefSCMatrix&) const;
    00318     void accumulate_transform(const RefSCMatrix&a,const RefSymmSCMatrix&b,
    00319                         SCMatrix::Transform = SCMatrix::NormalTransform) const;
    00320     void accumulate_transform(const RefSCMatrix&a,const RefDiagSCMatrix&b,
    00321                         SCMatrix::Transform = SCMatrix::NormalTransform) const;
    00322     void accumulate_transform(const RefSymmSCMatrix&a,
    00323                               const RefSymmSCMatrix&b) const;
    00324
    00325     void randomize() const;
    00326     void assign(const RefSymmSCMatrix&) const;
    00327     void scale(double) const;
    00328     void assign(double) const;
    00329     void assign(const double*) const;
    00330     void assign(const double**) const;
    00331     void convert(double*) const;
    00332     void convert(double**) const;
    00333     void accumulate(const RefSymmSCMatrix&) const;
    00334     void element_op(const Ref<SCElementOp>&) const;
    00335     void element_op(const Ref<SCElementOp2>&,
    00336                     const RefSymmSCMatrix&) const;
    00337     void element_op(const Ref<SCElementOp3>&,
    00338                     const RefSymmSCMatrix&,
    00339                     const RefSymmSCMatrix&) const;
    00340     double trace() const;
    00341     int n() const;
    00342     RefSCDimension dim() const;
    00343     Ref<SCMatrixKit> kit() const;
    00344     void print(std::ostream&) const;
    00345     void print(const char*title=0,
    00346                std::ostream&out=ExEnv::out0(), int =10) const;
    00347     void save(StateOut&);
    00349     void restore(StateIn&);
    00350
    00352     double solve_lin(const RefSCVector&) const;
    00354     double determ() const;
    00356     RefDiagSCMatrix eigvals() const;
    00358     RefSCMatrix eigvecs() const;
    00362     void diagonalize(const RefDiagSCMatrix& eigvals,
    00363                      const RefSCMatrix& eigvecs) const;
    00365     SymmSCMatrixdouble operator()(int i,int j) const;
    00369     int nblock() const;
    00373     RefSymmSCMatrix block(int i) const;
    00374 };
    00376 RefSymmSCMatrix operator*(double,const RefSymmSCMatrix&);
    00377
    00380 class RefDiagSCMatrix: public Ref<DiagSCMatrix> {
    00381     // standard overrides
    00382   public:
    00385     RefDiagSCMatrix();
    00387     RefDiagSCMatrix(const RefDiagSCMatrix& m);
    00389     RefDiagSCMatrix(DiagSCMatrix *m);
    00390     ~RefDiagSCMatrix();
    00392     RefDiagSCMatrix& operator=(DiagSCMatrix* m);
    00394     RefDiagSCMatrix& operator=(const RefDiagSCMatrix & m);
    00395
    00396     // matrix specific members
    00397   public:
    00400     RefDiagSCMatrix(const RefSCDimension&,const Ref<SCMatrixKit>&);
    00402     RefSCMatrix operator*(const RefSCMatrix&) const;
    00403     RefDiagSCMatrix operator*(double) const;
    00405     RefDiagSCMatrix operator+(const RefDiagSCMatrix&) const;
    00406     RefDiagSCMatrix operator-(const RefDiagSCMatrix&) const;
    00408     RefDiagSCMatrix i() const;
    00410     RefDiagSCMatrix gi() const;
    00413     RefDiagSCMatrix clone() const;
    00414     RefDiagSCMatrix copy() const;
    00415     void set_element(int,double) const;
    00416     void accumulate_element(int,double) const;
    00417     double get_element(int) const;
    00418     void randomize() const;
    00419     void assign(const RefDiagSCMatrix&) const;
    00420     void scale(double) const;
    00421     void assign(double) const;
    00422     void assign(const double*) const;
    00423     void convert(double*) const;
    00424     void accumulate(const RefDiagSCMatrix&) const;
    00425     void element_op(const Ref<SCElementOp>&) const;
    00426     void element_op(const Ref<SCElementOp2>&,
    00427                     const RefDiagSCMatrix&) const;
    00428     void element_op(const Ref<SCElementOp3>&,
    00429                     const RefDiagSCMatrix&,
    00430                     const RefDiagSCMatrix&) const;
    00431     int n() const;
    00432     RefSCDimension dim() const;
    00433     Ref<SCMatrixKit> kit() const;
    00434     double trace() const;
    00435     void print(std::ostream&) const;
    00436     void print(const char*title=0,
    00437                std::ostream&out=ExEnv::out0(), int =10) const;
    00438     void save(StateOut&);
    00440     void restore(StateIn&);
    00442     double determ() const;
    00444     DiagSCMatrixdouble operator()(int i) const;
    00448     int nblock() const;
    00452     RefDiagSCMatrix block(int i) const;
    00453 };
    00455 RefDiagSCMatrix operator*(double,const RefDiagSCMatrix&);
    00456
    00457 class SCVectordouble {
    00458    friend class RefSCVector;
    00459   private:
    00460     RefSCVector vector;
    00461     int i;
    00462
    00463     SCVectordouble(SCVector*,int);
    00464   public:
    00465     SCVectordouble(const SCVectordouble&);
    00466     ~SCVectordouble();
    00467     double operator=(double a);
    00468     double operator=(const SCVectordouble&);
    00469     operator double();
    00470     double val() const;
    00471 };
    00472
    00473 class SCMatrixdouble {
    00474    friend class RefSCMatrix;
    00475   private:
    00476     RefSCMatrix matrix;
    00477     int i;
    00478     int j;
    00479
    00480     SCMatrixdouble(SCMatrix*,int,int);
    00481   public:
    00482     SCMatrixdouble(const SCMatrixdouble&);
    00483     ~SCMatrixdouble();
    00484     double operator=(double a);
    00485     double operator=(const SCMatrixdouble&);
    00486     operator double();
    00487     double val() const;
    00488 };
    00489
    00490 class SymmSCMatrixdouble {
    00491    friend class RefSymmSCMatrix;
    00492   private:
    00493     RefSymmSCMatrix matrix;
    00494     int i;
    00495     int j;
    00496
    00497     SymmSCMatrixdouble(SymmSCMatrix*,int,int);
    00498   public:
    00499     SymmSCMatrixdouble(const SCMatrixdouble&);
    00500     ~SymmSCMatrixdouble();
    00501     double operator=(double a);
    00502     double operator=(const SymmSCMatrixdouble&);
    00503     operator double();
    00504     double val() const;
    00505 };
    00506
    00507 class DiagSCMatrixdouble {
    00508    friend class RefDiagSCMatrix;
    00509   private:
    00510     RefDiagSCMatrix matrix;
    00511     int i;
    00512     int j;
    00513
    00514     DiagSCMatrixdouble(DiagSCMatrix*,int,int);
    00515   public:
    00516     DiagSCMatrixdouble(const SCMatrixdouble&);
    00517     ~DiagSCMatrixdouble();
    00518     double operator=(double a);
    00519     double operator=(const DiagSCMatrixdouble&);
    00520     operator double();
    00521     double val() const;
    00522 };
    00523
    00524 }
    00525
    00526 #ifdef INLINE_FUNCTIONS
    00527 #include <math/scmat/matrix_i.h>
    00528 #endif
    00529
    00530 #endif
    00531
    00532 // Local Variables:
    00533 // mode: c++
    00534 // c-file-style: "CLJ"
    00535 // End: