Julia 中使用 Python 函式
既然你在前一個練習中已經用 PythonCall 從 Python 匯入了 math 套件,現在可以開始呼叫該套件裡的函式。
你把 math 以別名 pymath 匯入,所以當你要呼叫 math 的函式時,請在函式前加上 pymath 前綴。這能清楚表明你使用的是從 Python 匯入的套件版本。
本練習屬於課程
Julia 中級
練習說明
- 使用 Julia 的中括號語法,定義向量
x,內容為從負三到三、步長為 1 的數值序列,包含 0。 - 使用
math套件中的 Python 函式fabs,取得x的第二個值的絕對值。 - 使用
math套件中的 Python 函式pow,將x的第六個值提升為x的第七個值的次方。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
using PythonCall
pymath = pyimport("math")
# Define a vector x from -3 to 3
x = ____
# Print the absolute value of the second value in x using pymath
println(____.____(____))
# Print the sixth value of x raised to the power of the 7th value of x
println(____.____(x[6], ____))