高等数学公式精粹解析

一、微积分公式

1. 导数公式

  • 定义:导数是描述函数在某一点的瞬时变化率。
  • 公式:[ f’(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} ]
  • 举例:求函数 ( f(x) = x^2 ) 在 ( x = 2 ) 处的导数。
def derivative(f, x, h=0.00001):
    return (f(x + h) - f(x)) / h

def f(x):
    return x**2

result = derivative(f, 2)
print("导数:", result)

2. 积分公式

  • 定义:积分是求函数在某一区间内的累积量。
  • 公式:[ \int f(x) \, dx = F(x) + C ]
  • 举例:求函数 ( f(x) = x^2 ) 在区间 ([0, 1]) 上的积分。
import math

def integral(f, a, b):
    return math.fsum([f(x) for x in range(a, b+1)]) / (b - a)

result = integral(f, 0, 1)
print("积分:", result)

二、线性代数公式

1. 矩阵乘法

  • 定义:矩阵乘法是两个矩阵之间的运算。
  • 公式:[ C = AB ]
  • 举例:求矩阵 ( A = \begin{pmatrix} 1 & 2 \ 3 & 4 \end{pmatrix} ) 和 ( B = \begin{pmatrix} 5 & 6 \ 7 & 8 \end{pmatrix} ) 的乘积。
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]

C = [[sum(a*b for a, b in zip(A_row, B_col)) for B_col in zip(*B)] for A_row in A]
print("矩阵乘积:", C)

2. 矩阵行列式

  • 定义:行列式是方阵的一个数值。
  • 公式:[ \text{det}(A) = a{11}a{22} - a{12}a{21} ]
  • 举例:求矩阵 ( A = \begin{pmatrix} 1 & 2 \ 3 & 4 \end{pmatrix} ) 的行列式。
def determinant(A):
    return A[0][0] * A[1][1] - A[0][1] * A[1][0]

result = determinant([[1, 2], [3, 4]])
print("行列式:", result)

离散数学公式精粹解析

一、图论公式

1. 图的连通性

  • 定义:图的连通性是指图中任意两个顶点之间都存在路径。
  • 公式:[ \text{连通} \Leftrightarrow \text{任意两个顶点都有路径} ]
  • 举例:判断图 ( G ) 是否连通。
def is_connected(G):
    visited = set()
    queue = [G[0]]
    while queue:
        node = queue.pop(0)
        if node not in visited:
            visited.add(node)
            queue.extend([n for n in G if n not in visited and G[node][n] == 1])
    return len(visited) == len(G)

# 假设 G 是一个图的邻接矩阵
G = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]
print("图是否连通:", is_connected(G))

2. 最短路径

  • 定义:最短路径是指从一个顶点到另一个顶点的路径中,权值之和最小的路径。
  • 公式:[ \text{Dijkstra算法} ]
  • 举例:求图 ( G ) 中从顶点 ( s ) 到顶点 ( t ) 的最短路径。
import heapq

def dijkstra(G, s, t):
    distances = {node: float('infinity') for node in G}
    distances[s] = 0
    priority_queue = [(0, s)]
    while priority_queue:
        current_distance, current_node = heapq.heappop(priority_queue)
        if current_distance > distances[current_node]:
            continue
        for neighbor, weight in G[current_node].items():
            distance = current_distance + weight
            if distance < distances[neighbor]:
                distances[neighbor] = distance
                heapq.heappush(priority_queue, (distance, neighbor))
    return distances[t]

# 假设 G 是一个图的邻接矩阵
G = {
    's': {'a': 1, 'b': 4},
    'a': {'b': 2, 'c': 5},
    'b': {'c': 1, 'd': 2},
    'c': {'d': 1},
    'd': {}
}
print("最短路径长度:", dijkstra(G, 's', 'd'))

通过以上对高等数学和离散数学中一些核心公式的解析,希望能够帮助读者更好地理解和掌握这些数学领域的基本概念和技巧。