May 14, 2021 Julia
Julia provides a complete set of basic arithmetic and bit operations for all its base numerical types, as well as an efficient, portable set of standard mathematical functions.
The following arithmetic operators apply to all basic numeric types:
The expression | Name | Describe |
---|---|---|
+x
|
One dollar plus method | x itself |
-x
|
One-dollar subtract | The opposite number |
x + y
|
Binary addition | Do the addition |
x - y
|
Binary subtract | Do subtract |
x * y
|
Multiplication | Do multiplication |
x / y
|
Division | Do the divide |
x \ y
|
Inverse | Equivalent to y / x |
x ^ y
|
Multiplicity | The y-power of x |
x % y
|
Take the rest | Equivalent to rem (x, y) |
and
Bool
of the Bool type:
The expression | Name | Describe |
---|---|---|
!x
|
Non - | true and false interchange |
Julia's type-boosting system makes arithmetic operations with mixed parameter types simple and natural. See Type Conversion and Type Promotion for details.
Examples of arithmetic operations:
julia> 1 + 2 + 3
6
julia> 1 - 2
-1
julia> 3*2/12
0.5
(Customary, low-priority operations, fill in more spaces before and after.) T his is not mandatory. )
The following bit operators apply to all integer types:
The expression | Name |
---|---|
~x
|
Reverse by bit |
x & y
|
By bit with |
x \| y
|
By bit or |
x $ y
|
By bit or |
x >>> y
|
Right Logical Shift (High Fill 0) |
x >> y
|
Arithmetic shift to the right (copy the original high position) |
x << y
|
Left logical/arithmetic shift |
Examples of bit operations:
julia> ~123
-124
julia> 123 & 234
106
julia> 123 | 234
251
julia> 123 $ 234
145
julia> ~uint32(123)
0xffffff84
julia> ~uint8(123)
0x84
Binary arithmetic and bit operations have corresponding compound assignment operators, i.e. the results of the operation are assigned to the left operand. T
he compound assignment operator is composed
=
a direct addition to the operator.
For example,
x += 3
equivalent to x s x s
x = x + 3
julia> x = 1
1
julia> x += 3
4
julia> x
4
The compound assignment operators are:
+= -= *= /= \= %= ^= &= |= $= >>>= >>= <<=
The comparison operator can be used for all underlying numeric types:
Operator | Name |
---|---|
==
|
Equals |
!=
|
Not equal to |
<
|
Less than |
<=
|
Less than or equal to |
>
|
Greater than |
>=
|
Is greater than or equal to |
Some examples:
julia> 1 == 1
true
julia> 1 == 2
false
julia> 1 != 2
true
julia> 1 == 1.0
true
julia> 1 < 2
true
julia> 1.0 > 3
false
julia> 1 >= 1.0
true
julia> -1 <= 1
true
julia> -1 <= -1
true
julia> -1 <= -2
false
julia> 3 < -0.5
false
Integers are compared by bit. Floating points are compared to the IEEE 754 standard:
Inf
equal to itself and is greater than all numbers, except
NaN
-Inf
equal to itself and is less than all numbers, except
NaN
NaN
not equal to, not greater than, not less than any number, including itself.
The last one above is about the
NaN
which is worth noting:
julia> NaN == NaN
false
julia> NaN != NaN
true
julia> NaN < NaN
false
julia> NaN > NaN
false
NaN
cause
some
trouble when used in a matrix:
julia> [1 NaN] == [1 NaN]
false
Julia provides additional functions to test these special values, which use hash values to compare:
Function | Test |
---|---|
isequal(x, y)
|
x whether it is equivalent to y |
isfinite(x)
|
x is a finite number |
isinf(x)
|
x is an infinite number |
isnan(x)
|
x is not a number |
isequal
which considers
NaN
be equal to itself:
julia> isequal(NaN,NaN)
true
julia> isequal([1 NaN], [1 NaN])
true
julia> isequal(NaN,NaN32)
true
isequal
also be used to distinguish between signed zeros:
julia> -0.0 == 0.0
true
julia> isequal(-0.0, 0.0)
false
Unlike most languages, Julia supports Python chain comparisons:
julia> 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
true
For the comparison of the
&&
calibration, the chain comparison uses the operator, and the element-by-element comparison uses
&
can also be used for arrays.
For example,
0 .< A .< 1
a corresponding Boolean array, with elements that meet the criteria
true
The operator
.<
specifically for arrays; O
nly when
A
and
B
are the same big,
A .< B
is legal. T
he result of the comparison is a Boolean array, the same size
A
B
S
uch operators are
called by
element operators; J
ulia provides a complete set of by-element
.*
.+
and so on. S
ome element-by-element operators can also accept pure quantities, such as
0 .< A .< B
.
This notation means that the corresponding pure operator is applied to each element.
Note the order in which chain comparisons are compared:
v(x) = (println(x); x)
julia> v(1) < v(2) <= v(3)
2
1
3
true
julia> v(1) > v(2) <= v(3)
2
1
false
The median value is calculated only once, not twice as
v(1) < v(2) && v(2) <= v(3)
H
owever, the order in which chain comparisons are calculated is uncertain. D
o not use expressions with side effects, such as printing, in chain comparisons.
If you need to use side effect expressions, it is
&&
to use the short-circuit operator (see
Short-circuit Evaluation for details).
Julia's operation priorities are from highest to lowest:
Type | Operator |
---|---|
Grammar |
.
Follow
::
|
Power |
^
And
.^
equivalent
|
Scores |
//
And
.//
|
Multiply and divide |
/ % & \
And
.* ./ .% .\
|
Displacement |
<< >> >>>
And
.<< .>> .>>>
|
Add or subtract |
+ - | $
And
.+ .-
|
Grammar |
: ..
Follow
|>
|
Comparison |
> < >= <= == === != !== <:
And
.> .< .>= .<= .== .!=
|
Logic |
&&
Follow the
||
follow
?
|
Assignment |
= += -= *= /= //= \= ^= %= \|= &= $= <<= >>= >>>=
And
.+= .-= .*= ./= .//= .\= .^= .%=
|
Julia offers a range of mathematical functions and operators:
Function | Describe | Returns the type |
---|---|---|
round(x)
|
Round x to the nearest integer | FloatingPoint |
iround(x)
|
Round x to the nearest integer | Integer |
floor(x)
|
Round x to -Inf | FloatingPoint |
ifloor(x)
|
Round x to -Inf | Integer |
ceil(x)
|
Round the x to the .Inf | FloatingPoint |
iceil(x)
|
Round the x to the .Inf | Integer |
trunc(x)
|
Round x to 0 | FloatingPoint |
itrunc(x)
|
Round x to 0 | Integer |
Function | Describe |
---|---|
div(x,y)
|
Truncation and dichoilation; |
fld(x,y)
|
Rounding down and divide; |
cld(x,y)
|
The wholeing and divide method is taken up; The trade-in is rounded to the .Inf |
rem(x,y)
|
Divide the remaining part; meet x s div (x, y) s y s rem (x,y), with the same number as x |
divrem(x,y)
|
Return (div (x,y), rem (x,y)) |
mod(x,y)
|
To take the remaining part of the module; to satisfy x sfld (x,y) s y s mod (x,y), the same number as y |
mod2pi(x)
|
Take the remaining part of the 2pi module; 0 slt; . . . mod2pi(x) . |
gcd(x,y...)
|
x, y, ... The maximum number of conventions, the same number as x |
lcm(x,y...)
|
x, y, ... the minimum male multiplied, with the same number as x |
Function | Describe |
---|---|
abs(x)
|
The amplitude of x |
abs2(x)
|
The square of the amplitude of x |
sign(x)
|
X has a positive or negative sign with a return value of -1, 0, or 1 |
signbit(x)
|
Is there a symbol bit, there is (true) or there is no (false) |
copysign(x,y)
|
Returns a number that has the amplitude of x, the sign bit of y |
flipsign(x,y)
|
Returns a number that has the amplitude of x, the sign bit of x.y |
Function | Describe |
---|---|
sqrt(x)
|
√ the square root of x x |
cbrt(x)
|
The cubic root of ?x x |
hypot(x,y)
|
sqrt with a smaller error (x^2 plus y^2) |
exp(x)
|
The x-power of the natural index e |
expm1(x)
|
When x is close to 0, exp(x)-1 is calculated precisely |
ldexp(x,n)
| efficiently calculate
x*2^n
|
|
log(x)
|
The natural pair of x |
log(b,x)
|
The number of pairs with b as the bottom x |
log2(x)
|
A pair of xs with 2 as the base |
log10(x)
|
A pair of xs with a base of 10 |
log1p(x)
|
When x is close to 0, the log is calculated accurately (1 plus x) |
exponent(x)
|
trunc(log2(x)) |
significand(x)
|
returns the binary significand (a.k.a. mantissa) of a floating-point number x |
Why there
hypot
expm1
log1p
functions, see John D. Cook's blog:
expm1,
log1p,
erfc,
and hypot.
Julia has all the standard triangular and bik functions built in
sin cos tan cot sec csc
sinh cosh tanh coth sech csch
asin acos atan acot asec acsc
asinh acosh atanh acoth asech acsch
sinc cosc atan2
All
but atan2
are single-argument functions.
atan2
x
of the
x
the points determined by x
and
y
In addition,
sinpi(x)
cospi(x)
provided with more accurate calculations of
sin(pi*x)
and
cos(pi*x)
respectively.
If you want to calculate a triangular function in degrees, not radians, you should use a function with a d suffix. F or example, sind(x) calculates the sine value of x, where the unit of x is degrees. The following list is all triangular functions in degrees:
sind cosd tand cotd secd cscd
asind acosd atand acotd asecd acscd
Function | Describe |
---|---|
erf(x)
|
The error function at x |
erfc(x)
|
Make up the error function. Accurate calculation of 1-erf(x) when x is large |
erfinv(x)
|
The anti-function of erf |
erfcinv(x)
|
The anti-function of erfc |
erfi(x)
|
Define the error function as -im erf (x im), where im is a unit of virtual number |
erfcx(x)
|
Complementary error function of scaling, i.e. accurate exp for larger x values (x x 2) x erfc(x) |
dawson(x)
|
The zoom virtual error function, also known as the Dawson function, is the exact exp (-x^2) erfi(x) sqrt(pi) / 2 for larger x values |
gamma(x)
|
The gamma function at x |
lgamma(x)
|
Accurate calculation log (gamma(x)) when x is large |
lfact(x)
|
Find a precise log for the larger x (factorial(x)); Equal to the x value of more than 1, lgamma (x1), otherwise equal to 0 |
digamma(x)
|
The digamma function at x, which is a derivative of the derivative |
beta(x,y)
|
Beta function at (x, y). |
lbeta(x,y)
|
Get accurate log (beta(x,y)) for larger x or y values |
eta(x)
|
Dirichlet eta function at x |
zeta(x)
|
The Riemann zeta function at x |
airy(z)
,
airyai(z)
,
airy(0,z)
|
The Airy Ai function at z |
airyprime(z)
,
airyaiprime(z)
,
airy(1,z)
|
The conductor at z of the Airy Ai function |
airybi(z)
,
airy(2,z)
|
The Airy Bi function at z |
airybiprime(z)
,
airy(3,z)
|
The conductor at z of the Airy Bi function |
airyx(z)
,
airyx(k,z)
|
Scale the Airy Ai function and the k-to-z guide |
besselj(nu,z)
|
The Bezier function for z-level nu |
besselj0(z)
|
besselj(0,z) |
besselj1(z)
|
besselj(1,z) |
besseljx(nu,z)
|
The scaling Bezier function for z-level nu |
bessely(nu,z)
|
Bezier function for second-order nu in z |
bessely0(z)
|
bessely(0,z) |
bessely1(z)
|
bessely(1,z) |
besselyx(nu,z)
|
The scaling Bezier function for second-order nu in z |
besselh(nu,k,z)
|
The Bezier function for the third-order nu in z (e.g. the Hankel function); |
hankelh1(nu,z)
|
besselh(nu, 1, z) |
hankelh1x(nu,z)
|
Zoom besselh (nu, 1, z) |
hankelh2(nu,z)
|
besselh(nu, 2, z) |
hankelh2x(nu,z)
|
Zoom Besselh (Nu, 2, Z) |
besseli(nu,z)
|
The corrected Bezier function for z-level nu |
besselix(nu,z)
|
The scaling of z-level nu corrects the Bezier function |
besselk(nu,z)
|
The corrected Bezier function for second-order nu in z |
besselkx(nu,z)
|
The scaling correction for second-order o corrects the Bezier function |