in flink-ml-python/pyflink/ml/linalg.py [0:0]
def dot(self, other: Union[Vector, np.ndarray, Sized]) -> np.ndarray:
"""
Dot product of two Vectors.
Examples:
::
>>> sparse = SparseVector(4, [1, 3], [3.0, 4.0])
>>> sparse.dot(sparse)
25.0
>>> sparse.dot(array.array('d', [1., 2., 3., 4.]))
22.0
>>> sparse2 = SparseVector(4, [2], [1.0])
>>> sparse.dot(sparse2)
0.0
>>> sparse.dot(np.array([[1, 1], [2, 2], [3, 3], [4, 4]]))
array([22., 22.])
"""
if isinstance(other, np.ndarray):
if other.ndim not in (2, 1):
raise ValueError('Cannot call dot with %d-dimensional array' % other.ndim)
assert len(self) == other.shape[0], "dimension mismatch"
return np.dot(self._values, other[self._indices])
assert len(self) == len(other), "dimension mismatch"
if isinstance(other, DenseVector):
return np.dot(other.to_array()[self._indices], self._values)
elif isinstance(other, SparseVector):
self_cmind = np.in1d(self._indices, other._indices, assume_unique=True)
self_values = self._values[self_cmind]
if self_values.size == 0:
return np.float_(0.0) # type: ignore
else:
other_cmind = np.in1d(other._indices, self._indices, assume_unique=True)
return np.dot(self_values, other._values[other_cmind])
else:
if isinstance(other, (array.array, np.ndarray, list, tuple, range)):
return self.dot(DenseVector(other))
raise ValueError('Cannot call with the type %s' % (type(other)))