addes
This commit is contained in:
parent
0c62b308cc
commit
2e1bbcf7cf
28
ex_45.py
Normal file
28
ex_45.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import numpy as np
|
||||||
|
from scipy import misc
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
from util.io import readvalue
|
||||||
|
def positive_int(s):
|
||||||
|
i = int(s)
|
||||||
|
if(i <= 0):
|
||||||
|
raise ValueError("{} is negative".format(i))
|
||||||
|
return i
|
||||||
|
|
||||||
|
file_input_name = readvalue("Input file name > ", str)
|
||||||
|
file_output_name = readvalue("Output file name > ", str)
|
||||||
|
cutoff = readvalue("Frequencies to cut > ", positive_int)
|
||||||
|
|
||||||
|
fin = misc.imread(file_input_name, flatten=True)
|
||||||
|
|
||||||
|
|
||||||
|
transformed = np.fft.fft2(fin)
|
||||||
|
|
||||||
|
|
||||||
|
new_transformed = np.zeros(transformed.shape, dtype=np.complex)
|
||||||
|
new_transformed[cutoff:-cutoff, cutoff:-cutoff] = transformed[cutoff:-cutoff, cutoff:-cutoff]
|
||||||
|
|
||||||
|
|
||||||
|
new = np.fft.ifft2(new_transformed).real
|
||||||
|
|
||||||
|
misc.imsave(file_output_name, new)
|
58
ex_46.py
Normal file
58
ex_46.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
|
||||||
|
def bisec(f, a, b, eps, nmax):
|
||||||
|
"""
|
||||||
|
Takes f:[a,b] -> |R and tries to compute the null point
|
||||||
|
using bisection.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if(f(a)*f(b) >= 0):
|
||||||
|
raise ValueError("f(a)*f(b) >= 0")
|
||||||
|
|
||||||
|
x_minus = a
|
||||||
|
x_plus = b
|
||||||
|
|
||||||
|
if(a > b):
|
||||||
|
x_minus = b
|
||||||
|
x_plus = a
|
||||||
|
|
||||||
|
for i in range(nmax):
|
||||||
|
x_minus, x_plus, err = bisec_one(x_minus, x_plus, f, eps)
|
||||||
|
if(err < eps):
|
||||||
|
break
|
||||||
|
if(err < eps):
|
||||||
|
return x_minus, err
|
||||||
|
raise ValueError("bisection hit nmax")
|
||||||
|
|
||||||
|
|
||||||
|
def bisec_one(x_minus, x_plus, f, eps):
|
||||||
|
a = x_minus
|
||||||
|
b = x_plus
|
||||||
|
|
||||||
|
x_m = (b + a) / 2
|
||||||
|
y_m = f(x_m)
|
||||||
|
|
||||||
|
if(y_m < 0):
|
||||||
|
if(y_m > -1*eps):
|
||||||
|
return x_m, x_m, -y_m
|
||||||
|
|
||||||
|
if(f(x_m)*f(b) >= 0):
|
||||||
|
return a, x_m, -y_m
|
||||||
|
return x_m, b, -y_m
|
||||||
|
|
||||||
|
if(y_m < eps):
|
||||||
|
return x_m, x_m, y_m
|
||||||
|
|
||||||
|
if(f(a)*f(x_m) >= 0):
|
||||||
|
return x_m, b, y_m
|
||||||
|
return a, x_m, y_m
|
||||||
|
|
||||||
|
|
||||||
|
if( __name__ == "__main__"):
|
||||||
|
f1 = lambda x: x
|
||||||
|
f2 = lambda x: x**3
|
||||||
|
f3 = lambda x: -x + 1
|
||||||
|
f4 = lambda x: -x**3 + 1
|
||||||
|
|
||||||
|
fs = [f1, f2, f3, f4]
|
||||||
|
for f in fs:
|
||||||
|
print(bisec(f, -12, 10, 0.001, 100))
|
Loading…
Reference in New Issue
Block a user