AlgebraicJulia / AlgebraicJulia/Catlab.jl

Utilities for programming with ACSets and Operad Algebras

未关闭
#341 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Julia
星标
724
派生
73
PR 合并指标
30 天内没有已合并 PR

描述

Possibly related to AlgebraicJulia/ACSets.jl#7. One of the great things about programming with an ACSet `X: C-->FinSet` is that every set `X(c), c in Ob(C)` has a globally consistent ordering so you can iterate over it without allocations. When you are implementing an operad algebra, you have a set of arguments, which are ACSets `xs = [X1, X2, ... Xn]` and you want to map a function `f` over this list and put the outputs somewhere. If `f: Int --> Vector`, then we could do something like this:

```julia
collect(Iterators.flatten(map(f, xs)))
```

but that allocates an array for each `x` in `xs` to hold the output and then you flatten it into single array by concatenating the vectors together. If you knew the lengths of `f(x)` ahead of time, you could preallocate the output. But in ACT oriented computing, we have a coproduct that tells us where each element of `f(x)` goes in this big concatenated array.

```julia
colimtmap!(f::Function, output, C::Colimit, xs) = begin
for (i,x) in enumerate(xs)
y = f(i, x)
I = legs(C)[i](1:length(y))
output[I] .= y
end
return output
end
```

You would call this as
```julia
y = zeros(Float64, sum(nports.(xs))
colimitmap!(y, coproduct([FinSet(nports(x)) for x in xs]), xs) do (i,x)
println("working on argument $i")
f(x)
end
```

This came up in an algebra of Circular Port Graphs. I want to execute `f` over each box which will produce a vector of Float64, then concatenate all the outputs into one big vector. I am going to call this function in a tight loop, so I can't afford to allocate and flatten every time.

Do you think this is worth having in Catlab? How does this fit into the larger picture of things you can do with ACSets? As David always says, "a mathematician sees a definition and asks 'what are some examples of this?', but a category theorist looks at a definition and asks 'what is this an example of'"

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。