diff --git a/README.md b/README.md index 1e1e998..2314bb1 100644 --- a/README.md +++ b/README.md @@ -967,6 +967,7 @@ inner_product(intvec, intvec) → double precision | inner product | 0.8.0 l1_distance(intvec, intvec) → double precision | taxicab distance | 0.8.0 l2_distance(intvec, intvec) → double precision | Euclidean distance | 0.8.0 l2_norm(intvec) → double precision | Euclidean norm | 0.8.0 +vector_dims(intvec) → integer | number of dimensions | 0.8.0 ### Bit Type diff --git a/sql/vector.sql b/sql/vector.sql index f3d17aa..2809fd3 100644 --- a/sql/vector.sql +++ b/sql/vector.sql @@ -692,6 +692,9 @@ CREATE FUNCTION cosine_distance(intvec, intvec) RETURNS float8 CREATE FUNCTION l1_distance(intvec, intvec) RETURNS float8 AS 'MODULE_PATHNAME', 'intvec_l1_distance' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; +CREATE FUNCTION vector_dims(intvec) RETURNS integer + AS 'MODULE_PATHNAME', 'intvec_vector_dims' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; + CREATE FUNCTION l2_norm(intvec) RETURNS float8 AS 'MODULE_PATHNAME', 'intvec_l2_norm' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE; diff --git a/src/intvec.c b/src/intvec.c index fc9283b..03716d1 100644 --- a/src/intvec.c +++ b/src/intvec.c @@ -580,6 +580,18 @@ intvec_l1_distance(PG_FUNCTION_ARGS) PG_RETURN_FLOAT8((double) distance); } +/* + * Get the dimensions of an int vector + */ +FUNCTION_PREFIX PG_FUNCTION_INFO_V1(intvec_vector_dims); +Datum +intvec_vector_dims(PG_FUNCTION_ARGS) +{ + IntVector *a = PG_GETARG_INTVEC_P(0); + + PG_RETURN_INT32(a->dim); +} + /* * Get the L2 norm of an int vector */ diff --git a/test/expected/intvec.out b/test/expected/intvec.out index 2e84d68..1e93589 100644 --- a/test/expected/intvec.out +++ b/test/expected/intvec.out @@ -228,6 +228,12 @@ SELECT intvec_cmp('[2,3]', '[1,2,3]'); 1 (1 row) +SELECT vector_dims('[1,2,3]'::intvec); + vector_dims +------------- + 3 +(1 row) + SELECT l2_distance('[0,0]'::intvec, '[3,4]'); l2_distance ------------- diff --git a/test/sql/intvec.sql b/test/sql/intvec.sql index bbc67db..264bdc7 100644 --- a/test/sql/intvec.sql +++ b/test/sql/intvec.sql @@ -48,6 +48,8 @@ SELECT intvec_cmp('[1,2,3]', '[1,2]'); SELECT intvec_cmp('[1,2]', '[2,3,4]'); SELECT intvec_cmp('[2,3]', '[1,2,3]'); +SELECT vector_dims('[1,2,3]'::intvec); + SELECT l2_distance('[0,0]'::intvec, '[3,4]'); SELECT l2_distance('[0,0]'::intvec, '[0,1]'); SELECT l2_distance('[1,2]'::intvec, '[3]');