\seekquarry\yioop\libraryLinearAlgebra

Class useful for handling linear algebra operations on associative array with key => value pairs where the value is a number.

We call such key => value array, term vectors, or more simply, vectors.

Summary

Methods
Properties
Constants
add()
distortion()
dot()
distance()
length()
multiply()
normalize()
similarity()
subtract()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

add()

add(mixed  $vector1, mixed  $vector2) : array

Adds two vectors component-wise. Treat empty components in either array as zero entries. If either vector is in fact a constant then add that constant to each entry

Parameters

mixed $vector1

first term vector to add. If is a scalar then add that scalar to all components of other vector

mixed $vector2

second term vector to add. If is a scalar then add that scalar to all components of other vector

Returns

array —

associative array corresponding to component-wise adding these two vectors.

distortion()

distortion(array  $vector1, array  $vector2) : float

Calculates the distortion between two term vectors 1. Check each word in first term vector to see if it exists in second.

If the word X of first term vector does not exist in second term vector, square the score of word X and add to the $sum and increase the number of $not_in_common words by one.

  1. In case the term X is common between first term vector and second term vector, subtract first and second vectors weight for this term, square the result and add to $sum.
  2. Then check each word in second term vector to see if it exists in first, in case the word Y is not in the first term vector, square the weight of word Y and add it to the $sum and increase the number of $not_in_common words by one.
  3. At the end, calculate the distortion between sentence1 and sentence2 by dividing $sum by $not_in_common words.

Parameters

array $vector1

(term => weight) pairs of the first sentence

array $vector2

(term => weight) pairs of the second sentence

Returns

float —

the distortion distance between the two sentences

dot()

dot(array  $vector1, array  $vector2) 

Computes the inner product (the dot product) of two term vectors

Parameters

array $vector1

first term vector in product

array $vector2

second term vector in product

distance()

distance(array  $vector1, array  $vector2, integer  $norm_power = 2) : \seekquarry\yioop\library\number

Computes the L_k distance between two vectors. When k=2, this corresponds to Euclidean length

Parameters

array $vector1

first term vector to determine distance between

array $vector2

second term vector to determine distance between

integer $norm_power

which norm, L_{$norm_power}, to use. $norm_power should be >= 1

Returns

\seekquarry\yioop\library\number —

L_{$norm_power} distance between two vectors

length()

length(array  $vector, integer  $norm_power = 2) : \seekquarry\yioop\library\number

Computes the L_k length of a vector. When k=2, this corresponds to Euclidean length

Parameters

array $vector

to compute the length of

integer $norm_power

which norm, L_{$norm_power}, to use. $norm_power should be >= 1

Returns

\seekquarry\yioop\library\number —

length of vector with respect to desired metric.

multiply()

multiply(array  $scalar_vec_mat, array  $vector) : array

Perform multiplication of either a scalar, vector, or a matrix and a vector

Parameters

array $scalar_vec_mat

the scalar, vector or matrix to multiply against the vector

array $vector

the vector to multiply against

Returns

array —

the new vector after it has been multiplied

normalize()

normalize(array  $vector) : array

Computes a unit length vector in the direction of the supplied vector

Parameters

array $vector

vector to find unit vector for

Returns

array —

unit vector in desired direction (on zero input vector, returns zero output vector)

similarity()

similarity(array  $vector1, array  $vector2) : \seekquarry\yioop\library\number

Computes the cosine similarity between two vectors: ($vector1 * $vector2)/(||$vector1||*||$vector2||)

Parameters

array $vector1

first term vector to compare

array $vector2

second term vector to compare

Returns

\seekquarry\yioop\library\number —

a score measuring how similar these two vectors are with respect to cosine similarity

subtract()

subtract(array  $vector1, array  $vector2) : array

Subtracts two vectors component-wise. Treat empty components in either array as zero entries. If either vector is in fact a constant then subtract that constant from each entry

Parameters

array $vector1

first term vector to subtract. If is a scalar then subtract that scalar from all components of other vector

array $vector2

second term vector to subtract. If is a scalar then subtract that scalar from all components of other vector

Returns

array —

associative array corresponding to component-wise subtracting these two vectors.