polygo

package module
Version: v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2022 License: MIT Imports: 8 Imported by: 0

README

polygo

polygo is a fast, light-weight library of tools that make working with polynomials easy in Go.

What's New

  • A graphing tool

What's to Come

  • More polynomial tools (integration, critial point finding, binomial expansion, etc.)

Installation

go get -u github.com/SeanJxie/polygo

Documentation

You can find the full list of functions through godoc: https://pkg.go.dev/github.com/seanjxie/polygo

Examples

  • Graph functions:
package main

import (
	"fmt"
	"github.com/SeanJxie/polygo"
)

func main() {
	p1, _ := polygo.NewRealPolynomial([]float64{0, -2, 0, 1})
	p2, _ := polygo.NewRealPolynomial([]float64{-5, -2, 5, 1})

	graphOptions := polygo.GraphOptions{
		ShowIntersections:      true,
		ShowAxis:               true,
		ShowAxisLabels:         true,
		ShowIntersectionLabels: true,
		ShowRootLabels:         true,
		ShowRoots:              true,
		ShowYintercepts:        true,
		ShowGrid:               true,
	}

	graph, _ := polygo.NewGraph([]*polygo.RealPolynomial{p2, p1}, polygo.Point{X: 0, Y: 0}, 1000, 1000, 5, 5, 0.01, 1.0, &graphOptions)
	graph.SaveAsPNG("graph1.png")
}

Output: graph1.png

  • Create a simple quadratic and find the root:
package main

import (
	"fmt"
	"github.com/SeanJxie/polygo"
)

func main() {
	quadCoefficients := []float64{0, 0, 2}
	quad, _ := polygo.NewRealPolynomial(quadCoefficients)

	root, _ := quad.FindRootWithin(-1, 1)
	
	quad.PrintExpr()
	fmt.Printf("Root: %f\n", root) 
}

Output:

0.000000x^0 + 0.000000x^1 + 1.000000x^2
Root: 0.000000
  • Create a polynomial and find the derivative:
package main

import (
	"github.com/SeanJxie/polygo"
)

func main() {
	coeffs := []float64{5, 2, 5, 2, 63, 1, 2, 5, 1}
	poly, _ := polygo.NewRealPolynomial(coeffs)
	deriv := poly.Derivative()
	poly.PrintExpr()
	deriv.PrintExpr()
}

Output:

5.000000x^0 + 2.000000x^1 + 5.000000x^2 + 2.000000x^3 + 63.000000x^4 + 1.000000x^5 + 2.000000x^6 + 5.000000x^7 + 1.000000x^8
2.000000x^0 + 10.000000x^1 + 6.000000x^2 + 252.000000x^3 + 5.000000x^4 + 12.000000x^5 + 35.000000x^6 + 8.000000x^7
  • Find the intersection of two polynomials:
func main() {
	cubic, _ := polygo.NewRealPolynomial([]float64{0, -2, 0, 1})
	affine, _ := polygo.NewRealPolynomial([]float64{3, 5})
	cubic.PrintExpr()
	affine.PrintExpr()
	intersections, err := cubic.FindIntersectionsWithin(-10, 10, affine)

	if err != nil {
		fmt.Printf("error %v\n", err)
	} else {
		fmt.Printf("Intersections: %v\n", intersections) 
	}
}

Output:

Intersections: [[-2.397661540892259 -8.988307704461295] [-0.44080771150488296 0.7959614424755855] [2.8384692523971413 17.1923462619857]]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetNewtonIterations

func SetNewtonIterations(n int) error

SetNewtonIterations sets the number of iterations used in Newton's Method implmentation in root solving functions.

Types

type GraphOptions added in v1.1.0

type GraphOptions struct {
	ShowAxis          bool
	ShowGrid          bool
	ShowIntersections bool
	ShowRoots         bool
	ShowYintercepts   bool

	ShowAxisLabels         bool
	ShowIntersectionLabels bool
	ShowRootLabels         bool
	ShowYinterceptLabels   bool
}

