HP Forums
New Sum of Powers Log Function - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: New Sum of Powers Log Function (/thread-16557.html)

Pages: 1 2


RE: New Sum of Powers Log Function - Albert Chan - 04-02-2021 01:29 AM

(04-01-2021 11:55 PM)Namir Wrote:  I am looking to solve for x given all other parameters--n, s, scale.

I guess the only sure way is to actually do the sum.
Because of the power-law nature of functions, we should use log scale, like before.

I have no idea what should be the right guess for it, so I just start with g=1.
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.

Code:
from mpmath import *
def show(x): print float(x); return x

nbx_to_s = lambda n,b,x: 1+fsum(k**(x*b**(n-k)) for k in xrange(2,n+1))
nbs_to_x = lambda n,b,s,g=1.: findroot(lambda x: log(nbx_to_s(n,b,show(x))/s), g, tol=1e-4)

>>> n, b = 100, 0.8
>>> s = nbx_to_s(n, b, 1.6) # = 2238.0808062735837
>>> x = nbs_to_x(n, b, s)
1.0
1.0
1.25
1.69268857579
1.59352468855
1.5999199506
1.60000007609
1.6

>>> s = nbx_to_s(n, b, 16) # = 1.0000003500527855e+32
>>> x = nbs_to_x(n, b, s)
1.0
1.0
1.25
23.7615084486
15.9522719123
15.999999997
16.0
16.0


RE: New Sum of Powers Log Function - Albert Chan - 04-02-2021 01:18 PM

(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



RE: New Sum of Powers Log Function - Namir - 04-04-2021 03:41 PM

Thanks Albert. Will be looking at your recent feedback.