NxN行列の複素行列クラス
ベクトルとの掛け算、逆行行列演算を実装しているので、
大学のレポート・研究で役立つかも。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; namespace MatrixNxN { [Serializable] public class ComplexMatrix { public ComplexMatrix(int n) { sMatrix = new Complex[n, n]; } public ComplexMatrix(Complex[,] Matrix) { int n = Matrix.GetLength(0); sMatrix = new Complex[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { sMatrix[i, j] = sMatrix[i, j] + Matrix[i, j]; } } } public ComplexMatrix(Complex[][] Matrix, int n) { sMatrix = new Complex[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { sMatrix[i, j] = sMatrix[i, j] + Matrix[i][j]; } } } public Complex[,] sMatrix; public Complex Get(int i, int j) { return this.sMatrix[i, j]; } public int GetRank() { return sMatrix.GetLength(0); } public void Set(int i, int j, Complex Aij) { this.sMatrix[i, j] = new Complex(0, 0) + Aij; } private static ComplexMatrix inv_complex(ComplexMatrix Mat) { int n = Mat.sMatrix.GetLength(0); ComplexMatrix Resultsmat = new ComplexMatrix(Mat.sMatrix); Complex[,] a = Resultsmat.sMatrix; Complex p, q; int i, j, k; for (k = 0; k < n; k++) { p = a[k, k]; a[k, k] = new Complex(1, 0); for (j = 0; j < n; j++) a[k, j] /= p; for (i = 0; i < n; i++) if (i != k) { q = a[i, k]; a[i, k] = new Complex(0, 0); for (j = 0; j < n; j++) a[i, j] -= q * a[k, j]; } } return Resultsmat; } public ComplexMatrix Conjugate() { ComplexMatrix Result = new ComplexMatrix(this.sMatrix.GetLength(0)); for (int i = 0; i < this.sMatrix.GetLength(0); i++) { for (int j = 0; j < this.sMatrix.GetLength(0); j++) { Complex s; s = Complex.Conjugate(sMatrix[i, j]); Result.sMatrix[i, j] = Result.sMatrix[i, j] + s; } } return Result; } public ComplexMatrix Re() { ComplexMatrix Result = new ComplexMatrix(this.sMatrix.GetLength(0)); for (int i = 0; i < this.sMatrix.GetLength(0); i++) { for (int j = 0; j < this.sMatrix.GetLength(0); j++) { Complex s; s = sMatrix[i, j]; Result.sMatrix[i, j] = Result.sMatrix[i, j] + s.Real; } } return Result; } public ComplexMatrix Sqrt() { ComplexMatrix Result = new ComplexMatrix(this.sMatrix.GetLength(0)); for (int i = 0; i < this.sMatrix.GetLength(0); i++) { for (int j = 0; j < this.sMatrix.GetLength(0); j++) { Complex s; s = Complex.Conjugate(sMatrix[i, j]); Result.sMatrix[i, j] = Result.sMatrix[i, j] + Complex.Sqrt(s); } } return Result; } public ComplexMatrix Inv() { ComplexMatrix Results = new ComplexMatrix(this.sMatrix.GetLength(0)); Results = Results + ComplexMatrix.inv_complex(this); return Results; } private static void mul_complex(Complex[,] a, Complex[,] b, Complex[,] c) { int n = a.GetLength(0); Complex s; int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); for (k = 0; k < n; k++) s += a[i, k] * b[k, j]; c[i, j] = s; } } private static void mul_complex(ComplexMatrix Mata, ComplexMatrix Matb, ComplexMatrix Matc) { Complex[,] a = Mata.sMatrix; Complex[,] b = Matb.sMatrix; int n = a.GetLength(0); Matc = new ComplexMatrix(n); //for (int ia = 0; ia < n; ia++) //{ // for (int ja = 0; ja < n; ja++) // { // Matc.sMatrix[ia,ja]=Add(new Complex()); // } //} Complex s; int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); for (k = 0; k < n; k++) s += a[i, k] * b[k, j]; Matc.sMatrix[i, j] = s; } } public static ComplexMatrix operator *(ComplexMatrix Mata, ComplexMatrix Matb) { Complex[,] a = Mata.sMatrix; Complex[,] b = Matb.sMatrix; int n = a.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); for (k = 0; k < n; k++) s += a[i, k] * b[k, j]; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexMatrix operator +(ComplexMatrix Mata, ComplexMatrix Matb) { Complex[,] a = Mata.sMatrix; Complex[,] b = Matb.sMatrix; int n = a.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); s = s + a[i, j] + b[i, j]; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexMatrix operator -(ComplexMatrix Mata, ComplexMatrix Matb) { Complex[,] a = Mata.sMatrix; Complex[,] b = Matb.sMatrix; int n = a.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); s = s + a[i, j] - b[i, j]; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexMatrix operator *(Complex dA, ComplexMatrix Matb) { Complex[,] b = Matb.sMatrix; int n = b.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); s = s + b[i, j] * dA; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexMatrix operator *(ComplexMatrix Matb, Complex dA) { Complex[,] b = Matb.sMatrix; int n = b.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); s = s + b[i, j] * dA; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexVectorH operator *(ComplexVectorH Vect, ComplexMatrix Mata) { int n = Mata.sMatrix.GetLength(0); ComplexVectorH ResultsVect = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); for (int j = 0; j < n; j++) { s = s + Vect.sVectorH[j] * Mata.sMatrix[j, i]; } ResultsVect.sVectorH[i] = s; } return ResultsVect; } public static ComplexVectorV operator *(ComplexMatrix Mata, ComplexVectorV Vect) { int n = Mata.sMatrix.GetLength(0); ComplexVectorV ResultsVect = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); for (int j = 0; j < n; j++) { s = s + Mata.sMatrix[i, j] * Vect.sVectorV[j]; } ResultsVect.sVectorV[i] = s; } return ResultsVect; } public static ComplexMatrix IdentityMatix(int n) { ComplexMatrix Matc = new ComplexMatrix(n); Complex s = new Complex(1, 0); ; int i; for (i = 0; i < n; i++) { Matc.sMatrix[i, i] = s; } return Matc; } public static ComplexMatrix ZeroMatrix(int n) { ComplexMatrix Matc = new ComplexMatrix(n); return Matc; } } public class ComplexVectorV { public Complex[] sVectorV; public ComplexVectorV(int n) { sVectorV = new Complex[n]; } public ComplexVectorV(Complex[] VectorV, int n) { sVectorV = new Complex[n]; for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + VectorV[i]; sVectorV[i] = s; } } public ComplexVectorV(Complex[] LstVectV) { int n = LstVectV.Length; sVectorV = new Complex[n]; for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + LstVectV[i]; sVectorV[i] = s; } } public ComplexVectorH T() { ComplexVectorH sVector = new ComplexVectorH(this.sVectorV.Length); this.sVectorV.CopyTo(sVector.sVectorH, 0); return sVector; } public static ComplexVectorV operator +(ComplexVectorV Vct1, ComplexVectorV Vct2) { Complex[] v1 = Vct1.sVectorV; Complex[] v2 = Vct2.sVectorV; int n = v1.Length; ComplexVectorV Vc = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] + v2[i]; Vc.sVectorV[i] = s; } return Vc; } public static ComplexVectorV operator -(ComplexVectorV Vct1, ComplexVectorV Vct2) { Complex[] v1 = Vct1.sVectorV; Complex[] v2 = Vct2.sVectorV; int n = v1.Length; ComplexVectorV Vc = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] - v2[i]; Vc.sVectorV[i] = s; } return Vc; } public static ComplexVectorV operator *(ComplexVectorV Vct1, Complex dA) { Complex[] v1 = Vct1.sVectorV; int n = v1.Length; ComplexVectorV Vc = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] * dA; Vc.sVectorV[i] = s; } return Vc; } public static ComplexVectorV operator *(Complex dA, ComplexVectorV Vct1) { Complex[] v1 = Vct1.sVectorV; int n = v1.Length; ComplexVectorV Vc = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] * dA; Vc.sVectorV[i] = s; } return Vc; } public static Complex operator *(ComplexVectorV VectorV, ComplexVectorH VectorH) { Complex s = new Complex(0, 0); int n = VectorV.sVectorV.Length; for (int i = 0; i < n; i++) { s = s + VectorV.sVectorV[i] * VectorH.sVectorH[i]; } return s; } public static Complex operator *(ComplexVectorH VectorH, ComplexVectorV VectorV) { Complex s = new Complex(0, 0); int n = VectorV.sVectorV.Length; for (int i = 0; i < n; i++) { s = s + VectorV.sVectorV[i] * VectorH.sVectorH[i]; } return s; } } public class ComplexVectorH { public Complex[] sVectorH; public ComplexVectorH(int n) { sVectorH = new Complex[n]; } public ComplexVectorH(Complex[] VectorH, int n) { sVectorH = new Complex[n]; for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + VectorH[i]; sVectorH[i] = s; } } public ComplexVectorH(Complex[] LstVectH) { int n = LstVectH.Length; sVectorH = new Complex[n]; for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + LstVectH[i]; sVectorH[i] = s; } } public ComplexVectorV T() { int n = this.sVectorH.Length; ComplexVectorV sVectorV = new ComplexVectorV(n); this.sVectorH.CopyTo(sVectorV.sVectorV, 0); return sVectorV; } public static ComplexVectorH operator +(ComplexVectorH Vct1, ComplexVectorH Vct2) { Complex[] v1 = Vct1.sVectorH; Complex[] v2 = Vct2.sVectorH; int n = v1.Length; ComplexVectorH Vc = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] + v2[i]; Vc.sVectorH[i] = s; } return Vc; } public static ComplexVectorH operator -(ComplexVectorH Vct1, ComplexVectorH Vct2) { Complex[] v1 = Vct1.sVectorH; Complex[] v2 = Vct2.sVectorH; int n = v1.Length; ComplexVectorH Vc = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] - v2[i]; Vc.sVectorH[i] = s; } return Vc; } public static ComplexVectorH operator *(ComplexVectorH Vct1, Complex dA) { Complex[] v1 = Vct1.sVectorH; int n = v1.Length; ComplexVectorH Vc = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] * dA; Vc.sVectorH[i] = s; } return Vc; } public static ComplexVectorH operator *(Complex dA, ComplexVectorH Vct1) { Complex[] v1 = Vct1.sVectorH; int n = v1.Length; ComplexVectorH Vc = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] * dA; Vc.sVectorH[i] = s; } return Vc; } } }
いろいろなサイトからコードコピペしているので、引用先の方ごめんなさい。