#C:\Python27\Lib\site-packages
#Make sure this file is called "QuadFuncs.py" and inside your site-packages folder
#Note: This module DOES NOT handle fractions, but it will handle decimals.
#Command for import:
#import QuadFuncs as qf
#Imports
import math
import __builtin__
#Functions
#Syntax: qf.zeros(equation='y = ax^2 + bx + c')
def zeros(equation):
equationlen = len(equation)
start = equation[4:equationlen]
asep1 = start.partition('x')[0]
aorig = asep1
aorig2 = aorig.partition('x')[0]
anew = float(aorig2)
bsep1 = start[3:equationlen]
bsep2 = bsep1.partition(' ')
bsep3 = bsep2[2]
bsep4 = bsep3.partition(' ')
bsep5 = bsep4[0] + bsep4[2]
borig = bsep5
borig2 = borig.partition('x')[0]
bnew = float(borig2)
csep1 = borig[3:equationlen]
csep2 = csep1.partition(' ')
csep3 = csep2[2]
csep4 = csep3.partition(' ')
csep5 = csep4[0] + csep4[2]
corig = csep5
cnew = float(corig)
discr = (((bnew)**2) - (4*(anew)*(cnew)))
if discr == 0:
__builtin__.x1 = 0
__builtin__.x2 = None
elif discr < 0:
__builtin__.x1 = None
__builtin__.x2 = None
elif discr > 0:
__builtin__.x1 = ((-1*(bnew)) + (math.sqrt(discr))/(2*(anew)))
__builtin__.x2 = ((-1*(bnew)) - (math.sqrt(discr))/(2*(anew)))
#Syntax: qf.vertex(equation='y = ax^2 + bx + c')
def vertex(equation):
equationlen = len(equation)
start = equation[4:equationlen]
asep1 = start.partition('x')[0]
aorig = asep1
aorig2 = aorig.partition('x')[0]
anew = float(aorig2)
bsep1 = start[3:equationlen]
bsep2 = bsep1.partition(' ')
bsep3 = bsep2[2]
bsep4 = bsep3.partition(' ')
bsep5 = bsep4[0] + bsep4[2]
borig = bsep5
borig2 = borig.partition('x')[0]
bnew = float(borig2)
csep1 = borig[3:equationlen]
csep2 = csep1.partition(' ')
csep3 = csep2[2]
csep4 = csep3.partition(' ')
csep5 = csep4[0] + csep4[2]
corig = csep5
cnew = float(corig)
__builtin__.h = ((-1*(bnew))/(2*(anew)))
__builtin__.k = ((4*(anew)*(cnew)) - ((bnew)**2)/(4*(anew)))
if __builtin__.h == -0:
__builtin__.h = 0
if __builtin__.k == -0:
__builtin__.k = 0
__builtin__.vert = ((__builtin__.h), (__builtin__.k))
#Syntax: qf.axisofsym(equation='y = ax^2 + bx + c')
def axisofsym(equation):
equationlen = len(equation)
start = equation[4:equationlen]
asep1 = start.partition('x')[0]
aorig = asep1
aorig2 = aorig.partition('x')[0]
anew = float(aorig2)
bsep1 = start[3:equationlen]
bsep2 = bsep1.partition(' ')
bsep3 = bsep2[2]
bsep4 = bsep3.partition(' ')
bsep5 = bsep4[0] + bsep4[2]
borig = bsep5
borig2 = borig.partition('x')[0]
bnew = float(borig2)
__builtin__.axis = ((-1*(bnew))/(2*(anew)))
if __builtin__.axis == -0:
__builtin__.axis = 0
#Syntax: qf.vertform(equation='y = ax^2 + bx + c')
def vertform(equation):
equationlen = len(equation)
start = equation[4:equationlen]
asep1 = start.partition('x')[0]
aorig = asep1
aorig2 = aorig.partition('x')[0]
anew = float(aorig2)
bsep1 = start[3:equationlen]
bsep2 = bsep1.partition(' ')
bsep3 = bsep2[2]
bsep4 = bsep3.partition(' ')
bsep5 = bsep4[0] + bsep4[2]
borig = bsep5
borig2 = borig.partition('x')[0]
bnew = float(borig2)
csep1 = borig[3:equationlen]
csep2 = csep1.partition(' ')
csep3 = csep2[2]
csep4 = csep3.partition(' ')
csep5 = csep4[0] + csep4[2]
corig = csep5
cnew = float(corig)
hnew = ((-1*(bnew))/(2*(anew)))
knew = ((4*(anew)*(cnew)) - ((bnew)**2)/(4*(anew)))
__builtin__.vertexform = 'y = %f(x - %f)^2 + (%f)' % (anew, hnew, knew)
#Syntax: qf.standform(equation='y = a(x - h) + k')
def standform(vertequation):
equationlen = len(vertequation)
start = vertequation[4:equationlen]
asep1 = start.partition('(')[0]
aorig = asep1
aorig2 = aorig.partition('(')[0]
anew = float(aorig2)
hsep1 = start[4:equationlen]
hsep2 = hsep1.partition(')')[0]
hsep3 = hsep2[2]
hsep4 = hsep3.partition(' ')
hsep5 = hsep4[0] + hsep4[2]
horig = hsep5
horig2 = float(horig)
hnew = (horig2)*-1
ksep1 = hsep1[5:equationlen]
ksep2 = ksep1.partition(' ')
ksep3 = ksep2[0] + ksep2[2]
korig = ksep3
knew = float(korig)
bnew = -1*2*(hnew)
cnew = (anew)*((hnew)**2) + (knew)
__builtin__.standardform = 'y = (%f)x^2 + (%f)x + (%f)' % (anew, bnew, cnew)
This is a module that allows for basic Quadratic Functions. Syntax is stated in the code. I am pretty new to programming, so any advice would help! (Note: I know optimization is possible, please don't spam comments. Thanks!)
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.