有理数系的线性代数运算程序(1)有理数类

有理数系的线性代数运算程序(1)有理数类

线性代数主要涉及矩阵的运算,尤其是矩阵中行/列变换,矩阵的秩、行列式、线性方程组、特征值等问题。在开始设计之前,先完成有理数系的运算。我们要将分数形式的数字展现成分数而不是近似小数。

首先添加一个类RationalNumber,表示一个有理数,如果它是整数,则分母为1,如果它是小数,就保存成最简分数。如果是负有理数,保证负号在分子上。这样定义后重载四则运算和判等运算的运算符。

class RationalNumber
    {
        private int _num, _den;
        private int gcd(int x,int y)
        {
            if(x%y==0)
            {
                return y;
            }
            else
            {
                return gcd(y, x % y);
            }
        }
        public RationalNumber()
        {
            _num = 0;
            _den = 1;
        }
        public RationalNumber(int x)
        {
            _num = x;
            _den = 1;
        }
        public RationalNumber(int nn,int dd)
        {
            int sign = 1;
            int g = gcd(Math.Abs(nn), Math.Abs(dd));
            if(nn*dd<0)
            {
                sign = -1;
            }
            _num = sign*Math.Abs(nn)/g;
            _den = Math.Abs(dd)/g;
        }
        public string ToLaTeX()
        {
            if (_den == 1)
            {
                return _num.ToString();
            }
            else
            {
                if (_num > 0)
                {
                    return String.Format("\\frac{{{0}}}{{{1}}}", _num, _den);
                }
                else
                {
                    return String.Format("-\\frac{{{0}}}{{{1}}}", -_num, _den);
                }
            }
        }
        public override string ToString()
        {
            return String.Format("{0}/{1}", _num, _den);
        }
        public static RationalNumber operator+(RationalNumber self,RationalNumber o)
        {
            return new RationalNumber(self._num * o._den + self._den * o._num, self._den * o._den);
        }
        public static RationalNumber operator -(RationalNumber self, RationalNumber o)
        {
            return new RationalNumber(self._num * o._den - self._den * o._num, self._den * o._den);
        }
        public static RationalNumber operator *(RationalNumber self, RationalNumber o)
        {
            return new RationalNumber(self._num * o._num, self._den * o._den);
        }
        public static RationalNumber operator /(RationalNumber self, RationalNumber o)
        {
            return new RationalNumber(self._num * o._den, self._den * o._num);
        }
        public static RationalNumber operator /( int o, RationalNumber self)
        {
            return new RationalNumber( self._den * o, self._num);
        }
        public static RationalNumber operator /(RationalNumber self, int o)
        {
            return new RationalNumber(self._num , self._den *o );
        }
        public static bool operator ==(RationalNumber self, RationalNumber o)
        {
            return self._num == o._num && self._den == o._den;
        }
        public static bool operator !=(RationalNumber self, RationalNumber o)
        {
            return !(self._num == o._num && self._den == o._den);
        }
        public static bool operator ==(RationalNumber self, int o)
        {
            return self._den == 1 && self._num == o;
        }
        public static bool operator !=(RationalNumber self, int o)
        {
            return (self._den != 1  || (self._den ==1 && self._num != o));
        }
        public override bool Equals(Object o)
        {
            return this is RationalNumber &&  this == (RationalNumber)o;
        }
        public override int GetHashCode()
        {
            return Convert.ToInt32(String.Format("{0}{1}",_num , _den));
        }
    }

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注