o & i…ã@sVddlmZddlmZddlZddlmZdd d „Zdd d „ZGdd„dejƒZ dS)é)Ú annotations)ÚCallableN)ÚnnÚx1ú torch.TensorÚx2ÚreturncCs<t |¡rt |¡sJdƒ‚t | ¡|¡St ||¡S)zD Complex dot product between tensors x1 and x2: sum(x1.*x2) zx1 and x2 must both be complex)ÚtorchÚ is_complexÚsumÚconj)rr©r új/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/layers/conjugate_gradient.pyÚ_zdots rÚxcCst||ƒ}t |¡r |jS|S)z9 Complex dot product between tensor x and itself )rr r Úreal)rÚresr r rÚ _zdot_singles  rcs6eZdZdZd‡fdd„ Zddd„Zddd„Z‡ZS)ÚConjugateGradientaÍ Congugate Gradient (CG) solver for linear systems Ax = y. For linear_op that is positive definite and self-adjoint, CG is guaranteed to converge CG is often used to solve linear systems of the form Ax = y, where A is too large to store explicitly, but can be computed via a linear operator. As a result, here we won't set A explicitly as a matrix, but rather as a linear operator. For example, A could be a FFT/IFFT operation Ú linear_oprÚnum_iterÚintcstƒ ¡||_||_dS)zs Args: linear_op: Linear operator num_iter: Number of iterations to run CG N)ÚsuperÚ__init__rr)Úselfrr©Ú __class__r rr7s  zConjugateGradient.__init__rrÚpÚrÚrsoldrú=tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]c Cs`| |¡}t||ƒ}||}|||}|||}t|ƒ}||} |}| ||}||||fS)a9 perform one iteration of the CG method. It takes the current solution x, the current search direction p, the current residual r, and the old residual norm rsold as inputs. Then it computes the new solution, search direction, residual, and residual norm, and returns them. )rrr) rrrrrÚdyZp_dot_dyÚalphaZrsnewÚbetar r rÚupdateBs     zConjugateGradient.updateÚycCsT|| |¡}t|ƒ}|}t|jƒD]}| ||||¡\}}}}|dkr'|Sq|S)a  run conjugate gradient for num_iter iterations to solve Ax = y Args: x: tensor (real or complex); Initial guess for linear system Ax = y. The size of x should be applicable to the linear operator. For example, if the linear operator is FFT, then x is HCHW; if the linear operator is a matrix multiplication, then x is a vector y: tensor (real or complex); Measurement. Same size as x Returns: x: Solution to Ax = y g»½×Ùß|Û=)rrÚrangerr$)rrr%rrrÚ_ir r rÚforwardWsþzConjugateGradient.forward)rrrr) rrrrrrrrrr )rrr%rrr)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr$r(Ú __classcell__r r rrr*s  r)rrrrrr)rrrr) Ú __future__rÚtypingrr rrrÚModulerr r r rÚs