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?

pythonのユーザー定義関数「sympyで pprint の連結を教えて下さい。」AI先生へ。

Last updated at Posted at 2025-07-14

ChatGPT先生へ

・あと一歩です。print文で対応。

✅ 出力例(想定)

C           = k⋅⎡  a²   b   z
                        *  
                c   d²    w
                x    y 
sympy で pprint の連結を教えて下さい。

from sympy import *
var('a,b,c,d')
var('x,y')
A=Matrix([[a, b], [c, d]])
B=Matrix([[x], [y]])
pprint(A)
pprint(B)
# pprint(A,"*",B)
# TypeError: pretty_print() takes 1 positional argument but 3 were given

⎡a  b⎤   ⎡x⎤
⎢    ⎥ * ⎢ ⎥
⎣c  d⎦   ⎣y⎦
1. sympy で pprint の連結を教えて下さい。
2. お願いします。
3. True がでました。
4. 上記の 📌 出力イメージ(整形出力): にするソースコードを教えて下さい。
5. True がでます。
6. ✅ 出力結果(整形表示)にしてほしい。
7. Pythonバージョン の確認方法を教えて下さい。
8. Python 3.11.2 です。
9. 表示できません。
10. ⎡a⋅x + b⋅y⎤   ⎡a⋅x + b⋅y⎤ ⎢         ⎥ = ⎢         ⎥ ⎣c⋅x + d⋅y⎦   ⎣c⋅x + d⋅y⎦
11. A*B の中身でお願いします。
12. 出力イメージを 横に並べて下さい。
13. ⎡a  b⎤    ⎡x⎤   ⎡a⋅x + b⋅y⎤ ⎢    ⎥ * ⎢ ⎥ = ⎢         ⎥ ⎣c  d⎦    ⎣y⎦   ⎣c⋅x + d⋅y⎦ 上記のズレをなくして下さい。
14. ユーザー定義関数でお願いします。
15. 左辺だけのユーザー定義関数も教えて下さい。
16. 上記の pretty_matrix_multiplication の左辺と右辺を 逆にして下さい。
17. 上記で、pretty_matrix_multiplication_reversed(C, A, B): を pretty_matrix_multiplication_reversed(C, k, A, B): にして。k は共通因子です。
18. C は、入力のままで。
19. 要素が 2 乗の対応をお願いします。行数が多い要素の場合に対応して下さい。
20. 小文字の 2 も表示できますか。
21. 本チャット、この会話で出てきた全ての質問を、いつもの Markdown のテキスト形式で一覧にしてください。追番も。
22. 不足しています。
23. sympy で pprint の連結を教えて下さい。から

(最後だけ)

✅ 完全対応版コード

from sympy import *
init_printing(use_unicode=True)

def pretty_matrix_multiplication_reversed(C_label, k, A, B):
    """
    C_label = k * A * B を整形して横に表示する関数。
    - C_label: Symbol または任意の式(例: Symbol('C'))
    - k: スカラー係数(例: k)
    - A, B: sympy.Matrix(任意行数、任意内容)
    """

    def pretty_lines(expr):
        """sympy expr を整形表示の1行ごとのリストに変換"""
        return pretty(expr, use_unicode=True).splitlines()

    # 行列を整形文字列に分解
    C_lines = pretty_lines(C_label)
    A_lines = pretty_lines(A)
    B_lines = pretty_lines(B)

    # 各列の最大幅を取得
    width_C = max(len(s) for s in C_lines)
    width_A = max(len(s) for s in A_lines)
    width_B = max(len(s) for s in B_lines)
    width_k = len(str(k)) + 1  # 例:k⋅ の幅

    # 中央演算子
    mul_lines = ['*'] * max(len(A_lines), len(B_lines))
    eq_lines = ['='] * max(len(C_lines), len(A_lines), len(B_lines))

    # 全体の行数を最大に合わせる
    total_lines = max(len(C_lines), len(A_lines), len(B_lines))

    for i in range(total_lines):
        # 各行が存在しない場合は空白で埋める
        C_row = C_lines[i] if i < len(C_lines) else ' ' * width_C
        A_row = A_lines[i] if i < len(A_lines) else ' ' * width_A
        B_row = B_lines[i] if i < len(B_lines) else ' ' * width_B

        # 中央の行にだけ k を表示(他は空白)
        k_str = str(k) + '' if i == total_lines // 2 else ' ' * width_k

        # 中央演算子の処理
        eq = '=' if i == total_lines // 2 else ' '
        mul = '*' if i == total_lines // 2 else ' '

        # 整形(左寄せ+幅合わせ)
        C_row = C_row.ljust(width_C)
        A_row = A_row.ljust(width_A)
        B_row = B_row.ljust(width_B)
        eq = eq.center(3)
        mul = mul.center(3)

        # 出力行を構築
        print(f"{C_row}{eq}{k_str}{A_row}{mul}{B_row}")

✅ 使用例:要素に2乗を含み、行数も多い場合

a, b, c, d, x, y, z, w, k = symbols('a b c d x y z w k')

A = Matrix([
    [a**2, b],
    [c,    d**2],
    [x,    y]
])
B = Matrix([
    [z],
    [w]
])
C = Symbol('C')  # 左辺は展開せず表示

pretty_matrix_multiplication_reversed(C, k, A, B)

Gemini先生へ

いつもと違うおすすめです。

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?