mmaths

package module
v0.0.0-...-1eefd4b Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 10 Imported by: 30

README

go mmaths

Just a set of math routines that I wrote to supplement go's math package.

updates

since go v1.10

Function Round has been changed to RoundTo as the go math package now includes a Round functon

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Binomial

func Binomial(n, k int) int

Binomial returns the binomial coefficient of (n,k), also commonly referred to as "n choose k".

The binomial coefficient, C(n,k), is the number of unordered combinations of k elements in a set that is n elements big, and is defined as

C(n,k) = n!/((n-k)!k!)

n and k must be non-negative with n >= k, otherwise Binomial will panic. No check is made for overflow.

func BytePow

func BytePow(p int) byte

BytePow returns integer i to the power p

func CCWEtoCWN

func CCWEtoCWN(ccwe float64) float64

CCWEtoCWN converts trig angles (counter-clockwise east) to clockwise north from https://math.stackexchange.com/questions/1589793/a-formula-to-convert-a-counter-clockwise-angle-to-clockwise-angle-with-an-offset: (−θ+90°) mod 360°: The negative on θ deals with the fact that we are changing from counterclockwise to clockwise. The +90° deals with the offset of ninety degrees. And lastly we need to mod by 360° to keep our angle in the desired range [0°,360°]

func Dateday

func Dateday(t time.Time) time.Time

func GradientDescent

func GradientDescent(A Matrix, B []float64, n int, alpha float64) []float64

GradientDescent is a first-order iterative optimization algorithm for finding the minimum of a function. used to solve for x in Ax=B from: https://stackoverflow.com/questions/16422287/linear-regression-library-for-go-language

func IntPow

func IntPow(i, p int) int

IntPow returns integer i to the power p

func LinearRegression

func LinearRegression(cs [][]float64) (m, b, r2 float64)

func LinearTransform

func LinearTransform(l, h, u float64) float64

LinearTransform linearly transforms U[0.0,1.0] to [l,h] space

func LogLinearTransform

func LogLinearTransform(l, h, u float64) float64

LogLinearTransform linearly transforms U[0.0,1.0] to 10^[l,h] space

func PnPoly

func PnPoly(v [][]float64, p []float64) bool

PnPoly determins whether a point lies within a polygon

func PnPolyC

func PnPolyC(v []complex128, p complex128, tolerance float64) bool

PnPolyC determins whether a point lies within a polygon (using complex coordinates)

func PnPolyLong

func PnPolyLong(v [][]float64, p []float64, tol float64) bool

PnPolyLong determins whether a point lies within a polygon with more rigor: PnPoly requires the point to be completely within prism

func Primes

func Primes(k int) []int

Primes : brute-force finds the first k prime numbers

func QsortIndx

func QsortIndx(a QsortIndxInterface, prng *rand.Rand) []int

QsortIndx same as above, but preserves original slice index modified from: https://stackoverflow.com/questions/23276417/golang-custom-sort-is-faster-than-native-sort#23278451

func RelativeDifference

func RelativeDifference(f0, f1 float64) float64

RelativeDifference returns the relative difference between to values

func RoundRange

func RoundRange(minVal, maxVal float64) (float64, float64)

RoundRange determines the likely range that encompasses the given values

func RoundTo

func RoundTo(f float64, places int) float64

RoundTo previously "Round" but now included in math after Go v1.10

func RoundToValue

func RoundToValue(f, roundTo float64, roundUpDwn int) float64

RoundToValue rounds to the nearest multiple of the given roundTo value

func SignificantFigure

func SignificantFigure(f float64, sigFig int) float64

SignificantFigure converts the number to n significant figures ex: 1234567 to 1230000 at significant 3 figures

func SortMapFloat

func SortMapFloat(m map[int]float64, reverse bool) ([]int, []float64)

SortMapFloat returns the key-values sorted by value

func SortMapInt

func SortMapInt(m map[int]int) ([]int, []int)

SortMapInt returns an IndexedSlice sorted by value

func ThomasBoundaryCondition

func ThomasBoundaryCondition(a, b, c, d, x map[int]float64, first, last int)

ThomasBoundaryCondition is a Thomas algorithm solution which solves a tri-diagonal system of equations converted from python code in: Bittelli, M., Campbell, G.S., and Tomei, F., 2015. Soil Physics with Python. Oxford University Press.

func UniqueInts

func UniqueInts(input []int) []int

UniqueInts returns a unique subset of the int slice provided. from: https://kylewbanks.com/blog/creating-unique-slices-in-go

func Wateryear

func Wateryear(dt time.Time) int

Types

type BinaryNode

type BinaryNode struct {
	Left  *BinaryNode
	Right *BinaryNode
	Val   float64
	Indx  int
}

func (*BinaryNode) AddNode

func (node *BinaryNode) AddNode(is *IndexedSlice, first, last int)

func (*BinaryNode) IndexOf

func (n *BinaryNode) IndexOf(val float64) int

IndexOf -1: place at start; n: place at end; i: place after i, before i+1

func (*BinaryNode) Search

func (n *BinaryNode) Search(val *float64, sid *int)

type Circle

type Circle struct {
	Centroid Point
	Radius   float64
}

func (*Circle) Contains

func (c *Circle) Contains(p []float64) bool

type Extent

type Extent struct{ Xn, Xx, Yn, Yx float64 }

Extent is a 2D square spatial extent

func (*Extent) Contains

func (ex *Extent) Contains(p Point) bool

func (*Extent) New

func (ex *Extent) New(s [][]float64)

func (*Extent) Radius

func (ex *Extent) Radius() float64

type IndexedSlice

type IndexedSlice struct {
	Indx []int
	Val  []float64
}

IndexedSlice alias to float array being sorted and interfaces to Go-native sort.Sort

func (IndexedSlice) Indices

func (is IndexedSlice) Indices() []int

Indices returns the index property

func (IndexedSlice) Len

func (is IndexedSlice) Len() int

func (IndexedSlice) Less

func (is IndexedSlice) Less(i, j int) bool

func (*IndexedSlice) New

func (is *IndexedSlice) New(v []float64)

New IndexSlice constructor, default indices

func (IndexedSlice) Partition

func (is IndexedSlice) Partition(i int) (left QsortIndxInterface, right QsortIndxInterface)

Partition splits index array around pivot

func (IndexedSlice) Swap

func (is IndexedSlice) Swap(i, j int)

type IntSlice

type IntSlice []int

IntSlice : alias to index array being sorted

func (IntSlice) Len

func (is IntSlice) Len() int

func (IntSlice) Less

func (is IntSlice) Less(i, j int) bool

func (IntSlice) Partition

func (is IntSlice) Partition(i int) (left QsortInterface, right QsortInterface)

Partition : splits index array around pivot

func (IntSlice) Swap

func (is IntSlice) Swap(i, j int)

type LineSegment

type LineSegment struct {
	P0, P1 Point
	// contains filtered or unexported fields
}

LineSegment represents a stright line between two points

func (*LineSegment) Build

func (ls *LineSegment) Build()

func (*LineSegment) Intersection2D

func (l0 *LineSegment) Intersection2D(l1 *LineSegment) (Point, float64)

Intersection2D returns the 2D intersection of two line segments. Returns nil if lines do not intersect.

func (*LineSegment) IntersectionX

func (ls *LineSegment) IntersectionX(x float64) *Point

func (*LineSegment) IntersectionY

func (ls *LineSegment) IntersectionY(y float64) *Point

func (*LineSegment) Intersects

func (ls *LineSegment) Intersects(p Point, toWithin float64) bool

type Matrix

type Matrix [][]float64

Matrix alias for a 2d slice

func (Matrix) GaussJordanElimination

func (mt Matrix) GaussJordanElimination() Matrix

GaussJordanElimination is an algorithm for solving systems of linear equations. It is usually understood as a sequence of operations performed on the associated matrix of coefficients. This method can also be used to find the rank of a matrix, to calculate the determinant of a matrix, and to calculate the inverse of an invertible square matrix. from: http://www.sanfoundry.com/cpp-program-implement-gauss-jordan-elimination/

func (Matrix) Multiply

func (mt Matrix) Multiply(mp Matrix) Matrix

Multiply matrices

func (Matrix) Print

func (mt Matrix) Print()

Print matrix

func (Matrix) T

func (mt Matrix) T() Matrix

T transpose matrix

type Plane

type Plane struct {
	// contains filtered or unexported fields
}

Plane defined as: ax + by + cz + d = 0 normal vector is (-a,-b,1) slope = Sqrt(a ^ 2 + b ^ 2) aspect = Atan2(-b, -a) // Asin(-a / Sqrt(a ^ 2 + b ^ 2)) // theta = asin(-a/sqrt(a^2+b^2))

