Engee documentation
Notebook

Degree expansion, root extraction and logarithmisation

This example shows different ways to find the degree, root, exponent and logarithm

Degree expansion

Let's set a square matrix $X$ of integers with dimension 3$\times$3.

In [ ]:
X = [3 1 4
     5 1 3
     2 8 7];

Elevation of the matrix to degree $Y=X^n$ is equal to matrix multiplication $X$ of itself by itself $n-1$ times.

In [ ]:
X ^ 2
Out[0]:
3×3 Matrix{Int64}:
 22  36  43
 26  30  44
 60  66  81

The element-by-element expansion of a matrix into a degree is equivalent to the element-by-element multiplication of the matrix by itself. For example: $Y =X^2= X \odot X$.

In [ ]:
X .^ 2
Out[0]:
3×3 Matrix{Int64}:
  9   1  16
 25   1   9
  4  64  49

The following expressions can be used for degree expansion with assignment:

In [ ]:
X ^= 2
Out[0]:
3×3 Matrix{Int64}:
 22  36  43
 26  30  44
 60  66  81
In [ ]:
X = [3 1 4
     5 1 3
     2 8 7];
X .^= 2
Out[0]:
3×3 Matrix{Int64}:
  9   1  16
 25   1   9
  4  64  49

Elevation to negative degree is also supported:

In [ ]:
X = [3 1 4
     5 1 3
     2 8 7];
X ^ (-3)
Out[0]:
3×3 Matrix{Float64}:
  0.057581   -0.051794     -0.00260417
  0.0529514  -0.000868056  -0.0257523
 -0.0792824   0.0353009     0.0225694

Such an operation corresponds to multiplying the inverse matrix $X^{-1}$ itself by itself $n-1$ times.

In [ ]:
isequal(X^(-3), inv(X)^3)
Out[0]:
true

Let's consider ascension to a degree with an exponent in the form of a rational or floating-point number:

In [ ]:
Q = 2//3;
q = 2/3;
X ^ Q
Out[0]:
3×3 Matrix{ComplexF64}:
   2.33466+2.22045e-16im  -0.261273-4.44089e-16im   1.58405+1.11022e-16im
   2.70242-3.33067e-16im   0.774717+0.0im          0.882171+1.11022e-16im
 -0.210172+0.0im            3.68872+0.0im           3.50221-2.22045e-16im
In [ ]:
isequal(X^Q, X^q)
Out[0]:
true

Root extraction

There are several ways to calculate the square root of a matrix $Y = \sqrt{X}$:

  • by using the function sqrt();
  • by increasing in degree $\frac{1}{2}$;
  • using the square root operator .
In [ ]:
sqrt(X)
Out[0]:
3×3 Matrix{Float64}:
  2.02239   -0.373716  0.907713
  1.79475    0.866793  0.401277
 -0.461978   2.29118   2.5495
In [ ]:
isequal(X,sqrt(X))
Out[0]:
true

For each of the methods of square root extraction there is its element-by-element analogue:

In [ ]:
sqrt.(X)
Out[0]:
3×3 Matrix{Float64}:
 1.73205  1.0      2.0
 2.23607  1.0      1.73205
 1.41421  2.82843  2.64575
In [ ]:
isequal(.√X,sqrt.(X)) && isequal(X.^(0.5),sqrt.(X))
Out[0]:
true

To calculate an integer square root, you can use the function isqrt():

In [ ]:
isqrt(26)
Out[0]:
5

You can calculate the cube root $y = \sqrt[3]{x}$ in several ways:

  • using the function cbrt();
  • using the cube root operator .
In [ ]:
x = 1e3;
cbrt(x)
Out[0]:
10.0
In [ ]:
isequal(x,cbrt(x))
Out[0]:
true

The computation of the cube root of matrices and roots of large orders $n$ is possible only by degree expansion $\frac{1}{n}$.

For the operation and the function cbrt() the element-by-element calculation of matrices is also available.

In [ ]:
cbrt.(X)
Out[0]:
3×3 Matrix{Float64}:
 1.44225  1.0  1.5874
 1.70998  1.0  1.44225
 1.25992  2.0  1.91293
In [ ]:
isequal(.∛x,cbrt.(x))
Out[0]:
true

Please note that an error will be generated when calculating an even-order root of a negative real number. For correct calculations in this case, the sub-root expression must be converted to a complex representation.

In [ ]:
sqrt(complex(-1))
Out[0]:
0.0 + 1.0im

Exponent and exponential functions

Calculating the exponent of a number is done as follows:

In [ ]:
exp(1)
Out[0]:
2.718281828459045

To accurately calculate $exp(a)-1$, with $a$ near zero, you can use the function expm1()

In [ ]:
expm1(eps()/10)
Out[0]:
2.2204460492503132e-17

For comparison, the result of calculating $a$ near zero with the function exp():

In [ ]:
exp(eps()/10)-1.0
Out[0]:
0.0

There are also functions for calculating degree by base 2 and base 10.

In [ ]:
exp2(10)
Out[0]:
1024.0
In [ ]:
exp10(7)
Out[0]:
1.0e7

To efficiently calculate the degree from the expression $x\cdot2^{n}$, we use the function ldexp(x,n), where x is a floating point number, n is an integer.

In [ ]:
ldexp(2.0,10)
Out[0]:
2048.0

Logarithmisation

The function log(). is used to calculate the natural logarithm.

In [ ]:
log(exp(1))
Out[0]:
1.0

You can also calculate the logarithm on bases 2 and 10:

In [ ]:
log2(2048)
Out[0]:
11.0
In [ ]:
log10(1e-6)
Out[0]:
-6.0

The logarithm of $x=log_a{b}$ on a given base $a$ is calculated using the function log(a,b).

In [ ]:
log(8,512)
Out[0]:
3.0

For exact calculation of the natural logarithm log(x+1) for x near zero, the function is used:

In [ ]:
log1p(eps()/10)
Out[0]:
2.2204460492503132e-17

For comparison, the standard function log(1+x) will produce the following result:

In [ ]:
log(1+eps()/2)
Out[0]:
0.0

Conclusion

In this demo, we have covered the basic techniques for using operations and functions to calculate degree, root, exponent, and logarithm. For more information on these operations and functions, see the Engee [Mathematical Operations and Elementary Functions] documentation (https://engee.com/helpcenter/stable/julia/manual/mathematical-operations.html).