矩阵运算 相加,相减,乘标量,相乘

练习下矩阵的基本运算:O(∩_∩)O~

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class MyMatrix3 
    {
        int m11, m12, m13;
        int m21, m22, m23;
        int m31, m32, m33;


        //矩阵相加
        public MyMatrix3 AddMatrix3(MyMatrix3 m1,MyMatrix3 m2)
        {
            MyMatrix3 newMatrix3 = new MyMatrix3();

            newMatrix3.m11 = m1.m11 + m2.m11;
            newMatrix3.m12 = m1.m12 + m2.m12;
            newMatrix3.m13 = m1.m13 + m2.m13;

            newMatrix3.m21 = m1.m21 + m2.m21;
            newMatrix3.m22 = m1.m22 + m2.m22;
            newMatrix3.m23 = m1.m23 + m2.m23;


            newMatrix3.m31 = m1.m31 + m2.m31;
            newMatrix3.m32 = m1.m32 + m2.m32;
            newMatrix3.m33 = m1.m33 + m2.m33;

            return newMatrix3;
        }

        //矩阵相加
        public MyMatrix3 SubMatrix3(MyMatrix3 m1, MyMatrix3 m2) 
        {
            MyMatrix3 newMatrix3 = new MyMatrix3();

            newMatrix3.m11 = m1.m11 - m2.m11;
            newMatrix3.m12 = m1.m12 - m2.m12;
            newMatrix3.m13 = m1.m13 - m2.m13;

            newMatrix3.m21 = m1.m21 - m2.m21;
            newMatrix3.m22 = m1.m22 - m2.m22;
            newMatrix3.m23 = m1.m23 - m2.m23;


            newMatrix3.m31 = m1.m31 - m2.m31;
            newMatrix3.m32 = m1.m32 - m2.m32;
            newMatrix3.m33 = m1.m33 - m2.m33;

            return newMatrix3;
        }


        //矩阵和标量相乘
        public MyMatrix3 ScalarMultiply(MyMatrix3 a,int scale) 
        {
            MyMatrix3 newMatrix3 = new MyMatrix3();
            newMatrix3.m11 = a.m11 * scale;
            newMatrix3.m12 = a.m12 * scale;
            newMatrix3.m12 = a.m12 * scale;

            newMatrix3.m21 = a.m21 * scale;
            newMatrix3.m22 = a.m22 * scale;
            newMatrix3.m23 = a.m23 * scale;

            newMatrix3.m21 = a.m21 * scale;
            newMatrix3.m22 = a.m22 * scale;
            newMatrix3.m23 = a.m23 * scale;

            return newMatrix3;
        }


        //矩阵相乘
        public MyMatrix3 ScalarMatrix3(MyMatrix3 a,MyMatrix3 b) 
        {
            MyMatrix3 newMatrix3 = new MyMatrix3();

            newMatrix3.m11 = a.m11 * b.m11 + a.m12 * b.m21 + a.m13 * b.m31;
            newMatrix3.m12 = a.m11 * b.m12 + a.m12 * b.m22 + a.m13 * b.m32;
            newMatrix3.m13 = a.m11 * b.m13 + a.m12 * b.m23 + a.m13 * b.m33;

            newMatrix3.m21 = a.m21 * b.m11 + a.m22 * b.m21 + a.m23 * b.m31;
            newMatrix3.m22 = a.m21 * b.m12 + a.m22 * b.m22 + a.m23 * b.m32;
            newMatrix3.m23 = a.m21 * b.m13 + a.m22 * b.m23 + a.m23 * b.m33;

            newMatrix3.m31 = a.m31 * b.m11 + a.m32 * b.m21 + a.m33 * b.m31;
            newMatrix3.m32 = a.m31 * b.m12 + a.m32 * b.m22 + a.m33 * b.m32;
            newMatrix3.m33 = a.m31 * b.m13 + a.m32 * b.m23 + a.m33 * b.m33;


            return newMatrix3;
        }
    }
}
posted @ 2015-04-05 15:11  盘子脸  阅读(589)  评论(0编辑  收藏  举报