程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 在C#中實現矩陣運算

在C#中實現矩陣運算

編輯:C#入門知識

環境:


主機:XP

開發環境:VS2008

 

功能:

在C#中實現矩陣運算

 


源代碼:


[csharp] 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
 
//矩陣數據結構   
//二維矩陣   
class _Matrix   
{    
    public int m;   
    public int n;   
    public float[] arr; 
       
    //初始化   
    public _Matrix() 
    { 
        m = 0;   
        n = 0;  
    } 
 
    public _Matrix(int mm,int nn) 
    { 
        m = mm;   
        n = nn;  
    } 
 
    //設置m   
    public void set_mn(int mm,int nn) 
    { 
        m = mm;   
        n = nn;  
    }  
 
 
    //設置m   
    public void set_m(int mm) 
    {   
        m = mm;   
    }  
 
    //設置n   
    public void set_n(int nn) 
    {   
        n = nn;   
    } 
    
    //初始化   
    public void init_matrix() 
    {   
        arr = new float[m * n];   
    }   
 
    //釋放   
    public void free_matrix() 
    { 
        //delete [] arr; 
    }   
 
    //讀取i,j坐標的數據   
    //失敗返回-31415,成功返回值   
    public float read(int i,int j) 
    { 
        if (i >= m || j >= n) 
        { 
            return -31415; 
        } 
 
        //return *(arr + i * n + j); 
        return arr[i * n + j]; 
    }   
 
    //寫入i,j坐標的數據   
    //失敗返回-1,成功返回1   
    public int write(int i,int j,float val) 
    { 
        if (i >= m || j >= n) 
        { 
            return -1; 
        } 
 
        arr[i * n + j] = val; 
        return 1; 
    }   
}; 
 
//二維運算類   
class _Matrix_Calc   
{    
    //初始化 
    public  _Matrix_Calc() 
    { 
 
    } 
 
    //C = A + B   
    //成功返回1,失敗返回-1   
    public  int add(ref _Matrix A,ref _Matrix B,ref _Matrix C) 
    {   
        int i = 0;   
        int j = 0;   
           
        //判斷是否可以運算   
        if (A.m != B.m || A.n != B.n ||   
            A.m != C.m || A.n != C.n)   
        {   
            return -1;   
        }   
        //運算   
        for (i = 0;i < C.m;i++)   
        {   
            for (j = 0;j < C.n;j++)   
            {   
                C.write(i,j,A.read(i,j) + B.read(i,j));   
            }   
        }   
           
        return 1;   
    }   
 
    //C = A - B   
    //成功返回1,失敗返回-1   
    public  int subtract(ref _Matrix A,ref _Matrix B, ref _Matrix C) 
    {   
        int i = 0;   
        int j = 0;   
           
        //判斷是否可以運算   
        if (A.m != B.m || A.n != B.n ||  
            A.m != C.m || A.n != C.n)   
        {   
            return -1;   
        }   
        //運算   
        for (i = 0;i < C.m;i++)   
        {   
            for (j = 0;j < C.n;j++)   
            {   
                C.write(i,j,A.read(i,j) - B.read(i,j));   
            }   
        }   
           
        return 1;   
    }  
 
    //C = A * B   
    //成功返回1,失敗返回-1   
    public int multiply(ref _Matrix A, ref _Matrix B, ref _Matrix C) 
    {   
        int i = 0;   
        int j = 0;   
        int k = 0;   
        float temp = 0;   
           
        //判斷是否可以運算   
        if (A.m != C.m || B.n != C.n ||   
            A.n != B.m)   
        {   
            return -1;   
        }   
        //運算   
        for (i = 0;i < C.m;i++)   
        {   
            for (j = 0;j < C.n;j++)   
            {   
                temp = 0;   
                for (k = 0;k < A.n;k++)   
                {   
                    temp += A.read(i,k) * B.read(k,j);   
                }   
                C.write(i,j,temp);   
            }   
        }   
           
        return 1;   
    }  
 
