Skip to content

psf/black code formatting #1277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions arithmetic_analysis/bisection.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import math


def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano
def bisection(
function, a, b
): # finds where the function becomes 0 in [a,b] using bolzano

start = a
end = b
if function(a) == 0: # one of the a or b is a root for the function
return a
elif function(b) == 0:
return b
elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
elif (
function(a) * function(b) > 0
): # if none of these are root and they are both positive or negative,
# then his algorithm can't find the root
print("couldn't find root in [a,b]")
return
else:
mid = start + (end - start) / 2.0
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
while abs(start - mid) > 10 ** -7: # until we achieve precise equals to 10^-7
if function(mid) == 0:
return mid
elif function(mid) * function(start) < 0:
Expand All @@ -27,7 +31,8 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us


def f(x):
return math.pow(x, 3) - 2*x - 5
return math.pow(x, 3) - 2 * x - 5


if __name__ == "__main__":
print(bisection(f, 1, 1000))
7 changes: 2 additions & 5 deletions arithmetic_analysis/in_static_equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ def in_static_equilibrium(
if __name__ == "__main__":
# Test to check if it works
forces = array(
[
polar_force(718.4, 180 - 30),
polar_force(879.54, 45),
polar_force(100, -90)
])
[polar_force(718.4, 180 - 30), polar_force(879.54, 45), polar_force(100, -90)]
)

location = array([[0, 0], [0, 0], [0, 0]])

Expand Down
21 changes: 14 additions & 7 deletions arithmetic_analysis/intersection.py
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))
4 changes: 1 addition & 3 deletions arithmetic_analysis/lu_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def LUDecompose(table):


if __name__ == "__main__":
matrix = numpy.array([[2, -2, 1],
[0, 1, 2],
[5, 3, 1]])
matrix = numpy.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
L, U = LUDecompose(matrix)
print(L)
print(U)
6 changes: 3 additions & 3 deletions arithmetic_analysis/newton_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ def newton(function, function1, startingInt):
x_n = startingInt
while True:
x_n1 = x_n - function(x_n) / function1(x_n)
if abs(x_n - x_n1) < 10**-5:
if abs(x_n - x_n1) < 10 ** -5:
return x_n1
x_n = x_n1


def f(x):
return (x**3) - (2 * x) - 5
return (x ** 3) - (2 * x) - 5


def f1(x):
return 3 * (x**2) - 2
return 3 * (x ** 2) - 2


if __name__ == "__main__":
Expand Down
23 changes: 12 additions & 11 deletions arithmetic_analysis/newton_raphson_method.py
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))
4 changes: 2 additions & 2 deletions backtracking/all_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def generate_all_combinations(n: int, k: int) -> [[int]]:
>>> generate_all_combinations(n=4, k=2)
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
"""

result = []
create_all_state(1, n, k, [], result)
return result
Expand All @@ -34,7 +34,7 @@ def print_all_state(total_list):
print(*i)


if __name__ == '__main__':
if __name__ == "__main__":
n = 4
k = 2
total_list = generate_all_combinations(n, k)
Expand Down
34 changes: 17 additions & 17 deletions backtracking/all_permutations.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
'''
"""
In this problem, we want to determine all possible permutations
of the given sequence. We use backtracking to solve this problem.

Time complexity: O(n! * n),
where n denotes the length of the given sequence.
'''
"""


def generate_all_permutations(sequence):
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])


def create_state_space_tree(sequence, current_sequence, index, index_used):
'''
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly len(sequence) - index children.
It terminates when it reaches the end of the given sequence.
'''
"""

if index == len(sequence):
print(current_sequence)
return
if index == len(sequence):
print(current_sequence)
return

for i in range(len(sequence)):
if not index_used[i]:
current_sequence.append(sequence[i])
index_used[i] = True
create_state_space_tree(sequence, current_sequence, index + 1, index_used)
current_sequence.pop()
index_used[i] = False
for i in range(len(sequence)):
if not index_used[i]:
current_sequence.append(sequence[i])
index_used[i] = True
create_state_space_tree(sequence, current_sequence, index + 1, index_used)
current_sequence.pop()
index_used[i] = False


'''
"""
remove the comment to take an input from the user

print("Enter the elements")
sequence = list(map(int, input().split()))
'''
"""

sequence = [3, 1, 2, 4]
generate_all_permutations(sequence)
Expand Down
28 changes: 14 additions & 14 deletions backtracking/all_subsequences.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
'''
"""
In this problem, we want to determine all possible subsequences
of the given sequence. We use backtracking to solve this problem.

Time complexity: O(2^n),
where n denotes the length of the given sequence.
'''
"""


def generate_all_subsequences(sequence):
create_state_space_tree(sequence, [], 0)
create_state_space_tree(sequence, [], 0)


def create_state_space_tree(sequence, current_subsequence, index):
'''
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly two children.
It terminates when it reaches the end of the given sequence.
'''
"""

if index == len(sequence):
print(current_subsequence)
return
if index == len(sequence):
print(current_subsequence)
return

create_state_space_tree(sequence, current_subsequence, index + 1)
current_subsequence.append(sequence[index])
create_state_space_tree(sequence, current_subsequence, index + 1)
current_subsequence.pop()
create_state_space_tree(sequence, current_subsequence, index + 1)
current_subsequence.append(sequence[index])
create_state_space_tree(sequence, current_subsequence, index + 1)
current_subsequence.pop()


'''
"""
remove the comment to take an input from the user

print("Enter the elements")
sequence = list(map(int, input().split()))
'''
"""

sequence = [3, 1, 2, 4]
generate_all_subsequences(sequence)
Expand Down
40 changes: 23 additions & 17 deletions backtracking/minimax.py
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))
Loading