我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用scipy.linalg.solve_banded()。
def _radau(alpha, beta, xr): '''From <http://www.scientificpython.net/pyblog/radau-quadrature>: Compute the Radau nodes and weights with the preassigned node xr. Based on the section 7 of the paper Some modified matrix eigenvalue problems, Gene Golub, SIAM Review Vol 15, No. 2, April 1973, pp.318--334. ''' from scipy.linalg import solve_banded n = len(alpha)-1 f = numpy.zeros(n) f[-1] = beta[-1] A = numpy.vstack((numpy.sqrt(beta), alpha-xr)) J = numpy.vstack((A[:, 0:-1], A[0, 1:])) delta = solve_banded((1, 1), J, f) alphar = alpha.copy() alphar[-1] = xr + delta[-1] x, w = orthopy.line.schemes.custom(alphar, beta, mode='numpy') return x, w
def _lobatto(alpha, beta, xl1, xl2): '''Compute the Lobatto nodes and weights with the preassigned node xl1, xl2. Based on the section 7 of the paper Some modified matrix eigenvalue problems, Gene Golub, SIAM Review Vol 15, No. 2, April 1973, pp.318--334, and http://www.scientificpython.net/pyblog/radau-quadrature ''' from scipy.linalg import solve_banded, solve n = len(alpha)-1 en = numpy.zeros(n) en[-1] = 1 A1 = numpy.vstack((numpy.sqrt(beta), alpha-xl1)) J1 = numpy.vstack((A1[:, 0:-1], A1[0, 1:])) A2 = numpy.vstack((numpy.sqrt(beta), alpha-xl2)) J2 = numpy.vstack((A2[:, 0:-1], A2[0, 1:])) g1 = solve_banded((1, 1), J1, en) g2 = solve_banded((1, 1), J2, en) C = numpy.array(((1, -g1[-1]), (1, -g2[-1]))) xl = numpy.array((xl1, xl2)) ab = solve(C, xl) alphal = alpha alphal[-1] = ab[0] betal = beta betal[-1] = ab[1] x, w = orthopy.line.schemes.custom(alphal, betal, mode='numpy') return x, w
def __init__(self, x, y): x = np.asarray(x, dtype=float) y = np.asarray(y, dtype=float) n = x.shape[0] dx = np.diff(x) dy = np.diff(y, axis=0) dxr = dx.reshape([dx.shape[0]] + [1] * (y.ndim - 1)) c = np.empty((3, n - 1) + y.shape[1:]) if n > 2: A = np.ones((2, n)) b = np.empty((n,) + y.shape[1:]) b[0] = 0 b[1:] = 2 * dy / dxr s = solve_banded((1, 0), A, b, overwrite_ab=True, overwrite_b=True, check_finite=False) c[0] = np.diff(s, axis=0) / (2 * dxr) c[1] = s[:-1] c[2] = y[:-1] else: c[0] = 0 c[1] = dy / dxr c[2] = y[:-1] super(_QuadraticSpline, self).__init__(c, x)
def __init__(self, x, y, yp=None): npts = len(x) mat = np.zeros((3, npts)) # enforce continuity of 1st derivatives mat[1,1:-1] = (x[2: ]-x[0:-2])/3. mat[2,0:-2] = (x[1:-1]-x[0:-2])/6. mat[0,2: ] = (x[2: ]-x[1:-1])/6. bb = np.zeros(npts) bb[1:-1] = ((y[2: ]-y[1:-1])/(x[2: ]-x[1:-1]) - (y[1:-1]-y[0:-2])/(x[1:-1]-x[0:-2])) if yp is None: # natural cubic spline mat[1,0] = 1. mat[1,-1] = 1. bb[0] = 0. bb[-1] = 0. elif yp == '3d=0': mat[1, 0] = -1./(x[1]-x[0]) mat[0, 1] = 1./(x[1]-x[0]) mat[1,-1] = 1./(x[-2]-x[-1]) mat[2,-2] = -1./(x[-2]-x[-1]) bb[ 0] = 0. bb[-1] = 0. else: mat[1, 0] = -1./3.*(x[1]-x[0]) mat[0, 1] = -1./6.*(x[1]-x[0]) mat[2,-2] = 1./6.*(x[-1]-x[-2]) mat[1,-1] = 1./3.*(x[-1]-x[-2]) bb[ 0] = yp[0]-1.*(y[ 1]-y[ 0])/(x[ 1]-x[ 0]) bb[-1] = yp[1]-1.*(y[-1]-y[-2])/(x[-1]-x[-2]) y2 = solve_banded((1,1), mat, bb) self.x, self.y, self.y2 = (x, y, y2)