在C#中使用泛型创建数学库

时间:2020-03-05 18:53:39  来源:igfitidea点击:

是否有使用泛型创建不依赖于选择用于存储数据的基本类型的数学库的可行方法?

换句话说,假设我要编写一个Fraction类。分数可以用两个整数或者两个双精度数或者诸如此类的值表示。重要的是基本的四个算术运算都已定义好。因此,我希望能够编写Fraction <int> frac = new Fraction <int>(1,2)和/或者Fraction <double> frac = new Fraction <double>(0.1,1.0)

不幸的是,没有接口代表四个基本操作(+,-,*,/)。有没有人找到可行的,可行的方法来实现这一目标?

解决方案

回答

首先,类应将泛型参数限制为基元(公共类Fraction其中T:struct,new())。

其次,我们可能需要创建隐式转换重载,以便我们可以处理从一种类型到另一种类型的转换,而无需编译器哭泣。

第三,当合并不同类型的分数时,我们也可以使四个基本运算符重载,以使接口更加灵活。

最后,我们必须考虑如何处理上溢和下溢。一个好的库在处理溢出方面将是非常明确的。否则,我们将无法相信不同分数类型的运算结果。

回答

我相信这可以回答问题:

http://www.codeproject.com/KB/cs/genericnumerics.aspx

回答

这是一种相对简单的抽象操作员的方法。

abstract class MathProvider<T>
    {
        public abstract T Divide(T a, T b);
        public abstract T Multiply(T a, T b);
        public abstract T Add(T a, T b);
        public abstract T Negate(T a);
        public virtual T Subtract(T a, T b)
        {
            return Add(a, Negate(b));
        }
    }

    class DoubleMathProvider : MathProvider<double>
    {
        public override double Divide(double a, double b)
        {
            return a / b;
        }

        public override double Multiply(double a, double b)
        {
            return a * b;
        }

        public override double Add(double a, double b)
        {
            return a + b;
        }

        public override double Negate(double a)
        {
            return -a;
        }
    }

    class IntMathProvider : MathProvider<int>
    {
        public override int Divide(int a, int b)
        {
            return a / b;
        }

        public override int Multiply(int a, int b)
        {
            return a * b;
        }

        public override int Add(int a, int b)
        {
            return a + b;
        }

        public override int Negate(int a)
        {
            return -a;
        }
    }

    class Fraction<T>
    {
        static MathProvider<T> _math;
        // Notice this is a type constructor.  It gets run the first time a
        // variable of a specific type is declared for use.
        // Having _math static reduces overhead.
        static Fraction()
        {
            // This part of the code might be cleaner by once
            // using reflection and finding all the implementors of
            // MathProvider and assigning the instance by the one that
            // matches T.
            if (typeof(T) == typeof(double))
                _math = new DoubleMathProvider() as MathProvider<T>;
            else if (typeof(T) == typeof(int))
                _math = new IntMathProvider() as MathProvider<T>;
            // ... assign other options here.

            if (_math == null)
                throw new InvalidOperationException(
                    "Type " + typeof(T).ToString() + " is not supported by Fraction.");
        }

        // Immutable impementations are better.
        public T Numerator { get; private set; }
        public T Denominator { get; private set; }

        public Fraction(T numerator, T denominator)
        {
            // We would want this to be reduced to simpilest terms.
            // For that we would need GCD, abs, and remainder operations
            // defined for each math provider.
            Numerator = numerator;
            Denominator = denominator;
        }

        public static Fraction<T> operator +(Fraction<T> a, Fraction<T> b)
        {
            return new Fraction<T>(
                _math.Add(
                  _math.Multiply(a.Numerator, b.Denominator),
                  _math.Multiply(b.Numerator, a.Denominator)),
                _math.Multiply(a.Denominator, b.Denominator));
        }

        public static Fraction<T> operator -(Fraction<T> a, Fraction<T> b)
        {
            return new Fraction<T>(
                _math.Subtract(
                  _math.Multiply(a.Numerator, b.Denominator),
                  _math.Multiply(b.Numerator, a.Denominator)),
                _math.Multiply(a.Denominator, b.Denominator));
        }

        public static Fraction<T> operator /(Fraction<T> a, Fraction<T> b)
        {
            return new Fraction<T>(
                _math.Multiply(a.Numerator, b.Denominator),
                _math.Multiply(a.Denominator, b.Numerator));
        }

        // ... other operators would follow.
    }

如果无法实现所使用的类型,则将在运行时而不是编译时失败(这很糟糕)。 " MathProvider <T>"实现的定义始终是相同的(也很糟糕)。我建议我们只是避免在Cand使用中执行此操作,因为某些其他语言更适合此抽象级别。

编辑:固定的加和减定义为"分数<T>"。
另一个有趣且简单的事情是实现在抽象语法树上运行的MathProvider。这个想法立即指向做诸如自动区分的事情:http://conal.net/papers/beautiful-differentiation/

回答

这是泛型类型附带的一个细微问题。假设算法涉及除法,例如说是高斯消元法,则可以求解方程组。如果我们输入整数,将会得到错误的答案,因为我们将执行整数除法。但是,如果我们传入的双参数发生时具有整数值,那么我们将得到正确的答案。

与Cholesky分解一样,平方根也会发生相同的情况。分解整数矩阵会出错,而分解恰好具有整数值的双精度矩阵会很好。