-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Logic: algebraic normal form (Zhegalkin polynomial) #13686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
gxyd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't duplicated comments to similar requested changes, but please make those. Ex, use https:// prefix for links but I have made this comment only in one place.
|
I would hope that we get code coverage in, otherwise it is difficult to see how much testing has been done.
|
|
I think you have mistakenly changed the mode of sympy/logic/boolalg.py from 100644 -> 100755. Please remove that. |
|
Also for the test file, seems like you made it executable. |
sympy/logic/boolalg.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test case for this is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was refactored as
def to_anf(self, deep=True):
args = range(1, len(self.args) + 1)
args = (combinations(self.args, j) for j in args)
args = chain.from_iterable(args) # powerset
args = (And(*arg) for arg in args)
args = map(lambda x: to_anf(x, deep=deep) if deep else x, args)
return Xor(*list(args), remove_true=False)
And covered with tests
assert to_anf(Or(x, y)) == Xor(x, y, And(x, y))
assert to_anf(Or(Implies(x, y), And(x, y), y)) == \
Xor(x, True, x & y, remove_true=False)
assert to_anf(Or(Nand(x, y), Nor(x, y), Xnor(x, y), Implies(x, y))) == True
assert to_anf(Or(x, Not(y), Nor(x,z), And(x, y), Nand(y, z))) == \
Xor(True, And(y, z), And(x, y, z), remove_true=False)
sympy/logic/boolalg.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for this as well is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was refactored as
def to_anf(self, deep=True):
args = range(1, len(self.args) + 1)
args = (combinations(self.args, j) for j in args)
args = chain.from_iterable(args) # powerset
args = (And(*arg) for arg in args)
args = map(lambda x: to_anf(x, deep=deep) if deep else x, args)
return Xor(*list(args), remove_true=False)
And covered with tests
assert to_anf(Or(x, y)) == Xor(x, y, And(x, y))
assert to_anf(Or(Implies(x, y), And(x, y), y)) == \
Xor(x, True, x & y, remove_true=False)
assert to_anf(Or(Nand(x, y), Nor(x, y), Xnor(x, y), Implies(x, y))) == True
assert to_anf(Or(x, Not(y), Nor(x,z), And(x, y), Nand(y, z))) == \
Xor(True, And(y, z), And(x, y, z), remove_true=False)
|
@asmeurer Please provide updates on this PR? |
|
@konstantintogoi Please resolve conflicts in this PR, if you are still interested to work on this. |
Added new features for converting boolean expression to Algebraic Normal Form. New functions: - to_anf - is_anf - distribute_xor_over_and - _convert_to_varsANF - anf_coeffs - ANFform - minterm - maxterm - monomial New instance method 'to_anf' and new class method '_to_anf'
|
✅ Hi, I am the SymPy bot (v150). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.6. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
Codecov Report
@@ Coverage Diff @@
## master #13686 +/- ##
=============================================
+ Coverage 75.545% 75.598% +0.052%
=============================================
Files 642 644 +2
Lines 167277 167812 +535
Branches 39441 39561 +120
=============================================
+ Hits 126371 126864 +493
- Misses 35382 35409 +27
- Partials 5524 5539 +15 |
Fixed. |
Fixed. |
Done. |
sympy/logic/boolalg.py
Outdated
| return _convert_to_varsPOS(k, variables) | ||
|
|
||
|
|
||
| def monomial(k, variables): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
monomial can be too much a common word that can be used by other disciplines.
https://en.wikipedia.org/wiki/Monomial
So I would have a suggestion to name this more specific. like prefixing bool_monimial, bool_minterm, bool_maxterm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've renamed the functions
ANFform. FunctionANFformconverts a list of truth values to an expression in Algebraic Normal Form (ANF).BooleanFunction.to_anfthat converts an expression to ANF by equivalent transformations.is_anfthat checks if an expression is ANF.to_anfthat converts an expression to ANF if it is not ANF.distribute_xor_over_and. Given a sentencesconsisting of conjunction and exclusive disjunctions of literals, it returns an equivalent exclusive disjunction.bool_mintermthat returns the k-th minterm of a fixed ordered set of binary variables.bool_maxtermthat returns the k-th maxterm of a fixed ordered set of binary variables.bool_monomialthat returns the k-th monomial of a fixed ordered set of binary variables.