Skip to content

Feature request: T method for transposition applying to 1D vectors #9530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jolespin opened this issue Aug 7, 2017 · 10 comments
Closed

Feature request: T method for transposition applying to 1D vectors #9530

jolespin opened this issue Aug 7, 2017 · 10 comments

Comments

@jolespin
Copy link

jolespin commented Aug 7, 2017

Would it be possible to apply the .T syntax to 1-dimensional arrays? Or does this violate some mathematical assumption about transposition and dimensionality?

x = np.linspace(0,1,5)
# array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
x.T
# array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
np.transpose([x])
# array([[ 0.  ],
#        [ 0.25],
#        [ 0.5 ],
#        [ 0.75],
#        [ 1.  ]])

# Future?
x.T
# array([[ 0.  ],
#        [ 0.25],
#        [ 0.5 ],
#        [ 0.75],
#        [ 1.  ]])
@charris
Copy link
Member

charris commented Aug 7, 2017

Transpose doesn't change the number of dimensions, just reverses their order. I think another function would be needed, and the multidimensional case should also be included.

@eric-wieser
Copy link
Member

eric-wieser commented Aug 7, 2017

Yeah, I think what's really needed here is a "transpose last two axes" function (#7495), in which automatic promotion from 1d to 2d seems reasonable

@njsmith
Copy link
Member

njsmith commented Aug 8, 2017

@jolespin: Notice that np.transpose([x]) is not the same as np.transpose(x). In the first case, you're effectively doing np.array([x]) as a (somewhat confusing and non-idiomatic) way to promote x to a 2-dimensional row vector, and then transposing that.

@eric-wieser: So would a 1d array be promoted to a row vector or a column vector before being transposed? If we had an operation to swap the last two axes, then it would make more sense to me if it fails when there aren't two axes to swap. In the face of ambiguity, refuse the temptation to guess, and all that.

@eric-wieser
Copy link
Member

@njsmith You'd promote an (N,) array to a (1, N) (row) array (and then transpose to (N,1) (column)), as broadcasting is always done by adding singular axes to the left. Perhaps this is still too magic, but I would say the choice between row and column is clear

@eric-wieser
Copy link
Member

Also, np.transpose([x]) will bite you if x is already 2d, as that produces a 3d array.

I should draw attention to the edit to my original comment linking to a mostly-duplicate issue, #7495

@njsmith
Copy link
Member

njsmith commented Aug 8, 2017

I don't think broadcasting is relevant here at all. Broadcasting is a way of lining up parallel loops, not a method for arbitrary munging of arrays to meet some shape requirement. Note that np.dot also coerces 1d arrays to 2d, but it does not use the broadcasting rule.

Also, if that's the promotion behavior you want you can already get it by explicitly writing np.atleast_2d(x).T, or if we had some sort of stacked_transpose it could be stacked_transpose(np.atleast_2d(x)).

@shoyer
Copy link
Member

shoyer commented Aug 8, 2017

I don't think broadcasting is relevant here at all. Broadcasting is a way of lining up parallel loops, not a method for arbitrary munging of arrays to meet some shape requirement.

+1

Using the model of generalized ufuncs, the "transpose last two axes" function would have the core dimensions signature (m,n)->(n,m). Following the rules for generalized ufuncs, that means it should error, not broadcast when given 1D inputs. (See #8811 and #5077 for discussion about broadcasting inside core dimensions of gufuncs.)

@mhvk
Copy link
Contributor

mhvk commented Sep 13, 2017

In #8827 (comment), @charris suggested .M[atrix]T[ranspose]=.MT as the name for the possible new property. With such a property, transposing a shape (N,) to (N, 1) would seem reasonable -- the implementation would then be np.swapaxes(atleast_2d(array), -2, -1).

@njsmith
Copy link
Member

njsmith commented Sep 13, 2017

Changing the operation's spelling from a function to a property does not change my opinion on the semantics :-)

@seberg seberg added Project Possible project, may require specific skills and long commitment and removed Project Possible project, may require specific skills and long commitment labels May 9, 2023
@seberg
Copy link
Member

seberg commented Jun 21, 2023

I just merged .mT, so closing.

@seberg seberg closed this as completed Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants