Yioop_V9.5_Source_Code_Documentation

LinearAlgebra
in package

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.

Tags
author

Chris Pollett chris@pollett.org

Table of Contents

add()  : array<string|int, mixed>
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
distance()  : number
Computes the L_k distance between two vectors. When k=2, this corresponds to Euclidean length
distortion()  : float
Calculates the distortion between two term vectors 1. Check each word in first term vector to see if it exists in second.
dot()  : mixed
Computes the inner product (the dot product) of two term vectors
length()  : number
Computes the L_k length of a vector. When k=2, this corresponds to Euclidean length
multiply()  : array<string|int, mixed>
Perform multiplication of either a scalar, vector, or a matrix and a vector
normalize()  : array<string|int, mixed>
Computes a unit length vector in the direction of the supplied vector
similarity()  : number
Computes the cosine similarity between two vectors: ($vector1 * $vector2)/(||$vector1||*||$vector2||)
subtract()  : array<string|int, mixed>
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

Methods

add()

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

public static add(mixed $vector1, mixed $vector2) : array<string|int, mixed>
Parameters
$vector1 : mixed

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

$vector2 : mixed

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

Return values
array<string|int, mixed>

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

distance()

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

public static distance(array<string|int, mixed> $vector1, array<string|int, mixed> $vector2[, int $norm_power = 2 ]) : number
Parameters
$vector1 : array<string|int, mixed>

first term vector to determine distance between

$vector2 : array<string|int, mixed>

second term vector to determine distance between

$norm_power : int = 2

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

Return values
number

L_{$norm_power} distance between two vectors

distortion()

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

public static distortion(array<string|int, mixed> $vector1, array<string|int, mixed> $vector2) : float

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. 2. 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. 3. 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. 4. At the end, calculate the distortion between sentence1 and sentence2 by dividing $sum by $not_in_common words.

Parameters
$vector1 : array<string|int, mixed>

(term => weight) pairs of the first sentence

$vector2 : array<string|int, mixed>

(term => weight) pairs of the second sentence

Return values
float

the distortion distance between the two sentences

dot()

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

public static dot(array<string|int, mixed> $vector1, array<string|int, mixed> $vector2) : mixed
Parameters
$vector1 : array<string|int, mixed>

first term vector in product

$vector2 : array<string|int, mixed>

second term vector in product

Return values
mixed

length()

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

public static length(array<string|int, mixed> $vector[, int $norm_power = 2 ]) : number
Parameters
$vector : array<string|int, mixed>

to compute the length of

$norm_power : int = 2

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

Return values
number

length of vector with respect to desired metric.

multiply()

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

public static multiply(array<string|int, mixed> $scalar_vec_mat, array<string|int, mixed> $vector) : array<string|int, mixed>
Parameters
$scalar_vec_mat : array<string|int, mixed>

the scalar, vector or matrix to multiply against the vector

$vector : array<string|int, mixed>

the vector to multiply against

Return values
array<string|int, mixed>

the new vector after it has been multiplied

normalize()

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

public static normalize(array<string|int, mixed> $vector) : array<string|int, mixed>
Parameters
$vector : array<string|int, mixed>

vector to find unit vector for

Return values
array<string|int, mixed>

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

similarity()

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

public static similarity(array<string|int, mixed> $vector1, array<string|int, mixed> $vector2) : number
Parameters
$vector1 : array<string|int, mixed>

first term vector to compare

$vector2 : array<string|int, mixed>

second term vector to compare

Return values
number

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

subtract()

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

public static subtract(array<string|int, mixed> $vector1, array<string|int, mixed> $vector2) : array<string|int, mixed>
Parameters
$vector1 : array<string|int, mixed>

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

$vector2 : array<string|int, mixed>

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

Return values
array<string|int, mixed>

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


        

Search results