- #1
- 2,138
- 2,713
While the prefix of the thread is Python, this could be easily generalised to any language.
It is absolutely not the first time I am working with an array, but definitely the first time I am facing the task of defining the transpose of a non-square matrix. I have worked so much with arrays in Java, but unfortunately this simple yet tricky problem never came to my mind.
Numpy has a
Addition:
When I was learning Java, whenever I used any inbuilt function, I made it a habit to read up that documentation of that class, and then the class itself (from NetBeans, I could open the inbuilt classes). Similarly, in PyCharm, I navigated to the Numpy module, and opened the
As seen above, the only statement in the function is
How does this function work? Or am I looking up the wrong function? (I have Python 3.7.4)
It is absolutely not the first time I am working with an array, but definitely the first time I am facing the task of defining the transpose of a non-square matrix. I have worked so much with arrays in Java, but unfortunately this simple yet tricky problem never came to my mind.
Numpy has a
transpose
function. But without using that, how can you define the transpose of a general m x n matrix, where ##m \neq n##?Addition:
When I was learning Java, whenever I used any inbuilt function, I made it a habit to read up that documentation of that class, and then the class itself (from NetBeans, I could open the inbuilt classes). Similarly, in PyCharm, I navigated to the Numpy module, and opened the
_multiarray_umath.py
script. For the transpose
function, I found this:
Python:
def transpose(self, *axes): # real signature unknown; restored from __doc__
"""
a.transpose(*axes)
Returns a view of the array with axes transposed.
For a 1-D array this has no effect, as a transposed vector is simply the
same vector. To convert a 1-D array into a 2D column vector, an additional
dimension must be added. `np.atleast2d(a).T` achieves this, as does
`a[:, np.newaxis]`.
For a 2-D array, this is a standard matrix transpose.
For an n-D array, if axes are given, their order indicates how the
axes are permuted (see Examples). If axes are not provided and
``a.shape = (i[0], i[1], ... i[n-2], i[n-1])``, then
``a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0])``.
Parameters
----------
axes : None, tuple of ints, or `n` ints
* None or no argument: reverses the order of the axes.
* tuple of ints: `i` in the `j`-th place in the tuple means `a`'s
`i`-th axis becomes `a.transpose()`'s `j`-th axis.
* `n` ints: same as an n-tuple of the same ints (this form is
intended simply as a "convenience" alternative to the tuple form)
Returns
-------
out : ndarray
View of `a`, with axes suitably permuted.
See Also
--------
ndarray.T : Array property returning the array transposed.
ndarray.reshape : Give a new shape to an array without changing its data.
Examples
--------
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
[3, 4]])
>>> a.transpose()
array([[1, 3],
[2, 4]])
>>> a.transpose((1, 0))
array([[1, 3],
[2, 4]])
>>> a.transpose(1, 0)
array([[1, 3],
[2, 4]])
"""
pass
pass
.How does this function work? Or am I looking up the wrong function? (I have Python 3.7.4)
Last edited: