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

Polyval handles non-array as second argument #601

Merged
merged 3 commits into from
Jun 27, 2023

Conversation

HugoNumworks
Copy link
Contributor

This would allow the evaluation of a polynomial at single value, as allowed in numpy.
https://numpy.org/doc/stable/reference/generated/numpy.polyval.html

}
if(!ndarray_object_is_array_like(o_x)) {
// Try to convert o_x to a list
o_x = mp_obj_new_list(1, &o_x);
Copy link
Owner

Choose a reason for hiding this comment

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

What happens, if o_x can't be converted to a list?

@v923z
Copy link
Owner

v923z commented Apr 24, 2023

Whenever there is a user-facing change, increment the version number in

#define ULAB_VERSION 6.0.7
. This would be 6.0.8 now.

@v923z
Copy link
Owner

v923z commented Apr 25, 2023

I'm trying to understand what this PR wants to solve: you call the function

bool ndarray_object_is_array_like(mp_obj_t o_in) {
which basically tells you, whether an object is iterable. If so, then you iterate over it in
mp_obj_t x_item, x_iterable = mp_getiter(o_x, &x_buf);
while ((x_item = mp_iternext(x_iterable)) != MP_OBJ_STOP_ITERATION) {
mp_float_t _x = mp_obj_get_float(x_item);
mp_float_t y = p[0];
for(uint8_t j=0; j < plen-1; j++) {
y *= _x;
y += p[j+1];
}
*array++ = y;
}
Why is a conversion to a list going to help?

@HugoNumworks
Copy link
Contributor Author

I'm trying to understand what this PR wants to solve: you call the function

I just needed Polyval to handle scalar and not only lists. Otherwise,

np.polyval([3,0,1], 5)

raises a inputs are not iterable message.
Converting to a list beforehand was an easy way to handle np.polyval([3,0,1], 5) as if the user inputted np.polyval([3,0,1], [5])

The big flaw is that it returns a list in both case which is not Numpy's behavior, so I have to rework this anyway.

@v923z
Copy link
Owner

v923z commented Apr 26, 2023

You could call the

ndarray_obj_t *ndarray_from_mp_obj(mp_obj_t obj, uint8_t other_type) {
function in that case.

@HugoNumworks
Copy link
Contributor Author

@v923z , thanks for the suggestion.
I went with a different implementation where I no longer need to convert to a list.
Now a scalar is returned if the second argument is a scalar.
I also incremented the version number and factorized a method.
I Hope this helps !

if(!ndarray_object_is_array_like(o_x)) {
return mp_obj_new_float(poly_eval(mp_obj_get_float(o_x), p, plen));
}

Copy link
Owner

Choose a reason for hiding this comment

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

What happens, if the input is not array-like, but not a float, either? E.g., what do you do with a complex scalar?

Copy link
Contributor Author

@HugoNumworks HugoNumworks Apr 26, 2023

Choose a reason for hiding this comment

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

It will happen the same as if the input is array-like, with non-float elements: a type error such as

TypeError: can't convert complex to float

@v923z v923z merged commit 112d4f8 into v923z:master Jun 27, 2023
16 checks passed
@HugoNumworks HugoNumworks deleted the polyval-second-argument-array branch June 28, 2023 09:34
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

Successfully merging this pull request may close these issues.

None yet

2 participants