user3316598
user3316598

Reputation: 173

What does "ufunc did not contain a loop with signature matching types" mean?

I have code that I thought was great. I am getting the following error message:

Traceback (most recent call last):
  File "C:\Users\ablev\eclipse-workspace\matrix\matrix.py", line 149, in <module>
    print("Transpose of matrice is:\n" + transpose(subtract())),
numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U25'), dtype('int32')) -> None

What does this mean?

def matrix_one():
    print("Enter your first 3x3 matrix: ")
    global matrix1
    matrix1=[]
    for i in range(3):
            while True:
                row=input().split()
                row=list(map(int,row))
                if len(row) != 3:
                    print(f"Please enter 3 rows of 3 columns of\
numbers separated by a space: ")
                else:
                    break
            matrix1.append(row)
    
    print("Your first 3x3 matrix is: ")
    for i in range(3):
        for j in range(3):
            print(matrix1[i][j],end=" ")
        print()

def matrix_two():
    print("Enter your second 3x3 matrix: ")
    global matrix2
    matrix2=[]
    for i in range(3):
            while True:
                row=input().split()
                row=list(map(int,row))
                if len(row) != 3:
                    print(f"Please enter 3 rows of 3 columns of\
numbers separated by a space: ")
                else:
                    break
            matrix2.append(row)
    print("Your second 3x3 matrix is: ")
    for i in range(3):
        for j in range(3):
            print(matrix2[i][j],end=" ")
        print()

def subtract():
    '''function to subtract results of matrix'''
    results = np.subtract(matrix1,matrix2)
    return results
def transpose(function):
    '''transpose function'''
    transpose = np.transpose(function)
    return transpose()
matrix_one()
matrix_two()
subtract()
print(subtract())
print("Transpose of matrice is:\n" + transpose(subtract())),

Upvotes: 1

Views: 8568

Answers (1)

rikyeah
rikyeah

Reputation: 2013

As the comments pointed out,

print("Transpose of matrice is:\n", transpose(subtract()))

instead of

print("Transpose of matrice is:\n" + transpose(subtract()))

will solve your problem since the operator + need strings as arguments, and the result of transpose is not a string but an np.array.

Another approach would be to make transpose() return the string representation of the matrix, like so:

def transpose(function):
'''transpose function'''
trans = np.transpose(function)
return str(trans)

Leaving out the extra parenthesis.

Upvotes: 2

Related Questions