GraphOptions specify some visual options that the RealPolynomialGraph can have.

The options are:

ShowAxis               // show the x = 0 and y = 0 axis lines.
ShowGrid               // show the grid.
ShowIntersections      // highlight the intersection points of all polynomials.
ShowRoots              // highlight the roots of all polynomials.
ShowYintercepts        // highlight the y-intercepts of all polynomials.
ShowAxisLabels         // label axis values.
ShowIntersectionLabsls // label intersection points.
ShowRootLabels         // label roots.
ShowYinterceptLabels   // label y-intercepts.

type Point added in v1.0.1

type Point struct {
	X, Y float64
}

A point in R^2.

func (Point) String added in v1.1.0

func (p Point) String() string

type RealPolynomial

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

A RealPolynomial is represented as a slice of coefficients ordered increasingly by degree. For example, one can imagine: 5x^0 + 4x^1 + (-2)x^2 + ...

func NewRealPolynomial

func NewRealPolynomial(coeffs []float64) (*RealPolynomial, error)

NewRealPolynomial returns a new *RealPolynomial instance with the given coeffs.

func (*RealPolynomial) Add

Add adds the current instance and rp2 and returns the sum. The current instance is also set to the sum.

func (*RealPolynomial) At

func (rp *RealPolynomial) At(x float64) float64

At returns the value of the current instance evaluated at x.

func (*RealPolynomial) CoeffAtDegree added in v1.0.1

func (rp *RealPolynomial) CoeffAtDegree(n int) float64

CoeffAtDegree returns the coefficient at degree n.

n should be positive.

func (*RealPolynomial) CountRootsWithin

func (rp *RealPolynomial) CountRootsWithin(a, b float64) int

CountRootsWithin returns the number of roots of the current instance on the closed interval [a, b]. If there are an infinite amount of roots, -1 is returned.

func (*RealPolynomial) Degree

func (rp *RealPolynomial) Degree() int

Degree returns the degree of the current instance.

func (*RealPolynomial) Derivative

func (rp *RealPolynomial) Derivative() *RealPolynomial

Derivative returns the derivative of the current instance. The current instance is not modified.

func (*RealPolynomial) Equal

func (rp1 *RealPolynomial) Equal(rp2 *RealPolynomial) bool

Equal returns true if the current instance is equal to rp2. Otherwise, false is returned.

func (*RealPolynomial) EuclideanDiv

func (rp1 *RealPolynomial) EuclideanDiv(rp2 *RealPolynomial) (*RealPolynomial, *RealPolynomial)

EuclideanDiv divides the current instance by rp2 and returns the result as a quotient-remainder pair. The current instance is also set to the quotient.

func (*RealPolynomial) FindIntersectionWithin

func (rp *RealPolynomial) FindIntersectionWithin(a, b float64, rp2 *RealPolynomial) (Point, error)

FindIntersectionWithin returns ANY intersection point of the current instance and rp2 existing on the closed interval [a, b]. If there are no intersections on the provided interval, an error is set.

func (*RealPolynomial) FindIntersectionsWithin

func (rp *RealPolynomial) FindIntersectionsWithin(a, b float64, rp2 *RealPolynomial) ([]Point, error)

FindIntersectionsWithin returns ALL intersection point of the current instance and rp2 existing on the closed interval [a, b]. Unlike FindIntersectionWithin, no error is set if there are no intersections on the provided interval. Instead, an empty slice is returned. If there are an infinite number or solutions, an error is set.

func (*RealPolynomial) FindRootWithin

func (rp *RealPolynomial) FindRootWithin(a, b float64) (float64, error)

FindRootWithin returns ANY root of the current instance existing on the closed interval [a, b]. If there are no roots on the provided interval, an error is set.

func (*RealPolynomial) FindRootsWithin

func (rp *RealPolynomial) FindRootsWithin(a, b float64) ([]float64, error)

