SCALAR_ VECTOR_ MUL
On this page
Multiplies each element in the vector blob with the specified scalar value and returns a single vector blob.
It is a scalar function.
Syntax
SCALAR_VECTOR_MUL(scalar_value, vector_expression)
Arguments
-
scalar_
: A scalar value.value -
vector_
: An expression that evaluates to a vector.expression The vector must be encoded as a blob containing packed single-precision or double-precision floating-point numbers in little-endian byte order.
Return Type
A blob containing packed single-precision floating-point numbers in little-endian byte order.
Remarks
A vector can be of any length, but blob lengths must be divisible by 4 bytes (or 8 bytes, depending on the datatype specified).
You can specify the datatype of the vector elements in which this operation is performed on the vector by adding a suffix to the function._
.
Suffix |
Datatype |
---|---|
|
8-bit signed integer |
|
16-bit signed integer |
|
32-bit signed integer |
|
64-bit signed integer |
|
32-bit floating-point number (IEEE standard format) |
|
64-bit floating-point number (IEEE standard format) |
Examples
The following queries use the JSON_
function to view the output in JSON format.
Multiplying a Vector with a Scalar
The following example multiplies a vector blob with a scalar value.
SELECT JSON_ARRAY_UNPACK(SCALAR_VECTOR_MUL(10, JSON_ARRAY_PACK('[6.21,3.84,7.19]'))) "Result";
+------------------------------------+
| Result |
+------------------------------------+
| [62.0999985,38.3999977,71.9000015] |
+------------------------------------+
You can also view the vector blob in hexadecimal format using the HEX()
function.
SELECT HEX(SCALAR_VECTOR_MUL(10, JSON_ARRAY_PACK('[6.21,3.84,7.19]'))) "Result";
+--------------------------+
| Result |
+--------------------------+
| 6666784299991942CDCC8F42 |
+--------------------------+
Typecasting the Datatype Using Suffixes
To perform this operation on vectors specified in a format other than the default 32-bit floating point number, specify a suffix with the SCALAR_
function.
SELECT JSON_ARRAY_UNPACK_I16(SCALAR_VECTOR_MUL_I16(10, JSON_ARRAY_PACK_I16('[6.21,3.84,7.19]')));
+-------------------------------------------------------------------------------------------+
| JSON_ARRAY_UNPACK_I16(SCALAR_VECTOR_MUL_I16(10, JSON_ARRAY_PACK_I16('[6.21,3.84,7.19]'))) |
+-------------------------------------------------------------------------------------------+
| [60,40,70] |
+-------------------------------------------------------------------------------------------+
In this query, the JSON_
converts the input values to 16-bit integers, hence the fractional values are lost.
Related Topics
Last modified: February 28, 2023