数学必背常识

· · 算法·理论

线性代数

行列式

x=A^{-1}b=\frac1{\det(A)}A^{*}b=\frac1{\det(A)}\sum_{i=1}^n\text{col}(A,i)b |A|=\sum\limits_{1\le j_1<j_2<\cdots<j_k\le n}A\left(\begin{matrix}i_1&i_2&\cdots&i_k\\j_1&j_2&\cdots&j_k\end{matrix}\right)\hat{A}\left(\begin{matrix}i_1&i_2&\cdots&i_k\\j_1&j_2&\cdots&j_k\end{matrix}\right)

矩阵

线性方程组

\text{rank}(A+B)= \text{rank}\left(\begin{bmatrix}I&I\end{bmatrix}\begin{bmatrix}A&O\\O&B\end{bmatrix}\begin{bmatrix}I\\I\end{bmatrix}\right)\le\text{rank}\left(\begin{bmatrix}A&O\\O&B\end{bmatrix}\right)= \text{rank}(A)+\text{rank}(B) \text{rank}(A)+\text{rank}(B)= \text{rank}\left(\begin{bmatrix} A&O\\O&B\\\end{bmatrix}\right)\le \text{rank}\left(\begin{bmatrix} A&-I_n\\O&B\\\end{bmatrix}\right)=\text{rank}\left(\begin{bmatrix} AB&O\\O&I_n\\\end{bmatrix}\right)=\text{rank}(AB)

线性空间与线性变换

特征值与二次型

数学分析

集合与映射

数列极限

函数极限与连续函数

微分

f(x) \text{d}f(x) \int f(x)\text{d}x
x^a ax^{a-1}\text{d}x \begin{cases}\frac1{a+1}x^{a+1}+C&a\ne -1\\\ln\lvert x\rvert +C&a=-1\end{cases}
\sin x \cos x\text{d}x -\cos x+C
\cos x \sin x\text{d}x \sin x+C
\tan x \sec^2 x\text{d}x -\ln\lvert \cos x\rvert +C
\cot x -\csc^2x\text{d}x \ln\lvert \sin x\rvert +C
\sec x \tan x\sec x\text{d}x \ln\lvert \sec x+\tan x\rvert +C
\csc x -\cot x\csc x\text{d}x \ln\lvert \csc x-\cot x\rvert +C
\arcsin x \frac{\text{d}x}{\sqrt{1-x^2}} \color{green}x\arcsin x+\sqrt{1-x^2}+C
\arccos x -\frac{\text{d}x}{\sqrt{1-x^2}} \color{green}x\arccos x-\sqrt{1-x^2}+C
\arctan x \frac{\text{d}x}{1+x^2} \color{green}x\arctan x-\frac12\ln(1+x^2)+C
\operatorname{arccot} x -\frac{\text{d}x}{1+x^2} \color{green}x\operatorname{arccot} x+\frac12\ln(1+x^2)+C
a^x(a\ne 1) a^x\ln a\text{d}x \frac{a^x}{\ln a}+C
\log_ax(a\ne 1) \frac{\text{d}x}{x\ln a} \color{green}\frac{x\ln x-x}{\ln a}+C
\operatorname{sh}x=\frac{e^x-e^{-x}}{2} \operatorname{ch}x\text{d}x \operatorname{ch}x+C
\operatorname{ch}x=\frac{e^x+e^{-x}}{2} \operatorname{sh} x\text{d}x \operatorname{sh}x+C
\operatorname{th}x=\frac{e^x-e^{-x}}{e^x+e^{-x}} \operatorname{sech}^2x\text{d}x \color{green}\ln(\operatorname{ch}x)+C
\operatorname{cth}x=\frac{e^x+e^{-x}}{e^x-e^{-x}} -\operatorname{csch}^2x\text{d}x \color{green}\ln(\operatorname{sh}x)+C
\text{sh}^{-1}x=\ln(x+\sqrt{1+x^2}) \frac{\text{d}x}{\sqrt{1+x^2}} \color{green}x\operatorname{sh}^{-1}x-\sqrt{1+x^2}+C
\text{ch}^{-1}x=\ln(x+\sqrt{x^2-1}) \frac{\text{d}x}{\sqrt{x^2-1}} \color{green}x\operatorname{ch}^{-1}x-\sqrt{x^2-1}+C
\text{th}^{-1}x=\frac12\ln\frac{1+x}{1-x} \frac{\text{d}x}{1-x^2} \color{green}x\operatorname{th}^{-1}x+\frac12\ln\lvert x^2-1\rvert +C
\text{cth}^{-1}=\frac12\ln{\frac{x+1}{x-1}} \frac{\text{d}x}{1-x^2} \color{green}x\operatorname{cth}^{-1}x+\frac12\ln\lvert x^2-1\rvert +C
\frac 1{\sqrt{a^2-x^2}} \color{green}\frac{x}{(a^2-x^2)^{3/2}} \arcsin\frac xa+C
\frac{1}{\sqrt{x^2\pm a^2}} \color{green}-\frac{x}{(x^2\pm a^2)^{3/2}} \ln\left\lvert x+\sqrt{x^2\pm a^2}\right\rvert +C
\frac1{x^2-a^2} \color{green}-\frac{2x}{(x^2-a^2)^2} \frac1{2a}\ln\left\lvert \frac{x-a}{x+a}\right\rvert
\frac{1}{x^2+a^2} \color{green}-\frac{2x}{(x^2+a^2)^2} \frac1a\arctan{\frac xa}+C
\sqrt{a^2-x^2} \color{green}-\frac x{\sqrt{a^2-x^2}} \frac{x}2\sqrt{a^2-x^2}+\frac{a^2}2\arcsin\frac{x}a+C
\sqrt{x^2\pm a^2} \color{green}\frac{x}{\sqrt {x^2\pm a^2}} \frac x{2}\sqrt{x^2\pm a^2}\pm\frac{a^2}2\ln\lvert x+\sqrt{x^2\pm a^2}\rvert +C

微分中值定理及其应用

f(x) \text{Maclaurin} 公式 \text{Lagrange} 余项
e^x 1+x+\frac{x^2}{2!}+\frac{x^3}{3!}+\cdots +\frac{x^n}{n!}+o(x^n) \frac{e^{\theta x}}{(n+1)!}x^{n+1}
\sin x x-\frac{x^3}{3!}+\frac{x^5}{5!}-\cdots +(-1)^n\frac{x^{2n+1}}{(2n+1)!}+o(x^{2n+2}) (-1)^{n+1}\frac{\cos(\theta x)}{(2n+3)!}x^{2n+3}
\cos x 1-\frac{x^2}{2!}+\frac{x^4}{4!}-\cdots+(-1)^n\frac{x^{2n}}{(2n)!}+o(x^{2n+1}) (-1)^{n+1}\frac{\cos(\theta x)}{(2n+2)!}x^{2n+2}
\ln (1+x) x-\frac{x^2}2+\frac{x^3}3-\frac{x^4}4+\cdots+(-1)^{n-1}\frac{x^n}n+o(x^n) (-1)^n\frac{x^{n+1}}{n(1+\theta x)^{n+1}}
(1+x)^a \left(\begin{matrix}a\\0\end{matrix}\right)+\left(\begin{matrix}a\\1\end{matrix}\right)x+\left(\begin{matrix}a\\2\end{matrix}\right)x^2+\left(\begin{matrix}a\\3\end{matrix}\right)x^3+\cdots +\left(\begin{matrix}a\\n\end{matrix}\right)x^n+o(x^n) \left(\begin{matrix}a\\n+1\end{matrix}\right)(1+\theta x)^{a-n-1}x^{n+1}
\frac1{1+x} 1-x+x^2-x^3+\cdots +(-1)^nx^n+o(x^n) (-1)^{n+1}\frac{x^{n+1}}{(1+\theta x)^{n+2}}
\sqrt{1+x} 1+\frac12x-\frac1{2\cdot 4}x^2+\frac{1\cdot 3}{2\cdot 4\cdot 6}x^3-\cdots +(-1)^{n-1}\frac{(2n-3)!!}{(2n)!!}x^n+o(x^n) (-1)^{n+1}\frac{(2n-1)!!x^{n+1}}{(2n+2)!!(1+\theta x^{n+\frac12})}

不定积分

定积分

