gpt4 book ai didi

postgresql - postgresql中两个向量(行)的叉积

转载 作者:行者123 更新时间:2023-11-29 13:30:33 24 4
gpt4 key购买 nike

我试图在 postgresql 中计算两个向量的叉积。插图:输入:

edgex edgey edgez
6.80837996699847 39.4163159399759 0
0 0 12

使用查询在 postgresql 中对这两行(向量)进行叉积

最佳答案

为其创建一个函数。假设这些是 3D 点 PostGIS 几何图形:

CREATE OR REPLACE FUNCTION ST_CrossProduct(point_a geometry, point_b geometry)
RETURNS geometry AS
$BODY$SELECT ST_SetSRID(ST_MakePoint(
a2 * b3 - a3 * b2,
a3 * b1 - a1 * b3,
a1 * b2 - a2 * b1), ST_SRID($1))
FROM (SELECT
ST_X($1) AS a1, ST_Y($1) AS a2, COALESCE(ST_Z($1), 0.0) AS a3,
ST_X($2) AS b1, ST_Y($2) AS b2, COALESCE(ST_Z($2), 0.0) AS b3
) AS f$BODY$
LANGUAGE sql IMMUTABLE;

使用它:

SELECT ST_AsText(ST_CrossProduct(
ST_MakePoint(6.80837996699847, 39.4163159399759, 0),
ST_MakePoint(0, 0, 12)));

st_astext
------------------------------------------------
POINT Z (472.995791279711 -81.7005596039816 0)
(1 row)

关于postgresql - postgresql中两个向量(行)的叉积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24693955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com