2018年1月9日火曜日

学習環境

解析入門〈3〉(松坂 和夫(著)、岩波書店)の第14章(多変数の関数)、14.1(微分可能性と勾配ベクトル)、問題2.を取り組んでみる。


  1. f x = i = 1 n x i 2 g r a d f x = ( - 1 2 - 1 2 1 2 2 x 1 i = 1 n x i 2 , , 1 2 2 x n i = 1 n x i 2 ) = x 1 x , , x n x = x x

コード(Emacs)

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, sqrt, Derivative

n = 5
xs = symbols([f'x{i}' for i in range(1, n + 1)])

f = sqrt(sum([xi ** 2 for xi in xs]))
gradf = [Derivative(f, xn, 1) for xn in xs]

for t in [gradf, [D.doit() for D in gradf]]:
    pprint(t)
    print()

入出力結果(Terminal, Jupyter(IPython))

$ ./sample2.py
⎡   ⎛   _____________________________⎞     ⎛   _____________________________⎞ 
⎢ ∂ ⎜  ╱   2     2     2     2     2 ⎟   ∂ ⎜  ╱   2     2     2     2     2 ⎟ 
⎢───⎝╲╱  x₁  + x₂  + x₃  + x₄  + x₅  ⎠, ───⎝╲╱  x₁  + x₂  + x₃  + x₄  + x₅  ⎠,
⎣∂x₁                                    ∂x₂                                   

    ⎛   _____________________________⎞     ⎛   _____________________________⎞ 
  ∂ ⎜  ╱   2     2     2     2     2 ⎟   ∂ ⎜  ╱   2     2     2     2     2 ⎟ 
 ───⎝╲╱  x₁  + x₂  + x₃  + x₄  + x₅  ⎠, ───⎝╲╱  x₁  + x₂  + x₃  + x₄  + x₅  ⎠,
 ∂x₃                                    ∂x₄                                   

    ⎛   _____________________________⎞⎤
  ∂ ⎜  ╱   2     2     2     2     2 ⎟⎥
 ───⎝╲╱  x₁  + x₂  + x₃  + x₄  + x₅  ⎠⎥
 ∂x₅                                  ⎦

⎡               x₁                                x₂                          
⎢────────────────────────────────, ────────────────────────────────, ─────────
⎢   _____________________________     _____________________________     ______
⎢  ╱   2     2     2     2     2     ╱   2     2     2     2     2     ╱   2  
⎣╲╱  x₁  + x₂  + x₃  + x₄  + x₅    ╲╱  x₁  + x₂  + x₃  + x₄  + x₅    ╲╱  x₁  +

      x₃                                x₄                                x₅  
───────────────────────, ────────────────────────────────, ───────────────────
_______________________     _____________________________     ________________
   2     2     2     2     ╱   2     2     2     2     2     ╱   2     2     2
 x₂  + x₃  + x₄  + x₅    ╲╱  x₁  + x₂  + x₃  + x₄  + x₅    ╲╱  x₁  + x₂  + x₃ 

             ⎤
─────────────⎥
_____________⎥
     2     2 ⎥
 + x₄  + x₅  ⎦

$

HTML5

<div id="graph0"></div>
<pre id="output0"></pre>
<label for="r0">r = </label>
<input id="r0" type="number" min="0" value="0.5">
<label for="dx">dx = </label>
<input id="dx" type="number" min="0" step="0.0001" value="0.005">
<br>
<label for="x1">x1 = </label>
<input id="x1" type="number" value="-5">
<label for="x2">x2 = </label>
<input id="x2" type="number" value="5">
<br>
<label for="y1">y1 = </label>
<input id="y1" type="number" value="-5">
<label for="y2">y2 = </label>
<input id="y2" type="number" value="5">
<br>

<label for="a0">a0 = </label>
<input id="a0" type="number" min="1" step="1" value="1">

<button id="draw0">draw</button>
<button id="clear0">clear</button>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.6/d3.min.js" integrity="sha256-5idA201uSwHAROtCops7codXJ0vja+6wbBrZdQ6ETQc=" crossorigin="anonymous"></script>

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

JavaScript

let div0 = document.querySelector('#graph0'),
    pre0 = document.querySelector('#output0'),
    width = 600,
    height = 600,
    padding = 50,
    btn0 = document.querySelector('#draw0'),
    btn1 = document.querySelector('#clear0'),
    input_r = document.querySelector('#r0'),
    input_dx = document.querySelector('#dx'),
    input_x1 = document.querySelector('#x1'),
    input_x2 = document.querySelector('#x2'),
    input_y1 = document.querySelector('#y1'),
    input_y2 = document.querySelector('#y2'),
    input_a0 = document.querySelector('#a0'),
    inputs = [input_r, input_dx, input_x1, input_x2, input_y1, input_y2,
              input_a0],
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let res = [];
        for (let i = start; i < end; i += step) {
            res.push(i);
        }
        return res;
    };

let f = (x) => Math.abs(x),
    g = (x) => x / Math.abs(x);

let draw = () => {
    pre0.textContent = '';


    let r = parseFloat(input_r.value),
        dx = parseFloat(input_dx.value),
        x1 = parseFloat(input_x1.value),
        x2 = parseFloat(input_x2.value),
        y1 = parseFloat(input_y1.value),
        y2 = parseFloat(input_y2.value),
        a0 = parseInt(input_a0.value, 10);
    
    if (r === 0 || dx === 0 || x1 > x2 || y1 > y2) {
        return;
    }

    let points = [],
        lines = [],
        fns = [[f, 'green'],
               [g, 'blue']];

    fns
        .forEach((o) => {
            let [fn, color] = o;
            
            for (let x = x1; x <= x2; x += dx) {
                let y = fn(x);
                
                if (Math.abs(y) < Infinity) {
                    points.push([x, y, color]);
                }
            }
        });
    
    let xscale = d3.scaleLinear()
        .domain([x1, x2])
        .range([padding, width - padding]);

    let yscale = d3.scaleLinear()
        .domain([y1, y2])
        .range([height - padding, padding]);

    let xaxis = d3.axisBottom().scale(xscale);
    let yaxis = d3.axisLeft().scale(yscale);
    div0.innerHTML = '';
    let svg = d3.select('#graph0')
        .append('svg')
        .attr('width', width)
        .attr('height', height);

    svg.selectAll('circle')
        .data(points)
        .enter()
        .append('circle')
        .attr('cx', (d) => xscale(d[0]))
        .attr('cy', (d) => yscale(d[1]))
        .attr('r', r)
        .attr('fill', (d) => d[2] || 'green');

    svg.selectAll('line')
        .data([[x1, 0, x2, 0], [0, y1, 0, y2]].concat(lines))
        .enter()
        .append('line')
        .attr('x1', (d) => xscale(d[0]))
        .attr('y1', (d) => yscale(d[1]))
        .attr('x2', (d) => xscale(d[2]))
        .attr('y2', (d) => yscale(d[3]))
        .attr('stroke', (d) => d[4] || 'black');
    
    svg.append('g')
        .attr('transform', `translate(0, ${height - padding})`)
        .call(xaxis);

    svg.append('g')
        .attr('transform', `translate(${padding}, 0)`)
        .call(yaxis);
    p(fns.join('\n'));
};

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








0 コメント:

コメントを投稿