如何存储对称矩阵? [英] How to store a symmetric matrix?

查看:169
本文介绍了如何存储对称矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种方法是在内存中存储对称矩阵的最佳方法?

Which is the best way to store a symmetric matrix in memory?

最好节省一半的空间,而又不会过多地降低结构的速度和复杂性.这是一个与语言无关的问题,但是如果您需要做一些假设,只需假设它是一种很好的古老普通编程语言,例如C或C ++.

It would be good to save half of the space without compromising speed and complexity of the structure too much. This is a language-agnostic question but if you need to make some assumptions just assume it's a good old plain programming language like C or C++..

似乎有一种感觉,就是有一种方法可以使事情简单化,或者只是在矩阵本身很大的时候,对吗?

It seems a thing that has a sense just if there is a way to keep things simple or just when the matrix itself is really big, am I right?

仅出于形式上的考虑,我的意思是,对于我要存储的数据,此断言始终是正确的

Just for the sake of formality I mean that this assertion is always true for the data I want to store

matrix[x][y] == matrix[y][x]

推荐答案

我发现许多高性能程序包仅存储整个矩阵,但随后仅读取上三角或下三角.然后,他们可能会在计算过程中使用额外的空间来存储临时数据.

I find that many high performance packages just store the whole matrix, but then only read the upper triangle or lower triangle. They might then use the additional space for storing temporary data during the computation.

但是,如果存储确实是一个问题,那么只需将构成上三角形的n(n+1)/2元素存储在一维数组中即可.如果这使您的访问变得复杂,只需定义一组辅助函数即可.

However if storage is really an issue then just store the n(n+1)/2 elements making the upper triangle in a one-dimensional array. If that makes access complicated for you, just define a set of helper functions.

在C中访问矩阵matA,您可以定义一个宏:

In C to access a matrix matA you could define a macro:

#define A(i,j, dim) ((i <= j)?matA[i*dim + j]:matA[j*dim + i])

然后,您几乎可以正常访问阵列.

then you can access your array nearly normally.

这篇关于如何存储对称矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