FindRootsWithin returns ALL roots of the current instance existing on the closed interval [a, b]. Unlike FindRootWithin, no error is set if there are no solutions on the provided interval. Instead, an empty slice is returned. If there are an infinite number of solutions on [a, b], an error is set.

func (*RealPolynomial) IsDegree added in v1.0.1

func (rp *RealPolynomial) IsDegree(n int) bool

IsDegree returns true if current instance is of degree n. Otherwise, false is returned.

func (*RealPolynomial) IsZero

func (rp *RealPolynomial) IsZero() bool

IsZero returns true if current instance is equal to the zero polynomial. Otherwise, false is returned.

func (*RealPolynomial) LeadCoeff

func (rp *RealPolynomial) LeadCoeff() float64

LeadCoeff Returns the coefficient of the highest degree term of the current instance.

func (*RealPolynomial) Mul

Mul multiplies the current instance with rp2 and returns the product. The current instance is also set to the product.

func (*RealPolynomial) MulNaive

func (rp1 *RealPolynomial) MulNaive(rp2 *RealPolynomial) *RealPolynomial

MulNaive multiplies the current instance with rp2 and returns the product. The current instance is also set to the product.

It is not recommended to use this function. Use Mul instead.

func (*RealPolynomial) MulS

func (rp *RealPolynomial) MulS(s float64) *RealPolynomial

MulS multiplies the current instance with the scalar s and returns the product. The current instance is also set to the product.

func (*RealPolynomial) NumCoeffs

func (rp *RealPolynomial) NumCoeffs() int

NumCoeffs returns the number of coefficients of the current instance.

func (*RealPolynomial) Print added in v1.1.0

func (rp *RealPolynomial) Print()

PrintExpr prints the string expression of the current instance in increasing sum form to standard output.

func (*RealPolynomial) ShiftRight

func (rp *RealPolynomial) ShiftRight(offset int) *RealPolynomial

ShiftRight shifts the coefficients of each term in the current instance rightwards by offset and returns the resulting polynomial. The current instance is not modified. A right shift by N is equivalent to multipliying the current instance by x^N.

func (*RealPolynomial) String added in v1.1.0

func (rp *RealPolynomial) String() string

Expr returns a string representation of the current instance in increasing sum form.

func (*RealPolynomial) Sub

Sub subtracts rp2 from the current instance and returns the difference. The current instance is also set to the difference.

type RealPolynomialGraph added in v1.1.0

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

A RealPolynomialGraph represents the graph of a set of polynomials.

func NewGraph added in v1.1.0

func NewGraph(elements []*RealPolynomial, center Point, xResolution, yResolution int, viewX, viewY, xRenderStep, gridStep float64, options *GraphOptions) (*RealPolynomialGraph, error)

NewGraph returns a new *RealPolynomialGraph instance with the provided settings.

If any settings are invalid, an appropriate error is set.

The settings are:

center      // the point at which the graph is centered.
xResolution // the width of the graph in pixels.
yResolution // the height of the graph in pixels.
viewX       // the width of the viewing area. For example, a viewX of 1.0 will provide a graph spanning the horizontally closed interval [center.X - 1.0, center.X + 1.0].
viewY       // the height of the viewing area. For example, a viewY of 1.0 will provide a graph spanning the vertically closed interval [center.Y - 1.0, center.Y + 1.0].
xRenderStep // the detail the polynomial curves are rendered at. The closer this positive value is to 0.0, the more precise the curves will be (recommended to be 0.01).
gridStep    // the gap between consecutive axis lines.
options     // a *GraphOptions instance.

func (*RealPolynomialGraph) SaveAsPNG added in v1.1.0

func (g *RealPolynomialGraph) SaveAsPNG(path string) error

SaveAsPNG renders and saves the current instance as a PNG image file to the provided path.

If any rendering errors occur, an error addressing the problem is set.

Jump to

Keyboard shortcuts

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