diagonal :: Acc (Matrix Double) -> Acc (Vector Double)

15 views
Skip to first unread message

Alexandre Delanoë

unread,
Jun 5, 2018, 7:46:53 PM6/5/18
to Accelerate
Hello,
I am wondering how to write this function with accelerate:

diag :: Acc (Matrix Double) -> Acc (Vector Double)
diag = undefined

Thanks for your suggestions and this great library.
Alexandre

Trevor McDonell

unread,
Jun 5, 2018, 8:03:04 PM6/5/18
to accelerat...@googlegroups.com

Hi Alexandre,

I guess you want to just extract the diagonal of a square matrix? The following would work:

diag :: Elt e => Acc (Matrix e) -> Acc (Vector e)
diag mat =
  generate                          -- https://hackage.haskell.org/package/accelerate-1.2.0.0/docs/Data-Array-Accelerate.html#v:generate
    (indexTail (shape mat))         -- assume the matrix is square, then 'shape mat = Z :. n :. n'
    (\ix -> let Z :. i = unlift ix  -- at each index, read the diagonal element from 'mat'
            in  mat ! index2 i i)

If you want to go the other direction, you could do something like this.

Hope that helps!

-Trev


--
You received this message because you are subscribed to the Google Groups "Accelerate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelerate-hask...@googlegroups.com.
Visit this group at https://groups.google.com/group/accelerate-haskell.
For more options, visit https://groups.google.com/d/optout.

Henning Thielemann

unread,
Jun 6, 2018, 3:01:11 AM6/6/18
to Accelerate

On Tue, 5 Jun 2018, Alexandre Delanoë wrote:

> Hello,
> I am wondering how to write this function with accelerate:
>
> diag :: Acc (Matrix Double) -> Acc (Vector Double)
> diag = undefined

I would implement it using 'backpermute'.

Alexandre Delanoë

unread,
Jun 6, 2018, 4:10:37 AM6/6/18
to accelerat...@googlegroups.com
Le 06 juin 18, vers 09:00, Henning Thielemann ecrivait:
Indeed, here is the solution (done with npouillard, thx to him):

diag :: forall e. Elt e => Acc (Matrix e) -> Acc (Vector e)
diag m = backpermute (indexTail (shape m)) (lift1 (\(Z :. x) -> (Z :. x :. (x :: Exp Int)))) (m :: Acc (Array DIM2 e))

If you have a better solution, I would be glad to know it.
Best regards,
--
Alexandre
Reply all
Reply to author
Forward
0 new messages