# How to do basic mathematical computations

## Description

How do we write the most common mathematical operations in a given piece of software? For example, how do we write multiplication, or exponentiation, or logarithms, in Python vs. R vs. Excel, and so on?

## Solution, in Excel

View this solution alone.

Each of the formulas shown on the right of the table below is valid code for use in Excel formulas, in cells in a worksheet.

Mathematical notation Excel notation Example
$x+y$ x+y =A1+B1
$x-y$ x-y =A1-B1
$xy$ x*y =A1*B1
$\frac xy$ x/y =A1/B1
$x^y$ x^y =A1^B1
$\vert x\vert$ ABS(x) =ABS(A1)
$\ln x$ LN(x) =LN(A1)
$\log_a b$ LOG(b,a) =LOG(A1,B1)
$e^x$ EXP(x) =EXP(A1)
$\pi$ PI() =PI()
$\sin x$ SIN(x) =SIN(A1)
$\sin^{-1} x$ ASIN(x) =ASIN(A1)
$\sqrt x$ SQRT(x) =SQRT(A1)

Other trigonometric functions are also available besides just SIN, including COS, TAN, etc.

See a problem? Tell us or edit the source.

## Solution, in Julia

View this solution alone.

Mathematical notation Julia code
$x+y$ x+y
$x-y$ x-y
$xy$ x*y
$\frac xy$ x/y (or y\x)
$\left\lfloor\frac xy\right\rfloor$ x÷y
remainder of $x\div y$ x%y
$x^y$ x^y
$\vert x\vert$ abs(x)
$\ln x$ log(x)
$\log_a b$ log(a,b)
$e^x$ exp(x)
$\pi$ pi
$\sin x$ sin(x)
$\sin^{-1} x$ asin(x)
$\sqrt x$ sqrt(x)

Other trigonometric functions are also available besides just sin including cos, tan, etc.

See a problem? Tell us or edit the source.

## Using NumPy, in Python

View this solution alone.

This answer assumes you have imported NumPy as follows.

1
import numpy as np

Mathematical notation Python code Requires NumPy?
$x+y$ x+y no
$x-y$ x-y no
$xy$ x*y no
$\frac xy$ x/y no
$\left\lfloor\frac xy\right\rfloor$ x//y no
$\left\lfloor\frac xy\right\rfloor$ np.floor_divide(x,y) yes
remainder of $x\div y$ x%y no
remainder of $x\div y$ np.remainder(x,y) yes
$x^y$ x**y no
$\vert x\vert$ abs(x) no
$\vert x\vert$ np.abs(x) yes
$\ln x$ np.log(x) yes
$\log_a b$ np.log(b)/np.log(a) yes
$e^x$ np.exp(x) yes
$\pi$ np.pi yes
$\sin x$ np.sin(x) yes
$\sin^{-1} x$ np.asin(x) yes
$\sqrt x$ x**0.5 no
$\sqrt x$ np.sqrt(x) yes

Other trigonometric functions are also available besides just np.sin, including np.cos, np.tan, etc.

NumPy automatically applies any of these functions to all entries of a NumPy array or pandas Series, but the built-in Python functions do not have this feature. For example, to square all numbers in an array, see below.

1
2
3
import numpy as np
example_array = np.array( [ -3, 2, 0.5, -1, 10, 9.2, -3.3 ] )
example_array ** 2

1
array([  9.  ,   4.  ,   0.25,   1.  , 100.  ,  84.64,  10.89])


See a problem? Tell us or edit the source.

## Using SymPy, in Python

View this solution alone.

This answer assumes you have imported SymPy as follows.

1
2
from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output

Mathematical notation Python code Requires SymPy?
$x+y$ x+y no
$x-y$ x-y no
$xy$ x*y no
$\frac xy$ x/y no
$\left\lfloor\frac xy\right\rfloor$ x//y no
remainder of $x\div y$ x%y no
$x^y$ x**y no
$\vert x\vert$ abs(x) no
$\ln x$ log(x) yes
$\log_a b$ log(b,a) yes
$e^x$ E yes
$\pi$ pi yes
$\sin x$ sin(x) yes
$\sin^{-1} x$ asin(x) yes
$\sqrt x$ sqrt(x) yes

Other trigonometric functions are also available besides just sin, including cos, tan, etc.

Note that SymPy gives precise answers to mathematical queries, which may not be what you want.

1
sqrt(2)


$\displaystyle \sqrt{2}$

If you want a decimal approximation instead, you can use the N function.

1
N(sqrt(2))


$\displaystyle 1.4142135623731$

Or you can use the evalf function.

1
sqrt(2).evalf()


$\displaystyle 1.4142135623731$

By contrast, if you need an exact rational number when Python gives you an approximation, you can use the Rational function to build one. Note the differences below:

1
1/3


$\displaystyle 0.333333333333333$

1
Rational(1,3)


$\displaystyle \frac{1}{3}$

See a problem? Tell us or edit the source.

## Solution, in Python

View this solution alone.

For those expressions that need the Python math package, use the code import math beforehand to ensure that package is loaded. Alternatively, you can write from math import * and thus drop the math prefixes in the table below.

Mathematical notation Python code Requires math package?
$x+y$ x+y no
$x-y$ x-y no
$xy$ x*y no
$\frac xy$ x/y no
$\left\lfloor\frac xy\right\rfloor$ x//y no
remainder of $x\div y$ x%y no
$x^y$ x**y no
$\vert x\vert$ abs(x) no
$\ln x$ math.log(x) yes
$\log_a b$ math.log(b,a) yes
$e^x$ math.exp(x) yes
$\pi$ math.pi yes
$\sin x$ math.sin(x) yes
$\sin^{-1} x$ math.asin(x) yes
$\sqrt x$ x**0.5 no
$\sqrt x$ math.sqrt(x) yes

Other trigonometric functions are also available besides just math.sin, including math.cos, math.tan, etc.

See a problem? Tell us or edit the source.

## Solution, in R

View this solution alone.

For those expressions that need the Python math package, use the code import math beforehand to ensure that package is loaded. Alternatively, you can write from math import * and thus drop the math prefixes in the table below.

Mathematical notation R code
$x+y$ x+y
$x-y$ x-y
$xy$ x*y
$\frac xy$ x/y
$x^y$ x^y
$\vert x\vert$ abs(x)
$\ln x$ log(x)
$\log_a b$ log(b,a)
$e^x$ exp(x)
$\pi$ pi
$\sin x$ sin(x)
$\sin^{-1} x$ asin(x)
$\sqrt x$ sqrt(x)

Other trigonometric functions are also available besides just sin, including cos, tan, etc.

R naturally applies these functions across vectors. For example, you can square all the entries in a vector as in the example below.

1
2
example.vector <- c( -3, 2, 0.5, -1, 10, 9.2, -3.3 )
example.vector ^ 2

1
[1]   9.00   4.00   0.25   1.00 100.00  84.64  10.89