Square root of matrix ?

526 views
Skip to first unread message

Yossi Biton

unread,
Mar 13, 2015, 1:05:00 PM3/13/15
to tor...@googlegroups.com
is there a torch function which computes the square root of a matrix (not an element-wise square root)?
I have a square positive definite matrix A, and i have to compute A^(-0.5).
It's actually the inverse of A^0.5, but i can find a way to compute A^0.5.

soumith

unread,
Mar 13, 2015, 2:49:22 PM3/13/15
to torch7 on behalf of Yossi Biton
Take torch.svd and then take the :sqrt on the eigen values.

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

Yossi Biton

unread,
Mar 14, 2015, 7:27:18 AM3/14/15
to tor...@googlegroups.com
thanks ! 

so if anyone interested this piece of code add the powm method to Tensor class :
function torch.Tensor.powm(self, power)
   local U,D,V = torch.svd(self)
   local result = U*(D:pow(power):diag())*V:t()
   return result
end
can't guarantee it works for all kinds of matrix, but in my case when the matrix is positive definite the call A:powm(-0.5) works fine.

usage example : 
th> A = torch.randn(5,5)
                                                                      [0.0001s]
th> A = A:t()*A -- make A positive semi-definite
                                                                      [0.0000s]
th> A_sqrt = A:powm(0.5)
                                                                      [0.0001s]
th> A
  3.1788  -0.2700   3.5322   0.5113   0.4057
 -0.2700   0.5782  -0.6825   1.6945  -0.1207
  3.5322  -0.6825   5.3633   0.2835  -1.0688
  0.5113   1.6945   0.2835  11.3951  -4.5324
  0.4057  -0.1207  -1.0688  -4.5324   4.4123
[torch.DoubleTensor of dimension 5x5]

                                                                      [0.0002s]
th> A_sqrt*A_sqrt
  3.1788  -0.2700   3.5322   0.5113   0.4057
 -0.2700   0.5782  -0.6825   1.6945  -0.1207
  3.5322  -0.6825   5.3633   0.2835  -1.0688
  0.5113   1.6945   0.2835  11.3951  -4.5324
  0.4057  -0.1207  -1.0688  -4.5324   4.4123
[torch.DoubleTensor of dimension 5x5]



On Friday, March 13, 2015 at 8:49:22 PM UTC+2, smth chntla wrote:
Take torch.svd and then take the :sqrt on the eigen values.

Ajay Talati

unread,
Mar 14, 2015, 8:03:53 AM3/14/15
to tor...@googlegroups.com
Nice :)))

You should get that documented and merged?

Even for really big sparse matrices, I don't know if there's any more efficient or accurate methods better than SVD for square roots ??
Reply all
Reply to author
Forward
0 new messages