func NewPlane

func NewPlane(ps []Point) (*Plane, error)

type Point

type Point struct{ X, Y, Z, M float64 }

Point is a 4D coordinate struct

func (*Point) Distance

func (p *Point) Distance(p0 Point) float64

func (*Point) MidPoint

func (p *Point) MidPoint(p0 Point) Point

func (*Point) Rotate

func (p *Point) Rotate(angle float64, porig Point) Point

type Polyline

type Polyline struct {
	S [][]float64
}

func (*Polyline) Chainage

func (p *Polyline) Chainage() [][]float64

type PriorityQueue

type PriorityQueue struct {
	// contains filtered or unexported fields
}

PriorityQueue represents the queue

func NewPriorityQueue

func NewPriorityQueue() PriorityQueue

NewPriorityQueue initializes an empty priority queue.

func (*PriorityQueue) Len

func (p *PriorityQueue) Len() int

Len returns the number of elements in the queue.

func (*PriorityQueue) Pop

func (p *PriorityQueue) Pop() (interface{}, error)

Pop removes the element with the highest priority from the queue and returns it. In case of an empty queue, an error is returned.

func (*PriorityQueue) Push

func (p *PriorityQueue) Push(v interface{}, priority float64)

Push inserts a new element into the queue. No action is performed on duplicate elements.

func (*PriorityQueue) UpdatePriority

func (p *PriorityQueue) UpdatePriority(x interface{}, newPriority float64)

UpdatePriority changes the priority of a given item. If the specified item is not present in the queue, no action is performed.

type Prism

type Prism struct {
	Z           []complex128 // complex coordinates
	Top, Bot, A float64
}

Prism struct represents a singular model prism

func (*Prism) Centroid

func (q *Prism) Centroid() complex128

CentroidXY returns the coordinates of the prism centroid

func (*Prism) CentroidXY

func (q *Prism) CentroidXY() (x, y float64)

CentroidXY returns the coordinates of the prism centroid

func (*Prism) Contains

func (q *Prism) Contains(x, y, z float64) bool

Contains returns true if the given particle is contained by the prism bounds

func (*Prism) ContainsXY

func (q *Prism) ContainsXY(x, y float64) bool

ContainsXY returns true if the given (x,y) coordinates are contained by the prism planform bounds

func (*Prism) New

func (q *Prism) New(z []complex128, top, bot float64)

New prism constructor

type QsortIndxInterface

type QsortIndxInterface interface {
	sort.Interface
	// Partition returns slice[:i] and slice[i+1:]
	// These should references the original memory
	// since this does an in-place sort
	Partition(i int) (left QsortIndxInterface, right QsortIndxInterface)
	Indices() []int
}

QsortIndxInterface interface to sort.Sort

type QsortInterface

type QsortInterface interface {
	sort.Interface
	// Partition returns slice[:i] and slice[i+1:]
	// These should references the original memory
	// since this does an in-place sort
	Partition(i int) (left QsortInterface, right QsortInterface)
}

QsortInterface interface to sort.Sort

func Qsort

func Qsort(a QsortInterface, prng *rand.Rand) QsortInterface

Qsort a (quick) sorting algorithm that is apparently faster then Go's native sort.Sort from: https://stackoverflow.com/questions/23276417/golang-custom-sort-is-faster-than-native-sort#23278451

type Triangle

type Triangle struct{ P0, P1, P2 Point }

func (*Triangle) Area

func (t *Triangle) Area() float64

func (*Triangle) BarycentricWeights

func (t *Triangle) BarycentricWeights(x, y float64) []float64

func (*Triangle) Circumcircle

func (t *Triangle) Circumcircle() *Circle

func (*Triangle) Contains

func (t *Triangle) Contains(x, y float64) bool

func (*Triangle) EdgeLengths

func (t *Triangle) EdgeLengths() []float64

func (*Triangle) MinMaxInteriorAngle

func (t *Triangle) MinMaxInteriorAngle() (float64, float64)

func (*Triangle) New

func (t *Triangle) New(p0, p1, p2 []float64)

Directories

Path Synopsis
modified from https://stackoverflow.com/questions/27161533/find-the-shortest-distance-between-a-point-and-line-segments-not-line
modified from https://stackoverflow.com/questions/27161533/find-the-shortest-distance-between-a-point-and-line-segments-not-line

Jump to

Keyboard shortcuts

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