On this page:
2.8.1 Exercises 5
2.8.1.1 Question 13
2.8.1.2 Question 14
2.8.1.3 Question 15
2.8.1.4 Question 16
2.8.1.5 Question 17

2.8 Predefined Functions

The possibility of defining new functions is fundamental for increasing the language’s flexibility and its ability to adapt to the problems we want to solve. The new functions, however, must be defined in terms of others that were either defined by the user or, at most, predefined in the language.

As we will see, Julia has a vast set of predefined functions. In many cases, they suffice for what we want to do. Nevertheless, we should not restrain from defining new functions whenever we deem it necessary.

In this table we see a set of mathematical functions predefined in Julia. Note that, due to syntax limitations (which are also present in other programming languages), there are cases in which Julia uses a notation that differs from the traditional mathematical notation. For example, the square root function \(\sqrt{x}\) is written as sqrt(x).

The name sqrt is a contraction of the words square and root

Similar mechanisms are used for several other functions, includind the absolute value function \(|x|\) (written abs(x)), the exponentiation \(x^y\) (written x^y), and the remainder between the numbers \(m\) and \(n\) (written m%n). This table shows some of the equivalencies between the invocations of Julia’s functions and the correspondent mathematics invocations.

Some mathematical functions predefined in Julia.

Function

 

Arguments

 

Result

abs

 

A number

 

The absolute value of the argument.

sin

 

A number

 

The sine of the argument (in radians).

cos

 

A number

 

The cosine of the argument (in radians).

atan

 

One or two numbers

 

With only one argument, the arc tangent of the argument (in radians). With two arguments, the arc tangent of the division between the first and the second, where the sign of the arguments is used to determine the quadrant.

sqrt

 

A number

 

The square root of the argument.

exp

 

A number

 

The exponential value with base \(e\) of the argument.

^

 

Two numbers

 

The first argument raised to the power of the second argument.

log

 

One or two arguments

 

With one argument, the natural logarithm of the argument. With two arguments, the logarithm of the second argument with the first argument as its base.

max

 

Multiple Numbers

 

The highest argument.

min

 

Multiple Numbers

 

The lowest argument.

round

 

A number

 

Rounds the argument to the nearest integer.

floor

 

A number

 

Rounds down the argument to the nearest integer.

ceil

 

A number

 

Rounds up the argument to the nearest integer.

Julia’s predefined math functions.

Julia

 

Mathematics

abs(x)

 

\(|x|\)

sin(x)

 

\(\sin x\)

cos(x)

 

\(\cos x\)

atan(x)

 

\(\arctan x\)

atan(y, x)

 

\(\arctan \frac{y}{x}\)

sqrt(x)

 

\(\sqrt{x}\)

exp(x)

 

\(e^x\)

x^y

 

\(x^y\)

log(x)

 

\(\log_e x\)

floor(x)

 

\(\lfloor x\rfloor\)

ceil(x)

 

\(\lceil x\rceil\)

2.8.1 Exercises 5
2.8.1.1 Question 13

Translate the following mathematical expressions into Julia’s notation:
  1. \(\sqrt{\frac{1}{\log 2^{\left|(3-9\log 25)\right|}}}\)

  2. \(\frac{\cos^4 \frac{2}{\sqrt 5}}{\arctan 3}\)

  3. \(\frac{1}{2} + \sqrt 3 + \sin^{\frac{5}{2}} 2\)

2.8.1.2 Question 14

Translate the following Julia expressions into mathematical notation:
  1. log(sin(2^4+floor(atan(pi))/sqrt(5)+pi))

  2. cos(cos(cos(0.5)))^5

  3. sin(cos(sin(pi/3)/3)/3)

2.8.1.3 Question 15

Define the function odd that, for a given number, evaluates if it is odd, i.e., if the remainder of that number when divided by two is one.

2.8.1.4 Question 16

The area \(A\) of a pentagon inscribed in a circle of radius \(r\) is given by the following expression: \[A = \frac{5}{8}r^2\sqrt{10 + 2\sqrt{5}}\] Define a function that calculates this area and test it with values of your choice.

2.8.1.5 Question 17

Define a function that calculates the volume of an ellipsoid with semi-axes \(a\), \(b\) and \(c\). This volume can be obtained by using the formula: \(V=\frac{4}{3}\pi a b c\)