pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] PR #802: Pgvector crossproduct
7+ messages / 3 participants
[nested] [flat]
* [pgvector/pgvector] PR #802: Pgvector crossproduct
@ 2025-03-18 23:20 "matteodurighetto (@matteodurighetto)" <noreply+matteodurighetto@github.com>
0 siblings, 0 replies; 7+ messages in thread
From: matteodurighetto (@matteodurighetto) @ 2025-03-18 23:20 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hello, this is a simple feature: cross product for 3 dimensional vectors, but it is useful for some physics and math calculations .
I have add this function : vector_cross_product (vector,vector) → vector | Cross Product right-handed | 0.8.0
with the operator symbol <*>
It check if the vectors are the same dimensions and the dimension is 3, the calculate the new vector, then check it there are some overflows and if it's all correct, it returns the result vector.
I have done some testing and it seems to work correctly
inline patch 1: README.md, sql/vector.sql, src/vector.c and 2 more (+96/-0 lines) download inline-1.patch
diff --git a/README.md b/README.md
index 08f36ed21..d9210d669 100644
--- a/README.md
+++ b/README.md
@@ -963,6 +963,7 @@ Operator | Description | Added
<#> | negative inner product |
<=> | cosine distance |
<+> | taxicab distance | 0.7.0
+<*> | cross product | 0.8.0
### Vector Functions
@@ -977,6 +978,7 @@ l2_normalize(vector) → vector | Normalize with Euclidean norm | 0.7.0
subvector(vector, integer, integer) → vector | subvector | 0.7.0
vector_dims(vector) → integer | number of dimensions |
vector_norm(vector) → double precision | Euclidean norm |
+vector_cross_product(vector,vector) → vector | Cross Product right-handed | 0.8.0
### Vector Aggregate Functions
diff --git a/sql/vector.sql b/sql/vector.sql
index 7fc36712b..4cd5a23e2 100644
--- a/sql/vector.sql
+++ b/sql/vector.sql
@@ -111,6 +111,9 @@ CREATE FUNCTION vector_avg(double precision[]) RETURNS vector
CREATE FUNCTION vector_combine(double precision[], double precision[]) RETURNS double precision[]
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+CREATE FUNCTION vector_cross_product(vector, vector ) RETURNS vector
+ AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
-- vector aggregates
CREATE AGGREGATE avg(vector) (
@@ -181,6 +184,11 @@ CREATE OPERATOR <#> (
COMMUTATOR = '<#>'
);
+CREATE OPERATOR <*> (
+ LEFTARG = vector, RIGHTARG = vector, PROCEDURE = vector_cross_product,
+ COMMUTATOR = '<*>'
+);
+
CREATE OPERATOR <=> (
LEFTARG = vector, RIGHTARG = vector, PROCEDURE = cosine_distance,
COMMUTATOR = '<=>'
diff --git a/src/vector.c b/src/vector.c
index a5b2aac36..a7ef50fe3 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -616,6 +616,70 @@ inner_product(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT8((double) VectorInnerProduct(a->dim, a->x, b->x));
}
+/*
+ * Get the cross product of two 3D vectors
+ */
+
+FUNCTION_PREFIX PG_FUNCTION_INFO_V1(vector_cross_product);
+Datum
+vector_cross_product(PG_FUNCTION_ARGS)
+{
+ Vector *a = PG_GETARG_VECTOR_P(0);
+ Vector *b = PG_GETARG_VECTOR_P(1);
+ bool orientation = PG_GETARG_BOOL(2);
+ float *ax = a->x;
+ float *bx = b->x;
+ Vector *result;
+ float *rx;
+
+ /* check if vector has different dimension */
+ CheckDims(a, b);
+
+ /* define the result vector */
+ result = InitVector(a->dim);
+ rx = result->x;
+
+ /* check if vector has 3 dimensions */
+
+ if (a->dim != 3 || b->dim != 3)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("cross product is only defined for 3-dimensional vectors")));
+
+ orientation = true; /* right-handed orientation */
+
+ /* orientation == true is "right-handed" */
+ if ( orientation == true )
+ {
+ rx[0] = ax[1] * bx[2] - ax[2] * bx[1];
+ rx[1] = ax[2] * bx[0] - ax[0] * bx[2];
+ rx[2] = ax[0] * bx[1] - ax[1] * bx[0];
+ }
+ /* orientation == false is "left-handed" */
+ else if ( orientation == false )
+ {
+ rx[0] = ax[2] * bx[1] - ax[1] * bx[2];
+ rx[1] = ax[0] * bx[2] - ax[2] * bx[0];
+ rx[2] = ax[1] * bx[0] - ax[0] * bx[1];
+ }
+ else
+ {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("invalid orientation value, this could be true (right-handed) or false (left-handed) ")));
+ }
+
+ /* Check for overflow */
+ for (int i = 0; i < a->dim; i++)
+ {
+ if (isinf(rx[i]))
+ float_overflow_error();
+ }
+
+ PG_RETURN_POINTER(result);
+
+}
+
/*
* Get the negative inner product of two vectors
*/
diff --git a/test/expected/vector_type.out b/test/expected/vector_type.out
index 674865822..49258ab2f 100644
--- a/test/expected/vector_type.out
+++ b/test/expected/vector_type.out
@@ -670,3 +670,21 @@ SELECT sum(v) FROM unnest(ARRAY['[1,2]'::vector, '[3]']) v;
ERROR: different vector dimensions 2 and 1
SELECT sum(v) FROM unnest(ARRAY['[3e38]'::vector, '[3e38]']) v;
ERROR: value out of range: overflow
+SELECT vector_cross_product('[1,0,0]'::vector, '[0,1,0]'::vector);
+ vector_cross_product
+----------------------
+ [0,0,1]
+(1 row)
+
+SELECT vector_cross_product('[1,0,0]'::vector, '[0,0,1]'::vector);
+ vector_cross_product
+----------------------
+ [0,-1,0]
+(1 row)
+
+SELECT vector_cross_product('[3,-3,1]'::vector, '[4,9,2]'::vector);
+ vector_cross_product
+----------------------
+ [-15,-2,39]
+(1 row)
+
diff --git a/test/sql/vector_type.sql b/test/sql/vector_type.sql
index 088b040aa..0d14f1ab6 100644
--- a/test/sql/vector_type.sql
+++ b/test/sql/vector_type.sql
@@ -152,3 +152,7 @@ SELECT sum(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]', NULL]) v;
SELECT sum(v) FROM unnest(ARRAY[]::vector[]) v;
SELECT sum(v) FROM unnest(ARRAY['[1,2]'::vector, '[3]']) v;
SELECT sum(v) FROM unnest(ARRAY['[3e38]'::vector, '[3e38]']) v;
+
+SELECT vector_cross_product('[1,0,0]'::vector, '[0,1,0]'::vector);
+SELECT vector_cross_product('[1,0,0]'::vector, '[0,0,1]'::vector);
+SELECT vector_cross_product('[3,-3,1]'::vector, '[4,9,2]'::vector);
\ No newline at end of file
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #802: Pgvector crossproduct
@ 2025-03-19 03:03 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: jkatz (@jkatz) @ 2025-03-19 03:03 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
From 5 minutes of search, I couldn't find any other PostgreSQL extensions that expose this (PostGIS has an [internal function](https://postgis.net/docs/doxygen/3.5/d2/ddd/lwgeodetic_8c_ab49bfa54fdded3ab7135b890d977cebf.html]) - primarily I'm trying to see if anyone has defined an operator for this.
Given the mathematical notation is `x` for cross product, I'd be hesitant to use `<*>` here (and I was also trying to find some prior art if PostgreSQL extensions used `<*>`; it's hard to search for operators 😢 ). However - while there's still discussion on the patch itself, I'd suggest we try to use an operator closer to the mathematical one, or a commonly accepted one, maybe `<x>`?
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #802: Pgvector crossproduct
@ 2025-03-19 03:06 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: jkatz (@jkatz) @ 2025-03-19 03:06 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I may have bikeshedded on operator definitions too soon - I forgot the cross product was limited to 3 dimensions. I don't know if it makes sense to support this in pgvector, given pgvector supports n-dimensional vectors. If you need speed of calculation, you could use PL/Rust to define a cross-product function.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #802: Pgvector crossproduct
@ 2025-03-19 03:12 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2025-03-19 03:12 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @matteodurighetto, thanks for the PR, but I don't think it's common enough to add. You can create a SQL function for it without pgvector.
```sql
CREATE FUNCTION cross_product(a real[], b real[]) RETURNS real[] AS $$
SELECT ARRAY[a[2] * b[3] - a[3] * b[2], a[3] * b[1] - a[1] * b[3], a[1] * b[2] - a[2] * b[1]];
$$ LANGUAGE SQL;
SELECT cross_product('{1,0,0}', '{0,1,0}');
SELECT cross_product('{1,0,0}', '{0,0,1}');
SELECT cross_product('{3,-3,1}', '{4,9,2}');
```
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #802: Pgvector crossproduct
@ 2025-03-19 03:12 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2025-03-19 03:12 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #802: Pgvector crossproduct
@ 2025-03-19 07:01 ` "matteodurighetto (@matteodurighetto)" <noreply+matteodurighetto@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: matteodurighetto (@matteodurighetto) @ 2025-03-19 07:01 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hello @ankane @jkatz thank you to have review this pull request/enhancement.
I know it's possible to defines yourself the function, but I think it's quite useful to have this operator just available in some cases with the check of dimensions and so on, especially for some math/physical applications and some checks ( like to verify if the vectors are parallel and you check the result is 0 vector ).
Moreover it's a basic operation between vectors like inner product (that it's a function of pg_vector).
Kind Regards
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] PR #802: Pgvector crossproduct
@ 2025-03-19 12:23 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: jkatz (@jkatz) @ 2025-03-19 12:23 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@matteodurighetto You can [define an operator](https://www.postgresql.org/docs/current/sql-createoperator.html) on a stored function, per [`CREATE OPERATOR`](https://www.postgresql.org/docs/current/sql-createoperator.html)
^ permalink raw [nested|flat] 7+ messages in thread
end of thread, other threads:[~2025-03-19 12:23 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-18 23:20 [pgvector/pgvector] PR #802: Pgvector crossproduct "matteodurighetto (@matteodurighetto)" <noreply+matteodurighetto@github.com>
2025-03-19 03:03 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-03-19 03:06 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-03-19 03:12 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-19 03:12 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-19 07:01 ` "matteodurighetto (@matteodurighetto)" <noreply+matteodurighetto@github.com>
2025-03-19 12:23 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox