2016年9月18日日曜日

学習環境/開発環境

線型代数入門 (松坂 和夫(著)、岩波書店)の第4章(複素数、複素ベクトル空間)、5(複素数と平面幾何学)、問2.を取り組んでみる。

問2.

αβ 2α = αλ αγ λ=α α 2 γααβ+βγ 2α λ= 1 2 ( α+β+γ βγ 2α )

JavaScript(number.js) で確認してみる。

HTML5

<div id="graph0"></div>

<label for="theta00">
  <span style="color:red">α</span> = cos
</label>
<input id="theta00" type="number" min="-3.150" max="3.150" step="0.001" 
       value="-1.0000"> +
<i>i</i>sin<span id="theta01"></span>
<br>
<label for="theta10">
  <span style="color:green">β</span> = cos
</label>
<input id="theta10" type="number" min="-3.150" max="3.150" step="0.001"
       value="-2.500"> +
<i>i</i>sin<span id="theta11"></span>
<br>
<label for="theta20">
  <span style="color:blue">γ</span> = cos
</label>
<input id="theta20" type="number" min="-3.150" max="3.150" step="0.001"
       value="1.000"> +
<i>i</i>sin<span id="theta21"></span>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script src="number.js"></script>
<script src="sample2.js"></script>

JavaScript

コード(Emacs)

(function () {
    'use strict';
    var width = 600,
        height = 600,
        padding = 50,
        min = -1.5,
        max = 1.5,
        xscale,
        yscale,
        xaxis,
        yaxis,
        svg,
        div_graph = document.querySelector('#graph0'),
        input_theta0 = document.querySelector('#theta00'),
        input_theta1 = document.querySelector('#theta10'),
        input_theta2 = document.querySelector('#theta20'),
        span_theta0 = document.querySelector('#theta01'),
        span_theta1 = document.querySelector('#theta11'),
        span_theta2 = document.querySelector('#theta21'),
        inputs = [input_theta0, input_theta1, input_theta2],
        calc,
        output;

    xscale = d3.scaleLinear()
        .domain([min, max])
        .range([padding, width - padding]);
    yscale = d3.scaleLinear()
        .domain([min, max])
        .range([height - padding, padding]);
    xaxis = d3.axisBottom().scale(xscale);
    yaxis = d3.axisLeft().scale(yscale);

    calc = function (alpha, beta, gamma) {
        return (1/2).mul(alpha.add(beta)
                         .add(gamma)
                         .sub(beta.mul(gamma).div(alpha)));
    };
    output = function (zs) {
        var theta0 = parseFloat(input_theta0.value),
            theta1 = parseFloat(input_theta1.value),
            theta2 = parseFloat(input_theta2.value),
            alpha = Complex.fromMagnitudeAndArgment(1, theta0),
            beta = Complex.fromMagnitudeAndArgment(1, theta1),
            gamma = Complex.fromMagnitudeAndArgment(1, theta2),
            lambda = calc(alpha, beta, gamma),
            zs = [alpha, beta, gamma, lambda],        
            colors = ['red', 'green', 'blue', 'black'],
            points;

        span_theta0.innerText = theta0;
        span_theta1.innerText = theta1;
        span_theta2.innerText = theta2;
        points = zs.map(function (z, i) {
            return [xscale(z.getReal()), yscale(z.getImag()), colors[i]];
        });
        
        div_graph.innerHTML = '';
        svg = d3.select('#graph0')
            .append('svg')
            .attr('width', width)
            .attr('height', height);
        svg.selectAll('circle')
            .data(points)
            .enter()
            .append('circle')
            .attr('cx', function (d) {
                return d[0];
            })
            .attr('cy', function (d) {
                return d[1];
            })
            .attr('r', 5)
            .attr('fill', function (d) {
                return d[2];
            });

        points.pop();
        points.push(points[0]);
        svg.selectAll('line')
            .data(points.slice(0, -1))
            .enter()
            .append('line')
            .attr('x1', function (d) {
                return d[0];
            })
            .attr('y1', function (d) {
                return d[1];
            })
            .attr('x2', function (d, i) {
                return points[i + 1][0];
            })
            .attr('y2', function (d, i) {
                return points[i + 1][1];
            })
            .attr('stroke', 'black');

        svg.append('line')
            .attr('x1', xscale(alpha.getReal()))
            .attr('y1', yscale(alpha.getImag()))
            .attr('x2', xscale(lambda.getReal()))
            .attr('y2', yscale(lambda.getImag()))
            .attr('stroke', 'black');
        
        svg.append('circle')
            .attr('cx', xscale(0))
            .attr('cy', yscale(0))
            .attr('r', xscale(1) - xscale(0))
            .attr('stroke', 'black')
            .attr('fill', 'rgba(0, 0, 0, 0)');
        svg.append('g')
            .attr('transform', 'translate(0, ' + (height / 2) + ')')
            .call(xaxis);
        svg.append('g')
            .attr('transform', 'translate(' + (width / 2) + ', 0)')
            .call(yaxis);
    };

    inputs.forEach(function (input) {
        input.onchange = output;
    });
    output();
}());
+ isin
+ isin
+ isin

0 コメント:

コメントを投稿