C# 实数矩阵行列式计算

原文链接: C# 实数矩阵行列式计算

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7namespace MatrixHandler { 8 class Num { 9 int numerator = 0; 10 int denominator = 1; 11 12 13 public static Num operator *(Num n1, Num n2) { 14 15 return new Num(n1.Numerator * n2.Numerator, n1.Denominator * n2.Denominator); 16 } 17 public static Num operator /(Num n1, Num n2) { 18 if (n1.Numerator < 0 && n2.Numerator < 0) 19 return new Num(-n1.Numerator * n2.Denominator, n1.Denominator * -n2.Numerator); 20 return new Num(n1.Numerator * n2.Denominator, n1.Denominator * n2.Numerator); 21 } 22 23 public static Num operator +(Num n1, Num n2) { 24 return new Num(n1.Numerator * n2.Denominator + n2.Numerator * n1.Denominator, n1.Denominator * n2.Denominator); 25 } 26 27 public static Num operator -(Num n1, Num n2) { 28 return new Num(n1.Numerator * n2.Denominator - n2.Numerator * n1.Denominator, n1.Denominator * n2.Denominator); 29 } 30 public static Num operator -(Num n) { 31 return new Num(-n.Numerator, n.Denominator); 32 } 33 public Num getCopy() { 34 return new Num(Numerator, Denominator); 35 } 36 public int Numerator { 37 get { 38 return numerator; 39 } 40 41 set { 42 numerator = value; 43 } 44 } 45 46 public int Denominator { 47 get { 48 return denominator; 49 } 50 51 set { 52 denominator = value; 53 } 54 } 55 56 public Num(int numerator, int denominator) { 57 this.Numerator = numerator; 58 this.Denominator = denominator; 59 reduce(); 60 } 61 public Num() { } 62 public Num(string s) { 63 64 if (s.IndexOf('.') != -1) { 65 string[] str = s.Split('.'); 66 Numerator = Convert.ToInt32(str[0] + str[1]); 67 Denominator = tenPow(str[1].Length); 68 } else if (s.IndexOf('/') != -1) { 69 string[] str = s.Split('/'); 70 Numerator = Convert.ToInt32(str[0]); 71 Denominator = Convert.ToInt32(str[1]); 72 } else { 73 Numerator = Convert.ToInt32(s); 74 } 75 76 reduce(); 77 } 78 public Num(int val) { 79 Denominator = 1; 80 Numerator = val; 81 } 82 public override string ToString() { 83 return denominator == 1 ? Numerator + "" : Numerator + "/" + Denominator; 84 } 85 86 87 public int gcd(int a, int b) { 88 int m = Math.Abs(a); 89 int n = Math.Abs(b); 90 int t; 91 while (m != 0) { 92 t = n % m; 93 n = m; 94 m = t; 95 } 96 return n; 97 } 98 99 private void reduce() { 100 int t = gcd(Numerator, Denominator); 101 Denominator /= t; 102 Numerator /= t; 103 if (Numerator == 0) 104 Denominator = 1; 105 106 if (Numerator > 0 != Denominator > 0) { 107 Numerator = -Math.Abs(Numerator); 108 Denominator = Math.Abs(Denominator); 109 } 110 } 111 112 private int tenPow(int n) { 113 int res = 1; 114 while (n != 0) { 115 res *= 10; 116 --n; 117 } 118 return res; 119 } 120 } 121}

