0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

理系学生のためのnumpy入門

Posted at

前提

基本的にpythonの知識がなくても読めるようにはしておりますが、わからない点あればコメントにて質問いただくと、追記する形で対応しようと考えていおります。

numpy導入

numpyはライブラリなのでインポートが必須なので以下の分をコードで追加,初回に必ず実行すること

import numpy

numpyのベクトル

例えば(1,2,3)のようなベクトルを用意したいときは以下のように書けばベクトル定義できる。

a_vecter = numpy.array([1,2,3])

a_vecterについては単なる変数なのでいかなる名前でもよい。特に0ベクトルに関しては以下のように作ることもできる。

zero_verter = numpy.zeros(3)

(0,0,0)ときは3次元なのでzeros(3)であり,(0,0)ならばzeros(2)にする。

ベクトルの演算

ベクトルの和

a_vecter = numpy.array([1,2,3])
b_vecter = numpy.array([4,5,6])
print(a_vecter + b_vecter)

ベクトルの差

a_vecter = numpy.array([1,2,3])
b_vecter = numpy.array([4,5,6])
print(a_vecter - b_vecter)

ベクトルの内積

a_vecter = numpy.array([1,2,3])
b_vecter = numpy.array([4,5,6])
print(numpy.dot(a_vecter,b_vecter))

ベクトルの外積

a_vecter = numpy.array([1,2,3])
b_vecter = numpy.array([4,5,6])
print(numpy.cross(a_vecter,b_vecter))

ベクトルの長さ(ユークリッド距離)

a_vecter = numpy.array([1,2,3])
b_vecter = numpy.array([4,5,6])
print(numpy.linalg.norm(a_vecter,b_vecter))

numpyの行列

行列は二次元の配列を用いて定義できる。

\begin{pmatrix}
3 & 1 & 4\\
1 & 5 & 9\\
2 & 6 & 5
\end{pmatrix}

上記の行列を定義したいときは、以下のように定義する。

a_matrix = numpy.array([[3,1,4],[1,5,9],[2,9,5]])

特に単位行列はidentity()を用いて定義できる。

I_matrix = numpy.identity(3)

3かけ3の単位行列なのでidentity(3)で、2かけ2の単位行列ならばidentity(2)を用いる。

行列の演算

行列とベクトルの積

ベクトル同士の積と同じくdotにて計算可能

\begin{pmatrix}
3 & 1 & 4\\
1 & 5 & 9\\
2 & 6 & 5
\end{pmatrix}
\begin{pmatrix}
1\\
2\\
3\\
\end{pmatrix}
a_matrix = numpy.array([[3,1,4],[1,5,9],[2,9,5]])
a_vecter = numpy.array([1,2,3])
print(numpy.dot(a_matrix,a_vecter))

逆ベクトル

逆ベクトルはinv関数を用いて計算ができる

a_matrix = numpy.array([[3,1,4],[1,5,9],[2,9,5]])
print(numpy.linalg.inv(a_matrix))

固有値,固有ベクトル

逆ベクトルはinv関数を用いて計算ができる

a_matrix = numpy.array([[3,1,4],[1,5,9],[2,9,5]])
eigenvalues, eigenvectors = np.linalg.eig(a_matrix)
print("固有値:",eigenvalues)
print("固有ベクトル:",eigenvectors)

あとがき

メモ書き風になってしまい申し訳ありません。こちらも動作チェックを行っておりますが、万が一誤っている点、アドバイス等あればコメント残していただければ幸いです。最後までお読みいただきありがとうございました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?