Skip to content

Instantly share code, notes, and snippets.

@philzook58
Last active January 26, 2020 19:34
Show Gist options
  • Save philzook58/51289c6b016b30d7a6ec75bdb247b945 to your computer and use it in GitHub Desktop.
Save philzook58/51289c6b016b30d7a6ec75bdb247b945 to your computer and use it in GitHub Desktop.
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
function vrep(x::HRep)
VRep(x.n, nullspace(x.A))
end
function hrep(x::VRep)
HRep(x.n, nullspace(x.A')')
end
function meet(x::HRep, y::HRep)
@assert x.n == y.n
HRep(x.n, [x.A ; y.A])
end
function meet(x::VRep, y::VRep)
@assert x.n == y.n
xh = hrep(x)
yh = hrep(y)
HRep(x.n, [xh.A ; yh.A])
end
function meet(x::HRep, y::VRep)
@assert x.n == y.n
yh = hrep(y)
HRep(x.n, [x.A ; yh.A])
end
function meet(x::VRep, y::HRep)
@assert x.n == y.n
xh = hrep(x)
HRep(x.n, [xh.A ; y.A])
end
function join(x::VRep, y::VRep)
@assert x.n == y.n
VRep(x.n, [x.A y.A])
end
function join(x::HRep, y::HRep)
yv = vrep(y)
join(x,yv)
end
function join(x::VRep, y::HRep)
xv = vrep(x)
join(xv,y)
end
function join(x::HRep, y::HRep)
xv = vrep(x)
join(xv,y)
end
function rid(n)
VRep(n, [ Matrix(I,n,n) ; Matrix(I,n,n) ] )
end
function rsub(r :: VRep,p :: HRep)
all(isapprox.(p.A * r.A , 0; atol=eps(Float64), rtol=0))
end
function rsub(r :: VRep, p :: VRep)
p = hrep(p)
all(isapprox.(p.A * r.A , 0; atol=eps(Float64), rtol=0))
end
function heq(p,q)
rsub(p,q) && rsub(q,p)
end
function converse(p :: VRep)
n, g = size(p.A)
VRep( n - p.n , [p.A[p.n + 1 : end, :] ; p.A[1 : p.n, :] ])
end
function converse(p :: HRep)
c, n = size(p.A)
HRep( n - p.n , [p.A[:, p.n + 1 : end] p.A[:, 1 : p.n]])
end
function top(n)
VRep(n, Matrix(I, n, n))
end
function bottom(n)
HRep(n, Matrix(I, n, n))
end
function compose(x::HRep, y::HRep)
cx, nx = size(x.A)
cy, ny = size(y.A)
na = x.n
nb = nx - x.n # which should equal y.n
nc = ny - y.n
B = [ x.A zeros( cx, ny - y.n) ;
zeros( cy, x.n ) y.A ]
C = nullspace(B)
return VRep([ C[1 : n.x, :] ;
C[ nx+y.n + 1:end, :] ])
end
@philzook58
Copy link
Author

function compose(x::VRep, y::VRep)
   x1 = hrep(x)
   y1 = hrep(y)
   compose(x1,y1)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment