Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (testpypi.python.org).
Help us improve Python packaging - Donate today!

some usefull math code

Project Description

Description

Simple Math is a python package containing useful and interesting functions (and soon, classes). The following table contains the functions and a short description.

Function Description
exno returns the Expanded Notation of a number as a string
factors returns all factors of an integer as a list
gcf returns the Greatest Common Factor of two numbers (may remove)
gcfl returns the Greatest Common Factor of a list of numbers
lcd returns the Least Common Denominator of two numbers
lcdl returns the Least Common Denominator of a list of numbers
mean returns the Mean (Average) of a list of numbers
median returns the Median of a list of numbers
mode returns the Mode(s) in a list
plval returns the Digit in a Place Value of a number
prifac returns the Prime Factorization (Decomposition) of a number in a list
prime returns True if a number is prime and False if not
primes_to returns a list of all the primes up to a number
psr returns True if a number has a Perfect Square Root and False if not
refine returns a parsed math term that is more readable by python

(There will soon be classes)


Installation

From pip:

pip install simple_math

or:

pip3 install simple_math

What’s New

  • certain functions now always give correct values in python2
  • refine is a bit cleaned up as well as changed.
  • median and mode both have an opptional argument “sort” that determines whether or not list is sorted in function.

Examples

exno:

Return the Expanded Notation of “num” as a string

>>> simple_math.exno(325)
'( 3 * 100 ) + ( 2 * 10 ) + ( 5 * 1 )'
>>> simple_math.exno(30201)
'( 3 * 10000 ) + ( 2 * 100 ) + ( 1 * 1 )'
>>> simple_math.exno(30201.023)
'( 3 * 10000 ) + ( 2 * 100 ) + ( 1 * 1 ) + ( 2 * 0.01 ) + ( 3 * 0.001 )'
>>> simple_math.exno(30201.023,powers=True)
'( 3 * 10**4 ) + ( 2 * 10**2 ) + ( 1 * 10**0 ) + ( 2 * 10**-2 ) + ( 3 * 10**-3 )'
>>> simple_math.exno(1,powers=True)
'( 1 * 10**0 )'
>>>

factors:

Return all factors of an int as a list Raises ValueError for negitive or floating point numbers

>>> simple_math.factors(10)
[1, 2, 5, 10]
>>> simple_math.factors(100)
[1, 2, 4, 5, 10, 20, 25, 50, 100]
>>> simple_math.factors(83) # prime number
[1, 83]
>>>

gcf:

Return the Greatest Common Factor of two numbers Already implemented in module “fractions” (and “math” in python 3.5) may remove in future

>>> simple_math.gcf(4,6)
2
>>> simple_math.gcf(1,1000)
1
>>> simple_math.gcf(20,10)
10
>>>

gcfl:

Return the Greatest Common Factor of a list of numbers

>>> simple_math.gcfl([4,6,8,10])
2
>>> simple_math.gcfl([15,12,9,6])
3
>>> simple_math.gcfl([number for number in range(100)]) #l=[0,1,2,3,4,5 ... 99]
1
>>>

lcd:

Return the Least Common Denominator of two numbers

>>> simple_math.lcd(2,3)
6
>>> simple_math.lcd(14,7)
14
>>> simple_math.lcd(1000,1001)
1001000
>>>

lcdl:

Return the Least Common Denominator of a list of numbers

>>> simple_math.lcdl([1,2,3])
6
>>> simple_math.lcdl([12,6,18])
36
>>> simple_math.lcdl([0,1,2,3,4,5])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "simple_math.py", line 128, in lcdl
    raise ValueError('invalid number "0"')
ValueError: invalid number "0"
>>>

mean:

Return the Mean (Average) of a list of numbers

>>> simple_math.mean([1,2,3])
2.0
>>> simple_math.mean([3])
3.0
>>> simple_math.mean([9,4,10])
7.666666666666667
>>> simple_math.mean([1,2,3,4])
2.5
>>>

median:

Return the Median of a list of numbers Second optional argument determines whether or not list is sorted in function

>>> simple_math.median([1,2,3])
2.0
>>> simple_math.median([1,2,3,6,7,8])
4.5
>>> simple_math.median([0.1,0.9,0.6,0.7])
0.6499999999999999
>>> simple_math.median([1,3,5,2,4,6],False) # incorrect! only use
false if list is already sorted
3.5
>>>

mode:

Return the Mode(s) in a list Second optional argument determines whether or not list is sorted in function

>>> simple_math.mode([1,2,3,4,5])
[1, 2, 3, 4, 5]
>>> simple_math.mode([1,2,3,4,5,1,3,6])
[1, 3]
>>> simple_math.mode([1,1,2,3,3,4,5,6],False)
[1, 3]
>>> simple_math.mode([0.1,0.2,0.3,0.1,0.4,0.6])
[0.1]
>>>

plval:

Return the Place Value of a number Place_value refers to n where the specified digit is in the 10^n slot Raises ValueError if place value provided is invalid

>>> simple_math.plval(123)
3
>>> simple_math.plval(1024,3)
1
>>> simple_math.plval(1024.1024,-3)
2
>>>

prifac:

Return the Prime Factorization of num in a list

>>> simple_math.prifac(8)
[2, 2, 2]
>>> simple_math.prifac(6)
[2, 3]
>>> simple_math.prifac(2*3*5*7)
[2, 3, 5, 7]
>>> simple_math.prifac(0) # 0 < 2
[]
>>>

prime:

Return True if num is a prime number and False if not

>>> simple_math.prime(3)
True
>>> simple_math.prime(4)
False
>>> simple_math.prime(13)
True
>>> simple_math.prime(1)
False
>>> simple_math.prime(-1)
False
>>>

primes_to:

Return a list of all the primes up to a number

>>> simple_math.primes_to(10)
[2, 3, 5, 7]
>>> simple_math.primes_to(0)
[]
>>> simple_math.primes_to(17)
[2, 3, 5, 7, 11, 13, 17]
>>>

psr:

Return True in a number has a Perfect Square Root and False if not

>>> simple_math.psr(4)
True
>>> simple_math.psr(5)
False
>>> simple_math.psr(0)
True
>>> simple_math.psr(123**2)
True
>>>

refine:

Return a parsed math term more readable by python. Second parameter is a list of single characters reserved as unknown values
>>> simple_math.refine('2(3+4)')
'2*(3+4)'
>>> simple_math.refine('2xy',['x','y'])
'2*x*y'
>>> simple_math.refine('4{3-[3x^2+(9x-3)]y}+z',['x','y','z'])
'4*(3-(3*x**2+(9x-3))*y)+z'
>>> simple_math.refine('y=(x^2)[z]',['y','x','z'])
'y=(x**2)*(z)'
>>>

For problems, bugs, and suggestions of simple_math, you can contact me at

Release History

Release History

This version
History Node

1.2.1

History Node

1.2.0

History Node

1

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
simple_math-1.2.1.tar.gz (12.7 kB) Copy SHA256 Checksum SHA256 Source Oct 30, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting