3d Vector Types and operators

From: Andrew Bailey <hazlorealidad(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: 3d Vector Types and operators
Date: 2009-10-14 17:04:26
Message-ID: 5bb15ef10910141004n6ef843bm3ef8d0bafcfd1234@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Josip 2009-10-14 17:05:52 How ad an increasing index to a query result?
Previous Message Peter Hunsberger 2009-10-14 16:56:06 Re: Query to find contiguous ranges on a column