Julia(未来可能替代Python与R语言) 数学函数

关注微信公共号:小程在线

关注CSDN博客:程志伟的博客

版本号 V1.4.2(2020-05-23)

 

一、数学函数

1.round()

四舍五入函数,保留到整数位

julia> round(456.321)
456.0

julia> round(456.521)
457.0

    高版本报错,低版本能实现的功能如下:

julia> round(123.54)
124.0

julia> round(123.54,1)
123.5

julia> round(123.54,-1)
120.0

 

2.rand()和randn()

rand():结果服从[0,1]的均匀分布;

randn():结果服从N(0,1)的正态分布。

julia> show(rand(9))
[0.9481507680803489, 0.4945941107267209, 0.024940697154182567, 0.5704158279340583, 0.6270574650692144, 0.5003381646018885, 0.26248735776413357, 0.26685131628428826, 0.12640432182698547]

得到一个3*2的数据集
julia> show(rand(3,2))
[0.9186417696309077 0.3274585091521769;

0.3649384914441276 0.6292180877607207;

0.909662033678333 0.6009276756647459]


julia> show(rand(Int8,10))
Int8[71, 2, 79, 101, -46, -67, 29, 26, -44, -53]


julia> show(rand(Bool,10))
Bool[1, 0, 0, 1, 0, 1, 1, 1, 1, 0]

给定数据区间的整数组
julia> show(rand(1:6,10))
[6, 1, 6, 6, 5, 1, 3, 5, 5, 6]


julia> show(randn(10))
[2.4737348494389457, 0.02381500170203782, -0.06133256088133444, -0.6994129834660351, -0.605475467534163, 0.9128761414810291, -1.2132405170942984, -0.09117753461036124, 0.3269714960309829, -0.9200957668459002]

 

srand表示随机数,但是只在低版本支持

julia> srand(12345)

julia> show(randn(6))
[1.1723579360368195,0.8527074591390049,0.4155652148758223,0.5164248452392485,
0.6857588179209828,0.28227210708857803]

 

3.sum()

julia>  A = Array(Int64,3,4);A[:] = 1:12;show(A)
3x4 Array{Int64,2}:
 1  4  7  10

julia> 

 2  5  8  11
 3  6  9  12

 

对列求和
julia> sum(A,1)
1x4 Array{Int64,2}:
 6  15  24  33

对行求和

julia> sum(A,2)
3x1 Array{Int64,2}:
 22
 26
 30

对布尔值求和

julia> sum([true,false,true,true,false])
3

 

4.mean()函数

julia> mean([1,2,3])
2.0

julia> mean([true,false])
0.5

julia> mean(A,1)
1x4 Array{Float64,2}:
 2.0  5.0  8.0  11.0

julia> mean(A,2)
3x1 Array{Float64,2}:
 5.5
 6.5
 7.5

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章