funcs

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2022 License: MIT Imports: 6 Imported by: 4

Documentation

Overview

Package funcs provides functionality to apply mathematical functions to DataFrames.

Index

Constants

This section is empty.

Variables

View Source
var ErrUndefined = errors.New("undefined")

ErrUndefined indicates that the PiecewiseFuncDefn's domain is not defined for a given row.

Functions

func Evaluate

func Evaluate(ctx context.Context, df *dataframe.DataFrame, fn PiecewiseFuncDefn, col interface{}, opts ...EvaluateOptions) error

Evaluate applies a PiecewiseFuncDefn to a particular Series in a DataFrame.

Example:

fn := funcs.RegFunc("sin((2*𝜋*x)/24)")
funcs.Evaluate(ctx, df, fn, 1)

Types

type EvaluateOptions

type EvaluateOptions struct {

	// CustomFns adds custom functions to be used within fn.
	//
	// Example:
	//
	//  CustomFns: map[string]func(args ...float64) float64{
	//     // Add sinc function: https://en.wikipedia.org/wiki/Sinc_function
	//     "sinc": func(args ...float64) float64 {
	//        if args[0] == 0 {
	//           return 1
	//        }
	//        return math.Sin(args[0]) / args[0]
	//     }
	//  }
	//
	CustomFns map[string]func(args ...float64) float64

	// CustomConstants adds custom constants to be used within fn.
	// NOTE: π, 𝜋, pi, Φ, phi, e, E are already provided unless over-ridden here.
	//
	// Example:
	//
	//  CustomConstants: map[string]float64{"ħ":  6.62607015E-34/(2*math.Pi)}
	CustomConstants map[string]float64

	// DontLock can be set to true if the DataFrame should not be locked.
	DontLock bool

	// Range is used to limit which rows the PiecewiseFuncDefn gets applied to.
	Range *dataframe.Range

	// NoConcurrency is not yet implemented.
	NoConcurrency bool
}

EvaluateOptions modifies the behavior of the Evaluate function.

type PiecewiseFuncDefn

type PiecewiseFuncDefn []SubFuncDefn

PiecewiseFuncDefn represents a piecewise function. A piecewise function is a function that is defined on a sequence of intervals.

See: https://mathworld.wolfram.com/PiecewiseFunction.html

Example:

fn := []funcs.SubFunc{
   {
      Fn:     "sin(x)+2*y",
      Domain: &[]dataframe.Range{dataframe.RangeFinite(0, 2)}[0],
   },
   {
      Fn:     "0",
      Domain: nil,
   },
}

type SubFuncDefn

type SubFuncDefn struct {

	// Fn is a string representing the function. Most functions from the math package that return a single float64 are supported.
	// The equivalent function name is all lower-cased. Therefore RoundToEven becomes roundtoeven. See https://golang.org/pkg/math/.
	// The variables used in Fn must correspond to the Series' names in the DataFrame. Custom functions and constants can be defined
	// and added using the options.
	//
	// Example: "sin(x)+2*y"
	//
	Fn string

	// Domain of Fn based on DataFrame's rows.
	Domain *dataframe.Range
}

SubFuncDefn represents a subset of the piecewise function. PiecewiseFuncDefn consists of potentially numerous SubFuncDefn's over its relevant domain. The domain is defined based on the DataFrame's rows.

func RegFunc

func RegFunc(fn string) []SubFuncDefn

RegFunc represents a regular function not split into different domain segments.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL