Help with quadtree matrix column calculation algorithm?

Given this definition and test matrix:

data (Eq a, Show a) => QT a = C a | Q (QT a) (QT a) (QT a) (QT a)
    deriving (Eq, Show)

data (Eq a, Num a, Show a) => Mat a = Mat {nexp :: Int, mat :: QT a}
    deriving (Eq, Show)

-- test matrix, exponent is 2, that is matrix is 4 x 4
test = Mat 2 (Q (C 5) (C 6) (Q (C 1) (C 0) (C 2) (C 1)) (C 3))

|     |     |
|  5  |  6  |
|     |     |
-------------
|1 | 0|     |
|--|--|  3  |
|2 | 1|     |

I'm trying to write a function that will list the sum of columns , for example [13, 11, 18, 18]. The basic idea is to summarize each sub-square:

  • If quadtree (C c), then print the duplicate value 2 ^ (n - 1)times the value c * 2 ^ (n - 1). Example: the first quadri matters (C 5), so we repeat 5 * 2^(2 - 1) = 10, 2 ^ (n - 1) = 2once, getting [5, 5].
  • Otherwise, considering (Q a b c d)we are zipWithcolsum a and c (and b and d).

Of course, this does not work (not even compilation), because after some recursion we have:

zipWith (+) [[10, 10], [12, 12]] [zipWith (+) [[1], [0]] [[2], [1]], [6, 6]]

Haskell, , - , , . colsum:

colsum :: (Eq a, Show a, Num a) => Mat a -> [a]
colsum m = csum (mat m)
    where
        n = nexp m
        csum (C c)       = take (2 ^ n) $ repeat (c * 2 ^ n)
        csum (Q a b c d) = zipWith (+) [colsum $ submat a, colsum $ submat b]
                                       [colsum $ submat c, colsum $ submat d]
        submat q = Mat (n - 1) q

...

+3
3

colsum:

colsum :: (Eq a, Show a, Num a) => Mat a -> [a]
colsum m = csum (mat m)
    where
        n = nexp m
        csum (C c)       = take (2 ^ n) $ repeat (c * 2 ^ n)
        csum (Q a b c d) = zipWith (+) [colsum $ submat a, colsum $ submat b]
                                       [colsum $ submat c, colsum $ submat d]
        submat q = Mat (n - 1) q

, , csum (Q a b c d) = ....

. colsum . ZipWith (+) -:

ghci> :t zipWith (+)
zipWith (+) :: Num a => [a] -> [a] -> [a]

, ZipWith (+). , :

[colsum $ submat a, colsum $ submat b]

[[a]], [a] .

, , ( , , , ):

((colsum $ submat a) ++ (colsum $ submat b))

, c d, .

+1

, "-" , QuadTree, nexp Matrix (C_).

, , . Mat, . , Integral "" , , Integral, ,

m = 2 ^ n; k = 2 ^ n k (m * x)

, , - ++, .

,

+2

.

, 2 n & times; 2 n. , , , .

  • - , .
  • , n & ge; 1, , (.. 2 n-1 & times; 2 n-1).
  • , . , n = 0 (.. 1 1; 1), - ? - - , .

.

  • , 2 n 2 n , .

    (: replicate genericReplicate hoogle).

  • , n & ge; 1, . , - . (.. ), ().

    (: ).

  • , , , 1 & times; 1. , 1 & times; 1 !

- 2 d d & times; 2 d d - . , . "" 1 & times; 1, , 0. , .

0

Source: https://habr.com/ru/post/1790610/


All Articles