Python Forum
Division by zero and value of argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Division by zero and value of argument
#1
I am new with python also new with this forum.
I have searched this topic but didn't give me better solutions (may be my search skill is poor)

import numpy as np
from scipy import special

def somb(x):
    return 0.5 if x == 0.0 else special.j1(x) / x

x = np.linspace(-1, 1, 3)
print(x)
y = somb(x)
print(x)
print(y)
Output:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
If I change to
import numpy as np
from scipy import special

def somb(x):
    return 0.5 if (x == 0.0).all() else special.j1(x) / x

x = np.linspace(-1, 1, 3)
print(x)
y = somb(x)
print(x)
print(y)
Output:
[-1. 0. 1.] [-1. 0. 1.] [0.44005059 nan 0.44005059] Warnings: RuntimeWarning: invalid value encountered in true_divide return 0.5 if (x == 0.0).all() else special.j1(x) / x
The return value still give a nan at x==0.
Then I modified to
import numpy as np
from scipy import special

def somb(x):
    x[x==0] = np.finfo(float).smallest_normal
    return special.j1(x) / x

x = np.linspace(-1, 1, 3)
print(x)
y = somb(x)
print(x)
print(y)
Output:
[-1. 0. 1.] [-1.00000000e+000 2.22507386e-308 1.00000000e+000] [0.44005059 0.5 0.44005059]
No warnings/errors and the results are as expected, but changing the x values.
In my understanding, changing x in def locally will not change the x value outside the def.
Then I made changes like this.
import numpy as np
from scipy import special

def somb(x):
    xi = x * 1.0
    xi[x==0] = np.finfo(float).smallest_normal
    return special.j1(xi) / xi

x = np.linspace(-1, 1, 3)
print(x)
y = somb(x)
print(x)
print(y)
Output:
[-1. 0. 1.] [-1. 0. 1.] [0.44005059 0.5 0.44005059]
This was great.
But adds one xi variable, and manipulates zero with smallest float value.
I think this is not a good approach.
Please advise with better solution.
Thank you in advance.
Reply
#2
Divide by zero will result in a NaN. Numpy has a function that replaces NaN with a number (zero by default).
import numpy as np
from scipy import special
 
def somb(x):
    return np.nan_to_num(special.j1(x) / x, nan=0.5)
 
print(somb(np.linspace(-1, 1, 3)))
Reply
#3
Thank you. But still give RuntimeWarning: invalid value encountered in true_divide
Of course it can be suppressed np.seterr(divide='ignore', invalid='ignore'), but is there any better solution?

def somb(x):
    x[x==0] = np.finfo(float).smallest_normal
    return special.j1(x) / x
Can you explain why manipulating the x value inside def will change the x value outside this function?
This question is not related to the somb function anymore (solved using np.nan_to_num() ), but I need the answer for coding other functions.
Reply
#4
Why not suppress the warning?
import numpy as np
from scipy import special
  
def somb(x):
    with np.errstate(invalid='ignore'):
        return np.nan_to_num(special.j1(x) / x, nan=0.5)
  
print(somb(np.linspace(-1, 1, 3)))
Output:
[0.44005059 0.5 0.44005059]
This essentially says "I know some of the results may be invalid, but I'm going to fix that". It is very much like using try/except, and nothing is more Python that try/except.
Reply
#5
In your example you create a numpy array. You then create a variable named "x" and assign it the value of the numpy array. When you call somb(x), you are not passing the variable "x", you are passing the object it references. In somb() you also have a variable named "x" that is assigned the value of the first argument passed to somb(). somb.x and global x are different variables, but they reference the same numpy array object. If you use somb.x to modify the array, the change will be seen in global x. This happens because both variables reference the same array.
Reply
#6
OK, Thanks a lot for your explanation.
Also for suppressing the warnings.
I learn a lot from this.

Thank you again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Division questions Dionysis 5 993 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Floor division problem with plotting x-axis tick labels Mark17 5 2,047 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 2,093 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Floor division return value Chirumer 8 3,698 Nov-26-2020, 02:34 PM
Last Post: DeaD_EyE
  Integer division plozaq 2 1,939 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 4,878 Jun-12-2020, 01:52 PM
Last Post: dgrunwal
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,737 Mar-03-2020, 08:34 AM
Last Post: buran
  Division of an integer into sub-numbers Richard_SS 4 2,884 Jun-14-2019, 11:47 AM
Last Post: DeaD_EyE
  Logic of using floor division and modulus for a different variable at different time SB_J 2 2,462 Nov-01-2018, 07:25 PM
Last Post: SB_J

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020