Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Nothing direct, as far as I know. sympy.cse() gives some elements for building this, but using it needs some programming :

sage: var("beta, t")
(beta, t)
sage: foo=-36*beta*t*exp(-4*beta*t) +14*exp(-4*beta*t)-12*beta*t*exp(-6*beta*t) -18*exp(-6*beta*t)+5
sage: import sympy
sage: bar=sympy.cse(foo._sympy_())
sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which isn't far from what you seek.

Of note : if you have Mathematica (or the (gratis, but not free) Wolfram Engine),

sage: foo._mathematica_().FullSimplify().sage()
-2*(18*beta*t - 7)*e^(-4*beta*t) - 6*(2*beta*t + 3)*e^(-6*beta*t) + 5

does what you want (and some more, such as factoring coefficients).

HTH,

Nothing direct, as far as I know. sympy.cse() gives some elements for building this, but using it needs some programming :

sage: var("beta, t")
(beta, t)
sage: foo=-36*beta*t*exp(-4*beta*t) +14*exp(-4*beta*t)-12*beta*t*exp(-6*beta*t) -18*exp(-6*beta*t)+5
sage: import sympy
sage: bar=sympy.cse(foo._sympy_())
sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which isn't far from what you seek.

EDIT : Above,

sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()

can be replaced by

sage: bar[1][0].subs([u for u in reversed(bar[0])])._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which in turn can be replaced by the (slightly unreadable) one-liner:

sage: list(map(lambda u:u.subs(reversed(Tmp:=sympy.cse(foo._sympy_())[0])), Tmp[1]))
[-36*beta*t*exp(-4*beta*t) - 12*beta*t*exp(-6*beta*t) + 5 + 14*exp(-4*beta*t) - 18*exp(-6*beta*t)]

which rewrites a list of expressions (and returns a one-element list of a single expression, following sympy.cse convention).

Of note : : if you have Mathematica (or the (gratis, but not free) Wolfram Engine),

sage: foo._mathematica_().FullSimplify().sage()
-2*(18*beta*t - 7)*e^(-4*beta*t) - 6*(2*beta*t + 3)*e^(-6*beta*t) + 5

does what you want (and some more, such as factoring coefficients).

HTH,

Nothing direct, as far as I know. sympy.cse() gives some elements for building this, but using it needs some programming :

sage: var("beta, t")
(beta, t)
sage: foo=-36*beta*t*exp(-4*beta*t) +14*exp(-4*beta*t)-12*beta*t*exp(-6*beta*t) -18*exp(-6*beta*t)+5
sage: import sympy
sage: bar=sympy.cse(foo._sympy_())
sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which isn't far from what you seek.

EDIT : Above,

sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()

can be replaced by

sage: bar[1][0].subs([u for u in reversed(bar[0])])._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which in turn can be replaced by the (slightly unreadable) one-liner:

sage: list(map(lambda v:v._sage_(), map(lambda u:u.subs(reversed(Tmp:=sympy.cse(foo._sympy_())[0])), Tmp[1]))
[-36*beta*t*exp(-4*beta*t) - 12*beta*t*exp(-6*beta*t) + 5 + 14*exp(-4*beta*t) - 18*exp(-6*beta*t)]
Tmp[1])))
[-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5]

which rewrites a list of expressions (and returns a one-element list of a single expression, following sympy.cse convention).

Of note : if you have Mathematica (or the (gratis, but not free) Wolfram Engine),

sage: foo._mathematica_().FullSimplify().sage()
-2*(18*beta*t - 7)*e^(-4*beta*t) - 6*(2*beta*t + 3)*e^(-6*beta*t) + 5

does what you want (and some more, such as factoring coefficients).

HTH,

Nothing direct, as far as I know. sympy.cse() gives some elements for building this, but using it needs some programming :

sage: var("beta, t")
(beta, t)
sage: foo=-36*beta*t*exp(-4*beta*t) +14*exp(-4*beta*t)-12*beta*t*exp(-6*beta*t) -18*exp(-6*beta*t)+5
sage: import sympy
sage: bar=sympy.cse(foo._sympy_())
sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which isn't far from what you seek.

