public static SquareMatrix Inverse(SquareMatrix A) { int N = A.x.GetLength(0); SquareMatrix Acp = new SquareMatrix(A); SquareMatrix Ainv = Identity(N); double tmp; int c; for (int i = 0; i < N; i++) { c = i; for (int k = i + 1; k < N; k++) { if (Math.Abs(Acp.x[c, i]) < Math.Abs(Acp.x[k, i])) c = k; } if (c != i) { for (int j = 0; j < N; j++) { tmp = Acp.x[c, j]; Acp.x[c, j] = Acp.x[i, j]; Acp.x[i, j] = tmp; tmp = Ainv.x[c, j]; Ainv.x[c, j] = Ainv.x[i, j]; Ainv.x[i, j] = tmp; } } tmp = Acp.x[i, i]; for (int j = 0; j < N; j++) { Acp.x[i, j] /= tmp; Ainv.x[i, j] /= tmp; } for (int k = 0; k < N; k++) { if (k == i) continue; tmp = Acp.x[k, i]; for (int j = 0; j < N; j++) { Acp.x[k, j] -= tmp * Acp.x[i, j]; Ainv.x[k, j] -= tmp * Ainv.x[i, j]; } } } return Ainv; }
public static double Det(SquareMatrix A) { int N = A.x.GetLength(0); SquareMatrix Acp = new SquareMatrix(A); double det = 1.0; double tmp; int c; for (int i = 0; i < N; i++) { c = i; for (int k = i + 1; k < N; k++) { if (Math.Abs(Acp.x[c, i]) < Math.Abs(Acp.x[k, i])) c = k; } if (c != i) { for (int j = 0; j < N; j++) { tmp = Acp.x[c, j]; Acp.x[c, j] = Acp.x[i, j]; Acp.x[i, j] = tmp; } det = -det; } tmp = Acp.x[i, i]; for (int j = i; j < N; j++) { Acp.x[i, j] /= tmp; } det *= tmp; for (int k = i + 1; k < N; k++) { tmp = Acp.x[k, i]; for (int j = i; j < N; j++) { Acp.x[k, j] -= tmp * Acp.x[i, j]; } } } return det; }
public SquareMatrix(SquareMatrix A) { this.x = (double[,])A.x.Clone(); }
public static double Tr(SquareMatrix A) { int N = A.x.GetLength(0); double tr = 0.0; for (int k = 0; k < N; k++) { tr += A.x[k, k]; } return tr; }
public static SquareMatrix Pow(SquareMatrix A, int n) { SquareMatrix pow = Identity(n); for (int k = 1; k <= Math.Abs(n); k++) { pow *= A; } if (n < 0) pow = Inverse(pow); return pow; }
static void Main(string[] args) { Complex I = Complex.I; Complex z1 = 1+I; Complex z2 = -1+2*I; Complex z3 = z1/z2; Complex z4 = Complex.Exp(I*Math.PI/2); Console.WriteLine("Complex"); Console.WriteLine((-z1).ToString()); Console.WriteLine(Complex.Arg(-I)); double[,] x = { {2, 2, 1}, {3, 0, 2}, {4, 3, 2} }; Console.WriteLine("\n@A"); SquareMatrix A = new SquareMatrix(x); Console.WriteLine(A.ToString()); SquareMatrix Ainv = SquareMatrix.Inverse(A); Console.WriteLine(SquareMatrix.Det(A)); Console.WriteLine(Ainv.ToString()); double[,] y = { {1, 2, 1}, {2, 1, 0}, {1, 1, 2} }; Console.WriteLine("\n@B"); SquareMatrix B = new SquareMatrix(y); Console.WriteLine(B.ToString()); SquareMatrix Binv = SquareMatrix.Inverse(B); Console.WriteLine(SquareMatrix.Det(B)); Console.WriteLine(Binv.ToString("f1")); double[] a = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; double[] b = { 2.0, 3.5, 5.0, 4.0, 5.5, 7.0}; Console.WriteLine("\nStatistics"); Console.WriteLine(Statistics.Average(a) + ", " + Statistics.Average(b)); Console.WriteLine(Statistics.Variance(a) + ", " + Statistics.Variance(b)); Console.WriteLine(Statistics.UnbiasedVariance(a) + ", " + Statistics.UnbiasedVariance(b)); Console.WriteLine(Statistics.Covariance(a, b)); Console.WriteLine(Statistics.Corr(a, b)); Console.WriteLine(Statistics.Erf(1.0)); /* Console.WriteLine("\nAnalysis"); const int N = 256; double[] f1 = new double[N]; for (int n = 0; n < N; n++) { f1[n] = n <= N / 2 ? -1.0 + 4.0 * n / N : 3.0 - 4.0 * n / N; } Complex[] c1 = Analysis.DFT(f1); Complex[] c2 = Analysis.FFT(f1); for (int n = 0; n < N; n++) { Console.WriteLine(Complex.Re(c1[n]).ToString("f4") + ", " + Complex.Re(c2[n]).ToString("f4")); } */ Console.WriteLine("\nSequences"); Console.WriteLine(Sequences.Fac(6)); Console.WriteLine(Sequences.Fac(4)); Console.WriteLine(Sequences.Fac(7)); Console.WriteLine(Sequences.DFac(3)); Console.WriteLine(Sequences.DFac(8)); Console.WriteLine(Sequences.DFac(5)); Console.WriteLine(Sequences.Fibonacci(8)); Console.WriteLine(Sequences.Fibonacci(4)); Console.WriteLine(Sequences.Fibonacci(10)); Console.WriteLine(Sequences.C(1, 0)); Console.WriteLine(Sequences.C(2, 0)); Console.WriteLine(Sequences.C(2, 1)); Console.WriteLine(Sequences.C(3, 3)); Console.WriteLine(Sequences.C(4, 2)); Console.WriteLine(Sequences.Catalan(1)); Console.WriteLine(Sequences.Catalan(4)); Console.WriteLine(Sequences.Catalan(7)); GraphForm gph = new GraphForm(); double[] x1 = new double[501]; double[] y1 = new double[501]; double t; for (int n = 0; n <= 500; n++) { t = Math.PI * n / 125.0; x1[n] = 6.0*Math.Cos(3*t); y1[n] = 6.0*Math.Sin(4*t); } gph.DrawCurve2D(x1, y1); // Application.Run(gph); Console.ReadKey(); }