Skip to content

Commit

Permalink
For 1-EX4
Browse files Browse the repository at this point in the history
  • Loading branch information
wistaria committed Jul 2, 2018
1 parent 623e921 commit 0fa6702
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 60 deletions.
16 changes: 15 additions & 1 deletion exercise/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ZIP = example-1-L2.zip example-1-L3.zip \
ZIP = example-1-L2.zip example-1-L3.zip example-1-EX4.zip \
example-2-L1.zip example-2-L2.zip example-2-L3.zip example-2-EX3.zip example-2-L4.zip example-2-EX4.zip

EXAMPLE1L2 = matrix/cmatrix.h matrix/matrix_example.c matrix/vector.dat matrix/matrix.dat \
Expand All @@ -11,6 +11,11 @@ EXAMPLE1L3 = matrix/cmatrix.h matrix/matrix_example.c matrix/vector.dat matrix/m
linear_system/lu_decomp.c linear_system/input1.dat \
monte_carlo/mersenne_twister.h

EXAMPLE1EX4 = matrix/cmatrix.h \
eigenvalue_problem/diag.c eigenvalue_problem/dsyev.h eigenvalue_problem/matrix1.dat \
svd/svd.c svd/full_svd.c svd/dgesvd.h svd/matrix2.dat svd/matrix3.dat svd/matrix4.dat \
linear_regression/measurement1.dat linear_regression/measurement2.dat

EXAMPLE2L1 = basics/bisection.c eigenvalue_problem/double_well.c linear_system/determinant.c linear_system/lu_decomp.c matrix/cmatrix.h linear_system/input1.dat linear_system/input2.dat

