Julia 数学函数完全指南(中文版)
“内置 + 高性能 + 支持复数” —— Julia 是数学家的编程天堂
一、Julia 数学函数总览
类别 代表函数 基本函数 abs, sign, sqrt, cbrt三角函数 sin, cos, tan, asin, acos, atan双曲函数 sinh, cosh, tanh指数对数 exp, log, log10, log2幂函数 ^, sqrt, hypot取整 round, floor, ceil, trunc特殊函数 gamma, beta, erf, besselj随机数 rand, randn, randexp
所有函数 :
支持 实数、复数、有理数
自动 向量化 (sin.([1,2,3]))
调用 高性能 BLAS / libm
二、基本数学函数
函数 说明 示例 abs(x)绝对值 abs(-5) → 5, abs(-3.14) → 3.14abs2(x)绝对值平方(更快) abs2(3+4im) → 25sign(x)符号 sign(-5) → -1, sign(0) → 0sqrt(x)平方根 sqrt(16) → 4.0, sqrt(-1) → 0.0 + 1.0imcbrt(x)立方根 cbrt(27) → 3.0hypot(x,y)斜边长 √(x²+y²) hypot(3,4) → 5.0
abs.([-2, -1, 0, 1, 2]) # [2, 1, 0, 1, 2]
三、三角函数与反三角函数
函数 说明 sin, cos, tan弧度制 sind, cosd, tand角度制 (度)asin, acos, atan反三角 atan(y,x)二参数反正切(象限正确)
sin(π/2) # 1.0
cosd(180) # -1.0
atan(1, 0) # π/2(第二象限)
四、双曲函数
sinh(x) = (e^x - e^-x)/2
cosh(x) = (e^x + e^-x)/2
tanh(x) = sinh(x)/cosh(x)
tanh(1) ≈ 0.76159
五、指数与对数
函数 说明 exp(x)e^x log(x)自然对数(底 e) log(b, x)以 b 为底 log10(x)底 10 log2(x)底 2
exp(1) ≈ 2.71828
log(exp(3)) == 3.0
log10(100) == 2.0
log2(8) == 3.0
六、取整与舍入
函数 说明 示例 round(x)四舍五入 round(3.6) → 4.0floor(x)向下取整 floor(3.9) → 3.0ceil(x)向上取整 ceil(3.1) → 4.0trunc(x)向零取整 trunc(-3.7) → -3.0round(T, x)转为类型 T round(Int, 3.7) → 4
round(3.5) # 4.0(银行家舍入:偶数保留)
round(2.5) # 2.0
七、特殊数学函数(SpecialFunctions.jl)
需安装:] add SpecialFunctions
using SpecialFunctions
gamma(5) # 24.0((n-1)!)
beta(2, 3) # 0.0833(B函数)
erf(1) # 误差函数
besselj(0, 1) # 贝塞尔函数
八、复数支持(自动!)
z = 3 + 4im
sqrt(z) # √z
exp(z) # e^z
log(z) # 主支对数
sin(z) # 复数正弦
exp(im * π) + 1 ≈ 0im # 欧拉公式
九、随机数生成
函数 分布 rand()[0,1) 均匀 randn()标准正态 randexp()指数分布 rand(T, dims...)指定类型和维度
rand(5) # 5个 [0,1) 浮点数
randn(3, 3) # 3×3 标准正态矩阵
rand(Int, 1:100, 5) # 5个 1~100 随机整数
设置种子:using Random; Random.seed!(42)
十、向量化与广播(., @.)
x = [0, π/2, π, 3π/2, 2π]
sin.(x) # [0.0, 1.0, 0.0, -1.0, 0.0]
cos.(x)
# @. 宏:自动加点
@. sin(x)^2 + cos(x)^2 # [1.0, 1.0, 1.0, 1.0, 1.0]
十一、数学常量
常量 值 π (pi)3.14159… ℯ (e)2.71828… im虚数单位 Inf正无穷 NaN非数
π * 2^2 # 圆面积
exp(1) # ℯ^1
十二、综合示例:数值积分(梯形法则)
function trapezoid(f, a, b, n=1000)
h = (b - a) / n
s = (f(a) + f(b)) / 2
for i in 1:n-1
s += f(a + i*h)
end
return s * h
end
# 积分 sin(x) 从 0 到 π
result = trapezoid(sin, 0, π, 10000)
println("∫sin(x) dx from 0 to π = $result") # ≈ 2.0
十三、综合示例:复数曼德博集
function mandelbrot(c, max_iter=100)
z = 0im
for i in 1:max_iter
z = z^2 + c
abs(z) > 2 && return i
end
return max_iter
end
# 生成网格
x = range(-2, 1, length=400)
y = range(-1.5, 1.5, length=400)
grid = [mandelbrot(complex(i,j)) for j in y, i in x]
# 可视化(需 Plots)
# using Plots
# heatmap(grid, c=:viridis, aspect_ratio=1, axis=false)
十四、数学函数速查表
类别 函数 基本 abs, abs2, sign, sqrt, cbrt, hypot三角 sin, cos, tan, asin, acos, atan角度 sind, cosd, tand双曲 sinh, cosh, tanh指数 exp, log, log10, log2取整 round, floor, ceil, trunc特殊 gamma, beta, erf, besselj随机 rand, randn, randexp
十五、小练习(立即上手)
计算 √(3² + 4²) 不使用 ^
将 45° 转为弧度并计算 sin
生成 100 个标准正态随机数,计算均值和标准差
验证 exp(imθ) = cosθ + i*sinθ
实现 factorial(n) 用 gamma 函数
答案示例
# 1. 斜边
hypot(3, 4) # 5.0
# 2. 45° sin
sin(45 * π / 180) # ≈ 0.7071
sind(45) # 相同
# 3. 正态统计
using Statistics
x = randn(100)
mean(x), std(x)
# 4. 欧拉公式
θ = π/4
exp(im*θ) ≈ cos(θ) + im*sin(θ) # true
# 5. 阶乘
myfactorial(n) = gamma(n+1)
myfactorial(5) # 120.0
十六、性能建议
建议 说明 用 abs2(x) 代替 abs(x)^2 避免开方 用 hypot(x,y) 代替 sqrt(x^2 + y^2) 数值更稳定 向量化 f.(x) 比循环快 10~100 倍 复数函数原生支持 无需额外库
恭喜!你已精通 Julia 数学函数!
现在你可以:
写出优雅精确 的数学代码
处理复数信号 如呼吸般自然
轻松实现数值算法
绘制曼德博集 仅需几行
下一站推荐
主题 为什么学 LinearAlgebra矩阵、特征值、SVD Statistics统计函数 Optim.jl优化 DifferentialEquations.jl微分方程 Plots.jl数学可视化
需要我:
生成 数学函数思维导图 ?
写一个 科学计算小工具箱 ?
实现 高精度 π 计算 (Machin 公式)?
绘制 复平面函数图像 ?
随时告诉我!