矩阵三角分解(LU+高斯+平方根+求解方法+求逆)

#include 
#include 
#include 
#include 
using namespace std;
typedef vector<vector<double>> Matrix;
void dispMatrix(Matrix m);       //输出矩阵值
void dispRes(vector<double> r);  //输出解向量
void normlize(Matrix &m, int i); //行列式第i行归一
Matrix eye(int n);               //返回一个n阶单位矩阵
vector<double> solve(Matrix a, Matrix L, Matrix U);
Matrix LU(Matrix &m, Matrix &U);       //分解n*n
vector<double> LUsolve(Matrix a);      //对n*n的矩阵分解并求出解
Matrix LU1(Matrix &m, Matrix &U);      //分解n*m
Matrix colesky(Matrix a);              //a是正定对称矩阵 n*n
vector<double> coleskySolve(Matrix a); //对与平方根法分解结果进行求解
Matrix OSR(Matrix a, Matrix &T);       //optimized square root改进平方根
vector<double> OSR_solve(Matrix a);
Matrix transpos(Matrix m); //求矩阵的转置
Matrix multi(Matrix a, Matrix b);
Matrix Inv(Matrix a); //对矩阵求逆
//TODO:改进平方根 求逆
int main()
{
    ios::sync_with_stdio("false");
    int n;
    cin >> n;
    Matrix M1(n, vector<double>(n, 0));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) //这里需要注意是输入n*n 还是n*n+1 根据需要改写
            cin >> M1[i][j];
    // dispMatrix(M1);
    Matrix m = Inv(M1);

    // dispMatrix(d);

    /* 
    case1:n*n
     dispRes(LUsolve(M1));
     case2:n*m
    Matrix U(n, vector(n + 1, 0));
    Matrix L = LU1(M1, U, n);

     */
    system("pause");
    return 0;
}

