-
Notifications
You must be signed in to change notification settings - Fork 0
/
lin_alg.sql
54 lines (42 loc) · 1.01 KB
/
lin_alg.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
create or replace function dot(in x float[][], in y float[])
returns float[]
language plpgsql stable
as $$
declare
output float[];
i int;
j int;
aggregate float;
begin
for i in 1..array_length(x, 1) loop
aggregate = 0;
for j in 1..array_length(x, 2) loop
aggregate := aggregate + x[i][j] * y[j];
end loop;
output := array_append(output, aggregate);
end loop;
return output;
end;
$$;
create or replace function t(in x float[][])
returns float[][]
language plpgsql stable
as $$
declare
output float[][];
i int;
j int;
i_len int; -- features in input
j_len int; -- elems in input
begin
i_len = array_length(x, 1);
j_len = array_length(x, 2);
output := array_fill(NULL::float, ARRAY[j_len, i_len]);
for j in 1..j_len loop
for i in 1..i_len loop
output[j][i] = x[i][j];
end loop;
end loop;
return output;
end;
$$;