Python numpy.base_repr() Examples

The following are 30 code examples for showing how to use numpy.base_repr(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: cellpylib   Author: lantunes   File: ca_functions.py    License: Apache License 2.0 6 votes vote down vote up
def totalistic_rule(neighbourhood, k, rule):
    """
    The totalistic rule as described in NKS. The average color is mapped to a whole number in [0, k - 1].
    The rule number is in base 10, but interpreted in base k. For a 1-dimensional cellular automaton, there are
    3k - 2 possible average colors in the 3-cell neighbourhood. There are n(k - 1) + 1 possible average colors for a 
    k-color cellular automaton with an n-cell neighbourhood.
    :param neighbourhood: a k-color array of any size
    :param k: the number of colors in this cellular automaton, where only 2 <= k <= 36 is supported
    :param rule: the k-color cellular automaton rule number in base 10, interpreted in base k
    :return: the result, a number from 0 to k - 1, of applying the given rule on the given state
    """
    # e.g. np.base_repr(777, base=3) -> '1001210'; the zfill pads the string with zeroes: '1'.zfill(3) -> '001'
    #   Bases greater than 36 not handled in base_repr.
    n = neighbourhood.size
    rule_string = np.base_repr(rule, base=k).zfill(n*(k - 1) + 1)
    if len(rule_string) > n*(k - 1) + 1:
        raise Exception("rule number out of range")
    neighbourhood_sum = np.sum(neighbourhood)
    # the rightmost element of the rule is for the average color 0, in NKS convention
    return int(rule_string[n*(k - 1) - neighbourhood_sum], k) 
Example 2
Project: recruit   Author: Frank-qlu   File: test_numeric.py    License: Apache License 2.0 5 votes vote down vote up
def test_base3(self):
        assert_equal(np.base_repr(3**5, 3), '100000') 
Example 3
Project: recruit   Author: Frank-qlu   File: test_numeric.py    License: Apache License 2.0 5 votes vote down vote up
def test_positive(self):
        assert_equal(np.base_repr(12, 10), '12')
        assert_equal(np.base_repr(12, 10, 4), '000012')
        assert_equal(np.base_repr(12, 4), '30')
        assert_equal(np.base_repr(3731624803700888, 36), '10QR0ROFCEW') 
Example 4
Project: recruit   Author: Frank-qlu   File: test_numeric.py    License: Apache License 2.0 5 votes vote down vote up
def test_negative(self):
        assert_equal(np.base_repr(-12, 10), '-12')
        assert_equal(np.base_repr(-12, 10, 4), '-000012')
        assert_equal(np.base_repr(-12, 4), '-30') 
Example 5
Project: recruit   Author: Frank-qlu   File: test_numeric.py    License: Apache License 2.0 5 votes vote down vote up
def test_base_range(self):
        with assert_raises(ValueError):
            np.base_repr(1, 1)
        with assert_raises(ValueError):
            np.base_repr(1, 37) 
Example 6
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base3(self):
        assert_equal(np.base_repr(3**5, 3), '100000') 
Example 7
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_positive(self):
        assert_equal(np.base_repr(12, 10), '12')
        assert_equal(np.base_repr(12, 10, 4), '000012')
        assert_equal(np.base_repr(12, 4), '30')
        assert_equal(np.base_repr(3731624803700888, 36), '10QR0ROFCEW') 
Example 8
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_negative(self):
        assert_equal(np.base_repr(-12, 10), '-12')
        assert_equal(np.base_repr(-12, 10, 4), '-000012')
        assert_equal(np.base_repr(-12, 4), '-30') 
Example 9
Project: vnpy_crypto   Author: birforce   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base3(self):
        assert_equal(np.base_repr(3**5, 3), '100000') 
Example 10
Project: vnpy_crypto   Author: birforce   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_positive(self):
        assert_equal(np.base_repr(12, 10), '12')
        assert_equal(np.base_repr(12, 10, 4), '000012')
        assert_equal(np.base_repr(12, 4), '30')
        assert_equal(np.base_repr(3731624803700888, 36), '10QR0ROFCEW') 
Example 11
Project: vnpy_crypto   Author: birforce   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_negative(self):
        assert_equal(np.base_repr(-12, 10), '-12')
        assert_equal(np.base_repr(-12, 10, 4), '-000012')
        assert_equal(np.base_repr(-12, 4), '-30') 
Example 12
Project: vnpy_crypto   Author: birforce   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base_range(self):
        with assert_raises(ValueError):
            np.base_repr(1, 1)
        with assert_raises(ValueError):
            np.base_repr(1, 37) 
Example 13
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base3(self):
        assert_equal(np.base_repr(3**5, 3), '100000') 
Example 14
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_positive(self):
        assert_equal(np.base_repr(12, 10), '12')
        assert_equal(np.base_repr(12, 10, 4), '000012')
        assert_equal(np.base_repr(12, 4), '30')
        assert_equal(np.base_repr(3731624803700888, 36), '10QR0ROFCEW') 
Example 15
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_negative(self):
        assert_equal(np.base_repr(-12, 10), '-12')
        assert_equal(np.base_repr(-12, 10, 4), '-000012')
        assert_equal(np.base_repr(-12, 4), '-30') 
Example 16
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base_range(self):
        with assert_raises(ValueError):
            np.base_repr(1, 1)
        with assert_raises(ValueError):
            np.base_repr(1, 37) 
Example 17
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base3(self):
        assert_equal(np.base_repr(3**5, 3), '100000') 
Example 18
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_positive(self):
        assert_equal(np.base_repr(12, 10), '12')
        assert_equal(np.base_repr(12, 10, 4), '000012')
        assert_equal(np.base_repr(12, 4), '30')
        assert_equal(np.base_repr(3731624803700888, 36), '10QR0ROFCEW') 
Example 19
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_negative(self):
        assert_equal(np.base_repr(-12, 10), '-12')
        assert_equal(np.base_repr(-12, 10, 4), '-000012')
        assert_equal(np.base_repr(-12, 4), '-30') 
Example 20
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base_range(self):
        with assert_raises(ValueError):
            np.base_repr(1, 1)
        with assert_raises(ValueError):
            np.base_repr(1, 37) 
Example 21
def test_base3(self):
        assert_equal(np.base_repr(3**5, 3), '100000') 
Example 22
def test_positive(self):
        assert_equal(np.base_repr(12, 10), '12')
        assert_equal(np.base_repr(12, 10, 4), '000012')
        assert_equal(np.base_repr(12, 4), '30')
        assert_equal(np.base_repr(3731624803700888, 36), '10QR0ROFCEW') 
Example 23
def test_negative(self):
        assert_equal(np.base_repr(-12, 10), '-12')
        assert_equal(np.base_repr(-12, 10, 4), '-000012')
        assert_equal(np.base_repr(-12, 4), '-30') 
Example 24
def test_base_range(self):
        with assert_raises(ValueError):
            np.base_repr(1, 1)
        with assert_raises(ValueError):
            np.base_repr(1, 37) 
Example 25
Project: cupy   Author: cupy   File: __init__.py    License: MIT License 5 votes vote down vote up
def base_repr(number, base=2, padding=0):  # NOQA (needed to avoid redefinition of `number`)
    """Return a string representation of a number in the given base system.

    .. seealso:: :func:`numpy.base_repr`
    """
    return numpy.base_repr(number, base, padding)


# -----------------------------------------------------------------------------
# Linear algebra
# ----------------------------------------------------------------------------- 
Example 26
Project: pySINDy   Author: luckystarufo   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base3(self):
        assert_equal(np.base_repr(3**5, 3), '100000') 
Example 27
Project: pySINDy   Author: luckystarufo   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_positive(self):
        assert_equal(np.base_repr(12, 10), '12')
        assert_equal(np.base_repr(12, 10, 4), '000012')
        assert_equal(np.base_repr(12, 4), '30')
        assert_equal(np.base_repr(3731624803700888, 36), '10QR0ROFCEW') 
Example 28
Project: pySINDy   Author: luckystarufo   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_negative(self):
        assert_equal(np.base_repr(-12, 10), '-12')
        assert_equal(np.base_repr(-12, 10, 4), '-000012')
        assert_equal(np.base_repr(-12, 4), '-30') 
Example 29
Project: pySINDy   Author: luckystarufo   File: test_numeric.py    License: MIT License 5 votes vote down vote up
def test_base_range(self):
        with assert_raises(ValueError):
            np.base_repr(1, 1)
        with assert_raises(ValueError):
            np.base_repr(1, 37) 
Example 30
Project: mxnet-lambda   Author: awslabs   File: test_numeric.py    License: Apache License 2.0 5 votes vote down vote up
def test_base3(self):
        assert_equal(np.base_repr(3**5, 3), '100000')