学習環境
- Surface 3 (4G LTE)、Surface 3 タイプ カバー、Surface ペン(端末)
- Windows 10 Pro (OS)
- 数式入力ソフト(TeX, MathML): MathType
- MathML対応ブラウザ: Firefox、Safari
- MathML非対応ブラウザ(Internet Explorer, Google Chrome...)用JavaScript Library: MathJax
ラング線形代数学(上)(S.ラング (著)、芹沢 正三 (翻訳)、ちくま学芸文庫)の1章(R^nにおけるベクトル)、6(複素数)、練習問題1、2、3.を取り組んでみる。
コード(Emacs)
HTML5
<pre id="output0"></pre> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample4.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.add = add;
that.mul = mul;
that.conjugate = conjugate;
that.inv = inv;
that.mag = mag;
that.isEqual = isEqual;
return that;
};
let innerProd = (A, B) => {
return A.reduce((prev, next, i) => prev.add(next.mul(B[i].conjugate())),
Complex(0, 0));
};
let output = () => {
let a = Complex(Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100)),
b = Complex(Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100)),
c = Complex(Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100));
p('4.');
let l = a.mul(b).conjugate(),
r = a.conjugate().mul(b.conjugate());
p(l.isEqual(r));
p(l);
p(r);
l = a.add(b).conjugate();
r = a.conjugate().add(b.conjugate());
p(l.isEqual(r));
p(l);
p(r);
p('5.');
l = a.mul(b).mag();
r = a.mag() * b.mag();
p(l === r);
p(l);
p(r);
p('6.');
let A = [],
B = [],
C = [],
O = [],
n = Math.floor(Math.random() * 100);
for (let k = 0; k < n; k += 1) {
A.push(Complex(Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100)));
B.push(Complex(Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100)));
C.push(Complex(Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100)));
O.push(Complex(0, 0));
}
p('6.');
p('SP1');
l = innerProd(A, B);
r = innerProd(B, A).conjugate();
p(l.isEqual(r));
p(l);
p(r);
p('SP2');
l = innerProd(A, B.map((z, i) => z.add(C[i])));
r = innerProd(A, B).add(innerProd(A, C));
p(l.isEqual(r));
p(l);
p(r);
p('SP3');
l = innerProd(A.map((z) => a.mul(z)), B);
r = a.mul(innerProd(A, B));
p(l.isEqual(r));
p(l);
p(r);
l = innerProd(A, B.map((z) => a.mul(z)));
r = a.conjugate().mul(innerProd(A, B));
p(l.isEqual(r));
p(l);
p(r);
p('SP4');
p(innerProd(O, O));
p(innerProd(A, A));
};
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';
output();
0 コメント:
コメントを投稿