RosettaCodeData/Task/Identity-matrix/DuckDB/identity-matrix.duckdb
2025-08-11 18:05:26 -07:00

20 lines
559 B
Text

# Produce an (i,j,value) table representing the matrix I(m,n)
# which is the identity matrix if m = n
create or replace function I(m,n,zero) as table (
with recursive cte(i,j,ij,value) as (
select 1, 1, (null,null), zero
union all
select if(j=n, i+1, i) as i,
if(j=n, 1, j+1) as j,
(i, j) as ij,
zero + if(i=j, 1, 0) as value
from cte
where i <= m
)
select ij[1] as i, ij[2] as j, value
from cte
offset 1
);
# Generate a 3x3 matrix with DOUBLE(3,2) values:
from I(3, 3, 0.000);