All pastes #983712 Raw Edit

factorpart for sympy

public python v1 · immutable
·published 2008-04-13 15:53 UTC
rendered paste body
import sympydef factorpart(expr, part):	if not isinstance(expr, sympy.Add):		return expr	dummyk = sympy.Wild("k")	converted = []	notconverted = []	for q in expr.args:		#print q		r = q.match(part * dummyk)		if r is not None:			#print "\t", repr(r[dummyk])			converted.append(r[dummyk])		else:			notconverted.append(q)	# The together(),cancel() is to make things like	# "factorpart(x**2 - y**2, (x-y))" work.	return part * sympy.cancel(sympy.together(sympy.Add(*converted))) \		+ sympy.Add(*notconverted)def selftest():	from sympy.abc import x, y	from sympy import pi, sin	d = sympy.Symbol("delta")	e = d/pi + d*sin(x) / pi + 1	print factorpart(e, d)	e = factorpart(x**2 - y**2, (x-y))	print eif __name__ == '__main__':	selftest()