Summary of chapters 5-7 of Python mathematical programming

Mathematical programming, as the name suggests, is to use programming to serve mathematics. Mathematics is the key, and programming is the tool. To understand this point, in Chapter 6, when I encounter the images of Barnsley ferns and Mandelbrot set, but I can't understand their mathematical expressions, I don't hesitate anymore - I don't need to understand it now. When I can use it in the future, it's enough to remember that I once had an answer. At present, I am more interested in database and visualization, so I can roughly understand this part.

Chapter 5 set and probability

Module: Symphony

A set consists of element s or members.
Features: 1 "Clearly defined". 2. Any two elements in the set are not equal.

Functions and methods:

-Create collection

  1. FinitsSet()
    Purpose: create a collection
    Code example:
from sympy import FiniteSet
s= FiniteSet(1, 2, 3, 4, 6)

TIPS:
a. The same set can contain different types of numbers
b. Cardinality: cardinality in a set refers to the number of elements in the set
c. Empty set - created by: finiseset(), without entering any data into the function
d. You can also create collections from lists or tuples
Code example:

members = [1, 2, 3]
t = FiniteSet(*members)
*For special syntax, elements in a list are passed as independent parameters rather than as a list

-Subset, superset and power set

  1. .is_subset()
    Function: subset test
    Code example:
t.is_subset(s)
return True,Then explain t yes s Subset of
  1. .is_superset()
    Function: superset test
  2. .powerset()
    Action: power set creation
  3. .is_proper_subset()
    Function: true subset test
  4. .is_proper_superset()
    Function: true superset test

-Set operation

  1. union()
    Function: find the union of two sets
    Code example:
from sympy import FiniteSet, pi
s = FiniteSet(1, 2, 3)
t = FiniteSet(4, 5, 6)
m = s.union(t)              
TIPS: s Itself has not changed, union()Create a new set as their union, which can be passed to the variable we specify
  1. Intersection ()
    Code example:
n = s.intersect(t)
  1. Cartesian product
    Definition: a new set consisting of all possible pairs of elements extracted from each original set.
    Code example:
p = s*t
 s, t Are all sets, then p Get their Cartesian product 
 Each pair of elements in Cartesian product is a tuple, containing elements from the first set and the second set respectively
 
 Get the elements in it as follows
for elem in p:
    print(elem)

TIPS:
a. The cardinality of Cartesian product is the product of the cardinality of the original set

len(p) = len(s)*len(t)

-Probability calculation

The sixth chapter draws geometry and fractal

Module: matplotlib
Use: import Matplotlib pyplot as plt

-Draw geometry

  1. plt.figure() creates a figure object
  2. plt.axes() creates an axis
  3. gcf() function and gca() function get the reference of the current Figure and Axes object

Code example:

import matplotlib.pyplot as plt

def create_circle ():
    circle = plt.Circle((0, 0), radius= 0.5 , fc="g") 
    									#fc specifies the color of the circle ec specifies the edge color 
    return circle

def show_shape(patch):

    ax = plt.gca()
    ax.set_aspect("equal")              #Set aspect ratio
    ax.add_patch(patch)

    plt.axis("scaled")                  #Automatically adjust the value range of the number axis
    plt.show()

if __name__ == "__main__":
    c = create_circle()
    show_shape(c)
 In the above code,
 create_circle()In function plt.Circle()The first parameter entered specifies the center position,
 keyword radius Specify the radius.
 show_shape()In the function, plt.gca()Get the current Axes Object, that is, the reference of the coordinate axis object,
 Then add the created image to the Axes Object, by show()Show it
  1. animation()
    Purpose: create dynamic graphics
    Code example:
#Animated graphics

"""
A growing circle
"""

from matplotlib import pyplot as plt
from matplotlib import animation

#Create a circle
def create_circle():
    circle = plt.Circle((0, 0), 0.05)
    return circle

#Radius expansion
def update_radius(i, circle):
    circle.radius = i * 0.5
    return circle,

#Create animation
def create_animation():
    fig = plt.gcf()
    ax = plt.axes(xlim=(-10, 10), ylim=(-10, 10))
    ax.set_aspect("equal")
    circle = create_circle()
    ax.add_patch(circle)
    
    anim = animation.FuncAnimation(
        fig, update_radius, fargs= (circle,), frames=30, interval=50)
    plt.title("Simple Circle Animation")
    plt.show()

if __name__ == "__main__":
    create_animation()

TIPS:
a. animation.FuncAnimation() function input

  • fig is the current Figure object
  • update_radius is responsible for drawing each frame. Its parameters include frame number and block object, which are provided by fargs()
  • fargs () contains all (except the frame number) to pass to update_ Parameters of radius() function
  • Frames is the number of frames in the animation
  • Interval refers to the time interval between two frames

-Fractal strategy

Fractal is very difficult, so is the mathematical formula, but there is no clue how it came from, so I skipped this part.

Chapter 7 solving calculus problems

There are few new functions involved in Chapter 7, but there are many applications for them.

Module: Symphony

  1. Limit()
    Action: solving limits
    Code example:
from sympy import Limit, Symbol, sin          #S is a special Symphony class that contains definitions of positive and negative infinity and other special values
import math
x = Symbol("x")
Limit(1/x, x, S.Infinity).doit() #1/x function, the independent variable is tending to infinity
Limit(1/x, x, 0, dir="-").doit() #1/x function, argument tends to - 0
Limit(1/x, x, 0, dir="+").doit()
Limit(sin(x)/x, x, 0).doit()
 As shown above, Limit()The first parameter of is the expression, and the second parameter is the specified argument
 The third parameter is the approaching value of the independent variable. By default, it approaches the target value from the positive direction (that is, it gradually approaches the target value from a value larger than the target value)
 Specify if you want to approach from a negative direction dir="-" 
  1. Derivative()
    Function: derivation
    Code example:
from sympy import Symbol, Derivative

t = Symbol("t")
St = 5*t**2 + 2*t +8
Derivative(St, t).doit()            
#If not added doit() returns a Derivative object by default. At this time, the Derivative is not really calculated
Derivative(St, t).doit().subs({t:1})        #Pass Sub() function to replace with the number we want
And Limit()Similarly, the first parameter is an expression and the second parameter is an argument
 Can pass subs()Replace the independent variable with a specific value to find the specific value

Postscript

Python and mathematics, relatively speaking, Python is much simpler.
Go back and learn math well. When math is clear, there is a strong need to simplify calculation and realize visualization with Python. It's not too late to come back and get python.

Tags: Python

Posted by mikeschroeder on Tue, 19 Apr 2022 02:04:21 +0930