piatok, 15 jún 2012 09:56 Written by 7239 times
Rate this item
(5 votes)

C# - Inverzná matica 3x3

Jednoduchý program v c# na výpočet inverznej matice 3x3 pre .NET Framework 3.5.

Zo zadanej štvorcovej matice 3x3 program vypočita pomocou metódy determinantov inverznú maticu 3x3 (ak existuje). Podmienka existencie inverznej matice je daná determinantom vstupnej (zadanej) matice. V prílohe sa nachádza DLL a jednoduchá Windows form aplikácia.

 

A - štovrcová matica veľkosti 3x3

|A| - determinant matice

A-1 - inverzná matica

 

invMatrix1

 

Pričom, ak |A| = 0 k matici A neexistuje inverzná matica.

Zdrojový kód:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MathOperations
{
    public class Matrix
    {
        public bool InverseMatrixCalc(out string errorMessage, double[,] inputMatrix, out double[,] inverseMatrix)
        {
            errorMessage = "";
            //|inputMatrix[0,0] inputMatrix[0,1] inputMatrix[0,2]|-1
            //|inputMatrix[1,0] inputMatrix[1,1] inputMatrix[1,2]|
            //|inputMatrix[2,0] inputMatrix[2,1] inputMatrix[2,2]|
            inverseMatrix = new double[3, 3];
            try
            {
                if (inputMatrix.Length != 9 || inputMatrix.Rank != 2)
                {
                    errorMessage = "Vstupna matica ma nespravnu velkost.";
                    return false;
                }
                //DET  =  a11(a33a22-a32a23)-a21(a33a12-a32a13)+a31(a23a12-a22a13)
                double DET = inputMatrix[0,0] * (inputMatrix[2,2] * inputMatrix[1,1]
                            - inputMatrix[2,1] * inputMatrix[1,2])
                            - inputMatrix[1,0] * (inputMatrix[2,2] * inputMatrix[0,1]
                            - inputMatrix[2,1] * inputMatrix[0,2])
                            + inputMatrix[2,0] * (inputMatrix[1,2] * inputMatrix[0,1]
                            - inputMatrix[1,1] * inputMatrix[0,2]);
                //podmienka pre vypocet inverznej matice
                if (DET == 0)
                {
                    errorMessage = "K zadanej vstupnej matici neexistuje inverzna matica.";
                    return false;
                }
                DET = 1 / DET;
                //| a11 a12 a13 |-1             |   a33a22-a32a23  -(a33a12-a32a13)   a23a12-a22a13 |
                //| a21 a22 a23 |    =  1/DET * | -(a33a21-a31a23)   a33a11-a31a13  -(a23a11-a21a13)|
                //| a31 a32 a33 |               |   a32a21-a31a22  -(a32a11-a31a12)   a22a11-a21a12 |
                inverseMatrix[0, 0] = DET * (inputMatrix[2, 2] * inputMatrix[1, 1]
                                    - inputMatrix[2, 1] * inputMatrix[1, 2]);
                inverseMatrix[0, 1] = DET * (-1 * (inputMatrix[2, 2] * inputMatrix[0, 1]
                                    - inputMatrix[2, 1] * inputMatrix[0, 2]));
                inverseMatrix[0, 2] = DET * (inputMatrix[1, 2] * inputMatrix[0, 1]
                                    - inputMatrix[1, 1] * inputMatrix[0, 2]);
                inverseMatrix[1, 0] = DET * (-1 * (inputMatrix[2, 2] * inputMatrix[1, 0]
                                    - inputMatrix[2, 0] * inputMatrix[1, 2]));
                inverseMatrix[1, 1] = DET * (inputMatrix[2, 2] * inputMatrix[0, 0]
                                    - inputMatrix[2, 0] * inputMatrix[0, 2]);
                inverseMatrix[1, 2] = DET * (-1 * (inputMatrix[1, 2] * inputMatrix[0, 0]
                                    - inputMatrix[1, 0] * inputMatrix[0, 2]));
                inverseMatrix[2, 0] = DET * (inputMatrix[2, 1] * inputMatrix[1, 0]
                                    - inputMatrix[2, 0] * inputMatrix[1, 1]);
                inverseMatrix[2, 1] = DET * (-1 * (inputMatrix[2, 1] * inputMatrix[0, 0]
                                    - inputMatrix[2, 0] * inputMatrix[0, 1]));
                inverseMatrix[2, 2] = DET * (inputMatrix[1, 1] * inputMatrix[0, 0]
                                    - inputMatrix[1, 0] * inputMatrix[0, 1]);
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return false;
            }
            return true;
        }
    }
}
Last modified on pondelok, 19 december 2016 21:24
Ing.Peter Šuba

Zakladateľ www.projectik.eu

It's time for another revolution

(Why so serious?)

Website: www.projectik.eu