0

I have an array which has values, if calcualted precisely: [3.056565 3.924560] however in python it prints rounded numbers and the array is [3.06, 3.93]. The array is the used to calculate something else and the result is different than expected. How can I make that array to stop rounding itself?

Here is the code where you can see that array is not precisely calcualted:

import datetime
import math
import numpy as np

def numGradient(f, x, h):


  n = len(x)
  g = [0]*n
  g = np.array(g)

  for i in range(n):
    e = [0]*n
    e[i] = 1
    e=np.array(e)


    g[i] = (f(x+e*h)-f(x-e*h))//(2*h)

  return(g)

def myFun(x) :
    return ( 0.6 + ((math.sin(x[0]**2-x[1]**2))**2-0.5)/((1+0.001*(x[0]**2+x[1]**2))**2) )
x_old=(3,4)

xthis = x_old - 0.01*numGradient(myFun, x_old, 10**(-6))

print(xthis)
8
  • 3
    Basically, numpy.array's are not "round themself" besides ordinary rounding that occur in floating-point arithmetic. (Rounding on printing can occur that does not affect the numbers itself.) Could you please provide more concise code snippet that demonstrate the behaviour you are asking about? Expected and obtained answers are also would be helpful. Commented Dec 9, 2017 at 14:41
  • So basically it comes down to this equation: x_old- a*numGradient(f, x_old, 10**(-6)), where the result is [ 3.06 3.93] and it should be 3.056565 3.924560 when calculating outside python ( for example using R) Commented Dec 9, 2017 at 14:51
  • You probably have to reproduce the effect with a simpler code. It is very difficult to debug such a long snippet. It is not clear, why this discrepancy occur, without simple example. Commented Dec 9, 2017 at 15:00
  • well its not a discrepancy, im asking if there is possibilty to display more digits in python and set it to not round the number to 2 digits. Commented Dec 9, 2017 at 15:25
  • 1
    However, you believe that it "should be" 3.056565. The difference between this number and 3.06 is what I mean under "discrepancy" in the comment above. It is not clear what is the source of this discrepancy. Probably, your implementation of this code in a different language is different in some other ways. This is why I believe that you have to reproduce the same effect with simple example (i.e. one arithmetic operation). Commented Dec 9, 2017 at 15:39

1 Answer 1

3

There are several issues with your code. It probably does not compute what you expect it to compute.

First, this is the wrong way to initialize a NumPy array

  g = [0]*n
  g = np.array(g)

Replace it with

  g = np.zeros(n)

Second, do the same with e.

for i in range(n):
    e = np.zeros(n)
    e[i] = 1

And most importantly replace

g[i] = (f(x+e*h)-f(x-e*h))//(2*h)

with

g[i] = (f(x+e*h)-f(x-e*h))/(2*h)

This is floating point division after all. And // is the so-called integer division in Python 3.

After the changes you will get

[ 3.056565  3.92456 ]

Which is probably what you should be getting.

Here is the complete code after the changes:

import datetime
import math
import numpy as np

def numGradient(f, x, h):


  n = len(x)
  g = np.zeros(n)

  for i in range(n):
    e = np.zeros(n)
    e[i] = 1
    g[i] = (f(x+e*h)-f(x-e*h))/(2*h)

  return(g)

def myFun(x) :
    return ( 0.6 + 
       ((math.sin(x[0]**2-x[1]**2))**2-0.5)/((1+0.001*(x[0]**2+x[1]**2))**2) )


x_old=(3,4)

np.set_printoptions(precision=6)

xthis = x_old - 0.01*numGradient(myFun, x_old, 10**(-6))

print(xthis)
Sign up to request clarification or add additional context in comments.

1 Comment

one nitpick: the extra parens are useless in np.zeros((n)). +1 overall.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.