Post Reply 
New Sum of Powers Log Function
04-02-2021, 01:18 PM
Post: #22
RE: New Sum of Powers Log Function
(04-02-2021 01:29 AM)Albert Chan Wrote:  Note that guess is evaluated twice. This is a fluke of mpmath, to simplify code.
The branched solver(s) re-calculated from the same guess again.

This is the patch to fix this issue.
C:\python\Lib\site-packages\mpmath\calculus\optimization.py, findroot():

Right before entering solver, i.e. below comment "use solver"
Code:
        # use solver
        f0, p = f, [tuple(x0), fx] # cache previous result
        def f(*x):
            if x!=p[0]: p[0]=x; p[1]=f0(*x)
            return p[1]

What is shocking is the patch may save more than 1 call.
Example, copied from help(findroot):

>>> f = [lambda x1, x2: x1**2 + x2, lambda x1, x2: 5*x1**2 - 3*x1 + 2*x2 - 3]
>>> findroot(f, (0, 0))
[-0.618033988749895]
[-0.381966011250105]

Patched findroot reduced f calls, from 24, down to 17 Smile
Code:
01  0.0                0.0
02  1.45519152284e-11  0.0
03  0.0                1.45519152284e-11
04  -1.00000000001     1.45519152285e-11
05  -0.500000000005    7.27595761425e-12
06  -0.49999999999     7.27595761425e-12
07  -0.500000000005    2.18278728426e-11
08  -0.625             -0.374999999999
09  -0.624999999986    -0.374999999999
10  -0.625             -0.374999999985
11  -0.618055555556    -0.381944444445
12  -0.618055555541    -0.381944444445
13  -0.618055555556    -0.38194444443
14  -0.618033988958    -0.381966011042
15  -0.618033988943    -0.381966011042
16  -0.618033988958    -0.381966011028
17  -0.61803398875     -0.38196601125
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
New Sum of Powers Log Function - Namir - 03-29-2021, 04:53 PM
RE: New Sum of Powers Log Function - C.Ret - 03-29-2021, 08:39 PM
RE: New Sum of Powers Log Function - Namir - 03-30-2021, 11:05 AM
RE: New Sum of Powers Log Function - Gene - 03-30-2021, 01:43 PM
RE: New Sum of Powers Log Function - C.Ret - 03-30-2021, 04:01 PM
RE: New Sum of Powers Log Function - Namir - 03-30-2021, 05:56 PM
RE: New Sum of Powers Log Function - Namir - 03-31-2021, 01:27 PM
RE: New Sum of Powers Log Function - Namir - 03-31-2021, 02:19 PM
RE: New Sum of Powers Log Function - Namir - 04-01-2021, 06:05 PM
RE: New Sum of Powers Log Function - Namir - 04-01-2021, 11:55 PM
RE: New Sum of Powers Log Function - Albert Chan - 04-02-2021 01:18 PM
RE: New Sum of Powers Log Function - Namir - 04-04-2021, 03:41 PM



User(s) browsing this thread: 1 Guest(s)