NDArray::dot
public static function dot(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray;
The dot function performs the dot product of two arrays. The behaviour of the function
varies depending on the dimensions and shapes of the input arrays:
- If both
$aand$bare 1-D arrays, the dot product is computed as the inner product of the vectors. - If both
$aand$bare 2-D arrays, the dot product is computed as the matrix multiplication (See NDArray::matmul). - If either
$aor$bis a 0-D (scalar) array, the dot product is equivalent to element-wise multiplication. (See NDArray::multiply). - If
$ais an N-D array and$bis a 1-D array, the dot product is computed as the sum product over the last axis of$aand$b.
Parameters
$a $b
Type
NDArray|array|long|double
- The arrays to perfom the dot product.
Return
Type - NDArray
- If both
$aand$bare scalars or 1-D arrays, the dot product operation yields a scalar value. In this case, a single scalar is returned as the result.- If either
$aor$bis a scalar and the other is an array, or if both$aand$bare arrays with any dimensionality greater than 1, the dot product operation results in an array. The returned array will have a shape determined by the dimensions of$aand$baccording to the dot product rules.
Notes
tip
GPU SUPPORTED
This operation is supported by GPU (VRAM) and contains a custom CUDA kernel.
Examples
- Example 1
- Example 2
- Example 3
use \NDArray as nd;
$a = nd::array([[1, 2],[3, 4]]);
$b = nd::array([1, 2]);
$result = nd::dot($a, $b);
print_r($result);
[5, 11]
use \NDArray as nd;
$a = new nd([[2, -2], [1, -1]]);
$b = new nd([[3, -3], [2, -1]]);
$c = nd::dot($a, $b); // Equivalent to nd::matmul($a, $b)
print_r($c);
[[2, -4],
[1, -2]]
use \NDArray as nd;
$a = new nd([2, -2]);
$b = new nd([1, -1.5]);
$c = nd::dot($a, $b);
print_r($c);
5