Website Design United States, Website Design California, Website Designing United States, Website Designing California - Python

A Long Library! It simply holds functions that i am fond of (mainly when I'm being subjected to torture (a.k.a. the drudgery that is homework))
functions include:

# Author: Daniel Turner
# Date: 23/12/2005
# Email:
# Licence = GNU GPL
# Any bugs, email me!
# More stuff to be implemented later...
# Always add to the revision log/errata

def quadratic(a,b,c): #in the form ax^2 + bx + c = 0
import math
d = (b*b) - (4*a*c)
if (d < 0):
return -1
x = (-b + math.sqrt(d))/(2*a)
y = (-b - math.sqrt(d))/(2*a)
return x, y # returns what x could be

def foil(a,b,c,d): #in the form (ax+b)(cx+d)
e = a*float(c)
f = (a*d)+(b*float(c))
g = (b*float(d))
return e,f,g # returns in the form ex^2 + fx + c = 0

def acceleration(u,v,t): # where u is the final velocity, v is the original, and t is the time taken
a = (u - v) / float(t)
return a #returns the acceleration.

def speed(d,t): # where d = distance, t = time
v = d/float(t)
return v # returns speed.

def charge(I, t): # I = current in amperes, t = time in seconds
Q = I/float(t)
return Q # Q = charge in columbs

def resistance(v,I): #Ohms law, u know the drill...
R = v/float(I)
return R #returns RESISTANCE

def voltage(R, I): # ohms law... u should know the drill by now...!
v = I*float(R)
return v # Returns VOLTAGE

def current(R, v): # third, final, ohms Law
I = v*float(R)
return I #Returns current

def power(w, t): # calculates Power, W is enery transfered in J, t is time in seconds
P = w/float(t)
return P # Returns power in J/s a.k.a Watts.

def workdone(F, s): #calculates the energy transfered by a force. F is the force in N, s is distance, usually meters
w = F*float(s)
return w # returns the energy transfered in Joules.

def nonInvAmp(Rf, Rg): # Claculate the Gain of a non-inverting omp amp (ideal) where Rf is the feed back, Rg is the resistor that tae the inverting in the ground
G = 1 + (Rf/float(Rg))
return G # ... return the gain...

def invAmp(R1, R2): # calc the gain of an inverting amplifier, R2 is the feed back, R1 is on the Vin
Ga = (-R2)/ float(R1)
return Ga # ...returns the Gain...

def freq555(t): # Calculate the time period of a 555 IC (Ideal) t is the time period is SECONDS
f = 1/float(t)
return f # Return the frequency in Hz.

def gradient(x1, y1, x2, y2): # claculate the gradient, give 2 points on the line (x1, y1) and (x2, y2)
grad = (y2 - y1) / (x2 - float(x1))
return grad # Return the gradient

def intercept(x1, y1, x2, y2): # Calculate the Y intercept of a line, given 2 points on the line (x1, y1) and (x2, y2)
inte = (y1 - (((y2 - float(y1)) / (x2 - float(x1))) *float(x1)))
return inte # return the y intercept

def hypot(a,b): # hypotenuse calculation, given the 2 shorter sides (right angle triangle)
import math
return math.sqrt((a**2+b**2)) # return the length of the hypotenuse

def hypotS(c,b): # Calcultates the "other side" given the hyportenuse and another side (right angle triangle) (b is always less then c)
if (c > b): #make sure it's valid input ...
import math
return math.sqrt((c**2-b**2)) # ... then return the correct value
if (c <= b): # If it's invalid...
return -1 # To hell with it! lol, no math performed.

def hso(a,o): # Calculate the lenght of the hypotenuse, given the oposite (o) and tha angle (a which is opposite to o)
if (a >= 90):
return -1 # Validity check
import math
h = (o)/(math.sin(a*3.1415926535/180)) # Do magic
return h # Give the answer, h is the hypotenuse's length

def osh(a,h): # Calculate the length of opposite, given hypotensue (h) and the angle which is opposite the length to be calculated (a)
if (a>=90): # Validity check
return - 1
import math
o = h * math.sin(a*3.1415926535/180) # Do some more magic
return o #Return the lengt of the opposite side.

def uca(a,adj): # Calculate the hypotenuse given an angle (a) and the length (adj) which is adjacent to the angle.
if (a>=90): # Validity check
return -1
import math
h = adj/(math.cos(a*3.1415926535/180) # Do magic
#return h #Return the hypotenuse (h)

def ach(a,h): # Calculate the "adjacent" (adj) given the hypotenuse (h) and the angle which is directly adjacent to the lentgh being calculated.
if (a>=90): # Validity check
return -1
import math
adj = h * math.cos(a*3.1415926535/180) # Blashpemy
return adj #Return adjacent (adj)

def ato(a,o): # Calculate the Adjacent (adj) given the opposite and tha angle a which is adjacent to the length being calculated
if (a>=90): # Validity check
return -1
import math
adj = o/math.tan(a*3.1415926535/180) # Worship (a.k.a Put foot in the perverbial "it")
return adj # Return the desired length

def ota(a,adj): # Calculate, the "opposite" length, given the correct angle (which is adjacent to the adj length), and the adjacent (adj) length
if (a>=90):
return -1 # Validity Check
import math
o = adj*math.tam(a*3.1415926/180) # Do nasty things to the maths dept.
return o #return the "opposite" length

def areaRect(a,b): # Calculate the area of a rectangle (or square) given the length of 2 sides
c =a*b
return c # Return the area

def perimeterRect(a,b): # Calculate the perimeter of a rectangle (or sqaure) given 2 sides
c = (2*a) + (2*b)
return c # Return the perimeter.

def circleArea(r): # Calculate a circle's area give it's radius (r)
a = 3.1415926535*r**2
return a # Return the area

def circumfrence(r): # Calculate the circumfrence of a circle
c = 3.1415926535*r*2
return c # Return the circumfrence
# Revision Log:
# Daniel Turner 22/12/05 1600: Made library
# Daniel Turner 22/12/05 1630: added nonInvAmp(), InvAmp() and freq555()
# Daniel Turner 22/12/05 1922: added gradient() and intercept()
# Daniel Turner 22/12/05 1945: added to the acceleration function to try to make it calc in float
# Daniel Turner 22/12/05 1956: converted ALL functions to floating point arithmetic
# Daniel Turner 23/12/05 1036: James Gave me the idea for Trigonometry calculations. Implemented the pythag ones...
# Daniel Turner 23/13/05 1208: Implemented the first 2 Sine things from SOHCAHTOA.
# Daniel Turner 23/12/05 1222: implemented the first 2 Cosine things from SOHCAHTOA (still no idea how to get sin^-1, cos^-1 or tan^-1
# Daniel Turner 23/12/05 1233: Implemented The first 2 Tangent things from SOHCAHTOA (snin^-1 still eludes me :-s)
# Daniel Turner 23/12/05 1520: The last 4 trig things won't compile , Added areaRect(a,b) and perimeterRect(a,b)
# Daniel Turner 23/12/05 1530: Implemented circleArea(r) and circumfrence(r)

# Errata:
# Daniel Turner 23/12/05 1520: hca(a,adj) ach(a,h ato(a.o) and ota(a,adj) refuse to comile: give syntax errors (x)

42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Tel: 91-011-40502005, 9810067295



© 2008-2009