Skip to content

Empowering Go developers with numerical integration, equation solving, and differentiation tools. Perform complex mathematical tasks effortlessly.

License

Notifications You must be signed in to change notification settings

rocas777/kairos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Automation Badge

Kairos - numerical calculus

The Kairos project is a versatile library developed in Go that provides comprehensive tools for numerical computations in the domains of integration, equation solving, and differentiation. Each subpackage within Kairos is designed to offer specialized functionality, allowing users to perform mathematical operations with ease and precision.

The key features of Kairos:

  • Differentiation
  • Integration
  • Equation Solver

Index

  1. Kairos - Numerical Calculus
    1. Getting started
  2. Kairos: Differentiation Package
    1. Simple Derivative
      1. Local Derivative
      2. Range Derivative
    2. Symmetric Derivative
      1. Local Derivative
      2. Range Derivative
    3. Higher Order Derivative
      1. Local Derivative
      2. Range Derivative
  3. Kairos: Equation Solver Package
    1. Bisection
    2. False Position (Regula False)
    3. NewtonRaphson
    4. Secant
  4. Kairos: Integration Package
    1. Trapezoidal Rule
      1. Definite Integral
      2. Anti-Derivative
    2. Simpson's 1/3 Rule
      1. Definite Integral
      2. Anti-Derivative
    3. Simpson's 3/8 Rule
      1. Definite Integral
      2. Anti-Derivative
    4. Adaptative Simpson Rule
      1. Definite Integral
      2. Anti-Derivative
  5. Documentation Reference

Getting started

Getting Kairos

With Go module support, simply add the following import

import "github.com/rocas777/kairos"

to your code, and then go [build|run|test] will automatically fetch the necessary dependencies.

Otherwise, run the following Go command to install the kairos package:

$ go get -u github.com/rocas777/kairos

Kairos: Differentiation Package

The differentiation package in the Kairos library equips Go developers with utilities for calculating derivatives of functions. Whether you need a first-order derivative or an arbitrary nth-order derivative, Kairos has you covered.

Overview

  • First Order Derivatives:

  • Arbitrary Order Derivatives:

    • HigherOrder Method: Utilizes the symmetric algorithm recursively to calculate nth-order derivatives.

Users can choose the method that best suits their accuracy and efficiency requirements.

Simple Derivative

The Simple struct provides methods for calculating the first derivative based on the regular definition. It uses the limit concept to approximate infinitesimals with 'H'. The derivative is computed as the slope of the function between points 'x' and 'x + H'.

Local derivative

package main

import (
	"fmt"
	"github.com/rocas777/kairos/differentiation"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Simple instance with default H value (0.1)
	simple := differentiation.NewSimple(0.1)

	// Calculate the first order derivative at the point x = 2
	result := simple.LocalDerivative(f, 2)
	fmt.Println("First Derivative at x = 2:", result)
}

Range Derivative

package main

import (
	"fmt"
	"github.com/rocas777/kairos/differentiation"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Simple instance with default H value (0.1)
	simple := differentiation.NewSimple(0.1)

	// Calculate the first order derivative over the range [0, 2] with 5 samples
	rangeDerivative := simple.RangeDerivative(f, 0, 2, 5)
	fmt.Println("First Derivative over the Range [0, 2]:", rangeDerivative)
}

Symmetric Derivative

The Symmetric struct provides methods for calculating the first derivative based on the symmetric definition. It uses 'H' as an approximation of infinitesimals and computes the derivative as the slope between points 'x - H' and 'x + H'.

Local derivative

Usage

package main

import (
	"fmt"
	"github.com/rocas777/kairos/differentiation"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Symmetric instance with default H value (0.1)
	symmetric := differentiation.NewSymmetric(0.1)

	// Calculate the first order derivative at the point x = 2
	result := symmetric.LocalDerivative(f, 2)
	fmt.Println("First Derivative at x = 2:", result)
}

Range Derivative

Usage

package main

import (
	"fmt"
	"github.com/rocas777/kairos/differentiation"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Symmetric instance with default H value (0.1)
	symmetric := differentiation.NewSymmetric(0.1)

	// Calculate the first order derivative over the range [0, 2] with 5 samples
	rangeDerivative := symmetric.RangeDerivative(f, 0, 2, 5)
	fmt.Println("First Derivative over the Range [0, 2]:", rangeDerivative)
}

Higher Order Derivative

The HigherOrder struct contains methods for calculating nth-order derivatives. It utilizes the symmetric algorithm recursively to achieve higher-order derivatives.

Local derivative

package main

import (
	"fmt"
	"github.com/rocas777/kairos/differentiation"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new HigherOrder instance with default H value (0.1) and Order (2)
	higherOrder := differentiation.NewHigherOrder(0.1, 2)

	// Calculate the first order derivative at the point x = 2
	result := higherOrder.LocalDerivative(f, 2)
	fmt.Println("First Derivative at x = 2:", result)
}

Range Derivative

package main

import (
	"fmt"
	"github.com/rocas777/kairos/differentiation"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new HigherOrder instance with default H value (0.1) and Order (2)
	higherOrder := differentiation.NewHigherOrder(0.1, 2)

	// Calculate the first order derivative over the range [0, 2] with 5 samples
	rangeDerivative := higherOrder.RangeDerivative(f, 0, 2, 5)
	fmt.Println("First Derivative over the Range [0, 2]:", rangeDerivative)
}

Kairos: Equation Solver Package

The equation package in the Kairos library provides utilities for solving equations and finding the roots of functions. It offers multiple root-finding methods, including the Bisection method, False Position method, Newton-Raphson method, and Secant method. Users can choose the most suitable method for their specific functions and interval constraints to efficiently locate zeros of the given function.

Overview

Note: These methods assume the provided function is continuous on the considered interval.

Bisection

The Bisection struct provides a method to find the zero of a function using the Bisection method on an interval [a, b]. The method can be limited by CycleLimit, which restricts the number of cycles to prevent the algorithm from running indefinitely. A solution is considered definitive once the difference of the interval [a, b] is below Epsilon.

Usage

package main

import (
	"fmt"
	"github.com/rocas777/kairos/equation"
)

func main() {
	// Example function: f(x) = x^2 - 4
	f := func(x float64) float64 {
		return x*x - 4
	}

	// Create a new Bisection instance with default Epsilon (0.01) and CycleLimit (100)
	bisection := equation.NewBisection(0.01, 100)

	// Find the zero of the function on the interval [1, 3]
	result := bisection.Zero(f, 1, 3)
	fmt.Println("Zero of the function:", result)
}

FalsePosition

The FalsePosition struct provides a method to find the zero of a function using the False Position method on an interval [a, b]. The method iteratively refines the estimate of the zero based on linear interpolation.

Usage

package main

import (
	"fmt"
	"github.com/rocas777/kairos/equation"
)

func main() {
	// Example function: f(x) = x^2 - 4
	f := func(x float64) float64 {
		return x*x - 4
	}

	// Create a new FalsePosition instance with default Epsilon (0.01) and CycleLimit (100)
	falsePosition := equation.NewFalsePosition(0.01, 100)

	// Find the zero of the function on the interval [1, 3]
	result := falsePosition.Zero(f, 1, 3)
	fmt.Println("Zero of the function:", result)
}

NewtonRaphson

The NewtonRaphson struct provides a method to find the zero of a function using the Newton-Raphson method. The method iteratively refines the estimate of the zero based on the function's local behavior.

Usage

package main

import (
	"fmt"
	"github.com/rocas777/kairos/equation"
)

func main() {
	// Example function: f(x) = x^2 - 4
	f := func(x float64) float64 {
		return x*x - 4
	}

	// Example derivative function: f'(x) = 2*x
	dxF := func(x float64) float64 {
		return 2 * x
	}

	// Create a new NewtonRaphson instance with default Epsilon (0.01) and CycleLimit (100)
	newtonRaphson := equation.NewNewtonRaphson(0.01, 100)

	// Find the zero of the function using the derivative on the initial estimate 3
	result := newtonRaphson.Zero(f, dxF, 3)
	fmt.Println("Zero of the function:", result)
}

Secant

The Secant struct provides a method to find the zero of a function using the Secant method. The method iteratively refines the estimate of the zero based on a secant line between two points.

Usage

package main

import (
	"fmt"
	"github.com/rocas777/kairos/equation"
)

func main() {
	// Example function: f(x) = x^2 - 4
	f := func(x float64) float64 {
		return x*x - 4
	}

	// Create a new Secant instance with default Epsilon (0.01) and CycleLimit (100)
	secant := equation.NewSecant(0.01, 100)

	// Find the zero of the function on the interval [1, 3]
	result := secant.Zero(f, 1, 3)
	fmt.Println("Zero of the function:", result)
}

Kairos: Integration Package

The integration package in the Kairos library provides utilities for numerical integration of functions. It includes several methods for calculating definite integrals, such as the Trapezoidal Rule, Simpson's 1/3 Rule, Simpson's 3/8 Rule, and adaptive Simpson integration. Users can choose the appropriate method based on the precision and efficiency requirements of their mathematical analysis.

Overview

Trapezoid Rule

The Trapezoid struct provides a method to calculate the definite integral of a given function using the Trapezoidal Rule. This rule approximates the integral by dividing the interval into N trapezoids and summing their areas. The higher the value of N, the more accurate the integration will be; however, this will lead to a more time-expensive method.

