[英]How do I convert a tuple to an Integer in python
So i am trying to check if on one of all possible permutations I will get a form where the matrix is diagonally dominant but when trying to check for it i get an error 所以我试图检查是否在所有可能的排列之一上我将得到矩阵对角占主导地位的形式,但是当尝试检查它时我得到了一个错误
import numpy
from itertools import product
A = numpy.array([[10., -1., 2., 0.],
[2., -1., 10., -1.],
[-1., 11., -1., 3.],
[0.0, 3., -1., 8.]])
def dominance(A):
dominance=True
n=4
sumC=numpy.sum(numpy.absolute(A),axis=0)
sumR=numpy.sum(numpy.absolute(A),axis=1)
resC = [0 for i in range(n)]
resR= [0 for i in range(n)]
for i in range(n):
resC[i]=sumC[i]-A[i,i]
resR[i]=sumR[i]-A[i,i]
if A[i,i]<resC[i] or A[i,i]<resR[i]:
dominance=False
break
return dominance
def permutate(iterable, r=None):
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
for indices in product(range(n), repeat=r):
if len(set(indices)) == r:
yield tuple(pool[i] for i in indices)
if dominance(A):
print "Es dominante"
else:
for i in permutate(A):
if dominance(list(i)):
print "this way is dominant"
print i
break
this here is the error 这是错误
Traceback (most recent call last):
File "Prueba.py", line 37, in <module>
if dominance(list(i)):
File "Prueba.py", line 16, in dominance
resC[i]=sumC[i]-A[i,i]
TypeError: list indices must be integers, not tuple
You define A
as a list of lists. 您将A
定义为列表列表。 You get this error when you pass it to dominance
: 将错误传递给dominance
时,您会得到以下错误:
In [87]: dominance(A)
---------------------------------------------------------------------------
...
8 for i in range(n):
----> 9 resC[i]=sumC[i]-A[i,i]
10 resR[i]=sumR[i]-A[i,i]
11 if A[i,i]<resC[i] or A[i,i]<resR[i]:
TypeError: list indices must be integers, not tuple
But if you first make A
an array, it runs fine: 但是,如果首先使A
成为数组,则可以正常运行:
In [94]: dominance(np.array(A))
Out[94]: False
I won't dig into why dominance
has problems, but it looks like dominance
was written with numpy arrays in mind, not lists of lists. 我不会深入研究为什么dominance
会带来问题,但是看起来dominance
是在考虑numpy数组而不是列表列表的情况下编写的。 sumC=numpy.sum(numpy.absolute(A),axis=0)
treats A
like an array (it works with list A
because internally absolute
converts it to an array). sumC=numpy.sum(numpy.absolute(A),axis=0)
将A
视为数组(它与列表A
一起A
因为内部absolute
将其转换为数组)。
The 2nd call to dominance
must also get an array: dominance
的第二次调用还必须获得一个数组:
dominance(np.array(i))
You are approaching this very inefficiently! 您的处理效率非常低!
If you are seeking strict diagonal dominance: 如果您正在寻求严格的对角线优势:
each row of the matrix can only have 0 or 1 strictly dominant values 矩阵的每一行只能有0或1个严格的主导值
each column of the matrix can only have 0 or 1 strictly dominant values 矩阵的每一列只能有0或1个严格的主导值
if any row or column has 0 strictly dominant values, there are no strictly diagonally dominant permutations of the matrix 如果任何行或列具有0个严格的主导值,则矩阵不存在严格的对角优势的排列
if every row and every column has exactly 1 strictly dominant value, then there is exactly 1 strictly diagonally dominant permuted matrix: for each row of the original matrix, the column index of the dominant item specifies the index of the row in the permuted result matrix 如果每一行和每一列都具有正好是1个严格的主导值,那么正好有1个严格对角地主导的置换矩阵:对于原始矩阵的每一行,主导项的列索引指定置换结果矩阵中的行的索引
This results in an O(n^2) algorithm instead of O(n^2 * n!) - for a 10*10 matrix, it should be something like 3 million times faster. 这样就产生了O(n ^ 2)算法,而不是O(n ^ 2 * n!)-对于10 * 10的矩阵,它应该快300万倍。
If you are seeking non-strict diagonal dominance: 如果您正在寻求非严格的对角线优势:
each row of the matrix can only have 0, 1 or 2 dominant values 矩阵的每一行只能有0、1或2个主导值
each column of the matrix can only have 0, 1 or 2 dominant values 矩阵的每一列只能有0、1或2个主导值
if any row or column has 0 dominant values, there are no diagonally dominant permutations of the matrix 如果任何行或列具有0个优势值,则矩阵不存在对角优势优势排列
(reduction:) if a row has exactly 1 dominant value, and that value's column contains 2 dominant values, the other dominant value in that column can be trivially disregarded (and vice-versa) (减少:)如果一行恰好具有1个主导值,并且该值的列包含2个主导值,则该列中的另一个主导值可以忽略不计(反之亦然)
if every row and every column has exactly 1 dominant value, then there is exactly 1 diagonally dominant permuted matrix: for each row of the original matrix, the column index of the dominant item specifies the index of the row in the permuted result matrix 如果每一行和每一列都具有正好为1的主导值,则正好有1个对角优势主导的置换矩阵:对于原始矩阵的每一行,优势项的列索引指定置换结果矩阵中的行的索引
sets of rows having 2 dominant values can combine to form circuits, where each circuit has 0, 1, or 2 diagonally dominant solutions (and selecting a dominant value for one row forces the selection for all other rows in the circuit). 具有2个主导值的行的集合可以合并形成电路,其中每个电路具有0、1或2个对角主导解(并且为一行选择一个主导值会强制选择该电路中的所有其他行)。 A branch-and-prune approach can very quickly find all valid solutions. 分支修剪方法可以非常快速地找到所有有效的解决方案。
For a matrix having t rows with 2 dominant values, this results in an O(n^2 * 2^t) algorithm instead of O(n^2 * n!) - for a 10*10 matrix, it should be between 3 thousand and 3 million times faster (depending on the number of rows having 2 dominant values). 对于具有2个主值的t行的矩阵,这将导致O(n ^ 2 * 2 ^ t)算法,而不是O(n ^ 2 * n!)-对于10 * 10的矩阵,它应介于3之间快一千三百万倍(取决于具有2个主导值的行数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.