2017年4月25日火曜日

学習環境

ラング線形代数学(上)(S.ラング (著)、芹沢 正三 (翻訳)、ちくま学芸文庫)の1章(R^nにおけるベクトル)、6(複素数)、練習問題7.を取り組んでみる。


    1. e i( θ 1 + θ 2 ) =cos( θ 1 + θ 2 )+isin( θ 1 + θ 2 ) =cos θ 1 cos θ 2 sin θ 1 sin θ 2 +isin θ 1 cos θ 2 +icos θ 1 sin θ 2 =( cos θ 1 +isin θ 1 )( cos θ 2 +isin θ 2 ) = e i θ 1 · e i θ 2 a+bi a 2 + b 2 =1 1sinθ+i1cosθ= e it

    2. z=a+bi z= a 2 + b 2 ( a a 2 + b 2 + b a 2 + b 2 i ) = a 2 + b 2 ( cosθ+isinθ )

    3. z 1 z 2 = r 1 e i θ 1 r 2 e i θ 2 = r 1 r 2 e i θ 1 e i θ 2 = r 1 r 2 e i( θ 1 + θ 2 )

    4. z=r e iθ z 1 n = r 1 n e iθ n w= r 1 n e i θ n w n =r e inθ· 1 n =r e iθ

コード(Emacs)

HTML5

<pre id="output0"></pre>
θ = <input id="theta0" type="number" value="1.2">
θ_1 = <input id="theta1" type="number" value="2.3">
θ_2 = <input id="theta2" type="number" value="3.4">
<br>
z = <input id="a0" type="number" value="5.6"> + <input id="b0" type="number" value="7.8">i
<br>
r_1 = <input id="r1" type="number" min="0" value="9.0">
r_2 = <input id="r2" type="number" min="0" value="1.2">
<br>
n = <input id="n0" type="number" min="1" step="1" value="5">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample7.js"></script>

JavaScript

let pre0 = document.querySelector('#output0'),
    input_0 = document.querySelector('#theta0'),
    input_1 = document.querySelector('#theta1'),
    input_2 = document.querySelector('#theta2'),
    input_a = document.querySelector('#a0'),
    input_b = document.querySelector('#b0'),
    input_r1 = document.querySelector('#r1'),
    input_r2 = document.querySelector('#r2'),
    input_n = document.querySelector('#n0'),
    inputs = [input_0, input_1, input_2, input_a, input_b, input_r1, input_r2,
             input_n],
    btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let result = [];

        for (let i = start; i < end; i += step) {
            result.push(i);
        }
        return result;
    };

let Complex = (x, y) => {
    let that = {},
        toString = () => `${x} + ${y}i`,
        real = () => x,
        imag = () => y,
        add = (z) => Complex(x + z.real(), y + z.imag()),
        mul = (z) => Complex(x * z.real() - y * z.imag(),
                             x * z.imag() + y * z.real()),
        conjugate = () => Complex(x, -y),
        inv = () => {
            let den = Math.pow(x, 2) + Math.pow(y, 2);
            return Complex(x / den, -y / den);
        },
        mag = () => Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)),
        isEqual = (z) => x === z.real() && y === z.imag();

    that.toString = toString;
    that.real = real;
    that.imag = imag;
    that.add = add;
    that.mul = mul;
    that.conjugate = conjugate;
    that.inv = inv;
    that.mag = mag;
    that.isEqual = isEqual;
    
    return that;
};

let f = (theta) => Complex(Math.cos(theta), Math.sin(theta));

let output = () => {
    let theta = parseFloat(input_0.value),
        theta1 = parseFloat(input_1.value),
        theta2 = parseFloat(input_2.value);
    

    p('a.');
    let left= f(theta1 + theta2),
        right = f(theta1).mul(f(theta2));

    p(left.isEqual(right));
    p(left);
    p(right);

    p('b.');
    let a = parseFloat(input_a.value),
        b = parseFloat(input_b.value),
        r = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)),
        theta_b = Math.acos(a / r);

    p(`r = ${r}, θ = ${theta_b}`);
    left = Complex(a, b);
    right = Complex(r, 0).mul(f(theta_b));
    p(left.isEqual(right));
    p(left);
    p(right);

    p('c.');
    let r1 = parseFloat(input_r1.value),
        r2 = parseFloat(input_r2.value),
        z1 = Complex(r1, 0).mul(f(theta1)),
        z2 = Complex(r2, 0).mul(f(theta2));

    left = z1.mul(z2);
    right = Complex(r1 * r2, 0).mul(f(theta1 + theta2));
    p(left.isEqual(right));
    p(left);
    p(right);

    p('d.');
    let z = Complex(a, b),
        n = parseInt(input_n.value, 10),
        w = Complex(Math.pow(z.mag(), 1 / n), 0).mul(f(Math.acos(a / z.mag()) / n));

    let wn = w;
    for (let i = 2; i <= n; i += 1) {
        wn = wn.mul(w);
    }
    p(wn.isEqual(z));
    p(wn);
    p(z);
};

inputs.forEach((input) => input.onchange = output);
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();

θ = 
θ_1 = 
θ_2 = 

z = + i
r_1 = r_2 =
n =

0 コメント:

コメントを投稿

Comments on Google+: