def jacobi(a, b, tol=0.0005): """ Solve a linear matrix equation, or system of linear scalar equations using the Jacobi Method. :param a: Coefficient matrix :type a: array_like, shape (M, M) :param b: Ordinate or "dependent variable" values :type b: array_like, shape (M,) or (M, N) :return: Solution to the system a x = b :rtype: ndarray, shape (M,) or (M, N) depending on b :raises: :py:exc:`LinAlgError` If `a` is singular or not square. **Examples:** Solve the system of equations ``3 * x0 + x1 = 9`` and ``x0 + 2 * x1 = 8``: >>> import bohrium as np >>> a = np.array([[3,1], [1,2]]) >>> b = np.array([9,8]) >>> x = np.linalg.jacobi(a, b) >>> x array([ 2., 3.]) Check that the solution is correct: >>> (np.dot(a, x) == b).all() True """ x = np.ones_like(b) D = 1 / np.diag(a) R = np.diag(np.diag(a)) - a T = D[:, np.newaxis] * R C = D * b error = tol + 1 while error > tol: xo = x x = np.add.reduce(T * x, -1) + C error = norm(x - xo) / norm(x) return x
def jacobi(a, b, tol=0.0005): """ Solve a linear matrix equation, or system of linear scalar equations using the Jacobi Method. :param a: Coefficient matrix :type a: array_like, shape (M, M) :param b: Ordinate or "dependent variable" values :type b: array_like, shape (M,) or (M, N) :return: Solution to the system a x = b :rtype: ndarray, shape (M,) or (M, N) depending on b :raises: :py:exc:`LinAlgError` If `a` is singular or not square. **Examples:** Solve the system of equations ``3 * x0 + x1 = 9`` and ``x0 + 2 * x1 = 8``: >>> import bohrium as np >>> a = np.array([[3,1], [1,2]]) >>> b = np.array([9,8]) >>> x = np.linalg.jacobi(a, b) >>> x array([ 2., 3.]) Check that the solution is correct: >>> (np.dot(a, x) == b).all() True """ x = np.ones_like(b) D = 1/np.diag(a) R = np.diag(np.diag(a)) - a T = D[:,np.newaxis]*R C = D*b error = tol + 1 while error > tol: xo = x x = np.add.reduce(T*x,-1) + C error = norm(x-xo)/norm(x) return x
def get_drhodS(salt, temp, p): betaS = 0.78e-3 rho0 = 1024. return betaS * rho0 * bh.ones_like(temp)