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

 

 

 

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