    //行列式的值,只能計算2 * 2,3 * 3   
    //失敗返回-31415,成功返回值   
    public float det(ref _Matrix A) 
    {   
        float value = 0;   
           
        //判斷是否可以運算   
        if (A.m != A.n || (A.m != 2 && A.m != 3))   
        {   
            return -31415;   
        }   
        //運算   
        if (A.m == 2)   
        {   
            value = A.read(0,0) * A.read(1,1) - A.read(0,1) * A.read(1,0);   
        }   
        else   
        {   
            value = A.read(0,0) * A.read(1,1) * A.read(2,2) +    
                    A.read(0,1) * A.read(1,2) * A.read(2,0) +   
                    A.read(0,2) * A.read(1,0) * A.read(2,1) -   
                    A.read(0,0) * A.read(1,2) * A.read(2,1) -   
                    A.read(0,1) * A.read(1,0) * A.read(2,2) -   
                    A.read(0,2) * A.read(1,1) * A.read(2,0);   
        }   
           
        return value;   
    } 
 
    //求轉置矩陣,B = AT   
    //成功返回1,失敗返回-1   
    public int transpos(ref _Matrix A,ref _Matrix B) 
    {   
        int i = 0;   
        int j = 0;   
           
        //判斷是否可以運算   
        if (A.m != B.n || A.n != B.m)   
        {   
            return -1;   
        }   
        //運算   
        for (i = 0;i < B.m;i++)   
        {   
            for (j = 0;j < B.n;j++)   
            {   
                B.write(i,j,A.read(j,i));   
            }   
        }   
           
        return 1;   
    }   
 
    //求逆矩陣,B = A^(-1)   
    //成功返回1,失敗返回-1   
    public int inverse(ref _Matrix A, ref _Matrix B) 
    {   
        int i = 0;   
        int j = 0;   
        int k = 0;   
        _Matrix m = new _Matrix(A.m,2 * A.m);   
        float temp = 0;   
        float b = 0;   
           
        //判斷是否可以運算   
        if (A.m != A.n || B.m != B.n || A.m != B.m)   
        {   
            return -1;   
        }   
           
        /* 
        //如果是2維或者3維求行列式判斷是否可逆 
        if (A.m == 2 || A.m == 3) 
        { 
            if (det(A) == 0) 
            { 
                return -1; 
            } 
        } 
        */   
           
        //增廣矩陣m = A | B初始化   
        m.init_matrix();   
        for (i = 0;i < m.m;i++)   
        {   
            for (j = 0;j < m.n;j++)   
            {   
                if (j <= A.n - 1)   
                {   
                    m.write(i,j,A.read(i,j));   
                }   
                else   
                {   
                    if (i == j - A.n)   
                    {   
                        m.write(i,j,1);   
                    }   
                    else   
                    {   
                        m.write(i,j,0);   
                    }   
                }   
            }   
        }   
           
        //高斯消元   
        //變換下三角   
        for (k = 0;k < m.m - 1;k++)   
        {   
            //如果坐標為k,k的數為0,則行變換   
            if (m.read(k,k) == 0)   
            {   
                for (i = k + 1;i < m.m;i++)   
                {   
                    if (m.read(i,k) != 0)   
                    {   
                        break;   
                    }   
                }   
                if (i >= m.m)   
                {   
                    return -1;   
                }   
                else   
                {   
                    //交換行   
                    for (j = 0;j < m.n;j++)   
                    {   
                        temp = m.read(k,j);   
                        m.write(k,j,m.read(k + 1,j));   
                        m.write(k + 1,j,temp);   
                    }   
                }   
            }   
               
            //消元   
            for (i = k + 1;i < m.m;i++)   
            {   
                //獲得倍數   
                b = m.read(i,k) / m.read(k,k);   
                //行變換   
                for (j = 0;j < m.n;j++)   
                {   
                    temp = m.read(i,j) - b * m.read(k,j);   
                    m.write(i,j,temp);   
                }   
            }   
        }   
        //變換上三角   
        for (k = m.m - 1;k > 0;k--)   
        {   
            //如果坐標為k,k的數為0,則行變換   
            if (m.read(k,k) == 0)   
            {   
                for (i = k + 1;i < m.m;i++)   
                {   
                    if (m.read(i,k) != 0)   
                    {   
                        break;   
                    }   
                }   
                if (i >= m.m)   
                {   
                    return -1;   
                }   
                else   
                {   
                    //交換行   
                    for (j = 0;j < m.n;j++)   
                    {   
                        temp = m.read(k,j);   
                        m.write(k,j,m.read(k + 1,j));   
                        m.write(k + 1,j,temp);   
                    }   
                }   
            }   
               
            //消元   
            for (i = k - 1;i >= 0;i--)   
            {   
                //獲得倍數   
                b = m.read(i,k) / m.read(k,k);   
                //行變換   
                for (j = 0;j < m.n;j++)   
                {   
                    temp = m.read(i,j) - b * m.read(k,j);   
                    m.write(i,j,temp);   
                }   
            }   
        }   
        //將左邊方陣化為單位矩陣   
        for (i = 0;i < m.m;i++)   
        {   
            if (m.read(i,i) != 1)   
            {   
                //獲得倍數   
                b = 1 / m.read(i,i);   
                //行變換   
                for (j = 0;j < m.n;j++)   
                {   
                    temp = m.read(i,j) * b;   
                    m.write(i,j,temp);   
                }   
            }   
        }   
        //求得逆矩陣   
        for (i = 0;i < B.m;i++)   
        {   
            for (j = 0;j < B.m;j++)   
            {   
                B.write(i,j,m.read(i,j + m.m));   
            }   
        }   
        //釋放增廣矩陣   
        m.free_matrix();   
           
        return 1;   
    }   
};   
 
