Julia 0.6 から 1.x への移植

整数の直後に .^ と書かない

例えば,2.^3 のように,整数の直後に .^ を置く式は,意図が不明瞭なので禁止された. .^ の前が整数であることを明示するには,.^ の前に空白を書く. .^ の前が浮動小数点数あることを明示するには,ピリオドのあとに数字を書く.

配列とスカラーの加減算は .+, .- を用いる

v0.6.4 では, 配列 v にスカラー a を加減する( v+a または v-a )ことができたが, v1.x ではできなくなった. 代わりに,v.+a または v.-a とする. あるいは,式の前に @. を置いて,@. v+a または @. v-a と書いてもよい.

1次元配列(ベクトル)の場合

julia> v = [1, 2, 3, 4]
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> v + 1 # エラー
ERROR: MethodError: no method matching +(::Array{Int64,1}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:538
  +(!Matched::BigInt, ::Union{Int16, Int32, Int64, Int8}) at gmp.jl:530
  +(!Matched::ArrayInterface.StaticInt{0}, ::Integer) at /home/runner/.julia/packages/ArrayInterface/n3usB/src/static.jl:57
  ...

julia> v .+ 1
4-element Array{Int64,1}:
 2
 3
 4
 5

julia> @. v + 1
4-element Array{Int64,1}:
 2
 3
 4
 5

julia> v - 1 # エラー
ERROR: MethodError: no method matching -(::Array{Int64,1}, ::Int64)
For element-wise subtraction, use broadcasting with dot syntax: array .- scalar
Closest candidates are:
  -(!Matched::Missing, ::Number) at missing.jl:115
  -(!Matched::BigFloat, ::Union{Int16, Int32, Int64, Int8}) at mpfr.jl:425
  -(!Matched::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:117
  ...

julia> v .- 1
4-element Array{Int64,1}:
 0
 1
 2
 3

julia> @. v - 1
4-element Array{Int64,1}:
 0
 1
 2
 3

2次元配列(行列)の場合

julia> m=[ 1 2; 3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> m+1 # エラー
ERROR: MethodError: no method matching +(::Array{Int64,2}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:538
  +(!Matched::BigInt, ::Union{Int16, Int32, Int64, Int8}) at gmp.jl:530
  +(!Matched::ArrayInterface.StaticInt{0}, ::Integer) at /home/runner/.julia/packages/ArrayInterface/n3usB/src/static.jl:57
  ...

julia> m.+1
2×2 Array{Int64,2}:
 2  3
 4  5

julia> @. m+1
2×2 Array{Int64,2}:
 2  3
 4  5

julia> m-1 # エラー
ERROR: MethodError: no method matching -(::Array{Int64,2}, ::Int64)
For element-wise subtraction, use broadcasting with dot syntax: array .- scalar
Closest candidates are:
  -(!Matched::Missing, ::Number) at missing.jl:115
  -(!Matched::BigFloat, ::Union{Int16, Int32, Int64, Int8}) at mpfr.jl:425
  -(!Matched::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:117
  ...

julia> m.-1
2×2 Array{Int64,2}:
 0  1
 2  3

julia> @. m-1
2×2 Array{Int64,2}:
 0  1
 2  3

配列とスカラーの加減算による更新は .+=, .-= を用いる

v0.6.4 では, 配列 v にスカラー a を加減して更新すること( v+a または v-a )ができたが, v1.x ではできなくなった. 代わりに,v.+=a または v.=a とする. あるいは,式の前に @. を置いて,@. v+=a または @. v-=a と書いてもよい.

1次元配列(ベクトル)の場合

julia> v = [1, 2, 3, 4]
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> v += 1 # エラー
ERROR: MethodError: no method matching +(::Array{Int64,1}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:538
  +(!Matched::BigInt, ::Union{Int16, Int32, Int64, Int8}) at gmp.jl:530
  +(!Matched::ArrayInterface.StaticInt{0}, ::Integer) at /home/runner/.julia/packages/ArrayInterface/n3usB/src/static.jl:57
  ...

julia> v .+= 1
4-element Array{Int64,1}:
 2
 3
 4
 5

julia> @. v += 1
4-element Array{Int64,1}:
 3
 4
 5
 6

julia> v -= 1 # エラー
ERROR: MethodError: no method matching -(::Array{Int64,1}, ::Int64)
For element-wise subtraction, use broadcasting with dot syntax: array .- scalar
Closest candidates are:
  -(!Matched::Missing, ::Number) at missing.jl:115
  -(!Matched::BigFloat, ::Union{Int16, Int32, Int64, Int8}) at mpfr.jl:425
  -(!Matched::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:117
  ...

julia> v .-= 1
4-element Array{Int64,1}:
 2
 3
 4
 5

julia> @. v -= 1
4-element Array{Int64,1}:
 1
 2
 3
 4

2次元配列(行列)の場合

julia> m = [1 2; 3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> m += 1 # エラー
ERROR: MethodError: no method matching +(::Array{Int64,2}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:538
  +(!Matched::BigInt, ::Union{Int16, Int32, Int64, Int8}) at gmp.jl:530
  +(!Matched::ArrayInterface.StaticInt{0}, ::Integer) at /home/runner/.julia/packages/ArrayInterface/n3usB/src/static.jl:57
  ...

julia> m .+= 1
2×2 Array{Int64,2}:
 2  3
 4  5

julia> @. m += 1
2×2 Array{Int64,2}:
 3  4
 5  6

julia> m -= 1 # エラー
ERROR: MethodError: no method matching -(::Array{Int64,2}, ::Int64)
For element-wise subtraction, use broadcasting with dot syntax: array .- scalar
Closest candidates are:
  -(!Matched::Missing, ::Number) at missing.jl:115
  -(!Matched::BigFloat, ::Union{Int16, Int32, Int64, Int8}) at mpfr.jl:425
  -(!Matched::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:117
  ...

julia> m .-= 1
2×2 Array{Int64,2}:
 2  3
 4  5

julia> @. m -= 1
2×2 Array{Int64,2}:
 1  2
 3  4

なお,配列 v にスカラー a を乗除して更新すること( v*=a または v/=a )は v1.x でも可能である.

julia> v = [1, 2, 3, 4]
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> v *= 2
4-element Array{Int64,1}:
 2
 4
 6
 8

julia> v /= 2
4-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0

zeros(a) は廃止. zero(a) を用いる

v0.6.4 では,配列 a に対して zeros(a) というメソッドがあったが,廃止された. v1.x では,代わりに zero(a) を用いる.

このメソッドは,a の要素と同じ要素の型で,a と同じ寸法の配列を作り, 要素の値を全て 0 にする命令である.

julia> a = [1 2; 3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> zeros(a) # エラー
ERROR: MethodError: no method matching zeros(::Array{Int64,2})
Closest candidates are:
  zeros(!Matched::Union{Integer, AbstractUnitRange}...) at array.jl:520
  zeros(!Matched::Type{StaticArrays.SArray{Tuple{N},T,1,N} where T}) where N at /home/runner/.julia/packages/StaticArrays/l7lu2/src/SVector.jl:31
  zeros(!Matched::Type{StaticArrays.MArray{Tuple{N},T,1,N} where T}) where N at /home/runner/.julia/packages/StaticArrays/l7lu2/src/MVector.jl:24
  ...

julia> zero(a)
2×2 Array{Int64,2}:
 0  0
 0  0

julia> a *= 1.0
2×2 Array{Float64,2}:
 1.0  2.0
 3.0  4.0

julia> zeros(a)
ERROR: MethodError: no method matching zeros(::Array{Float64,2})
Closest candidates are:
  zeros(!Matched::Union{Integer, AbstractUnitRange}...) at array.jl:520
  zeros(!Matched::Type{StaticArrays.SArray{Tuple{N},T,1,N} where T}) where N at /home/runner/.julia/packages/StaticArrays/l7lu2/src/SVector.jl:31
  zeros(!Matched::Type{StaticArrays.MArray{Tuple{N},T,1,N} where T}) where N at /home/runner/.julia/packages/StaticArrays/l7lu2/src/MVector.jl:24
  ...

ones(a) は廃止. one(a)を用いる

v0.6.4 では,配列 a に対して zeros(a) というメソッドがあったが,廃止された. v1.1 では,代わりに zero(a) を用いる.

このメソッドは,a の要素と同じ要素の型で,a と同じ寸法の配列を作り, 要素の値を全て 1 にする命令である.

julia> a = [1 2; 3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> ones(a) # エラー
ERROR: MethodError: no method matching ones(::Array{Int64,2})
Closest candidates are:
  ones(!Matched::Union{Integer, AbstractUnitRange}...) at array.jl:520
  ones(!Matched::Type{StaticArrays.SArray{Tuple{N},T,1,N} where T}) where N at /home/runner/.julia/packages/StaticArrays/l7lu2/src/SVector.jl:32
  ones(!Matched::Type{StaticArrays.MArray{Tuple{N},T,1,N} where T}) where N at /home/runner/.julia/packages/StaticArrays/l7lu2/src/MVector.jl:25
  ...

julia> one(a)
2×2 Array{Int64,2}:
 1  0
 0  1

julia> a *= 1.0
2×2 Array{Float64,2}:
 1.0  2.0
 3.0  4.0

julia> one(a)
2×2 Array{Float64,2}:
 1.0  0.0
 0.0  1.0

realmax(t), realmin(t) は廃止. floatmax(t), floatmin(t) を用いる

v0.6.4 の,型 t に対する関数 realmax(a), realmin は廃止となった. 代わりに, v0.6.4 の,型 t に対する関数 floatmax(a), floatmin を用いる.

これらは各々,型 t で表される最大の数,最小の数を返す.

PyPlot パッケージでは o.m の形式が推奨される

v0.6.4 では,PyPlot パッケージで o[:s] の形で, オブジェクト o のシンボル s を読むが,推奨されなくなった. 代わりに,o.s の形を使うことが推奨される.

  • plt[:figure]plt.figure とする.
  • ax1[:plot]ax1.plot とする.
julia> using PyPlot

julia> fig = plt.figure()
PyPlot.Figure(PyObject <Figure size 640x480 with 0 Axes>)

julia> ax1 = fig.add_subplot(121)
PyObject <AxesSubplot:>

julia> ax1.plot([3, 2, 1])
1-element Array{PyCall.PyObject,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f9494f58dc0>

julia> ax2 = fig.add_subplot(122)
PyObject <AxesSubplot:>

julia> ax2.plot([2, 3, 1])
1-element Array{PyCall.PyObject,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f94951cca00>

アスペクト比(縦横の寸法の比)を等しくするのに, plt[:axes]()[:set_aspect]("equal") と書いていたが, plt.axes().set_aspect("equal") とする.

julia> using PyPlot


julia> xs = -1:0.1:1
-1.0:0.1:1.0

julia> ys = xs .^ 2;

julia> zs = xs .^ 3;

julia> plot(xs, ys);

julia> plot(xs, zs);

julia> plt.axes().set_aspect("equal")

linspace は廃止. LinSpace を用いる

v0.6.4で等差数列を作る linspace 関数は,v1.x で廃止された.

関数 LinRange または range を用いる.

v0.6.4 では

  • linspace(a,b) は,初項 a ,最終項 b ,長さ 50 の等差数列を作る.
  • linspace(a,b,n) は,初項 a ,最終項 b ,長さ n の等差数列を作る.

v1.1で,上の2つに対応するのは,関数 LinRange(a,b,n) である. LinRange は3番目の引数を省略できない.

julia> linspace(-1, 1) # エラー
ERROR: UndefVarError: linspace not defined

julia> LinRange(-1, 1) # エラー
ERROR: MethodError: no method matching LinRange(::Int64, ::Int64)
Closest candidates are:
  LinRange(::Any, ::Any, !Matched::Integer) at range.jl:413

julia> LinRange(-1, 1, 50)
50-element LinRange{Float64}:
 -1.0,-0.959184,-0.918367,-0.877551,-0.836735,…,0.877551,0.918367,0.959184,1.0

より柔軟に等差数列を作るには,range 関数を用いる.

関数 range は,引数として,初項を必ず指定する必要がある. 関数 range に,2つの数字を引数に指定すると,2つ目の引数は最終項である. 最終項目は キーワードパラメータ stop で指定することもできる. さらに,キーワード引数として,数列の長さ length ,等差 step を指定できる. step の既定値は 1 である.

julia> range(1, 10) # エラー
ERROR: ArgumentError: At least one of `length` or `step` must be specified

julia> # 初項 1, 最終項 10, 等差 1 (指定しない)
       range(1, 10, length = 10)
1.0:1.0:10.0

julia> # 初項 1, 最終項 10, 等差 2
       range(1, 10, step = 2)
1:2:9

julia> collect(ans)
5-element Array{Int64,1}:
 1
 3
 5
 7
 9

なお, 最終項 stop と長さ length が指定され, 等差 step が指定されない場合には step は計算される. したがって,v0.6.4 の linspace(-1,1) に対応する,もう一つの書き方は range(-1,1,length=50) である.

julia> r1 = LinRange(-1, 1, 50)
50-element LinRange{Float64}:
 -1.0,-0.959184,-0.918367,-0.877551,-0.836735,…,0.877551,0.918367,0.959184,1.0

julia> r2 = range(-1, 1, length = 50)
-1.0:0.04081632653061224:1.0

両者は計算方法が異なるので,数値は微妙に異なる.



julia> r1 = collect(r1)
50-element Array{Float64,1}:
 -1.0
 -0.9591836734693877
 -0.9183673469387754
 -0.8775510204081634
 -0.8367346938775511
 -0.7959183673469388
 -0.7551020408163265
 -0.7142857142857144
 -0.6734693877551021
 -0.6326530612244898
  ⋮
  0.6734693877551021
  0.7142857142857142
  0.7551020408163265
  0.7959183673469388
  0.8367346938775511
  0.8775510204081634
  0.9183673469387754
  0.9591836734693877
  1.0

julia> r2 = collect(r2)
50-element Array{Float64,1}:
 -1.0
 -0.9591836734693877
 -0.9183673469387755
 -0.8775510204081632
 -0.8367346938775511
 -0.7959183673469388
 -0.7551020408163265
 -0.7142857142857143
 -0.673469387755102
 -0.6326530612244898
  ⋮
  0.673469387755102
  0.7142857142857143
  0.7551020408163265
  0.7959183673469388
  0.8367346938775511
  0.8775510204081632
  0.9183673469387755
  0.9591836734693877
  1.0

julia> r1 .== r2 # 全て true ではない
50-element BitArray{1}:
 1
 1
 0
 0
 1
 1
 1
 0
 0
 1
 ⋮
 0
 0
 1
 1
 1
 0
 0
 1
 1

julia> abs.(r1 .- r2) # 残差
50-element Array{Float64,1}:
 0.0
 0.0
 1.1102230246251565e-16
 1.1102230246251565e-16
 0.0
 0.0
 0.0
 1.1102230246251565e-16
 1.1102230246251565e-16
 0.0
 ⋮
 1.1102230246251565e-16
 1.1102230246251565e-16
 0.0
 0.0
 0.0
 1.1102230246251565e-16
 1.1102230246251565e-16
 0.0
 0.0

julia> isapprox.(r1, r2) # 全て true となる
50-element BitArray{1}:
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
 ⋮
 1
 1
 1
 1
 1
 1
 1
 1
 1

logspace は廃止. exp10.(LinRange(a,b,n)) などを用いる

v0.6.4で,$10$ のべき乗で等比数列を作る logspace は,v1.x で廃止された. 代わりに,exp10.(LinRange(a,b,n)) などを用いる.

julia> logspace(-2, 2, 5) # エラー
ERROR: UndefVarError: logspace not defined

julia> exp10.(LinRange(-2, 2, 5)) # LinRange を用いる
5-element Array{Float64,1}:
   0.01
   0.1
   1.0
  10.0
 100.0

julia> exp10.(range(-2, stop = 2)) # range を用いる
5-element Array{Float64,1}:
   0.01
   0.1
   1.0
  10.0
 100.0

srand(n) は廃止. Random.seed!(n) を用いる

v0.6.4 では,関数 srand(m) を用いて, 乱数の種をリセットしたが,v1.x では廃止された.

代わりに,Random パッケージを using してから Random.seed!(n) を用いる.

julia> srand(1234) # エラー
ERROR: UndefVarError: srand not defined

julia> using Random # Random パッケージを読み込む

julia> Random.seed!(1234)
Random.MersenneTwister(UInt32[0x000004d2], Random.DSFMT.DSFMT_state(Int32[-1393240018, 1073611148, 45497681, 1072875908, 436273599, 1073674613, -2043716458, 1073445557, -254908435, 1072827086  …  -599655111, 1073144102, 367655457, 1072985259, -1278750689, 1018350124, -597141475, 249849711, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt128[0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000  …  0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000], 1002, 0)