PHP Class MathPHP\LinearAlgebra\Vector

Inheritance: implements Countable, implements ArrayAccess, implements JsonSerializable
Show file Open project: markrogoyski/math-php Class Usage Examples

Public Methods

Method Description
__construct ( array $A ) Constructor
__toString ( ) : string Print the vector as a string Ex: [1, 2, 3]
add ( Vector $B ) : Vector Add (A + B)
asColumnMatrix ( ) : Matrix Get the vector as an nx1 column matrix
asRowMatrix ( ) : Matrix Get the vector as a 1xn row matrix
count ( ) : integer ************************************************************************ Countable INTERFACE ************************************************************************
crossProduct ( Vector $B ) : Vector Cross product (AxB) https://en.wikipedia.org/wiki/Cross_product
directProduct ( Vector $B ) : Matrix Direct product (dyadic)
dotProduct ( Vector $B ) : number Dot product (inner product) (A⋅B) https://en.wikipedia.org/wiki/Dot_product
get ( integer $i ) : number Get a specific value at position i
getN ( ) : integer Get item count (n)
getVector ( ) : array Get matrix
innerProduct ( Vector $B ) : number Inner product (convience method for dot product) (A⋅B)
jsonSerialize ( ) ************************************************************************ JsonSerializable INTERFACE ************************************************************************
l1Norm ( ) : number l₁-norm (|x|₁) Also known as Taxicab norm or Manhattan norm
l2Norm ( ) : number l²-norm (|x|₂) Also known as Euclidean norm, Euclidean length, L² distance, ℓ² distance Used to normalize a vector.
length ( ) : number Vector length (magnitude) Same as l2-norm
maxNorm ( ) : number Max norm (infinity norm) (|x|∞)
normalize ( ) : Vector Normalize (Â) The normalized vector  is a vector in the same direction of A but with a norm (length) of 1. It is a unit vector.
offsetExists ( $i ) : boolean ************************************************************************ ArrayAccess INTERFACE ************************************************************************
offsetGet ( $i )
offsetSet ( $i, $value )
offsetUnset ( $i )
outerProduct ( Vector $B ) : Matrix Outer product (A⨂B) https://en.wikipedia.org/wiki/Outer_product
pNorm ( $p ) : number p-norm (|x|p) Also known as lp norm
perp ( Vector $B ) : Vector Perpendicular of A on B https://en.wikipedia.org/wiki/Vector_projection#Vector_projection
perpDotProduct ( Vector $B ) : number Perp dot product (A⊥⋅B) A modification of the two-dimensional dot product in which A is replaced by the perpendicular vector rotated 90º degrees.
perpendicular ( ) : Vector Perpendicular (A⊥) A vector perpendicular to A (A-perp) with the length that is rotated 90º counter clockwise.
projection ( Vector $B ) : Vector Projection of A onto B https://en.wikipedia.org/wiki/Vector_projection#Vector_projection
scalarDivide ( number $k ) : Vector Scalar divide kA = [k / a₁, k / a₂, k / a₃ .
scalarMultiply ( number $k ) : Vector Scalar multiplication (scale) kA = [k * a₁, k * a₂, k * a₃ .
subtract ( Vector $B ) : Vector Subtract (A - B)
sum ( ) : number Sum of all elements

Method Details

__construct() public method

Constructor
public __construct ( array $A )
$A array 1 x n vector

__toString() public method

Print the vector as a string Ex: [1, 2, 3]
public __toString ( ) : string
return string

add() public method

A = [a₁, a₂, a₃] B = [b₁, b₂, b₃] A + B = [a₁ + b₁, a₂ + b₂, a₃ + b₃]
public add ( Vector $B ) : Vector
$B Vector
return Vector

asColumnMatrix() public method

Example: V = [1, 2, 3] [1] R = [2] [3]
public asColumnMatrix ( ) : Matrix
return Matrix

asRowMatrix() public method

Example: V = [1, 2, 3] R = [ [1, 2, 3] ]
public asRowMatrix ( ) : Matrix
return Matrix

count() public method

************************************************************************ Countable INTERFACE ************************************************************************
public count ( ) : integer
return integer

crossProduct() public method

| i j k | A x B = | a₀ a₁ a₂ | = |a₁ a₂| - |a₀ a₂| + |a₀ a₁| | b₀ b₁ b₂ | |b₁ b₂|i |b₀ b₂|j |b₀ b₁|k = (a₁b₂ - b₁a₂) - (a₀b₂ - b₀a₂) + (a₀b₁ - b₀a₁)
public crossProduct ( Vector $B ) : Vector
$B Vector
return Vector

directProduct() public method

[A₁] [A₁B₁ A₁B₂ A₁B₃] AB = ABᵀ = [A₂] [B₁ B₂ B₃] = [A₂B₁ A₂B₂ A₂B₃] [A₃] [A₃B₁ A₃B₂ A₃B₃]
public directProduct ( Vector $B ) : Matrix
$B Vector
return Matrix

dotProduct() public method

Dot product (inner product) (A⋅B) https://en.wikipedia.org/wiki/Dot_product
public dotProduct ( Vector $B ) : number
$B Vector
return number

get() public method

Get a specific value at position i
public get ( integer $i ) : number
$i integer index
return number

getN() public method

Get item count (n)
public getN ( ) : integer
return integer number of items

getVector() public method

Get matrix
public getVector ( ) : array
return array of arrays

innerProduct() public method

Inner product (convience method for dot product) (A⋅B)
public innerProduct ( Vector $B ) : number
$B Vector
return number

jsonSerialize() public method

************************************************************************ JsonSerializable INTERFACE ************************************************************************
public jsonSerialize ( )

l1Norm() public method

https://en.wikipedia.org/wiki/Norm_(mathematics)#Taxicab_norm_or_Manhattan_norm |x|₁ = ∑|xᵢ|
public l1Norm ( ) : number
return number

l2Norm() public method

http://mathworld.wolfram.com/L2-Norm.html https://en.wikipedia.org/wiki/Norm_(mathematics)#Euclidean_norm ______ |x|₂ = √∑|xᵢ|²
public l2Norm ( ) : number
return number

length() public method

Vector length (magnitude) Same as l2-norm
public length ( ) : number
return number

maxNorm() public method

|x|∞ = max |x|
public maxNorm ( ) : number
return number

normalize() public method

http://mathworld.wolfram.com/NormalizedVector.html A Â ≡ --- |A| where |A| is the l²-norm (|A|₂)
public normalize ( ) : Vector
return Vector

offsetExists() public method

************************************************************************ ArrayAccess INTERFACE ************************************************************************
public offsetExists ( $i ) : boolean
return boolean

offsetGet() public method

public offsetGet ( $i )

offsetSet() public method

public offsetSet ( $i, $value )

offsetUnset() public method

public offsetUnset ( $i )

outerProduct() public method

Outer product (A⨂B) https://en.wikipedia.org/wiki/Outer_product
public outerProduct ( Vector $B ) : Matrix
$B Vector
return Matrix

pNorm() public method

https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm |x|p = (∑|xᵢ|ᵖ)¹/ᵖ
public pNorm ( $p ) : number
return number

perp() public method

A⋅B⊥ perpᵇA = ---- B⊥ |B|²
public perp ( Vector $B ) : Vector
$B Vector
return Vector

perpDotProduct() public method

http://mathworld.wolfram.com/PerpDotProduct.html
public perpDotProduct ( Vector $B ) : number
$B Vector
return number

perpendicular() public method

[a] [-b] A = [b] A⊥ = [a]
public perpendicular ( ) : Vector
return Vector

projection() public method

A⋅B projᵇA = --- B |B|²
public projection ( Vector $B ) : Vector
$B Vector
return Vector

scalarDivide() public method

..]
public scalarDivide ( number $k ) : Vector
$k number Scale factor
return Vector

scalarMultiply() public method

..]
public scalarMultiply ( number $k ) : Vector
$k number Scale factor
return Vector

subtract() public method

A = [a₁, a₂, a₃] B = [b₁, b₂, b₃] A - B = [a₁ - b₁, a₂ - b₂, a₃ - b₃]
public subtract ( Vector $B ) : Vector
$B Vector
return Vector

sum() public method

Sum of all elements
public sum ( ) : number
return number