python study, math modules, example source code

__author__ = 'mare'


l = list( range(0, 10) )
print( sum(l) )
print( sum(l, 100) )
print( max(l) )
print( min(l) )
print( abs(-11) )
#45
#145
#9
#0
#11

print( "-----" )
print( pow(2, 10) ) #2^10
print( pow(2, 10, 100) )  #(2^10)%10
print( divmod(10, 7) )
print( divmod(5.3, 2.1) )
#1024
#24
#(1, 3)
#(2.0, 1.0999999999999996)

print( round(152.394) )
print( round(152.394, 1) )
print( round(152.394, -1) )
#152
#152.4
#150.0

import math
print( math.ceil(3.14 ) )
print( math.floor(3.14) )
print( math.trunc(3.14) )
#4
#3
#3

print("---")
print( math.copysign(6.5, -0.0) )
print( math.fabs(-6.5) )
print( math.factorial(3.0) )
print( math.modf(-6.5) )
l = [3.14, 1.24, 5.23]
print( math.fsum(l) )
#-6.5
#6.5
#6
#(-0.5, -6.0)
#9.610000000000001


print("---")
print( math.fmod(5.5, 3) )
print( 5.5%3 )
print( math.fmod(-5.5, 3) )
print( -5.5 % 3 )
#2.5
#2.5
#-2.5
#0.5

print( math.pow(2, 10) )
print( math.pow(1.5, 3) )
print( math.sqrt(2) ) #sqrt(2)
print( math.exp(2) ) #e^2
print( math.log(math.e) ) #ln(e)
#1024.0
#3.375
#1.4142135623730951
#7.38905609893065
#1.0

print("---")
r = math.radians(30)
print(math.sin(r))
r = math.radians(180)
v = math.cos(r)
print( v )
print( r == math.acos(v) )
print( math.degrees(r) )
#0.49999999999999994
#-1.0
#True
#180.0


print("---")
#fractions
import fractions
print( fractions.Fraction(4,16) )
print( fractions.Fraction(-6, 21) )
print( fractions.Fraction(3) )
#1/4
#-2/7
#3

f = fractions.Fraction(4,3)
print(f)
#4/3
f2 = fractions.Fraction(f)
print(f2)
#4/3

print("---")
#special functions
print( fractions.Fraction('6/21') )
print( fractions.Fraction('3.14') )
print( fractions.Fraction('     -0.34     '))
s = """
-0.34
"""
print( fractions.Fraction(s) )
#2/7
#157/50
#-17/50
#-17/50

print("---")
from fractions import Fraction
print( Fraction.from_float(0.5) )
print( Fraction.from_decimal(4) )
#1/2
#4

print("---")
from math import pi, cos
from fractions import Fraction
print( Fraction.from_float(pi) )
#884279719003555/281474976710656
print( Fraction.from_float( pi ).limit_denominator(100) )
#311/99

f = Fraction.from_float(3.14)
print( f.__floor__() )
#3

import math
print( math.floor(f) )
print( math.ceil(f) )
print( round(f) )
#3
#3
#4
#3


import fractions
fractions.gcd(120, 180)
fractions.gcd(0.5, 6)
#60, 0.5

print("---")

#decimal
import decimal
print( decimal.Decimal(3) )
print( decimal.Decimal('1.1') )
print( decimal.Decimal(str(1/7)) )
#decimal.Deciaml( (0,(3,1,4),-2) )
print( decimal.Decimal("-Infinity") )
print( decimal.Decimal('-0') )
print( decimal.Decimal('NaN') )
#3
#1.1
#0.14285714285714285
#-Infinity
#-0
#NaN


a, b = decimal.Decimal('3.14'), decimal.Decimal('0.4')
print( a+b )
print( a-b )
print( a*b )
print( a/b )
print( a**b )
#3.54
#2.74
#1.256
#7.85
#1.580417606406722895935118558

a = decimal.Decimal('3.14')
print( a*3 )
print( divmod(a, 2) )
print( round(a, 1) )
print( int(a) )
#9.42
#(Decimal('1'), Decimal('1.14'))
#3.1
#3

print("---")
rawData = '3.14|5.3|1.65|9|-1.28'
l = [decimal.Decimal(x) for x in rawData.split('|') ]
print(l)
#[Decimal('3.14'), Decimal('5.3'), Decimal('1.65'), Decimal('9'), Decimal('-1.28')]
print( max(l) )
print( min(l) )
print( sum(l) )
print( sorted(l) )
#9
#-1.28
#17.81
#[Decimal('-1.28'), Decimal('1.65'), Decimal('3.14'), Decimal('5.3'), Decimal('9')]



from decimal import Decimal
d = Decimal("3.14")
print( d.sqrt() )
#1.772004514666935040199112510
print( d.exp() )
print( d.ln() )
#23.10386685872218278457908458
#1.144222799920161998805694448

d2 = Decimal("-1.414")
print( d.compare(d2) )
print( d.copy_abs() )
print( d.copy_negate() )
print( d.copy_sign(d2) )
#1
#3.14
#-3.14
#-3.14

print( d2.is_signed() )
print( d.is_finite() )
print( d.is_infinite() )
print( d.is_zero() )
#True
#True
#False
#False


#random
import random
print( random.random() )
print( random.random() )
#0.7774163660360384
#0.7987333021579469

print( random.uniform(3, 4) )
#3.013465537004017

for i in range(3):
 print( random.gauss(1, 1.0) )
#0.07892791910471075
#0.49322741367164435
#0.6201914100688197


print( [random.randrange(20) for i in range(10) ])
#[0, 15, 11, 5, 17, 15, 0, 0, 11, 4]

print( random.sample( range(20), 10 ))
#[16, 4, 8, 0, 15, 10, 2, 19, 7, 13]

print( [random.randrange(0,20,3) for i in range(5) ] )
#[18, 18, 18, 12, 12]


print("---")
l = list(range(10))
print(l)
print( [random.choice(l) for i in range(3) ])
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#[1, 0, 6]

print( random.sample(l,3))
#[5, 4, 8]


l = list( range(10) )
print( l )
random.shuffle(l)
print( l )
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#[2, 4, 1, 7, 3, 0, 8, 9, 5, 6]


l = list( range(10) )
s = random.sample(l, 10)
print( l, s)
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [9, 5, 6, 8, 7, 1, 3, 4, 0, 2]


Comments

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)