AlgebraicJulia / AlgebraicJulia/GraphicalLinearAlgebra.jl

Computing functorially with linear relations

Đang mở
#3 15 bình luận 0 reaction 0 người được giao Xem trên GitHub
question
Ngôn ngữ chính
Julia
Star
8
Fork
1
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

@epatters @mehalter, I came up with this approach for computing with linear relations. It relies on the idea that a LinRel `f:R^n-R^m` can be stored as a basis of a subspace in `R^{n + m}`. If this is right, we can use the `@instance` macro and the `functor` function to start computing some semantics for Circuits, Markov Chains, Chemical Systems, etc.

```julia
module LinRel
using LinearAlgebra

import Base: size

struct Basis{T}
columns::Matrix{T}
end

size(b::Basis, i::Int) = size(b.columns, i)

struct RelBasis{T}
dom::Basis{T}
codom::Basis{T}
end

RelBasis(a::Matrix, b::Matrix) = RelBasis(Basis(a), Basis(b))
RelBasis(a::Basis, b::Matrix) = RelBasis(a, Basis(b))
RelBasis(a::Matrix, b::Basis) = RelBasis(Basis(a), b)

function compose(v::RelBasis, u::RelBasis)
A = v.dom.columns
B = v.codom.columns
C = u.dom.columns
D = u.codom.columns
X = B\C
@show X
@show B*X
@show C - B*X
@show matches = sum(abs.(C - B*X), dims=1) .< 1e-12
vecs = []
for ( i,b ) in enumerate(matches)
if b
push!(vecs, (A*X[:,i], D[:, i]))
end
end
@show vecs
Â₀ = hcat(map(first, vecs)...)
D₀ = hcat(map(last, vecs)...)
return RelBasis(Â₀, Matrix(D₀))
end

function otimes(v::Basis,u::Basis)
a = v.columns
b = u.columns
T = eltype(a)
n,m = size(a)
k,l = size(b)
return vcat(hcat(a, zeros(T, n,l)), hcat(zeros(T, k,m),b))
end

otimes(v::RelBasis, u::RelBasis) = RelBasis(otimes(v.dom,u.dom), otimes(v.codom,u.codom))

#tests

u = [1.0 1;
1 0]
v = [1.0 1;
1 0;
1 -1]
w = [1.0 1 2;
1 3 1;
1 -1 0]
y = [1.0 1 1;
3 5 1]

h = compose(RelBasis(u, v), RelBasis(w,y))

println(h.dom.columns)
println(h.codom.columns)

h = otimes(RelBasis(u, v), RelBasis(w,y))
println(h.dom.columns)
println(h.codom.columns)

end
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.