Skip to main content

numerical quantization

Project description

general-purpose quantization. includes directed rounding, tie-breaking rounding, randomized rounding, truncation, rounding to multiples, negative zeroes, and a bit more!

how to install

pip install pyquantize
alternatives

using python explicitly:

python -m pip install pyquantize

or visit the PyPI webpage: https://pypi.org/project/pyquantize/
or install the latest github version:

git clone https://github.com/deftasparagusanaconda/pyquantize/
cd pyquantize
pip install -e .

how to use

there is only one function in the module: quantize
import it like so:

from pyquantize import quantize

quantize(3.14, 0.8)
# 3.2

usage:

quantize(number,
		quantum     = 1,
		offset      = 0,
		centre      = 0,
		threshold   = 0.5,
		directed    = False,
		signed_zero = True,
		mode        = 'even')

number: an int or a float type. no default. the number to be quantized

quantum: an int or a float type. default is 1. the number will be quantized to multiples of this quantum. quantize(x, quantum=0.7) will snap the number to […, -1.4, -0.7, 0, 0.7, 1.4, …]

offset: an int or a float type. default is 0. the quantization grid will be offset by this amount. quantize(x, quantum=0.7, offset=0.2) will change the grid from […, -1.4, -0.7, 0, 0.7, 1.4, …] to […, -1.2, -0.5, 0.2, 0.9, 1.6, …]

centre: an int or a float type. default is 0. affects 'toward' and 'away' modes. quantize(x, centre=float('inf'), mode='toward') is the same as quantize(x, mode='ceil'). quantize(x, centre=float('-inf'), mode='toward') is the same as quantize(x, mode='floor') similarly so for mode='away'

threshold an int or a float type. default is 0.5. must satisfy 0 ≤ threshold ≤ 1. it determines the percentage at which the number is rounded up or down

directed: a bool type. default is False. if False, mode is only applied for ties (where the number is exactly between multiples of quantum, like 0.5 between 0 and 1). if True, mode is always applied

signed_zero: a bool type. default is True. if True, whenever the result is zero, it shows whether it was rounded from the negative or positive side. for example, -0.1 rounds to -0.0 instead of 0.0. if False, this rounds to 0.0 as usual

mode: a str type. default is 'even'. determines the method for quantization. options are:
'threshold' - quantize down if the fractional part is less than threshold
'floor' - quantize down toward -∞
'ceil' - quantize up toward +∞
'toward' - quantize toward centre
'away' - quantize away from centre
'even' - quantize toward nearest even multiple (default)
'odd' - quantize toward nearest odd multiple
'alternate' - quantize up or down alternately according to quantize.alternate_last
'random' - quantize up or down randomly
'stochastic' - quantize up or down according to stochastic probability

'alternate', 'random', 'stochastic' are non-deterministic

when mode is 'alternate', the last state is remembered as an attribute of the function, which you can access as quantize.alternate_last (a bool type)

(this function may occasionally round to unexpected results, due to floating point imprecision)

tidbits

to simulate rounding, try:

def qround(number, digits=0, *args, **kwargs):
	return quantize(number, quantum=10**-digits, *args, **kwargs)

print(qround(2.34, 1, directed=True, mode='stochastic'))
# 2.3 or 2.4

unlike python's round, you can even round a number to a non-integer amount of digits!

print(qround(2.34, 1.5))
# 2.2135943621178655

to simulate rounded division, try:

def qdivmod(dividend, divisor, *args, **kwargs):
	result = quantize(dividend/divisor, *args, **kwargs)
	return result, dividend-result*divisor

print(qdivmod(2.34, 1, directed=True, mode='stochastic'))
# (1, 1.0) or (2, -0.5)

stochastic division! neat huh?? or try even-rounded integer division:

print(qdivmod(3, 2, mode='even'))
# (2, -1)
print(qdivmod(4, 2, mode='even'))
# (2, 0)
print(qdivmod(5, 2, mode='even'))
# (2, 1)

or check that stochastic mode works:

count = 0
for i in range(10**5):
	count += quantize(0.9, directed=True, mode='stochastic')

print(count/10**5)
# ≈0.9

(psst! these functions arent actually defined in pyquantize. just fun little trinkets. or you can ask me if youd like them to be included!)

how to uninstall

pip uninstall pyquantize

the end ~

if you can help me port this to other languages, i the open-source community would be super grateful! :)
and if you liked this, please please give me a star it really helps

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyquantize-0.2.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyquantize-0.2.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file pyquantize-0.2.0.tar.gz.

File metadata

  • Download URL: pyquantize-0.2.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pyquantize-0.2.0.tar.gz
Algorithm Hash digest
SHA256 daffef448738adaa0122ef7547ec322f7adf21c0b4c5689d4ea819c3fe379415
MD5 e69582f5367af0accdf038c465a984a1
BLAKE2b-256 3ceef7f46d067c3b251cbf5a58868fa85e71526b4eeacbaba49f84a610020bb5

See more details on using hashes here.

File details

Details for the file pyquantize-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyquantize-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pyquantize-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fddd8897055e6924467fe22c5c819a54c26933a2968d2a4f55b73f311735195a
MD5 ee66925115ddee6cfcab51a03dcad594
BLAKE2b-256 f81983abca3fd5f8c8caffd427c524188ac1dc0aa96dde22c1db43ea878889ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page