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%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20path%20%3D%20Path(__file__).parent%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%20Implementation%20Notes%0A%0A%20%20%20%20The%20internals%20of%20ChebPy%20have%20been%20designed%20to%20resemble%20the%20design%20structure%20of%20MATLAB%20Chebfun.%0A%20%20%20%20The%20Chebfun%20v5%20class%20diagram%20thus%20provides%20a%20useful%20map%20for%20understanding%20how%20the%20various%20pieces%0A%20%20%20%20of%20ChebPy%20fit%20together%20(diagram%20courtesy%20of%20the%20Chebfun%20team%2C%20available%0A%20%20%20%20%5Bhere%5D(https%3A%2F%2Fgithub.com%2Fchebfun%2Fchebfun%2Fwiki%2FClass-diagram))%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20file%20%3D%20path%20%2F%20%22chebfun-v5-class-diag.png%22%0A%20%20%20%20assert%20file.exists()%0A%20%20%20%20mo.image(file.read_bytes())%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%20At%20this%20stage%2C%20only%20a%20relatively%20small%20subset%20of%20MATLAB%20Chebfun%20has%20been%20implemented%20in%20ChebPy.%0A%20%20%20%20In%20the%20class%20diagram%20above%2C%20this%20consists%20of%20all%20the%20classes%20traced%20by%20the%20path%20going%20up%20from%0A%20%20%20%20from%20%60Chebtech%60%20(green%20box%20in%20the%20bottom%20right)%2C%20to%20%60Chebfun%60%20(blue%20box%20near%20the%20top-left).%0A%20%20%20%20More%20explicitly%2C%20the%20following%20classes%20currently%20exist%20in%20ChebPy%3A%0A%0A%20%20%20%20-%20%60Chebfun%60%20(%5Bcore%2Fchebfun.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Fchebfun.py))%0A%20%20%20%20-%20%60Fun%60%20(%5Bcore%2Ffun.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Ffun.py))%0A%20%20%20%20-%20%60Classicfun%60%20(%5Bcore%2Fclassicfun.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Fclassicfun.py))%0A%20%20%20%20-%20%60Bndfun%60%20(%5Bcore%2Fbndfun.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Fbndfun.py))%0A%20%20%20%20-%20%60Onefun%60%20(%5Bcore%2Fonefun.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Fonefun.py))%0A%20%20%20%20-%20%60Smoothfun%60%20(%5Bcore%2Fsmoothfun.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Fsmoothfun.py))%0A%20%20%20%20-%20%60Chebtech%60%20(%5Bcore%2Fchebtech.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Fchebtech.py))%0A%20%20%20%20-%20%60Chebtech%60%20(%5Bcore%2Fchebtech.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Fchebtech.py))%0A%0A%20%20%20%20ChebPy%20additionally%20provides%20the%20following%20classes%20which%20do%20not%20appear%20in%20their%20present%20form%20in%20MATLAB%20Chebfun%3A%0A%0A%20%20%20%20-%20%60Interval%60%20(%5Bcore%2Futilities.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Futilities.py))%0A%20%20%20%20-%20%60Domain%60%20(%5Bcore%2Futilities.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Futilities.py))%0A%0A%20%20%20%20ChebPy%20allows%20the%20user%20to%20override%20some%20default%20preferences%2C%20similar%20to%20what%20is%20available%20through%0A%20%20%20%20%60chebfunpref%60%20in%20%60Chebfun%60%20(bottom%20right).%20Not%20all%20options%20are%20the%20same%2C%20and%20chebpy%20allows%0A%20%20%20%20additional%20customisation%20not%20found%20in%20%60Chebfun%60.%0A%0A%20%20%20%20-%20%60UserPrefs%60%20(%5Bcore%2Fsettings.py%5D(https%3A%2F%2Fgithub.com%2Fchebpy%2Fchebpy%2Fblob%2Fmaster%2Fchebpy%2Fcore%2Fsettings.py))%0A%0A%20%20%20%20The%20general%20rule%20is%20that%20each%20ChebPy%20class%20lives%20in%20its%20own%20python%20file.%0A%20%20%20%20%22%22%22)%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%20One%20can%20explore%20the%20organisation%20of%20the%20library%20in%20practice%20as%20follows.%0A%20%20%20%20We%20describe%20the%20three%20core%20components%20with%20reference%20to%20the%20colours%20in%20the%20above%20class%20diagram.%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%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20return%20np%2C%20plt%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20optional%20plot%20settings%0A%20%20%20%20import%20matplotlib%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%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%20%23%23%23%20Chebfun%20(blue)%0A%0A%20%20%20%20We'll%20start%20by%20creating%20an%20identity%20chebfun%20on%20an%20arbitrary%20non-unit%20interval%3A%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%0A%20%20%20%20x%20%3D%20chebfun(%22x%22%2C%20%5B-2%2C%203%5D)%0A%20%20%20%20x%0A%20%20%20%20return%20chebfun%2C%20x%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%20This%20variable%20is%20an%20object%20of%20class%20%60Chebfun%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(x)%3A%0A%20%20%20%20type(x)%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%20%23%23%23%20Fun%20(yellow)%0A%0A%20%20%20%20Conceptually%2C%20%60Chebfun%60%20objects%20are%20defined%20as%20a%20collection%20(numpy%20array)%20of%20Fun%20objects.%20One%20can%20access%20these%0A%20%20%20%20via%20the%20%60.funs%60%20attribute%2C%20and%20in%20this%20example%2C%20since%20our%20function%20is%20globally%20smooth%2C%20our%20chebfun%20is%20composed%0A%20%20%20%20of%20single%20Fun%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(x)%3A%0A%20%20%20%20x.funs%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%20Fun%20is%20an%20abstract%20class%2C%20so%20we%20don't%20actually%20see%20Fun%20objects%20per-se%2C%20but%20rather%20objects%20further%20down%0A%20%20%20%20the%20inheritance%20tree.%20Specifically%2C%20we%20see%20objects%20of%20type%20%60BndFun%60%2C%20denoting%20a%20function%20defined%20on%20a%20bounded%0A%20%20%20%20interval.%0A%0A%20%20%20%20Here's%20a%20piecewise%20smooth%20function%20created%20by%20inducing%20a%20discontinuity%20via%20use%20of%20the%20maximum%20operator.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20x)%3A%0A%20%20%20%20f%20%3D%20np.sin(x).maximum(-np.sin(x))%0A%20%20%20%20f%0A%20%20%20%20return%20(f%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%20Here%20the%20representation%20consists%20of%20two%20%60Fun%60%20objects%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f)%3A%0A%20%20%20%20f.funs%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%20The%20core%20%60Chebfun%60%20class%20constructor%20accepts%20iterable%20collections%20of%20%60Fun%60%20objects%2C%0A%20%20%20%20and%20the%20above%20chebfun%20can%20be%20recreated%20as%20follows%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f)%3A%0A%20%20%20%20from%20chebpy.chebfun%20import%20Chebfun%0A%0A%20%20%20%20Chebfun(f.funs)%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%20The%20Fun%20objects%20defining%20a%20chebfun%20must%20provide%20a%20non-overlapping%20and%20complete%20partition%20of%20the%20global%0A%20%20%20%20approximation%20interval.%20ChebPy-specific%20exceptions%20will%20be%20raised%20if%20the%20user%2Fdeveloper%20does%20not%20properly%0A%20%20%20%20account%20for%20this.%0A%0A%20%20%20%20To%20see%20this%2C%20let's%20break%20the%20second%20Fun%20into%20two%20sub-pieces%20(using%20%60fun.restrict%60)%20and%20attempt%20various%0A%20%20%20%20reconstruction%20permutations.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f)%3A%0A%20%20%20%20f.breakpoints%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f%2C%20plt)%3A%0A%20%20%20%20from%20chebpy.utilities%20import%20Interval%0A%0A%20%20%20%20_%2C%20a%2C%20c%20%3D%20f.breakpoints%0A%20%20%20%20b%20%3D%201%0A%20%20%20%20fun0%20%3D%20f.funs%5B0%5D%0A%20%20%20%20fun1%20%3D%20f.funs%5B1%5D.restrict(Interval(a%2C%20b))%0A%20%20%20%20fun2%20%3D%20f.funs%5B1%5D.restrict(Interval(b%2C%20c))%0A%20%20%20%20fun0.plot(linewidth%3D3)%0A%20%20%20%20fun1.plot(linewidth%3D3)%0A%20%20%20%20fun2.plot(linewidth%3D3)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20fun0%2C%20fun1%2C%20fun2%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%20So%2C%20the%20following%20works%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Chebfun%2C%20fun0%2C%20fun1%2C%20fun2)%3A%0A%20%20%20%20Chebfun(%5Bfun0%2C%20fun1%2C%20fun2%5D)%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%20However%2C%20the%20following%20will%20raise%20an%20exception%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20%3E%3E%20Chebfun(%5Bfun0%2C%20fun2%5D)%0A%20%20%20%20IntervalGap%3A%20The%20supplied%20Interval%20objects%20do%20not%20form%20a%20complete%20partition%20of%20the%20approximation%20interval%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20As%20will%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20%3E%3E%20Chebfun(%5Bfun0%2C%20f.funs%5B1%5D%2C%20fun1%5D)%0A%20%20%20%20IntervalOverlap%3A%20The%20supplied%20Interval%20objects%20overlap%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%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%20%23%23%23%20Onefun%20(green)%0A%0A%20%20%20%20A%20Fun%20object%20is%20defined%20by%20the%20composition%20of%20%60Onefun%60%20object%20and%20an%20Interval%20object.%20A%20%60Onefun%60%20object%20in%20ChebPy%2C%0A%20%20%20%20as%20with%20MATLAB%20Chebfun%2C%20define%20a%20set%20of%20core%20approximation%20behaviour%20on%20the%20unit%20interval%20%5B-1%2C1%5D.%20The%20computational%0A%20%20%20%20mechanics%20of%20mapping%20these%20operations%20to%20arbitrary%20intervals%20%5Ba%2Cb%5D%20is%20managed%2C%20in%20part%2C%20by%20the%20a%20corresponding%0A%20%20%20%20Interval%20object.%0A%0A%20%20%20%20To%20illustrate%2C%20let's%20take%20the%20first%20component%20%60Fun%60%20from%20earlier%20(which%20was%20specifically%20a%20%60Bndfun%60)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f)%3A%0A%20%20%20%20f.funs%5B0%5D%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%20The%20Onefun%20and%20Interval%20object%20components%20are%20accessed%20as%20follows%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f)%3A%0A%20%20%20%20f.funs%5B0%5D.onefun%2C%20f.funs%5B0%5D.interval%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%20%60Onefun%60%20is%20an%20abstract%20class%2C%20so%20what%20we%20actually%20see%20instantiated%20is%20an%20object%20of%20type%20%60Chebtech%60.%0A%20%20%20%20To%20see%20that%20the%20%60Onefun%60%20object%20is%20what%20is%20claims%20to%20be%20(a%20representation%20defined%20on%20the%20unit-interval)%2C%0A%20%20%20%20we%20can%20plot%20it%20(users%20will%20rarely%20do%20this%20in%20practice%2C%20but%20this%20can%20nevertheless%20be%20a%20useful%20feature%0A%20%20%20%20for%20developers)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f%2C%20plt)%3A%0A%20%20%20%20f.funs%5B0%5D.onefun.plot(linewidth%3D3)%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%20So%20while%20the%20%60Onefun%60%20attribute%20determines%20approximation%20behaviour%2C%20the%20interval%20attribute%20manages%0A%20%20%20%20the%20mapping%20to%20and%20from%20the%20approximation%20interval%20to%20%5B-1%2C1%5D.%20So%20for%20instance%2C%20one%20can%20evaluate%20the%20interval%0A%20%20%20%20object%20at%20some%20set%20of%20points%20in%20%5B-1%2C1%5D%20and%20obtain%20these%20values%20mapped%20to%20%5Ba%2Cb%5D%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f%2C%20np)%3A%0A%20%20%20%20f.funs%5B0%5D.interval(np.linspace(-1%2C%201%2C%2011))%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%20%23%23%20User%20preferences%20(%60UserPrefs%60)%0A%0A%20%20%20%20The%20user%20may%20want%20to%20specify%20different%20tolerances%2C%20for%20example%20if%20speed%20is%20important%20or%20the%20function%20under%0A%20%20%20%20consideration%20is%20particularly%20difficult.%20It%20is%20also%20possible%20to%20change%20default%20behaviour%20like%20plotting.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(chebfun%2C%20plt)%3A%0A%20%20%20%20from%20chebpy%20import%20UserPreferences%0A%20%20%20%20from%20chebpy.settings%20import%20DefaultPreferences%0A%0A%20%20%20%20user_prefs%20%3D%20UserPreferences()%0A%0A%20%20%20%20with%20user_prefs%20as%20local_prefs%3A%0A%20%20%20%20%20%20%20%20local_prefs.eps%20%3D%201e-10%20%20%23%20lower%20the%20tolerance%20in%20chebpy%0A%20%20%20%20%20%20%20%20cheb%20%3D%20chebfun(lambda%20x%3A%20x**2)%0A%20%20%20%20assert%20user_prefs.eps%20%3D%3D%20DefaultPreferences.eps%20%20%23%20value%20did%20reset!%0A%0A%20%20%20%20with%20user_prefs%3A%20%20%23%20we%20don't%20have%20to%20assign%20a%20new%20name%0A%20%20%20%20%20%20%20%20user_prefs.N_plot%20%3D%2021%20%20%23%20use%20fewer%20points%20for%20plotting%0A%20%20%20%20%20%20%20%20cheb.plot(marker%3D%22x%22%2C%20label%3D%22N_plot%20%3D%2021%22)%0A%20%20%20%20%20%20%20%20user_prefs.reset(%22N_plot%22)%20%20%23%20restore%20default%0A%20%20%20%20%20%20%20%20cheb.plot(label%3D%22default%20N_plot%22)%0A%0A%20%20%20%20plt.legend()%0A%20%20%20%20plt.show()%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
f7ecc799f78c908c4f48c19cba44db5c