Matrix operation C#

Change from: https://blog.csdn.net/jdh99/article/details/7889499

Implementing Matrix Operations in C#


This article blog link: http://blog.csdn.net/jdh99 Author: jdh, reprint please note.


Environmental Science:

Host: XP

Development environment: VS2008


Functions:

Implementing Matrix Operations in C#


Source code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. //Matrix data structure  
  11. //Two-dimensional matrix  
  12. class _Matrix    
  13. {     
  14.     public int m;    
  15.     public int n;    
  16.     public float[] arr;  
  17.         
  18.     //Initialization  
  19.     public _Matrix()  
  20.     {  
  21.         m = 0;    
  22.         n = 0;   
  23.     }  
  24.   
  25.     public _Matrix(int mm,int nn)  
  26.     {  
  27.         m = mm;    
  28.         n = nn;   
  29.     }  
  30.   
  31.     //Set m  
  32.     public void set_mn(int mm,int nn)  
  33.     {  
  34.         m = mm;    
  35.         n = nn;   
  36.     }   
  37.   
  38.   
  39.     //Set m  
  40.     public void set_m(int mm)  
  41.     {    
  42.         m = mm;    
  43.     }   
  44.   
  45.     //Set n  
  46.     public void set_n(int nn)  
  47.     {    
  48.         n = nn;    
  49.     }  
  50.      
  51.     //Initialization  
  52.     public void init_matrix()  
  53.     {    
  54.         arr = new float[m * n];    
  55.     }    
  56.   
  57.     //Release  
  58.     public void free_matrix()  
  59.     {  
  60.         //delete [] arr;  
  61.     }    
  62.   
  63.     //Read the data of i,j coordinates.  
  64.     //Failed return - 31415, successful return value  
  65.     public float read(int i,int j)  
  66.     {  
  67.         if (i >= m || j >= n)  
  68.         {  
  69.             return -31415;  
  70.         }  
  71.   
  72.         //return *(arr + i * n + j);  
  73.         return arr[i * n + j];  
  74.     }    
  75.   
  76.     //Write data in i,j coordinates.  
  77.     //Failed Return - 1, Successful Return 1  
  78.     public int write(int i,int j,float val)  
  79.     {  
  80.         if (i >= m || j >= n)  
  81.         {  
  82.             return -1;  
  83.         }  
  84.   
  85.         arr[i * n + j] = val;  
  86.         return 1;  
  87.     }    
  88. };  
  89.   
  90. //Two-Dimensional Operational Class  
  91. class _Matrix_Calc    
  92. {     
  93.     //Initialization  
  94.     public  _Matrix_Calc()  
  95.     {  
  96.   
  97.     }  
  98.   
  99.     //C = A + B    
  100.     //Successful Return 1, Failed Return - 1  
  101.     public  int add(ref _Matrix A,ref _Matrix B,ref _Matrix C)  
  102.     {    
  103.         int i = 0;    
  104.         int j = 0;    
  105.             
  106.         //Judging whether it can be operated or not  
  107.         if (A.m != B.m || A.n != B.n ||    
  108.             A.m != C.m || A.n != C.n)    
  109.         {    
  110.             return -1;    
  111.         }    
  112.         //Operation  
  113.         for (i = 0;i < C.m;i++)    
  114.         {    
  115.             for (j = 0;j < C.n;j++)    
  116.             {    
  117.                 C.write(i,j,A.read(i,j) + B.read(i,j));    
  118.             }    
  119.         }    
  120.             
  121.         return 1;    
  122.     }    
  123.   
  124.     //C = A - B    
  125.     //Successful Return 1, Failed Return - 1  
  126.     public  int subtract(ref _Matrix A,ref _Matrix B, ref _Matrix C)  
  127.     {    
  128.         int i = 0;    
  129.         int j = 0;    
  130.             
  131.         //Judging whether it can be operated or not  
  132.         if (A.m != B.m || A.n != B.n ||   
  133.             A.m != C.m || A.n != C.n)    
  134.         {    
  135.             return -1;    
  136.         }    
  137.         //Operation  
  138.         for (i = 0;i < C.m;i++)    
  139.         {    
  140.             for (j = 0;j < C.n;j++)    
  141.             {    
  142.                 C.write(i,j,A.read(i,j) - B.read(i,j));    
  143.             }    
  144.         }    
  145.             
  146.         return 1;    
  147.     }   
  148.   
  149.     //C = A * B    
  150.     //Successful Return 1, Failed Return - 1  
  151.     public int multiply(ref _Matrix A, ref _Matrix B, ref _Matrix C)  
  152.     {    
  153.         int i = 0;    
  154.         int j = 0;    
  155.         int k = 0;    
  156.         float temp = 0;    
  157.             
  158.         //Judging whether it can be operated or not  
  159.         if (A.m != C.m || B.n != C.n ||    
  160.             A.n != B.m)    
  161.         {    
  162.             return -1;    
  163.         }    
  164.         //Operation  
  165.         for (i = 0;i < C.m;i++)    
  166.         {    
  167.             for (j = 0;j < C.n;j++)    
  168.             {    
  169.                 temp = 0;    
  170.                 for (k = 0;k < A.n;k++)    
  171.                 {    
  172.                     temp += A.read(i,k) * B.read(k,j);    
  173.                 }    
  174.                 C.write(i,j,temp);    
  175.             }    
  176.         }    
  177.             
  178.         return 1;    
  179.     }   
  180.   
  181.     //The value of determinant can only be calculated as 2*2,3*3.  
  182.     //Failed return - 31415, successful return value  
  183.     public float det(ref _Matrix A)  
  184.     {    
  185.         float value = 0;    
  186.             
  187.         //Judging whether it can be operated or not  
  188.         if (A.m != A.n || (A.m != 2 && A.m != 3))    
  189.         {    
  190.             return -31415;    
  191.         }    
  192.         //Operation  
  193.         if (A.m == 2)    
  194.         {    
  195.             value = A.read(0,0) * A.read(1,1) - A.read(0,1) * A.read(1,0);    
  196.         }    
  197.         else    
  198.         {    
  199.             value = A.read(0,0) * A.read(1,1) * A.read(2,2) +     
  200.                     A.read(0,1) * A.read(1,2) * A.read(2,0) +    
  201.                     A.read(0,2) * A.read(1,0) * A.read(2,1) -    
  202.                     A.read(0,0) * A.read(1,2) * A.read(2,1) -    
  203.                     A.read(0,1) * A.read(1,0) * A.read(2,2) -    
  204.                     A.read(0,2) * A.read(1,1) * A.read(2,0);    
  205.         }    
  206.             
  207.         return value;    
  208.     }  
  209.   
  210.     //Find the transpose matrix, B = AT  
  211.     //Successful Return 1, Failed Return - 1  
  212.     public int transpos(ref _Matrix A,ref _Matrix B)  
  213.     {    
  214.         int i = 0;    
  215.         int j = 0;    
  216.             
  217.         //Judging whether it can be operated or not  
  218.         if (A.m != B.n || A.n != B.m)    
  219.         {    
  220.             return -1;    
  221.         }    
  222.         //Operation  
  223.         for (i = 0;i < B.m;i++)    
  224.         {    
  225.             for (j = 0;j < B.n;j++)    
  226.             {    
  227.                 B.write(i,j,A.read(j,i));    
  228.             }    
  229.         }    
  230.             
  231.         return 1;    
  232.     }    
  233.   
  234.     //Inverse matrix, B = A^(-1)  
  235.     //Successful Return 1, Failed Return - 1  
  236.     public int inverse(ref _Matrix A, ref _Matrix B)  
  237.     {    
  238.         int i = 0;    
  239.         int j = 0;    
  240.         int k = 0;    
  241.         _Matrix m = new _Matrix(A.m,2 * A.m);    
  242.         float temp = 0;    
  243.         float b = 0;    
  244.             
  245.         //Judging whether it can be operated or not  
  246.         if (A.m != A.n || B.m != B.n || A.m != B.m)    
  247.         {    
  248.             return -1;    
  249.         }    
  250.             
  251.         /*  
  252.         //If the determinant is found in two or three dimensions, whether it is reversible or not is judged. 
  253.         if (A.m == 2 || A.m == 3)  
  254.         {  
  255.             if (det(A) == 0)  
  256.             {  
  257.                 return -1;  
  258.             }  
  259.         }  
  260.         */    
  261.             
  262.         //Augmented matrix m = A | B initialization  
  263.         m.init_matrix();    
  264.         for (i = 0;i < m.m;i++)    
  265.         {    
  266.             for (j = 0;j < m.n;j++)    
  267.             {    
  268.                 if (j <= A.n - 1)    
  269.                 {    
  270.                     m.write(i,j,A.read(i,j));    
  271.                 }    
  272.                 else    
  273.                 {    
  274.                     if (i == j - A.n)    
  275.                     {    
  276.                         m.write(i,j,1);    
  277.                     }    
  278.                     else    
  279.                     {    
  280.                         m.write(i,j,0);    
  281.                     }    
  282.                 }    
  283.             }    
  284.         }    
  285.             
  286.         //Gauss elimination  
  287.         //Transform lower triangle  
  288.         for (k = 0;k < m.m - 1;k++)    
  289.         {    
  290.             //If the coordinate is K and the number of K is 0, then the row transformation  
  291.             if (m.read(k,k) == 0)    
  292.             {    
  293.                 for (i = k + 1;i < m.m;i++)    
  294.                 {    
  295.                     if (m.read(i,k) != 0)    
  296.                     {    
  297.                         break;    
  298.                     }    
  299.                 }    
  300.                 if (i >= m.m)    
  301.                 {    
  302.                     return -1;    
  303.                 }    
  304.                 else    
  305.                 {    
  306.                     //Exchange line  
  307.                     for (j = 0;j < m.n;j++)    
  308.                     {    
  309.                         temp = m.read(k,j);    
  310.                         m.write(k,j,m.read(k + 1,j));    
  311.                         m.write(k + 1,j,temp);    
  312.                     }    
  313.                 }    
  314.             }    
  315.                 
  316.             //Elimination  
  317.             for (i = k + 1;i < m.m;i++)    
  318.             {    
  319.                 //Get multiple.  
  320.                 b = m.read(i,k) / m.read(k,k);    
  321.                 //Line transformation  
  322.                 for (j = 0;j < m.n;j++)    
  323.                 {    
  324.                     temp = m.read(i,j) - b * m.read(k,j);    
  325.                     m.write(i,j,temp);    
  326.                 }    
  327.             }    
  328.         }    
  329.         //Transform upper triangle  
  330.         for (k = m.m - 1;k > 0;k–)    
  331.         {    
  332.             //If the coordinate is K and the number of K is 0, then the row transformation  
  333.             if (m.read(k,k) == 0)    
  334.             {    
  335.                 for (i = k + 1;i < m.m;i++)    
  336.                 {    
  337.                     if (m.read(i,k) != 0)    
  338.                     {    
  339.                         break;    
  340.                     }    
  341.                 }    
  342.                 if (i >= m.m)    
  343.                 {    
  344.                     return -1;    
  345.                 }    
  346.                 else    
  347.                 {    
  348.                     //Exchange line  
  349.                     for (j = 0;j < m.n;j++)    
  350.                     {    
  351.                         temp = m.read(k,j);    
  352.                         m.write(k,j,m.read(k + 1,j));    
  353.                         m.write(k + 1,j,temp);    
  354.                     }    
  355.                 }    
  356.             }    
  357.                 
  358.             //Elimination  
  359.             for (i = k - 1;i >= 0;i–)    
  360.             {    
  361.                 //Get multiple.  
  362.                 b = m.read(i,k) / m.read(k,k);    
  363.                 //Line transformation  
  364.                 for (j = 0;j < m.n;j++)    
  365.                 {    
  366.                     temp = m.read(i,j) - b * m.read(k,j);    
  367.                     m.write(i,j,temp);    
  368.                 }    
  369.             }    
  370.         }    
  371.         //The left square matrix is transformed into the unit matrix.  
  372.         for (i = 0;i < m.m;i++)    
  373.         {    
  374.             if (m.read(i,i) != 1)    
  375.             {    
  376.                 //Get multiple.  
  377.                 b = 1 / m.read(i,i);    
  378.                 //Line transformation  
  379.                 for (j = 0;j < m.n;j++)    
  380.                 {    
  381.                     temp = m.read(i,j) * b;    
  382.                     m.write(i,j,temp);    
  383.                 }    
  384.             }    
  385.         }    
  386.         //Find the inverse matrix.  
  387.         for (i = 0;i < B.m;i++)    
  388.         {    
  389.             for (j = 0;j < B.m;j++)    
  390.             {    
  391.                 B.write(i,j,m.read(i,j + m.m));    
  392.             }    
  393.         }    
  394.         //Release augmentation matrix  
  395.         m.free_matrix();    
  396.             
  397.         return 1;    
  398.     }    
  399. };    
  400.   
  401. namespace test  
  402. {  
  403.     public partial class Form1 : Form  
  404.     {  
  405.         double zk;  
  406.         double xkg, pkg, kk, xk, pk, q, r;  
  407.   
  408.         public Form1()  
  409.         {  
  410.             InitializeComponent();  
  411.             xk = 0;  
  412.             pk = 0;  
  413.             q = 0.00001;  
  414.             r = 0.0001;  
  415.   
  416.   
  417.             int i = 0;  
  418.             int j = 0;  
  419.             int k = 0;    
  420.   
  421.             _Matrix_Calc m_c = new _Matrix_Calc();    
  422.             //_Matrix m1 = new _Matrix(3,3);    
  423.             //_Matrix m2 = new _Matrix(3,3);  
  424.             //_Matrix m3 = new _Matrix(3,3);  
  425.   
  426.             _Matrix m1 = new _Matrix(2, 2);  
  427.             _Matrix m2 = new _Matrix(2, 2);  
  428.             _Matrix m3 = new _Matrix(2, 2);   
  429.                 
  430.             //Initialize memory  
  431.             m1.init_matrix();    
  432.             m2.init_matrix();    
  433.             m3.init_matrix();    
  434.                 
  435.             //Initialize data  
  436.             k = 1;    
  437.             for (i = 0;i < m1.m;i++)    
  438.             {    
  439.                 for (j = 0;j < m1.n;j++)    
  440.                 {    
  441.                     m1.write(i,j,k++);    
  442.                 }    
  443.             }    
  444.                 
  445.             for (i = 0;i < m2.m;i++)    
  446.             {    
  447.                 for (j = 0;j < m2.n;j++)    
  448.                 {    
  449.                     m2.write(i,j,k++);    
  450.                 }    
  451.             }  
  452.   
  453.             m_c.multiply(ref m1,ref  m2, ref m3);  
  454.             //output.Text = Convert.ToString(m3.read(1,1));  
  455.             output.Text = Convert.ToString(m_c.det(ref m1));  
  456.         }  
  457.         /* 
  458.         private void button1_Click(object sender, EventArgs e) 
  459.         { 
  460.             zk = Convert.ToDouble(input.Text); 
  461.  
  462.             //Time equation 
  463.             xkg = xk; 
  464.             pkg = pk + q; 
  465.   
  466.             //state equation 
  467.             kk = pkg / (pkg + r); 
  468.             xk = xkg + kk * (zk - xkg); 
  469.             pk = (1 - kk) * pkg; 
  470.  
  471.             //output 
  472.             output.Text = Convert.ToString(xk); 
  473.         } 
  474.  
  475.         private void textBox1_TextChanged(object sender, EventArgs e) 
  476.         { 
  477.  
  478.         } 
  479.          * */  
  480.     }  
  481. }  
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;

