Mathematics

Mathematical Operators

Base.:- — Method

1. -(x)

Unary minus operator.

See also: abs, flipsign.

Examples

1. julia> -1
2. -1

4. julia> -(2)
5. -2

7. julia> -[1 2; 3 4]
8. 2×2 Matrix{Int64}:
9. -1  -2
10. -3  -4

source

Base.:+ — Function

1. dt::Date + t::Time -> DateTime

The addition of a Date with a Time produces a DateTime. The hour, minute, second, and millisecond parts of the Time are used along with the year, month, and day of the Date to create the new DateTime. Non-zero microseconds or nanoseconds in the Time type will result in an InexactError being thrown.

source

1. +(x, y...)

Addition operator. x+y+z+... calls this function with all arguments, i.e. +(x, y, z, ...).

Examples

1. julia> 1 + 20 + 4
2. 25

4. julia> +(1, 20, 4)
5. 25

source

Base.:- — Method

1. -(x, y)

Subtraction operator.

Examples

1. julia> 2 - 3
2. -1

4. julia> -(2, 4.5)
5. -2.5

source

Base.:* — Method

1. *(x, y...)

Multiplication operator. x*y*z*... calls this function with all arguments, i.e. *(x, y, z, ...).

Examples

1. julia> 2 * 7 * 8
2. 112

4. julia> *(2, 7, 8)
5. 112

source

Base.:/ — Function

1. /(x, y)

Right division operator: multiplication of x by the inverse of y on the right. Gives floating-point results for integer arguments.

Examples

1. julia> 1/2
2. 0.5

4. julia> 4/2
5. 2.0

7. julia> 4.5/2
8. 2.25

source

