Skip to content
Edoardo Pasca edited this page Jun 3, 2019 · 4 revisions

Welcome to the CCPi-Framework wiki!

Machine precision

Some calculations should return the same value but are different due to machine precision.

With Python is not so clear what type the numbers are stored in. Additionally NumPy seems to use different strategies. We have encountered problems related to reductions like calculating the norm of an array (possibly multidimensional).

One would expect the following to return the same number for 1D arrays (just for simplicity):

a = numpy.asarray([some array], dtype=numpy.float32)
res1 = (a**2).sum()
res2 = a.dot(a)
res3 = functools.reduce(lambda x,y: x + y*y, a, 0)

Now, NumPy stores the result of sum in a container of the same type of the array it's summing. It is advisable, instead, to use a container of higher precision if possible. In the third method Python seems to be smart enough to store the result in a float64 by itself.

See https://github.com/vais-ral/CCPi-Framework/pull/273 and https://github.com/vais-ral/CCPi-Framework/issues/292

Docstring conventions

Python Docstrings are written in restructured text. Normally main elements we would use are in the example below

def function(arg1, arg2):
    '''Use the first line to describe what the function/class does

    Leave an empty line and give more details if possible. Add cool LaTeX math as
    .. math::
      
      f(x) = c*||A*x-b||_2^2
    
    You can use lists as
      + this
      + is a
      + unordered list

    If the function has parameters add the reason of the parameter and values as:

    :params: :code:`arg1`: say something about the parameter
    :params: :code:`arg2`: as above
    '''
    pass
Clone this wiki locally