arithmetic operators¶
MatrixOne supports basic arithmetic operators such as addition, subtraction, multiplication, and division between vectors or between vectors and scalars. These operators perform element-by-element arithmetic operations and return a new vector.
Note
Subtraction (-), multiplication (*) and division (/) are all similar to addition examples and will not be described in detail.
Add¶
Function Description¶
+ is used to add two elements.
Function Syntax¶
> SELECT para1 + para2
Example¶
drop table if exists vec_table;
create table vec_table(a int, b vecf32(3), c vecf64(3));
insert into vec_table values(1, "[1,2,3]", "[4,5,6]");
mysql> select * from vec_table;
+-------+--------------------------+
| a | b | c |
+-------+--------------------------+
| 1 | [1, 2, 3] | [4, 5, 6] |
+-------+--------------------------+
1 row in set (0.00 sec)
mysql> select b + "[1,2,3]" from vec_table;
+-----------------+
| b + [1,2,3] |
+-----------------+
| [2, 4, 6] |
+-----------------+
1 row in set (0.00 sec)
mysql> select b + 1 from vec_table;
+---------------+
| b + 1 |
+---------------+
| [2, 3, 4] |
+---------------+
1 row in set (0.01 sec)
mysql> select cast("[1,2,3]" as vecf32(3)) + 5.0;
+---------------------------------------+
| cast([1,2,3] as vecf32(3)) + 5.0 |
+---------------------------------------+
| [6, 7, 8] |
+---------------------------------------+
1 row in set (0.00 sec)
limit¶
When two vector type parameters are added together, the dimensions of the vector should be the same.
If the two vector type parameters are added, with the types vecf32 and vecf64, the result will be converted to vecf64.
When the vector type and scalar type data are subtracted, the vector type data must be subtracted.
Divide¶
Function Description¶
/ is used to divide two vector elements.
Function Syntax¶
> SELECT para1 / para2
Example¶
drop table if exists vec_table;
create table vec_table(a int, b vecf32(3), c vecf64(3));
insert into vec_table values(1, "[1,2,3]", "[4,5,6]");
mysql> select * from vec_table;
+-------+--------------------------+
| a | b | c |
+-------+--------------------------+
| 1 | [1, 2, 3] | [4, 5, 6] |
+-------+--------------------------+
1 row in set (0.00 sec)
mysql> select b/b from vec_table;
+---------------+
| b / b |
+---------------+
| [1, 1, 1] |
+---------------+
1 row in set (0.00 sec)
mysql> select cast("[1,2,3]" as vecf32(3)) / b from vec_table;;
+------------------------------------+
| cast([1,2,3] as vecf32(3)) / b |
+------------------------------------+
| [1, 1, 1] |
+------------------------------------+
1 row in set (0.00 sec)
mysql> select b/2 from vec_table;
+--------------------+
| b / 2 |
+--------------------+
| [0.5, 1, 1.5] |
+--------------------+
1 row in set (0.00 sec)
limit¶
The denominator element is not allowed to be 0, otherwise an error will occur.
The dimensions should be the same when both parameters are vector types.
If the two vector type parameters are added, with the types vecf32 and vecf64, the result will be converted to vecf64.
When the vector type and scalar type data are divided, the vector type data must be a divisor.