EXAMPLE2L2 = monte_carlo/random.c monte_carlo/histogram.c \
Expand Down Expand Up @@ -54,6 +59,15 @@ example-1-L3.zip : $(EXAMPLE1L3)
zip example-1-L3.zip example-1-L3/*
rm -rf example-1-L3

example-1-EX4.zip : $(EXAMPLE1EX4)
rm -rf example-1-EX4
mkdir example-1-EX4
cp -p $(EXAMPLE1EX4) example-1-EX4
echo "CFLAGS = -O3" > example-1-EX4/Makefile
echo "LDLIBS = -llapack -lblas" >> example-1-EX4/Makefile
zip example-1-EX4.zip example-1-EX4/*
rm -rf example-1-EX4

example-2-L1.zip : $(EXAMPLE2L1)
rm -rf example-2-L1
mkdir example-2-L1
Expand Down
File renamed without changes.
22 changes: 12 additions & 10 deletions exercise/exercise-1-4.tex
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@

\noindent
\begin{itemize}
\item 講義のページ: \verb+http://exa.phys.s.u-tokyo.ac.jp/ja/lectures/2017S-computer1+
\item 講義のページ: \verb+http://exa.phys.s.u-tokyo.ac.jp/ja/lectures/2018s-computer1+

\item サンプルプログラム: \\ {\small \verb+https://github.com/todo-group/computer-experiments/tree/master/exercise/ex4+}
\item サンプルプログラム: \\ {\small \verb+https://github.com/todo-group/computer-experiments/releases/download/2018-1-EX4/example-1-EX4.zip+}

\item 準備練習
\begin{enumerate}
\item ハウスホルダー法による対角化のサンプルプログラム({\tt diag.c})をコンパイル・実行せよ
\item ハウスホルダー法による対角化のサンプルプログラム({\tt diag.c})をコンパイル・実行せよ
\begin{quote} \tt
\$ \underline{cc diag.c -o diag -llapack -lm} \\
\$ \underline{./diag matrix1.dat}
\end{quote}
\item {\tt diag.c}で得られた固有ベクトルが互いに正規直交していることを確認するコードを作成し実行せよ。(それぞれの固有ベクトルを行とする行列とその転置行列をかけて、単位行列になることを確認すればよい)
\item {\tt diag.c}で得られた固有ベクトルが互いに正規直交していることを確認するコードを作成し実行せよ。(それぞれの固有ベクトルを行とする行列とその転置行列をかけて、単位行列になることを確認すればよい。可能であればBLASライブラリの{\tt dgemm} [EX3応用課題3]を使ってみよ)
\end{enumerate}

\item 基本課題
Expand All @@ -38,20 +38,22 @@
\[ \lambda_k = \frac{1}{2 (1 - \cos (\pi (2 k - 1) / (2 n + 1)))} \ \ (k=1,\cdots,n)\]
で与えられることが知られている。べき乗法を用いて、最大固有値を計算するプログラムを作成し、その結果を理論式と比較せよ。(上の式では添字は1から始まっているが、C言語では0から始まることに注意)

また、$n$を変えた時、最大固有値の収束の速さがどのように変わるか調べよ
\item ファイル{\tt measurement.dat}に、ある実験で得られたデータが収められている。1カラム目は$x$、2カラム目は$y$、3カラム目は$y$の誤差の値である。最小二乗法によりデータを多項式でフィッティングするプログラムを作成せよ。
また、$n$を変えた時、最大固有値の収束の速さがどのように変わるか調べよ
\item ファイル{\tt measurement1.dat}に、ある実験で得られたデータが収められている。1カラム目は$x$、2カラム目は$y$、3カラム目は$y$の誤差の値である。最小二乗法によりデータを多項式でフィッティングするプログラムを作成せよ。(実習EX3で用いたLU分解、あるいは応用課題2の特異値分解を使って良い)

多項式の最大次数を大きくしていくとフィッティング結果はどのように変化するか?何次の多項式を使うのが最も良いと考えられるか考察せよ。
多項式の最大次数を大きくしていくとフィッティング結果はどのように変化するか?何次の多項式を使うのが最も良いと考えられるか考察せよ

また、同様の解析をファイル{\tt measurement2.dat}に対して行ってみよ
\end{enumerate}
\item 応用課題
\begin{enumerate}
\item Lanczos法により固有値を計算するプログラムを作成せよ。Ritz値が、繰り返しに従ってどのように振る舞うか図示してみよ。収束の速さをべき乗法と比較せよ
\item 特異値分解のサンプルプログラム({\tt svd.c})をコンパイル・実行せよ。入力{\tt matrix2.dat}を用いて、講義L4の例が再現されるかどうか確認せよ
\item Lanczos法により固有値を計算するプログラムを作成せよ。Ritz値が、繰り返しに従ってどのように振る舞うか図示してみよ。収束の速さをべき乗法と比較せよ
\item 特異値分解のサンプルプログラム({\tt svd.c})をコンパイル・実行せよ。入力{\tt matrix2.dat}を用いて、講義L4の例が再現されるかどうか確認せよ
\begin{quote} \tt
\$ \underline{cc svd.c -o svd -llapack -lm} \\
\$ \underline{./svd matrix2.dat}
\end{quote}
\item {\tt svd.c}中のLAPACKの特異値分解{\tt dgesvd}の呼び出し(54行目)では、行列の次元({\tt m}と{\tt n})、左特異ベクトル({\tt u})と右特異ベクトル({\tt vt})の順番が、もともとの{\tt dgesvd}のドキュメント\footnote{\url{http://www.netlib.org/lapack/explore-html/d8/d2d/dgesvd_8f.html}}とは逆になっている。このプログラムが期待通り正しくSVDを行うなぜか? (ヒント: 講義L3 p.11「Cで作成した行列をFortranに渡すと転置されてしまう」)
さらに、完全SVDを行うプログラム例({\tt full\_svd.c})、$m<n$の行列({\tt matrix3.dat})、ランク落ちしている正方行列({\tt matrix4.dat})も用意されている。出力結果を確認してみよ
\item 画像ファイルを行列形式に変換\footnote{\url{https://github.com/todo-group/computer-experiments/tree/master/misc}に、JPEGやPNGなどの形式の画像ファイルをグレイスケールに変換し、行列の形で書き出すPythonスクリプトがある}し、SVDで圧縮してみよ。どの程度まで圧縮可能か?
\end{enumerate}
\end{itemize}
Expand Down
58 changes: 29 additions & 29 deletions exercise/linear_regression/measurement1.dat
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
0 0.580648 0.204491
0.344828 0.609459 0.193339
0.689655 0.39766 0.30636
1.03448 0.877432 0.142957
1.37931 0.848513 0.19464
1.72414 0.864161 0.225476
2.06897 0.864239 0.176591
2.41379 0.749068 0.227067
2.75862 0.562729 0.269818
3.10345 0.710051 0.233051
3.44828 0.264611 0.255446
3.7931 0.134522 0.201986
4.13793 0.385165 0.235645
4.48276 0.238063 0.126738
4.82759 0.0141938 0.246382
5.17241 -0.720246 0.247899
5.51724 -0.899335 0.211179
5.86207 -1.0018 0.14178
6.2069 -1.35531 0.161611
6.55172 -2.14932 0.182808
6.89655 -2.22324 0.142614
7.24138 -2.80371 0.207168
7.58621 -2.41989 0.255174
7.93103 -3.81289 0.179022
8.27586 -3.83862 0.21709
8.62069 -4.2214 0.258575
8.96552 -5.28855 0.209733
9.31034 -5.56133 0.201754
9.65517 -5.46929 0.189443
10 -6.37134 0.267552
0.344828 1.28722 0.193339
0.689655 1.72941 0.30636
1.03448 2.83938 0.142957
1.37931 3.41688 0.19464
1.72414 4.01517 0.225476
2.06897 4.57411 0.176591
2.41379 4.99401 0.227067
2.75862 5.31897 0.269818
3.10345 5.95381 0.233051
3.44828 5.9721 0.255446
3.7931 6.28197 0.201986
4.13793 6.94878 0.235645
4.48276 7.19407 0.126738
4.82759 7.33881 0.246382
5.17241 6.94919 0.247899
5.51724 7.09115 0.211179
5.86207 7.28595 0.14178
6.2069 7.20593 0.161611
6.55172 6.66162 0.182808
6.89655 6.81362 0.142614
7.24138 6.43529 0.207168
7.58621 6.99747 0.255174
7.93103 5.75905 0.179022
8.27586 5.86411 0.21709
8.62069 5.58835 0.258575
8.96552 4.60444 0.209733
9.31034 4.39111 0.201754
9.65517 4.51882 0.189443
10 3.62866 0.267552
5 changes: 0 additions & 5 deletions exercise/svd/matrix1.dat

This file was deleted.

9 changes: 5 additions & 4 deletions exercise/svd/matrix2.dat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
3 6
1.0 2.0 3.0 4.0 5.0 6.0
6.0 4.0 5.0 2.0 3.0 4.0
8.0 9.0 7.0 10.0 11.0 12.0
4 3
1.0 2.0 3.0
6.0 4.0 5.0
8.0 9.0 7.0
10.0 11.0 12.0
15 changes: 4 additions & 11 deletions exercise/svd/matrix3.dat
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
10 10
9.99439 9.01517 7.97973 7.01960 5.98625 5.00495 4.00370 2.99069 2.01022 0.99348
9.01517 8.95895 8.05503 6.94639 6.03829 4.98501 3.99194 3.02323 1.97395 1.01674
7.97973 8.05503 7.92561 7.07372 5.94514 5.02528 4.00454 2.97520 2.02975 0.98047
7.01960 6.94639 7.07372 6.92436 6.06071 4.96467 4.00854 3.01106 1.98172 1.01301
5.98625 6.03829 5.94514 6.06071 5.94389 5.04397 3.97119 3.01506 1.99432 1.00125
5.00495 4.98501 5.02528 4.96467 5.04397 4.95041 4.05049 2.95445 2.03459 0.98131
4.00370 3.99194 4.00454 4.00854 3.97119 4.05049 3.93367 3.07003 1.94144 1.03334
2.99069 3.02323 2.97520 3.01106 3.01506 2.95445 3.07003 2.92066 2.06878 0.96014
2.01022 1.97395 2.02975 1.98172 1.99432 2.03459 1.94144 2.06878 1.93936 1.03543
0.99348 1.01674 0.98047 1.01301 1.00125 0.98131 1.03334 0.96014 1.03543 0.97922
3 6
1.0 2.0 3.0 4.0 5.0 6.0
6.0 4.0 5.0 2.0 3.0 4.0
8.0 9.0 7.0 10.0 11.0 12.0
11 changes: 11 additions & 0 deletions exercise/svd/matrix4.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
10 10
9.99439 9.01517 7.97973 7.01960 5.98625 5.00495 4.00370 2.99069 2.01022 0.99348
9.01517 8.95895 8.05503 6.94639 6.03829 4.98501 3.99194 3.02323 1.97395 1.01674
7.97973 8.05503 7.92561 7.07372 5.94514 5.02528 4.00454 2.97520 2.02975 0.98047
7.01960 6.94639 7.07372 6.92436 6.06071 4.96467 4.00854 3.01106 1.98172 1.01301
5.98625 6.03829 5.94514 6.06071 5.94389 5.04397 3.97119 3.01506 1.99432 1.00125
5.00495 4.98501 5.02528 4.96467 5.04397 4.95041 4.05049 2.95445 2.03459 0.98131
4.00370 3.99194 4.00454 4.00854 3.97119 4.05049 3.93367 3.07003 1.94144 1.03334
2.99069 3.02323 2.97520 3.01106 3.01506 2.95445 3.07003 2.92066 2.06878 0.96014
2.01022 1.97395 2.02975 1.98172 1.99432 2.03459 1.94144 2.06878 1.93936 1.03543
0.99348 1.01674 0.98047 1.01301 1.00125 0.98131 1.03334 0.96014 1.03543 0.97922

0 comments on commit 0fa6702

Please sign in to comment.