// Matrix data structure  
// Two-dimensional matrix  
class _Matrix  
{   
    public int m;  
    public int n;  
    public float[] arr;

    / / initialization  
    public _Matrix()
    {
        m = 0;  
        n = 0; 
    }

    public _Matrix(int mm,int nn)
    {
        m = mm;  
        n = nn; 
    }

    / / set m  
    public void set_mn(int mm,int nn)
    {
        m = mm;  
        n = nn; 
    } 


    / / set m  
    public void set_m(int mm)
    {  
        m = mm;  
    } 

    / / set n  
    public void set_n(int nn)
    {  
        n = nn;  
    }

    / / initialization  
    public void init_matrix()
    {  
        arr = new float[m * n];  
    }  

    / / release  
    public void free_matrix()
    {
        //delete [] arr;
    }  

    // Read the data of i,j coordinates  
    // Failed return - 31415, successful return value  
    public float read(int i,int j)
    {
        if (i >= m || j >= n)
        {
            return -31415;
        }

        //return *(arr + i * n + j);
        return arr[i * n + j];
    }  

    // Write data in i,j coordinates  
    // Failed return - 1, successful return 1  
    public int write(int i,int j,float val)
    {
        if (i >= m || j >= n)
        {
            return -1;
        }

        arr[i * n + j] = val;
        return 1;
    }  
};

