Code Monkey home page Code Monkey logo

Comments (8)

AlexandreDecan avatar AlexandreDecan commented on June 22, 2024 1

This has already been proposed but (temporarily) rejected, see #42 #60 #61.

from portion.

AlexandreDecan avatar AlexandreDecan commented on June 22, 2024 1

Yes, intervals are immutable so any property can be computed and cached at creation time.

from portion.

ozeasx avatar ozeasx commented on June 22, 2024

Just to weigh in, interval length is mathematically well-defined for any type of interval. The length of a real interval is usually defined as b - a, for both closed and open intervals since endpoints have no size. The question is which value to assign to unbounded intervals. It could be +inf or undefined depending on the domain. In any case, I don't see a loss of generality by implementing such a property.

I am aware that portion does not support discrete intervals explicitly. But for discrete intervals, the size of an interval is its set order. If the interval is open, the open endpoint is not part of the set of course.

Interval size plays an important role in integration theory and real analysis. However, my use case scenario is scheduling applications.

Thank you,

from portion.

AlexandreDecan avatar AlexandreDecan commented on June 22, 2024

What would be the length of the empty interval, without loss of generality about the objects being used as bounds?

Notice that if you really need to compute the length of (numerical) intervals, you can already define your own function that accepts an interval and returns its length, or you can subclass the Interval class to add such a property. See the (not-yet-released) recent changes we made https://github.com/AlexandreDecan/portion#specialize--customize-intervals (in particular, the create_api function that makes it easy to keep the existing API while subclassing intervals).

from portion.

ozeasx avatar ozeasx commented on June 22, 2024

Just like unbounded intervals, the length of an empty interval could be 0 or undefined, depending on the domain. A possible solution to keep the generality would be to define a default value for these special cases, but let the user change it.

In any case, thanks for the suggestion. I'm subclassing Interval but the final notation is still a bit cumbersome since I use the function closedopen to create the intervals I work with.

I did it like this:

def set_size(self):
    size = 0
    for i in self:
        if isinstance(i.upper, int) and isinstance(i.lower, int):
            size += i.upper - i.lower

    self.size = size

Many thanks

from portion.

ozeasx avatar ozeasx commented on June 22, 2024

I only need bounded and non-empty closedopen intervals, so I ended up doing this. Everything seems to be working as expected for my use case. This way I do not need to import portion to use the function closedopen, just this module. Please let me know if this can break something else though. It is not a suggestion in any way, just a solution I found to my scenario.

import portion as P

class Interval(P.Interval):
    def __init__(self, *intervals):
        if len(intervals) == 2 and all(isinstance(x, int) for x in intervals):
            super().__init__(P.closedopen(*intervals))
        else:
            super().__init__(*intervals)

        self.set_size()


    def set_size(self):
        size = 0
        for i in self:
            if isinstance(i.upper, int) and isinstance(i.lower, int):
                size += i.upper - i.lower

        self.size = size

from portion.

AlexandreDecan avatar AlexandreDecan commented on June 22, 2024

Hi. I think that's ok w.r.t. breaking changes. However, I don't think it's a good idea to create atomic intervals using the constructor of the Interval class. Is the sole purpose to accept integers is to avoid to write a call to P.closedopen, then why not create an alias for this function? (e.g., c = partial(P.closedopen, klass=Interval))

from portion.

ozeasx avatar ozeasx commented on June 22, 2024

That's probably better, thank you.
Would it be possible to define size when the interval is created?
I would like to avoid calculating it at every size call.

from portion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.