angchen

angchen

 

2009年4月14日

解线性方程组的头文件

 1#ifndef LINEARFUN_H
 2#define LINEARFUN_H
 3
 4#include<iostream>
 5#include<cstdlib>
 6#include"Matrix.h"
 7using namespace std;
 8
 9class LinearFun
10{
11public:
12    LinearFun();
13    LinearFun(Matrix ma, Matrix vb);
14    LinearFun(const LinearFun& lf);
15
16    ~LinearFun();
17    LinearFun& operator =(const LinearFun& lf);
18
19    Matrix Lx_b(Matrix& L, Matrix& b, const int flag);//if flag==1 for the LU Method,else for others. 
20    Matrix Ux_b(Matrix& U, Matrix& b);
21    //need to remember that J-I and G-S 's x0 will change as routine over.So they can not use  simultaneously.
22    Matrix JacobiIteration(Matrix& A1, Matrix& b, Matrix& x0, const double tol);                                                                    
23    Matrix GaussSeidelIteration(Matrix& A1, Matrix& b, Matrix& x0, const double tol);
24    Matrix SOR(Matrix& A1, Matrix& b, Matrix& x0, const double weight, const double tol);
25    Matrix CG(Matrix& A1, Matrix& b, Matrix& x0, const double tol);
26    Matrix QROrth(Matrix& A1, Matrix& b);//using the QR orthogonalization to solve the LS problem.
27
28private:
29    Matrix A;
30    Matrix B;
31}
;
32
33#endif

posted @ 2009-04-14 12:24 陈安 阅读(299) | 评论 (1)编辑 收藏

Matrix 头文件

#ifndef MATRIX_H
#define MATRIX_H

#include
<iostream>
#include
<cstdlib>

using namespace std;

class Matrix;
ostream
& operator <<(ostream& output, const Matrix& m);
istream
& operator >>(istream& input , Matrix& m);
const bool operator ==(const Matrix& M1, const Matrix& M2);

const Matrix operator +(const Matrix& M1, const Matrix& M2);
const Matrix operator -(const Matrix& M1, const Matrix& M2);
Matrix 
operator *(const Matrix& M1, const Matrix& M2);
Matrix 
operator *(const double k, const Matrix& M1);
const Matrix operator ~(const Matrix& M1);//the transpose of the matrix
Matrix operator /(const Matrix& M1, double k);

class Matrix
{
public:
    Matrix();
    Matrix(
int n);
    Matrix(
int row, int col);
    Matrix(
int row, int col, double *a);
    Matrix(
const Matrix& other);

    
~Matrix();

    
int GetRow() const;
    
int GetCol() const;
    
double GetData(int i, int j) const;

    Matrix
& operator =(const Matrix& other);
    friend ostream
& operator <<(ostream& output, const Matrix& m);
    friend istream
& operator >>(istream& input , Matrix& m);
    friend 
const bool operator ==(const Matrix& M1, const Matrix& M2);

    friend 
const Matrix operator +(const Matrix& M1, const Matrix& M2);
    friend 
const Matrix operator -(const Matrix& M1, const Matrix& M2);
    friend Matrix 
operator *(const Matrix& M1, const Matrix& M2);
    friend Matrix 
operator *(const double k, const Matrix& M2);
    friend 
const Matrix operator ~(const Matrix& M1);//
    friend Matrix operator /(const Matrix& M1, double k);//overload the / for matrix's divide.
    
//i.e.each component of matrix divide by k.


    
double& operator ()(int i, int j);//we set ()quote in ordor to use () as a left-hand value.
    
//return a vector which from M1(rcBegin:rcEnd, rc2) or M1(rc1, rcBegin:rcEnd)
    Matrix& GetColVectorOfMatrix(int c, int rBegin, const Matrix& M1, Matrix& v);//int rBegin, int rEnd, 
    Matrix& Get2DataOfCol(int c, int r1, int r2, const Matrix& M1, Matrix& v);
    
//get two entries of a certain column i.e. (r1,c),(r2,c).
    Matrix& GetSubMatrix(int r1, int r2, int c1, int c2, Matrix& M1, Matrix& m);
    Matrix
& SetSubMatrix(int r1, int r2, int c1, int c2, Matrix& M1, Matrix& m);

    Matrix
& SwapRow(int i, int j, Matrix& M1);//exchange the two rows which are select.
    Matrix& SwapCol(int i, int j, Matrix& M1);//exchange the two columns which are select.
    int* MaxRowCol(int RCn[2], const Matrix& M1, const int flag);//RCn for the Row and column number.
    
//flag=1 representive for do not consider the diagonal entries.

    
double Norm1(const Matrix& M1);//solve the norm1.
    double NormInfinite(const Matrix& M1);//solve the infinite-norm 
    double Norm2Frobenius(const Matrix& M1);//solve the Frobenius-norm for matrix and 2-norm 
                                                
//for vector.
    double ConditionNum1(const Matrix& M1);//solve the condition of matrix with norm1.
    double ConditionNumInf(const Matrix& M1);//solve the condition of matrix with infinite-norm.
    double DotVector(const Matrix& M1, const Matrix& M2);

    Matrix
& inverse(Matrix& M1);//by the Gauss-Jordon Emlimination Method with the all-big-povit.
    Matrix& LU(Matrix& M1);//by the Gauss Emlimination Method.
    Matrix HouseholderTranf(Matrix& x);//return v ,and the householder matrix equal to I-2*w*w' 
    
//which w=v/norm2(v).
    Matrix GivensTranf(Matrix& v);//return a vector which c=vr(0,0) and s=vr(1,0).
    Matrix QRHouseholder(Matrix& M1);
    Matrix
* QRHouseholderReturnQR(Matrix& M1, Matrix *qr);//return Q and R.

    
double PowerMethod(Matrix& M1, Matrix& x0, const double tol);//power method return a biggest eigenvalue
    double InversePower(Matrix& M1, Matrix& x0, const double tol);//InversePower method return a smallest eigenvalue
    Matrix JacobiSolInv(Matrix& M1, const double tol);//return all of the eigenvalue of matrix.
                                
//the matrix need to be square and symmetry.

private:
    
int row;
    
int col;
    
double *M;

}
;
#endif

posted @ 2009-04-14 12:19 陈安 阅读(729) | 评论 (0)编辑 收藏

仅列出标题  

导航

统计

常用链接

留言簿(1)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