How can I transpose a matrix in Groovy?
I need transpose an array from rows into cols and cols into rows
def mtrx = [
[1,2,3],
[4,5,6]
]
//mtrx.anyMethod()
//expected开发者_StackOverflow社区 result
//[[1,4],[2,5],[3,6]]
Do you know a direct method from it?
I don't know a Groovy method that transposes this case; if you know another way to transpose please tell me it.
Groovy lists have a transpose()
method:
def transposed = mtrx.transpose()
It might be a bit of overkill for what you need, but the Apache Commons Math library includes a comprehensive linear algebra module. The user's guide for the linear algebra module includes a simple matrix multiplication example. You can include this module in your Grails project by adding:
runtime ('org.apache.commons','commons-math','2.2')
to your BuildConfig.groovy file.
精彩评论