Beispiel #1
0
def printGreenPen(penName, funcs, file=sys.stdout, docstring=None):

	if docstring is not None:
		print('"""%s"""' % docstring)

	print(
'''from fontTools.pens.basePen import BasePen, OpenContourError
try:
	import cython
except ImportError:
	# if cython not installed, use mock module with no-op decorators and types
	from fontTools.misc import cython

if cython.compiled:
	# Yep, I'm compiled.
	COMPILED = True
else:
	# Just a lowly interpreted script.
	COMPILED = False


__all__ = ["%s"]

class %s(BasePen):

	def __init__(self, glyphset=None):
		BasePen.__init__(self, glyphset)
'''% (penName, penName), file=file)
	for name,f in funcs:
		print('		self.%s = 0' % name, file=file)
	print('''
	def _moveTo(self, p0):
		self.__startPoint = p0

	def _closePath(self):
		p0 = self._getCurrentPoint()
		if p0 != self.__startPoint:
			self._lineTo(self.__startPoint)

	def _endPath(self):
		p0 = self._getCurrentPoint()
		if p0 != self.__startPoint:
			# Green theorem is not defined on open contours.
			raise OpenContourError(
							"Green theorem is not defined on open contours."
			)
''', end='', file=file)

	for n in (1, 2, 3):


		subs = {P[i][j]: [X, Y][j][i] for i in range(n+1) for j in range(2)}
		greens = [green(f, BezierCurve[n]) for name,f in funcs]
		greens = [sp.gcd_terms(f.collect(sum(P,()))) for f in greens] # Optimize
		greens = [f.subs(subs) for f in greens] # Convert to p to x/y
		defs, exprs = sp.cse(greens,
				     optimizations='basic',
				     symbols=(sp.Symbol('r%d'%i) for i in count()))

		print()
		for name,value in defs:
			print('	@cython.locals(%s=cython.double)' % name, file=file)
		if n == 1:
			print('''\
	@cython.locals(x0=cython.double, y0=cython.double)
	@cython.locals(x1=cython.double, y1=cython.double)
	def _lineTo(self, p1):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
''', file=file)
		elif n == 2:
			print('''\
	@cython.locals(x0=cython.double, y0=cython.double)
	@cython.locals(x1=cython.double, y1=cython.double)
	@cython.locals(x2=cython.double, y2=cython.double)
	def _qCurveToOne(self, p1, p2):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
		x2,y2 = p2
''', file=file)
		elif n == 3:
			print('''\
	@cython.locals(x0=cython.double, y0=cython.double)
	@cython.locals(x1=cython.double, y1=cython.double)
	@cython.locals(x2=cython.double, y2=cython.double)
	@cython.locals(x3=cython.double, y3=cython.double)
	def _curveToOne(self, p1, p2, p3):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
		x2,y2 = p2
		x3,y3 = p3
''', file=file)
		for name,value in defs:
			print('		%s = %s' % (name, value), file=file)

		print(file=file)
		for name,value in zip([f[0] for f in funcs], exprs):
			print('		self.%s += %s' % (name, value), file=file)

	print('''
if __name__ == '__main__':
	from fontTools.misc.symfont import x, y, printGreenPen
	printGreenPen('%s', ['''%penName, file=file)
	for name,f in funcs:
		print("		      ('%s', %s)," % (name, str(f)), file=file)
	print('		     ])', file=file)
Beispiel #2
0
	def __missing__(self, i):
		args = ['p%d'%d for d in range(i+1)]
		f = green(self._symfunc, BezierCurve[i])
		f = sp.gcd_terms(f.collect(sum(P,()))) # Optimize
		return sp.lambdify(args, f)
Beispiel #3
0
def printGreenPen(penName, funcs, file=sys.stdout):

	print(
'''from fontemon_blender_addon.fontTools.misc.py23 import *
from fontemon_blender_addon.fontTools.pens.basePen import BasePen

class %s(BasePen):

	def __init__(self, glyphset=None):
		BasePen.__init__(self, glyphset)
'''%penName, file=file)
	for name,f in funcs:
		print('		self.%s = 0' % name, file=file)
	print('''
	def _moveTo(self, p0):
		self.__startPoint = p0

	def _closePath(self):
		p0 = self._getCurrentPoint()
		if p0 != self.__startPoint:
			self._lineTo(self.__startPoint)

	def _endPath(self):
		p0 = self._getCurrentPoint()
		if p0 != self.__startPoint:
			# Green theorem is not defined on open contours.
			raise NotImplementedError
''', end='', file=file)

	for n in (1, 2, 3):

		if n == 1:
			print('''
	def _lineTo(self, p1):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
''', file=file)
		elif n == 2:
			print('''
	def _qCurveToOne(self, p1, p2):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
		x2,y2 = p2
''', file=file)
		elif n == 3:
			print('''
	def _curveToOne(self, p1, p2, p3):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
		x2,y2 = p2
		x3,y3 = p3
''', file=file)
		subs = {P[i][j]: [X, Y][j][i] for i in range(n+1) for j in range(2)}
		greens = [green(f, BezierCurve[n]) for name,f in funcs]
		greens = [sp.gcd_terms(f.collect(sum(P,()))) for f in greens] # Optimize
		greens = [f.subs(subs) for f in greens] # Convert to p to x/y
		defs, exprs = sp.cse(greens,
				     optimizations='basic',
				     symbols=(sp.Symbol('r%d'%i) for i in count()))
		for name,value in defs:
			print('		%s = %s' % (name, value), file=file)
		print(file=file)
		for name,value in zip([f[0] for f in funcs], exprs):
			print('		self.%s += %s' % (name, value), file=file)

	print('''
if __name__ == '__main__':
	from fontemon_blender_addon.fontTools.misc.symfont import x, y, printGreenPen
	printGreenPen('%s', ['''%penName, file=file)
	for name,f in funcs:
		print("		      ('%s', %s)," % (name, str(f)), file=file)
	print('		     ])', file=file)
