3d Vector Types and operators - Mailing list pgsql-general

From Andrew Bailey
Subject 3d Vector Types and operators
Date
Msg-id 5bb15ef10910141004n6ef843bm3ef8d0bafcfd1234@mail.gmail.com
Whole thread Raw
Responses Re: 3d Vector Types and operators
List pgsql-general
Hi,

I cant find in the documentation support for a 3 dimensional vector,
I have only seen the array type, I am interested in doing vector dot
products and vector cross products, also summing vectors and
multiplying by a scalar quantity

select array[1,2,3]+array[2,4,5];
select 2*array[1,2,3];

The error message is:
 No operator matches the given name and argument type(s). You might
need to add explicit type casts.

Has anyone tried to do this before?

Has anyone written operators for this?

I have got as far as

CREATE or replace FUNCTION add(anyarray, anyarray) RETURNS anyarray
    AS 'select array[$1[1] + $2[1],$1[2] + $2[2],$1[3] + $2[3]];'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;

drop FUNCTION dot(anyarray, anyarray);
CREATE or replace FUNCTION dot(anyarray, anyarray) RETURNS int
    AS 'select $1[1] * $2[1]+$1[2] * $2[2]+$1[3] * $2[3];'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;

It works for integer arrays:

 select add(array[1,2,3],array[2,4,5]);
   add
---------
 {3,6,8}
(1 row)

select dot(array[1,2,3],array[2,4,5]);
 dot
-----
  25


but it gives me an error for a floating point array

epm=# select add(array[1.2,2,3],array[2,4,5]);
ERROR:  function add(numeric[], integer[]) does not exist
LINE 1: select add(array[1.2,2,3],array[2,4,5]);
               ^
HINT:  No function matches the given name and argument types. You
might need to add explicit type casts.

How can I fix it to cope with real or integer arrays?


How could I change this to use operators?

Is it efficient? Can it be made more efficient?



Thanks in advance

Andy Bailey

pgsql-general by date:

Previous
From: Peter Hunsberger
Date:
Subject: Re: Query to find contiguous ranges on a column
Next
From: Josip
Date:
Subject: How ad an increasing index to a query result?