对称可寻址矩阵

发布于 2024-08-04 22:57:47 字数 164 浏览 3 评论 0原文

我正在寻找在Python中创建具有对称寻址的二维整数矩阵(即矩阵[2,3]和矩阵[3,2]将返回相同的值)。整数将进行加法和减法,并用于逻辑比较。我最初的想法是预先创建整数对象,并尝试用一些相当于 Python 的指针来填充列表列表。但我不知道该怎么做。实现此目的的最佳方法是什么?我应该使用列表还是其他数据结构?

I'm looking to create a 2d matrix of integers with symmetric addressing ( i.e. matrix[2,3] and matrix[3,2] will return the same value ) in python. The integers will have addition and subtraction done on them, and be used for logical comparisons. My initial idea was to create the integer objects up front and try to fill a list of lists with some python equivalent of pointers. I'm not sure how to do it, though. What is the best way to implement this, and should I be using lists or another data structure?

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

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

发布评论

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

评论(4

动听の歌 2024-08-11 22:57:47

Golub 和 Van Loan 的“矩阵计算”一书概述了一种可行的寻址方案:

将数据打包到向量中并按如下方式访问,假设 i >= j:

a_ij = A.vec((j-1)n - j(j-1)/2 + i)    

Golub and Van Loan's "Matrix Computations" book outlines a feasible addressing scheme:

You pack the data in to a vector and access as follows, assuming i >= j:

a_ij = A.vec((j-1)n - j(j-1)/2 + i)    
雨轻弹 2024-08-11 22:57:47

使用完整的方 numpy 矩阵可能会更好。是的,它浪费了一半的内存来存储冗余值,但是在 Python 中滚动你自己的对称矩阵会通过将整数存储和处理为 Python 对象来浪费更多的内存和 CPU。

You're probably better off using a full square numpy matrix. Yes, it wastes half the memory storing redundant values, but rolling your own symmetric matrix in Python will waste even more memory and CPU by storing and processing the integers as Python objects.

七度光 2024-08-11 22:57:47

一种更简单、更干净的方法是仅使用以排序元组作为键的字典。这些元组与您的矩阵索引相对应。重写__getitem____setitem__以按排序元组访问字典;这是一个示例类:

class Matrix(dict):
    def __getitem__(self, index):
        return super(Matrix, self).__getitem__(tuple(sorted(index)))
    def __setitem__(self, index, value):
        return super(Matrix, self).__setitem__(tuple(sorted(index)), value)

然后像这样使用它:

>>> matrix = Matrix()
>>> matrix[2,3] = 1066
>>> print matrix
{(2, 3): 1066}
>>> matrix[2,3]
1066
>>> matrix[3,2]
1066
>>> matrix[1,1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "z.py", line 3, in __getitem__
    return super(Matrix, self).__getitem__(tuple(sorted(index)))
KeyError: (1, 1)

A simpler and cleaner way is to just use a dictionary with sorted tuples as keys. The tuples correspond with your matrix index. Override __getitem__ and __setitem__ to access the dictionary by sorted tuples; here's an example class:

class Matrix(dict):
    def __getitem__(self, index):
        return super(Matrix, self).__getitem__(tuple(sorted(index)))
    def __setitem__(self, index, value):
        return super(Matrix, self).__setitem__(tuple(sorted(index)), value)

And then use it like this:

>>> matrix = Matrix()
>>> matrix[2,3] = 1066
>>> print matrix
{(2, 3): 1066}
>>> matrix[2,3]
1066
>>> matrix[3,2]
1066
>>> matrix[1,1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "z.py", line 3, in __getitem__
    return super(Matrix, self).__getitem__(tuple(sorted(index)))
KeyError: (1, 1)
定格我的天空 2024-08-11 22:57:47

您只需要存储矩阵的下三角形。通常,这是通过一个 n(n+1)/2 长度的列表来完成的。您需要重载 __getitem__ 方法来解释该条目的含义。

You only need to store the lower triangle of the matrix. Typically this is done with one n(n+1)/2 length list. You'll need to overload the __getitem__ method to interpret what the entry means.

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