矩阵类

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6namespace MatrixHandler { 7 class Matrix { 8 Num[,] nums; 9 int n; 10 11 internal Num[,] Nums { 12 get { 13 return nums; 14 } 15 16 set { 17 nums = value; 18 } 19 } 20 21 public int N { 22 get { 23 return n; 24 } 25 26 set { 27 n = value; 28 } 29 } 30 31 public Matrix(Num[,] nums,int n) { 32 this.Nums = nums; 33 this.N = n; 34 } 35 36 37 38 //余子式 39 public Matrix getM(int x,int y) { 40 if (N < 2) return null; 41 Num[,] temp = new Num[N - 1, N - 1]; 42 for(int i = 0; i < N; ++i) { 43 for(int j = 0; j < N; ++j) { 44 if (i < x && j < y) { temp[i, j] = Nums[i, j].getCopy(); } 45 if(i<x && j > y) { temp[i, j-1] = Nums[i, j].getCopy(); } 46 if(i>x && j < y) { temp[i - 1, j] = Nums[i, j].getCopy(); } 47 if(i>x && j > y) { temp[i - 1, j - 1] = Nums[i, j].getCopy(); } 48 } 49 } 50 return new Matrix(temp, N - 1); 51 } 52 53 public Num getVal() { 54 if (N == 1) 55 return Nums[0, 0]; 56 if (N == 2) 57 return Nums[0, 0] * Nums[1, 1] - Nums[0, 1] * Nums[1, 0]; 58 Num res = new Num(0); 59 int t = 1; 60 for(int i = 0; i < N; ++i) { 61 res = res + nums[0,i] * (new Num(t)) * (getM(0, i).getVal().getCopy()); 62 t *= -1; 63 } 64 65 return res; 66 } 67 68 public Matrix getInverse() { 69 Num val = getVal().getCopy(); 70 71 Num[,] temp = new Num[N, N]; 72 Num[,] com = getCompany().Nums; 73 for (int i = 0; i < N; ++i) { 74 for(int j = 0; j < N; ++j) { 75 temp[i,j] = (com[i, j]/val).getCopy(); 76 } 77 } 78 Matrix res = new Matrix(temp,n); 79 return res; 80 } 81 82 public Matrix getCompany() { 83 Num[,] temp = new Num[N,N]; 84 for(int i = 0; i < N; ++i) { 85 for(int j = 0; j < N; ++j) { 86 int t = (i + j) % 2 == 0 ? 1 : -1; 87 temp[j, i] = ( this.getM(i,j).getVal() * (new Num(t))).getCopy(); 88 } 89 } 90 Matrix res=new Matrix(temp,N); 91 return res; 92 } 93 public override string ToString() { 94 string res=""; 95 int x=0, y=0; 96 for (x = 0; x < N; ++x) { 97 for (y = 0; y < N; ++y) { 98 res += Nums[x, y]; 99 if (y == N - 1) res += "\n"; 100 else res += "\t"; 101 } 102 103 } 104 return res; 105 } 106 } 107} 108 109 110using System; 111using System.Collections.Generic; 112using System.Linq; 113using System.Text; 114using System.Threading.Tasks; 115 116namespace MatrixHandler { 117 class Program { 118 119 delegate void Handler(); 120 static void Main(string[] args) { 121 while (true) { 122 mainView(); 123 } 124 } 125 126 public static void mainView(){ 127 Console.WriteLine("实矩阵行列式计算,请选择功能:\n1,行列式求值\t2,矩阵求逆\t3,矩阵求伴随"); 128 try { 129 int flag = Convert.ToInt32(Console.ReadLine()); 130 Handler[] h = new Handler[3]; 131 h[0] = new Handler(getVal); 132 h[1] = new Handler(getInverse); 133 h[2] = new Handler(getCompany); 134 Console.WriteLine("请输入矩阵"); 135 h[flag - 1].Invoke(); 136 137 } catch (Exception) { 138 Console.WriteLine("格式错误!数字之间只能输入一个空格!且本程序只能处理方阵."); 139 } 140 141 142 } 143 144 public static void getVal() { 145 Matrix m = getMatrix(); 146 Console.WriteLine("输入的行列式为:\n"+m+"该行列式的值为:"+m.getVal()); 147 } 148 149 public static void getInverse() { 150 Matrix m = getMatrix(); 151 if (m.getVal().Numerator == 0) 152 Console.WriteLine("该矩阵不可逆!"); 153 else 154 Console.WriteLine("输入的矩阵为:\n" + m + "该矩阵的逆矩阵为:\n" + m.getInverse()); 155 } 156 157 public static void getCompany() { 158 Matrix m = getMatrix(); 159 Console.WriteLine("输入的矩阵为:\n" + m + "该矩阵的伴随矩阵为:\n" + m.getCompany()); 160 } 161 162 public static Matrix getMatrix() { 163 string s = Console.ReadLine(); 164 string[] row = s.Split(' '); 165 int n = row.Length; 166 Num[,] nums = new Num[n,n]; 167 for (int i = 0; i < n; ++i) 168 nums[0,i] = new Num(row[i]).getCopy(); 169 170 for(int i = 1; i < n; ++i) { 171 string st = Console.ReadLine(); 172 string[] rowt = st.Split(' '); 173 for(int j=0;j<n;++j) 174 nums[i, j] = new Num(rowt[j]).getCopy(); 175 } 176 177 return new Matrix(nums, n); 178 } 179 180 } 181}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Python之time模块的时间戳、时间字符串格式化与转换

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y