# sympy [1] - Symbolic Python

https://docs.sympy.org/latest/index.html

https://github.com/sympy/sympy/wiki

1. Meurer A, Smith CP, Paprocki M, Čertík O, Kirpichev SB, Rocklin M, Kumar A, Ivanov S, Moore JK, Singh S, Rathnayake T, Vig S, Granger BE, Muller RP, Bonazzi F, Gupta H, Vats S, Johansson F, Pedregosa F, Curry MJ, Terrel AR, Roučka Š, Saboo A, Fernando I, Kulal S, Cimrman R, Scopatz A. (2017) sympy: symbolic computing in Python. PeerJ Computer Science 3:e103 https://doi.org/10.7717/peerj-cs.103

<!-- @article{10.7717/peerj-cs.103,
 title = {sympy: symbolic computing in Python},
 author = {Meurer, Aaron and Smith, Christopher P. and Paprocki, Mateusz and \v{C}ert\'{i}k, Ond\v{r}ej and Kirpichev, Sergey B. and Rocklin, Matthew and Kumar, AMiT and Ivanov, Sergiu and Moore, Jason K. and Singh, Sartaj and Rathnayake, Thilina and Vig, Sean and Granger, Brian E. and Muller, Richard P. and Bonazzi, Francesco and Gupta, Harsh and Vats, Shivam and Johansson, Fredrik and Pedregosa, Fabian and Curry, Matthew J. and Terrel, Andy R. and Rou\v{c}ka, \v{S}t\v{e}p\'{a}n and Saboo, Ashutosh and Fernando, Isuru and Kulal, Sumith and Cimrman, Robert and Scopatz, Anthony},
 year = 2017,
 month = jan,
 keywords = {Python, Computer algebra system, Symbolics},
 abstract = {
            sympy is an open source computer algebra system written in pure Python. It is built with a focus on extensibility and ease of use, through both interactive and programmatic applications. These characteristics have led sympy to become a popular symbolic library for the scientific Python ecosystem. This paper presents the architecture of sympy, a description of its features, and a discussion of select submodules. The supplementary material provide additional examples and further outline details of the architecture and features of sympy.
         },
 volume = 3,
 pages = {e103},
 journal = {PeerJ Computer Science},
 issn = {2376-5992},
 url = {https://doi.org/10.7717/peerj-cs.103},
 doi = {10.7717/peerj-cs.103}
} -->


```python
import sympy
```

## Number representation


```python
sympy.sqrt(7)
```


```python
sympy.Float(9/4)
```


```python
sympy.Integer(9/4) ## drops the fractional part
```


```python
sympy.Rational(9/4)
```

## Defining variables (i.e. symbols)


```python
x, y, z, t = sympy.symbols('x y z t')
```

## Expresssions

- can type them directly into the cell:


```python
x**2+3*x+2
```


```python
type(x**2+3*x+2)
```

- alternatively, `sympify` will converts a string to a sympy function


```python
function_string = 'x**2+3*x+2'
function_string
```


```python
## Equivalent statement
# function_sympy = sympy.sympify('x**2+3*x+2')

function_sympy = sympy.sympify(function_string, evaluate=False)
function_sympy
```

Let's prove that it took a string and converted it to a sympy object:


```python
print(type(function_string))
print(type(function_sympy))
```


```python
sympy.sympify("2/3 + 1/6", evaluate=False)
```

`simpify` also attempts to simplify a provided forumla:


```python
sympy.sympify("2/3 + 1/6", evaluate=True)
```

Also does so for equations with variables.


```python
sympy.sympify("(1 + x)*(2 + x**2)*(2 + x**2)*(2 + x**2)", evaluate=False)
```

Let' go back and see what `Rational` would do with $\frac{2}{3} + \frac{1}{6}$:


```python
sympy.Rational(2/3 + 1/6)
```

This looks complicated, but it is just the manner tha sympy stores the resulting number:


```python
print(3752999689475413/4503599627370496)
print(5/6)
```

#### Evaluate an expression or function

Convert a sympy expressions to a float

The following two are equivalent:
- `evalf()`
- `sympy.N()`


```python
test_func = sympy.sqrt(7)*sympy.pi
test_func
```


```python
test_func.evalf()
```


```python
sympy.N(test_func)
```

Returning to our fraction above, we could get float value via:


```python
sympy.sympify("2/3 + 1/6").evalf()
```

## Polynomials

sympy can also create and work with polynomials.

Recall: 2nd Degree Polynomial (Three coefficients: [M, N, O] --> $Mx^2 + Nx + O$)

One can do this using two approaches, which result in two different object types:
1. A sympy "expression"
1. A sympy Poly

##### Approach 1
 Using a sympy "expresssion"


```python
polynomial_simple_expr = -4*x**2 + 1*x -2
polynomial_simple_expr
```


```python
type(polynomial_simple_expr)
```

##### Approach 2
Using sympy's `Poly` function (https://docs.sympy.org/latest/modules/polys/reference.html)

    - Efficiently transform an expression into a polynomial


```python
polynomial_simple_poly = sympy.Poly(-4*x**2 + 1*x -2)
polynomial_simple_poly
```


```python
type(polynomial_simple_poly)
```

One can also create a polynomial by providing a list of **ALL** of the coefficients (including those that might be zero).


```python
polynomial_simple_poly = sympy.Poly.from_list([-4, 1, -2], x)
polynomial_simple_poly
```

Print the degree:

- of a sympy expression


```python
sympy.degree(polynomial_simple_expr)
```

- of a sympy `Poly`


```python
sympy.degree(polynomial_simple_poly)
```

#### Evaluating Polynomials

- Use `subs` to substitute values (https://docs.sympy.org/latest/tutorial/basic_operations.html)
- `subs` works for both
    - expressions (e.g. sympy.core.add.Add, sympy.core.power.Pow)
    - `Poly` (i.e. sympy.polys.polytools.Poly)

##### Evaluating a Single Variable Polynomial

- expression


```python
polynomial_simple_expr.subs(x, 2)
```

- Poly


```python
polynomial_simple_poly.subs(x, 2)
```

##### Evaluating a Multiple Variable Polynomial

- Using `subs` pass a
    - list of tuples
    - dictionary 


```python
polynomial_multi_expr = x**2 + x*y + 3
polynomial_multi_expr
```

**Approach 1**: Substitute the two variables by providing a list:


```python
polynomial_multi_expr.subs([(x, 3), (y, 2)])
```


```python
type(polynomial_multi_expr.subs([(x, 3), (y, 2)]))
```

**Approach 2**: SSubstitute the two variables by providing a dictionary:


```python
polynomial_multi_expr.subs({x: 3, y: 2})
```

To obtain a float, use `evalf` (https://docs.sympy.org/latest/modules/evalf.html)

Important: the variable values are passed as a dictionary (i.e. lists don't work as they did for `subs`).


```python
polynomial_multi_expr.evalf(subs = {x: 3, y: 2})
```


```python
type(polynomial_multi_expr.evalf(subs = {x: 3, y: 2}))
```

### Expanding a polynomial

Notice: we are not specifying a Poly object.


```python
polynomial_expr = (-4*x**2 + 1*x - 2)**2
polynomial_expr
```


```python
polynomial_expr.expand()
```


```python
polynomial_expr
```

sympy `Poly` will expand it automatically:


```python
polynomial_poly = sympy.Poly((-4*x**2 + 1*x - 2)**2)
polynomial_poly
```

### Factor a polynomial


```python
polynomial_expr = (16*x**4 - 8*x**3 + 17*x**2 - 4*x + 4).factor()
polynomial_expr
```


```python
type(polynomial_expr)
```

Notice this does not work for sympy Poly:


```python
# sympy.Poly((-4*x**2 + 1*x - 2)**2).fractor()

polynomial_poly.factor()
```

### Math with Polynomials

#### Multiply two polynomials together

Expression


```python
polynomial_simple_expr
```


```python
polynomial_simple_expr * polynomial_simple_expr
```

sympy `Poly`


```python
polynomial_simple_poly
```


```python
polynomial_simple_poly * polynomial_simple_poly
```

Recall: `Poly` automatically expands the polynomials.

#### Calculus

#### Taking Derivatives
 -`diff()` (https://docs.sympy.org/latest/modules/core.html?highlight=diff#sympy.core.function.diff)


**First derivative** (noted as $f'(x)$ or as $\frac{df}{dx}$, which is the same as $\frac{d}{dx}f$)

Taking the first derivative of a function (e.g. f(x)) tells us:
1. if the function at a given point (ie. x) is increasing or decreasing (i.e. the **direction of change**), and
1. by how much it is increasing or decreasing (i.e. **a rate**)

##### sympy Expressions


```python
polynomial_expr = 2 * x**3 + 1
polynomial_expr
```

**Approach 1**


```python
sympy.diff(polynomial_expr, x, 1)
```

**Approach 2**


```python
polynomial_expr.diff(x)
```


```python
polynomial_expr.diff(x, 1)
```

##### sympy `Poly`


```python
polynomial_poly = sympy.poly(2 * x**3 + 1)
polynomial_poly
```

**Approach 1**


```python
polynomial_poly.diff(x)
```

**Approach 2**


```python
sympy.diff(polynomial_poly, x)
```

**Note**: that `sympy.diff(polynomial_poly, x, 1)` or `polynomial_poly.diff(x, 1)` does not work for a sympy poly object, as it did using the sympy expression above.

Therefore, it is generally better to have your **functions as sympy expressions** if you plan to preforms some **calculus** on them.

**Second derivative** (noted as $f''(x)$ or as $\frac{d^2f}{dx}$)

Taking the second derivative of a function (e.g. f(x)) tells us:
1. the **shape** of the curve at a specified point (i.e. x)
    - **postive** value: the curve is **convex**
    - **negative** value: the curve is **concave**


```python
polynomial_expr.diff(x, x)
```


```python
polynomial_expr.diff(x, 2)
```


```python
sympy.diff(polynomial_expr, x, 2)
```

##### Derivative of Multiple Variable Functions


```python
polynomial_multi_expr
```

1st derivative with respect to x:


```python
sympy.diff(polynomial_multi_expr, x, 1)
```

1st derivative with respect to y:


```python
sympy.diff(polynomial_multi_expr, y, 1)
```

2nd-order mixed derivatives:

Derivative with respect to x **and then** to y:
- i.e. $\frac{d}{dy}(\frac{df}{dx})$

$f(x) = x^2 + xy + 3$

$f'(x) = \frac{df}{dx} = \frac{d}{dx}(x^2 + xy + 3) = 2x + y$

$\frac{df'(x)}{dy} = \frac{d}{dy}(2x + y) = 1$


```python
sympy.diff(polynomial_multi_expr, x, y)
```

### Integration

The integral of a function (e.g. f(x)) tells us:

1. the area that resides under the function (e.g. the amount of water in a curve shaped pool).

#### Indefinite integral (i.e. without limits/bounds)

$\int (2x^3+1) \,dx = \frac{x^4}{2} + 1x$

(recall: if you take the derivative of the result, you will get the input)

**Approaches 1**
- sympy expression
- sympy Poly


```python
polynomial_expr
```


```python
sympy.integrate(polynomial_expr, x)
```


```python
sympy.integrate(polynomial_poly, x)
```

**Approaches 2**
- sympy expression
- sympy Poly


```python
polynomial_expr.integrate(x)
```


```python
polynomial_poly.integrate(x)
```

#### Definate integral (i.e. evaluated between two boundary conditions)

$\int_1^2 (2x^3+1) \,dx = \frac{x^4}{2} + 1x$

Notice: the tuple being passed (i.e. `(x, 1, 2)`)


```python
sympy.integrate(polynomial_expr, (x, 1, 2))
```


```python
polynomial_expr.integrate((x, 1, 2))
```

This is where I personally find some of the logic/consistency breaking down in sympy. It seems like `sympy.integrate` only works with expresssions.


```python
sympy.integrate(polynomial_poly, (x, 1, 2))
```

## Visualizing functions (i.e. plotting)

- backend: matplotlib

Plot a function (i.e. f(x)) by its single variable

(doing something like f(x,y) would require a 2D plot)


```python
sympy.plot(x**2 + 20)
```

Change the x-axis range:


```python
sympy.plot(x**2 + 20, (x, -2, 2))
```


```python
function = x**4
plot_func = sympy.plot(function)
plot_der1 = sympy.plot(sympy.diff(function, x, 1))
plot_der2 = sympy.plot(sympy.diff(function, x, 2))
```

## Solving equations

- Finding the "roots" of the equation (i.e. what the variable values are that sets the equation to zero).

Equation:

$x^2 = 4$

Rearrange:

$x^2 -4 = 0$

Solve:

Solutions are
1. x = 2
1. x = -2


```python
solutions = sympy.solve(x**2 - 4)
solutions
```


```python
solutions[0]
```

### Misc sympy functions

Lambdify: https://docs.sympy.org/latest/modules/utilities/lambdify.html
    - transforms a sympy expression to a lambda function that can be used to evaluate (solve) equations

- https://docs.python.org/3/reference/expressions.html#lambda

Examples of lambda functions
- `lambda x: x+x`
- `lambda a, b: a+b`

And you can directly evaluate a lambda function:


```python
(lambda x: x*2)(12)
```


```python
function = x**2

derivative_function = function.diff(x)
derivative_function
```


```python
f1 = sympy.lambdify(x, derivative_function)
```


```python
f1(3)
```


```python
multi_var_func = (x**2 + y**3 + z**4)
```

List the variables


```python
multi_var_func.free_symbols
```

To evaluate the function for when x=1, y=2 and z=3 and return an regular Python `int` type:


```python
f = sympy.lambdify([x, y, z], multi_var_func)

f(1, 2, 3)
```


```python
type(f(1, 2, 3))
```

You can also evalute the function to return a `sympy.core.numbers.Integer` type:


```python
multi_var_func.subs([(x, 1), (y, 2), (z, 3)])
```


```python
type(multi_var_func.subs([(x, 1), (y, 2), (z, 3)]))
```

To obtain a `sympy.core.numbers.Float`:


```python
multi_var_func.evalf(subs = {x: 1, y: 2, z: 3})
```


```python
type(multi_var_func.evalf(subs = {x: 1, y: 2, z: 3}))
```

## Unit Conversions with sympy


```python
from sympy.physics.units import convert_to
from sympy.physics.units import speed_of_light
from sympy.physics.units import kilometer, meter, centimeter, millimeter 
from sympy.physics.units import second, minute, hour
```


```python
speed_of_light
```


```python
speed_of_light.evalf()
```

To obtain a value, you must specify the desired units:


```python
convert_to(speed_of_light, [meter, minute])
```


```python
convert_to(speed_of_light, [millimeter, hour])
```


```python
#help(sympy.physics.units)
```

#### Constants built into sympy
1. A: ampere
1. Bq: becquerel
1. C: coulomb
1. D: dioptre
1. F: farad
1. G: gravitational_constant
1. Gy: gray
1. H: henry
1. Hz: hertz
1. J: joule
1. K: kelvin
1. N: newton
1. Pa: pascal
1. R: molar_gas_constant
1. S: siemens
1. T: tesla
1. V: volt
1. W: watt
1. Wb: weber
1. Z0: vacuum_impedance
1. __all__: ['Dimension', 'DimensionSystem', 'UnitSystem', 'convert_to',...
1. acceleration: Dimension(acceleration)
1. acceleration_due_to_gravity: acceleration_due_to_gravity
1. action: Dimension(action, A)
1. amount: Dimension(amount_of_substance)
1. amount_of_substance: Dimension(amount_of_substance)
1. ampere: ampere
1. amperes: ampere
1. amu: atomic_mass_constant
1. amus: atomic_mass_constant
1. angular_mil: angular_mil
1. angular_mils: angular_mil
1. anomalistic_year: anomalistic_year
1. anomalistic_years: anomalistic_year
1. astronomical_unit: astronomical_unit
1. astronomical_units: astronomical_unit
1. atm: atmosphere
1. atmosphere: atmosphere
1. atmospheres: atmosphere
1. atomic_mass_constant: atomic_mass_constant
1. atomic_mass_unit: atomic_mass_constant
1. atto: Prefix('atto', 'a', -18)
1. au: astronomical_unit
1. avogadro: avogadro_constant
1. avogadro_constant: avogadro_constant
1. avogadro_number: avogadro_number
1. bar: bar
1. bars: bar
1. becquerel: becquerel
1. bit: bit
1. bits: bit
1. boltzmann: boltzmann_constant
1. boltzmann_constant: boltzmann_constant
1. byte: byte
1. c: speed_of_light
1. candela: candela
1. candelas: candela
1. capacitance: Dimension(capacitance)
1. cd: candela
1. centi: Prefix('centi', 'c', -2)
1. centiliter: centiliter
1. centiliters: centiliter
1. centimeter: centimeter
1. centimeters: centimeter
1. charge: Dimension(charge, Q)
1. cl: centiliter
1. cm: centimeter
1. common_year: common_year
1. common_years: common_year
1. conductance: Dimension(conductance, G)
1. coulomb: coulomb
1. coulomb_constant: coulomb_constant
1. coulombs: coulomb
1. current: Dimension(current, I)
1. dHg0: 13.5951
1. day: day
1. days: day
1. deca: Prefix('deca', 'da', 1)
1. deci: Prefix('deci', 'd', -1)
1. deciliter: deciliter
1. deciliters: deciliter
1. decimeter: decimeter
1. decimeters: decimeter
1. deg: degree
1. degree: degree
1. degrees: degree
1. dioptre: dioptre
1. dl: deciliter
1. dm: decimeter
1. draconic_year: draconic_year
1. draconic_years: draconic_year
1. e0: vacuum_permittivity
1. eV: electronvolt
1. electric_constant: vacuum_permittivity
1. electric_force_constant: coulomb_constant
1. electronvolt: electronvolt
1. electronvolts: electronvolt
1. elementary_charge: elementary_charge
1. energy: Dimension(energy, E)
1. exa: Prefix('exa', 'E', 18)
1. exbi: Prefix('exbi', 'Y', 60, 2)
1. exbibyte: exbibyte
1. exbibytes: exbibyte
1. farad: farad
1. faraday_constant: faraday_constant
1. farads: farad
1. feet: foot
1. femto: Prefix('femto', 'f', -15)
1. foot: foot
1. force: Dimension(force, F)
1. frequency: Dimension(frequency, f)
1. ft: foot
1. full_moon_cycle: full_moon_cycle
1. full_moon_cycles: full_moon_cycle
1. g: gram
1. gaussian_year: gaussian_year
1. gaussian_years: gaussian_year
1. gee: acceleration_due_to_gravity
1. gees: acceleration_due_to_gravity
1. gibi: Prefix('gibi', 'Y', 30, 2)
1. gibibyte: gibibyte
1. gibibytes: gibibyte
1. giga: Prefix('giga', 'G', 9)
1. gram: gram
1. grams: gram
1. gravitational_constant: gravitational_constant
1. gray: gray
1. h: hour
1. hbar: hbar
1. hecto: Prefix('hecto', 'h', 2)
1. henry: henry
1. henrys: henry
1. hertz: hertz
1. hour: hour
1. hours: hour
1. hz: hertz
1. impedance: Dimension(impedance, Z)
1. inch: inch
1. inches: inch
1. inductance: Dimension(inductance)
1. josephson_constant: josephson_constant
1. joule: joule
1. joules: joule
1. julian_year: julian_year
1. julian_years: julian_year
1. kPa: kilopascal
1. kat: katal
1. katal: katal
1. kelvin: kelvin
1. kelvins: kelvin
1. kg: kilogram
1. kibi: Prefix('kibi', 'Y', 10, 2)
1. kibibyte: kibibyte
1. kibibytes: kibibyte
1. kilo: Prefix('kilo', 'k', 3)
1. kilogram: kilogram
1. kilograms: kilogram
1. kilometer: kilometer
1. kilometers: kilometer
1. km: kilometer
1. l: liter
1. length: Dimension(length, L)
1. lightyear: lightyear
1. lightyears: lightyear
1. liter: liter
1. liters: liter
1. luminosity: Dimension(luminous_intensity)
1. luminous_intensity: Dimension(luminous_intensity)
1. lux: lux
1. lx: lux
1. ly: lightyear
1. m: meter
1. magnetic_constant: magnetic_constant
1. magnetic_density: Dimension(magnetic_density, B)
1. magnetic_flux: Dimension(magnetic_flux)
1. magnetic_flux_density: Dimension(magnetic_density, B)
1. mass: Dimension(mass, M)
1. mebi: Prefix('mebi', 'Y', 20, 2)
1. mebibyte: mebibyte
1. mebibytes: mebibyte
1. mega: Prefix('mega', 'M', 6)
1. meter: meter
1. meters: meter
1. mg: milligram
1. mho: siemens
1. mhos: siemens
1. mi: mile
1. micro: Prefix('micro', 'mu', -6)
1. microgram: microgram
1. micrograms: microgram
1. micrometer: micrometer
1. micrometers: micrometer
1. micron: micrometer
1. microns: micrometer
1. microsecond: microsecond
1. microseconds: microsecond
1. mil: angular_mil
1. mile: mile
1. miles: mile
1. milli: Prefix('milli', 'm', -3)
1. milli_mass_unit: milli_mass_unit
1. milligram: milligram
1. milligrams: milligram
1. milliliter: milliliter
1. milliliters: milliliter
1. millimeter: millimeter
1. millimeters: millimeter
1. millisecond: millisecond
1. milliseconds: millisecond
1. minute: minute
1. minutes: minute
1. ml: milliliter
1. mm: millimeter
1. mmHg: mmHg
1. mmu: milli_mass_unit
1. mmus: milli_mass_unit
1. mol: mole
1. molar_gas_constant: molar_gas_constant
1. mole: mole
1. moles: mole
1. momentum: Dimension(momentum)
1. ms: millisecond
1. nano: Prefix('nano', 'n', -9)
1. nanometer: nanometer
1. nanometers: nanometer
1. nanosecond: nanosecond
1. nanoseconds: nanosecond
1. nautical_mile: nautical_mile
1. nautical_miles: nautical_mile
1. newton: newton
1. newtons: newton
1. nm: nanometer
1. nmi: nautical_mile
1. ns: nanosecond
1. ohm: ohm
1. ohms: ohm
1. optical_power: dioptre
1. pa: pascal
1. pascal: pascal
1. pascals: pascal
1. pebi: Prefix('pebi', 'Y', 50, 2)
1. pebibyte: pebibyte
1. pebibytes: pebibyte
1. percent: percent
1. percents: percent
1. permille: permille
1. peta: Prefix('peta', 'P', 15)
1. pico: Prefix('pico', 'p', -12)
1. picometer: picometer
1. picometers: picometer
1. picosecond: picosecond
1. picoseconds: picosecond
1. planck: planck
1. planck_acceleration: planck_acceleration
1. planck_angular_frequency: planck_angular_frequency
1. planck_area: planck_area
1. planck_charge: planck_charge
1. planck_current: planck_current
1. planck_density: planck_density
1. planck_energy: planck_energy
1. planck_energy_density: planck_energy_density
1. planck_force: planck_force
1. planck_impedance: planck_impedance
1. planck_intensity: planck_intensity
1. planck_length: planck_length
1. planck_mass: planck_mass
1. planck_momentum: planck_momentum
1. planck_power: planck_power
1. planck_pressure: planck_pressure
1. planck_temperature: planck_temperature
1. planck_time: planck_time
1. planck_voltage: planck_voltage
1. planck_volume: planck_volume
1. pm: picometer
1. pound: pound
1. pounds: pound
1. power: Dimension(power)
1. pressure: Dimension(pressure)
1. ps: picosecond
1. psi: psi
1. quart: quart
1. quarts: quart
1. rad: radian
1. radian: radian
1. radians: radian
1. s: second
1. second: second
1. seconds: second
1. sidereal_year: sidereal_year
1. sidereal_years: sidereal_year
1. siemens: siemens
1. speed: Dimension(velocity)
1. speed_of_light: speed_of_light
1. sr: steradian
1. stefan: stefan_boltzmann_constant
1. stefan_boltzmann_constant: stefan_boltzmann_constant
1. steradian: steradian
1. steradians: steradian
1. tebi: Prefix('tebi', 'Y', 40, 2)
1. tebibyte: tebibyte
1. tebibytes: tebibyte
1. temperature: Dimension(temperature, T)
1. tera: Prefix('tera', 'T', 12)
1. tesla: tesla
1. teslas: tesla
1. time: Dimension(time, T)
1. torr: mmHg
1. tropical_year: tropical_year
1. tropical_years: tropical_year
1. u0: magnetic_constant
1. ug: microgram
1. um: micrometer
1. us: microsecond
1. v: volt
1. vacuum_impedance: vacuum_impedance
1. vacuum_permeability: magnetic_constant
1. vacuum_permittivity: vacuum_permittivity
1. velocity: Dimension(velocity)
1. volt: volt
1. voltage: Dimension(voltage, U)
1. volts: volt
1. volume: Dimension(volume)
1. von_klitzing_constant: von_klitzing_constant
1. watt: watt
1. watts: watt
1. wb: weber
1. weber: weber
1. webers: weber
1. yard: yard
1. yards: yard
1. yd: yard
1. year: tropical_year
1. years: tropical_year
1. yocto: Prefix('yocto', 'y', -24)
1. yotta: Prefix('yotta', 'Y', 24)
1. zepto: Prefix('zepto', 'z', -21)
1. zetta: Prefix('zetta', 'Z', 21)

# Practicle Usage Example

A particle is moving in space with a velocity that can be defined by the following function:

$v(t) = t^2 - 2t - 8$

where time `t` is in seconds and the velocity `v` is measured in $\frac{\text{meter}}{\text{second}}$:

1. Plot the function.
1. At what time(s) is the particle at rest (i.e. $v(t) = 0$)? 
1. What is the acceleration of the particle at a time of 5.3 seconds into its trajectory? (Hint: acceleration is the first derivative of the velocity)

Original source: https://www.georgebrown.ca/sites/default/files/2020-05/Applications%20of%20Derivatives.pdf

First define what our sympy variable is:


```python
t = sympy.symbols('t')
velocity_function = t**2 - 2*t - 8
```

1. Plot the function


```python
plot_func = sympy.plot(velocity_function)
```

2. At what time(s) is the particle at rest (i.e. $v(t) = 0$)?

- That means we need to solve the equation an find its **roots**.

As learned above, we do this using the `solve` function.


```python
solutions = sympy.solve(velocity_function)
solutions
```

However, now it is time to **think** about the solutions. 

Since a negative time is not possible in our scenario, the answer to the question can only be 4 seconds.

3. What is the acceleration of the particle at t=3 seconds?

- Recall that acceleration is defined as the first derivative of the velecity with respect to time

$a(t) = \frac{d}{dt} v(t) = \frac{d}{dt} (t^2 - 2t - 8)$


```python
first_derivative = velocity_function.diff(t, 1)
first_derivative
```

Now evaluate the first derivative at a time of 5.3 seconds:


```python
time = 5.3
```


```python
acceleration = first_derivative.evalf(subs = {t: time})
acceleration
```


```python
print(f'The acceleration of the particle at {time} s into its trajectory is '
      f' {acceleration:0.1f} m/s^2.')
```

---
Side note about how the unit was obtained:

Derivative can also be expressed mathematically in a different way (i.e. via Leibniz):

$\frac{d}{dx} f(x) = \lim_{x \to x_0} f(x) = \lim_{x \to x_0} \frac{f(x) - f(x_0)}{x - x_0}$

if `f(x)` has units of $\frac{\text{meter}}{\text{second}}$

and `x` has units of $\text{second}$, then we have

$\lim_{x \to x_0} \frac{\frac{\text{meter}}{\text{second}} - \frac{\text{meter}}{\text{second}}}{\text{second} - \text{seconds}}$

$ = \frac{\frac{\text{meter}}{\text{second}}}{\text{second}} = \frac{\text{meter}}{\text{second second}} = \frac{\text{meter}}{\text{second}^2}$


```python

```

0bd64f89a9889e042eca6c561ae353e7e3b691e9
ipynb
Jupyter Notebook
sympy.ipynb
karlkirschner/2020_Scientific_Programming
e7830468194eb2ef7824bc46f6d9ee112c652e35
MIT
2020-03-30T12:24:57.000Z
2020-03-30T13:34:27.000Z
sympy.ipynb
karlkirschner/2020_Scientific_Programming
e7830468194eb2ef7824bc46f6d9ee112c652e35
MIT
sympy.ipynb
karlkirschner/2020_Scientific_Programming
e7830468194eb2ef7824bc46f6d9ee112c652e35
MIT
2020-04-18T10:23:47.000Z
2020-05-01T10:38:21.000Z
Qwen/Qwen-72B
1. YES
2. YES