Skip to content

overhaul our mod functionality #2519

Closed

Description

@stress-tess

This has been edited to reflect new requirements for this issues the original is below

Right now our mod is incorrect for large scalars uint/float and for edge cases in the modulus (+/-np.inf, '+/-0.0`)

Attempting to update it to fmod correctly handles large scalars but is not incorrect on negative floats and edge cases both being modded and doing the modding.

In this issue we should:

  • add ak.fmod which matches np.fmod using the chpl fmod function
  • update ak.mod to match np.mod by doing something similar to floorDivisionHelper, by cleverly modifying the return of the chpl fmod

ORIGINAL:
While working #2508, I found the section of test_divmod that says it's putting the edge cases in the denominator doesn't do that and is identical to the numerator

arkouda/tests/stats_test.py

Lines 235 to 251 in 2034dcb

# Edge cases in the numerator
edge_case = [-np.inf, -7.0, -0.0, np.nan, 0.0, 7.0, np.inf]
np_edge_case = np.array(edge_case)
ak_edge_case = ak.array(np_edge_case)
ak_div, ak_mod = ak.divmod(ak_edge_case, ak.arange(1, len(edge_case)+1))
np_div, np_mod = np.divmod(np_edge_case, np.arange(1, len(edge_case)+1))
self.assertTrue(np.allclose(ak_div.to_ndarray(), np_div, equal_nan=True))
self.assertTrue(np.allclose(ak_mod.to_ndarray(), np_mod, equal_nan=True))
# Edge cases in the denominator
edge_case = [-np.inf, -7.0, np.nan, 7.0, np.inf]
np_edge_case = np.array(edge_case)
ak_edge_case = ak.array(np_edge_case)
ak_div, ak_mod = ak.divmod(ak_edge_case, ak.arange(1, len(edge_case)+1))
np_div, np_mod = np.divmod(np_edge_case, np.arange(1, len(edge_case)+1))
self.assertTrue(np.allclose(ak_div.to_ndarray(), np_div, equal_nan=True))
self.assertTrue(np.allclose(ak_mod.to_ndarray(), np_mod, equal_nan=True))

I ran some quick tests on master and mod behaves incorrectly with the float edge cases in the denominator

>>> edge_case = [-np.inf, -7.0, np.nan, 7.0, np.inf]
>>> np_edge_case = np.array(edge_case)
>>> ak_edge_case = ak.array(np_edge_case)
>>> ak_ind = ak.arange(1, len(edge_case)+1)
>>> np_ind = np.arange(1, len(edge_case)+1)

# edge cases in numerator (behaves correctly)
>>> ak_edge_case % ak_ind
array([nan 1 nan 3 nan])

>>> np_edge_case % np_ind
array([nan,  1., nan,  3., nan])

# edge cases in denominator (incorrect results)
>>> ak_ind % ak_edge_case
array([nan -5 nan 4 nan])

>>> np_ind % np_edge_case
array([-inf,  -5.,  nan,   4.,   5.])

Activity

  1. stress-tess commented on Jun 21, 2023

    @stress-tess
    MemberAuthor

    In the issue in which this was discovered, we are switching mod to use fmod. While doing this I encountered different behavior for negative floats.

    It seems like it's a well documented thing and % and fmod are meant to behave differently. There's an fmod function both in python's math module and in numpy for this reason.

    I think we should have a mod and fmod as well to match numpy since these do different things
    np.mod

    Computes the remainder complementary to the floor_divide function. It is equivalent to the Python modulus operatorx1 % x2 and has the same sign as the divisor x2. The MATLAB function equivalent to np.remainder is mod.

    np.fmod

    This is the NumPy implementation of the C library function fmod, the remainder has the same sign as the dividend x1. It is equivalent to the Matlab(TM) rem function and should not be confused with the Python modulus operator x1 % x2.

    Ideally these would map one-to-one onto the chapel mod and fmod functions. I added a comment expressing this to the issue i posted in their repo

  2. stress-tess commented on Jun 21, 2023

    @stress-tess
    MemberAuthor

    More information about the different definitions can be found here

    python and numpy's %/mod uses the floored definition and their fmod uses the truncated definition. I asked the chapel folks what definition their mod is using

  3. changed the title [-]incorrect results for `mod` with float edge_cases[/-] [+]incorrect results for `mod` with float edge_cases and negatives[/+] on Jun 22, 2023
  4. stress-tess commented on Jun 22, 2023

    @stress-tess
    MemberAuthor

    playing around with this a bit with @jaketrookman, it looks like a lot of the time mod is fmod + modulus with some exceptions (maybe when evenly divides). Theres some other things like how the sign is determined. hopefully we can look at the definitions and try to find an easy way to convert the results from fmod into what we expect from mod

  5. changed the title [-]incorrect results for `mod` with float edge_cases and negatives[/-] [+]overhaul our `mod` functionality[/+] on Jun 22, 2023
  6. added
    bug
    on Jun 23, 2023
  7. added a commit that references this issue on Jun 23, 2023
    ae92d29
  8. added a commit that references this issue on Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions