{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ベクトルや行列の作り方" ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

目次

\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using LinearAlgebra: I" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1次元配列=縦ベクトル=ベクトル" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* コンマは「単に並べる」というニュアンス.\n", "* セミコロンと改行は「縦に並べる」というニュアンス.\n", "\n", "Julia言語では「単に数値を並べただけの1次元配列」は「数値を縦に並べてできる縦ベクトル」とみなされる. \n", "\n", "したがって, 以下の3つは全部縦ベクトルになる. 以下では縦ベクトルを単にベクトルと呼ぶ." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = [1;2;3]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = [\n", " 1\n", " 2\n", " 3\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1×n行列とは異なる双対ベクトルの作り方" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* 空白は「横に並べる」というニュアンス.\n", "\n", "次は1×3行列(2次元配列)になる. この節の目標はこれの説明ではない." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 Array{Int64,2}:\n", " 2 3 -1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = [2 3 -1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1×3行列(1×3の2次元配列)と3次元ベクトル(長さ3の1次元配列)の積は1次元ベクトル(長さ1の1次元配列)になる." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Array{Int64,1}:\n", " 5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f*v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "スカラーを得るにはこうする. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(f*v)[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "これは非常に面倒である. 以下のようにすればこのような手間がいらなくなる.\n", "\n", "次は3次元双対ベクトルになる. (双対ベクトルは双対空間の要素=線形汎函数という意味. 教科書ではよく横ベクトル表記される.)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 LinearAlgebra.Transpose{Int64,Array{Int64,1}}:\n", " 2 3 -1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = transpose([2, 3, -1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(双対ベクトル)\\*(ベクトル)はスカラーになる. 双対空間の概念が実装されているのはJulia言語の大きな特徴である." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g*v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(ベクトル)*(双対ベクトル)は行列になる." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 2 3 -1\n", " 4 6 -2\n", " 6 9 -3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v*g" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "以下のようにして双対ベクトルを作ることもできる. こちらの方が書く分量が減る." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:\n", " 2 3 -1" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h = [2,3,-1]'" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h*v" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 2 3 -1\n", " 4 6 -2\n", " 6 9 -3" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v*h" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ただし, ' は単に転置を取るだけではなく共役を取る操作も含まれている." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Complex{Int64},1}:\n", " 1 + 2im\n", " 3 + 4im" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1+2im, 3+4im]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×2 LinearAlgebra.Adjoint{Complex{Int64},Array{Complex{Int64},1}}:\n", " 1-2im 3-4im" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "次のような場合には積を意味する\\*を省略できる." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30 + 0im" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a'a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "単に共役を取るには次のようにする." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Complex{Int64},1}:\n", " 1 - 2im\n", " 3 - 4im" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conj(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "実部と虚部は次の通り." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 1\n", " 3" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "real(a)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 2\n", " 4" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imag(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 行列の作り方\n", "\n", "* 空白は「横に並べる」という意味.\n", "* セミコロンは「縦に並べる」という意味.\n", "* LinearAlgebra.I は単位行列のように使える." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [1 2 3; 4 5 6; 7 8 9]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [\n", " 1 2 3\n", " 4 5 6\n", " 7 8 9\n", "]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 11 2 3\n", " 4 15 6\n", " 7 8 19" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A + 10I" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10I*v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(行列)\\*(ベクトル)はベクトルになる." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 14\n", " 32\n", " 50" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A*v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(双対ベクトル)\\*(行列)は双対ベクトルになる." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 LinearAlgebra.Transpose{Int64,Array{Int64,1}}:\n", " 7 11 15" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g*A" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:\n", " 7 11 15" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h*A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(双対ベクトル)\\*(行列)\\*(ベクトル)はスカラーになる. (1×1行列になったりしない)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "74" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g*A*v" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "74" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h*A*v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## dot-syntaxで行列を作る方法" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1:4" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1:4" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1:3" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 1:3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(縦ベクトル) .+ (横(双対)ベクトル) の形式で行列を作ることもできる." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Complex{Int64},2}:\n", " 1+1im 1+2im 1+3im\n", " 2+1im 2+2im 2+3im\n", " 3+1im 3+2im 3+3im\n", " 4+1im 4+2im 4+3im" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x .+ im*y'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "より一般に φ.(x, y') の形式で行列を作ることができる." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Complex{Int64},2}:\n", " 1+1im 1+2im 1+3im\n", " 4+1im 4+2im 4+3im\n", " 9+1im 9+2im 9+3im\n", " 16+1im 16+2im 16+3im" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "φ(x, y) = x^2 + im*y\n", "φ.(x, y')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 行列の連結\n", "\n", "* 空白は「横に並べる」という意味\n", "* セミコロンと改行は「縦に並べる」という意味" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 Array{Int64,2}:\n", " 1 2 3 1\n", " 4 5 6 2\n", " 7 8 9 3\n", " 2 3 -1 0" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\n", " A v\n", " g 0\n", "]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 11 12\n", " 13 14" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "P = [11 12; 13 14]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 21 22\n", " 23 24" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q = [21 22; 23 24]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 31 32\n", " 33 34" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R = [31 32; 33 34]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 41 42\n", " 43 44" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = [41 42; 43 44]" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 Array{Int64,2}:\n", " 11 12 21 22\n", " 13 14 23 24\n", " 31 32 41 42\n", " 33 34 43 44" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T = [P Q; R S]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## セミコロンのcollectとしての使い方\n", "\n", "1:3 はベクトル=1次元配列にはならない." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1:3" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1:3" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "UnitRange{Int64}" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "typeof(1:3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1次元配列に変換するには以下のような方法がある." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Array(1:3)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vector(1:3)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "collect(1:3)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1:3;]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "セミコロンは次のような使い方もできる. (時間を逆戻しする動画を作るときに使われる.)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 4\n", " 3\n", " 2\n", " 1" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1:5; 4:-1:1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## meshgrid\n", "\n", "Julia言語の文化圏ではメモリ効率が悪いmeshgridは好かれていない.\n", "\n", "どうしてもmeshgridを使いたければ以下のようにする." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0:1.0:3.0" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = range(1, 3, length=3)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0:1.0:4.0" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = range(1, 4, length=4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### repeatを使う方法" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 1.0 2.0 3.0\n", " 1.0 2.0 3.0\n", " 1.0 2.0 3.0\n", " 1.0 2.0 3.0" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_grid = repeat(x', length(y))" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 1.0 1.0 1.0\n", " 2.0 2.0 2.0\n", " 3.0 3.0 3.0\n", " 4.0 4.0 4.0" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_grid = repeat(y, 1, length(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 複素数を経由する方法" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Complex{Float64},2}:\n", " 1.0+1.0im 2.0+1.0im 3.0+1.0im\n", " 1.0+2.0im 2.0+2.0im 3.0+2.0im\n", " 1.0+3.0im 2.0+3.0im 3.0+3.0im\n", " 1.0+4.0im 2.0+4.0im 3.0+4.0im" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = @.(x' + im*y)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 1.0 2.0 3.0\n", " 1.0 2.0 3.0\n", " 1.0 2.0 3.0\n", " 1.0 2.0 3.0" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_grid = real(z)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 1.0 1.0 1.0\n", " 2.0 2.0 2.0\n", " 3.0 3.0 3.0\n", " 4.0 4.0 4.0" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_grid = imag(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### meshgridは効率が悪い" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ψ (generic function with 1 method)" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ψ(x,y) = x^2 - y^2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "以下の2つの結果は同じになる. x_grid, y_gridを使っている側は無駄にコードを長くしていてかつ無駄にメモリを消費している." ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 0.0 3.0 8.0\n", " -3.0 0.0 5.0\n", " -8.0 -5.0 0.0\n", " -15.0 -12.0 -7.0" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ψ.(x_grid, y_grid)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 0.0 3.0 8.0\n", " -3.0 0.0 5.0\n", " -8.0 -5.0 0.0\n", " -15.0 -12.0 -7.0" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ψ.(x', y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 初期化されたもしくはされていないベクトルや行列の作り方" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 初期化された配列の作り方" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "0を並べる." ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Float64,2}:\n", " 0.0 0.0 0.0\n", " 0.0 0.0 0.0" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "O = zeros(2, 3)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Int64,2}:\n", " 0 0 0\n", " 0 0 0" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "O_int = zeros(Int, 2, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1を並べる." ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Float64,2}:\n", " 1.0 1.0 1.0\n", " 1.0 1.0 1.0" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "K = ones(2, 3)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Int64,2}:\n", " 1 1 1\n", " 1 1 1" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "K_int = ones(Int, 2, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "対角成分に1を並べ, 他は0にする." ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×4 Array{Int64,2}:\n", " 1 0 0 0\n", " 0 1 0 0\n", " 0 0 1 0" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "E = Matrix{Int}(I, 3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "乱数で初期化." ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 0.13125773388044792\n", " 0.25280252772597267\n", " 0.577204341724449 " ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = rand(3) # 0~1の一様分布" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -0.571556 -0.0732707 1.83136 \n", " -0.300118 -1.54188 -0.0802414\n", " -0.295303 -0.998139 0.0670566" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R = randn(3,3) # 標準正規分布乱数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "既存の行列と同じ型・同じサイズの零行列と単位行列." ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Float64,2}:\n", " 0.0 0.0 0.0\n", " 0.0 0.0 0.0" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zero(K)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " 1.0 0.0 0.0\n", " 0.0 1.0 0.0\n", " 0.0 0.0 1.0" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one(R)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 初期化されていない配列の作り方" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0736794302e-313\n", " 3.0988588e-316 \n", " 3.1004738e-316 " ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u = Array{Float64, 1}(undef, 3)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0737161768e-313 \n", " 1.07371617365e-313\n", " 3.0989157e-316 " ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u = Vector{Float64}(undef, 3)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×4 Array{Float64,2}:\n", " 3.58943e-316 3.58946e-316 0.0 0.0\n", " 3.58943e-316 3.58949e-316 0.0 0.0\n", " 3.58946e-316 3.58234e-316 0.0 0.0" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = Array{Float64, 2}(undef, 3, 4)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×4 Array{Float64,2}:\n", " 3.46142e-316 3.57871e-316 0.0 0.0\n", " 1.07063e-313 3.46147e-316 0.0 0.0\n", " 3.46142e-316 3.46135e-316 0.0 0.0" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = Matrix{Float64}(undef, 3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "既存の配列と同じ型・同じサイズの初期化されていない入れる." ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Float64,2}:\n", " 5.55738e-316 3.09868e-316 5.54975e-316\n", " 5.55743e-316 3.09886e-316 0.0 " ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = similar(K)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "_draft": { "nbviewer_url": "https://gist.github.com/f23dc3e8bcf3ec3968f1982174cdf15d" }, "gist": { "data": { "description": "msfd28/ベクトルや行列の作り方.ipynb", "public": true }, "id": "f23dc3e8bcf3ec3968f1982174cdf15d" }, "kernelspec": { "display_name": "Julia 1.1.0", "language": "julia", "name": "julia-1.1" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.1.0" }, "toc": { "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "目次", "title_sidebar": "目次", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }