github.com/livekit/protocol@v1.39.3/utils/math.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package utils
    16  
    17  import (
    18  	"math"
    19  
    20  	"golang.org/x/exp/constraints"
    21  )
    22  
    23  func LogisticFunc(x0, L, k float64) func(x float64) float64 {
    24  	return func(x float64) float64 {
    25  		return L / (1 + math.Exp(-k*(x-x0)))
    26  	}
    27  }
    28  
    29  func FastLogisticFunc(x0, L, k float64) func(x float64) float64 {
    30  	return func(x float64) float64 {
    31  		return L / 2 * (1 + k*(x-x0)/(1+math.Abs(k*(x-x0))))
    32  	}
    33  }
    34  
    35  func Abs[T constraints.Signed | constraints.Float](v T) T {
    36  	if v < 0 {
    37  		return -v
    38  	}
    39  	return v
    40  }