.NET 中的稀疏多维数组或矩阵库

发布于 2024-07-26 05:23:03 字数 295 浏览 2 评论 0原文

我需要在 .NET 应用程序中使用最多 4 维的稀疏矩阵。 矩阵的大小(如果表示为 .NET 数组)可能会达到 400MB。

该数组可能非常稀疏,我需要能够非常快速地实例化和处理它(尽管这不是不行)。 因此,我需要一个稀疏数组库,它可以从 .NET 3.5 中使用(我认为它排除了使用托管 C++ 中的 BGL?),它尽可能密集,并且支持快速随机访问索引。 它必须可序列化为某种可以廉价缓存的密集格式。

.NET 中是否存在这样的东西? 自由软件? 成熟?

蒂亚

·安德鲁·马修斯

I have a need for a sparse matrix in up to 4 dimensions in a .NET application. The size of the matrix (if represented as a .NET Array) would potentially top 400MB.

The array is likely to be very sparse, and I need to be able to instantiate and dispose it very quickly (although that's not a no go). I'm therefore after a sparse array library, consumable from .NET 3.5 (which I believe rules out using BGL from Managed C++?) that is as dense as possible as I can get and the supports fast random access indexing. It must be serializable to some dense format that can be inexpensively cached.

Does such a thing exist (yet) for .NET? FOSS? Mature?

TIA

Andrew Matthews

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看轻我的陪伴 2024-08-02 05:23:03

使用字典实现您自己的字典相当简单。 下面的实现适用于 2 维,但您可以轻松实现 3 或 4 维。 当矩阵稀疏时,存储非常有效。 如果您打算频繁添加或删除列,那么这不是一个好的实现。

class SparseMatrix<T>
    {
        public T this[int i, int j]
        {
            get
            {
                T result;
                if (!_data.TryGetValue(new Key(i, j), out result))
                    return default(T);
                return result;
            }
            set { _data[new Key(i, j)] = value; } // Could remove values if value == default(T)
        }

        private struct Key
        {
            public Key(int i, int j)
            {
                _i = i;
                _j = j;
            }

            private readonly int _i;    
            private readonly int _j;
            public override bool Equals(object obj)
            {
                if (!(obj is Key))
                    return false;
                var k = (Key) obj;
                return k._i == _i && k._j == _j;
            }

            public override int GetHashCode()
            {
                return _i << 16 + _j; // Could be smarter based on the distribution of i and j
            }


        }

        private readonly Dictionary<Key, T> _data = new Dictionary<Key, T>();
    }

It is fairly simple to implement your own with a Dictionary. The implementation below works for 2 dimensions but you can easily implement 3 or 4 dimensions. The storage is very efficient when the matrix is sparse. It is not a good implementation if you plan to add or remove columns frequently.

class SparseMatrix<T>
    {
        public T this[int i, int j]
        {
            get
            {
                T result;
                if (!_data.TryGetValue(new Key(i, j), out result))
                    return default(T);
                return result;
            }
            set { _data[new Key(i, j)] = value; } // Could remove values if value == default(T)
        }

        private struct Key
        {
            public Key(int i, int j)
            {
                _i = i;
                _j = j;
            }

            private readonly int _i;    
            private readonly int _j;
            public override bool Equals(object obj)
            {
                if (!(obj is Key))
                    return false;
                var k = (Key) obj;
                return k._i == _i && k._j == _j;
            }

            public override int GetHashCode()
            {
                return _i << 16 + _j; // Could be smarter based on the distribution of i and j
            }


        }

        private readonly Dictionary<Key, T> _data = new Dictionary<Key, T>();
    }
擦肩而过的背影 2024-08-02 05:23:03

我会推荐 dnAnalytics。 它支持稀疏矩阵,并有许多选项,包括强大的求解器、对 IMKL 的支持等。

I would recommend dnAnalytics. It supports sparse matrices, and has many options including robust solvers, support for IMKL, etc.

撩心不撩汉 2024-08-02 05:23:03

你会如何“轻松实现”,比如 4 维矩阵或张量? 我只看到上面的 i 和 j 索引......

how would you "easily implement", say a 4 dimensional matrix or tensor? I only see i and j indices above...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文