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.
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.
X ^ 2
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$.
X .^ 2
The following expressions can be used for degree expansion with assignment:
X ^= 2
X = [3 1 4
5 1 3
2 8 7];
X .^= 2
Elevation to negative degree is also supported:
X = [3 1 4
5 1 3
2 8 7];
X ^ (-3)
Such an operation corresponds to multiplying the inverse matrix $X^{-1}$ itself by itself $n-1$ times.
isequal(X^(-3), inv(X)^3)
Let's consider ascension to a degree with an exponent in the form of a rational or floating-point number:
Q = 2//3;
q = 2/3;
X ^ Q
isequal(X^Q, X^q)
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
√
.
sqrt(X)
isequal(√X,sqrt(X))
For each of the methods of square root extraction there is its element-by-element analogue:
sqrt.(X)
isequal(.√X,sqrt.(X)) && isequal(X.^(0.5),sqrt.(X))
To calculate an integer square root, you can use the function isqrt()
:
isqrt(26)
You can calculate the cube root $y = \sqrt[3]{x}$ in several ways:
- using the function
cbrt()
; - using the cube root operator
∛
.
x = 1e3;
cbrt(x)
isequal(∛x,cbrt(x))
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.
cbrt.(X)
isequal(.∛x,cbrt.(x))
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.
sqrt(complex(-1))
Exponent and exponential functions¶
Calculating the exponent of a number is done as follows:
exp(1)
To accurately calculate $exp(a)-1$, with $a$ near zero, you can use the function expm1()
expm1(eps()/10)
For comparison, the result of calculating $a$ near zero with the function exp()
:
exp(eps()/10)-1.0
There are also functions for calculating degree by base 2 and base 10.
exp2(10)
exp10(7)
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.
ldexp(2.0,10)
Logarithmisation¶
The function log()
. is used to calculate the natural logarithm.
log(exp(1))
You can also calculate the logarithm on bases 2 and 10:
log2(2048)
log10(1e-6)
The logarithm of $x=log_a{b}$ on a given base $a$ is calculated using the function log(a,b)
.
log(8,512)
For exact calculation of the natural logarithm log(x+1)
for x near zero, the function is used:
log1p(eps()/10)
For comparison, the standard function log(1+x)
will produce the following result:
log(1+eps()/2)
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).