Julia程序设计1 介绍和基础数学运算
Julia程序設計1 介紹和基礎數學運算
- 介紹
- 簡單數學運算
介紹
本來打算用Matlab來寫MATH 575A和MATH 575B的筆記的,但最近關于Matlab的新聞讓我覺得可能還是用免費開源又強大的Julia比較好。這個系列的博文介紹Julia程序設計和數值計算,我也是從Matlab轉戰Julia的,就邊學邊寫吧。
Julia下載安裝比較簡單,在https://julialang.org/downloads/就可以下載
我選的是JuliaPro 1.4.2-1 Windows版本的,安裝過程就選了一下路徑。安裝好后打開JuliaPro 1.4.2-1界面長這樣(三個框框是我加的。。。)
這個界面我覺得還是比較友好的,第一欄是菜單欄,左邊豎著的是工具欄,整個界面分為三個區域:Atom、REPL和Workplace。藍框框是編輯器Atom,綠框框是Workplace,黃框框里面的是REPL(Read-Eval-Print-Loop),官方的解釋是
? it reads what a user types,
? the compiler evaluates what it reads,
? it prints out the return value after evaluation, and
? it loops back and does it all over again.
其實就是命令行窗口。在REPL中輸入命令可以按tab鍵自動補全,輸入?加函數或命令可以搜索幫助文檔,輸入?后REPL會從Julia mode變成help mode,比如? println:
help?> println search: println printstyled print sprint isprintprintln([io::IO], xs...)Print (using print) xs followed by a newline. If io is not supplied, prints to stdout.Examples≡≡≡≡≡≡≡≡≡≡julia> println("Hello, world")Hello, worldjulia> io = IOBuffer();julia> println(io, "Hello, world")julia> String(take!(io))"Hello, world\n"簡單數學運算
更具體的可以參考Julia中文文檔https://juliacn.readthedocs.io/en/latest/manual/mathematical-operations.html
1)復數表示: A+Bim
julia> 1+1im 1 + 1imjulia> 2+(1+1im) 3 + 1imjulia> 2.0+(1+1im) 3.0 + 1.0imjulia> (2.0+2.0im)+(1.0+1.0im) 3.0 + 3.0im注意im和虛部B之間不能有空格,不然會報錯
julia> 1 + 1 im ERROR: syntax: extra token "im" after end of expression2)有理數表示:Rational(A,B)
julia> Rational(2,3) 2//3julia> Rational(2,3)+2 8//3julia> Rational(2,3)+2.0 2.6666666666666665注意第一個R是要大寫的,不然會報錯
julia> rational(2,3) ERROR: UndefVarError: rational not defined Stacktrace:[1] top-level scope at none:03)常用的常數
π\piπ和eee是非常常用的兩個常數,在Julia中這兩個常數被預定義為pi和?
注意這里的 ? 不是英語字母e!在Julia中的輸入方法是先輸入\euler再按tab就會轉換成?,因為Julia是支持Unicode字符輸入的,通用方法是輸入符號的Unicode,按tab即可,根據這個方法可以定義希臘字母變量
julia> α,β=0,1;julia> α,β (0, 1)4)真值:true,false
做數值計算時true被當成1,false被當成0
賦值的時候要注意不能直接=true,要用=Bool(true),并且首字母要大寫,不然會報錯
julia> a = Bool(true) truejulia> a = bool(true) ERROR: UndefVarError: bool not defined Stacktrace:[1] top-level scope at none:05)算數運算
再補充一個整除÷
6)比較運算
有幾個比較特殊的值:
? Positive zero is equal but not greater than negative zero.
? Inf is equal to itself and greater than everything else except NaN.
? -Inf is equal to itself and less then everything else except NaN.
? NaN is not equal to, not less than, and not greater than anything,
including itself.
以及相應的用來判斷的函數:
7)邏輯運算
第一個a改成!a,異或運算符號是?,上表給的符號已經沒有再使用了,或者用函數xor(a,b)
8)復合賦值運算
這個和C語言的很像,沒什么特別的。
9)運算優先級
優先級從上到下遞減:
總結
以上是生活随笔為你收集整理的Julia程序设计1 介绍和基础数学运算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 矩阵分析与多元统计12 0-1矩阵 交换
- 下一篇: Julia程序设计2 数值类型