跳至內容

脈衝星和中子星/Julia 入門教程 - 脈衝星天文學家

來自華夏公益教科書,開放的書籍,為了開放的世界

Julia 是一種相對較新的語言,它非常強大,但目前文件難以查詢和理解。在這裡,我們提供一些對脈衝星天文學家感興趣的筆記。維基百科上的許多圖都是使用 Julia 製作的。Julia 可以輕鬆地 [下載 並安裝到大多數計算機上。它有自己的強大例程,但也可以直接使用 Python 或 C 程式碼。它專為並行或分散式計算而設計,程式設計師為程式碼執行的速度感到自豪——它比 Python 快得多。

Julia 可以從個人電腦執行,使用 Julia 筆記本或使用網路託管服務。Julia 可以用作簡單的計算器

julia> 3+3
6
julia> x=10
10
julia> x+2
12

也可以編寫代數表示式(比大多數語言更簡單)

julia> x=10
10
julia> 2x
20
julia> 2*x
20

字串可以輕鬆建立

julia> x="Hello";
julia> x
"hello"

請注意行末使用“;”。如果使用,則該行不會輸出任何內容。

建立值陣列

[編輯 | 編輯原始碼]
julia> x=linspace(0,99,100)
julia> x+10

將分配 100 個值,從 0 到 99。第二個命令將為陣列中的每個元素加 10。

要設定初始化為零的陣列,我們可以使用

julia> x=zeros(100)

將分配一個包含 100 個零的陣列。

要分配均值為零、方差為 1 的隨機高斯噪聲,我們可以編寫

julia> x = randn(100) # Gaussian, random noise
julia> y = rand(100) # Linear, random deviates between 0 and 1

請注意 # 符號用作註釋。

製作圖表

[編輯 | 編輯原始碼]

在 Julia 中製作圖表有多種方法,但它們需要新增新的包。這些包是

  • PyPlot
  • Winston
  • Gaston

要安裝包,例如:

julia> Pkg.add("Gaston")

Gaston 基於 gnuplot,gnuplot 需要在您的系統上獨立安裝。

julia> Pkg.add("Gaston")  # This only needs to be done once
julia> using Gaston;
julia> set_terminal("x11");
julia> x=randn(1000); # Set some random numbers
julia> plot(x) # Note not using a semi-colon here as we want to see the result

我們也可以使用兩個陣列

julia> using Gaston;
julia> set_terminal("x11");
julia> x=linspace(0,100,1000);
julia> y=randn(1000); # Set some random numbers
julia> plot(x,y)

這將生成 MATLAB 風格的圖表

julia> using Winston;
julia> x=linspace(0,100,1000);
julia> y=randn(1000); # Set some random numbers
julia> plot(x,y,"b") # Plot as a blue line
julia> plot(x,y,"r") # Plot as a red line
julia> plot(x,y,"ro") # Plot as red open circles
julia> plot(x,y,"x") # Plot as red crosses
julia> oplot(x,y,"b") # Overplot the current figure using a blue line
julia> xlabel("My x-label") # Add an x-label to the current figure
julia> ylabel("My y-label (\\mus s)") # Add a y-lable to the current figure. Note the use of LaTeX symbols
julia> title("This is my title") # Add a title to the plot
julia> savefig("try.png") # Save a png file of the figure in the local directory

圖表也可以從多個元件構建,如下所示

julia> using Winston;
julia> p = FramedPlot(xlabel="x-label",ylabel="y-label",xlog=true,ylog=true,xrange=(9e-4,20),yrange=(1e-14,1e-9)) # Note the setting of labels and logarithmic scaling and ranges.
julia> x = abs(randn(1000))
julia> y = abs(randn(1000))*1e-11
julia> pts = Points(x,y)
julia> add(p,pts) # This will draw the figure as requested
julia> closefig() # This closes the current figure
julia> x2 = abs(randn(1000))
julia> y2 = abs(randn(1000))*1e-11
julia> l = Curve(x2,y2,"type","dotted") # this will now add a dotted line
julia> setattr(pts,label="My data points");  # Set a label
julia> setattr(l,label="a dotted line");
julia> leg = Legend(.1,.9, {pts,l})
julia> add(p,pts,l,leg)

Pyplot 使用 python 繪圖例程

julia> using PyPlot
julia> x=linspace(0,100,1000);
julia> y=randn(1000); # Set some random numbers
julia> p = plot(x,y)
julia> xlabel("My x label")
julia> ylabel("My y label")
julia> title("My title")
julia> grid("on")
julia> cla() # Clear the figure
julia> p = plot(x,y,linestyle="-.",marker="None",label="Base Plot",color="red") # Plot using dotted lines in red

可以使用以下方法繪製 Aitoff 投影

julia> using PyPlot
julia> subplot(projection="aitoff")
julia> grid("on")

讀取和寫入檔案

[編輯 | 編輯原始碼]

數學例程

[編輯 | 編輯原始碼]
華夏公益教科書