where $f_j = f(x_j)$ are the values of $f$ at equidistant sampling points $(x_j)_{j=1}^{N-1}$ with distance $h = x_1 - x_0$. As a matrix, this can simply be written as

$$V = h \begin{pmatrix}

1 & 0 & 0 & 0 & \ldots & 0 \\

1 & 1 & 0 & 0 & \ldots & 0 \\

1 & 1 & 1 & 0 & \ldots & 0 \\

\vdots & & & \ddots & & \\

1 & 1 & 1 & 1 & \ldots & 1

\end{pmatrix}$$

- Implement the Volterra operator with a parameter `h :: Double` as an instance of the `Operator` type class from the lecture, but with the `Arr` type alias replaced by

```haskell

typeArrr=ArrayrDIM1Double

```

Though it is possible to do this using a matrix formulation and e.g. `mmultS`, this unneccessarily inefficient. A better possibility is to note the connection between the Volterra operator and Haskell's `scanl` function. Moreover, there is an implementation of `scanl` for unboxed `Vector`s from `Data.Vector.Unboxed` (*not* HMatrix's `Numeric.Container`) as well as conversion functions `toUnboxed` and `fromUnboxed` for Repa `U` arrays to and from these `Vector`s. Use these to implement the operator.

*Note*: The adjoint (i.e. transpose) of the operator can be found most easily by noting the similarity between the Volterra matrix above and its transpose: the adjoint simply proceeds in the opposite direction.

- Use the regularized CG implementation from the lecture to compute regularized *derivatives* of the functions

$$\begin{align}

f_1(x) &= e^{-x^2} \\

f_2(x) &= \text{sgn}(x) = \begin{cases} -1 & x < 0 \\ 1 & x \geq 0 \end{cases}

\end{align}$$

sampled at 200 points between -2 and 2 and distorted by random noise between -0.1 and 0.1. The CG tolerance should be set suffieciently low (e.g. 1e-6).

- Plot the results together with the noisy functions and, in the case of $f_1$, the analytical derivative.

