Skip to content

Instantly share code, notes, and snippets.

View philzook58's full-sized avatar

Philip Zucker philzook58

View GitHub Profile
@philzook58
philzook58 / linrel.jl
Last active January 26, 2020 19:34
Sketch of linear relations in julia
#A*x = 0
using LinearAlgebra
struct HRep{T}
n :: Int64 #input size
A :: AbstractArray{T}
end
struct VRep{T}
n :: Int64 #input size
A :: AbstractArray{T}
end
@philzook58
philzook58 / interval_lens.hs
Created February 7, 2020 15:43
Interval Computation with Lens?
module Numeric.ADLens.Interval where
import Numeric.Interval
-- interval combinators
type Lens a b = a -> (b, b -> a)
class Lattice a where
(/\) :: a -> a -> a
(\/) :: a -> a -> a
@philzook58
philzook58 / numbery.py
Created February 9, 2020 05:38
floating point examples in z3py
from z3 import *
def numbery_proofs(sort):
x = Const("x", sort)
y = Const("y", sort)
z = Const("z", sort)
print("Commutativity")
prove(x + y == y + x) #Commutativity
print("Associativity")
prove(((x + y) + z) == ((x + (y + z)))) #associativity
@philzook58
philzook58 / adder.py
Last active February 16, 2020 20:29
GraphViz category in python
plus = GraphCat.block("+", ["a","b"], ["c"])
I = GraphCat.idd()
p1 = plus
p2 = p1 @ (p1 * p1)
p3 = p1 @ (p2 * p2)
p4 = p1 @ (p3 * p3)
d = p4.run()
d.format = "png"
d.render("adders")
@philzook58
philzook58 / nbe.hs
Created February 17, 2020 20:00
Haskell implementation of normalization by evaluation
{-# LANGUAGE GADTs, NoImplicitPrelude, StandaloneDeriving, RankNTypes #-}
module NBE where
import Data.Monoid
import Control.Category
import Prelude hiding ((.), id, fst, snd)
import Control.Arrow ((&&&))
data FM a = Pure a | Mappend (FM a) (FM a) | Mempty deriving (Eq, Show)
@philzook58
philzook58 / petricat.py
Created February 29, 2020 20:15
petri net combinators for cvxpy
T = 10 # total number of time steps as a global parameter
class PetriCat():
def compose(f,g):
def res():
a, b , fcon = f()
b1, c, gcon = g()
return a, c, fcon + gcon + [b == b1]
def idd():
def res()
x = cvx.Variable((T-1,1), integer = True)
@philzook58
philzook58 / prelude.py
Created March 8, 2020 19:43
synthesizing sorting network with z3py
from z3 import *
def compare_and_swap_z3(x,y):
x1, y1 = FreshInt(), FreshInt()
c = If(x <= y, And(x1 == x, y1 == y) , And(x1 == y, y1 == x) )
return x1, y1, c
# predicates of interest
def sorted_list(x): # list is sorted
return And([x <= y for x,y in zip(x , x[1:])])
@philzook58
philzook58 / controller.py
Last active March 10, 2020 02:51
convex optimization as a category.
def controller(Cat,pend_step, pos, vel):
acc = Cat.idd(3)
for i in range(10):
acc = acc @ pend_step
init = Cat.const(pos) * Cat.const(vel) * Cat.idd(1)
return acc @ init
@philzook58
philzook58 / matrix.py
Created March 23, 2020 03:05
syzygies
def matrix_to_eqs(m):
nrows, ncols = m.shape
gens = [sy.Dummy() for i in range(ncols)]
eqs = m @ sy.Matrix(gens)
return eqs, gens
def eqs_to_matrix(eqns, gens):
return sy.Matrix( [[ eq.coeff(g) for g in gens] for eq in eqns])
@philzook58
philzook58 / finset.py
Last active May 5, 2020 14:59
finite set category in python
from collections import Counter
class FinSet():
def init(self, dom ,cod , f):
''' In order to specify a morphism, we need to give a python set that is the domain, a python set
that is the codomain, and a dictionary f that encodes a function between these two sets.
We could by assumption just use f.keys() implicitly as the domain, however the codomain is not inferable from just f.
In other categories that domain might not be either, so we chose to require both symmettrically.
'''
assert( dom == set(f.keys())) # f has value for everything in domain
assert( all( [y in cod for y in f.value()] )) # f has only values in codomain