mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
Added Whitespace and Docstring (#924)
* Added Whitespace and Docstring I modified the file to make Pylint happier and make the code more readable. * Beautified Code and Added Docstring I modified the file to make Pylint happier and make the code more readable. * Added DOCSTRINGS, Wikipedia link, and whitespace I added DOCSTRINGS and whitespace to make the code more readable and understandable. * Improved Formatting * Wrapped comments * Fixed spelling error for `movement` variable * Added DOCSTRINGs * Improved Formatting * Corrected whitespace to improve readability. * Added docstrings. * Made comments fit inside an 80 column layout.
This commit is contained in:
@ -1,32 +1,36 @@
|
||||
"""Lower-Upper (LU) Decomposition."""
|
||||
|
||||
# lower–upper (LU) decomposition - https://en.wikipedia.org/wiki/LU_decomposition
|
||||
import numpy
|
||||
|
||||
def LUDecompose (table):
|
||||
|
||||
def LUDecompose(table):
|
||||
# Table that contains our data
|
||||
# Table has to be a square array so we need to check first
|
||||
rows,columns=numpy.shape(table)
|
||||
L=numpy.zeros((rows,columns))
|
||||
U=numpy.zeros((rows,columns))
|
||||
if rows!=columns:
|
||||
rows, columns = numpy.shape(table)
|
||||
L = numpy.zeros((rows, columns))
|
||||
U = numpy.zeros((rows, columns))
|
||||
if rows != columns:
|
||||
return []
|
||||
for i in range (columns):
|
||||
for j in range(i-1):
|
||||
sum=0
|
||||
for k in range (j-1):
|
||||
sum+=L[i][k]*U[k][j]
|
||||
L[i][j]=(table[i][j]-sum)/U[j][j]
|
||||
L[i][i]=1
|
||||
for j in range(i-1,columns):
|
||||
sum1=0
|
||||
for k in range(i-1):
|
||||
sum1+=L[i][k]*U[k][j]
|
||||
U[i][j]=table[i][j]-sum1
|
||||
return L,U
|
||||
for i in range(columns):
|
||||
for j in range(i - 1):
|
||||
sum = 0
|
||||
for k in range(j - 1):
|
||||
sum += L[i][k] * U[k][j]
|
||||
L[i][j] = (table[i][j] - sum) / U[j][j]
|
||||
L[i][i] = 1
|
||||
for j in range(i - 1, columns):
|
||||
sum1 = 0
|
||||
for k in range(i - 1):
|
||||
sum1 += L[i][k] * U[k][j]
|
||||
U[i][j] = table[i][j] - sum1
|
||||
return L, U
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
matrix =numpy.array([[2,-2,1],
|
||||
[0,1,2],
|
||||
[5,3,1]])
|
||||
L,U = LUDecompose(matrix)
|
||||
matrix = numpy.array([[2, -2, 1],
|
||||
[0, 1, 2],
|
||||
[5, 3, 1]])
|
||||
L, U = LUDecompose(matrix)
|
||||
print(L)
|
||||
print(U)
|
||||
|
@ -1,18 +1,25 @@
|
||||
"""Newton's Method."""
|
||||
|
||||
# Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method
|
||||
|
||||
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
|
||||
x_n=startingInt
|
||||
while True:
|
||||
x_n1=x_n-function(x_n)/function1(x_n)
|
||||
if abs(x_n-x_n1) < 10**-5:
|
||||
return x_n1
|
||||
x_n=x_n1
|
||||
|
||||
|
||||
# function is the f(x) and function1 is the f'(x)
|
||||
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:
|
||||
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__":
|
||||
print(newton(f,f1,3))
|
||||
print(newton(f, f1, 3))
|
||||
|
Reference in New Issue
Block a user