spatial/r3: add Divergence

This commit is contained in:
soypat
2022-07-26 01:27:49 -03:00
committed by Dan Kortschak
parent c4e2c8cf3e
commit 4ee7932b6c
2 changed files with 62 additions and 0 deletions

View File

@@ -83,6 +83,18 @@ func Cos(p, q Vec) float64 {
return Dot(p, q) / (Norm(p) * Norm(q))
}
// Divergence returns the divergence of the vector field at the point p,
// approximated using finite differences with the given step sizes.
func Divergence(p, step Vec, field func(Vec) Vec) float64 {
sx := Vec{X: step.X}
divx := (field(Add(p, sx)).X - field(Sub(p, sx)).X) / step.X
sy := Vec{Y: step.Y}
divy := (field(Add(p, sy)).Y - field(Sub(p, sy)).Y) / step.Y
sz := Vec{Z: step.Z}
divz := (field(Add(p, sz)).Z - field(Sub(p, sz)).Z) / step.Z
return 0.5 * (divx + divy + divz)
}
// minElem return a vector with the minimum components of two vectors.
func minElem(a, b Vec) Vec {
return Vec{