![]() | LANGUAGE | スカ友 | 俺、関東の国王だけど |
matrixのライブラリ(C#版 & F#版) |
---|
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; } } } |
open System
この記事は 2021年2月22日 月 16:53 に作者によって編集されましたopen Microsoft.FSharp.Collections type Matrix = class val m : float[,] new (rows : int, cols : int) = { m = Array2D.create rows cols 0.0 } member x.Item with get (i, j) = x.m.[i - 1, j - 1] and set (i, j) v = x.m.[i - 1, j - 1] <- v member x.Get (i : int) (j : int) = x.m.[i - 1, j - 1] member x.Set (i : int) (j : int) (v : float) = x.m.[i - 1, j - 1] <- v member x.Rows with get () = x.m.GetLength(0) member x.Cols with get () = x.m.GetLength(1) member x.T () : Matrix = let ret = new Matrix(x.Cols, x.Rows) for i = 0 to x.Rows - 1 do for j = 0 to x.Cols - 1 do ret.m.[j, i] <- x.m.[i, j] ret static member Identity (n : int) : Matrix = let ret = new Matrix(n, n) for i = 0 to n - 1 do ret.m.[i, i] <- 1.0 ret static member (+) (x : Matrix, y : Matrix) : Matrix = if x.Rows <> y.Rows || x.Cols <> y.Cols then raise (new Exception()) let ret = new Matrix(x.Rows, x.Cols) for i = 0 to ret.Rows - 1 do for j = 0 to ret.Cols - 1 do ret.m.[i, j] <- x.m.[i, j] + y.m.[i, j] ret static member (-) (x : Matrix, y : Matrix) : Matrix = if x.Rows <> y.Rows || x.Cols <> y.Cols then raise (new Exception()) let ret = new Matrix(x.Rows, x.Cols) for i = 0 to ret.Rows - 1 do for j = 0 to ret.Cols - 1 do ret.m.[i, j] <- x.m.[i, j] - y.m.[i, j] ret static member (*) (k : float, x : Matrix) : Matrix = let ret = new Matrix(x.Rows, x.Cols) for i = 0 to ret.Rows - 1 do for j = 0 to ret.Cols - 1 do ret.m.[i, j] <- k * x.m.[i, j] ret static member (*) (x : Matrix, k : float) : Matrix = k * x static member (/) (x : Matrix, k : float) : Matrix = (1.0 / k) * x static member (*) (x : Matrix, y : Matrix) : Matrix = if x.Cols <> y.Rows then raise (new Exception()) let M = x.Rows let S = x.Cols let N = y.Cols let ret = new Matrix(M, N) for i = 0 to M - 1 do for j = 0 to N - 1 do for k = 0 to S - 1 do ret.m.[i, j] <- ret.m.[i, j] + x.m.[i, k] * y.m.[k, j] ret static member Dot (x : Matrix) (y : Matrix) : float = if (x.Rows <> 1 && x.Cols <> 1) || (y.Rows <> 1 && y.Cols <> 1) then raise (new Exception()) let mutable ret : float = 0.0 if x.Rows = 1 then if y.Rows = 1 then if x.Cols <> y.Cols then raise (new Exception()) for i = 0 to x.Cols - 1 do ret <- ret + x.m.[0, i] * y.m.[0, i] else // y.Cols = 1 if x.Cols <> y.Rows then raise (new Exception()) for i = 0 to x.Cols - 1 do ret <- ret + x.m.[0, i] * y.m.[i, 0] else // x.Cols = 1 if y.Rows = 1 then if x.Rows <> y.Cols then raise (new Exception()) for i = 0 to x.Rows - 1 do ret <- ret + x.m.[i, 0] * y.m.[0, i] else // y.Cols = 1 if x.Rows <> y.Rows then raise (new Exception()) for i = 0 to x.Rows - 1 do ret <- ret + x.m.[i, 0] * y.m.[i, 0] ret member x.Norm () : float = sqrt(Matrix.Dot x x) member x.Normalize () : Matrix = x / x.Norm() member x.Each (f : float -> float) : Matrix = let ret = new Matrix(x.Rows, x.Cols) for i = 0 to x.Rows - 1 do for j = 0 to x.Cols - 1 do ret.m.[i, j] <- f x.m.[i, j] ret end |
up
|
up
|
up
|