## 2017年4月22日土曜日

### 数学 - JavaScript - 線形代数学 - R^nにおけるベクトル – 複素数(乗法、逆数、除算、絶対値、共役)

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

1. $\frac{-1-3i}{1+9}=-\frac{1}{10}-\frac{3}{10}i$

2. $2+0i$

3. $\left(-1+i\right)\left(2-i\right)=-1+3i$

4. $-1+3i$

5. $6\pi +\left(7+{\pi }^{2}\right)i$

6. $-2\pi +\pi i$

7. $\left(\sqrt{2}\pi -3\right)+\left(\pi +3\sqrt{2}\right)i$

8. $\left(-3-i\right)\left(i+3\right)=-8-6i$

1. $\frac{1-i}{1+1}=\frac{1}{2}-\frac{1}{2}i$

2. $\frac{3-i}{9+1}=\frac{3}{10}-\frac{1}{10}i$

3. $\frac{3+4i}{4+1}=\frac{3}{5}+\frac{4}{5}i$

4. $\frac{2+i}{4+1}=\frac{2}{5}+\frac{1}{5}i$

5. $1-i$

6. $\frac{1+i}{1+1}=\frac{1}{2}+\frac{1}{2}i$

7. $\frac{-2+6i}{9+1}=-\frac{1}{5}+\frac{3}{5}i$

8. $\frac{-1-i}{1+1}=-\frac{1}{2}-\frac{1}{2}i$

1. $\begin{array}{l}x+yi\\ \\ \frac{x+yi}{x-yi}=\frac{{x}^{2}-{y}^{2}+2xyi}{{x}^{2}+{y}^{2}}\\ \sqrt{\frac{{\left({x}^{2}-{y}^{2}\right)}^{2}+4{x}^{2}{y}^{2}}{{\left({x}^{2}+{y}^{2}\right)}^{2}}}\\ =\sqrt{\frac{{\left({x}^{2}+{y}^{2}\right)}^{2}}{{\left({x}^{2}+{y}^{2}\right)}^{2}}}\\ =1\\ \\ |\alpha |\end{array}$

コード(Emacs)

HTML5

<pre id="output0"></pre>
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>

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


JavaScript

let pre0 = document.querySelector('#output0'),
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.mul = mul;
that.conjugate = conjugate;
that.inv = inv;
that.mag = mag;
that.isEqual = isEqual;

return that;
};

let output = () => {
p('1.');
[[-1/10, -3/10, Complex(-1, 3).inv()],
[2, 0, Complex(1, 1).mul(Complex(1, -1))],
[-1, 3, Complex(1, 1).mul(Complex(0, 1)).mul(Complex(2, -1))],
[-1, 3, Complex(-1, 1).mul(Complex(2, -1))],
[6 * Math.PI, 7 + Math.pow(Math.PI, 2),
Complex(7, Math.PI).mul(Complex(Math.PI, 1))],
[-2 * Math.PI, Math.PI, Complex(1, 2).mul(Complex(0, Math.PI))],
[Math.sqrt(2) * Math.PI - 3, Math.PI + 3 * Math.sqrt(2),
Complex(Math.sqrt(2), 1).mul(Complex(Math.PI, 3))],
[-8, -6, Complex(1, 1).mul(Complex(-2, 1)).mul(Complex(3, 1))]
].forEach((x) => {
let z1 = Complex(x[0], x[1]),
z2 = x[2];

p(z1.isEqual(z2));
p(z1);
p(z2);
});
p('2.');
[[1/2, -1/2, Complex(1, 1).inv()],
[3/10, -1/10, Complex(3, 1).inv()],
[3/5, 4/5, Complex(2, 1).mul(Complex(2, -1).inv())],
[2/5, 1/5, Complex(2, -1).inv()],
[1, -1, Complex(1, 1).mul(Complex(0, 1).inv())],
[1/2, 1/2, Complex(0, 1).mul(Complex(1, 1).inv())],
[-1/5, 3/5, Complex(0, 2).mul(Complex(3, -1).inv())],
[-1/2, -1/2, Complex(-1, 1).inv()]
].forEach((x) => {
let z1 = Complex(x[0], x[1]),
z2 = x[2];

p(z1.isEqual(z2));
p(z1);
p(z2);
});
p('3.');
let z = Complex(2, 3);
p(z.mul(z.conjugate().inv()).mag());
p(z.mag());
p(z.conjugate().conjugate().mag());
};

btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();