Matrix OSR(Matrix a, Matrix &T)
{
    int n = (int)a.size();
    Matrix L = eye(n);
    T[0][0] = a[0][0];
    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < i; j++)
        {
            T[i][j] = a[i][j];
            for (int k = 0; k <= j - 1; k++)
                T[i][j] -= T[i][k] * L[j][k];
            L[i][j] = T[i][j] / T[j][j];
        }
        T[i][i] = a[i][i];
        for (int k = 0; k <= i - 1; k++)
            T[i][i] -= T[i][k] * L[i][k];
    }
    return L;
}
vector<double> OSR_solve(Matrix a)
{
    int n = (int)a.size();
    Matrix T(n, vector<double>(n, 0));
    Matrix L = OSR(a, T);
    Matrix Lt = transpos(L);
    // dispMatrix(L);
    // dispMatrix(T);
    vector<double> res;
    for (int i = 0; i < n; i++)
        res.push_back(a[i][n]);
    // dispRes(res);
    for (int i = 0; i < n; i++)
        for (int k = 0; k <= i - 1; k++)
            res[i] -= L[i][k] * res[k];

    for (int i = 0; i < n; i++)
        res[i] /= T[i][i];
    // dispRes(res);

    for (int i = n - 1; i >= 0; i--)
        for (int k = i + 1; k < n; k++)
            res[i] -= Lt[i][k] * res[k];
    // dispRes(res);
    return res;
}
void dispMatrix(Matrix m)
{
    for (auto i : m)
    {
        for (auto ele : i)
            // cout << ele << " ";
            cout << setw(13) << ele;
        cout << endl;
    }
}
void dispRes(vector<double> r)
{
    cout << "res:\n";
    for (auto ele : r)
        cout << ele << " ";
    cout << endl;
}
void normlize(Matrix &m, int i)
{
    for (auto ele : m[i])
        ele /= m[i][i];
}
// 杜里尔特分解 对于n*n的矩阵分解 base**
Matrix LU(Matrix &m, Matrix &U)
{
    int n = (int)m.size();
    Matrix L = eye(n);
    //初始化u的第一行和L的第一列
    for (int i = 0; i < n; i++)
        U[0][i] = m[0][i];
    for (int i = 1; i < n; i++)
        L[i][0] = m[i][0] / U[0][0];
    for (int r = 1; r < n; r++)
    {
        for (int i = r; i < n; i++)
        {
            U[r][i] = m[r][i];
            for (int k = 0; k <= r - 1; k++)
                U[r][i] -= L[r][k] * U[k][i];
        }
        for (int i = r + 1; i < n; i++)
        {
            L[i][r] = m[i][r];
            for (int k = 0; k <= r - 1; k++)
            {
                L[i][r] -= L[i][k] * U[k][r];
            }
            L[i][r] /= U[r][r];
        }
    }
    return L;
}
// 杜里尔特分解 对于n*k的矩阵分解 base**
Matrix LU1(Matrix &m, Matrix &U)
{
    int n = (int)m.size();
    int k = (int)m[0].size();
    Matrix L = eye(n);
    //初始化u的第一行和L的第一列
    U[0] = m[0];
    for (int i = 1; i < n; i++)
        L[i][0] = m[i][0] / U[0][0];
    for (int r = 1; r < n; r++)
    {
        for (int i = r; i < k; i++)
        {
            U[r][i] = m[r][i];
            for (int k = 0; k <= r - 1; k++)
                U[r][i] -= L[r][k] * U[k][i];
        }
        for (int i = r + 1; i < n; i++)
        {
            L[i][r] = m[i][r];
            for (int k = 0; k <= r - 1; k++)
            {
                L[i][r] -= L[i][k] * U[k][r];
            }
            L[i][r] /= U[r][r];
        }
    }
    return L;
}
Matrix eye(int n)
{
    Matrix m(n, vector<double>(n, 0));
    for (int i = 0; i < n; i++)
        m[i][i] = 1;
    return m;
}
vector<double> LUsolve(Matrix a)
{
    int n = (int)a.size();
    Matrix U(n, vector<double>(n, 0));
    Matrix L = LU(a, U);
    return solve(a, L, U);
}
vector<double> solve(Matrix a, Matrix L, Matrix U)
{
    int n = (int)a.size();
    vector<double> res;
    for (int i = 0; i < n; i++)
        res.push_back(a[i][n]);
    // dispRes(res);
    for (int i = 0; i < n; i++)
    {
        for (int k = 0; k <= i - 1; k++)
            res[i] -= L[i][k] * res[k];
        res[i] /= L[i][i];
    }
    // dispRes(res);
    for (int i = n - 1; i >= 0; i--)
    {
        for (int k = i + 1; k < n; k++)
            res[i] -= U[i][k] * res[k];
        res[i] /= U[i][i];
    }
    // dispRes(res);
    return res;
}
Matrix colesky(Matrix a)
{
    int n = (int)a.size();
    Matrix L(n, vector<double>(n, 0));
    for (int i = 0; i < n; i++)
    {
        L[i][i] = a[i][i];
        for (int k = 0; k <= i - 1; k++)
            L[i][i] -= L[i][k] * L[i][k];
        L[i][i] = sqrt(L[i][i]);
        for (int j = i + 1; j < n; j++)
        {
            L[j][i] = a[j][i];
            for (int k = 0; k <= i - 1; k++)
                L[j][i] -= L[j][k] * L[i][k];
            L[j][i] /= L[i][i];
        }
    }
    return L;
}
vector<double> coleskySolve(Matrix a)
{
    Matrix L = colesky(a);
    Matrix Lt = transpos(L);
    return solve(a, L, Lt);
}
Matrix transpos(Matrix a)
{
    int n = (int)a.size(), m = (int)a[0].size();
    Matrix t(m, vector<double>(n, 0));
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            t[i][j] = a[j][i];
    return t;
}
Matrix Inv(Matrix a) //对矩阵求逆
{
    int n = (int)a.size();
    Matrix ma = a;
    // 在ma后面加上E
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            if (j != i)
                ma[i].push_back(0);
            else
                ma[i].push_back(1);
        }
    Matrix U(n, vector<double>(2 * n, 0));
    Matrix L = LU1(ma, U);
    cout << endl;
    dispMatrix(ma);
    cout << endl;
    dispMatrix(L);
    dispMatrix(U);
    Matrix inv(n, vector<double>(n, 0));
    vector<double> res(n, 0);
    for (int j = 0; j < n; j++)
    {
        for (int i = 0; i < n; i++)
            res[i] = U[i][j + n];
        for (int i = n - 1; i >= 0; i--)
        {
            for (int k = i + 1; k < n; k++)
                res[i] -= U[i][k] * res[k];
            res[i] /= U[i][i];
        }
        for (int i = 0; i < n; i++)
            inv[i][j] = res[i];
    }
    dispMatrix(inv);
    return inv;
}
/* 
示例
TODO:
4
6 2 1 -1
2 4 1 0
1 1 4 -1
-1 0 -1 3

3 
3 3 5
3 5 9
5 9 17
3 
3 3 5 0
3 5 9 -2
5 9 17 -4
4
2 2 3  4 1
2 4 9 16 1
4 8 24 63 3
6 16 51 100 -29
3
2 2 3 3
4 7 7 1
-2 4 5 -7
3
1 1 -1
1 2 -2
-2 1 1
3
2 -1 3 1
4 2 5 4
1 2 0 7*/

你可能感兴趣的:(计算方法)