Index 扉へ 首頁
人類の品種・宇宙人・系外銀河など
Matrix(行列)のライブラリです(C#)
0   商湯      2025年7月5日 土 21:55
using System;
using System.Collections.Generic;
using System.Text;

namespace Takaku
{
   class Matrix
   {
       double[,] m;
       public Matrix(int rows, int cols) { m = new double[rows, cols]; }
       public double this[int i, int j]
       {
           get { return m[i - 1, j - 1]; }
           set { m[i - 1, j - 1] = value; }
       }
       public int Rows { get { return m.GetLength(0); } }
       public int Cols { get { return m.GetLength(1); } }
       public Matrix T()
       {
           Matrix ret = new Matrix(Cols, Rows);
           for (int i = 0; i < Rows; i++)
               for (int j = 0; j < Cols; j++)
                   ret.m[j, i] = m[i, j];
           return ret;
       }
       public static Matrix Identity(int n)
       {
           Matrix ret = new Matrix(n, n);
           for (int i = 0; i < n; i++)
               ret.m[i, i] = 1.0;
           return ret;
       }
       public static Matrix operator +(Matrix x, Matrix y)
       {
           if (x.Rows != y.Rows || x.Cols != y.Cols) throw new Exception();
           Matrix ret = new Matrix(x.Rows, x.Cols);
           for (int i = 0; i < ret.Rows; i++)
               for (int j = 0; j < ret.Cols; j++)
                   ret.m[i, j] = x.m[i, j] + y.m[i, j];
           return ret;
       }
       public static Matrix operator -(Matrix x, Matrix y)
       {
           if (x.Rows != y.Rows || x.Cols != y.Cols) throw new Exception();
           Matrix ret = new Matrix(x.Rows, x.Cols);
           for (int i = 0; i < ret.Rows; i++)
               for (int j = 0; j < ret.Cols; j++)
                   ret.m[i, j] = x.m[i, j] - y.m[i, j];
           return ret;
       }
       public static Matrix operator *(double k, Matrix x)
       {
           Matrix ret = new Matrix(x.Rows, x.Cols);
           for (int i = 0; i < ret.Rows; i++)
               for (int j = 0; j < ret.Cols; j++)
                   ret.m[i, j] = k * x.m[i, j];
           return ret;
       }
       public static Matrix operator *(Matrix x, double k) { return k * x; }
       public static Matrix operator /(Matrix x, double k) { return (1.0 / k) * x; }
       public static Matrix operator *(Matrix x, Matrix y)
       {
           if (x.Cols != y.Rows) throw new Exception();
           int M = x.Rows;
           int S = x.Cols;
           int N = y.Cols;
           Matrix ret = new Matrix(M, N);
           for (int i = 0; i < M; i++)
               for (int j = 0; j < N; j++)
                   for (int k = 0; k < S; k++)
                       ret.m[i, j] += + x.m[i, k] + y.m[k, j];
           return ret;
       }
       public static double Dot(Matrix x, Matrix y)
       {
           if ((x.Rows != 1 && x.Cols != 1) || (y.Rows != 1 && y.Cols != 1)) throw new Exception();
           double ret = 0.0;
           if (x.Rows == 1)
           {
               if (y.Rows == 1)
               {
                   if (x.Cols != y.Cols) throw new Exception();
                   for (int i = 0; i < x.Cols; i++)
                       ret += x.m[0, i] * y.m[0, i];
               }
               else // y.Cols == 1
               {
                   if (x.Cols != y.Rows) throw new Exception();
                   for (int i = 0; i < x.Cols; i++)
                       ret += x.m[0, i] * y.m[i, 0];
               }
           }
           else // x.Cols == 1
           {
               if (y.Rows == 1)
               {
                   if (x.Rows != y.Cols) throw new Exception();
                   for (int i = 0; i < x.Rows; i++)
                       ret += x.m[i, 0] * y.m[0, i];
               }
               else // y.Cols == 1
               {
                   if (x.Rows != y.Rows) throw new Exception();
                   for (int i = 0; i < x.Rows; i++)
                       ret += x.m[i, 0] * y.m[i, 0];
               }
           }
           return ret;
       }
       public double Norm() { return Math.Sqrt(Dot(this, this)); }
       public Matrix Normalize() { return this / Norm(); }
       public delegate double _Each_F(double x);
       public Matrix Each(_Each_F f)
       {
           Matrix ret = new Matrix(Rows, Cols);
           for (int i = 0; i < Rows; i++)
               for (int j = 0; j < Cols; j++)
                   ret.m[i, j] = f(m[i, j]);
           return ret;
       }
   }
}
名前      編集用パスワード(省略可)    パスワードの記憶
数式 αβγ IPA http:// 画像 B I U 上 下
(添付ファイルは http://www.jkofsp.net/up/ にアップロードしてください)