LoginSignup
8
3

More than 5 years have passed since last update.

julia を使ってみる 1 (インストールから文字列・整数・浮動小数点・虚数等)

Last updated at Posted at 2018-08-14

julia V1.0.0が公開されたのでjuliaを利用してみる

juliaのインストール

実行環境は次の通り

$ uname -prsv
Darwin 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64 i386

Download Juliaからダウンロード

maxOS XのJuliia.DMGをダウンロードして展開すると次の通り
Screen Shot 2018-08-14 at 16.08.51.png
Julia-1.0.appをApplicationsにドラッグ&ドロップ

juilaへのシンボリックリンクの作成

これで、ApplicationsフォルダのJulia-1.0.appをダブルクリックしてJuliaのコンソールが起動するが、このままでは不便なので、/usr/local/binにリンクを作成しておく。

$ ln -s /Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia

これでTerminalからjuliaを起動できるようになる

$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.0.0 (2018-08-08)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> 

juliaで Hello World!

コンソールが起動する事がわかったので、とりあえず"Hello World!"

julia> println("Hello World!")
Hello World!

julia> println('Hello World!')
ERROR: syntax: invalid character literal

'は文字列の指定には利用できない

文字・文字列

'は文字(シングルキャラクタ)の指定に利用する

julia> println('a')
a
  • Int()で文字からアスキーコード(ユニコード)への変換
  • Char()で整数から文字への変換
julia> Int('a')
97

julia> Char(97)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

文字列の操作

文字の連結

文字の連結に"*"を利用するのはちょっと驚き

julia> "ABC" + "DEF"
ERROR: MethodError: no method matching +(::String, ::String)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
Stacktrace:
 [1] top-level scope at none:0

julia> "ABC" * "DEF"
"ABCDEF"

部分文字列の抽出

一文字目が0では無かった
1-basedで位置指定を行う julia は Fortran文化圏に所属しているのか?

julia> "ABCDEF"[0]
ERROR: BoundsError: attempt to access "ABCDEF"
  at index [0]
Stacktrace:
 [1] checkbounds at ./strings/basic.jl:193 [inlined]
 [2] codeunit at ./strings/string.jl:87 [inlined]
 [3] getindex(::String, ::Int64) at ./strings/string.jl:206
 [4] top-level scope at none:0

julia> "ABCDEF"[1]
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)

pythonのスライス指定とはちょっと趣が異なる

julia> "ABCDEF"[1:1]
"A"

julia> "ABCDEF"[1:2]
"AB"

julia> "ABCDEF"[2:]
ERROR: syntax: missing last argument in "2:" range expression 

julia> "ABCDEF"[:]
"ABCDEF"

julia> "ABCDEF"[:2]
'B': ASCII/Unicode U+0042 (category Lu: Letter, uppercase)

REPL環境でのhelpの表示

?を入力

julia > ?

julia >のプロンプトが help?>に変わるので調べたい内容を入力

help?> Int
search: Int Int8 Int64 Int32 Int16 Int128 Integer intersect intersect! InteractiveUtils InterruptException UInt UInt8

  Int64 <: Signed

  64-bit signed integer type.

整数

この環境でのデフォルトの整数

julia> Int
Int64

64bitの整数がオーバーフローした場合の挙動

julia> 2^63 -1
9223372036854775807

julia> 2^63 -1 + 1
-9223372036854775808

バイナリの内部表現そのまま処理されている

整数の型指定を行う

明示的に変数の型を指定してインクリメントしてみる

julia> a = Int128(2^63 - 1)
9223372036854775807

julia> a + 1
9223372036854775808

Int64の領域を超えて格納できることがわかったので、最初から2^64 - 1を代入してみる

julia> a = Int128(2^64 - 1)
-1

期待した結果にはならない
Int128のaに代入されているのはInt64で計算された2^64-1だった
そのため結果が-1となっている

それなら、Int128で宣言した変数を利用して計算を実行し、その結果をaに代入してみることとしよう

julia> b = Int128(2)
2

julia> b^64 - 1
18446744073709551615

julia> a = (b^64 -1)
18446744073709551615

julia> a = Int128(2)^64 -1
18446744073709551615

期待どおりの結果が得られた

算術演算

3*a を3aと省略して処理可能

julia> a = 2
2

julia> 3a
6

下記のような演算も可能

julia> 3a^2
12

julia> (3a)^2 
36

虚数の利用

julia> a = 1 + 1im
1 + 1im

julia> b = 1 - 1im
1 - 1im

julia> a*b
2 + 0im

分数の利用

julia> a = 1//2
1//2

julia> b = 1//3
1//3

julia> a + b
5//6

計算機の速度

倍精度で行列を計算した場合の演算速度の出力


julia> peakflops()/ 1e9
109.94325491912406

julia> peakflops()/ 1e9
143.87130452841583

julia> peakflops()/ 1e9
131.39247054954794

動作環境のレポート

julia> versioninfo()
Julia Version 1.0.0
Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, haswell)

今日はここまで:smile:

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3