Skip to content

Commit

Permalink
Final touches.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelsonric committed Sep 26, 2023
1 parent a1f555a commit 11d7c48
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
27 changes: 21 additions & 6 deletions docs/literate/pixel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ 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.*
# 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
# ```math
# xRy \iff f(x, y) = 0.
# ```
#
# 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``.
# References:
# - Spivak, David I. et al. "Pixel Arrays: A fast and elementary method for solving
# nonlinear systems." *arXiv: Numerical Analysis* (2016): n. pag.
const PixelArray{N} = Array{BoolRig, N}

function PixelArray(f::Function, xdim::NamedTuple, ydim::NamedTuple, tol::Real)
Expand Down Expand Up @@ -36,6 +42,15 @@ function Base.isless(x::Int, y::BoolRig)
y == true ? x < 1 : x < 0
end;
# Example 2.4.1 in Spivak et. al.
# ```math
# \begin{align*}
# & \text{Solve relations:} && \cos(\ln(z^2 + 10^{-3}x)) - x + 10^{-5}z^{-1} = 0 && \text{(Equation 1)} \\
# & && \cosh(w + 10^{-3}y) + y + 10^{-4}w = 2 && \text{(Equation 2)} \\
# & && \tan(x + y)(x - 2)^{-1}(x + 3)^{-1}y^{-2} = 1 && \text{(Equation 3)} \\
# & \text{Range and resolution:} && w, x, y, z \in [-3, 3)@124 && \\
# & \text{Expose variables:} && (w, z) && \\
# & \end{align*}
# ```
function f₁(x::Real, z::Real)
0 - (cos(log(z^2 + x / 10^3)) x + 1 / z / 10^5)
end
Expand All @@ -56,11 +71,11 @@ 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="Equation 1", xlabel="x", ylabel="z")
#
spy(R₂; title="f₂(w, y) = 0", xlabel="w", ylabel="y")
spy(R₂; title="Equation 2", xlabel="w", ylabel="y")
#
spy(R₃; title="f₃(x, y) = 0", xlabel="x", ylabel="y")
spy(R₃; title="Equation 3", xlabel="x", ylabel="y")
#
diagram = @relation (w, z) where (w::n, x::n, y::n, z::n) begin
R₁(x, z)
Expand All @@ -82,5 +97,5 @@ problem = InferenceProblem(diagram, hom_map, ob_map)

R = solve(problem)

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

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. The function defined below
constructs a pixel array by discretizing a relation of the form
```math
xRy \iff f(x, y) = 0.
```

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``.
References:
- Spivak, David I. et al. "Pixel Arrays: A fast and elementary method for solving
nonlinear systems." *arXiv: Numerical Analysis* (2016): n. pag.

````@example pixel
const PixelArray{N} = Array{BoolRig, N}
Expand Down Expand Up @@ -54,6 +60,15 @@ nothing #hide
````

Example 2.4.1 in Spivak et. al.
```math
\begin{align*}
& \text{Solve relations:} && \cos(\ln(z^2 + 10^{-3}x)) - x + 10^{-5}z^{-1} = 0 && \text{(Equation 1)} \\
& && \cosh(w + 10^{-3}y) + y + 10^{-4}w = 2 && \text{(Equation 2)} \\
& && \tan(x + y)(x - 2)^{-1}(x + 3)^{-1}y^{-2} = 1 && \text{(Equation 3)} \\
& \text{Range and resolution:} && w, x, y, z \in [-3, 3)@124 && \\
& \text{Expose variables:} && (w, z) && \\
& \end{align*}
```

````@example pixel
function f₁(x::Real, z::Real)
Expand All @@ -79,15 +94,15 @@ nothing #hide
````

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

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

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

````@example pixel
Expand All @@ -113,6 +128,6 @@ problem = InferenceProblem(diagram, hom_map, ob_map)
R = solve(problem)
spy(R; title="f₁(x, z) = 0 ∧ f₂(w, y) = 0 ∧ f₃(x, y) = 0", xlabel="w", ylabel="z")
spy(R; title="Solution", xlabel="w", ylabel="z")
````

0 comments on commit 11d7c48

Please sign in to comment.