直角坐标显式方程 y=f(x),x\in[a,b] 直角坐标参数方程 \begin{cases}x=x(t)\,\\y=y(t)\,\end{cases}t\in[a,b] 极坐标方程 r=r(\theta),\theta\in[a,b]
平面图形面积 \int_a^bf(x)\text{d}x \int_a^b\vert y(t)x'(t)\vert\text{d}t \frac12\int_a^br^2(\theta)\text{d}\theta
曲线弧长 \int_a^b\sqrt{1+[f'(x)]^2} \int_a^b\sqrt{[x'(t)]^2+[y'(t)]^2}\text{d}t \int_a^b\sqrt{r^2(\theta)+[r'(\theta)]^2}\text{d}\theta
旋转体体积 \pi\int_a^b f^2(x)\text{d}x $\pi\int_a^by^2(t) x'(t)
旋转曲面面积 $2\pi\int_a^b f(x) \sqrt{1+[f'(x)]^2}\text{d}x$
某点曲率 $\dfrac{ f''(x) }{(1+[f'(x)]^2)^{3/2}}$

反常积分

常微分方程

类型 例子 通解
可分离变量 \frac{\text dy}{\text dx}=\varphi(x)\psi(y) \int\frac{\text dy}{\psi(y)}=\int\varphi(x)\text dx
齐次方程 \frac{\text dy}{\text dx}=g(\frac yx) (u=\frac yx,c\in\mathbb R)\int\frac{\text du}{g(u)-u}=\ln\vert cx\vert
一阶齐次线性方程 \frac{\text dy}{\text dx}=-p(x)y (c\in \mathbb R)y=ce^{-\int p(x)\text dx}
一阶线性方程 \frac{\text dy}{\text dx}=-p(x)y+f(x) (c\in \mathbb R)y=e^{-\int p(x)\text dx}(c+\int f(x)e^{\int p(x)\text dx}\text dx)
伯努利方程 \frac{\text dy}{\text dx}+p(x)y=f(x)y^n 等价于一阶线性方程 (z=y^{1-n})\frac{\text dz}{\text dx}+(1-n)p(x)z=(1-n)f(x) 的通解和可能的解 y=0
二阶常系数齐次线性方程 \frac{\text d^2y}{\text dx^2}+p\frac{\text dy}{\text dx}+qy=0 \begin{cases}c_1e^{\lambda_1x}+c_2e^{\lambda_2x}&\Delta>0&\lambda_1,\lambda_2=\frac{-p\pm \sqrt{\Delta}}{2}\\(c_1+c_2x)e^{\lambda x}&\Delta=0&\lambda=-\frac{p}2\\e^{\alpha x}(c_1\cos(\beta x)+c_2\sin(\beta x))&\Delta<0&\alpha=-\frac p2,\beta=\frac{\sqrt{-\Delta}}2\end{cases}

程序设计

typedef struct A
{
   int a,b; 
}B;//定义的 B 是类型 A 的别名,相当于 A
B a={2,3};
struct A
{
   int a,b; 
}B={2,3};//定义的 B 类型为 A 的结构体变量
char (*(*(*apt)[5])())[6];
typedef char CHA[6];
typedef CHA *CHAP;
typedef CHAP CHAPF();
typedef CHAPF *CHAPFP;
typedef CHAPFP CHAPFPA[5];
typedef CHAPFPA *CHAPFPAP; 
CHAPFPAP apt;
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int *a=(int*)malloc(sizeof(int)*114514);
    int *b=(int*)calloc(114514,sizeof(int));//内容会被清零
    a=realloc(a,sizeof(int)*114514);
    free(a),free(b);
}

free(a),a=NULL,它什么也不做。

#include <stdio.h>
#include <stdlib.h>
#include<assert.h>

int main() {
    FILE *fp1, *fp2;

    // 使用fopen打开文件
    fp1 = fopen("example.txt", "w"); // "w"模式表示写入,并在文件不存在时创建文件
    if (fp1 == NULL) {
        perror("Error opening file");
        return EXIT_FAILURE;
    }
    // 向文件写入数据
    fprintf(fp1, "This is a test.\n");
    fclose(fp1); // 记得关闭文件

    // 使用freopen打开文件
    // freopen会关闭FILE指针原来关联的文件(如果有的话),然后重新打开指定的文件
    fp2 = freopen("example.txt", "r", stdin); // 将stdin重定向到example.txt,"r"模式表示读取
    assert(fp2==stdin);
    if (fp2 == NULL) {
        perror("Error opening file");
        return EXIT_FAILURE;
    }
    // 从文件读取数据
    char buffer[100];
    while (fgets(buffer, sizeof(buffer), stdin)) { // 使用标准输入读取,实际上现在是从文件读取
        printf("%s", buffer); // 打印读取的内容
    }
    fclose(stdin); // 记得关闭文件

    return EXIT_SUCCESS;
}

在C语言中,文件的读写方式由打开文件时指定的模式决定。这些模式在调用fopen或freopen函数时作为第二个参数传入。以下是一些常用的文件打开模式及其含义:

  1. "r":只读模式。文件必须存在,不能用于创建新文件。

    • 例子:FILE *fp = fopen("file.txt", "r");
  2. "w":只写模式。文件会被截断为零长度,即清空文件内容,如果文件不存在则创建新文件。

    • 例子:FILE *fp = fopen("file.txt", "w");
  3. "a":追加模式。写入时会从文件末尾开始追加内容,如果文件不存在则创建新文件。

    • 例子:FILE *fp = fopen("file.txt", "a");
  4. "r+":读写模式。文件必须存在,用于读写。

    • 例子:FILE *fp = fopen("file.txt", "r+");
  5. "w+":读写模式。文件会被截断为零长度,即清空文件内容,如果文件不存在则创建新文件。

    • 例子:FILE *fp = fopen("file.txt", "w+");
  6. "a+":读写模式。写入时会从文件末尾开始追加内容,如果文件不存在则创建新文件。

    • 例子:FILE *fp = fopen("file.txt", "a+");
  7. "b":二进制模式。用于二进制文件的读写,可以与上述模式组合使用,如"rb"、"wb"、"ab"等。

    • 例子:FILE *fp = fopen("file.bin", "rb");
  8. "t":文本模式。这是默认模式,用于文本文件的读写,可以与上述模式组合使用,如"rt"、"wt"、"at"等。

  9. "e":与"t"相同,用于兼容某些编译器。

  10. "x":独占创建且写入模式。如果文件已存在,则fopen调用失败返回NULL。可以与"w"、"w+"、"a"、"a+"组合使用,如"wx"、"w+x"等。

  11. "N":无缓冲模式。用于与"r"、"w"、"a"、"r+"、"w+"、"a+"组合,如"rN"、"wN"等。

下面是如何指定文件打开模式的例子:

// 以只读模式打开文件
FILE *fp_read = fopen("example.txt", "r");

// 以只写模式打开文件,如果文件存在则清空内容
FILE *fp_write = fopen("example.txt", "w");

// 以追加模式打开文件,写入时会追加到文件末尾
FILE *fp_append = fopen("example.txt", "a");

// 以读写模式打开文件,文件必须存在
FILE *fp_read_write = fopen("example.txt", "r+");

// 以二进制模式打开文件
FILE *fp_binary = fopen("example.bin", "rb");

feof 是 C 语言标准库中的一个函数,用于检测文件流是否已经到达了文件末尾(EOF)。函数的原型如下:

int feof(FILE *stream);

参数 stream 是一个指向 FILE 结构的指针,该结构标识了流。当设置了与流关联的文件结束标识符时,feof 函数返回非零值,否则返回0。这意味着如果文件结束,则返回非0值,否则返回0。文件结束标识符只能被 clearerr() 函数清除。

feof 函数通常在读取文件时使用,用来判断是否已经读到文件的末尾。例如,在读取文件的循环中,可以在每次读取操作后检查 feof 以确定是否需要继续处理数据或终止循环。需要注意的是,feof 函数并不是在读取到文件末尾时立即返回非零值,而是在下一次读取操作尝试超过文件末尾时才设置文件结束标识符。因此,正确的使用方式是在读取操作之后检查 feof,而不是在读取操作之前。

这里是一个使用 feof 的简单示例:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Failed to open file");
        return 1;
    }
    int c;
    while ((c = fgetc(file)) != EOF) {
        putchar(c);
    }
    if (feof(file)) {
        printf("End of file reached.\n");
    } else {
        printf("Error reading file.\n");
    }
    fclose(file);
    return 0;
}

在这个例子中,程序会持续从文件中读取字符,直到遇到文件末尾。每次读取操作后,都会检查 feof 来确定是否到达文件末尾。如果到达文件末尾,feof 会返回非零值,程序将打印“End of file reached.”。

在C语言中,rewind、fseek和ftell函数都是用于操作文件流中的文件位置指针的,它们各自有不同的用途:

  1. rewind函数:

    • 函数原型:void rewind(FILE *stream);

    • rewind函数将指定文件流stream的文件位置指针重置到文件的开头。这相当于将文件位置指针移动到文件的起始位置,但不改变流的模式。这个函数经常在需要重新从头开始读取文件时使用。

    • 例子:

      FILE *fp = fopen("example.txt", "r");
      if (fp != NULL) {
          // 读取文件内容
          rewind(fp); // 将文件指针重置到文件开头
          // 再次读取文件内容
          fclose(fp);
      }
  2. fseek函数:

    • 函数原型:int fseek(FILE *stream, long offset, int whence);

    • fseek函数将指定文件流stream的文件位置指针移动到指定的offset位置。offset是相对于whence参数指定的位置的偏移量。

    • whence参数可以是以下值之一:

      • SEEK_SET:文件开始处(文件指针加上偏移量)。
      • SEEK_CUR:当前文件位置(当前文件指针加上偏移量)。
      • SEEK_END:文件末尾(文件大小加上偏移量,通常用于移动到文件末尾或在末尾之前的位置)。
    • 例子:

      FILE *fp = fopen("example.txt", "r");
      if (fp != NULL) {
          // 将文件指针移动到文件开头后100个字节的位置
          fseek(fp, 100, SEEK_SET);
          // 读取文件内容
          fclose(fp);
      }
  3. ftell函数:

    • 函数原型:long ftell(FILE *stream);

    • ftell函数返回指定文件流stream的当前文件位置指针的值,即从文件开头到当前位置的偏移量(以字节为单位)。这个值可以用于记录当前的文件位置,以便之后使用fseek函数回到这个位置。

    • 例子:

      FILE *fp = fopen("example.txt", "r");
      if (fp != NULL) {
          long position = ftell(fp); // 获取当前文件位置
          // 执行一些文件操作
          fseek(fp, position, SEEK_SET); // 返回到之前记录的位置
          fclose(fp);
      }

这三个函数在文件处理中非常有用,它们允许程序动态地在文件中移动位置指针,以便在需要时读取或写入文件的不同部分。

在 C 语言中,运算符的优先级决定了表达式中运算的顺序。以下是 C 语言中所有运算符的优先级列表,从最高到最低:

  1. 后缀运算符

    • 成员访问运算符 . 和 ->(访问成员)
    • 后置增量运算符 ++
    • 后置减量运算符 --
    • [](数组下标)
    • ()(函数调用)
  2. 一元运算符

    • ++(前置增量)
    • --(前置减量)
    • +(正号)
    • -(负号)
    • !(逻辑非)
    • ~(按位取反)
    • *(指针类型转换)
    • &(取地址)
    • sizeof(大小计算)
  3. 乘法和除法运算符

    • *(乘法)
    • /(除法)
    • %(取模)
  4. 加法和减法运算符

    • +(加法)
    • -(减法)
  5. 位移运算符

    • <<(左移)
    • >>(右移)
  6. 关系运算符

    • <(小于)
    • >(大于)
    • <=(小于等于)
    • >=(大于等于)
  7. 相等运算符

    • ==(等于)
    • !=(不等于)
  8. 位与运算符

    • &(位与)
  9. 位异或运算符

    • ^(位异或)
  10. 位或运算符

    • |(位或)
  11. 逻辑与运算符

    • &&(逻辑与)
  12. 逻辑或运算符

    • ||(逻辑或)
  13. 条件运算符

    • ?:(三元条件运算符)
  14. 赋值运算符

    • =(赋值)
    • +=(加赋值)
    • -=(减赋值)
    • *=(乘赋值)
    • /=(除赋值)
    • %=(取模赋值)
    • <<=(左移赋值)
    • >>=(右移赋值)
    • &=(位与赋值)
    • ^=(位异或赋值)
    • |=(位或赋值)
  15. 逗号运算符

    • ,(逗号)

请注意,当表达式中出现多个具有相同优先级的运算符时,它们将从左到右进行计算。此外,括号 () 可以用来改变运算的优先级,使得括号内的表达式优先计算。

在 C 语言中,数组指针和指针数组是两个不同的概念,它们在语法和使用上有明显的区别:

指针数组(Array of Pointers)

指针数组是指一个指针数组,即一个数组,其元素都是指针。这种类型的数组可以存储多个指针,每个指针指向不同的数据类型(通常是相同类型的数据)。

声明示例:

int* ptrArray[10]; // 一个包含10个int类型指针的数组

在这个例子中,ptrArray 是一个数组,它包含10个元素,每个元素都是一个指向 int 类型数据的指针。

数组指针(Pointer to an Array)

数组指针实际上是指一个指向数组的指针。这是一个指针,它指向一个数组。

声明示例:

int (*arrayPtr)[10]; // 一个指向包含10个int类型元素的数组的指针

在这个例子中,arrayPtr 是一个指针,它指向一个包含10个 int 类型元素的数组。

区别

示例用法:

// 数组指针的使用
int* ptrArray[10];
for (int i = 0; i < 10; i++) {
    ptrArray[i] = &someInts[i]; // 假设someInts是一个int数组
}

// 指针数组的使用
int array[10];
int (*arrayPtr)[10] = &array; // arrayPtr指向array数组

在实际编程中,选择使用数组指针还是指针数组取决于具体的应用场景和需求。