# Introduction

In this notebook, we will demonstrate some of the differences between SymPy's `logic` module, and PyEDA's logic expressions.


```python
import sympy
```


```python
import pyeda.boolalg.expr
import pyeda.boolalg.bfarray
```

# Create Variables

The `xs` array is a tuple of SymPy symbolic variables,
and the `ys` array is a PyEDA function array.


```python
xs = sympy.symbols(",".join("x%d" % i for i in range(64)))
```


```python
ys = pyeda.boolalg.bfarray.exprvars('y', 64)
```

# Basic Boolean Functions

Create a SymPy XOR function:


```python
f = sympy.Xor(*xs[:4])
```

Create a PyEDA `XOR` function:


```python
g = pyeda.boolalg.expr.Xor(*ys[:4])
```

SymPy `atoms` method is similar to PyEDA's `support` property:


```python
f.atoms()
```


```python
g.support
```

SymPy's `subs` method is similar to PyEDA's `restrict` method:


```python
f.subs({xs[0]: 0, xs[1]: 1})
```


```python
g.restrict({ys[0]: 0, ys[1]: 1})
```

# Conversion to NNF

Conversion to negation normal form is also similar. One difference is that SymPy inverts the variables by applying a `Not` operator, but PyEDA converts inverted variables to *complements* (a negative *literal*).


```python
sympy.to_nnf(f)
```


```python
type(sympy.Not(xs[0]))
```


```python
g.to_nnf()
```


```python
type(~ys[0])
```

# Conversion to DNF

Conversion to disjunctive normal form, on the other hand, has some differences. With only four input variables, `SymPy` takes a couple seconds to do the calculation. The output is large, with unsimplified values and redundant clauses.


```python
sympy.to_dnf(f)
```

PyEDA's DNF conversion is minimal:


```python
g.to_dnf()
```

It's a little hard to do an apples-to-apples comparison, because 1) SymPy is pure Python and 2) the algorithms are probably different.

The `simplify_logic` function actually looks better for comparison:


```python
from sympy.logic import simplify_logic
```


```python
simplify_logic(f)
```


```python
simplify_logic(f)
```

Running this experiment from `N=2` to `N=6` shows that PyEDA's runtime grows significantly slower.


```python
import numpy as np
import matplotlib.pyplot as plt
```


```python
%matplotlib inline
```


```python
N = 5

sympy_times = (.000485,  .000957, .00202,  .00426, .0103)
pyeda_times = (.0000609, .000104, .000147, .00027, .000451)

ind = np.arange(N)  # the x locations for the groups
width = 0.35        # the width of the bars

fig, ax = plt.subplots()

rects1 = ax.bar(ind, sympy_times, width, color='r')
rects2 = ax.bar(ind + width, pyeda_times, width, color='y')

# add some text for labels, title and axes ticks
ax.set_ylabel('Time (s)')
ax.set_title('SymPy vs. PyEDA: Xor(x[0], x[1], ..., x[n-1]) to DNF')
ax.set_xticks(ind + width)
ax.set_xticklabels(('N=2', 'N=3', 'N=4', 'N=5', 'N=6'))

ax.legend((rects1[0], rects2[0]), ('SymPy', 'PyEDA'))

plt.show()
```

Going a bit further, things get worse.

These numbers are from my laptop:

|  N | sympy    | pyeda    | ratio  |
|----|----------|----------|--------|
|  2 |  .000485 | .0000609 |   7.96 |
|  3 |  .000957 | .000104  |   9.20 |
|  4 |  .00202  | .000147  |  13.74 |
|  5 |  .00426  | .00027   |  15.78 |
|  6 |  .0103   | .000451  |  22.84 |
|  7 |  .0231   | .000761  |  30.35 |
|  8 |  .0623   | .00144   |  43.26 |
|  9 |  .162    | .00389   |  41.65 |
| 10 |  .565    | .00477   | 118.45 |
| 11 | 1.78     | .012     | 148.33 |
| 12 | 6.46     | .0309    | 209.06 |

# Simplification

SymPy supports some obvious simplifications, but PyEDA supports more. Here are a few examples.


```python
sympy.Equivalent(xs[0], xs[1], 0)
```


```python
pyeda.boolalg.expr.Equal(ys[0], ys[1], 0)
```


```python
sympy.ITE(xs[0], 0, xs[1])
```


```python
pyeda.boolalg.expr.ITE(ys[0], 0, ys[1])
```


```python
sympy.Or(xs[0], sympy.Or(xs[1], xs[2]))
```


```python
pyeda.boolalg.expr.Or(ys[0], pyeda.boolalg.expr.Or(ys[1], ys[2]))
```


```python
sympy.Xor(xs[0], sympy.Not(sympy.Xor(xs[1], xs[2])))
```


```python
pyeda.boolalg.expr.Xor(ys[0], pyeda.boolalg.expr.Xnor(ys[1], ys[2]))
```

6ba3f5ab6cdf8e8078b293d60572d70afaab2f08
ipynb
Jupyter Notebook
ipynb/SymPy_Comparison.ipynb
ivotimev/pyeda
c2d21901db0801342024db0adcdd9747e77d031f
BSD-2-Clause
2015-01-12T08:06:28.000Z
2022-03-26T17:18:06.000Z
thirdparty/pyeda/ipynb/SymPy_Comparison.ipynb
PhilippvK/python-myeda
e8501a24535d73997fed9445b45b08ca93cb4b0b
MIT
2015-02-23T17:29:52.000Z
2022-01-16T16:04:01.000Z
thirdparty/pyeda/ipynb/SymPy_Comparison.ipynb
PhilippvK/python-myeda
e8501a24535d73997fed9445b45b08ca93cb4b0b
MIT
2015-04-09T22:11:23.000Z
2021-12-23T15:53:50.000Z
Qwen/Qwen-72B
1. YES
2. YES