├── .github └── workflows │ └── deploy.yml ├── .gitignore ├── .vscode └── extensions.json ├── LICENSE ├── README.md ├── cache.ts ├── docs ├── 404.md ├── config.yml ├── cs │ ├── ads │ │ ├── amortized-analysis.md │ │ ├── assets │ │ │ ├── b-plus-tree-example.png │ │ │ ├── binomial-queue-example.png │ │ │ └── red-black-tree-example.svg │ │ ├── avl-tree.md │ │ ├── b-plus-tree.md │ │ ├── binomial-queue.md │ │ ├── index.md │ │ ├── leftist-heap.md │ │ ├── red-black-tree.md │ │ ├── skew-heap.md │ │ └── splay-tree.md │ ├── db │ │ ├── assets │ │ │ └── relational-model.structure.png │ │ ├── index.md │ │ ├── introduction-to-sql.md │ │ └── relational-model.md │ ├── index.md │ ├── others │ │ ├── c-language-cheatsheet.md │ │ ├── index.md │ │ ├── manuals-standards.md │ │ └── mit-missing-semester.md │ └── quantum │ │ ├── assets │ │ ├── classic-logic-gates-and-circuits.cnot-circuit.svg │ │ ├── index.cover.jpg │ │ ├── spin-and-qubits.rotate.svg │ │ ├── spin-and-qubits.spin-angle.svg │ │ ├── spin.spin-1.svg │ │ ├── spin.spin-2.svg │ │ ├── spin.spin-3.svg │ │ ├── spin.spin-4.svg │ │ ├── spin.spin-angle.svg │ │ └── spin.stern-gerlach-experiment.svg │ │ ├── bells-inequality.md │ │ ├── classic-logic-gates-and-circuits.md │ │ ├── entanglement.md │ │ ├── index.md │ │ ├── linear-algebra.md │ │ ├── quantum-gates-and-circuits.md │ │ ├── spin-and-qubits.md │ │ └── spin.md ├── ctf │ ├── crypto │ │ ├── aes.md │ │ ├── assets │ │ │ ├── aes-cbc.png │ │ │ ├── aes-cfb.png │ │ │ ├── aes-ctr.png │ │ │ ├── aes-decrypt.png │ │ │ ├── aes-ecb.png │ │ │ ├── aes-encrypt.png │ │ │ ├── aes-ofb.png │ │ │ └── ecc-addition.png │ │ ├── basis.md │ │ ├── diffie-hellman.md │ │ ├── ecc.md │ │ ├── index.md │ │ └── rsa.md │ ├── game │ │ ├── assets │ │ │ ├── zjuctf2024-es.jpg │ │ │ └── zjuctf2024-silence.png │ │ ├── index.md │ │ └── zjuctf2024.md │ └── index.md ├── index.md ├── misc │ ├── index.md │ └── test │ │ ├── customToken.md │ │ ├── index.md │ │ ├── latex.md │ │ └── markdown.md └── sci │ ├── index.md │ ├── la │ ├── index.md │ ├── prove-that.md │ └── what-is.md │ ├── ma │ ├── cheatsheet.md │ ├── completeness-of-real-numbers.md │ ├── index.md │ └── prove-that.md │ ├── phy │ ├── chapter-27.md │ ├── chapter-28.md │ ├── chapter-29-30.md │ └── index.md │ └── prob │ ├── chapter-1.md │ ├── chapter-2.md │ ├── chapter-3.md │ ├── distribution.md │ └── index.md ├── env.d.ts ├── index.html ├── package-lock.json ├── package.json ├── public ├── assets │ ├── line │ └── silence ├── favicon.ico └── robots.txt ├── src ├── App.vue ├── assets │ ├── css │ │ ├── cursor.styl │ │ ├── global.styl │ │ ├── libertinus-serif.css │ │ ├── main.styl │ │ ├── markdown.styl │ │ ├── prism-one-dark.css │ │ ├── prism-one-light.css │ │ ├── reset.styl │ │ └── twikoo.css │ ├── font │ │ ├── LibertinusSerif-Bold.woff │ │ ├── LibertinusSerif-BoldItalic.woff │ │ ├── LibertinusSerif-Italic.woff │ │ ├── LibertinusSerif-Regular.woff │ │ ├── LibertinusSerif-Semibold.woff │ │ ├── LibertinusSerif-SemiboldItalic.woff │ │ └── LibertinusSerifDisplay-Regular.woff │ ├── img │ │ ├── quote-left.svg │ │ ├── quote-right.svg │ │ └── waving.png │ └── ts │ │ ├── cursor.ts │ │ ├── dayjs.ts │ │ ├── fontawesome.ts │ │ ├── latex.ts │ │ ├── leftbar.tsx │ │ ├── markdown.ts │ │ ├── rightbar.ts │ │ ├── search.ts │ │ ├── types.ts │ │ └── utils.ts ├── components │ ├── Comment.vue │ ├── Content.vue │ ├── Footer.vue │ ├── LeftBar.vue │ ├── LinkTo.vue │ ├── Logo.vue │ ├── Metadata.vue │ ├── RightBar.vue │ ├── RightBarDetail.vue │ ├── Search.vue │ ├── Timestamp.vue │ └── md │ │ ├── Anchor.vue │ │ ├── BlockCode.vue │ │ ├── BlockMath.vue │ │ ├── Delimiter.vue │ │ ├── DotPattern.vue │ │ ├── Fold.vue │ │ ├── Grid.vue │ │ ├── Heading.vue │ │ ├── ImageCaptioned.vue │ │ ├── Index.vue │ │ ├── InlineMath.vue │ │ ├── LinkCard.vue │ │ ├── Note.vue │ │ ├── Tab.vue │ │ └── Waterfall.vue ├── main.ts └── router │ └── index.ts ├── todo.md ├── tsconfig.app.json ├── tsconfig.json ├── tsconfig.node.json ├── vercel.json └── vite.config.ts /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Build and Deploy to Vercel 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: 7 | - main 8 | 9 | jobs: 10 | build-and-deploy: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - name: Checkout App Repository 15 | uses: actions/checkout@v3 16 | with: 17 | fetch-depth: 0 18 | 19 | - name: Checkout Plugin Repository 20 | uses: actions/checkout@v3 21 | with: 22 | repository: "Xecades/vite-plugin-vue-xecades-note" 23 | path: plugin 24 | fetch-depth: 0 25 | 26 | - name: Setup Node.js 27 | uses: actions/setup-node@v3 28 | with: 29 | node-version: 20 30 | 31 | - name: Install Dependencies in app and plugin 32 | run: npm ci && npm --prefix ./plugin ci 33 | 34 | - name: Build Plugin 35 | run: npm --prefix ./plugin run build 36 | 37 | - name: Install Plugin 38 | run: npm install ./plugin 39 | 40 | - name: Generate Cache 41 | run: npm run cache 42 | 43 | - name: Build Project 44 | run: npm run build 45 | 46 | - name: Copy vercel.json 47 | run: cp vercel.json ./dist 48 | 49 | - name: Deploy to Vercel 50 | uses: amondnet/vercel-action@v25 51 | with: 52 | vercel-token: ${{ secrets.VERCEL_TOKEN }} 53 | vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} 54 | vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} 55 | working-directory: ./dist 56 | vercel-args: "--prod" 57 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | .DS_Store 12 | dist 13 | dist-ssr 14 | coverage 15 | *.local 16 | 17 | /cypress/videos/ 18 | /cypress/screenshots/ 19 | 20 | # Editor directories and files 21 | .vscode/* 22 | !.vscode/extensions.json 23 | .idea 24 | *.suo 25 | *.ntvs* 26 | *.njsproj 27 | *.sln 28 | *.sw? 29 | 30 | *.tsbuildinfo 31 | .vercel 32 | /cache 33 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": ["Vue.volar"] 3 | } 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alpha :: Note \[α\] 2 | 3 | Blazing fast static site generator for notes, equipped with [powerful markdown features](https://note.xecades.xyz/misc/test/customToken). View the live site at [note.xecades.xyz](https://note.xecades.xyz/). 4 | 5 | --- 6 | 7 | **Note** is part of the **Alpha α** project. 8 | 9 | - **Homepage**: [xecades.xyz](https://xecades.xyz/) ([Xecades/Homepage](https://github.com/Xecades/Homepage)) 10 | - **Note**: [note.xecades.xyz](https://note.xecades.xyz/) (This Repo) 11 | - **Blog**: [blog.xecades.xyz](https://blog.xecades.xyz/) (Private Repo) (*WIP*) 12 | 13 | \* This project relies on the [vite-plugin-vue-xecades-note](https://github.com/Xecades/vite-plugin-vue-xecades-note) plugin. Run `npm link` before running the project. 14 | 15 | --- 16 | 17 | - **Tech stack**: Vite, Vue.js, TypeScript 18 | - **License**: GPL-3.0 19 | -------------------------------------------------------------------------------- /cache.ts: -------------------------------------------------------------------------------- 1 | import { launch } from "vite-plugin-vue-xecades-note"; 2 | 3 | launch({ 4 | componentDir: "src/components/md", 5 | pluginName: "vite-plugin-vue-xecades-note", 6 | }); 7 | -------------------------------------------------------------------------------- /docs/404.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 404 Not Found 3 | comment: false 4 | timestamp: false 5 | --- 6 | 7 | ::dot-pattern 8 | 9 | 「你从哪里来,要到哪里去」 10 | 11 | :::span{style="font-size: 0.9em"} 12 | 你可以在左上角搜索,或者点击[这里](/)返回主页。 13 | ::: 14 | 15 | :: 16 | -------------------------------------------------------------------------------- /docs/config.yml: -------------------------------------------------------------------------------- 1 | nav: 2 | - cs: 3 | - db: 4 | - relational-model 5 | - introduction-to-sql 6 | - quantum: 7 | - spin 8 | - linear-algebra 9 | - spin-and-qubits 10 | - entanglement 11 | - bells-inequality 12 | - classic-logic-gates-and-circuits 13 | - quantum-gates-and-circuits 14 | - ads: 15 | - avl-tree 16 | - amortized-analysis 17 | - splay-tree 18 | - red-black-tree 19 | - b-plus-tree 20 | - leftist-heap 21 | - skew-heap 22 | - binomial-queue 23 | - others: 24 | - mit-missing-semester 25 | - c-language-cheatsheet 26 | - manuals-standards 27 | - ctf: 28 | - game: 29 | - zjuctf2024 30 | - crypto: 31 | - basis 32 | - aes 33 | - rsa 34 | - diffie-hellman 35 | - ecc 36 | - sci: 37 | - phy: 38 | - chapter-27 39 | - chapter-28 40 | - chapter-29-30 41 | - prob: 42 | - distribution 43 | - chapter-1 44 | - chapter-2 45 | - chapter-3 46 | - ma: 47 | - cheatsheet 48 | - prove-that 49 | - completeness-of-real-numbers 50 | - la: 51 | - prove-that 52 | - what-is 53 | - misc: 54 | - test: 55 | - customToken 56 | - latex 57 | - markdown 58 | 59 | icon: 60 | cs: code 61 | ctf: flag 62 | sci: atom 63 | misc: box-archive 64 | -------------------------------------------------------------------------------- /docs/cs/ads/assets/b-plus-tree-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/cs/ads/assets/b-plus-tree-example.png -------------------------------------------------------------------------------- /docs/cs/ads/assets/binomial-queue-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/cs/ads/assets/binomial-queue-example.png -------------------------------------------------------------------------------- /docs/cs/ads/b-plus-tree.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: B+ Tree 3 | --- 4 | 5 | $M$ 阶 B+ Tree 是一种多路搜索树,它的每个节点最多有 $M$ 个子节点,每个节点的子节点数目比它的键数多一个。 6 | 7 | B+ Tree 的所有数据都保存于叶子结点,非叶子节点只保存键值,用于索引。 8 | 9 | ::fold{title="$M$ 阶 B+ Tree" expand success} 10 | 1. 根节点要么是叶子节点,要么有 $2$ 到 $M$ 个子节点; 11 | 2. 所有非叶子结点(根节点除外),都有 $\lceil M/2 \rceil$ 到 $M$ 个子节点; 12 | 3. 所有叶子结点都有 $\lceil M/2 \rceil$ 到 $M$ 个数据。 13 | 4. 所有叶子结点都在同一层; 14 | :: 15 | 16 | $3$ 阶 B+ 树又称 **2-3 树**,$4$ 阶 B+ 树又称 **2-3-4 树**。 17 | 18 | ![一棵 2-3-4 树](assets/b-plus-tree-example.png) 19 | -------------------------------------------------------------------------------- /docs/cs/ads/binomial-queue.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Binomial Queue 3 | --- 4 | 5 | Binomial Queue 并非一棵树,而是一个由 Binomial Tree 组成的森林。其中 Binomial Tree 递归定义如下。 6 | 7 | ::fold{expand title="Binomial Tree" success} 8 | - A binomial tree of height $0$ is a one-node tree. 9 | - A binomial tree, $B_k$ of height $k$, is formed by attaching a binomial tree, $B_{k–1}$, to the root of another binomial tree, $B_{k–1}$. 10 | :: 11 | 12 | ![](assets/binomial-queue-example.png) 13 | 14 | 显然,$B_k$ 高度为 $k$,有 $2^k$ 个节点,每一层的节点数满足二项式系数,且其根节点有 $k$ 个子树,分别是 $B_0, B_1, \ldots, B_{k-1}$。 15 | 16 | Binomial Queue 类似于 size 的二进制表示,其中每一位对应一个 Binomial Tree。例如,$13 = 1101_2$,对应的 Binomial Queue 为 $B_3, B_2, B_0$。在进行 Merge 时,也就是普通的二进制加法。 17 | 18 | 在具体实现中,可用 Left-Child-Next-Sibling 表示法。 19 | -------------------------------------------------------------------------------- /docs/cs/ads/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 高级数据结构和算法分析 3 | --- 4 | 5 | ::fold{always expand info title="摘要" .fold-fontawesome-list} 6 |  :pen: 浙江大学《高级数据结构和算法分析》课程笔记 7 | 8 |  :clock: **修读时间**:2024 年秋冬学期 9 | 10 |  :triangle-exclamation: **注意事项**:本课程笔记很不完善,只记录了前面几次课的内容. 11 | :: 12 | 13 | :asterisk 14 | 15 | :index 16 | -------------------------------------------------------------------------------- /docs/cs/ads/leftist-heap.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Leftist Heap 3 | --- 4 | 5 | 首先有如下定义。 6 | 7 | ::fold{expand title="Null Path Length - $\text{Npl}(x)$"} 8 | **Null Path Length**:任意节点 $x$ 到最近的没有两个子节点的节点(即至多有一个孩子)的距离。规定 $\text{Npl}(\text{NULL}) = -1$。 9 | :: 10 | 11 | 显然可以得到如下递推式。 12 | 13 | $$ 14 | \text{Npl}(x) = \min\{\text{Npl}(\text{left}(x)), \text{Npl}(\text{right}(x))\} + 1 15 | $$ 16 | 17 | ::fold{expand title="Leftist Heap" success} 18 | 在堆的基础上,满足对于任意节点 $x$,左儿子的 $\text{Npl}$ 大于等于右儿子的 $\text{Npl}$。 19 | :: 20 | 21 | 据此可以通过归纳法得到如下定理。 22 | 23 | > A leftist heap of $r$ nodes on the right path must have at least $2^r - 1$ nodes. 24 | 25 | 该定理保证了 Right Path 长度的上界为 $O(\log n)$,Leftist Heap 正是利用此特性,让 Merge 操作永远发生于 Right Path 上,从而保证了 $O(\log n)$ 的复杂度。 26 | 27 | --- 28 | 29 | ## Merge & Insert 30 | 31 | 插入本质上是特殊的 Merge,因此我们只需要考虑 Merge 操作即可。 32 | 33 | 思路是先将两条 Right Path 合并按键值排序,然后保留左子树不变拼接成新的树,最后在 Right Path 上根据 $\text{Npl}$ 交换左右子树。 34 | 35 | --- 36 | 37 | ## Delete-Min 38 | 39 | 删除根节点后,将左右子树合并即可。 40 | -------------------------------------------------------------------------------- /docs/cs/ads/skew-heap.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Skew Heap 3 | --- 4 | 5 | Skew Heap 之于 Leftist Heap,就如同 Splay Tree 之于 AVL Tree。为了判断是否需要交换左右子树,Leftist Heap 给每个节点附加了一个 $\text{Npl}$ 值,这是 Leftist Heap 带来的额外内存开销。 6 | 7 | Skew Heap 则不再需要 $\text{Npl}$ 值,而是选择在 Merge 时,**永远进行左右子树的交换**。 8 | 9 | 显然,这样的操作会导致树的形态变得不规则,甚至可能退化为一条链,但是实际上我们可以通过摊还分析证明,Skew Heap 的摊还复杂度是 $O(\log n)$。 10 | 11 | --- 12 | 13 | **摊还分析**:略。 -------------------------------------------------------------------------------- /docs/cs/db/assets/relational-model.structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/cs/db/assets/relational-model.structure.png -------------------------------------------------------------------------------- /docs/cs/db/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 数据库系统 3 | --- 4 | 5 | ::fold{always expand info title="摘要" .fold-fontawesome-list} 6 |  :pen: 浙江大学《数据库系统》课程笔记 7 | 8 |  :clock: **修读时间**:2025 年春夏学期 9 | 10 |  :book: **使用教材**:*Database System Concepts* - 7th Edition 11 | 12 |  :triangle-exclamation: **注意事项**:本笔记只是课堂内容的真子集,仅记录了一些我认为可能遗忘的知识点. 请以教材和课堂讲义为准. 13 | :: 14 | 15 | :asterisk 16 | 17 | :index 18 | -------------------------------------------------------------------------------- /docs/cs/db/introduction-to-sql.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.3 Introduction to SQL 3 | --- 4 | 5 | 一堆语法,没有值得记的. 6 | -------------------------------------------------------------------------------- /docs/cs/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 计算机科学 3 | --- 4 | 5 | :index 6 | -------------------------------------------------------------------------------- /docs/cs/others/c-language-cheatsheet.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: C Language Cheatsheet 3 | --- 4 | 5 | ## Format String 6 | 7 | The syntax of a format placeholder is 8 | 9 | ```c 10 | %[parameter][flags][width][.precision][length]type 11 | ``` 12 | 13 | \* Square brackets indicate optional parts of the placeholder. 14 | 15 | ### Type 16 | 17 | | Type | Description | 18 | | :--: | -- | 19 | | `%` | Prints `%`. | 20 | | `d` | Decimal `signed int`. | 21 | | `u` | Decimal `unsigned int`. | 22 | | `o` | Octal `unsigned int`. | 23 | | `x`, `X` | Hexadecimal `unsigned int`. `x` for lower-case and `X` for upper-case. | 24 | | `f` | Float-point types in fixed-point notation. | 25 | | `e`, `E` | Float-point types in exponential notation: `d.ddde±dd`. `e` for lower-case "e" and `E` for upper-case. | 26 | | `g`, `G` | Float-point types in either fixed-point or exponential notation, whichever is more appropriate for its magnitude. `g` for lower-case and `G` for upper-case. | 27 | | `a`, `A` | Float-point in hexadecimal notation, starting with `0x` or `0X`. `a` for lower-case and `A` for upper-case. | 28 | | `s` | `null`-terminated string. | 29 | | `c` | `char`. | 30 | | `p` | `void*` in an implementation-defined format. | 31 | | `n` | Print nothing, but writes the number of characters written so far into an integer pointer parameter. | 32 | 33 | Examples: 34 | 35 | ```c 36 | printf("%%"); // % 37 | printf("%d", 123); // 123 38 | printf("%d", -123); // -123 39 | printf("%u", 123); // 123 40 | printf("%u", -123); // 4294967173 41 | printf("%o", 123); // 173 42 | printf("%x", 123); // 7b 43 | printf("%X", 123); // 7B 44 | printf("%f", 123.456); // 123.456000 45 | printf("%e", 123.456); // 1.234560e+02 46 | printf("%E", 123.456); // 1.234560E+02 47 | printf("%g", 123.456); // 123.456 48 | printf("%G", 123.456); // 123.456 49 | printf("%g", 123456789.123); // 1.23457e+08 50 | printf("%G", 123456789.123); // 1.23457E+08 51 | printf("%a", 123.456); // 0x1.edd2f1a9fbe77p+6 52 | printf("%A", 123.456); // 0X1.EDD2F1A9FBE77P+6 53 | printf("%s", "Hello"); // Hello 54 | printf("%c", 'H'); // H 55 | 56 | int a = 123; 57 | printf("%p", &a); // 0x16d79b3d8 58 | 59 | int n; 60 | printf("Hello%n", &n); // Hello 61 | printf("%d", n); // 5 62 | ``` 63 | 64 | ### Others 65 | 66 | For further information, refer to [Wikipedia](https://en.wikipedia.org/wiki/Printf). 67 | 68 | Here are some common examples: 69 | 70 | ```c 71 | printf("[%5d]", 123); // [ 123] 72 | printf("[%-5d]", 123); // [123 ] 73 | printf("[%05d]", 123); // [00123] 74 | printf("[%+5d]", 123); // [ +123] 75 | printf("[%*d]", 5, 123); // [ 123] (dynamic width) 76 | printf("[%.1f]", 123.456); // [123.5] 77 | printf("[%10f]", 123.456); // [123.456000] (10 characters in total) 78 | printf("[%10.5f]", 123.456); // [ 123.45600] (5 precision, 10 characters) 79 | printf("[%010.5f]", 123.456); // [0123.45600] (5 precision, 10 characters) 80 | ``` -------------------------------------------------------------------------------- /docs/cs/others/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 其他 3 | --- 4 | 5 | ::fold{expand info title="摘要"} 6 | 一些学习过程中遇到的其他有意思的东西。 7 | :: 8 | 9 | :asterisk 10 | 11 | :index 12 | -------------------------------------------------------------------------------- /docs/cs/others/manuals-standards.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Manuals & Standards 3 | --- 4 | 5 | ## Semantic Versioning 6 | 7 | **Refer to**: [Semantic Versioning](https://semver.org/). 8 | 9 | Given a version number `MAJOR.MINOR.PATCH`, increment the: 10 | 11 | - `MAJOR` version when you make incompatible API changes. 12 | - `MINOR` version when you add functionality in a backward compatible manner. 13 | - `PATCH` version when you make backward compatible bug fixes. 14 | 15 | --- 16 | 17 | ## Conventional Commits 18 | 19 | **Refer to**: [Conventional Commits](https://www.conventionalcommits.org/) / [Angular Convention](https://github.com/angular/angular/blob/main/CONTRIBUTING.md#-commit-message-format). 20 | 21 | Structure of the commit message: 22 | 23 | ``` 24 | [optional scope]: 25 | 26 | [optional body] 27 | 28 | [optional footer(s)] 29 | ``` 30 | 31 | - **`fix`**: correlates with `PATCH` in SemVer. 32 | - **`feat`**: correlates with `MINOR` in SemVer. 33 | - **`BREAKING CHANGE`**: a commit that has a footer `BREAKING CHANGE`, or appends a `!` after the type/scope, correlating with `MAJOR` in SemVer. 34 | - **``**: `build`, `chore`, `ci`, `docs`, `style`, `refactor`, `perf`, `test`, etc. 35 | - **``**: 36 | - use the imperative, present tense: "change" not "changed" nor "changes". 37 | - don't capitalize the first letter. 38 | - no dot (`.`) at the end. 39 | 40 | Example: 41 | 42 | ``` 43 | chore!: drop support for Node 6 44 | 45 | BREAKING CHANGE: use JavaScript features not available in Node 6. 46 | ``` 47 | -------------------------------------------------------------------------------- /docs/cs/quantum/assets/index.cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/cs/quantum/assets/index.cover.jpg -------------------------------------------------------------------------------- /docs/cs/quantum/assets/spin-and-qubits.rotate.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | cos 102 | (ɑ) 103 | s 104 | in 105 | ( 106 | ɑ) 107 | 108 | x 109 | y 110 | 111 | ɑ 112 | a 113 | 1 114 | 1 115 | si 116 | n(ɑ) 117 | cos 118 | (ɑ) 119 | 120 | 121 | -------------------------------------------------------------------------------- /docs/cs/quantum/assets/spin-and-qubits.spin-angle.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | N 56 | S 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /docs/cs/quantum/assets/spin.spin-1.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | 20 | 21 | 22 | 23 | S 24 | N 25 | 26 | 27 | -------------------------------------------------------------------------------- /docs/cs/quantum/assets/spin.spin-2.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | N 40 | S 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/cs/quantum/assets/spin.spin-3.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | S 26 | N 27 | 28 | 29 | -------------------------------------------------------------------------------- /docs/cs/quantum/assets/spin.spin-4.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | S 40 | N 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/cs/quantum/assets/spin.spin-angle.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | N 56 | S 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /docs/cs/quantum/assets/spin.stern-gerlach-experiment.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | South 84 | 85 | North 86 | 87 | N 88 | S 89 | N 90 | S 91 | N 92 | S 93 | Source 94 | 95 | 96 | -------------------------------------------------------------------------------- /docs/cs/quantum/bells-inequality.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.5 Bell's Inequality 3 | --- 4 | 5 | 我们之前学习的理论都来自于量子力学的**哥本哈根诠释**(Copenhagen interpretation),但在量子力学的早期发展阶段,还有另一种解释——**隐变量理论**(Hidden variable theory). **贝尔**则通过一个巧妙的实验来区分这两种理论. 6 | 7 | --- 8 | 9 | ## 隐变量理论 10 | 11 | 隐变量理论认为,量子力学中的随机性是因为我们无法观测到系统的所有**隐变量**,而不是因为系统本身是随机的. 也就是说,如果我们能够观测到所有的隐变量,那么量子力学的预测就会变得确定. 12 | 13 | 我们先来看看隐变量理论如何解释量子纠缠. 14 | 15 | --- 16 | 17 | 考虑量子态为 $\frac{1}{\sqrt{2}}\lvert\uparrow\rangle\lvert\uparrow\rangle+\frac{1}{\sqrt{2}}\lvert\downarrow\rangle\lvert\downarrow\rangle$ 的两个电子,实验结果表明它们的测量结果一定相同. 在隐变量理论的解释中,这两个电子在产生纠缠的时候,就**已经确定了各自的自旋方向**. 18 | 19 | ::fold{expand info title=纸牌类比} 20 | 设想有一桌打乱的纸牌,我们从中随机选出一张,并且不看它,然后把它从中间撕成两半,分别寄给远在宇宙两端的 Alice 和 Bob. 21 | 22 | Alice 和 Bob 对收到的牌的类型一无所知,但一旦他们看到自己手中的牌,就能立刻知道对方手中的牌的类型. 在其中并没有什么超距作用,只是因为这两张牌在一开始就已经确定了. 23 | :: 24 | 25 | 隐变量理论认为,**测量的结果在测量之前已经确定了**;哥本哈根解释认为,**测量的结果是在测量时才确定的**. 这一问题即为赫赫有名的 **EPR 佯谬**(Einstein-Podolsky-Rosen paradox). 26 | 27 | --- 28 | 29 | ## 贝尔实验 30 | 31 | 设想我们生成一系列的电子对,每对电子**都处于 $\frac{1}{\sqrt{2}}\lvert\uparrow\rangle\lvert\uparrow\rangle+\frac{1}{\sqrt{2}}\lvert\downarrow\rangle\lvert\downarrow\rangle$ 的纠缠态**,每一对的电子都分别发给 Alice 和 Bob. 32 | 33 | Alice 在 $0^\circ$、$120^\circ$ 和 $240^\circ$ 中**等概率随机选择一个方向**,测量自己手中的电子的自旋并得到一个 0 或 1 的结果. 在 Alice 测量之后,Bob 也随机选择一个方向,并测量自己手中的电子的自旋. 34 | 35 | 所有电子对测量结束后,Alice 和 Bob 都得到了一个 01 串,然后我们一位一位地**比较两个 01 串的内容**,如果相同写下 A(Agree),不同写下 D(Disagree),最后我们**统计 A 的占比**. 36 | 37 | 理论分析表明,**两种解释对于 A 的占比会有不同的预测**. 38 | 39 | ### 哥本哈根解释的预测 40 | 41 | 首先,如果 Alice 和 Bob 都选到了相同的测量方向,也就是相同的**基**,那么他们的测量结果一定相同,接下来我们考虑基不同的情况. 42 | 43 | 例如,如果 Alice 选到了 $(\lvert\searrow\rangle, \lvert\nwarrow\rangle)$,Bob 选到了 $(\lvert\swarrow\rangle, \lvert\nearrow\rangle)$. Alice 先进行测量,因此需要将 $\frac{1}{\sqrt{2}}\lvert\uparrow\rangle\lvert\uparrow\rangle+\frac{1}{\sqrt{2}}\lvert\downarrow\rangle\lvert\downarrow\rangle$ 投影到 Alice 的基下. 首先我们有如下定理. 44 | 45 | ::fold{expand success title=定理} 46 | 对任意的基 $(\lvert b_0\rangle, \lvert b_1\rangle)$,有 47 | 48 | $$ 49 | \frac{1}{\sqrt{2}}\begin{bmatrix}1\\0\end{bmatrix}\otimes\begin{bmatrix}1\\0\end{bmatrix} + \frac{1}{\sqrt{2}}\begin{bmatrix}0\\1\end{bmatrix}\otimes\begin{bmatrix}0\\1\end{bmatrix} = \frac{1}{\sqrt{2}}\lvert b_0\rangle\lvert b_0\rangle+\frac{1}{\sqrt{2}}\lvert b_1\rangle\lvert b_1\rangle. 50 | $$ 51 | 52 | 易证. 53 | :: 54 | 55 | 因此原量子态**等于** $\frac{1}{\sqrt{2}}\lvert\searrow\rangle\lvert\searrow\rangle+\frac{1}{\sqrt{2}}\lvert\nwarrow\rangle\lvert\nwarrow\rangle$. 这说明 Alice 测量之后系统**等概率坍缩**到 $\lvert\searrow\rangle\lvert\searrow\rangle$ 或 $\lvert\nwarrow\rangle\lvert\nwarrow\rangle$,然后 Bob 开始测量. 56 | 57 | 当系统坍缩到 $\lvert\searrow\rangle\lvert\searrow\rangle$ 时,Bob 拿到的电子是 $\lvert\searrow\rangle$,而 $\lvert\searrow\rangle = \frac{1}{2}\lvert\swarrow\rangle + \frac{\sqrt{3}}{2}\lvert\nearrow\rangle$,因此 Bob 有 $1/4$ 的概率得到 0,有 $3/4$ 的概率得到 1,也就是说有 **$1/4$ 的概率**得到与 Alice 相同的结果. 同理,当系统坍缩到 $\lvert\nwarrow\rangle\lvert\nwarrow\rangle$ 时,Bob 有 **$1/4$ 的概率**得到相同的结果. 58 | 59 | 他们有 $1/3$ 的时间选到相同的基,这种情况下结果相同的概率是 $1$;有 $2/3$ 的时间选到不同的基,对应概率 $1/4$,因此 AD 串中 A 的比例应该为 60 | 61 | $$ 62 | \frac{1}{3}\times 1 + \frac{2}{3}\times \frac{1}{4} = \frac{1}{2}. 63 | $$ 64 | 65 | --- 66 | 67 | ### 隐变量理论的预测 68 | 69 | 因为**每个方向上**的测量结果在测量之前就已经确定了,所以系统一开始有 $2^3=8$ 种可能:000、001、010、011、100、101、110、111,每一位分别代表 $(\lvert\uparrow\rangle, \lvert\downarrow\rangle)$、$(\lvert\searrow\rangle, \lvert\nwarrow\rangle)$ 和 $(\lvert\swarrow\rangle, \lvert\nearrow\rangle)$ 的测量结果. 70 | 71 | 记 $(\lvert\uparrow\rangle, \lvert\downarrow\rangle)$ 为 $a$,$(\lvert\searrow\rangle, \lvert\nwarrow\rangle)$ 为 $b$,$(\lvert\swarrow\rangle, \lvert\nearrow\rangle)$ 为 $c$,用 $(a, b)$ 表示 Alice 在 $a$ 方向测量,Bob 在 $b$ 方向测量的结果. 下表给出了所有可能的结果. 72 | 73 | ```typst 每种初始情况下可能的测量结果 74 | #{ 75 | show table.cell.where(y: 1): text.with(weight: "bold") 76 | table( 77 | columns: 10, 78 | inset: 9pt, 79 | align: horizon, 80 | stroke: none, 81 | table.hline(y: 0), 82 | table.hline(y: 1), 83 | table.hline(y: 2, stroke: 2pt), 84 | table.hline(y: 10), 85 | table.vline(x: 1, stroke: 2pt), 86 | table.header([], table.cell(colspan: 9)[*Measurement Directions*]), 87 | [*Config.*], $bold((a,a))$, $bold((a,b))$, $bold((a,c))$, $bold((b,a))$, $bold((b,b))$, $bold((b,c))$, $bold((c,a))$, $bold((c,b))$, $bold((c,c))$, 88 | [*000*], [A], [A], [A], [A], [A], [A], [A], [A], [A], 89 | [*001*], [A], [A], [D], [A], [A], [D], [D], [D], [A], 90 | [*010*], [A], [D], [A], [D], [A], [D], [A], [D], [A], 91 | [*011*], [A], [D], [D], [D], [A], [A], [D], [A], [A], 92 | [*100*], [A], [D], [D], [D], [A], [A], [D], [A], [A], 93 | [*101*], [A], [D], [A], [D], [A], [D], [A], [D], [A], 94 | [*110*], [A], [A], [D], [A], [A], [D], [D], [D], [A], 95 | [*111*], [A], [A], [A], [A], [A], [A], [A], [A], [A], 96 | ) 97 | } 98 | ``` 99 | 100 | 此处我们**不知道 $8$ 种初始情况的概率分布**,但每一个初始情况中,Alice 和 Bob 选择某组特定的方向的概率是确定的,都是 $1/9$. 101 | 102 | 上表中,每一行至少有 $5$ 个 A,这说明每个初始情况至少有 $5/9$ 的概率会得到 A,因此**总的 A 的比例至少是 $5/9$**,这与哥本哈根解释的预测不同. 103 | 104 | --- 105 | 106 | ### 实验结果 107 | 108 | 关于该验证方法的首次实验在 1972 年进行,实验结果表明 A 和 D 出现的频率均等,但由于实验仍有一些未能排除的漏洞,说服力不足. 2015 年,Ronald Hansen 等人的实验成功排除所有漏洞,严格证明了**哥本哈根解释**的正确性. 109 | 110 | --- 111 | 112 | ## Ekert 密钥分发协议 113 | 114 | **Ekert 密钥分发协议**是一种基于量子纠缠的密钥分发协议,又称为 **E91 协议**,它(的某一个变种)的原理和贝尔实验类似. 115 | 116 | 和贝尔实验一样,Alice 和 Bob 分别得到一系列电子对,每对电子都处于 $\frac{1}{\sqrt{2}}\lvert\uparrow\rangle\lvert\uparrow\rangle+\frac{1}{\sqrt{2}}\lvert\downarrow\rangle\lvert\downarrow\rangle$ 的纠缠态,然后 Alice 和 Bob 在三个测量方向中等概率随机选择一个,测量自己的电子的自旋,并记录下选择的基. 117 | 118 | 完成 $3n$ 次测量后,Alice 和 Bob 通过公开的信道比较他们的基. 一共会有大约 $n$ 个基相同,**选择对应的测量结果作为密钥**. 119 | 120 | 然后 Alice 和 Bob 需要验证没有第三者监听,他们通过公开的信道**比对剩下的 $2n$ 个比特**. 假设 Eve 对密钥分发过程进行监听,那么她一定需要进行测量,这样就会**破坏电子对的纠缠态**. 从贝尔实验的计算结果得到,如果没有监听,在这 $2n$ 个比特中,**不相同的概率是 $1/4$**. 如果监听了,容易计算出**不相同的概率会变成 $3/8$**. 121 | 122 | 通过统计剩下的 $2n$ 个比特中**不同的比特数**,Alice 和 Bob 确认是否有监听. 123 | 124 | 该协议已经在实验室中通过纠缠态的光子成功实现. 125 | -------------------------------------------------------------------------------- /docs/cs/quantum/classic-logic-gates-and-circuits.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.6 Classic Logic, Gates, and Circuits 3 | --- 4 | 5 | 本章介绍了经典逻辑电路,并引入了**可逆逻辑门**的概念,然后在此基础上介绍了一种以**桌球碰撞**来实现逻辑运算的理想模型. 本文只介绍可逆逻辑门的部分:CNOT 门、Toffoli 门、Fredkin 门. 6 | 7 | --- 8 | 9 | ## 受控非门 CNOT 10 | 11 | **受控非门**(Controlled-NOT gate)接收两个输入比特,传出两个输出比特. 它的作用是:**当第一个比特是 1 时,将第二个比特取反;当第一个比特是 0 时,第二个比特保持不变**. 用函数的形式表达,可以写为 12 | 13 | $$ 14 | f(x, y) = (x, x \oplus y). 15 | $$ 16 | 17 | CNOT 门是**可逆的**,给定一组输出,可以唯一确定输入. 我们后面介绍的所有门都满足这个条件. 18 | 19 | 同时我们注意到,两个 CNOT 门的作用可以**相互抵消**,即 $(x, y) \mapsto (x, x \oplus y) \mapsto (x, x \oplus (x \oplus y)) = (x, y)$. 20 | 21 | 我们可以通过一个**扇出**(Fan-out,在本书中指的是将一个比特复制到多个比特,即下图的小黑点)和一个**异或门**来实现 CNOT 电路. 22 | 23 | ![](./assets/classic-logic-gates-and-circuits.cnot-circuit.svg){.inv width=400px} 24 | 25 | 在量子电路中,我们用如下记号表示. 26 | 27 | ```typst 28 | #import "@preview/quill:0.6.0": * 29 | #scale(180%, reflow: true, quantum-circuit( 30 | lstick($x$), 1, ctrl(1), 1, rstick($x$), [\ ], 31 | lstick($y$), 1, targ(), 1, rstick($x xor y$) 32 | )) 33 | ``` 34 | 35 | --- 36 | 37 | ## Toffoli 门 38 | 39 | **Toffoli 门**有三个输入和三个输出,**当前两个输入都是 1 时,将第三个输入取反**,因此又称为**CCNOT 门**(Controlled-Controlled-NOT gate). 它可以用函数表示为 40 | 41 | $$ 42 | T(x, y, z) = (x, y, (x \land y) \oplus z). 43 | $$ 44 | 45 | 和 CNOT 门一样,Toffoli 门也是可逆的,并且两个 Toffoli 门的作用可以相互抵消. 46 | 47 | 它用如下的记号表示. 48 | 49 | ```typst 50 | #import "@preview/quill:0.6.0": * 51 | #scale(180%, reflow: true, quantum-circuit( 52 | lstick($x$), 1, ctrl(1), 1, rstick($x$), [\ ], 53 | lstick($y$), 1, ctrl(1), 1, rstick($y$), [\ ], 54 | lstick($z$), 1, targ(), 1, rstick($(x and y) xor z$) 55 | )) 56 | ``` 57 | 58 | Toffoli 门是**通用门**,因为它可以实现 **NAND 门**和**扇出**. 59 | 60 | $$ 61 | \begin{align*} 62 | \text{NAND:}\quad & T(x, y, 1) = (x, y, (x \land y) \oplus 1) = (x, y, x \uparrow y), \\[0.7em] 63 | \text{扇出:}\quad & T(x, 1, 0) = (x, 1, (x \land 1) \oplus 0) = (x, 1, x). 64 | \end{align*} 65 | $$ 66 | 67 | --- 68 | 69 | ## Fredkin 门 70 | 71 | **Fredkin 门**有三个输入和三个输出,**当前第一个输入是 1 时,交换第二个和第三个输入**,对应函数 72 | 73 | $$ 74 | F(x, y, z) = (x, (\neg x \land y) \lor (x \land z), (\neg x \land z) \lor (x \land y)). 75 | $$ 76 | 77 | 或者更直观地写成 78 | 79 | $$ 80 | \begin{align*} 81 | F(0, y, z) &= (0, y, z), \\[0.7em] 82 | F(1, y, z) &= (1, z, y). 83 | \end{align*} 84 | $$ 85 | 86 | Fredkin 门也是自身的逆. 可用如下记号表示. 87 | 88 | ```typst 89 | #import "@preview/quill:0.6.0": * 90 | #scale(180%, reflow: true, quantum-circuit( 91 | lstick($x$), 1, ctrl(1), 1, rstick($x$), [\ ], 92 | lstick($y$), 1, swap(1), 1, rstick($(not x and y) or (x and z)$), [\ ], 93 | lstick($z$), 1, swap(0), 1, rstick($(not x and z) or (x and y)$) 94 | )) 95 | ``` 96 | 97 | Fredkin 门也是**通用门**,因为可以实现**与门**、**非门**和**扇出**. 98 | 99 | $$ 100 | \begin{align*} 101 | \text{与门:}\quad & F(x, y, 0) = (x, \neg x\land y, x\land y), \\[0.7em] 102 | \text{非门、扇出:}\quad & F(x, 0, 1) = (x, x, \neg x). 103 | \end{align*} 104 | $$ 105 | -------------------------------------------------------------------------------- /docs/cs/quantum/entanglement.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.4 Entanglement 3 | --- 4 | 5 | 本章简要介绍了**量子纠缠**(Entanglement)的数学模型,以及量子纠缠的表现形式. 在本章中,我们研究的对象是**两个孤立系统**之间的量子效应. 6 | 7 | --- 8 | 9 | ## 张量积 10 | 11 | 我们从**非量子纠缠**的情况引入. 12 | 13 | 假设 Alice 和 Bob **分别拥有一个量子比特**,Alice 的在 $(\lvert a_0\rangle, \lvert a_1\rangle)$ 基下的态为 $\lvert v\rangle = c_0\lvert a_0\rangle + c_1\lvert a_1\rangle$,Bob 的在 $(\lvert b_0\rangle, \lvert b_1\rangle)$ 基下的态为 $\lvert w\rangle = d_0\lvert b_0\rangle + d_1\lvert b_1\rangle$. 14 | 15 | 定义 $\lvert v\rangle$ 和 $\lvert w\rangle$ 的**张量积**(Tensor product)为 $\lvert v\rangle\otimes\lvert w\rangle$,也可以写成 $\lvert v\rangle\lvert w\rangle$. 它的运算**遵循乘法分配律**,即 16 | 17 | $$ 18 | \begin{align*} 19 | \lvert v\rangle\lvert w\rangle &= (c_0\lvert a_0\rangle + c_1\lvert a_1\rangle)(d_0\lvert b_0\rangle + d_1\lvert b_1\rangle) \\ 20 | &= c_0d_0\lvert a_0\rangle\lvert b_0\rangle + c_0d_1\lvert a_0\rangle\lvert b_1\rangle + c_1d_0\lvert a_1\rangle\lvert b_0\rangle + c_1d_1\lvert a_1\rangle\lvert b_1\rangle. 21 | \end{align*} 22 | $$ 23 | 24 | 在上式中,每一个张量积的第一项都属于 Alice,第二项都属于 Bob,我们定义张量积**不遵循交换律**,即 $\lvert v\rangle\lvert w\rangle \neq \lvert w\rangle\lvert v\rangle$. 25 | 26 | 可见 $\lvert v\rangle\lvert w\rangle$ 是 $\lvert a_0\rangle\lvert b_0\rangle$、$\lvert a_0\rangle\lvert b_1\rangle$、$\lvert a_1\rangle\lvert b_0\rangle$、$\lvert a_1\rangle\lvert b_1\rangle$ 的**线性组合**,它们分别对应着 Alice 和 Bob 测量之后的四种可能的态. 27 | 28 | $c_0d_0$ 为 $\lvert a_0\rangle\lvert b_0\rangle$ 的**概率振幅**,其平方 $c_0^2d_0^2$ 描述了两人测量之后,Alice 得到 $\lvert a_0\rangle$,Bob 得到 $\lvert b_0\rangle$ 的**概率**. 29 | 30 | 换一个记号,将 $\lvert v\rangle\lvert w\rangle$ 写成 31 | 32 | $$ 33 | \lvert v\rangle\lvert w\rangle = r\lvert a_0\rangle\lvert b_0\rangle + s\lvert a_0\rangle\lvert b_1\rangle + t\lvert a_1\rangle\lvert b_0\rangle + u\lvert a_1\rangle\lvert b_1\rangle, 34 | $$ 35 | 36 | 那么 **$r^2+s^2+t^2+u^2=1$**,并且 **$ru = st = c_0d_0c_1d_1$**. 37 | 38 | --- 39 | 40 | ## 量子纠缠 41 | 42 | 更进一步,我们不再要求 $ru = st$,**当 $ru \neq st$ 时,我们称 $\lvert v\rangle$ 和 $\lvert w\rangle$ 两个量子比特发生了量子纠缠**. 发生量子纠缠的比特无法写成 $\lvert v\rangle = c_0\lvert a_0\rangle + c_1\lvert a_1\rangle$ 的形式,也即**张量积无法分解**. 43 | 44 | --- 45 | 46 | 假设 Alice 和 Bob 的量子比特的张量积为 47 | 48 | $$ 49 | \frac{1}{2}\lvert a_0\rangle\lvert b_0\rangle + \frac{1}{2}\lvert a_0\rangle\lvert b_1\rangle + \frac{1}{\sqrt{2}}\lvert a_1\rangle\lvert b_0\rangle + 0\lvert a_1\rangle\lvert b_1\rangle. 50 | $$ 51 | 52 | **外侧系数乘积**为 $1/2\times0=0$,**内侧系数乘积**为 $1/2\times1/\sqrt{2}\neq0$,两者不相等,因此它们发生了量子纠缠. 53 | 54 | 该式同时说明,当两人进行测量时,有 $1/4$ 的概率得到 00,有 $1/4$ 的概率得到 01,有 $1/2$ 的概率得到 10,不可能得到 11. 55 | 56 | 现在考虑这样的情况,**Alice 先进行测量**,而 Bob 不进行测量. 我们可以把张量积改写为 57 | 58 | $$ 59 | \lvert a_0\rangle\left(\frac{1}{2}\lvert b_0\rangle + \frac{1}{2}\lvert b_1\rangle\right) + \lvert a_1\rangle\left(\frac{1}{\sqrt{2}}\lvert b_0\rangle + 0\lvert b_1\rangle\right). 60 | $$ 61 | 62 | 为了**保持括号内部为单位向量**,提取系数,得到 63 | 64 | $$ 65 | \frac{1}{\sqrt{2}}\lvert a_0\rangle\left(\frac{1}{\sqrt{2}}\lvert b_0\rangle + \frac{1}{\sqrt{2}}\lvert b_1\rangle\right) + \frac{1}{\sqrt{2}}\lvert a_1\rangle\left(1\lvert b_0\rangle + 0\lvert b_1\rangle\right). 66 | $$ 67 | 68 | 这说明,当 Alice 测量时,她有均等的概率得到 0 或 1,如果得到 0,她的量子比特坍缩到 $\lvert a_0\rangle$,**整个系统的张量积坍缩到 $\lvert a_0\rangle\left(\frac{1}{\sqrt{2}}\lvert b_0\rangle + \frac{1}{\sqrt{2}}\lvert b_1\rangle\right)$**,这个式子可以因式分解,说明 Bob 的量子比特同时坍缩到了 $\frac{1}{\sqrt{2}}\lvert b_0\rangle + \frac{1}{\sqrt{2}}\lvert b_1\rangle$,和 Alice **不再发生纠缠**. 69 | 70 | 如果 Alice 得到 1,那么 Bob 的量子比特会坍缩到 $\lvert b_0\rangle$,也就是说,一旦 Alice 观测到 1,Bob 一定会观测到 0,这个现象是瞬时的,**不受光速限制**. 71 | 72 | --- 73 | 74 | ## 超光速通信 75 | 76 | 发生纠缠的量子比特确实**不受距离的限制**,即使 Alice 和 Bob 在宇宙的两头,Alice 对自己的量子比特进行测量,Bob 的量子比特也会瞬间坍缩到相应的态. 这**看似违背爱因斯坦的相对论,实则不然**. 77 | 78 | 我们回到刚刚研究的例子,Alice 和 Bob 的量子比特的张量积为 79 | 80 | $$ 81 | \frac{1}{2}\lvert a_0\rangle\lvert b_0\rangle + \frac{1}{2}\lvert a_0\rangle\lvert b_1\rangle + \frac{1}{\sqrt{2}}\lvert a_1\rangle\lvert b_0\rangle + 0\lvert a_1\rangle\lvert b_1\rangle. 82 | $$ 83 | 84 | 我们已经知道,如果 Alice 先进行测量,她有 $1/2$ 的概率得到 0,有 $1/2$ 的概率得到 1. 我们来看看**如果 Bob 抢先进行了测量**,Alice 后测量,她的结果会是什么. 85 | 86 | 在这种情况下,两人都就行了测量,可以直接**将系数平方**计算概率,即两人有 $1/4$ 的概率得到 00,有 $1/4$ 的概率得到 01,有 $1/2$ 的概率得到 10,不可能得到 11. 也就是说,Alice 有 $1/4+1/4=1/2$ 的概率得到 0,有 $1/2+0=1/2$ 的概率得到 1,**这和只有 Alice 测量的情况是一样的**. 这个结果对任意的量子纠缠态都成立. 87 | 88 | 这说明,Alice 和 Bob **没有办法区分是谁先进行了测量**,**无法传递信息**,自然也就无法进行超光速通信. 89 | 90 | --- 91 | 92 | ## 张量积的标准基 93 | 94 | $\mathbb{R}^2$ 的标准基是 $\left(\begin{bmatrix}1\\0\end{bmatrix}, \begin{bmatrix}0\\1\end{bmatrix}\right)$,当 Alice 和 Bob 使用标准基时,他们的量子比特的张量积为 95 | 96 | $$ 97 | r\begin{bmatrix}1\\0\end{bmatrix} \otimes \begin{bmatrix}1\\0\end{bmatrix} + s\begin{bmatrix}1\\0\end{bmatrix} \otimes \begin{bmatrix}0\\1\end{bmatrix} + t\begin{bmatrix}0\\1\end{bmatrix} \otimes \begin{bmatrix}1\\0\end{bmatrix} + u\begin{bmatrix}0\\1\end{bmatrix} \otimes \begin{bmatrix}0\\1\end{bmatrix}. 98 | $$ 99 | 100 | 因此,$\mathbb{R}^2\otimes\mathbb{R}^2$ 的标准基为 101 | 102 | $$ 103 | \left(\begin{bmatrix}1\\0\end{bmatrix} \otimes \begin{bmatrix}1\\0\end{bmatrix}, \begin{bmatrix}1\\0\end{bmatrix} \otimes \begin{bmatrix}0\\1\end{bmatrix}, \begin{bmatrix}0\\1\end{bmatrix} \otimes \begin{bmatrix}1\\0\end{bmatrix}, \begin{bmatrix}0\\1\end{bmatrix} \otimes \begin{bmatrix}0\\1\end{bmatrix}\right). 104 | $$ 105 | 106 | 这是一个四维空间,为了简化,我们**将其用 $\mathbb{R}^4$ 的标准基表示**. 107 | 108 | $$ 109 | \begin{matrix*} 110 | \begin{bmatrix}1\\0\\0\\0\end{bmatrix} = \begin{bmatrix}1\\0\end{bmatrix} \otimes \begin{bmatrix}1\\0\end{bmatrix}, & 111 | \begin{bmatrix}0\\1\\0\\0\end{bmatrix} = \begin{bmatrix}1\\0\end{bmatrix} \otimes \begin{bmatrix}0\\1\end{bmatrix}, \\[3em] 112 | \begin{bmatrix}0\\0\\1\\0\end{bmatrix} = \begin{bmatrix}0\\1\end{bmatrix} \otimes \begin{bmatrix}1\\0\end{bmatrix}, & 113 | \begin{bmatrix}0\\0\\0\\1\end{bmatrix} = \begin{bmatrix}0\\1\end{bmatrix} \otimes \begin{bmatrix}0\\1\end{bmatrix}. 114 | \end{matrix*} 115 | $$ 116 | 117 | 为了方便记忆,可以将其理解为 118 | 119 | $$ 120 | \begin{bmatrix}a_0\\a_1\end{bmatrix} \otimes \begin{bmatrix}b_0\\b_1\end{bmatrix} = \begin{bmatrix}a_0\begin{bmatrix}b_0\\b_1\end{bmatrix}\\[1.5em] a_1\begin{bmatrix}b_0\\b_1\end{bmatrix}\end{bmatrix} = \begin{bmatrix}a_0b_0\\a_0b_1\\a_1b_0\\a_1b_1\end{bmatrix}. 121 | $$ 122 | 123 | --- 124 | 125 | ## CNOT 门 126 | 127 | **CNOT 门**可用于产生量子纠缠. 它的矩阵表示为 128 | 129 | $$ 130 | \begin{bmatrix} 131 | 1 & 0 & 0 & 0 \\ 132 | 0 & 1 & 0 & 0 \\ 133 | 0 & 0 & 0 & 1 \\ 134 | 0 & 0 & 1 & 0 135 | \end{bmatrix}. 136 | $$ 137 | 138 | 假设有一对**非纠缠**的量子比特,它们的张量积为 139 | 140 | $$ 141 | \frac{1}{\sqrt{2}}\begin{bmatrix}1\\1\end{bmatrix}\otimes\begin{bmatrix}1\\0\end{bmatrix} = \frac{1}{\sqrt{2}}\begin{bmatrix}1\\0\\1\\0\end{bmatrix}. 142 | $$ 143 | 144 | 将它们发送到 CNOT 门,会使得张量积**左乘 CNOT 门的矩阵**,得到 145 | 146 | $$ 147 | \frac{1}{\sqrt{2}}\begin{bmatrix} 148 | 1 & 0 & 0 & 0 \\ 149 | 0 & 1 & 0 & 0 \\ 150 | 0 & 0 & 0 & 1 \\ 151 | 0 & 0 & 1 & 0 152 | \end{bmatrix}\begin{bmatrix}1\\0\\1\\0\end{bmatrix} = \frac{1}{\sqrt{2}}\begin{bmatrix}1\\0\\0\\1\end{bmatrix}. 153 | $$ 154 | 155 | 得到的张量积外层乘积不等于内层,因此发生了**量子纠缠**. 它可以被写成 156 | 157 | $$ 158 | \frac{1}{\sqrt{2}}\begin{bmatrix}1\\0\end{bmatrix}\otimes\begin{bmatrix}1\\0\end{bmatrix} + \frac{1}{\sqrt{2}}\begin{bmatrix}0\\1\end{bmatrix}\otimes\begin{bmatrix}0\\1\end{bmatrix}. 159 | $$ 160 | 161 | 当 Alice 和 Bob 进行测量时,他们**要么同时得到 0,要么同时得到 1**,这个张量积将在以后经常出现. 162 | -------------------------------------------------------------------------------- /docs/cs/quantum/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 量子计算 3 | --- 4 | 5 | ::fold{always expand info title="摘要"} 6 | ![*Quantum Computing for Everyone*](./assets/index.cover.jpg) 7 | 8 |  :book: *Quantum Computing for Everyone* 书籍阅读笔记 9 | 10 |  :clock: **开始时间**:2025 年 2 月 9 日 11 | 12 |  :clock: **结束时间**:2025 年 2 月 17 日 13 | 14 |  :pencil: **书籍评价**:本书十分入门,很多方面进行了简化,只需要高中数学水平即可理解,适合非计算机专业学生阅读,计算机专业的学生一周之内看完绰绰有余,可以茶余饭后读一读,**不建议将其视为专业课程的自学教材**. 15 | 16 |  :triangle-exclamation: **注意事项**:笔者只记录了前 7 章的笔记,由于剩下两章 Quantum Algorithms 和 Impact of Quantum Computing 只是简单介绍了一下量子算法,过于简单,没有必要记录笔记. 17 | :: 18 | 19 | :asterisk 20 | 21 | :index 22 | -------------------------------------------------------------------------------- /docs/cs/quantum/linear-algebra.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.2 Linear Algebra 3 | --- 4 | 5 | 量子用到的线性代数和传统线性代数有一些符号上的不同,我们主要使用 Paul Dirac 的符号体系. 除此之外,在科研领域,主要采用**复数域**来进行计算,而本书为了便于读者理解,主要使用**实数域**. 本文只介绍量子计算中用到的和常规线性代数有所不同的部分. 6 | 7 | --- 8 | 9 | ## Bra-Ket 记号 10 | 11 | 在 Dirac 符号体系中,我们称行向量为 **Bra**,记作 $\langle \cdot \rvert$,列向量为 **Ket**,记作 $\lvert \cdot \rangle$,例如 12 | 13 | $$ 14 | \begin{align*} 15 | \text{行向量 Bra } \langle v\rvert &= \begin{bmatrix} 1 & \sqrt{2} & -\pi & 3\end{bmatrix}, \\[1em] 16 | \text{列向量 Ket } \lvert w\rangle &= \begin{bmatrix} 1 \\ 0 \\ -3\end{bmatrix}. 17 | \end{align*} 18 | $$ 19 | 20 | 向量的元素个数称为向量的**维度**,相同维数的 Bra 和 Ket 可以进行**内积**,例如对于 $n$ 维 Bra $\langle a\rvert= \begin{bmatrix} a_1 & a_2 & \cdots & a_n \end{bmatrix}$ 和 $n$ 维 Ket $\lvert b\rangle = \begin{bmatrix} b_1 & b_2 & \cdots & b_n \end{bmatrix}^\top$,它们的内积为 21 | 22 | $$ 23 | \langle a|b\rangle = \begin{bmatrix} a_1 & a_2 & \cdots & a_n \end{bmatrix} \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{bmatrix} = a_1 b_1 + a_2 b_2 + \cdots + a_n b_n. 24 | $$ 25 | 26 | 因此,向量的**模长** $\lvert\lvert a\rangle\rvert$ 也可以表示为 $\sqrt{\langle a|a\rangle}$. 27 | 28 | --- 29 | 30 | ## 标准正交基 31 | 32 | 在 $\mathbb{R}^2$ 平面中,我们定义如下几组**标准正交基**(Orthonormal basis): 33 | 34 | $$ 35 | \begin{align*} 36 | \lvert\uparrow\rangle = \begin{bmatrix} 1 \\ 0 \end{bmatrix}&\text{ 和 }\lvert\downarrow\rangle = \begin{bmatrix} 0 \\ 1 \end{bmatrix}, \\[1em] 37 | \lvert\rightarrow\rangle = \begin{bmatrix} \dfrac{1}{\sqrt{2}} \\[1em] \dfrac{-1}{\sqrt{2}} \end{bmatrix}&\text{ 和 }\lvert\leftarrow\rangle = \begin{bmatrix} \dfrac{1}{\sqrt{2}} \\[1em] \dfrac{1}{\sqrt{2}} \end{bmatrix}, \\[2.5em] 38 | \lvert\nearrow\rangle = \begin{bmatrix} \dfrac{1}{2} \\[1em] \dfrac{-\sqrt{3}}{2} \end{bmatrix}&\text{ 和 }\lvert\swarrow\rangle = \begin{bmatrix} \dfrac{\sqrt{3}}{2} \\[1em] \dfrac{1}{2} \end{bmatrix}. 39 | \end{align*} 40 | $$ 41 | 42 | 给定一组标准正交基 $\{\lvert b_1\rangle, \lvert b_2\rangle, \cdots, \lvert b_n\rangle\}$,我们可以将任意的 $n$ 维 Ket $\lvert v\rangle$ **标准正交分解**为 43 | 44 | $$ 45 | \lvert v\rangle = \langle b_1|v\rangle \lvert b_1\rangle + \langle b_2|v\rangle \lvert b_2\rangle + \cdots + \langle b_n|v\rangle \lvert b_n\rangle. 46 | $$ 47 | 48 | --- 49 | 50 | ## 有序基 51 | 52 | 将集合的大括号 $\{\}$ 改为小括号 $()$,我们就得到了**有序基**(Ordered basis). 有序基强调了向量的排列顺序不能改变,例如在 $\mathbb{R}^2$ 平面中 $\left(\lvert\uparrow\rangle, \lvert\downarrow\rangle\right) \neq \left(\lvert\downarrow\rangle, \lvert\uparrow\rangle\right)$. 53 | 54 | --- 55 | 56 | ## 矩阵 57 | 58 | **矩阵**可以用一行 Ket 或一列 Bra 来表示,例如 59 | 60 | $$ 61 | \begin{align*} 62 | A &= \begin{bmatrix}1 & -4 & 2 \\ 2 & 3 & 0\end{bmatrix} = \begin{bmatrix}\langle a_1\rvert \\ \langle a_2\rvert\end{bmatrix}, \\[1em] 63 | B &= \begin{bmatrix}1 & 2 \\ 7 & 5 \\ 6 & 1\end{bmatrix} = \begin{bmatrix}\lvert b_1\rangle & \lvert b_2\rangle\end{bmatrix}. 64 | \end{align*} 65 | $$ 66 | 67 | $A$ 和 $B$ 的**矩阵乘法**则可以表示为 68 | 69 | $$ 70 | AB = \begin{bmatrix}\langle a_1\rvert \\ \langle a_2\rvert\end{bmatrix} \begin{bmatrix}\lvert b_1\rangle & \lvert b_2\rangle\end{bmatrix} = \begin{bmatrix}\langle a_1|b_1\rangle & \langle a_1|b_2\rangle \\ \langle a_2|b_1\rangle & \langle a_2|b_2\rangle\end{bmatrix}. 71 | $$ 72 | 73 | --- 74 | 75 | 设 $B=\begin{bmatrix}\lvert b_1\rangle & \lvert b_2\rangle & \cdots & \lvert b_n\rangle\end{bmatrix}$,则 76 | 77 | $$ 78 | B^\top B=\begin{bmatrix}\langle b_1\rvert \\ \langle b_2\rvert \\ \vdots \\ \langle b_n\rvert\end{bmatrix} \begin{bmatrix}\lvert b_1\rangle & \lvert b_2\rangle & \cdots & \lvert b_n\rangle\end{bmatrix} = \begin{bmatrix}\langle b_1|b_1\rangle & \langle b_1|b_2\rangle & \cdots & \langle b_1|b_n\rangle \\ \langle b_2|b_1\rangle & \langle b_2|b_2\rangle & \cdots & \langle b_2|b_n\rangle \\ \vdots & \vdots & \ddots & \vdots \\ \langle b_n|b_1\rangle & \langle b_n|b_2\rangle & \cdots & \langle b_n|b_n\rangle\end{bmatrix}. 79 | $$ 80 | 81 | 因此,**$\{\lvert b_1\rangle, \lvert b_2\rangle, \cdots, \lvert b_n\rangle\}$ 是标准正交基当且仅当 $B^\top B=I$**,其中 $I$ 是单位矩阵. 82 | 83 | 前面提到,$\lvert v\rangle$ 的标准正交分解可以写成 84 | 85 | $$ 86 | \begin{align*} 87 | \lvert v\rangle &= x_1\lvert b_1\rangle + x_2\lvert b_2\rangle + \cdots + x_n\lvert b_n\rangle \\[1em] 88 | &=\langle b_1|v\rangle \lvert b_1\rangle + \langle b_2|v\rangle \lvert b_2\rangle + \cdots + \langle b_n|v\rangle \lvert b_n\rangle. 89 | \end{align*} 90 | $$ 91 | 92 | 我们可以用矩阵来得到一个更紧凑的表达: 93 | 94 | $$ 95 | \begin{bmatrix}x_1 \\ x_2 \\ \vdots \\ x_n\end{bmatrix} = B^\top \lvert v\rangle = \begin{bmatrix}\langle b_1|v\rangle \\ \langle b_2|v\rangle \\ \vdots \\ \langle b_n|v\rangle\end{bmatrix}. 96 | $$ 97 | 98 | --- 99 | 100 | ## 正交矩阵、酉矩阵 101 | 102 | 在实数域上,**正交矩阵**(Orthogonal matrix)是指满足 $A^\top A=I$ 的矩阵. 它的复数版本是 **酉矩阵**(Unitary matrix),指满足 $A^\dagger A=I$ 的矩阵,其中 $A^\dagger$ 是 $A$ 的**厄米共轭**(Hermitian conjugate)(即将 $A$ 的转置取复共轭). 正交矩阵是一种特殊的酉矩阵. 103 | 104 | 下面简要介绍两类特殊的正交矩阵: 105 | 106 | $$ 107 | \begin{bmatrix} 108 | \dfrac{1}{\sqrt{2}} & \dfrac{1}{\sqrt{2}} \\[1em] 109 | \dfrac{1}{\sqrt{2}} & \dfrac{-1}{\sqrt{2}} 110 | \end{bmatrix} 111 | \text{ 和 } 112 | \begin{bmatrix} 113 | 1 & 0 & 0 & 0 \\ 114 | 0 & 1 & 0 & 0 \\ 115 | 0 & 0 & 0 & 1 \\ 116 | 0 & 0 & 1 & 0 117 | \end{bmatrix}. 118 | $$ 119 | 120 | - **前者**对应有序基 $\left(\lvert\leftarrow\rangle, \lvert\rightarrow\rangle\right)$,也和 **Hadamard 门**有关. 121 | - **后者**是将 $\mathbb{R}^4$ 空间的常用基最后两个元素交换位置得到的,它对应 **CNOT 门**. 122 | 123 | **所有的量子电路都由这两种门构成**. 124 | -------------------------------------------------------------------------------- /docs/cs/quantum/spin-and-qubits.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.3 Spin and Qubits 3 | --- 4 | 5 | 本章正式给出**量子比特**的定义及其数学模型,并且介绍了一种基础的量子密钥分发协议——**BB84 协议**. 6 | 7 | --- 8 | 9 | ## 量子叠加态 10 | 11 | 电子在经过一次 Stern-Gerlach 装置的测量后,会有形成**两种可能的结果**,因此我们的研究是在一个**二维**的向量空间内进行的. 在本书中我们只研究二维平面的量子现象,因此更进一步局限到 **$\mathbb{R}^2$** 上,对于三维的情况,则是 $\mathbb{C}^2$. 12 | 13 | 在我们的数学模型中,只考虑**单位向量**. 14 | 15 | 选择装置的**测量方向对应于选择一个有序基 $(\lvert b_1\rangle, \lvert b_2\rangle)$**,其中的**两个向量对应着两种可能的测量结果**,我们将前者与自旋 N 对应,后者与自旋 S 对应. 16 | 17 | 在进行测量之前,电子处于 $\lvert b_1\rangle$ 和 $\lvert b_2\rangle$ 的**叠加态**,我们用 $(\lvert b_1\rangle, \lvert b_2\rangle)$ 的**线性组合**来表示这个态,即 **$c_1\lvert b_1\rangle + c_2\lvert b_2\rangle$**,其中 $c_1, c_2 \in \mathbb{R}$ 且 $c_1^2 + c_2^2 = 1$. 18 | 19 | 在测量之后,电子会**坍缩**到 $\lvert b_1\rangle$ 和 $\lvert b_2\rangle$ 其中的一个态上,并且**测量结果为 $\lvert b_1\rangle$ 的概率为 $c_1^2$,为 $\lvert b_2\rangle$ 的概率为 $c_2^2$**. 参数 $c_1$ 和 $c_2$ 称为**概率振幅**(Probability amplitude). 20 | 21 | --- 22 | 23 | 例如,在一开始选择的**竖直方向**的实验装置中,所对应的**基为 $(\lvert\uparrow\rangle, \lvert\downarrow\rangle)$**,其中 24 | 25 | $$ 26 | \lvert\uparrow\rangle = \begin{bmatrix}1\\0\end{bmatrix},\quad\lvert\downarrow\rangle = \begin{bmatrix}0\\1\end{bmatrix}. 27 | $$ 28 | 29 | 第一个向量代表着电子上旋,即在 $0^\circ$ 方向自旋为 N;第二个向量代表着电子下旋,即在 $0^\circ$ 方向自旋为 S. **水平方向**的实验装置对应的**基为 $(\lvert\rightarrow\rangle, \lvert\leftarrow\rangle)$**,其中 30 | 31 | $$ 32 | \lvert\rightarrow\rangle = \begin{bmatrix} \dfrac{1}{\sqrt{2}} \\[1em] \dfrac{-1}{\sqrt{2}} \end{bmatrix},\quad\lvert\leftarrow\rangle = \begin{bmatrix} \dfrac{1}{\sqrt{2}} \\[1em] \dfrac{1}{\sqrt{2}} \end{bmatrix}. 33 | $$ 34 | 35 | 设想一个初始态未知的电子射向**竖直方向**的装置,它的初始态一定可以写成 **$c_1\lvert\uparrow\rangle + c_2\lvert\downarrow\rangle$**,其中 $c_1^2 + c_2^2 = 1$. 然后我们开始测量,此时电子有 $c_1^2$ 的概率坍缩到 $\lvert\uparrow\rangle$,有 $c_2^2$ 的概率坍缩到 $\lvert\downarrow\rangle$. 36 | 37 | 现在我们重复相同的实验,继续在**竖直方向**上进行测量,假设初始测量结果是 $\lvert\uparrow\rangle$,那么在下一次测量中,电子的初始态就是 $\lvert\uparrow\rangle=1\lvert\uparrow\rangle+0\lvert\downarrow\rangle$,$c_1^2=1, c_2^2=0$. 意味着再次进行相同的测量时,**电子必然会仍然维持在 $\lvert\uparrow\rangle$**. 38 | 39 | 如果第二次实验选择在**水平方向**进行测量,假设第一次竖直方向的测量结果是 $\lvert\uparrow\rangle$,我们需要**把它写成水平方向的基的线性组合**,即求解 $x_1, x_2$ 使得 40 | 41 | $$ 42 | \lvert\uparrow\rangle = x_1\lvert\rightarrow\rangle + x_2\lvert\leftarrow\rangle. 43 | $$ 44 | 45 | ::fold{expand info title=求解过程} 46 | 在上一章我们已经知道如何计算这个式子,首先写出基的矩阵形式 47 | 48 | $$ 49 | B = \begin{bmatrix} \lvert\rightarrow\rangle & \lvert\leftarrow\rangle \end{bmatrix} = \begin{bmatrix} \dfrac{1}{\sqrt{2}} & \dfrac{1}{\sqrt{2}} \\[1em] \dfrac{-1}{\sqrt{2}} & \dfrac{1}{\sqrt{2}} \end{bmatrix}, 50 | $$ 51 | 52 | 然后将其求转置,再乘以 $\lvert\uparrow\rangle$,即 53 | 54 | $$ 55 | \begin{bmatrix}x_1\\x_2\end{bmatrix} = B^\top\lvert\uparrow\rangle = \begin{bmatrix} \dfrac{1}{\sqrt{2}} & \dfrac{-1}{\sqrt{2}} \\[1em] \dfrac{1}{\sqrt{2}} & \dfrac{1}{\sqrt{2}} \end{bmatrix} \begin{bmatrix}1\\0\end{bmatrix} = \begin{bmatrix} \dfrac{1}{\sqrt{2}} \\[1em] \dfrac{1}{\sqrt{2}} \end{bmatrix}. 56 | $$ 57 | :: 58 | 59 | 因此,$\lvert\uparrow\rangle = \dfrac{1}{\sqrt{2}}\lvert\rightarrow\rangle + \dfrac{1}{\sqrt{2}}\lvert\leftarrow\rangle$,这说明在第二次测量时,有 $1/2$ 的概率测量结果为 $\lvert\rightarrow\rangle$,有 $1/2$ 的概率测量结果为 $\lvert\leftarrow\rangle$. 这和实验结果是一致的. 60 | 61 | ::fold{expand success title=等价量子叠加态} 62 | 设 $\lvert v\rangle=c_1\lvert b_1\rangle+c_2\lvert b_2\rangle$,那么 $-\lvert v\rangle=-c_1\lvert b_1\rangle-c_2\lvert b_2\rangle$,它们对应的概率都是 $c_1^2$ 和 $c_2^2$,因此它们无法通过实验来区分,我们称这两个态是**等价的**. 63 | :: 64 | 65 | --- 66 | 67 | ## 基的旋转 68 | 69 | 从标准基 $\left(\begin{bmatrix}1\\0\end{bmatrix}, \begin{bmatrix}0\\1\end{bmatrix}\right)$ 出发,**顺时针旋转 $\alpha$ 角**,得到 $\left(\begin{bmatrix}\cos\alpha\\-\sin\alpha\end{bmatrix}, \begin{bmatrix}\sin\alpha\\\cos\alpha\end{bmatrix}\right)$. 70 | 71 | ::grid{gap=10px} 72 | :sep{span=12} 73 | ![从标准基顺时针旋转 $\alpha$ 角](./assets/spin-and-qubits.rotate.svg){.inv width=250px} 74 | 75 | :sep{span=12} 76 | ![实验装置旋转 $\theta$ 角](./assets/spin-and-qubits.spin-angle.svg){.inv width=140px} 77 | :: 78 | 79 | 当旋转 $\alpha=90^\circ$ 时,得到基 $\left(\begin{bmatrix}\cos90^\circ\\-\sin90^\circ\end{bmatrix}, \begin{bmatrix}\sin90^\circ\\\cos90^\circ\end{bmatrix}\right)=\left(\begin{bmatrix}0\\-1\end{bmatrix}, \begin{bmatrix}1\\0\end{bmatrix}\right)$,而 $\begin{bmatrix}0\\-1\end{bmatrix}$ 等价于 $\begin{bmatrix}0\\1\end{bmatrix}$,所以旋转 $90^\circ$ 就相当于**交换两个基向量**. 80 | 81 | 如果用 $\theta$ 表示实验装置的旋转角度,那么 $\theta$ 从 $0^\circ$ 转到 $180^\circ$ 等价于 $\alpha$ 从 $0^\circ$ 转到 $90^\circ$,所以**有 $\theta = 2\alpha$**. 因此,竖直方向的实验装置旋转 $\theta$ 角得到的基向量为 82 | 83 | $$ 84 | \left(\begin{bmatrix}\cos\left(\dfrac{\theta}{2}\right)\\[1em]-\sin\left(\dfrac{\theta}{2}\right)\end{bmatrix}, \begin{bmatrix}\sin\left(\dfrac{\theta}{2}\right)\\[1em]\cos\left(\dfrac{\theta}{2}\right)\end{bmatrix}\right). 85 | $$ 86 | 87 | --- 88 | 89 | ## 量子比特 90 | 91 | 定义**量子比特为 $\mathbb{R}^2$ 空间内的任何单位 Ket**. 给定一个用于测量的基 $(\lvert b_0\rangle, \lvert b_1\rangle)$,定义 **$\lvert b_0\rangle$ 代表比特 0,$\lvert b_1\rangle$ 代表比特 1**,而其他的态则是**叠加态**. 在量子计算中,为了获得计算结果,我们必须对量子比特进行观测,而观测必定会导致量子比特坍缩为某个基向量,所以**最终得到的一定是传统意义上的 01 比特**. 92 | 93 | --- 94 | 95 | ## 量子干涉 96 | 97 | 考虑 $\lvert\leftarrow\rangle$ 和 $\lvert\rightarrow\rangle$ 两个量子比特,如果我们在标准基下进行测量,它们分别都有 $1/2$ 的概率测量结果为 0 和 1. 现在我们**对两者进行叠加**,得到 $\lvert v\rangle=\frac{1}{\sqrt{2}}\lvert\leftarrow\rangle+\frac{1}{\sqrt{2}}\lvert\rightarrow\rangle$,然后再在标准基下进行测量,得到的**结果一定是 0**,因为 98 | 99 | $$ 100 | \lvert v\rangle = \frac{1}{\sqrt{2}}\lvert\leftarrow\rangle+\frac{1}{\sqrt{2}}\lvert\rightarrow\rangle = 1\lvert\uparrow\rangle+0\lvert\downarrow\rangle. 101 | $$ 102 | 103 | 这说明 $\lvert\leftarrow\rangle$ 和 $\lvert\rightarrow\rangle$ 中产生 0 的项**干涉相长**(Interfered constructively),产生 1 的项**干涉相消**(Interfered destructively). 104 | 105 | --- 106 | 107 | ## BB84 密钥分发协议 108 | 109 | **BB84** 协议得名于它的发明者 Charles Bennett 和 Gilles Brassard,以及它的发明年份 1984 年. 这是一个**基于量子力学的密钥分发协议**,它的安全性基于**量子力学的不可克隆性**. 该协议使用如下两组基: 110 | 111 | $$ 112 | \begin{align*} 113 | V &= (\lvert\uparrow\rangle, \lvert\downarrow\rangle) = \left(\begin{bmatrix}1\\0\end{bmatrix}, \begin{bmatrix}0\\1\end{bmatrix}\right),\\[1em] 114 | H &= (\lvert\rightarrow\rangle, \lvert\leftarrow\rangle) = \left(\begin{bmatrix}\dfrac{1}{\sqrt{2}}\\[1em]\dfrac{-1}{\sqrt{2}}\end{bmatrix}, \begin{bmatrix}\dfrac{1}{\sqrt{2}}\\[1em]\dfrac{1}{\sqrt{2}}\end{bmatrix}\right). 115 | \end{align*} 116 | $$ 117 | 118 | 假设 Alice 相和 Bob 沟通密钥,并且想要确保中途没有被别人(设为 Eve)监听. 119 | 120 | 首先,Alice 生成一串长度为 $4n$ 的常规比特序列($n$ 需要足够大),对于每一个比特,**等概率随机选择一个对应的基 $V$ 或 $H$**,然后将其转换为量子比特,发送给 Bob. 例如,如果想要发送 0,并且随机选择了基 $H$,那么 Alice 会发送 $\lvert\rightarrow\rangle$. 同时 **Alice 在本地保存每个比特选择的基的记录**. 121 | 122 | Bob 同样等概率**随机选择一系列 $4n$ 个 $V$ 或 $H$**,然后用它们分别对 Alice 发送的量子比特进行观测,并在本地留存记录. 123 | 124 | 由于 Alice 和 Bob 选择的基是随机的,对于每一个比特,他们有 $1/2$ 的概率选到相同的基,此时 Bob 会获得正确的结果;有 $1/2$ 的概率选到不同的基,此时 Bob 又有 $1/2$ 的概率选到正确的基(想一下 Stern-Gerlach 实验),但是由于无法确定是否正确,所以 **Bob 最终得到的比特串中只有 $2n$ 个是正确的**. 125 | 126 | Alice 和 Bob 通过公开的信道交换他们的基选择记录,然后**去掉不匹配的基对应的比特**,剩下 $2n$ 个比特. 127 | 128 | 如果 Eve 想要监听,她必须**拦截并重新发送** Alice 发送的量子比特,但是**想要保证重新发送的量子比特和原始的量子比特一致是不可能的**,因为 Eve 无法知道 Alice 选择的基,一旦拦截,量子比特就会**不可逆地**坍缩为她选择的某个基向量,并且由于概率都是 $1/2$,所以**Eve 无法完全模拟 Alice 的行为**. 129 | 130 | 但是 Eve 可能采用另一个策略,假设她拦截了 Alice 发送的量子比特,然后**在 $V$ 和 $H$ 两个基中随机选择一个进行观测**,然后再将观测结果转发给 Bob,我们考虑一下这种情况. 131 | 132 | 假如 Eve 没有监听,那么 Alice 和 Bob 的比特串会有 $2n$ 个是匹配的. 但如果监听了,Eve 有一半的可能选到错误的基并对量子比特进行了修改,因此 Bob **只有 $1/4$ 的概率得到正确的比特**. 133 | 134 | 现在 Alice 和 Bob **通过公开的信道比较 $2n$ 个匹配当中的 $n$ 个比特**,如果完全一致,说明没有监听,他们可以使用剩下的 $n$ 个比特作为密钥;如果有 $1/4$ 的比特不一致,说明通信被监听了,他们需要**通过其他方式重新建立通信**. 135 | -------------------------------------------------------------------------------- /docs/cs/quantum/spin.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.1 Spin 3 | --- 4 | 5 | 一个**量子比特**(Quantum bit, Qubit)是量子计算的基本单元,它可以由**电子自旋**或**光子偏振**来表示. 在介绍量子比特之前,我们先从一个实验引入. 6 | 7 | --- 8 | 9 | ## Stern-Gerlach 实验 10 | 11 | 在玻尔的原子行星轨道模型中,电子绕核旋转产生磁场,其中内层电子磁场正反抵消,而外层电子可能无法抵消. 因此在某些原子(例如银原子)中,最外层电子会让整个原子带上一个**微小磁矩**. 据此原理,Otto Stern 和 Walther Gerlach 设计了一个实验,尝试观测银原子的磁矩方向. 12 | 13 | ![Stern-Gerlach 实验](./assets/spin.stern-gerlach-experiment.svg){.inv width=450px} 14 | 15 | 装置中间的 V 形磁体会产生一个非均匀磁场,使得 S 极的效果比 N 极强. 如果银原子 N 极朝上,那么它会受到磁体 S 极更强的吸引力,最终向上偏转;反之,如果 S 极朝上,那么他会被磁体 S 极排斥,从而向下偏转. 如果 NS 极水平,那就不会偏转. 16 | 17 | 理论上来说,由于射出的银原子的磁矩方向是随机的,那么在屏幕上应该会出现一个连续的条纹. 但实验结果并不是这样,而是只出现了最上方和最下方两个点,说明这个**观测行为本身对银原子的方向产生了影响**.(具体物理原理不在本书讨论范围内) 18 | 19 | --- 20 | 21 | ## Spin 22 | 23 | 电子本身也有类似上述银原子的性质,对于不同的实验结果,我们定义电子的**自旋**(Spin)方向如下:(圆圈表示电子落到屏幕上的位置) 24 | 25 | ::grid{gap=5px} 26 | :sep{span=5} 27 | ![实验结果](./assets/spin.spin-1.svg){.inv width=60px} 28 | 29 | :sep{span=1 .center} 30 | $\Longrightarrow$ 31 | 32 | :v{3rem} 33 | 34 | :sep{span=5} 35 | ![Spin N](./assets/spin.spin-2.svg){.inv width=55px} 36 | 37 | :sep{span=5 offset=1} 38 | ![实验结果](./assets/spin.spin-3.svg){.inv width=60px} 39 | 40 | :sep{span=1 .center} 41 | $\Longrightarrow$ 42 | 43 | :v{3rem} 44 | 45 | :sep{span=5} 46 | ![Spin S](./assets/spin.spin-4.svg){.inv width=55px} 47 | :: 48 | 49 | 为了在二维平面内更好地描述自旋,定义电子旋转的角度如下: 50 | 51 | ![自旋为 N,朝 $\theta$ 角方向的电子](./assets/spin.spin-angle.svg){.inv width=140px} 52 | 53 | 竖直向上为 $0^\circ$,顺时针旋转为正方向. 54 | 55 | 在研究电子自旋之前,我们先看一个**量子钟隐喻**. 56 | 57 | ::fold{expand info title=量子钟隐喻} 58 | 设想一个挂在墙上的钟,它的指针可以指向 12 个小时刻度,但是你看不到钟面,只能向它提问来得到时间,而且**只能提问 “现在是不是某点?”** 这样的问题. 59 | 60 | 在经典物理学当中,如果你提问 “现在是不是 3 点”,那么你有 $1/12$ 的概率得到 “是” 的回答,这是很自然的. 但是我们这个量子钟不一样,不管你问它现在是不是几点,它一定**要么回答 “是”,要么回答 “现在的时间是你提问的时间都指针反方向指向的那个时间”**. 例如你问它 “现在是不是 3 点”,它要么回答 “是”,要么回答 “现在是 9 点”. 61 | :: 62 | 63 | 这个隐喻可以帮助我们理解电子自旋的性质. 在 Stern-Gerlach 实验中,银原子的朝向本身是有 $0\sim360^\circ$ 无限种可能的,但是在观测之后,它要么回答 $0^\circ$,要么回答 $180^\circ$. 64 | 65 | --- 66 | 67 | ## 多次测量 68 | 69 | 在 Stern-Gerlach 实验(的电子版本)中,如果在自旋为 N 的电子射出的位置再加一个和第一个装置朝向相同的装置,也就是再进行一次相同的测量,实验表明,**第二次测量的结果和第一次测量的结果是一样的**. 也就是说,如果第一次测量得到的是 N,那么第二次测量也是 N;如果第一次测量得到的是 S,那么第二次测量也是 S. 不管再后面进行多少次测量,**只要测量的方向相同,结果都是一样的**. 70 | 71 | 然而,如果第二个装置旋转了 $90^\circ$,实验表明,**第二次测量的结果完全随机**,$50\%$ 的概率是 N,$50\%$ 的概率是 S. 这个随机是**真随机**,理论可以证明,**不可能通过任何手段来预测第二次测量的结果**. 72 | 73 | 更进一步,在旋转 $90^\circ$ 的装置后再加上和第一个装置朝向相同的装置,我们发现**第三次测量结果仍然是随机的**. 也就是说,如果连着三次相同测量,结果是相同的,但仅仅是把第二次测量的装置旋转了 $90^\circ$,原本相同的第一、第三次结果竟然变成不一样的了. 74 | -------------------------------------------------------------------------------- /docs/ctf/crypto/aes.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: AES 加密 3 | --- 4 | 5 | ## 算法流程 6 | 7 | **AES**(Advanced Encryption Standard)是一种**块加密算法**(Block Cipher). 算法首先对明文进行分组,分组长度固定为 128 位(即 16 字节),密钥长度可选 128、192 或 256 位. 对于每一个 16 字节的明文块,进行如下步骤(以 AES-128 为例): 8 | 9 | ::grid 10 | :sep{span=12} 11 | ![块加密](./assets/aes-encrypt.png) 12 | 13 | :sep{span=12} 14 | ![块解密](./assets/aes-decrypt.png) 15 | :: 16 | 17 | 1. **密钥扩展 KeyExpansion**:从 128 位密钥生成 11 个 128 位轮密钥. 18 | 2. **轮密钥加 AddRoundKey**:将明文与第 0 轮密钥异或. 19 | 3. **轮**:共 10 轮,前 9 轮为主轮,最后一轮(最终轮)不包含 MixColumns 步骤. 20 | 1. **字节替换 SubBytes**:使用 S-box 替换字节. 21 | 2. **行移位 ShiftRows**:对每行进行循环移位. 22 | 3. **列混淆 MixColumns**:对每列进行线性变换.(在最终轮被跳过) 23 | 4. **轮密钥加 AddRoundKey**:将轮密钥与状态矩阵异或. 24 | 25 | ## 分块加密模式 26 | 27 | 得到每一块密文后,为确保不同块之间互相影响,通常使用如下分块加密模式: 28 | 29 | ![](./assets/aes-ecb.png){.inv} 30 | 31 | ![](./assets/aes-cbc.png){.inv} 32 | 33 | ![](./assets/aes-ctr.png){.inv} 34 | 35 | ![](./assets/aes-cfb.png){.inv} 36 | 37 | ![](./assets/aes-ofb.png){.inv} 38 | 39 | ## 填充模式 40 | 41 | 由于 AES 要求明文长度必须是 16 字节的整数倍,因此需要对明文进行填充. 常见的填充模式见 [Using Padding in Encryption](https://www.di-mgt.com.au/cryptopad.html). 42 | 43 | ## 攻击 44 | 45 | 目前唯一对 AES 有效的攻击是**侧信道攻击**. 对于设计不良的实现,例如如果未能保证不同密钥的加密复杂度相同,攻击者可以通过测量加密时间、机器功耗等信息,进而推断密钥. 46 | 47 | 因此,在 CTF 中,对 AES 的攻击主要集中在**填充模式**和**分块加密模式**上. 48 | -------------------------------------------------------------------------------- /docs/ctf/crypto/assets/aes-cbc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/crypto/assets/aes-cbc.png -------------------------------------------------------------------------------- /docs/ctf/crypto/assets/aes-cfb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/crypto/assets/aes-cfb.png -------------------------------------------------------------------------------- /docs/ctf/crypto/assets/aes-ctr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/crypto/assets/aes-ctr.png -------------------------------------------------------------------------------- /docs/ctf/crypto/assets/aes-decrypt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/crypto/assets/aes-decrypt.png -------------------------------------------------------------------------------- /docs/ctf/crypto/assets/aes-ecb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/crypto/assets/aes-ecb.png -------------------------------------------------------------------------------- /docs/ctf/crypto/assets/aes-encrypt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/crypto/assets/aes-encrypt.png -------------------------------------------------------------------------------- /docs/ctf/crypto/assets/aes-ofb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/crypto/assets/aes-ofb.png -------------------------------------------------------------------------------- /docs/ctf/crypto/assets/ecc-addition.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/crypto/assets/ecc-addition.png -------------------------------------------------------------------------------- /docs/ctf/crypto/basis.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 密码学数学基础 3 | --- 4 | 5 | ## 二次剩余 6 | 7 | ::fold{expand success title=定义} 8 | 若存在整数 $x$ 使得 $x^2 \equiv a \pmod{p}$,则称 $a$ 是模 $p$ 的二次剩余,否则称为二次非剩余. 9 | :: 10 | 11 | $p$ 为合数的讨论较为复杂(NP-hard),这里只讨论 $p$ 为奇素数的情况. 12 | 13 | --- 14 | 15 | ### 判定 16 | 17 | **Legendre 符号 $\left(\dfrac{a}{p}\right)$** $:= a^{(p-1)/2}\bmod p$,其中 $p$ 是奇素数. 18 | 19 | - 若 $(a / p) = 1$,则 $a$ 是二次剩余. 20 | - 若 $(a / p) = -1$,则 $a$ 是二次非剩余. 21 | - 若 $(a / p) = 0$,则 $a \equiv 0 \pmod p$. 即 $p \mid a$. 22 | 23 | **性质:** 24 | 25 | - **完全积性**:对任意 $a, b \in \mathbb{Z}$,有 $\left(\dfrac{ab}{p}\right) = \left(\dfrac{a}{p}\right)\cdot\left(\dfrac{b}{p}\right)$. 26 | - **二次互反律**:对任意奇素数 $p \neq q$,有 $\left(\dfrac{p}{q}\right)\cdot\left(\dfrac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}$. 27 | - **二次剩余的个数**:对奇素数 $p$,模 $p$ 意义下二次剩余和二次非剩余均有 $\dfrac{p-1}{2}$ 个. 28 | 29 | ### 求解 30 | 31 | **当 $p \equiv 3 \pmod{4}$ 时**,对于方程 $x^2 \equiv a \pmod{p}$, 32 | 33 | - $a^{(p+1)/4} \bmod p$ 为一个解.(费马小定理推得) 34 | - **Atkin 算法**:令 $b \equiv (2a)^{(p-5)/8}\pmod p$,$\mathrm{i}\equiv2ab^2\pmod p$,则 $\mathrm{i}^2\equiv-1\pmod p$,且 $ab(i-1)\bmod p$ 为一个解. 35 | 36 | **当 $p \equiv 1 \pmod{4}$ 时**,可由 **Tonelli-Shanks 算法**求解. 37 | 38 | - SageMath:`mod(a, p).sqrt()`{language=python}. 39 | 40 | --- 41 | 42 | ## 离散对数 43 | 44 | ::fold{expand success title=定义} 45 | 1. 设 $G$ 是一个群,若存在 $g \in G$ 使得 $G = \{g^k\mid k\in\mathbb{N}\}$,则称 $G$ 是一个**循环群**,$g$ 是 $G$ 的**生成元**. 46 | 2. 对 $G$ 中的任意元素 $y$,都可以写成 $y = g^k$ 的形式,称 $k$ 为 $y$ 在群 $G$ 中的离散对数,简称**对数**. 47 | 3. 设 $m \geqslant 1$,$(a, m) = 1$,称使得 $a^d \equiv 1 \pmod{m}$ 的最小正整数 $d$ 为 $a$ 模 $m$ 的**阶**,记作 $\operatorname{ord}_m(a)$ 或 $\delta_m(a)$. 当 $\operatorname{ord}_m(a) = \varphi(m)$ 时,称 $a$ 是 $m$ 的**原根**. 48 | :: 49 | 50 | **模 $m$ 剩余系存在原根的充要条件**:$m = 2, 4, p^k, 2p^k$,其中 $p$ 是奇素数,$k$ 是正整数. 51 | 52 | **Lagrange 定理**:$g^k$ 在有限群 $G$ 中的阶是 $\dfrac{\operatorname{ord}(g)}{\gcd(k, \operatorname{ord}(g))}$. 53 | 54 | --- 55 | 56 | ### 离散对数问题 DLP 57 | 58 | 已知 $g$、$p$ 和 $y$,对于方程 $y = g^x \pmod{p}$,求解 $x$ 是一个困难问题. 59 | 60 | 用到离散对数的加密算法:[Diffie-Hellman 密钥交换](./diffie-hellman)、ElGamal 加密算法、椭圆曲线算法等. 61 | 62 | --- 63 | 64 | ### 攻击途径 65 | 66 | [**Alpertron 离散对数求解器**](https://www.alpertron.com.ar/DILOG.HTM) 67 | 68 | [**SageMath**](https://doc.sagemath.org/html/en/reference/groups/sage/groups/generic.html):能自动选择算法,可计算椭圆曲线上的离散对数. 69 | 70 | **Baby-Step Giant-Step 算法**:时间复杂度 $\Theta(\sqrt{p})$. 71 | 72 | **Pohlig-Hellman 算法**:当 $\operatorname{ord}_p(g)$ 是光滑数时,可分解为子问题求解. 73 | 74 | - 若 $n = \displaystyle\prod_{i=1}^k p_i^{e_i}$ 的所有质因子 $p_i$ 都很小,则称 $n$ 是一个**光滑数**.(不是严格定义) 75 | - 如果 $p$ 是质数,则 $\operatorname{ord}_p(g) = \varphi(p) = p-1$,这也是常用 $p=2q+1$ 的原因. 76 | - 时间复杂度 $\Theta\left(\displaystyle\sum_{i=1}^k e_i\left(\log p + \sqrt{p_i}\right)\right)$. 77 | 78 | **Pollard's $\rho$ 算法**:一种随机算法,时间复杂度 $\Theta(\sqrt{p})$. 79 | 80 | **Pollard's Kangaroo 算法**:如果已知 $x \in [a, b]$,时间复杂度 $\Theta(\sqrt{b-a})$. 81 | 82 | --- 83 | 84 | ### Pohlig-Hellman 算法原理 85 | 86 | ::note{info} 87 | 以下证明来自 [Wikipedia](https://en.wikipedia.org/wiki/Pohlig%E2%80%93Hellman_algorithm). 88 | :: 89 | 90 | 对于离散对数问题 $y = g^x \pmod{p}$,设 $n := \operatorname{ord}_p(g) = \displaystyle\prod_{i=1}^k p_i^{e_i}$. 91 | 92 | 1. 对每个 $i \in \{1, \cdots, r\}$: 93 | - 计算 $g_i\equiv g^{n/p_i^{e_i}}\pmod p$. 由 Lagrange 定理,$g_i$ 的阶是 $p_i^{e_i}$. 94 | - 计算 $y_i \equiv y^{n/p_i^{e_i}} \equiv g^{xn/p_i^{e_i}} \equiv g_i^x \equiv g_i^{x\bmod p_i^{e_i}} \equiv g_i^{x_i} \pmod p$. 由于 $n$ 是光滑数,$x_i$ 的范围为 $[0, p_i^{e_i})$,可知其范围较小,可以用其他算法求解. 95 | 2. 得到 $x\equiv x_i\pmod{p_i^{e_i}}$,由中国剩余定理得到 $x$. 96 | 97 | ::fold{expand info title="用 Pohlig-Hellman 算法求解椭圆曲线上的离散对数问题 $G\times x=A$"} 98 | ```python 99 | facts = list(factor(G.order())) 100 | mods, rems = [], [] 101 | 102 | for fact in facts: 103 | t = fact[0] ** fact[1] 104 | G0 = G * (G.order() // t) 105 | A0 = A * (G.order() // t) 106 | mods.append(t) 107 | rems.append(discrete_log(A0, G0)) 108 | 109 | x = crt(rems, mods) 110 | ``` 111 | :: 112 | 113 | 若 $n$ 并非完全光滑,例如存在一个大素数因子,仍可通过 Pohlig-Hellman 算法限制 $x$ 的范围,见 [PicoCTF 2017: ECC2 的 Writeup](https://gist.github.com/jproney/7e6cb7a40a8bf342e978a900a32e4dfc). 114 | -------------------------------------------------------------------------------- /docs/ctf/crypto/diffie-hellman.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Diffie-Hellman 密钥交换 3 | --- 4 | 5 | Diffie-Hellman 密钥交换协议(DH)是一种**公钥交换协议**,用于在不安全的通信信道上协商密钥. 该协议的安全性基于**离散对数问题**. 6 | 7 | --- 8 | 9 | ## 算法流程 10 | 11 | Diffie-Hellman 协议的流程如下: 12 | 13 | 1. **初始化**:Alice 和 Bob 共享一个**大素数** $p$ 和一个**原根** $g$. 14 | 2. **生成密钥**: 15 | - Alice 选择一个私钥 $a$,计算公钥 $A = g^a \bmod p$. 16 | - Bob 选择一个私钥 $b$,计算公钥 $B = g^b \bmod p$. 17 | 3. **交换公钥**:Alice 将 $A$ 发送给 Bob,Bob 将 $B$ 发送给 Alice. 该步骤可以在不安全的网络中进行. 18 | 4. **计算密钥**:Alice 计算 $K = B^a \bmod p$,Bob 计算 $K = A^b \bmod p$,得到相同的值 $K = g^{ab}\bmod p$ 用于后续通信(例如作为对称加密算法 AES 的密钥). 19 | 20 | --- 21 | 22 | ## 攻击 23 | 24 | 见[离散对数问题](./basis#6)部分. 25 | -------------------------------------------------------------------------------- /docs/ctf/crypto/ecc.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 椭圆曲线加密 3 | --- 4 | 5 | **椭圆曲线加密**(Elliptic Curve Cryptography,ECC)是一种基于**椭圆曲线**的公钥加密算法. 与 RSA 相比,具有更高的安全性和更小的密钥长度. ECC 的安全性基于椭圆曲线**离散对数问题**(ECDLP). 目前椭圆曲线常采用的有限域有: 6 | 7 | - 以素数为模的有限域 $\mathrm{GF}(p)$. 8 | - 以 $2$ 的幂次为模的有限域 $\mathrm{GF}(2^m)$,常用于硬件实现. 9 | 10 | --- 11 | 12 | ## 椭圆曲线 13 | 14 | 椭圆曲线不是椭圆,而是满足如下方程的曲线: 15 | 16 | $$ 17 | y^2 + axy + by = x^3 + cx^2 + dx + e 18 | $$ 19 | 20 | 并非所有曲线都适于加密,我们通常使用 **Weierstrass 形式**的椭圆曲线: 21 | 22 | $$ 23 | \boxed{y^2 = x^3 + ax + b\text{,其中}\ 4a^3 + 27b^2 \neq 0} 24 | $$ 25 | 26 | 我们进一步在椭圆曲线上定义加法和标量乘法,使得曲线上的点集构成一个**Abel 群**. 27 | 28 | 设 $P$、$Q$ 是椭圆曲线上的两点,则 $R = P + Q$ 可以直观理解为:过 $P$、$Q$ 作一条直线,与曲线相交于第三点 $R'$,将 $R'$ 关于 $x$ 轴对称得到 $R$. 29 | 30 | 若 $P = Q$,则作曲线的切线;若连线无第三个交点,则定义 $P$、$Q$ 交于**无穷远点 $\mathcal{O}$**. 此处 $\mathcal{O}$ 即为椭圆曲线上的**单位元**. 31 | 32 | 椭圆曲线上的**标量乘法**则定义为点的累加. 可以验证以上定义满足 Abel 群的性质. 33 | 34 | ![椭圆曲线上的加法](./assets/ecc-addition.png) 35 | 36 | 在实际应用中,我们将椭圆曲线的参数和点的坐标限制在有限域 $\mathbb{F}_p$ 上,得到如下形式的离散点集: 37 | 38 | $$ 39 | E(\mathbb{F}_p) = \{(x, y) \in \mathbb{F}_p^2 \mid y^2=x^3+ax+b\text{,其中}\ 4a^3+27b^2\neq0\} \cup \{\mathcal{O}\} 40 | $$ 41 | 42 | ::note{info} 43 | 离散情况下的可视化动画:[The Animated Elliptic Curve - Visualizing Elliptic Curve Cryptography](https://curves.xargs.org/) 44 | :: 45 | 46 | 椭圆曲线上的标量乘法是一个**陷门函数**,如果将椭圆曲线加法和整数乘法对应,那么椭圆曲线乘法就是乘方运算,进而构成了**离散对数问题**,离散对数问题的一般性质在此处也适用. 47 | 48 | --- 49 | 50 | ## ECDH 密钥交换 51 | 52 | **ECDH**(Elliptic Curve Diffie–Hellman)即椭圆曲线上的 [**DH 密钥交换算法**](./diffie-hellman),其过程如下: 53 | 54 | 1. **初始化**:选择一条**椭圆曲线** $E$ 和一个**阶为素数**的**生成元** $G$,并公开这些参数. 现实中通常使用预先定义的椭圆曲线参数,如 NIST 的 [P-256 曲线](https://neuromancer.sk/std/nist/P-256). 55 | 2. **生成密钥**: 56 | - Alice 选择一个随机整数 $n_A$ 作为私钥,计算公钥 $Q_A = n_AG$. 57 | - Bob 选择一个随机整数 $n_B$ 作为私钥,计算公钥 $Q_B = n_BG$. 58 | 3. **交换公钥**:Alice 将 $Q_A$ 发送给 Bob,Bob 将 $Q_B$ 发送给 Alice. 59 | 4. **计算密钥**:Alice 计算 $S = n_AQ_B$,Bob 计算 $S = n_BQ_A$. 得到相同的值 $S = n_An_B \cdot G$ 用于后续通信. 60 | 61 | 当 $x$ 给定时,$y$ 最多只可能有两个值,并且无论使用哪个值,最后 $S$ 的横坐标都是相同的,因此交换公钥时只需传输 $x$ 坐标即可. 62 | 63 | --- 64 | 65 | ## ECDSA 数字签名 66 | 67 | **椭圆曲线数字签名算法**(Elliptic Curve Digital Signature Algorithm,ECDSA)是基于椭圆曲线的 **DSA 数字签名**算法. 68 | 69 | --- 70 | 71 | ### 签名算法 72 | 73 | 假设 Alice 想要对消息 $m$ 进行签名,签名过程如下: 74 | 75 | 1. **初始化**: 76 | - 选择一条**椭圆曲线** $E$ 和一个**阶为素数**的**生成元** $G$,并公开这些参数. 77 | - Alice 选择区间 $[1, n-1]$ 的随机整数 $d_A$ 作为私钥,计算公钥 $Q_A = d_A\times G$. 其中 $n$ 是生成元 $G$ 的阶. 78 | 2. 计算消息的**哈希值** $e = H(m)$,其中 $H$ 是哈希函数. 79 | 3. 选择一个随机整数 $k \in [1, n-1]$,计算椭圆曲线上的点 $(x_1, y_1) = k \times G$. 80 | 4. 计算 $r = x_1 \bmod n$,若 $r = 0$ 则返回第 3 步. 81 | 5. 计算 $s = k^{-1}(e + rd_A) \bmod n$,若 $s = 0$ 则返回第 3 步. 82 | 6. 得到签名 $(r, s)$. 83 | 84 | 不同签名必须使用**不同的 $k$ 值**,否则可通过第 5 步的 $s$ 值推导出私钥 $d_A$. 85 | 86 | --- 87 | 88 | ### 验证算法 89 | 90 | Bob 收到 Alice 的消息 $m$ 和签名 $(r, s)$,同时已知她的公钥 $Q_A$,验证签名的过程如下: 91 | 92 | 1. 验证 $Q_A$ 在椭圆曲线上,并且 $Q_A \neq \mathcal{O}$,$n\times Q_A = \mathcal{O}$. 93 | 2. 验证 $r$ 和 $s$ 是否在区间 $[1, n-1]$ 内. 94 | 3. 计算消息的哈希值 $e = H(m)$. 95 | 4. 计算 $u_1 = e\cdot s^{-1} \bmod n$ 和 $u_2 = r\cdot s^{-1} \bmod n$. 96 | 5. 计算点 $P = u_1 \times G + u_2 \times Q_A$ 并取其横坐标 $x_P$. 验证 $x_P \neq \mathcal{O}$. 97 | 6. 验证 $x_P \equiv r \pmod n$. 98 | 99 | --- 100 | 101 | ## 攻击途径 102 | 103 | 对于 $\mathbb{F}_p$ 上的 ECDLP 问题 $Q = n \times G$: 104 | 105 | **MOV 攻击**:当 $G$ 的 Embedding Degree 较小($\leqslant 6$)时,可通过该攻击求解. 106 | 107 | - **Embedding Degree**:满足 $p^k \equiv 1 \pmod N$ 的最小正整数 $k$. 其中 $N = \operatorname{ord}(G)$. 108 | - [SageMath 实现](https://github.com/jvdsn/crypto-attacks/blob/master/attacks/ecc/mov_attack.py)、[论文](https://fse.studenttheses.ub.rug.nl/22732/1/bMATH_2020_SmitR.pdf)、[StackExchange 上的简介](https://crypto.stackexchange.com/a/1875). 109 | 110 | --- 111 | 112 | ## 例题 113 | 114 | ### CryptoHack - Moving Problems 115 | 116 | ::fold{expand info title="题目大意"} 117 | 给出 Weierstrass 形式的椭圆曲线 $E(\mathbb{F}_p)$ 的参数如下: 118 | 119 | ```python 120 | p = 1331169830894825846283645180581 121 | a = -35 122 | b = 98 123 | E = EllipticCurve(GF(p), [a, b]) 124 | G = E(479691812266187139164535778017, 125 | 568535594075310466177352868412) 126 | ``` 127 | 128 | Alice 和 Bob 通过该椭圆曲线进行 ECDH 密钥交换,你截获两人的公钥如下: 129 | 130 | ```python 131 | A = E(1110072782478160369250829345256, 132 | 800079550745409318906383650948) 133 | B = E(1290982289093010194550717223760, 134 | 762857612860564354370535420319) 135 | ``` 136 | 137 | 求公共密钥 $S$ 的横坐标. 138 | :: 139 | 140 | 此题密钥交换过程天衣无缝,所以问题只可能出现在曲线参数的选择上. 首先对 $G$ 的阶进行分解: 141 | 142 | ```python 143 | print(factor(G.order())) 144 | # 2 * 7 * 271 * 23687 * 1153763334005213 145 | ``` 146 | 147 | 最后一项 $1153763334005213$ 非常大,因此不能用朴素 Pohlig-Hellman 算法求解(可以用 PH 来缩减问题规模,但对于本题没必要). 注意到参数 $a$ 和 $b$ 都很小,尝试计算 Embedding Degree: 148 | 149 | ```python 150 | MAX_K = 6 151 | N = G.order() 152 | for k in range(1, MAX_K + 1): 153 | if p ** k % N == 1: 154 | print(k) # 2 155 | break 156 | ``` 157 | 158 | Embedding Degree 为 $2$,因此可以通过 MOV 攻击求解. 159 | 160 | --- 161 | 162 | ## 相关资源 163 | 164 | - [**Standard curve database**](https://neuromancer.sk/std/) 165 | - [**Python ecdsa 库**](https://pypi.org/project/ecdsa/): 166 | ```python 167 | from ecdsa.ecdsa import Public_key, Private_key, generator_192 168 | from random import randrange 169 | 170 | G = generator_192 171 | n = G.order() 172 | secret = randrange(1, n) 173 | pubkey = Public_key(G, G * secret) 174 | privkey = Private_key(pubkey, secret) 175 | 176 | m = ... 177 | sig = privkey.sign(m, randrange(1, n)) 178 | 179 | assert pubkey.verifies(m, sig) 180 | ``` 181 | - [**SageMath**](https://doc.sagemath.org/html/en/reference/arithmetic_curves/sage/schemes/elliptic_curves/constructor.html): 182 | ```python 183 | from sage.all import * 184 | 185 | p = ... 186 | K = GF(p) 187 | a = K(...) 188 | b = K(...) 189 | E = EllipticCurve(K, [a, b]) 190 | G = E.gens()[0] 191 | ``` 192 | -------------------------------------------------------------------------------- /docs/ctf/crypto/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Crypto 3 | --- 4 | 5 | 通用工具: 6 | 7 | - 一系列攻击代码:[jvdsn/crypto-attacks](https://github.com/jvdsn/crypto-attacks) 8 | 9 | :index 10 | -------------------------------------------------------------------------------- /docs/ctf/crypto/rsa.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: RSA 加密 3 | --- 4 | 5 | **RSA**(Rivest-Shamir-Adleman)是一种**非对称加密算法**. 算法基于数论中的**大数分解**问题,其安全性基于大整数分解的困难性. 6 | 7 | --- 8 | 9 | ## 算法流程 10 | 11 | **密钥生成:** 12 | 13 | 1. **选择大素数 $p$ 和 $q$**,计算 $N = p \times q$. 14 | 2. **计算欧拉函数 $\varphi(N)$** $= (p-1)(q-1)$. 15 | 3. **选择公钥 $e$**,满足 $1 < e < \varphi(N)$ 且 $\gcd(e, \varphi(N)) = 1$. 16 | 4. **计算私钥 $d$**,满足 $d = e^{-1} \bmod{\varphi(N)}$. 17 | 5. **公钥**为 $(N, e)$,**私钥**为 $(N, d)$. 18 | 19 | **加密**:$c = m^e \bmod{N}$. 其中 $m$ 为明文,$c$ 为密文. 20 | 21 | **解密**:$m = c^d \bmod{N}$. 其中 $m$ 为明文,$c$ 为密文. 22 | 23 | --- 24 | 25 | ## 攻击 26 | 27 | 以后再写. 28 | -------------------------------------------------------------------------------- /docs/ctf/game/assets/zjuctf2024-es.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/game/assets/zjuctf2024-es.jpg -------------------------------------------------------------------------------- /docs/ctf/game/assets/zjuctf2024-silence.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/docs/ctf/game/assets/zjuctf2024-silence.png -------------------------------------------------------------------------------- /docs/ctf/game/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 比赛 3 | --- 4 | 5 | ::fold{expand info title="摘要"} 6 | 一些打过或参与出题的 CTF 比赛的 writeup。 7 | :: 8 | 9 | :asterisk 10 | 11 | :index 12 | -------------------------------------------------------------------------------- /docs/ctf/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: CTF 3 | --- 4 | 5 | :index 6 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 主页 3 | displayTitle: Xecades Notes 4 | --- 5 | 6 | ::dot-pattern 7 | :::p{style="font-size: 1.1em"} 8 | Hi []{.waving} 欢迎来到我的笔记本~ 9 | ::: 10 | 11 | :::span{.home-links} 12 | [主页](https://xecades.xyz) 13 | [博客](https://blog.xecades.xyz/) 14 | [GitHub](https://github.com/Xecades/Note) 15 | [CC98](https://www.cc98.org/user/id/731275){title="校内论坛,需要校网连接"} 16 | ::: 17 | :: 18 | 19 | ::grid{align=equal gapx=10px gapy=20px .home-grid} 20 | :sep{span=24} 21 | :::fold{always expand title="关于我" info .fold-fontawesome-list} 22 |  :user: 网名 Xecades,读作 /'zɛkeɪdz/ 23 | 24 |  :graduation-cap: 浙江大学图灵班 CS 专业 23 级本科生,目前大二 25 | 26 |  :flag: CTF 选手,浙江大学 AAA 战队队员 27 | 28 |  :envelope: i$@$xecades.xyz 29 | ::: 30 | 31 | :sep{span=24} 32 | :::fold{always expand title="关于本站" warning} 33 | 推荐使用电脑浏览器访问本站,以获得最佳阅读体验。 34 | 35 | :v{2rem} 36 | 37 | ::::div{style="font-size: 0.9em; line-height: 1.5em;"} 38 | $^\ast$ 本站是「Alpha」项目的子项目,所有代码按 [GPL-3.0 协议](https://github.com/Xecades/Note/blob/main/LICENSE)开源于 [GitHub](https://github.com/Xecades/Note)。 39 | 40 | $^\ast$ 若未特殊说明,本站所有笔记均采用 [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh) 许可协议发布。 41 | :::: 42 | ::: 43 | 44 | :sep{span=12 sm:span=24} 45 | :::fold{always expand title="站点统计" default .fold-fontawesome-list} 46 |  :file-lines: 页面总数:@PAGE_COUNT 47 | 48 |  :pen-nib: 总词数:@WORD_COUNT 49 | 50 |  :calendar-days: 最后更新:@LAST_UPDATE 51 | ::: 52 | 53 | :sep{span=12 sm:span=24} 54 | :::fold{always expand title="最近更新" success .fold-fontawesome-list .home-recent-updates} 55 | @RECENT_UPDATES 56 | ::: 57 | 58 | :: 59 | -------------------------------------------------------------------------------- /docs/misc/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 杂项 3 | --- 4 | 5 | :index 6 | -------------------------------------------------------------------------------- /docs/misc/test/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 测试 3 | --- 4 | 5 | ::fold{expand info title="摘要"} 6 | 一些关于本网站的功能性测试。 7 | 8 | 可能会在稳定后移除。 9 | :: 10 | 11 | :asterisk 12 | 13 | :index 14 | -------------------------------------------------------------------------------- /docs/misc/test/markdown.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Markdown 测试 3 | --- 4 | 5 | 普通文字 6 | 7 | --- 8 | 9 | ## 二级标题 10 | 11 | ### 三级标题 12 | 13 | #### 四级标题 14 | 15 | ##### 五级标题 16 | 17 | ###### 六级标题 18 | 19 | --- 20 | 21 | - [x] 选中 22 | - [ ] 未选中 23 | 24 | --- 25 | 26 | Sunt ullamco esse esse sit aliquip aliquip ea laboris ea nulla. Quis eiusmod enim aliqua consectetur sit ea. Commodo anim enim sit qui nisi culpa labore fugiat nisi est nulla ad. Dolore dolor magna Lorem sunt dolor commodo eu ad aliquip officia officia incididunt deserunt. Adipisicing veniam aliquip incididunt et amet velit nulla quis magna culpa aliqua nulla. Lorem proident Lorem nostrud elit quis quis id duis incididunt reprehenderit. 27 | 28 | Magna do ipsum incididunt nisi nisi do et consectetur excepteur amet. Irure qui aute incididunt velit consectetur nisi et sint elit dolor voluptate. Magna et non ullamco minim commodo amet culpa anim ea excepteur consequat pariatur laboris. Duis reprehenderit ipsum officia duis ut veniam. Ex deserunt labore velit minim laboris veniam magna velit exercitation ea minim. Velit amet veniam aute minim. Dolore occaecat officia minim dolor cupidatat ut sint. 29 | 30 | Tempor voluptate aliquip consectetur excepteur culpa. Et dolor in adipisicing commodo ex magna dolore ut aliquip. Nulla nisi nulla commodo id proident sit proident dolor dolore ipsum ullamco. Fugiat Lorem est sunt excepteur Lorem officia labore. Ut et tempor in exercitation nostrud amet eiusmod. 31 | 32 | Exercitation incididunt dolore cupidatat mollit veniam esse reprehenderit eiusmod cillum eu tempor dolor aliqua minim. Laboris voluptate consequat exercitation nisi ipsum. Cillum anim voluptate sunt est nostrud magna quis ad irure. 33 | 34 | --- 35 | 36 | *斜体*、**粗体**、***加粗斜体***、~~删除线~~ 37 | 38 | --- 39 | 40 | 在一个段落中的[文字链接](https://www.baidu.com),然后是后面的文字。 41 | 42 | --- 43 | 44 | - 无序列表项 1 45 | - 无序列表项 2 46 | - 无序列表项 3 47 | 48 | --- 49 | 50 | 1. 有序列表项 1 51 | 2. 有序列表项 2 52 | 3. 有序列表项 3 53 | 54 | --- 55 | 56 | 图片展示 57 | 58 | ![这里是图片标题,支持 $\LaTeX$ 和**加粗**文字](https://medium-zoom.francoischalifour.com/image-3.a41d7456.jpg) 59 | 60 | ![`cat /data/flag`](https://medium-zoom.francoischalifour.com/image-4.a4d08f7d.jpg) 61 | 62 | ![](https://s2.loli.net/2022/07/09/pTQyYHRSXjLCtFU.png) 63 | 64 | --- 65 | 66 | > > > 三级引用。Proident id dolore consectetur eu nulla anim sint magna veniam culpa mollit anim nostrud elit. Laboris ullamco nulla officia esse deserunt est aliqua. Ex deserunt mollit consectetur consequat duis deserunt et pariatur. Labore pariatur dolor ut excepteur amet ex fugiat amet tempor ullamco aute. Cupidatat non nulla ut laborum dolor nostrud quis quis. 67 | > > 68 | > > 二级引用。Exercitation aliquip commodo voluptate sit nulla. Voluptate laborum commodo esse elit culpa velit. Occaecat consequat pariatur deserunt nulla reprehenderit eiusmod. Consequat nostrud labore do laborum anim duis laborum proident laboris elit. Enim culpa aliqua voluptate aliqua dolor esse nisi culpa consectetur anim minim sint nulla incididunt. 69 | > 70 | > 一级引用。Et amet ea anim ut excepteur consequat amet dolore. Nulla incididunt do et minim do ea consequat aute dolore. Eiusmod exercitation proident aliqua et officia laborum occaecat reprehenderit exercitation ea ut fugiat pariatur. Anim do veniam ex pariatur proident quis id aute consequat incididunt excepteur quis tempor id. 71 | 72 | --- 73 | 74 | > 多个引用 75 | > 76 | > 多个引用 77 | > 78 | > 多个引用 79 | > 80 | > Cupidatat consequat incididunt nostrud laborum incididunt in sunt aute. Elit consectetur consectetur qui fugiat incididunt laborum amet officia quis cupidatat amet ut ullamco. Esse sunt aliqua commodo qui laboris sint enim. 81 | 82 | --- 83 | 84 | 行内代码块 `#include`{language=cpp}。Mollit dolore est in et aliquip adipisicing et nulla id nulla esse laborum minim nulla. In ad irure qui magna Lorem ad eiusmod do do `laborum ex duis`. Reprehenderit nulla nisi laborum incididunt voluptate sunt et cupidatat commodo consectetur deserunt. Non officia aliquip enim duis. Ea irure magna excepteur labore eiusmod officia sit id sint anim ipsum duis labore Lorem. Culpa cillum `voluptate duis exercitation cillum esse incididunt laborum magna`. Est ut commodo non magna nisi sit proident deserunt consectetur ut anim ullamco ut adipisicing. 85 | 86 | > `scanf()`{language=c} 用于输入数据,而 `printf()`{language=c} 用于输出数据。 87 | 88 | --- 89 | 90 | 表格 91 | 92 | | Type | Description | 93 | | :--: | -- | 94 | | `%` | Prints `%`. | 95 | | `d` | Decimal `signed int`. | 96 | | `u` | Decimal `unsigned int`. | 97 | | `o` | Octal `unsigned int`. | 98 | | `x`, `X` | Hexadecimal `unsigned int`. `x` for lower-case and `X` for upper-case. | 99 | | `f` | Float-point types in fixed-point notation. | 100 | | `e`, `E` | Float-point types in exponential notation: `d.ddde±dd`. `e` for lower-case "e" and `E` for upper-case. | 101 | | `g`, `G` | Float-point types in either fixed-point or exponential notation, whichever is more appropriate for its magnitude. `g` for lower-case and `G` for upper-case. | 102 | | `a`, `A` | Float-point in hexadecimal notation, starting with `0x` or `0X`. `a` for lower-case and `A` for upper-case. | 103 | | `s` | `null`-terminated string. | 104 | | `c` | `char`. | 105 | | `p` | `void*` in an implementation-defined format. | 106 | | `n` | Print nothing, but writes the number of characters written so far into an integer pointer parameter. | 107 | 108 | --- 109 | 110 | 代码块 111 | 112 | ```c 113 | #include 114 | 115 | int main() 116 | { 117 | printf("Hello world\n"); 118 | printf("Nulla culpa ut laborum sint esse elit minim sit. Eiusmod et exercitation et laboris voluptate aute et veniam excepteur. Anim dolore culpa commodo adipisicing et aute. In est aliquip duis tempor cillum ullamco qui. Et officia cillum ex et dolor esse magna veniam. Dolor sint velit dolor commodo in. Irure excepteur adipisicing dolore ipsum velit ipsum nisi Lorem pariatur dolor excepteur dolor dolor.\n"); 119 | return 0; 120 | } 121 | ``` 122 | 123 | ``` 124 | 这个代码块不带语言标识 125 | let a = 1; 126 | ``` 127 | -------------------------------------------------------------------------------- /docs/sci/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 数理 3 | --- 4 | 5 | :index 6 | -------------------------------------------------------------------------------- /docs/sci/la/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 线性代数 3 | --- 4 | 5 | ::fold{expand info title="摘要"} 6 | 早期学习线性代数的课程笔记,纯英文记录。 7 | 8 | 保留原文仅作参考。 9 | :: 10 | 11 | :asterisk 12 | 13 | :index 14 | -------------------------------------------------------------------------------- /docs/sci/la/prove-that.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Prove That... 3 | --- 4 | 5 | C'mon, prove these linear algebra things! 6 | 7 | ::note{info} 8 | For the content below, I use $W\leqslant V$ to denote $W$ is a subspace of $V$, and $\exists!$ to denote "there exists a unique". 9 | :: 10 | 11 | ## Group, Ring & Field 12 | 13 | 1. $\mathbb{Q}(\sqrt{2}) = \{a+b\sqrt{2}\mid a,b\in\mathbb{Q}\}$ is a field. 14 | 2. $\mathbb{Q}$ is the smallest number field (i.e. subfield of $\mathbb{C}$). 15 | 16 | --- 17 | 18 | ## Linear Space 19 | 20 | 1. For $W\subset V(\mathbf{F})$, $W\leqslant V\iff W$ is closed under addition and scalar multiplication. 21 | 2. $S_1\subset S_2\subset V(\mathbf{F})\Rightarrow\operatorname{span}S_1\subset\operatorname{span}S_2$. 22 | 3. If $S_1$ and $S_2$ are linearly independent, then $\operatorname{span}S_1=\operatorname{span}S_2\Rightarrow|S_1|=|S_2|$. 23 | 4. Functions $\exp(\lambda_1x)$, $\exp(\lambda_2x)$ and $\exp(\lambda_3x)$ are linearly independent. ($\lambda_1$, $\lambda_2$ and $\lambda_3$ are distinct) 24 | 5. For $W_1, W_2\leqslant V(\mathbf{F})$: 25 | 1. $W_1\cup W_2\leqslant V\iff W_1\subset W_2$ or $W_2\subset W_1$. 26 | 2. $W_1 + W_2 = \operatorname{span}(W_1\cup W_2)$. 27 | 3. $\dim(W_1 + W_2) = \dim W_1 + \dim W_2 - \dim(W_1\cap W_2)$. 28 | 6. For $W_1, W_2\leqslant V(\mathbf{F})$, the following propositions are equivalent: 29 | 1. $W_1 \cap W_2 = \{0\}$. 30 | 2. $\forall\alpha\in W_1 + W_2$, $\exists!\alpha_1\in W_1$ and $\exists!\alpha_2\in W_2$ such that $\alpha = \alpha_1 + \alpha_2$. 31 | 3. If $0 = \alpha_1 + \alpha_2 (\alpha_1 \in W_1, \alpha_2 \in W_2)$, then $\alpha_1 = \alpha_2 = 0$. 32 | 4. $\dim(W_1 + W_2) = \dim W_1 + \dim W_2$. 33 | 34 | --- 35 | 36 | ## Inner Product Space 37 | 38 | 1. (*Cauchy–Schwarz inequality*) $|\langle\alpha,\beta\rangle|\leq \|\alpha\|\cdot\|\beta\|$. 39 | 2. (*Triangle inequality*) $\|\alpha\|+\|\beta\| \geq \|\alpha+\beta\|$. 40 | 3. (*Pythagorean theorem*) $\|\alpha\|^2+\|\beta\|^2 = \|\alpha+\beta\|^2\iff\alpha\perp\beta\iff\angle(\alpha,\beta)=\dfrac{\pi}{2}$. 41 | 4. (*Gram–Schmidt process*) Any Euclidean space has an orthonormal basis. (The method to construct it is called Gram–Schmidt process.) 42 | 5. If $B = \{\varepsilon_1, \varepsilon_2, \cdots, \varepsilon_n\}$ is an orthonormal basis of $V(\mathbf{F})$, then $\forall\alpha\in V$, $\alpha = \sum\limits_{i=1}^n\langle\alpha,\varepsilon_i\rangle\cdot\varepsilon_i$. 43 | 44 | --- 45 | 46 | ## Linear Transformation 47 | 48 | 1. $\sigma:V\to W$ is injective $\iff\ker\sigma = \{0\}$. 49 | 2. $\sigma:V\to W$ is surjective $\iff\operatorname{im}\sigma = W$. 50 | 3. If $B=\{\alpha_1,\alpha_2,\dots,\alpha_n\}$ is a basis of $V$, then $\forall \beta_1, \beta_2, \dots, \beta_n\in W$, $\exists !\sigma:V \to W$ such that $\sigma(\alpha_i) = \beta_i$. 51 | 4. For $\sigma: V \to W$, $\operatorname{rank}\sigma + \dim\ker\sigma = \dim V$. 52 | 5. For $\sigma: V \to W$, if $\dim V = \dim W = n$, the following propositions are equivalent: 53 | 1. $\sigma$ is injective. 54 | 2. $\sigma$ is surjective. 55 | 3. $\operatorname{rank}\sigma = n$. 56 | 6. $V\cong W \iff \dim V = \dim W$. 57 | 58 | --- 59 | 60 | ## Matrix 61 | 62 | 1. $\mathbf{M}(\sigma)$ and $\sigma$ are one-to-one. 63 | 2. Prove with matrix: if $\dim V(\mathbf{F})=m$, $\dim W(\mathbf{F})=n$, then $\mathcal{L}(V, W)\cong \mathbf{F}^{m\times n}$. 64 | 3. Prove with matrix: $\dim\mathcal{L}(V, W)=\dim V\cdot\dim W$. 65 | -------------------------------------------------------------------------------- /docs/sci/la/what-is.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: What is... 3 | --- 4 | 5 | Here are some easy-to-forget terms in linear algebra. Check whether you are familiar with them all. 6 | 7 | ## Set 8 | 9 | 1. **Power set**, or $P(A)$ and $2^A$ for a set $A$. 10 | 11 | --- 12 | 13 | ## Group and Field 14 | 15 | 1. **Semigroup**. 16 | 2. **Monoid**. 17 | 3. **Group**, or $\langle G:\circ\rangle$. 18 | 4. **Abelian group**. 19 | 5. **Field**, or $\langle F:+, \circ\rangle$. 20 | 21 | --- 22 | 23 | ## Vector Space 24 | 25 | 1. **Dimension**, or $\dim V$. 26 | 2. **Rank**, or $\operatorname{rank} A$. 27 | 3. For $W_1, W_2\leq V(\mathbf{F})$, what is $W_1+W_2$, $W_1\oplus W_2$. 28 | 4. **Inner product space**. 29 | 5. **Euclidean space**. 30 | 6. **Schmidt orthonormalization**. 31 | -------------------------------------------------------------------------------- /docs/sci/ma/cheatsheet.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Cheatsheet 3 | --- 4 | 5 | ::fold{expand success title="Disclaimer"} 6 | By saying "cheatsheet" I don't mean something used for cheating, but rather a quick reference for the most important concepts and formulas. I don't encourage cheating in any way. 7 | :: 8 | 9 | ## Equivalent Infinitesimals 10 | 11 | When $x\to0$: 12 | 13 | - $\sin x\sim \tan x\sim\arcsin x\sim\arctan x\sim x$ 14 | - $1 - \cos x \sim \dfrac{1}{2} x ^ 2$ 15 | - $\ln(1 + x) \sim e^x - 1 \sim x$ 16 | - $(1 + x) ^ a - 1 \sim ax$ 17 | 18 | --- 19 | 20 | ## Derivatives 21 | 22 | - $(\tan x)' = \sec ^ 2 x \longleftrightarrow (\cot x)' = - \csc ^ 2 x$ 23 | - $(\sec x)' = \sec x \tan x \longleftrightarrow (\csc x)' = - \csc x \cot x$ 24 | - $(\arcsin x)' = \dfrac{1}{\sqrt{1 - x ^ 2}} \longleftrightarrow (\arccos x)' = - \dfrac{1}{\sqrt{1 - x ^ 2}}$ 25 | - $(\arctan x)' = \dfrac{1}{1 + x ^ 2} \longleftrightarrow (\operatorname{arccot} x)' = - \dfrac{1}{1 + x ^ 2}$ 26 | 27 | --- 28 | 29 | ## Integrals 30 | 31 | - $\displaystyle\int\dfrac{x}{\sqrt{a^2 \pm x^2}}\mathrm{d}x = \pm\sqrt{a^2 \pm x^2}+C$ 32 | - $\displaystyle\int\dfrac{1}{\sqrt{x^2+a^2}}\mathrm{d}x = \ln\left(x+\sqrt{x^2+a^2}\right)+C$ 33 | - $\displaystyle\int\dfrac{1}{\sqrt{a^2-x^2}}\mathrm{d}x = \arcsin\dfrac{x}{a}+C$ 34 | - $\displaystyle\int\dfrac{\mathrm{d}x}{x^2 - a^2} = \dfrac{1}{2a}\int\left(\dfrac{1}{x-a}-\dfrac{1}{x+a}\right)\mathrm{d}x = \dfrac{1}{2a}\ln\left|\dfrac{x-a}{x+a}\right|+C$ 35 | - $\displaystyle\int\dfrac{\mathrm{d}x}{x^2 + a^2} = \dfrac{1}{a}\arctan\dfrac{x}{a}+C$ 36 | - $\displaystyle\int\sqrt{a^2-x^2}\mathrm{d}x = \dfrac{x}{2}\sqrt{a^2-x^2}+\dfrac{a^2}{2}\arcsin\dfrac{x}{a}+C$, $a>0$ 37 | - $\displaystyle\int\sqrt{x^2\pm a^2}\mathrm{d}x = \dfrac{x}{2}\sqrt{x^2\pm a^2}+\dfrac{a^2}{2}\ln\left(x+\sqrt{x^2\pm a^2}\right)+C$, $a>0$ 38 | - $\int\tan x\mathrm{d}x = -\ln|\cos x|+C$ 39 | - $\int\cot x\mathrm{d}x = \ln|\sin x|+C$ 40 | - $\int\sec x\mathrm{d}x = \ln|\sec x+\tan x|+C$ 41 | - $\int\csc x\mathrm{d}x = -\ln|\csc x+\cot x|+C = \ln|\csc x-\cot x|+C$ 42 | - $\int\sec^3x\mathrm{d}x = \dfrac{1}{2}\sec x\tan x+\dfrac{1}{2}\ln|\sec x+\tan x|+C$ 43 | 44 | --- 45 | 46 | ## Taylor Series 47 | 48 | With $\theta\in(0, 1)$ and $\xi = x_0 + \theta(x - x_0)$: 49 | 50 | - $f(x) = f(x_0) + f'(x_0)(x-x_0) + \cdots + \dfrac{f^{(n)}(x_0)}{n!}(x-x_0)^n + \dfrac{f^{(n+1)}(\xi)}{(n+1)!}(x-x_0)^{n+1}$ 51 | - $e^x = 1 + x + \dfrac{x^2}{2!} + \dfrac{x^3}{3!} + \cdots + \dfrac{x^n}{n!} + \dfrac{e^{\theta x}}{(n+1)!}x^{n+1}$ 52 | - $\sin x = x - \dfrac{x^3}{3!} + \dfrac{x^5}{5!} - \cdots + (-1)^n\dfrac{x^{2n+1}}{(2n+1)!} + (-1)^{n+1}\dfrac{\cos\theta x}{(2n+3)!}x^{2n+3}$ 53 | - $\cos x = 1 - \dfrac{x^2}{2!} + \dfrac{x^4}{4!} - \cdots + (-1)^n\dfrac{x^{2n}}{(2n)!} + (-1)^{n+1}\dfrac{\sin\theta x}{(2n+2)!}x^{2n+2}$ 54 | - $\ln(1 + x) = x - \dfrac{x^2}{2} + \dfrac{x^3}{3} - \cdots + (-1)^{n-1}\dfrac{x^n}{n} + o(x^n)$ 55 | - $(1+x)^\alpha = \dbinom{\alpha}{0} + \dbinom{\alpha}{1}x + \dbinom{\alpha}{2}x^2 + \cdots + \dbinom{\alpha}{n}x^n + o(x^n)$ 56 | - $\dfrac{1}{1-x} = 1 + x + x^2 + \cdots + x^n + o(x^n)$ 57 | - $\arctan x = x - \dfrac{x^3}{3} + \dfrac{x^5}{5} + \cdots + (-1)^n\dfrac{x^{2n+1}}{2n+1} + o(x^{2n+1})$ 58 | - $\arcsin x = x + \dfrac{x^3}{6} + \dfrac{3x^5}{40} + o(x^5)$ 59 | - $\arccos x = \dfrac{\pi}{2} - \arcsin x$ 60 | -------------------------------------------------------------------------------- /docs/sci/ma/completeness-of-real-numbers.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Completeness of the Real Numbers 3 | --- 4 | 5 | ## Least upper bound property 6 | 7 | Every nonempty subset of $\mathbb{R}$ having an upper bound has a least upper bound. 8 | 9 | ## Monotone convergence theorem 10 | 11 | If a sequence of $\mathbb{R}$ is monotone and bounded, then it converges. 12 | 13 | ## Nested interval theorem 14 | 15 | For a sequence of **closed** intervals $\{[a_n,b_n]\}$, if $[a_{n+1},b_{n+1}]\subset[a_n,b_n]$ for all $n\in\mathbb{N}^\star$ and $\lim\limits_{n\to\infty}{|a_n-b_n|}=0$, then exists exactly one $\xi\in\mathbb{R}$ so that $\xi\in[a_n,b_n]$ for all $n\in\mathbb{N}^\star$. 16 | 17 | ## Bolzano-Weierstrass theorem 18 | 19 | Every bounded sequence of $\mathbb{R}$ has a convergent subsequence. 20 | 21 | ## Cauchy criterion 22 | 23 | A sequence of $\mathbb{R}$ is convergent if and only if it is a fundamental sequence. 24 | 25 | > **Fundamental sequence**: A sequence $\{x_n\}$ is called a fundamental sequence if for all $\varepsilon > 0$, exists $N\in\mathbb{N}^\star$ so that $|x_{n+p} - x_n| < \varepsilon$ for all $n>N$ and $p\in\mathbb{N}^\star$. Fundamental sequence is also known as Cauchy sequence. 26 | 27 | ## Heine-Borel theorem 28 | 29 | If $H$ is an infinite **open** cover of **closed** interval $[a, b]$, then exists a finite subset $H_0\subset H$ so that $H_0$ is an **open** cover of $[a,b]$. 30 | 31 | > Or: If $H = \{(a_\alpha, b_\alpha) | \alpha \in \Lambda\}$ which satisfies $\bigcup\limits_{\alpha\in\Lambda}{(a_\alpha, b_\alpha)} \supset [a, b]$, then exists a finite subset $H_0\subset H$ so that $\bigcup\limits_{(a_\alpha, b_\alpha)\in H_0}{(a_\alpha, b_\alpha)} \supset [a, b]$. 32 | -------------------------------------------------------------------------------- /docs/sci/ma/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 数学分析 3 | --- 4 | 5 | ::fold{expand info title="摘要"} 6 | 早期学习数学分析的课程笔记,纯英文记录。 7 | 8 | 保留原文仅作参考。 9 | :: 10 | 11 | :asterisk 12 | 13 | :index 14 | -------------------------------------------------------------------------------- /docs/sci/ma/prove-that.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Prove That... 3 | --- 4 | 5 | Here are theorems and proofs that are essential in mathematical analysis. It would be nice if you could prove them smoothly. 6 | 7 | ## Set 8 | 9 | 1. (*De Morgan's laws*) 10 | - $(A \cup B)^C = A^C \cap B^C$ 11 | - $(A \cap B)^C = A^C \cup B^C$ 12 | 2. Any countable union of countable sets is a countable set. 13 | 14 | --- 15 | 16 | ## Limit of a sequence 17 | 18 | 1. Prove with definition: $\lim\limits_{n\to\infty}{\sqrt[n]{n}} = 1$. 19 | 2. If $\lim\limits_{n\to\infty}{a_n} = a$, then $\lim\limits_{n\to\infty}{\dfrac{a_1+a_2+\cdots+a_n}{n}} = a$. 20 | 3. For convergent sequences $\{x_n\}$ and $\{y_n\}$, if $\lim\limits_{n\to\infty}x_n=a$, $\lim\limits_{n\to\infty}y_n=b$, and $aN$. 21 | 4. If $\lim\limits_{n\to\infty}x_n=a$, $\lim\limits_{n\to\infty}y_n=b\neq0$, then $\lim\limits_{n\to\infty}\dfrac{x_n}{y_n}=\dfrac{a}{b}$. 22 | 5. $a_n = \left(1 + \dfrac{1}{n}\right)^n$ and $b_n = \left(1 + \dfrac{1}{n}\right)^{n + 1}$ converges to $e$. 23 | 6. $a_n = 1 + \dfrac{1}{1!} + \dfrac{1}{2!} + \cdots + \dfrac{1}{n!}$ converges to $e$. 24 | 7. $a_n = 1 + \dfrac{1}{2} + \dfrac{1}{3} + \cdots + \dfrac{1}{n} - \ln n$ converges to $\gamma$. 25 | 8. $\lim\limits_{n\to\infty}a_n=A\iff\lim\limits_{n\to\infty}a_{2n-1}=\lim\limits_{n\to\infty}a_{2n}=A$. 26 | 9. $\{a_n\}$ is convergent $\iff$ every non-trivial subsequence $\{a_{n_k}\}$ of $\{a_n\}$ converges. 27 | 10. $a_n = \sin n$ is divergent. 28 | 11. Prove with *Nested interval theorem*: $\mathbb{R}$ is uncountable. 29 | 12. Refer to [Completeness of the Real Numbers](completeness-of-real-numbers), and prove 30 | 1. *Least upper bound* $\Rightarrow$ *Monotone convergence*. 31 | 2. *Monotone convergence* $\Rightarrow$ *Nested interval*. 32 | 3. *Nested interval* $\Rightarrow$ *Bolzano-Weierstrass*. 33 | 4. *Bolzano-Weierstrass* $\Rightarrow$ *Cauchy criterion*. 34 | 5. *Cauchy criterion* $\Rightarrow$ *Least upper bound*. 35 | 6. *Cauchy criterion* $\Rightarrow$ *Nested interval*. 36 | 7. *Nested interval* $\Rightarrow$ *Least upper bound*. 37 | 38 | --- 39 | 40 | ## Limit of a function 41 | 42 | 1. If $\lim\limits_{x\to a}f(x)=A$, $\lim\limits_{x\to a}g(x)=B$ and $A>B$, then exists $\delta>0$ so that $f(x)>g(x)$ for all $x\in\mathring{U}(x_0, \delta)$. 43 | 2. If $\lim\limits_{x\to x_0}f(x)=A$, then exists $\delta>0$ so that $f(x)$ is bounded in $\mathring{U}(x_0, \delta)$. 44 | 3. Prove with definition: $\lim\limits_{x\to0}\dfrac{\sin x}{x} = 1$. 45 | 4. (*Heine's theorem*) The necessary and sufficient condition for $\lim\limits_{x\to a}f(x)=A$ is that for all sequences $\{x_n\}$ which converges to $a$ and $x_n\neq a$, $\lim\limits_{n\to\infty}f(x_n)=A$. 46 | 5. Prove with *Heine's theorem*: $f(x)=\sin\dfrac{1}{x}$ has no limit as $x\to0$. 47 | 6. (*Cauchy criterion*) Prove with *Heine's theorem*: $\lim\limits_{x\to a}f(x)$ exists if and only if for all $\varepsilon>0$, exists $\delta>0$ so that $|f(x)-f(y)|<\varepsilon$ for all $x,y\in\mathring{U}(x_0, \delta)$. 48 | 7. Prove with definition: $\lim\limits_{x\to\infty}\left(1+\dfrac{1}{x}\right)^x=e$. 49 | 50 | --- 51 | 52 | ## Continuity 53 | 54 | 1. Every irrational point of $R(x)$ is continuous, every rational point of $R(x)$ is removable discontinuous, where 55 | > $$R(x)=\left\{\begin{align}\frac{1}{q}&\quad \text{if}\ x=\frac{p}{q}\text{, with}\ p\in\mathbb{Z}\ \text{and}\ q\in\mathbb{N}\ \text{coprime.}\\0&\quad \text{if}\ x\ \text{is irrational.}\end{align}\right.$$ 56 | 2. If $u=g(x)$ is continuous at $x_0$, and $y=f(u)$ is continuous at $u_0=g(x_0)$, then $f\circ g(x)=f(g(x))$ is continuous at $x_0$. 57 | 3. If $f(x)$ is continuous in closed interval $[a, b]$, 58 | 1. then it is bounded in $[a, b]$. 59 | 2. then $\max f(x)$ and $\min f(x)$ exists in $[a, b]$. 60 | 3. and $f(a)\cdot f(b)<0$, then exists $\xi\in(a, b)$ so that $f(\xi)=0$. 61 | 4. then it can reach all values between $\min f(x)$ and $\max f(x)$. 62 | 5. (*Cantor's theorem*) then it is uniformly continuous in $[a, b]$. 63 | 4. The sufficient and necessary condition for $f(x)$ to be uniformly continuous in $D$ is that for all sequences $\{x_n\}, \{y_n\}\in D^\mathbb{N}$ that satisfies $\lim\limits_{n\to\infty}(x_n-y_n)=0$, $\lim\limits_{n\to\infty}(f(x_n)-f(y_n))=0$. 64 | 5. If $f(x)$ is continuous in finite open interval $(a, b)$, then $f(x)$ is uniformly continuous on $(a, b)$ if and only if $\lim\limits_{x\to a^+}f(x)$ and $\lim\limits_{x\to b^-}f(x)$ exist. 65 | 66 | --- 67 | 68 | ## Derivative 69 | 70 | 1. (*Darboux's theorem*) If $f(x)$ is differentiable in $(a, b)$, then for every $y$ between $f'(a)$ and $f'(b)$, there exists $\xi\in(a, b)$ so that $f'(\xi)=y$. 71 | 2. (*Rolle's theorem*) If $f(x)$ is continuous in $[a, b]$, differentiable in $(a, b)$, and $f(a)=f(b)$, then exists $\xi\in(a, b)$ so that $f'(\xi)=0$. 72 | 3. If $f(x)$ is twice differentiable in $[a, b]$, and $f(a) = f(b) = 0$, then $\forall x\in(a, b)$, $\exists\xi\in(a, b)$, such that $2f(x) = f''(\xi)(x - a)(x - b)$. 73 | 4. If $f'(x)$ is bounded in $(a, b)$, then $f(x)$ is uniformly continuous in $(a, b)$. 74 | 5. If $f(x)$ is twice differentiable at $x=0$, $\lim\limits_{x\to0}\dfrac{f(x)}{x}=0$, and $f''(0)=4$, 75 | 1. find $\lim\limits_{x\to0}\dfrac{f(x)}{x^2}$ and $\lim\limits_{x\to0}\left(1+\dfrac{f(x)}{x}\right)^{1/x}$. (answer: $2$ and $e^2$) 76 | 2. point out two mistakes: $\lim\limits_{x\to0}\dfrac{f(x)}{x^2}=\lim\limits_{x\to0}\dfrac{f'(x)}{2x}=\lim\limits_{x\to0}\dfrac{f''(x)}{2}=\dfrac{1}{2}f''(0)=2$. 77 | 6. Prove using *Taylor series with Lagrange remainder*: $e$ is irrational. 78 | -------------------------------------------------------------------------------- /docs/sci/phy/chapter-27.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.27 Gauss' Law 3 | --- 4 | 5 | ## 流量 Flux 6 | 7 | ::fold{title="流量" expand} 8 | 单位时间流过某表面的体积. 9 | :: 10 | 11 | 对于平面 $\vec{A}$: 12 | 13 | $$ 14 | \Phi = \vec{v}\cdot\vec{A} = \frac{L}{t}\cdot A = \frac{V}{t} 15 | $$ 16 | 17 | 一般式: 18 | 19 | $$ 20 | \Phi = \oiint \vec{v}\cdot\mathrm{d}\vec{A} 21 | $$ 22 | 23 | 对于闭合曲面,若内部无源无汇,则 $\Phi = \displaystyle\oiint \vec{v}\cdot\mathrm{d}\vec{A} = 0$;若有源,则 $\Phi \geqslant 0$,代表流出;若有汇,则 $\Phi \leqslant 0$,代表流入. 24 | 25 | --- 26 | 27 | ## Gauss' Law 28 | 29 | ::fold{title="电通量" expand} 30 | $$ 31 | \Phi_E := \displaystyle\oiint \vec{E}\cdot\mathrm{d}\vec{A} 32 | $$ 33 | :: 34 | 35 | ::fold{title="Gauss' Law" success expand} 36 | $$ 37 | \begin{align*} 38 | \text{积分形式}&\quad \Phi_E = \oiint \vec{E}\cdot\mathrm{d}\vec{A} \equiv \frac{q_{\text{enc}}}{\varepsilon_0} \\ 39 | \text{微分形式}&\quad \nabla\cdot\vec{E} = \frac{\rho}{\varepsilon_0} 40 | \end{align*} 41 | $$ 42 | :: 43 | 44 | > $q_{\text{enc}}$:闭合曲面内包围的电荷量. 45 | > 46 | > $\rho$:电荷体积密度. 47 | > 48 | > $\nabla$ 算符:$\nabla = \dfrac{\partial}{\partial x}\vec{\imath} + \dfrac{\partial}{\partial y}\vec{\jmath} + \dfrac{\partial}{\partial z}\vec{k}$ 49 | 50 | --- 51 | 52 | ### 微分形式推导 53 | 54 | 首先由于数学上的[高斯公式](https://zh.wikipedia.org/wiki/%E9%AB%98%E6%96%AF%E6%95%A3%E5%BA%A6%E5%AE%9A%E7%90%86),有 55 | 56 | $$ 57 | \Phi_E = \oiint \vec{E}\cdot\mathrm{d}\vec{A} = \iiint \nabla\cdot\vec{E}\cdot\mathrm{d}V 58 | $$ 59 | 60 | 而 61 | 62 | $$ 63 | \frac{q_{\text{enc}}}{\varepsilon_0} = \iiint \frac{\rho}{\varepsilon_0}\cdot\mathrm{d}V 64 | $$ 65 | 66 | 两式相等,故有 67 | 68 | $$ 69 | \nabla\cdot\vec{E} = \frac{\rho}{\varepsilon_0} 70 | $$ 71 | 72 | 证毕. 73 | 74 | --- 75 | 76 | ### 推 Coloumb's Law 77 | 78 | 点电荷周围电场球形对称,取半径为 $R$ 的球面为高斯面,有 79 | 80 | $$ 81 | \Phi_E = \oiint \vec{E}\cdot\mathrm{d}\vec{A} = E\cdot\oiint\mathrm{d}\vec{A} = E\cdot S = E\cdot 4\pi R^2 82 | $$ 83 | 84 | 故 $E\cdot 4\pi R^2 = \dfrac{q}{\varepsilon_0}$,即 85 | 86 | $$ 87 | E = \frac{1}{4\pi\varepsilon_0}\frac{q}{R^2} 88 | $$ 89 | 90 | 即为 Coulomb's Law. 91 | 92 | --- 93 | 94 | ## 应用 95 | 96 | ### 均匀带电球体 97 | 98 | ::fold{title="均匀带电球体" expand} 99 | 半径为 $a$ 的均匀带电球体,电荷密度为 $\rho$,求球体内外的电场强度. 100 | :: 101 | 102 | 当 $r > a$ 时,取半径为 $r$ 的球面为高斯面,有 103 | 104 | $$ 105 | \Phi_E = \oiint \vec{E}\cdot\mathrm{d}\vec{A} = E\cdot 4\pi r^2 = \frac{Q}{\varepsilon_0} 106 | $$ 107 | 108 | 故 $E = \dfrac{1}{4\pi\varepsilon_0}\dfrac{Q}{r^2} = \dfrac{\rho a^3}{3\varepsilon_0 r^2}$(和点电荷的结果一致). 109 | 110 | 当 $r < a$ 时,有 111 | 112 | $$ 113 | \Phi_E = \oiint \vec{E}\cdot\mathrm{d}\vec{A} = E\cdot 4\pi r^2 = \frac{q_{\text{enc}}}{\varepsilon_0} 114 | $$ 115 | 116 | 其中 117 | 118 | $$ 119 | q_{\text{enc}} = \rho\cdot\frac{4}{3}\pi r^3 120 | $$ 121 | 122 | 故 $E = \dfrac{\rho}{3\varepsilon_0}r$($\propto r$). 123 | 124 | --- 125 | 126 | ### 电荷分布于导体表面 127 | 128 | 首先导体内电场一定为 $0$,否则会有电荷在导体内部运动,违背静电平衡. 129 | 130 | 因此 $\Phi_E = \displaystyle\oiint \vec{E}\cdot\mathrm{d}\vec{A} = 0$,故 $Q = 0$,即导体内部电荷量为 $0$,说明导体所有电荷都分布于表面. 131 | -------------------------------------------------------------------------------- /docs/sci/phy/chapter-28.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.28 Electric Potential Energy and Potential 3 | --- 4 | 5 | ## 电势 6 | 7 | 单位正电荷在电场中的电势能. 8 | 9 | --- 10 | 11 | ## 电场强度 $\to$ 电势 12 | 13 | AB 两点电势差 14 | 15 | $$ 16 | \Delta V_{AB} = V_B - V_A \equiv -\int_A^B \vec{E}\cdot\mathrm{d}\vec{l} 17 | $$ 18 | 19 | 若取无穷远为势能零点,**即取 $A$ 为 $\infty$**,则 20 | 21 | $$ 22 | V_B = \int_B^\infty \vec{E}\cdot\mathrm{d}\vec{l} 23 | $$ 24 | 25 | --- 26 | 27 | ## 电势 $\to$ 电场强度 28 | 29 | $$ 30 | \vec{E} = -\vec{\nabla}V 31 | $$ 32 | 33 | 笛卡尔坐标系下 34 | 35 | $$ 36 | \vec{\nabla}V = \frac{\partial V}{\partial x}\vec{\imath} + \frac{\partial V}{\partial y}\vec{\jmath} + \frac{\partial V}{\partial z}\vec{k} 37 | $$ 38 | 39 | 球坐标系下 40 | 41 | $$ 42 | \vec{\nabla}V = \frac{\partial V}{\partial r}\vec{r} + \frac{1}{r}\frac{\partial V}{\partial \theta}\vec{\theta} + \frac{1}{r\sin\theta}\frac{\partial V}{\partial \varphi}\vec{\varphi} 43 | $$ 44 | -------------------------------------------------------------------------------- /docs/sci/phy/chapter-29-30.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.29 / 30 Capacitance and Dielectrics 3 | --- 4 | 5 | ## 电容 6 | 7 | $$ 8 | C = \frac{q}{\Delta V} 9 | $$ 10 | 11 | $C$ 类比容积,$\Delta V$ 类比压强,$q$ 类比分子数. 压强越大,能容纳的分子数越多. 12 | 13 | 计算任意电容: 14 | 15 | 1. 假设电荷 $q$; 16 | 2. 计算电场强度 $E$(Gauss's Law); 17 | 3. 积分得到电势差 $\Delta V$; 18 | 4. 计算电容 $C = \dfrac{q}{\Delta V}$. 19 | 20 | 平行板电容器 $C = \dfrac{\varepsilon_0 A}{d}$. 21 | 22 | --- 23 | 24 | ## 串并联 25 | 26 | 和电阻串并联相反. 27 | 28 | $$ 29 | \begin{align*} 30 | \text{串联}&\quad \frac{1}{C} = \frac{1}{C_1} + \frac{1}{C_2} + \cdots \\ 31 | \text{并联}&\quad C = C_1 + C_2 + \cdots 32 | \end{align*} 33 | $$ 34 | 35 | --- 36 | 37 | ## 能量 38 | 39 | $$ 40 | U = \frac{1}{2}CV^2 = \frac{1}{2}\frac{Q^2}{C} 41 | $$ 42 | 43 | 能量密度(单位体积的能量): 44 | 45 | $$ 46 | u = \dfrac{U}{V} = \dfrac{1}{2}\varepsilon_0 E^2 47 | $$ 48 | 49 | --- 50 | 51 | TBD 52 | -------------------------------------------------------------------------------- /docs/sci/phy/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 物理学 3 | --- 4 | 5 | ::fold{expand info title="摘要"} 6 | 浙江大学《普通物理学 II(H)》课程笔记。 7 | 8 | 修读时间:2024 年秋冬学期。 9 | :: 10 | 11 | :asterisk 12 | 13 | :index 14 | -------------------------------------------------------------------------------- /docs/sci/prob/chapter-1.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.1 事件及其概率 3 | --- 4 | 5 | ## 公理化定义 6 | 7 | ::fold{title="概率空间 $(\Omega,\mathcal{F},P)$" expand success} 8 | 1. **样本空间 $\Omega$**:样本点 $\omega$ 的全体. 9 | 2. **事件域 $\mathcal{F}$**:$\Omega$ 中满足以下条件的子集的全体: 10 | - $\Omega\in\mathcal{F}$; 11 | - 若 $A\in\mathcal{F}$,则 $\overline{A}\in\mathcal{F}$; 12 | - 若 $A_1,A_2,\ldots\in\mathcal{F}$,则 $\bigcup_{i=1}^\infty A_i\in\mathcal{F}$. 13 | 3. **概率测度 $P$**:$\mathcal{F}\to\mathbb{R}$ 的映射,满足: 14 | - 非负性:$P(A)\geqslant 0$; 15 | - 规范性:$P(\Omega)=1$; 16 | - 可列可加性:若 $A_1,A_2,\ldots\in\mathcal{F}$ 两两互不相容,则 17 | $$ 18 | P\left(\sum_{i=1}^\infty A_i\right)=\sum_{i=1}^\infty P(A_i). 19 | $$ 20 | :: 21 | 22 | $\mathcal{F}$ 称为 $\Omega$ 上的 $\sigma$-代数或 $\sigma$-域,$\mathcal{F}$ 中的元素($\Omega$ 的子集)称为事件. 23 | 24 | 若 $\Omega = \mathbb{R}^n$,取一切左开右闭的 $n$ 维矩形及其(有限或可列)并、(有限或可列)交、逆所组成的集合为 $\mathcal{F}$,记作 $\mathcal{B}^n$,称为 $n$ 维 Borel $\sigma$-代数. 25 | 26 | --- 27 | 28 | - 若 $A_1,A_2,\ldots,A_n$ 两两互不相容,则 29 | $$ 30 | P\left(\sum_{i=1}^n A_i\right)=\sum_{i=1}^n P(A_i). 31 | $$ 32 | - 若 $B\subset A$,则 $P(A-B)=P(A)-P(B)$.(其实 $A-B$ 的写法已经蕴含了 $B\subset A$.) 33 | - $P(A\cup B)=P(A)+P(B)-P(AB)$. 34 | - $P(A\setminus B)=P(A)-P(AB)$. 35 | 36 | --- 37 | 38 | ## 条件概率 39 | 40 | 在事件 $B$ 发生的条件下 $A$ 发生的概率 41 | 42 | $$ 43 | P(A\mid B)=\frac{P(AB)}{P(B)}. 44 | $$ 45 | 46 | 有乘法公式 47 | 48 | $$ 49 | P(AB)=P(A\mid B)\cdot P(B). 50 | $$ 51 | 52 | 推广: 53 | 54 | $$ 55 | P(A_1A_2\cdots A_n)=P(A_1)\cdot P(A_2\mid A_1)\cdot P(A_3\mid A_1A_2)\cdots P(A_n\mid A_1A_2\cdots A_{n-1}). 56 | $$ 57 | 58 | --- 59 | 60 | ### 完备事件组 61 | 62 | ::fold{title="完备事件组" expand} 63 | 满足以下条件的事件列 $\{A_1,A_2,\cdots,A_n,\cdots\}$ 称为 $\Omega$ 的一个完备事件组: 64 | 65 | 1. $A_i$ 两两互不相容,且 $P(A_i)>0$; 66 | 2. $\sum_{i=1}^\infty A_i=\Omega$. 67 | 68 | 最简单的完备事件组是 $\{A,\overline{A}\}$. 69 | :: 70 | 71 | --- 72 | 73 | ### 全概率公式 74 | 75 | ::fold{title="全概率公式" expand success} 76 | 设 $\{A_1,A_2,\cdots,A_n,\cdots\}$ 是一个完备事件组,则对任一事件 $B$ 有 77 | 78 | $$ 79 | P(B)=\sum_{i=1}^\infty P(A_i)\cdot P(B\mid A_i). 80 | $$ 81 | :: 82 | 83 | > 注意到 $A_iB\subset A_i$,故 $(A_iB)$ 两两互不相容,由可列可加性得 84 | > 85 | > $$ 86 | > \begin{align*} 87 | > P(B)=P(B\Omega)&=P\left(B\sum_{i=1}^\infty A_i\right)=P\left(\sum_{i=1}^\infty A_iB\right)\\ 88 | > &=\sum_{i=1}^\infty P(A_iB)=\sum_{i=1}^\infty P(A_i)\cdot P(B\mid A_i). 89 | > \end{align*} 90 | > $$ 91 | > 92 | > 证毕. 93 | 94 | 全概率公式本质是一个加权平均,$P(A_i)$ 即为权重. 95 | 96 | --- 97 | 98 | ### 贝叶斯公式 99 | 100 | ::fold{title="贝叶斯公式" expand success} 101 | 贝叶斯公式是对全概率公式的简单推广,即 102 | 103 | $$ 104 | P(A_i\mid B)=\frac{P(A_i)\cdot P(B\mid A_i)}{\sum_{k=1}^\infty P(A_k)\cdot P(B\mid A_k)}. 105 | $$ 106 | :: 107 | 108 | $P(A_i)$ 是在没有进一步关于 $B$ 的信息时对 $A_i$ 的概率,称为**先验概率**; 109 | 110 | 在得知新的信息 $B$ 后,人们对 $A_i$ 发生的可能性有了新的估计,得到条件概率 $P(A_i\mid B)$,称为**后验概率**. 111 | 112 | ::fold{title="**例 1.34**:贝叶斯决策" expand} 113 | 某工厂有四条流水线生产同一种样品,其中每条流水线产量分别占总产量的 $12\%$,$25\%$,$25\%$ 和 $38\%$,且各流水线的不合格率分别为 $0.06$,$0.05$,$0.04$ 和 $0.03$. 114 | 115 | 某客户购买了一件产品,发现是不合格品,向厂家提出索赔 $10000$ 元. 按规定,工厂要求四条流水线共同承担责任. 问每条流水线应该各赔付多少? 116 | :: 117 | 118 | > 假设 $B$ 表示“任取一件产品是不合格品”,$A_i$ 表示“该产品是由第 $i$ 条流水线生产的”,则 119 | > 120 | > $$ 121 | > \begin{align*} 122 | > P(B) &= \sum_{i=1}^4 P(A_i)\cdot P(B\mid A_i) \\ 123 | > &= 0.12\times 0.06 + 0.25\times 0.05 + 0.25\times 0.04 + 0.38\times 0.03 \\ 124 | > &= 0.0411. 125 | > \end{align*} 126 | > $$ 127 | > 128 | > 说明工厂产品不合格率为 $4.11\%$. 现在客户所购买产品为不合格品,即 $B$ 发生了,因此需要求出在 $B$ 条件下 $A_i$ 发生的概率 $P(A_i\mid B)$,并按比例分配赔偿金额. 129 | > 130 | > 由贝叶斯公式 131 | > 132 | > $$ 133 | > P(A_i\mid B)=\frac{P(A_i)\cdot P(B\mid A_i)}{P(B)} 134 | > $$ 135 | > 136 | > 计算得到 $P(A_1\mid B)\approx0.175$,$P(A_2\mid B)\approx0.304$,$P(A_3\mid B)\approx0.243$,$P(A_4\mid B)\approx0.278$,故每条流水线应分别赔付 $1750$ 元,$3040$ 元,$2430$ 元和 $2780$ 元. 137 | 138 | --- 139 | 140 | ## 事件独立性 141 | 142 | > **$A$ 与 $B$ 独立**:$P(A\mid B)=P(A)$,或 $P(AB)=P(A)\cdot P(B)$. 143 | 144 | 任何事件都与 $\emptyset$ 和 $\Omega$ 独立. 145 | 146 | 若 $A$ 与 $B$ 互不相容($P(AB)=0$),且 $P(A)P(B)\neq0$,则 $A$ 与 $B$ 一定不独立. 147 | 148 | > **$n$ 个事件相互独立**:对一切 $1\leqslant i 150 | > $$ 151 | > \left\{\begin{align*} 152 | > &P(A_iA_j)=P(A_i)P(A_j) \\ 153 | > &P(A_iA_jA_k)=P(A_i)P(A_j)P(A_k) \\ 154 | > &\cdots \\ 155 | > &P(A_1A_2\cdots A_n)=P(A_1)P(A_2)\cdots P(A_n) 156 | > \end{align*}\right. 157 | > $$ 158 | 159 | 上述 $\displaystyle\binom{n}{2}+\cdots+\binom{n}{n}=2^n-n-1$ 个等式缺一不可. 160 | -------------------------------------------------------------------------------- /docs/sci/prob/chapter-2.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.2 随机变量与分布函数 3 | --- 4 | 5 | ## 分布函数 6 | 7 | ### 一元 8 | 9 | **分布函数 $F(x)$** $=P(\xi\leqslant x)$,$-\inftyx)=1-[1-F(x)]^n$. 65 | - **$(\xi_1^*, \xi_n^*)$ 的联合分布函数** 66 | $$ 67 | \begin{align*} 68 | F(x, y)&=P(\xi_1^*\leqslant x,\xi_n^*\leqslant y) \\ 69 | &=P(\xi_n^*\leqslant y)-P(\xi_1^*>x,\xi_n^*\leqslant y) \\ 70 | &=F^n(y)-P\left(\bigcap_{i=1}^n(x<\xi_n\leqslant y)\right) \\[1em] 71 | &=\begin{cases} 72 | F^n(y)-[F(y)-F(x)]^n & x $\xi$,$\eta$ 相互独立,都服从参数为 $1$ 的指数分布,分别求 $\alpha=\xi+\eta$ 与 $\beta=\dfrac{\xi}{\eta}$ 的密度. 84 | 85 | $x,y>0$ 时,$p(x,y)=e^{-(x+y)}$,这是 $(\xi,\eta)$ 的联合密度. 86 | 87 | 函数组 $\begin{cases} u=x+y \\ v=x/y \end{cases}$ 存在反函数,当 $x,y>0$,$u,v>0$ 时 88 | 89 | $$ 90 | \begin{align*} 91 | J^{-1}&=\dfrac{\partial(u,v)}{\partial(x,y)}=\begin{vmatrix} 1 & 1 \\[.5em] \frac{1}{y} & -\frac{x}{y^2}\end{vmatrix} \\[2em] 92 | &=-\dfrac{x+y}{y^2}=-\dfrac{(1+v)^2}{u} 93 | \end{align*} 94 | $$ 95 | 96 | 故 $|J|=\dfrac{u}{(1+v)^2}$. 因此 $(\alpha,\beta)$ 的联合密度为 97 | 98 | $$ 99 | q(u,v)=\begin{cases} 100 | \dfrac{ue^{-u}}{(1+v)^2} & u>0,\ v>0 \\[1em] 101 | 0 & \text{otherwise} 102 | \end{cases} 103 | $$ 104 | 105 | $\alpha$ 和 $\beta$ 各自的密度即为 $q(u,v)$ 的边际密度. 106 | :: 107 | -------------------------------------------------------------------------------- /docs/sci/prob/chapter-3.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Ch.3 数字特征与特征函数 3 | --- 4 | 5 | ## 期望 6 | 7 | **期望 $\mathbb{E}(\xi)$** $=\displaystyle\int_{-\infty}^{\infty}x\mathrm{d}F(x)$,如果 $\displaystyle\int_{-\infty}^{\infty}|x|\mathrm{d}F(x)<\infty$.(这种积分形式称作 **Stieltjes 积分**) 8 | 9 | - 变形可得另一计算式 $\mathbb{E}(\xi)=\displaystyle\int_0^{\infty}P(\xi>t)\,\mathrm{d}t-\int_{-\infty}^0P(\xi\leqslant t)\,\mathrm{d}t$. 10 | - 为什么写 $\mathrm{d}F(x)$ 而不是 $p(x)\mathrm{d}x$?因为前者适用于任何分布,后者只适用于连续分布. 在连续分布的意义下,$\mathrm{d}F(x)=p(x)\mathrm{d}x$. 11 | 12 | **随机变量函数的期望**:若 $\eta=f(\xi)$,则 $\mathbb{E}(\eta)=\mathbb{E}(f(\xi))=\displaystyle\int_{-\infty}^{\infty}f(x)\mathrm{d}F_\xi(x)$. 13 | 14 | - 该式对于连续分布是显然的:$\mathbb{E}(f(\xi))=\displaystyle\int_{-\infty}^{\infty}f(x)p(x)\mathrm{d}x$. 15 | - 该式可以推广到多维随机向量:$\mathbb{E}(f(\xi_1,\xi_2))=\displaystyle\int_{-\infty}^{\infty}\displaystyle\int_{-\infty}^{\infty}f(x_1,x_2)\mathrm{d}F(x_1,x_2)$. 16 | - **若 $\xi$ 与 $\eta$ 同分布,则 $\mathbb{E}(f(\xi))=\mathbb{E}(f(\eta))$**.(显然) 17 | - **若 $\forall f$ 都有 $\mathbb{E}(f(\xi))=\mathbb{E}(f(\eta))$,则 $\xi$ 与 $\eta$ 同分布**.(*) 18 | 19 | **性质**: 20 | 21 | - 若 $a\leqslant\xi\leqslant b$,则 $\mathbb{E}(\xi)$ 存在,且 **$a\leqslant\mathbb{E}(\xi)\leqslant b$**.(显然) 22 | - 若 $|\xi|\leqslant\eta$,且 $\mathbb{E}(\eta)$ 存在,则 $\mathbb{E}(\xi)$ 存在,且 **$|\mathbb{E}(\xi)|\leqslant\mathbb{E}(|\xi|)\leqslant\mathbb{E}(\eta)$**.(*) 23 | - **$\mathbb{E}$ 是线性的**:$\mathbb{E}(a\xi+b\eta+c)=a\mathbb{E}(\xi)+b\mathbb{E}(\eta)+c$. 24 | - 若 $\xi_1,\xi_2,\cdots,\xi_n$ 相互独立,则 **$\mathbb{E}(\xi_1\xi_2\cdots\xi_n)=\mathbb{E}(\xi_1)\mathbb{E}(\xi_2)\cdots\mathbb{E}(\xi_n)$**. 25 | 26 | **Stein 引理**:设 $\xi\sim N(0,1)$,$g(x)$ 连续可微,$\mathbb{E}(|\xi g(\xi)|)<\infty$,$\mathbb{E}(|g'(\xi)|)<\infty$,则: 27 | 28 | - **$\mathbb{E}\left[\xi g(\xi)\right]=\mathbb{E}\left[g'(\xi)\right]$**. 通过分部积分证明. 29 | - **若上式对 $\forall$ 有界连续、且导数也有界连续的 $g(x)$ 成立,则 $\xi\sim N(0,1)$**. 30 | 31 | --- 32 | 33 | ## 条件期望 34 | 35 | **条件期望 $\mathbb{E}(\eta|\xi=x)$** $=\displaystyle\int_{-\infty}^\infty y\mathrm{d}F_{\eta|\xi}(y|x)$. 36 | 37 | - 对于连续型随机变量:$\mathbb{E}(\eta|\xi=x)=\displaystyle\int_{-\infty}^\infty yp_{\eta|\xi}(y|x)\mathrm{d}y$. 38 | 39 | **条件期望 $\mathbb{E}(\eta|\xi)$** $=m(\xi)$,其中 $m(x)=\mathbb{E}(\eta|\xi=x)$. 称作**已知 $\xi$ 时 $\eta$ 的条件期望**. 40 | -------------------------------------------------------------------------------- /docs/sci/prob/distribution.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 常用分布表 3 | --- 4 | 5 | ## 离散型 6 | 7 | ### 退化分布 / 单点分布 8 | 9 | ::fold{title="退化分布 / 单点分布" info expand} 10 | **概率分布 $p_{\xi}(k)$** $=\begin{cases}1 & k=c \\ 0 & k\neq c\end{cases}$ 11 | 12 | **期望 $\mathbb{E}(\xi)$** $=c$ 13 | 14 | **方差 $\operatorname{Var}(\xi)$** $=0$ 15 | 16 | **特征函数 $\varphi(t)$** $=e^{\mathrm{i}ct}$ 17 | :: 18 | 19 | $\xi$ 恒为常数 $c$,即 $P(\xi=c)=1$. 20 | 21 | --- 22 | 23 | ### 两点分布 / 伯努利分布 / 01 分布 24 | 25 | ::fold{title="两点分布 / 伯努利分布 / 01 分布" info expand} 26 | **记作 $B(1,p)$** 27 | 28 | **概率分布 $p_{\xi}(k)$** $=\begin{cases}p & k=1 \\ 1-p & k=0\end{cases}$ 29 | 30 | **期望 $\mathbb{E}(\xi)$** $=p$ 31 | 32 | **方差 $\operatorname{Var}(\xi)$** $=pq$ 33 | 34 | **特征函数 $\varphi(t)$** $=pe^{\mathrm{i}t}+q$ 35 | :: 36 | 37 | $\xi$ 为事件 $A$(概率为 $p$)的示性函数,即 $\xi=\begin{cases} 1 & \text{如果 } A \text{ 发生} \\ 0 & \text{如果 } A \text{ 不发生}\end{cases}$. 38 | 39 | --- 40 | 41 | ### 二项分布 42 | 43 | ::fold{title="二项分布" info expand} 44 | **记作 $B(n,p)$** 45 | 46 | **概率分布 $p_{\xi}(k)$** $=\displaystyle\binom{n}{k} p^kq^{n-k}:=b(k;n,p)$,$k=0,1,\cdots,n$ 47 | 48 | **期望 $\mathbb{E}(\xi)$** $=np$ 49 | 50 | **方差 $\operatorname{Var}(\xi)$** $=npq$ 51 | 52 | **特征函数 $\varphi(t)$** $=(pe^{\mathrm{i}t}+q)^n$ 53 | :: 54 | 55 | $\xi$ 为 $n$ 次独立重复试验中事件 $A$ 发生的次数. 56 | 57 | 1. $b(k;n,p) = b(n-k;n,1-p)$. 58 | 2. $k=m$ 时 $b(k;n,p)$ 达到最大,$m$ 称为**最可能成功次数**. 59 | - 若 $(n+1)p$ 是整数,则 $m=(n+1)p$ 或 $m=(n+1)p-1$; 60 | - 若 $(n+1)p$ 不是整数,则 $m=\lfloor (n+1)p \rfloor$. 61 | 3. 若 $\xi\sim B(n,p)$,则 $P(\xi=k+1)=\dfrac{(n-k)p}{(k+1)q}\cdot P(\xi=k)$. 62 | 4. (**泊松定理**)假设 $p$ 与 $n$ 有关,记作 $p_n$,若 $\lim\limits_{n\to\infty}np_n=\lambda$,则 $\lim\limits_{n\to\infty}b(k;n,p_n)=\dfrac{\lambda^k}{k!}e^{-\lambda}$. 即 $B(n,p_n)\to P(\lambda)$. 63 | 64 | --- 65 | 66 | ### 泊松分布 67 | 68 | ::fold{title="泊松分布" info expand} 69 | **记作 $P(\lambda)$**,$\lambda>0$ 70 | 71 | **概率分布 $p_{\xi}(k)$** $=\dfrac{\lambda^k}{k!}e^{-\lambda}:=p(k;\lambda)$,$k=0,1,2,\cdots$ 72 | 73 | **期望 $\mathbb{E}(\xi)$** $=\lambda$ 74 | 75 | **方差 $\operatorname{Var}(\xi)$** $=\lambda$ 76 | 77 | **特征函数 $\varphi(t)$** $=e^{\lambda(e^{\mathrm{i}t}-1)}$ 78 | :: 79 | 80 | 由泊松定理引入,$\xi$ 取一切非负整数. 81 | 82 | --- 83 | 84 | ### 几何分布 85 | 86 | ::fold{title="几何分布" info expand} 87 | **概率分布 $p_{\xi}(k)$** $=pq^{k-1}:=g(k;p)$,$k=1,2,3,\cdots$ 88 | 89 | **期望 $\mathbb{E}(\xi)$** $=\dfrac{1}{p}$ 90 | 91 | **方差 $\operatorname{Var}(\xi)$** $=\dfrac{q}{p^2}$ 92 | 93 | **特征函数 $\varphi(t)$** $=\dfrac{pe^{\mathrm{i}t}}{1-qe^{\mathrm{i}t}}$ 94 | :: 95 | 96 | $\xi$ 为伯努利试验首次成功的次数. 97 | 98 | **无记忆性**:$P(\xi>k+m\mid \xi>m)=P(\xi>k)$. 即:如果前 $m$ 次试验都失败,那么从第 $m+1$ 次开始到成功的次数 $\eta$ 仍服从几何分布. 99 | 100 | --- 101 | 102 | ### 超几何分布 103 | 104 | ::fold{title="超几何分布" info expand} 105 | **概率分布 $p_{\xi}(k)$** $=\dfrac{\binom{M}{k}\binom{N-M}{n-k}}{\binom{N}{n}}$,$k=0,1,2,\cdots,\min(n,M)$ 106 | 107 | **期望 $\mathbb{E}(\xi)$** $=n\cdot\dfrac{M}{N}$ 108 | 109 | **方差 $\operatorname{Var}(\xi)$** $=n\cdot\dfrac{M}{N}\left(1-\dfrac{M}{N}\right)\cdot\dfrac{N-n}{N-1}$ 110 | :: 111 | 112 | $N$ 件产品中有 $M$ 件次品,从中抽取 $n$ 件,其中次品数为 $\xi$. 113 | 114 | --- 115 | 116 | ### 巴斯卡分布 117 | 118 | ::fold{title="巴斯卡分布" info expand} 119 | **概率分布 $p_{\xi}(k)$** $=\displaystyle\binom{k-1}{r-1}p^r q^{k-r}$,$k=r,r+1,r+2,\cdots$ 120 | 121 | **期望 $\mathbb{E}(\xi)$** $=\dfrac{r}{p}$ 122 | 123 | **方差 $\operatorname{Var}(\xi)$** $=\dfrac{rq}{p^2}$ 124 | 125 | **特征函数 $\varphi(t)$** $=\left(\dfrac{pe^{\mathrm{i}t}}{1-qe^{\mathrm{i}t}}\right)^r$ 126 | :: 127 | 128 | $\xi$ 为得到第 $r$ 次成功时的伯努利试验次数. $r=1$ 时即为几何分布. 129 | 130 | --- 131 | 132 | ## 连续型 133 | 134 | ### 均匀分布 135 | 136 | ::fold{title="均匀分布" success expand} 137 | **记作 $U(a,b)$** 138 | 139 | **概率密度 $p_{\xi}(x)$** $=\begin{cases}\frac{1}{b-a} & a\leqslant x\leqslant b \\[.5em] 0 & \text{otherwise}\end{cases}$ 140 | 141 | **期望 $\mathbb{E}(\xi)$** $=\dfrac{a+b}{2}$ 142 | 143 | **方差 $\operatorname{Var}(\xi)$** $=\dfrac{(b-a)^2}{12}$ 144 | 145 | **特征函数 $\varphi(t)$** $=\dfrac{e^{\mathrm{i}tb}-e^{\mathrm{i}ta}}{\mathrm{i}t(b-a)}$ 146 | :: 147 | 148 | $\xi$ 在区间 $[a,b]$ 上均匀取值. 149 | 150 | --- 151 | 152 | ### 正态分布 / 高斯分布 153 | 154 | ::fold{title="正态分布 / 高斯分布" success expand} 155 | **记作 $N(a,\sigma^2)$** 156 | 157 | **概率密度 $p_{\xi}(x)$** $=\dfrac{1}{\sqrt{2\pi}\sigma}\exp\left\{{-\dfrac{(x-a)^2}{2\sigma^2}}\right\}$,$-\infty0$ 181 | 182 | **分布函数 $F(x)$** $=\begin{cases}1-e^{-\lambda x} & x\geqslant 0 \\ 0 & x<0\end{cases}$ 183 | 184 | **期望 $\mathbb{E}(\xi)$** $=\dfrac{1}{\lambda}$ 185 | 186 | **方差 $\operatorname{Var}(\xi)$** $=\dfrac{1}{\lambda^2}$ 187 | 188 | **特征函数 $\varphi(t)$** $=\left(1-\dfrac{\mathrm{i}t}{\lambda}\right)^{-1}$ 189 | :: 190 | 191 | --- 192 | 193 | ### $\chi^2$ 分布 194 | 195 | ::fold{title="$\chi^2$ 分布" success expand} 196 | **记作 $\chi^2(n)$**,$n\in\mathbb{N}^+$ 197 | 198 | **概率密度 $p_{\xi}(x)$** $=\begin{cases}\frac{1}{2^{n/2}\Gamma(n/2)}x^{n/2-1}e^{-x/2} & x\geqslant 0 \\ 0 & x<0\end{cases}$ 199 | 200 | **期望 $\mathbb{E}(\xi)$** $=n$ 201 | 202 | **方差 $\operatorname{Var}(\xi)$** $=2n$ 203 | 204 | **特征函数 $\varphi(t)$** $=(1-2\mathrm{i}t)^{-n/2}$ 205 | 206 | 其中 $\Gamma(n)$ 为第一类欧拉积分. 207 | :: 208 | 209 | --- 210 | 211 | ### $\Gamma$ 分布 212 | 213 | ::fold{title="$\Gamma$ 分布" success expand} 214 | **记作 $\Gamma(\lambda,r)$**,$\lambda>0,\ r>0$ 215 | 216 | **概率密度 $p_{\xi}(x)$** $=\begin{cases}\frac{\lambda^r}{\Gamma(r)}x^{r-1}e^{-\lambda x} & x\geqslant 0 \\ 0 & x<0\end{cases}$ 217 | 218 | **期望 $\mathbb{E}(\xi)$** $=\dfrac{r}{\lambda}$ 219 | 220 | **方差 $\operatorname{Var}(\xi)$** $=\dfrac{r}{\lambda^2}$ 221 | 222 | **特征函数 $\varphi(t)$** $=\left(1-\dfrac{\mathrm{i}t}{\lambda}\right)^{-r}$ 223 | 224 | 其中 $\Gamma(r)$ 为第一类欧拉积分. 225 | :: 226 | 227 | 当 $r$ 为整数时也称**爱尔兰分布**. 228 | 229 | $r=1$ 时,退化为指数分布. 230 | -------------------------------------------------------------------------------- /docs/sci/prob/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 概率论 3 | --- 4 | 5 | ::fold{expand info title="摘要"} 6 | 浙江大学《概率论(H)》课程笔记。 7 | 8 | 修读时间:2024 年秋冬学期。 9 | :: 10 | 11 | :asterisk 12 | 13 | :index 14 | -------------------------------------------------------------------------------- /env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Xecades Notes 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "note", 3 | "version": "1.0.0", 4 | "private": true, 5 | "type": "module", 6 | "author": { 7 | "name": "Xecades", 8 | "email": "i@xecades.xyz", 9 | "url": "https://xecades.xyz" 10 | }, 11 | "description": "Yet another personal website.", 12 | "license": "GPL-3.0-only", 13 | "scripts": { 14 | "dev": "vite", 15 | "build": "run-p type-check \"build-only {@}\" --", 16 | "preview": "vite preview", 17 | "build-only": "vite build", 18 | "type-check": "vue-tsc --build", 19 | "cache": "tsx ./cache.ts", 20 | "clean": "rm -rf dist cache" 21 | }, 22 | "dependencies": { 23 | "@fortawesome/fontawesome-svg-core": "^6.6.0", 24 | "@fortawesome/free-brands-svg-icons": "^6.6.0", 25 | "@fortawesome/free-regular-svg-icons": "^6.6.0", 26 | "@fortawesome/free-solid-svg-icons": "^6.6.0", 27 | "@fortawesome/vue-fontawesome": "^3.0.8", 28 | "@vueuse/core": "^12.5.0", 29 | "@yeger/vue-masonry-wall": "^5.0.16", 30 | "canvas-confetti": "^1.9.3", 31 | "dayjs": "^1.11.13", 32 | "fuse.js": "^7.0.0", 33 | "hotkeys-js": "^3.13.9", 34 | "is-mobile": "^5.0.0", 35 | "is-relative-url": "^4.0.0", 36 | "katex": "^0.16.20", 37 | "medium-zoom": "^1.1.0", 38 | "overlayscrollbars": "^2.10.1", 39 | "overlayscrollbars-vue": "^0.5.9", 40 | "throttle-debounce": "^5.0.2", 41 | "vercel": "^41.0.2", 42 | "vivus": "^0.4.6", 43 | "vue": "^3.5.13", 44 | "vue-animate-height": "^1.1.2", 45 | "vue-router": "^4.5.0" 46 | }, 47 | "devDependencies": { 48 | "@tsconfig/node22": "^22.0.0", 49 | "@types/canvas-confetti": "^1.9.0", 50 | "@types/node": "^22.10.2", 51 | "@types/throttle-debounce": "^5.0.2", 52 | "@types/vivus": "^0.4.7", 53 | "@vitejs/plugin-vue": "^5.2.1", 54 | "@vitejs/plugin-vue-jsx": "^4.1.1", 55 | "@vue/tsconfig": "^0.7.0", 56 | "autoprefixer": "^10.4.20", 57 | "npm-run-all2": "^7.0.2", 58 | "stylus": "^0.64.0", 59 | "stylus-loader": "^8.1.1", 60 | "tsx": "^4.19.2", 61 | "typescript": "~5.6.3", 62 | "vite": "^5.4.11", 63 | "vite-plugin-vue-devtools": "^7.6.8", 64 | "vue-tsc": "^2.1.10" 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /public/assets/line: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/public/assets/line -------------------------------------------------------------------------------- /public/assets/silence: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/public/assets/silence -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/public/favicon.ico -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Allow: / 3 | -------------------------------------------------------------------------------- /src/App.vue: -------------------------------------------------------------------------------- 1 | 28 | 29 | 37 | 38 | 43 | -------------------------------------------------------------------------------- /src/assets/css/cursor.styl: -------------------------------------------------------------------------------- 1 | @import "./global.styl"; 2 | 3 | :root { 4 | --cursor-bg: rgba(0, 0, 0, 0.25); 5 | --cursor-pointer-bg: rgba(0, 0, 0, 0.1); 6 | --cursor-pointer-scale: 2.5; 7 | --cursor-active-bg: rgba(0, 0, 0, 0.5); 8 | --cursor-transition-duration: 0.1s; 9 | } 10 | 11 | /* todo */ 12 | /* @media (prefers-color-scheme: dark) { 13 | :root { 14 | --cursor-bg: rgba(255, 255, 255, 0.25); 15 | --cursor-pointer-bg: rgba(255, 255, 255, 0.1); 16 | --cursor-active-bg: rgba(255, 255, 255, 0.5); 17 | 18 | --cursor-image: url("data:image/svg+xml,") 19 | 4 4, 20 | auto; 21 | } 22 | } */ 23 | 24 | /** Disable cursor for mobile devices */ 25 | /* todo */ 26 | /* @media screen and (pointer: coarse) { 27 | #cursor-trail { 28 | display: none; 29 | } 30 | } */ 31 | 32 | body[data-cursor-override] * 33 | cursor: none !important; 34 | 35 | .__cursor 36 | position: fixed; 37 | transition: all var(--cursor-transition-duration) ease-in-out; 38 | transition-property: background, transform, border-radius, height, width, opacity; 39 | perspective: 1000px; 40 | pointer-events: none; 41 | transform: translate(-50%, -50%); 42 | 43 | &#cursor-main 44 | width: 8px; 45 | height: 8px; 46 | background: rgba(0, 0, 0, 0.5); 47 | z-index: 10000001; 48 | border-radius: 50%; 49 | 50 | &[data-state="text"] 51 | opacity: 0.9; 52 | background: var(--cursor-text-color); 53 | height: calc(var(--cursor-text-font-size) + 4px); 54 | width: 3px; 55 | border-radius: 2px; 56 | 57 | &[data-active] 58 | opacity: 0.6; 59 | transform: translate(-50%, -50%) scale(0.9); 60 | 61 | &[data-hidden] 62 | background: transparent; 63 | 64 | &#cursor-trail 65 | width: 16px; 66 | height: 16px; 67 | background: var(--cursor-bg); 68 | z-index: 10000000; 69 | border-radius: 50%; 70 | 71 | &[data-state="pointer"] 72 | background: var(--cursor-pointer-bg); 73 | transform: translate(-50%, -50%) scale(var(--cursor-pointer-scale)); 74 | border-radius: 50%; 75 | 76 | &[data-active] 77 | background: var(--cursor-active-bg); 78 | transform: translate(-50%, -50%) scale(0.5); 79 | 80 | &[data-hidden], &[data-state="none"] 81 | background: transparent; 82 | -------------------------------------------------------------------------------- /src/assets/css/global.styl: -------------------------------------------------------------------------------- 1 | // Mixins 2 | dual($name, $normal, $small) 3 | {$name}: $normal; 4 | @media only screen and (max-width: 748px) 5 | {$name}: $small; 6 | 7 | scheme($name, $light, $dark) 8 | {$name}: $light; 9 | @media (prefers-color-scheme: dark) 10 | {$name}: $dark; 11 | 12 | dualr($name, $normal, $small) 13 | * 14 | dual($name, $normal, $small); 15 | 16 | schemer($name, $light, $dark) 17 | * 18 | scheme($name, $light, $dark); 19 | 20 | // Fonts 21 | $font-family = "Libertinus Serif", "Georgia", "Noto Serif SC", serif; 22 | $monospace = "Fira Code", "Fira Mono", Menlo, Consolas, "DejaVu Sans Mono", monospace; 23 | 24 | // Colors 25 | $theme-color = #60a5fa; 26 | $text-color = #181818; 27 | $text-color-d = #d1d5db; 28 | $background-light = #fefefe; 29 | $background-dark = #1a1b1c; 30 | 31 | // SVG 32 | $checkbox-svg = 'data:image/svg+xml;charset=UTF-8,'; 33 | -------------------------------------------------------------------------------- /src/assets/css/libertinus-serif.css: -------------------------------------------------------------------------------- 1 | @font-face { 2 | font-family: "Libertinus Serif"; 3 | font-style: normal; 4 | font-weight: 400; 5 | src: local("Libertinus Serif"), 6 | url("../font/LibertinusSerif-Regular.woff") format("woff"); 7 | } 8 | @font-face { 9 | font-family: "Libertinus Serif"; 10 | font-style: italic; 11 | font-weight: 400; 12 | src: local("Libertinus Serif"), 13 | url("../font/LibertinusSerif-Italic.woff") format("woff"); 14 | } 15 | @font-face { 16 | font-family: "Libertinus Serif"; 17 | font-style: normal; 18 | font-weight: 600; 19 | src: local("Libertinus Serif"), 20 | url("../font/LibertinusSerif-Semibold.woff") format("woff"); 21 | } 22 | @font-face { 23 | font-family: "Libertinus Serif"; 24 | font-style: italic; 25 | font-weight: 600; 26 | src: local("Libertinus Serif"), 27 | url("../font/LibertinusSerif-SemiboldItalic.woff") format("woff"); 28 | } 29 | @font-face { 30 | font-family: "Libertinus Serif"; 31 | font-style: normal; 32 | font-weight: 700; 33 | src: local("Libertinus Serif"), 34 | url("../font/LibertinusSerif-Bold.woff") format("woff"); 35 | } 36 | @font-face { 37 | font-family: "Libertinus Serif"; 38 | font-style: italic; 39 | font-weight: 700; 40 | src: local("Libertinus Serif"), 41 | url("../font/LibertinusSerif-BoldItalic.woff") format("woff"); 42 | } 43 | @font-face { 44 | font-family: "Libertinus Serif Display"; 45 | font-style: normal; 46 | font-weight: 400; 47 | src: local("Libertinus Serif Display"), 48 | url("../font/LibertinusSerifDisplay-Regular.woff") format("woff"); 49 | } 50 | -------------------------------------------------------------------------------- /src/assets/css/main.styl: -------------------------------------------------------------------------------- 1 | @import "https://fonts.loli.net/css2?family=Noto+Serif+SC:wght@200..900&display=swap"; 2 | // @import "https://fonts.cdnfonts.com/css/libertinus-serif"; 3 | @import "./libertinus-serif.css"; 4 | @import "./global.styl"; 5 | 6 | schemer(--body-background-color, $background-light, $background-dark); 7 | schemer(--body-text-color, $text-color, $text-color-d); 8 | 9 | // Overlay Scrollbar Theme 10 | .os-theme-dark .os-scrollbar-handle 11 | scheme(--os-handle-bg, alpha(black, 0.44), alpha(white, 0.44)); 12 | scheme(--os-handle-bg-hover, alpha(black, 0.55), alpha(white, 0.55)); 13 | scheme(--os-handle-bg-active, alpha(black, 0.66), alpha(white, 0.66)); 14 | 15 | // Global Fonts 16 | body 17 | font-family: $font-family; 18 | color: var(--body-text-color); 19 | 20 | // Tap / Selection / Outline Effects 21 | * 22 | -webkit-tap-highlight-color: transparent; 23 | 24 | *::selection 25 | background-color: alpha($theme-color, 0.25); 26 | 27 | body 28 | outline: none; 29 | 30 | // Background Color 31 | body 32 | background-color: var(--body-background-color); 33 | 34 | // Scrollbar Flickering Prevention 35 | html 36 | padding-right: calc(100% - 100vw); 37 | overflow-x: hidden; 38 | 39 | // Prevent Scrollbar Flickering 40 | #app 41 | width: 100vw; 42 | 43 | // Animation 44 | @keyframes shake-x 45 | 0%, 100% 46 | transform: translateX(0); 47 | 50% 48 | transform: translateX(2px); 49 | 50 | // Medium Zoom 51 | schemer(--overlay-color, #47484a85, #0e0e0ed6); 52 | 53 | .medium-zoom-image[data-ic-zoomable] 54 | z-index: 10000; 55 | border-radius: 0; 56 | 57 | .medium-zoom-overlay 58 | z-index: 9999; 59 | 60 | -------------------------------------------------------------------------------- /src/assets/css/reset.styl: -------------------------------------------------------------------------------- 1 | * 2 | margin: 0; 3 | padding: 0; 4 | border: 0; 5 | font-size: 100%; 6 | font: inherit; 7 | vertical-align: baseline; 8 | 9 | ol, 10 | ul 11 | list-style: none; 12 | 13 | blockquote, 14 | q 15 | quotes: none; 16 | 17 | blockquote:before, 18 | blockquote:after, 19 | q:before, 20 | q:after 21 | content: ""; 22 | content: none; 23 | 24 | table 25 | border-collapse: collapse; 26 | border-spacing: 0; 27 | 28 | a 29 | text-decoration: none; 30 | -------------------------------------------------------------------------------- /src/assets/font/LibertinusSerif-Bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/src/assets/font/LibertinusSerif-Bold.woff -------------------------------------------------------------------------------- /src/assets/font/LibertinusSerif-BoldItalic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/src/assets/font/LibertinusSerif-BoldItalic.woff -------------------------------------------------------------------------------- /src/assets/font/LibertinusSerif-Italic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/src/assets/font/LibertinusSerif-Italic.woff -------------------------------------------------------------------------------- /src/assets/font/LibertinusSerif-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/src/assets/font/LibertinusSerif-Regular.woff -------------------------------------------------------------------------------- /src/assets/font/LibertinusSerif-Semibold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/src/assets/font/LibertinusSerif-Semibold.woff -------------------------------------------------------------------------------- /src/assets/font/LibertinusSerif-SemiboldItalic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/src/assets/font/LibertinusSerif-SemiboldItalic.woff -------------------------------------------------------------------------------- /src/assets/font/LibertinusSerifDisplay-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/src/assets/font/LibertinusSerifDisplay-Regular.woff -------------------------------------------------------------------------------- /src/assets/img/quote-left.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 13 | 14 | -------------------------------------------------------------------------------- /src/assets/img/quote-right.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /src/assets/img/waving.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xecades/Note/6c1d514abe1a159479e72ba8709deafe4be03260/src/assets/img/waving.png -------------------------------------------------------------------------------- /src/assets/ts/dayjs.ts: -------------------------------------------------------------------------------- 1 | import dayjs from "dayjs"; 2 | import relativeTime from "dayjs/plugin/relativeTime"; 3 | import locale_zhcn from "dayjs/locale/zh-cn"; 4 | 5 | dayjs.extend(relativeTime); 6 | dayjs.locale(locale_zhcn); 7 | 8 | export default dayjs; 9 | -------------------------------------------------------------------------------- /src/assets/ts/fontawesome.ts: -------------------------------------------------------------------------------- 1 | import { library } from "@fortawesome/fontawesome-svg-core"; 2 | import { 3 | faAngleDown, 4 | faAngleRight, 5 | faArrowUpRightFromSquare, 6 | faAsterisk, 7 | faAtom, 8 | faBars, 9 | faBoxArchive, 10 | faCaretLeft, 11 | faCaretRight, 12 | faChevronLeft, 13 | faChevronRight, 14 | faCode, 15 | faCube, 16 | faExclamationCircle, 17 | faExclamationTriangle, 18 | faFaceFrown, 19 | faFile, 20 | faFlag, 21 | faFolder, 22 | faInfoCircle, 23 | faLightbulb, 24 | faLink, 25 | faMagnifyingGlass, 26 | faSpinner, 27 | faXmark, 28 | } from "@fortawesome/free-solid-svg-icons"; 29 | import { faStar } from "@fortawesome/free-regular-svg-icons"; 30 | import { faGithub } from "@fortawesome/free-brands-svg-icons"; 31 | 32 | library.add( 33 | faAngleRight, 34 | faAngleDown, 35 | faBars, 36 | faMagnifyingGlass, 37 | faCaretRight, 38 | faCaretLeft, 39 | faXmark, 40 | faFile, 41 | faFolder, 42 | faFaceFrown, 43 | faAsterisk, 44 | faCube, 45 | faLightbulb, 46 | faInfoCircle, 47 | faExclamationCircle, 48 | faExclamationTriangle, 49 | faLink, 50 | faArrowUpRightFromSquare, 51 | faStar, 52 | faGithub, 53 | faChevronRight, 54 | faCode, 55 | faAtom, 56 | faBoxArchive, 57 | faFlag, 58 | faChevronLeft, 59 | faSpinner 60 | ); 61 | -------------------------------------------------------------------------------- /src/assets/ts/latex.ts: -------------------------------------------------------------------------------- 1 | import katex from "katex"; 2 | import type { KatexOptions } from "katex"; 3 | 4 | const _render = (options: KatexOptions) => (raw: string) => { 5 | try { 6 | return katex.renderToString(raw, options); 7 | } catch (error) { 8 | console.error(error); 9 | return `${raw}`; 10 | } 11 | }; 12 | 13 | const config: KatexOptions = { 14 | throwOnError: true, 15 | macros: { "\\ketbra": "\\mathinner{|{#1}\\rangle\\!\\langle{#2}|}" }, 16 | }; 17 | 18 | const render_inline = _render({ displayMode: false, ...config }); 19 | const render_block = _render({ displayMode: true, ...config }); 20 | const render = (raw: string, opts: KatexOptions) => _render(opts)(raw); 21 | 22 | export { render, render_inline, render_block }; 23 | -------------------------------------------------------------------------------- /src/assets/ts/leftbar.tsx: -------------------------------------------------------------------------------- 1 | import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; 2 | import { RouterLink } from "vue-router"; 3 | import { LEFTBAR_STATUS } from "./types"; 4 | import { 5 | isMobile, 6 | isWidthLessThan, 7 | LEFTBAR_THRESHOLD, 8 | SMALL_SCREEN_WIDTH, 9 | } from "./utils"; 10 | 11 | import type { NavNode } from "vite-plugin-vue-xecades-note"; 12 | import type { JSX } from "vue/jsx-runtime"; 13 | 14 | /** 15 | * Determine leftbar status. 16 | */ 17 | export const get_leftbar_status = (): LEFTBAR_STATUS => { 18 | if (isMobile()) { 19 | return LEFTBAR_STATUS.SHOW_SEARCH_AND_CATEGORY; 20 | } else { 21 | return isWidthLessThan(SMALL_SCREEN_WIDTH) 22 | ? LEFTBAR_STATUS.SHOW_SEARCH_AND_CATEGORY 23 | : isWidthLessThan(LEFTBAR_THRESHOLD) 24 | ? LEFTBAR_STATUS.ONLY_SEARCH_BUTTON 25 | : LEFTBAR_STATUS.HOVER_TO_SHOW; 26 | } 27 | }; 28 | 29 | /** 30 | * Render the leftbar navigation. 31 | * 32 | * @param node - The nav node to render 33 | * @param is_root - Whether the node is the root node. Default is `false` 34 | * @returns Rendered nav node 35 | */ 36 | export const render_list = ( 37 | node: NavNode, 38 | is_root: boolean = false 39 | ): JSX.Element => { 40 | let { title, children, link } = node; 41 | 42 | const text_comp: JSX.Element = {title}; 43 | const icon_comp: JSX.Element = ( 44 | 45 | 46 | 47 | ); 48 | const title_comp: JSX.Element = ( 49 | 50 | {icon_comp} 51 | {text_comp} 52 | 53 | ); 54 | 55 | // If it is a leaf node, mark and return 56 | if (children.length === 0) { 57 | (title_comp.props as any).leaf = true; 58 | return title_comp; 59 | } 60 | 61 | let child_comps: JSX.Element[] = children.map((child) => ( 62 |
  • {render_list(child)}
  • 63 | )); 64 | let children_comp: JSX.Element =
      {child_comps}
    ; 65 | 66 | // If it is the root node, no need to add a title 67 | if (is_root) { 68 | return children_comp; 69 | } 70 | 71 | return <>{[title_comp, children_comp]}; 72 | }; 73 | -------------------------------------------------------------------------------- /src/assets/ts/markdown.ts: -------------------------------------------------------------------------------- 1 | import config from "@cache/config"; 2 | import { useRouter } from "vue-router"; 3 | import { assertType } from "./types"; 4 | 5 | import type { NavNode, RouteMeta, URL } from "vite-plugin-vue-xecades-note"; 6 | 7 | /** 8 | * Locate the target node in the navigation tree recursively. 9 | * 10 | * @param target - The target path 11 | * @returns The target node 12 | */ 13 | export const locate_node = (target: URL): NavNode | undefined => { 14 | const nodes: NavNode[] = config.nav; 15 | 16 | // If the target is the / page, return a dummy root node. 17 | if (target === "/") { 18 | return { title: "", name: "", link: "/", children: nodes }; 19 | } 20 | 21 | const dfs = (nodes: NavNode[]): NavNode | undefined => { 22 | for (const node of nodes) { 23 | if (node.link === target) return node; 24 | 25 | const result = dfs(node.children); 26 | if (result) return result; 27 | } 28 | }; 29 | 30 | return dfs(nodes); 31 | }; 32 | 33 | /** 34 | * Get the route of a link. 35 | * 36 | * @param link - The link of the route 37 | * @returns The route of the link 38 | */ 39 | export const route_of = (link: URL) => { 40 | const routes = useRouter().getRoutes(); 41 | return routes.find((route) => route.path === link)!; 42 | }; 43 | 44 | /** 45 | * Get the metadata of a link. 46 | * 47 | * @param link - The link of the route 48 | * @returns Meta of the link 49 | */ 50 | export const meta_of = (link: URL | NavNode): RouteMeta => { 51 | if (typeof link === "string") 52 | return assertType(route_of(link).meta); 53 | return meta_of(link.link as URL); 54 | }; 55 | 56 | /** 57 | * Get the leaf index nodes of a navigation tree. 58 | * 59 | * @param root - The root node 60 | * @returns The leaf index nodes 61 | */ 62 | export const leaf_nodes = (root: NavNode): NavNode[] => { 63 | // If some children are leaf nodes, return the root node 64 | if (root.children.some((node) => node.children.length === 0)) { 65 | return [root]; 66 | } 67 | 68 | return root.children.flatMap(leaf_nodes); 69 | }; 70 | -------------------------------------------------------------------------------- /src/assets/ts/rightbar.ts: -------------------------------------------------------------------------------- 1 | import { isWidthLessThan, RIGHTBAR_THRESHOLD } from "./utils"; 2 | import { RIGHTBAR_STATUS } from "./types"; 3 | 4 | import type { MarkdownHeaderJsx } from "vite-plugin-vue-xecades-note"; 5 | import type { Ref } from "vue"; 6 | 7 | /** Header type used for ref rendering */ 8 | export type SerialHeader = MarkdownHeaderJsx & { 9 | width: string; 10 | indent: string; 11 | opacity: string; 12 | index: number; 13 | }; 14 | export type CascadeHeader = SerialHeader & { children: SerialHeader[] }; 15 | 16 | const width_preset = ["50px", "40px", "30px", "20px", "13px"]; 17 | const indent_preset = ["0rem", "1.3rem", "1.7rem", "2.3rem", "2.8rem"]; 18 | const opacity_preset = ["1", "0.8", "0.7", "0.7", "0.7"]; 19 | 20 | /** 21 | * Determine rightbar status (i.e. whether to display or not). 22 | * 23 | * @note Only when the screen width is less than `RIGHTBAR_THRESHOLD`, 24 | * will the rightbar be hidden. 25 | */ 26 | export const get_rightbar_status = (): RIGHTBAR_STATUS => 27 | isWidthLessThan(RIGHTBAR_THRESHOLD) 28 | ? RIGHTBAR_STATUS.HIDE 29 | : RIGHTBAR_STATUS.SHOW; 30 | 31 | /** 32 | * Append width and indent properties to TOC data. 33 | * 34 | * @param toc - Raw TOC data imported from json 35 | * @returns Normalized TOC data 36 | */ 37 | export const serial_toc = (toc: MarkdownHeaderJsx[]): SerialHeader[] => { 38 | const levels = toc.map((item) => item.level); 39 | const minLevel = Math.min(...levels); 40 | const maxLevel = Math.max(...levels); 41 | 42 | return toc.map((item, i) => ({ 43 | ...item, 44 | width: width_preset[4 + item.level - maxLevel], 45 | indent: indent_preset[item.level - minLevel], 46 | opacity: opacity_preset[item.level - minLevel], 47 | level: item.level - minLevel, 48 | index: i, 49 | })); 50 | }; 51 | 52 | export const cascade_toc = (s_toc: SerialHeader[]): CascadeHeader[] => { 53 | let res: CascadeHeader[] = []; 54 | let prev_root = 0; 55 | 56 | for (let i = 1; i < s_toc.length; i++) { 57 | if (s_toc[i].level === s_toc[prev_root].level) { 58 | const children = s_toc.slice(prev_root + 1, i); 59 | res.push({ ...s_toc[prev_root], children }); 60 | prev_root = i; 61 | } 62 | } 63 | 64 | if (prev_root < s_toc.length) { 65 | const children = s_toc.slice(prev_root + 1); 66 | res.push({ ...s_toc[prev_root], children }); 67 | } 68 | 69 | return res; 70 | }; 71 | 72 | /** 73 | * Scroll listener class for rightbar. 74 | */ 75 | export class ScrollListener { 76 | targets: Element[]; 77 | store: Ref; 78 | 79 | /** 80 | * Constructor. 81 | * 82 | * @param in_view - Ref to store the index of the element in view 83 | */ 84 | constructor(in_view: Ref) { 85 | this.targets = []; 86 | this.store = in_view; 87 | 88 | window.onscroll = () => { 89 | for (let i = 0; i < this.targets.length; i++) { 90 | if (this.in_viewport(this.targets[i])) { 91 | this.store.value = i; 92 | break; 93 | } 94 | } 95 | }; 96 | } 97 | 98 | /** 99 | * Add an element to the listener. 100 | * 101 | * @param target - The element to add to the listener 102 | */ 103 | listen(target: Element): void { 104 | this.targets.push(target); 105 | } 106 | 107 | /** 108 | * Clear the listener. 109 | */ 110 | reset(): void { 111 | this.targets = []; 112 | this.store.value = -1; 113 | } 114 | 115 | /** 116 | * Check whether an element is in viewport. 117 | * 118 | * @param el - The element to check 119 | * @returns Whether the element is in the viewport 120 | */ 121 | private in_viewport(el: Element): boolean { 122 | const vh = window.innerHeight || document.documentElement.clientHeight; 123 | const vw = window.innerWidth || document.documentElement.clientWidth; 124 | 125 | const rect: DOMRect = el.getBoundingClientRect(); 126 | 127 | return ( 128 | rect.top >= 0 && 129 | rect.left >= 0 && 130 | rect.bottom <= vh && 131 | rect.right <= vw 132 | ); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /src/assets/ts/search.ts: -------------------------------------------------------------------------------- 1 | import type { FuseResult, FuseResultMatch, RangeTuple } from "fuse.js"; 2 | import type { 3 | CachedSearchFn, 4 | SearchTarget, 5 | } from "vite-plugin-vue-xecades-note"; 6 | 7 | /** Search results. */ 8 | export type Result = { 9 | /** Highlight area */ 10 | type?: "title" | "content"; 11 | 12 | /** Text before highlight */ 13 | before?: string; 14 | 15 | /** Highlight text */ 16 | mark?: string; 17 | 18 | /** Text after highlight */ 19 | after?: string; 20 | } & SearchTarget; 21 | 22 | /** Maximum characters before highlight */ 23 | const BEFORE_CNT: number = 10; 24 | 25 | let search_internal: CachedSearchFn; 26 | 27 | /** 28 | * Search and return parsed results. 29 | * 30 | * @param query - Search query 31 | * @param onAfterLoad - Callback after database is loaded 32 | * @returns Parsed search results 33 | */ 34 | export const search = async ( 35 | query: string, 36 | onAfterLoad: () => void 37 | ): Promise => { 38 | const range = (i: RangeTuple): number => i[1] - i[0]; 39 | const longest = (indices: readonly RangeTuple[]): RangeTuple => 40 | indices.reduce((acc, cur) => (range(cur) > range(acc) ? cur : acc)); 41 | 42 | if (search_internal === undefined) { 43 | search_internal = (await import("@cache/search")).default; 44 | onAfterLoad(); 45 | } 46 | 47 | let searchResults: FuseResult[] | Result[] = 48 | search_internal(query); 49 | let results: Result[] = []; 50 | 51 | if (query !== "") { 52 | for (let res of searchResults as FuseResult[]) { 53 | let match = (res.matches as FuseResultMatch[])[0]; 54 | 55 | // Always highlights the longest match 56 | let [s, e] = longest(match.indices); 57 | 58 | let type = match.key as "title" | "content"; 59 | let text = match.value as string; 60 | 61 | let before = "", 62 | mark = "", 63 | after = ""; 64 | 65 | if (type === "content") { 66 | if (s - BEFORE_CNT > 0) { 67 | before = "..." + text.slice(s - BEFORE_CNT, s).trimStart(); 68 | } else { 69 | before = text.slice(0, s); 70 | } 71 | 72 | mark = text.slice(s, e + 1); 73 | after = text.slice(e + 1); 74 | } else if (type === "title") { 75 | before = text.slice(0, s); 76 | mark = text.slice(s, e + 1); 77 | after = text.slice(e + 1); 78 | } 79 | 80 | results.push({ 81 | ...res.item, 82 | type, 83 | before, 84 | mark, 85 | after, 86 | }); 87 | } 88 | } else { 89 | // if query is empty, show all posts 90 | results = searchResults as Result[]; 91 | } 92 | 93 | return results; 94 | }; 95 | -------------------------------------------------------------------------------- /src/assets/ts/types.ts: -------------------------------------------------------------------------------- 1 | export const assertType = (value: any): T => value as unknown as T; 2 | 3 | /** rightbar status. */ 4 | export enum RIGHTBAR_STATUS { 5 | /** To display */ 6 | SHOW, 7 | 8 | /** To hide */ 9 | HIDE, 10 | } 11 | 12 | /** leftbar status. */ 13 | export enum LEFTBAR_STATUS { 14 | /** 15 | * Use hover status to determine whether to display. 16 | * 17 | * @note This is the default status. Whether to show the leftbar is 18 | * determined by mouse events. 19 | */ 20 | HOVER_TO_SHOW = "HOVER_TO_SHOW", 21 | 22 | /** 23 | * Only show the search button. 24 | * 25 | * @note This status means the category list and the detailed 26 | * article list will **NEVER** be shown. 27 | */ 28 | ONLY_SEARCH_BUTTON = "ONLY_SEARCH_BUTTON", 29 | 30 | /** 31 | * Show the search button and the category list. 32 | */ 33 | SHOW_SEARCH_AND_CATEGORY = "SHOW_SEARCH_AND_CATEGORY", 34 | } 35 | -------------------------------------------------------------------------------- /src/assets/ts/utils.ts: -------------------------------------------------------------------------------- 1 | import is_mobile from "is-mobile"; 2 | 3 | /** 4 | * Check whether the current window width is less than the given width. 5 | * 6 | * @param width - The width to compare with 7 | * @returns Whether the current window width is less than the given width 8 | */ 9 | export const isWidthLessThan = (width: number) => window.innerWidth < width; 10 | 11 | /** 12 | * Check whether the current window width is *small*. 13 | * 14 | * @note *Small* means screen width less than 768px.\ 15 | * This **shouldn't** be used to check whether 16 | * the current device is mobile or not. 17 | * 18 | * @returns Whether the current device is *small* or not 19 | */ 20 | export const isSmallScreen = () => isWidthLessThan(768); 21 | 22 | /** 23 | * Check whether the current device is mobile or not. 24 | * 25 | * @note This function uses `is-mobile` package to detect, 26 | * which is based on UA string and feature detection. 27 | * 28 | * @returns Whether the current device is mobile or not 29 | * 30 | * @see https://www.npmjs.com/package/is-mobile 31 | */ 32 | export const isMobile = () => is_mobile({ tablet: true }); 33 | 34 | export const sleep = (ms: number) => 35 | new Promise((res) => setTimeout(res, ms)); 36 | 37 | /** 38 | * Navigate to the element with the given ID. 39 | * 40 | * @param id - ID of the element to navigate to 41 | * @param smooth - Whether to scroll smoothly or not 42 | * @param pushState - Whether to push state to history or not 43 | */ 44 | export const navigate = ( 45 | id: string, 46 | smooth: boolean = true, 47 | pushState: boolean = true 48 | ) => { 49 | const OFFSET = 4 * 16; 50 | let el = document.getElementById(id); 51 | if (el) { 52 | const y = el.getBoundingClientRect().top + window.scrollY - OFFSET; 53 | 54 | window.scrollTo({ top: y, behavior: smooth ? "smooth" : "auto" }); 55 | if (pushState) history.pushState(history.state, "", `#${id}`); 56 | } 57 | }; 58 | 59 | export const shuffle = (array: T[]): T[] => { 60 | let i = array.length; 61 | 62 | while (i !== 0) { 63 | let rnd = Math.floor(Math.random() * i); 64 | i--; 65 | [array[i], array[rnd]] = [array[rnd], array[i]]; 66 | } 67 | 68 | return array; 69 | }; 70 | 71 | /** The screen width of a small screen. */ 72 | export const SMALL_SCREEN_WIDTH: number = 768; 73 | 74 | /** The minimum screen width required for rightbar to display. */ 75 | export const RIGHTBAR_THRESHOLD: number = 880; 76 | 77 | /** The threshold screen width required for leftbar to switch style. */ 78 | export const LEFTBAR_THRESHOLD: number = 880; 79 | -------------------------------------------------------------------------------- /src/components/Comment.vue: -------------------------------------------------------------------------------- 1 | 26 | 27 | 32 | 33 | 158 | -------------------------------------------------------------------------------- /src/components/Content.vue: -------------------------------------------------------------------------------- 1 | 57 | 58 | 84 | 85 | 89 | 90 | 119 | -------------------------------------------------------------------------------- /src/components/Footer.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 19 | 20 | 49 | -------------------------------------------------------------------------------- /src/components/LinkTo.vue: -------------------------------------------------------------------------------- 1 | 15 | 16 | 38 | 39 | 61 | -------------------------------------------------------------------------------- /src/components/Logo.vue: -------------------------------------------------------------------------------- 1 | 40 | 41 | 57 | 58 | 86 | -------------------------------------------------------------------------------- /src/components/Metadata.vue: -------------------------------------------------------------------------------- 1 | 43 | 44 | 65 | 66 | 108 | -------------------------------------------------------------------------------- /src/components/RightBarDetail.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 29 | 30 | 55 | -------------------------------------------------------------------------------- /src/components/Timestamp.vue: -------------------------------------------------------------------------------- 1 | 24 | 25 | 32 | 33 | 58 | -------------------------------------------------------------------------------- /src/components/md/Anchor.vue: -------------------------------------------------------------------------------- 1 | 5 | 6 | 14 | 15 | 34 | -------------------------------------------------------------------------------- /src/components/md/BlockCode.vue: -------------------------------------------------------------------------------- 1 | 13 | 14 | 22 | -------------------------------------------------------------------------------- /src/components/md/BlockMath.vue: -------------------------------------------------------------------------------- 1 | 16 | 17 | 25 | -------------------------------------------------------------------------------- /src/components/md/Delimiter.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/components/md/DotPattern.vue: -------------------------------------------------------------------------------- 1 | 27 | 28 | 53 | -------------------------------------------------------------------------------- /src/components/md/Fold.vue: -------------------------------------------------------------------------------- 1 | 45 | 46 | 79 | 80 | 144 | -------------------------------------------------------------------------------- /src/components/md/Grid.vue: -------------------------------------------------------------------------------- 1 | 78 | 79 | 105 | 106 | 138 | -------------------------------------------------------------------------------- /src/components/md/Heading.vue: -------------------------------------------------------------------------------- 1 | 11 | 12 | 18 | 19 | 47 | -------------------------------------------------------------------------------- /src/components/md/ImageCaptioned.vue: -------------------------------------------------------------------------------- 1 | 17 | 18 | 26 | 27 | 60 | -------------------------------------------------------------------------------- /src/components/md/InlineMath.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 11 | -------------------------------------------------------------------------------- /src/components/md/LinkCard.vue: -------------------------------------------------------------------------------- 1 | 9 | 10 | 36 | 37 | 103 | -------------------------------------------------------------------------------- /src/components/md/Note.vue: -------------------------------------------------------------------------------- 1 | 15 | 16 | 26 | 27 | 48 | -------------------------------------------------------------------------------- /src/components/md/Waterfall.vue: -------------------------------------------------------------------------------- 1 | 25 | 26 | 41 | 42 | 46 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | // Modules 2 | import { createApp } from "vue"; 3 | import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; 4 | import MasonryWall from "@yeger/vue-masonry-wall"; 5 | 6 | // Vue 7 | import App from "./App.vue"; 8 | 9 | // JS 10 | import "./assets/ts/fontawesome"; 11 | // import "./assets/ts/cursor"; 12 | import router from "./router"; 13 | 14 | // CSS 15 | import "./assets/css/reset.styl"; 16 | // import "./assets/css/cursor.styl"; 17 | import "katex/dist/katex.min.css"; 18 | import "overlayscrollbars/overlayscrollbars.css"; 19 | 20 | // Console 21 | const consoleMessage = () => { 22 | const year = new Date().getFullYear(); 23 | console.log(` 24 | ┌─Xecades Alpha::Note────────────────────────────────┐ 25 | │ │ 26 | │ Yet another notebook │ 27 | │ Part of the Alpha Project. │ 28 | │ │ 29 | ├────────────────────────────────────────────────────┤ 30 | │ │ 31 | │ Github https://github.com/Xecades/Note/ │ 32 | │ Website https://xecades.xyz/ │ 33 | │ QQ [DNS TXT] https://qq.xecades.xyz/ │ 34 | │ │ 35 | ├────────────────────────────────────────────────────┤ 36 | │ │ 37 | │ Built with Vue.js and... and my laptop. │ 38 | │ │ 39 | ├────────────────────────────────────────────────────┤ 40 | │ │ 41 | │ - [GPL-3.0 License] for code │ 42 | │ - [CC BY-NC-SA 4.0 License] for notes │ 43 | │ │ 44 | │ Copyright © 2024 - ${year} Xecades │ 45 | │ │ 46 | └────────────────────────────────────────────────────┘ 47 | `); 48 | }; 49 | 50 | // Main 51 | (async () => { 52 | consoleMessage(); 53 | 54 | const app = createApp(App); 55 | app.use(router); 56 | app.use(MasonryWall); 57 | app.component("font-awesome-icon", FontAwesomeIcon); 58 | app.mount("#app"); 59 | })(); 60 | -------------------------------------------------------------------------------- /src/router/index.ts: -------------------------------------------------------------------------------- 1 | import routes from "@cache/routes"; 2 | import { createRouter, createWebHistory } from "vue-router"; 3 | import { assertType } from "@/assets/ts/types"; 4 | 5 | import type { RouteMeta } from "vite-plugin-vue-xecades-note"; 6 | 7 | const router = createRouter({ 8 | routes: routes, 9 | scrollBehavior: (_, __, saved) => saved ?? { left: 0, top: 0 }, 10 | history: createWebHistory(), 11 | }); 12 | 13 | router.afterEach((to, from) => { 14 | const meta = assertType(to.meta); 15 | if (meta.type === "root") document.title = "Xecades Notes"; 16 | else document.title = `${meta.attr.title} | Xecades Notes`; 17 | }); 18 | 19 | export default router; 20 | -------------------------------------------------------------------------------- /todo.md: -------------------------------------------------------------------------------- 1 | ## 近期 2 | 3 | - typst 生成的 svg,支持文本选中 4 | - typst 支持中文(让 workflow 先安装字体) 5 | - 修改 . 的渲染 6 | - LeftBar 添加折叠(?) 7 | - 深色模式切换按钮(切换的时候给所有元素设置临时的 transition !important?)(放 rightbar?或者不要也可以) 8 | - index comp,hover 范围广一点 9 | - 字体 cdn 10 | - 添加 backtotop 按钮 11 | 12 | ## 灵感 13 | 14 | - index page,"Xecades Notes"特大居中,页面滚动过程中逐渐回到左上角 15 | - 元胞自动机、陨石雨、Gravity 16 | 17 | ## 长期 18 | 19 | - 搓一个 counter 20 | - 重新设计 logo 21 | - 搓一个评论系统(考虑使用 Serverless) 22 | - Status 页面(如果有了后端) 23 | - CDN 24 | 25 | ## Index Comp 26 | 27 | - 加上 tag?(例如“课程笔记”) 28 | - 考虑不同 category 对应不同颜色? 29 | 30 | ## Bug 31 | 32 | - `\n\n\n---\n` 会被 MarkdownItAnchor 误处理成标题 33 | - 行内第一个 token 为 mdc 时,会渲染失败,当前解决方案是在前面加上   34 | 35 | ## 当前 36 | 37 | - Code block 行高亮 38 | - Code block 添加标题、复制按钮 39 | - Dark Mode code block prism css error(目前是在 twikoo 中设置 theme 为 none) 40 | - MDCShorthand 前加 MDCBlock,shorthand 会渲染失败 41 | - 支持 Markdown 内部 style 标签 42 | - AnimateHeight 嵌套的时候,第一次点击内部的 fold 会不反应(推测是 AnimateHeight 库的问题,手搓一个?) 43 | - Table 加上滚动条(横向) 44 | - 搜索改成 token 级别的 45 | - fold、tab 等支持搜索 46 | - block code meta 参数的处理(转义、引号) 47 | - 搜索支持上下键选择,回车跳转 48 | 49 | 以及代码中 @todo 项 50 | 51 | ## 暂时不重要的 TODO 52 | 53 | - 添加 HTML `` 那一堆属性 54 | - 代码块换行 55 | - Command + P 打印样式 56 | - 想办法过滤掉 \`[公式]\` 等 slot 内容 57 | - 搜索功能保留 LaTeX 源码(?) 58 | - Safari 上那一堆 favicon 的显示 59 | - Safari 控制台字体问题 60 | - 标点挤压,需要中英文排版系统 61 | - 搜索数据库中空格的处理(要不直接删掉?) 62 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@vue/tsconfig/tsconfig.dom.json", 3 | "include": ["env.d.ts", "src/**/*", "src/**/*.vue", "cache/**/*"], 4 | "exclude": ["src/**/__tests__/*"], 5 | "compilerOptions": { 6 | "composite": true, 7 | "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", 8 | "strict": true, 9 | "jsx": "preserve", 10 | "jsxImportSource": "vue", 11 | "lib": ["ES2021", "DOM"], 12 | 13 | "baseUrl": ".", 14 | "paths": { 15 | "@/*": ["./src/*"], 16 | "@cache/*": ["./cache/*"] 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "files": [], 3 | "references": [ 4 | { 5 | "path": "./tsconfig.node.json" 6 | }, 7 | { 8 | "path": "./tsconfig.app.json" 9 | } 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /tsconfig.node.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node22/tsconfig.json", 3 | "include": [ 4 | "vite.config.*", 5 | "vitest.config.*", 6 | "cypress.config.*", 7 | "nightwatch.conf.*", 8 | "playwright.config.*" 9 | ], 10 | "compilerOptions": { 11 | "composite": true, 12 | "noEmit": true, 13 | "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", 14 | 15 | "module": "ESNext", 16 | "moduleResolution": "Bundler", 17 | "types": ["node"] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "rewrites": [{ "source": "/:path*", "destination": "/" }], 3 | "trailingSlash": false, 4 | "github": { 5 | "enabled": false 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /vite.config.ts: -------------------------------------------------------------------------------- 1 | import { fileURLToPath, URL } from "node:url"; 2 | import { defineConfig } from "vite"; 3 | 4 | import vue from "@vitejs/plugin-vue"; 5 | import vueJsx from "@vitejs/plugin-vue-jsx"; 6 | import vueDevTools from "vite-plugin-vue-devtools"; 7 | import vueXecadesNote from "vite-plugin-vue-xecades-note"; 8 | import autoprefixer from "autoprefixer"; 9 | 10 | const customElement = ["rb", "center"]; 11 | 12 | export default defineConfig({ 13 | plugins: [ 14 | // [@vitejs/plugin-vue] 15 | vue({ 16 | template: { 17 | compilerOptions: { 18 | isCustomElement: (tag) => customElement.includes(tag), 19 | }, 20 | }, 21 | include: [/\.vue$/], 22 | }), 23 | 24 | // [@vitejs/plugin-vue-jsx] 25 | vueJsx(), 26 | 27 | // [vite-plugin-vue-devtools] 28 | vueDevTools(), 29 | 30 | // [vite-plugin-vue-xecades-note] 31 | // @ts-ignore 32 | vueXecadesNote({ componentDir: "src/components/md" }), 33 | ], 34 | resolve: { 35 | alias: { 36 | "@": fileURLToPath(new URL("./src", import.meta.url)), 37 | "@cache": fileURLToPath(new URL("./cache", import.meta.url)), 38 | }, 39 | }, 40 | css: { 41 | postcss: { 42 | plugins: [autoprefixer()], 43 | }, 44 | }, 45 | }); 46 | --------------------------------------------------------------------------------