Link Search Menu Expand Document (external link)

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
xy x-y =A1-B1
xy x*y =A1*B1
xy x/y =A1/B1
xy x^y =A1^B1
|x| ABS(x) =ABS(A1)
lnx LN(x) =LN(A1)
logab LOG(b,a) =LOG(A1,B1)
ex EXP(x) =EXP(A1)
π PI() =PI()
sinx SIN(x) =SIN(A1)
sin1x ASIN(x) =ASIN(A1)
x SQRT(x) =SQRT(A1)

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

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Solution, in Julia

View this solution alone.

Mathematical notation Julia code
x+y x+y
xy x-y
xy x*y
xy x/y (or y\x)
xy x÷y
remainder of x÷y x%y
xy x^y
|x| abs(x)
lnx log(x)
logab log(a,b)
ex exp(x)
π pi
sinx sin(x)
sin1x asin(x)
x sqrt(x)

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

Content last modified on 24 July 2023.

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
xy x-y no
xy x*y no
xy x/y no
xy x//y no
xy np.floor_divide(x,y) yes
remainder of x÷y x%y no
remainder of x÷y np.remainder(x,y) yes
xy x**y no
|x| abs(x) no
|x| np.abs(x) yes
lnx np.log(x) yes
logab np.log(b)/np.log(a) yes
ex np.exp(x) yes
π np.pi yes
sinx np.sin(x) yes
sin1x np.asin(x) yes
x x**0.5 no
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
array([  9.  ,   4.  ,   0.25,   1.  , 100.  ,  84.64,  10.89])

Content last modified on 24 July 2023.

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
xy x-y no
xy x*y no
xy x/y no
xy x//y no
remainder of x÷y x%y no
xy x**y no
|x| abs(x) no
lnx log(x) yes
logab log(b,a) yes
ex E yes
π pi yes
sinx sin(x) yes
sin1x asin(x) yes
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)

2

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

1
N(sqrt(2))

1.4142135623731

Or you can use the evalf function.

1
sqrt(2).evalf()

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

0.333333333333333

1
Rational(1,3)

13

Content last modified on 24 July 2023.

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
xy x-y no
xy x*y no
xy x/y no
xy x//y no
remainder of x÷y x%y no
xy x**y no
|x| abs(x) no
lnx math.log(x) yes
logab math.log(b,a) yes
ex math.exp(x) yes
π math.pi yes
sinx math.sin(x) yes
sin1x math.asin(x) yes
x x**0.5 no
x math.sqrt(x) yes

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

Content last modified on 24 July 2023.

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
xy x-y
xy x*y
xy x/y
xy x^y
|x| abs(x)
lnx log(x)
logab log(b,a)
ex exp(x)
π pi
sinx sin(x)
sin1x asin(x)
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]   9.00   4.00   0.25   1.00 100.00  84.64  10.89

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Topics that include this task