calculus

package module
v0.0.0-...-70fc99d Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 6 Imported by: 3

README

Go Calculus

A golang library supporting definite integration and differentiation of real valued, single variable, elementary and non-elementary functions.

Usage

~$ go get github.com/TheDemx27/calculus

package main

import (
  "fmt"
  "math"
  clc "github.com/rpopplewell/calculus"
)

func main() {
  // Create a new function.
  f := clc.NewFunc("1/(x^2+1)")

  // Integrate from 0 to 20.
  fmt.Println(f.AntiDiff(0, 20))

  // Differentiate at point Pi/2.
  fmt.Println(f.Diff(math.Pi/2))
}

Alternatively, you can define any function that takes and returns a float64, (i.e. a function of the form type usrFunction func(float64) float64), and then perform calculus on it. This allows you to do calculus on practically any mapping.

func fn(x float64) float64 {
  return 2*math.Floor(x)
}

func main() {
  clc.AntiDiff(fn, 0, 10)
  clc.Diff(fn, 0.5)
}

In all cases, adding an additional int argument will specify how many samples you want to use. You can also evaluate the function at a certain point using f.Eval(), and return the expression defining the function using f.Name(), e.g.

  fmt.Printf("The value of %s when x = 20 is %g\n", f.Name(), f.Eval(20))

TODO

* Gaussian Quadrature ✓ * Implement Risch Algorithm * Implement Symbolic Differentiation Patterns

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AntiDiff

func AntiDiff(f cFunc, a, b float64, i ...int) float64

* Implementation of AntiDiff()

func CountElements

func CountElements(ops string, elements []string) int

* Counts the number of elements, e.g. 2*x+345 -> [2,*,x,+,345] -> 5

func Diff

func Diff(fn cFunc, point float64, usrPrec ...int) float64

* Implementation of Diff()

func GlqNodes

func GlqNodes(n int, f cFunc) (node []float64, weight []float64)

func IsElement

func IsElement(ops string, elements []string, i int) bool

* Determines if a given character is the last character in an element.

func LegendrePoly

func LegendrePoly(n int) []cFunc

func NewtonRaphson

func NewtonRaphson(f, df cFunc, x0 float64) float64

func SetPrec

func SetPrec(defaultPrec int, usrPrec []int) int

* Determines how many samples to use

Types

type Function

type Function struct {
	F    string
	Toks [][]string
}

func NewFunc

func NewFunc(F string) Function

func (Function) AntiDiff

func (fnc Function) AntiDiff(lower float64, upper float64, usrPrec ...int) float64

* Wrappers for Diff and AntiDiff for evaluating the function inputted as a string with the Function struct

func (Function) Diff

func (fnc Function) Diff(point float64, usrPrec ...int) float64

func (Function) Eval

func (fnc Function) Eval(val float64) float64

* Evaluates a function string e.g. 2*x at a given 'val' for x.

func (Function) GetFunc

func (fnc Function) GetFunc() string

func (Function) GetToksArray

func (fnc Function) GetToksArray(j int) []string

func (Function) GetToksArrayAbstract

func (fnc Function) GetToksArrayAbstract() []string

func (Function) GetToksArrayLit

func (fnc Function) GetToksArrayLit() []string

func (Function) GetToksString

func (fnc Function) GetToksString(j int) string

func (Function) GetToksStringAbstract

func (fnc Function) GetToksStringAbstract() string

func (Function) GetToksStringLit

func (fnc Function) GetToksStringLit() string

func (Function) ParseChars

func (fnc Function) ParseChars() [][]string

* Returns toks. * i => tok index in function * [i][0] => constant/variable * [i][1] => value

func (Function) ParseToks

func (fnc Function) ParseToks() []string

func (*Function) SetFunc

func (fnc *Function) SetFunc(name string)

func (Function) SymDiff

func (fnc Function) SymDiff() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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