Definite Integral

package main

import (
	"fmt"
	"github.com/rocas777/kairos/integration"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Trapezoid instance with default N (10)
	trapezoid := integration.NewTrapezoid(10)

	// Calculate the definite integral of the function over the interval [0, 1]
	result := trapezoid.DefiniteIntegral(f, 0, 1)
	fmt.Println("Definite Integral:", result)
}

Anti-derivative

package main

import (
	"fmt"
	"github.com/rocas777/kairos/integration"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Trapezoid instance with default N (10)
	trapezoid := integration.NewTrapezoid(10)

	// Calculate the approximate antiderivative of the function over the interval [0, 1] with 5 samples
	result := trapezoid.AntiDerivative(f, 0, 1, 5)
	fmt.Println("Approximate Anti-Derivative:", result)
}

Simpson 1/3 Rule

The Simpson_1_3 struct provides a method to calculate the definite integral of a given function using the Simpson algorithm, specifically the 1/3 composite rule. This works by dividing the interval into N pieces and making a polynomial interpolation between two successive points to calculate the area below the interpolation. The higher the value of N, the more accurate the integration will be; however, this will lead to a more time-expensive method.

Definite Integral

package main

import (
	"fmt"
	"github.com/rocas777/kairos/integration"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Simpson_1_3 instance with default N (2)
	simpson13 := integration.NewSimpson_1_3(2)

	// Calculate the definite integral of the function over the interval [0, 1]
	result := simpson13.DefiniteIntegral(f, 0, 1)
	fmt.Println("Definite Integral:", result)
}

Anti-derivative

package main

import (
	"fmt"
	"github.com/rocas777/kairos/integration"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Simpson_1_3 instance with default N (2)
	simpson13 := integration.NewSimpson_1_3(2)

	// Calculate the approximate antiderivative of the function over the interval [0, 1] with 5 samples
	result := simpson13.AntiDerivative(f, 0, 1, 5)
	fmt.Println("Approximate Anti-Derivative:", result)
}

Simpson 3/8 Rule

The Simpson_3_8 struct provides a method to calculate the definite integral of a given function using the Simpson algorithm, specifically the 3/8 composite rule. This works by dividing the interval into N pieces and making a polynomial interpolation using three successive points to calculate the area below the interpolation. The higher the value of N, the more accurate the integration will be; however, this will lead to a more time-expensive method.

Definite Integral

package main

import (
	"fmt"
	"github.com/rocas777/kairos/integration"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Simpson_3_8 instance with default N (3)
	simpson38 := integration.NewSimpson_3_8(3)

	// Calculate the definite integral of the function over the interval [0, 1]
	result := simpson38.DefiniteIntegral(f, 0, 1)
	fmt.Println("Definite Integral:", result)
}

Anti-derivative

package main

import (
	"fmt"
	"github.com/rocas777/kairos/integration"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new Simpson_3_8 instance with default N (3)
	simpson38 := integration.NewSimpson_3_8(3)

	// Calculate the approximate antiderivative of the function over the interval [0, 1] with 5 samples
	result := simpson38.AntiDerivative(f, 0, 1, 5)
	fmt.Println("Approximate Anti-Derivative:", result)
}

Adaptive Simpson Integration

The SimpsonAdaptive struct provides a method to calculate the definite integral of a given function using adaptive Simpson integration. It combines the simplicity of the Simpson 1/3 Rule with adaptivity to improve accuracy. The algorithm automatically adjusts the number of intervals based on the function's behavior, comparing the results by dividing intervals into more subintervals using the epsilon criterion.

Definite Integral

package main

import (
	"fmt"
	"github.com/rocas777/kairos/integration"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new SimpsonAdaptive instance with default Epsilon (0.1)
	simpsonAdaptive := integration.NewSimpsonAdaptive(0.1)

	// Calculate the definite integral of the function over the interval [0, 1]
	result := simpsonAdaptive.DefiniteIntegral(f, 0, 1)
	fmt.Println("Definite Integral:", result)
}

Anti-derivative

package main

import (
	"fmt"
	"github.com/rocas777/kairos/integration"
)

func main() {
	// Example function: f(x) = x^2
	f := func(x float64) float64 {
		return x * x
	}

	// Create a new SimpsonAdaptive instance with default Epsilon (0.1)
	simpsonAdaptive := integration.NewSimpsonAdaptive(0.1)

	// Calculate the approximate antiderivative of the function over the interval [0, 1] with 5 samples
	result := simpsonAdaptive.AntiDerivative(f, 0, 1, 5)
	fmt.Println("Approximate Anti-Derivative:", result)
}

Documentation Reference

For detailed documentation and examples, please refer to the official documentation on pkg.go.dev.