import%20marimo%0A%0A__generated_with%20%3D%20%220.18.4%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20seaborn%20as%20sns%0A%0A%20%20%20%20sns.set(font_scale%3D1.5)%0A%20%20%20%20sns.set_style(%22whitegrid%22)%0A%20%20%20%20sns.set_palette(%22deep%22)%0A%20%20%20%20matplotlib.rc(%22figure%22%2C%20figsize%3D(9%2C%205)%2C%20dpi%3D100)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Complex%20Chebfuns%0A%0A%20%20%20%20As%20of%20%60v0.4.0%60%20ChebPy%20supports%20complex%20variable%20representations.%0A%20%20%20%20This%20makes%20it%20extremely%20convenient%20to%20perform%20certain%20computations%20in%20the%20complex%20plane.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20chebpy%20import%20chebfun%0A%20%20%20%20return%20(chebfun%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20For%20example%20here%20is%20how%20we%20can%20plot%20a%20series%20of%20%22Bernstein%20ellipses%22%20-%20important%20objects%0A%20%20%20%20in%20the%20convergence%20theory%20of%20Chebyshev%20series%20approximations%20for%20analytic%20functions.%0A%20%20%20%20They%20are%20computed%20as%20transformations%20of%20the%20scaled%20complex%20unit%20circle%20under%20the%20Joukowsky%20map%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(chebfun)%3A%0A%20%20%20%20x%20%3D%20chebfun(%22x%22%2C%20%5B-1%2C%201%5D)%0A%20%20%20%20z%20%3D%20np.exp(2%20*%20np.pi%20*%201j%20*%20x)%0A%0A%20%20%20%20def%20joukowsky(z)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Apply%20the%20Joukowsky%20transformation%20to%20a%20complex%20number.%0A%0A%20%20%20%20%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3A%20Complex%20input%20value%0A%0A%20%20%20%20%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20The%20transformed%20complex%20value%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20return%200.5%20*%20(z%20%2B%201%20%2F%20z)%0A%0A%20%20%20%20for%20rho%20in%20np.arange(1.1%2C%202%2C%200.1)%3A%0A%20%20%20%20%20%20%20%20ellipse%20%3D%20joukowsky(rho%20*%20z)%0A%20%20%20%20%20%20%20%20ellipse.plot(linewidth%3D2)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20(ellipse%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Per%20the%20first%20line%20of%20the%20above%20code%20segment%2C%20each%20of%20these%20ellipses%20is%20a%20complex-valued%20function%0A%20%20%20%20of%20the%20real%20variable%20%60x%60%20defined%20on%20%60%5B-1%2C%201%5D%60.%20It%20is%20trivial%20to%20extract%20the%20real%20and%20imaginary%20components%0A%20%20%20%20and%20plot%20these%20on%20the%20%60x%60%20domain%2C%20which%20we%20do%20for%20the%20last%20(largest)%20ellipse%20in%20the%20sequence%20as%20follows%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ellipse)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots()%0A%20%20%20%20ellipse.real().plot(linewidth%3D3%2C%20label%3D%22real%22)%0A%20%20%20%20ellipse.imag().plot(linewidth%3D3%2C%20label%3D%22imag%22)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Here%20is%20an%20example%20of%20using%20ChebPy%20to%20perform%20a%20contour%20integral%20calculation%0A%20%20%20%20(replicating%20Trefethen%20%26%20Hale's%0A%20%20%20%20%5Bexample%5D(https%3A%2F%2Fwww.chebfun.org%2Fexamples%2Fcomplex%2FKeyholeContour.html))%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(chebfun)%3A%0A%20%20%20%20def%20keyhole(r%2C%20r_outer%2C%20e)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Create%20a%20keyhole%20contour%20in%20the%20complex%20plane.%0A%0A%20%20%20%20%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20r%3A%20Inner%20radius%20of%20the%20keyhole%0A%20%20%20%20%20%20%20%20%20%20%20%20r_outer%3A%20Outer%20radius%20of%20the%20keyhole%0A%20%20%20%20%20%20%20%20%20%20%20%20e%3A%20Half-height%20of%20the%20keyhole%20slit%0A%0A%20%20%20%20%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20Four%20chebfun%20segments%20forming%20the%20keyhole%20contour%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20v%20%3D%20%5B-r_outer%20%2B%20e%20*%201j%2C%20-r%20%2B%20e%20*%201j%2C%20-r%20-%20e%20*%201j%2C%20-r_outer%20-%20e%20*%201j%5D%0A%20%20%20%20%20%20%20%20s%20%3D%20chebfun(%22x%22%2C%20%5B0%2C%201%5D)%0A%20%20%20%20%20%20%20%20z0%20%3D%20v%5B0%5D%20%2B%20(v%5B1%5D%20-%20v%5B0%5D)%20*%20s%0A%20%20%20%20%20%20%20%20z1%20%3D%20v%5B1%5D%20*%20v%5B2%5D%20**%20s%20%2F%20v%5B1%5D%20**%20s%0A%20%20%20%20%20%20%20%20z2%20%3D%20v%5B2%5D%20%2B%20s%20*%20(v%5B3%5D%20-%20v%5B2%5D)%0A%20%20%20%20%20%20%20%20z3%20%3D%20v%5B3%5D%20*%20v%5B0%5D%20**%20s%20%2F%20v%5B3%5D%20**%20s%0A%20%20%20%20%20%20%20%20return%20(z0%2C%20z1%2C%20z2%2C%20z3)%0A%0A%20%20%20%20z0%2C%20z1%2C%20z2%2C%20z3%20%3D%20keyhole(r%3D0.2%2C%20r_outer%3D2%2C%20e%3D0.1)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots()%0A%20%20%20%20kwds%20%3D%20dict(color%3D%22b%22%2C%20linewidth%3D3)%0A%20%20%20%20z0.plot(ax%3D_ax%2C%20**kwds)%0A%20%20%20%20z1.plot(ax%3D_ax%2C%20**kwds)%0A%20%20%20%20z2.plot(ax%3D_ax%2C%20**kwds)%0A%20%20%20%20z3.plot(ax%3D_ax%2C%20**kwds)%0A%20%20%20%20_ax.plot(%5B-4%2C%200%5D%2C%20%5B0%2C%200%5D%2C%20color%3D%22r%22%2C%20linewidth%3D2%2C%20linestyle%3D%22-%22)%0A%20%20%20%20_ax.axis(%22equal%22)%0A%20%20%20%20_ax.set_xlim(%5B-2.2%2C%202.2%5D)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20z0%2C%20z1%2C%20z2%2C%20z3%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20then%20perform%20the%20numerical%20integration%20as%20follows%2C%20obtaining%20a%20typically%20high-accuracy%20result.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(z0%2C%20z1%2C%20z2%2C%20z3)%3A%0A%20%20%20%20def%20f(x)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Calculate%20the%20product%20of%20log(x)%20and%20tanh(x).%0A%0A%20%20%20%20%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3A%20Input%20value%20or%20array%0A%0A%20%20%20%20%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20log(x)%20*%20tanh(x)%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20return%20np.log(x)%20*%20np.tanh(x)%0A%0A%20%20%20%20def%20contour_integral(z%2C%20f)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Calculate%20the%20contour%20integral%20of%20a%20function%20along%20a%20path.%0A%0A%20%20%20%20%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3A%20Complex%20path%20as%20a%20chebfun%0A%20%20%20%20%20%20%20%20%20%20%20%20f%3A%20Function%20to%20integrate%0A%0A%20%20%20%20%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20The%20value%20of%20the%20contour%20integral%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20integral%20%3D%20f(z)%20*%20z.diff()%0A%20%20%20%20%20%20%20%20return%20integral.sum()%0A%0A%20%20%20%20y0%20%3D%20np.sum(%5Bcontour_integral(z%2C%20f)%20for%20z%20in%20(z0%2C%20z1%2C%20z2%2C%20z3)%5D)%20%20%23%20numerical%20integral%0A%20%20%20%20y1%20%3D%204j%20*%20np.pi%20*%20np.log(np.pi%20%2F%202)%20%20%23%20exact%20value%0A%0A%20%20%20%20print(f%22%20%20%20y0%20%3D%20%7By0%3A%2B.15f%7D%22)%0A%20%20%20%20print(f%22%20%20%20y1%20%3D%20%7By1%3A%2B.15f%7D%22)%0A%20%20%20%20print(f%22y0-y1%20%3D%20%7By0%20-%20y1%3A%2B.15f%7D%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
ca43d6f7a740999b26b13e50903bede6