Beispiel #4
0
def test_simplify_matrix_expressions():
    # Various simplification functions
    assert type(gcd_terms(C*D + D*C)) == MatAdd
    a = gcd_terms(2*C*D + 4*D*C)
    assert type(a) == MatMul
    assert a.args == (2, (C*D + 2*D*C))
Beispiel #5
0
def test_simplify_matrix_expressions():
    # Various simplification functions
    assert type(gcd_terms(C*D + D*C)) == MatAdd
    a = gcd_terms(2*C*D + 4*D*C)
    assert type(a) == MatMul
    assert a.args == (2, (C*D + 2*D*C))
    print(Sum(1,(i,0,n-1)).doit())
    T = Sum((-1)**(n-j) * C(j,k), (j,0,n-1))
    T2 = T.subs(n, 2*n)
    T3 = T.subs(n,2*n+1)
    print(T2)
    print(T3)
    for i in range(0,5):
        D = -2*factorial(i)
        a2 = T2.subs(k,i).doit().simplify() #.expand(force=True)
        #print(factor(a))
        b2 = a2.subs(n, n/2)
        print('[even n]: {} = ({})/{}'.format(factor(b2), (b2*D).expand(), D))

        a3 = T3.subs(k,i).doit().simplify()
        b3 = a3.subs(n, (n-1)/2)
        print('[odd n]: {} = {}'.format(factor(b3), gcd_terms(b3.expand())))


    raise
    f = T - (1-(-1)**n)/(-2)**(k+1)
    f *= -2*factorial(k) / Product(n-2*i, (i, 0, floor(k/2)))
    print(f)
    for i in range(0,8):
        a = f.subs(k,i).doit().simplify() #.expand(force=True)
        print(factor(a))

    '''
-2*(-(-2)**(-k - 1)*(-(-1)**n + 1) + Sum((-1)**(-j + n)*binomial(j, k), (j, 0, n - 1)))*k!/Product(-2*i + n, (i, 0, floor(k/2)))
0
1
1
Beispiel #7
0
def Stirling_subset_polynomial(n, z):
    'Stirling_polynomial(n, z) = Stirling{z, z-n}/fall(z, n+1), for [int n>0]'

    assert n > 0
    r = Stirling_subset_tail(n, z) / ff(z, n + 1)
    return gcd_terms(factor(r.simplify()))
Beispiel #8
0
def Stirling_polynomial(n, z):
    'Stirling_polynomial(n, z) = Stirling[z, z-n]/fall(z, n+1), for [int n>0]'

    assert n > 0
    r = Stirling_circle_tail(n, z) / ff(z, n + 1)
    return gcd_terms(factor(r.simplify()))
Beispiel #9
0
	def __missing__(self, i):
		args = ['p%d'%d for d in range(i+1)]
		f = green(self._symfunc, BezierCurve[i])
		f = sp.gcd_terms(f.collect(sum(P,()))) # Optimize
		return sp.lambdify(args, f)
Beispiel #10
0
def printGreenPen(penName, funcs, file=sys.stdout):

	print(
'''from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
from fontTools.pens.basePen import BasePen

class %s(BasePen):

	def __init__(self, glyphset=None):
		BasePen.__init__(self, glyphset)
'''%penName, file=file)
	for name,f in funcs:
		print('		self.%s = 0' % name, file=file)
	print('''
	def _moveTo(self, p0):
		self.__startPoint = p0

	def _closePath(self):
		p0 = self._getCurrentPoint()
		if p0 != self.__startPoint:
			self._lineTo(self.__startPoint)

	def _endPath(self):
		p0 = self._getCurrentPoint()
		if p0 != self.__startPoint:
			# Green theorem is not defined on open contours.
			raise NotImplementedError
''', end='', file=file)

	for n in (1, 2, 3):

		if n == 1:
			print('''
	def _lineTo(self, p1):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
''', file=file)
		elif n == 2:
			print('''
	def _qCurveToOne(self, p1, p2):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
		x2,y2 = p2
''', file=file)
		elif n == 3:
			print('''
	def _curveToOne(self, p1, p2, p3):
		x0,y0 = self._getCurrentPoint()
		x1,y1 = p1
		x2,y2 = p2
		x3,y3 = p3
''', file=file)
		subs = {P[i][j]: [X, Y][j][i] for i in range(n+1) for j in range(2)}
		greens = [green(f, BezierCurve[n]) for name,f in funcs]
		greens = [sp.gcd_terms(f.collect(sum(P,()))) for f in greens] # Optimize
		greens = [f.subs(subs) for f in greens] # Convert to p to x/y
		defs, exprs = sp.cse(greens,
				     optimizations='basic',
				     symbols=(sp.Symbol('r%d'%i) for i in count()))
		for name,value in defs:
			print('		%s = %s' % (name, value), file=file)
		print(file=file)
		for name,value in zip([f[0] for f in funcs], exprs):
			print('		self.%s += %s' % (name, value), file=file)

	print('''
if __name__ == '__main__':
	from fontTools.misc.symfont import x, y, printGreenPen
	printGreenPen('%s', ['''%penName, file=file)
	for name,f in funcs:
		print("		      ('%s', %s)," % (name, str(f)), file=file)
	print('		     ])', file=file)
Beispiel #11
0
def int_cs_gh(d, s, r):
    g, h = find_gh(d, s, r, z)
    g = gcd_terms(g)
    h = gcd_terms(h)

    return g, h