如何培养一个存储在第二个功率嵌套词典中的稀疏三角矩阵?
我有一个稀疏的三角矩阵,我需要将其提升到第二个力量。问题是,要求之一是矩阵被存储为嵌套词典(我需要创建Python实现),因此Numpy的标准函数不应用。 (我知道这种存储矩阵的方式可能过于杀伤,但这是对大学课程的
过多的
def __init__(self, *matrix):
self.rare_values = dict()
if len(matrix) > 0:
for i, line in enumerate(matrix):
if i == 0:
self.n = line
continue
value = line[0]
i = line[1]
j = line[2]
if j > i:
i = line[2]
j = line[1]
if i in self.rare_values.keys():
if j in self.rare_values[i].keys():
self.rare_values[i][j] += value
else:
dict_column = {j: value}
self.rare_values[i].update(dict_column)
else:
dict_line = {i: {j: value}}
self.rare_values.update(dict_line)
) 。是否有可能实施算法来提高第二功率的矩阵?如果是这样,如果您可以就我如何处理它提供伪代码或极简主义的解释,我将非常感激。
谢谢你!
I have a sparse triangular matrix that I need to raise to the 2nd power. The problem is, one of the requirements is that the matrix is stored as a nested dictionary (I need to create a python implementation), and so the standard functions from numpy do not apply. (I know this way of storing a matrix is probably overkill, but this is for a university course)
Below is how I've implemented the creation of the matrix itself (rare is the equivalent to sparse here):
def __init__(self, *matrix):
self.rare_values = dict()
if len(matrix) > 0:
for i, line in enumerate(matrix):
if i == 0:
self.n = line
continue
value = line[0]
i = line[1]
j = line[2]
if j > i:
i = line[2]
j = line[1]
if i in self.rare_values.keys():
if j in self.rare_values[i].keys():
self.rare_values[i][j] += value
else:
dict_column = {j: value}
self.rare_values[i].update(dict_column)
else:
dict_line = {i: {j: value}}
self.rare_values.update(dict_line)
My question is, would it be possible to implement an algorithm to raise such a matrix to the 2nd power? If so, I would be more than thankful if you could provide a pseudocode or minimalist explanation as to how I may go about it.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的代码可以显着简化为:
这会产生与您的 __init__ 方法相同的结构,只是更清晰(IMO)。
从那里开始,对每个值进行平方就像迭代 dict-of-dicts 一样简单,例如使用嵌套的 for 循环迭代
.items()
:示例:
来自注释的后续操作
下面的示例展示了如何对矩阵求幂(不是我上面展示的按元素运算)。
请注意,此代码更改了您的版本和我的原始版本的一些内容。它接受 (r,c,v) 顺序的项目,并且不会尝试像强制三角矩阵那样“标准化”索引。
如果您想要这种行为,则必须编辑此版本才能将其重新添加回来。
The code you provided can be significantly reduced to something like:
This produces the same structure as your
__init__
method, just more clearly (IMO).From there, squaring each of the values is as simple as iterating over the dict-of-dicts, e.g. using nested for loops iterating over
.items()
:Example:
Follow-up from comments
The following example shows how you might raise the matrix to a power (not the element-wise operation I showed above).
Note this code changes some things from your version and my original version. It accepts items in (r,c,v) order and doesn't attempt to "normalize" the indices like you do to force a triangular matrix.
If you want that behavior you'll have to edit this version to re-add that back.