Last active
March 23, 2022 06:04
-
-
Save JarnaChao09/69fa3fc6a8a10087edf4e9a08c911417 to your computer and use it in GitHub Desktop.
my_project
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
from math import ceil | |
from input_handler import __input_eval as enput | |
def __euler_step(dy_dx, initial_x, initial_y, steps, delta_x=0.00001, silent=False): | |
""" | |
Performs `steps` steps of Euler's Method, starting at (`initial_x`, | |
`initial_y`). `dy_dx` is a function that takes 2 arguments, `x` and `y`. | |
`delta_x` is the step size for x. `silent` controls whether the intermediate | |
steps are printed. The final value of `y` is returned. | |
Example: Given dy/dx = 2y, y(0) = 1/2, do 5 steps of Euler's method | |
``` | |
euler_step("2*y", 0, 0.5, 5, 0.1) | |
``` | |
""" | |
x = initial_x | |
y = initial_y | |
if not silent: | |
print("x, y, dy/dx, deltaY, new y") | |
for _ in range(steps): | |
# micropy on the numworks calculator is broken, as seen below | |
dy_dx_at_x = eval(dy_dx.replace('x', str(x)).replace('y', str(y))) | |
delta_y = dy_dx_at_x * delta_x | |
new_y = y + delta_y | |
if not silent: | |
print([x, y, dy_dx_at_x, delta_y, new_y]) | |
x += delta_x | |
y += delta_y | |
if not silent: print([x, y]) | |
return y | |
def euler(dy_dx, initial_x, initial_y, final_x, delta_x=0.00001, silent=False): | |
""" | |
Like euler_step, but continues Euler's method until `final_x` is reached. | |
Example: Given dy/dx = 2y, y(0) = 1/2, estimate y(1/2) | |
``` | |
euler("2*y", 0, 0.5, 0.5, 0.1) | |
``` | |
""" | |
return __euler_step(dy_dx, initial_x, initial_y, ceil((final_x - initial_x) / delta_x), delta_x, silent) | |
def friendly_euler(silent=False): | |
""" | |
Like euler, but more friendly | |
""" | |
dy_dx = input("dy_dx: ") | |
initial_x = enput("xi: ", float) | |
initial_y = enput("y(" + str(initial_x) + "): ", float) | |
final_x = enput("final x value: ", float) | |
delta_x = enput("delta x: ", float) | |
return __euler_step(dy_dx, initial_x, initial_y, ceil((final_x - initial_x) / delta_x), delta_x, silent) |
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
def __extendedEuclidean(a, b): | |
if a == 0: | |
return b, 0, 1 | |
gcd, x1, y1 = __extendedEuclidean(b%a, a) | |
x = y1 - (b // a) * x1 | |
y = x1 | |
return gcd, x, y | |
def extendedEuclidean(a = None, b = None): | |
if a == None and b == None: | |
a = int(input("a = ")) | |
b = int(input("b = ")) | |
g, x, y = __extendedEuclidean(a, b) | |
print(str(g) + " = (" + str(a) + ") * (" + str(x) + ") + (" + str(b) + ") * (" + str(y) + ")") |
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
__evaluated_types = [ | |
int, float, complex, hex, oct | |
] | |
__split = " " | |
# out is the message right before the input, t is the type the input should be cast as | |
def __input_eval(out, t): | |
data = input(out+"\n") | |
if t in __evaluated_types: | |
data = eval(data) | |
return t(data) | |
def __input_list_eval(out, t): | |
raw = input(out+":\n").split(__split) | |
ret = [] | |
for i in range(len(raw)): | |
if t in __evaluated_types: | |
ret.append(t(eval(raw[i]))) | |
else: | |
ret.append(t(raw[i])) | |
return ret | |
def __input_list_list_eval(out1, out2, t): | |
return [__input_list_eval(out1 + str(j + 1), float) for j in range(__input_eval(out2, int))] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment