如何将 (a+b+c)**2 重写为 a**2 + b**2 + c*2 + 2*(a*b + b*c + c*a) & ^2-->* *2

how to rewrite (a+b+c)**2 as a**2 + b**2 + c*2 + 2*(a*b + b*c + c*a) & ^2-->**2

sympy 转换:(a+b+c)^2 --> a^2 +b^2+c^2+2*(ab + bc + c*a) : 我想要

抱歉添加

同情转换:我想要

(a+b+c)**2 --> a**2 +b**2+c**2+2*(a*b + b*c + c*a)

我试试

from sympy import *
var('a b c')
f=(a+b+c)**2
print("#f",f)
print("#e",expand(f))
#f (a + b + c)**2
#e a**2 + 2*a*b + 2*a*c + b**2 + 2*b*c + c**2

(2022-02-02) 我尝试使用因子 atom

from sympy import *
var('a b c x')
f_str="a+b+c"
g_str="a**2+b**2+c**2"
f    =factor(simplify(f_str))
g    =factor(simplify(g_str))
mySubs={f:1,g:13}
#
h =factor(f**2-g)
ha=list(h.atoms(Number))[0]
hb=h/ha
Le=(f.subs(mySubs))**2
Ri1=g.subs(mySubs)
Ri2=h.subs({hb:x})
print("#","(",f_str,")**2=",g_str,"+",ha,"*(",hb,")")
print("#",f_str,"=",mySubs[f],",",g_str,"=",mySubs[g])
print("#",Le,"**2=",Ri1,"+",ha,"*(",solve(Eq(Le,Ri1+Ri2))[0],")")
# ( a+b+c )**2= a**2+b**2+c**2 + 2 *( a*b + a*c + b*c )
# a+b+c = 1 , a**2+b**2+c**2 = 13
# 1 **2= 13 + 2 *( -6 )

(2022-02-04)‖价值潜艇

from sympy import *
var('a b c')
f_str="a+b+c         "
g_str="a**2+b**2+c**2"
h_str="a*b+a*c+b*c   "
mySubs={a:0,b:-2,c:3}
f=factor(simplify(f_str)).subs(mySubs)
g=factor(simplify(g_str)).subs(mySubs)
h=factor(simplify(h_str)).subs(mySubs)
print("#",mySubs)
print("#",f_str,"=",f)
print("#",g_str,"=",g)
print("#",h_str,"=",h)
print("#",f_str,"**2=",g_str,"+2*(",h_str,")")
print("#",f,"**2=",g,"+2*(",h,")")
# {a: 0, b: -2, c: 3}
# a+b+c          = 1
# a**2+b**2+c**2 = 13
# a*b+a*c+b*c    = -6
# a+b+c          **2= a**2+b**2+c**2 +2*( a*b+a*c+b*c    )
# 1 **2= 13 +2*( -6 )

(2022-02-05) 价值订阅

from sympy import *
var('a b c')
f=a+b+c
g=a**2+b**2+c**2
h=a*b+a*c+b*c
mySubs={a:0,b:-2,c:3}
#
f_val=factor(f).subs(mySubs)
g_val=factor(g).subs(mySubs)
h_val=factor(h).subs(mySubs)
print("#",mySubs)
print("#",f,"=",f_val)
print("#",g,"=",g_val)
print("#",h,"=",h_val)
print("#",f,"**2=",g_val,"+2*(",h,")")
print("#",f_val,"**2=",g_val,"+2*(",h_val,")")
# {a: 0, b: -2, c: 3}
# a + b + c = 1
# a**2 + b**2 + c**2 = 13
# a*b + a*c + b*c = -6
# a + b + c **2= 13 +2*( a*b + a*c + b*c )
# 1 **2= 13 +2*( -6 )

(参考)

2022 Mathematics IA Q1 < 大学共同考试是日本大学的共同入学考试

只有日语

https://cdn.mainichi.jp/item/jp/etc/kyotsu-2022/pdf/MT1P.pdf#page=1

>>> from sympy.parsing.sympy_parser import *
>>> f = parse_expr('(a+b+c)^2', transformations=(auto_symbol, convert_xor))
>>> f
(a + b + c)**2
>>> from sympy import Pow
>>> nopow, pow = f.expand().as_independent(Pow)
>>> pow + nopow.factor()
a**2 + b**2 + c**2 + 2*(a*b + a*c + b*c)