forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
psf/black code formatting (TheAlgorithms#1277)
- Loading branch information
1 parent
07f04a2
commit 9eac17a
Showing
291 changed files
with
6,100 additions
and
4,657 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
import math | ||
|
||
def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points | ||
|
||
def intersection( | ||
function, x0, x1 | ||
): # function is the f we want to find its root and x0 and x1 are two random starting points | ||
x_n = x0 | ||
x_n1 = x1 | ||
while True: | ||
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n))) | ||
if abs(x_n2 - x_n1) < 10**-5: | ||
x_n2 = x_n1 - ( | ||
function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n)) | ||
) | ||
if abs(x_n2 - x_n1) < 10 ** -5: | ||
return x_n2 | ||
x_n=x_n1 | ||
x_n1=x_n2 | ||
x_n = x_n1 | ||
x_n1 = x_n2 | ||
|
||
|
||
def f(x): | ||
return math.pow(x , 3) - (2 * x) -5 | ||
return math.pow(x, 3) - (2 * x) - 5 | ||
|
||
|
||
if __name__ == "__main__": | ||
print(intersection(f,3,3.5)) | ||
print(intersection(f, 3, 3.5)) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
# Implementing Newton Raphson method in Python | ||
# Author: Syed Haseeb Shah (github.com/QuantumNovice) | ||
#The Newton-Raphson method (also known as Newton's method) is a way to | ||
#quickly find a good approximation for the root of a real-valued function | ||
# The Newton-Raphson method (also known as Newton's method) is a way to | ||
# quickly find a good approximation for the root of a real-valued function | ||
from sympy import diff | ||
from decimal import Decimal | ||
|
||
|
||
def NewtonRaphson(func, a): | ||
''' Finds root from the point 'a' onwards by Newton-Raphson method ''' | ||
""" Finds root from the point 'a' onwards by Newton-Raphson method """ | ||
while True: | ||
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) ) | ||
c = Decimal(a) - (Decimal(eval(func)) / Decimal(eval(str(diff(func))))) | ||
|
||
a = c | ||
|
||
# This number dictates the accuracy of the answer | ||
if abs(eval(func)) < 10**-15: | ||
return c | ||
if abs(eval(func)) < 10 ** -15: | ||
return c | ||
|
||
|
||
# Let's Execute | ||
if __name__ == '__main__': | ||
if __name__ == "__main__": | ||
# Find root of trigonometric function | ||
# Find value of pi | ||
print('sin(x) = 0', NewtonRaphson('sin(x)', 2)) | ||
print("sin(x) = 0", NewtonRaphson("sin(x)", 2)) | ||
|
||
# Find root of polynomial | ||
print('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4)) | ||
print("x**2 - 5*x +2 = 0", NewtonRaphson("x**2 - 5*x +2", 0.4)) | ||
|
||
# Find Square Root of 5 | ||
print('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1)) | ||
print("x**2 - 5 = 0", NewtonRaphson("x**2 - 5", 0.1)) | ||
|
||
# Exponential Roots | ||
print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0)) | ||
print("exp(x) - 1 = 0", NewtonRaphson("exp(x) - 1", 0)) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
import math | ||
import math | ||
|
||
''' Minimax helps to achieve maximum score in a game by checking all possible moves | ||
""" Minimax helps to achieve maximum score in a game by checking all possible moves | ||
depth is current depth in game tree. | ||
nodeIndex is index of current node in scores[]. | ||
if move is of maximizer return true else false | ||
leaves of game tree is stored in scores[] | ||
height is maximum height of Game tree | ||
''' | ||
""" | ||
|
||
def minimax (Depth, nodeIndex, isMax, scores, height): | ||
|
||
if Depth == height: | ||
return scores[nodeIndex] | ||
def minimax(Depth, nodeIndex, isMax, scores, height): | ||
|
||
if isMax: | ||
return (max(minimax(Depth + 1, nodeIndex * 2, False, scores, height), | ||
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height))) | ||
return (min(minimax(Depth + 1, nodeIndex * 2, True, scores, height), | ||
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height))) | ||
if Depth == height: | ||
return scores[nodeIndex] | ||
|
||
if __name__ == "__main__": | ||
|
||
scores = [90, 23, 6, 33, 21, 65, 123, 34423] | ||
height = math.log(len(scores), 2) | ||
if isMax: | ||
return max( | ||
minimax(Depth + 1, nodeIndex * 2, False, scores, height), | ||
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height), | ||
) | ||
return min( | ||
minimax(Depth + 1, nodeIndex * 2, True, scores, height), | ||
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height), | ||
) | ||
|
||
print("Optimal value : ", end = "") | ||
print(minimax(0, 0, True, scores, height)) | ||
|
||
if __name__ == "__main__": | ||
|
||
scores = [90, 23, 6, 33, 21, 65, 123, 34423] | ||
height = math.log(len(scores), 2) | ||
|
||
print("Optimal value : ", end="") | ||
print(minimax(0, 0, True, scores, height)) |
Oops, something went wrong.