def outputExprToLatexFormat( expr ): if not isinstance( expr, pt.Sum ): raise pt.PTSymbolicException( "Expression passed to outputExprToLatexFormat() must be an instance of a fully-distributed Sum." ) s = "" for term in expr.terms: if not isinstance( term, pt.Product ): raise pt.PTSymbolicException( "Each term in the Sum passed to outputExprToLatexFormat() must be a single Product." ) line = "" AOrder = 0 for factor in term.terms: if isinstance( factor, pt.TermA ): AOrder += 1 elif isinstance( factor, pt.MatrixK ): line += " D" + str( factor.flavorLabel ) + "," + str( factor.indices[0] ) + "," + str( factor.indices[1] ) + "} " elif isinstance( factor, pt.FourierSum ): line += "{F," for index in factor.indices: line += str( index[0] ) + "," + str( index[1] ) + "," line = line[:-1] + "} " # Remove the trailing comma on the last index. elif isinstance( factor, pt.CoefficientFloat ): line += "{C," + str( factor.eval() ) + "} " s += "{A," + str( AOrder ) + "} " + line + '\n' return s
def _paralleleval_dual_getExpandedExpr(args): i = args[0] nTerms = args[1] product = args[2] sumOfProducts = args[3] print ">> [MT] Evaluating expansion for " + str(i) + " of " + str( nTerms) + ", running deep copy." expandedSum = pt.Sum([]) if isinstance(product, pt.Product): for term in sumOfProducts.terms: if not isinstance(term, pt.Product) or pt.getDualProductAOrder( product, term) <= 6: p = pt.Product([product, term]) p.reduceTree() p = p.getExpandedExpr() expandedSum.addTerm(p) else: for term in sumOfProducts.terms: p = pt.Product([product, term]) p.reduceTree() p = p.getExpandedExpr() expandedSum.addTerm(p) expandedSum = expandedSum.getExpandedExpr() return expandedSum
def _paralleleval_combineLikeTerms(args): termSetId = args[0] expr = args[1] print ">> [MT] Combining like terms for set " + str(termSetId) + "." A = pt.combineLikeTerms(expr) return A
def _paralleleval_distributeAllTraces(args): i = args[0] nTerms = args[1] term = args[2] print ">> [MT] Evaluating trace distribution and simplification for " + str( i) + " of " + str(nTerms) + "." return pt.distributeAllTraces(term)
def execParallelSumExpansion(expr): if not isinstance(expr, pt.Sum): raise pt.PTSymbolicException( "A Sum instance must be passed to parallel getExpandedExpr() (single)." ) procs = mp.Pool(processes=N_PROCS) parallelExprs = [] for i in range(0, len(expr.terms)): parallelExprs.append([i, len(expr.terms), expr.terms[i]]) distributedTerms = procs.map(_paralleleval_single_getExpandedExpr, parallelExprs) result = pt.Sum(distributedTerms) result.reduceTree() return result
def execParallelTraceDistribution(expr): if not isinstance(expr, pt.Sum): raise pt.PTSymbolicException( "A Sum instance must be passed to parallel distributeAllTraces().") print ">> [MT] Evaluating trace distribution and simplification for " + str( len(expr.terms)) + "." procs = mp.Pool(processes=N_PROCS) parallelExprs = [] for i in range(0, len(expr.terms)): parallelExprs.append([i, len(expr.terms), expr.terms[i]]) distributedTerms = procs.map(_paralleleval_distributeAllTraces, parallelExprs) result = pt.Sum(distributedTerms) result.reduceTree() return result
def execParallelCombineLikeTerms(expr, blockSize=BLOCK_SIZE): if not isinstance(expr, pt.Sum): raise pt.PTSymbolicException( "A Sum instance must be passed to parallel combineLikeTerms().") procs = mp.Pool(processes=N_PROCS) parallelExprs = [] oneMoreSet = 0 if not len(expr.terms) % blockSize == 0: oneMoreSet = 1 for i in range(0, int(len(expr.terms) / blockSize) + oneMoreSet): parallelExprs.append( [i, pt.Sum(expr.terms[i * blockSize:i * blockSize + blockSize])]) distributedTerms = procs.map(_paralleleval_combineLikeTerms, parallelExprs) result = pt.Sum(distributedTerms) result.reduceTree() result.simplify() if blockSize > len(expr.terms): return result else: print ">> Combining like terms with block size of " + str( blockSize * 2) + "." return execParallelCombineLikeTerms(result, blockSize * 2) # if blockSize > len( result.terms ) * 30: # return result #else: #if sameBlockSizeAttempts > 3: # print ">> Combining like terms with block size of " + str( blockSize * 2 ) + "." # return execParallelCombineLikeTerms( result, blockSize * 2 ) #else: # return execParallelCombineLikeTerms( result, len( result.terms ), sameBlockSizeAttempts + 1, blockSize )
def execParallelDualExpansion(expr1, expr2): print ">> [MT] Evaluating " + str( len(expr1.terms) * len(expr2.terms)) + " terms in parallel." procs = mp.Pool(processes=N_PROCS) counter = atomicCounter() parallelExprs = [] for i in range(0, len(expr1.terms)): parallelExprs.append( [i, len(expr1.terms), deepcopy(expr1.terms[i]), deepcopy(expr2)]) expandedTerms = procs.map(_paralleleval_dual_getExpandedExpr, parallelExprs) return pt.Sum(expandedTerms)
def tZ05(): Mup = pt.MatrixM( False, "up" ) Mdn = pt.MatrixM( False, "dn" ) A = pt.Product( [pt.CoefficientFloat( 1.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn )] ) B = pt.Product( [pt.CoefficientFloat( 2.0 ), pt.TermA(), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn )] ) C = pt.areTermsCommon(A, B) return str( C )
def tZ08(): Mup = pt.MatrixM( False, "up" ) Mdn = pt.MatrixM( False, "dn" ) Dup = pt.MatrixK( "up" ) Dup.fourierTransform() Ddn = pt.MatrixK( "dn" ) Ddn.fourierTransform() F1 = pt.FourierSum( [0,1,0,1], 4 ) F2 = pt.FourierSum( [0,0,1,1], 4 ) A = pt.Product( [pt.CoefficientFloat( 1.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Ddn, Dup, F1 ] ) B = pt.Product( [pt.CoefficientFloat( 2.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Dup, Ddn, F2 ] ) C = pt.areTermsCommon(A, B) return str( C )
import PTSymbolicObjects as pt import PTMultithreading as ptmt import sys import PTOutput as ptout Mup = pt.MatrixM(True, "") Mdn = pt.MatrixM(True, "") detMup = pt.DetM(Mup) detMdn = pt.DetM(Mdn) print "Calculating first derivative..." D1up = detMup.derivative() D1dn = detMdn.derivative() D1up.reduceTree() D1dn.reduceTree() D1up = D1up.getExpandedExpr() D1dn = D1dn.getExpandedExpr() D1up.reduceTree() D1dn.reduceTree() D1up = pt.distributeAllTraces(D1up) D1dn = pt.distributeAllTraces(D1dn) D1up.reduceTree() D1dn.reduceTree() D1up = D1up.getExpandedExpr() D1dn = D1dn.getExpandedExpr() D1up.reduceTree() D1dn.reduceTree() print "Calculating second derivative..." D2up = D1up.derivative()
def tB01(): A = pt.MatrixB() return str( A )
def pA01(): A = pt.Sum( [GenericTestTerm(0,0), GenericTestTerm(1,0), GenericTestTerm(2,0), GenericTestTerm(3,0)] ) B = pt.Sum( [GenericTestTerm(4,0), GenericTestTerm(5,0), GenericTestTerm(6,0), GenericTestTerm(7,0)] ) C = ptmt.execParallelDualExpansion( A, B ) return str( C )
def tA04(): A = pt.MatrixM( False, "up" ) return str( A )
def tA06(): A = pt.MatrixM( True, "up" ) B = A.derivative().derivative() return str( B )
def aX01(): A = {0: 0, 1: 0, 2: 0, 3: 2} B = pt._getTerminatedContraction( 3, A ) return str( B )
def tY04(): Mup = pt.MatrixM( False, "up" ) Mdn = pt.MatrixM( False, "dn" ) Dup = pt.MatrixK( "up" ) Dup.fourierTransform() Dup.indices = 0 Ddn = pt.MatrixK( "dn" ) Ddn.fourierTransform() Ddn.indices = 0 A1 = pt.Product( [pt.CoefficientFloat( 1.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Ddn, Dup ] ) A2 = pt.Product( [pt.CoefficientFloat( 9.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Dup, Dup ] ) A3 = pt.Product( [pt.CoefficientFloat( 2.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Dup, Ddn ] ) A4 = pt.Product( [pt.CoefficientFloat( 5.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Dup, Dup ] ) D = pt.Sum( [ A3, A2, A4, A1 ] ) E = pt.combineLikeTerms( D ) E.simplify() return str( E )
def tX10(): A = [ (1, 2), (0, 2) ] B = pt._constructContractionDict( A ) return str( B )
def aX02(): A = {0: 0, 4: 2, 2: 1, 3: 2} B = pt._terminateContractions( A ) return str( B )
def tX09(): A = [(1, 2), (3, 4),(2, 6), (5, 6), (0, 7), (0, 4) ] B = pt._constructContractionDict( A ) return str( B )
def tX07(): A = [(1,2),(3,4),(0,5),(0,2),(4,6)] B = pt._constructContractionDict( A ) return str( B )
def tX06(): A = [(0,1),(2,3),(4,5),(6,7),(0,2),(2,4),(4,6)] B = pt._constructContractionDict( A ) return str( B )
def tX02(): A = [(0,2),(2,3),(0,1)] B = pt._constructContractionDict( A ) return str( B )
def aX03(): A = {0: 0, 1: 0, 4: 2, 2: 3, 3: 1} B = pt._terminateContractions( A ) return str( B )
def tZ07(): Mup = pt.MatrixM( False, "up" ) Mdn = pt.MatrixM( False, "dn" ) Dup = pt.MatrixK( "up" ) Dup.fourierTransform() Ddn = pt.MatrixK( "dn" ) Ddn.fourierTransform() A = pt.Product( [pt.CoefficientFloat( 1.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Ddn, Dup ] ) B = pt.Product( [pt.CoefficientFloat( 2.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Dup, Ddn ] ) C = pt.areTermsCommon(A, B) return str( C )
def tA03(): A = pt.MatrixM( True, "up" ) return str( A )
def tA01(): A = pt.MatrixM() return str( A )
def tY01(): Mup = pt.MatrixM( False, "up" ) Mdn = pt.MatrixM( False, "dn" ) Dup = pt.MatrixK( "up" ) Dup.fourierTransform() Dup.indices = 0 Ddn = pt.MatrixK( "dn" ) Ddn.fourierTransform() Ddn.indices = 0 A = pt.Product( [pt.CoefficientFloat( 1.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Ddn, Dup ] ) B = pt.Product( [pt.CoefficientFloat( 2.0 ), pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn ), Dup, Ddn ] ) C = pt.Sum( [ A, B ] ) D = pt.combineLikeTerms( C ) D.simplify() return str( D )
def tA02(): A = pt.MatrixM( False ) return str( A )
def tZ02(): Mup = pt.MatrixM( False, "up" ) Mdn = pt.MatrixM( False, "dn" ) A = pt.Product( [pt.TermA(), pt.DetM( Mup ), pt.DetM( Mdn )] ) B = pt.areTermsCommon(A, A) return str( B )