dyna3/engine-proto/Engine.Numerical.jl

53 lines
1.8 KiB
Julia
Raw Normal View History

module Numerical
2024-02-16 00:00:46 +00:00
using Random: default_rng
2024-02-10 22:39:26 +00:00
using LinearAlgebra
using AbstractAlgebra
2024-02-13 01:34:12 +00:00
using HomotopyContinuation:
Variable, Expression, AbstractSystem, System, LinearSubspace,
nvariables, isreal, witness_set, results
import GLMakie
using ..Algebraic
# --- polynomial conversion ---
# hat tip Sascha Timme
# https://github.com/JuliaHomotopyContinuation/HomotopyContinuation.jl/issues/520#issuecomment-1317681521
function Base.convert(::Type{Expression}, f::MPolyRingElem)
variables = Variable.(symbols(parent(f)))
2024-02-13 01:34:12 +00:00
f_data = zip(coefficients(f), exponent_vectors(f))
sum(cf * prod(variables .^ exp_vec) for (cf, exp_vec) in f_data)
end
# create a ModelKit.System from an ideal in a multivariate polynomial ring. the
# variable ordering is taken from the polynomial ring
function System(I::Generic.Ideal)
eqns = Expression.(gens(I))
variables = Variable.(symbols(base_ring(I)))
System(eqns, variables = variables)
end
2024-02-10 22:39:26 +00:00
# --- sampling ---
2024-02-16 00:00:46 +00:00
function real_samples(F::AbstractSystem, dim; rng = default_rng())
2024-02-10 22:39:26 +00:00
# choose a random real hyperplane of codimension `dim` by intersecting
# hyperplanes whose normal vectors are uniformly distributed over the unit
# sphere
# [to do] guard against the unlikely event that one of the normals is zero
normals = transpose(hcat(
2024-02-16 00:00:46 +00:00
(normalize(randn(rng, nvariables(F))) for _ in 1:dim)...
2024-02-10 22:39:26 +00:00
))
cut = LinearSubspace(normals, fill(0., dim))
2024-02-16 00:00:46 +00:00
filter(isreal, results(witness_set(F, cut, seed = 0x1974abba)))
2024-02-10 22:39:26 +00:00
end
2024-02-13 01:34:12 +00:00
AbstractAlgebra.evaluate(pt::Point, vals::Vector{<:RingElement}) =
GLMakie.Point3f([evaluate(u, vals) for u in pt.coords])
function AbstractAlgebra.evaluate(sph::Sphere, vals::Vector{<:RingElement})
radius = 1 / evaluate(sph.coords[1], vals)
center = radius * [evaluate(u, vals) for u in sph.coords[3:end]]
GLMakie.Sphere(GLMakie.Point3f(center), radius)
end
end