pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feedFrom: matteodurighetto (@matteodurighetto) <noreply+matteodurighetto@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #802: Pgvector crossproduct
Date: Tue, 18 Mar 2025 23:20:45 +0000
Message-ID: <gh-pgvector-pgvector-802@github.com> (raw)
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
view thread (7+ messages) latest in thread
Message-ID: <gh-pgvector-pgvector-802@github.com>
Permalink: ../gh-pgvector-pgvector-802@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