go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/mathutil/normalize.go (about) 1 /* 2 3 Copyright (c) 2024 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package mathutil 9 10 // Normalize takes a list of values and maps them 11 // onto the interval [0, 1.0]. 12 // 13 // It is important to take from the above that the output 14 // will always be on a positive interval regardless of the 15 // sign of the inputs. 16 func Normalize[T Operatable](values []T) []float64 { 17 min, max := MinMax(values) 18 19 // swap these around if we're dealing with a negative min & max 20 if min < 0 && max < 0 { 21 max, min = min, max 22 } 23 24 delta := float64(max) - float64(min) 25 output := make([]float64, 0, len(values)) 26 for _, v := range values { 27 outputValue := float64(v) 28 outputValue = outputValue - float64(min) 29 outputValue = outputValue / delta 30 output = append(output, outputValue) 31 } 32 return output 33 }