[Base.:](#Base.:\-Tuple{Any, Any}) — Method

1. \(x, y)

Left division operator: multiplication of y by the inverse of x on the left. Gives floating-point results for integer arguments.

Examples

1. julia> 3 \ 6
2. 2.0

4. julia> inv(3) * 6
5. 2.0

7. julia> A = [4 3; 2 1]; x = [5, 6];

9. julia> A \ x
10. 2-element Vector{Float64}:
11. 6.5
12. -7.0

14. julia> inv(A) * x
15. 2-element Vector{Float64}:
16. 6.5
17. -7.0

source

Base.:^ — Method

1. ^(x, y)

Exponentiation operator. If x is a matrix, computes matrix exponentiation.

If y is an Int literal (e.g. 2 in x^2 or -3 in x^-3), the Julia code x^y is transformed by the compiler to Base.literal_pow(^, x, Val(y)), to enable compile-time specialization on the value of the exponent. (As a default fallback we have Base.literal_pow(^, x, Val(y)) = ^(x,y), where usually ^ == Base.^ unless ^ has been defined in the calling namespace.) If y is a negative integer literal, then Base.literal_pow transforms the operation to inv(x)^-y by default, where -y is positive.

Examples

1. julia> 3^5
2. 243

4. julia> A = [1 2; 3 4]
5. 2×2 Matrix{Int64}:
6. 1  2
7. 3  4

9. julia> A^3
10. 2×2 Matrix{Int64}:
11. 37   54
12. 81  118

source

Base.fma — Function

1. fma(x, y, z)

Computes x*y+z without rounding the intermediate result x*y. On some systems this is significantly more expensive than x*y+z. fma is used to improve accuracy in certain algorithms. See muladd.

source

Base.muladd — Function

1. muladd(A, y, z)

Combined multiply-add, A*y .+ z, for matrix-matrix or matrix-vector multiplication. The result is always the same size as A*y, but z may be smaller, or a scalar.

Julia 1.6

These methods require Julia 1.6 or later.

Examples

1. julia> A=[1.0 2.0; 3.0 4.0]; B=[1.0 1.0; 1.0 1.0]; z=[0, 100];

3. julia> muladd(A, B, z)
4. 2×2 Matrix{Float64}:
5. 3.0    3.0
6. 107.0  107.0

source

1. muladd(x, y, z)

Combined multiply-add: computes x*y+z, but allowing the add and multiply to be merged with each other or with surrounding operations for performance. For example, this may be implemented as an fma if the hardware supports it efficiently. The result can be different on different machines and can also be different on the same machine due to constant propagation or other optimizations. See fma.

Examples

1. julia> muladd(3, 2, 1)
2. 7

4. julia> 3 * 2 + 1
5. 7

source

Base.inv — Method

1. inv(x)

Return the multiplicative inverse of x, such that x*inv(x) or inv(x)*x yields one(x) (the multiplicative identity) up to roundoff errors.

If x is a number, this is essentially the same as one(x)/x, but for some types inv(x) may be slightly more efficient.

Examples

1. julia> inv(2)
2. 0.5

4. julia> inv(1 + 2im)
5. 0.2 - 0.4im

7. julia> inv(1 + 2im) * (1 + 2im)
8. 1.0 + 0.0im

10. julia> inv(2//3)
11. 3//2

Julia 1.2

inv(::Missing) requires at least Julia 1.2.

source

Base.div — Function

1. div(x, y)
2. ÷(x, y)

The quotient from Euclidean (integer) division. Generally equivalent to a mathematical operation x/y without a fractional part.

See also: cld, fld, rem, divrem.

Examples

1. julia> 9 ÷ 4
2. 2

4. julia> -5 ÷ 3
5. -1

7. julia> 5.0 ÷ 2
8. 2.0

10. julia> div.(-5:5, 3)'
11. 1×11 adjoint(::Vector{Int64}) with eltype Int64:
12. -1  -1  -1  0  0  0  0  0  1  1  1

source

Base.fld — Function

1. fld(x, y)

Largest integer less than or equal to x/y. Equivalent to div(x, y, RoundDown).

See also div, cld, fld1.

Examples

1. julia> fld(7.3,5.5)
2. 1.0

4. julia> fld.(-5:5, 3)'
5. 1×11 adjoint(::Vector{Int64}) with eltype Int64:
6. -2  -2  -1  -1  -1  0  0  0  1  1  1

Because fld(x, y) implements strictly correct floored rounding based on the true value of floating-point numbers, unintuitive situations can arise. For example:

1. julia> fld(6.0,0.1)
2. 59.0
3. julia> 6.0/0.1
4. 60.0
5. julia> 6.0/big(0.1)
6. 59.99999999999999666933092612453056361837965690217069245739573412231113406246995

What is happening here is that the true value of the floating-point number written as 0.1 is slightly larger than the numerical value 1/10 while 6.0 represents the number 6 precisely. Therefore the true value of 6.0 / 0.1 is slightly less than 60. When doing division, this is rounded to precisely 60.0, but fld(6.0, 0.1) always takes the floor or the true value, so the result is 59.0.

source

Base.cld — Function

1. cld(x, y)

Smallest integer larger than or equal to x/y. Equivalent to div(x, y, RoundUp).

See also div, fld.

Examples

1. julia> cld(5.5,2.2)
2. 3.0

4. julia> cld.(-5:5, 3)'
5. 1×11 adjoint(::Vector{Int64}) with eltype Int64:
6. -1  -1  -1  0  0  0  1  1  1  2  2

source

Base.mod — Function

1. mod(x::Integer, r::AbstractUnitRange)

Find y in the range r such that $x ≡ y (mod n)$, where n = length(r), i.e. y = mod(x - first(r), n) + first(r).

See also mod1.

Examples

1. julia> mod(0, Base.OneTo(3))
2. 3

4. julia> mod(3, 0:2)
5. 0

Julia 1.3

This method requires at least Julia 1.3.

source

1. mod(x, y)
2. rem(x, y, RoundDown)

The reduction of x modulo y, or equivalently, the remainder of x after floored division by y, i.e. x - y*fld(x,y) if computed without intermediate rounding.

The result will have the same sign as y, and magnitude less than abs(y) (with some exceptions, see note below).

Note

When used with floating point values, the exact result may not be representable by the type, and so rounding error may occur. In particular, if the exact result is very close to y, then it may be rounded to y.

See also: rem, div, fld, mod1, invmod.

1. julia> mod(8, 3)
2. 2

4. julia> mod(9, 3)
5. 0

7. julia> mod(8.9, 3)
8. 2.9000000000000004

10. julia> mod(eps(), 3)
11. 2.220446049250313e-16

13. julia> mod(-eps(), 3)
14. 3.0

16. julia> mod.(-5:5, 3)'
17. 1×11 adjoint(::Vector{Int64}) with eltype Int64:
18. 1  2  0  1  2  0  1  2  0  1  2

source

1. rem(x::Integer, T::Type{<:Integer}) -> T
2. mod(x::Integer, T::Type{<:Integer}) -> T
3. %(x::Integer, T::Type{<:Integer}) -> T

Find y::T such that x ≡ y (mod n), where n is the number of integers representable in T, and y is an integer in [typemin(T),typemax(T)]. If T can represent any integer (e.g. T == BigInt), then this operation corresponds to a conversion to T.

Examples

1. julia> 129 % Int8
2. -127

source

Base.rem — Function

1. rem(x, y)
2. %(x, y)

Remainder from Euclidean division, returning a value of the same sign as x, and smaller in magnitude than y. This value is always exact.

See also: div, mod, mod1, divrem.

Examples

1. julia> x = 15; y = 4;

3. julia> x % y
4. 3

6. julia> x == div(x, y) * y + rem(x, y)
7. true

9. julia> rem.(-5:5, 3)'
10. 1×11 adjoint(::Vector{Int64}) with eltype Int64:
11. -2  -1  0  -2  -1  0  1  2  0  1  2

source

Base.Math.rem2pi — Function

1. rem2pi(x, r::RoundingMode)

Compute the remainder of x after integer division by 2π, with the quotient rounded according to the rounding mode r. In other words, the quantity

1. x - 2π*round(x/(2π),r)

without any intermediate rounding. This internally uses a high precision approximation of 2π, and so will give a more accurate result than rem(x,2π,r)

  • if r == RoundNearest, then the result is in the interval $[-π, π]$. This will generally be the most accurate result. See also RoundNearest.
  • if r == RoundToZero, then the result is in the interval $[0, 2π]$ if x is positive,. or $[-2π, 0]$ otherwise. See also RoundToZero.
  • if r == RoundDown, then the result is in the interval $[0, 2π]$. See also RoundDown.
  • if r == RoundUp, then the result is in the interval $[-2π, 0]$. See also RoundUp.

Examples

1. julia> rem2pi(7pi/4, RoundNearest)
2. -0.7853981633974485

4. julia> rem2pi(7pi/4, RoundDown)
5. 5.497787143782138

source

Base.Math.mod2pi — Function

1. mod2pi(x)

Modulus after division by 2π, returning in the range $[0,2π)$.

This function computes a floating point representation of the modulus after division by numerically exact 2π, and is therefore not exactly the same as mod(x,2π), which would compute the modulus of x relative to division by the floating-point number 2π.

Note

Depending on the format of the input value, the closest representable value to 2π may be less than 2π. For example, the expression mod2pi(2π) will not return 0, because the intermediate value of 2*π is a Float64 and 2*Float64(π) < 2*big(π). See rem2pi for more refined control of this behavior.

Examples

1. julia> mod2pi(9*pi/4)
2. 0.7853981633974481

source

Base.divrem — Function

1. divrem(x, y, r::RoundingMode=RoundToZero)

The quotient and remainder from Euclidean division. Equivalent to (div(x,y,r), rem(x,y,r)). Equivalently, with the default value of r, this call is equivalent to (x÷y, x%y).

See also: fldmod, cld.

Examples

1. julia> divrem(3,7)
2. (0, 3)

4. julia> divrem(7,3)
5. (2, 1)

source

Base.fldmod — Function

1. fldmod(x, y)

The floored quotient and modulus after division. A convenience wrapper for divrem(x, y, RoundDown). Equivalent to (fld(x,y), mod(x,y)).

See also: fld, cld, fldmod1.

source

Base.fld1 — Function

1. fld1(x, y)

Flooring division, returning a value consistent with mod1(x,y)

See also mod1, fldmod1.

Examples

1. julia> x = 15; y = 4;

3. julia> fld1(x, y)
4. 4

6. julia> x == fld(x, y) * y + mod(x, y)
7. true

9. julia> x == (fld1(x, y) - 1) * y + mod1(x, y)
10. true

source

Base.mod1 — Function

1. mod1(x, y)

Modulus after flooring division, returning a value r such that mod(r, y) == mod(x, y) in the range $(0, y]$ for positive y and in the range $[y,0)$ for negative y.

See also fld1, fldmod1.

Examples

1. julia> mod1(4, 2)
2. 2

4. julia> mod1(4, 3)
5. 1

source

Base.fldmod1 — Function

1. fldmod1(x, y)

Return (fld1(x,y), mod1(x,y)).

See also fld1, mod1.

source

Base.:// — Function

1. //(num, den)

Divide two integers or rational numbers, giving a Rational result.

Examples

1. julia> 3 // 5
2. 3//5

4. julia> (3 // 5) // (2 // 1)
5. 3//10

source

Base.rationalize — Function

1. rationalize([T<:Integer=Int,] x; tol::Real=eps(x))

Approximate floating point number x as a Rational number with components of the given integer type. The result will differ from x by no more than tol.

Examples

1. julia> rationalize(5.6)
2. 28//5

4. julia> a = rationalize(BigInt, 10.3)
5. 103//10

7. julia> typeof(numerator(a))
8. BigInt

source

Base.numerator — Function

1. numerator(x)

Numerator of the rational representation of x.

Examples

1. julia> numerator(2//3)
2. 2

4. julia> numerator(4)
5. 4

source

Base.denominator — Function

1. denominator(x)

Denominator of the rational representation of x.

Examples

1. julia> denominator(2//3)
2. 3

4. julia> denominator(4)
5. 1

source

Base.:<< — Function

1. <<(x, n)

Left bit shift operator, x << n. For n >= 0, the result is x shifted left by n bits, filling with 0s. This is equivalent to x * 2^n. For n < 0, this is equivalent to x >> -n.

Examples

1. julia> Int8(3) << 2
2. 12

4. julia> bitstring(Int8(3))
5. "00000011"

7. julia> bitstring(Int8(12))
8. "00001100"

See also >>, >>>, exp2, ldexp.

source

1. <<(B::BitVector, n) -> BitVector

Left bit shift operator, B << n. For n >= 0, the result is B with elements shifted n positions backwards, filling with false values. If n < 0, elements are shifted forwards. Equivalent to B >> -n.

Examples

1. julia> B = BitVector([true, false, true, false, false])
2. 5-element BitVector:
3. 1
4. 0
5. 1
6. 0
7. 0

9. julia> B << 1
10. 5-element BitVector:
11. 0
12. 1
13. 0
14. 0
15. 0

17. julia> B << -1
18. 5-element BitVector:
19. 0
20. 1
21. 0
22. 1
23. 0

source

Base.:>> — Function

1. >>(x, n)

Right bit shift operator, x >> n. For n >= 0, the result is x shifted right by n bits, where n >= 0, filling with 0s if x >= 0, 1s if x < 0, preserving the sign of x. This is equivalent to fld(x, 2^n). For n < 0, this is equivalent to x << -n.

Examples

1. julia> Int8(13) >> 2
2. 3

4. julia> bitstring(Int8(13))
5. "00001101"

7. julia> bitstring(Int8(3))
8. "00000011"

10. julia> Int8(-14) >> 2
11. -4

13. julia> bitstring(Int8(-14))
14. "11110010"

16. julia> bitstring(Int8(-4))
17. "11111100"

See also >>>, <<.

source

1. >>(B::BitVector, n) -> BitVector

Right bit shift operator, B >> n. For n >= 0, the result is B with elements shifted n positions forward, filling with false values. If n < 0, elements are shifted backwards. Equivalent to B << -n.

Examples

1. julia> B = BitVector([true, false, true, false, false])
2. 5-element BitVector:
3. 1
4. 0
5. 1
6. 0
7. 0

9. julia> B >> 1
10. 5-element BitVector:
11. 0
12. 1
13. 0
14. 1
15. 0

17. julia> B >> -1
18. 5-element BitVector:
19. 0
20. 1
21. 0
22. 0
23. 0

source

Base.:>>> — Function

1. >>>(x, n)

Unsigned right bit shift operator, x >>> n. For n >= 0, the result is x shifted right by n bits, where n >= 0, filling with 0s. For n < 0, this is equivalent to x << -n.

For Unsigned integer types, this is equivalent to >>. For Signed integer types, this is equivalent to signed(unsigned(x) >> n).

Examples

1. julia> Int8(-14) >>> 2
2. 60

4. julia> bitstring(Int8(-14))
5. "11110010"

7. julia> bitstring(Int8(60))
8. "00111100"

BigInts are treated as if having infinite size, so no filling is required and this is equivalent to >>.

See also >>, <<.

source

1. >>>(B::BitVector, n) -> BitVector

Unsigned right bitshift operator, B >>> n. Equivalent to B >> n. See >> for details and examples.

source

Base.bitrotate — Function

1. bitrotate(x::Base.BitInteger, k::Integer)

bitrotate(x, k) implements bitwise rotation. It returns the value of x with its bits rotated left k times. A negative value of k will rotate to the right instead.

Julia 1.5

This function requires Julia 1.5 or later.

See also: <<, circshift, BitArray.

1. julia> bitrotate(UInt8(114), 2)
2. 0xc9

4. julia> bitstring(bitrotate(0b01110010, 2))
5. "11001001"

7. julia> bitstring(bitrotate(0b01110010, -2))
8. "10011100"

10. julia> bitstring(bitrotate(0b01110010, 8))
11. "01110010"

source

Base.:: — Function

1. (:)(start::CartesianIndex, [step::CartesianIndex], stop::CartesianIndex)

Construct CartesianIndices from two CartesianIndex and an optional step.

Julia 1.1

This method requires at least Julia 1.1.

Julia 1.6

The step range method start:step:stop requires at least Julia 1.6.

Examples

1. julia> I = CartesianIndex(2,1);

3. julia> J = CartesianIndex(3,3);

5. julia> I:J
6. 2×3 CartesianIndices{2, Tuple{UnitRange{Int64}, UnitRange{Int64}}}:
7. CartesianIndex(2, 1)  CartesianIndex(2, 2)  CartesianIndex(2, 3)
8. CartesianIndex(3, 1)  CartesianIndex(3, 2)  CartesianIndex(3, 3)

10. julia> I:CartesianIndex(1, 2):J
11. 2×2 CartesianIndices{2, Tuple{StepRange{Int64, Int64}, StepRange{Int64, Int64}}}:
12. CartesianIndex(2, 1)  CartesianIndex(2, 3)
13. CartesianIndex(3, 1)  CartesianIndex(3, 3)

source

1. (:)(start, [step], stop)

Range operator. a:b constructs a range from a to b with a step size of 1 (a UnitRange) , and a:s:b is similar but uses a step size of s (a StepRange).

: is also used in indexing to select whole dimensions and for Symbol literals, as in e.g. :hello.

source

Base.range — Function

1. range(start, stop, length)
2. range(start, stop; length, step)
3. range(start; length, stop, step)
4. range(;start, length, stop, step)

Construct a specialized array with evenly spaced elements and optimized storage (an AbstractRange) from the arguments. Mathematically a range is uniquely determined by any three of start, step, stop and length. Valid invocations of range are:

  • Call range with any three of start, step, stop, length.
  • Call range with two of start, stop, length. In this case step will be assumed

to be one. If both arguments are Integers, a UnitRange will be returned.

Examples

1. julia> range(1, length=100)
2. 1:100

4. julia> range(1, stop=100)
5. 1:100

7. julia> range(1, step=5, length=100)
8. 1:5:496

10. julia> range(1, step=5, stop=100)
11. 1:5:96

13. julia> range(1, 10, length=101)
14. 1.0:0.09:10.0

16. julia> range(1, 100, step=5)
17. 1:5:96

19. julia> range(stop=10, length=5)
20. 6:10

22. julia> range(stop=10, step=1, length=5)
23. 6:1:10

25. julia> range(start=1, step=1, stop=10)
26. 1:1:10

If length is not specified and stop - start is not an integer multiple of step, a range that ends before stop will be produced.

1. julia> range(1, 3.5, step=2)
2. 1.0:2.0:3.0

Special care is taken to ensure intermediate values are computed rationally. To avoid this induced overhead, see the LinRange constructor.

Julia 1.1

stop as a positional argument requires at least Julia 1.1.

Julia 1.7

The versions without keyword arguments and start as a keyword argument require at least Julia 1.7.

source

Base.OneTo — Type

1. Base.OneTo(n)

Define an AbstractUnitRange that behaves like 1:n, with the added distinction that the lower limit is guaranteed (by the type system) to be 1.

source

Base.StepRangeLen — Type

1. StepRangeLen(         ref::R, step::S, len, [offset=1]) where {  R,S}
2. StepRangeLen{T,R,S}(  ref::R, step::S, len, [offset=1]) where {T,R,S}
3. StepRangeLen{T,R,S,L}(ref::R, step::S, len, [offset=1]) where {T,R,S,L}

A range r where r[i] produces values of type T (in the first form, T is deduced automatically), parameterized by a reference value, a step, and the length. By default ref is the starting value r[1], but alternatively you can supply it as the value of r[offset] for some other index 1 <= offset <= len. In conjunction with TwicePrecision this can be used to implement ranges that are free of roundoff error.

source

Base.:== — Function

1. ==(x, y)

Generic equality operator. Falls back to \===. Should be implemented for all types with a notion of equality, based on the abstract value that an instance represents. For example, all numeric types are compared by numeric value, ignoring type. Strings are compared as sequences of characters, ignoring encoding. For collections, == is generally called recursively on all contents, though other properties (like the shape for arrays) may also be taken into account.

This operator follows IEEE semantics for floating-point numbers: 0.0 == -0.0 and NaN != NaN.

The result is of type Bool, except when one of the operands is missing, in which case missing is returned (three-valued logic). For collections, missing is returned if at least one of the operands contains a missing value and all non-missing values are equal. Use isequal or \=== to always get a Bool result.

Implementation

New numeric types should implement this function for two arguments of the new type, and handle comparison to other types via promotion rules where possible.

isequal falls back to ==, so new methods of == will be used by the Dict type to compare keys. If your type will be used as a dictionary key, it should therefore also implement hash.

If some type defines ==, isequal, and isless then it should also implement < to ensure consistency of comparisons.

source

Base.:!= — Function

1. !=(x, y)
2. ≠(x,y)

Not-equals comparison operator. Always gives the opposite answer as \==.

Implementation

New types should generally not implement this, and rely on the fallback definition !=(x,y) = !(x==y) instead.

Examples

1. julia> 3 != 2
2. true

4. julia> "foo" ≠ "foo"
5. false

source

1. !=(x)

Create a function that compares its argument to x using !=, i.e. a function equivalent to y -> y != x. The returned function is of type Base.Fix2{typeof(!=)}, which can be used to implement specialized methods.

Julia 1.2

This functionality requires at least Julia 1.2.

source

Base.:!== — Function

1. !==(x, y)
2. ≢(x,y)

Always gives the opposite answer as \===.

Examples

1. julia> a = [1, 2]; b = [1, 2];

3. julia> a ≢ b
4. true

6. julia> a ≢ a
7. false

source

Base.:< — Function

1. <(x, y)

Less-than comparison operator. Falls back to isless. Because of the behavior of floating-point NaN values, this operator implements a partial order.

Implementation

New numeric types with a canonical partial order should implement this function for two arguments of the new type. Types with a canonical total order should implement isless instead.

Examples

1. julia> 'a' < 'b'
2. true

4. julia> "abc" < "abd"
5. true

7. julia> 5 < 3
8. false

source

1. <(x)

Create a function that compares its argument to x using <, i.e. a function equivalent to y -> y < x. The returned function is of type Base.Fix2{typeof(<)}, which can be used to implement specialized methods.

Julia 1.2

This functionality requires at least Julia 1.2.

source

Base.:<= — Function

1. <=(x, y)
2. ≤(x,y)

Less-than-or-equals comparison operator. Falls back to (x < y) | (x == y).

Examples

1. julia> 'a' <= 'b'
2. true

4. julia> 7 ≤ 7 ≤ 9
5. true

7. julia> "abc" ≤ "abc"
8. true

10. julia> 5 <= 3
11. false

source

1. <=(x)

Create a function that compares its argument to x using <=, i.e. a function equivalent to y -> y <= x. The returned function is of type Base.Fix2{typeof(<=)}, which can be used to implement specialized methods.

Julia 1.2

This functionality requires at least Julia 1.2.

source

Base.:> — Function

1. >(x, y)

Greater-than comparison operator. Falls back to y < x.

Implementation

Generally, new types should implement < instead of this function, and rely on the fallback definition >(x, y) = y < x.

Examples

1. julia> 'a' > 'b'
2. false

4. julia> 7 > 3 > 1
5. true

7. julia> "abc" > "abd"
8. false

10. julia> 5 > 3
11. true

source

1. >(x)

Create a function that compares its argument to x using >, i.e. a function equivalent to y -> y > x. The returned function is of type Base.Fix2{typeof(>)}, which can be used to implement specialized methods.

Julia 1.2

This functionality requires at least Julia 1.2.

source

Base.:>= — Function

1. >=(x, y)
2. ≥(x,y)

Greater-than-or-equals comparison operator. Falls back to y <= x.

Examples

1. julia> 'a' >= 'b'
2. false

4. julia> 7 ≥ 7 ≥ 3
5. true

7. julia> "abc" ≥ "abc"
8. true

10. julia> 5 >= 3
11. true

source

1. >=(x)

Create a function that compares its argument to x using >=, i.e. a function equivalent to y -> y >= x. The returned function is of type Base.Fix2{typeof(>=)}, which can be used to implement specialized methods.

Julia 1.2

This functionality requires at least Julia 1.2.

source

Base.cmp — Function

1. cmp(x,y)

Return -1, 0, or 1 depending on whether x is less than, equal to, or greater than y, respectively. Uses the total order implemented by isless.

Examples

1. julia> cmp(1, 2)
2. -1

4. julia> cmp(2, 1)
5. 1

7. julia> cmp(2+im, 3-im)
8. ERROR: MethodError: no method matching isless(::Complex{Int64}, ::Complex{Int64})
9. [...]

source

1. cmp(<, x, y)

Return -1, 0, or 1 depending on whether x is less than, equal to, or greater than y, respectively. The first argument specifies a less-than comparison function to use.

source

1. cmp(a::AbstractString, b::AbstractString) -> Int

Compare two strings. Return 0 if both strings have the same length and the character at each index is the same in both strings. Return -1 if a is a prefix of b, or if a comes before b in alphabetical order. Return 1 if b is a prefix of a, or if b comes before a in alphabetical order (technically, lexicographical order by Unicode code points).

Examples

1. julia> cmp("abc", "abc")
2. 0

4. julia> cmp("ab", "abc")
5. -1

7. julia> cmp("abc", "ab")
8. 1

10. julia> cmp("ab", "ac")
11. -1

13. julia> cmp("ac", "ab")
14. 1

16. julia> cmp("α", "a")
17. 1

19. julia> cmp("b", "β")
20. -1

source

Base.:~ — Function

1. ~(x)

Bitwise not.

See also: !, &, |.

Examples

1. julia> ~4
2. -5

4. julia> ~10
5. -11

7. julia> ~true
8. false

source

Base.:& — Function

1. x & y

Bitwise and. Implements three-valued logic, returning missing if one operand is missing and the other is true. Add parentheses for function application form: (&)(x, y).

See also: |, xor, &&.

Examples

1. julia> 4 & 10
2. 0

4. julia> 4 & 12
5. 4

7. julia> true & missing
8. missing

10. julia> false & missing
11. false

source

Base.:| — Function

1. x | y

Bitwise or. Implements three-valued logic, returning missing if one operand is missing and the other is false.

See also: &, xor, ||.

Examples

1. julia> 4 | 10
2. 14

4. julia> 4 | 1
5. 5

7. julia> true | missing
8. true

10. julia> false | missing
11. missing

source

Base.xor — Function

1. xor(x, y)
2. ⊻(x, y)

Bitwise exclusive or of x and y. Implements three-valued logic, returning missing if one of the arguments is missing.

The infix operation a ⊻ b is a synonym for xor(a,b), and ⊻ can be typed by tab-completing \xor or \veebar in the Julia REPL.

Examples

1. julia> xor(true, false)
2. true

4. julia> xor(true, true)
5. false

7. julia> xor(true, missing)
8. missing

10. julia> false ⊻ false
11. false

13. julia> [true; true; false] .⊻ [true; false; false]
14. 3-element BitVector:
15. 0
16. 1
17. 0

source

Base.nand — Function

1. nand(x, y)
2. ⊼(x, y)

Bitwise nand (not and) of x and y. Implements three-valued logic, returning missing if one of the arguments is missing.

The infix operation a ⊼ b is a synonym for nand(a,b), and ⊼ can be typed by tab-completing \nand or \barwedge in the Julia REPL.

Examples

1. julia> nand(true, false)
2. true

4. julia> nand(true, true)
5. false

7. julia> nand(true, missing)
8. missing

10. julia> false ⊼ false
11. true

13. julia> [true; true; false] .⊼ [true; false; false]
14. 3-element BitVector:
15. 0
16. 1
17. 1

source

Base.nor — Function

1. nor(x, y)
2. ⊽(x, y)

Bitwise nor (not or) of x and y. Implements three-valued logic, returning missing if one of the arguments is missing.

The infix operation a ⊽ b is a synonym for nor(a,b), and ⊽ can be typed by tab-completing \nor or \veebar in the Julia REPL.

Examples

1. julia> nor(true, false)
2. false

4. julia> nor(true, true)
5. false

7. julia> nor(true, missing)
8. false

10. julia> false ⊽ false
11. true

13. julia> [true; true; false] .⊽ [true; false; false]
14. 3-element BitVector:
15. 0
16. 0
17. 1

source

Base.:! — Function

1. !(x)

Boolean not. Implements three-valued logic, returning missing if x is missing.

See also ~ for bitwise not.

Examples

1. julia> !true
2. false

4. julia> !false
5. true

7. julia> !missing
8. missing

10. julia> .![true false true]
11. 1×3 BitMatrix:
12. 0  1  0

source

1. !f::Function

Predicate function negation: when the argument of ! is a function, it returns a function which computes the boolean negation of f.

See also ∘.

Examples

1. julia> str = "∀ ε > 0, ∃ δ > 0: |x-y| < δ ⇒ |f(x)-f(y)| < ε"
2. "∀ ε > 0, ∃ δ > 0: |x-y| < δ ⇒ |f(x)-f(y)| < ε"

4. julia> filter(isletter, str)
5. "εδxyδfxfyε"

7. julia> filter(!isletter, str)
8. "∀  > 0, ∃  > 0: |-| <  ⇒ |()-()| < "

source

&& — Keyword

1. x && y

Short-circuiting boolean AND.

See also &, the ternary operator ? :, and the manual section on control flow.

Examples

1. julia> x = 3;

3. julia> x > 1 && x < 10 && x isa Int
4. true

6. julia> x < 0 && error("expected positive x")
7. false

source

|| — Keyword

1. x || y

Short-circuiting boolean OR.

See also: |, xor, &&.

Examples

1. julia> pi < 3 || ℯ < 3
2. true

4. julia> false || true || println("neither is true!")
5. true

source

Mathematical Functions

Base.isapprox — Function

1. isapprox(x, y; atol::Real=0, rtol::Real=atol>0 ? 0 : √eps, nans::Bool=false[, norm::Function])

Inexact equality comparison. Two numbers compare equal if their relative distance or their absolute distance is within tolerance bounds: isapprox returns true if norm(x-y) <= max(atol, rtol*max(norm(x), norm(y))). The default atol is zero and the default rtol depends on the types of x and y. The keyword argument nans determines whether or not NaN values are considered equal (defaults to false).

For real or complex floating-point values, if an atol > 0 is not specified, rtol defaults to the square root of eps of the type of x or y, whichever is bigger (least precise). This corresponds to requiring equality of about half of the significand digits. Otherwise, e.g. for integer arguments or if an atol > 0 is supplied, rtol defaults to zero.

The norm keyword defaults to abs for numeric (x,y) and to LinearAlgebra.norm for arrays (where an alternative norm choice is sometimes useful). When x and y are arrays, if norm(x-y) is not finite (i.e. ±Inf or NaN), the comparison falls back to checking whether all elements of x and y are approximately equal component-wise.

The binary operator ≈ is equivalent to isapprox with the default arguments, and x ≉ y is equivalent to !isapprox(x,y).

Note that x ≈ 0 (i.e., comparing to zero with the default tolerances) is equivalent to x == 0 since the default atol is 0. In such cases, you should either supply an appropriate atol (or use norm(x) ≤ atol) or rearrange your code (e.g. use x ≈ y rather than x - y ≈ 0). It is not possible to pick a nonzero atol automatically because it depends on the overall scaling (the “units”) of your problem: for example, in x - y ≈ 0, atol=1e-9 is an absurdly small tolerance if x is the radius of the Earth in meters, but an absurdly large tolerance if x is the radius of a Hydrogen atom in meters.

Julia 1.6

Passing the norm keyword argument when comparing numeric (non-array) arguments requires Julia 1.6 or later.

Examples

1. julia> isapprox(0.1, 0.15; atol=0.05)
2. true

4. julia> isapprox(0.1, 0.15; rtol=0.34)
5. true

7. julia> isapprox(0.1, 0.15; rtol=0.33)
8. false

10. julia> 0.1 + 1e-10 ≈ 0.1
11. true

13. julia> 1e-10 ≈ 0
14. false

16. julia> isapprox(1e-10, 0, atol=1e-8)
17. true

19. julia> isapprox([10.0^9, 1.0], [10.0^9, 2.0]) # using `norm`
20. true

source

1. isapprox(x; kwargs...) / ≈(x; kwargs...)

Create a function that compares its argu

(本文超出站点单文档体积上限,已截断)