matrix

package module
v0.0.0-...-2ae757b Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2022 License: MIT Imports: 5 Imported by: 0

README

matrix

A linear-algebra based Go library for matrices, vectors, as well as the Gram-Schmidt process,Einstein summation convention and more.

GoDoc

This Go Linear Algebra Library is based on the popular Mathematics for Machine Learning Specialization on Coursera. It involves classic methods of vector and matrix operations, such as dot and inner product, and some tougher challenges, such as finding the eigenvectors of a given matrix or the Gram-Schmidt process.

Installation

Downloading the package should be fairly simple. Run the code below in your directory terminal.

go get github.com/timothy102/matrix

Once you have done that, import the package in your go file.

import "github.com/timothy102/matrix"

If you prefer not to write matrix all the time or for some other reason you want to change the name, type this.

import name "github.com/timothy102/matrix"

Usage

Let's take a look at simple matrix addition.


 m1 :=matrix.RandomMatrix(3,2)
 m2 :=matrix.RandomMatrix(3,2) 
 result :=m1.Add(m2)

 result.PrintByRow()
 

And the output:

 [[7.2,4.2,5.3]
 [9.4,8.5,8.6]]
 

Same applies for vectors.

vector:=NewVector([]float64{2.0,4.0,3.2})
vector.MultipliedByScalar(2.0)

Output:

[4.0,8.0,6.4]

I hope this library offers you to dig deeper into the world of linear algebra and to apply it to some cool machine learning concepts. Looking forward for feedback!

Contact

Feel free to reach out via any of the following: LinkedIn, Medium, Gmail

Documentation

Overview

Package matrix implements matrix and vector operations along with some difficult methods such as the Gram-Schmidt process, Einstein summation convention, eigenvector calculation and more.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InnerProduct

func InnerProduct(matrix Matrix, vector1, vector2 Vector) float64

InnerProduct returns the inner product of two vectors via the matrix. Check the dotProduct function for a simplified dot product of two vectors.

func PageRank

func PageRank(linkMatrix Matrix, pages int)

PageRank algorithm returns the probabilites of randomly ending up on either of the pages. Page connections should be put inside the link matrix.

func PrintVector

func PrintVector(vec Vector)

PrintVector prints the vector components

func Quadratic

func Quadratic(a, b, c float64) (float64, float64)

Quadratic takes a,b,c parameters of a quadratic equation as inputs and returns both solutions via the discriminant.

func Sigmoid

func Sigmoid(x float64) float64

Sigmoid returns sigmoid of x.

func SigmoidPrime

func SigmoidPrime(x float64) float64

SigmoidPrime returns the sigmoid derivative of x.

func VecToArray

func VecToArray(vec Vector) []float64

VecToArray returns a slice of vector elements.

Types

type Matrix

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

Matrix type does matrix math

func AllSameNumber

func AllSameNumber(row, column int, number float64) Matrix

AllSameNumber returns a row * column matrix of number.

func FromArray

func FromArray(arr []float64) Matrix

FromArray returns a matrix from array

func FromValues

func FromValues(row, column int, values []float64) Matrix

FromValues returns a matrix from a set of values

func Identity

func Identity(n int) Matrix

Identity function returns an n*n identity matrix

func Matmul

func Matmul(a, b Matrix) Matrix

Matmul does the matrix multiplication. A's rows must match B's columns

func NewMatrix

func NewMatrix(slice [][]float64) Matrix

NewMatrix returns a matrix and an error

func Ones

func Ones(row, column int) Matrix

Ones returns a matrix of ones.

func RandomValuedMatrix

func RandomValuedMatrix(row, column int) Matrix

RandomValuedMatrix returns a row*column random valued matrix.

func Zeros

func Zeros(row, column int) Matrix

Zeros returns a matrix of zeros.

func (Matrix) Add

func (m Matrix) Add(mat Matrix) Matrix

Add performs elementary matrix addition

func (Matrix) Adjoint

func (m Matrix) Adjoint() (Matrix, error)

Adjoint returns the adjoint matrix

func (*Matrix) At

func (m *Matrix) At(rowIndex, columnIndex int) float64

At method finds the value at rowIndex,columnIndex

func (Matrix) Dimensions

func (m Matrix) Dimensions() (int, int)

Dimensions returns the number of rows and columns of m.

func (Matrix) Divide

func (m Matrix) Divide(mat Matrix) Matrix

Divide performs elementary matrix division

func (Matrix) DotProduct

func (m Matrix) DotProduct(m2 Matrix) float64

DotProduct returns the dot product of two matrices

func (Matrix) EinsteinConvention

func (m Matrix) EinsteinConvention(m2 Matrix) Matrix

EinsteinConvention returns the multiplication matrix of two matrices, given that rows of A matches columns of B. According to this convention, when an index variable appears twice in a single term and is not otherwise defined, it implies summation of that term over all the values of the index.

func (Matrix) Find2x2Determinant

func (m Matrix) Find2x2Determinant() float64

Find2x2Determinant returns the determinant of a 2x2 matrix

func (Matrix) FindDeterminant

func (m Matrix) FindDeterminant() float64

FindDeterminant returns the matrix determinant

func (Matrix) Inverse

func (m Matrix) Inverse() Matrix

Inverse returns the inverse of a matrix

func (Matrix) Inverse2x2

func (m Matrix) Inverse2x2() Matrix

Inverse2x2 returns the inverse of a 2x2 matrix

func (Matrix) MapFunc

func (m Matrix) MapFunc(f func(x float64) float64) Matrix