namespace test 

    public partial class Form1 : Form 
    { 
        double zk; 
        double xkg, pkg, kk, xk, pk, q, r; 
 
        public Form1() 
        { 
            InitializeComponent(); 
            xk = 0; 
            pk = 0; 
            q = 0.00001; 
            r = 0.0001; 
 
 
            int i = 0; 
            int j = 0; 
            int k = 0;   
 
            _Matrix_Calc m_c = new _Matrix_Calc();   
            //_Matrix m1 = new _Matrix(3,3);   
            //_Matrix m2 = new _Matrix(3,3); 
            //_Matrix m3 = new _Matrix(3,3); 
 
            _Matrix m1 = new _Matrix(2, 2); 
            _Matrix m2 = new _Matrix(2, 2); 
            _Matrix m3 = new _Matrix(2, 2);  
               
            //初始化內存   
            m1.init_matrix();   
            m2.init_matrix();   
            m3.init_matrix();   
               
            //初始化數據   
            k = 1;   
            for (i = 0;i < m1.m;i++)   
            {   
                for (j = 0;j < m1.n;j++)   
                {   
                    m1.write(i,j,k++);   
                }   
            }   
               
            for (i = 0;i < m2.m;i++)   
            {   
                for (j = 0;j < m2.n;j++)   
                {   
                    m2.write(i,j,k++);   
                }   
            } 
 
            m_c.multiply(ref m1,ref  m2, ref m3); 
            //output.Text = Convert.ToString(m3.read(1,1)); 
            output.Text = Convert.ToString(m_c.det(ref m1)); 
        } 
        /*
        private void button1_Click(object sender, EventArgs e)
        {
            zk = Convert.ToDouble(input.Text);
 
            //時間方程
            xkg = xk;
            pkg = pk + q;
 
            //狀態方程
            kk = pkg / (pkg + r);
            xk = xkg + kk * (zk - xkg);
            pk = (1 - kk) * pkg;
 
            //輸出
            output.Text = Convert.ToString(xk);
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
 
        }
         * */ 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved