LoginSignup
1
0

More than 1 year has passed since last update.

C#で行列計算

Posted at

C#で行列計算

久しぶりの投稿です。
ずっと前に、C#で行列計算をしてディープラーニングをするプログラムを作りました。(過去の記事を参照してください)
その時問題だったのは、行列計算するクラスが非常に使いにくかったことです。

今回、そのクラスを0から作り直して使い勝手の良いものを作ろうと思いました。
そして、できあがったのは↓これです。
まだ、行列の足し算しかできませんが、今後引き算、掛け算、転置行列ができるようにして、ディープラーニングをやってみたいと思います。

行列計算クラス

Matrix.cs
namespace Matrix2
{
    class Matrix_code {
        private int code_number;
        public int Code_number
        {
            get
            {
                return code_number;
            }
        }

        /// <summary>
        /// 0:正常
        /// 1:配列要素数オーバー
        /// </summary>
        /// <param name="c"></param>
        public Matrix_code(int c)
        {
            code_number = c;
        }
    }

    class Matrix
    {
        private double[] data = new double[150];
        private int _x, _y;

        public int _X {
            get
            {
                return _x;
            }
            //set
            //{
            //    _x = value;
            //}
        }

        public int _Y
        {
            get
            {
                return _y;
            }
            //set
            //{
            //    _y = value;
            //}
        }

        public double this[int x, int y]          //行列の各要素を取得、設定するインデクサー
        {
            set
            {
                data[((y - 1) * _y) + x - 1] = value;
            }
            get
            {
                return data[((y - 1) * _y) + x - 1];
            }
        }

        public Matrix(int x,int y)
        {
            _x = x;
            _y = y;
        }

        public Matrix_code setValue(params double[] v)       //行列のデータを設定する
        {
            if (v.Length > _x * _y) return new Matrix_code(1);

            for (int i = 0; i < v.Length; i++)
            {
                data[i] = v[i];
            }

            return new Matrix_code(0);
        }

        public Matrix_code setValue(Matrix matrix)
        {
            if ((_x != matrix._x) || (_y != matrix._y)) return new Matrix_code(1);

            data = matrix.data;

            return new Matrix_code(0);
        }

        public static Matrix operator+(Matrix m1,Matrix m2)           //行列の足し算
        {
            if ((m1._x != m2._x) || (m1._y != m2._y)) return new Matrix(1, 1);

            Matrix matrix = new Matrix(m1._x, m1._y);

            for (int i = 0; i < m1._x * m1._y; i++)
            {
                matrix.data[i] = m1.data[i] + m2.data[i];
            }

            return matrix;
        }

        public static Matrix operator +(double v, Matrix m)
        {
            Matrix matrix = new Matrix(m._x, m._y);

            for (int i = 0; i < m._x * m._y; i++)
            {
                matrix.data[i] = m.data[i] + v;
            }

            return matrix;
        }

        public static Matrix operator +(Matrix m, double v)
        {
            Matrix matrix = new Matrix(m._x, m._y);

            for (int i = 0; i < m._x * m._y; i++)
            {
                matrix.data[i] = m.data[i] + v;
            }

            return matrix;
        }
    }
}

使用例

Form1.cs
using System;
using System.Windows.Forms;

namespace Matrix2
{
    public partial class Form1 : Form
    {
        Matrix m1 = new Matrix(2, 2);    //2✕2の行列
        Matrix m2 = new Matrix(2, 2);    //2✕2の行列

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)      //ボタン1が押されたとき
        {
            m1.setValue(
                1, 2,
                3, 4);            //m1 に行列のデータセット
            m2.setValue(
                10, 11,
                12, 13);          //m2 に行列のデータセット

            Matrix m;

            m = m1 + m2;         //足し算

            label1.Text = m[1, 2].ToString();    //要素(1,2)を取得し、ラベル1に表示
        }
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0