linear

package module
v0.0.0-...-cc7d180 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2017 License: MIT Imports: 7 Imported by: 0

README

Linear Algebra Functions

Provides Vector operations, Equation (Line) operations and functions to work with Systems of equations to produce Triangular Form, Reduced Row-Echelon Form and carry out Gaussian elimination.

Written to complete the Udacity Linear Algebra course at https://www.udacity.com/course/linear-algebra-refresher-course--ud953

See cmd/main.go for how the quizes are completed.

Examples

result, err := linear.NewVector(8.218, -9.341).Add(linear.NewVector(-1.129, 2.111))
angle, err := linear.NewVector(3.183, -7.627).AngleBetween(linear.NewVector(-2.668, 5.319))
line1 := linear.NewEquation(linear.NewVector(7.204, 3.182), 8.68) // 7.204x + 3.182y = 8.68
line2 := linear.NewEquation(linear.NewVector(8.172, 4.114), 9.883)
intersection, intersects, equal, err := line1.IntersectionWith(line2)

Documentation

Index

Constants

View Source
const DefaultTolerance = 1e-10

DefaultTolerance is the tolerance to use for comparisons.

Variables

This section is empty.

Functions

This section is empty.

Types

type Equation

type Equation struct {
	NormalVector Vector
	ConstantTerm float64
}

Equation consists of a normal vector which specifies the direction, and a constant term. The basepoint is calculated as required.

func NewEquation

func NewEquation(normalVector Vector, constantTerm float64) Equation

NewEquation creates a new Equation based on the variable terms, e.g.: Ax + By = C For example, for a 2D vector (2, 3) and constant 5 would result in 2x + 3y = 5 The slope intercept (y = mx + b) form of that would be:

2x + 3y = 5
     3y = 5 - 2x
      y = 1/3(5-2x)

To get back to standard form: To find the y intercept (a point on the line), set x to zero.

2x + 3y = 5
     3y = 5 - 0
      y = 5/3
y intercept = (0, 5/3)

To find the x intercept, set y to zero.

2x + 3y = 5
2x - 0 = 5
2x = 5
x = 5/2
 x intercept = (5/2, 0)

func (Equation) CancelTerm

func (l1 Equation) CancelTerm(target Equation, termIndex int) (Equation, error)

CancelTerm cancels a term in the target line by determining the coefficient which links them and applying the first term to the second term to cancel them out.

func (Equation) Eq

func (l1 Equation) Eq(l2 Equation) (bool, error)

Eq determines if two lines are equal.

func (Equation) FirstNonZeroCoefficient

func (l1 Equation) FirstNonZeroCoefficient() (index int, value float64, ok bool)

FirstNonZeroCoefficient finds the first non-zero coefficient of the normal vector of the plane. If a non-zero coefficient is not found, ok is set to false.

func (Equation) IntersectionWith

func (l1 Equation) IntersectionWith(l2 Equation) (intersection Vector, intersects bool, equal bool, err error)

IntersectionWith calculates the intersection with another 2D line. intersects is set to true if the lines intersect. equal is set to true if the lines are equal and therefore intersect infinitely many times.

func (Equation) IsParallelTo

func (l1 Equation) IsParallelTo(l2 Equation) (bool, error)

IsParallelTo determines whether two lines are parallel to each other.

func (Equation) NonZeroValuePoint

func (l1 Equation) NonZeroValuePoint() (nonzero Vector, ok bool)

NonZeroValuePoint finds a point on the Line where one of the dimension values is not zero. If a non-zero coefficient is not found, ok is set to false.

func (Equation) Scale

func (l1 Equation) Scale(scalar float64) Equation

Scale scales the line by a scalar multiplier.

func (Equation) String

func (l1 Equation) String() string

func (Equation) X

func (l1 Equation) X(y float64) (float64, error)

X gets the X value for a given Y value.

func (Equation) Y

func (l1 Equation) Y(x float64) (float64, error)

Y gets the Y value for a given X.

type Parameterization

type Parameterization struct {
	Basepoint        Vector
	DirectionVectors []Vector
}

Parameterization provides a way of enumerating the many possible solutions to a system of equations which has infinite solutions.

func (Parameterization) String

func (p1 Parameterization) String() string

type Radian

type Radian float64

Radian is a unit of measurement for angles: 1 radian = 180/π

func NewRadian

func NewRadian(degrees float64) Radian

NewRadian creates a radian from degrees.

func (Radian) Degrees

func (r Radian) Degrees() float64

Degrees converts from radians to degrees.

type System

type System []Equation

System defines a system of equations, where each equation is a linear equation.

func NewSystem

func NewSystem(equations ...Equation) System

NewSystem creates a new system of equations.

func (System) Add

func (s1 System) Add(srcIndex int, dstIndex int, coefficient int) (System, error)

Add adds the equation with srcIndex multiplied by the coefficient to the equation with index dstIndex.

func (System) AllEquationsHaveSameNumberOfTerms

func (s1 System) AllEquationsHaveSameNumberOfTerms() bool

AllEquationsHaveSameNumberOfTerms returns true when all equations in the system have the same number of terms.

func (System) ComputeRREF

func (s1 System) ComputeRREF() (s System, ok bool, err error)

ComputeRREF computes the Reduced Row Echelon Form of the system. ok returns whether all of the terms in the equation have got a value (i.e. there is a solution.)

func (System) Eq

func (s1 System) Eq(s2 System) (bool, error)

Eq determies whether two systems are equal.

func (System) FindFirstNonZeroCoefficients

func (s1 System) FindFirstNonZeroCoefficients() (indices []int)

FindFirstNonZeroCoefficients finds the indices of the first non-zero coefficient of each equation in the system. If a non-zero coefficient is not found, then -1 is returned for that item.

func (System) IsRREF

func (s1 System) IsRREF() (bool, error)

IsRREF determines whether a system is in Reduced Row Echelon form.

func (System) IsTriangularForm

func (s1 System) IsTriangularForm() (triangular bool, allLeadingTermsAreOne bool, err error)

IsTriangularForm determines whether the system is in triangular form, where the top row starts with a non-zero term, the next one down starts with a zero etc., the one after that starts with two zero terms etc.

func (System) Multiply

func (s1 System) Multiply(index int, coefficient float64) (System, error)

Multiply multiplies an equation by a coefficient.

func (System) Parameterize

func (s1 System) Parameterize() (Parameterization, error)

Parameterize handles the case when an infinite number of solutions is found to a system of equations. This occurs when one or more of the coefficients is "free". The function returns a Parameterization object, which consists of a basepoint vector (the )

func (System) Solve

func (s1 System) Solve() (solution Vector, noSolution bool, infiniteSolutions bool, err error)

Solve solves the equation using Gaussian Elimination and returns whether the solution has a single solution, no solutions, infinite solutions or can't be calculated due to an error.

func (System) String

func (s1 System) String() string

String writes out each equation in the system, delineated by commas and surrounded by braces, e.g. { 1x₁ + 2x₂ + 3x₃ = 4, 5x₁ + 6x₂ + 7x₃ = 8 }

func (System) Swap

func (s1 System) Swap(a int, b int) (System, error)

Swap swaps elements in the system.

func (System) TriangularForm

func (s1 System) TriangularForm() (System, error)

TriangularForm organises the system by leading term.

type Vector

type Vector []float64

Vector represents an array of values.

func NewVector

func NewVector(values ...float64) Vector

NewVector creates a vector with the dimensions specified by the argument.

func (Vector) Add

func (v1 Vector) Add(v2 Vector) (Vector, error)

Add adds the input vector to the current vector and returns a new vector.

func (Vector) AngleBetween

func (v1 Vector) AngleBetween(v2 Vector) (Radian, error)

AngleBetween returns the angle (in radians) between the current vector and v2, or an error if the dimensions of the vectors do not match.

func (Vector) AreaOfParallelogram

func (v1 Vector) AreaOfParallelogram(v2 Vector) (float64, error)

AreaOfParallelogram calculates the area of a parallelogram spanned by the basis vector and input vector for 2D and 3D inputs.

func (Vector) AreaOfTriangle

func (v1 Vector) AreaOfTriangle(v2 Vector) (float64, error)

AreaOfTriangle calculates the area of a parallelogram spanned by the basis vector and input vector for 2D and 3D inputs.

func (Vector) CrossProduct

func (v1 Vector) CrossProduct(v2 Vector) (Vector, error)

CrossProduct calculates the cross product of the current vector and the input vector. The cross product produces a vector which is:

  • orthogonal to both v1 and v2.
  • has a magnitude of the magnitude of v1 * the magnitude of v2 * the sine of the angle between v1 and v2

v2 must be a vector with 3 dimensions.

func (Vector) DotProduct

func (v1 Vector) DotProduct(v2 Vector) (float64, error)

DotProduct calculates the dot product of the current vector and the input vector, or an error if the dimensions of the vectors do not match.

func (Vector) Eq

func (v1 Vector) Eq(v2 Vector) bool

Eq compares an input vector against the current vector.

func (Vector) EqWithinTolerance

func (v1 Vector) EqWithinTolerance(v2 Vector, tolerance float64) bool

EqWithinTolerance tests that a vector is equal, within a given tolerance.

func (Vector) IsOrthogonalTo

func (v1 Vector) IsOrthogonalTo(v2 Vector) (bool, error)

IsOrthogonalTo calculates whether the current vector is orthogonal to the input vector by calculating the dot product. If the dot product is zero, then the vectors are orthogonal.

func (Vector) IsParallelTo

func (v1 Vector) IsParallelTo(v2 Vector) (bool, error)

IsParallelTo calculates whether the current vector is parallel to the input vector by normalizing both vectors, and comparing them. In the case that the

func (Vector) IsZeroVector

func (v1 Vector) IsZeroVector() bool

IsZeroVector returns true if all of the values in the vector are within tolerance of zero.

func (Vector) Magnitude

func (v1 Vector) Magnitude() float64

Magnitude calculates the magnitude of the vector by calculating the square root of the sum of each element squared.

func (Vector) Mul

func (v1 Vector) Mul(v2 Vector) (Vector, error)

Mul muliplies the input vector and the current vector together and returns a new vector.

func (Vector) Normalize

func (v1 Vector) Normalize() Vector

Normalize normalizes the magnitude of a vector to 1 and returns a new vector.

func (Vector) Projection

func (v1 Vector) Projection(v2 Vector) (Vector, error)

Projection calculates the projection of the v2 vector onto the basis vector (v1) by calculating the unit vector of v1 and scaling it.

func (Vector) ProjectionOrthogonalComponent

func (v1 Vector) ProjectionOrthogonalComponent(v2 Vector) (Vector, error)

ProjectionOrthogonalComponent calculates the projection of v2 onto the basis vector (v1) and uses that to calculate a component which is orthogonal to the basis vector and perpendicular to v2.

func (Vector) Round

func (v1 Vector) Round(decimals int) Vector

Round rounds the vector to the specified number of places.

func (Vector) Scale

func (v1 Vector) Scale(scalar float64) Vector

Scale muliplies the current vector by the scalar input and returns a new vector.

func (Vector) String

func (v1 Vector) String() string

func (Vector) Sub

func (v1 Vector) Sub(v2 Vector) (Vector, error)

Sub subtracts the input vector from the current vector and returns a new vector.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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