Created
December 4, 2016 13:09
-
-
Save hmenn/c8ec2095fa6539b535aacc81dd1597fa to your computer and use it in GitHub Desktop.
Calculate polynomial equations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!usr/bin/env | |
| ## HASAN MEN - Algorithm HW4 - Q1 | |
| import math # math.floor and math.ceil | |
| def calcExpo(expo,x): | |
| "Calculates exponential values like x^2 , x^5" | |
| #print("expo:",expo," floor" ,math.floor(expo/2),"ceil" ,math.ceil(expo/2)) | |
| if expo==1: | |
| return x | |
| elif expo==0: | |
| return 1 | |
| return calcExpo(math.floor(expo/2),x) * calcExpo(math.ceil(expo/2),x) | |
| # Time Efficiency O(nlogn) | |
| def calcPoly(coefs,expos,x,n): | |
| """Calculates polynomial equation | |
| coef: coefficients array | |
| expo: exponentials array | |
| x: value for solving equation | |
| n: size of array / number of elements in polynomial | |
| """ | |
| res = 0 | |
| for i in range (0,n): | |
| #print("I:",i," Expo:",expo[i]," Coef:",coef[i]) | |
| res += calcExpo(expos[i],x)*coefs[i] | |
| return res | |
| coefArr = [2,3,4] | |
| expoArr = [2,3,0] | |
| x=2 | |
| size=3 | |
| result = calcPoly(coefArr,expoArr,x,size) | |
| print("X:",x," Size:",size," CoefArr: ",coefArr," ExpoArr: ",expoArr) | |
| print("Resul :",result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment