spatial/r3: add Gradient

This commit is contained in:
soypat
2022-07-30 19:06:11 -03:00
committed by Dan Kortschak
parent e5b200df74
commit 1fbf8fb724
3 changed files with 45 additions and 2 deletions

View File

@@ -97,6 +97,19 @@ func Divergence(p, step Vec, field func(Vec) Vec) float64 {
return 0.5 * (divx + divy + divz)
}
// Gradient returns the gradient of the scalar field at the point p,
// approximated using finite differences with the given step sizes.
func Gradient(p, step Vec, field func(Vec) float64) Vec {
dx := Vec{X: step.X}
dy := Vec{Y: step.Y}
dz := Vec{Z: step.Z}
return Vec{
X: field(Add(p, dx)) - field(Sub(p, dx)),
Y: field(Add(p, dy)) - field(Sub(p, dy)),
Z: field(Add(p, dz)) - field(Sub(p, dz)),
}
}
// minElem return a vector with the minimum components of two vectors.
func minElem(a, b Vec) Vec {
return Vec{