Problem manipulating an array in python using numpy

  numerical-methods, numpy, numpy-ndarray, python

So I’ve been trying to implement Jacobi’s method for solving systems of linear equations in python, and I’ve developed my code based on something like the following:

from numpy import array, zeros, diag, diagflat, dot

def jacobi(A,b,N=25,x=None):
"""Solves the equation Ax=b via the Jacobi iterative method."""
# Create an initial guess if needed                                                                                                                                                            
    if x is None:
        x = zeros(len(A[0]))

# Create a vector of the diagonal elements of A                                                                                                                                                
# and subtract them from A                                                                                                                                                                     
D = diag(A)
R = A - diagflat(D)

# Iterate for N times                                                                                                                                                                          
for i in range(N):
    x = (b - dot(R,x)) / D
return x

A = array([[2.0,1.0],[5.0,7.0]])
b = array([11.0,13.0])

guess = array([1.0,1.0])

sol = jacobi(A,b,N=25,x=guess)

But, when I try to develop a solution by taking user’s input for matrix size and elements as floats one by one, like:

    dim = int(input(""))
    A = np.zeros((dim,dim))
    b = np.zeros((dim,1))
    x = np.zeros((dim))
    for i in range(dim):
        for j in range(dim):
            A[i,j] = float(input(""))
    for i in range(dim):
        b[i,0] = float(input(""))

I usually get the error:
only size-1 arrays can be converted to Python scalars

when defining my function as:

def Jacobi(A, B, x, n):

    D = np.diag(A)
    R = A - np.diagflat(D)
    for i in range(n):
        x = (B -,x))/ D
    return x

I believe the problem might reside in the x update in the for loop and I’ve tried to find some manipulation inside numpy to fix it, but no success so far. I would appreciate any kind of help. I’m sorry if this doesn’t follow some guideline, I’m kind of new here.

Source: Python Questions