Skip to content

Add the transpose function for SimpleArray#509

Merged
yungyuc merged 1 commit into
solvcon:masterfrom
ThreeMonth03:issue506
Apr 18, 2025
Merged

Add the transpose function for SimpleArray#509
yungyuc merged 1 commit into
solvcon:masterfrom
ThreeMonth03:issue506

Conversation

@ThreeMonth03
Copy link
Copy Markdown
Collaborator

According to issue #506 , I implement the void SimpleArray::reverse() and wrap it to the python.

@ThreeMonth03 ThreeMonth03 force-pushed the issue506 branch 3 times, most recently from 55a3613 to 292506b Compare April 17, 2025 18:34
@yungyuc yungyuc added the array Multi-dimensional array implementation label Apr 18, 2025
Copy link
Copy Markdown
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good. One minor enhancement to the tests and their readability:

  • Inner function name

Comment thread tests/test_buffer.py Outdated
self.assertEqual(sarr_clone[3], 2.0) # should be the original value

def test_SimpleArray_transpose(self):
def check(sarr, ndarr):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename the inner function check to check_identical for readability.

Comment thread tests/test_buffer.py Outdated
sarr = sarr.transpose(inplace=False)
check(sarr, ndarrT)

ndarrT = ndarr.transpose(0, 2, 1, 3)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, numpy is so .... confusing. I grow more fond of SimpleArray.

Comment thread tests/test_buffer.py Outdated

sarr = modmesh.SimpleArrayFloat64(array=ndarr)
sarr.transpose()
check(sarr, ndarrT)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also test sarr.transpose().copy()?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default paremeter bool inplace is true, and it would return nothing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be an issue. In that case, there will not be an equivalent of ndarr.transpose.copy().

Let's give it more discussions and revise in a new PR.

Copy link
Copy Markdown
Collaborator Author

@ThreeMonth03 ThreeMonth03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yungyuc You could review the pull request when you are available.

Comment on lines +673 to +680
if (axis[it] >= m_shape.size() || axis[it] < 0)
{
throw std::runtime_error("SimpleArray: axis out of range");
}
if (new_shape[it] != -1)
{
throw std::runtime_error("SimpleArray: axis already set");
}
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the small_vector axis; is the permutation of the sequence from 0 to n - 1.

{
self_copy.transpose(make_shape(axis));
}
return py::cast(std::move(self_copy));
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why the parameter of py::cast() should be rvalue.

{ return self.reshape(make_shape(shape)); })
.def(
"transpose",
[](wrapped_type & self, py::object axis, bool inplace) -> py::object
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to deal with the in-place function, the return type of transpose() is py::object.

{ return self.reshape(make_shape(shape)); })
.def(
"transpose",
[](wrapped_type & self, py::object const & axis, bool const & inplace) -> py::object
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to return None when inplace = True, the return type could be py::object.

Comment thread tests/test_buffer.py Outdated

sarr = modmesh.SimpleArrayFloat64(array=ndarr)
sarr.transpose()
check(sarr, ndarrT)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default paremeter bool inplace is true, and it would return nothing.

Comment thread tests/test_buffer.py
Comment on lines +242 to +244
noarr = sarr.transpose()
check_identical(sarr, ndarrT)
self.assertEqual(noarr, None)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of in-place transpose is None.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. It's OK to leave it behaving like this right now. But I think we will later need to consider how to align the returning behaviors for various array functions that @Tommy-Hsu is planning in https://docs.google.com/document/d/1r6ESbTZPtzmYpnx-sepgp7kXWwwM48XoeVny0dqrg8s .

Also cc @KHLee529 . We will have a lot of fun code to write.

Copy link
Copy Markdown
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGFN

Comment thread tests/test_buffer.py
Comment on lines +242 to +244
noarr = sarr.transpose()
check_identical(sarr, ndarrT)
self.assertEqual(noarr, None)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. It's OK to leave it behaving like this right now. But I think we will later need to consider how to align the returning behaviors for various array functions that @Tommy-Hsu is planning in https://docs.google.com/document/d/1r6ESbTZPtzmYpnx-sepgp7kXWwwM48XoeVny0dqrg8s .

Also cc @KHLee529 . We will have a lot of fun code to write.

Comment thread tests/test_buffer.py Outdated

sarr = modmesh.SimpleArrayFloat64(array=ndarr)
sarr.transpose()
check(sarr, ndarrT)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be an issue. In that case, there will not be an equivalent of ndarr.transpose.copy().

Let's give it more discussions and revise in a new PR.

@yungyuc yungyuc merged commit 4e4373d into solvcon:master Apr 18, 2025
12 checks passed
@yungyuc yungyuc linked an issue Apr 18, 2025 that may be closed by this pull request
@yungyuc
Copy link
Copy Markdown
Member

yungyuc commented Apr 18, 2025

See the follow-up issue #511

@ThreeMonth03 ThreeMonth03 deleted the issue506 branch April 19, 2025 03:07
@yungyuc yungyuc linked an issue Apr 22, 2025 that may be closed by this pull request
@yungyuc yungyuc changed the title Add the transpose function for SimpleArray. Add the transpose function for SimpleArray Oct 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

array Multi-dimensional array implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants