On Wed, Jan 26, 2011 at 8:29 PM, Joshua Holbrook <josh.holbrook@gmail.com>wrote:
The only disadvantage I see, is that choosing the axes to operate on in a program or function requires string manipulation.
One possibility would be for the Python exposure to accept lists or tuples of integers. The subscript 'ii' could be [(0,0)], and 'ij,jk->ik' could be [(0,1), (1,2), (0,2)]. Internally it would convert this directly to a C-string to pass to the API function. -Mark
What if you made objects i, j, etc. such that i*j = (0, 1) and etcetera? Maybe you could generate them with something like (i, j, k) = einstein((1, 2, 3)) .
Feel free to disregard me since I haven't really thought too hard about things and might not even really understand what the problem is *anyway*. I'm just trying to help brainstorm. :)
No worries. :) The problem is that someone will probably want to dynamically generate the axes to process in a loop, rather than having them hardcoded beforehand. For example, generalizing the diag function as follows. Within Python, creating lists and tuples is probably more natural. -Mark
def diagij(x, i, j): ... ss = "" ... so = "" ... # should error check i, j ... fill = ord('b') ... for k in range(x.ndim): ... if k in [i, j]: ... ss += 'a' ... else: ... ss += chr(fill) ... so += chr(fill) ... fill += 1 ... ss += '->' + so + 'a' ... return np.einsum(ss, x) ... x = np.arange(3*3*3).reshape(3,3,3) diagij(x, 0, 1) array([[ 0, 12, 24], [ 1, 13, 25], [ 2, 14, 26]])
[np.diag(x[:,:,i]) for i in range(3)] [array([ 0, 12, 24]), array([ 1, 13, 25]), array([ 2, 14, 26])]
diagij(x, 1, 2) array([[ 0, 4, 8], [ 9, 13, 17], [18, 22, 26]])