I want to calculate the determinant of mm subarrays of a mm*n dimensional arrays, and would like to do this in a fast/more elegant way. The brute-force approach works:
import numpy as n
array=n.array([[[0.,1.,2.,3.],[2,1,1,0]],[[0.5, 0.5,2,2],[0.5,1,0,2]]])
detarray=n.zeros(4)
for i in range(4):
detarray[i]= n.linalg.det(array[:,:,i])
I would have tried doing this with apply_along_axis, but I know this is only for 1D arguments to the function, and so I presume I can't get this to work.
However, I thought apply_over_axes should also work:
n.apply_over_axes(n.linalg.det, array, [0,1])
but this gives me an error: "det() takes exactly 1 argument (2 given)"
Does anyone know why this doesn't work? If this type of calculation is really not possible with apply_over_axes, is there a better way to do this rather than the for-loop?
apply_over_axes
'iterates' on a list of axes. Not over an axis but over the dimensions. Say you have a 3d array, and want to sum on axes 1 and 2. Nowsum
takes a tuple, but say it didn't. You'd have to sum on 2 and then on 1. – hpaulj