Skip to content

Add comparison operators #475

Description

@istmarc

Hello,
I've noticed that comparison operators are missing from the ndslice implementation. If possible I would like to add support for the following comparison operators:

  • Greater than: gt or >
  • Greater or equal than: ge
  • Less than: lt or <
  • Less or equal than: le
  • Equal: ==
  • Different: !=

Activity

  1. jmh530 commented on Aug 21, 2025

    @jmh530
    Contributor

    opEquals is already defined for mir Slices.

    For opCmp, the only thing that makes sense is going element by element (edit: meaning you want to know whether or not data at each location in one array is bigger or smaller than in the other, meaning the shape of the output should match the input), which means a map operation. I think the reason it is not defined is because if you want to return a new slice then it implies an allocation. But you don't always need to do that so the API prefers to let the user decide when they actually need to.

  2. istmarc commented on Aug 21, 2025

    @istmarc
    Author

    Yeah I'm interested in comparing element by element. The shape and the type should be the same.
    Will you be interested in having the operations that allocate by default. When the user doesn't want allocations he can allocate his own slice and do a for loop, for example the following:

    // Allocated slice a, b and c
    foreach(i; 0..a.elementCount) {
      c.iterator[i] = a.iterator[i] > b.iterator[i];
    }

    Or by default we can have maybe the following code that allocate.

    // Allocated slice a and b
    auto c = a > b;
    // or auto c = a.gt(b);

    Please let me know if that's of interest for mir developpers or if by design it shouldn't have allocation then that's fair.

  3. 9il commented on Aug 21, 2025

    @9il
    Member
    import mir.ndslice: zip, slice, map;
    // this kind of API is more common for scripting languages.
    alias gt = (a, b) => zip(a, b).map!"a < b"; 
    auto c = a.gt(b); // lazy
    auto d = c.slice; // allocated
  4. 9il commented on Aug 21, 2025

    @9il
    Member

    mir is slightly low level for this

  5. istmarc commented on Aug 21, 2025

    @istmarc
    Author
    import mir.ndslice: zip, slice, map;
    // this kind of API is more common for scripting languages.
    alias gt = (a, b) => zip(a, b).map!"a < b"; 
    auto c = a.gt(b); // lazy
    auto d = c.slice; // allocated

    Thanks a lot for your reply, it does exactly what I need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions