## 2020年4月9日木曜日

### 数学 - Python - 解析学 - 多変数の関数 - 多変数の関数 - 微分可能性と勾配 - 2次元、内積、偏微分、座標

1. $\begin{array}{l}f\left(X\right)\\ =A·X\\ =\left(a,b\right)·\left(x,y\right)\\ =ax+by\end{array}$

よって求める x、 y の偏徴を A の座標で表すと、

$\begin{array}{l}\frac{\partial f}{\partial x}=a\\ \frac{\partial f}{\partial y}=b\end{array}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
import random
from sympy import symbols, Derivative, Matrix
from sympy.plotting import plot3d

print('2.')

a, b, x, y = symbols('a, b, x, y')
A = Matrix([a, b])
X = Matrix([x, y])
f = A.dot(X)

class TestPartialDerivative(TestCase):
def test_dx(self):
self.assertEqual(Derivative(f, x, 1).doit(), a)

def test_dy(self):
self.assertEqual(Derivative(f, y, 1).doit(), b)

p = plot3d(*[f.subs({a: random.randrange(-10, 11),
b: random.randrange(-10, 11)})
for _ in range(5)],
show=False)

p.show()
p.save('sample2.png')

if __name__ == "__main__":
main()


% ./sample2.py -v
2.
test_dx (__main__.TestPartialDerivative) ... ok
test_dy (__main__.TestPartialDerivative) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK
%