Beispiel #1
0
    def random_element(self, num_bound=None, den_bound=None, *args, **kwds):
        """
        Return an random element of `\QQ`.

        EXAMPLES::

            sage: QQ.random_element(10,10)
            1/4

        Passes extra positional or keyword arguments through::

            sage: QQ.random_element(10,10, distribution='1/n')
            -1

        """
        global ZZ
        if ZZ is None:
            import integer_ring
            ZZ = integer_ring.ZZ
        if num_bound == None:
            num = ZZ.random_element(*args, **kwds)
            den = ZZ.random_element(*args, **kwds)
            while den == 0: den = ZZ.random_element(*args, **kwds)
            return self((num, den))
        else:
            if num_bound == 0:
                num_bound = 2
            if den_bound is None:
                den_bound = num_bound
                if den_bound < 1:
                    den_bound = 2
            num = ZZ.random_element(-num_bound, num_bound+1, *args, **kwds)
            den = ZZ.random_element(1, den_bound+1, *args, **kwds)
            while den == 0: den = ZZ.random_element(1, den_bound+1, *args, **kwds)
            return self((num,den))
Beispiel #2
0
    def random_element(self, num_bound=None, den_bound=None, *args, **kwds):
        """
        EXAMPLES::
        
            sage: QQ.random_element(10,10)
            1/4

        Passes extra positional or keyword arguments through::
        
            sage: QQ.random_element(10,10, distribution='1/n')
            -1

        """
        global ZZ
        if ZZ is None:
            import integer_ring
            ZZ = integer_ring.ZZ
        if num_bound == None:
            num = ZZ.random_element(*args, **kwds)
            den = ZZ.random_element(*args, **kwds)
            while den == 0:
                den = ZZ.random_element(*args, **kwds)
            return self((num, den))
        else:
            if num_bound == 0:
                num_bound = 2
            if den_bound is None:
                den_bound = num_bound
                if den_bound < 1:
                    den_bound = 2
            num = ZZ.random_element(-num_bound, num_bound + 1, *args, **kwds)
            den = ZZ.random_element(1, den_bound + 1, *args, **kwds)
            while den == 0:
                den = ZZ.random_element(1, den_bound + 1, *args, **kwds)
            return self((num, den))
Beispiel #3
0
    def random_element(self, num_bound=None, den_bound=None, *args, **kwds):
        """
        Return an random element of `\QQ`.

        Elements are constructed by randomly choosing integers
        for the numerator and denominator, not neccessarily coprime.

        INPUT:

        -  ``num_bound`` -- a positive integer, specifying a bound
           on the absolute value of the numerator.
           If absent, no bound is enforced.

        -  ``den_bound`` -- a positive integer, specifying a bound
           on the value of the denominator.
           If absent, the bound for the numerator will be reused.

        Any extra positional or keyword arguments are passed through to
        :meth:`sage.rings.integer_ring.IntegerRing_class.random_element`.

        EXAMPLES::

            sage: QQ.random_element()
            -4
            sage: QQ.random_element()
            0
            sage: QQ.random_element()
            -1/2

        In the following example, the resulting numbers range from
        -5/1 to 5/1 (both inclusive),
        while the smallest possible positive value is 1/10::

            sage: QQ.random_element(5, 10)
            -2/7

        Extra positional or keyword arguments are passed through::

            sage: QQ.random_element(distribution='1/n')
            0
            sage: QQ.random_element(distribution='1/n')
            -1

        """
        global ZZ
        if ZZ is None:
            import integer_ring
            ZZ = integer_ring.ZZ
        if num_bound is None:
            num = ZZ.random_element(*args, **kwds)
            den = ZZ.random_element(*args, **kwds)
            while den == 0: den = ZZ.random_element(*args, **kwds)
            return self((num, den))
        else:
            if num_bound == 0:
                num_bound = 2
            if den_bound is None:
                den_bound = num_bound
                if den_bound < 1:
                    den_bound = 2
            num = ZZ.random_element(-num_bound, num_bound+1, *args, **kwds)
            den = ZZ.random_element(1, den_bound+1, *args, **kwds)
            while den == 0: den = ZZ.random_element(1, den_bound+1, *args, **kwds)
            return self((num,den))
Beispiel #4
0
    def random_element(self, num_bound=None, den_bound=None, *args, **kwds):
        """
        Return an random element of `\QQ`.

        Elements are constructed by randomly choosing integers
        for the numerator and denominator, not neccessarily coprime.

        INPUT:

        -  ``num_bound`` -- a positive integer, specifying a bound
           on the absolute value of the numerator.
           If absent, no bound is enforced.

        -  ``den_bound`` -- a positive integer, specifying a bound
           on the value of the denominator.
           If absent, the bound for the numerator will be reused.

        Any extra positional or keyword arguments are passed through to
        :meth:`sage.rings.integer_ring.IntegerRing_class.random_element`.

        EXAMPLES::

            sage: QQ.random_element()
            -4
            sage: QQ.random_element()
            0
            sage: QQ.random_element()
            -1/2

        In the following example, the resulting numbers range from
        -5/1 to 5/1 (both inclusive),
        while the smallest possible positive value is 1/10::

            sage: QQ.random_element(5, 10)
            -2/7

        Extra positional or keyword arguments are passed through::

            sage: QQ.random_element(distribution='1/n')
            0
            sage: QQ.random_element(distribution='1/n')
            -1

        """
        global ZZ
        if ZZ is None:
            import integer_ring
            ZZ = integer_ring.ZZ
        if num_bound is None:
            num = ZZ.random_element(*args, **kwds)
            den = ZZ.random_element(*args, **kwds)
            while den == 0:
                den = ZZ.random_element(*args, **kwds)
            return self((num, den))
        else:
            if num_bound == 0:
                num_bound = 2
            if den_bound is None:
                den_bound = num_bound
                if den_bound < 1:
                    den_bound = 2
            num = ZZ.random_element(-num_bound, num_bound + 1, *args, **kwds)
            den = ZZ.random_element(1, den_bound + 1, *args, **kwds)
            while den == 0:
                den = ZZ.random_element(1, den_bound + 1, *args, **kwds)
            return self((num, den))