C#-分数矩阵类(visual studio 2019)

分数类

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) n = t;
			nume = numerator / t;
			deno = denominator / t;
		}
		public static implicit operator Rational(int numerator)
		{
			return new Rational(numerator);
		}
		public static implicit operator double(Rational a)
		{
			return (double)a.nume / a.deno;
		}
		public override string ToString()
		{
			return string.Format(deno == 1 ? "{0}" : "{0}/{1}", nume, deno);
		}
		public static Rational operator +(Rational a, Rational b)
		{
			return new Rational(a.nume * b.deno + b.nume * a.deno, a.deno * b.deno);
		}
		public static Rational operator -(Rational a, Rational b)
		{
			return new Rational(a.nume * b.deno - b.nume * a.deno, a.deno * b.deno);
		}
		public static Rational operator *(Rational a, Rational b)
		{
			return new Rational(a.nume * b.nume, a.deno * b.deno);
		}
		public static Rational operator /(Rational a, Rational b)
		{
			return new Rational(a.nume * b.deno, a.deno * b.nume);
		}
		public static bool operator ==(Rational a, Rational b)
		{
			if (a.nume == b.nume && a.deno == b.deno)
				return true;
			else
				return false;
		}
		public static bool operator !=(Rational a, Rational b)
		{
			if (a.nume != b.nume || a.deno != b.deno)
				return true;
			else
				return false;
		}
		public override bool Equals(object obj) { return (this == (Rational)obj); }
		public override int GetHashCode() { return base.GetHashCode(); }
	}
}

矩阵类

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MathLib
{
    public class Matrix<T>
    {
        static readonly Func<T, T> abs = (T a) => (dynamic)a > 0 ? a : (dynamic)a * -1;
        public T[,] Data;
        public Matrix(T[,] data)
        {
            Debug.Assert(data != null);
            Data = data;
        }
        public static Matrix<T> I(int n)
        {
            var res = new T[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);
            return new Matrix<T>(res);
        }
        public override string ToString()
        {
            return ToString("{0,6}");
        }
        public string ToString(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;
        }
        public static 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))
            {
                throw new Exception("矩阵不相同,无法进行加法运算");
            }
            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;
        }
        public static 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;
        }
        public static 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;
        }
        public static Matrix<T> operator *(T factor, Matrix<T> M)
        {
            return M * factor;
        }
        public static 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 = new T[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];
                }
            }
            return new Matrix<T>(list);
        }
        public static Matrix<T> operator ~(Matrix<T> M)
        {
            int row = M.Data.GetLength(1);
            int col = M.Data.GetLength(0);
            T[,] list = new T[row, col];
            for (int i = 0; i < row; i++)
                for (int j = 0; j < col; j++)
                {
                    list[i, j] = M.Data[j, i];
                }
            return new Matrix<T>(list);
        }
        public static Matrix<T> operator /(Matrix<T> M1, Matrix<T> M2)
        {
            return M1 * invert(M2);
        }
        public static bool operator ==(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])
                            return true;
            }
            else
                return false;
            return true;
        }
        public static bool operator !=(Matrix<T> M1, Matrix<T> M2)
        {
            if (M1 != M2)
                return true;
            else
                return false;
        }
        private static Matrix<T> invert(Matrix<T> M)
        {
            if (M.Data.GetLength(0) != M.Data.GetLength(1))
            {
                throw new Exception("erro!");
            }
            int row = M.Data.GetLength(0);
            int col = M.Data.GetLength(1);
            T[,] list1 = new T[2 * row + 1, 2 * col + 1];
            T[,] list = new T[2 * row + 1, 2 * col + 1];
            T[,] list2 = new T[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];
                }
            }
            return new Matrix<T>(list2); ;
        }
        public override bool Equals(object obj) { return (this == (Matrix<T>)obj); }
        public override int GetHashCode() { return base.GetHashCode(); }
    }
}

主类

using System;

namespace MathLib
{
    public class Test
    {
        static void Main(string[] args)
        {
            Console.WriteLine("========== Rational Test ==========");
            var fa = new Rational(3, 12);
            var fb = new Rational(4, 6);
            var fc = new Rational(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 = new Matrix<double>(new double[,] {
                { 1, 2, 3, 4, 5 },
                { 2, 3, 4, 5, 6 },
                { 3, 4, 5, 6, 7 }
            });
            var b = new Matrix<double>(new double[,] {
                { 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 = new Matrix<Rational>(new Rational[,] {
                        { new Rational(1), new Rational(1, 2), new Rational(1, 5) },
                       { new Rational(1, 4), new Rational(1, 3), new Rational(1, 6) },
                    { new Rational(1, 9), new Rational(1, 8), new Rational(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();
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值