Useful Functions#

Here is an (incomplete) list of functions that you may find useful in your computer analysis. Clicking the links will take you to the documentation for each function which will explain how to use it. If you wish to use a numpy function you must first import numpy as np.

A complete list of the built in python functions and numpy functions are linked.

Built in Python Functions#

  • min() - returns the smallest value in a list/array.

  • max() - returns the largest value in a list/array.

  • len() - returns the length (number of value) in a list/array.

  • pow() - returns the value of one number to the power of a second.

  • abs() - returns the absolute value of a number.

  • range() - creates a sequence of numbers (start, stop and step can be defined), by default the sequence will start from 0 and end at N-1 in steps of 1.

Basic Trigonometric Functions#

  • np.sin() - calculates the sine of an angle theta, which itself must be provided in radians.

  • np.cos()- calculates the cosine of an angle theta, which itself must be provided in radians.

  • np.tan() - calculates the tan of an angle theta, which itself must be provided in radians.

  • np.arcsin() - calculates the inverse sine of a value, giving the answer in radians.

  • np.arccos() - calculates the inverse cosine of a value, giving the answer in radians.

  • np.arctan() - calculates the inverse tan of a value, giving the answer in radians.

  • np.radians() - converts an angle from degrees to radians.

  • np.degrees() - converts an angle from radians to degrees.

Rounding#

  • np.round() - rounds a value to the given number of decimals.

  • np.around() - rounds an array to a given number of decimals.

Basic Maths and Statistics#

  • np.linspace() - returns evenly spaced numbers over a specified interval.

  • np.prod() - returns the product of array elements over a given axis.

  • np.sum() - returns the sum of array elements over a given axis.

  • np.exp() - calculates the exponenital of a value or each elements in an array.

  • np.log() - calculates the natural log of a value or each element in an array.

  • np.log10() - calculates the base 10 logarithm for a value or each element in an array.

  • np.sqrt() - returns the non-negative square-root of an array, element-wise.

  • np.cbrt() - return the cube-root of an array, element-wise.

  • np.square() - return the element-wise square of the input.

  • np.abs() - calculates the absolute value element-wise.

  • np.mean() - calculates and returns the mean of an array.

  • np.std() - computes the standard deviation of the values in an array.

Note: the arguement “ddof = 1” must be added when calculating the standard deviation of a sample. Without this the equation used to calculate the standard deviation is that for the population:

\[ \sigma_{N-1} = \sqrt{\frac{\sum{(x-\bar{x})^2}}{N}} \]

rather than:

\[ \sigma_{N-1} = \sqrt{\frac{\sum{(x-\bar{x})^2}}{N-1}} \]

which is the formula we need (note the N-1 in the denominator).

See the example below as to how to calculate the mean, standard deviation and standard error for a sample of measurements:

import numpy as np

# First we load or enter our data into a numpy array.
Measurements = np.array([45, 50, 47, 56, 35, 54, 49, 48, 49, 43])

# Next, we perform our calculations.
average = np.mean(Measurements)
standard_deviation = np.std(Measurements, ddof = 1)
standard_error = standard_deviation / len(Measurements)

# Then we can print our results
print("The mean value of the measurements is: ", average, "\n")
print("The standard deviation of the measurements is: ", standard_deviation, "\n")
print("The mean standard error of the measurements is: ", standard_error, "\n")

# Then we can report our final value appropriately:
print("The best estimate of our results is: [" , np.round(average, 1), "+/-", np.round(standard_error, 1), "] Units.")
The mean value of the measurements is:  47.6 

The standard deviation of the measurements is:  5.853773711604051 

The mean standard error of the measurements is:  0.5853773711604051 

The best estimate of our results is: [ 47.6 +/- 0.6 ] Units.