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