From 7f4b9e242014ddb9366b391ff494c0543d97d911 Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Tue, 24 Mar 2020 16:17:05 -0400 Subject: [PATCH 01/12] Added first experiment, and began conversion process to Dagger instance --- experiments/DaggerInstanceGLA/Manifest.toml | 324 ++++++++++++++++++ experiments/DaggerInstanceGLA/Project.toml | 11 + .../src/DaggerInstanceGLA.jl | 62 ++++ 3 files changed, 397 insertions(+) create mode 100644 experiments/DaggerInstanceGLA/Manifest.toml create mode 100644 experiments/DaggerInstanceGLA/Project.toml create mode 100644 experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl diff --git a/experiments/DaggerInstanceGLA/Manifest.toml b/experiments/DaggerInstanceGLA/Manifest.toml new file mode 100644 index 000000000..8708814c3 --- /dev/null +++ b/experiments/DaggerInstanceGLA/Manifest.toml @@ -0,0 +1,324 @@ +# This file is machine-generated - editing it directly is not advised + +[[ArnoldiMethod]] +deps = ["DelimitedFiles", "LinearAlgebra", "Random", "SparseArrays", "StaticArrays", "Test"] +git-tree-sha1 = "2b6845cea546604fb4dca4e31414a6a59d39ddcd" +uuid = "ec485272-7323-5ecc-a04f-4719b315124d" +version = "0.0.4" + +[[AutoHashEquals]] +git-tree-sha1 = "45bb6705d93be619b81451bb2006b7ee5d4e4453" +uuid = "15f4f7f2-30c1-5605-9d31-71845cf9641f" +version = "0.2.0" + +[[Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[BinaryProvider]] +deps = ["Libdl", "SHA"] +git-tree-sha1 = "5b08ed6036d9d3f0ee6369410b830f8873d4024c" +uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232" +version = "0.5.8" + +[[Catlab]] +deps = ["AutoHashEquals", "Compat", "Compose", "DataStructures", "IterativeSolvers", "JSON", "LightGraphs", "LightXML", "LinearAlgebra", "LinearMaps", "Logging", "Match", "MetaGraphs", "Parameters", "Pkg", "Reexport", "Requires", "StaticArrays", "Statistics", "Test"] +git-tree-sha1 = "861e8ff10cb2507d444ab3d9925698bcec958c62" +uuid = "134e5e36-593f-5add-ad60-77f754baafbe" +version = "0.6.0" + +[[CodecZlib]] +deps = ["TranscodingStreams", "Zlib_jll"] +git-tree-sha1 = "ded953804d019afa9a3f98981d99b33e3db7b6da" +uuid = "944b1d66-785c-5afd-91f1-9de20f533193" +version = "0.7.0" + +[[ColorTypes]] +deps = ["FixedPointNumbers", "Random"] +git-tree-sha1 = "c4c1cca28748906265ed62c788d6fe6f0134d264" +uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" +version = "0.10.0" + +[[Colors]] +deps = ["ColorTypes", "FixedPointNumbers", "InteractiveUtils", "Reexport"] +git-tree-sha1 = "2fdeb981ebcf52cd800ddb6a0aa5eac34153552d" +uuid = "5ae59095-9a9b-59fe-a467-6f913c188581" +version = "0.12.0" + +[[Compat]] +deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] +git-tree-sha1 = "0198d18b28c093bef39872a22f1a897218a925f5" +uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" +version = "3.8.0" + +[[Compose]] +deps = ["Base64", "Colors", "DataStructures", "Dates", "IterTools", "JSON", "LinearAlgebra", "Measures", "Printf", "Random", "Requires", "UUIDs"] +git-tree-sha1 = "034174e607d254b8ca0853a1a9029b265114bf6c" +uuid = "a81c6b42-2e10-5240-aca2-a61377ecd94b" +version = "0.8.2" + +[[Dagger]] +deps = ["Distributed", "LinearAlgebra", "MemPool", "Profile", "Random", "Serialization", "SharedArrays", "SparseArrays", "Statistics", "StatsBase", "Test"] +git-tree-sha1 = "39dd9351c6637a71d1925e26e49adcd8a25ebf0b" +uuid = "d58978e5-989f-55fb-8d15-ea34adc7bf54" +version = "0.8.0" + +[[DataAPI]] +git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252" +uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" +version = "1.1.0" + +[[DataStructures]] +deps = ["InteractiveUtils", "OrderedCollections"] +git-tree-sha1 = "5a431d46abf2ef2a4d5d00bd0ae61f651cf854c8" +uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +version = "0.17.10" + +[[Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[DelimitedFiles]] +deps = ["Mmap"] +uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" + +[[Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + +[[FileIO]] +deps = ["Pkg"] +git-tree-sha1 = "3d7cb2c4c850439f19c4d6d3fbe1dce6481cddb1" +uuid = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" +version = "1.2.4" + +[[FixedPointNumbers]] +git-tree-sha1 = "3ba9ea634d4c8b289d590403b4a06f8e227a6238" +uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" +version = "0.8.0" + +[[Inflate]] +git-tree-sha1 = "f5fc07d4e706b84f72d54eedcc1c13d92fb0871c" +uuid = "d25df0c9-e2be-5dd7-82c8-3ad0b3e990b9" +version = "0.1.2" + +[[InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[IterTools]] +git-tree-sha1 = "05110a2ab1fc5f932622ffea2a003221f4782c18" +uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e" +version = "1.3.0" + +[[IterativeSolvers]] +deps = ["LinearAlgebra", "Printf", "Random", "RecipesBase", "SparseArrays"] +git-tree-sha1 = "f272a02b57a1a1d2150a1a6cc4a3441ee806ab96" +uuid = "42fd0dbc-a981-5370-80f2-aaf504508153" +version = "0.8.3" + +[[JLD2]] +deps = ["CodecZlib", "DataStructures", "FileIO", "Mmap", "Printf"] +git-tree-sha1 = "343d1ec8ae7d57a608d79ef12e9a853cbb9acd96" +uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819" +version = "0.1.3" + +[[JSON]] +deps = ["Dates", "Mmap", "Parsers", "Unicode"] +git-tree-sha1 = "b34d7cef7b337321e97d22242c3c2b91f476748e" +uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +version = "0.21.0" + +[[LibGit2]] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[LightGraphs]] +deps = ["ArnoldiMethod", "DataStructures", "Distributed", "Inflate", "LinearAlgebra", "Random", "SharedArrays", "SimpleTraits", "SparseArrays", "Statistics"] +git-tree-sha1 = "f40c4dbcd957cc3afc8cca0ff26c9f8304def00d" +uuid = "093fc24a-ae57-5d10-9952-331d41423f4d" +version = "1.3.1" + +[[LightXML]] +deps = ["BinaryProvider", "Libdl"] +git-tree-sha1 = "be855e3c975b89746b09952407c156b5e4a33a1d" +uuid = "9c8b4983-aa76-5018-a973-4c85ecc9e179" +version = "0.8.1" + +[[LinearAlgebra]] +deps = ["Libdl"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[LinearMaps]] +deps = ["LinearAlgebra", "SparseArrays"] +git-tree-sha1 = "e204a96dbb8d49fbca24086c586734435d7bf5b5" +uuid = "7a12625a-238d-50fd-b39a-03d52299707e" +version = "2.6.1" + +[[Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[MacroTools]] +deps = ["Markdown", "Random"] +git-tree-sha1 = "f7d2e3f654af75f01ec49be82c231c382214223a" +uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" +version = "0.5.5" + +[[Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[Match]] +git-tree-sha1 = "5cf525d97caf86d29307150fcba763a64eaa9cbe" +uuid = "7eb4fadd-790c-5f42-8a69-bfa0b872bfbf" +version = "1.1.0" + +[[Measures]] +git-tree-sha1 = "e498ddeee6f9fdb4551ce855a46f54dbd900245f" +uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e" +version = "0.3.1" + +[[MemPool]] +deps = ["DataStructures", "Distributed", "Mmap", "Random", "Serialization", "Sockets", "Test"] +git-tree-sha1 = "d52799152697059353a8eac1000d32ba8d92aa25" +uuid = "f9f48841-c794-520a-933b-121f7ba6ed94" +version = "0.2.0" + +[[MetaGraphs]] +deps = ["JLD2", "LightGraphs", "Random"] +git-tree-sha1 = "43ebbe06b22d213e4a8750424f9c7d1311bee2a6" +uuid = "626554b9-1ddb-594c-aa3c-2596fe9399a5" +version = "0.6.4" + +[[Missings]] +deps = ["DataAPI"] +git-tree-sha1 = "de0a5ce9e5289f27df672ffabef4d1e5861247d5" +uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28" +version = "0.4.3" + +[[Mmap]] +uuid = "a63ad114-7e13-5084-954f-fe012c677804" + +[[OrderedCollections]] +deps = ["Random", "Serialization", "Test"] +git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1" +uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +version = "1.1.0" + +[[Parameters]] +deps = ["OrderedCollections"] +git-tree-sha1 = "b62b2558efb1eef1fa44e4be5ff58a515c287e38" +uuid = "d96e819e-fc66-5662-9728-84c9c7592b0a" +version = "0.12.0" + +[[Parsers]] +deps = ["Dates", "Test"] +git-tree-sha1 = "0c16b3179190d3046c073440d94172cfc3bb0553" +uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" +version = "0.3.12" + +[[Pkg]] +deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Test", "UUIDs"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" + +[[Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[Profile]] +deps = ["Printf"] +uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" + +[[REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[Random]] +deps = ["Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[RecipesBase]] +git-tree-sha1 = "b4ed4a7f988ea2340017916f7c9e5d7560b52cae" +uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" +version = "0.8.0" + +[[Reexport]] +deps = ["Pkg"] +git-tree-sha1 = "7b1d07f411bc8ddb7977ec7f377b97b158514fe0" +uuid = "189a3867-3050-52da-a836-e630ba90ab69" +version = "0.2.0" + +[[Requires]] +deps = ["UUIDs"] +git-tree-sha1 = "d37400976e98018ee840e0ca4f9d20baa231dc6b" +uuid = "ae029012-a4dd-5104-9daa-d747884805df" +version = "1.0.1" + +[[SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" + +[[Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[SharedArrays]] +deps = ["Distributed", "Mmap", "Random", "Serialization"] +uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" + +[[SimpleTraits]] +deps = ["InteractiveUtils", "MacroTools"] +git-tree-sha1 = "2bdf3b6300a9d66fe29ee8bb51ba100c4df9ecbc" +uuid = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" +version = "0.9.1" + +[[Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[SortingAlgorithms]] +deps = ["DataStructures", "Random", "Test"] +git-tree-sha1 = "03f5898c9959f8115e30bc7226ada7d0df554ddd" +uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c" +version = "0.3.1" + +[[SparseArrays]] +deps = ["LinearAlgebra", "Random"] +uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[[StaticArrays]] +deps = ["LinearAlgebra", "Random", "Statistics"] +git-tree-sha1 = "5a3bcb6233adabde68ebc97be66e95dcb787424c" +uuid = "90137ffa-7385-5640-81b9-e52037218182" +version = "0.12.1" + +[[Statistics]] +deps = ["LinearAlgebra", "SparseArrays"] +uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + +[[StatsBase]] +deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics"] +git-tree-sha1 = "19bfcb46245f69ff4013b3df3b977a289852c3a1" +uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" +version = "0.32.2" + +[[Test]] +deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[TranscodingStreams]] +deps = ["Random", "Test"] +git-tree-sha1 = "7c53c35547de1c5b9d46a4797cf6d8253807108c" +uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" +version = "0.9.5" + +[[UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[Zlib_jll]] +deps = ["Libdl", "Pkg"] +git-tree-sha1 = "fd36a6739e256527287c5444960d0266712cd49e" +uuid = "83775a58-1f1d-513f-b197-d71354ab007a" +version = "1.2.11+8" diff --git a/experiments/DaggerInstanceGLA/Project.toml b/experiments/DaggerInstanceGLA/Project.toml new file mode 100644 index 000000000..46ca2ddad --- /dev/null +++ b/experiments/DaggerInstanceGLA/Project.toml @@ -0,0 +1,11 @@ +name = "DaggerInstanceGLA" +uuid = "eaa498f4-19f8-4d9b-a7d5-989fa76892d7" +authors = ["bosonbaas "] +version = "0.1.0" + +[deps] +AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f" +Catlab = "134e5e36-593f-5add-ad60-77f754baafbe" +Dagger = "d58978e5-989f-55fb-8d15-ea34adc7bf54" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +LinearMaps = "7a12625a-238d-50fd-b39a-03d52299707e" diff --git a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl new file mode 100644 index 000000000..b027c9c76 --- /dev/null +++ b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl @@ -0,0 +1,62 @@ +module DaggerInstanceGLA + +import Base: + +using AutoHashEquals + +export LinearMapDom, LinearMap + +using Dagger +using LinearAlgebra +using Catlab.LinearAlgebra.GraphicalLinearAlgebra +using Catlab +using LinearMaps +import LinearMaps: adjoint +const LMs = LinearMaps + + +@auto_hash_equals struct LinearMapDom + N::Int +end + +struct MatrixThunk + thunk::Thunk + size::Int +end + +@instance LinearFunctions(LinearMapDom, MatrixThunk) begin + @import adjoint + + +(f::MatrixThunk, g::MatrixThunk) = delayed(+)(f, g) + + dom(f::LinearMap) = LinearMapDom(size(f,2)) + codom(f::LinearMap) = LinearMapDom(size(f,1)) + + compose(f::LinearMap, g::LinearMap) = g*f + id(V::LinearMapDom) = LMs.UniformScalingMap(1, V.N) + + oplus(V::LinearMapDom, W::LinearMapDom) = LinearMapDom(V.N + W.N) + oplus(f::LinearMap, g::LinearMap) = LMs.BlockDiagonalMap(f, g) + mzero(::Type{LinearMapDom}) = LinearMapDom(0) + braid(V::LinearMapDom, W::LinearMapDom) = + LinearMap(braid_lm(V.N), braid_lm(W.N), W.N+V.N, V.N+W.N) + + mcopy(V::LinearMapDom) = LinearMap(mcopy_lm, plus_lm, 2*V.N, V.N) + delete(V::LinearMapDom) = LinearMap(delete_lm, zero_lm(V.N), 0, V.N) + plus(V::LinearMapDom) = LinearMap(plus_lm, mcopy_lm, V.N, 2*V.N) + zero(V::LinearMapDom) = LinearMap(zero_lm(V.N), delete_lm, V.N, 0) + + plus(f::LinearMap, g::LinearMap) = f+g + scalar(V::LinearMapDom, c::Number) = LMs.UniformScalingMap(c, V.N) + antipode(V::LinearMapDom) = LMs.UniformScalingMap(-1, V.N) +end + +braid_lm(n::Int) = x::AbstractVector -> vcat(x[n+1:end], x[1:n]) +mcopy_lm(x::AbstractVector) = vcat(x, x) +delete_lm(x::AbstractVector) = eltype(x)[] +plus_lm(x::AbstractVector) = begin + n = length(x) ÷ 2 + x[1:n] + x[n+1:end] +end +zero_lm(n::Int) = x::AbstractVector -> zeros(eltype(x), n) + +end From 23d31bad12412dd268a18b895592d5d0bbe7ea0b Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Wed, 25 Mar 2020 14:58:52 -0400 Subject: [PATCH 02/12] Fully Implemented DAG (non-functional) and added tests --- .../src/DaggerInstanceGLA.jl | 51 +++++++++++-------- experiments/DaggerInstanceGLA/src/test.jl | 11 ++++ 2 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 experiments/DaggerInstanceGLA/src/test.jl diff --git a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl index b027c9c76..6882a22e3 100644 --- a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl +++ b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl @@ -3,7 +3,9 @@ module DaggerInstanceGLA import Base: + using AutoHashEquals -export LinearMapDom, LinearMap +export DagDom, LinearMap, MatrixThunk, matrixToThunk, + dom, codom, adjoint, +, compose, id, oplus, mzero, + braid, mcopy, delete, plus, zero, scalar, antipode using Dagger using LinearAlgebra @@ -14,40 +16,45 @@ import LinearMaps: adjoint const LMs = LinearMaps -@auto_hash_equals struct LinearMapDom +@auto_hash_equals struct DagDom N::Int end struct MatrixThunk thunk::Thunk - size::Int + dom::Int + codom::Int end -@instance LinearFunctions(LinearMapDom, MatrixThunk) begin - @import adjoint +matrixToThunk(A::LinearMap) = begin + delayed(x->x)(A) +end + +@instance LinearFunctions(DagDom, MatrixThunk) begin - +(f::MatrixThunk, g::MatrixThunk) = delayed(+)(f, g) + adjoint(f::MatrixThunk) = MatrixThunk(delayed(adjoint)(f.thunk), f.codom, f.dom) + +(f::MatrixThunk, g::MatrixThunk) = MatrixThunk(delayed(+)(f.thunk, g.thunk), f.dom, f.codom) - dom(f::LinearMap) = LinearMapDom(size(f,2)) - codom(f::LinearMap) = LinearMapDom(size(f,1)) + dom(f::MatrixThunk) = f.dom + codom(f::MatrixThunk) = f.codom - compose(f::LinearMap, g::LinearMap) = g*f - id(V::LinearMapDom) = LMs.UniformScalingMap(1, V.N) + compose(f::MatrixThunk, g::MatrixThunk) = MatrixThunk(delayed(*)(g.thunk,f.thunk), g.dom, f.codom) + id(V::DagDom) = MatrixThunk(delayed(x->x)(LMs.UniformScalingMap(1, V.N)), V.N, V.N) - oplus(V::LinearMapDom, W::LinearMapDom) = LinearMapDom(V.N + W.N) - oplus(f::LinearMap, g::LinearMap) = LMs.BlockDiagonalMap(f, g) - mzero(::Type{LinearMapDom}) = LinearMapDom(0) - braid(V::LinearMapDom, W::LinearMapDom) = - LinearMap(braid_lm(V.N), braid_lm(W.N), W.N+V.N, V.N+W.N) + oplus(V::DagDom, W::DagDom) = DagDom(V.N + W.N) + oplus(f::MatrixThunk, g::MatrixThunk) = MatrixThunk(delayed(LMs.BlockDiagonalMap)(f.thunk, g.thunk), f.dom+g.dom, f.codom+g.codom) + mzero(::Type{DagDom}) = DagDom(0) + braid(V::DagDom, W::DagDom) = + MatrixThunk(delayed(x->x)(LinearMap(braid_lm(V.N), braid_lm(W.N), W.N+V.N, V.N+W.N)),V.N+W.N, W.N+V.N) - mcopy(V::LinearMapDom) = LinearMap(mcopy_lm, plus_lm, 2*V.N, V.N) - delete(V::LinearMapDom) = LinearMap(delete_lm, zero_lm(V.N), 0, V.N) - plus(V::LinearMapDom) = LinearMap(plus_lm, mcopy_lm, V.N, 2*V.N) - zero(V::LinearMapDom) = LinearMap(zero_lm(V.N), delete_lm, V.N, 0) + mcopy(V::DagDom) = MatrixThunk(delayed(x->x)(LinearMap(mcopy_lm, plus_lm, 2*V.N, V.N)), V.N, 2*V.N) + delete(V::DagDom) = MatrixThunk(delayed(x->x)(LinearMap(delete_lm, zero_lm(V.N), 0, V.N)), V.N, 0) + plus(V::DagDom) = MatrixThunk(delayed(x->x)(LinearMap(plus_lm, mcopy_lm, V.N, 2*V.N)), 2*V.N, V.N) + zero(V::DagDom) = MatrixThunk(delayed(x->x)(LinearMap(zero_lm(V.N), delete_lm, V.N, 0)), 0, V.N) - plus(f::LinearMap, g::LinearMap) = f+g - scalar(V::LinearMapDom, c::Number) = LMs.UniformScalingMap(c, V.N) - antipode(V::LinearMapDom) = LMs.UniformScalingMap(-1, V.N) + plus(f::MatrixThunk, g::MatrixThunk) = f+g + scalar(V::DagDom, c::Number) = MatrixThunk(delayed(x->x)(LMs.UniformScalingMap(c, V.N)), V.N, V.N) + antipode(V::DagDom) = scalar(V, -1) end braid_lm(n::Int) = x::AbstractVector -> vcat(x[n+1:end], x[1:n]) diff --git a/experiments/DaggerInstanceGLA/src/test.jl b/experiments/DaggerInstanceGLA/src/test.jl new file mode 100644 index 000000000..1ee0c5b26 --- /dev/null +++ b/experiments/DaggerInstanceGLA/src/test.jl @@ -0,0 +1,11 @@ +using Catlab.LinearAlgebra.GraphicalLinearAlgebra, DaggerInstanceGLA, Catlab, Catlab.Doctrines +V, W = Ob(FreeLinearFunctions, :V, :W) +f, g, h = Hom(:f, V, W), Hom(:g, V, W), Hom(:h, W, W) +fop = MatrixThunk(matrixToThunk(LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0])), 4,3) +gop = MatrixThunk(matrixToThunk(LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0])), 4,3) +hop = MatrixThunk(matrixToThunk(LinearMap([1 4 -1; -1 1 0; 1 1 -1.0])), 3,3) +d = Dict(f=>fop, g=>gop, h => hop, V => DagDom(4), W=>DagDom(3)) +F(ex) = functor((DagDom, MatrixThunk), ex, generators=d) +M = plus(f,g)⋅h + +F(M) From 45aa94ab1c90db6f994e56aa3fea17d0a6e736f3 Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Thu, 26 Mar 2020 17:15:36 -0400 Subject: [PATCH 03/12] Added imports from Doctrines to keep from conflicts --- experiments/DaggerInstanceGLA/Manifest.toml | 25 ++++++++++++++++--- .../src/DaggerInstanceGLA.jl | 10 +++++--- experiments/DaggerInstanceGLA/src/test.jl | 14 ++++++++++- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/experiments/DaggerInstanceGLA/Manifest.toml b/experiments/DaggerInstanceGLA/Manifest.toml index 8708814c3..89037ddad 100644 --- a/experiments/DaggerInstanceGLA/Manifest.toml +++ b/experiments/DaggerInstanceGLA/Manifest.toml @@ -21,8 +21,8 @@ uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232" version = "0.5.8" [[Catlab]] -deps = ["AutoHashEquals", "Compat", "Compose", "DataStructures", "IterativeSolvers", "JSON", "LightGraphs", "LightXML", "LinearAlgebra", "LinearMaps", "Logging", "Match", "MetaGraphs", "Parameters", "Pkg", "Reexport", "Requires", "StaticArrays", "Statistics", "Test"] -git-tree-sha1 = "861e8ff10cb2507d444ab3d9925698bcec958c62" +deps = ["AutoHashEquals", "Compat", "Compose", "DataStructures", "IterativeSolvers", "JSON", "LightGraphs", "LightXML", "LinearAlgebra", "LinearMaps", "LinearOperators", "Logging", "Match", "MetaGraphs", "Parameters", "Pkg", "Random", "Reexport", "Requires", "StaticArrays", "Statistics", "Test"] +path = "/Users/abaas3/.julia/dev/Catlab" uuid = "134e5e36-593f-5add-ad60-77f754baafbe" version = "0.6.0" @@ -85,6 +85,11 @@ uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" deps = ["Random", "Serialization", "Sockets"] uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" +[[FastClosures]] +git-tree-sha1 = "acebe244d53ee1b461970f8910c235b259e772ef" +uuid = "9aa1b823-49e4-5ca5-8b0f-3971ec8bab6a" +version = "0.3.2" + [[FileIO]] deps = ["Pkg"] git-tree-sha1 = "3d7cb2c4c850439f19c4d6d3fbe1dce6481cddb1" @@ -156,6 +161,12 @@ git-tree-sha1 = "e204a96dbb8d49fbca24086c586734435d7bf5b5" uuid = "7a12625a-238d-50fd-b39a-03d52299707e" version = "2.6.1" +[[LinearOperators]] +deps = ["FastClosures", "LinearAlgebra", "Printf", "SparseArrays", "TimerOutputs"] +git-tree-sha1 = "3d4043d5cc711f2fcdca545bfd4aafe5bc0b1bee" +uuid = "5c8ed15e-5a4c-59e4-a42b-c7e8811fb125" +version = "1.0.1" + [[Logging]] uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" @@ -296,14 +307,20 @@ uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" [[StatsBase]] deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics"] -git-tree-sha1 = "19bfcb46245f69ff4013b3df3b977a289852c3a1" +git-tree-sha1 = "a6102b1f364befdb05746f386b67c6b7e3262c45" uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" -version = "0.32.2" +version = "0.33.0" [[Test]] deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +[[TimerOutputs]] +deps = ["Printf"] +git-tree-sha1 = "311765af81bbb48d7bad01fb016d9c328c6ede03" +uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" +version = "0.5.3" + [[TranscodingStreams]] deps = ["Random", "Test"] git-tree-sha1 = "7c53c35547de1c5b9d46a4797cf6d8253807108c" diff --git a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl index 6882a22e3..c04a5d717 100644 --- a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl +++ b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl @@ -5,12 +5,16 @@ using AutoHashEquals export DagDom, LinearMap, MatrixThunk, matrixToThunk, dom, codom, adjoint, +, compose, id, oplus, mzero, - braid, mcopy, delete, plus, zero, scalar, antipode - + braid, mcopy, delete, plus, zero, scalar, antipode, + ⊗ using Dagger using LinearAlgebra using Catlab.LinearAlgebra.GraphicalLinearAlgebra -using Catlab +using Catlab, Catlab.Doctrines +import ...Doctrines: + Ob, Hom, dom, codom, compose, ⋅, ∘, id, oplus, ⊕, mzero, braid, + dagger, dunit, dcounit, mcopy, Δ, delete, ◊, mmerge, ∇, create, □, + plus, zero, coplus, cozero, meet, top, join, bottom using LinearMaps import LinearMaps: adjoint const LMs = LinearMaps diff --git a/experiments/DaggerInstanceGLA/src/test.jl b/experiments/DaggerInstanceGLA/src/test.jl index 1ee0c5b26..de8a6113b 100644 --- a/experiments/DaggerInstanceGLA/src/test.jl +++ b/experiments/DaggerInstanceGLA/src/test.jl @@ -1,11 +1,23 @@ using Catlab.LinearAlgebra.GraphicalLinearAlgebra, DaggerInstanceGLA, Catlab, Catlab.Doctrines + V, W = Ob(FreeLinearFunctions, :V, :W) f, g, h = Hom(:f, V, W), Hom(:g, V, W), Hom(:h, W, W) + fop = MatrixThunk(matrixToThunk(LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0])), 4,3) gop = MatrixThunk(matrixToThunk(LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0])), 4,3) hop = MatrixThunk(matrixToThunk(LinearMap([1 4 -1; -1 1 0; 1 1 -1.0])), 3,3) + +#fop = LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0]) +#gop = LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0]) +#hop = LinearMap([1 4 -1; -1 1 0; 1 1 -1.0]) + + d = Dict(f=>fop, g=>gop, h => hop, V => DagDom(4), W=>DagDom(3)) F(ex) = functor((DagDom, MatrixThunk), ex, generators=d) +#d = Dict(f=>fop, g=>gop, h => hop, V => LinearMapDom(4), W=>LinearMapDom(3)) +#F(ex) = functor((LinearMapDom, LinearMap), ex, generators=d) + M = plus(f,g)⋅h -F(M) +print(Matrix(collect(F(M).thunk))) +print('\n') From 19f97ddfc055ed081c4378ce6862bed8ea47cb28 Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Thu, 26 Mar 2020 17:33:44 -0400 Subject: [PATCH 04/12] Added some comments --- experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl index c04a5d717..b6fe58c7e 100644 --- a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl +++ b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl @@ -24,6 +24,11 @@ const LMs = LinearMaps N::Int end +# This structure was created to keep track of dom and codom information. +# This information can be updated efficiently, and keeping it here keeps +# LinearFunctions from having to think the thunk each time the dom or codom +# is queried + struct MatrixThunk thunk::Thunk dom::Int From c55e6d5a0b8bd1738827b20ec5a03a68129a28be Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Fri, 27 Mar 2020 15:28:30 -0400 Subject: [PATCH 05/12] Cleaned up unnecessary manifest file --- experiments/DaggerInstanceGLA/Manifest.toml | 341 -------------------- 1 file changed, 341 deletions(-) delete mode 100644 experiments/DaggerInstanceGLA/Manifest.toml diff --git a/experiments/DaggerInstanceGLA/Manifest.toml b/experiments/DaggerInstanceGLA/Manifest.toml deleted file mode 100644 index 89037ddad..000000000 --- a/experiments/DaggerInstanceGLA/Manifest.toml +++ /dev/null @@ -1,341 +0,0 @@ -# This file is machine-generated - editing it directly is not advised - -[[ArnoldiMethod]] -deps = ["DelimitedFiles", "LinearAlgebra", "Random", "SparseArrays", "StaticArrays", "Test"] -git-tree-sha1 = "2b6845cea546604fb4dca4e31414a6a59d39ddcd" -uuid = "ec485272-7323-5ecc-a04f-4719b315124d" -version = "0.0.4" - -[[AutoHashEquals]] -git-tree-sha1 = "45bb6705d93be619b81451bb2006b7ee5d4e4453" -uuid = "15f4f7f2-30c1-5605-9d31-71845cf9641f" -version = "0.2.0" - -[[Base64]] -uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" - -[[BinaryProvider]] -deps = ["Libdl", "SHA"] -git-tree-sha1 = "5b08ed6036d9d3f0ee6369410b830f8873d4024c" -uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232" -version = "0.5.8" - -[[Catlab]] -deps = ["AutoHashEquals", "Compat", "Compose", "DataStructures", "IterativeSolvers", "JSON", "LightGraphs", "LightXML", "LinearAlgebra", "LinearMaps", "LinearOperators", "Logging", "Match", "MetaGraphs", "Parameters", "Pkg", "Random", "Reexport", "Requires", "StaticArrays", "Statistics", "Test"] -path = "/Users/abaas3/.julia/dev/Catlab" -uuid = "134e5e36-593f-5add-ad60-77f754baafbe" -version = "0.6.0" - -[[CodecZlib]] -deps = ["TranscodingStreams", "Zlib_jll"] -git-tree-sha1 = "ded953804d019afa9a3f98981d99b33e3db7b6da" -uuid = "944b1d66-785c-5afd-91f1-9de20f533193" -version = "0.7.0" - -[[ColorTypes]] -deps = ["FixedPointNumbers", "Random"] -git-tree-sha1 = "c4c1cca28748906265ed62c788d6fe6f0134d264" -uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" -version = "0.10.0" - -[[Colors]] -deps = ["ColorTypes", "FixedPointNumbers", "InteractiveUtils", "Reexport"] -git-tree-sha1 = "2fdeb981ebcf52cd800ddb6a0aa5eac34153552d" -uuid = "5ae59095-9a9b-59fe-a467-6f913c188581" -version = "0.12.0" - -[[Compat]] -deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] -git-tree-sha1 = "0198d18b28c093bef39872a22f1a897218a925f5" -uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "3.8.0" - -[[Compose]] -deps = ["Base64", "Colors", "DataStructures", "Dates", "IterTools", "JSON", "LinearAlgebra", "Measures", "Printf", "Random", "Requires", "UUIDs"] -git-tree-sha1 = "034174e607d254b8ca0853a1a9029b265114bf6c" -uuid = "a81c6b42-2e10-5240-aca2-a61377ecd94b" -version = "0.8.2" - -[[Dagger]] -deps = ["Distributed", "LinearAlgebra", "MemPool", "Profile", "Random", "Serialization", "SharedArrays", "SparseArrays", "Statistics", "StatsBase", "Test"] -git-tree-sha1 = "39dd9351c6637a71d1925e26e49adcd8a25ebf0b" -uuid = "d58978e5-989f-55fb-8d15-ea34adc7bf54" -version = "0.8.0" - -[[DataAPI]] -git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252" -uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" -version = "1.1.0" - -[[DataStructures]] -deps = ["InteractiveUtils", "OrderedCollections"] -git-tree-sha1 = "5a431d46abf2ef2a4d5d00bd0ae61f651cf854c8" -uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" -version = "0.17.10" - -[[Dates]] -deps = ["Printf"] -uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" - -[[DelimitedFiles]] -deps = ["Mmap"] -uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" - -[[Distributed]] -deps = ["Random", "Serialization", "Sockets"] -uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" - -[[FastClosures]] -git-tree-sha1 = "acebe244d53ee1b461970f8910c235b259e772ef" -uuid = "9aa1b823-49e4-5ca5-8b0f-3971ec8bab6a" -version = "0.3.2" - -[[FileIO]] -deps = ["Pkg"] -git-tree-sha1 = "3d7cb2c4c850439f19c4d6d3fbe1dce6481cddb1" -uuid = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" -version = "1.2.4" - -[[FixedPointNumbers]] -git-tree-sha1 = "3ba9ea634d4c8b289d590403b4a06f8e227a6238" -uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" -version = "0.8.0" - -[[Inflate]] -git-tree-sha1 = "f5fc07d4e706b84f72d54eedcc1c13d92fb0871c" -uuid = "d25df0c9-e2be-5dd7-82c8-3ad0b3e990b9" -version = "0.1.2" - -[[InteractiveUtils]] -deps = ["Markdown"] -uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" - -[[IterTools]] -git-tree-sha1 = "05110a2ab1fc5f932622ffea2a003221f4782c18" -uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e" -version = "1.3.0" - -[[IterativeSolvers]] -deps = ["LinearAlgebra", "Printf", "Random", "RecipesBase", "SparseArrays"] -git-tree-sha1 = "f272a02b57a1a1d2150a1a6cc4a3441ee806ab96" -uuid = "42fd0dbc-a981-5370-80f2-aaf504508153" -version = "0.8.3" - -[[JLD2]] -deps = ["CodecZlib", "DataStructures", "FileIO", "Mmap", "Printf"] -git-tree-sha1 = "343d1ec8ae7d57a608d79ef12e9a853cbb9acd96" -uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819" -version = "0.1.3" - -[[JSON]] -deps = ["Dates", "Mmap", "Parsers", "Unicode"] -git-tree-sha1 = "b34d7cef7b337321e97d22242c3c2b91f476748e" -uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" -version = "0.21.0" - -[[LibGit2]] -uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" - -[[Libdl]] -uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" - -[[LightGraphs]] -deps = ["ArnoldiMethod", "DataStructures", "Distributed", "Inflate", "LinearAlgebra", "Random", "SharedArrays", "SimpleTraits", "SparseArrays", "Statistics"] -git-tree-sha1 = "f40c4dbcd957cc3afc8cca0ff26c9f8304def00d" -uuid = "093fc24a-ae57-5d10-9952-331d41423f4d" -version = "1.3.1" - -[[LightXML]] -deps = ["BinaryProvider", "Libdl"] -git-tree-sha1 = "be855e3c975b89746b09952407c156b5e4a33a1d" -uuid = "9c8b4983-aa76-5018-a973-4c85ecc9e179" -version = "0.8.1" - -[[LinearAlgebra]] -deps = ["Libdl"] -uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" - -[[LinearMaps]] -deps = ["LinearAlgebra", "SparseArrays"] -git-tree-sha1 = "e204a96dbb8d49fbca24086c586734435d7bf5b5" -uuid = "7a12625a-238d-50fd-b39a-03d52299707e" -version = "2.6.1" - -[[LinearOperators]] -deps = ["FastClosures", "LinearAlgebra", "Printf", "SparseArrays", "TimerOutputs"] -git-tree-sha1 = "3d4043d5cc711f2fcdca545bfd4aafe5bc0b1bee" -uuid = "5c8ed15e-5a4c-59e4-a42b-c7e8811fb125" -version = "1.0.1" - -[[Logging]] -uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" - -[[MacroTools]] -deps = ["Markdown", "Random"] -git-tree-sha1 = "f7d2e3f654af75f01ec49be82c231c382214223a" -uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" -version = "0.5.5" - -[[Markdown]] -deps = ["Base64"] -uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" - -[[Match]] -git-tree-sha1 = "5cf525d97caf86d29307150fcba763a64eaa9cbe" -uuid = "7eb4fadd-790c-5f42-8a69-bfa0b872bfbf" -version = "1.1.0" - -[[Measures]] -git-tree-sha1 = "e498ddeee6f9fdb4551ce855a46f54dbd900245f" -uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e" -version = "0.3.1" - -[[MemPool]] -deps = ["DataStructures", "Distributed", "Mmap", "Random", "Serialization", "Sockets", "Test"] -git-tree-sha1 = "d52799152697059353a8eac1000d32ba8d92aa25" -uuid = "f9f48841-c794-520a-933b-121f7ba6ed94" -version = "0.2.0" - -[[MetaGraphs]] -deps = ["JLD2", "LightGraphs", "Random"] -git-tree-sha1 = "43ebbe06b22d213e4a8750424f9c7d1311bee2a6" -uuid = "626554b9-1ddb-594c-aa3c-2596fe9399a5" -version = "0.6.4" - -[[Missings]] -deps = ["DataAPI"] -git-tree-sha1 = "de0a5ce9e5289f27df672ffabef4d1e5861247d5" -uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28" -version = "0.4.3" - -[[Mmap]] -uuid = "a63ad114-7e13-5084-954f-fe012c677804" - -[[OrderedCollections]] -deps = ["Random", "Serialization", "Test"] -git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1" -uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" -version = "1.1.0" - -[[Parameters]] -deps = ["OrderedCollections"] -git-tree-sha1 = "b62b2558efb1eef1fa44e4be5ff58a515c287e38" -uuid = "d96e819e-fc66-5662-9728-84c9c7592b0a" -version = "0.12.0" - -[[Parsers]] -deps = ["Dates", "Test"] -git-tree-sha1 = "0c16b3179190d3046c073440d94172cfc3bb0553" -uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" -version = "0.3.12" - -[[Pkg]] -deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Test", "UUIDs"] -uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" - -[[Printf]] -deps = ["Unicode"] -uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" - -[[Profile]] -deps = ["Printf"] -uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" - -[[REPL]] -deps = ["InteractiveUtils", "Markdown", "Sockets"] -uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" - -[[Random]] -deps = ["Serialization"] -uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" - -[[RecipesBase]] -git-tree-sha1 = "b4ed4a7f988ea2340017916f7c9e5d7560b52cae" -uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" -version = "0.8.0" - -[[Reexport]] -deps = ["Pkg"] -git-tree-sha1 = "7b1d07f411bc8ddb7977ec7f377b97b158514fe0" -uuid = "189a3867-3050-52da-a836-e630ba90ab69" -version = "0.2.0" - -[[Requires]] -deps = ["UUIDs"] -git-tree-sha1 = "d37400976e98018ee840e0ca4f9d20baa231dc6b" -uuid = "ae029012-a4dd-5104-9daa-d747884805df" -version = "1.0.1" - -[[SHA]] -uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" - -[[Serialization]] -uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" - -[[SharedArrays]] -deps = ["Distributed", "Mmap", "Random", "Serialization"] -uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" - -[[SimpleTraits]] -deps = ["InteractiveUtils", "MacroTools"] -git-tree-sha1 = "2bdf3b6300a9d66fe29ee8bb51ba100c4df9ecbc" -uuid = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" -version = "0.9.1" - -[[Sockets]] -uuid = "6462fe0b-24de-5631-8697-dd941f90decc" - -[[SortingAlgorithms]] -deps = ["DataStructures", "Random", "Test"] -git-tree-sha1 = "03f5898c9959f8115e30bc7226ada7d0df554ddd" -uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c" -version = "0.3.1" - -[[SparseArrays]] -deps = ["LinearAlgebra", "Random"] -uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" - -[[StaticArrays]] -deps = ["LinearAlgebra", "Random", "Statistics"] -git-tree-sha1 = "5a3bcb6233adabde68ebc97be66e95dcb787424c" -uuid = "90137ffa-7385-5640-81b9-e52037218182" -version = "0.12.1" - -[[Statistics]] -deps = ["LinearAlgebra", "SparseArrays"] -uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" - -[[StatsBase]] -deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics"] -git-tree-sha1 = "a6102b1f364befdb05746f386b67c6b7e3262c45" -uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" -version = "0.33.0" - -[[Test]] -deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[[TimerOutputs]] -deps = ["Printf"] -git-tree-sha1 = "311765af81bbb48d7bad01fb016d9c328c6ede03" -uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" -version = "0.5.3" - -[[TranscodingStreams]] -deps = ["Random", "Test"] -git-tree-sha1 = "7c53c35547de1c5b9d46a4797cf6d8253807108c" -uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" -version = "0.9.5" - -[[UUIDs]] -deps = ["Random", "SHA"] -uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" - -[[Unicode]] -uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" - -[[Zlib_jll]] -deps = ["Libdl", "Pkg"] -git-tree-sha1 = "fd36a6739e256527287c5444960d0266712cd49e" -uuid = "83775a58-1f1d-513f-b197-d71354ab007a" -version = "1.2.11+8" From 000ffb1291469c86ce7eb63fffb374a502d84cca Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Fri, 27 Mar 2020 16:35:35 -0400 Subject: [PATCH 06/12] Cleaned up by introducing a constructor --- .../src/DaggerInstanceGLA.jl | 22 +++++++++---------- experiments/DaggerInstanceGLA/src/test.jl | 6 ++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl index b6fe58c7e..dda6ae6ea 100644 --- a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl +++ b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl @@ -3,10 +3,10 @@ module DaggerInstanceGLA import Base: + using AutoHashEquals -export DagDom, LinearMap, MatrixThunk, matrixToThunk, +export DagDom, LinearMap, MatrixThunk, dom, codom, adjoint, +, compose, id, oplus, mzero, braid, mcopy, delete, plus, zero, scalar, antipode, - ⊗ + ⊕ using Dagger using LinearAlgebra using Catlab.LinearAlgebra.GraphicalLinearAlgebra @@ -35,8 +35,8 @@ struct MatrixThunk codom::Int end -matrixToThunk(A::LinearMap) = begin - delayed(x->x)(A) +MatrixThunk(A::LinearMap) = begin + MatrixThunk(delayed(identity)(A), size(A,2), size(A,1)) end @instance LinearFunctions(DagDom, MatrixThunk) begin @@ -48,21 +48,21 @@ end codom(f::MatrixThunk) = f.codom compose(f::MatrixThunk, g::MatrixThunk) = MatrixThunk(delayed(*)(g.thunk,f.thunk), g.dom, f.codom) - id(V::DagDom) = MatrixThunk(delayed(x->x)(LMs.UniformScalingMap(1, V.N)), V.N, V.N) + id(V::DagDom) = MatrixThunk(LMs.UniformScalingMap(1, V.N)) oplus(V::DagDom, W::DagDom) = DagDom(V.N + W.N) oplus(f::MatrixThunk, g::MatrixThunk) = MatrixThunk(delayed(LMs.BlockDiagonalMap)(f.thunk, g.thunk), f.dom+g.dom, f.codom+g.codom) mzero(::Type{DagDom}) = DagDom(0) braid(V::DagDom, W::DagDom) = - MatrixThunk(delayed(x->x)(LinearMap(braid_lm(V.N), braid_lm(W.N), W.N+V.N, V.N+W.N)),V.N+W.N, W.N+V.N) + MatrixThunk(LinearMap(braid_lm(V.N), braid_lm(W.N), W.N+V.N, V.N+W.N)) - mcopy(V::DagDom) = MatrixThunk(delayed(x->x)(LinearMap(mcopy_lm, plus_lm, 2*V.N, V.N)), V.N, 2*V.N) - delete(V::DagDom) = MatrixThunk(delayed(x->x)(LinearMap(delete_lm, zero_lm(V.N), 0, V.N)), V.N, 0) - plus(V::DagDom) = MatrixThunk(delayed(x->x)(LinearMap(plus_lm, mcopy_lm, V.N, 2*V.N)), 2*V.N, V.N) - zero(V::DagDom) = MatrixThunk(delayed(x->x)(LinearMap(zero_lm(V.N), delete_lm, V.N, 0)), 0, V.N) + mcopy(V::DagDom) = MatrixThunk(LinearMap(mcopy_lm, plus_lm, 2*V.N, V.N)) + delete(V::DagDom) = MatrixThunk(LinearMap(delete_lm, zero_lm(V.N), 0, V.N)) + plus(V::DagDom) = MatrixThunk(LinearMap(plus_lm, mcopy_lm, V.N, 2*V.N)) + zero(V::DagDom) = MatrixThunk(LinearMap(zero_lm(V.N), delete_lm, V.N, 0)) plus(f::MatrixThunk, g::MatrixThunk) = f+g - scalar(V::DagDom, c::Number) = MatrixThunk(delayed(x->x)(LMs.UniformScalingMap(c, V.N)), V.N, V.N) + scalar(V::DagDom, c::Number) = MatrixThunk(LMs.UniformScalingMap(c, V.N)) antipode(V::DagDom) = scalar(V, -1) end diff --git a/experiments/DaggerInstanceGLA/src/test.jl b/experiments/DaggerInstanceGLA/src/test.jl index de8a6113b..7e3ffc6d5 100644 --- a/experiments/DaggerInstanceGLA/src/test.jl +++ b/experiments/DaggerInstanceGLA/src/test.jl @@ -3,9 +3,9 @@ using Catlab.LinearAlgebra.GraphicalLinearAlgebra, DaggerInstanceGLA, Catlab, Ca V, W = Ob(FreeLinearFunctions, :V, :W) f, g, h = Hom(:f, V, W), Hom(:g, V, W), Hom(:h, W, W) -fop = MatrixThunk(matrixToThunk(LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0])), 4,3) -gop = MatrixThunk(matrixToThunk(LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0])), 4,3) -hop = MatrixThunk(matrixToThunk(LinearMap([1 4 -1; -1 1 0; 1 1 -1.0])), 3,3) +fop = MatrixThunk(LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0])) +gop = MatrixThunk(LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0])) +hop = MatrixThunk(LinearMap([1 4 -1; -1 1 0; 1 1 -1.0])) #fop = LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0]) #gop = LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0]) From 3fe899cf65b583158fc0fba9bc6e61dbd7fc5450 Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Fri, 27 Mar 2020 17:30:36 -0400 Subject: [PATCH 07/12] Added testing suite --- experiments/DaggerInstanceGLA/Project.toml | 1 + experiments/DaggerInstanceGLA/src/test.jl | 23 -------------- .../test/DaggerInstanceGLA.jl | 31 +++++++++++++++++++ .../DaggerInstanceGLA/test/runtests.jl | 5 +++ 4 files changed, 37 insertions(+), 23 deletions(-) delete mode 100644 experiments/DaggerInstanceGLA/src/test.jl create mode 100644 experiments/DaggerInstanceGLA/test/DaggerInstanceGLA.jl create mode 100644 experiments/DaggerInstanceGLA/test/runtests.jl diff --git a/experiments/DaggerInstanceGLA/Project.toml b/experiments/DaggerInstanceGLA/Project.toml index 46ca2ddad..87a7f58d8 100644 --- a/experiments/DaggerInstanceGLA/Project.toml +++ b/experiments/DaggerInstanceGLA/Project.toml @@ -9,3 +9,4 @@ Catlab = "134e5e36-593f-5add-ad60-77f754baafbe" Dagger = "d58978e5-989f-55fb-8d15-ea34adc7bf54" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LinearMaps = "7a12625a-238d-50fd-b39a-03d52299707e" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/experiments/DaggerInstanceGLA/src/test.jl b/experiments/DaggerInstanceGLA/src/test.jl deleted file mode 100644 index 7e3ffc6d5..000000000 --- a/experiments/DaggerInstanceGLA/src/test.jl +++ /dev/null @@ -1,23 +0,0 @@ -using Catlab.LinearAlgebra.GraphicalLinearAlgebra, DaggerInstanceGLA, Catlab, Catlab.Doctrines - -V, W = Ob(FreeLinearFunctions, :V, :W) -f, g, h = Hom(:f, V, W), Hom(:g, V, W), Hom(:h, W, W) - -fop = MatrixThunk(LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0])) -gop = MatrixThunk(LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0])) -hop = MatrixThunk(LinearMap([1 4 -1; -1 1 0; 1 1 -1.0])) - -#fop = LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0]) -#gop = LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0]) -#hop = LinearMap([1 4 -1; -1 1 0; 1 1 -1.0]) - - -d = Dict(f=>fop, g=>gop, h => hop, V => DagDom(4), W=>DagDom(3)) -F(ex) = functor((DagDom, MatrixThunk), ex, generators=d) -#d = Dict(f=>fop, g=>gop, h => hop, V => LinearMapDom(4), W=>LinearMapDom(3)) -#F(ex) = functor((LinearMapDom, LinearMap), ex, generators=d) - -M = plus(f,g)⋅h - -print(Matrix(collect(F(M).thunk))) -print('\n') diff --git a/experiments/DaggerInstanceGLA/test/DaggerInstanceGLA.jl b/experiments/DaggerInstanceGLA/test/DaggerInstanceGLA.jl new file mode 100644 index 000000000..dc48f2cff --- /dev/null +++ b/experiments/DaggerInstanceGLA/test/DaggerInstanceGLA.jl @@ -0,0 +1,31 @@ +using Catlab.LinearAlgebra.GraphicalLinearAlgebra, + DaggerInstanceGLA, Catlab, Catlab.Doctrines +using Test + +V, W = Ob(FreeLinearFunctions, :V, :W) +f, g, h = Hom(:f, V, W), Hom(:g, V, W), Hom(:h, W, W) + +fmap = LinearMap([1 1 1 1; 2 2 2 3; 3 3 3 3.0]) +gmap = LinearMap([3 1 1 1; -1 -1 -1 -1; 0 2 0 0.0]) +hmap = LinearMap([1 4 -1; -1 1 0; 1 1 -1.0]) + +fop = MatrixThunk(fmap) +gop = MatrixThunk(gmap) +hop = MatrixThunk(hmap) + +d = Dict(f=>fop, g=>gop, h => hop, V => DagDom(4), W=>DagDom(3)) +F(ex) = functor((DagDom, MatrixThunk), ex, generators=d) + +dmap = Dict(f=>fmap, g=>gmap, h => hmap, V => LinearMapDom(4), W=>LinearMapDom(3)) +Fmap(ex) = functor((LinearMapDom, LinearMap), ex, generators=dmap) + +M = plus(f,g)⋅h +N = adjoint(g)⋅f⋅mcopy(W)+h⋅mcopy(W) +O = (adjoint(g)⊕f)+(h⊕(g⋅adjoint(f))) + +expressions = [M,N,O] + +for expression in expressions + @test Matrix(collect(F(expression).thunk)) == + Matrix(Fmap(expression)) +end diff --git a/experiments/DaggerInstanceGLA/test/runtests.jl b/experiments/DaggerInstanceGLA/test/runtests.jl new file mode 100644 index 000000000..bb01a5afd --- /dev/null +++ b/experiments/DaggerInstanceGLA/test/runtests.jl @@ -0,0 +1,5 @@ +using Test + +@testset "DaggerInstanceGLA" begin + include("DaggerInstanceGLA.jl") +end From f1e5acb5b7f0b31036959f10bf5fc3bafa067933 Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Fri, 27 Mar 2020 17:30:51 -0400 Subject: [PATCH 08/12] Fixed valuation of BlockDiagonal as function --- experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl index dda6ae6ea..b4c20ca65 100644 --- a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl +++ b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl @@ -47,11 +47,15 @@ end dom(f::MatrixThunk) = f.dom codom(f::MatrixThunk) = f.codom - compose(f::MatrixThunk, g::MatrixThunk) = MatrixThunk(delayed(*)(g.thunk,f.thunk), g.dom, f.codom) + compose(f::MatrixThunk, g::MatrixThunk) = + MatrixThunk(delayed(*)(g.thunk,f.thunk), g.dom, f.codom) id(V::DagDom) = MatrixThunk(LMs.UniformScalingMap(1, V.N)) oplus(V::DagDom, W::DagDom) = DagDom(V.N + W.N) - oplus(f::MatrixThunk, g::MatrixThunk) = MatrixThunk(delayed(LMs.BlockDiagonalMap)(f.thunk, g.thunk), f.dom+g.dom, f.codom+g.codom) + oplus(f::MatrixThunk, g::MatrixThunk) = + MatrixThunk(delayed((f,g)->LMs.BlockDiagonalMap(f,g))(f.thunk, g.thunk), + f.dom+g.dom, f.codom+g.codom) + mzero(::Type{DagDom}) = DagDom(0) braid(V::DagDom, W::DagDom) = MatrixThunk(LinearMap(braid_lm(V.N), braid_lm(W.N), W.N+V.N, V.N+W.N)) From 208f553a12cf5cc910fcba4a054ed21c74a47b0f Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Fri, 27 Mar 2020 17:42:41 -0400 Subject: [PATCH 09/12] Ignored Manifest.toml files for Experiments --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 9fddebe4b..713f69df7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ *.jl.cov *.jl.*.cov *.jl.mem + +# Experiments +experiments/*/Manifest.toml From 7c298588cd5d681450bc8c605e22cfb5449ad975 Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Fri, 3 Apr 2020 16:39:41 -0400 Subject: [PATCH 10/12] Fixed dependency issue --- experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl index b4c20ca65..7c72276c1 100644 --- a/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl +++ b/experiments/DaggerInstanceGLA/src/DaggerInstanceGLA.jl @@ -11,7 +11,7 @@ using Dagger using LinearAlgebra using Catlab.LinearAlgebra.GraphicalLinearAlgebra using Catlab, Catlab.Doctrines -import ...Doctrines: +import Catlab.Doctrines: Ob, Hom, dom, codom, compose, ⋅, ∘, id, oplus, ⊕, mzero, braid, dagger, dunit, dcounit, mcopy, Δ, delete, ◊, mmerge, ∇, create, □, plus, zero, coplus, cozero, meet, top, join, bottom From 71c25809b4407bb1057a7bcb0b800aad581e013b Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Fri, 3 Apr 2020 16:40:17 -0400 Subject: [PATCH 11/12] Dagger Instance of a Symmetric Monoidal Category --- experiments/DaggerAMC/Project.toml | 10 ++ experiments/DaggerAMC/src/DaggerAMC.jl | 131 +++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 experiments/DaggerAMC/Project.toml create mode 100644 experiments/DaggerAMC/src/DaggerAMC.jl diff --git a/experiments/DaggerAMC/Project.toml b/experiments/DaggerAMC/Project.toml new file mode 100644 index 000000000..cbe3556b3 --- /dev/null +++ b/experiments/DaggerAMC/Project.toml @@ -0,0 +1,10 @@ +name = "DaggerAMC" +uuid = "85a14c48-69db-4cd9-97af-587947037424" +authors = ["bosonbaas "] +version = "0.1.0" + +[deps] +AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f" +Catlab = "134e5e36-593f-5add-ad60-77f754baafbe" +Dagger = "d58978e5-989f-55fb-8d15-ea34adc7bf54" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/experiments/DaggerAMC/src/DaggerAMC.jl b/experiments/DaggerAMC/src/DaggerAMC.jl new file mode 100644 index 000000000..9049b1e36 --- /dev/null +++ b/experiments/DaggerAMC/src/DaggerAMC.jl @@ -0,0 +1,131 @@ +module DaggerAMC + +import Base: + +using AutoHashEquals + +export DagDom, ThunkArr, + dom, codom, compose, id, oplus, mzero, + zero, ⊕ + # To implement + # braid, mcopy, delete, plus, zero, + +using Dagger +using LinearAlgebra +#using Catlab.Doctrines.AdditiveMonoidal +using Catlab, Catlab.Doctrines +import Catlab.Doctrines: + Ob, Hom, dom, codom, compose, ⋅, ∘, id, oplus, ⊕, mzero, braid, + dagger, dunit, dcounit, mcopy, Δ, delete, ◊, mmerge, ∇, create, □, + plus, zero, coplus, cozero, meet, top, join, bottom + + +@auto_hash_equals struct DagDom + N::Int +end + +# This structure was created to keep track of dom and codom information. +# This information can be updated efficiently, and keeping it here keeps +# LinearFunctions from having to think the thunk each time the dom or codom +# is queried + +struct ThunkArr + input::Array{Tuple{Int64,Int64},1} + output::Array{Int64,1} + thunks::Array{Thunk,1} +end + +input_nodes(f::ThunkArr) = begin + input_n = Set{Int64}() + for port in f.input + push!(input_n,port[1]) + end + return input_n +end + +#MatrixThunk(A::LinearMap) = begin +# MatrixThunk(delayed(identity)(A), size(A,2), size(A,1)) +#end + +@instance AdditiveSymmetricMonoidalCategory(DagDom, ThunkArr) begin + zero(V::DagDom) = DagDom(0) + mzero(::Type{DagDom}) = DagDom(0) + dom(f::ThunkArr) = size(f.input)[1] + codom(f::ThunkArr) = size(f.output)[1] + + compose(f::ThunkArr,g::ThunkArr) = begin + add_ind = (x,n) -> x+n + n_output = add_ind.(f.output,size(g.thunks)[1]) + n_input = g.input + # f_inputs stores what thunks will be passed in from g + f_inputs = Dict(x => Array{Thunk}(undef, length(f.thunks[x].inputs)) for x in input_nodes(f)) + # Fill out the values that will be passed in from g + for port_num in 1:length(f.input) + port = f.input[port_num] + g_node = g.thunks[g.output[port_num]] + f_inputs[port[1]][port[2]] = g_node + end + for (key,g_in) in f_inputs + f.thunks[key].inputs = Tuple(g_in) + end + + n_thunks = vcat(g.thunks,f.thunks) + ThunkArr(n_input, n_output, n_thunks) + end + + oplus(V::DagDom, W::DagDom) = DagDom(V.N + W.N) + oplus(f::ThunkArr, g::ThunkArr) = begin + add_tup = (x,n) -> (x[1]+n,x[2]) + add_ind = (x,n) -> x+n + n_thunks = vcat(f.thunks, g.thunks) + n_input = vcat(f.input, add_tup.(g.input,size(f.thunks)[1])) + n_output = vcat(f.output, add_ind.(g.output,size(f.thunks)[1])) + ThunkArr(n_input, n_output, n_thunks) + end + + id(V::DagDom) = begin + add_port = x -> (x,1) + id_thunks = [delayed(identity)(1) for x in 1:V.N] + id_input = add_port.(Array(1:V.N)) + id_output = Array(1:V.N) + ThunkArr(id_input, id_output, id_thunks) + end + + braid(V::DagDom, W::DagDom) = begin + # Next to be implemented + end + + #adjoint(f::MatrixThunk) = MatrixThunk(delayed(adjoint)(f.thunk), f.codom, f.dom) + #+(f::MatrixThunk, g::MatrixThunk) = MatrixThunk(delayed(+)(f.thunk, g.thunk), f.dom, f.codom) + + #compose(f::MatrixThunk, g::MatrixThunk) = + # MatrixThunk(delayed(*)(g.thunk,f.thunk), g.dom, f.codom) + #id(V::DagDom) = MatrixThunk(LMs.UniformScalingMap(1, V.N)) + + #oplus(V::DagDom, W::DagDom) = DagDom(V.N + W.N) + #oplus(f::MatrixThunk, g::MatrixThunk) = + # MatrixThunk(delayed((f,g)->LMs.BlockDiagonalMap(f,g))(f.thunk, g.thunk), + # f.dom+g.dom, f.codom+g.codom) + # + #mzero(::Type{DagDom}) = DagDom(0) + #braid(V::DagDom, W::DagDom) = + #MatrixThunk(LinearMap(braid_lm(V.N), braid_lm(W.N), W.N+V.N, V.N+W.N)) + + #mcopy(V::DagDom) = MatrixThunk(LinearMap(mcopy_lm, plus_lm, 2*V.N, V.N)) + #delete(V::DagDom) = MatrixThunk(LinearMap(delete_lm, zero_lm(V.N), 0, V.N)) + #plus(V::DagDom) = MatrixThunk(LinearMap(plus_lm, mcopy_lm, V.N, 2*V.N)) + + #plus(f::MatrixThunk, g::MatrixThunk) = f+g + #scalar(V::DagDom, c::Number) = MatrixThunk(LMs.UniformScalingMap(c, V.N)) + #antipode(V::DagDom) = scalar(V, -1) +end + +#braid_lm(n::Int) = x::AbstractVector -> vcat(x[n+1:end], x[1:n]) +#mcopy_lm(x::AbstractVector) = vcat(x, x) +#delete_lm(x::AbstractVector) = eltype(x)[] +#plus_lm(x::AbstractVector) = begin +# n = length(x) ÷ 2 +# x[1:n] + x[n+1:end] +#end +#zero_lm(n::Int) = x::AbstractVector -> zeros(eltype(x), n) + +end From 41a5ae5a84d16c6a4decb6c15016258d04b4cc87 Mon Sep 17 00:00:00 2001 From: bosonbaas Date: Tue, 7 Apr 2020 17:15:21 -0400 Subject: [PATCH 12/12] Added working tests, but now variable interference is an issue --- experiments/DaggerAMC/Project.toml | 2 + experiments/DaggerAMC/src/DaggerAMC.jl | 60 ++++++++++++++++++-------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/experiments/DaggerAMC/Project.toml b/experiments/DaggerAMC/Project.toml index cbe3556b3..faa79d093 100644 --- a/experiments/DaggerAMC/Project.toml +++ b/experiments/DaggerAMC/Project.toml @@ -8,3 +8,5 @@ AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f" Catlab = "134e5e36-593f-5add-ad60-77f754baafbe" Dagger = "d58978e5-989f-55fb-8d15-ea34adc7bf54" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +LinearMaps = "7a12625a-238d-50fd-b39a-03d52299707e" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/experiments/DaggerAMC/src/DaggerAMC.jl b/experiments/DaggerAMC/src/DaggerAMC.jl index 9049b1e36..95ed91022 100644 --- a/experiments/DaggerAMC/src/DaggerAMC.jl +++ b/experiments/DaggerAMC/src/DaggerAMC.jl @@ -3,16 +3,16 @@ module DaggerAMC import Base: + using AutoHashEquals -export DagDom, ThunkArr, +export DagDom, ThunkArr, copy, dom, codom, compose, id, oplus, mzero, - zero, ⊕ + zero, ⊕, braid # To implement - # braid, mcopy, delete, plus, zero, + # mcopy, delete, plus, zero, using Dagger using LinearAlgebra #using Catlab.Doctrines.AdditiveMonoidal -using Catlab, Catlab.Doctrines +using Catlab, Catlab.Doctrines, Catlab.Programs import Catlab.Doctrines: Ob, Hom, dom, codom, compose, ⋅, ∘, id, oplus, ⊕, mzero, braid, dagger, dunit, dcounit, mcopy, Δ, delete, ◊, mmerge, ∇, create, □, @@ -42,9 +42,23 @@ input_nodes(f::ThunkArr) = begin return input_n end -#MatrixThunk(A::LinearMap) = begin -# MatrixThunk(delayed(identity)(A), size(A,2), size(A,1)) -#end +copy(A::ThunkArr) = begin + n_thunks = [Thunk(x.f, x.inputs...) for x in A.thunks] + n_input = A.input + n_output = A.output + ThunkArr(n_input, n_output, n_thunks) +end + +ThunkArr(A::AbstractArray) = begin + id_thunks = [delayed(identity)(A[x]) for x in 1:length(A)[1]] + id_input = [] + id_output = Array(1:length(A)) + ThunkArr(id_input, id_output, id_thunks) +end + +ThunkArr(A) = begin + ThunkArr([A]) +end @instance AdditiveSymmetricMonoidalCategory(DagDom, ThunkArr) begin zero(V::DagDom) = DagDom(0) @@ -54,31 +68,37 @@ end compose(f::ThunkArr,g::ThunkArr) = begin add_ind = (x,n) -> x+n - n_output = add_ind.(f.output,size(g.thunks)[1]) - n_input = g.input + cf = f + cg = g + n_output = add_ind.(cf.output,size(cg.thunks)[1]) + n_input = cg.input # f_inputs stores what thunks will be passed in from g - f_inputs = Dict(x => Array{Thunk}(undef, length(f.thunks[x].inputs)) for x in input_nodes(f)) + f_inputs = Dict(x => Array{Thunk}(undef, length(cf.thunks[x].inputs)) for x in input_nodes(cf)) # Fill out the values that will be passed in from g - for port_num in 1:length(f.input) - port = f.input[port_num] - g_node = g.thunks[g.output[port_num]] + for port_num in 1:length(cf.input) + port = cf.input[port_num] + g_node = cg.thunks[cg.output[port_num]] f_inputs[port[1]][port[2]] = g_node end for (key,g_in) in f_inputs - f.thunks[key].inputs = Tuple(g_in) + cf.thunks[key].inputs = Tuple(g_in) end - n_thunks = vcat(g.thunks,f.thunks) + n_thunks = vcat(cg.thunks,cf.thunks) ThunkArr(n_input, n_output, n_thunks) end oplus(V::DagDom, W::DagDom) = DagDom(V.N + W.N) + + # Make copies of thunks to keep from variable interference oplus(f::ThunkArr, g::ThunkArr) = begin add_tup = (x,n) -> (x[1]+n,x[2]) add_ind = (x,n) -> x+n - n_thunks = vcat(f.thunks, g.thunks) - n_input = vcat(f.input, add_tup.(g.input,size(f.thunks)[1])) - n_output = vcat(f.output, add_ind.(g.output,size(f.thunks)[1])) + cf = f + cg = g + n_thunks = vcat(cf.thunks, cg.thunks) + n_input = vcat(cf.input, add_tup.(cg.input,size(cf.thunks)[1])) + n_output = vcat(cf.output, add_ind.(cg.output,size(cf.thunks)[1])) ThunkArr(n_input, n_output, n_thunks) end @@ -91,7 +111,9 @@ end end braid(V::DagDom, W::DagDom) = begin - # Next to be implemented + vw_id = id(V.N + W.N) + vw_id.output = vcat(vw_id.output[V.N+1:end],vw_id.output[1:V.N]) + return vw_id end #adjoint(f::MatrixThunk) = MatrixThunk(delayed(adjoint)(f.thunk), f.codom, f.dom)