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?

Function to Expand Numerator with Single Denominator in SymPy「SymPyで分子のみを展開し通分形式で出力する関数」をAI先生に教えてもらいました。

0
Last updated at Posted at 2026-09-26

・しょうがないナーです。戻り値は文字列でした。

Gemini先生へ

sympy で sqrt(6)*(1 + sqrt(3))/8 を (sqrt(6)+3*sqrt(2))/8 にしてほしい。

(回答省略)

# SymPyで分子のみを展開し、(分子)/分母 の形(文字列)にまとめるユーザー定義関数
from sympy import sqrt, expand

def expand_numerator_str(expr):
    """
    分数の分子を展開し、(sqrt(6) + 3*sqrt(2))/8 の形式で返す関数
    """
    # 分子と分母を分離
    numer, denom = expr.as_numer_denom()
    
    # 分子だけを展開
    expanded_numer = expand(numer)
    
    # 割り算の表示形式 (分子)/分母 に整えて返す
    return f"({expanded_numer})/{denom}"

# 式の定義
expr = sqrt(6) * (1 + sqrt(3)) / 8

# 関数の実行
result = expand_numerator_str(expr)

print(result)

実行結果

(sqrt(6) + 3*sqrt(2))/8

`

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?