EDIT : Above,

sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()

can be replaced by

sage: bar[1][0].subs([u for u in reversed(bar[0])])._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which in turn can be replaced by the (slightly unreadable) one-liner:

sage: list(map(lambda v:v._sage_(), map(lambda u:u.subs(reversed(Tmp:=sympy.cse(foo._sympy_())[0])), u:u.subs(Tmp2:=reversed(Tmp:=sympy.cse(foo._sympy_())[0])), Tmp[1])))
[-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5]

which rewrites a list of expressions (and returns a one-element list of a single expression, following sympy.cse convention).

Of note : if you have Mathematica (or the (gratis, but not free) Wolfram Engine),

sage: foo._mathematica_().FullSimplify().sage()
-2*(18*beta*t - 7)*e^(-4*beta*t) - 6*(2*beta*t + 3)*e^(-6*beta*t) + 5

does what you want (and some more, such as factoring coefficients).

HTH,

Nothing direct, as far as I know. sympy.cse() gives some elements for building this, but using it needs some programming :

sage: var("beta, t")
(beta, t)
sage: foo=-36*beta*t*exp(-4*beta*t) +14*exp(-4*beta*t)-12*beta*t*exp(-6*beta*t) -18*exp(-6*beta*t)+5
sage: import sympy
sage: bar=sympy.cse(foo._sympy_())
sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which isn't far from what you seek.

EDIT : Above,

sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()

can be replaced by

sage: bar[1][0].subs([u for u in reversed(bar[0])])._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

Reading sympy.cse's documentation leads to :

sage: sympy.cse(foo._sympy_(), postprocess=lambda r, e:list(map(lambda u:u.subs(reversed(r))._sage_(), e)))
[-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5]

which in turn can be replaced by the (slightly unreadable) one-liner:

sage: list(map(lambda v:v._sage_(), map(lambda u:u.subs(Tmp2:=reversed(Tmp:=sympy.cse(foo._sympy_())[0])), Tmp[1])))
[-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5]

which rewrites a list of expressions (and returns a one-element list of a single expression, following sympy.cse convention).is less obvious but clearer.

Of note : if you have Mathematica (or the (gratis, but not free) Wolfram Engine),

sage: foo._mathematica_().FullSimplify().sage()
-2*(18*beta*t - 7)*e^(-4*beta*t) - 6*(2*beta*t + 3)*e^(-6*beta*t) + 5

does what you want (and some more, such as factoring coefficients).

HTH,

EDIT : A previous answer (seriously out of scope...) has been edited out.

Nothing direct, as far as I know. sympy.cse() gives some elements for building this, but using it needs some programming :

sage: var("beta, t")
(beta, t)
sage: foo=-36*beta*t*exp(-4*beta*t) +14*exp(-4*beta*t)-12*beta*t*exp(-6*beta*t) -18*exp(-6*beta*t)+5
sage: import sympy
sage: bar=sympy.cse(foo._sympy_())
sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

which isn't far from what you seek.

EDIT : Above,

sage: old=bar[1][0]
sage: while (new:=old.subs(bar[0]))!=old: old, new=(new, new.subs(bar[0]))
sage: new._sage_()

can be replaced by

sage: bar[1][0].subs([u for u in reversed(bar[0])])._sage_()
-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5

Reading sympy.cse's documentation leads to :

sage: sympy.cse(foo._sympy_(), postprocess=lambda r, e:list(map(lambda u:u.subs(reversed(r))._sage_(), e)))
[-36*beta*t*e^(-4*beta*t) - 12*beta*t*e^(-6*beta*t) + 14*e^(-4*beta*t) - 18*e^(-6*beta*t) + 5]

which is less obvious but clearer.

Of note : if you have Mathematica (or the (gratis, but not free) Wolfram Engine),

sage: foo._mathematica_().FullSimplify().sage()
-2*(18*beta*t - 7)*e^(-4*beta*t) - 6*(2*beta*t + 3)*e^(-6*beta*t) + 5

does what you want (and some more, such as factoring coefficients).

HTH,