{"nbformat_minor":0,"nbformat":4,"cells":[{"metadata":{"hidden":false},"source":[" # Image deconvolution\n","\n"," ## Task\n","\n","Given a blurred image $y$, find the original image $x$, where\n","\n","$$T x = y,$$\n","\n","and $T$ is a blurring operator, e.g. an operator assigns to each pixel an\n","average of all pixels in some neighbourhood.\n","\n","Depending on the blurring operator, it is not clear in general whether this\n","problem has a solution at all. A frequent approach is to replace the problem by\n","\n","$$\\text{Minimize } {\\lVert T x - y \\rVert}^2 \\text{ with respect to } x,$$\n","\n","or equivalently\n","\n","$$T^* T x = T^* y.$$\n","\n","This equation can e.g. be solved using CG, but it turns out to be numerically\n","unstable: if the blurred image $y$ is distorted with some noise, the\n","reconstruction $x$ has significantly amplified noise.\n","\n"," ## Regularization\n","\n","A simple way to stablilize the problem is Tikhonov regularization, which\n","replaces the problem by\n","\n","$$\\text{Minimize } {\\lVert T x - y \\rVert}^2 + \\alpha {\\lVert x \\rVert}^2 \\text{\n","with respect to } x,$$\n","\n","for some *regularization parameter* $\\alpha > 0$ or equivalently\n","\n","$$(T^* T + \\alpha \\mathbb{1}) x = T^* y.$$\n","\n","$\\alpha$ has to be chosen large enough to avoid the instabilities of the\n","problem, but small enough in order not to change the problem too much.\n","\n","There are several ways to choose the parameter (semi-)automatically depending on\n","the blurred and distorted image $y$ and the noise level. Mathematically, one is\n","interested in the question under what circumstances and how fast the regularized\n","reconstruction converge to the true solution.\n","\n","For this example, we will simply choose a regularization parameter manually.\n","\n"," ## Imports and basics\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["{-# OPTIONS_GHC -O3 #-}\n","\n","{-# LANGUAGE BangPatterns #-}\n","{-# LANGUAGE ScopedTypeVariables #-}\n","\n","import Control.Monad.Identity\n","import Data.Array.Repa as R hiding ((++))\n","import Data.Array.Repa.Eval\n","import Data.Array.Repa.Stencil\n","import Data.Array.Repa.Stencil.Dim2\n","import Data.Array.Repa.Algorithms.Pixel (doubleLuminanceOfRGB8, rgb8OfGreyDouble)\n","import Data.Array.Repa.IO.BMP (readImageFromBMP, writeImageToBMP)\n","import Data.Random.Normal (normalsIO')\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":["Alias, for convenience.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["type Arr r = Array r DIM2 Double\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":["Loading and saving of images -- rather uninteresting.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["loadImage :: FilePath -> IO (Arr U)\n","loadImage file = do\n"," loaded <- readImageFromBMP file\n"," case loaded of\n"," Left err -> error $ show err\n"," Right img -> computeP $ R.map doubleLuminanceOfRGB8 img\n","\n","saveImage :: FilePath -> Arr U -> IO ()\n","saveImage file img = do\n"," !m <- foldAllP max 0 img\n"," !img' <- computeP $ R.map (rgb8OfGreyDouble . (/ m)) img\n"," writeImageToBMP file img'\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":[" ## Operator type class\n","\n","We need functions for both the blurring operator and its adjoint. This can be\n","done nicely using a typeclass.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["class Operator a where\n"," evalOp :: a -> Arr U -> Arr D\n"," adjointOp :: a -> Arr U -> Arr D\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":[" ## CG solver\n","\n","The Repa CG solver for the regularized normal equation\n","\n","$$(T^* T + \\alpha \\mathbb{1}) x = T^* y.$$\n","\n","The code tries to use as few operator evaluations as possible.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["data CGState = CGState { cgx :: Arr U\n"," , cgp :: Arr U\n"," , cgr :: Arr U\n"," , cgr2 :: Double\n"," }\n","\n","cgreg :: Operator a => a -> Double -> Arr U -> Arr U -> [CGState]\n","cgreg op reg rhs initial =\n"," runIdentity $ do\n"," (res :: Arr U) <- computeP $ rhs -^ evalOp op initial\n"," rInit <- computeP $ adjointOp op res\n"," r2Init <- normSquaredP rInit\n"," return $ iterate cgStep (CGState initial rInit rInit r2Init)\n"," where normSquaredP = sumAllP . R.map (^(2::Int))\n"," scale a = R.map (* a)\n"," cgStep :: CGState -> CGState\n"," cgStep (CGState x p r r2) =\n"," runIdentity $ do\n"," !(q :: Arr U) <- computeP $ evalOp op p\n"," !p2 <- normSquaredP p\n"," !q2 <- normSquaredP q\n"," let alpha = r2 / (q2 + reg*p2)\n"," !x' <- computeP $ x +^ scale alpha p\n"," !(s :: Arr U) <- computeP $ adjointOp op q\n"," !r' <- computeP $ r -^ scale alpha (s +^ scale reg p)\n"," !r2' <- normSquaredP r'\n"," let beta = r2' / r2\n"," !p' <- computeP $ r' +^ scale beta p\n"," return $ CGState x' p' r' r2'\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":["`cgreg` returns a lazy list of all iterates. `takeUntil` is uses to implement\n","the stopping rule; it is similar to `takeWhile`, but also returns the final\n","iterate.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["takeUntil :: (a -> Bool) -> [a] -> [a]\n","takeUntil _ [] = []\n","takeUntil predicate (x:xs)\n"," | predicate x = [x]\n"," | otherwise = x : takeUntil predicate xs\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":["We want to be able to output some information about the iterates, i.e. run an IO\n","action over the list of iterates, before returning the final one. One option\n","would be something like\n","\n","```haskell\n","fmap last . forM iterates $ \\it -> do [...]\n","```\n","\n","However, this would perform the `fmap last` only *after* the IO actions are\n","done, therfore retaining the entire list of iterates in memory.\n","\n","We want all but the final iterate to be garbage collected directly after\n","printing information. This can be done using a monadic fold, where we do not\n","actually accumulate a result but only return the last value.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["process :: Monad m => [a] -> (a -> m ()) -> m a\n","process xs f = foldM (\\_ x -> f x >> return x) undefined xs\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":["`runCG` wrapper that calls `cg`, print information and implements the stopping\n","rule based on relative residuals.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["runCG :: Operator a => Double -> a -> Double -> Arr U -> IO (Arr U)\n","runCG tol op reg rhs = do\n"," let initial = computeS $ fromFunction (extent rhs) (const 0)\n"," let steps' = cgreg op reg rhs initial\n"," let r20 = cgr2 $ head steps'\n"," let steps = takeUntil (\\x -> sqrt (cgr2 x / r20) < tol) steps'\n"," result <- process (zip [(1::Int)..] steps) $ \\(n, cgs) ->\n"," putStrLn $ show n ++ \" \" ++ show (sqrt $ cgr2 cgs / r20)\n"," return $ cgx . snd $ result\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":[" ## Blurring operator\n","\n","The implementation of the averaging operator uses repa stencils.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["data StencilOp = StencilOp { getStencil :: Stencil DIM2 Double }\n","\n","instance Operator StencilOp where\n"," evalOp = (delay .) . mapStencil2 (BoundConst 0) . getStencil\n"," adjointOp = evalOp\n","\n","mkKernel :: Int -> StencilOp\n","mkKernel n = StencilOp $ makeStencil2 n n getElem\n"," where getElem (Z:.i:.j)\n"," | max (abs i) (abs j) <= n = Just (1 / fromIntegral (n*n))\n"," | otherwise = Nothing\n"],"cell_type":"code"},{"metadata":{"hidden":false},"source":[" ## Main\n","\n","The main function loads an image, blurs and distorts it, and performs\n","regularized and unregularized reconstructions.\n","\n"],"cell_type":"markdown"},{"execution_count":null,"outputs":[],"metadata":{"collapsed":false},"source":["main :: IO ()\n","main = do\n"," img <- loadImage \"grumpy.bmp\"\n"," saveImage \"exact.bmp\" img\n","\n"," let sh = extent img\n","\n"," -- Repa only supports stencils of size up to 7. Efficient implementations of\n"," -- more general blurring (or other convolution) operators can be done using e.g.\n"," -- FFTs.\n"," let kernel = mkKernel 7\n","\n"," blurry <- computeP $ evalOp kernel img\n"," saveImage \"blurry.bmp\" blurry\n","\n"," putStrLn \"blurry reconstructed\"\n"," runCG 1e-3 kernel 0 blurry >>= saveImage \"blurry_reconstructed.bmp\"\n","\n"," putStrLn \"blurry regularized\"\n"," runCG 1e-3 kernel 0.1 blurry >>= saveImage \"blurry_regularized.bmp\"\n","\n"," mean <- (/ fromIntegral (size sh)) <$> sumAllP blurry\n"," rnd <- normalsIO' (0, 0.15*mean)\n"," let noiseArr = fromList sh $ take (size sh) rnd :: Arr U\n"," (noisy :: Arr U) <- computeP $ blurry +^ noiseArr\n"," saveImage \"noisy.bmp\" noisy\n","\n"," putStrLn \"noisy reconstructed\"\n"," runCG 1e-3 kernel 0 noisy >>= saveImage \"noisy_reconstructed.bmp\"\n","\n"," putStrLn \"noisy regularized\"\n"," runCG 1e-3 kernel 0.1 noisy >>= saveImage \"noisy_regularized.bmp\"\n"],"cell_type":"code"}],"metadata":{"language_info":{"name":"haskell","version":"7.10.2"},"kernelspec":{"display_name":"Haskell","name":"haskell","language":"haskell"}}}