{ "metadata": { "language": "haskell", "name": "", "signature": "sha256:77784b243dcc073ca234e267b10dd773decbb010b9dbb529bf20bf1714830221" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multiple plots with Rlang-QQ\n", "An example of `Rlang-QQ-0.1.2.0`, which supports multiple figures.\n", "\n", "First a pile of imports needed:" ] }, { "cell_type": "code", "collapsed": false, "input": [ ":set -XQuasiQuotes -XTupleSections -XViewPatterns\n", "import IHaskell.Display -- shouldn't be needed but it turns out that it is essential\n", "import IHaskell.Display.Rlangqq () -- to check it's installed\n", "import Data.Monoid\n", "import Control.Applicative\n", "import Data.List" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Rlang-QQ saves intermediate files. There is some code\n", "hidden in `IHaskell.Display.RlangQQ` and `RlangQQ.ParseKnitted`\n", "which gets those messages/warnings/plots." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just some more of the same integration stuff that may be easier to think about because it's written in haskell" ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- | trapezoid rule. doesn't reuse previous values\n", "trap :: Fractional a\n", " => (a -> a) -- ^ function\n", " -> a -- ^ lower bound\n", " -> a -- ^ upepr bound\n", " -> Int -- ^ number of panels to divide the interval into\n", " -> a -- ^ integral approximation\n", "trap f a b n =\n", " let h = (b - a) / fromIntegral n\n", " xs = take (n-1) $ iterate (h+) (a+h)\n", " -- easier to see as [a+h, a+2*h .. b-h])\n", " -- but some interval arithmetic doesn't support [a..b]\n", " in h * foldl' (\\s x -> s + f x) ((f a + f b) / 2) xs" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "improve :: Fractional a\n", " => Int -- n\n", " -> a -- O(h^n) estimate with step size h/2\n", " -> a -- O(h^n) estimate with step size h\n", " -> a -- O(h^(n+1)) estimate\n", "improve n x y = let c = 4^(n-1) in (c*x - y) / (c - 1)\n", "\n", "rombM :: Fractional a\n", " => (a -> a)\n", " -> a\n", " -> a\n", " -> [[a]]\n", "rombM f a b =\n", " let is = map (\\n -> trap f a b (2^n)) [0..]\n", " in map snd\n", " $ iterate\n", " (\\(n,s) -> (n+1, zipWith (improve n) (tail s) s))\n", " (2,is)\n", "\n", "romb :: Fractional a\n", " => (a -> a)\n", " -> a\n", " -> a\n", " -> [a] -- successive approximations\n", "romb f a b = map head $ rombM f a b" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "xs = take 5 $ romb exp 0 1 :: [Double]\n", "\n", "print xs" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "[1.8591409142295225,1.7188611518765928,1.7182826879247577,1.7182818287945305,1.7182818284590786]" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "sse <- do\n", " [pun| (sse) |] <- [rDisp|\n", " m <- lm( log($(xs) / (exp(1)-1)) ~ I(1:length($(xs))))\n", " plot(m, which=1:2)\n", " print(predict(m))\n", " hs_sse <- sum( resid(m)^2 )\n", " |]\n", " return (sse :: Double)\n", "() -- needed to force a display" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "()" ] }, { "metadata": {}, "output_type": "display_data", "text": [] }, { "html": [ "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] }, { "html": [ "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] }, { "metadata": {}, "output_type": "display_data", "text": [] }, { "metadata": {}, "output_type": "display_data", "text": [] }, { "metadata": {}, "output_type": "display_data", "text": [ " 1 2 3 4 5 \n", " 4.741e-02 3.162e-02 1.583e-02 3.381e-05 -1.576e-02" ] }, { "metadata": {}, "output_type": "display_data", "text": [] }, { "metadata": {}, "output_type": "display_data", "text": [] }, { "metadata": {}, "output_type": "display_data", "text": [] }, { "metadata": {}, "output_type": "display_data" } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "sse" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "2.4619397033555017e-3" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }