cosine function

From: Shmagi Kavtaradze <kavtaradze(dot)s(at)gmail(dot)com>
To: pgsql-novice novice <pgsql-novice(at)postgresql(dot)org>
Subject: cosine function
Date: 2016-04-28 21:03:44
Message-ID: CAHY6mawUHzGxz9SCXDcwZrnocK9BvQtJ6xEw04EP4cgE=_NPhg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I want to create a cosine distance function for arrays. Array entries looks
like:

[-0.000434909090909,6.65454545455e-05,-0.000470090909091,-0.000557909090909]

and I want to calculate cosine distance. I created function but not sure if
it is right.

#include "postgres.h"
#include <string.h>

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

PG_FUNCTION_INFO_V1(cos_embedded);

Datum
cos_embedded(PG_FUNCTION_ARGS)
{
ArrayType *A, *B;
float *result;
float dot = 0.0;
float denom_a = 0.0;
float denom_b = 0.0;
A = PG_GETARG_ARRAYTYPE_P(0);
B = PG_GETARG_ARRAYTYPE_P(1);

for(unsigned int i = 0u; i < sizeof(A); ++i) {
dot += A[i] * B[i] ;
denom_a += A[i] * A[i] ;
denom_b += B[i] * B[i] ;
}
result = dot / (sqrt(denom_a) * sqrt(denom_b)) ;
PG_RETURN_FLOAT(result);
}

Thanks in advance.

Browse pgsql-novice by date

  From Date Subject
Next Message richard 2016-04-29 08:27:58 Re: How to view the activity of postgresql
Previous Message Keith 2016-04-28 12:50:59 Re: How to view the activity of postgresql