简体   繁体   English

我正在尝试使用python实现strassen的算法,但出现错误

[英]I'm trying to implement strassen's algorithm using python and I'm getting a error

here is my code: 这是我的代码:

# n*n additions 

def add(A, B):
    n = len(A)
    C = [[0 for j in range(n)] for i in range(n)]
    for j in range(n):
        for i in range (n):
            C[i][j] = A[i][j] + B[i][j]
    return C


# n*n subtraction

def sub(A, B):
    n = len(A)
    C = [[0 for j in range(n)] for i in range(n)]
    for i in range(n):
        for j in range(n):
            C[i][j] = A[i][j] - B[i][j]
    return C

def strassens(A, B):
    # Implementation of the strassen algorithm

    n = len(A[0])
    C = [[0 for j in range(n)] for i in range(n)]
    if n==1:
      C[0][0] = A[0][0]*B[0][0]
    else:


        # the new sub-matrices by dividing n (n/2)
        m = n/2
        A11 = [[0 for j in range(m)] for i in range(m)]
        A12 = [[0 for j in range(m)] for i in range(m)]
        A21 = [[0 for j in range(m)] for i in range(m)]
        A22 = [[0 for j in range(m)] for i in range(m)]

        B11 = [[0 for j in range(m)] for i in range(m)]
        B12 = [[0 for j in range(m)] for i in range(m)]
        B21 = [[0 for j in range(m)] for i in range(m)]
        B22 = [[0 for j in range(m)] for i in range(m)]

        AR = [[0 for j in range(m)] for i in range(m)]
        BR = [[0 for j in range(m)] for i in range(m)]

        # create 4 sub-matrices(top left, top right bottom letf and right:
        for i in range(m):
            for j in range(m):
                A11[i][j] = A[i][j]            
                A12[i][j] = A[i][j + m]
                A21[i][j] = A[i + m][j]    
                A22[i][j] = A[i + m][j + m] 

                B11[i][j] = B[i][j]            
                B12[i][j] = B[i][j + m]    
                B21[i][j] = B[i + m][j]    
                B22[i][j] = B[i + m][j + m] 

        # making the calculation for all the ps


            BR = sub(B12, B22) #(b12-b22)
            p1 = strassens(A11, BR) # p1 = (a11) * (b12-b22)

            AR = add(A11, A12)      # a11 + a12
            p2 = strassens(AR, B11)  # p2 = (a21+a22) * (b11)

            AR = add(A21, A22) # a21 + a22
            p3 = strassens(B11, AR)  # p3 = (b11) * (a21 + a22)

            BR = sub(B21, B11) # b21 - b11
            p4 =strassens(A22, BR)   # p4 = (a22) * (b21 - b11)

            BR =  add(B11,B22)     #b11+b22
            AR = add(A11, A12)      # a11 + a12
            p5 = strassens(AR, BR)  # p5 = (a11+a12) * (b11+b22)   

            AR = sub(A12, A22) # a12 - a22
            BR = add(B21, B22)      # b11 + b12
            p6 = strassens(AR, BR) # p6 = (a21-a11) * (b11+b12)

            AR = sub(A11, A21) # a12 - a21
            BR = add(B11, B12)      # b11 + b12
            p7 = strassens(AR, BR) # p7 = (a12-a21) * (b11+b12)

        # reqrouping all the ps into c11, c22,c21,c12

            AR = add(p5, p4) # p5 + p4
            BR = add(AR, p6) # p5 + p4 + p6
            c11 = sub(BR, p2) # c11 = p1 + p4 - p2 + p6

            c12 = add(p1, p2) # c12 = p1 + p2
            c21 = add(p3, p4)  # c21 = p3 + p4



            AR = add(p5, p1) # p5 + p1
            BR = sub(AR, p3) # p5 + p1 - p3
            c22 = sub(BR, p7) # c22 = p5 + p1 - p3 - p7

        # Grouping the results obtained in a single matrix:
            C = [[0 for j in range(n)] for i in range(n)]
            for i in range(m):
                for j in range(m):
                    C[i][j] = c11[i][j]
                    C[i][j + m] = c12[i][j]
                    C[i + m][j] = c21[i][j]
                    C[i + m][j + m] = c22[i][j]
            return C

here is the error that I get: 这是我得到的错误:

>>> a=[[1,2,4],[0,2,3]]
>>> b=[[9,2,4],[0,2,3]]

>>> strassens(a,b)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    strassens(a,b)
  File "C:\Python33\Python\homework.py", line 34, in strassens
    A11 = [[0 for j in range(m)] for i in range(m)]
TypeError: 'float' object cannot be interpreted as an integer

In Python 3, you need to use // for int division. 在Python 3中,您需要将//用于int除法。 / gives a float: /给出一个浮点数:

m = n // 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试实现:np.maximum.outer in Python 3x 但我收到此错误:NotImplementedError - I´m trying to implement: np.maximum.outer in Python 3x but I´m getting this error: NotImplementedError 我正在尝试实现一个极小极大算法来创建一个井字游戏机器人,但我遇到了递归错误 - I'm trying to implement a minimax algorithm to create a tic-tac-toe bot, but i'm getting a recursion error 尝试使用Selenium 2与Python绑定,但我收到导入错误 - Trying to use Selenium 2 with Python bindings, but I'm getting an import error 我正在尝试使用此 python 代码创建 wordcloud,但不断出现错误 - I'm trying to create a wordcloud with this python code, but keeps getting error 我正在尝试标准化我的属性,但由于 Python 找不到它而出现标准缩放器错误 - I'm trying to standardize my attribute but I'm getting standard scaler error because Python can not find it 我正在尝试在Python中实现Bubblesort算法,但无法理解为什么它在一种情况下有效而在另一种情况下无效 - I'm trying to implement the bubblesort algorithm in Python and can't understand why it works in one case and not another 我正在尝试在 python 中使用 HTML img 标签发送邮件,但出现以下错误 - I'm trying to use HTML img tag in python to send a mail, but I'm getting the following error 我正在尝试使用 oops 开发 tkinter 应用程序但收到此错误 - I'm trying to develop tkinter app using oops but getting this error 我试图理解为什么在使用 paramiko 1.7.6 时会出现“Permission Denied”错误 - I'm trying to understand why I'm getting a “Permission Denied” error when using paramiko 1.7.6 我正在尝试使用python在ubuntu上安装django = 1.9,并且在管理员登录时遇到以下错误 - I'm trying to install django=1.9 on ubuntu using python and I'm getting the following errors for admin login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM