Skip to content

Commit

Permalink
Fiddled with pixel array documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelsonric committed Sep 26, 2023
1 parent 71573b0 commit a1f555a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 47 deletions.
44 changes: 22 additions & 22 deletions docs/literate/pixel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ using Catlab
using UnicodePlots

using Catlab.CategoricalAlgebra.FinRelations: BoolRig
# Spivak, Dobson, Kumari, and Wu, *Pixel Arrays*: A fast and elementary method for solving nonlinear systems.
# Spivak, Dobson, Kumari, and Wu, *Pixel Arrays: A fast and elementary method for solving nonlinear systems.*
#
# A pixel array is an array with entries in the boolean semiring.
# A pixel array is an array with entries in the boolean semiring. The function defined below constructs a pixel array by discretizing a relation of the form ``xRy \iff f(x, y) = 0``.
const PixelArray{N} = Array{BoolRig, N}

function PixelArray(f::Function, xdim::NamedTuple, ydim::NamedTuple, tol::Real)
Expand All @@ -30,12 +30,12 @@ function PixelArray(f::Function, xdim::NamedTuple, ydim::NamedTuple, tol::Real)
end

result
end
# For plotting.
end;
# For plotting:
function Base.isless(x::Int, y::BoolRig)
y == true ? x < 1 : x < 0
end;
# Example 2.4.1
# Example 2.4.1 in Spivak et. al.
function f₁(x::Real, z::Real)
0 - (cos(log(z^2 + x / 10^3)) x + 1 / z / 10^5)
end
Expand All @@ -52,35 +52,35 @@ w = x = y = z = (lower = -3, upper = 3, resolution = 125)

tol = .2

A= PixelArray(f₁, x, z, tol)
A= PixelArray(f₂, w, y, tol)
A= PixelArray(f₃, x, y, tol);
# f₁(x, z)
spy(A₁)
# f₂(w, y)
spy(A₂)
# f₃(x, y)
spy(A₃)
R= PixelArray(f₁, x, z, tol)
R= PixelArray(f₂, w, y, tol)
R= PixelArray(f₃, x, y, tol);
#
spy(R₁; title="f₁(x, z) = 0", xlabel="x", ylabel="z")
#
spy(R₂; title="f₂(w, y) = 0", xlabel="w", ylabel="y")
#
spy(R₃; title="f₃(x, y) = 0", xlabel="x", ylabel="y")
#
diagram = @relation (w, z) where (w::n, x::n, y::n, z::n) begin
f(x, z)
f(w, y)
f(x, y)
R(x, z)
R(w, y)
R(x, y)
end

to_graphviz(diagram; box_labels=:name, junction_labels=:variable)
#
hom_map = Dict{Symbol, PixelArray}(
:f => A₁,
:f => A₂,
:f => A₃)
:R => R₁,
:R => R₂,
:R => R₃)

ob_map = Dict(
:n => 125)

problem = InferenceProblem(diagram, hom_map, ob_map)

A = solve(problem)
R = solve(problem)

spy(A)
spy(R; title="f₁(x, z) = 0 ∧ f₂(w, y) = 0 ∧ f₃(x, y) = 0", xlabel="w", ylabel="z")
#
45 changes: 20 additions & 25 deletions docs/src/generated/pixel.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ using UnicodePlots
using Catlab.CategoricalAlgebra.FinRelations: BoolRig
````

Spivak, Dobson, Kumari, and Wu, *Pixel Arrays*: A fast and elementary method for solving nonlinear systems.
Spivak, Dobson, Kumari, and Wu, *Pixel Arrays: A fast and elementary method for solving nonlinear systems.*

A pixel array is an array with entries in the boolean semiring.
A pixel array is an array with entries in the boolean semiring. The function defined below constructs a pixel array by discretizing a relation of the form ``xRy \iff f(x, y) = 0``.

````@example pixel
const PixelArray{N} = Array{BoolRig, N}
Expand All @@ -40,10 +40,11 @@ function PixelArray(f::Function, xdim::NamedTuple, ydim::NamedTuple, tol::Real)
end
result
end
end;
nothing #hide
````

For plotting.
For plotting:

````@example pixel
function Base.isless(x::Int, y::BoolRig)
Expand All @@ -52,7 +53,7 @@ end;
nothing #hide
````

Example 2.4.1
Example 2.4.1 in Spivak et. al.

````@example pixel
function f₁(x::Real, z::Real)
Expand All @@ -71,53 +72,47 @@ w = x = y = z = (lower = -3, upper = 3, resolution = 125)
tol = .2
A₁ = PixelArray(f₁, x, z, tol)
A₂ = PixelArray(f₂, w, y, tol)
A₃ = PixelArray(f₃, x, y, tol);
R₁ = PixelArray(f₁, x, z, tol)
R₂ = PixelArray(f₂, w, y, tol)
R₃ = PixelArray(f₃, x, y, tol);
nothing #hide
````

f₁(x, z)

````@example pixel
spy(A₁)
spy(R₁; title="f₁(x, z) = 0", xlabel="x", ylabel="z")
````

f₂(w, y)

````@example pixel
spy(A₂)
spy(R₂; title="f₂(w, y) = 0", xlabel="w", ylabel="y")
````

f₃(x, y)

````@example pixel
spy(A₃)
spy(R₃; title="f₃(x, y) = 0", xlabel="x", ylabel="y")
````

````@example pixel
diagram = @relation (w, z) where (w::n, x::n, y::n, z::n) begin
f₁(x, z)
f₂(w, y)
f₃(x, y)
R₁(x, z)
R₂(w, y)
R₃(x, y)
end
to_graphviz(diagram; box_labels=:name, junction_labels=:variable)
````

````@example pixel
hom_map = Dict{Symbol, PixelArray}(
:f₁ => A₁,
:f₂ => A₂,
:f₃ => A₃)
:R₁ => R₁,
:R₂ => R₂,
:R₃ => R₃)
ob_map = Dict(
:n => 125)
problem = InferenceProblem(diagram, hom_map, ob_map)
A = solve(problem)
R = solve(problem)
spy(A)
spy(R; title="f₁(x, z) = 0 ∧ f₂(w, y) = 0 ∧ f₃(x, y) = 0", xlabel="w", ylabel="z")
````

0 comments on commit a1f555a

Please sign in to comment.