~madteam/mg5amcnlo/series2.0

« back to all changes in this revision

Viewing changes to aloha/aloha_lib.py

  • Committer: olivier-mattelaer
  • Date: 2020-08-21 09:16:56 UTC
  • mfrom: (284.2.24 python3)
  • Revision ID: olivier-mattelaer-20200821091656-iizux2mj94tkssuo
pass to 2.8.0 (and move to python3 branch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
 
45
45
from __future__ import division
 
46
from __future__ import absolute_import
 
47
from __future__ import print_function
46
48
from array import array
47
49
import collections
48
50
from fractions import Fraction
49
51
import numbers
50
52
import re
51
53
import aloha # define mode of writting
 
54
from six.moves import range
52
55
 
53
56
try:
54
57
    import madgraph.various.misc as misc
143
146
            if isinstance(expression, (MultLorentz, AddVariable, LorentzObject)):
144
147
                try:
145
148
                    expr = expression.expand().get_rep([0])
146
 
                except KeyError, error:
 
149
                except KeyError as error:
147
150
                    if error.args != ((0,),):
148
151
                        raise
149
152
                    else:
150
 
                        raise aloha.ALOHAERROR, '''Error in input format. 
 
153
                        raise aloha.ALOHAERROR('''Error in input format. 
151
154
    Argument of function (or denominator) should be scalar.
152
 
    We found %s''' % expression
 
155
    We found %s''' % expression)
153
156
                new = expr.simplify()
154
157
                if not isinstance(new, numbers.Number):
155
158
                    new = new.factorize()
171
174
            else:
172
175
                module = 'cmath.'
173
176
            try:
174
 
                return str(eval("%s%s(%s)" % (module,fct_tag, ','.join(`x` for x in argument))))
175
 
            except Exception, error:
176
 
                print error
177
 
                print "cmath.%s(%s)" % (fct_tag, ','.join(`x` for x in argument))
 
177
                return str(eval("%s%s(%s)" % (module,fct_tag, ','.join(repr(x) for x in argument))))
 
178
            except Exception as error:
 
179
                print(error)
 
180
                print("cmath.%s(%s)" % (fct_tag, ','.join(repr(x) for x in argument)))
178
181
        if str(fct_tag)+str(argument) in self.inverted_fct:
179
182
            tag = self.inverted_fct[str(fct_tag)+str(argument)]
180
183
            v = tag.split('(')[1][:-1]
223
226
            if not hasattr(term, 'vartype'):
224
227
                if isinstance(term, dict):
225
228
                    # allow term of type{(0,):x}
226
 
                    assert term.values() == [0]
 
229
                    assert list(term.values()) == [0]
227
230
                    term = term[(0,)]
228
231
                constant += term
229
232
                del self[pos]
265
268
                else:
266
269
                    nbminus += 1
267
270
        if countprefact and max(countprefact.values()) >1:
268
 
            fact_prefactor = sorted(countprefact.items(), key=lambda x: x[1], reverse=True)[0][0]
 
271
            fact_prefactor = sorted(list(countprefact.items()), key=lambda x: x[1], reverse=True)[0][0]
269
272
        else:
270
273
            fact_prefactor = 1
271
274
        if nbplus < nbminus:
560
563
                else:
561
564
                    nbminus += 1
562
565
    
563
 
            newadd.prefactor = sorted(countprefact.items(), key=lambda x: x[1], reverse=True)[0][0]
 
566
            newadd.prefactor = sorted(list(countprefact.items()), key=lambda x: x[1], reverse=True)[0][0]
564
567
            if nbplus < nbminus:
565
568
                newadd.prefactor *= -1
566
569
            if newadd.prefactor != 1:
621
624
        """ initialization of the object with default value """        
622
625
        #array.__init__(self, 'i', old) <- done already in new !!
623
626
        self.prefactor = prefactor
624
 
        assert isinstance(self.prefactor, (float,int,long,complex))
 
627
        assert isinstance(self.prefactor, (float,int,int,complex))
625
628
    
626
629
    def get_id(self):
627
630
        assert len(self) == 1
684
687
#                    self.append(new_id)
685
688
#            return self
686
689
        else:
687
 
            raise Exception, 'Cann\'t replace a Variable by %s' % type(expression)
 
690
            raise Exception('Cann\'t replace a Variable by %s' % type(expression))
688
691
        
689
692
    
690
693
    def get_all_var_names(self):
785
788
            text = '(%s)' % (' * '.join(t))
786
789
        return text
787
790
        
788
 
    __rep__ = __str__
 
791
    __repr__ = __str__
789
792
    
790
793
    def factorize(self):
791
794
        return self
1457
1460
        else:
1458
1461
            # Special case for Scalar object
1459
1462
            self.data = 0
1460
 
            self.next = self.nextscalar
1461
1463
                
1462
1464
    def __iter__(self):
1463
1465
        return self
1464
1466
 
1465
 
    def next(self):
 
1467
    def __next__(self):
 
1468
        if not self.len:
 
1469
            return self.nextscalar()
 
1470
 
1466
1471
        for i in range(self.len):
1467
1472
            if self.data[i] < 3:
1468
1473
                self.data[i] += 1
1470
1475
            else:
1471
1476
                self.data[i] = 0
1472
1477
        raise StopIteration
 
1478
    #Python2
 
1479
    next = __next__
1473
1480
            
1474
1481
    def nextscalar(self):
1475
1482
        if self.data:
1495
1502
    import cProfile
1496
1503
    def create():
1497
1504
        for i in range(10000):
1498
 
            LorentzObjectRepresentation.compare_indices(range(i%10),[4,3,5])       
 
1505
            LorentzObjectRepresentation.compare_indices(list(range(i%10)),[4,3,5])       
1499
1506
        
1500
1507
    cProfile.run('create()')