Summary

Objectives

  • Automate the generation of snippets using babel and the pen docker.
1
Logistic map
1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

def logistic(r, x):
  return r * x * (1 - x)

def plot_system(r, x0, n, ax=None):
1
kl divergence of samples
1
2
3
4
5
6
7
import random
import math

def kl_divergence(p, q):
  return sum(p[i] * math.log(p[i]/q[i], 2) for i in range(len(p)))

p = [
1
cross entropy of samples
1
2
3
4
5
import numpy as np
import matplotlib.pyplot as plt

def cross_entropy(p, q):
  return -sum([p[i]*np.log2(q[i]) for i in range(len(p))])
1
R-squared (R2)
1
2
3
4
5
6
import numpy as np
import pandas as pd
from sklearn.metrics import r2_score

def r2(x, y):
  return r2_score(x, y)

Template

1
2
3
4
5
6
code-snippet) {
    echo "#+BEGIN_SRC text -n :f penf -u code-snippet-from-natural-language/2 python :async :results verbatim code"
    cat "$input_fp" | postprocess
    echo -n "#+END_SRC"
}
;;

Prompt used

pf-code-snippet-from-natural-language/2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
title: code snippet from natural language
future-titles:
- Get code snippet
- Get snippet
next-version: get-code-snippet.prompt
doc: Get a bash one liner from natural langauge
prompt: |+
    Language: Ruby
    Task: Reverse lines from stdout
    Code: while s = gets; puts s.chop.reverse; end
    <delim>
    Language: Haskell
    Task: Multiple Each Item in a List by 2
    Code:
    map (*2) [1..10]
    <delim>
    Language: bash
    Task: Recursively remove all "node_modules" folders
    Code:
    find . -name "node_modules" -exec rm -rf '{}' +
    <delim>
    Language: Haskell
    Task: Apply a function to both bits of a tuple:
    Code:
    both :: (a -> b) -> (a, a) -> (b, b)
    both = join (***)
    <delim>
    Language: Python
    Task: Palindrome Python One-Liner
    Code: phrase.find(phrase[::-1])
    <delim>
    Language: <1>
    Task: <2>
    Code:    
engine: OpenAI Davinci
temperature: 0.8
max-generated-tokens: 60
top-p: 1
stop-sequences:
- "<delim>"
vars:
- language
- task
examples:
- bash
- suspend laptop

Demo

Some more demos

1
N choose K
1
2
def nCk(n,k):
  return int( reduce(mul, (Fraction(n-i, i+1) for i in range(k)), 1) )
1
inverse laplace transform
1
2
3
4
5
import sympy as sp
sp.init_printing()
t = sp.symbols('t')
f = sp.exp(-t)
sp.inverse_laplace_transform(f, t, s)

Ruby?

1
inverse laplace transform
1
2
3
4
require 'mathn'

def inverse_laplace_transform(expr, s, t)
expr.subs(s, 0).subs(t, 0).simplify.expand.collect { |x| x.subs(t,