cos
Compute the cosine of x expressed in radians.
Base.cos — Function
cos(x::T) where {T <: Number} -> float(T)Compute cosine of x, where x is in radians.
Throw a DomainError if isinf(x), return a T(NaN) if isnan(x).
cos(A::AbstractMatrix)Compute the matrix cosine of a square matrix A.
If A is symmetric or Hermitian, its eigendecomposition (eigen) is used to compute the cosine. Otherwise, the cosine is determined by calling exp.
Examples
julia> cos(fill(1.0, (2,2)))
2×2 Matrix{Float64}:
0.291927 -0.708073
-0.708073 0.291927Methods
julia> methods(cos, (Any,), [Base, Base.Math, Base.MathConstants, Base.MPFR])# 8 methods for generic function "cos" from Base: [1] cos(a::ComplexF16) @ math.jl:1527 [2] cos(::Missing) @ math.jl:1548 [3] cos(x::BigFloat) @ mpfr.jl:946 [4] cos(a::Float16) @ math.jl:1526 [5] cos(::Irrational{:π}) @ mathconstants.jl:147 [6] cos(z::Complex{T}) where T @ complex.jl:912 [7] cos(x::T) where T<:Union{Float32, Float64} @ special/trig.jl:98 [8] cos(x::Real) @ math.jl:1543
Examples
julia> using UnicodePlotsjulia> lineplot(-π, π, cos)┌────────────────────────────────────────┐ 1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠚⠉⡏⠓⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ cos(x) │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀⠀⡇⠀⠈⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠁⠀⠀⠀⡇⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠃⠀⠀⠀⠀⡇⠀⠀⠀⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠏⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠱⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠁⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ f(x) │⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⢤⠧⠤⠤⠤⠤⠤⠤⠤⡧⠤⠤⠤⠤⠤⠤⠼⡤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠁⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⢀⡞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⢀⡜⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⡀⠀⠀⠀⠀⠀⠀│ -1 │⠀⠀⠀⠀⣀⡠⠎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⢄⡀⠀⠀⠀⠀│ └────────────────────────────────────────┘ ⠀-4⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀4⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀x⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Real Numbers
julia> cos(0)
1.0
julia> cos(0.5*pi)
6.123233995736766e-17
julia> cos(pi)
-1.0
julia> pi
π = 3.1415926535897...
julia> cos.([0 0.5*pi pi 1.5*pi 2*pi])
1×5 Matrix{Float64}:
1.0 6.12323e-17 -1.0 -1.83697e-16 1.0big float, cos(pi/2) == 0:
julia> cos(pi/2)
6.123233995736766e-17
julia> cos(pi/big"2")
5.48458720489676038371065313197849010525379118254343975589502858496071344256677e-78Complex
julia> cos(0+0im)
1.0 - 0.0implot real part
julia> using UnicodePlotsjulia> cos_real(x, y) = real(cos(x + y*im))cos_real (generic function with 1 method)julia> surfaceplot(-2pi:0.01:2pi, -pi:0.01:pi, cos_real)┌────────────────────────────────────────┐ 10 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ ┌──┐ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣶⡄⠀⠀⠀⠀⠀⠀⣿⣿⣿⡀⠀⠀⠀⠀⠀⠀⣰⠆⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣷⠀⠀⠀⠀⠀⢸⣿⣿⣿⣧⠀⠀⠀⠀⠀⢀⡿⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⢰⣦⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣆⠀⠀⠀⠀⣼⣿⣿⣿⣿⣷⣄⠀⠀⢀⣾⠃⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⣿⡆⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣆⠀⠀⢀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⢸⣿⡀⠀⠀⠀⢀⣿⣿⣿⣿⣿⣿⣷⣦⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⢻⣷⡀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠻⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⢿⣿⣶⣤⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠈⢻⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⡟⠙⠛⢿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⢻⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⡿⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠈⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⠿⠃⠀⠀⠀⠀⠀⠘⣿⣿⣿⡟⠀⠀⠀⠀⠀⠀⠙⠿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠀⢀⡠⠼⠤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠻⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │▄▄│ │⠊⠁⠀⠀⠀⠀⠉⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ └──┘ └────────────────────────────────────────┘ -10
Tips
- Call
cospito computecos(x*pi)
See Also
cospi, sincos, cosd, cosh, acos
Extended Inputs
Matrix
With Array like input:
julia> methods(cos, (Any,), [LinearAlgebra])# 6 methods for generic function "cos" from Base: [1] cos(D::Diagonal) @ /opt/hostedtoolcache/julia/1.12.2/x64/share/julia/stdlib/v1.12/LinearAlgebra/src/diagonal.jl:879 [2] cos(J::UniformScaling) @ /opt/hostedtoolcache/julia/1.12.2/x64/share/julia/stdlib/v1.12/LinearAlgebra/src/uniformscaling.jl:176 [3] cos(A::Hermitian{var"#s4811", S} where {var"#s4811"<:Complex, S<:(AbstractMatrix{<:var"#s4811"})}) @ /opt/hostedtoolcache/julia/1.12.2/x64/share/julia/stdlib/v1.12/LinearAlgebra/src/symmetric.jl:898 [4] cos(A::Union{Hermitian{T, S} where S, SymTridiagonal{T, V} where V<:AbstractVector{T}, Symmetric{T, S} where S} where T<:Real) @ /opt/hostedtoolcache/julia/1.12.2/x64/share/julia/stdlib/v1.12/LinearAlgebra/src/symmetric.jl:894 [5] cos(A::AbstractMatrix{<:Complex}) @ /opt/hostedtoolcache/julia/1.12.2/x64/share/julia/stdlib/v1.12/LinearAlgebra/src/dense.jl:1125 [6] cos(A::AbstractMatrix{<:Real}) @ /opt/hostedtoolcache/julia/1.12.2/x64/share/julia/stdlib/v1.12/LinearAlgebra/src/dense.jl:1116
julia> cos([1 2; 3 4])
2×2 Matrix{Float64}:
0.855423 -0.110876
-0.166315 0.689109
julia> cos.([1 2; 3 4])
2×2 Matrix{Float64}:
0.540302 -0.416147
-0.989992 -0.653644
julia> cos([1 2; 3 4]) == cos.([1 2; 3 4])
falseTech Notes
cos(::Number): by pure juliacos(::BigFloat): by MPFR
Version History
Introduced in Julia v1.0 (2018)