Python Doing Automatic Differentiation

AI Thread Summary
The discussion centers on an interactive session utilizing the ODE Playground, a repository for Automatic Differentiation code. It demonstrates two methods for approximating the square root of 2: Newton's method and the bisection method. Users can input a function via a lambda expression without needing to specify step sizes or other numerical artifacts. The results show that Newton's method converges to approximately 1.414213562373095 after 7 iterations, while the bisection method yields a result of approximately 1.4142135623733338 after 38 iterations. The session also introduces dual numbers, showcasing their properties and operations, such as the calculation involving dual numbers 3.0, 5.0, and a variable dual number 7.0.
m4r35n357
Messages
657
Reaction score
148
This is a brief interactive session using my ODE Playground, which is my repository of Automatic Differentiation code. It illustrates two solutions of ##\sqrt {2}## with the user-specified function provided via a lambda, using Newton's method and then bisection. Note that the user needs to provide no step sizes or other requirements or artifacts of numerical differencing, just the function itself!

Python:
$ ipython3
Python 3.7.1 (default, Oct 22 2018, 11:21:55)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from ad import *                                                      
ad module loaded

In [2]: from playground import *                                              
playground module loaded

In [3]: Newton(lambda x: (x**2), x0=1.0, target=2.0)                          
Out[3]: ResultType(count=7, sense='', mode='ROOT', x=1.414213562373095, f=2.0000000000000004, dx=-1.5700924586837747e-16)

In [4]: bisect(lambda x: (x**2), xa=1.4, xb=1.5, target=2.0)                  
Out[4]: ResultType(count=38, sense='', mode='ROOT', x=1.4142135623733338, f=2.0000000000006755, dx=7.276401703393276e-13)
Enjoy!
 
Last edited:
  • Like
Likes jedishrfu and Dale
Technology news on Phys.org
OK here's some dual numbers:

Code:
$ ipython3
Python 3.7.1 (default, Oct 22 2018, 11:21:55)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from ad import *                                                      
ad module loaded

In [2]: a = Dual.get(3.0)                                                      

In [3]: b = Dual.get(5.0)                                                      

In [4]: c = Dual.get(7.0, variable=True)                                      

In [5]: print(a)                                                              
3.0 0.0

In [6]: print(b)                                                              
5.0 0.0

In [7]: print(c)                                                              
7.0 1.0

In [8]: print(c**2 - a * c)                                                    
28.0 11.0
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
18
Views
3K
Back
Top