using System;using System.Diagnostics;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MathLib
{publicclassMatrix<T>{staticreadonly Func<T, T> abs =(T a)=>(dynamic)a >0? a :(dynamic)a *-1;public T[,] Data;publicMatrix(T[,] data){
Debug.Assert(data !=null);
Data = data;}publicstatic Matrix<T>I(int n){var res =newT[n, n];for(int row =0; row < n; row++)for(int col =0; col < n; col++)
res[row, col]=(T)(dynamic)(row == col ?1:0);returnnewMatrix<T>(res);}publicoverridestringToString(){returnToString("{0,6}");}publicstringToString(string format){string list ="";int row = Data.GetLength(0);int col = Data.GetLength(1);for(int i =0; i < row; i++){
list +="[";for(int K =0; K < col; K++){
list +=string.Format(format, Data[i, K]);if(K != col -1)
list +=string.Format(format," ,", Data[i, K]);}
list +="]\n";}return list;}publicstatic Matrix<T>operator+(Matrix<T> M1, Matrix<T> M2){if(M1.Data.GetLength(0)!= M2.Data.GetLength(0)&& M2.Data.GetLength(1)!= M1.Data.GetLength(1)){thrownewException("矩阵不相同,无法进行加法运算");}else{int row = M1.Data.GetLength(0);int col = M1.Data.GetLength(1);for(int i =0; i < row; i++)for(int j =0; j < col; j++)
M1.Data[i, j]=(dynamic)M1.Data[i, j]+(dynamic)M2.Data[i, j];}return M1;}publicstatic Matrix<T>operator-(Matrix<T> M1){int row = M1.Data.GetLength(0);int col = M1.Data.GetLength(1);for(int i =0; i < row; i++)for(int j =0; j < col; j++)
M1.Data[i, j]=(dynamic)M1.Data[i, j]-(dynamic)M1.Data[i, j];return M1;}publicstatic Matrix<T>operator*(Matrix<T> M,T factor){int row = M.Data.GetLength(0);int col = M.Data.GetLength(1);for(int i =0; i < row; i++)for(int j =0; j < col; j++)
M.Data[i, j]=(dynamic)M.Data[i, j]* factor;return M;}publicstatic Matrix<T>operator*(T factor, Matrix<T> M){return M * factor;}publicstatic Matrix<T>operator*(Matrix<T> M1, Matrix<T> M2){
Debug.Assert(M1.Data.GetLength(1)== M2.Data.GetLength(0));int row = M1.Data.GetLength(0);int col = M2.Data.GetLength(1);
T[,] list =newT[row, col];int n = M2.Data.GetLength(0);for(int i =0; i < row; i++){for(int j =0; j < col; j++){
list[i, j]=(dynamic)M1.Data[i,0]*(dynamic)M2.Data[0, j];for(int k =1; k < n; k++)
list[i, j]+=(dynamic)M1.Data[i, k]*(dynamic)M2.Data[k, j];}}returnnewMatrix<T>(list);}publicstatic Matrix<T>operator~(Matrix<T> M){int row = M.Data.GetLength(1);int col = M.Data.GetLength(0);
T[,] list =newT[row, col];for(int i =0; i < row; i++)for(int j =0; j < col; j++){
list[i, j]= M.Data[j, i];}returnnewMatrix<T>(list);}publicstatic Matrix<T>operator/(Matrix<T> M1, Matrix<T> M2){return M1 *invert(M2);}publicstaticbooloperator==(Matrix<T> M1, Matrix<T> M2){int row = M1.Data.GetLength(0);int col = M1.Data.GetLength(1);if(M1.Data.GetLength(0)== M2.Data.GetLength(0)&& M1.Data.GetLength(1)== M2.Data.GetLength(1)){for(int i =0; i < row; i++)for(int j =0; j < col; j++)if((dynamic)M1.Data[i, j]==(dynamic)M2.Data[i, j])returntrue;}elsereturnfalse;returntrue;}publicstaticbooloperator!=(Matrix<T> M1, Matrix<T> M2){if(M1 != M2)returntrue;elsereturnfalse;}privatestatic Matrix<T>invert(Matrix<T> M){if(M.Data.GetLength(0)!= M.Data.GetLength(1)){thrownewException("erro!");}int row = M.Data.GetLength(0);int col = M.Data.GetLength(1);
T[,] list1 =newT[2* row +1,2* col +1];
T[,] list =newT[2* row +1,2* col +1];
T[,] list2 =newT[row, col];I(row);for(int i =0; i <2* row +1; i++){for(int j =0; j <2* col +1; j++)
list[i, j]=(dynamic)0;}for(int i =0; i < row; i++)for(int j =0; j < col; j++)
list[i, j]= M.Data[i, j];for(int k =0; k < row; k++){for(int t = col; t <=2* col; t++){if((t - k)== row){
list[k, t]=(dynamic)1;}else{
list[k, t]=(dynamic)0;}}}for(int k =0; k < row; k++){if(list[k, k]!=(dynamic)1){
list1[k, k]= list[k, k];
list[k, k]=(dynamic)1;for(int p = k +1; p <2* col; p++){
list[k, p]=(dynamic)list[k, p]/(dynamic)list1[k, k];}}for(int q =0; q < row; q++){if(q != k){
list1[q, k]= list[q, k];for(int p =0; p <2* col; p++){
list[q, p]= list[q, p]-(dynamic)list1[q, k]* list[k, p];}}else{continue;}}}for(int x =0; x < row; x++){for(int y = col; y <2* col; y++){
list2[x, y - col]= list[x, y];}}returnnewMatrix<T>(list2);;}publicoverrideboolEquals(object obj){return(this==(Matrix<T>)obj);}publicoverrideintGetHashCode(){returnbase.GetHashCode();}}}
主类
using System;namespace MathLib
{publicclassTest{staticvoidMain(string[] args){
Console.WriteLine("========== Rational Test ==========");var fa =newRational(3,12);var fb =newRational(4,6);var fc =newRational(6,9);
Console.WriteLine("{0} + {1} - {2} = {3}", fa, fb, fc, fa + fb - fc);
Console.WriteLine("{0} {2} {1}", fa,.25, fa ==0.25?"==":"!=");
Console.WriteLine("{0} {2} {1}", fb, fc,(fb == fc ?"==":"!="));
Console.WriteLine("========== Matrix<double> Test ==========");var a =newMatrix<double>(newdouble[,]{{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}});var b =newMatrix<double>(newdouble[,]{{17,24,01,08,15},{23,05,07,14,16},{04,06,13,20,22},{10,12,19,21,03},{11,18,25,02,09}});var c = Matrix<double>.I(5)/ b;
Console.WriteLine("∵ a = \n{0}", a);
Console.WriteLine("∴ ~a = \n{0}",~a);
Console.WriteLine("∵ b = \n{0}", b);
Console.WriteLine("∴ a * b = \n{0}", a * b);
Console.WriteLine("∵ c = 1/b =\n{0}", c.ToString("{0,9:f6}"));
Console.WriteLine("∴ b * c =\n{0}",(b * c).ToString("{0,5:f2}"));
Console.WriteLine("? b * c {0} I\n", b * c == Matrix<double>.I(5)?"!=":"==");
Console.WriteLine("========== Matrix<Rational> Test ==========");var e =newMatrix<Rational>(newRational[,]{{newRational(1),newRational(1,2),newRational(1,5)},{newRational(1,4),newRational(1,3),newRational(1,6)},{newRational(1,9),newRational(1,8),newRational(1,303)}// 将最后数字7,替换为学号后三位数。});var f = Matrix<Rational>.I(3)/ e;
Console.WriteLine("∵ e =\n{0}", e);
Console.WriteLine("∴ 60 * ~e =\n{0}",60*~e);
Console.WriteLine("∵ f = 1/e =\n{0}", f.ToString("{0,12}"));
Console.WriteLine("∴ e * f {0} I", e * f == Matrix<Rational>.I(3)?"==":"!=");
Console.ReadKey();}}}
分数类using System;namespace MathLib{ public class Rational { private int nume = 0, deno = 1; public Rational(int numerator = 0, int denominator = 1) { int n = Math.Abs(numerator), d = Math.Abs(denominator), t; while ((d = n % (t = d)) != 0)