:- module(linalg, [matmul/6, dot/4, cholesky/3, blocked_cholesky/4, batched_cholesky/4, spd/2,
                   differentiable_cholesky/3, cholesky_vjp/4]).
:- use_module(library(array)).
:- use_module(library(array/api)).
:- use_module(library(array/grad), [derivative/3]).

% Linear algebra as boxes: products, and Cholesky factorizations, plain, blocked and batched.

matmul(A, B, C, N, M, P) :-
   shape(A, [N, M]), shape(B, [M, P]), shape(C, [N, P]),
   over([I:N, J:P], [C[I, J] := 0]),
   over([I:N, J:P, K:M], [C[I, J] := C[I, J] + A[I, K] * B[K, J]]).

dot(A, B, S, N) :-
   shape(A, [N]), shape(B, [N]),
   S @= vecdot(A, B).

% cholesky(+A, -L, +N): L lower triangular with L L^T = A, right-looking: per column K, the
% column scaled into C, stored, and the trailing triangle updated by C's outer product.
cholesky(A, L, N) :-
   shape(A, [N, N]), shape(C, [N]),
   L @= tril(A),
   over([K:N], [over([I:N], [C[I] := where(I =:= K, sqrt(L[K, K]), where(K < I, L[I, K] / sqrt(L[K, K]), 0))]),
                over([I:N], [L[I, K] := where(K =< I, C[I], L[I, K])]),
                over([I:N, J:N], [L[I, J] := where(K < J, where(J =< I, L[I, J] - C[I] * C[J], L[I, J]), L[I, J])])]).

% differentiable_cholesky(+A, -L, +N): cholesky/3 with its backward. The factor is
% overwritten column by column, so reverse mode cannot read the values its boxes need:
% the rule gives the adjoint in closed form instead, see cholesky_vjp/4. Forward mode
% differentiates the boxes themselves.
differentiable_cholesky(A, L, N) :-
   cholesky(A, L, N),
   derivative(L, [A], [vjp(cholesky_vjp(L, N))]).

% cholesky_vjp(+L, +N, +Lbar, -Abars): Abars = [Abar], the adjoint of A from that of its
% Cholesky factor L, Lbar: with Phi the lower triangle, its diagonal halved,
% S = L^-T Phi(L^T Lbar) L^-1, and, the factorization reading the lower triangle of A alone,
% Abar = S + S^T below the diagonal, S on it, 0 above. L^-1 by forward substitution, a
% column per work item, its rows in order.
cholesky_vjp(L, N, Lbar, [Abar]) :-
   shape(T, [N, N]), shape(P, [N, N]), shape(Linv, [N, N]), shape(Acc, []),
   shape(U, [N, N]), shape(S, [N, N]), shape(Abar, [N, N]),
   over([I:N, J:N], [T[I, J] := 0]),
   over([I:N, J:N, K:N], [T[I, J] := T[I, J] + where(J =< K, L[K, I] * Lbar[K, J], 0)]),
   over([I:N, J:N], [P[I, J] := where(J < I, T[I, J], where(I =:= J, 0.5 * T[I, J], 0))]),
   over([J:N], [over([I:N], [over([], [Acc := 0]),
                             over([K:N], [Acc := Acc + where(K < I, L[I, K] * Linv[K, J], 0)]),
                             Linv[I, J] := (where(I =:= J, 1.0, 0.0) - Acc) / L[I, I]])]),
   over([I:N, J:N], [U[I, J] := 0]),
   over([I:N, J:N, K:N], [U[I, J] := U[I, J] + P[I, K] * Linv[K, J]]),
   over([I:N, J:N], [S[I, J] := 0]),
   over([I:N, J:N, K:N], [S[I, J] := S[I, J] + Linv[K, I] * U[K, J]]),
   over([I:N, J:N], [Abar[I, J] := where(J < I, S[I, J] + S[J, I], where(I =:= J, S[I, J], 0))]).

% spd(+N, -Vals): the matrix min(i, j) + 1, whose Cholesky factor is the triangle of ones.
spd(N, Vals) :-
   findall(V, ( between(0, N, I), I < N, between(0, N, J), J < N, V is min(I, J) + 1 ), Vals).

% blocked_cholesky(+A, -L, +N, +NB): L L^T = A by panels of NB columns. Per panel: a sweep
% factors the diagonal block; the panel's columns are copied to P, from which a kernel per
% row solves the rows below the block against the block, L21 = A21 L11^-T; the finished panel
% is copied to P again, and the trailing matrix loses its outer product, a staged reduction
% over the panel's columns launched over the active block only.
blocked_cholesky(A, L, N, NB) :-
   Panels is N // NB,
   shape(A, [N, N]), shape(C, [N]), shape(P, [N, NB]), shape(S, []),
   L @= tril(A),
   over([Kb:Panels], [
      over([K:NB], [over([I:N], [C[I] := where(I =:= Kb*NB + K, sqrt(L[Kb*NB + K, Kb*NB + K]),
                                         where(Kb*NB + K < I, where(I < Kb*NB + NB,
                                               L[I, Kb*NB + K] / sqrt(L[Kb*NB + K, Kb*NB + K]), 0), 0))]),
                    over([I:N], [L[I, Kb*NB + K] := where(Kb*NB + K =< I, where(I < Kb*NB + NB, C[I], L[I, Kb*NB + K]),
                                                           L[I, Kb*NB + K])]),
                    over([I:N, J:N], [L[I, J] := where(Kb*NB + K < J, where(J < Kb*NB + NB, where(J =< I, where(I < Kb*NB + NB,
                                                 L[I, J] - C[I] * C[J], L[I, J]), L[I, J]), L[I, J]), L[I, J])])]),
      over([I:N, Kk:NB], [P[I, Kk] := L[I, Kb*NB + Kk]]),
      over([I:N], [over([J:NB], [over([], [S := 0]),
                                 over([Kk:NB], [S := S + where(Kk < J, L[I, Kb*NB + Kk] * P[Kb*NB + J, Kk], 0)]),
                                 L[I, Kb*NB + J] := where(Kb*NB + NB =< I, (L[I, Kb*NB + J] - S) / P[Kb*NB + J, J],
                                                          L[I, Kb*NB + J])])]),
      over([I:N, Kk:NB], [P[I, Kk] := L[I, Kb*NB + Kk]]),
      over([I:N, J:N, Kk:NB], [L[I, J] := where(Kb*NB + NB =< J, where(J =< I, L[I, J] - P[I, Kk] * P[J, Kk], L[I, J]), L[I, J])])]).

batched_cholesky(A, L, M, N) :-
   shape(A, [M, N, N]), shape(C, [M, N]),
   L @= tril(A),
   over([B:M, K:N], [over([I:N], [C[B, I] := where(I =:= K, sqrt(L[B, K, K]), where(K < I, L[B, I, K] / sqrt(L[B, K, K]), 0))]),
                     over([I:N], [L[B, I, K] := where(K =< I, C[B, I], L[B, I, K])]),
                     over([I:N, J:N], [L[B, I, J] := where(K < J, where(J =< I, L[B, I, J] - C[B, I] * C[B, J], L[B, I, J]), L[B, I, J])])]).
