Skip to content

Commit b7cfd30

Browse files
alexfiklinducer
authored andcommitted
geometric_algebra: use int.bit_count
1 parent be2c780 commit b7cfd30

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

pymbolic/geometric_algebra/__init__.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,6 @@ def permutation_sign(p: Iterable[int]) -> int:
151151
return s
152152

153153

154-
def bit_count(i: int) -> int:
155-
"""Count the number of set bits in *i*."""
156-
157-
return i.bit_count()
158-
159-
160154
def canonical_reordering_sign(a_bits: int, b_bits: int) -> int:
161155
"""Count the number of basis vector swaps required to
162156
get the combination of *a_bits* and *b_bits* into canonical order.
@@ -629,7 +623,7 @@ def stringify(self, coeff_stringifier, enclosing_prec):
629623

630624
terms = []
631625
for bits in sorted(self.data.keys(),
632-
key=lambda _bits: (bit_count(_bits), _bits)):
626+
key=lambda _bits: (_bits.bit_count(), _bits)):
633627
coeff = self.data[bits]
634628

635629
# {{{ try to find a stringifier
@@ -868,7 +862,7 @@ def inv(self):
868862
(bits, coeff), = self.data.items()
869863

870864
# (1.1.54) in [HS]
871-
grade = bit_count(bits)
865+
grade = bits.bit_count()
872866
if grade*(grade-1)//2 % 2:
873867
coeff = -coeff
874868

@@ -884,7 +878,7 @@ def rev(self):
884878
"""
885879
new_data = {}
886880
for bits, coeff in self.data.items():
887-
grade = bit_count(bits)
881+
grade = bits.bit_count()
888882
if grade*(grade-1)//2 % 2 == 0:
889883
new_data[bits] = coeff
890884
else:
@@ -900,7 +894,7 @@ def invol(self):
900894
"""
901895
new_data = {}
902896
for bits, coeff in self.data.items():
903-
grade = bit_count(bits)
897+
grade = bits.bit_count()
904898
if grade % 2 == 0:
905899
new_data[bits] = coeff
906900
else:
@@ -989,7 +983,7 @@ def gen_blades(self, grade=None):
989983
yield MultiVector({bits: coeff}, self.space)
990984
else:
991985
for bits, coeff in self.data.items():
992-
if bit_count(bits) == grade:
986+
if bits.bit_count() == grade:
993987
yield MultiVector({bits: coeff}, self.space)
994988

995989
def project(self, r):
@@ -999,7 +993,7 @@ def project(self, r):
999993
"""
1000994
new_data = {}
1001995
for bits, coeff in self.data.items():
1002-
if bit_count(bits) == r:
996+
if bits.bit_count() == r:
1003997
new_data[bits] = coeff
1004998

1005999
return MultiVector(new_data, self.space)
@@ -1019,7 +1013,7 @@ def xproject(self, r, dtype=None):
10191013
def all_grades(self):
10201014
"""Return a :class:`set` of grades occurring in *self*."""
10211015

1022-
return {bit_count(bits) for bits, coeff in self.data.items()}
1016+
return {bits.bit_count() for bits, coeff in self.data.items()}
10231017

10241018
def get_pure_grade(self):
10251019
"""If *self* only has components of a single grade, return
@@ -1031,7 +1025,7 @@ def get_pure_grade(self):
10311025
result = None
10321026

10331027
for bits in self.data.keys():
1034-
grade = bit_count(bits)
1028+
grade = bits.bit_count()
10351029
if result is None:
10361030
result = grade
10371031
elif result == grade:
@@ -1045,7 +1039,7 @@ def odd(self):
10451039
"""Extract the odd-grade blades."""
10461040
new_data = {}
10471041
for bits, coeff in self.data.items():
1048-
if bit_count(bits) % 2:
1042+
if bits.bit_count() % 2:
10491043
new_data[bits] = coeff
10501044

10511045
return MultiVector(new_data, self.space)
@@ -1054,7 +1048,7 @@ def even(self):
10541048
"""Extract the even-grade blades."""
10551049
new_data = {}
10561050
for bits, coeff in self.data.items():
1057-
if bit_count(bits) % 2 == 0:
1051+
if bits.bit_count() % 2 == 0:
10581052
new_data[bits] = coeff
10591053

10601054
return MultiVector(new_data, self.space)

0 commit comments

Comments
 (0)