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

已查看 15 次
跳至第一个未读帖子

Alexandre Delanoë

未读,
2018年6月5日 19:46:532018/6/5
收件人 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

未读,
2018年6月5日 20:03:042018/6/5
收件人 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

未读,
2018年6月6日 03:01:112018/6/6
收件人 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ë

未读,
2018年6月6日 04:10:372018/6/6
收件人 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
回复全部
回复作者
转发
0 个新帖子