From 20ef3a3a0bc6b90fdb47306bdb882d1665df5af3 Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Thu, 5 Oct 2023 16:20:20 +0100 Subject: [PATCH] Sorted DTW algorithm and path extraction without decomposing into modules --- fluid/example/dtw/matrix-update.fld | 31 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/fluid/example/dtw/matrix-update.fld b/fluid/example/dtw/matrix-update.fld index fbb7a9cf8..fd211cbf7 100644 --- a/fluid/example/dtw/matrix-update.fld +++ b/fluid/example/dtw/matrix-update.fld @@ -35,20 +35,29 @@ let feq Infty Infty = True; let minAndPrev (i, j) im1 jm1 ijm1 = let minim = fminimal [im1, jm1, ijm1] in if feq minim im1 then - ((i - 1, j), minim) + ((i, j + 1), minim) else if feq minim jm1 then - ((i, j - 1), minim) - else ((i - 1, j - 1), minim); - -let localMinUpdate seq1 seq2 matrix (i, j) = + ((i + 1, j ), minim) + else ((i, j), minim); +let extractPath indmatrix (i, j) = + let traverser (n,m) matrix accum = + if and (n == 1) (m == 1) + then accum + else + traverser (matrix!(n,m)) matrix ((n - 1,m - 1) : accum) + in traverser (i,j) indmatrix Nil; +let localMinUpdate seq1 seq2 (costmatrix, indmatrix) (i, j) = let iEntr = nth (i - 1) seq1; jEntr = nth (j - 1) seq2; dist = (iEntr - jEntr) * (iEntr - jEntr); - im1 = matrix!(i , j + 1); - jm1 = matrix!(i + 1, j); - im1jm1 = matrix!(i , j ); - ((prei,prej), minim) = minAndPrev (i + 1, j + 1) im1 jm1 im1jm1; + im1 = costmatrix!(i , j + 1); + jm1 = costmatrix!(i + 1, j); + im1jm1 = costmatrix!(i , j ); + (prev, minim) = minAndPrev (i, j) im1 jm1 im1jm1; newVal = fadd (FNum dist) minim - in matrixUpdate matrix (i + 1,j + 1) newVal -in foldl (localMinUpdate [3,1,2,2,1] [2,0,0,3,3,1,0]) (costMatrixInit 6 8 2) (nextIndices 5 7 2) \ No newline at end of file + in (matrixUpdate costmatrix (i + 1,j + 1) newVal, matrixUpdate indmatrix (i + 1,j + 1) prev) +in + let (finished, indices) = foldl (localMinUpdate [3,1,2,2,1] [2,0,0,3,3,1,0]) (costMatrixInit 6 8 2, [| 0 | (i,j) in (6, 8) |]) (nextIndices 5 7 2) + in + (finished, extractPath indices (6,8)) \ No newline at end of file