- Main Page
- Related Pages
- Classes
- Files

- File List

# abstract.h

    00001 //
    00002 // abstract.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_abstract_h
    00029 #define _math_scmat_abstract_h
    00030
    00031 #ifdef __GNUC__
    00032 #pragma interface
    00033 #endif
    00034
    00035 #include <util/group/message.h>
    00036
    00037 #include <util/state/state.h>
    00038 #include <math/scmat/dim.h>
    00039 #include <math/scmat/block.h>
    00040 #include <iostream>
    00041
    00042 namespace sc {
    00043
    00044 class SCMatrix;
    00045 class SymmSCMatrix;
    00046 class DiagSCMatrix;
    00047 class SCVector;
    00048
    00049 class SCElementOp;
    00050 class SCElementOp2;
    00051 class SCElementOp3;
    00052
    00053 class RefSCDimension;
    00054
    00058 class SCMatrixKit: public DescribedClass {
    00059   protected:
    00060     Ref<MessageGrp> grp_;
    00061
    00062   public:
    00063     SCMatrixKit();
    00064     SCMatrixKit(const Ref<KeyVal>&);
    00065     ~SCMatrixKit();
    00066
    00067     // these members are default in local.cc
    00070     static SCMatrixKit* default_matrixkit();
    00071     static void set_default_matrixkit(const Ref<SCMatrixKit> &);
    00072
    00073     Ref<MessageGrp> messagegrp() const;
    00074
    00076     virtual SCMatrix* matrix(const RefSCDimension&,const RefSCDimension&) = 0;
    00077     virtual SymmSCMatrix* symmmatrix(const RefSCDimension&) = 0;
    00078     virtual DiagSCMatrix* diagmatrix(const RefSCDimension&) = 0;
    00079     virtual SCVector* vector(const RefSCDimension&) = 0;
    00080
    00083     SCMatrix* restore_matrix(StateIn&,
    00084                              const RefSCDimension&,
    00085                              const RefSCDimension&);
    00086     SymmSCMatrix* restore_symmmatrix(StateIn&,
    00087                                      const RefSCDimension&);
    00088     DiagSCMatrix* restore_diagmatrix(StateIn&,
    00089                                      const RefSCDimension&);
    00090     SCVector* restore_vector(StateIn&,
    00091                              const RefSCDimension&);
    00092 };
    00093
    00094
    00097 class SCVector: public DescribedClass {
    00098   protected:
    00099     RefSCDimension d;
    00100     Ref<SCMatrixKit> kit_;
    00101   public:
    00102     SCVector(const RefSCDimension&, SCMatrixKit *);
    00103
    00105     virtual void save(StateOut&);
    00106     virtual void restore(StateIn&);
    00107
    00109     Ref<SCMatrixKit> kit() const { return kit_; }
    00110
    00111     // concrete functions (some can be overridden)
    00113     virtual SCVector* copy();
    00115     virtual SCVector* clone();
    00116
    00117     virtual ~SCVector();
    00119     int n() const { return d->n(); }
    00121     virtual double maxabs() const;
    00123     virtual void normalize();
    00125     virtual void randomize();
    00127     void assign(double val) { assign_val(val); }
    00129     void assign(const double* v) { assign_p(v); }
    00132     void assign(SCVector* v) { assign_v(v); }
    00134     virtual void assign_val(double val);
    00135     virtual void assign_p(const double* v);
    00136     virtual void assign_v(SCVector *v);
    00138     virtual void convert(double* v) const;
    00141     virtual void convert(SCVector*);
    00142     virtual void convert_accumulate(SCVector*);
    00144     virtual void scale(double val);
    00145
    00147     RefSCDimension dim() const { return d; }
    00149     virtual void set_element(int i,double val) = 0;
    00151     virtual void accumulate_element(int,double) = 0;
    00153     virtual double get_element(int i) const = 0;
    00155     void accumulate_product(SymmSCMatrix* m, SCVector* v)
    00156         { accumulate_product_sv(m,v); }
    00157     void accumulate_product(SCMatrix* m, SCVector* v)
    00158         {  accumulate_product_rv(m,v); }
    00159     virtual void accumulate_product_sv(SymmSCMatrix* m, SCVector* v);
    00160     virtual void accumulate_product_rv(SCMatrix* m, SCVector* v) = 0;
    00162     virtual void accumulate(const SCVector*v) = 0;
    00164     virtual void accumulate(const SCMatrix*m) = 0;
    00166     virtual double scalar_product(SCVector*) = 0;
    00168     virtual void element_op(const Ref<SCElementOp>&) = 0;
    00169     virtual void element_op(const Ref<SCElementOp2>&,
    00170                             SCVector*) = 0;
    00171     virtual void element_op(const Ref<SCElementOp3>&,
    00172                             SCVector*,SCVector*) = 0;
    00174     void print(std::ostream&o=ExEnv::out0()) const;
    00175     void print(const char* title=0,std::ostream&out=ExEnv::out0(),int=10) const;
    00176     virtual void vprint(const char*title=0,std::ostream&out=ExEnv::out0(),
    00177                         int=10) const = 0;
    00178
    00180     Ref<MessageGrp> messagegrp() const;
    00181
    00186     virtual Ref<SCMatrixSubblockIter> local_blocks(
    00187         SCMatrixSubblockIter::Access) = 0;
    00189     virtual Ref<SCMatrixSubblockIter> all_blocks(SCMatrixSubblockIter::Access) = 0;
    00190 };
    00191
    00195 class SCMatrix: public DescribedClass {
    00196   protected:
    00197     RefSCDimension d1,d2;
    00198     Ref<SCMatrixKit> kit_;
    00199   public:
    00200     // used to control transformations
    00201     enum Transform { NormalTransform = 0, TransposeTransform = 1 };
    00202
    00203     // concrete functions (some can be overridden)
    00204     SCMatrix(const RefSCDimension&, const RefSCDimension&, SCMatrixKit *);
    00205     virtual ~SCMatrix();
    00206
    00208     virtual void save(StateOut&);
    00209     virtual void restore(StateIn&);
    00210
    00212     Ref<SCMatrixKit> kit() const { return kit_; }
    00213
    00215     int nrow() const { return d1->n(); }
    00217     int ncol() const { return d2->n(); }
    00219     virtual double maxabs() const;
    00221     virtual void randomize();
    00223     void assign(double val) { assign_val(val); }
    00225     void assign(const double* m) { assign_p(m); }
    00227     void assign(const double** m) { assign_pp(m); }
    00229     void assign(SCMatrix* m) { assign_r(m); }
    00231     virtual void assign_val(double val);
    00232     virtual void assign_p(const double* m);
    00233     virtual void assign_pp(const double** m);
    00234     virtual void assign_r(SCMatrix* m);
    00237     virtual void convert(double*) const;
    00238     virtual void convert(double**) const;
    00241     virtual void convert(SCMatrix*);
    00242     virtual void convert_accumulate(SCMatrix*);
    00244     virtual void scale(double val);
    00246     virtual void scale_diagonal(double val);
    00248     virtual void shift_diagonal(double val);
    00250     virtual void unit();
    00252     virtual SCMatrix* copy();
    00254     virtual SCMatrix* clone();
    00255
    00256     // pure virtual functions
    00258     RefSCDimension rowdim() const { return d1; }
    00259     RefSCDimension coldim() const { return d2; }
    00261     virtual double get_element(int,int) const = 0;
    00262     virtual void set_element(int,int,double) = 0;
    00263     virtual void accumulate_element(int,int,double) = 0;
    00264
    00268     virtual SCMatrix * get_subblock(int br, int er, int bc, int ec) =0;
    00269
    00271     virtual void assign_subblock(SCMatrix *m, int, int, int, int, int=0, int=0) =0;
    00272
    00274     virtual void accumulate_subblock(SCMatrix *m, int, int, int, int, int=0,int=0) =0;
    00275
    00277     virtual SCVector * get_row(int i) =0;
    00278     virtual SCVector * get_column(int i) =0;
    00279
    00281     virtual void assign_row(SCVector *v, int i) =0;
    00282     virtual void assign_column(SCVector *v, int i) =0;
    00283
    00285     virtual void accumulate_row(SCVector *v, int i) =0;
    00286     virtual void accumulate_column(SCVector *v, int i) =0;
    00287
    00289     virtual void accumulate(const SCMatrix* m) = 0;
    00290     virtual void accumulate(const SymmSCMatrix* m) = 0;
    00291     virtual void accumulate(const DiagSCMatrix* m) = 0;
    00292     virtual void accumulate(const SCVector*) = 0;
    00294     virtual void accumulate_outer_product(SCVector*,SCVector*) = 0;
    00295     void accumulate_product(SCMatrix*m1,SCMatrix*m2)
    00296         { accumulate_product_rr(m1,m2); }
    00297     void accumulate_product(SCMatrix*m1,SymmSCMatrix*m2)
    00298         { accumulate_product_rs(m1,m2); }
    00299     void accumulate_product(SCMatrix*m1,DiagSCMatrix*m2)
    00300         { accumulate_product_rd(m1,m2); }
    00301     void accumulate_product(SymmSCMatrix*m1,SCMatrix*m2)
    00302         { accumulate_product_sr(m1,m2); }
    00303     void accumulate_product(DiagSCMatrix*m1,SCMatrix*m2)
    00304         { accumulate_product_dr(m1,m2); }
    00305     void accumulate_product(SymmSCMatrix*m1,SymmSCMatrix*m2)
    00306         { accumulate_product_ss(m1,m2); }
    00307     virtual void accumulate_product_rr(SCMatrix*,SCMatrix*) = 0;
    00308     virtual void accumulate_product_rs(SCMatrix*,SymmSCMatrix*);
    00309     virtual void accumulate_product_rd(SCMatrix*,DiagSCMatrix*);
    00310     virtual void accumulate_product_sr(SymmSCMatrix*,SCMatrix*);
    00311     virtual void accumulate_product_dr(DiagSCMatrix*,SCMatrix*);
    00312     virtual void accumulate_product_ss(SymmSCMatrix*,SymmSCMatrix*);
    00314     virtual void transpose_this() = 0;
    00316     virtual double trace() =0;
    00318     virtual double invert_this() = 0;
    00320     virtual double determ_this() = 0;
    00321
    00324     virtual void svd_this(SCMatrix *U, DiagSCMatrix *sigma, SCMatrix *V);
    00325     virtual double solve_this(SCVector*) = 0;
    00326     virtual void gen_invert_this();
    00327
    00330     virtual void schmidt_orthog(SymmSCMatrix*, int n) =0;
    00331
    00335     virtual int schmidt_orthog_tol(SymmSCMatrix*, double tol, double*res=0)=0;
    00336
    00338     virtual void element_op(const Ref<SCElementOp>&) = 0;
    00339     virtual void element_op(const Ref<SCElementOp2>&,
    00340                             SCMatrix*) = 0;
    00341     virtual void element_op(const Ref<SCElementOp3>&,
    00342                             SCMatrix*,SCMatrix*) = 0;
    00344     void print(std::ostream&o=ExEnv::out0()) const;
    00345     void print(const char* title=0,std::ostream& out=ExEnv::out0(),
    00346                int =10) const;
    00347     virtual void vprint(const char*title=0,
    00348                         std::ostream&out=ExEnv::out0(),int =10) const = 0;
    00349
    00351     Ref<MessageGrp> messagegrp() const;
    00352
    00355     virtual Ref<SCMatrixSubblockIter> local_blocks(
    00356         SCMatrixSubblockIter::Access) = 0;
    00358     virtual Ref<SCMatrixSubblockIter> all_blocks(
    00359         SCMatrixSubblockIter::Access) = 0;
    00360 };
    00361
    00364 class SymmSCMatrix: public DescribedClass {
    00365   protected:
    00366     RefSCDimension d;
    00367     Ref<SCMatrixKit> kit_;
    00368   public:
    00369     SymmSCMatrix(const RefSCDimension&, SCMatrixKit *);
    00370     ~SymmSCMatrix();
    00371
    00373     Ref<SCMatrixKit> kit() const { return kit_; }
    00374
    00376     virtual void save(StateOut&);
    00377     virtual void restore(StateIn&);
    00379     virtual double maxabs() const;
    00381     virtual void randomize();
    00383     void assign(double val) { assign_val(val); }
    00385     void assign(const double* m) { assign_p(m); }
    00387     void assign(const double** m) { assign_pp(m); }
    00390     void assign(SymmSCMatrix* m) { assign_s(m); }
    00392     virtual void assign_val(double val);
    00393     virtual void assign_p(const double* m);
    00394     virtual void assign_pp(const double** m);
    00395     virtual void assign_s(SymmSCMatrix* m);
    00397     virtual void convert(double*) const;
    00398     virtual void convert(double**) const;
    00401     virtual void convert(SymmSCMatrix*);
    00402     virtual void convert_accumulate(SymmSCMatrix*);
    00404     virtual void scale(double);
    00406     virtual void scale_diagonal(double);
    00408     virtual void shift_diagonal(double);
    00410     virtual void unit();
    00412     int n() const { return d->n(); }
    00414     virtual SymmSCMatrix* copy();
    00416     virtual SymmSCMatrix* clone();
    00417
    00418     // pure virtual functions
    00420     RefSCDimension dim() const { return d; }
    00422     virtual double get_element(int,int) const = 0;
    00423     virtual void set_element(int,int,double) = 0;
    00424     virtual void accumulate_element(int,int,double) = 0;
    00425
    00429     virtual SCMatrix * get_subblock(int br, int er, int bc, int ec) =0;
    00430     virtual SymmSCMatrix * get_subblock(int br, int er) =0;
    00431
    00433     virtual void assign_subblock(SCMatrix *m, int, int, int, int) =0;
    00434     virtual void assign_subblock(SymmSCMatrix *m, int, int) =0;
    00435
    00437     virtual void accumulate_subblock(SCMatrix *m, int, int, int, int) =0;
    00438     virtual void accumulate_subblock(SymmSCMatrix *m, int, int) =0;
    00439
    00441     virtual SCVector * get_row(int i) =0;
    00442
    00444     virtual void assign_row(SCVector *v, int i) =0;
    00445
    00447     virtual void accumulate_row(SCVector *v, int i) =0;
    00448
    00451     virtual void diagonalize(DiagSCMatrix*d,SCMatrix*m) = 0;
    00453     virtual void accumulate(const SymmSCMatrix* m) = 0;
    00455     virtual void accumulate_symmetric_sum(SCMatrix*) = 0;
    00456     virtual void accumulate_symmetric_product(SCMatrix*);
    00457     virtual void accumulate_transform(SCMatrix*,SymmSCMatrix*,
    00458                             SCMatrix::Transform = SCMatrix::NormalTransform);
    00459     virtual void accumulate_transform(SCMatrix*,DiagSCMatrix*,
    00460                             SCMatrix::Transform = SCMatrix::NormalTransform);
    00461     virtual void accumulate_transform(SymmSCMatrix*,SymmSCMatrix*);
    00462     virtual void accumulate_symmetric_outer_product(SCVector*);
    00465     virtual double scalar_product(SCVector* v);
    00467     virtual double trace() = 0;
    00469     virtual double invert_this() = 0;
    00471     virtual double determ_this() = 0;
    00472
    00473     virtual double solve_this(SCVector*) = 0;
    00474     virtual void gen_invert_this() = 0;
    00475
    00477     virtual void element_op(const Ref<SCElementOp>&) = 0;
    00478     virtual void element_op(const Ref<SCElementOp2>&,
    00479                             SymmSCMatrix*) = 0;
    00480     virtual void element_op(const Ref<SCElementOp3>&,
    00481                             SymmSCMatrix*,SymmSCMatrix*) = 0;
    00483     void print(std::ostream&o=ExEnv::out0()) const;
    00484     void print(const char* title=0,std::ostream& out=ExEnv::out0(),
    00485                int =10) const;
    00486     virtual void vprint(const char* title=0,
    00487                         std::ostream& out=ExEnv::out0(), int =10) const;
    00488
    00490     Ref<MessageGrp> messagegrp() const;
    00491
    00494     virtual Ref<SCMatrixSubblockIter> local_blocks(
    00495         SCMatrixSubblockIter::Access) = 0;
    00497     virtual Ref<SCMatrixSubblockIter> all_blocks(
    00498         SCMatrixSubblockIter::Access) = 0;
    00499 };
    00500
    00503 class DiagSCMatrix: public DescribedClass {
    00504   protected:
    00505     RefSCDimension d;
    00506     Ref<SCMatrixKit> kit_;
    00507   public:
    00508     DiagSCMatrix(const RefSCDimension&, SCMatrixKit *);
    00509     ~DiagSCMatrix();
    00510
    00512     Ref<SCMatrixKit> kit() const { return kit_; }
    00513
    00515     virtual void save(StateOut&);
    00516     virtual void restore(StateIn&);
    00517
    00519     virtual double maxabs() const;
    00521     virtual void randomize();
    00523     void assign(double val) { assign_val(val); }
    00525     void assign(const double*p) { assign_p(p); }
    00528     void assign(DiagSCMatrix*d_a) { assign_d(d_a); }
    00530     virtual void assign_val(double val);
    00531     virtual void assign_p(const double*);
    00532     virtual void assign_d(DiagSCMatrix*);
    00534     virtual void convert(double*) const;
    00537     virtual void convert(DiagSCMatrix*);
    00538     virtual void convert_accumulate(DiagSCMatrix*);
    00540     virtual void scale(double);
    00542     int n() const { return d->n(); }
    00544     virtual DiagSCMatrix* copy();
    00546     virtual DiagSCMatrix* clone();
    00547
    00548     // pure virtual functions
    00550     RefSCDimension dim() const { return d; }
    00552     virtual double get_element(int) const = 0;
    00553     virtual void set_element(int,double) = 0;
    00554     virtual void accumulate_element(int,double) = 0;
    00556     virtual void accumulate(const DiagSCMatrix* m) = 0;
    00558     virtual double trace() = 0;
    00560     virtual double determ_this() = 0;
    00562     virtual double invert_this() = 0;
    00564     virtual void gen_invert_this() = 0;
    00566     virtual void element_op(const Ref<SCElementOp>&) = 0;
    00567     virtual void element_op(const Ref<SCElementOp2>&,
    00568                             DiagSCMatrix*) = 0;
    00569     virtual void element_op(const Ref<SCElementOp3>&,
    00570                             DiagSCMatrix*,DiagSCMatrix*) = 0;
    00572     void print(std::ostream&o=ExEnv::out0()) const;
    00573     void print(const char* title=0,
    00574                std::ostream& out=ExEnv::out0(), int =10) const;
    00575     virtual void vprint(const char* title=0,
    00576                         std::ostream& out=ExEnv::out0(), int =10) const;
    00577
    00579     Ref<MessageGrp> messagegrp() const;
    00580
    00583     virtual Ref<SCMatrixSubblockIter> local_blocks(
    00584         SCMatrixSubblockIter::Access) = 0;
    00586     virtual Ref<SCMatrixSubblockIter> all_blocks(
    00587         SCMatrixSubblockIter::Access) = 0;
    00588 };
    00589
    00590 }
    00591
    00592 #endif
    00593
    00594 // Local Variables:
    00595 // mode: c++
    00596 // c-file-style: "CLJ"
    00597 // End: