web-dev-qa-db-ja.com

C#でジェネリックスを使用して数学ライブラリを作成する

ジェネリックスを使用して、データを格納するために選択した基本型に依存しない数学ライブラリを作成するための実行可能な方法はありますか?

つまり、Fractionクラスを作成したいとします。分数は、2つのintまたは2つのdoubleなどで表すことができます。重要なことは、基本的な4つの算術演算が明確に定義されていることです。ですから、Fraction<int> frac = new Fraction<int>(1,2)Fraction<double> frac = new Fraction<double>(0.1, 1.0)を書けるようにしたいと思います。

残念ながら、4つの基本操作(+、-、*、/)を表すインターフェイスはありません。誰かがこれを実装するための実行可能で実行可能な方法を見つけましたか?

28
Sklivvz

これは、比較的苦痛のない演算子を抽象化する方法です。

    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>実装の定義は常に同じになります(これも悪いです)。これをC#で行うのは避け、F#またはこのレベルの抽象化により適した他の言語を使用することをお勧めします。

編集:Fraction<T>の加算と減算の定義を修正しました。もう1つの興味深く簡単なことは、抽象構文木を操作するMathProviderを実装することです。このアイデアは、自動微分のようなことをすぐに指し示します: http://conal.net/papers/beautiful-differentiation/

29
fryguybob

私はこれがあなたの質問に答えると信じています:

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

6
Peter

ジェネリック型に伴う微妙な問題があります。アルゴリズムに、連立方程式を解くためのガウスの消去法などの除算が含まれているとします。整数を渡すと、integer除算を実行するため、間違った答えが返されます。しかし、整数値を持つ二重引数を渡すと、正しい答えが得られます。

コレスキー分解の場合と同じことが平方根でも起こります。整数行列の因数分解はうまくいきませんが、たまたま整数値を持つdoubleの行列の因数分解は問題ありません。

3
John D. Cook

まず、クラスは汎用パラメーターをプリミティブに制限する必要があります(public class Fraction where T:struct、new())。

次に、コンパイラが泣くことなく、あるタイプから別のタイプへのキャストを処理できるように、おそらく 暗黙のキャストオーバーロード を作成する必要があります。

第3に、4つの基本的な演算子をオーバーロードして、異なるタイプの分数を組み合わせるときにインターフェイスをより柔軟にすることができます。

最後に、算術のオーバーフローとアンダーフローをどのように処理しているかを考慮する必要があります。優れたライブラリは、オーバーフローの処理方法を非常に明確にします。そうしないと、異なる分数タイプの操作の結果を信頼できません。

2
Will