Julia 为它所有的基础数值类型,提供了整套的基础算术和位运算,也提供了一套高效、可移植的标准数学函数。

3.1 算数运算符(Arithmetic Operators)

Expression Name Description
+x unary plus the identity operation
-x unary minus maps values to their additive inverses
x + y binary plus performs addition
x - y binary minus performs subtraction
x * y times performs multiplication
x / y divide performs division
x \ y inverse divide equivalent to y / x
x ^ y power raises x to the yth power
x % y remainder equivalent to rem(x,y)

以及bool类型的“否”

Expression Name Description
!x negation changes true to false and vice versa

3.2 位运算(Bitwise Operators)

Expression Name
~x bitwise not
x & y bitwise and
`x y` bitwise or
x ⊻ y bitwise xor (exclusive or)
x >>> y logical shift right
x >> y arithmetic shift right
x << y logical/arithmetic shift left

3.3 复合赋值运算(Updating Operators)

  1. += -= *= /= \= ÷= %= ^= &= |= ⊻= >>>= >>= <<=

An updating operator rebinds the variable on the left-hand side. As a result, the type of the variable may change.

  1. julia> x = 0x01; typeof(x)
  2. UInt8
  3. julia> x *= 2 # Same as x = x * 2
  4. 2
  5. julia> typeof(x)
  6. Int64

3.4 向量点乘运算(Vectorized Dot Operators)

[1,2,3] .^ 3 is defined as computing the elementwise (or “vectorized”) result [1^3, 2^3, 3^3].

  1. julia> [1,2,3] .^ 3
  2. 3-element Array{Int64,1}:
  3. 1
  4. 8
  5. 27

注意:向量点乘支持updating operator

同时,julia支持向量kronecker乘积运算
if you define ⊗(A,B) = kron(A,B) to give a convenient infix syntax A ⊗ B for Kronecker products (kron), then [A,B] .⊗ [C,D] will compute [A⊗C, B⊗D] with no additional coding.

  1. julia> a = [[1 2 3];[6 7 8]]
  2. 2×3 Array{Int64,2}:
  3. 1 2 3
  4. 6 7 8
  5. julia> b= [[6 7];[8 9];[9 10]]
  6. 3×2 Array{Int64,2}:
  7. 6 7
  8. 8 9
  9. 9 10
  10. julia> c = kron(a,b)
  11. 6×6 Array{Int64,2}:
  12. 6 7 12 14 18 21
  13. 8 9 16 18 24 27
  14. 9 10 18 20 27 30
  15. 36 42 42 49 48 56
  16. 48 54 56 63 64 72
  17. 54 60 63 70 72 80

3.5 比较运算符(Comparison Operators)

Operator Name
== equality
!=, inequality
< less than
<=, less than or equal to
> greater than
>=, greater than or equal to
  1. julia> 1 == 1
  2. true
  3. julia> 1 == 2
  4. false
  5. julia> 1 != 2
  6. true
  7. julia> 1 == 1.0
  8. true
  9. julia> 1 < 2
  10. true
  11. julia> 1.0 > 3
  12. false
  13. julia> 1 >= 1.0
  14. true
  15. julia> -1 <= 1
  16. true
  17. julia> -1 <= -1
  18. true
  19. julia> -1 <= -2
  20. false
  21. julia> 3 < -0.5
  22. false

Integers are compared in the standard manner – by comparison of bits. Floating-point numbers are compared according to the IEEE 754 standard:

  • Finite numbers are ordered in the usual manner.

  • Positive zero is equal but not greater than negative zero.

  • Inf is equal to itself and greater than everything else except NaN.

  • -Inf is equal to itself and less then everything else except NaN.

  • NaN is not equal to, not less than, and not greater than anything, including itself.

  1. julia> NaN == NaN
  2. false
  3. julia> NaN != NaN
  4. true
  5. julia> NaN < NaN
  6. false
  7. julia> NaN > NaN
  8. false
  9. julia> [1 NaN] == [1 NaN]
  10. false

Julia provides additional functions to test numbers for special values, which can be useful in situations like hash key comparisons:

