<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>第４講 数値計算（1/2） on 2024年度 発展プログラミング演習（春学期）</title>
    <link>https://ksuap.github.io/2024spring/lesson04/index.html</link>
    <description>Recent content in 第４講 数値計算（1/2） on 2024年度 発展プログラミング演習（春学期）</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>ja</language>
    <lastBuildDate>Thu, 13 Apr 2023 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://ksuap.github.io/2024spring/lesson04/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>ニュートン法による平方根の計算</title>
      <link>https://ksuap.github.io/2024spring/lesson04/newton/index.html</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://ksuap.github.io/2024spring/lesson04/newton/index.html</guid>
      <description>ニュートン法による平方根の計算 アルゴリズム 絶対値 例題 任意桁の計算 ユーザ定義型2（複素数） 練習問題 まとめ アルゴリズム 平方根は Math.sqrtメソッドの呼び出しで求められると述べました． ここでは，Math.sqrtを使わず，ニュートン法を用いて平方根を計算してみましょう．
ニュートン法は，関数$f(x)$が与えられた時，その導関数$f&#39;(x)$を用いて，$f(x) = 0$となる解$x$を数値的に求める方法です． $f(x)=x^2 - 2$とすると，$f(x)=0$の解は$x=\sqrt{2}$ ($x &gt; 0$のとき)です． このように，$f(x)=x^2 - n$の解$x=\sqrt{n} (x &gt; 0)$をニュートン法で求めることで，平方根を計算します． ニュートン法のアルゴリズムは次の通りです．
適当な出発点 $x_0$から始まります． $f(x_0)$の時の$y$座標の値を計算し，$(x_0, y_0)$を求めます． $y_0$の絶対値が閾値（threshold; $0.00001$程度）より小さければ，$x_0$が平方根の値となります． $(x_0, y_0)$における$f(x)$の接線の式$l_0$を求めます． $l_0$と$x$軸との交点座標を求めます．この交点を$x_1$とし，手順の最初に戻ります（今までの$x_0$を$x_1$に置き換えて処理を進めてください）． 絶対値 絶対値は，Math.absメソッドを利用して求められます．
Double value = -10.5; Double positiveValue = Math.abs(value); // =&amp;gt; 10.5 が代入される． ニュートン法 例題 実際にニュートン法のプログラムを書いてみましょう．
import java.util.ArrayList; public class SquareRoot{ void run(String[] args){ ArrayList&amp;lt;Double&amp;gt; targets = findTargets(args); for(Double value: targets) { Double result = calculate(value); System.</description>
    </item>
    <item>
      <title>任意桁の計算</title>
      <link>https://ksuap.github.io/2024spring/lesson04/biginteger/index.html</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://ksuap.github.io/2024spring/lesson04/biginteger/index.html</guid>
      <description>ニュートン法による平方根の計算 任意桁の計算 BigInteger BigIntegerの初期化 Integer型，BigInteger型の相互変換 BigIntegerの四則演算 サンプルプログラム 例題 階乗改 ユーザ定義型2（複素数） 練習問題 まとめ BigInteger Java言語の IntegerやLong型は表せる桁数が決まっています．Integerは 32ビット，Longでも64ビットであるため，それぞれ， $-2^{31}〜2^{31} - 1$，$-2^{63}〜2^{63} - 1$ までの値までしか扱えません．
Javaでは任意桁の計算が行える型が存在します． 任意桁の整数を表すBigIntegerと任意桁の実数を表すBigDecimalです． これらを扱ってみましょう．
BigInteger の初期化 任意桁の整数を表す型です． BigIntegerを利用するときは，import java.math.BigInteger;というimport 文が必要です． BigInteger型の実体を作成するときは，new BigInteger(&amp;quot;表したい数&amp;quot;)のように数値を文字列で指定してください． この型を扱うとき，通常の四則演算記号が使えない点に注意してください．
BigInteger value1 = new BigInteger(&amp;#34;10&amp;#34;); BigInteger value2 = new BigInteger(&amp;#34;20&amp;#34;); BigInteger value3 = value1.add(value2); // =&amp;gt; OK BigInteger value4 = value1 + value2; // =&amp;gt; コンパイルエラー．Integer型，BigInteger型の相互変換 プログラム中でInteger型の値をBigIntegerとして扱いたい場合や，逆にBigInteger型の値をInteger型として扱いたい場合があります． 以下のプログラムのような操作により，相互変換が可能です．
Integer intValue = 10; // Integer型からBigInteger型へ変換する． BigInteger bigValue = BigInteger.</description>
    </item>
    <item>
      <title>ユーザ定義の型2（複素数型）</title>
      <link>https://ksuap.github.io/2024spring/lesson04/complex/index.html</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://ksuap.github.io/2024spring/lesson04/complex/index.html</guid>
      <description>ニュートン法による平方根の計算 任意桁の計算 ユーザ定義型2（複素数） 複素数型の定義 複素数型を利用するプログラムの作成 例題 absolute 例題 conjugate 練習問題 まとめ 複素数型の定義 ユーザ定義の型である複素数型Complexを作成しましょう． フィールドには，Double型のreal，imagを定義してください． また，printメソッドでComplexを出力するようにしましょう．
public class Complex{ Double real; Double imag; void print(){ System.out.printf(&amp;#34;%5.2f + %5.2f i&amp;#34;, this.real, this.imag); } void println(){ this.print(); System.out.println(); } public String toString(){ return String.format(&amp;#34;%5.2f + %5.2f i&amp;#34;, this.real, this.imag); } }上記のように，独自の型にはフィールドとメソッドを同時に定義できます．
なお，toStringメソッドはその実体を文字列に変換するときに自動的に呼び出されます． 標準的な Java の実体を拡張しているため，メソッドのシグネチャは必ず public String toString() である必要があります． もし，publicをつけ忘れるとコンパイルエラーになります．toString内で 利用しているString.formatメソッドは C 言語の sprintf と同じ機能を持つものです． 書式付きフォーマッタで文字列を作成します．
Complex complex = // ... 何か値を作成する． System.</description>
    </item>
    <item>
      <title>練習問題</title>
      <link>https://ksuap.github.io/2024spring/lesson04/assignments/index.html</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://ksuap.github.io/2024spring/lesson04/assignments/index.html</guid>
      <description> ニュートン法による平方根の計算 任意桁の計算 ユーザ定義型2（複素数） 練習問題 線形合同法による擬似乱数列 ニュートン法による立方根の計算 Fibonacci数列（任意桁） Complexの四則演算 まとめ 1. 線形合同法による擬似乱数列 線形合同法（Linear Congruential Generators）を用いて，0〜1の範囲の乱数をコマンドライン引数で指定された数だけ求めてください． コマンドライン引数で何も指定されなかった場合は，10が指定されたものとしてください． クラス名は LinearCongruentialGenerator としてください．
このプログラムも，素数の一覧 と同じように ArrayList&amp;lt;Double&amp;gt;を返すメソッドを作成し， 返されたArrayList&amp;lt;Double&amp;gt;の実体をそのままSystem.out.printlnに渡してください．
線形合同法は，擬似乱数を発生させるアルゴリズムです．以下の漸化式で求めます．
$$X_{n+1}=(A\times X_n + B) \mod M$$ $A$，$B$，$M$は定数です． $A$は自分の誕生日（月日．3桁もしくは4桁），$B$は1， $M$は65535，$X_0$は自分の年齢としてください． 完成すれば，$A$，$B$，$M$，$X_0$の値を変更して結果がどのように変わるかを確認しましょう． ただし，$A</description>
    </item>
    <item>
      <title>まとめ</title>
      <link>https://ksuap.github.io/2024spring/lesson04/summary/index.html</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://ksuap.github.io/2024spring/lesson04/summary/index.html</guid>
      <description> ニュートン法による平方根の計算 アルゴリズム 絶対値 例題 任意桁の計算 BigInteger BigIntegerの初期化 Integer型，BigInteger型の相互変換 BigIntegerの四則演算 サンプルプログラム 例題 階乗改 ユーザ定義型2（複素数型） 複素数型の定義 複素数型を利用するプログラムの作成 例題 absolute 例題 conjugate 練習問題 線形合同法による擬似乱数列 ニュートン法による立方根の計算 Fibonacci数列（任意桁） Complexの四則演算 まとめ まとめ 任意桁の計算には，BigInteger，BigDecimalを利用する． 利用には，import文が必要． BigIntegerを利用するときは，import java.math.BigInteger;． BigDecimalを利用するときは，import java.math.BigDecimal;． 通常の四則演算などの演算子（+，*など）は利用できない． 四則演算などはBigIntegerで定義されているメソッド呼び出しで計算する． BigDecimalも同様にメソッド呼び出しで四則演算などを実現する． ユーザ定義の型には，変数（フィールド）とメソッドを定義できる． どちらも，その型の変数を経由してアクセスする． 第３講 ユーザ定義型も参照のこと． </description>
    </item>
  </channel>
</rss>