# Algebra and Symbolic Math With Sympy
The mathematical problems and solutions
in our programs so far have all involved the
manipulation of numbers. But there’s another
way math is taught, learned, and practiced, and that’s
in terms of symbols and the operations between them.
Just think of all the xs and ys in a typical algebra prob-
lem. We refer to this type of math as symbolic math.
---

## Defining Symbols and Symbolic Operations


```python
from sympy import Symbol
a = Symbol('x')
a + a + 1
```




$\displaystyle 2 x + 1$




```python
p = x*(x + x)
p
```




$\displaystyle 2 x^{2}$




```python
p = (x + 2)*(x + 3)
p
```




$\displaystyle \left(x + 2\right) \left(x + 3\right)$



## Working with Expressions

### Factorizing and Expanding Expressions


```python
from sympy import Symbol
from sympy import factor

x = Symbol('x')
y = Symbol('y')

expr = x**2 - y**2
factor(expr)
```




$\displaystyle \left(x - y\right) \left(x + y\right)$



## Pretty Printing



```python
from sympy import Symbol
from sympy import factor
from sympy import init_printing

init_printing(order='rev-lex')

x = Symbol('x')
y = Symbol('y')

expr = x*x + 2*x*y + y*y

pprint(expr)
```

### Printing a Series



```python
from sympy import Symbol, pprint, init_printing
def print_series(n):    
    # Initialize printing system with reverse order
    init_printing(order='rev-lex')

    x = Symbol('x')
    series = x
    for i in range(2, n+1):
        series = series + (x**i)/i
    pprint(series)

if __name__ == '__main__':
    n = input('Enter the number of terms you want in the series: ')
    print_series(int(n))
```

## Substituting in Values
Let’s see how we can use SymPy to plug values into an algebraic expression.
This will let us calculate the value of the expression for certain values of the
variables. Consider the mathematical expression x 2 + 2xy + y 2 , which can be
defined as follows:


```python
from sympy import Symbol

x = Symbol('x')
y = Symbol('y')
expr = x*x + x*y + x*y + y*y

res = expr.subs({x:1, y:2})
print("the answer is: " + str(res))
```

### On Python Dictionaries



```python
sampledict = {"key1":5, "key2":20}
sampledict['key1']
expr_subs = expr.subs({x:1-y})
```

### Calculating the Value of a Series
Let’s revisit the series-printing program. In addition to printing the series,
we want our program to be able to find the value of the series for a par-
ticular value of x. That is, our program will now take two inputs from the
user—the number of terms in the series and the value of x for which the
value of the series will be calculated. Then, the program will output both
the series and the sum. The following program extends the series printing
program to include these enhancements:


```python
from sympy import Symbol, pprint, init_printing
def print_series(n, x_value):
    # Initialize printing system with reverse order
    init_printing(order='rev-lex')

    x = Symbol('x')
    series = x
    for i in range(2, n+1):
        series = series + (x**i)/i  

    pprint(series)

    # Evaluate the series at x_value
    series_value = series.subs({x:x_value})
    print('Value of the series at {0}: {1}'.format(x_value, series_value))

if __name__ == '__main__':
    n = input('Enter the number of terms you want in the series: ')
    x_value = input('Enter the value of x at which you want to evaluate the series: ')
    print_series(int(n), float(x_value))
```

2a5426b91a3018c930230e3013b90335a682f028
ipynb
Jupyter Notebook
2021/Python-Maths/chapter4.ipynb
Muramatsu2602/python-study
c81eb5d2c343817bc29b2763dcdcabed0f6a42c6
MIT
2021/Python-Maths/chapter4.ipynb
Muramatsu2602/python-study
c81eb5d2c343817bc29b2763dcdcabed0f6a42c6
MIT
2021/Python-Maths/chapter4.ipynb
Muramatsu2602/python-study
c81eb5d2c343817bc29b2763dcdcabed0f6a42c6
MIT
Qwen/Qwen-72B
1. YES
2. YES