Function Tests if
isequal(x, y) x and y are identical
isfinite(x) x is a finite number
isinf(x) x is infinite
isnan(x) x is not a number

3.6 链式比较(Chaining Comparison)

Julia支持Python式的链式比较

  1. julia> 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
  2. true

3.7 基础函数

3.7.1 Rounding Function

Function Description Return type
round(x) round x to the nearest integer typeof(x)
round(T, x) round x to the nearest integer T
floor(x) round x towards -Inf typeof(x)
floor(T, x) round x towards -Inf T
ceil(x) round x towards +Inf typeof(x)
ceil(T, x) round x towards +Inf T
trunc(x) round x towards zero typeof(x)
trunc(T, x) round x towards zero T

3.7.2 Division Function

Function Description
div(x,y) truncated division; quotient rounded towards zero
fld(x,y) floored division; quotient rounded towards -Inf
cld(x,y) ceiling division; quotient rounded towards +Inf
rem(x,y) remainder; satisfies x == div(x,y)*y + rem(x,y); sign matches x
mod(x,y) modulus; satisfies x == fld(x,y)*y + mod(x,y); sign matches y
mod1(x,y) mod() with offset 1; returns r∈(0,y] for y>0 or r∈[y,0) for y<0, where mod(r, y) == mod(x, y)
mod2pi(x) modulus with respect to 2pi; 0 <= mod2pi(x) < 2pi
divrem(x,y) returns (div(x,y),rem(x,y))
fldmod(x,y) returns (fld(x,y),mod(x,y))
gcd(x,y…) greatest positive common divisor of x, y,…
lcm(x,y…) least positive common multiple of x, y,…
  • 取余运算在计算商值向0方向舍弃小数位

  • 取模运算在计算商值向负无穷方向舍弃小数位

3.7.3 Sign and Absolute Function

Function Description
abs(x) a positive value with the magnitude of x
abs2(x) the squared magnitude of x
sign(x) indicates the sign of x, returning -1, 0, or +1
signbit(x) indicates whether the sign bit is on (true) or off (false)
copysign(x,y) a value with the magnitude of x and the sign of y
flipsign(x,y) a value with the magnitude of x and the sign of x*y

3.7.4 Powers,Logs and Roots

Function Description
sqrt(x), √x square root of x
cbrt(x), ∛x cube root of x
hypot(x,y) hypotenuse of right-angled triangle with other sides of length x and y
exp(x) natural exponential function at x
expm1(x) accurate exp(x)-1 for x near zero
ldexp(x,n) x*2^n computed efficiently for integer values of n
log(x) natural logarithm of x
log(b,x) base b logarithm of x
log2(x) base 2 logarithm of x
log10(x) base 10 logarithm of x
log1p(x) accurate log(1+x) for x near zero
exponent(x) binary exponent of x
significand(x) binary significand (a.k.a. mantissa) of a floating-point number x

为什么要有 hypot , expm1 , log1p等函数,参见 John D. Cook 的博客: expm1, log1p, erfchypot

3.7.5 Trigonometric and Hyperbolic Functions

  1. sin cos tan cot sec csc
  2. sinh cosh tanh coth sech csch
  3. asin acos atan acot asec acsc
  4. asinh acosh atanh acoth asech acsch
  5. sinc cosc atan2

除了 atan2 之外,都是单参数函数。 atan2 给出了 x 轴,与由 x 、 y 确定的点之间的弧度
另外, sinpi(x)cospi(x)各自被提供给更准确的 sin(pi*x)cos(pi*x) 的计算

如果想要以度,而非弧度,为单位计算三角函数,应使用带 d 后缀的函数。例如,sind(x) 计算 x 的正弦值,这里 x 的单位是度。以下的列表是全部的以度为单位的三角函数:

  1. sind cosd tand cotd secd cscd
  2. asind acosd atand acotd asecd acscd

3.7.6 Special Functions

Function Description
gamma(x) gamma function at x
lgamma(x) accurate log(gamma(x)) for large x
lfact(x) accurate log(factorial(x)) for large x;
same as lgamma(x+1) for x > 1, zero otherwise
beta(x,y) beta function at x,y
lbeta(x,y) accurate log(beta(x,y)) for large x or y