[Tutor] How to solve the overflow error in double_scalars

Dennis Lee Bieber wlfraed at ix.netcom.com
Thu Apr 28 20:29:50 EDT 2022


On Fri, 29 Apr 2022 00:18:17 +0530, SATYABRATA DATTA
<iamsatyabrata428 at gmail.com> declaimed the following:

>Inside a python package Encountering overflow due to a part of code
>
>this is my code part
>
>    def V1(self, bosons, fermions, X):
>        X = np.asanyarray(X, dtype=np.float64)
>        phi1 = X[...,0]
>
>        y1 = (17/3)*(4*(self.y6**4) - 3*(self.g**4))*(phi1**4)
>
>        y1 -= (4*(self.y6**4) -
>3*(self.g**4))*(2*np.log(np.abs((phi1)/(self.renormScaleSq**0.5))+
>1e-100))*(phi1**4)
>        return (y1/(64*np.pi*np.pi))
>

	<SNIP>

>and getting a completely wrong output,I think it is occuring due to the log
>function but don't know how to resolve it

	Not a direct help, but I'd suggest breaking those long expressions into
a sequence of short steps with temporary variables. That would let you
isolate which term(s) are causing the problem(s).

	You may also simplify some of the code by pulling out common
sub-expressions: you are computing

	4 * (self.y6**4)
and
	3 * (self.g**4)

on both lines... (and since numpy appears to be involved given the np.
references, y6 and g could be numpy arrays, making those computations
costly to repeat). Say: y6quadby4, gquadby3 <G>

	17 / 3				(SEVENTEENTHIRDS)
and
	64*np.pi*np.pi		(PISQUAREBY64)

are invariants, and could be pre-computed at the module level and bound to
"constant" names rather than computing them each time this function is
invoked.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list