def Heigenvectors(a, UPLO='L'): _assertRank2(a) _assertSquareness(a) t =_commonType(a) real_t = _array_type[0][_array_precision[t]] a = _castCopyAndTranspose(t, a) n = a.getshape()[0] liwork = 5*n+3 iwork = num.zeros((liwork,),'l') if _array_kind[t] == 1: # Complex routines take different arguments lapack_routine = lapack_lite2.zheevd w = num.zeros((n,), real_t) lwork = 1 work = num.zeros((lwork,), t) lrwork = 1 rwork = num.zeros((lrwork,),real_t) results = lapack_routine('V', UPLO, n, a, n,w, work, -1, rwork, -1, iwork, liwork, 0) lwork = int(abs(work[0])) work = num.zeros((lwork,), t) lrwork = int(rwork[0]) rwork = num.zeros((lrwork,),real_t) results = lapack_routine('V', UPLO, n, a, n,w, work, lwork, rwork, lrwork, iwork, liwork, 0) else: lapack_routine = lapack_lite2.dsyevd w = num.zeros((n,), t) lwork = 1 work = num.zeros((lwork,),t) results = lapack_routine('V', UPLO, n, a, n,w, work, -1, iwork, liwork, 0) lwork = int(work[0]) work = num.zeros((lwork,),t) results = lapack_routine('V', UPLO, n, a, n,w, work, lwork, iwork, liwork, 0) if results['info'] > 0: raise LinAlgError, 'Eigenvalues did not converge' return (w,a)
def _raw_fft(a, n=None, axis=-1, init_function=fftpack.cffti, work_function=fftpack.cfftf, fft_cache=_fft_cache): a = num.asarray(a) if n == None: n = a.getshape()[axis] wsave = init_function(n) if a.getshape()[axis] != n: s = list(a.getshape()) if s[axis] > n: index = [slice(None)] * len(s) index[axis] = slice(0, n) a = a[index] else: index = [slice(None)] * len(s) index[axis] = slice(0, s[axis]) s[axis] = n z = num.zeros(s, a.type()) z[index] = a a = z if axis != -1: a = num.swapaxes(a, axis, -1) # print work_function, a r = work_function(a, wsave) if axis != -1: r = num.swapaxes(r, axis, -1) return r
def frame_constant(a, shape, cval=0): """frame_nearest creates an oversized copy of 'a' with new 'shape' and the contents of 'a' in the center. The boundary pixels are copied from the nearest edge pixel in 'a'. >>> a = num.arange(16, shape=(4,4)) >>> frame_constant(a, (8,8), cval=42) array([[42, 42, 42, 42, 42, 42, 42, 42], [42, 42, 42, 42, 42, 42, 42, 42], [42, 42, 0, 1, 2, 3, 42, 42], [42, 42, 4, 5, 6, 7, 42, 42], [42, 42, 8, 9, 10, 11, 42, 42], [42, 42, 12, 13, 14, 15, 42, 42], [42, 42, 42, 42, 42, 42, 42, 42], [42, 42, 42, 42, 42, 42, 42, 42]]) """ b = num.zeros(shape, typecode=a.type()) delta = (num.array(b.shape) - num.array(a.shape)) dy = delta[0] // 2 dx = delta[1] // 2 my = a.shape[0] + dy mx = a.shape[1] + dx b[dy:my, dx:mx] = a # center b[:dy,dx:mx] = cval # top b[my:,dx:mx] = cval # bottom b[dy:my, :dx] = cval # left b[dy:my, mx:] = cval # right b[:dy, :dx] = cval # topleft b[:dy, mx:] = cval # topright b[my:, :dx] = cval # bottomleft b[my:, mx:] = cval # bottomright return b
def frame_constant(a, shape, cval=0): """frame_nearest creates an oversized copy of 'a' with new 'shape' and the contents of 'a' in the center. The boundary pixels are copied from the nearest edge pixel in 'a'. >>> a = num.arange(16, shape=(4,4)) >>> frame_constant(a, (8,8), cval=42) array([[42, 42, 42, 42, 42, 42, 42, 42], [42, 42, 42, 42, 42, 42, 42, 42], [42, 42, 0, 1, 2, 3, 42, 42], [42, 42, 4, 5, 6, 7, 42, 42], [42, 42, 8, 9, 10, 11, 42, 42], [42, 42, 12, 13, 14, 15, 42, 42], [42, 42, 42, 42, 42, 42, 42, 42], [42, 42, 42, 42, 42, 42, 42, 42]]) """ b = num.zeros(shape, typecode=a.type()) delta = (num.array(b.shape) - num.array(a.shape)) dy = delta[0] // 2 dx = delta[1] // 2 my = a.shape[0] + dy mx = a.shape[1] + dx b[dy:my, dx:mx] = a # center b[:dy, dx:mx] = cval # top b[my:, dx:mx] = cval # bottom b[dy:my, :dx] = cval # left b[dy:my, mx:] = cval # right b[:dy, :dx] = cval # topleft b[:dy, mx:] = cval # topright b[my:, :dx] = cval # bottomleft b[my:, mx:] = cval # bottomright return b
def _raw_fft(a, n=None, axis=-1, init_function=fftpack.cffti, work_function=fftpack.cfftf, fft_cache = _fft_cache ): a = num.asarray(a) if n == None: n = a.getshape()[axis] wsave = init_function(n) if a.getshape()[axis] != n: s = list(a.getshape()) if s[axis] > n: index = [slice(None)]*len(s) index[axis] = slice(0,n) a = a[index] else: index = [slice(None)]*len(s) index[axis] = slice(0,s[axis]) s[axis] = n z = num.zeros(s, a.type()) z[index] = a a = z if axis != -1: a = num.swapaxes(a, axis, -1) # print work_function, a r = work_function(a, wsave) if axis != -1: r = num.swapaxes(r, axis, -1) return r
def singular_value_decomposition(a, full_matrices = 0): _assertRank2(a) n = a.getshape()[1] m = a.getshape()[0] t =_commonType(a) real_t = _array_type[0][_array_precision[t]] a = _fastCopyAndTranspose(t, a) if full_matrices: nu = m nvt = n option = 'A' else: nu = min(n,m) nvt = min(n,m) option = 'S' s = num.zeros((min(n,m),), real_t) u = num.zeros((nu, m), t) vt = num.zeros((n, nvt), t) iwork = num.zeros((8*min(m,n),), 'l') if _array_kind[t] == 1: # Complex routines take different arguments lapack_routine = lapack_lite2.zgesdd rwork = num.zeros((5*min(m,n)*min(m,n) + 5*min(m,n),), real_t) lwork = 1 work = num.zeros((lwork,), t) results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt, work, -1, rwork, iwork, 0) lwork = int(abs(work[0])) work = num.zeros((lwork,), t) results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt, work, lwork, rwork, iwork, 0) else: lapack_routine = lapack_lite2.dgesdd lwork = 1 work = num.zeros((lwork,), t) results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt, work, -1, iwork, 0) lwork = int(work[0]) work = num.zeros((lwork,), t) results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt, work, lwork, iwork, 0) if results['info'] > 0: raise LinAlgError, 'SVD did not converge' return multiarray.transpose(u), s, multiarray.transpose(vt) # why copy here?
def eigenvalues(a): _assertRank2(a) _assertSquareness(a) t =_commonType(a) real_t = _array_type[0][_array_precision[t]] a = _fastCopyAndTranspose(t, a) n = a.getshape()[0] dummy = num.zeros((1,), t) if _array_kind[t] == 1: # Complex routines take different arguments lapack_routine = lapack_lite2.zgeev w = num.zeros((n,), t) rwork = num.zeros((n,),real_t) lwork = 1 work = num.zeros((lwork,), t) results = lapack_routine('N', 'N', n, a, n, w, dummy, 1, dummy, 1, work, -1, rwork, 0) lwork = int(abs(work[0])) work = num.zeros((lwork,), t) results = lapack_routine('N', 'N', n, a, n, w, dummy, 1, dummy, 1, work, lwork, rwork, 0) else: lapack_routine = lapack_lite2.dgeev wr = num.zeros((n,), t) wi = num.zeros((n,), t) lwork = 1 work = num.zeros((lwork,), t) results = lapack_routine('N', 'N', n, a, n, wr, wi, dummy, 1, dummy, 1, work, -1, 0) lwork = int(work[0]) work = num.zeros((lwork,), t) results = lapack_routine('N', 'N', n, a, n, wr, wi, dummy, 1, dummy, 1, work, lwork, 0) if num.logical_and.reduce(num.equal(wi, 0.)): w = wr else: w = wr+1j*wi if results['info'] > 0: raise LinAlgError, 'Eigenvalues did not converge' return w
def _correlate2d_fft(data0, kernel0, output=None, mode="nearest", cval=0.0): """_correlate2d_fft does 2d correlation of 'data' with 'kernel', storing the result in 'output' using the FFT to perform the correlation. supported 'mode's include: 'nearest' elements beyond boundary come from nearest edge pixel. 'wrap' elements beyond boundary come from the opposite array edge. 'reflect' elements beyond boundary come from reflection on same array edge. 'constant' elements beyond boundary are set to 'cval' """ shape = data0.shape kshape = kernel0.shape oversized = (num.array(shape) + num.array(kshape)) dy = kshape[0] // 2 dx = kshape[1] // 2 kernel = num.zeros(oversized, typecode=num.Float64) kernel[:kshape[0], :kshape[1]] = kernel0[::-1, :: -1] # convolution <-> correlation data = iraf_frame.frame(data0, oversized, mode=mode, cval=cval) complex_result = (isinstance(data, _nt.ComplexType) or isinstance(kernel, _nt.ComplexType)) Fdata = fft.fft2d(data) del data Fkernel = fft.fft2d(kernel) del kernel num.multiply(Fdata, Fkernel, Fdata) del Fkernel if complex_result: convolved = fft.inverse_fft2d(Fdata, s=oversized) else: convolved = fft.inverse_real_fft2d(Fdata, s=oversized) result = convolved[kshape[0] - 1:shape[0] + kshape[0] - 1, kshape[1] - 1:shape[1] + kshape[1] - 1] if output is not None: output._copyFrom(result) else: return result
def _correlate2d_fft(data0, kernel0, output=None, mode="nearest", cval=0.0): """_correlate2d_fft does 2d correlation of 'data' with 'kernel', storing the result in 'output' using the FFT to perform the correlation. supported 'mode's include: 'nearest' elements beyond boundary come from nearest edge pixel. 'wrap' elements beyond boundary come from the opposite array edge. 'reflect' elements beyond boundary come from reflection on same array edge. 'constant' elements beyond boundary are set to 'cval' """ shape = data0.shape kshape = kernel0.shape oversized = (num.array(shape) + num.array(kshape)) dy = kshape[0] // 2 dx = kshape[1] // 2 kernel = num.zeros(oversized, typecode=num.Float64) kernel[:kshape[0], :kshape[1]] = kernel0[::-1,::-1] # convolution <-> correlation data = iraf_frame.frame(data0, oversized, mode=mode, cval=cval) complex_result = (isinstance(data, _nt.ComplexType) or isinstance(kernel, _nt.ComplexType)) Fdata = fft.fft2d(data) del data Fkernel = fft.fft2d(kernel) del kernel num.multiply(Fdata, Fkernel, Fdata) del Fkernel if complex_result: convolved = fft.inverse_fft2d( Fdata, s=oversized) else: convolved = fft.inverse_real_fft2d( Fdata, s=oversized) result = convolved[ kshape[0]-1:shape[0]+kshape[0]-1, kshape[1]-1:shape[1]+kshape[1]-1 ] if output is not None: output._copyFrom( result ) else: return result
def frame_wrap(a, shape, cval=None): """frame_wrap creates an oversized copy of 'a' with new 'shape' and the contents of 'a' in the center. The boundary pixels are wrapped around to the opposite edge pixels in 'a'. >>> a = num.arange(16, shape=(4,4)) >>> frame_wrap(a, (8,8)) array([[10, 11, 8, 9, 10, 11, 8, 9], [14, 15, 12, 13, 14, 15, 12, 13], [ 2, 3, 0, 1, 2, 3, 0, 1], [ 6, 7, 4, 5, 6, 7, 4, 5], [10, 11, 8, 9, 10, 11, 8, 9], [14, 15, 12, 13, 14, 15, 12, 13], [ 2, 3, 0, 1, 2, 3, 0, 1], [ 6, 7, 4, 5, 6, 7, 4, 5]]) """ b = num.zeros(shape, typecode=a.type()) delta = (num.array(b.shape) - num.array(a.shape)) dy = delta[0] // 2 dx = delta[1] // 2 my = a.shape[0] + dy mx = a.shape[1] + dx sy = delta[0] - dy sx = delta[1] - dx b[dy:my, dx:mx] = a # center b[:dy,dx:mx] = a[-dy:,:] # top b[my:,dx:mx] = a[:sy,:] # bottom b[dy:my,:dx] = a[:,-dx:] # left b[dy:my,mx:] = a[:, :sx] # right b[:dy,:dx] = a[-dy:,-dx:] # topleft b[:dy,mx:] = a[-dy:,:sx ] # topright b[my:,:dx] = a[:sy, -dx:] # bottomleft b[my:,mx:] = a[:sy, :sx] # bottomright return b
def frame_reflect(a, shape, cval=None): """frame_reflect creates an oversized copy of 'a' with new 'shape' and the contents of 'a' in the center. The boundary pixels are reflected from the nearest edge pixels in 'a'. >>> a = num.arange(16, shape=(4,4)) >>> frame_reflect(a, (8,8)) array([[ 5, 4, 4, 5, 6, 7, 7, 6], [ 1, 0, 0, 1, 2, 3, 3, 2], [ 1, 0, 0, 1, 2, 3, 3, 2], [ 5, 4, 4, 5, 6, 7, 7, 6], [ 9, 8, 8, 9, 10, 11, 11, 10], [13, 12, 12, 13, 14, 15, 15, 14], [13, 12, 12, 13, 14, 15, 15, 14], [ 9, 8, 8, 9, 10, 11, 11, 10]]) """ b = num.zeros(shape, typecode=a.type()) delta = (num.array(b.shape) - num.array(a.shape)) dy = delta[0] // 2 dx = delta[1] // 2 my = a.shape[0] + dy mx = a.shape[1] + dx sy = delta[0] - dy sx = delta[1] - dx b[dy:my, dx:mx] = a # center b[:dy,dx:mx] = a[:dy,:][::-1,:] # top b[my:,dx:mx] = a[-sy:,:][::-1,:] # bottom b[dy:my,:dx] = a[:,:dx][:,::-1] # left b[dy:my,mx:] = a[:,-sx:][:,::-1] # right b[:dy,:dx] = a[:dy,:dx][::-1,::-1] # topleft b[:dy,mx:] = a[:dy,-sx:][::-1,::-1] # topright b[my:,:dx] = a[-sy:,:dx][::-1,::-1] # bottomleft b[my:,mx:] = a[-sy:,-sx:][::-1,::-1] # bottomright return b
def frame_wrap(a, shape, cval=None): """frame_wrap creates an oversized copy of 'a' with new 'shape' and the contents of 'a' in the center. The boundary pixels are wrapped around to the opposite edge pixels in 'a'. >>> a = num.arange(16, shape=(4,4)) >>> frame_wrap(a, (8,8)) array([[10, 11, 8, 9, 10, 11, 8, 9], [14, 15, 12, 13, 14, 15, 12, 13], [ 2, 3, 0, 1, 2, 3, 0, 1], [ 6, 7, 4, 5, 6, 7, 4, 5], [10, 11, 8, 9, 10, 11, 8, 9], [14, 15, 12, 13, 14, 15, 12, 13], [ 2, 3, 0, 1, 2, 3, 0, 1], [ 6, 7, 4, 5, 6, 7, 4, 5]]) """ b = num.zeros(shape, typecode=a.type()) delta = (num.array(b.shape) - num.array(a.shape)) dy = delta[0] // 2 dx = delta[1] // 2 my = a.shape[0] + dy mx = a.shape[1] + dx sy = delta[0] - dy sx = delta[1] - dx b[dy:my, dx:mx] = a # center b[:dy, dx:mx] = a[-dy:, :] # top b[my:, dx:mx] = a[:sy, :] # bottom b[dy:my, :dx] = a[:, -dx:] # left b[dy:my, mx:] = a[:, :sx] # right b[:dy, :dx] = a[-dy:, -dx:] # topleft b[:dy, mx:] = a[-dy:, :sx] # topright b[my:, :dx] = a[:sy, -dx:] # bottomleft b[my:, mx:] = a[:sy, :sx] # bottomright return b
def frame_nearest(a, shape, cval=None): """frame_nearest creates an oversized copy of 'a' with new 'shape' and the contents of 'a' in the center. The boundary pixels are copied from the nearest edge pixel in 'a'. >>> a = num.arange(16, shape=(4,4)) >>> frame_nearest(a, (8,8)) array([[ 0, 0, 0, 1, 2, 3, 3, 3], [ 0, 0, 0, 1, 2, 3, 3, 3], [ 0, 0, 0, 1, 2, 3, 3, 3], [ 4, 4, 4, 5, 6, 7, 7, 7], [ 8, 8, 8, 9, 10, 11, 11, 11], [12, 12, 12, 13, 14, 15, 15, 15], [12, 12, 12, 13, 14, 15, 15, 15], [12, 12, 12, 13, 14, 15, 15, 15]]) """ b = num.zeros(shape, typecode=a.type()) delta = (num.array(b.shape) - num.array(a.shape)) dy = delta[0] // 2 dx = delta[1] // 2 my = a.shape[0] + dy mx = a.shape[1] + dx b[dy:my, dx:mx] = a # center b[:dy,dx:mx] = a[0:1,:] # top b[my:,dx:mx] = a[-1:,:] # bottom b[dy:my, :dx] = a[:, 0:1] # left b[dy:my, mx:] = a[:, -1:] # right b[:dy, :dx] = a[0,0] # topleft b[:dy, mx:] = a[0,-1] # topright b[my:, :dx] = a[-1, 0] # bottomleft b[my:, mx:] = a[-1, -1] # bottomright return b
def frame_nearest(a, shape, cval=None): """frame_nearest creates an oversized copy of 'a' with new 'shape' and the contents of 'a' in the center. The boundary pixels are copied from the nearest edge pixel in 'a'. >>> a = num.arange(16, shape=(4,4)) >>> frame_nearest(a, (8,8)) array([[ 0, 0, 0, 1, 2, 3, 3, 3], [ 0, 0, 0, 1, 2, 3, 3, 3], [ 0, 0, 0, 1, 2, 3, 3, 3], [ 4, 4, 4, 5, 6, 7, 7, 7], [ 8, 8, 8, 9, 10, 11, 11, 11], [12, 12, 12, 13, 14, 15, 15, 15], [12, 12, 12, 13, 14, 15, 15, 15], [12, 12, 12, 13, 14, 15, 15, 15]]) """ b = num.zeros(shape, typecode=a.type()) delta = (num.array(b.shape) - num.array(a.shape)) dy = delta[0] // 2 dx = delta[1] // 2 my = a.shape[0] + dy mx = a.shape[1] + dx b[dy:my, dx:mx] = a # center b[:dy, dx:mx] = a[0:1, :] # top b[my:, dx:mx] = a[-1:, :] # bottom b[dy:my, :dx] = a[:, 0:1] # left b[dy:my, mx:] = a[:, -1:] # right b[:dy, :dx] = a[0, 0] # topleft b[:dy, mx:] = a[0, -1] # topright b[my:, :dx] = a[-1, 0] # bottomleft b[my:, mx:] = a[-1, -1] # bottomright return b
def frame_reflect(a, shape, cval=None): """frame_reflect creates an oversized copy of 'a' with new 'shape' and the contents of 'a' in the center. The boundary pixels are reflected from the nearest edge pixels in 'a'. >>> a = num.arange(16, shape=(4,4)) >>> frame_reflect(a, (8,8)) array([[ 5, 4, 4, 5, 6, 7, 7, 6], [ 1, 0, 0, 1, 2, 3, 3, 2], [ 1, 0, 0, 1, 2, 3, 3, 2], [ 5, 4, 4, 5, 6, 7, 7, 6], [ 9, 8, 8, 9, 10, 11, 11, 10], [13, 12, 12, 13, 14, 15, 15, 14], [13, 12, 12, 13, 14, 15, 15, 14], [ 9, 8, 8, 9, 10, 11, 11, 10]]) """ b = num.zeros(shape, typecode=a.type()) delta = (num.array(b.shape) - num.array(a.shape)) dy = delta[0] // 2 dx = delta[1] // 2 my = a.shape[0] + dy mx = a.shape[1] + dx sy = delta[0] - dy sx = delta[1] - dx b[dy:my, dx:mx] = a # center b[:dy, dx:mx] = a[:dy, :][::-1, :] # top b[my:, dx:mx] = a[-sy:, :][::-1, :] # bottom b[dy:my, :dx] = a[:, :dx][:, ::-1] # left b[dy:my, mx:] = a[:, -sx:][:, ::-1] # right b[:dy, :dx] = a[:dy, :dx][::-1, ::-1] # topleft b[:dy, mx:] = a[:dy, -sx:][::-1, ::-1] # topright b[my:, :dx] = a[-sy:, :dx][::-1, ::-1] # bottomleft b[my:, mx:] = a[-sy:, -sx:][::-1, ::-1] # bottomright return b
def eigenvectors(a): """eigenvectors(a) returns u,v where u is the eigenvalues and v is a matrix of eigenvectors with vector v[i] corresponds to eigenvalue u[i]. Satisfies the equation dot(a, v[i]) = u[i]*v[i] """ _assertRank2(a) _assertSquareness(a) t =_commonType(a) real_t = _array_type[0][_array_precision[t]] a = _fastCopyAndTranspose(t, a) n = a.getshape()[0] dummy = num.zeros((1,), t) if _array_kind[t] == 1: # Complex routines take different arguments lapack_routine = lapack_lite2.zgeev w = num.zeros((n,), t) v = num.zeros((n,n), t) lwork = 1 work = num.zeros((lwork,),t) rwork = num.zeros((2*n,),real_t) results = lapack_routine('N', 'V', n, a, n, w, dummy, 1, v, n, work, -1, rwork, 0) lwork = int(abs(work[0])) work = num.zeros((lwork,),t) results = lapack_routine('N', 'V', n, a, n, w, dummy, 1, v, n, work, lwork, rwork, 0) else: lapack_routine = lapack_lite2.dgeev wr = num.zeros((n,), t) wi = num.zeros((n,), t) vr = num.zeros((n,n), t) lwork = 1 work = num.zeros((lwork,),t) results = lapack_routine('N', 'V', n, a, n, wr, wi, dummy, 1, vr, n, work, -1, 0) lwork = int(work[0]) work = num.zeros((lwork,),t) results = lapack_routine('N', 'V', n, a, n, wr, wi, dummy, 1, vr, n, work, lwork, 0) if num.logical_and.reduce(num.equal(wi, 0.)): w = wr v = vr else: w = wr+1j*wi v = num.array(vr,type=num.Complex) ind = num.nonzero( num.equal( num.equal(wi,0.0) # true for real e-vals ,0) # true for complex e-vals ) # indices of complex e-vals for i in range(len(ind)/2): v[ind[2*i]] = vr[ind[2*i]] + 1j*vr[ind[2*i+1]] v[ind[2*i+1]] = vr[ind[2*i]] - 1j*vr[ind[2*i+1]] if results['info'] > 0: raise LinAlgError, 'Eigenvalues did not converge' return w,v
def linear_least_squares(a, b, rcond=1.e-10): """solveLinearLeastSquares(a,b) returns x,resids,rank,s where x minimizes 2-norm(|b - Ax|) resids is the sum square residuals rank is the rank of A s is an rank of the singual values of A in desending order If b is a matrix then x is also a matrix with corresponding columns. If the rank of A is less than the number of columns of A or greater than the numer of rows, then residuals will be returned as an empty array otherwise resids = sum((b-dot(A,x)**2). Singular values less than s[0]*rcond are treated as zero. """ one_eq = len(b.getshape()) == 1 if one_eq: b = b[:, num.NewAxis] _assertRank2(a, b) m = a.getshape()[0] n = a.getshape()[1] n_rhs = b.getshape()[1] ldb = max(n,m) if m != b.getshape()[0]: raise LinAlgError, 'Incompatible dimensions' t =_commonType(a, b) real_t = _array_type[0][_array_precision[t]] bstar = num.zeros((ldb,n_rhs),t) bstar[:b.getshape()[0],:n_rhs] = copy.copy(b) a,bstar = _castCopyAndTranspose(t, a, bstar) s = num.zeros((min(m,n),),real_t) nlvl = max( 0, int( math.log( float(min( m,n ))/2. ) ) + 1 ) iwork = num.zeros((3*min(m,n)*nlvl+11*min(m,n),), 'l') if _array_kind[t] == 1: # Complex routines take different arguments lapack_routine = lapack_lite2.zgelsd lwork = 1 rwork = num.zeros((lwork,), real_t) work = num.zeros((lwork,),t) results = lapack_routine( m, n, n_rhs, a, m, bstar,ldb , s, rcond, 0,work,-1,rwork,iwork,0 ) lwork = int(abs(work[0])) rwork = num.zeros((lwork,),real_t) a_real = num.zeros((m,n),real_t) bstar_real = num.zeros((ldb,n_rhs,),real_t) results = lapack_lite2.dgelsd( m, n, n_rhs, a_real, m, bstar_real,ldb , s, rcond, 0,rwork,-1,iwork,0 ) lrwork = int(rwork[0]) work = num.zeros((lwork,), t) rwork = num.zeros((lrwork,), real_t) results = lapack_routine( m, n, n_rhs, a, m, bstar,ldb , s, rcond, 0,work,lwork,rwork,iwork,0 ) else: lapack_routine = lapack_lite2.dgelsd lwork = 1 work = num.zeros((lwork,), t) results = lapack_routine( m, n, n_rhs, a, m, bstar,ldb , s, rcond, 0,work,-1,iwork,0 ) lwork = int(work[0]) work = num.zeros((lwork,), t) results = lapack_routine( m, n, n_rhs, a, m, bstar,ldb , s, rcond, 0,work,lwork,iwork,0 ) if results['info'] > 0: raise LinAlgError, 'SVD did not converge in Linear Least Squares' resids = num.array([],type=t) if one_eq: x = copy.copy(num.ravel(bstar)[:n]) if (results['rank']==n) and (m>n): resids = num.array([num.sum((num.ravel(bstar)[n:])**2)]) else: x = copy.copy(num.transpose(bstar)[:n,:]) if (results['rank']==n) and (m>n): resids = copy.copy(num.sum((num.transpose(bstar)[n:,:])**2)) return x,resids,results['rank'],copy.copy(s[:min(n,m)])
def qr_decomposition(a, mode='full'): """ calculates A=QR, Q orthonormal, R upper triangle matrix. mode: 'full' ==> (Q,R) as return value 'r' ==> (None, R) as return value 'economic' ==> (None, A') where the diagonal + upper triangle part of A' is R. This is faster if one only requires R. """ _assertRank2(a) t=_commonType(a) m = a.getshape()[0] n = a.getshape()[1] mn = min(m,n) tau = num.zeros((mn,), t) # a: convert num storing order to fortran storing order a = _castCopyAndTranspose(t, a) if _array_kind[t] == 1: lapack_routine = lapack_lite2.zgeqrf routine_name='ZGEQRF' else: lapack_routine = lapack_lite2.dgeqrf routine_name='DGEQRF' # calculate optimal size of work data 'work' lwork = 1 work = num.zeros((lwork,), t) results=lapack_routine(m, n, a, m, tau, work, -1, 0) if results['info'] > 0: raise LinAlgError, '%s returns %d' % (routine_name, results['info']) # do qr decomposition lwork = int(abs(work[0])) work = num.zeros((lwork,),t) results=lapack_routine(m, n, a, m, tau, work, lwork, 0) if results['info'] > 0: raise LinAlgError, '%s returns %d' % (routine_name, results['info']) # atemp: convert fortrag storing order to num storing order atemp = num.transpose(a) # economic mode if mode[0]=='e': return None, atemp # generate r r = num.zeros((mn,n), t) for i in range(mn): r[i, i:] = atemp[i, i:] # 'r'-mode, that is, calculate only r if mode[0]=='r': return None, r # from here on: build orthonormal matrix q from a if _array_kind[t] == 1: lapack_routine = lapack_lite2.zungqr routine_name = "ZUNGQR" else: lapack_routine = lapack_lite2.dorgqr routine_name = "DORGQR" # determine optimal lwork lwork = 1 work=num.zeros((lwork,), t) results=lapack_routine(m,mn,mn, a, m, tau, work, -1, 0) if results['info'] > 0: raise LinAlgError, '%s returns %d' % (routine_name, results['info']) # compute q lwork = int(abs(work[0])) work=num.zeros((lwork,), t) results=lapack_routine(m,mn,mn, a, m, tau, work, lwork, 0) if results['info'] > 0: raise LinAlgError, '%s returns %d' % (routine_name, results['info']) q = num.transpose(a[:mn,:]) return q,r