Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql postgres中数组的向量算法_Sql_Arrays_Postgresql - Fatal编程技术网

Sql postgres中数组的向量算法

Sql postgres中数组的向量算法,sql,arrays,postgresql,Sql,Arrays,Postgresql,postgres似乎支持使用运算符本机追加数组: || array-to-array concatenation ARRAY[1,2,3] || ARRAY[4,5,6] {1,2,3,4,5,6} 来源: 它是否本地支持向量/元素算术运算(即,不为其编写新函数?) 例如,类似于: [1,2,3] + [1,1,1] = [2,3,4] [1,2,3] - [1,1,1] = [0,1,2] [1,2,3] * [2,2,2] = [2,4,6] [1,2,3] / [2,2,2

postgres似乎支持使用运算符本机追加数组:

||  array-to-array concatenation    ARRAY[1,2,3] || ARRAY[4,5,6]    {1,2,3,4,5,6}
来源:

它是否本地支持向量/元素算术运算(即,不为其编写新函数?)

例如,类似于:

[1,2,3] + [1,1,1] = [2,3,4]
[1,2,3] - [1,1,1] = [0,1,2]
[1,2,3] * [2,2,2] = [2,4,6]
[1,2,3] / [2,2,2] = [.5, 1, 1.5]

相关的问题在这里:,虽然它大约有6年的历史,所以从那时起,postgres可能有了变化?

postgres没有支持这种算法的“数字数组”概念。但是,使用数组原语实现起来很容易。例如:

select t.*,
       (select array_agg(e.el1 * e.el2)
        from unnest(t.ar1, t.ar2) e(el1, el2)
       ) ar
from (select array[1,2,3] as ar1,  array[4,5,6] as ar2) t;
unnest()
按元素顺序“解压”两个数组。数组agg然后根据所需的数学运算“重新压缩”它们

实际上,Postgres保留了数组与子查询的顺序。但是,如果您希望明确,请使用带有顺序性的

select t.*,
       (select array_agg(e.el1 * e.el2 order by n)
        from unnest(t.ar1, t.ar2) with ordinality e(el1, el2, n)
       ) ar
from (select array[1, 2, 3] as ar1,  array[4, 5, 6] as ar2) t
如果您安装了,您可以从一些新函数和运算符中获得一些好处,但算术运算却没有