0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

微分と積分(日記)

Posted at
# プログラム名: integral_autodiff_pytorch.py
# よく使う積分公式と自動微分(PyTorch)の統合スクリプト

import sympy as sp
import torch

# --- SymPyによる積分公式(代表例) ---
x = sp.Symbol('x')

# いくつかの積分例(代表的なもののみ)
integrals = {
    '∫ x dx': sp.integrate(x, x),
    '∫ x^n dx': sp.integrate(x**3, x),
    '∫ sin(x) dx': sp.integrate(sp.sin(x), x),
    '∫ cos(x) dx': sp.integrate(sp.cos(x), x),
    '∫ 1/x dx': sp.integrate(1/x, x),
    '∫ e^x dx': sp.integrate(sp.exp(x), x),
    '∫ ln(x) dx': sp.integrate(sp.log(x), x),
    '∫ 1/(1+x^2) dx': sp.integrate(1/(1 + x**2), x),
    '∫ 1/sqrt(1-x^2) dx': sp.integrate(1/sp.sqrt(1 - x**2), x),
}

# 結果を表示
print("--- SymPyによる代表的な積分 ---")
for name, expr in integrals.items():
    print(f"{name} = {expr}")

# --- PyTorchによる自動微分(例: f(x) = x^3 + sin(x)) ---
print("\n--- PyTorchによる自動微分 ---")

# 入力値
x_val = torch.tensor(2.0, requires_grad=True)

# 関数定義: f(x) = x^3 + sin(x)
f = x_val**3 + torch.sin(x_val)

# 微分(backward)
f.backward()

# 微分結果
print(f"x = {x_val.item()}")
print(f"f(x) = {f.item()}")
print(f"df/dx = {x_val.grad.item()}")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?