polygo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2021 License: GPL-3.0 Imports: 4 Imported by: 0

README

PolyGo

A collection of tools that make working with polynomials easier in Go.

What's New

  • Fast polynomial multiplication through the Fast Fourier Transform.
  • Better documentation.

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

  • 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

Overview

Package polygo is a collection of tools that make working with polynomials easier in Go.

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 RealPolynomial

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

A real 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) 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) Expr

func (rp *RealPolynomial) Expr() string

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

func (*RealPolynomial) FindIntersectionWithin

func (rp *RealPolynomial) FindIntersectionWithin(a, b float64, rp2 *RealPolynomial) ([2]float64, error)

FindIntersectionWithin returns ANY intersection point (as a two-element slice) 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) ([][2]float64, error)

FindIntersectionsWithin returns ALL intersection point (as a two-element slice) 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) 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) PrintExpr

func (rp *RealPolynomial) PrintExpr()

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) Sub

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

Jump to

Keyboard shortcuts

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