Add vector.normalize_zero

This commit is contained in:
Lars Mueller 2022-01-12 12:16:55 +01:00
parent 71b6d9769d
commit 66315d0219

@ -162,6 +162,19 @@ function normalize(v)
return divide_scalar(v, length(v)) return divide_scalar(v, length(v))
end end
function normalize_zero(v)
local len = length(v)
if len == 0 then
-- Return a zeroed vector with the same keys
local zeroed = {}
for k in pairs(v) do
zeroed[k] = 0
end
return new(zeroed)
end
return divide_scalar(v, len)
end
function floor(v) function floor(v)
return apply(v, math.floor) return apply(v, math.floor)
end end