Skip to content
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

Support <,>,==,!=,<=,>=,int,bool,not operations on Constant #72

Merged
merged 1 commit into from
Jul 25, 2024

Conversation

arcondello
Copy link
Member

@arcondello arcondello commented Jul 25, 2024

This allows us to treat Constant as a number in many places. And is a step towards ArraySymbol.size() -> Constant, see #50 (comment)

@arcondello arcondello added the enhancement New feature or request label Jul 25, 2024
@arcondello arcondello requested a review from thisac July 25, 2024 17:36
@arcondello arcondello marked this pull request as ready for review July 25, 2024 17:37
Copy link
Contributor

@thisac thisac left a comment

Choose a reason for hiding this comment

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

LGTM, just a couple of minor questions.

if not self._is_scalar():
raise ValueError("the truth value of a constant with more than one element is ambiguous")

return <bool>deref(self.ptr.buff())
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the buff() method point at the first (only in this case) location in memory? What is the difference between what's returned by self.ptr (I assume this is a pythonic pointer object and not actually a memory address pointer?) and self.ptr.buff()?

Copy link
Member Author

Choose a reason for hiding this comment

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

self.ptr is a C++ pointer to a ConstantNode. That ConstantNode has a method buff() that returns a pointer to the beginning of the values. So this line is equivalent to static_cast<bool>(*(ptr->buff())).

Comment on lines +844 to +846
# https://stackoverflow.com/q/1521607 for the integer test
cdef double dummy
return modf(deref(self.ptr.buff()), &dummy) == <double>0.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting solution.

Comment on lines +819 to +821
# We could also check if rhs is another Constant and handle that differently,
# but that might lead to confusing behavior so we treat other Constants the
# same as any other symbol.
Copy link
Contributor

Choose a reason for hiding this comment

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

In what was should/could it be handled differently (unless not deferring to NumPy at all)? NumPy would just use rhs as if a NumPy array either way since it's being deferred there.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand the question

Copy link
Contributor

Choose a reason for hiding this comment

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

We could also check if rhs is another Constant and handle that differently

Just responding to this comment. Curious if there's anything that should or could be handled differently.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh. The alternative would be, when both sides of the operation are Constants, to have the result be another Constant or even to "demote" to a NumPy array.

Comment on lines +836 to +837
else:
return NotImplemented # this should never happen, but just in case
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
else:
return NotImplemented # this should never happen, but just in case
return NotImplemented # this should never happen, but just in case

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the nesting makes it clearer that it's because those are the only options.

operator.ne
]

for op in operators:
Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) It would've been nice to have this parameterized and test each operator separately.

Copy link
Member Author

@arcondello arcondello Jul 25, 2024

Choose a reason for hiding this comment

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

Isn't that what subTest does? Without needing an additional dependency.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, true. I somehow missed that line, and running the test using pytest only shows this as a single test.

Copy link
Contributor

Choose a reason for hiding this comment

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

pytest-subtests required to make pytest work with subTest. Good to know!

@arcondello arcondello merged commit 4ca820e into dwavesystems:main Jul 25, 2024
32 checks passed
@arcondello arcondello deleted the array-operations branch July 25, 2024 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants