def testPoisson2D(self): # Solve 1D Poisson systems of various sizes for n in self.n: h = 1.0 / n lmbd_min = 4.0 / h / h * (sin(pi * h / 2.0)**2 + sin(pi * h / 2.0)**2) lmbd_max = 4.0 / h / h * (sin((n - 1) * pi * h / 2.0)**2 + sin( (n - 1) * pi * h / 2.0)**2) cond = lmbd_max / lmbd_min tol = cond * self.eps n2 = n * n A = LinearOperator(n2, n2, lambda x: Poisson2dMatvec(x), symmetric=True) e = np.ones(n2) rhs = A * e cg = CG(A, matvec_max=2 * n2, outputStream=sys.stderr) cg.solve(rhs) err = np.linalg.norm(e - cg.bestSolution) / n sys.stderr.write(self.fmt % (n2, cg.nMatvec, cg.residNorm, err)) # Adjust tol because allclose() uses infinity norm self.assertTrue(np.allclose(e, cg.bestSolution, rtol=err * n))
def __init__(self, k, s, w=None, niter=10, **kwargs): """ TODO: empty docsting """ # get geometry ncoils = len(s) ndims = s[0].ndim ngrid = s[0].shape nknots = k.size / ndims k = k.reshape([nknots, ndims]) # set shared plan for each ForFT and AdjFT operations self._plan = get_plan(nknots, ngrid) self._plan.precompute(k) # define linear operators for iterative SENSE algo if w is not None: D_block = DiagonalOperator(diag=w.ravel()) else: D_block = IdentityOperator(nargin=nknots) blocks = [D_block] * ncoils D = BlockDiagonalLinearOperator(blocks=blocks) diag = np.sqrt(np.sum([_s**2 for _s in s], axis=0)) I = DiagonalOperator(diag=diag.ravel()) F = LinearOperator( nargin=self._plan.N_total, nargout=self._plan.M, matvec=lambda u: self._plan.forward(f_hat=u), matvec_transp=lambda u: self._plan.adjoint(f=u).ravel(), dtype=np.complex128, ) blocks = [[F * DiagonalOperator(diag=_s.ravel())] for _s in s] E = BlockLinearOperator(blocks=blocks) self.D = D self.E = E self.I = I self.m = None self.rhs = None self.shape = (ngrid, nknots * ncoils) self.solution = None self.solver = CG(op=I * E.T * D * E * I, matvec_max=niter)
def testPoisson1D(self): # Solve 1D Poisson systems of various sizes for n in self.n: lmbd_min = 4.0 * sin(pi/2.0/n) ** 2 lmbd_max = 4.0 * sin((n-1)*pi/2.0/n) ** 2 cond = lmbd_max/lmbd_min tol = cond * self.eps e = np.ones(n) rhs = Poisson1dMatvec(e) cg = CG(Poisson1dMatvec, matvec_max=2*n, outputStream=sys.stderr) cg.solve(rhs) err = np.linalg.norm(e-cg.bestSolution)/sqrt(n) sys.stderr.write(self.fmt % (n, cg.nMatvec, cg.residNorm, err)) self.failUnless(np.allclose(e, cg.bestSolution, rtol=tol))
def testPoisson1D(self): # Solve 1D Poisson systems of various sizes for n in self.n: lmbd_min = 4.0 * sin(pi/2.0/n) ** 2 lmbd_max = 4.0 * sin((n-1)*pi/2.0/n) ** 2 cond = lmbd_max/lmbd_min tol = cond * self.eps A = LinearOperator(n, n, lambda x: Poisson1dMatvec(x), symmetric=True) e = np.ones(n) rhs = A * e cg = CG(A, matvec_max=2*n, outputStream=sys.stderr) cg.solve(rhs) err = np.linalg.norm(e-cg.bestSolution)/sqrt(n) sys.stderr.write(self.fmt % (n, cg.nMatvec, cg.residNorm, err)) self.assertTrue(np.allclose(e, cg.bestSolution, rtol=tol))
def testPoisson1D(self): # Solve 1D Poisson systems of various sizes for n in self.n: lmbd_min = 4.0 * sin(pi / 2.0 / n)**2 lmbd_max = 4.0 * sin((n - 1) * pi / 2.0 / n)**2 cond = lmbd_max / lmbd_min tol = cond * self.eps A = LinearOperator(n, n, lambda x: Poisson1dMatvec(x), symmetric=True) e = np.ones(n) rhs = A * e cg = CG(A, matvec_max=2 * n, outputStream=sys.stderr) cg.solve(rhs) err = np.linalg.norm(e - cg.bestSolution) / sqrt(n) sys.stderr.write(self.fmt % (n, cg.nMatvec, cg.residNorm, err)) self.assertTrue(np.allclose(e, cg.bestSolution, rtol=tol))
def testPoisson2D(self): # Solve 1D Poisson systems of various sizes for n in self.n: h = 1.0/n lmbd_min = 4.0/h/h*(sin(pi*h/2.0)**2 + sin(pi*h/2.0)**2) lmbd_max = 4.0/h/h*(sin((n-1)*pi*h/2.0)**2 + sin((n-1)*pi*h/2.0)**2) cond = lmbd_max/lmbd_min tol = cond * self.eps n2 = n*n e = np.ones(n2) rhs = Poisson2dMatvec(e) cg = CG(Poisson2dMatvec, matvec_max=2*n2, outputStream=sys.stderr) cg.solve(rhs) err = np.linalg.norm(e-cg.bestSolution)/n sys.stderr.write(self.fmt % (n2, cg.nMatvec, cg.residNorm, err)) # Adjust tol because allclose() uses infinity norm self.failUnless(np.allclose(e, cg.bestSolution, rtol=err*n))
def testPoisson2D(self): # Solve 1D Poisson systems of various sizes for n in self.n: h = 1.0/n lmbd_min = 4.0/h/h*(sin(pi*h/2.0)**2 + sin(pi*h/2.0)**2) lmbd_max = 4.0/h/h*(sin((n-1)*pi*h/2.0)**2 + sin((n-1)*pi*h/2.0)**2) cond = lmbd_max/lmbd_min tol = cond * self.eps n2 = n*n A = LinearOperator(n2, n2, lambda x: Poisson2dMatvec(x), symmetric=True) e = np.ones(n2) rhs = A * e cg = CG(A, matvec_max=2*n2, outputStream=sys.stderr) cg.solve(rhs) err = np.linalg.norm(e-cg.bestSolution)/n sys.stderr.write(self.fmt % (n2, cg.nMatvec, cg.residNorm, err)) # Adjust tol because allclose() uses infinity norm self.assertTrue(np.allclose(e, cg.bestSolution, rtol=err*n))