Engee documentation
Notebook

Exponentiation, root extraction, and logarithmization

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

Exponentiation

Let's define a square matrix integers of dimension 3$\times$3.

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

Exponentiation of a matrix equivalent to matrix multiplication Look at yourself once.

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

Exponentiation of a matrix is equivalent to piecemeal multiplication of the matrix by itself. For example: .

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 exponentiation 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

Negative exponentiation 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

This operation corresponds to the multiplication of the inverse matrix Look at yourself once.

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

Consider exponentiation with an exponent in the form of a rational number or a 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

Calculate the square root of the matrix There are several ways to do this:

  • using the function sqrt();
  • exponentiation ;
  • 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 extracting the square root, there is its element-wise 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 the integer square root, you can use the function isqrt():

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

Calculate the cubic root There are several ways to do this:

  • 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

Calculating the cubic root of matrices and roots of large orders it is possible only through exponentiation .

For the operation and functions cbrt() Piecemeal matrix calculation 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

It should be noted that when calculating the root of an even order of a negative real number, an error will be generated. For correct calculations in this case, the root expression must be transformed into a complex representation.

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

Exponent and exponential functions

The calculation of the exponent of a number is performed as follows:

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

For accurate calculation , when near zero, you can use the function expm1()

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

For comparison, the result of the calculation is near zero. exp():

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

There are also functions for calculating degrees based on bases 2 and 10.

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

To efficiently calculate the degree from the expression The function is used ldexp(x,n), where x is a floating-point number and n is an integer.

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

Logarithmization

To calculate the natural logarithm, the function is used log().

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

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

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

The logarithm on a given basis calculated using the function log(a,b).

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

For accurate 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) it will give the following result:

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

Conclusion

In this demo, we looked at the basic techniques for using operations and functions to calculate the degree, root, exponent, and logarithm. Additional information on the listed operations and functions can be found in the Engee documentation. Mathematical operations and elementary functions.