Some Q code
fib: {[n]
$[n<2; n; fib[n-1]+fib[n-2]]
}
%% Read in files and extrapolate mortality rate | |
x = csv.read('life.csv','format', '%f %f'); | |
age = x{1}; | |
prob = x{2}; | |
model = stats.lm(log(prob(61:end)), log(age(61:100))); | |
pfun = @(a) util.if_(a<60, @()prob(a), util.if_(a>121, 1, exp(model.beta(1) + model.beta(2) * log(a)))); |
data IOAction a = Return a | |
| Put String (IOAction a) | |
| Get (String -> IOAction a) | |
get = Get Return | |
put s = Put s (Return ()) | |
seqio :: IOAction a -> (a -> IOAction b) -> IOAction b | |
seqio (Return a) f = f a | |
seqio (Put s io) f = Put s (seqio io f) |
classdef Dual | |
% DUAL Dual numbers class for automatic differentiation. | |
% | |
% To do automatic differentiation we introduce a number d such that d^2 == 0. | |
% Then by a simple application of Taylor's theorem we have | |
% | |
% f(x + d) = f(x) + f'(x) d | |
% | |
% with all higher-order terms vanishing. Therefore applying a function to a dual | |
% number x + d generates both the value f(x) *and* the derivative f'(x). |
module AI.Search.Examples.Graph where | |
import Control.Monad | |
import Control.Monad.ST | |
import Control.Applicative | |
import Data.STRef | |
import Data.Map (Map, (!)) | |
import qualified Data.Map as Map | |
import Data.List (nub) | |
import Data.Graph.Inductive (LNode, LEdge, Gr) |
function search(query,path) | |
% Download full size images from Google image search. Saves image files | |
% locally. Do not print or republish images without permission. | |
% | |
% Based on Python code by Craig Quiter at https://gist.github.com/crizCraig/2816295 | |
% | |
% Requires the JSONLab package, available from | |
% http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab-a-toolbox-to-encodedecode-json-files-in-matlaboctave | |
% | |
% USAGE |
sortBy c=m.map(:[]) -- O(n log n) | |
where m[]=[];m[x]=x;m x=m(n x);n[]=[];n[x]=[x];n(x:y:z)=p x y:n z | |
p[]y=y;p x[]=x;p (w:x)(y:z)=case c w y of GT->y:p(w:x)z;_->w:p x(y:z) | |
sort x=m(map(:[])x) | |
where m[]=[];m[x]=x;m x=m(n x);n[]=[];n[x]=[x];n(x:y:z)=p x y:n z | |
p[]y=y;p x[]=x;p (w:x)(y:z)=if w>y then y:p(w:x)z else w:p x(y:z) |
import Control.Applicative | |
import qualified Data.Set as Set | |
import AI.Search.Uninformed | |
step :: Set.Set String -> String -> [] String | |
step wordlist xs = Set.toList . Set.fromList . filter f $ remove1 xs ++ swap1 xs ++ add1 xs | |
where | |
f w = Set.member w wordlist | |
len = length xs | |
letters = ['a'..'z'] |
import Control.Probability | |
expected p = expectation (runProb p) | |
die = uniform [1..6] :: Bayes Double Integer | |
game n | |
| n == 0 = die | |
| otherwise = do | |
x <- die |
module FieldExtension where | |
{- | |
This module lets you define the exact Fibonacci function using the mathematically | |
elegant definition in terms of phi = (1 + sqrt 5) / 2, without losing precision | |
due to calculations being carried out in floating point. | |
phi :: Q5 | |
phi = 0.5 :+ 0.5 |