// Two-Dimensional Operational Class  
class _Matrix_Calc  
{   
    / / initialization
    public  _Matrix_Calc()
    {

    }

    //C = A + B  
    // Successful Return 1, Failed Return - 1  
    public  int add(ref _Matrix A,ref _Matrix B,ref _Matrix C)
    {  
        int i = 0;  
        int j = 0;  

        // Judging whether it can be operated on  
        if (A.m != B.m || A.n != B.n ||  
            A.m != C.m || A.n != C.n)  
        {  
            return -1;  
        }  
        / / operation  
        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  
    // Successful Return 1, Failed Return - 1  
    public  int subtract(ref _Matrix A,ref _Matrix B, ref _Matrix C)
    {  
        int i = 0;  
        int j = 0;  

        // Judging whether it can be operated on  
        if (A.m != B.m || A.n != B.n || 
            A.m != C.m || A.n != C.n)  
        {  
            return -1;  
        }  
        / / operation  
        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  
    // Successful Return 1, Failed Return - 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;  

        // Judging whether it can be operated on  
        if (A.m != C.m || B.n != C.n ||  
            A.n != B.m)  
        {  
            return -1;  
        }  
        / / operation  
        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;  
    } 

    // The value of determinant can only be calculated as 2*2,3*3  
    // Failed return - 31415, successful return value  
    public float det(ref _Matrix A)
    {  
        float value = 0;  

        // Judging whether it can be operated on  
        if (A.m != A.n || (A.m != 2 && A.m != 3))  
        {  
            return -31415;  
        }  
        / / operation  
        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;  
    }

    // Find the transpose matrix, B = AT  
    // Successful Return 1, Failed Return - 1  
    public int transpos(ref _Matrix A,ref _Matrix B)
    {  
        int i = 0;  
        int j = 0;  

        // Judging whether it can be operated on  
        if (A.m != B.n || A.n != B.m)  
        {  
            return -1;  
        }  
        / / operation  
        for (i = 0;i < B.m;i++)  
        {  
            for (j = 0;j < B.n;j++)  
            {  
                B.write(i,j,A.read(j,i));  
            }  
        }  

        return 1;  
    }  

    // Inverse matrix, B = A^(-1)  
    // Successful Return 1, Failed Return - 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;  

        // Judging whether it can be operated on  
        if (A.m != A.n || B.m != B.n || A.m != B.m)  
        {  
            return -1;  
        }  

        /* 
        // If the determinant is 2-D or 3-D, judge whether it is reversible or not 
        if (A.m == 2 || A.m == 3) 
        { 
            if (det(A) == 0) 
            { 
                return -1; 
            } 
        } 
        */  

        // Initialization of augmented matrix 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);  
                    }  
                }  
            }  
        }  

        // Gauss elimination  
        // Transform lower triangle  
        for (k = 0;k < m.m - 1;k++)  
        {  
            // If the coordinate is K and the number of K is 0, then the row transformation  
            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  
                {  
                    / / exchange line  
                    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);  
                    }  
                }  
            }  

            / / elimination  
            for (i = k + 1;i < m.m;i++)  
            {  
                // Get multiple  
                b = m.read(i,k) / m.read(k,k);  
                / / row transformation  
                for (j = 0;j < m.n;j++)  
                {  
                    temp = m.read(i,j) - b * m.read(k,j);  
                    m.write(i,j,temp);  
                }  
            }  
        }  
        // Transform upper triangle  
        for (k = m.m - 1;k > 0;k--)  
        {  
            // If the coordinate is K and the number of K is 0, then the row transformation  
            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  
                {  
                    / / exchange line  
                    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);  
                    }  
                }  
            }  

            / / elimination  
            for (i = k - 1;i >= 0;i--)  
            {  
                // Get multiple  
                b = m.read(i,k) / m.read(k,k);  
                / / row transformation  
                for (j = 0;j < m.n;j++)  
                {  
                    temp = m.read(i,j) - b * m.read(k,j);  
                    m.write(i,j,temp);  
                }  
            }  
        }  
        // Converting the left square matrix into the unit matrix  
        for (i = 0;i < m.m;i++)  
        {  
            if (m.read(i,i) != 1)  
            {  
                // Get multiple  
                b = 1 / m.read(i,i);  
                / / row transformation  
                for (j = 0;j < m.n;j++)  
                {  
                    temp = m.read(i,j) * b;  
                    m.write(i,j,temp);  
                }  
            }  
        }  
        // Finding the Inverse Matrix  
        for (i = 0;i < B.m;i++)  
        {  
            for (j = 0;j < B.m;j++)  
            {  
                B.write(i,j,m.read(i,j + m.m));  
            }  
        }  
        // Release augmentation matrix  
        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); 

            // Initialize memory  
            m1.init_matrix();  
            m2.init_matrix();  
            m3.init_matrix();  

            // Initialization data  
            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);

            // Time equation
            xkg = xk;
            pkg = pk + q;

            // Equation of State
            kk = pkg / (pkg + r);
            xk = xkg + kk * (zk - xkg);
            pk = (1 - kk) * pkg;

            / / output
            output.Text = Convert.ToString(xk);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
         * */
    }
}


Keywords: Windows

Added by spyder on Wed, 15 May 2019 18:23:44 +0300