00001
00002
00003 #ifndef CH_MATRIX_CLASSES__MATRIX_HXX
00004 #define CH_MATRIX_CLASSES__MATRIX_HXX
00005
00014 #include <math.h>
00015 #ifndef CH_MATRIX_CLASSES__INDEXMAT_HXX
00016 #include "indexmat.hxx"
00017 #endif
00018
00019
00020 namespace CH_Matrix_Classes {
00021
00022
00023
00024
00025
00026
00027
00028
00029
00033
00035 class Realrange
00036 {
00037 public:
00039 Real from;
00041 Real to;
00043 Real step;
00045 Real tol;
00047 Realrange(Real _from,Real _to,Real _step=1,Real _tol=1e-8):
00048 from(_from),to(_to),step(_step),tol(_tol){}
00050 ~Realrange(){}
00051 };
00052
00054
00055
00056
00057
00058
00062
00075 class Matrix: protected Memarrayuser
00076 {
00077 friend class Indexmatrix;
00078 friend class Symmatrix;
00079 friend class Sparsemat;
00080 friend class Sparsesym;
00081
00082 private:
00083
00084 static const Mtype mtype;
00085 Integer mem_dim;
00086 Integer nr,
00087 nc;
00088 Real *m;
00089
00090 bool is_init;
00091
00093 inline void init_to_zero();
00094
00095 public:
00096
00097
00098
00099
00100
00101
00105
00107 inline Matrix();
00109 inline Matrix(const Matrix&,Real d=1.,int atrans=0);
00111 inline Matrix(const Realrange&);
00118 inline Matrix(Integer nr,Integer nc);
00120 inline Matrix(Integer nr,Integer nc,Real d);
00122 inline Matrix(Integer nr,Integer nc,const Real* dp,Integer incr=1);
00124 inline Matrix(const std::vector<Real>& vec);
00126 ~Matrix(){memarray->free(m);}
00127
00128 #if (CONICBUNDLE_DEBUG>=1)
00130 void set_init(bool i){is_init=i;}
00132 bool get_init() const {return is_init;}
00133 #else
00135 void set_init(bool ){}
00137 bool get_init() const {return true;}
00138 #endif
00139
00141 inline Matrix& init(const Matrix &A,Real d=1.,int atrans=0);
00143 inline Matrix& init(const Indexmatrix& A,Real d=1.);
00145 inline Matrix& init(const Sparsemat& A, Real d=1.);
00147 inline Matrix& init(const Symmatrix& S,Real d=1.);
00149 inline Matrix& init(const Sparsesym&,Real d=1.);
00151 Matrix& init(const Realrange&);
00153 inline Matrix& init(Integer nr,Integer nc,Real d);
00155 inline Matrix& init(Integer nr,Integer nc,const Real *dp,Integer incr=1);
00157 inline Matrix& init(const std::vector<Real>& vec);
00158
00165 void newsize(Integer nr,Integer nc);
00166
00167
00169
00173
00175 inline Matrix(const Indexmatrix& A,Real d=1.);
00177 inline Matrix(const Sparsemat& A, Real d=1.);
00179 inline Matrix(const Symmatrix& S,Real d=1.);
00181 inline Matrix(const Sparsesym&,Real d=1.);
00182
00184
00185
00186
00187
00188
00192
00194 void dim(Integer& _nr, Integer& _nc) const {_nr=nr; _nc=nc;}
00195
00197 Integer dim() const {return nr*nc;}
00198
00200 Integer rowdim() const {return nr;}
00201
00203 Integer coldim() const {return nc;}
00204
00206 Mtype get_mtype() const {return mtype;}
00207
00209
00210
00211
00212
00213
00214
00215
00219
00221 inline Real& operator()(Integer i,Integer j);
00222
00224 inline Real& operator()(Integer i);
00225
00227 inline Real operator()(Integer i,Integer j) const;
00228
00230 inline Real operator()(Integer i) const;
00231
00233 Matrix operator()(const Indexmatrix& vecrow,const Indexmatrix& veccol) const;
00234
00236 Matrix operator()(const Indexmatrix& A) const;
00237
00238
00240 inline Real& operator[](Integer i);
00242 inline Real operator[](Integer i) const;
00243
00245 Matrix col(Integer i) const;
00247 Matrix row(Integer i) const;
00249 Matrix cols(const Indexmatrix &vec) const;
00251 Matrix rows(const Indexmatrix &vec) const;
00252
00254 Matrix& triu(Integer d=0);
00256 Matrix& tril(Integer d=0);
00257
00259 Matrix& subassign(const Indexmatrix& vecrow,const Indexmatrix& veccol,
00260 const Matrix& A);
00261
00263 Matrix& subassign(const Indexmatrix& vec,const Matrix& A);
00264
00265
00266
00268 Matrix& delete_rows(const Indexmatrix& ind);
00270 Matrix& delete_cols(const Indexmatrix& ind);
00271
00273 Matrix& insert_row(Integer i,const Matrix& v);
00275 Matrix& insert_col(Integer i,const Matrix& v);
00276
00278 inline Matrix& reduce_length(Integer n);
00279
00280
00282 Matrix& concat_right(const Matrix& A);
00284 Matrix& concat_below(const Matrix& A);
00286 Matrix& concat_right(Real d);
00288 Matrix& concat_below(Real d);
00289
00291 Real* get_store() {return m;}
00293 const Real* get_store() const {return m;}
00294
00295
00297
00298
00302
00304 friend Matrix diag(const Matrix& A);
00305
00307 friend inline Matrix triu(const Matrix& A,Integer i=0);
00309 friend inline Matrix tril(const Matrix& A,Integer i=0);
00310
00311
00313 friend inline Matrix concat_right(const Matrix& A,const Matrix& B);
00315 friend inline Matrix concat_below(const Matrix& A,const Matrix& B);
00316
00318 friend inline void swap(Matrix &A,Matrix &B);
00319
00321
00322
00323
00324
00325
00326
00330
00332 Matrix& xeya(const Matrix& A,Real d=1.,int atrans=0);
00334 Matrix& xpeya(const Matrix& A,Real d=1.);
00336 Matrix& xeya(const Indexmatrix& A,Real d=1.);
00338 Matrix& xpeya(const Indexmatrix& A,Real d=1.);
00339
00341
00345
00347 friend Matrix& xbpeya(Matrix& x,const Matrix& y,Real alpha=1.,Real beta=0.,int ytrans=0);
00348
00350 friend Matrix& xeyapzb(Matrix& x,const Matrix& y,const Matrix& z,Real alpha=1.,Real beta=1.);
00351
00353 friend Matrix& genmult(const Matrix& A,const Matrix& B,Matrix& C,
00354 Real alpha=1.,Real beta=0.,int atrans=0,int btrans=0);
00356
00357
00358
00359
00360
00361
00365
00367 inline Matrix& operator=(const Matrix &A);
00369 inline Matrix& operator*=(const Matrix &s);
00371 inline Matrix& operator+=(const Matrix &v);
00373 inline Matrix& operator-=(const Matrix &v);
00375 inline Matrix& operator%=(const Matrix &A);
00377 inline Matrix& operator/=(const Matrix &A);
00379 inline Matrix operator-() const;
00380
00382 inline Matrix& operator*=(Real d);
00384 inline Matrix& operator/=(Real d);
00386 inline Matrix& operator+=(Real d);
00388 inline Matrix& operator-=(Real d);
00389
00391 Matrix& transpose();
00392
00394
00398
00400 friend inline Matrix operator*(const Matrix &A,const Matrix& B);
00402 friend inline Matrix operator+(const Matrix &A,const Matrix& B);
00404 friend inline Matrix operator-(const Matrix &A,const Matrix& B);
00406 friend inline Matrix operator%(const Matrix &A,const Matrix& B);
00408 friend inline Matrix operator/(const Matrix &A,const Matrix& B);
00410 friend inline Matrix operator*(const Matrix &A,Real d);
00412 friend inline Matrix operator*(Real d,const Matrix &A);
00414 friend inline Matrix operator/(const Matrix& A,Real d);
00416 friend inline Matrix operator+(const Matrix& A,Real d);
00418 friend inline Matrix operator+(Real d,const Matrix& A);
00420 friend inline Matrix operator-(const Matrix& A,Real d);
00422 friend inline Matrix operator-(Real d,const Matrix& A);
00423
00425 friend Matrix transpose(const Matrix& A);
00426
00428
00429
00430
00431
00432
00436
00437
00439 Matrix& xeya(const Symmatrix& A,Real d=1.);
00441 Matrix& xpeya(const Symmatrix& A,Real d=1.);
00443 inline Matrix& operator=(const Symmatrix& S);
00445 inline Matrix& operator*=(const Symmatrix& S);
00447 inline Matrix& operator+=(const Symmatrix& S);
00449 inline Matrix& operator-=(const Symmatrix& S);
00450
00452 Matrix& xeya(const Sparsesym& A,Real d=1.);
00454 Matrix& xpeya(const Sparsesym& A,Real d=1.);
00456 inline Matrix& operator=(const Sparsesym &);
00458 inline Matrix& operator*=(const Sparsesym& S);
00460 inline Matrix& operator+=(const Sparsesym& S);
00462 inline Matrix& operator-=(const Sparsesym& S);
00463
00465 Matrix& xeya(const Sparsemat& A,Real d=1.);
00467 Matrix& xpeya(const Sparsemat& A,Real d=1.);
00469 inline Matrix& operator=(const Sparsemat & A);
00471 inline Matrix& operator*=(const Sparsemat &A);
00473 inline Matrix& operator+=(const Sparsemat &A);
00475 inline Matrix& operator-=(const Sparsemat &A);
00476
00478
00482
00484 friend std::vector<double>& assign(std::vector<double>& vec,const Matrix& A);
00485
00486
00488 friend Matrix& genmult(const Symmatrix& A,const Matrix& B,Matrix& C,
00489 Real alpha,Real beta,int btrans);
00491 friend Matrix& genmult(const Matrix& A,const Symmatrix& B,Matrix& C,
00492 Real alpha,Real beta,int atrans);
00493
00495 friend Matrix& genmult(const Sparsesym& A,const Matrix& B,Matrix& C,
00496 Real alpha,Real beta,int btrans);
00498 friend Matrix& genmult(const Matrix& A,const Sparsesym& B,Matrix& C,
00499 Real alpha,Real beta,int atrans);
00500
00502 friend Matrix& genmult(const Sparsemat& A,const Matrix& B,Matrix &C,
00503 Real alpha,Real beta, int atrans,int btrans);
00505 friend Matrix& genmult(const Matrix& A,const Sparsemat& B,Matrix &C,
00506 Real alpha,Real beta, int atrans,int btrans);
00507
00509
00510
00511
00512
00513
00517
00519 Matrix& rand(Integer nr,Integer nc,CH_Tools::GB_rand* random_generator=0);
00521 Matrix& shuffle(CH_Tools::GB_rand* random_generator=0);
00523 Matrix& inv(void);
00525 Matrix& sqrt(void);
00527 Matrix& sign(Real tol=1e-12);
00529 Matrix& floor(void);
00531 Matrix& ceil(void);
00533 Matrix& rint(void);
00535 Matrix& round(void);
00537 Matrix& abs(void);
00538
00540
00544
00546 friend inline Matrix rand(Integer nr,Integer nc,CH_Tools::GB_rand* random_generator=0);
00548 friend inline Matrix inv(const Matrix& A);
00550 friend inline Matrix sqrt(const Matrix& A);
00552 friend inline Matrix sign(const Matrix& A,Real tol=1e-12);
00554 friend inline Matrix floor(const Matrix& A);
00556 friend inline Matrix ceil(const Matrix& A);
00558 friend inline Matrix rint(const Matrix& A);
00560 friend inline Matrix round(const Matrix& A);
00562 friend Matrix abs(const Matrix& A);
00563
00565
00566
00567
00568
00569
00573
00575 Matrix& scale_rows(const Matrix& vec);
00577 Matrix& scale_cols(const Matrix& vec);
00578
00579
00581 int triu_solve(Matrix& rhs,Real tol=1e-10);
00583 int tril_solve(Matrix& rhs,Real tol=1e-10);
00584
00585
00586
00588 int QR_factor(Real tol=1e-10);
00590 int QR_factor(Matrix& Q,Real tol=1e-10);
00592 inline int QR_factor(Matrix& Q,Matrix& R,Real tol=1e-10) const;
00593
00595 int QR_factor(Indexmatrix& piv,Real tol=1e-10);
00597 int QR_factor(Matrix& Q,Indexmatrix& piv,Real tol=1e-10);
00599 inline int QR_factor(Matrix& Q,Matrix& R,Indexmatrix& piv,Real tol=1e-10) const;
00601 int Qt_times(Matrix& A,Integer r) const;
00603 int Q_times(Matrix& A,Integer r) const;
00605 int times_Q(Matrix& A,Integer r) const;
00611 int QR_solve(Matrix& rhs,Real tol=1e-10);
00612
00627 int QR_concat_right(const Matrix& A,Indexmatrix& piv,Integer r,Real tol=1e-10);
00628
00629
00630
00632 inline int ls(Matrix & rhs, Real tol=1e-10);
00633
00645 int nnls(Matrix& rhs,Matrix* dual=0,Real tol=1e-10) const;
00646
00648
00652
00654 friend Real trace(const Matrix& A);
00656 friend inline Real ip(const Matrix& A, const Matrix& B);
00658 friend inline Real colip(const Matrix& A,Integer j);
00660 friend inline Real rowip(const Matrix& A,Integer i);
00662 friend inline Matrix colsip(const Matrix& A);
00664 friend inline Matrix rowsip(const Matrix& A);
00666 friend inline Real norm2(const Matrix& A);
00668 friend Real normDsquared(const Matrix& A,const Matrix& d,int atrans=0,int dinv=0);
00669
00671 friend Matrix sumrows(const Matrix& A);
00673 friend Matrix sumcols(const Matrix& A);
00675 friend Real sum(const Matrix& A);
00676
00677
00678
00680 friend Matrix house(const Matrix &A,Integer i=0,Integer j=0,Real tol=1e-10);
00682 friend int rowhouse(Matrix &A,const Matrix& v,Integer i=0,Integer j=0);
00684 friend int colhouse(Matrix &A,const Matrix& v,Integer i=0,Integer j=0);
00685
00687 friend inline int QR_factor(const Matrix& A,Matrix& Q,Matrix &R,Real tol=1e-10);
00689 friend inline int QR_factor(const Matrix& A,Matrix& Q,Matrix &R,Indexmatrix& piv,Real tol=1e-10);
00690
00692
00693
00694
00695
00696
00700
00702 Indexmatrix find(Real tol=1e-10) const;
00703 Indexmatrix find_number(Real num=0.,Real tol=1e-10) const;
00704
00706
00710
00712 friend Matrix operator<(const Matrix &A,const Matrix &B);
00714 friend inline Matrix operator>(const Matrix &A,const Matrix &B);
00716 friend Matrix operator<=(const Matrix &A,const Matrix &B);
00718 friend inline Matrix operator>=(const Matrix &A,const Matrix &B);
00720 friend Matrix operator==(const Matrix &A,const Matrix &B);
00722 friend Matrix operator!=(const Matrix &A,const Matrix &B);
00724 friend Matrix operator<(const Matrix &A,Real d);
00726 friend Matrix operator>(const Matrix &A,Real d);
00728 friend Matrix operator<=(const Matrix &A,Real d);
00730 friend Matrix operator>=(const Matrix &A,Real d);
00732 friend Matrix operator==(const Matrix &A,Real d);
00734 friend Matrix operator!=(const Matrix &A,Real d);
00736 friend inline Matrix operator<(Real d,const Matrix &A);
00738 friend inline Matrix operator>(Real d,const Matrix &A);
00740 friend inline Matrix operator<=(Real d,const Matrix &A);
00742 friend inline Matrix operator>=(Real d,const Matrix &A);
00744 friend inline Matrix operator==(Real d,const Matrix &A);
00746 friend inline Matrix operator!=(Real d,const Matrix &A);
00747
00749 friend Matrix minrows(const Matrix& A);
00751 friend Matrix mincols(const Matrix& A);
00753 friend Real min(const Matrix& A,Integer *iindex=0,Integer *jindex=0);
00755 friend Matrix maxrows(const Matrix& A);
00757 friend Matrix maxcols(const Matrix& A);
00759 friend Real max(const Matrix& A,Integer *iindex=0,Integer *jindex=0);
00760
00762 friend inline Indexmatrix sortindex(const Matrix& vec);
00764 friend void sortindex(const Matrix& vec,Indexmatrix &ind);
00765
00767 friend inline Indexmatrix find(const Matrix& A,Real tol=1e-10);
00769 friend inline Indexmatrix find_number(const Matrix& A,Real num=0.,Real tol=1e-10);
00770
00772
00773
00774
00775
00776
00780
00783 void display(std::ostream& out,
00784 int precision=0,
00785 int width=0,
00786 int screenwidth=0
00787 ) const;
00788
00790
00794
00796 friend std::ostream& operator<<(std::ostream& o,const Matrix &v);
00798 friend std::istream& operator>>(std::istream& i,Matrix &v);
00799
00801
00802 };
00803
00805
00806
00807
00808
00809
00810 Matrix diag(const Matrix& A);
00811 Matrix& xbpeya(Matrix& x,const Matrix& y,Real alpha,Real beta,int ytrans);
00812 Matrix& xeyapzb(Matrix& x,const Matrix& y,const Matrix& z,Real alpha,Real beta);
00813 Matrix& genmult(const Matrix& A,const Matrix& B,Matrix& C,
00814 Real alpha,Real beta,int atrans,int btrans);
00815 Matrix transpose(const Matrix& A);
00816 std::vector<double>& assign(std::vector<double>& vec,const Matrix& A);
00817 Matrix& genmult(const Symmatrix& A,const Matrix& B,Matrix& C,
00818 Real alpha,Real beta,int btrans);
00819 Matrix& genmult(const Matrix& A,const Symmatrix& B,Matrix& C,
00820 Real alpha,Real beta,int atrans);
00821 Matrix& genmult(const Sparsesym& A,const Matrix& B,Matrix& C,
00822 Real alpha,Real beta,int btrans);
00823 Matrix& genmult(const Matrix& A,const Sparsesym& B,Matrix& C,
00824 Real alpha,Real beta,int atrans);
00825 Matrix& genmult(const Sparsemat& A,const Matrix& B,Matrix &C,
00826 Real alpha,Real beta, int atrans,int btrans);
00827 Matrix& genmult(const Matrix& A,const Sparsemat& B,Matrix &C,
00828 Real alpha,Real beta, int atrans,int btrans);
00829 Matrix abs(const Matrix& A);
00830 Real trace(const Matrix& A);
00831 Matrix sumrows(const Matrix& A);
00832 Matrix sumcols(const Matrix& A);
00833 Real sum(const Matrix& A);
00834 Matrix house(const Matrix &x,Integer i,Integer j);
00835 int rowhouse(Matrix &A,const Matrix& v,Integer i,Integer j);
00836 int colhouse(Matrix &A,const Matrix& v,Integer i,Integer j);
00837 Matrix operator<(const Matrix &A,const Matrix &B);
00838 Matrix operator<=(const Matrix &A,const Matrix &B);
00839 Matrix operator==(const Matrix &A,const Matrix &B);
00840 Matrix operator!=(const Matrix &A,const Matrix &B);
00841 Matrix operator<(const Matrix &A,Real d);
00842 Matrix operator>(const Matrix &A,Real d);
00843 Matrix operator<=(const Matrix &A,Real d);
00844 Matrix operator>=(const Matrix &A,Real d);
00845 Matrix operator==(const Matrix &A,Real d);
00846 Matrix operator!=(const Matrix &A,Real d);
00847 Matrix minrows(const Matrix& A);
00848 Matrix mincols(const Matrix& A);
00849 Real min(const Matrix& A,Integer *iindex,Integer *jindex);
00850 Matrix maxrows(const Matrix& A);
00851 Matrix maxcols(const Matrix& A);
00852 Real max(const Matrix& A,Integer *iindex,Integer *jindex);
00853 Indexmatrix sortindex(const Matrix& vec);
00854 void sortindex(const Matrix& vec,Indexmatrix &ind);
00855 std::ostream& operator<<(std::ostream& o,const Matrix &v);
00856 std::istream& operator>>(std::istream& i,Matrix &v);
00857
00858
00859
00860
00861
00862 inline void Matrix::init_to_zero()
00863 {
00864 nr=nc=0;mem_dim=0;m=0;
00865 chk_set_init(*this,1);
00866 }
00867
00868 inline Matrix& Matrix::init(Integer inr,Integer inc,Real d)
00869 {
00870 newsize(inr,inc);
00871 mat_xea(nr*nc,m,d);
00872 chk_set_init(*this,1);
00873 return *this;
00874 }
00875
00876 inline Matrix& Matrix::init(Integer inr,Integer inc,const Real* d,Integer incr)
00877 {
00878 newsize(inr,inc);
00879 if (incr==1) mat_xey(nr*nc,m,d);
00880 else mat_xey(nr*nc,m,Integer(1),d,incr);
00881 chk_set_init(*this,1);
00882 return *this;
00883 }
00884
00885 inline Matrix& Matrix::init(const Matrix& A,Real d,int atrans)
00886 {
00887 return xeya(A,d,atrans);
00888 }
00889
00890 inline Matrix& Matrix::init(const Indexmatrix& A,Real d)
00891 {
00892 return xeya(A,d);
00893 }
00894
00895 inline Matrix& Matrix::init(const std::vector<Real>& vec)
00896 {
00897 newsize(Integer(vec.size()),1); chk_set_init(*this,1);
00898 for(Integer i=0;i<nr;i++) m[i]=vec[i];
00899 return *this;
00900 }
00901
00902 inline Matrix::Matrix():Memarrayuser()
00903 {
00904 init_to_zero();
00905 }
00906
00907 inline Matrix::Matrix(const Matrix &A,Real d,int atrans):Memarrayuser()
00908 {
00909 init_to_zero();
00910 xeya(A,d,atrans);
00911 }
00912
00913 inline Matrix::Matrix(const Indexmatrix &A,Real d):Memarrayuser()
00914 {
00915 init_to_zero();
00916 xeya(A,d);
00917 }
00918
00919 inline Matrix::Matrix(Integer inr,Integer inc):Memarrayuser()
00920 {
00921 init_to_zero();
00922 newsize(inr,inc);
00923 }
00924
00925 inline Matrix::Matrix(const Realrange& range):Memarrayuser()
00926 {
00927 init_to_zero();
00928 init(range);
00929 }
00930
00931 inline Matrix::Matrix(Integer inr,Integer inc,Real d):Memarrayuser()
00932 {
00933 init_to_zero();
00934 init(inr,inc,d);
00935 }
00936
00937 inline Matrix::Matrix(Integer inr,Integer inc,const Real *d,Integer incr):Memarrayuser()
00938 {
00939 init_to_zero();
00940 init(inr,inc,d,incr);
00941 }
00942
00943 inline Matrix::Matrix(const std::vector<Real>& vec):Memarrayuser()
00944 {
00945 init_to_zero();
00946 init(vec);
00947 }
00948
00949 inline Real& Matrix::operator()(Integer i,Integer j)
00950 {
00951 chk_range(i,j,nr,nc);
00952 return m[j*nr+i];
00953 }
00954
00955 inline Real& Matrix::operator()(Integer i)
00956 {
00957 chk_range(i,0,nr*nc,1);
00958 return m[i];
00959 }
00960
00961 inline Real Matrix::operator()(Integer i,Integer j) const
00962 {
00963 chk_range(i,j,nr,nc);
00964 return m[j*nr+i];
00965 }
00966
00967 inline Real Matrix::operator()(Integer i) const
00968 {
00969 chk_range(i,0,nr*nc,1);
00970 return m[i];
00971 }
00972
00973 inline Real& Matrix::operator[](Integer i)
00974 {return (*this)(i);}
00975
00976 inline Real Matrix::operator[](Integer i) const
00977 {return (*this)(i);}
00978
00979 inline Matrix& Matrix::reduce_length(Integer n)
00980 { nr=min(nr*nc,max(Integer(0),n)); nc=1; return *this;}
00981
00982
00983 inline Real ip(const Matrix& A, const Matrix& B)
00984 {
00985 chk_add(A,B);
00986 return mat_ip(A.nc*A.nr,A.m,B.m);
00987 }
00988
00989 inline Real colip(const Matrix& A,Integer j)
00990 {
00991 chk_init(A);
00992 chk_range(j,j,A.nc,A.nc);
00993 return mat_ip(A.nr,A.m+j*A.nr);
00994 }
00995
00996 inline Real rowip(const Matrix& A,Integer i)
00997 {
00998 chk_init(A);
00999 chk_range(i,i,A.nr,A.nr);
01000 return mat_ip(A.nc,A.m+i,A.nr);
01001 }
01002
01003 inline Matrix colsip(const Matrix& A)
01004 {
01005 chk_init(A);
01006 Matrix tmp(1,A.nc); chk_set_init(tmp,1);
01007 for (Integer j=0;j<A.nc;j++)
01008 tmp[j]=colip(A,j);
01009 return tmp;
01010 }
01011
01012 inline Matrix rowsip(const Matrix& A)
01013 {
01014 chk_init(A);
01015 Matrix tmp(A.nr,1,0.);
01016 Real* mp =A.m;
01017 for (Integer j=0;j<A.nc;j++){
01018 Real *tp=tmp.m;
01019 for (Integer i=0;i<A.nr;i++,mp++)
01020 (*tp++)+=(*mp)*(*mp);
01021 }
01022 return tmp;
01023 }
01024
01025 inline Real norm2(const Matrix& A)
01026 {
01027 chk_init(A);
01028 return ::sqrt(mat_ip(A.nc*A.nr,A.m));
01029 }
01030
01031 inline Matrix& Matrix::operator=(const Matrix &A)
01032 { return xeya(A);}
01033
01034 inline Matrix& Matrix::operator*=(const Matrix &A)
01035 { Matrix C; return xeya(genmult(*this,A,C));}
01036
01037 inline Matrix& Matrix::operator+=(const Matrix &A)
01038 { return xpeya(A); }
01039
01040 inline Matrix& Matrix::operator-=(const Matrix &A)
01041 { return xpeya(A,-1.); }
01042
01043 Matrix& Matrix::operator%=(const Matrix &A)
01044 { chk_add(*this,A); mat_xhadey(nr*nc,m,A.m); return *this; }
01045
01046 Matrix& Matrix::operator/=(const Matrix &A)
01047 { chk_add(*this,A); mat_xinvhadey(nr*nc,m,A.m); return *this; }
01048
01049 inline Matrix Matrix::operator-() const
01050 { return Matrix(*this,-1.); }
01051
01052 inline Matrix& Matrix::operator*=(register Real d)
01053 { chk_init(*this); mat_xmultea(nr*nc,m,d); return *this; }
01054
01055 inline Matrix& Matrix::operator/=(register Real d)
01056 { chk_init(*this); mat_xmultea(nr*nc,m,1./d); return *this; }
01057
01058 inline Matrix& Matrix::operator+=(register Real d)
01059 { chk_init(*this); mat_xpea(nr*nc,m,d); return *this; }
01060
01061 inline Matrix& Matrix::operator-=(register Real d)
01062 { chk_init(*this); mat_xpea(nr*nc,m,-d); return *this; }
01063
01064
01065 inline Matrix operator*(const Matrix &A,const Matrix &B)
01066 {Matrix C; return genmult(A,B,C);}
01067 inline Matrix operator+(const Matrix &A,const Matrix &B)
01068 {Matrix C; return xeyapzb(C,A,B,1.,1.);}
01069 inline Matrix operator-(const Matrix &A,const Matrix &B)
01070 {Matrix C; return xeyapzb(C,A,B,1.,-1.);}
01071 inline Matrix operator%(const Matrix &A,const Matrix &B)
01072 {Matrix C(A); return C%=B;}
01073 inline Matrix operator/(const Matrix &A,const Matrix &B)
01074 {Matrix C(A); return C/=B;}
01075 inline Matrix operator*(const Matrix &A,Real d)
01076 {return Matrix(A,d);}
01077 inline Matrix operator*(Real d,const Matrix &A)
01078 {return Matrix(A,d);}
01079 inline Matrix operator/(const Matrix &A,Real d)
01080 {return Matrix(A,1./d);}
01081 inline Matrix operator+(const Matrix &A,Real d)
01082 {Matrix B(A); return B+=d;}
01083 inline Matrix operator+(Real d,const Matrix &A)
01084 {Matrix B(A); return B+=d;}
01085 inline Matrix operator-(const Matrix &A,Real d)
01086 {Matrix B(A); return B-=d;}
01087 inline Matrix operator-(Real d,const Matrix &A)
01088 {Matrix B(A,-1.); return B+=d;}
01089
01090 inline int Matrix::QR_factor(Matrix& Q,Matrix& R,Real tol) const
01091 {R=*this;return R.QR_factor(Q,tol);}
01092 inline int Matrix::QR_factor(Matrix& Q,Matrix& R,Indexmatrix& piv,Real tol) const
01093 {R=*this;return R.QR_factor(Q,piv,tol);}
01094 inline int Matrix::ls(Matrix & rhs, Real tol)
01095 { return this->QR_solve(rhs,tol);}
01096
01097 inline int QR_factor(const Matrix& A,Matrix& Q,Matrix &R,Real tol)
01098 {return A.QR_factor(Q,R,tol);}
01099 inline int QR_factor(const Matrix& A,Matrix& Q,Matrix &R,Indexmatrix& piv,Real tol)
01100 {return A.QR_factor(Q,R,piv,tol);}
01101
01102 inline Matrix triu(const Matrix& A,Integer i)
01103 {Matrix B(A); B.triu(i); return B;}
01104 inline Matrix tril(const Matrix& A,Integer i)
01105 {Matrix B(A); B.tril(i); return B;}
01106
01107 inline Matrix concat_right(const Matrix& A,const Matrix& B)
01108 {Matrix C(A.dim()+B.dim(),1);C=A;C.concat_right(B);return C;}
01109 inline Matrix concat_below(const Matrix& A,const Matrix& B)
01110 {Matrix C(A.dim()+B.dim(),1);C=A;C.concat_below(B);return C;}
01111
01112 inline void swap(Matrix &A,Matrix &B)
01113 {
01114 Real *hm=A.m;A.m=B.m;B.m=hm;
01115 Integer hi=A.nr;A.nr=B.nr;B.nr=hi;
01116 hi=A.nc;A.nc=B.nc;B.nc=hi;
01117 hi=A.mem_dim;A.mem_dim=B.mem_dim;B.mem_dim=hi;
01118 #if (CONICBUNDLE_DEBUG>=1)
01119 hi=A.is_init;A.is_init=B.is_init;B.is_init=hi;
01120 #endif
01121 }
01122
01123 inline Matrix rand(Integer rows,Integer cols,CH_Tools::GB_rand* random_generator)
01124 {Matrix A; return A.rand(rows,cols,random_generator);}
01125 inline Matrix inv(const Matrix& A)
01126 {Matrix B(A); return B.inv();}
01127 inline Matrix sqrt(const Matrix& A)
01128 {Matrix B(A); return B.sqrt();}
01129 inline Matrix sign(const Matrix& A,Real tol)
01130 {Matrix B(A); return B.sign(tol);}
01131 inline Matrix floor(const Matrix& A)
01132 {Matrix B(A); return B.floor();}
01133 inline Matrix ceil(const Matrix& A)
01134 {Matrix B(A); return B.floor();}
01135 inline Matrix rint(const Matrix& A)
01136 {Matrix B(A); return B.floor();}
01137 inline Matrix round(const Matrix& A)
01138 {Matrix B(A); return B.floor();}
01139
01140 inline Matrix operator>(const Matrix &A,const Matrix &B)
01141 {return B<A;}
01142 inline Matrix operator>=(const Matrix &A,const Matrix &B)
01143 {return B<=A;}
01144 inline Matrix operator<(Real d,const Matrix &A)
01145 {return A>d;}
01146 inline Matrix operator>(Real d,const Matrix &A)
01147 {return A<d;}
01148 inline Matrix operator<=(Real d,const Matrix &A)
01149 {return A>=d;}
01150 inline Matrix operator>=(Real d,const Matrix &A)
01151 {return A<=d;}
01152 inline Matrix operator==(Real d,const Matrix &A)
01153 {return A==d;}
01154 inline Matrix operator!=(Real d,const Matrix &A)
01155 {return A!=d;}
01156
01157 inline Indexmatrix sortindex(const Matrix& vec)
01158 {Indexmatrix ind;sortindex(vec,ind);return ind;}
01159
01160 inline Indexmatrix find(const Matrix& A,Real tol)
01161 {return A.find(tol);}
01162 inline Indexmatrix find_number(const Matrix& A,Real num,Real tol)
01163 {return A.find_number(num,tol);}
01164
01165 inline std::vector<double>& assign(std::vector<double>& vec,const Matrix& A)
01166 {
01167 chk_init(A);
01168 vec.resize(A.dim());
01169 for(Integer i=0;i<A.dim();i++) vec[i]=A(i);
01170 return vec;
01171 }
01172
01173 }
01174
01175
01176
01177
01178
01179 #ifndef CH_MATRIX_CLASSES__SYMMAT_HXX
01180 #include "symmat.hxx"
01181 #endif
01182
01183 #endif