Skip to content

Commit

Permalink
[Lang] Matrix lib: Stop changing dimension in transpose() (taichi-dev…
Browse files Browse the repository at this point in the history
…#6528)

Issue: taichi-dev#5819

### Brief Summary

This PR enforces that `vector.transpose()` returns a vector while
`matrix.transpose()` returns a matrix, which mimics the behavior of
numpy.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and quadpixels committed May 13, 2023
1 parent bf74167 commit 23aab1d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 43 deletions.
15 changes: 0 additions & 15 deletions python/taichi/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ def randn(dt=None):
return _randn(dt)


@pyfunc
def _matrix_transpose(mat):
"""Permute the first two axes of the matrix.
Args:
mat (:class:`~taichi.lang.matrix.Matrix`): Input matrix.
Returns:
Transpose of the input matrix.
"""
return matrix.Matrix([[mat(i, j) for i in range(mat.n)]
for j in range(mat.m)],
ndim=mat.ndim)


@pyfunc
def _matrix_cross3d(self, other):
return matrix.Matrix([
Expand Down
34 changes: 6 additions & 28 deletions python/taichi/lang/matrix_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,36 +123,14 @@ def determinant(x):
return None


def _vector_transpose(v):
shape = v.get_shape()
if isinstance(v, Vector):

@pyfunc
def _transpose():
return Matrix([[v[i] for i in static(range(shape[0]))]], ndim=1)

return _transpose()
if isinstance(v, Matrix):

@pyfunc
def _transpose():
return Vector([v[i, 0] for i in static(range(shape[0]))])

return _transpose()
return v


@preconditions(assert_tensor)
@func
def transpose(m):
shape = static(m.get_shape())
@pyfunc
def transpose(mat):
shape = static(mat.get_shape())
if static(len(shape) == 1):
return _vector_transpose(m)
result = _init_matrix((shape[1], shape[0]), dt=m.element_type())
for i in static(range(shape[0])):
for j in static(range(shape[1])):
result[j, i] = m[i, j]
return result
return Vector([mat[i] for i in static(range(shape[0]))])
return Matrix([[mat[i, j] for i in static(range(shape[0]))]
for j in static(range(shape[1]))])


@preconditions(arg_at(0, is_int_const),
Expand Down

0 comments on commit 23aab1d

Please sign in to comment.