MapFunc applies f to every element

func (Matrix) Multiply

func (m Matrix) Multiply(mat Matrix) Matrix

Multiply performs elementary matrix multiplication

func (Matrix) NumberOfColumns

func (m Matrix) NumberOfColumns() int

NumberOfColumns returns the number of columns.

func (Matrix) NumberOfElements

func (m Matrix) NumberOfElements() int

NumberOfElements returns the number of elements.

func (Matrix) NumberOfRows

func (m Matrix) NumberOfRows() int

NumberOfRows returns the number of columns.

func (Matrix) PrintByRow

func (m Matrix) PrintByRow()

PrintByRow prints the matrix by row.

func (Matrix) Randomize

func (m Matrix) Randomize() Matrix

Randomize randomizes m to random values

func (Matrix) RoundtoDecimals

func (m Matrix) RoundtoDecimals(decimals int) Matrix

RoundtoDecimals round all elements of a matrix to decimals accuracy.

func (Matrix) ScalarAdition

func (m Matrix) ScalarAdition(scalar float64) Matrix

ScalarAdition adds a scalar to every elements

func (Matrix) ScalarMultiplication

func (m Matrix) ScalarMultiplication(scalar float64) Matrix

ScalarMultiplication multiplies every element with a scalar

func (Matrix) Shorten

func (m Matrix) Shorten(rowIndex, columnIndex int) Matrix

Shorten returns the so-called minor matrix, it shrinks all numbers that lie either with one coordinate on rowIndex or columnIndex

func (Matrix) Slice

func (m Matrix) Slice() [][]float64

Slice returns matrix.slice You can perform indexing with this method.

func (Matrix) Subtract

func (m Matrix) Subtract(mat Matrix) Matrix

Subtract performs elementary matrix subtraction

func (Matrix) ToArray

func (m Matrix) ToArray() []float64

ToArray returns the matrix in array form.

func (Matrix) TransformationInAChangedBasis

func (m Matrix) TransformationInAChangedBasis(basis Matrix) Matrix

TransformationInAChangedBasis function takes a given matrix as an input and outputs it in a changed basis

func (Matrix) Transpose

func (m Matrix) Transpose() Matrix

Transpose returns the tranpose of a matrix

type Vector

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

Vector type

func CalculateEigenvectors2x2

func CalculateEigenvectors2x2(m Matrix) ([]Vector, error)

CalculateEigenvectors2x2 returns the eigenvectors of the given 2x2 matrix

func GramSchmidt

func GramSchmidt(vectors []Vector) ([]Vector, error)

GramSchmidt function returns the concatenated matrix of orthogonal based vectors out of the vectors dataset. Based on the Mathematics for Machine learning Specialization.

func NewVector

func NewVector(slice []float64) Vector

NewVector returns a vector type

func RandomVector

func RandomVector(size int) Vector

RandomVector returns a random valued vector.

func VecAllSameNumber

func VecAllSameNumber(size int, number float64) Vector

VecAllSameNumber returns a vector of zeros

func VecOnes

func VecOnes(size int) Vector

VecOnes returns a vector of zeros

func VecZeros

func VecZeros(size int) Vector

VecZeros returns a vector of zeros

func (Vector) Add

func (v Vector) Add(v2 Vector) Vector

Add returns an elementary operation on two vectors.

func (Vector) AddMany

func (v Vector) AddMany(vectors []Vector) Vector

AddMany takes a slice of vectors and outputs their sum.

func (Vector) AngleBetween

func (v Vector) AngleBetween(v2 Vector) float64

AngleBetween returns the angle between two vectors.

func (Vector) ApplyMatrix

func (v Vector) ApplyMatrix(matrix Matrix) Vector

ApplyMatrix returns the vector through a matrix transformation.

func (Vector) ChangingBasis

func (v Vector) ChangingBasis(b1, b2 Vector) (Vector, error)

ChangingBasis returns the transformed vector if we were to change its basis vectors. Similar to a simple matrix transformation but inputs are two basis vectors. Basis vectors have to be perpendicular to each other in order to apply this transformation. The function takes care of this for you by sending back an error

func (Vector) DotProduct

func (v Vector) DotProduct(v2 Vector) float64

DotProduct returns the dot product of two vectors.

func (Vector) GetLength

func (v Vector) GetLength() float64

GetLength returns the length of a vector.

func (Vector) Map

func (v Vector) Map(f func(float64) float64) Vector

Map maps the vector by with the function

func (Vector) MultiplyByScalar

func (v Vector) MultiplyByScalar(scalar float64) Vector

MultiplyByScalar operates by adding a scalar elementary.

func (Vector) NumberOfElements

func (v Vector) NumberOfElements() int

NumberOfElements returns the number of elements.

func (Vector) ScalarProjection

func (v Vector) ScalarProjection(v2 Vector) float64

ScalarProjection returns the scalar projection of v onto v2.

func (Vector) Slice

func (v Vector) Slice() []float64

Slice returns vector.slice. You can perform indexing with this method.

func (Vector) Substract

func (v Vector) Substract(v2 Vector) Vector

Substract returns an elementary operation on two vectors.

func (Vector) SubstractMany

func (v Vector) SubstractMany(vectors []Vector) Vector

SubstractMany takes a slice of vectors and outputs their divergence.

func (Vector) VectorProjection

func (v Vector) VectorProjection(v2 Vector) Vector

VectorProjection returns the vector projection of v onto v2.

Jump to

Keyboard shortcuts

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