diff --git a/CNAME b/CNAME deleted file mode 100644 index 8bb6f35c..00000000 --- a/CNAME +++ /dev/null @@ -1 +0,0 @@ -note.lilyarnold.cc \ No newline at end of file diff --git a/search/search_index.json b/search/search_index.json index 5363d1c9..c2a7203c 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Welcome to Lily's Notebook!","text":""},{"location":"#about-me","title":"About me","text":""},{"location":"#my-github","title":"My GitHub","text":""},{"location":"#my-gmail-and-my-zju-email","title":"My Gmail and My ZJU Email","text":""},{"location":"#_1","title":"Welcome to Lily's Notebook!","text":""},{"location":"invisible/","title":"Invisible","text":"
\\(\\hspace{1cm}\\) \u4eca\u5929\u65e9\u4e0a\u8d77\u6765\uff0c\u5929\u7a7a\u7070\u9eef\u9eef\u7684\uff0c\u5f53\u7136\uff0c\u65f6\u65f6\u662f\u8fd9\u6837\uff0c\u4e5f\u6b63\u662f\u8fd9\u6837\uff0c\u624d\u65f6\u65f6\u662f\u5371\u9669\u7684\u3002
\\(\\hspace{1cm}\\) \u6211\u68b3\u4e86\u4e00\u4e2a\u9a6c\u5c3e\u8fab\u3002
\\(\\hspace{1cm}\\) \u6253\u5f00\u7535\u89c6\uff0c\u55e1\u55e1\u7684\u58f0\u97f3\u50cf\u662f\u4e00\u4e0b\u4ece\u8fdc\u65b9\u62bd\u5438\u8fdb\u6765\uff0c\u6a21\u7cca\u51fa\u4e00\u7247\u6e05\u6f88\u3002\u201c\u6839\u636e\u8fd1\u4e9b\u5e74\u6765\u79d1\u5b66\u5bb6\u7814\u7a76\u7edf\u8ba1\uff0c\u5168\u7403\u660e\u7ea4\u5269\u4f59\u91cf\u5927\u7ea6\u4e3a0.03\u6839/\u5e73\u65b9\u5343\u7c73\uff0c\u5168\u7403\u5404\u56fd\u5fd7\u613f\u8005\u4e5f\u90fd\u5728\u4e3a\u626b\u9664\u660e\u7ea4\u5949\u732e\u7740\u9c9c\u8840\u548c\u6c57\u6c34\uff0c\u636e\u4f30\u8ba1\uff0c\u4ee5\u76ee\u524d\u8fdb\u7a0b\u5168\u7403\u57fa\u672c\u4e0a\u53ef\u4ee5\u5728X\u5e74\u57fa\u672c\u5f7b\u5e95\u6e05\u9664\u660e\u7ea4\u3002\u7136\u800c\u4e13\u5bb6\u4ecd\u7136\u5efa\u8bae\u5e7f\u5927\u7fa4\u4f17\u5728\u6b63\u5e38\u751f\u6d3b\u4e4b\u4f59\u505a\u597d\u660e\u7ea4\u65e5\u5e38\u9632\u62a4\u3002\u4e0b\u9762\u64ad\u653e\u51e0\u4e2a\u660e\u7ea4\u9632\u62a4\u65e5\u5e38\u8d34\u58eb\uff1a1.\u65e5\u5e38\u5e94\u8be5\u4fdd\u6301\u653e\u677e\u5fc3\u6001\uff0c\u4e0d\u8981\u8fc7\u4e8e\u7126\u8651\u660e\u7ea4\u95ee\u9898\u2026\u2026\u201d
\\(\\hspace{1cm}\\) \u8001\u751f\u5e38\u8c08\uff0c\u6211\u6362\u4e86\u53f0\u3002
\\(\\hspace{1cm}\\) \u201c\u636e\u672c\u53f0\u8bb0\u8005\u62a5\u9053\uff0c\u672c\u5e02\u4e8e\u6628\u65e5\u53d1\u751f\u4e00\u8d77\u2018\u62a2\u53d1\u6848\u2019 \uff0c\u53d7\u5bb3\u4ebaX\u5148\u751f\u88ab\u952f\u65ad\u8fd120cm\u5934\u53d1\uff0c\u5e02\u503c\u4f30\u8ba1\u572820000\u5143\u5de6\u53f3\uff0c\u73b0\u6b63\u7d27\u6025\u8c03\u67e5\u6d89\u5acc\u4eba\u5458\uff0c\u8bf7\u5e7f\u5927\u5e02\u6c11\u6ce8\u610f\u597d\u81ea\u8eab\u5b89\u5168\u4fdd\u62a4\u2026\u2026\u201d
\\(\\hspace{1cm}\\) \u6211\u4e0d\u7531\u5f97\u6478\u4e86\u6478\u81ea\u5df1\u7684\u5934\u53d1\uff1a\u6211\u5f88\u559c\u6b22\u6211\u7684\u5934\u53d1\uff0c\u9ed1\u9ed1\u7684\uff0c\u4eae\u4eae\u7684\uff0c\u67d4\u987a\u7684\uff0c\u5e02\u4ef7\u5e94\u8be5\u5f88\u9ad8\uff0c\u6240\u4ee5\u6211\u4e00\u76f4\u6ca1\u6709\u5f88\u52aa\u529b\u5de5\u4f5c\uff0c\u5b9e\u5728\u4e0d\u884c\u4e86\uff0c\u6211\u5c31\u53bb\u628a\u5934\u53d1\u5356\u4e86\uff0c\u6211\u4e5f\u4e0d\u6015\u6b7b\uff0c\u4f46\u6211\u70ed\u7231\u751f\u6d3b\u3002
\\(\\hspace{1cm}\\) \u81f3\u4e8e\u4e3a\u4ec0\u4e48\u73b0\u5728\u5934\u53d1\uff0c\u6216\u8005\u8bf4\u9ad8\u8d28\u91cf\u5047\u53d1\uff0c\u7adf\u7136\u4ef7\u683c\u5982\u6b64\u9ad8\u6602\uff0c\u662f\u4ece\u660e\u7ea4\u7684\u53d1\u73b0\u5f00\u59cb\u7684\u3002\u5927\u6982\u5728\u5341\u51e0\u5e74\u524d\uff0c\u5728Stephen\u5c0f\u9547\uff0c\u9065\u8fdc\u7684\u5317\u6b27\uff0c\u5728\u90a3\u7247\u5b81\u9759\uff0c\u6f2b\u957f\u4e0e\u5bd2\u51b7\u4e4b\u95f4\uff0c\u7a81\u7136\u6709\u4e86\u70ed\u8840\u6cb8\u817e\uff1a\u4e00\u4e2a\u4e0b\u5348\uff0c\u9633\u5149\u80fd\u6620\u51fa\u7a7a\u4e2d\u7684\u9648\u7c92\uff1b\u8857\u4e0a\uff0c\u884c\u4eba\u4e09\u4e24\uff1b\u7a81\u7136\u5c31\u6709\u4e86\u4e00\u9053\u8840\u5149\u3002\u5f53\u65f6\u8def\u4eba\u5927\u6982\u90fd\u60ca\u5446\u4e86\uff0c\u51bb\u5728\u90a3\u7247\u5bd2\u51b7\u7684\u8840\u8272\u91cc\u3002\u662f\u4e00\u4e2a\u884c\u4eba\uff0c\u4e00\u4e2a\u8001\u7237\u7237\uff0c\u5a46\u5a11\u7740\u80cc\uff0c\u6162\u6162\u5730\u8d70\u7740\uff0c\u7136\u540e\u7a81\u7136\u62e6\u8170\u65ad\u4e86\uff0c\u5012\u4e0b\uff0c\u5929\u8fb9\u4e00\u7247\u7ea2\u3002\u5728\u4e00\u6bb5\u53ef\u6016\u7684\u9759\u6b62\u548c\u6c89\u9ed8\u4e4b\u540e\uff0c\u6709\u4eba\u4e0a\u524d\u67e5\u770b\uff0c\u53c8\u5012\u4e0b\u4e86\u3002
\\(\\hspace{1cm}\\) \u5bc2\u9759\u4e4b\u58f0\u3002
\\(\\hspace{1cm}\\) \u540e\u6765\u7684\u540e\u6765\uff0c\u7ec8\u4e8e\u6709\u4eba\u53d1\u73b0\uff0c\u5728\u90a3\u5929\u707f\u70c2\u7684\u9633\u5149\u4e0b\uff0c\u90a3\u91cc\u98d8\u7740\u4e00\u6bb5\u5c0f\u5c0f\u7684\u7ea4\u7ef4\u72b6\u7684\u7ec6\u4e1d\uff0c\u5f88\u7ec6\uff0c\u80fd\u53d1\u73b0\u53ea\u662f\u56e0\u4e3a\u5728\u9633\u5149\u4e0b\u90a3\u7ec6\u4e1d\u6709\u4e9b\u5fae\u7684\u95ea\u70c1\u53d1\u5149,\u6240\u4ee5\uff0c\u5c31\u53eb\u505a\u660e\u7ea4\uff0c\u591a\u7f8e\u4e3d\u7684\u540d\u5b57\uff0c\u7ea4\u7ec6\u800c\u8106\u5f31\u3002
\\(\\hspace{1cm}\\) \u6ca1\u6709\u4eba\u77e5\u9053\u660e\u7ea4\u662f\u54ea\u91cc\u6765\u7684\uff0c\u6709\u4eba\u8bf4\u662f\u4e00\u6b21\u9ad8\u7ea7\u6b66\u5668\u7684\u6ee5\u7528\uff0c\u67d0\u79cd\u5316\u5b66\u7269\u8d28\uff1b\u4e5f\u6709\u4eba\u8bf4\u662f\u67d0\u4e2a\u6050\u6016\u7ec4\u7ec7\u5455\u5c3d\u5fc3\u8840\u5728\u5168\u7403\u5e03\u7f6e\u4e0b\u51e0\u5904\u4ee5\u5f15\u8d77\u6050\u614c\u3002\u4e0d\u7ba1\u600e\u4e48\u6837\uff0c\u6050\u614c\u662f\u6709\u7684\uff1a\u636e\u76ee\u524d\u6240\u77e5\uff0c\u660e\u7ea4\u5206\u5e03\u4e0d\u5e7f\uff0c\u4f46\u6740\u4f24\u529b\u6781\u5927\u3002\u53d1\u73b0\u660e\u7ea4\u540e\uff0c\u4e5f\u53ea\u80fd\u5efa\u7acb\u7981\u884c\u533a\uff0c\u800c\u505a\u4e0d\u5230\u5f7b\u5e95\u7684\u201c\u6e05\u695a\u201d\u2014\u2014\u4e0d\u8fc7\u7528\u4ec0\u4e48\u60f3\u8981\u6e05\u695a\u660e\u7ea4\uff0c\u660e\u7ea4\u603b\u80fd\u5207\u65ad\uff0c\u4eba\u6216\u8005\u91d1\u5c5e\u3002
\\(\\hspace{1cm}\\) \u636e\u8bf4\u88ab\u660e\u7ea4\u5207\u65ad\u7684\u4e00\u77ac\u95f4\u5e76\u6ca1\u6709\u4ec0\u4e48\u5177\u4f53\u611f\u53d7\uff0c\u5176\u5b9e\u8fd9\u662f\u4e00\u79cd\u63a8\u65ad\uff0c\u56e0\u4e3a\u5f88\u591a\u884c\u8fdb\u5f88\u6162\u7684\u4eba\uff0c\u5c31\u50cf\u7b2c\u4e00\u4f8b\u53d7\u5bb3\u8005\u8001\u7237\u7237\uff0c\u90fd\u6b7b\u4e8e\u8eaf\u4f53\u7684\u5b8c\u5168\u5206\u9694\u3002
\\(\\hspace{1cm}\\) \u603b\u4e4b\uff0c\u5728\u90a3\u4e4b\u540e\uff0c\u4eba\u4eec\u90fd\u6709\u4e9b\u4e0d\u6562\u51fa\u95e8\u4e86\uff0c\u5728\u8857\u4e0a\u8d70\u4e5f\u5f88\u6162\u3002
\\(\\hspace{1cm}\\) \u51e0\u5e74\u524d\u6709\u4e00\u4e2a\u5e78\u8fd0\u7684\u5973\u5b69\u5b50\u9047\u5230\u660e\u7ea4\u5374\u6beb\u53d1\u672a\u635f\u2014\u2014\u786e\u5207\u7684\u8bf4\uff0c\u53ea\u635f\u6beb\u53d1\uff1a\u5973\u5b69\u8d70\u8def\u65f6\u5f88\u7231\u7529\u5934\u53d1\uff0c\u53d1\u95f4\u5411\u5de6\u6643\u65f6\u4e00\u4e0b\u88ab\u5207\u65ad\u4e86\u4e00\u622a\uff0c\u98d8\u98d8\u6d12\u6d12\u3002\u540e\u6765\uff0c\u7537\u5b50\u4e5f\u5f00\u59cb\u84c4\u957f\u53d1\uff0c\u4f20\u8bf4\u4e2d\u6f02\u6d6e\u5ea6\u8303\u56f4\u5f88\u5e7f\u7684\u9ad8\u8d28\u91cf\u5047\u53d1\u4e5f\u5f00\u59cb\u5728\u5e02\u573a\u4e0a\u5d1b\u8d77\u4e86\u3002
\\(\\hspace{1cm}\\) \u6211\u4e5f\u559c\u6b22\u8d70\u8def\u7684\u65f6\u5019\u9ad8\u9ad8\u5730\u7529\u5934\u53d1\uff0c\u6211\u89c9\u5f97\u771f\u5f88\u7f8e\uff0c\u5973\u5b69\u5b50\u7684\u9752\u6625\u7684\u7f8e\uff0c\u4e0d\u8fc7\uff0c\u4e5f\u8bb8\u662f\u56e0\u4e3a\u6211\u5728\u5fc3\u91cc\u7684\u4e00\u89d2\u6697\u81ea\u745f\u7f29\u5427\u3002
\\(\\hspace{1cm}\\) \u601d\u7eea\u53c8\u548c\u7535\u89c6\u7684\u58f0\u97f3\u76f8\u9047\u4e86\u3002
\\(\\hspace{1cm}\\) \u201c\u4e0b\u9762\u672c\u53f0\u4e3a\u60a8\u5e26\u6765\u5fd7\u613f\u6e05\u626b\u660e\u7ea4\u73b0\u573a\uff0c\u8ba9\u6211\u4eec\u4e3a\u5728\u573a\u7684\u6bcf\u4e00\u4f4d\u82f1\u96c4\u9f13\u638c\u559d\u5f69\uff01\u201d\u955c\u5934\u8f6c\u6362\uff0c\u4eba\u7fa4\u7199\u6518\uff0c\u6211\u770b\u89c1\u4e86\u5e74\u8f7b\u7684\u7537\u5973\uff0c\u4e2d\u5e74\u7684\u76b1\u7eb9\uff0c\u82b1\u767d\u7684\u5934\u53d1\uff0c\u4e73\u7259\uff0c\u9152\u7a9d\uff0c\u8033\u6d1e\u2026\u2026\u6240\u6709\u7684\u4e00\u5207\u90fd\u5728\u70ed\u70c8\u7684\u6c14\u6c1b\u4e2d\u6a21\u7cca\u6b86\u5c3d\uff0c\u8fd8\u6709\u9065\u9065\u7684\u70ed\u8840\u97f3\u4e50\u5728\u7a7a\u6c14\u4e2d\u6643\u8361\u3002
\\(\\hspace{1cm}\\) \u5927\u6982\u662f\u5ba3\u5e03\u4e86\u5f00\u59cb\uff0c\u4eba\u7fa4\u52a8\u8d77\u6765\u4e86\uff0c\u8dd1\u8d77\u6765\u4e86\uff0c\u5374\u4e00\u76f4\u662f\u62e5\u6324\u7684\u53c8\u6a21\u7cca\u8d77\u6765\u4e86\u3002\u6211\u77e5\u9053\u4ed6\u4eec\u4e3a\u4ec0\u4e48\u5954\u8dd1\uff0c\u56e0\u4e3a\u636e\u8bf4\u5feb\u901f\u901a\u8fc7\u660e\u7ea4\u800c\u6b7b\u4ea1\u6ca1\u6709\u4efb\u4f55\u75db\u82e6\u3002\u6211\u4e5f\u77e5\u9053\u4ed6\u4eec\u4e3a\u4ec0\u4e48\u62e5\u6324\uff0c\u56e0\u4e3a\u53ea\u6709\u62e5\u5728\u4e00\u8d77\uff0c\u624d\u80fd\u4fdd\u8bc1\u7ecf\u8fc7\u7684\u6bcf\u4e00\u5904\u5730\u65b9\u90fd\u6ca1\u6709\u660e\u7ea4\u9003\u8fc7\u3002
\\(\\hspace{1cm}\\) \u6211\u5374\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u662f\u4eba\u3002\u4e3a\u4ec0\u4e48\u4e0d\u662f\u8f66\u6216\u8005\u673a\u5668\u6216\u8005\u6280\u672f\u2026\u2026\u4e5f\u6709\u5f88\u591a\u8bf4\u6cd5\uff0c\u6709\u4eba\u8bf4\u660e\u7ea4\u5206\u5e03\u5b9e\u5728\u7a00\u5c11\u4e0d\u5fc5\u4e3a\u6b64\u6295\u5165\u592a\u591a\u7cbe\u529b\uff0c\u786e\u5b9e\uff0c\u6bcf\u5e74\u636e\u62a5\u9053\u5927\u6982\u53ea\u67092-3\u4f8b\u53d7\u5bb3\u8005\u3002\u4e5f\u6709\u4eba\u8bf4\u8fd9\u6837\u7684\u6d3b\u52a8\u4ece\u6765\u6ca1\u6709\u8fc7\u727a\u7272\uff0c\u8fd8\u53ef\u4ee5\u57f9\u517b\u7fa4\u4f17\u7684\u56e2\u7ed3\u5408\u4f5c\u80fd\u529b\uff0c\u8fd8\u6709\u4eba\u8bf4\u8fd9\u662f\u4e2a\u9634\u8c0b\u8bba\u3002
\\(\\hspace{1cm}\\) \u8fd8\u662f\u4e0d\u7ba1\u600e\u6837\uff0c\u603b\u6709\u5730\u65b9\u5728\u62db\u52df\u5fd7\u613f\u8005\uff0c\u4e5f\u603b\u6709\u8fd9\u6837\u7684\u5e74\u8f7b\u7f8e\u4e3d\uff0c\u70ed\u8840\u6f8e\u6e43\u7684\u5fd7\u613f\u8005\u81ea\u544a\u594b\u52c7\u3002
\\(\\hspace{1cm}\\) \u4e00\u9635\u6c89\u90c1\u7ffb\u6d8c\uff0c\u6211\u5173\u4e0a\u4e86\u7535\u89c6\u3002
\\(\\hspace{1cm}\\) \u6211\u6700\u6700\u4eb2\u7231\u7684\u8bfb\u8005\uff0c\u6211\u591a\u4e48\u96be\u5e0c\u671b\u548c\u4f60\u4eec\u4e00\u6837\u628a\u8fd9\u5f53\u6210\u4e00\u4e2a\u8981\u7231\u62a4\u5934\u53d1\u7684\u5c0f\u6545\u4e8b\uff0c\u53ef\u662f\u6211\u4e0d\u884c\uff0c\u6211\u9762\u5bf9\u7684\u4e0d\u662f\u865a\u7a7a\uff0c\u662f\u771f\u5b9e\u3002
\\(\\hspace{1cm}\\) \u6574\u7406\u4e86\u4e0b\u5934\u53d1\uff0c\u6211\u51fa\u4e86\u5bb6\u95e8\uff0c\u62ac\u5934\uff0c\u53ea\u770b\u89c1\u90a3\u6c89\u6c89\u7684\u5929\u5728\u884c\u8272\u5306\u5306\u4e2d\u9759\u9ed8\u4e0d\u8a00\u3002
"},{"location":"%E5%A4%A7%E4%BE%A0/","title":"\u5927\u4fa0","text":"\\(\\hspace{1cm}\\)\u5f88\u5c0f\u7684\u65f6\u5019\uff0c\u6211\u5c31\u77e5\u9053\uff0c\u6211\u4ee5\u540e\u8981\u505a\u4e00\u4ee3\u5927\u4fa0\uff0c\u884c\u8d70\u7eff\u6797\uff0c\u6d6a\u8361\u6c5f\u6e56\uff0c\u4e00\u8eab\u5251\u6c14\uff0c\u4e00\u8def\u6f47\u6d12\u3002
\\(\\hspace{1cm}\\)\u6c5f\u6e56\u4e0a\u7684\u4e8b\u60c5\u6211\u4eec\u5927\u591a\u90fd\u6709\u8033\u95fb\uff1a\u8981\u6709\u6b66\u529f\uff0c\u8981\u6709\u809d\u80c6\uff0c\u8981\u4e00\u8eab\u6e05\u98ce\u6717\u6717\uff0c\u6063\u610f\u5e73\u751f\uff0c\u4e0e\u4e16\u5043\u4ef0\u3002
\\(\\hspace{1cm}\\)\u4e8e\u662f\u6211\u4e0d\u65ad\u5730\u52aa\u529b\u7740\uff1a\u8d70\u5c71\u8d70\u6c34\uff0c\u95ef\u8361\u5929\u5730\uff0c\u56db\u5904\u8bbf\u5e08\uff0c\u56db\u5904\u5b66\u827a\u3002\u6768\u8fc7\u5927\u4fa0\u90a3\u6bb5\u4e0e\u6df1\u5c71\u4e3a\u4f34\u7684\u5c81\u6708\uff0c\u6211\u4e5f\u66fe\u76f8\u4f3c\u4eb2\u5386\u3002\u6211\u4ee5\u4e3a\u6211\u5c1d\u5c3d\u4e86\u6210\u957f\u4e3a\u4e00\u4ee3\u5927\u4fa0\u8fc7\u7a0b\u4e2d\u5e94\u6709\u7684\uff0c\u6240\u6709\u7684\u54c0\u621a\u4e0e\u5e78\u798f\uff0c\u6211\u4e3a\u6211\u7684\u4e00\u5207\u52aa\u529b\u800c\u611f\u5230\u5feb\u4e50\u4e0e\u6210\u5c31\u3002
\\(\\hspace{1cm}\\)\u6bcf\u6b21\u6e38\u5386\u56de\u5bb6\u65f6\uff0c\u5988\u5988\u5e38\u7528\u7565\u5e26\u5fe7\u4f24\u7684\u773c\u795e\u770b\u7740\u6211\uff0c\u6211\u77e5\u9053\uff0c\u5979\u662f\u592a\u62c5\u5fc3\u6211\uff0c\u5bb3\u6015\u6c5f\u6e56\u4e0a\u7684\u9ed1\u6697\u4f1a\u5c06\u6211\u541e\u6ca1\uff0c\u4e0d\u5269\u9aa8\u5934\uff1b\u6211\u4e5f\u77e5\u9053\uff0c\u5979\u4ecd\u662f\u65e5\u65e5\u591c\u591c\u76fc\u671b\u6211\u6210\u529f\uff0c\u5e26\u7740\u8346\u68d8\u7684\u5fc3\u548c\u96be\u8a00\u7684\u60c6\u6005\u3002\u603b\u5f97\u6765\u8bf4\uff0c\u5988\u5988\u662f\u5f88\u652f\u6301\u6211\u7684\uff0c\u5c31\u50cf\u7ae5\u5e74\u7684\u7761\u524d\u6545\u4e8b\u2014\u2014\u90a3\u4e9b\u6ee1\u662f\u4fa0\u8005\u98ce\u8303\u7684\u5feb\u610f\u6c5f\u6e56\u3002
\\(\\hspace{1cm}\\)\u90a3\u5929\uff0c\u6211\u6210\u5e74\u4e86\uff0c\u88f9\u4e86\u4e00\u4e2a\u5c0f\u5305\u88b1\uff1a\u4e00\u4e2a\u5988\u5988\u7ee3\u7684\u8377\u5305\uff0c\u51e0\u4e24\u94f6\u94b1\uff0c\u968f\u8eab\u8863\u7269\u3002\u7136\u540e\u6211\u5c31\u4e0a\u8def\u4e86\u2014\u2014\u6211\u51b3\u5b9a\u8981\u5f00\u542f\u81ea\u5df1\u7684\u4eba\u751f\uff1a\u5927\u4fa0\u7684\u4eba\u751f\u3002
\\(\\hspace{1cm}\\)\u7b2c\u4e00\u5929\uff0c\u6211\u6551\u4e86\u4e00\u4e2a\u843d\u96be\u7684\u5973\u5b50\u3002\u5979\u8877\u5fc3\u5730\u611f\u8c22\u6211\uff0c\u7136\u540e\u53c8\u5e26\u7740\u5fe7\u4f24\u5730\uff0c\u621a\u621a\u5730\u770b\u7740\u6211\uff0c\u55eb\u5685\u7740\uff1a\u53ef\u662f\u4f60\u5e2e\u4e0d\u4e86\u6211\u2014\u2014\u90a3\u5fe7\u4f24\u548c\u5988\u5988\u7684\u5fe7\u4f24\u6709\u4e9b\u76f8\u4f3c\u3002\u6211\u628a\u968f\u8eab\u7684\u94f6\u4e24\u90fd\u585e\u7ed9\u4e86\u5979\uff0c\u5979\u6ca1\u6709\u6536\uff0c\u8885\u5a1c\u7740\u8d70\u4e86\u3002
\\(\\hspace{1cm}\\)\u4e00\u4e9b\u8ff7\u832b\uff0c\u4e00\u4e9b\u611f\u52a8\uff0c\u548c\u4e00\u4e9b\u5fe7\u6101\u3002
\\(\\hspace{1cm}\\)\u6211\u6ca1\u6709\u6e38\u79fb\u3002\u6211\u8fd8\u662f\u884c\u8d70\u7740\uff0c\u53c8\u89c1\u4e86\u4e00\u4e9b\u4e8b\uff0c\u6551\u4e86\u4e00\u4e9b\u4eba\uff0c\u8fd8\u662f\u8d70\u8fc7\u5c71\u5c71\u6c34\u6c34\u3002\u4ed6\u4eec\u90fd\u53d8\u6210\u4e86\u6211\u3002
\\(\\hspace{1cm}\\)\u90a3\u5929\u6211\u4e8c\u5341\u4e94\u5c81\uff0c\u6211\u7ec8\u4e8e\u6765\u5230\u4e86\u6211\u4ece\u5c0f\u68a6\u91cc\u7684\u6bbf\u5802\uff1a\u4fa0\u4e4b\u95e8\u3002\u6211\u77e5\u9053\u6211\u4e0d\u662f\u540d\u95e8\u6b63\u6d3e\uff0c\u4f46\u6211\u60f3\uff0c\u7ecf\u5386\u4e86\u8fd9\u8bb8\u591a\uff0c\u6211\u4e00\u5b9a\u4f1a\u88ab\u8ba4\u53ef\u2014\u2014\u6b66\u529f\u4e5f\u597d\uff0c\u809d\u80c6\u4e5f\u7f62\uff0c\u4e0d\u81f3\u4e8e\u540d\u5782\u9752\u53f2\uff0c\u81f3\u5c11\u8db3\u591f\u6177\u6168\u3002
\\(\\hspace{1cm}\\)\u6211\u8fdb\u4e86\u95e8\uff0c\u81ea\u62a5\u5bb6\u95e8\uff0c\u5b88\u95e8\u4eba\u5f15\u6211\u5411\u524d\uff0c\u5230\u4e86\u4e00\u5f20\u53e4\u6734\u7684\u684c\u5b50\uff0c\u684c\u524d\u5750\u7740\u4e00\u4f4d\u5782\u5782\u8001\u8005\u3002\u6211\u8840\u8109\u55b7\u6d8c\u2014\u2014\u50cf\u662f\u68a6\u91cc\u3002
\\(\\hspace{1cm}\\)\u201c\u8981\u6210\u4e3a\u5927\u4fa0\uff1f\u201d
\\(\\hspace{1cm}\\)\u201c\u662f\u7684\uff0c\u662f\u7684\u3002\u201d
\\(\\hspace{1cm}\\)\u201c\u591a\u5927\uff1f\u201d
\\(\\hspace{1cm}\\)\u201c\u4e8c\u5341\u4e94\u4e86\u3002\u201d
\\(\\hspace{1cm}\\)\u6cdb\u9ec4\u7684\u7eb8\u9875\uff0c\u76d6\u4e86\u4e2a\u9c9c\u7ea2\u7684\u5370\u7ae0\uff0c\u5199\u7740\u4e9b\u4ec0\u4e48\u4fa0\u4e4b\u5927\u8005\uff0c\u63a8\u5411\u6211\u3002
\\(\\hspace{1cm}\\)\u201c\u8d44\u683c\u8bc1\uff0c\u5927\u4fa0\u3002\u201d
\\(\\hspace{1cm}\\)\u4e8e\u662f\uff0c\u6211\u591a\u4e86\u4e00\u7eb8\u8584\u8584\u7684\u4e66\u9875\uff0c\u8d70\u51fa\u4e86\u5c71\u95e8\u3002
\\(\\hspace{1cm}\\)\u6211\u7a81\u7136\u60f3\u8d77\u5f88\u591a\u5e74\u524d\uff0c\u5728\u5988\u5988\u8fd8\u5728\u4e3a\u6211\u8bb2\u7761\u524d\u6545\u4e8b\u7684\u65f6\u5019\uff1a\u6211\u5e74\u7eaa\u5c0f\uff0c\u603b\u662f\u542c\u5230\u4e00\u534a\u5c31\u660f\u660f\u5165\u7761\uff0c\u4f46\u5988\u5988\u603b\u4f1a\u8bb2\u5b8c\u5979\u7684\u6545\u4e8b\u3002
\\(\\hspace{1cm}\\)\u6211\u60f3\u8d77\u591a\u5e74\u524d\u7684\u90a3\u4e2a\u591c\u665a\uff0c\u661f\u661f\u5929\u771f\u5730\u5728\u591c\u7a7a\u6d82\u9e26\uff0c\u5988\u5988\u770b\u7740\u6211\u7761\u773c\u60fa\u5fea\uff0c\u8f7b\u58f0\u5730\u8bb2\u7740\u6545\u4e8b\u7684\u7ed3\u5c3e\uff1a\u4ed6\u6700\u7ec8\u6ca1\u80fd\u6210\u4e3a\u5927\u4fa0\uff0c\u4f46\u4ed6\u52c7\u6562\uff0c\u575a\u5b9a\uff0c\u5766\u8361\u800c\u6f84\u6f88\uff0c\u4ed6\u770b\u8fc7\u5c71\u7684\u9ad8\u5927\uff0c\u770b\u8fc7\u6d77\u7684\u8fbd\u9614\uff0c\u4eba\u4eec\u8bf4\u4ed6\u4e00\u4e8b\u65e0\u6210\uff0c\u53ea\u662f\u6d6a\u8361\u6c5f\u6e56\uff0c\u4ed6\u4e0d\u540e\u6094\u3002
"},{"location":"ADS/D%26C/lec/","title":"Divide and Conquer","text":""},{"location":"ADS/D%26C/lec/#divide-and-conquer","title":"Divide and Conquer","text":"
Main Idea
General recurrence: \\(T(N) = aT(N/b) + f(N)\\)
"},{"location":"ADS/D%26C/lec/#example","title":"Example","text":""},{"location":"ADS/D%26C/lec/#maximum-subsequence-sum-problem","title":"Maximum Subsequence Sum Problem","text":"static int MaxSubSum(const int A[],int left,int right){\n int MaxLeftSum, MaxRightSum;\n int MaxLeftBorderSum, MaxRightBorderSum; \n int LeftBorderSum, RightBorderSum;\n int Center, i;\n if( left == right ){\n if(A[left]>0){return A[left];}\n else{return 0;}\n }\n Center = ( Left +Right ) / 2;\n MaxLeftSum = MaxSubSum( A, Left, Center ) \n MaxRightSum = MaxSubSum( A, Center + 1, Right );\n MaxLeftBorderSum = 0; LeftBorderSum = 0;\n for(i=Center+1;i>=left;i--){\n LeftBorderSum += A[i];\n if(leftBorderSum>MaxleftBorderSum){\n MaxleftBorderSum = LeftBorderSum;\n }\n }\n MaxRightBorderSum = 0; \n RightBorderSum = 0;\n for(i=Center;i<=Right;i++){\n RightBorderSum += A[i];\n if(RightBorderSum > MaxRightBorderSum){\n MaxRightBOrderSum = RightBorderSum\n }\n }\n return Max(MaxLeftSum,MaxRightSum,MaxLeftBorderSum+MaxRightBorderSum);\n}\nint MaxsubsequenceSum( const int A[],intN) {\n return MaxSubSum( A, 0, N - 1 );}\n
"},{"location":"ADS/D%26C/lec/#tree-traversal","title":"Tree Traversal","text":"Given a postorder traversal of a binary tree, and an inorder traversal of the same tree, we can reconstruct the tree.
Postorder : DEBFCA\nInorder : DBEACF\n
void BuildTree(int postL, int postR, int inL, int inR){\n if(postL>postR){return;}\n int root = postorder[postR];\n int k;\n for(k=inL;k<=inR;k++){\n if(inorder[k]==root){break;}\n }\n int numLeft = k-inL;\n BuildTree(postL,postL+numLeft-1,inL,k-1);\n BuildTree(postL+numLeft,postR-1,k+1,inR);\n}\n
We are given an array of n points in the plane, and the problem is to find out the closest pair of points in the array. This problem arises in a number of applications. For example, in air-traffic control, you may want to monitor planes that come too close together, since this may indicate a possible collision.
The Brute force solution is \\(O(n^2)\\) , compute the distance between each pair and return the smallest. We can calculate the smallest distance in \\(O(nlogn)\\) time using Divide and Conquer strategy.
"},{"location":"ADS/D%26C/lec/#on-logn2-approach","title":"\\(O(n (logn)^2)\\)\u200b approach","text":"Input: An array of n points P[ ]
Output: The smallest distance between two points in the given array.
As a pre-processing step, the input array is sorted according to x coordinates.
1) Find the middle point in the sorted array, we can take P[n/2]
as middle point.
2) Divide the given array in two halves. The first subarray contains points from P[0]
to P[n/2]
. The second subarray contains points from P[n/2+1]
to P[n-1]
.
3) Recursively find the smallest distances in both subarrays. Let the distances be \\(d_l\\) and \\(d_r\\). Find the minimum of \\(d_l\\) and \\(d_r\\). Let the minimum be \\(d\\)\u200b\u200b.
4) From the above 3 steps, we have an upper bound \\(d\\) of minimum distance. Now we need to consider the pairs such that one point in pair is from the left half and the other is from the right half. Consider the vertical line passing through P[n/2]
and find all points whose x coordinate is closer than \\(d\\)\u200b to the middle vertical line. Build an array strip[ ]
of all such points.
5) Sort the array strip[ ]
according to \\(y\\) coordinates.
6) Find the smallest distance in strip[ ]
. This is tricky. From the first look, it seems to be a \\(O(n^2)\\) step, but it is actually \\(O(n)\\)\u200b\u200b. It can be proved geometrically that for every point in the strip, we only need to check at most 7 points after it (note that strip is sorted according to Y coordinate). See this for more analysis.
7) Finally return the minimum of d and distance calculated in the above step (step 6)
// A divide and conquer program in C/C++ to find the smallest distance from a\n// given set of points.\n\n#include <stdio.h>\n#include <float.h>\n#include <stdlib.h>\n#include <math.h>\n\n// A structure to represent a Point in 2D plane\nstruct Point{\n int x, y;\n};\n\n// Needed to sort array of points according to X coordinate\nint compareX(const void* a, const void* b){\n Point *p1 = (Point *)a, *p2 = (Point *)b;\n return (p1->x - p2->x);\n}\n// Needed to sort array of points according to Y coordinate\nint compareY(const void* a, const void* b){\n Point *p1 = (Point *)a, *p2 = (Point *)b;\n return (p1->y - p2->y);\n}\n\n// A utility function to find the distance between two points\nfloat dist(Point p1, Point p2){\n return sqrt( (p1.x - p2.x)*(p1.x - p2.x) +\n (p1.y - p2.y)*(p1.y - p2.y)\n );\n}\n// A Brute Force method to return the smallest distance between two points\n// in P[] of size n\nfloat bruteForce(Point P[], int n){\n float min = FLT_MAX;\n for (int i = 0; i < n; ++i)\n for (int j = i+1; j < n; ++j)\n if (dist(P[i], P[j]) < min)\n min = dist(P[i], P[j]);\n return min;\n}\n\n// A utility function to find a minimum of two float values\nfloat min(float x, float y)\n{\n return (x < y)? x : y;\n}\n\n// A utility function to find the distance between the closest points of\n// strip of a given size. All points in strip[] are sorted according to\n// y coordinate. They all have an upper bound on minimum distance as d.\n// Note that this method seems to be a O(n^2) method, but it's a O(n)\n// method as the inner loop runs at most 6 times\nfloat stripClosest(Point strip[], int size, float d)\n{\n float min = d; // Initialize the minimum distance as d\n\n qsort(strip, size, sizeof(Point), compareY); \n\n // Pick all points one by one and try the next points till the difference\n // between y coordinates is smaller than d.\n // This is a proven fact that this loop runs at most 6 times\n for (int i = 0; i < size; ++i)\n for (int j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j)\n if (dist(strip[i],strip[j]) < min)\n min = dist(strip[i], strip[j]);\n\n return min;\n}\n\n// A recursive function to find the smallest distance. The array P contains\n// all points sorted according to x coordinate\nfloat closestUtil(Point P[], int n)\n{\n // If there are 2 or 3 points, then use brute force\n if (n <= 3)\n return bruteForce(P, n);\n // Find the middle point\n int mid = n/2;\n Point midPoint = P[mid];\n // Consider the vertical line passing through the middle point\n // calculate the smallest distance dl on left of middle point and\n // dr on right side\n float dl = closestUtil(P, mid);\n float dr = closestUtil(P + mid, n-mid);\n // Find the smaller of two distances\n float d = min(dl, dr);\n // Build an array strip[] that contains points close (closer than d)\n // to the line passing through the middle point\n Point strip[n];\n int j = 0;\n for (int i = 0; i < n; i++)\n if (abs(P[i].x - midPoint.x) < d)\n strip[j] = P[i], j++;\n\n // Find the closest points in strip. Return the minimum of d and closest\n // distance is strip[]\n return min(d, stripClosest(strip, j, d) );\n}\n\n// The main function that finds the smallest distance\n// This method mainly uses closestUtil()\nfloat closest(Point P[], int n)\n{\n qsort(P, n, sizeof(Point), compareX);\n\n // Use recursive function closestUtil() to find the smallest distance\n return closestUtil(P, n);\n}\n\n// Driver program to test above functions\nint main(){\n Point P[] = { {2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};\n int n = sizeof(P) / sizeof(P[0]);\n printf(\"The smallest distance is %f \", closest(P, n));\n return 0;\n}\n
"},{"location":"ADS/D%26C/lec/#onlogn-approach","title":"\\(O(nlogn)\\)\u200b approach","text":""},{"location":"ADS/D%26C/lec/#solving-recurrences","title":"Solving Recurrences","text":""},{"location":"ADS/D%26C/lec/#substitution-method-guess-and-prove-by-induction","title":"Substitution method : Guess and prove by induction","text":"Makeing a good guess
"},{"location":"ADS/D%26C/lec/#recursion-tree-method","title":"Recursion-tree method","text":"Case 1 If \\(f(N) = O(N^{\\log_ba-\\epsilon})\\) for some \\(\\epsilon > 0\\), then \\(T(N) = \\Theta(N^{\\log_ba})\\)
Proof:(Refer to Slides)
\\[\\begin{align*}\\sum_{i=0}^{\\log_bN-1}a^if(N/b^i) &= O(N^{log_b{a-\\epsilon} }\\frac{b^{\\epsilon log_bN}-1}{b^\\epsilon-1})\\\\&=O(N^{\\log_ba-\\epsilon}N^{\\epsilon})\\\\&=O(N^{\\log_ba})\\end{align*}\\]Therefore : \\(T(N) = \\Theta(N^{\\log_ba})+O(N^{\\log_ba}) =\\Theta(N^{\\log_ba})\\)
Case 2 If \\(f(N) = \\Theta(N^{\\log_ba})\\), then \\(T(N) = \\Theta(N^{\\log_ba}\\log N)\\)
Proof
\\[\\begin{align*}\\sum_{i=0}^{\\log_bN-1}a^if(N/b^i) &= \\sum_{i=0}^{\\log_bN-1}a^i\\Theta((\\frac{N}{b^i})^{\\log_ba})\\\\&=\\Theta(N^{\\log_ba}\\sum_{i=0}^{\\log_bN-1}(\\frac{a}{b^{log_ba} })^i)\\\\&=\\Theta(N^{\\log_ba}\\sum_{i=0}^{\\log_bN-1}(1)\\\\&=\\Theta(N^{\\log_ba}\\log N)\\end{align*}\\]Therefore : \\(T(N) = \\Theta(N^{\\log_ba}\\log N) + O(N^{\\log_ba}) = \\Theta(N^{\\log_ba}\\log N)\\) Case 3 If \\(f(N) = \\Omega(N^{\\log_ba+\\epsilon})\\) for some \\(\\epsilon > 0\\), if \\(af(N/b) \\leq kf(N)\\) for some \\(k<1\\) and sufficiently large \\(N\\), then \\(T(N) = \\Theta(f(N))\\)
Proof
\\[\\begin{align*}\\sum_{i=0}^{\\log_bN-1}a^if(N/b^i) &= \\Omega(N^{\\log_ba+\\epsilon}\\frac{b^{\\epsilon log_bN}-1}{b^\\epsilon-1})\\\\&=\\Omega(N^{\\log_ba+\\epsilon}N^{\\epsilon})\\\\&=\\Omega(N^{\\log_ba+\\epsilon})\\end{align*}\\]Therefore : \\(T(N) = \\Theta(f(N))\\)
"},{"location":"ADS/Heap/lec/","title":"Heap","text":"
The null path length, \\(Npl(X)\\), of any node \\(X\\) is the length of the shortest path from \\(X\\) to a node without two children. Define \\(Npl(NULL) = \u20131\\).
"},{"location":"ADS/Heap/lec/#def2","title":"Def.2","text":"The leftist heap property is that for every node \\(X\\)\u200b in the heap, the null path length of the left child is at least as large as that of the right child.
"},{"location":"ADS/Heap/lec/#theorem1","title":"Theorem.1","text":"A leftist tree with \\(r\\) nodes on the right path must have at least \\(2^r \u2013 1\\)\u200b nodes.
For the tree,left path actually all equals to its roots' NPL
\\(r=1\\) clearly holds
If \\(NLP(X)=r\\le k\\) holds then if \\(NLP(X)=r=k+1:\\)\u200b
Its right subtree must have \\(NLP(X)=k\\)
\\(\\because when\\ NLP(X)= k \\ right \\ path \\ has \\ at\\ least\\ 2^k-1\\ nodes\\)\u200b
So all the right part at least \\(2^k-1\\)
\\(r=1\\) clearly holds
If \\(NLP(X)\\le k\\) holds then if \\(NLP(X)=k+1:\\)
Its left subtree's right path must be \\(\\ge k\\)\u200b
So at least right path is k, so at least \\(2^k-1\\) for the left part
Thus \\(2^k-1+2^k-1+1=2^{k+1}-1\\)\u200b
Induction concludes.
PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2 ){ \n if ( H1 == NULL ) return H2; \n if ( H2 == NULL ) return H1; \n if ( H1->Element < H2->Element ){return Merge1( H1, H2 );} \n else {return Merge1( H2, H1 );}\n}\nstatic PriorityQueue\nMerge1(PriorityQueue H1,PriorityQueue H2){\n if(H1->Left == NULL){H1->Left = H2;}\n else{\n H1->Right = Merge(H1->Right,H2);\n if(H1->Left->Npl<H1->Right->Npl){\n swapChildren(H1);\n }\n H1->Npl = H1->Right->Npl+1;\n }\n return H1;\n}\n
\\(T_p=O(logN)\\)\u200b
Iterative Version
For SORTING \\(loglogN\\) \u2192 \\(logN * loglogN\\)
Only have to maintain two pointers.
Any \\(M\\) consecutive operations take at most \\(O(M log N)\\) time.
Always swap the left and right children except that the largest of all the nodes on the right paths does not have its children swapped. \\(No Npl.\\)
Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children.
It is an open problem to determine precisely the expected right path length of both leftist and skew heaps.
"},{"location":"ADS/Heap/lec/#amortized-analysis-for-skew-heaps","title":"Amortized Analysis for Skew Heaps","text":"\\(D_i =\\) the root of the resulting tree.
\\(\\Phi(D_i)=\\)\u200b\u200b\u200b number of heavy nodes.
\\(\\Phi(D_i) =\\)number of right nodes?
No! Think of only operation where num of right nodes will decrease: will not happen because the largest of all the nodes on the right paths does not have its children swapped
So this \\(\\Phi(D_i)\\) will always be increasing.
"},{"location":"ADS/Heap/lec/#def","title":"Def:","text":"A node \\(p\\) is heavy if the number of descendants of \\(p\\)\u2019s right subtree is at least half of the number of descendants of \\(p\\), and light otherwise.
Note that the number of descendants of a node includes the node itself.
\\(T_{worst}=\\)all right nodes \\(=l_1+h_1+l_2+h_2\\)
All heavy points at the right path will ALWAYS turn into light points.
Original light points at most can ALL turn into heavy nodes.
Remember: also need to insert into left subtrees.
h is heavy nodes on the left paths,will note change,cause its descandents will never exchange! .
Therefore Credits = \\(l_1+l_2-h_1-h_2\\)\u200b\u200b\u200b.
\\(T_{amotized}= T_{worst}+credits \\le2(l_1+l_2)\\)\u200b
\\(l_{right}=O(logN)\\)\u200b\u200b ? -- Actually if we want \\(l_{right}\\) to be large ,best almost balanced.
\u5047\u5982root\u7684\u5de6subtree \u5f88\u591a\uff0c\u628a\u591a\u51fa\u6765\u7684\u7ed9\u53f3\u8fb9\u6765\u589e\u52a0\u53f3\u94fe\u957f\uff0c\u8fd9\u6837\u5de6\u53f3node\u4e00\u6837\uff0c\u4e14\u53f3node\u7684heavy nodes\u81f3\u5c11\u4e0d\u4f1a\u5c11.
"},{"location":"ADS/Heap/lec/#amortized-analysis","title":"Amortized Analysis","text":"\\(\\therefore\\) A priority queue of any size can be uniquely represented by a collection of binomial trees.
Example : \\(13=2^0+2^2+2^3=1101_2\\)
"},{"location":"ADS/Heap/lec/#operations_1","title":"Operations","text":""},{"location":"ADS/Heap/lec/#findmin","title":"FindMin","text":"The minimum key is in one of the roots.
There are at most \\(\u300clogN\\) roots, hence \\(T_p = O( logN)\\)\u200b\u200b.
\u4e8c\u8fdb\u5236\u5206\u89e3\uff0c\u6240\u4ee5\\(logN\\)
Then this operation will take \\(O(1)\\)\u200b.
"},{"location":"ADS/Heap/lec/#merge_1","title":"Merge","text":"To combine two trees \\(O(1)\\)
At most \\(O(logN)\\) times
! Must keep the trees in the binomial queue sorted by height.
"},{"location":"ADS/Heap/lec/#insert","title":"Insert","text":""},{"location":"ADS/Heap/lec/#deletemin","title":"DeleteMin","text":""},{"location":"ADS/Heap/lec/#implementation","title":"Implementation","text":"typedef struct BinNode *Position;\ntypedef struct Collection *BinQueue;\ntypedef struct BinNode *BinTree; /* missing from p.176 */\n\nstruct BinNode \n{ \n ElementType Element;\n Position LeftChild;\n Position NextSibling;\n} ;\n\nstruct Collection \n{ \n int CurrentSize; /* total number of nodes */\n BinTree TheTrees[ MaxTrees ];\n} ;\n
BinTree CombineTrees( BinTree T1, BinTree T2 )\n{ /* merge equal-sized T1 and T2 */\n if ( T1->Element > T2->Element )\n /* attach the larger one to the smaller one */\n return CombineTrees( T2, T1 );\n /* insert T2 to the front of the children list of T1 */\n T2->NextSibling = T1->LeftChild;\n T1->LeftChild = T2;\n return T1;\n}\n
\\(T_p = O( 1 )\\)\u200b
BinQueue Merge( BinQueue H1, BinQueue H2 )\n{ BinTree T1, T2, Carry = NULL; \n int i, j;\n if ( H1->CurrentSize + H2-> CurrentSize > Capacity ) ErrorMessage();\n H1->CurrentSize += H2-> CurrentSize;\n for ( i=0, j=1; j<= H1->CurrentSize; i++, j*=2 ) {\n T1 = H1->TheTrees[i]; T2 = H2->TheTrees[i]; /*current trees */\n switch( 4*!!Carry + 2*!!T2 + !!T1 ) { \n case 0: /* 000 */ /* Carry T1 T2 */\n case 1: /* 001 */ break; \n case 2: /* 010 */ H1->TheTrees[i] = T2; H2->TheTrees[i] = NULL; break;\n case 4: /* 100 */ H1->TheTrees[i] = Carry; Carry = NULL; break;\n case 3: /* 011 */ Carry = CombineTrees( T1, T2 );\n H1->TheTrees[i] = H2->TheTrees[i] = NULL; break;\n case 5: /* 101 */ Carry = CombineTrees( T1, Carry );\n H1->TheTrees[i] = NULL; break;\n case 6: /* 110 */ Carry = CombineTrees( T2, Carry );\n H2->TheTrees[i] = NULL; break;\n case 7: /* 111 */ H1->TheTrees[i] = Carry; \n Carry = CombineTrees( T1, T2 ); \n H2->TheTrees[i] = NULL; break;\n } /* end switch */\n } /* end for-loop */\n return H1;\n}\n
BinQueue Merge( BinQueue H1, BinQueue H2 )\n{ BinTree T1, T2, Carry = NULL; \n int i, j;\n if ( H1->CurrentSize + H2-> CurrentSize > Capacity ) ErrorMessage();\n H1->CurrentSize += H2-> CurrentSize;\n for ( i=0, j=1; j<= H1->CurrentSize; i++, j*=2 ) {\n T1 = H1->TheTrees[i]; T2 = H2->TheTrees[i]; /*current trees */\n switch( 4*!!Carry + 2*!!T2 + !!T1 ) { \n case 0: /* 000 */\n case 1: /* 001 */ break; \n case 2: /* 010 */ H1->TheTrees[i] = T2; H2->TheTrees[i] = NULL; break;\n case 4: /* 100 */ H1->TheTrees[i] = Carry; Carry = NULL; break;\n case 3: /* 011 */ Carry = CombineTrees( T1, T2 );\n H1->TheTrees[i] = H2->TheTrees[i] = NULL; break;\n case 5: /* 101 */ Carry = CombineTrees( T1, Carry );\n H1->TheTrees[i] = NULL; break;\n case 6: /* 110 */ Carry = CombineTrees( T2, Carry );\n H2->TheTrees[i] = NULL; break;\n case 7: /* 111 */ H1->TheTrees[i] = Carry; \n Carry = CombineTrees( T1, T2 ); \n H2->TheTrees[i] = NULL; break;\n } /* end switch */\n } /* end for-loop */\n return H1;\n}\n
\u54e8\u5175
BinQueue Merge( BinQueue H1, BinQueue H2 ){\n\n}\n
ElementType DeleteMin( BinQueue H ){\n BinQueue DeletedQueue; \n Position DeletedTree, OldRoot;\n ElementType MinItem = Infinity; /* the minimum item to be returned */ \n int i, j, MinTree; /* MinTree is the index of the tree with the minimum item */\n if ( IsEmpty( H ) ) { PrintErrorMessage(); return \u2013Infinity; }\n for ( i = 0; i < MaxTrees; i++) { /* Step 1: find the minimum item */\n if( H->TheTrees[i] && H->TheTrees[i]->Element < MinItem ){ \n MinItem = H->TheTrees[i]->Element; MinTree = i; \n } /* end if */\n } /* end for-i-loop */\n\n DeletedTree = H->TheTrees[MinTree]; \n H->TheTrees[MinTree] = NULL; /* Step 2: remove the MinTree from H => H\u2019 */ \n OldRoot = DeletedTree; /* Step 3.1: remove the root */ \n DeletedTree = DeletedTree->LeftChild; free(OldRoot);\n DeletedQueue = Initialize(); /* Step 3.2: create H\u201d */ \n DeletedQueue->CurrentSize = (1<<MinTree ) \u2013 1; /* 2MinTree \u2013 1 */\n for ( j = MinTree \u2013 1; j >= 0; j \u2013 \u2013 ) { \n DeletedQueue->TheTrees[j] = DeletedTree;\n DeletedTree = DeletedTree->NextSibling;\n DeletedQueue->TheTrees[j]->NextSibling = NULL;\n } /* end for-j-loop */\n\n H->CurrentSize \u2013 = DeletedQueue->CurrentSize + 1;\n H = Merge( H, DeletedQueue ); /* Step 4: merge H\u2019 and H\u201d */ \n return MinItem;\n}\n
In aggregate analysis, we show that for all n, a sequence of n operations takes worst-case time \\(T(n)\\) in total. In the worst case, the average cost, or amortized cost, per operation is therefore \\(T(n)/n\\). Note that this amortized cost applies to each operation, even when there are several types of operations in the sequence.
Stack operations
Let us analyze a sequence of n \\(PUSH, POP\\), and \\(MULTIPOP\\)\u200b operations on an initially empty stack
The worst-case time of any stack operation is therefore \\(O(n)\\), and hence a sequence of n operations costs \\(O(n^2)\\)
We can pop each object from the stack at most once for each time we have pushed it onto the stack.
Therefore, the number of times that \\(POP\\) can be called on a nonempty stack, including calls within \\(MULTIPOP\\), is at most the number of PUSH operations, which is at most n.
For any value of n, any sequence of n \\(PUSH, POP\\), and \\(MULTIPOP\\) operations takes a total of \\(O(n)\\) time. The average cost of an operation is \\(O(n)/n = O(1)\\)\u200b
We actually showed a worst-case bound of \\(O(n)\\) on a sequence of \\(n\\) operations. Dividing this total cost by \\(n\\)\u200b yielded the average cost per operation, or the amortized cost.
Incrementing a binary counter
Consider the problem of implementing a k-bit binary counter that counts upward from 0.
We use an array \\(A[0..k-1]\\) of bits, where \\(A.length=k\\), as the counter.
A binary number x that is stored in the counter has its lowest-order bit in \\(A[0]\\) and its highest-order bit in \\(A[k-1]\\)so that \\(x = \\sum_{i=0}^{k-1}A[i]\u00b72^i\\) Initially, \\(x=0\\) and thus \\(A[i]=0\\) for \\(i=0,1,... k-1\\)
To add \\(1\\)(modulo \\(2^k\\)\u200b) to the value in the counter, we use the following procedure.
Thus, a sequence of n INCREMENT operations on an initially zero counter takes time \\(O(nk)\\) in the worst case.
Similarly, bit \\(A[2]\\) flibs only every fourth time or \\(n/4\u300d\\) times in a sequence.
\\(\\sum_{i=0}^{k-1}\\frac{n}{2^i}\u300d<n\\sum_{i=0}^{\\infty}\\frac{1}{2^i}=2n\\)
1. If the set of stack operations included a MULTIPUSH operation, which pushes \\(k\\) items onto the stack, would the \\(O(1)\\)\u200b bound on the amortized cost of stack operations continue to hold?
Since one MULTIPUSH needs \\(\\Theta(k)\\) time, performing n MULTIPUSH operations, each with k elements, would take\\(\u0398(kn)\\) time, leading to amortized cost of \\(\\Theta(k)\\)\u200b.
Show that if a DECREMENT operation were included in the k-bit counter example, n operations could cost as much as \\(\\Theta(nk)\\)\u200b time.
2. Suppose we perform a sequence of \\(n\\) operations on a data structure in which the i th operation costs i if i is an exact power of 2, and 1 otherwise.
Use aggregate analysis to determine the amortized cost per operation.
"},{"location":"ADS/Heap/lec/#the-accounting-method","title":"The accounting method","text":"In the accounting method of amortized analysis, we assign differing charges to different operations, with some operations charged more or less than they actually cost. We call the amount we charge an operation its amortized cost. When an operation\u2019s amortized cost exceeds its actual cost, we assign the difference to specific objects in the data structure as credit.
Credit can help pay for later operations whose amortized cost is less than their actual cost.
If we want to show that in the worst case the average cost per operation is small by analyzing with amortized costs, we must ensure that the total amortized cost of a sequence of operations provides an upper bound on the total actual cost of the sequence.
this relationship must hold for all sequences of operations. Which means \\(\\sum \\hat{c}_i\\ge\\sum c_i\\) for all sequences of n operations.
Instead of representing prepaid work as credit stored with specific objects in the data structure, the potential method of amortized analysis represents the prepaid work as \u201cpotential energy,\u201d or just \u201cpotential,\u201d which can be released to pay for future operations. We associate the potential with the data structure as a whole rather than with specific objects within the data structure.
We will perform n operations, starting with an initial data structure \\(D_0\\)
For each \\(i=1,2,...n\\), we let \\(c_i\\) be the actual cost of the ith operation and \\(D_i\\) be the data structure that results after applying the ith operation to data structure \\(D_{i-1}\\).
A potential function \\(\\Phi\\) maps each data structure \\(D_i\\) to a real number \\(\\Phi(D_i)\\), which is the potential associated with data structure \\(D_i\\) .
The amortized cost \\(\\hat{c}_i\\) of the ith operation with respect to potential function \\(\\Phi\\) is defined by \\(\\hat{c}_i=c_i+\\Phi(D_i)-\\Phi(D_{i-1})\\)
the total amortized cost of the n operations is \\(\\sum\\hat{c}_i=\\sum c_i+\\Phi(D_n)-\\Phi(D_0)\\)
Define a potential function so that \\(\\Phi(D_i)\\ge \\Phi(D_0)\\) for all i!
Stack operations * See Previous Part.
Incrementing a binary counter we define the potential of the counter after the ith INCREMENT operation to be \\(b_i\\) , the number of 1s in the counter after the i th operation. * If the counter starts at zero, then \\(\\Phi(D_0)=0\\) Since \\(\\Phi(D_i)\\ge 0\\) for all i , the total amortized cost of a sequence of n INCREMENT operations is an upper bound on the total actual cost, and so the worst-case cost of n INCREMENT operations is \\(O(n)\\).
"},{"location":"ADS/NP/lec/#np-completeness","title":"NP - Completeness","text":""},{"location":"ADS/NP/lec/#turing-machind","title":"Turing Machind","text":"
NP: Nondeterministic polynomial-time * The problem is NP if we can prove any solution is true in polynomial time.
Example Hamilton cycle problem: Find a single cycle that contains every vertex \u2013 does this simple circuit include all the vertices?
Note: Not all decidable problems are in NP. For example, consider the problem of determining whether a graph does not have a Hamiltonian cycle.
There is a polynomial time algorithm that can solve the problem. \\(P \\subseteq NP\\)
The most difficult problems in NP.
EXAMPLE Suppose that we already know that the Hamiltonian cycle problem is NP-complete
. Prove that the traveling salesman problem is NP-complete as well. * Hamiltonian cycle problem: Given a graph \\(G=(V, E)\\), is there a simple cycle that visits all vertices? * Traveling salesman problem: Given a complete graph \\(G=(V, E)\\), with edge costs, and an integer K, is there a simple cycle that visits all vertices and has total cost \\(\\le K\\)?
The first problem that was proven to be NP-complete was the Satisfiability problem (Circuit-SAT): Input a boolean expression and ask if it has an assignment to the variables that gives the expression a value of 1. Cook showed in 1971 that all the problems in NP could be polynomially transformed to Satisfiability. He proved it by solving this problem on a nondeterministic Turing machine in polynomial time.
"},{"location":"ADS/NP/lec/#co-np","title":"co-NP","text":""},{"location":"ADS/NP/lec/#abstract-problem","title":"Abstract Problem","text":"An abstract problem \\(Q\\) is a binary relation on a set \\(I\\) of problem instances and a set \\(S\\) of problem solutions.
"},{"location":"ADS/NP/lec/#formal-language-theory-for-decision-problem","title":"Formal-language Theory \u2014 for decision problem","text":"x\u662f\u89e3\u7684\u5b9e\u4f8b
x
and the other is a binary string y
called a certificate. x
if there exists a certificate y
such that A(x, y) = 1
. L = { x \u2208 {0, 1}*
: there exists y \u2208 {0, 1}*
such that A(x, y) = 1}
. co-NP : \u8be5\u95ee\u9898\u548c\u5b83\u7684\u8865\u90fd\u5c5e\u4e8eNP
Some probles can only reach requirement2 but satisfy 1 -- NP-Hard
0-1 backpack problem
https://zhuanlan.zhihu.com/p/93857890
"},{"location":"ADS/Tree/lec/#avl-tree","title":"AVL Tree","text":"
An empty binary tree is height balanced.
If T is nonempty binary tree with \\(T_L\\) and \\(T_R\\) as its left and right subtrees,then \\(T\\) is height balanced iff
(1)\\(T_L\\) and \\(T_R\\) are height balanced
(2)\\(|h_L-h_R|\\le 1\\) where \\(h_l\\) and \\(h_R\\) are the heights of \\(T_L\\) and \\(T_R\\)\u200b\u200b
In an AVL Tree
, \\(BF(node)\\) = \\(-1,0,or\\ 1\\)
RR Rotation
LL Rotation
RL Rotation
LR Rotation
Details refer to slides.
\u52d8\u8bef \uff1a \\(n_h \\approx \\frac{1}{\\sqrt{5}}(\\frac{1+\\sqrt{5}}{2})^{h+3} - 1\\)\u200b
"},{"location":"ADS/Tree/lec/#splay-trees","title":"Splay Trees","text":"Target : Any \\(M\\) consecutive tree operations starting from an empty tree take at most \\(O(M log N)\\) time. * For any nonroot node \\(X\\) , denote its parent by \\(P\\) and grandparent by \\(G\\)
worst case bound > amortized bound > average case bound
"},{"location":"ADS/Tree/lec/#aggregate-analysis","title":"Aggregate analysis","text":"Show that for all n, a sequence of n operations takes worst-case time \\(T(n)\\) in total.
In the worst case, the average cost, or amortized cost, per operation is therefore \\(T(n)/n\\).
"},{"location":"ADS/Tree/lec/#accounting-method","title":"Accounting method","text":"When an operation\u2019s amortized cost \\(\\hat{c}_i\\) exceeds its actual cost \\(c_i\\) , we assign the difference to specific objects in the data structure as credit. Credit can help pay for later operations whose amortized cost is less than their actual cost.
"},{"location":"ADS/Tree/lec/#potential-method","title":"Potential Method","text":"Black-Height
The number of black nodes from the node to its descandent leaves (without counting NIL & itself)
"},{"location":"ADS/Tree/lec/#lemma","title":"Lemma","text":"A red-black tree with N internal nodes has height at most \\(2ln(N +1)\\)\u200b.
"},{"location":"ADS/Tree/lec/#operations","title":"Operations","text":""},{"location":"ADS/Tree/lec/#insert","title":"Insert","text":"Sketch : Insert & Colour red ; Pass Error to Root
Refer to \u7b97\u6cd5\u5bfc\u8bba !
Loop ends when z.p is black
Color the root black after the loop
Case 3 : To have a red far nephew.
Case 4 : (1) change colour of ? and w (2) far nephew change black (3) make brother new root
Beacuse property 5 holds so descants of w have no black node
A B+ tree of order M is a tree with the following structural properties:
Root can have less children like when constructing.
Btree Insert ( ElementType X, Btree T ) \n{ \n Search from root to leaf for X and find the proper leaf node;\n Insert X;\n while ( this node has M+1 keys ) {\n split it into 2 nodes with \u300c(M+1)/2 and \u300c(M+1)/2 keys, respectively;\n if (this node is the root)\n create a new root with two children;\n check its parent;\n // Every iteration O(M)\n }\n} \n
\\(Depth = O(log_{M/2}N)\\)\u200b
\\(T_{insert} = O(M* Depth)=O(logN*M/logM)\\)\u200b
\\(Y_{find} = O(logN)\\)\u200b
"},{"location":"ADS/approx/lec/","title":"Approximation","text":""},{"location":"ADS/approx/lec/#approximation","title":"Approximation","text":"
If an algorithm achieves an approximation ratio of \\(\\rho(n)\\), we call it a\\(\\rho(n)\\)-approximation algorithm.
Approximate Bin Packing Given N items of sizes \\(S_1 , S_2 , \u2026, S_N\\) , such that \\(0 < S_i \\le 1\\) for all \\(1 \\le i \\le N\\) . Pack these items in the fewest number of bins, each of which has unit capacity. Example : N = 7; \\(S_i\\) = 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8
void NextFit ( )\n{ read item1;\n while ( read item2 ) {\n if ( item2 can be packed in the same bin as item1 )\n place item2 in the bin;\n else\n create a new bin for item2;\n item1 = item2;\n } /* end-while */\n}\n
* Therefore the NextFit algorithm is a 2-approximation algorithm for the bin packing problem.[approximation ratio = 2] void FirstFit ( )\n{ while ( read item ) {\n scan for the first bin that is large enough for item;\n if ( found )\n place item in that bin;\n else\n create a new bin for item;\n } /* end-while */\n}\n
* Time complexity of FirstFit is \\(O(nlogn)\\) * But the approximation ratio of FirstFit is 2. On-line Algorithms Place an item before processing the next one, and can NOT change decision.
Off-line Algorithms View the entire item list before producing an answer.
"},{"location":"ADS/approx/lec/#the-knapsack-problem-fractional-version","title":"The Knapsack Problem \u2014 fractional version","text":"A knapsack with a capacity \\(M\\) is to be packed. Given \\(N\\) items. Each item \\(i\\) has a weight \\(w_i\\) and a profit \\(p_i\\) . If \\(x_i\\) is the percentage of the item \\(i\\) being packed, then the packed profit will be \\(p_ix_i\\) . * An optimal packing is a feasible one with maximum profit. That is, we are supposed to find the values of \\(x_i\\) such that \\(\\sum\\limits_{i=1}^n p_ix_i\\) obtains its maximum under the constrains: $\\sum\\limits_{i=1}^{n}w_ix_i \\le M $ and \\(x\\in [0,1] \\ for\\ 1\\le i \\le n\\)
"},{"location":"ADS/approx/lec/#the-knapsack-problem-0-1-version","title":"The Knapsack Problem \u2014 0-1 version","text":"NPC and NP-hard
\\(p_{opt}\\) is the optimal solution for the frac version
Refer to Slides.
"},{"location":"ADS/back/lec/","title":"backtracking","text":""},{"location":"ADS/back/lec/#backtracking","title":"Backtracking","text":"
The basic idea is that suppose we have a partial solution \\(( x_1, ... , x_i )\\) where each \\(x_k \\in S_k\\) for \\(1 \\le k \\le i < n\\). First we add \\(x_{i+1} \\in S_{i+1}\\) and check if \\(( x_1, ... , x_i, x_{i+1} )\\) satisfies the constrains. If the answer is \\(\u201cyes\u201d\\) we continue to add the next \\(x\\), else we delete \\(x_i\\) and backtrack to the previous partial solution \\(( x_1, ... , x_{i-1} )\\)\u200b.
"},{"location":"ADS/back/lec/#example","title":"Example","text":""},{"location":"ADS/back/lec/#eight-queens","title":"Eight Queens","text":""},{"location":"ADS/back/lec/#the-turnpike-reconstruction-problem","title":"The Turnpike Reconstruction Problem","text":"Given \\(N\\) points on the x-axis with coordinates \\(x_1 < x_2 < \u2026< x_N\\) . Assume that \\(x_1 = 0\\). There are \\(N ( N \u2013 1 ) / 2\\) distances between every pair of points.
Given \\(N ( N \u2013 1 ) / 2\\) distances. Reconstruct a point set from the distances.
Example
Complexity : \\(2^n\\)
bool Reconstruct ( DistType X[ ], DistSet D, int N, int left, int right )\n{ /* X[1]...X[left-1] and X[right+1]...X[N] are solved */\n bool Found = false;\n if ( Is_Empty( D ) )\n return true; /* solved */\n D_max = Find_Max( D );\n /* option 1\uff1aX[right] = D_max */\n /* check if |D_max-X[i]|\uf0ceD is true for all X[i]\u2019s that have been solved */\n OK = Check( D_max, N, left, right ); /* pruning */\n if ( OK ) { /* add X[right] and update D */\n X[right] = D_max;\n for ( i=1; i<left; i++ ) Delete( |X[right]-X[i]|, D);\n for ( i=right+1; i<=N; i++ ) Delete( |X[right]-X[i]|, D);\n Found = Reconstruct ( X, D, N, left, right-1 );\n if ( !Found ) { /* if does not work, undo */\n for ( i=1; i<left; i++ ) Insert( |X[right]-X[i]|, D);\n for ( i=right+1; i<=N; i++ ) Insert( |X[right]-X[i]|, D);\n }\n }\n /* finish checking option 1 */\n if ( !Found ) { /* if option 1 does not work */\n /* option 2: X[left] = X[N]-D_max */\n OK = Check( X[N]-D_max, N, left, right );\n if ( OK ) {\n X[left] = X[N] \u2013 D_max;\n for ( i=1; i<left; i++ ) Delete( |X[left]-X[i]|, D);\n for ( i=right+1; i<=N; i++ ) Delete( |X[left]-X[i]|, D);\n Found = Reconstruct (X, D, N, left+1, right );\n if ( !Found ) {\n for ( i=1; i<left; i++ ) Insert( |X[left]-X[i]|, D);\n for ( i=right+1; i<=N; i++ ) Insert( |X[left]-X[i]|,D);\n }\n }\n /* finish checking option 2 */\n } /* finish checking all the options */\n\n return Found;\n}\n
bool Backtracking ( int i )\n{ Found = false;\n if ( i > N )\n return true; /* solved with (x1, \u2026, xN) */\n for ( each xi in Si ) { \n /* check if satisfies the restriction R */\n OK = Check((x1, \u2026, xi) , R ); /* pruning */\n if ( OK ) {\n Count xi in;\n Found = Backtracking( i+1 );\n if ( !Found )\n Undo( i ); /* recover to (x1, \u2026, xi-1) */\n }\n if ( Found ) break; \n }\n return Found;\n}\n
\u6ce8\uff1a\u56de\u6eaf\u7684\u6548\u7387\u8ddfS\u7684\u89c4\u6a21\u3001\u7ea6\u675f\u51fd\u6570\u7684\u590d\u6742\u6027\u3001\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u7ed3\u70b9\u6570\u76f8\u5173\u3002\u7ea6\u675f\u51fd\u6570\u51b3\u5b9a\u4e86\u526a\u679d\u7684\u6548\u7387\uff0c\u4f46\u662f\u5982\u679c\u51fd\u6570\u672c\u8eab\u592a\u590d\u6742\u4e5f\u672a\u5fc5\u5408\u7b97\u3002\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u7ed3\u70b9\u6570\u6700\u96be\u4f30\u8ba1\uff0c\u4f7f\u5f97\u590d\u6742\u5ea6\u5206\u6790\u5f88\u96be\u5b8c\u6210\u3002
"},{"location":"ADS/back/lec/#different-construction-methods","title":"Different Construction Methods","text":"In practice , we use the first tree more.
"},{"location":"ADS/back/lec/#stick-problem","title":"Stick Problem","text":"George had a number of small sticks of the same length,whcih he cut into random sections. He wanted to know if he could reconstruct the original sticks from the sections.Given the lengths of the sections,help him to find the minimum possible length of the original sticks.
Example Sample Input
9\n5 2 1 5 2 1 5 2 1\n
Sample Output 6\n
Idea: 1.Enumerate the length of the original sticks. 2.Check by enumerate a permutation by backtracking. Everytime devcide a stick section should belong to which original stick.
Example:Tic-tac-toe
"},{"location":"ADS/dy/lec/#dynamic-programming","title":"Dynamic Programming","text":""},{"location":"ADS/dy/lec/#example","title":"Example:","text":""},{"location":"ADS/dy/lec/#1-fibonacci-numbers","title":"1. Fibonacci numbers","text":"
int fib(int n) {\n if (n <= 1) return n;\n return fib(n-1) + fib(n-2);\n}\n
If we use dynamic programming, we can reduce the time complexity to \\(O(n)\\).
int fib(int n) {\n int f[n+1];\n f[0] = 0;\n f[1] = 1;\n for (int i = 2; i <= n; i++) {\n f[i] = f[i-1] + f[i-2];\n }\n return f[n];\n}\n
"},{"location":"ADS/dy/lec/#2-ordering-matrix-multiplications","title":"2. Ordering Matrix Multiplications","text":"* In which order can we compute the product of n matrices with minimal computing time?
Total time complexity: \\(O(n^3)\\) -- compute all m[i][j]
void OptMatrix( const long r[ ], int N, TwoDimArray M ) \n{ int i, j, k, L; \n long ThisM; \n for( i = 1; i <= N; i++ ) M[ i ][ i ] = 0; \n for( k = 1; k < N; k++ ) /* k = j - i */ \n for( i = 1; i <= N - k; i++ ) { /* For each position */ \n j = i + k; M[ i ][ j ] = Infinity; \n for( L = i; L < j; L++ ) { \n ThisM = M[ i ][ L ] + M[ L + 1 ][ j ] \n + r[ i - 1 ] * r[ L ] * r[ j ]; \n if ( ThisM < M[ i ][ j ] ) /* Update min */ \n M[ i ][ j ] = ThisM; \n } /* end for-L */\n } /* end for-Left */\n}\n
\\(F[N][i] = min_{k}(F[k-i][i]+F[N-k-i][k+1]+r_{k-i-1} \\times r_i \\times r_{N-i})\\) ?
Given N words \\(w_1, w_2, \\cdots, w_N\\) and their search probabilities \\(p_1, p_2, \\cdots, p_N\\) . We want to arrange these words in a binary search tree in a way that minimize the expected total access time : \\(\\sum_{i=1}^{N} (d_i+1) \\times p_i\\) , where \\(d_i\\) is the depth of the node containing \\(w_i\\) in the binary search tree.
Method 1: Use single source shortest path algorithm \\(N\\) times.
Method 2: Floyd-Warshall Algorithm Define \\(D^k[i][j]\\)= \\(min\\{ length\\ of\\ path\\ i \\to {l \\le k }\\to j\\}\\) and \\(D^{-1}[ i ] [ j ] = Cost [ i ] [ j ]\\).
Then the length of the shortest path from i to j is \\(D^{N-1}[ i ] [ j ]\\).
/* A[ ] contains the adjacency matrix with A[ i ][ i ] = 0 */ \n/* D[ ] contains the values of the shortest path */ \n/* N is the number of vertices */ \n/* A negative cycle exists iff D[ i ][ i ] < 0 */ \nvoid AllPairs( TwoDimArray A, TwoDimArray D, int N ) \n{ int i, j, k; \n for ( i = 0; i < N; i++ ) /* Initialize D */ \n for( j = 0; j < N; j++ )\n D[ i ][ j ] = A[ i ][ j ]; \n for( k = 0; k < N; k++ ) /* add one vertex k into the path */\n for( i = 0; i < N; i++ ) \n for( j = 0; j < N; j++ ) \n if( D[ i ][ k ] + D[ k ][ j ] < D[ i ][ j ] ) \n /* Update shortest path */ \n D[ i ][ j ] = D[ i ][ k ] + D[ k ][ j ]; \n}\n
\\(T(N) = O(N^3)\\), but faster in a dense graph.
How to design a DP method? 1. Characterize the structure of an optimal solution. 2. Recursively define the value of an optimal solution. 3. Compute the value of an optimal solution in some order. 4. Reconstruct an optimal solution from computed information.
"},{"location":"ADS/dy/lec/#5-product-assembly","title":"5. Product Assembly","text":""},{"location":"ADS/greedy/lec/#greedy-algorithm","title":"Greedy Algorithm","text":""},{"location":"ADS/greedy/lec/#activities-arrangements","title":"Activities Arrangements","text":"
Given a set of activities \\(S = \\{ a_1, a_2, \u2026, a_n \\}\\) that wish to use a resource (e.g. a classroom).
Each \\(a_i\\) takes place during a time interval \\([s_i, f_i)\\).Activities ai and aj are compatible if \\(s_i \\ge f_j\\) or \\(s_j \\ge f_i\\)\u200b\u200b (i.e. their time intervals do not overlap).
"},{"location":"ADS/greedy/lec/#dynamic-programming","title":"Dynamic Programming","text":"c[i][k] : i is start activity j is end activity and i j not included.
//\u8bb0\u5fc6\u4fdd\u5b58\u641c\u7d22\u7ed3\u679c\nif S[i][j] == NULL : return 0;\nfor k\n if (c[i][k]==-1) c[i][k] = dfs(i,k)\n
Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve.
Prove that there is always an optimal solution to the original problem that makes the greedy choice, so that the greedy choice is always safe.
First choice can always be included !
Greedy algorithm works only if the local optimum is equal to the global optimum.
"},{"location":"ADS/greedy/lec/#change","title":"Change","text":""},{"location":"ADS/greedy/lec/#huffman-codes","title":"Huffman Codes","text":"The trick is:No code is a prefix of another!!
Or it will cause ambiguity when decoding!
"},{"location":"ADS/greedy/lec/#huffmans-algorithm","title":"Huffman's algorithm","text":"void Huffman ( PriorityQueue heap[ ], int C )\n{ consider the C characters as C single node binary trees,\n and initialize them into a min heap;\n for ( i = 1; i < C; i++ ) { \n create a new node;\n /* be greedy here */\n delete root from min heap and attach it to left_child of node;\n delete root from min heap and attach it to right_child of node;\n weight of node = sum of weights of its children;\n /* weight of a tree = sum of the frequencies of its leaves */\n insert node into min heap;\n }\n}\n/*T = O(ClogC)*/\n
"},{"location":"ADS/rand/lec/#randomized-algorithms","title":"Randomized Algorithms","text":""},{"location":"ADS/rand/lec/#example-the-hiring-problem","title":"[Example] The Hiring Problem","text":"
Assume M people are hired.Total Cost: \\(C = O(NC_i + MC_h)\\)
"},{"location":"ADS/rand/lec/#naive-solution","title":"Na\u00efve Solution","text":"int Hiring ( EventType C[ ], int N )\n{ /* candidate 0 is a least-qualified dummy candidate */\n int Best = 0;\n int BestQ = the quality of candidate 0;\n for ( i=1; i<=N; i++ ) {\n Qi = interview( i ); /* Ci */\n if ( Qi > BestQ ) {\n BestQ = Qi;\n Best = i;\n hire( i ); /* Ch */\n }\n }\n return Best;\n}\n
Worst Case: \\(O(N)\\) interviews, \\(O(N)\\) hires, \\(O(N)\\) comparisons Assume candidates are randomly ordered. * X = number of hires. E[X] = \\(\\sum\\limits_{i=1}^{N} i\u00b7Pr[X=i]\\) * $ X_i = \\begin{array}{ll} 1 if i is hired \\ 0 otherwise \\end{array}$ * \\(Pr[X_i = 1] = \\frac{1}{i}\\) * E[X] = Expected number of hires = \\(E[\\sum\\limits_{i=1}^{N} X_i] = \\sum\\limits_{i=1}^{N} E[X_i] = \\sum\\limits_{i=1}^{N} \\frac{1}{i} = O(\\log N) +O(1)\\) * \\(O(C_h \\log N + NC_i)\\)
"},{"location":"ADS/rand/lec/#randomized-solution","title":"Randomized Solution","text":"int RandomizedHiring ( EventType C[ ], int N )\n{ /* candidate 0 is a least-qualified dummy candidate */\n int Best = 0;\n int BestQ = the quality of candidate 0;\n\n randomly permute the list of candidates;\n\n for ( i=1; i<=N; i++ ) {\n Qi = interview( i ); /* Ci */\n if ( Qi > BestQ ) {\n BestQ = Qi;\n Best = i;\n hire( i ); /* Ch */\n }\n }\n}\n
"},{"location":"ADS/rand/lec/#radomized-permutation-algorithm","title":"Radomized Permutation Algorithm","text":"Assign each element A[ i ] a random priority P[ i ],and sort
void PermuteBySorting ( ElemType A[ ], int N )\n{\n for ( i=1; i<=N; i++ )\n A[i].P = 1 + rand()%(N3); \n /* makes it more likely that all priorities are unique */\n Sort A, using P as the sort keys;\n}\n
int OnlineHiring ( EventType C[ ], int N, int k )\n{\n int Best = N;\n int BestQ = - infinity ;\n for ( i=1; i<=k; i++ ) {\n Qi = interview( i );\n if ( Qi > BestQ ) BestQ = Qi;\n }\n for ( i=k+1; i<=N; i++ ) {\n Qi = interview( i );\n if ( Qi > BestQ ) {\n Best = i;\n break;\n }\n }\n return Best;\n}\n
"},{"location":"ADS/rand/lec/#example-quicksort","title":"[Example] Quicksort","text":"https://leovan.me/cn/2018/08/cnn/
"},{"location":"AI/net/#recurrent-neural-network-rnn","title":"Recurrent Neural Network \uff08RNN\uff09","text":"\\(O_t=g(V\\cdot S_t)\\)
\\(S_t=\\Phi(U\\cdot X_t+W\\cdot S_{t-1})\\) \\(\\Phi\\)\uff08\u6fc0\u6d3b\u51fd\u6570\uff09
"},{"location":"AI/net/#back-propagation","title":"Back propagation","text":""},{"location":"AI/net/#intro","title":"Intro","text":"\\(Cost=H(W_{11},W_{12},\u2026\u2026,W_{mn})\\)
\\(\\triangledown H=\\frac{\\partial H}{\\partial W_{11}}e_{11}+\\frac{\\partial H}{\\partial W_{12}}e_{12}+\u2026\u2026+\\frac{\\partial H}{\\partial W_{mn}}e_{mn}\\)
https://zhuanlan.zhihu.com/p/115571464
https://towardsdatascience.com/understanding-backpropagation-algorithm-7bb3aa2f95fd
\u53cd\u5411\u4f20\u64ad\uff08Backpropagation\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u7684\u4f18\u5316\u7b97\u6cd5\uff0c\u901a\u8fc7\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u5173\u4e8e\u7f51\u7edc\u53c2\u6570\u7684\u68af\u5ea6\uff0c\u5e76\u5229\u7528\u8fd9\u4e9b\u68af\u5ea6\u6765\u66f4\u65b0\u53c2\u6570\uff0c\u4ee5\u6700\u5c0f\u5316\u635f\u5931\u51fd\u6570\u3002
\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u5bf9\u7f51\u7edc\u8f93\u51fa\u7684\u68af\u5ea6\uff08\u5373\u635f\u5931\u51fd\u6570\u5173\u4e8e\u8f93\u51fa\u7684\u5bfc\u6570\uff09\u3002
\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u901a\u8fc7\u53cd\u5411\u4f20\u64ad\u548c\u68af\u5ea6\u4e0b\u964d\u7b97\u6cd5\u7684\u7ed3\u5408\u6765\u5b9e\u73b0\u7684\uff0c\u4ee5\u6700\u5c0f\u5316\u635f\u5931\u51fd\u6570\u5e76\u4f18\u5316\u795e\u7ecf\u7f51\u7edc\u7684\u53c2\u6570\u3002
"},{"location":"AI/net/#_2","title":"\u53c2\u6570\u66f4\u65b0","text":"\\(\u03b8_{new}=\u03b8_{old}\u2212\u03b1\u2207L(\u03b8_{old})\\)
\u5176\u4e2d\uff1a
\u8fd9\u4e2a\u66f4\u65b0\u89c4\u5219\u7684\u76f4\u89c2\u89e3\u91ca\u662f\uff0c\u6211\u4eec\u6cbf\u7740\u635f\u5931\u51fd\u6570\u4e0b\u964d\u6700\u5feb\u7684\u65b9\u5411\u66f4\u65b0\u53c2\u6570\u3002\u68af\u5ea6\u544a\u8bc9\u6211\u4eec\u635f\u5931\u51fd\u6570\u5728\u5f53\u524d\u70b9\u4e0a\u5347\u6700\u5feb\u7684\u65b9\u5411\uff0c\u6211\u4eec\u6cbf\u7740\u68af\u5ea6\u7684\u53cd\u65b9\u5411\u524d\u8fdb\uff0c\u4ee5\u964d\u4f4e\u635f\u5931\u3002
\u5b66\u4e60\u7387\u7684\u9009\u62e9\u662f\u5173\u952e\u7684\u3002\u5982\u679c\u5b66\u4e60\u7387\u592a\u5c0f\uff0c\u6536\u655b\u901f\u5ea6\u4f1a\u5f88\u6162\uff0c\u800c\u5982\u679c\u5b66\u4e60\u7387\u592a\u5927\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u8df3\u8fc7\u635f\u5931\u51fd\u6570\u7684\u6700\u5c0f\u503c\u3002\u56e0\u6b64\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u5b66\u4e60\u7387\u5bf9\u4e8e\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u81f3\u5173\u91cd\u8981\u3002
\u901a\u5e38\uff0c\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u5b66\u4e60\u7387\u53ef\u80fd\u4f1a\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\u800c\u53d8\u5316\uff0c\u8fd9\u88ab\u79f0\u4e3a\u5b66\u4e60\u7387\u8c03\u5ea6\uff08learning rate scheduling\uff09\u3002\u4f8b\u5982\uff0c\u521d\u59cb\u65f6\u53ef\u4ee5\u4f7f\u7528\u8f83\u5927\u7684\u5b66\u4e60\u7387\u4ee5\u5feb\u901f\u6536\u655b\uff0c\u7136\u540e\u968f\u7740\u8bad\u7ec3\u7684\u8fdb\u884c\u9010\u6e10\u51cf\u5c0f\u5b66\u4e60\u7387\uff0c\u4ee5\u63d0\u9ad8\u6536\u655b\u7684\u7cbe\u5ea6\u3002
\u603b\u7684\u6765\u8bf4\uff0c\u5b66\u4e60\u7387\u662f\u4e00\u4e2a\u5e73\u8861\u8bad\u7ec3\u901f\u5ea6\u548c\u6027\u80fd\u7684\u5173\u952e\u56e0\u7d20\u3002\u4e0d\u540c\u7684\u95ee\u9898\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u5b66\u4e60\u7387\uff0c\u56e0\u6b64\u5b83\u901a\u5e38\u9700\u8981\u901a\u8fc7\u5b9e\u9a8c\u6765\u8c03\u6574\u3002
"},{"location":"AI/net/#back-propagation-in-cnn","title":"Back propagation in CNN","text":"https://mmuratarat.github.io/2019-02-07/bptt-of-rnn
"},{"location":"AI/net/#long-short-term-memory","title":"Long Short-Term Memory","text":"https://easyai.tech/ai-definition/rnn/
https://blog.csdn.net/u012328159/article/details/87567358
"},{"location":"AI/net/#attention","title":"Attention","text":"https://www.cnblogs.com/gczr/p/14693829.html
https://zhuanlan.zhihu.com/p/379722366
"},{"location":"AI/pca/","title":"\u6570\u636e\u7684\u5411\u91cf\u8868\u793a\u53ca\u964d\u7ef4\u95ee\u9898","text":"\u6ce8\u660e\uff1a\u6765\u6e90
\u4e0b\u9762\u6211\u4eec\u7ee7\u7eed\u5728\u4e8c\u7ef4\u7a7a\u95f4\u5185\u8ba8\u8bba\u5411\u91cf\u3002\u4e0a\u6587\u8bf4\u8fc7\uff0c\u4e00\u4e2a\u4e8c\u7ef4\u5411\u91cf\u53ef\u4ee5\u5bf9\u5e94\u4e8c\u7ef4\u7b1b\u5361\u5c14\u76f4\u89d2\u5750\u6807\u7cfb\u4e2d\u4ece\u539f\u70b9\u51fa\u53d1\u7684\u4e00\u4e2a\u6709\u5411\u7ebf\u6bb5\u3002\u4f8b\u5982\u4e0b\u9762\u8fd9\u4e2a\u5411\u91cf\uff1a
\u5728\u4ee3\u6570\u8868\u793a\u65b9\u9762\uff0c\u6211\u4eec\u7ecf\u5e38\u7528\u7ebf\u6bb5\u7ec8\u70b9\u7684\u70b9\u5750\u6807\u8868\u793a\u5411\u91cf\uff0c\u4f8b\u5982\u4e0a\u9762\u7684\u5411\u91cf\u53ef\u4ee5\u8868\u793a\u4e3a(3,2)\uff0c\u8fd9\u662f\u6211\u4eec\u518d\u719f\u6089\u4e0d\u8fc7\u7684\u5411\u91cf\u8868\u793a\u3002
\u4e0d\u8fc7\u6211\u4eec\u5e38\u5e38\u5ffd\u7565\uff0c\u53ea\u6709\u4e00\u4e2a(3,2)\u672c\u8eab\u662f\u4e0d\u80fd\u591f\u7cbe\u786e\u8868\u793a\u4e00\u4e2a\u5411\u91cf\u7684\u3002\u6211\u4eec\u4ed4\u7ec6\u770b\u4e00\u4e0b\uff0c\u8fd9\u91cc\u76843\u5b9e\u9645\u8868\u793a\u7684\u662f\u5411\u91cf\u5728x\u8f74\u4e0a\u7684\u6295\u5f71\u503c\u662f3\uff0c\u5728y\u8f74\u4e0a\u7684\u6295\u5f71\u503c\u662f2\u3002\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u5176\u5b9e\u9690\u5f0f\u5f15\u5165\u4e86\u4e00\u4e2a\u5b9a\u4e49\uff1a\u4ee5x\u8f74\u548cy\u8f74\u4e0a\u6b63\u65b9\u5411\u957f\u5ea6\u4e3a1\u7684\u5411\u91cf\u4e3a\u6807\u51c6\u3002\u90a3\u4e48\u4e00\u4e2a\u5411\u91cf(3,2)\u5b9e\u9645\u662f\u8bf4\u5728x\u8f74\u6295\u5f71\u4e3a3\u800cy\u8f74\u7684\u6295\u5f71\u4e3a2\u3002\u6ce8\u610f\u6295\u5f71\u662f\u4e00\u4e2a\u77e2\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u4e3a\u8d1f\u3002
\u66f4\u6b63\u5f0f\u7684\u8bf4\uff0c\u5411\u91cf(x,y)\u5b9e\u9645\u4e0a\u8868\u793a\u7ebf\u6027\u7ec4\u5408\uff1a
\\[x(1,0)^\\mathsf{T}+y(0,1)^\\mathsf{T}\\]\u4e0d\u96be\u8bc1\u660e\u6240\u6709\u4e8c\u7ef4\u5411\u91cf\u90fd\u53ef\u4ee5\u8868\u793a\u4e3a\u8fd9\u6837\u7684\u7ebf\u6027\u7ec4\u5408\u3002\u6b64\u5904(1,0)\u548c(0,1)\u53eb\u505a\u4e8c\u7ef4\u7a7a\u95f4\u4e2d\u7684\u4e00\u7ec4\u57fa\u3002
\u6240\u4ee5\uff0c\u8981\u51c6\u786e\u63cf\u8ff0\u5411\u91cf\uff0c\u9996\u5148\u8981\u786e\u5b9a\u4e00\u7ec4\u57fa\uff0c\u7136\u540e\u7ed9\u51fa\u5728\u57fa\u6240\u5728\u7684\u5404\u4e2a\u76f4\u7ebf\u4e0a\u7684\u6295\u5f71\u503c\uff0c\u5c31\u53ef\u4ee5\u4e86\u3002\u53ea\u4e0d\u8fc7\u6211\u4eec\u7ecf\u5e38\u7701\u7565\u7b2c\u4e00\u6b65\uff0c\u800c\u9ed8\u8ba4\u4ee5(1,0)\u548c(0,1)\u4e3a\u57fa\u3002
\u6211\u4eec\u4e4b\u6240\u4ee5\u9ed8\u8ba4\u9009\u62e9(1,0)\u548c(0,1)\u4e3a\u57fa\uff0c\u5f53\u7136\u662f\u6bd4\u8f83\u65b9\u4fbf\uff0c\u56e0\u4e3a\u5b83\u4eec\u5206\u522b\u662fx\u548cy\u8f74\u6b63\u65b9\u5411\u4e0a\u7684\u5355\u4f4d\u5411\u91cf\uff0c\u56e0\u6b64\u5c31\u4f7f\u5f97\u4e8c\u7ef4\u5e73\u9762\u4e0a\u70b9\u5750\u6807\u548c\u5411\u91cf\u4e00\u4e00\u5bf9\u5e94\uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u4f46\u5b9e\u9645\u4e0a\u4efb\u4f55\u4e24\u4e2a\u7ebf\u6027\u65e0\u5173\u7684\u4e8c\u7ef4\u5411\u91cf\u90fd\u53ef\u4ee5\u6210\u4e3a\u4e00\u7ec4\u57fa\uff0c\u6240\u8c13\u7ebf\u6027\u65e0\u5173\u5728\u4e8c\u7ef4\u5e73\u9762\u5185\u53ef\u4ee5\u76f4\u89c2\u8ba4\u4e3a\u662f\u4e24\u4e2a\u4e0d\u5728\u4e00\u6761\u76f4\u7ebf\u4e0a\u7684\u5411\u91cf\u3002
\u4f8b\u5982\uff0c(1,1)\u548c(-1,1)\u4e5f\u53ef\u4ee5\u6210\u4e3a\u4e00\u7ec4\u57fa\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u5e0c\u671b\u57fa\u7684\u6a21\u662f1\uff0c\u56e0\u4e3a\u4ece\u5185\u79ef\u7684\u610f\u4e49\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u57fa\u7684\u6a21\u662f1\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u7528\u5411\u91cf\u70b9\u4e58\u57fa\u800c\u76f4\u63a5\u83b7\u5f97\u5176\u5728\u65b0\u57fa\u4e0a\u7684\u5750\u6807\u4e86\uff01\u5b9e\u9645\u4e0a\uff0c\u5bf9\u5e94\u4efb\u4f55\u4e00\u4e2a\u5411\u91cf\u6211\u4eec\u603b\u53ef\u4ee5\u627e\u5230\u5176\u540c\u65b9\u5411\u4e0a\u6a21\u4e3a1\u7684\u5411\u91cf\uff0c\u53ea\u8981\u8ba9\u4e24\u4e2a\u5206\u91cf\u5206\u522b\u9664\u4ee5\u6a21\u5c31\u597d\u4e86\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u57fa\u53ef\u4ee5\u53d8\u4e3a\\((\\frac{1}{\\sqrt{2}},\\frac{1}{\\sqrt{2}})\\)\u548c\\((-\\frac{1}{\\sqrt{2}},\\frac{1}{\\sqrt{2}})\\)\u3002
\u73b0\u5728\uff0c\u6211\u4eec\u60f3\u83b7\u5f97(3,2)\u5728\u65b0\u57fa\u4e0a\u7684\u5750\u6807\uff0c\u5373\u5728\u4e24\u4e2a\u65b9\u5411\u4e0a\u7684\u6295\u5f71\u77e2\u91cf\u503c\uff0c\u90a3\u4e48\u6839\u636e\u5185\u79ef\u7684\u51e0\u4f55\u610f\u4e49\uff0c\u6211\u4eec\u53ea\u8981\u5206\u522b\u8ba1\u7b97(3,2)\u548c\u4e24\u4e2a\u57fa\u7684\u5185\u79ef\uff0c\u4e0d\u96be\u5f97\u5230\u65b0\u7684\u5750\u6807\u4e3a\\((\\frac{5}{\\sqrt{2}},-\\frac{1}{\\sqrt{2}})\\)\u3002\u4e0b\u56fe\u7ed9\u51fa\u4e86\u65b0\u7684\u57fa\u4ee5\u53ca(3,2)\u5728\u65b0\u57fa\u4e0a\u5750\u6807\u503c\u7684\u793a\u610f\u56fe\uff1a
\u53e6\u5916\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u5217\u4e3e\u7684\u4f8b\u5b50\u4e2d\u57fa\u662f\u6b63\u4ea4\u7684\uff08\u5373\u5185\u79ef\u4e3a0\uff0c\u6216\u76f4\u89c2\u8bf4\u76f8\u4e92\u5782\u76f4\uff09\uff0c\u4f46\u53ef\u4ee5\u6210\u4e3a\u4e00\u7ec4\u57fa\u7684\u552f\u4e00\u8981\u6c42\u5c31\u662f\u7ebf\u6027\u65e0\u5173\uff0c\u975e\u6b63\u4ea4\u7684\u57fa\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u4e0d\u8fc7\u56e0\u4e3a\u6b63\u4ea4\u57fa\u6709\u8f83\u597d\u7684\u6027\u8d28\uff0c\u6240\u4ee5\u4e00\u822c\u4f7f\u7528\u7684\u57fa\u90fd\u662f\u6b63\u4ea4\u7684\u3002
\u4e0b\u9762\u6211\u4eec\u627e\u4e00\u79cd\u7b80\u4fbf\u7684\u65b9\u5f0f\u6765\u8868\u793a\u57fa\u53d8\u6362\u3002\u8fd8\u662f\u62ff\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u60f3\u4e00\u4e0b\uff0c\u5c06(3,2)\u53d8\u6362\u4e3a\u65b0\u57fa\u4e0a\u7684\u5750\u6807\uff0c\u5c31\u662f\u7528(3,2)\u4e0e\u7b2c\u4e00\u4e2a\u57fa\u505a\u5185\u79ef\u8fd0\u7b97\uff0c\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u65b0\u7684\u5750\u6807\u5206\u91cf\uff0c\u7136\u540e\u7528(3,2)\u4e0e\u7b2c\u4e8c\u4e2a\u57fa\u505a\u5185\u79ef\u8fd0\u7b97\uff0c\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u65b0\u5750\u6807\u7684\u5206\u91cf\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u77e9\u9635\u76f8\u4e58\u7684\u5f62\u5f0f\u7b80\u6d01\u7684\u8868\u793a\u8fd9\u4e2a\u53d8\u6362\uff1a
\\[\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\\\ -1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix} \\begin{pmatrix} 3 \\\\ 2 \\end{pmatrix} = \\begin{pmatrix} 5/\\sqrt{2} \\\\ -1/\\sqrt{2} \\end{pmatrix}\\]\u592a\u6f02\u4eae\u4e86\uff01\u5176\u4e2d\u77e9\u9635\u7684\u4e24\u884c\u5206\u522b\u4e3a\u4e24\u4e2a\u57fa\uff0c\u4e58\u4ee5\u539f\u5411\u91cf\uff0c\u5176\u7ed3\u679c\u521a\u597d\u4e3a\u65b0\u57fa\u7684\u5750\u6807\u3002\u53ef\u4ee5\u7a0d\u5fae\u63a8\u5e7f\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u6709m\u4e2a\u4e8c\u7ef4\u5411\u91cf\uff0c\u53ea\u8981\u5c06\u4e8c\u7ef4\u5411\u91cf\u6309\u5217\u6392\u6210\u4e00\u4e2a\u4e24\u884cm\u5217\u77e9\u9635\uff0c\u7136\u540e\u7528\u201c\u57fa\u77e9\u9635\u201d\u4e58\u4ee5\u8fd9\u4e2a\u77e9\u9635\uff0c\u5c31\u5f97\u5230\u4e86\u6240\u6709\u8fd9\u4e9b\u5411\u91cf\u5728\u65b0\u57fa\u4e0b\u7684\u503c\u3002\u4f8b\u5982(1,1)\uff0c(2,2)\uff0c(3,3)\uff0c\u60f3\u53d8\u6362\u5230\u521a\u624d\u90a3\u7ec4\u57fa\u4e0a\uff0c\u5219\u53ef\u4ee5\u8fd9\u6837\u8868\u793a\uff1a
\\[\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\\\ -1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix} \\begin{pmatrix} 1 & 2 & 3 \\\\ 1 & 2 & 3 \\end{pmatrix} = \\begin{pmatrix} 2/\\sqrt{2} & 4/\\sqrt{2} & 6/\\sqrt{2} \\\\ 0 & 0 & 0 \\end{pmatrix}\\]\u4e8e\u662f\u4e00\u7ec4\u5411\u91cf\u7684\u57fa\u53d8\u6362\u88ab\u5e72\u51c0\u7684\u8868\u793a\u4e3a\u77e9\u9635\u7684\u76f8\u4e58\u3002
\u4e00\u822c\u7684\uff0c\u5982\u679c\u6211\u4eec\u6709M\u4e2aN\u7ef4\u5411\u91cf\uff0c\u60f3\u5c06\u5176\u53d8\u6362\u4e3a\u7531R\u4e2aN\u7ef4\u5411\u91cf\u8868\u793a\u7684\u65b0\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u9996\u5148\u5c06R\u4e2a\u57fa\u6309\u884c\u7ec4\u6210\u77e9\u9635A\uff0c\u7136\u540e\u5c06\u5411\u91cf\u6309\u5217\u7ec4\u6210\u77e9\u9635B\uff0c\u90a3\u4e48\u4e24\u77e9\u9635\u7684\u4e58\u79efAB\u5c31\u662f\u53d8\u6362\u7ed3\u679c\uff0c\u5176\u4e2dAB\u7684\u7b2cm\u5217\u4e3aA\u4e2d\u7b2cm\u5217\u53d8\u6362\u540e\u7684\u7ed3\u679c\u3002
\u6570\u5b66\u8868\u793a\u4e3a\uff1a
\\[\\begin{pmatrix} p_1 \\\\ p_2 \\\\ \\vdots \\\\ p_R \\end{pmatrix} \\begin{pmatrix} a_1 & a_2 & \\cdots & a_M \\end{pmatrix} = \\begin{pmatrix} p_1a_1 & p_1a_2 & \\cdots & p_1a_M \\\\ p_2a_1 & p_2a_2 & \\cdots & p_2a_M \\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\ p_Ra_1 & p_Ra_2 & \\cdots & p_Ra_M \\end{pmatrix}\\]\u5176\u4e2d\\(p_i\\)\u662f\u4e00\u4e2a\u884c\u5411\u91cf\uff0c\u8868\u793a\u7b2ci\u4e2a\u57fa\uff0c\\(a_j\\)\u662f\u4e00\u4e2a\u5217\u5411\u91cf\uff0c\u8868\u793a\u7b2cj\u4e2a\u539f\u59cb\u6570\u636e\u8bb0\u5f55\u3002
\u7279\u522b\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91ccR\u53ef\u4ee5\u5c0f\u4e8eN\uff0c\u800cR\u51b3\u5b9a\u4e86\u53d8\u6362\u540e\u6570\u636e\u7684\u7ef4\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00N\u7ef4\u6570\u636e\u53d8\u6362\u5230\u66f4\u4f4e\u7ef4\u5ea6\u7684\u7a7a\u95f4\u4e2d\u53bb\uff0c\u53d8\u6362\u540e\u7684\u7ef4\u5ea6\u53d6\u51b3\u4e8e\u57fa\u7684\u6570\u91cf\u3002\u56e0\u6b64\u8fd9\u79cd\u77e9\u9635\u76f8\u4e58\u7684\u8868\u793a\u4e5f\u53ef\u4ee5\u8868\u793a\u964d\u7ef4\u53d8\u6362\u3002
\u6700\u540e\uff0c\u4e0a\u8ff0\u5206\u6790\u540c\u65f6\u7ed9\u77e9\u9635\u76f8\u4e58\u627e\u5230\u4e86\u4e00\u79cd\u7269\u7406\u89e3\u91ca\uff1a\u4e24\u4e2a\u77e9\u9635\u76f8\u4e58\u7684\u610f\u4e49\u662f\u5c06\u53f3\u8fb9\u77e9\u9635\u4e2d\u7684\u6bcf\u4e00\u5217\u5217\u5411\u91cf\u53d8\u6362\u5230\u5de6\u8fb9\u77e9\u9635\u4e2d\u6bcf\u4e00\u884c\u884c\u5411\u91cf\u4e3a\u57fa\u6240\u8868\u793a\u7684\u7a7a\u95f4\u4e2d\u53bb\u3002\u66f4\u62bd\u8c61\u7684\u8bf4\uff0c\u4e00\u4e2a\u77e9\u9635\u53ef\u4ee5\u8868\u793a\u4e00\u79cd\u7ebf\u6027\u53d8\u6362\u3002\u5f88\u591a\u540c\u5b66\u5728\u5b66\u7ebf\u6027\u4ee3\u6570\u65f6\u5bf9\u77e9\u9635\u76f8\u4e58\u7684\u65b9\u6cd5\u611f\u5230\u5947\u602a\uff0c\u4f46\u662f\u5982\u679c\u660e\u767d\u4e86\u77e9\u9635\u76f8\u4e58\u7684\u7269\u7406\u610f\u4e49\uff0c\u5176\u5408\u7406\u6027\u5c31\u4e00\u76ee\u4e86\u7136\u4e86\u3002
"},{"location":"AI/pca/#_2","title":"\u534f\u65b9\u5dee\u77e9\u9635\u53ca\u4f18\u5316\u76ee\u6807","text":"\u4e0a\u9762\u6211\u4eec\u8ba8\u8bba\u4e86\u9009\u62e9\u4e0d\u540c\u7684\u57fa\u53ef\u4ee5\u5bf9\u540c\u6837\u4e00\u7ec4\u6570\u636e\u7ed9\u51fa\u4e0d\u540c\u7684\u8868\u793a\uff0c\u800c\u4e14\u5982\u679c\u57fa\u7684\u6570\u91cf\u5c11\u4e8e\u5411\u91cf\u672c\u8eab\u7684\u7ef4\u6570\uff0c\u5219\u53ef\u4ee5\u8fbe\u5230\u964d\u7ef4\u7684\u6548\u679c\u3002\u4f46\u662f\u6211\u4eec\u8fd8\u6ca1\u6709\u56de\u7b54\u4e00\u4e2a\u6700\u6700\u5173\u952e\u7684\u95ee\u9898\uff1a\u5982\u4f55\u9009\u62e9\u57fa\u624d\u662f\u6700\u4f18\u7684\u3002\u6216\u8005\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u7ec4N\u7ef4\u5411\u91cf\uff0c\u73b0\u5728\u8981\u5c06\u5176\u964d\u5230K\u7ef4\uff08K\u5c0f\u4e8eN\uff09\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u9009\u62e9K\u4e2a\u57fa\u624d\u80fd\u6700\u5927\u7a0b\u5ea6\u4fdd\u7559\u539f\u6709\u7684\u4fe1\u606f\uff1f
\u8981\u5b8c\u5168\u6570\u5b66\u5316\u8fd9\u4e2a\u95ee\u9898\u975e\u5e38\u7e41\u6742\uff0c\u8fd9\u91cc\u6211\u4eec\u7528\u4e00\u79cd\u975e\u5f62\u5f0f\u5316\u7684\u76f4\u89c2\u65b9\u6cd5\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002
\u4e3a\u4e86\u907f\u514d\u8fc7\u4e8e\u62bd\u8c61\u7684\u8ba8\u8bba\uff0c\u6211\u4eec\u4ecd\u4ee5\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u5c55\u5f00\u3002\u5047\u8bbe\u6211\u4eec\u7684\u6570\u636e\u7531\u4e94\u6761\u8bb0\u5f55\u7ec4\u6210\uff0c\u5c06\u5b83\u4eec\u8868\u793a\u6210\u77e9\u9635\u5f62\u5f0f\uff1a
\\[\\begin{pmatrix} 1 & 1 & 2 & 4 & 2 \\\\ 1 & 3 & 3 & 4 & 4 \\end{pmatrix}\\]\u5176\u4e2d\u6bcf\u4e00\u5217\u4e3a\u4e00\u6761\u6570\u636e\u8bb0\u5f55\uff0c\u800c\u4e00\u884c\u4e3a\u4e00\u4e2a\u5b57\u6bb5\u3002\u4e3a\u4e86\u540e\u7eed\u5904\u7406\u65b9\u4fbf\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6bcf\u4e2a\u5b57\u6bb5\u5185\u6240\u6709\u503c\u90fd\u51cf\u53bb\u5b57\u6bb5\u5747\u503c\uff0c\u5176\u7ed3\u679c\u662f\u5c06\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u53d8\u4e3a\u5747\u503c\u4e3a0\uff08\u8fd9\u6837\u505a\u7684\u9053\u7406\u548c\u597d\u5904\u540e\u9762\u4f1a\u770b\u5230\uff09\u3002
\u6211\u4eec\u770b\u4e0a\u9762\u7684\u6570\u636e\uff0c\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u5747\u503c\u4e3a2\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u5747\u503c\u4e3a3\uff0c\u6240\u4ee5\u53d8\u6362\u540e\uff1a
\\[\\begin{pmatrix} -1 & -1 & 0 & 2 & 0 \\\\ -2 & 0 & 0 & 1 & 1 \\end{pmatrix}\\]\u6211\u4eec\u53ef\u4ee5\u770b\u4e0b\u4e94\u6761\u6570\u636e\u5728\u5e73\u9762\u76f4\u89d2\u5750\u6807\u7cfb\u5185\u7684\u6837\u5b50\uff1a
\u73b0\u5728\u95ee\u9898\u6765\u4e86\uff1a\u5982\u679c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u4e00\u7ef4\u6765\u8868\u793a\u8fd9\u4e9b\u6570\u636e\uff0c\u53c8\u5e0c\u671b\u5c3d\u91cf\u4fdd\u7559\u539f\u59cb\u7684\u4fe1\u606f\uff0c\u4f60\u8981\u5982\u4f55\u9009\u62e9\uff1f
\u901a\u8fc7\u4e0a\u4e00\u8282\u5bf9\u57fa\u53d8\u6362\u7684\u8ba8\u8bba\u6211\u4eec\u77e5\u9053\uff0c\u8fd9\u4e2a\u95ee\u9898\u5b9e\u9645\u4e0a\u662f\u8981\u5728\u4e8c\u7ef4\u5e73\u9762\u4e2d\u9009\u62e9\u4e00\u4e2a\u65b9\u5411\uff0c\u5c06\u6240\u6709\u6570\u636e\u90fd\u6295\u5f71\u5230\u8fd9\u4e2a\u65b9\u5411\u6240\u5728\u76f4\u7ebf\u4e0a\uff0c\u7528\u6295\u5f71\u503c\u8868\u793a\u539f\u59cb\u8bb0\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u5b9e\u9645\u7684\u4e8c\u7ef4\u964d\u5230\u4e00\u7ef4\u7684\u95ee\u9898\u3002
\u90a3\u4e48\u5982\u4f55\u9009\u62e9\u8fd9\u4e2a\u65b9\u5411\uff08\u6216\u8005\u8bf4\u57fa\uff09\u624d\u80fd\u5c3d\u91cf\u4fdd\u7559\u6700\u591a\u7684\u539f\u59cb\u4fe1\u606f\u5462\uff1f\u4e00\u79cd\u76f4\u89c2\u7684\u770b\u6cd5\u662f\uff1a\u5e0c\u671b\u6295\u5f71\u540e\u7684\u6295\u5f71\u503c\u5c3d\u53ef\u80fd\u5206\u6563\u3002
\u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u770b\u51fa\u5982\u679c\u5411x\u8f74\u6295\u5f71\uff0c\u90a3\u4e48\u6700\u5de6\u8fb9\u7684\u4e24\u4e2a\u70b9\u4f1a\u91cd\u53e0\u5728\u4e00\u8d77\uff0c\u4e2d\u95f4\u7684\u4e24\u4e2a\u70b9\u4e5f\u4f1a\u91cd\u53e0\u5728\u4e00\u8d77\uff0c\u4e8e\u662f\u672c\u8eab\u56db\u4e2a\u5404\u4e0d\u76f8\u540c\u7684\u4e8c\u7ef4\u70b9\u6295\u5f71\u540e\u53ea\u5269\u4e0b\u4e24\u4e2a\u4e0d\u540c\u7684\u503c\u4e86\uff0c\u8fd9\u662f\u4e00\u79cd\u4e25\u91cd\u7684\u4fe1\u606f\u4e22\u5931\uff0c\u540c\u7406\uff0c\u5982\u679c\u5411y\u8f74\u6295\u5f71\u6700\u4e0a\u9762\u7684\u4e24\u4e2a\u70b9\u548c\u5206\u5e03\u5728x\u8f74\u4e0a\u7684\u4e24\u4e2a\u70b9\u4e5f\u4f1a\u91cd\u53e0\u3002\u6240\u4ee5\u770b\u6765x\u548cy\u8f74\u90fd\u4e0d\u662f\u6700\u597d\u7684\u6295\u5f71\u9009\u62e9\u3002\u6211\u4eec\u76f4\u89c2\u76ee\u6d4b\uff0c\u5982\u679c\u5411\u901a\u8fc7\u7b2c\u4e00\u8c61\u9650\u548c\u7b2c\u4e09\u8c61\u9650\u7684\u659c\u7ebf\u6295\u5f71\uff0c\u5219\u4e94\u4e2a\u70b9\u5728\u6295\u5f71\u540e\u8fd8\u662f\u53ef\u4ee5\u533a\u5206\u7684\u3002
\u4e0b\u9762\uff0c\u6211\u4eec\u7528\u6570\u5b66\u65b9\u6cd5\u8868\u8ff0\u8fd9\u4e2a\u95ee\u9898\u3002
"},{"location":"AI/pca/#_3","title":"\u65b9\u5dee","text":"\u4e0a\u6587\u8bf4\u5230\uff0c\u6211\u4eec\u5e0c\u671b\u6295\u5f71\u540e\u6295\u5f71\u503c\u5c3d\u53ef\u80fd\u5206\u6563\uff0c\u800c\u8fd9\u79cd\u5206\u6563\u7a0b\u5ea6\uff0c\u53ef\u4ee5\u7528\u6570\u5b66\u4e0a\u7684\u65b9\u5dee\u6765\u8868\u8ff0\u3002\u6b64\u5904\uff0c\u4e00\u4e2a\u5b57\u6bb5\u7684\u65b9\u5dee\u53ef\u4ee5\u770b\u505a\u662f\u6bcf\u4e2a\u5143\u7d20\u4e0e\u5b57\u6bb5\u5747\u503c\u7684\u5dee\u7684\u5e73\u65b9\u548c\u7684\u5747\u503c\uff0c\u5373\uff1a
\\[Var(a)=\\frac{1}{m}\\sum_{i=1}^m{(a_i-\\mu)^2}\\]\u7531\u4e8e\u4e0a\u9762\u6211\u4eec\u5df2\u7ecf\u5c06\u6bcf\u4e2a\u5b57\u6bb5\u7684\u5747\u503c\u90fd\u5316\u4e3a0\u4e86\uff0c\u56e0\u6b64\u65b9\u5dee\u53ef\u4ee5\u76f4\u63a5\u7528\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u548c\u9664\u4ee5\u5143\u7d20\u4e2a\u6570\u8868\u793a\uff1a
\\[Var(a)=\\frac{1}{m}\\sum_{i=1}^m{a_i^2}\\]\u4e8e\u662f\u4e0a\u9762\u7684\u95ee\u9898\u88ab\u5f62\u5f0f\u5316\u8868\u8ff0\u4e3a\uff1a\u5bfb\u627e\u4e00\u4e2a\u4e00\u7ef4\u57fa\uff0c\u4f7f\u5f97\u6240\u6709\u6570\u636e\u53d8\u6362\u4e3a\u8fd9\u4e2a\u57fa\u4e0a\u7684\u5750\u6807\u8868\u793a\u540e\uff0c\u65b9\u5dee\u503c\u6700\u5927\u3002
"},{"location":"AI/pca/#_4","title":"\u534f\u65b9\u5dee","text":"\u5bf9\u4e8e\u4e0a\u9762\u4e8c\u7ef4\u964d\u6210\u4e00\u7ef4\u7684\u95ee\u9898\u6765\u8bf4\uff0c\u627e\u5230\u90a3\u4e2a\u4f7f\u5f97\u65b9\u5dee\u6700\u5927\u7684\u65b9\u5411\u5c31\u53ef\u4ee5\u4e86\u3002\u4e0d\u8fc7\u5bf9\u4e8e\u66f4\u9ad8\u7ef4\uff0c\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\u9700\u8981\u89e3\u51b3\u3002\u8003\u8651\u4e09\u7ef4\u964d\u5230\u4e8c\u7ef4\u95ee\u9898\u3002\u4e0e\u4e4b\u524d\u76f8\u540c\uff0c\u9996\u5148\u6211\u4eec\u5e0c\u671b\u627e\u5230\u4e00\u4e2a\u65b9\u5411\u4f7f\u5f97\u6295\u5f71\u540e\u65b9\u5dee\u6700\u5927\uff0c\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u7b2c\u4e00\u4e2a\u65b9\u5411\u7684\u9009\u62e9\uff0c\u7ee7\u800c\u6211\u4eec\u9009\u62e9\u7b2c\u4e8c\u4e2a\u6295\u5f71\u65b9\u5411\u3002
\u5982\u679c\u6211\u4eec\u8fd8\u662f\u5355\u7eaf\u53ea\u9009\u62e9\u65b9\u5dee\u6700\u5927\u7684\u65b9\u5411\uff0c\u5f88\u660e\u663e\uff0c\u8fd9\u4e2a\u65b9\u5411\u4e0e\u7b2c\u4e00\u4e2a\u65b9\u5411\u5e94\u8be5\u662f\u201c\u51e0\u4e4e\u91cd\u5408\u5728\u4e00\u8d77\u201d\uff0c\u663e\u7136\u8fd9\u6837\u7684\u7ef4\u5ea6\u662f\u6ca1\u6709\u7528\u7684\uff0c\u56e0\u6b64\uff0c\u5e94\u8be5\u6709\u5176\u4ed6\u7ea6\u675f\u6761\u4ef6\u3002\u4ece\u76f4\u89c2\u4e0a\u8bf4\uff0c\u8ba9\u4e24\u4e2a\u5b57\u6bb5\u5c3d\u53ef\u80fd\u8868\u793a\u66f4\u591a\u7684\u539f\u59cb\u4fe1\u606f\uff0c\u6211\u4eec\u662f\u4e0d\u5e0c\u671b\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\uff08\u7ebf\u6027\uff09\u76f8\u5173\u6027\u7684\uff0c\u56e0\u4e3a\u76f8\u5173\u6027\u610f\u5473\u7740\u4e24\u4e2a\u5b57\u6bb5\u4e0d\u662f\u5b8c\u5168\u72ec\u7acb\uff0c\u5fc5\u7136\u5b58\u5728\u91cd\u590d\u8868\u793a\u7684\u4fe1\u606f\u3002
\u6570\u5b66\u4e0a\u53ef\u4ee5\u7528\u4e24\u4e2a\u5b57\u6bb5\u7684\u534f\u65b9\u5dee\u8868\u793a\u5176\u76f8\u5173\u6027\uff0c\u7531\u4e8e\u5df2\u7ecf\u8ba9\u6bcf\u4e2a\u5b57\u6bb5\u5747\u503c\u4e3a0\uff0c\u5219\uff1a
\\[Cov(a,b)=\\frac{1}{m}\\sum_{i=1}^m{a_ib_i}\\]\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u5b57\u6bb5\u5747\u503c\u4e3a0\u7684\u60c5\u51b5\u4e0b\uff0c\u4e24\u4e2a\u5b57\u6bb5\u7684\u534f\u65b9\u5dee\u7b80\u6d01\u7684\u8868\u793a\u4e3a\u5176\u5185\u79ef\u9664\u4ee5\u5143\u7d20\u6570m\u3002
\u5f53\u534f\u65b9\u5dee\u4e3a0\u65f6\uff0c\u8868\u793a\u4e24\u4e2a\u5b57\u6bb5\u5b8c\u5168\u72ec\u7acb\u3002\u4e3a\u4e86\u8ba9\u534f\u65b9\u5dee\u4e3a0\uff0c\u6211\u4eec\u9009\u62e9\u7b2c\u4e8c\u4e2a\u57fa\u65f6\u53ea\u80fd\u5728\u4e0e\u7b2c\u4e00\u4e2a\u57fa\u6b63\u4ea4\u7684\u65b9\u5411\u4e0a\u9009\u62e9\u3002\u56e0\u6b64\u6700\u7ec8\u9009\u62e9\u7684\u4e24\u4e2a\u65b9\u5411\u4e00\u5b9a\u662f\u6b63\u4ea4\u7684\u3002
\u81f3\u6b64\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u964d\u7ef4\u95ee\u9898\u7684\u4f18\u5316\u76ee\u6807\uff1a\u5c06\u4e00\u7ec4N\u7ef4\u5411\u91cf\u964d\u4e3aK\u7ef4\uff08K\u5927\u4e8e0\uff0c\u5c0f\u4e8eN\uff09\uff0c\u5176\u76ee\u6807\u662f\u9009\u62e9K\u4e2a\u5355\u4f4d\uff08\u6a21\u4e3a1\uff09\u6b63\u4ea4\u57fa\uff0c\u4f7f\u5f97\u539f\u59cb\u6570\u636e\u53d8\u6362\u5230\u8fd9\u7ec4\u57fa\u4e0a\u540e\uff0c\u5404\u5b57\u6bb5\u4e24\u4e24\u95f4\u534f\u65b9\u5dee\u4e3a0\uff0c\u800c\u5b57\u6bb5\u7684\u65b9\u5dee\u5219\u5c3d\u53ef\u80fd\u5927\uff08\u5728\u6b63\u4ea4\u7684\u7ea6\u675f\u4e0b\uff0c\u53d6\u6700\u5927\u7684K\u4e2a\u65b9\u5dee\uff09\u3002
"},{"location":"AI/pca/#_5","title":"\u534f\u65b9\u5dee\u77e9\u9635","text":"\u4e0a\u9762\u6211\u4eec\u5bfc\u51fa\u4e86\u4f18\u5316\u76ee\u6807\uff0c\u4f46\u662f\u8fd9\u4e2a\u76ee\u6807\u4f3c\u4e4e\u4e0d\u80fd\u76f4\u63a5\u4f5c\u4e3a\u64cd\u4f5c\u6307\u5357\uff08\u6216\u8005\u8bf4\u7b97\u6cd5\uff09\uff0c\u56e0\u4e3a\u5b83\u53ea\u8bf4\u8981\u4ec0\u4e48\uff0c\u4f46\u6839\u672c\u6ca1\u6709\u8bf4\u600e\u4e48\u505a\u3002\u6240\u4ee5\u6211\u4eec\u8981\u7ee7\u7eed\u5728\u6570\u5b66\u4e0a\u7814\u7a76\u8ba1\u7b97\u65b9\u6848\u3002
\u6211\u4eec\u770b\u5230\uff0c\u6700\u7ec8\u8981\u8fbe\u5230\u7684\u76ee\u7684\u4e0e\u5b57\u6bb5\u5185\u65b9\u5dee\u53ca\u5b57\u6bb5\u95f4\u534f\u65b9\u5dee\u6709\u5bc6\u5207\u5173\u7cfb\u3002\u56e0\u6b64\u6211\u4eec\u5e0c\u671b\u80fd\u5c06\u4e24\u8005\u7edf\u4e00\u8868\u793a\uff0c\u4ed4\u7ec6\u89c2\u5bdf\u53d1\u73b0\uff0c\u4e24\u8005\u5747\u53ef\u4ee5\u8868\u793a\u4e3a\u5185\u79ef\u7684\u5f62\u5f0f\uff0c\u800c\u5185\u79ef\u53c8\u4e0e\u77e9\u9635\u76f8\u4e58\u5bc6\u5207\u76f8\u5173\u3002\u4e8e\u662f\u6211\u4eec\u6765\u4e86\u7075\u611f\uff1a
\u5047\u8bbe\u6211\u4eec\u53ea\u6709a\u548cb\u4e24\u4e2a\u5b57\u6bb5\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u5b83\u4eec\u6309\u884c\u7ec4\u6210\u77e9\u9635X\uff1a
\\[X=\\begin{pmatrix} a_1 & a_2 & \\cdots & a_m \\\\ b_1 & b_2 & \\cdots & b_m \\end{pmatrix}\\]\u7136\u540e\u6211\u4eec\u7528X\u4e58\u4ee5X\u7684\u8f6c\u7f6e\uff0c\u5e76\u4e58\u4e0a\u7cfb\u65701/m\uff1a
\\[\\frac{1}{m}XX^\\mathsf{T}=\\begin{pmatrix} \\frac{1}{m}\\sum_{i=1}^m{a_i^2} & \\frac{1}{m}\\sum_{i=1}^m{a_ib_i} \\\\ \\frac{1}{m}\\sum_{i=1}^m{a_ib_i} & \\frac{1}{m}\\sum_{i=1}^m{b_i^2} \\end{pmatrix}\\]\u5947\u8ff9\u51fa\u73b0\u4e86\uff01\u8fd9\u4e2a\u77e9\u9635\u5bf9\u89d2\u7ebf\u4e0a\u7684\u4e24\u4e2a\u5143\u7d20\u5206\u522b\u662f\u4e24\u4e2a\u5b57\u6bb5\u7684\u65b9\u5dee\uff0c\u800c\u5176\u5b83\u5143\u7d20\u662fa\u548cb\u7684\u534f\u65b9\u5dee\u3002\u4e24\u8005\u88ab\u7edf\u4e00\u5230\u4e86\u4e00\u4e2a\u77e9\u9635\u7684\u3002
\u6839\u636e\u77e9\u9635\u76f8\u4e58\u7684\u8fd0\u7b97\u6cd5\u5219\uff0c\u8fd9\u4e2a\u7ed3\u8bba\u5f88\u5bb9\u6613\u88ab\u63a8\u5e7f\u5230\u4e00\u822c\u60c5\u51b5\uff1a
\u8bbe\u6211\u4eec\u6709m\u4e2an\u7ef4\u6570\u636e\u8bb0\u5f55\uff0c\u5c06\u5176\u6309\u5217\u6392\u6210n\u4e58m\u7684\u77e9\u9635X\uff0c\u8bbe\\(C=\\frac{1}{m}XX^\\mathsf{T}\\)\uff0c\u5219C\u662f\u4e00\u4e2a\u5bf9\u79f0\u77e9\u9635\uff0c\u5176\u5bf9\u89d2\u7ebf\u5206\u522b\u4e2a\u5404\u4e2a\u5b57\u6bb5\u7684\u65b9\u5dee\uff0c\u800c\u7b2ci\u884cj\u5217\u548cj\u884ci\u5217\u5143\u7d20\u76f8\u540c\uff0c\u8868\u793ai\u548cj\u4e24\u4e2a\u5b57\u6bb5\u7684\u534f\u65b9\u5dee\u3002
"},{"location":"AI/pca/#_6","title":"\u534f\u65b9\u5dee\u77e9\u9635\u5bf9\u89d2\u5316","text":"\u6839\u636e\u4e0a\u8ff0\u63a8\u5bfc\uff0c\u6211\u4eec\u53d1\u73b0\u8981\u8fbe\u5230\u4f18\u5316\u76ee\u524d\uff0c\u7b49\u4ef7\u4e8e\u5c06\u534f\u65b9\u5dee\u77e9\u9635\u5bf9\u89d2\u5316\uff1a\u5373\u9664\u5bf9\u89d2\u7ebf\u5916\u7684\u5176\u5b83\u5143\u7d20\u5316\u4e3a0\uff0c\u5e76\u4e14\u5728\u5bf9\u89d2\u7ebf\u4e0a\u5c06\u5143\u7d20\u6309\u5927\u5c0f\u4ece\u4e0a\u5230\u4e0b\u6392\u5217\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u8fbe\u5230\u4e86\u4f18\u5316\u76ee\u7684\u3002\u8fd9\u6837\u8bf4\u53ef\u80fd\u8fd8\u4e0d\u662f\u5f88\u660e\u6670\uff0c\u6211\u4eec\u8fdb\u4e00\u6b65\u770b\u4e0b\u539f\u77e9\u9635\u4e0e\u57fa\u53d8\u6362\u540e\u77e9\u9635\u534f\u65b9\u5dee\u77e9\u9635\u7684\u5173\u7cfb\uff1a
\u8bbe\u539f\u59cb\u6570\u636e\u77e9\u9635X\u5bf9\u5e94\u7684\u534f\u65b9\u5dee\u77e9\u9635\u4e3aC\uff0c\u800cP\u662f\u4e00\u7ec4\u57fa\u6309\u884c\u7ec4\u6210\u7684\u77e9\u9635\uff0c\u8bbeY=PX\uff0c\u5219Y\u4e3aX\u5bf9P\u505a\u57fa\u53d8\u6362\u540e\u7684\u6570\u636e\u3002\u8bbeY\u7684\u534f\u65b9\u5dee\u77e9\u9635\u4e3aD\uff0c\u6211\u4eec\u63a8\u5bfc\u4e00\u4e0bD\u4e0eC\u7684\u5173\u7cfb\uff1a
\\[\\begin{array}{l l l} D & = & \\frac{1}{m}YY^\\mathsf{T} \\\\ & = & \\frac{1}{m}(PX)(PX)^\\mathsf{T} \\\\ & = & \\frac{1}{m}PXX^\\mathsf{T}P^\\mathsf{T} \\\\ & = & P(\\frac{1}{m}XX^\\mathsf{T})P^\\mathsf{T} \\\\ & = & PCP^\\mathsf{T} \\end{array}\\]\u73b0\u5728\u4e8b\u60c5\u5f88\u660e\u767d\u4e86\uff01\u6211\u4eec\u8981\u627e\u7684P\u4e0d\u662f\u522b\u7684\uff0c\u800c\u662f\u80fd\u8ba9\u539f\u59cb\u534f\u65b9\u5dee\u77e9\u9635\u5bf9\u89d2\u5316\u7684P\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f18\u5316\u76ee\u6807\u53d8\u6210\u4e86\u5bfb\u627e\u4e00\u4e2a\u77e9\u9635P\uff0c\u6ee1\u8db3\\(PCP^\\mathsf{T}\\)\u662f\u4e00\u4e2a\u5bf9\u89d2\u77e9\u9635\uff0c\u5e76\u4e14\u5bf9\u89d2\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u4f9d\u6b21\u6392\u5217\uff0c\u90a3\u4e48P\u7684\u524dK\u884c\u5c31\u662f\u8981\u5bfb\u627e\u7684\u57fa\uff0c\u7528P\u7684\u524dK\u884c\u7ec4\u6210\u7684\u77e9\u9635\u4e58\u4ee5X\u5c31\u4f7f\u5f97X\u4eceN\u7ef4\u964d\u5230\u4e86K\u7ef4\u5e76\u6ee1\u8db3\u4e0a\u8ff0\u4f18\u5316\u6761\u4ef6\u3002
\u81f3\u6b64\uff0c\u6211\u4eec\u79bb\u201c\u53d1\u660e\u201dPCA\u8fd8\u6709\u4ec5\u4e00\u6b65\u4e4b\u9065\uff01
\u73b0\u5728\u6240\u6709\u7126\u70b9\u90fd\u805a\u7126\u5728\u4e86\u534f\u65b9\u5dee\u77e9\u9635\u5bf9\u89d2\u5316\u95ee\u9898\u4e0a\uff0c\u6709\u65f6\uff0c\u6211\u4eec\u771f\u5e94\u8be5\u611f\u8c22\u6570\u5b66\u5bb6\u7684\u5148\u884c\uff0c\u56e0\u4e3a\u77e9\u9635\u5bf9\u89d2\u5316\u5728\u7ebf\u6027\u4ee3\u6570\u9886\u57df\u5df2\u7ecf\u5c5e\u4e8e\u88ab\u73a9\u70c2\u4e86\u7684\u4e1c\u897f\uff0c\u6240\u4ee5\u8fd9\u5728\u6570\u5b66\u4e0a\u6839\u672c\u4e0d\u662f\u95ee\u9898\u3002
\u7531\u4e0a\u6587\u77e5\u9053\uff0c\u534f\u65b9\u5dee\u77e9\u9635C\u662f\u4e00\u4e2a\u662f\u5bf9\u79f0\u77e9\u9635\uff0c\u5728\u7ebf\u6027\u4ee3\u6570\u4e0a\uff0c\u5b9e\u5bf9\u79f0\u77e9\u9635\u6709\u4e00\u7cfb\u5217\u975e\u5e38\u597d\u7684\u6027\u8d28\uff1a
1\uff09\u5b9e\u5bf9\u79f0\u77e9\u9635\u4e0d\u540c\u7279\u5f81\u503c\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u5fc5\u7136\u6b63\u4ea4\u3002
2\uff09\u8bbe\u7279\u5f81\u5411\u91cf\\(\\lambda\\)\u91cd\u6570\u4e3ar\uff0c\u5219\u5fc5\u7136\u5b58\u5728r\u4e2a\u7ebf\u6027\u65e0\u5173\u7684\u7279\u5f81\u5411\u91cf\u5bf9\u5e94\u4e8e\\(\\lambda\\)\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c06\u8fd9r\u4e2a\u7279\u5f81\u5411\u91cf\u5355\u4f4d\u6b63\u4ea4\u5316\u3002
\u7531\u4e0a\u9762\u4e24\u6761\u53ef\u77e5\uff0c\u4e00\u4e2an\u884cn\u5217\u7684\u5b9e\u5bf9\u79f0\u77e9\u9635\u4e00\u5b9a\u53ef\u4ee5\u627e\u5230n\u4e2a\u5355\u4f4d\u6b63\u4ea4\u7279\u5f81\u5411\u91cf\uff0c\u8bbe\u8fd9n\u4e2a\u7279\u5f81\u5411\u91cf\u4e3a\\(e_1,e_2,\\cdots,e_n\\)\uff0c\u6211\u4eec\u5c06\u5176\u6309\u5217\u7ec4\u6210\u77e9\u9635\uff1a
\\[E=\\begin{pmatrix} e_1 & e_2 & \\cdots & e_n \\end{pmatrix}\\]\u5219\u5bf9\u534f\u65b9\u5dee\u77e9\u9635C\u6709\u5982\u4e0b\u7ed3\u8bba\uff1a
\\[E^\\mathsf{T}CE=\\Lambda=\\begin{pmatrix} \\lambda_1 & & & \\\\ & \\lambda_2 & & \\\\ & & \\ddots & \\\\ & & & \\lambda_n \\end{pmatrix}\\]\u5176\u4e2d\\(\\Lambda\\)\u4e3a\u5bf9\u89d2\u77e9\u9635\uff0c\u5176\u5bf9\u89d2\u5143\u7d20\u4e3a\u5404\u7279\u5f81\u5411\u91cf\u5bf9\u5e94\u7684\u7279\u5f81\u503c\uff08\u53ef\u80fd\u6709\u91cd\u590d\uff09\u3002
\u4ee5\u4e0a\u7ed3\u8bba\u4e0d\u518d\u7ed9\u51fa\u4e25\u683c\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u5bf9\u8bc1\u660e\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u7ebf\u6027\u4ee3\u6570\u4e66\u7c4d\u5173\u4e8e\u201c\u5b9e\u5bf9\u79f0\u77e9\u9635\u5bf9\u89d2\u5316\u201d\u7684\u5185\u5bb9\u3002
\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u53d1\u73b0\u6211\u4eec\u5df2\u7ecf\u627e\u5230\u4e86\u9700\u8981\u7684\u77e9\u9635P\uff1a
\\[P=E^\\mathsf{T}\\]P\u662f\u534f\u65b9\u5dee\u77e9\u9635\u7684\u7279\u5f81\u5411\u91cf\u5355\u4f4d\u5316\u540e\u6309\u884c\u6392\u5217\u51fa\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u90fd\u662fC\u7684\u4e00\u4e2a\u7279\u5f81\u5411\u91cf\u3002\u5982\u679c\u8bbeP\u6309\u7167\\(\\Lambda\\)\u4e2d\u7279\u5f81\u503c\u7684\u4ece\u5927\u5230\u5c0f\uff0c\u5c06\u7279\u5f81\u5411\u91cf\u4ece\u4e0a\u5230\u4e0b\u6392\u5217\uff0c\u5219\u7528P\u7684\u524dK\u884c\u7ec4\u6210\u7684\u77e9\u9635\u4e58\u4ee5\u539f\u59cb\u6570\u636e\u77e9\u9635X\uff0c\u5c31\u5f97\u5230\u4e86\u6211\u4eec\u9700\u8981\u7684\u964d\u7ef4\u540e\u7684\u6570\u636e\u77e9\u9635Y\u3002
\u81f3\u6b64\u6211\u4eec\u5b8c\u6210\u4e86\u6574\u4e2aPCA\u7684\u6570\u5b66\u539f\u7406\u8ba8\u8bba\u3002\u5728\u4e0b\u9762\u7684\u4e00\u8282\uff0c\u6211\u4eec\u5c06\u7ed9\u51faPCA\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002
"},{"location":"AI/pca/#pca","title":"PCA\u7b97\u6cd5","text":"\u603b\u7ed3\u4e00\u4e0bPCA\u7684\u7b97\u6cd5\u6b65\u9aa4\uff1a
\u8bbe\u6709m\u6761n\u7ef4\u6570\u636e\u3002
1\uff09\u5c06\u539f\u59cb\u6570\u636e\u6309\u5217\u7ec4\u6210n\u884cm\u5217\u77e9\u9635X
2\uff09\u5c06X\u7684\u6bcf\u4e00\u884c\uff08\u4ee3\u8868\u4e00\u4e2a\u5c5e\u6027\u5b57\u6bb5\uff09\u8fdb\u884c\u96f6\u5747\u503c\u5316\uff0c\u5373\u51cf\u53bb\u8fd9\u4e00\u884c\u7684\u5747\u503c
3\uff09\u6c42\u51fa\u534f\u65b9\u5dee\u77e9\u9635\\(C=\\frac{1}{m}XX^\\mathsf{T}\\)
4\uff09\u6c42\u51fa\u534f\u65b9\u5dee\u77e9\u9635\u7684\u7279\u5f81\u503c\u53ca\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf
5\uff09\u5c06\u7279\u5f81\u5411\u91cf\u6309\u5bf9\u5e94\u7279\u5f81\u503c\u5927\u5c0f\u4ece\u4e0a\u5230\u4e0b\u6309\u884c\u6392\u5217\u6210\u77e9\u9635\uff0c\u53d6\u524dk\u884c\u7ec4\u6210\u77e9\u9635P
6\uff09\\(Y=PX\\)\u5373\u4e3a\u964d\u7ef4\u5230k\u7ef4\u540e\u7684\u6570\u636e
"},{"location":"AI/pca/#_7","title":"\u5b9e\u4f8b","text":"\u8fd9\u91cc\u4ee5\u4e0a\u6587\u63d0\u5230\u7684
\\[\\begin{pmatrix} -1 & -1 & 0 & 2 & 0 \\\\ -2 & 0 & 0 & 1 & 1 \\end{pmatrix}\\]\u4e3a\u4f8b\uff0c\u6211\u4eec\u7528PCA\u65b9\u6cd5\u5c06\u8fd9\u7ec4\u4e8c\u7ef4\u6570\u636e\u5176\u964d\u5230\u4e00\u7ef4\u3002
\u56e0\u4e3a\u8fd9\u4e2a\u77e9\u9635\u7684\u6bcf\u884c\u5df2\u7ecf\u662f\u96f6\u5747\u503c\uff0c\u8fd9\u91cc\u6211\u4eec\u76f4\u63a5\u6c42\u534f\u65b9\u5dee\u77e9\u9635\uff1a
\\[C=\\frac{1}{5}\\begin{pmatrix} -1 & -1 & 0 & 2 & 0 \\\\ -2 & 0 & 0 & 1 & 1 \\end{pmatrix}\\begin{pmatrix} -1 & -2 \\\\ -1 & 0 \\\\ 0 & 0 \\\\ 2 & 1 \\\\ 0 & 1 \\end{pmatrix}=\\begin{pmatrix} \\frac{6}{5} & \\frac{4}{5} \\\\ \\frac{4}{5} & \\frac{6}{5} \\end{pmatrix}\\]\u7136\u540e\u6c42\u5176\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\uff0c\u5177\u4f53\u6c42\u89e3\u65b9\u6cd5\u4e0d\u518d\u8be6\u8ff0\uff0c\u53ef\u4ee5\u53c2\u8003\u76f8\u5173\u8d44\u6599\u3002\u6c42\u89e3\u540e\u7279\u5f81\u503c\u4e3a\uff1a
\\[\\lambda_1=2,\\lambda_2=2/5\\]\u5176\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u5206\u522b\u662f\uff1a
\\[c_1\\begin{pmatrix} 1 \\\\ 1 \\end{pmatrix},c_2\\begin{pmatrix} -1 \\\\ 1 \\end{pmatrix}\\]\u5176\u4e2d\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u5206\u522b\u662f\u4e00\u4e2a\u901a\u89e3\uff0c\\(c_1\\)\u548c\\(c_2\\)\u53ef\u53d6\u4efb\u610f\u5b9e\u6570\u3002\u90a3\u4e48\u6807\u51c6\u5316\u540e\u7684\u7279\u5f81\u5411\u91cf\u4e3a\uff1a
\\[\\begin{pmatrix} 1/\\sqrt{2} \\\\ 1/\\sqrt{2} \\end{pmatrix},\\begin{pmatrix} -1/\\sqrt{2} \\\\ 1/\\sqrt{2} \\end{pmatrix}\\]\u56e0\u6b64\u6211\u4eec\u7684\u77e9\u9635P\u662f\uff1a
\\[P=\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\\\ -1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix}\\]\u53ef\u4ee5\u9a8c\u8bc1\u534f\u65b9\u5dee\u77e9\u9635C\u7684\u5bf9\u89d2\u5316\uff1a
\\[PCP^\\mathsf{T}=\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\\\ -1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix}\\begin{pmatrix} 6/5 & 4/5 \\\\ 4/5 & 6/5 \\end{pmatrix}\\begin{pmatrix} 1/\\sqrt{2} & -1/\\sqrt{2} \\\\ 1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix}=\\begin{pmatrix} 2 & 0 \\\\ 0 & 2/5 \\end{pmatrix}\\]\u6700\u540e\u6211\u4eec\u7528P\u7684\u7b2c\u4e00\u884c\u4e58\u4ee5\u6570\u636e\u77e9\u9635\uff0c\u5c31\u5f97\u5230\u4e86\u964d\u7ef4\u540e\u7684\u8868\u793a\uff1a
\\[Y=\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix}\\begin{pmatrix} -1 & -1 & 0 & 2 & 0 \\\\ -2 & 0 & 0 & 1 & 1 \\end{pmatrix}=\\begin{pmatrix} -3/\\sqrt{2} & -1/\\sqrt{2} & 0 & 3/\\sqrt{2} & -1/\\sqrt{2} \\end{pmatrix}\\]\u964d\u7ef4\u6295\u5f71\u7ed3\u679c\u5982\u4e0b\u56fe\uff1a
"},{"location":"AI/pca/#_8","title":"\u8fdb\u4e00\u6b65\u8ba8\u8bba","text":"\u6839\u636e\u4e0a\u9762\u5bf9PCA\u7684\u6570\u5b66\u539f\u7406\u7684\u89e3\u91ca\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230\u4e00\u4e9bPCA\u7684\u80fd\u529b\u548c\u9650\u5236\u3002PCA\u672c\u8d28\u4e0a\u662f\u5c06\u65b9\u5dee\u6700\u5927\u7684\u65b9\u5411\u4f5c\u4e3a\u4e3b\u8981\u7279\u5f81\uff0c\u5e76\u4e14\u5728\u5404\u4e2a\u6b63\u4ea4\u65b9\u5411\u4e0a\u5c06\u6570\u636e\u201c\u79bb\u76f8\u5173\u201d\uff0c\u4e5f\u5c31\u662f\u8ba9\u5b83\u4eec\u5728\u4e0d\u540c\u6b63\u4ea4\u65b9\u5411\u4e0a\u6ca1\u6709\u76f8\u5173\u6027\u3002
\u56e0\u6b64\uff0cPCA\u4e5f\u5b58\u5728\u4e00\u4e9b\u9650\u5236\uff0c\u4f8b\u5982\u5b83\u53ef\u4ee5\u5f88\u597d\u7684\u89e3\u9664\u7ebf\u6027\u76f8\u5173\uff0c\u4f46\u662f\u5bf9\u4e8e\u9ad8\u9636\u76f8\u5173\u6027\u5c31\u6ca1\u6709\u529e\u6cd5\u4e86\uff0c\u5bf9\u4e8e\u5b58\u5728\u9ad8\u9636\u76f8\u5173\u6027\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651Kernel PCA\uff0c\u901a\u8fc7Kernel\u51fd\u6570\u5c06\u975e\u7ebf\u6027\u76f8\u5173\u8f6c\u4e3a\u7ebf\u6027\u76f8\u5173\uff0c\u5173\u4e8e\u8fd9\u70b9\u5c31\u4e0d\u5c55\u5f00\u8ba8\u8bba\u4e86\u3002\u53e6\u5916\uff0cPCA\u5047\u8bbe\u6570\u636e\u5404\u4e3b\u7279\u5f81\u662f\u5206\u5e03\u5728\u6b63\u4ea4\u65b9\u5411\u4e0a\uff0c\u5982\u679c\u5728\u975e\u6b63\u4ea4\u65b9\u5411\u4e0a\u5b58\u5728\u51e0\u4e2a\u65b9\u5dee\u8f83\u5927\u7684\u65b9\u5411\uff0cPCA\u7684\u6548\u679c\u5c31\u5927\u6253\u6298\u6263\u4e86\u3002
\u6700\u540e\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0cPCA\u662f\u4e00\u79cd\u65e0\u53c2\u6570\u6280\u672f\uff0c\u4e5f\u5c31\u662f\u8bf4\u9762\u5bf9\u540c\u6837\u7684\u6570\u636e\uff0c\u5982\u679c\u4e0d\u8003\u8651\u6e05\u6d17\uff0c\u8c01\u6765\u505a\u7ed3\u679c\u90fd\u4e00\u6837\uff0c\u6ca1\u6709\u4e3b\u89c2\u53c2\u6570\u7684\u4ecb\u5165\uff0c\u6240\u4ee5PCA\u4fbf\u4e8e\u901a\u7528\u5b9e\u73b0\uff0c\u4f46\u662f\u672c\u8eab\u65e0\u6cd5\u4e2a\u6027\u5316\u7684\u4f18\u5316\u3002
\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u670b\u53cb\u4eec\u4e86\u89e3PCA\u7684\u6570\u5b66\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u73b0\u539f\u7406\uff0c\u501f\u6b64\u4e86\u89e3PCA\u7684\u9002\u7528\u573a\u666f\u548c\u9650\u5236\uff0c\u4ece\u800c\u66f4\u597d\u7684\u4f7f\u7528\u8fd9\u4e2a\u7b97\u6cd5\u3002
"},{"location":"AI/pca/#dla","title":"DLA","text":"\\(or \\Rightarrow max Tr(w^TS_bS_w^{-1}w) constrained to\\ \\ ww^T=1\\)
\\(m1 \\ m2 \u90fd\u662f\u5e73\u5747\u503c\\)
https://zhuanlan.zhihu.com/p/32658341
"},{"location":"CG/lec1/lec1/","title":"Lecture 1 - 2D Graphics","text":""},{"location":"CG/lec1/lec1/#lecture-1-2d-graphics","title":"Lecture 1 - 2D Graphics","text":""},{"location":"CG/lec1/lec1/#rasterization","title":"Rasterization","text":""},{"location":"CG/lec1/lec1/#scan-conversion","title":"Scan Conversion","text":""},{"location":"CG/lec1/lec1/#scan-converting-a-line-segment","title":"Scan converting a line segment","text":"
Bresenham's Line Drawing Algorithm
Algorithm
void Bresenham(int x1, int y1, int x2, int y2) {\n int b = 0; //or a given value\n int dx = x2 - x1;\n int dy = y2 - y1;\n int x = x1, y = y1;\n int P0 = 2 * dy * x1 - 2 * dx * y1 + 2 * dy + (2 * b - 1) * dx;\n for (int i = 0; i < dx; i++) {\n if (P0 > 0) {\n y++;\n P0 += 2 * dy - 2 * dx;\n } else {\n P0 += 2 * dy;\n }\n x++;\n plot(x, y);\n }\n\n}\n
"},{"location":"CG/lec1/lec1/#scan-converting-a-circle","title":"Scan converting a circle","text":"Method 1: Even-Odd Test
Method II Scan Line Polygon Filling Algorithm
"},{"location":"CMU%2015-445/Chap1/Chap1/#relational","title":"Relational","text":""},{"location":"CMU%2015-445/Chap1/Chap1/#relational-model","title":"Relational Model","text":"
Store database in simple data structures (relations).
Physical storage left up to the DBMS implementation.
Access data through high-level language, DBMS figures out best execution strategy.
A relation is an unordered set that contain the relationship of attributes that represent entities.
A tuple is a set of attribute values (also known as its domain) in the relation.
n-ary Relation :Table with n columns
"},{"location":"CMU%2015-445/Chap1/Chap1/#keys","title":"Keys","text":"Primaray Keys
A relation's primary key uniquely identifies a single tuple
Foriegn Keys
A foreign key specifies that an attribute from one relation has to map to a tuple in another relation.
"},{"location":"CMU%2015-445/Chap1/Chap1/#data-manipulation-languages-dml","title":"Data Manipulation Languages (DML)","text":"Methods to store and retrieve information from a database.
"},{"location":"CMU%2015-445/Chap1/Chap1/#relational-algebra","title":"Relational Algebra","text":""},{"location":"CMU%2015-445/Chap1/Chap1/#fundamental-operations","title":"Fundamental Operations","text":"\\(\\sigma -- Select\\)\u200b\u200b
\\(\\pi -- Projection\\)\u200b
\\(\\cup--Union\\)\u200b
\\(\\cap -- Intersection\\)\u200b
\\(- \\ --Difference\\)\u200b
\\(\\times --Product\\)\u200b
\\(\u22c8-- Join\\)\u200b
Example : Refer to ZJU course slides
A better approach is to state the high-level answer that you want the DBMS to compute.
"},{"location":"CMU%2015-445/Chap11/lec/#join-algorithms","title":"Join Algorithms","text":""},{"location":"CMU%2015-445/Chap11/lec/#nested-loop-join","title":"Nested Loop Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#naive-nested-loop-join","title":"Naive Nested Loop Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#block-nested-loop-join","title":"Block Nested Loop Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#index-nested-loop-join","title":"Index Nested Loop Join","text":"
sort R,S on join keys\ncursorR \u2190 Rsorted, cursorS \u2190 Ssorted\nwhile cursorR and cursorS:\n if cursorR > cursorS:\n increment cursorS\n if cursorR < cursorS:\n increment cursorR (and possibly backtrack cursors)\n elif cursorR and cursorS match:\n emit\n increment cursorS\n
Sort cost(R):\\(2M\u2219 (1 + \u2308log_{B-1}\u2308M/ B\u2309\u2309)\\) Sort cost(S):\\(2N\u2219 (1 + \u2308log_{B-1}\u2308N/ B\u2309\u2309)\\) Merge cost:\\((M + N)\\) Total cost:\\(2M\u2219 (1 + \u2308log_{B-1}\u2308M/ B\u2309\u2309) + 2N\u2219 (1 + \u2308log_{B-1}\u2308N/ B\u2309\u2309) + (M + N)\\)
The worst case for the merging phase is when the join attribute of all the tuples in both relations contains the same value
Cost: (M \u2219 N) + (sort cost)
WHEN IS SORT-MERGE JOIN USEFUL? * One or both tables are already sorted on join key. * Output must be sorted on join key. * The input relations may be sorted either by an explicit sort operator, or by scanning the relation using an index on the join key.
"},{"location":"CMU%2015-445/Chap11/lec/#hash-join","title":"Hash Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#simple-hash-join","title":"Simple Hash Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#optimizationprobe-filter","title":"Optimization:Probe Filter","text":"Create a probe filter.
First look in Bloom filter then check hash(because bloom filter is in cache,so it's faster!)
Partition Phase: Hash both tables on the join attribute into partitions Probe Phase: Compares tuples in corresponding partitions for each table
"},{"location":"CMU%2015-445/Chap11/lec/#partitioned-hash-join-edge-cases","title":"Partitioned HASH JOIN EDGE CASES","text":"If a partition does not fit in memory,recursively partition it with a different hash function * Repeat as needed * Eventually hash join the corresponding (sub-)partitions
If a single join key has so many matching records that they don\u2019t fit in memory, use a block nested loop join for that key * Worst case: attribute are all the same then recursive hash is not useful. * A relation does not need recursive partitioning if \\(M > n_h + 1\\), or equivalently \\(M > (b_s/M) + 1\\), which simplifies (approximately) to \\(M > \\sqrt{b}_s\\).
"},{"location":"CMU%2015-445/Chap11/lec/#hybrid-hash-join","title":"Hybrid Hash Join","text":""},{"location":"CMU%2015-445/Chap2/Chap2/","title":"Chapter 2","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#pre-knoledge","title":"Pre-Knoledge","text":"
Reference to https://runoob.com/sql/sql-update.html
"},{"location":"CMU%2015-445/Chap2/Chap2/#fundamentals","title":"Fundamentals","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#create-table-construct","title":"Create Table Construct","text":"create table instructor(\n ID char(5)\n name varchar(20) not null,\n dept_name varchar(20)\uff0c\n salary numeric(8,2)default 0\uff0c\n primary key(ID),\n foreign key(dept_name)references department\n)\uff1b\n
foreign key
\u8fd9\u610f\u5473\u7740\"dept_name\"\u5217\u4e2d\u7684\u503c\u5fc5\u987b\u5b58\u5728\u4e8e\"department\"\u8868\u4e2d\u7684\u5173\u8054\u5217\u4e2d\uff0c\u5426\u5219\u63d2\u5165\u6216\u66f4\u65b0\u64cd\u4f5c\u5c06\u88ab\u62d2\u7edd.foreign key (dept_name) references department) \n on delete cascade |set null |restrict|set default\n on update cascade |set null |restrict |set default,\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#drop-and-alter-table-constructs","title":"Drop and Alter Table Constructs","text":"drop table student.
: Deletes the table and its contents.
delete from student
: Deletes all contents of table, but retains table.
alter table
:
alter table r add A D
alter table student add resume varchar(256);\n
alter table r drop A
SELECT column1, column2, ...\n FROM table_name;\nSELECT column1, column2, ...\n FROM table_name;\nSELECT DISTINCT column1, column2, ...\n FROM table_name;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#sql-where","title":"SQL WHERE","text":"SELECT column1, column2, ...\n FROM table_name WHERE condition;\nSELECT column1, column2, ...\n FROM table_name\n ORDER BY column1, column2, ... ASC|DESC;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#sql-insert","title":"SQL INSERT","text":"INSERT INTO table_name (column1,column2,column3,...)\nVALUES (value1,value2,value3,...);\n
INSERT INTO student\nSELECT ID,name,dept_name,0\nFROM instructor\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#sql-update","title":"SQL Update","text":"UPDATE table_name\nSET column1 = value1, column2 = value2, ...\nWHERE condition;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#sql-delete","title":"SQL DELETE","text":"DELETE FROM table_name WHERE condition;\nDELETE FROM table_name;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#natural-join","title":"Natural Join","text":"select name, course_id\nfrom instructor, teaches\nwhere instructor.ID = teaches.ID;\n\nselect name, course_id\nfrom instructor natural join teaches;\n
course(course_id,title, dept_name,credits\nteaches(ID, course_id,sec_id,semester, year)\ninstructor(ID, name, dept_name,salary\uff09 \nDepartment has different meanings.\nselect name, title from (instructor natural join teaches\uff09join course using(course_id); \u5373\u89c4\u5b9a\u8fde\u63a5\u7684\u5c5e\u6027\uff0c\u5bf9\u5e94\u4e8eFind students who takes courses across his/her department.\nor:\nselect distinct student.id\n from (student natural join takes)join course using (course_id\uff09 \n where student.dept_name <> course.dept_name\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#rename","title":"Rename","text":"select ID, name, salary/12 as monthly_salary\nfrom instructor\n\nselect distinct T. name\nfrom instructor as T, instructor as S\nwhere T.salary > S.salary and S.dept_name = \u2018Comp. Sci.\u2019\n
instructor as T \u2261 instructor T\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#string-operations","title":"String operations","text":"like \u2018100 \\%' escape '\\' \nlike \u2018100 \\%' \nlike \u2018100 #%' escape \u2018#' \n
"},{"location":"CMU%2015-445/Chap2/Chap2/#order","title":"Order","text":"order by name desc\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#limit","title":"Limit","text":" select name\n from instructor\n order by salary desc\n limit 3\uff1b // limit 0,3 \n
"},{"location":"CMU%2015-445/Chap2/Chap2/#set-operations","title":"Set Operations","text":"Each of the above operations automatically eliminates duplicates
To retain all duplicates use the corresponding multiset versions union all, intersect all and except all.
Suppose a tuple occurs m times in r and n times in s, then, it occurs:
m + n times in r union all s
min(m,n) times in r intersect all s
max(0, m \u2013 n) times in r except all s
null signifies an unknown value or that a value does not exist.
The result of any arithmetic expression involving null is nul
5 + null returns null
Find all instructors whose salary is null.
select name \nfrom instructor \nwhere salary is null\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#aggregations-group-by","title":"Aggregations + Group By","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#aggregations","title":"Aggregations","text":"the SELECT output list.
SELECT COUNT(DISTINCT login)\n FROM student WHERE login LIKE \"%@cs\"\n
SELECT AVG(gpq),COUNT(sid)\n FROM student WHERE login LIKE '%@cs'\n
undefined.
Cause ERROR
SELECT AVG(s.gpa), e.cid\n FROM enrolled AS e JOIN student AS s\n ON e.sid = s.sid\n
SELECT AVG(s.gpa), e.cid\n FROM enrolled AS e JOIN student AS s\n ON e.sid = s.sid\n GROUP BY e.cid\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#having","title":"Having","text":"* We cannot use Aggregations to FILTER tuples because we have not computed it yet
"},{"location":"CMU%2015-445/Chap2/Chap2/#examples","title":"Examples","text":"select dept_name\nfrom student\ngroup by dept_name\nhaving count(distinct name)= count(id)\n
select dept_name\nfrom student\ngroup by dept_name\nhaving 1-count(distinct name)/ count(id)<0.001 ;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#nested-subqueries","title":"Nested Subqueries","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#set-membership","title":"Set Membership","text":"select distinct course_id\nfrom section\nwhere semester = \"Fall\" and year = 2009 and\n course_id in (select course_id from section\n where semester = 'Spring' and year = 2010);\n
select distinct course_id\nfrom section\nwhere semester = \u2019Fall\u2019 and year= 2009 and \u000b course_id not in (select course_id\n from section\n where semester = \u2019Spring\u2019 and year= 2010);\n
select count(distinct ID)\nfrom takes\nwhere (couse_id,sec_id,semester,year)in\n(select course_id,sec_id,semester,year from teaches \n where teaches.ID = '10101')\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#set-comparison","title":"Set Comparison","text":"select distinct T.name\nfrom instructor as T,instructor as S\nwhere T.salary > S.salary and S.dept_name = 'Biology'\n
select name\nfrom instructor\nwhere salary > some (select salary\n from instructor\n where dept_name = \u2019Biology\u2019);\n
select name\nfrom instructor\nwhere salary > all (select salary\n from instructor\n where dept_name = \u2019Biology\u2019);\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#test-for-empty-relations","title":"Test for Empty Relations","text":"select course_id\nfrom section as S\nwhere = 'Fall' and year = 2009 and\nexists(select * from section as T\n where semester = 'Spring' and year = 2010\n and S.course_id = T.course_id)\n
\\(Note\\ that\\ X \u2013 Y = \\emptyset \\Rightarrow X\\subset Y\\)
select distinct S.ID,S.name\nfrom student as S\nwhere not exists(\n (select course_id from course where dept_name = 'Biology')\n except\n (select T.course_id from takes as T\n where S.ID = T.ID)\n)\n
The unique construct tests whether a subquery has any duplicate tuples in its result.
Find all courses that were offered at most once in 2009
select T.couse_id\nfrom course as T\nwhere unique(select R.course_id \n from section as R\n where T.course_id = R.course_id and R.year = 2009)\n
select T.course_id\nfrom courses as T\nwhere unique(select R.course_id\n from section as R\n where T.course_id = R.course_id \n and R.year = 2009)\n and exists(select R.course_id\n from section as R\n where T.course_id = R.course_id \n and R.year = 2009)\n
and course_id in (select course_id from section\n where year = 2009) ;\n
select T.course_id\nfrom course as T\nwhere unique(select R.course_id,year,semester\n from section as R\n where T.course_id = R.course_id)\n and exists(select R.course_id,year,semester\n from section as R\n where T.course_id = R.course_id)\n
group by count(*) > 1
\u5b9e\u73b0The with clause provides a way of defining a temporary view whose definition is available only to the query in which the with clause occurs.
Find all departments with the maximum budget
with max_budget(value) as\n (select max(budget\n from department)\n select dept_name\n from department,max_budget\n where department.budget = max_budget.value)\n\nselect dept_name\nfrom department\nwhere budget = (select (max(budget) from department))\n
Complex Queries using With Clause
with dept_total(dept_name,value)as\n(select dept_name,sum(salary)from instructor\ngroup by dept_name),\n deot_total_avg(value)as\n(select avg(value)from dept_total)\nselect dept_name\nfrom dept_total,dept_total_avg\nwhere dept_total.value >= dept_total_ang.value;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#modification-of-the-database","title":"Modification of the Database","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#delete","title":"Delete","text":"delete from instructor\nwhere dept_name in (select dept_name from department\n where building = \u2019Watson\u2019);\n\ndelete from instructor\nwhere salary< (select avg (salary) from instructor);\n'''\nProblem: as we delete tuples from deposit, the average salary changes\nSolution used in SQL:\n1. First, compute avg salary and find all tuples to delete\n2. Next, delete all tuples found above (without ecomputing avg or retesting the tuples)\n'''\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#insert","title":"Insert","text":"insert into course\nvalues (\u2019CS-437\u2019, \u2019Database Systems\u2019, \u2019Comp. Sci.\u2019, 4);\ninsert into course (course_id, title, dept_name, credits)\nvalues (\u2019CS-437\u2019, \u2019Database Systems\u2019, \u2019Comp. Sci.\u2019, 4);\ninsert into student\nvalues (\u20193003\u2019, \u2019Green\u2019, \u2019Finance\u2019, null);\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#update","title":"Update","text":" update instructor\n set salary = salary * 1.03\n where salary > 100000;\n update instructor\n set salary = salary * 1.05\n where salary <= 100000;\n
The order is importantCan be done better using the case statement (next slide)
Case Statement for Conditional Updates
update instructor\nset salary = case when salary <= 100000 then salary * 1.05\nelse salary * 1.03 \nend\n
LIKE is used for string matching.
String-matching operators >%
matches any sequence of characters, including zero characters. Any substring. In other words, %
can match any string of any length, including an empty string. Any character.
'15%'
matches any string starting with \"15\", '%15'
matches any string ending with \"15\", and '%15%'
matches any string containing \"15\" anywhere.SELECT * FROM enrolled AS e\n WHERE e.cid LIKE '15_%'\n
_
matches any single character. It's used to specify that at a particular position, any character must match, but it doesn't matter which character. For example, '15_'
matches \"150\", \"151\", \"152\", etc., but not \"15\" etc.
SELECT * FROM student AS s\n WHERE s.login LIKE '%@c_'\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#substring","title":"SUBSTRING","text":"SELECT SUBSTRING(name,1,5) AS abbrv_name\n FROM student WHERE sid = 53688\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#upper","title":"UPPER","text":"SELECT * FROM student AS s\n WHERE UPPER(s.name) LIKE 'KAN%'\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#_1","title":"||
","text":"SQL-92
SELECT name FROM student\n WHERE login = LOWER(name) || '@cs'\n
MSSQL
SELECT name FROM student\n WHERE login = LOWER(name) + '@cs'\n
MySQL
SELECT name FROM student\n WHERE login = CONCAT(LOWER(name), '@cs')\n
'Li''ly'
will automatically concatenate SELECT NOW();\n SELECT CURRENT_TIMESTAMP();\n SELECT CURRENT_TIMESTAMP();\n SELECT EXTRACT(DAY FROM DATE('2018-08-29'));\n //SELECT DATE('2018-08-29')-DATE('2018-01-01');\n SELECT ROUND((UNIX_TIMESTAMP(DATE('2018-08-29'))-UNIX_TIMESTAMP(DATE('2018-01-01')))/(60*60*24),0) AS days;\n SELECT DATEDIFF(DATE('2018-08-29'),DATE('2018-01-01')) AS days;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#output-control-redirection","title":"Output Control + Redirection","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#output-redirection","title":"Output Redirection","text":"Store query results in another table: \u2192 Table must not already be defined. \u2192 Table will have the same # of columns with the same types as the input.
CREATE TABLE CourseIds (\nSELECT DISTINCT cid FROM enrolled);\n
Insert tuples from query into another table: \u2192 Inner SELECTmust generate the same columns as the
target table.
\u2192 DBMSs have different options/syntax on what to do with
integrity violations (e.g., invalid duplicates).
INSERT INTO CourseIds\n(SELECT DISTINCT cid FROM enrolled);\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#output-control","title":"Output Control","text":"SELECT sid FROM enrolled\nWHERE cid = '15-721'\nORDER BY grade DESC,1,sid ASC\n
offset -- skip
-- should combine with oredered by
clause SELECT sid, name FROM student\nWHERE login LIKE '%@cs'\nLIMIT 20 OFFSET 10 \n
select name from student\n Where sid in(\n select sid from enrolled\n where cid = '14-445 '\n )\n
SELECT name FROM student\nWHERE sid = ANY(\nSELECT sid FROM enrolled\nWHERE cid = '15-445'\n)\n
SELECT (SELECT S.name from student as S where S.sid = E = sid) as sname\nFROM enrolled as E\nwhere cid = '15-445'\n
Find student record with the highest id that is enrolled in at least one course.
SELECT sid,name FROM student\nWHERE sid IN(\nSELECT MAX(sid)FROM enrolled\n)\n
SELECT sid, name FROM student\nWHERE sid IN (\nSELECT sid FROM enrolled\nORDER BY sid DESC LIMIT 1\n)\n
SELECT student.sid, name\n FROM student\n JOIN (SELECT MAX(sid) AS sid\n FROM enrolled) AS max_e\n ON student.sid = max_e.sid;\n
Find all courses that have no students enrolled in it.
SELECT * FROM course\n WHERE NOT EXISTS(\n SELECT * FROM enrolled\n WHERE course.cid = enrolled.cid\n )\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#window-functions","title":"Window Functions","text":"Provides a way to write auxiliary statements for use in a larger query.
Think of it like a temp table just for one query.
Alternative to nested queries and views.
WITH cteName AS (\nSELECT 1\n)\nSELECT * FROM cteName\n
WITH cteSource (maxId) AS (\nSELECT MAX(sid) FROM enrolled\n)\nSELECT name FROM student, cteSource\nWHERE student.sid = cteSource.maxId\n
At each iteration, that SELECT produces a row with a new value one greater than the value of n from the previous row set. The first iteration operates on the initial row set (1) and produces 1+1=2; the second iteration operates on the first iteration\u2019s row set (2) and produces 2+1=3; and so forth. This continues until recursion ends, which occurs when n is no longer less than 5.
https://blog.csdn.net/yiyelanxin/article/details/86593473
Join operations take two relations and return as a result another relation.
Join condition defines which tuples in the two relations match, and what attributes are present in the result of the join.
Join type defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated.
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#examples","title":"Examples","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#types","title":"Types.","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#built-in-data-types-in-sql","title":"Built-in Data Types in SQL","text":"date \u20182005-7-27\u2019
time \u201809:00:30.75\u2019
timestamp \u20182005-7-27 09:00:30.75\u2019
interval \u20181\u2019 day
Subtracting a date/time/timestamp value from another gives an interval value
Interval values can be added to date/time/timestamp values
current_date(), current_time()
year(x), month(x), day(x), hour(x), minute(x), second(x)
create type Dollars as numeric (12,2) final\ncreate table department(\n dept_name varchar (20),\n building varchar (15),\n budget Dollars);\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#domains","title":"Domains","text":"create domain person_name char(20) not null\n
Domains can have constraints, such as not null, specified on them.
create domain degree_level varchar(10)\nconstraint degree_level_test\ncheck (value in (\u2019Bachelors\u2019, \u2019Masters\u2019, \u2019Doctorate\u2019));\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#large-object-types","title":"Large-Object Types","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#integrity-constraints","title":"Integrity Constraints","text":"Integrity constraints guard against accidental damage to the database, by ensuring that authorized changes to the database do not result in a loss of data consistency.
not null
name varchar(20) not null\nbudget numeric(12,2) not null\n
primary key
unique
check (P), where P is a predicate
create table section (\n course_id varchar (8),\n sec_id varchar (8),\n semester varchar (6),\n year numeric (4,0),\n building varchar (15),\n room_number varchar (7),\n time slot id varchar (4), \n primary key (course_id, sec_id, semester, year),\n check (semester in (\u2019Fall\u2019, \u2019Winter\u2019, \u2019Spring\u2019, \u2019Summer\u2019))\n);\n
check (semester in (\u2019Fall\u2019, \u2019Winter\u2019, \u2019Spring\u2019, \u2019Summer\u2019))\n
foreign key
Cascading Actions
create table course (\ncourse_id char(5) primary key,\ntitle varchar(20),\ndept_name varchar(20) references department\n)\ncreate table course (\n ...\ndept_name varchar(20),\nforeign key (dept_name) references department\n on delete cascade,\n on update cascade,\n ...)\n
create table person (\n ID char(10),\n name char(40),\n mother char(10),\n father char(10),\n primary key (ID),\n foreign key (father) references person,\n foreign key (mother) references person);\n
How to insert a tuple without causing constraint violation ?
Assertion
create assertion <assertion-name> check <predicate>;\ncreate assertion credits_earned_constraint check\n(not exists \n (select ID \n from student\n where tot_cred <> (\n select sum(credits)\n from takes natural join course\n where student.ID=takes.ID\n and grade is not null \n and grade<>\u2019F\u2019)))\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#view","title":"View","text":"create view faculty as \nselect ID, name, dept_name\nfrom instructor\n
select name\nfrom faculty\nwhere dept_name = \u2018Biology\u2019\n
create view departments_total_salary(dept_name, total_salary) as\nselect dept_name, sum (salary)\nfrom instructor\ngroup by dept_name;\n
create view physics_fall_2009 as\nselect course.course_id, sec_id, building, room_number\nfrom course, section\nwhere course.course_id = section.course_id\nand course.dept_name = \u2019Physics\u2019\nand section.semester = \u2019Fall\u2019\nand section.year = \u20192009\u2019;\n
create view physics_fall_2009_watson as\nselect course_id, room_number\nfrom physics_fall_2009\nwhere building= \u2019Watson\u2019;\n
create view faculty as\nselect ID, name, dept_name\nfrom instructor\n\ninsert into faculty values (\u201930765\u2019, \u2019Green\u2019, \u2019Music\u2019);\n
This insertion must be represented by the insertion of the tuple(\u201930765\u2019, \u2019Green\u2019, \u2019Music\u2019, null)
into the instructor relation insert into instructor values (\u201930765\u2019, \u2019Green\u2019, \u2019Music\u2019, null);
Some Updates cannot be Translated Uniquely
create view instructor_info a\nselect ID, name, building\nfrom instructor, department\nwhere instructor.dept_name= department.dept_name;\ninsert into instructor_info values (\u201969987\u2019, \u2019White\u2019, \u2019Taylor\u2019);\n
which department, if multiple departments in Taylor?
Most SQL implementations allow updates only on simple views(updatable views)
The from clause has only one database relation.
The select clause contains only attribute names of the relation, and does not have any expressions, aggregates, or distinct specification.
Any attribute not listed in the select clause can be set to null.
The query does not have a group by or having clause.
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#materialized-view","title":"Materialized View","text":"create a physical table containing all the tuples in the result of the query defining the view
If relations used in the query are updated, the materialized view result becomes out of date
create materialized view departments_total_salary(dept_name, total_salary) as\n select dept_name, sum (salary)\n from instructor\n group by dept_name;\nselect dept_name\nfrom departments_total_salary\nwhere total_salary > (select avg(total_salary) from departments_total_salary );\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#view-and-logical-data-indepencence","title":"View and Logical Data Indepencence","text":"If relation \\(S(a, b, c)\\) is split into two sub relations \\(S_1(a,b)\\) and \\(S_2(a,c)\\)\u200b. How to realize the logical data independence?
create table S1...;\ncreate table S2...;\ninsert into S1 select a,b from S;\ninsert into S2 select a,c from S;\ndrop table S;\ncreate view S(a,b,c)as select a,b,c from Sq natural join S2;\n
create view S(a,b,c)as select a,b,c from Sq natural join S2;
\u5b9e\u73b0\u5bf9\u539f\u8868\u7684\u67e5\u8be2\uff0c\u5bf9\u7528\u6237\u6765\u8bf4\u65e0\u533a\u522b.create table student\n( ID varchar (5),\n name varchar (20) not null,\n dept_name varchar (20),\n tot_cred numeric (3,0) default 0,\n primary key (ID) \n)\ncreate index studentID_index on student(ID)\n
select *\n from student\n where ID = \u201812345\u2019\n
AUTOCOMMIT=1;
-- Everytime auto commitSET AUTOCOMMIT=0;\nUPDATE account SET balance=balance -100 WHERE ano=\u20181001\u2019;\nUPDATE account SET balance=balance+100 WHERE ano=\u20181002\u2019;\nCOMMIT;\n
UPDATE account SET balance=balance -200 WHERE ano=\u20181003\u2019;\nUPDATE account SET balance=balance+200 WHERE ano=\u20181004\u2019; COMMIT;\n
UPDATE account SET balance=balance+balance*2.5%;\nCOMMIT;\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#acid-properties","title":"ACID Properties","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#authorization","title":"Authorization","text":"grant <privilege list> // privilege\uff1a\u6743\u9650\non <relation name or view name> to <user list>\n
a user-id
public, which allows all valid users the privilege granted
A role (more on this later)
grant select on instructor to U1, U2, U3\ngrant select on department to public\ngrant update (budget) on department to U1,U2\ngrant all privileges on department to U1\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#revoking-authorization-in-sql","title":"Revoking Authorization in SQL","text":"revoke <privilege list>\non <relation name or view name> \nfrom <user list>\nrevoke select on branch from U1, U2, U3\n
There are two approaches to accessing database from a general-purpose programming language.
API (application-program interface) for a program to interact with a database server
Embedded SQL -- provides a means by which a program can interact with a database server.
At runtime, these function calls connect to the database using an API that provides dynamic SQL facilities.
ODBC (Open Database Connectivity) works with C, C++, C#
public static void JDBCexample(String dbid, String userid, String passwd){ \n try { \n Connection conn = DriverManager.getConnection( \n \"jdbc:oracle:thin:@db.yale.edu:2000:univdb\", userid, passwd); \n Statement stmt = conn.createStatement(); \n ... Do Actual Work ....\n stmt.close();\n conn.close();\n }\n catch (SQLException sqle) { \n System.out.println(\"SQLException : \" + sqle);\n }\n }\n
* Update to database try {\n stmt.executeUpdate(\n \"insert into instructor values(\u201977987\u2019, \u2019Kim\u2019, \u2019Physics\u2019, 98000)\"\n );\n} \ncatch (SQLException sqle){\n System.out.println(\"Could not insert tuple. \" + sqle);\n}\n
* Execute query and fetch and print results ResultSet rset = stmt.executeQuery(\n \"select dept_name, avg (salary)\n from instructor\n group by dept_name\");\nwhile (rset.next()){\n System.out.println(rset.getString(\"dept_name\") + \" \" +rset.getFloat(2));\n}\n
rset.getString(\u201cdept_name\u201d)
and rset.getString(1)
equivalent if dept_name is the first argument of select result.
int a = rset.getInt(\u201ca\u201d); if (rset.wasNull()) Systems.out.println(\u201cGot null value\u201d);
PreparedStatement pStmt = conn.prepareStatement( \"insert into instructor values(?,?,?,?)\");\npStmt.setString(1, \"88877\"); \npStmt.setString(2, \"Perry\");\npStmt.setString(3, \"Finance\"); \npStmt.setInt(4, 125000);\n\npStmt.executeUpdate(); \n\npStmt.setString(1, \"88878\");\npStmt.executeUpdate();\n
WARNING: always use prepared statements when taking an input from the user and adding it to a query\"insert into instructor values(\u2019 \" + ID + \" \u2019, \u2019 \" + name + \" \u2019, \" + \n \" \u2019 \" + dept name + \" \u2019, \" + salary + \")\"\n
ResultSetMetaData rsmd = rs.getMetaData();\n for(int i = 1; i <= rsmd.getColumnCount(); i++) {\n System.out.println(rsmd.getColumnName(i));\n System.out.println(rsmd.getColumnTypeName(i));\n}\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#transaction-control-in-jdbc","title":"Transaction Control in JDBC","text":"bad idea for transactions with multiple updates
conn.setAutoCommit(false);\n
conn.commit(); \nconn.rollback();\n
conn.setAutoCommit(true)
turns on automatic commit.#sql iterator deptInfoIter ( String dept name, int avgSal);\n deptInfoIter iter = null;\n #sql iter = { select dept_name, avg(salary) as avgSal from instructor group by dept name };\n while (iter.next()) {\n String deptName = iter.dept_name();\n int avgSal = iter.avgSal();\n System.out.println(deptName + \" \" + avgSal);\n }\n iter.close();\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#odbc","title":"ODBC","text":"Each database system supporting ODBC provides a \"driver\" library that must be linked with the client program.
When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action, and fetch results.
ODBC program first allocates an SQL environment, then a database connection handle.
Opens database connection using SQLConnect( ). Parameters for SQLConnect:
Must also specify types of arguments:
int ODBCexample()\n{\n RETCODE error;\n HENV env; /* environment */ \n HDBC conn; /* database connection */ \n SQLAllocEnv(&env);\n SQLAllocConnect(env, &conn);\n SQLConnect(conn, 'db.yale.edu', SQL_NTS, 'avi', SQL_NTS,'avipasswd',SQL_NTS); \n { .... Do actual work ... }\n SQLDisconnect(conn); \n SQLFreeConnect(conn); \n SQLFreeEnv(env); \n}\n
Program sends SQL commands to database by using SQLExecDirect
Result tuples are fetched using SQLFetch()
SQLBindCol()
binds C language variables to attributes of the query result When a tuple is fetched, its attribute values are automatically stored in corresponding C variables.
Arguments to SQLBindCol()
ODBC stmt variable, attribute position in query result
The type conversion from SQL to C.
The address of the variable.
For variable-length types like character arrays,
Good programming requires checking results of every function call for errors; we have omitted most checks for brevity.
char deptname[80];\nfloat salary;\nint lenOut1, lenOut2;\n\nHSTMT stmt;\nchar * sqlquery = \"select dept_name, sum (salary) from instructor group by dept_name\";\nSQLAllocStmt(conn, &stmt);\nerror = SQLExecDirect(stmt, sqlquery, SQL_NTS);\n\nif (error == SQL SUCCESS) {\n SQLBindCol(stmt, 1, SQL_C_CHAR, deptname , 80, &lenOut1);\n SQLBindCol(stmt, 2, SQL_C_FLOAT, &salary, 0 , &lenOut2);\n while (SQLFetch(stmt) == SQL_SUCCESS) {\n printf (\" %s %g\\n\", deptname, salary);\n }\n}\nSQLFreeStmt(stmt, SQL_DROP);\n//\u5b9a\u4e49\u6570\u7ec4\u9700\u8981\u591a\u4e00\u4e2a\uff0c\u5426\u5219\u4f1a\u6709\u622a\u65ad\u3002\u5982 char deptname[11]; \u624d\u80fd\u5b9a\u4e49\u5341\u4e2a\u5143\u7ec4\u3002\n//\u5982\u679c\u7ed3\u679c\u4e3a\u7a7a\uff0c\u5219 lenOut \u4e3a -1.\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#odbc-prepared-statements","title":"ODBC Prepared Statements","text":"SQLPrepare(stmt, <SQL String>);\nSQLBindParameter(stmt, <parameter#>,\n \u2026 type information and value omitted for simplicity..)\n retcode = SQLExecute( stmt); \n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#embedded-sql","title":"Embedded SQL","text":"EXEC-SQL connect to server user user-name using password;\n
They are preceded by a colon (:) to distinguish from SQL variables (e.g., :credit_amount )
EXEC-SQL BEGIN DECLARE SECTION\n int credit-amount ;\nEXEC-SQL END DECLARE SECTION;\n
declare c cursor for <SQL query> \n
EXEC SQL\n declare c cursor for\n select ID, name\n from student\n where tot_cred > :credit_amount;\n
main( )\n{ EXEC SQL INCLUDE SQLCA; //\u58f0\u660e\u6bb5\u5f00\u59cb\n EXEC SQL BEGIN DECLARE SECTION;\n char account_no [11]; //host variables(\u5bbf\u4e3b\u53d8\u91cf)\u58f0\u660e\n char branch_name [16];\n int balance; \n EXEC SQL END DECLARE SECTION;//\u58f0\u660e\u6bb5\u7ed3\u675f\n EXEC SQL CONNECT TO bank_db USER Adam Using Eve; \n scanf (\u201c%s %s %d\u201d, account_no, branch_name, balance);\n EXEC SQL insert into account values (:account_no, :branch_name, :balance);\n If (SQLCA.sqlcode ! = 0){printf ( \u201cError!\\n\u201d);}\n else {printf (\u201cSuccess!\\n\u201d);}\u00e5\n
This statement causes the database system to execute the query and to save the results within a temporary relation.
The query uses the value of the host-language variable credit-amount at the time the open statement is executed.
EXEC SQL open c ;\n
EXEC SQL fetch c into :si, :sn\uff1b\n
A variable called SQLSTATE in the SQL communication area (SQLCA) gets set to '02000' to indicate no more data is available
The close statement causes the database system to delete the temporary relation that holds the result of the query.
EXEC SQL close c ;\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#updates-through-embedded-sql","title":"Updates Through Embedded SQL","text":" EXEC SQL \ndeclare c cursor for\nselect *\nfrom instructor\nwhere dept_name = 'Music'\nfor update\n
We then iterate through the tuples by performing fetch operations on the cursor , and after fetching each tuple we execute the following code:
update instructor\n set salary = salary + 1000\n where current of c\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#procedural-constructs-in-sql","title":"Procedural Constructs in SQL","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#sql-functions","title":"SQL Functions","text":" create function dept_count (dept_name varchar(20))\n returns integer\n begin\n declare d_count integer;\n select count (* ) into d_count\n from instructor\n where instructor.dept_name = dept_name\n\n return d_count;\n end\n
select dept_name, budget\nfrom department\nwhere dept_count (dept_name ) > 1\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#table-functions","title":"Table Functions","text":"create function instructors_of (dept_name char(20) )\nreturns table ( ID varchar(5),\n name varchar(20),\n dept_name varchar(20),\n salary numeric(8,2))\nreturn table\n(select ID, name, dept_name, salary\n from instructor\n where instructor.dept_name = instructors_of.dept_name)\n
select *\nfrom table (instructors_of (\u2018Music\u2019))\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#sql-procedures","title":"SQL Procedures","text":"create procedure dept_count_proc (in dept_name varchar(20), out d_count integer)\nbegin\n select count(*) into d_count\n from instructor\n where instructor.dept_name = dept_count_proc.dept_name\nend\n
declare d_count integer;\ncall dept_count_proc( \u2018Physics\u2019, d_count)\n
While and repeat statements
declare n integer default 0;\nwhile n < 10 do\n set n = n + 1\nend while \n
repeat\n set n = n \u2013 1\n until n = 0\nend repeat\n
For loop
declare n integer default 0;\n for r as\n select budget from department\n where dept_name = \u2018Music\u2019\n do\n set n = n - r.budget\n end for\n
* Conditional statements (if-then-else) if boolean expression \n then statement or compound statement \nelseif boolean expression \n then statement or compound statement \n else statement or compound statement \nend if\n
* SQL:1999 also supports a case statement similar to C case statement * EXAMPLE CREATE FUNCTION registerStudent(\n IN s_id VARCHAR(5),\n IN s_courseid VARCHAR(8),\n IN s_secud VARCHAR(8),\n IN s_semester VARCHAR(6),\n in s_year NUMERIC(4.0),\n OUT errorMsg VARCHAR(100)\n)\nRETURNS INTEGER\nBEGIN\n DECLARE currEnrol INT;\n SELECT COUNT(*) INTO currEnrol\n FROM takes\n WHERE course_id = s_courseid AND sec_id = s_secid AND semester = s_semester AND year = s_year;\n DECLARE LIMIT INT;\n SELECT capacity INTO limit\n FROM classroom NATURAL JOIN section\n WHERE course_id = s_courseid AND sec_id = s_secid AND semester = s_semester AND year = s_year;\n IF(currEnrol<limit)\n BEGIN\n INSET INTO takes VALUES\n (s_id,s_course,s_secid,s_semester,s_year,null);\n RETURN(0);\n END\n SET errorMsg = 'Enrollment limit reached for course'||'s_course_id'||'section'||s_secid;\n RETURN(-1);\nEND;\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#external-language-functionsprocedures","title":"External Language Functions/Procedures","text":"create procedure dept_count_proc(in dept_name varchar(20),out count integer)\nlanguage C\nexternal name '/usr/avi/bin/dept_count_proc'\ncreate function dept_count(dept_name varchar(20))\nreturns integer\nlanguage C\nexternal name '/usr/avi/bin/dept_count'\n
* Benefits of external language functions/procedures: more efficient for many operations, and more expressive power. * Drawbacks Code to implement function may need to be loaded into database system and executed in the database system\u2019s address space. * risk of accidental corruption of database structures * security risk, allowing users access to unauthorized data There are alternatives, which give good security at the cost of potentially worse performance. Direct execution in the database system\u2019s space is used when efficiency is more important than security."},{"location":"CMU%2015-445/Chap2_inclass/lec/#triggers","title":"Triggers","text":"CREATE TRIGGER account_trigger AFTER UPDATE of account ON balance \nreferencing NEW ROW AS nrow \nreferencing OLD ROW AS orow \nFOR EACH ROW\n WHEN nrow.balance - orow.balance > =200000 OR\n orow.balance - nrow.balance >=50000 \n BEGIN\n INSERT INTO account_log VALUES (nrow.account-number, nrow.balance-orow.balance , current_time() )\nEND\n
create trigger timeslot_check1 after insert on section\nreferencing new row as nrow\nfor each row\nwhen (nrow.time_slot_id not in (\n select time_slot_id\n from time_slot)) /* time_slot_id not present in time_slot */\nbegin\n rollback\nend;\n
create trigger timeslot_check2 after delete on timeslot\nreferencing old row as orow\nfor each row\nwhen (orow.time_slot_id not in (select time_slot_id from time_slot) /* last tuple for time slot id deleted from time slot */\nand orow.time_slot_id in (select time_slot_id from section))/* and time_slot_id still referenced from section*/\nbegin\n rollback\nend;\n
create trigger credits_earned after update of takes on grade\nreferencing new row as nrow\nreferencing old row as orow\nfor each row\nwhen nrow.grade <> \u2019F\u2019 and nrow.grade is not null\n and (orow.grade = \u2019F\u2019 or orow.grade is null)\nbegin atomic\n update student\n set tot_cred= tot_cred + \n (select credits\n from course\n where course.course_id= nrow.course_id)\n where student.id = nrow.id;\nend;\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#statement-level-triggers","title":"Statement Level Triggers","text":"Instead of executing a separate action for each affected row, a single action can be executed for all rows affected by a transaction.
for each statement
instead of for each row
create trigger grade_trigger after update of takes on grade\n referencing new table as new_table \n for each statement\n when exists(select avg(grade)\n from new_table\n group by course_id, sec_id, semester, year\n having avg(grade)< 60 )\n begin \n rollback\n end\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#recursive-queries","title":"Recursive Queries","text":"with recursive rec_prereq(course_id, prereq_id) as (\n select course_id, prereq_id\n from prereq\n union\n select rec_prereq.course_id, prereq.prereq_id\n from rec_prereq, prereq\n where rec_prereq.prereq_id = prereq.course_id)\nselect \u2217from rec_prereq;\n
"},{"location":"CMU%2015-445/Chap3/Chap3/#database-storage","title":"Database Storage","text":"
Non-Volatile : Persistent when loose power. Sequential Access Block Addressable Memory.
In the course, we will focus on the DRAM
Goal : Allow the DBMS to manage databases that exceed the amount of memory available
Virtual Memory : A technique that allows an operating system to provide more memory to a process than is physically available.
But also multitable clustering file organization.
Self-contained file : A file that contains all the information needed to manage the data it contains.
While not entirely true, as the DBMS may store some information to manage the data in other files.
Different DBMSs manage pages in files on disk in different ways.
like for SQLite,Only 4KB is atomatic read/write!--Hardware can only guarantee atomicity at 4KB.
If store tuples one by one , do not guarantee that the tuples are stored in that order.
As demonstrated in Heap File Organization
, the tradeoff between read and write is a common problem in database storage.
Every page contains a header of metadata about the page's contents. \u2192 Page Size \u2192 Checksum \u2192 DBMS Version \u2192 Transaction Visibility \u2192 Compression Information * Some systems require pages to be self-contained (e.g., Oracle)
"},{"location":"CMU%2015-445/Chap3/Chap3/#tuple-oriented-layout","title":"Tuple-oriented Layout","text":"When the page gets full, the DBMS writes it out disk and starts filling up the next page with records.
"},{"location":"CMU%2015-445/Chap3/Chap3/#log-structured-compaction","title":"Log-Structured Compaction","text":"
Compaction coalesces larger log files into smaller files by removing unnecessary records. Log-structured storage managers are more common today. This is partly due to the proliferation of RocksDB. Downsides: * Write - Amplification * Compaction is expensive
"},{"location":"CMU%2015-445/Chap3/Chap3/#tuple-layout","title":"Tuple Layout","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#denormalized-tuple-data","title":"Denormalized Tuple Data","text":"Details"},{"location":"CMU%2015-445/Chap3/Chap3/#tuple-storage","title":"Tuple Storage","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#data-representation","title":"Data Representation","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#variable-precision-numbers-fixed-precision-numbers","title":"Variable Precision Numbers & Fixed Precision Numbers","text":"Variable Precision Numbers : Typically faster than arbitrary precision numbers but can have rounding errors...
"},{"location":"CMU%2015-445/Chap3/Chap3/#large-values","title":"Large Values","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#external-value-storage","title":"External Value Storage","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#system-catalog","title":"System Catalog","text":"See Slides.
"},{"location":"CMU%2015-445/Chap6/lec/","title":"Problem #2: How the DBMS manages its memory and move data back-and-forth from disk.","text":"Memory region organized as an array of fixed-size pages. An array entry is called a frame. * When the DBMS requests a page, an exact copy is placed into one of these frames. Dirty pages are buffered and notwritten to disk immediately * \u2192 Write-Back Cache
"},{"location":"CMU%2015-445/Chap6/lec/#page-table","title":"Page Table","text":"Hash Table
See Slides
"},{"location":"CMU%2015-445/Chap6/lec/#scan-sharing","title":"Scan Sharing","text":"If a query wants to scan a table and another query is already doing this, then the DBMS will attach the second query's cursor to the existing cursor.
See Slides and Lectures
"},{"location":"CMU%2015-445/Chap6/lec/#buffer-pool-bypass","title":"Buffer Pool Bypass","text":"See Slides
"},{"location":"CMU%2015-445/Chap6/lec/#os-page-cache","title":"OS Page Cache","text":"Most disk operations go through the OS API. Unless you tell it not to, the OS maintains its own filesystem cache (i.e., the page cache)
"},{"location":"CMU%2015-445/Chap6/lec/#replacement-policies","title":"Replacement Policies","text":"When the DBMS needs to free up a frame to make room for a new page, it must decide which page to evict from the buffer pool.
"},{"location":"CMU%2015-445/Chap6/lec/#lru-least-recently-used","title":"LRU : Least Recently Used","text":"Maintain a single timestamp of when each page was last accessed. When the DBMS needs to evict a page, select the one with the oldest timestamp.
LRU and CLOCK replacement policies are susceptible to sequential flooding.
Track the history of last \\(K\\) references to each page as timestamps and compute the interval between subsequent accesses. The DBMS then uses this history to estimate the next time that page is going to be accessed.
"},{"location":"CMU%2015-445/Chap6/lec/#better-policies-localization","title":"Better Policies : Localization","text":"The DBMS chooses which pages to evict on a per txn/query basis. This minimizes the pollution of the buffer pool from each query.
Example: Postgres maintains a small ring buffer that is private to the query.
"},{"location":"CMU%2015-445/Chap6/lec/#better-policies-proority-hints","title":"Better Policies : Proority Hints","text":""},{"location":"CMU%2015-445/Chap6/lec/#dirty-pages","title":"Dirty Pages","text":"Fast Path: If a page in the buffer pool is not dirty, then the DBMS can simply \"drop\" it. Slow Path: If a page is dirty, then the DBMS must write back to disk to ensure that its changes are persisted.
The DBMS can periodically walk through the page table and write dirty pages to disk. When a dirty page is safely written, the DBMS can either evict the page or just unset the dirty flag. Need to be careful that the system doesn't write dirty pages before their log records are written...
"},{"location":"CMU%2015-445/Chap6/lec/#other-memory-pools","title":"Other Memory Pools","text":""},{"location":"CMU%2015-445/Chap8/Chap8/","title":"Chapter 8","text":"Index Types see Inclass Slide.(Chapter 14)
"},{"location":"CMU%2015-445/Chap8/Chap8/#b-tree-indexes","title":"B+ Tree Indexes","text":"A B+Tree is a self-balancing, ordered tree data structure that allows searches, sequential access,Insertions, and deletions in O(log n).
A B+Tree is an M-way search tree with the following properties:
Root can have only 2 branches.
"},{"location":"CMU%2015-445/Chap9/Chap9/","title":"Chap9","text":"a
"},{"location":"CMU%2015-445/ER/lec/","title":"E-R Model","text":""},{"location":"CMU%2015-445/ER/lec/#entity-relationship-model","title":"Entity-Relationship Model","text":""},{"location":"CMU%2015-445/ER/lec/#modeling","title":"Modeling","text":"
\u53cc\u6a2a\u7ebf\u8868\u793a\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u5fc5\u987b\u53c2\u4e0e\u5173\u7cfb\uff0c\u800c\u5355\u6a2a\u7ebf\u5219\u8868\u793a\u5bf9\u8c61\u53ef\u4ee5\u4e0d\u53c2\u4e0e\u5173\u7cfb
-----
====
<--->
one to one
relationship
\u5e26\u5c5e\u6027. : \u5982 takes
\u4e0a\u7684 grade
.
A database can be modeled as:
An entity is an object that exists and is distinguishable from other objects.
An entity set is a set of entities of the same type that share the same
properties.
"},{"location":"CMU%2015-445/ER/lec/#relationship","title":"Relationship","text":""},{"location":"CMU%2015-445/ER/lec/#attributes","title":"Attributes","text":"Entities have attributes.
Attribute types:
Derived(\u6d3e\u751f) attributes
date_of_birth
{phone_number}
multiple phone numbers.
If implemented , have to import using another new entity.
"},{"location":"CMU%2015-445/ER/lec/#mapping-cardinality-constraints","title":"Mapping Cardinality Constraints","text":"We express cardinality constraints by drawing either a directed line \\((\\to)\\), signifying \u201cone,\u201d or an undirected line \\((\u2014)\\), signifying \u201cmany,\u201d between the relationship set and the entity set.
"},{"location":"CMU%2015-445/ER/lec/#one-to-one","title":"One to one","text":""},{"location":"CMU%2015-445/ER/lec/#one-to-many","title":"One to many","text":""},{"location":"CMU%2015-445/ER/lec/#many-to-one","title":"Many to one","text":""},{"location":"CMU%2015-445/ER/lec/#many-to-many","title":"Many to many","text":"proj_guide
to instructor indicates each student has at most one guide for a projectFor Entity Sets.
For Relationship Sets.
An entity set that does not have a primary key is referred to as a weak entity set.
The existence of a weak entity set depends on the existence of a identifying entity set(\u6807\u8bc6\u6027\u5b9e\u4f53\u96c6)
Note: the primary key of the strong entity set is not explicitly stored with the weak entity set, since it is implicit in the identifying relationship.
If course_id
were explicitly stored, section could be made a strong entity, but then the relationship between section and course would be duplicated by an implicit relationship defined by the attribute course_id
common to course and section.
Suppose we have entity sets:
We model the fact that each student has an associated department using a relationship set stud_dept
The attribute dept_name in student below replicates information present in the relationship and is therefore redundant and needs to be removed.
BUT: when converting back to tables, in some cases the attribute gets reintroduced, as we will see later.
"},{"location":"CMU%2015-445/ER/lec/#reduction-to-relation-schemas","title":"Reduction to Relation Schemas","text":"inst_dept(id,name,salary,dept_name, building, budget)\n
instructor(id,name,salary,dept_name)\ndepartment(dept_name,building,budget)\n
instructor(id,name,salary,dept_name,building,budget)
, then dept_name
would be a candidate key.In inst_dept
, dept_name
is not a candidate key.The building
and budget
of a department are not determined by the department name alone,may have to be repeated.
Lossy Decomposition: If we decompose a relation into smaller relations, we may lose information.
Goal : Devise a Theory for the Following
A constraint is a condition that must be true for any instance of the database.
Functional Dependencies are constraints on the set of legal relations.
Let R be a relation schema \\(\\alpha \\in R\\) and \\(\\beta \\in R\\). The functional dependency \\(\\alpha\\to\\beta\\) holds on R if and only if:
A functional dependency is trivial if \\(\\beta\\) is a subset of \\(\\alpha\\).
Given a set F of functional dependencies, there are certain other functional dependencies that are logically implied by F.
Example:
Armstrong\u2019s Axioms
Armstrong\u2019s Axioms are a set of rules that allow us to infer all the functional dependencies that hold on a relation.
The axioms are:
Transitivity: If \\(\\alpha\\) \\(\\to\\) \\(\\beta\\) and \\(\\beta\\) \\(\\to\\) \\(\\gamma\\), then \\(\\alpha\\) \\(\\to\\) \\(\\gamma\\).
These rules are:
Additional Rules (can be derived from Armstrong\u2019s Axioms)
Union: If \\(\\alpha\\) \\(\\to\\) \\(\\beta\\) and \\(\\alpha\\) \\(\\to\\) \\(\\gamma\\), then \\(\\alpha\\) \\(\\to\\) \\(\\beta\\gamma\\).
Decomposition: If \\(\\alpha\\) \\(\\to\\) \\(\\beta\\gamma\\), then \\(\\alpha\\) \\(\\to\\) \\(\\beta\\) and \\(\\alpha\\) \\(\\to\\) \\(\\gamma\\).
Pseudotransitivity: If \\(\\alpha\\) \\(\\to\\) \\(\\beta\\) and \\(\\gamma\\beta\\) \\(\\to\\) \\(\\delta\\), then \\(\\alpha\\gamma\\) \\(\\to\\) \\(\\delta\\).
Given a set of attributes X, the closure of X under F, denoted X+, is the set of all attributes that are functionally determined by X.
Example:
result := alpha;\nwhile (changes to result) do\n for each beta -> gamma in F do\n begin\n if beta in result then result := result Union gamma \n end\n
Intuitively, a canonical cover of F is a \u201cminimal\u201d set of functional dependencies equivalent to F, having no redundant dependencies or redundant parts of dependencies
"},{"location":"CMU%2015-445/Relational/lec/#extraneous-attributes","title":"Extraneous Attributes","text":"Constraints, including functional dependencies, are costly to check in practice unless they pertain to only one relation. If it is sufficient to test only those dependencies on each individual relation of a decomposition in order to ensure that all functional dependencies hold, then that decomposition is dependency preserving * Because it is not always possible to achieve both BCNF and dependency preservation, we consider a weaker normal form, known as third normal form.
Let R be a relation schemma with a set of attributes that are partitioned into 3 nonempty subsets Y,Z,W.
We say that Y->->Z if and only if for all possible relations r(R) \\(\\in\\) r and \\(\\in\\) r then we have \\(\\in\\) r and \\(\\in\\) r * Note:we can also get Y->->W
"},{"location":"CMU%2015-445/Relational/lec/#fourth-normal-forth","title":"Fourth Normal Forth","text":"Example
"},{"location":"CS231n/RNN/lec/","title":"Lecture 8: Recurrent Neural Networks","text":""},{"location":"CS231n/RNN/lec/#vanilla-recurrent-neural-network","title":"(Vanilla) Recurrent Neural Network","text":"The state consists of a single \u201chidden\u201d vector h:
\\(h_t = f_W(h_{t-1},x_t)\\)
\\(h_t = tanh(W_{hh}h_{t-1}+W_{xh}x_t)\\)
\\(y_t = W_{hy}h_t\\)\u200b
So Backward time, we need to sum over because \\(f_W\\) is repeatedly used many times.
"},{"location":"CS231n/RNN/lec/#many-to-many","title":"Many to Many","text":""},{"location":"CS231n/RNN/lec/#many-to-one","title":"Many to One","text":""},{"location":"CS231n/RNN/lec/#one-to-many","title":"One to Many","text":""},{"location":"CS231n/RNN/lec/#sequence-to-sequence-many-to-one-one-to-many","title":"Sequence to Sequence: Many-to-one + one-to-many","text":"Vocabulary: [h,e,l,o]
Example training sequence: \u201chello\u201d
At test-time sample characters one at a time, feed back to model
112
lines withoud using pytorch.\"\"\"\nMinimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy)\nBSD License\n\"\"\"\nimport numpy as np\n\n# data I/O\ndata = open('input.txt', 'r').read() # should be simple plain text file\nchars = list(set(data))\ndata_size, vocab_size = len(data), len(chars)\nprint 'data has %d characters, %d unique.' % (data_size, vocab_size)\nchar_to_ix = { ch:i for i,ch in enumerate(chars) }\nix_to_char = { i:ch for i,ch in enumerate(chars) }\n\n# hyperparameters\nhidden_size = 100 # size of hidden layer of neurons\nseq_length = 25 # number of steps to unroll the RNN for\nlearning_rate = 1e-1\n\n# model parameters\nWxh = np.random.randn(hidden_size, vocab_size)*0.01 # input to hidden\nWhh = np.random.randn(hidden_size, hidden_size)*0.01 # hidden to hidden\nWhy = np.random.randn(vocab_size, hidden_size)*0.01 # hidden to output\nbh = np.zeros((hidden_size, 1)) # hidden bias\nby = np.zeros((vocab_size, 1)) # output bias\n\ndef lossFun(inputs, targets, hprev):\n \"\"\"\n inputs,targets are both list of integers.\n hprev is Hx1 array of initial hidden state\n returns the loss, gradients on model parameters, and last hidden state\n \"\"\"\n xs, hs, ys, ps = {}, {}, {}, {}\n hs[-1] = np.copy(hprev)\n loss = 0\n # forward pass\n for t in xrange(len(inputs)):\n xs[t] = np.zeros((vocab_size,1)) # encode in 1-of-k representation\n xs[t][inputs[t]] = 1\n hs[t] = np.tanh(np.dot(Wxh, xs[t]) + np.dot(Whh, hs[t-1]) + bh) # hidden state\n ys[t] = np.dot(Why, hs[t]) + by # unnormalized log probabilities for next chars\n ps[t] = np.exp(ys[t]) / np.sum(np.exp(ys[t])) # probabilities for next chars\n loss += -np.log(ps[t][targets[t],0]) # softmax (cross-entropy loss)\n # backward pass: compute gradients going backwards\n dWxh, dWhh, dWhy = np.zeros_like(Wxh), np.zeros_like(Whh), np.zeros_like(Why)\n dbh, dby = np.zeros_like(bh), np.zeros_like(by)\n dhnext = np.zeros_like(hs[0])\n for t in reversed(xrange(len(inputs))):\n dy = np.copy(ps[t])\n dy[targets[t]] -= 1 # backprop into y. see http://cs231n.github.io/neural-networks-case-study/#grad if confused here\n dWhy += np.dot(dy, hs[t].T)\n dby += dy\n dh = np.dot(Why.T, dy) + dhnext # backprop into h\n dhraw = (1 - hs[t] * hs[t]) * dh # backprop through tanh nonlinearity\n dbh += dhraw\n dWxh += np.dot(dhraw, xs[t].T)\n dWhh += np.dot(dhraw, hs[t-1].T)\n dhnext = np.dot(Whh.T, dhraw)\n for dparam in [dWxh, dWhh, dWhy, dbh, dby]:\n np.clip(dparam, -5, 5, out=dparam) # clip to mitigate exploding gradients\n return loss, dWxh, dWhh, dWhy, dbh, dby, hs[len(inputs)-1]\n\ndef sample(h, seed_ix, n):\n \"\"\" \n sample a sequence of integers from the model \n h is memory state, seed_ix is seed letter for first time step\n \"\"\"\n x = np.zeros((vocab_size, 1))\n x[seed_ix] = 1\n ixes = []\n for t in xrange(n):\n h = np.tanh(np.dot(Wxh, x) + np.dot(Whh, h) + bh)\n y = np.dot(Why, h) + by\n p = np.exp(y) / np.sum(np.exp(y))\n ix = np.random.choice(range(vocab_size), p=p.ravel())\n x = np.zeros((vocab_size, 1))\n x[ix] = 1\n ixes.append(ix)\n return ixes\n\nn, p = 0, 0\nmWxh, mWhh, mWhy = np.zeros_like(Wxh), np.zeros_like(Whh), np.zeros_like(Why)\nmbh, mby = np.zeros_like(bh), np.zeros_like(by) # memory variables for Adagrad\nsmooth_loss = -np.log(1.0/vocab_size)*seq_length # loss at iteration 0\nwhile True:\n # prepare inputs (we're sweeping from left to right in steps seq_length long)\n if p+seq_length+1 >= len(data) or n == 0: \n hprev = np.zeros((hidden_size,1)) # reset RNN memory\n p = 0 # go from start of data\n inputs = [char_to_ix[ch] for ch in data[p:p+seq_length]]\n targets = [char_to_ix[ch] for ch in data[p+1:p+seq_length+1]]\n\n # sample from the model now and then\n if n % 100 == 0:\n sample_ix = sample(hprev, inputs[0], 200)\n txt = ''.join(ix_to_char[ix] for ix in sample_ix)\n print '----\\n %s \\n----' % (txt, )\n\n # forward seq_length characters through the net and fetch gradient\n loss, dWxh, dWhh, dWhy, dbh, dby, hprev = lossFun(inputs, targets, hprev)\n smooth_loss = smooth_loss * 0.999 + loss * 0.001\n if n % 100 == 0: print 'iter %d, loss: %f' % (n, smooth_loss) # print progress\n\n # perform parameter update with Adagrad\n for param, dparam, mem in zip([Wxh, Whh, Why, bh, by], \n [dWxh, dWhh, dWhy, dbh, dby], \n [mWxh, mWhh, mWhy, mbh, mby]):\n mem += dparam * dparam\n param += -learning_rate * dparam / np.sqrt(mem + 1e-8) # adagrad update\n\n p += seq_length # move data pointer\n n += 1 # iteration counter \n
"},{"location":"CS231n/RNN/lec/#rnn-tradeoffs","title":"RNN Tradeoffs","text":"RNN Advantages:
RNN Disadvantages:
Multiply \\(W_{hh}\\)\u200b many times ! -- Really Bad !
Largest singular value > 1:Exploding gradients
Gradient Clipping : scale it if the norm is too big -- exploding gradients
grad_norm = np.sum(grad*grad)\nif grad_norm > threshold:\n grad*=(threshold/gram_norm)\n
Change RNN architecture !
"},{"location":"CS231n/RNN/lec/#long-short-term-memory-lstm_1","title":"Long Short Term Memory (LSTM)","text":""},{"location":"CS231n/RNN/lec/#gradient-flow","title":"Gradient Flow","text":"Do LSTMs solve the vanishing gradient problem?
The LSTM architecture makes it easier for the RNN to preserve information over many timesteps
- e.g. if the f = 1 and the i = 0, then the information of that cell is preserved indefinitely.
- By contrast, it\u2019s harder for vanilla RNN to learn a recurrent weight matrix \\(W_h\\) that preserves info in hidden state
LSTM doesn\u2019t guarantee that there is no vanishing/exploding gradient, but it does provide an easier way for the model to learn long-distance dependencies
"},{"location":"CS231n/lec10/lec/","title":"Video Understanding","text":""},{"location":"CS231n/lec10/lec/#late-fusion-approach","title":"Late Fusion Approach","text":"FC layers may result in overfitting
"},{"location":"CS231n/lec10/lec/#early-fusion-approach","title":"Early Fusion Approach","text":"
* One layer of temporal processing may not be enough!
"},{"location":"CS231n/lec10/lec/#comparison","title":"Comparison","text":""},{"location":"CS231n/lec10/lec/#general-comparison","title":"General Comparison","text":""},{"location":"CS231n/lec10/lec/#2d-conv-early-fusion-vs-3d-conv-3d-cnn","title":"2D Conv (Early Fusion) vs 3D Conv (3D CNN)","text":"Details see Slides. * 2D : perceptive of features of time at once and slide over the space feature. No temporal shift-invariance! Needs to learn separate filters for the same motion at different times in the clip.
* Sometimes don\u2019t backprop to CNN to save memory; pretrain and use it as a feature extractor : Like using pretrained C3D as a feature extractor.
"},{"location":"CS231n/lec10/lec/#recurrent-convolutional-network","title":"Recurrent Convolutional Network","text":"Actually not too much used because of the time complexity.(Not good for parallelization) Sequential processing is not good for parallelization.
"},{"location":"CS231n/lec10/lec/#spatio-temporal-self-attention-non-local-block","title":"Spatio-Temporal Self-Attention (Non Local Block)","text":"Trick:Add non-local blocks to 3D CNNs , initilize the weights of the non-local block with all zeros, and fine-tune the network.
"},{"location":"CS231n/lec10/lec/#inflating-2d-networks-to-3d-i3d","title":"Inflating 2D Networks to 3D (I3D)","text":"Refer to the slides for details. Trick: Pretrain 2D CNNs and inflate them to 3D CNNs by repeating the weights along the temporal dimension.
"},{"location":"CS231n/lec12/lec/","title":"Visualizing and Understanding","text":""},{"location":"CS231n/lec12/lec/#visualizing-what-models-have-learned","title":"Visualizing what models have learned:","text":""},{"location":"CS231n/lec12/lec/#visualizing-filters","title":"Visualizing filters","text":""},{"location":"CS231n/lec12/lec/#visualizing-final-layer-features","title":"Visualizing final layer features","text":"PCA
t-SNE
Like Train A Best Image
\\(I^* = argmax_I f(I)+R(I)\\)
\\(argmax_IS_c(I)-\\lambda||I||_2^2\\)
Simple regularizer: Penalize L2 norm of generated image
Better regularizer: Penalize L2 norm of image , also during optimization periodically
(1) Gaussian blur image
(2) Clip pixels with small values to 0
(3) Clip pixels with small gradients to 0
"},{"location":"CS231n/lec12/lec/#adversarial-perturbations","title":"Adversarial perturbations","text":""},{"location":"CS231n/lec12/lec/#style-transfer","title":"Style transfer","text":""},{"location":"CS231n/lec12/lec/#features-inversion","title":"Features inversion","text":"Given a CNN feature vector for an image, find a new image that:
https://github.com/google/deepdream
"},{"location":"CS231n/lec12/lec/#texture-synthesis","title":"Texture synthesis","text":"See Slides
"},{"location":"CS231n/lec12/lec/#neural-style-transfe","title":"Neural style transfe","text":"Average over all pairs of vectors, giving Gram matrix of shape C x C
Efficient to compute; reshape features from CxHxW to =CxHW then compute G = FFT
Details see Slides.
Problem: Style transfer requires many forward / backward passes through VGG; very slow!
Solution: Train another neural network to perform style transfer for us!
https://github.com/jcjohnson/fast-neural-style
Instance Normalization was developed for style transfer!
Network only for a single style.
a
"},{"location":"CS231n/lec14/lec/","title":"Lecture 14 Robot Learning","text":"a
"},{"location":"CS231n/lec15/lec/","title":"Generative Models","text":"Supervised vs Unsupervised Learning
Refer to Slides.
"},{"location":"CS231n/lec15/lec/#discriminative-vs-generative-models","title":"Discriminative vs Generative Models","text":"Details see slides.
Discriminative Model: Learn a probability distribution \\(p(y|x)\\)
Assign labels to data
Feature learning (with labels)
Shortcomes
Generative Model: Learn a probability distribution \\(p(x)\\)\u200b
Detect outliers
Feature learning (without labels)
Sample to generate new data
Conditional Generative Model: Learn \\(p(x|y)\\)
Assign labels, while rejecting outliers!
Generate new data conditioned on input labels
\\(p(x|y)=\\frac{p(y|x)}{p(y)}p(x)\\)
We can build a conditional generative model from other components!
\u300a\u7edf\u8ba1\u5b66\u4e60\u65b9\u6cd5\u300b
,\u5c31\u662f\u4f18\u5316\u6781\u5927\u4f3c\u7136\u51fd\u6570.\\(\\begin{align*}p(x) &= p(x_1,x_2,x_3,...x_T)\\\\ &=p(x_1)p(x_2|x_1)p(x_3|x_1,x_2)....\\\\&=\\Pi_{t=1}^Tp(x_t|x_1,...x_{t-1})\\end{align*}\\)
https://arxiv.org/pdf/1601.06759.pdf
Diagnal Order
.Pros:
Can explicitly compute likelihood \\(p(x)\\)
Explicit likelihood of training data gives good evaluation metric
Good samples
Con:
Improving PixelCNN performance
Variational Autoencoders (VAE) define an intractable density that we cannot explicitly compute or optimize.
But we will be able to directly optimize a lower bound on the density !
"},{"location":"CS231n/lec15/lec/#regular-non-variational-autoencoders","title":"(Regular, non-variational) Autoencoders","text":"Unsupervised method for learning feature vectors from raw data x, without any labels
Problem: How can we learn this feature transform from raw data?
Idea: Use the features to reconstruct the input data with a decoder \u201cAutoencoding\u201d = encoding itself
Somehow Compress the input data.
After training, throw away decoder.
Encoder can be used to initialize a supervised model.
Autoencoders learn latent features for data without any labels! Can use features to initialize a supervised model
z for each x
, then could train a conditional generative model p(x|z)
Basic idea: maximize likelihood of data
Problem: Impossible to integrate over all z!
Problem: No way to compute this! \\(p_{\\theta}(z|x)\\)
Solution: Train another network (encoder) that learns \\(q_{\\Phi}(z|x)\\approx p_{\\theta}(z|x)\\)
Use encoder to compute \\(q_{\\Phi}(z|x)\\approx p_{\\theta}(z|x)\\)
"},{"location":"CS231n/lec9/lec/","title":"Attention and Transformers","text":"Log on probability : Preserve precision :
Problem: Input sequence bottlenecked through fixed-sized vector. What if T=1000?
"},{"location":"CS231n/lec9/lec/#attention","title":"Attention","text":""},{"location":"CS231n/lec9/lec/#sequence-to-sequence-with-rnns-and-attention","title":"Sequence to Sequence with RNNs and Attention","text":"Use \\(s_1\\) to compute new context vector \\(c_2\\)
Use \\(c_2\\) to compute \\(s_2\\), \\(y_2\\)
"},{"location":"CS231n/lec9/lec/#example","title":"Example","text":"Input: Image I
Output: Sequence y = \\(y_1, y_2,..., y_T\\)
Encoder: \\(h_0\\) = \\(f_W(z)\\) where z is spatial CNN features \\(f_W(.)\\) is an MLP
Decoder: \\(y_t = g_V(y_{t-1},h_{t-1}, c)\\) where context vector c is often \\(c = h_0\\)
"},{"location":"CS231n/lec9/lec/#image-captioning-with-rnns-and-attention","title":"Image Captioning with RNNs and Attention","text":"Attention idea: New context vector at every time step.Each context vector will attend to different image regions
This entire process is differentiable:
Each timestep of decoder uses a different context vector that looks at different parts of the input image.
"},{"location":"CS231n/lec9/lec/#attention-layer","title":"Attention Layer","text":"Attention operation is permutation invariant. - Doesn't care about ordering of the features - Stretch H x W = N into N vectors
Change \\(f_{att}\\) to a simple dot product - only works well with key & value transformation trick (will mention in a few slides)
Change \\(f_{att}(.)\\) to a scaled simple dot product - Larger dimensions means more terms in the dot product sum. - So, the variance of the logits is higher. Large magnitude vectors will produce much higher logits. - So, the post-softmax distribution has lower-entropy, assuming logits are IID. - Ultimately, these large magnitude vectors will cause softmax to peak and assign very little weight to all others - Divide by \u221aD to reduce effect of large magnitude vectors
Notice that the input vectors are used for both the alignment as well as the attention calculations. - We can add more expressivity to the layer by adding a different FC layer before each of the two steps. * Self Attention Layer does not CARE about the order of the input vectors! * Use positional encoding to give the model some notion of order.
Desiderata of pos(.) :
"},{"location":"CV/ML-4360/BS/lec2/#lec2-for-ml-4360","title":"Lec2 for ML-4360","text":""},{"location":"CV/ML-4360/BS/lec2/#21-primitives-transformations","title":"2.1 Primitives & Transformations","text":""},{"location":"CV/ML-4360/BS/lec2/#homogeneous","title":"\\(Homogeneous\\)","text":"
\\(\\begin{bmatrix}I&t\\\\0^{T}&1\\end{bmatrix}\\)
"},{"location":"CV/ML-4360/BS/lec2/#euclidean-3dof","title":"Euclidean 3DOF","text":"\\(RR^T=I \\ der(R)=1\\)\u200b
\\(\\begin{bmatrix}R&t\\\\0^{T}&1\\end{bmatrix}\\)
\u6b63\u4ea4\u77e9\u9635\\(A^TA=I\\)
\\(\\begin{bmatrix}sR&t\\\\0^{T}&1\\end{bmatrix}\\)\u200b
\\(RR^{T}=I\\)
"},{"location":"CV/ML-4360/BS/lec2/#affine-6dof","title":"Affine: 6DOF","text":"\\(\\begin{bmatrix}A&t\\\\0^{T}&1\\end{bmatrix}\\)
arbitrary \\(2\u00d72\\) matrix
Parallels Remain!
An orthographic projection simply drops the z component of the 3D point in camera
coordinates \\(x_c\\) to obtain the corresponding 2D point on the image plane (= screen) \\(x_s\\)
\\(\\bar{x_s}=\\begin{bmatrix}s&0&0&0\\\\0&s&0&0\\\\0&0&0&1\\end{bmatrix}\\).
"},{"location":"CV/ML-4360/BS/lec2/#perspective-projection","title":"Perspective projection","text":""},{"location":"CV/ML-4360/BS/lec2/#chaining-transformations","title":"Chaining Transformations","text":"\\(\\tilde{x}_s=K[R \\ t]\\bar{x}_w\\)
\u6211\u7684\u7406\u89e3\uff1a\\(\\bar{x}_s\\)\u662f\u771f\u5b9e\u7684\u76f8\u673a\u5750\u6807\u7cfb\u4e0b\u7684\u5750\u6807\uff0c\u4e0e\\(\\tilde{x}_s\\)\u5dee\u4e00\u4e2a\u7cfb\u6570\uff0c\u5982\u679c\u6211\u4eec\u77e5\u9053\u8fd9\u4e2a\u7cfb\u6570\uff0c\u5c31\u53ef\u4ee5\u6062\u590d\u51fa\\(\\tilde{x}_s\\)\u5e76\u6839\u636e\\(\\tilde{x}_s\\)\u7684\u8ba1\u7b97\u65b9\u6cd5\u6062\u590d\u51fa\u4e16\u754c\u5750\u6807\u7cfb\u4e0b\u7684\u5750\u6807\\(\\bar{x}_c\\)
"},{"location":"CV/ML-4360/BS/lec2/#23-photometric-image-formation","title":"2.3 Photometric Image Formation","text":""},{"location":"CV/ML-4360/BS/lec2/#rendering-equation","title":"\\(Rendering\\ Equation\\)","text":"\\(dw=\\sin\\theta\\)\\(d\\theta\\)\\(d\\phi\\)
E(x)=d\\(\\Phi(x)\\)/dA
\\(L(p,w)=d^2\\Phi(p,\\omega)\\)/\\(d\\omega\\)\\(dAcos\\theta\\)
\\(L_r(p,w_r)=\\int_{H^2}f_r(p,w_i\\rightarrow w_r)L_i(p,w_i)cos\\theta_id_{w_i}\\)
"},{"location":"CV/ML-4360/BS/lec2/#back-to-cameras","title":"Back To Cameras","text":""},{"location":"CV/ML-4360/CBN/lec9/","title":"Coordinate-Based Networks","text":""},{"location":"CV/ML-4360/CBN/lec9/#implicit-neural-representations","title":"Implicit Neural Representations","text":""},{"location":"CV/ML-4360/CBN/lec9/#what-is-a-good-output-representation","title":"What is a good output representation?","text":"
Flat surface only in the canonical directions
No surface
#### Occupancy Networks
Also possible: signed distance field [Park et al., 2019]
\\(L(\\theta,\u03c8) = \\sum_{j=1}^KBCE(f_\u03b8(p_{ij},z_{i}),o_{ij})+KL[q_\u03c8(z|(p_{ij},o_{ij})_{j=1:K})\u2225p0(z)]\\)\u200b
Conditional Surface Light Field
Given any light & view direction, we want to know the color
"},{"location":"CV/ML-4360/CBN/lec9/#representing-motion","title":"Representing Motion","text":"Problems for occupancy network
Implicit models work well for simple objects but poorly on complex scenes
"},{"location":"CV/ML-4360/CBN/lec9/#convolutional-occupancy-networks","title":"Convolutional Occupancy Networks","text":""},{"location":"CV/ML-4360/CBN/lec9/#differentiable-volumetric-rendering","title":"Differentiable Volumetric Rendering","text":""},{"location":"CV/ML-4360/CBN/lec9/#learning-from-images","title":"Learning from Images","text":"Secant Method : A finite-difference approximation of Newton\u2019s method.
"},{"location":"CV/ML-4360/CBN/lec9/#backward-passdifferentiation","title":"Backward Pass(Differentiation)","text":"Comes to Backward Pass
Image Observation I
Loss \\(L(\\hat{I},I) = \\sum_u||\\hat{I}_u-I_u||\\)\u200b
Gradient of Loss Function
\\(\\frac{\\partial{L}}{\\partial{\\theta}} = \\sum_u \\frac{\\partial L}{\\partial\\hat{I_u}}\u00b7\\frac{\\partial{\\hat{I_u}}}{\\partial\\theta}\\)\u200b
\\(\\frac{\\partial{\\hat{I_u}}}{\\partial\\theta}=\\frac{\\partial{t_{\\theta}(\\hat{p})}}{\\partial\\theta} +\\frac{\\partial{t_{\\theta}(\\hat{p})}}{\\partial\\hat{p}}\u00b7\\frac{\\partial{\\hat{p}}}{\\partial\\theta}\\)\u200b
Note : \\(\\hat{p}\\) (The Surface Intersection) also depends on \\(\\theta\\)\u200b !!
So we have to apply total derivative !!
Differentiation of \\(f_{\\theta}(\\hat{p}=\\tau)\\) yields :
\\(\\frac{\\partial\\hat{p}}{\\partial\\theta}=-w(\\frac{\\partial f_{\\theta}(\\hat{p})}{\\partial\\hat{p}}\u00b7w)^{-1}\\frac{\\partial f_{\\theta}(\\hat{p})}{\\partial\\theta}\\)\u200b\u200b
Derivation :
Note \\(\\bold{w}\\) is a vector!
"},{"location":"CV/ML-4360/CBN/lec9/#neural-radiance-fields","title":"Neural Radiance Fields","text":"Task: Given a set of images of a scene, render image from novel viewpoint
\\(min_{\\theta}=\\sum_i||render_i(F_{\\theta})-I_i||^2\\)\u200b
Shoot ray, render ray to pixel, minimize reconstruction error via backpropagation
From the Paper :
We encourage the representation to be multiview consistent by restricting the network to predict the volume density \u03c3 as a function of only the location x, while allowing the RGB color c to be predicted as a function of both location and viewing direction.
To accomplish this, the MLP \\(F_\u0398\\) first processes the input 3D coordinate x with 8 fully-connected layers (using ReLU activations and 256 channels per layer), and outputs \u03c3 and a 256-dimensional feature vector.
This feature vector is then concatenated with the camera ray\u2019s viewing direction and passed to one additional fully-connected layer (using a ReLU activation and 128 channels) that output the view-dependent RGB color.
"},{"location":"CV/ML-4360/CBN/lec9/#volume-rendering-with-radiance-fields","title":"Volume Rendering with Radiance Fields","text":"From the Paper :
The volume density \\(\u03c3(x)\\) can be interpreted as the differential probability of a ray terminating at an infinitesimal particle at location \\(x\\)\u200b.
The expected color \\(C(r)\\) of camera ray \\(r(t) = o + td\\) with near and far bounds \\(t_n\\) and \\(t_f\\) is:
Having the network \\(F_\u0398\\) directly operate on \\(xyz\u03b8\u03c6\\)\u200b input coordinates results in renderings that perform poorly at representing high-frequency variation in color and geometry
(Recap Transformers)
"},{"location":"CV/ML-4360/PGM/lec5/#probabilistic-graphical-models","title":"Probabilistic Graphical Models","text":""},{"location":"CV/ML-4360/PGM/lec5/#structured-prediction","title":"Structured Prediction","text":""},{"location":"CV/ML-4360/PGM/lec5/#spatial-regularization","title":"Spatial regularization","text":"
\\(p(D)\\propto e^{-\\sum_i\\phi_{data}(d_i)-\\lambda\\sum_{(i\uff0cj)\\in S}\\phi_{smooth}(d_i,d_j)}\\)
\\(i\\)~\\(j\\) neighbouring pixels (on a 4-connected grid).
$\\phi_{smooth} $ is a regularization term that encourages neighboring pixels to have similar disparities.
where \\(I\\)and \\(J\\) are the image pairs. \\(\u03c3\\) and \\(\u03c4\\) are truncation thresholds.
Probabilistic graphical models encode local dependencies of the problem
Deep neural netwoks with image-based outputs (stereo, flow, semantics)
"},{"location":"CV/ML-4360/PGM/lec5/#markov-random-fields","title":"Markov Random Fields","text":""},{"location":"CV/ML-4360/PGM/lec5/#undirected-graphical-models-ugm","title":"Undirected graphical models (UGM)","text":"Refers to fully connected subgraphs in a graphical model, particularly in models like Markov Random Fields or Conditional Random Fields.
In this context, a clique is a group of nodes in a graph where every pair of nodes is directly connected.
"},{"location":"CV/ML-4360/PGM/lec5/#potential","title":"potential","text":"\\(P(x_1\u2026\u2026x_n)=\\frac{1}{Z}\\prod_{c\\in C}\\phi_c(x_c)\\)
\\(Z = \\sum_{x_1\u2026\u2026x_n}\\prod_{c\\in C}\\phi_c(x_c)\\)
"},{"location":"CV/ML-4360/PGM/lec5/#defination-of-markov-random-field","title":"Defination of Markov Random Field","text":"\\(p(X)=\\frac{1}{Z}\\prod_{k=1}^K\\phi_k(X_k)\\)
Examples\uff1a
"},{"location":"CV/ML-4360/PGM/lec5/#properties","title":"Properties","text":"\\(Condition \\ One\\)
"},{"location":"CV/ML-4360/PGM/lec5/#marginalizing-over-c-makes-a-and-b-dependent","title":"Marginalizing over c makes a and b dependent","text":"\\(Proof\\)
\\(Proof\\)
"},{"location":"CV/ML-4360/PGM/lec5/#global-markov-property","title":"Global Markov Property","text":""},{"location":"CV/ML-4360/PGM/lec5/#local-markov-property","title":"Local Markov Property","text":"A probability distribution that has a strictly positive mass or density satisfies the Markov properties with respect to an undirected graph G if and only if it is a Gibbs random field,
i.e. its density can be factorized over the (maximal) cliques of the graph.
"},{"location":"CV/ML-4360/PGM/lec5/#factor-graphs","title":"Factor Graphs","text":"\\(p(X) = \\frac{1}{Z}\\prod_{k=1}^Kf_k(X_k)_{k=1}^K\\)\u200b
"},{"location":"CV/ML-4360/PGM/lec5/#example","title":"Example","text":""},{"location":"CV/ML-4360/PGM/lec5/#belief-propagation","title":"Belief Propagation","text":""},{"location":"CV/ML-4360/PGM/lec5/#inference-in-chain-structured-factor-graphs","title":"Inference in Chain Structured Factor Graphs","text":"\\(p(a, b, c, d) = \\frac{1}{Z}f_1(a, b)f_2(b, c)f_3(c, d)f4(d)\\)
\\(p(a,b,c) = \\sum_{d}p(a,b,c,d)\\)
\\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ = \\frac{1}{Z}f_1(a,b)f_2(b,c)\\underbrace{\\sum_{d}f_3(c,d)f_4(d)}_{\u03bc_{d\u2192c}(c)}\\)
\\(p(a,b) = \\sum_{c}p(a,b,c)\\)
\\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ = \\frac{1}{Z}f_1(a,b)\\underbrace{\\sum_{c}f_2(b,c)\u03bc_{d\u2192c}(c)}_{\u03bc_{c\u2192b}(b)}\\)
\\(\u2026\u2026\\)
Factor-to-Variable Messages
Variable-to-Factor Messages
"},{"location":"CV/ML-4360/PGM/lec5/#sum-product-algorithm","title":"Sum-Product Algorithm","text":""},{"location":"CV/ML-4360/PGM/lec5/#belief-propagation_1","title":"Belief Propagation:","text":"\\(\\begin{align*}\\underset{\\text{a,b,c,d}}{\\text{max}}p(a,b,c,d)&= \\underset{\\text{a,b,c,d}}{\\text{max}}f_1(a,b)f2(b,c)f_3(c,d)\\\\&=\\underset{\\text{a,b,c}}{\\text{max}}f_1(a,b)f2(b,c)\\underbrace{\\underset{\\text{d}}{\\text{max}}f_3(c,d)}_{\\mu_{d->c}(c)}\\\\&=\u2026\u2026\\\\ &=\\underset{\\text{a}}{\\text{max}}\\mu_{b->a}(a)\\end{align*}\\)
"},{"location":"CV/ML-4360/PGM/lec5/#loopy-belief-propagation","title":"Loopy Belief Propagation","text":"Max-Product Belief Propagation on chain structured Markov Random Fields for Vehicle Localization
Let's consider an autonomous vehicle driving on a highway and tracking a vehicle in front in order to initiate an overtaking maneuver. Let \\(x_t\\in\\{1,2,3\\}\\) denote the lane the vehicle in front is driving on at time \\(t\\in\\{1,\\dots,10\\}\\). Unfortunately, the sensor readings are noisy as depicted below.
Selecting the most likely lane at each time \\(t\\) independently (green) leads to wrong estimates for \\(t=\\{3,7,10\\}\\). To solve this problem, and recover the correct situation depicted below
we can integrate prior knowledge and infer the most likely situation using max-product belief propagation. A sensible prior would favor staying on the same lane over changing one lane at a time over changing two lanes at a time. This prior can be integrated via a pairwise, chain-structured Markov Random Field (also called: Hidden Markov Model or HMM) where pairwise factors between adjacent frames modulate transition likelihoods:
\\[g_\\theta(x_i,x_{i+1}) = \\begin{bmatrix}0.8 & 0.2 & 0.0\\\\ 0.2 & 0.6 & 0.2 \\\\ 0.0 & 0.2 & 0.8\\end{bmatrix}\\] \\[ p_\\theta({\\bf x}) \\propto \\prod_{1}^{10} f_i(x_i)\\prod_{1}^{9}g_{\\theta}(x_i, x_{i+1})\\]You are given a noisy binary image (\\(10 \\times 10\\) pixels) which you want to denoise.
Make use of the Ising model for that purpose where neighboring pixels are encouraged to take the same value: \\(\\(p(x_1,\\dots,x_{100}) \\propto \\exp \\left\\{\\sum_{i=1}^{100} \\psi_i(x_i) + \\sum_{i\\sim j} \\psi_{ij} (x_i,x_j) \\right\\}\\)\\)
Here, \\(i\\) is the pixel index and \\(i\\sim j\\) are neighboring pixels on a 4-connected grid. The unary term \\(\\psi_i(x_i) = [x_i = o_i]\\) models the observation at pixel \\(i\\), and the pairwise term is the Ising prior \\(\\psi_{ij}(x_i,x_j) = \\alpha \\cdot [x_i = x_j]\\), where \\(\\alpha\\) controls the strength of the interaction/smoothing.
Because we have large number of variables in this exercise, we use logarithm factors to avoid potential numerical underflow issues.
Inputs: * num_vars
, num_states
, factors
, msg_fv
, msg_vf
, ne_var
Outputs: * max_marginals
: num_vars
x num_states
array of estimated max-marginals * map_est
: array comprising the estimated MAP state of each variable
Algorithm Pseudocode:
N=30
iterations do:Update all variable-to-factor messages: \\(\\lambda_{x\\rightarrow f}(x) = \\sum_{g\\in\\{ ne(x)\\setminus f\\}}\\lambda_{g\\rightarrow x}(x)\\)
Calculate Max-Marginals: \\(\\gamma_x(x) = \\sum_{g\\in\\{ ne(x)\\}}\\lambda_{g\\rightarrow x}(x)\\)
To minimize the ENERGY
Add Truncated penalty
\\(\\lambda\\)\u200b -- \"tradeoff\"
Very LOCAL pairwise terms cannot deal with some cases (like reflections)
Block matching suffers from ambiguities
Choosing window size is problematic (tradeoff)
Incorporating smoothness constraints can resolve some of the ambiguities and allows for choosing small windows (no bleeding artifacts)
Can be formulated as MAP inference in a discrete MRF
Representation
Voxel occupancy: This concept describes whether a voxel is occupied by a solid entity. In many tasks such as 3D reconstruction, object detection, SLAM (Simultaneous Localization and Mapping), etc., voxel occupancy is crucial. Typically, a voxel is either occupied by a solid entity or empty (occupied by other objects or background in space). In representations of voxel occupancy, a common method is binary, where a voxel is considered occupied when it's 1 and empty otherwise.
Voxel appearance: This concept describes the visual properties of a voxel, i.e., how it appears in images or voxel grids. It may include color, texture, brightness, etc.
Image Formation Process
Probabilistic Model
Joint Distribution: \\(p(\\bold{O},\\bold{A})=\\frac{1}{Z}\\Pi_{v\\in\\bold{V}}\u03c6_v(o_v)\\Pi_{r\\in\\bold{R}}\u03c8_r(o_r, a_r)\\)\u200b
Unary Potentails : \\(\u03c6_v(o_v) = \u03b3^{o_v} (1 \u2212 \u03b3)^{(1\u2212o_v)}\\)
Most voxels are empty \u21d2 \\(\u03b3\\)\u200b < 0.5
Ray Potentials :
If first occupied voxel \\(a_i^r\\) similar to the corresponding pixel , \\(\u03c8(o_r,a_r)\\) will increase
Depth Distribution For Single Ray
Message from and to the unary factors \\(\\mu_{\u03c6_i\\to o_i}\\) \\(\\mu_{o_i\\to \u03c6_i}\\) and \\(\\mu_{o_i\\to \u03c8_i}\\) can be easilly computed.
Occupancy message
\u200b Thus, resulting in [DERIVATION SEE PAPER BELOW]
\u200b \\(\\mu_{\u03c8\\to o_1}(o_1=1)=\\int_{a_1}v(a_1)\\mu(a_1)da_1\\)\u200b
\u200b \\(\\mu_{\u03c8\\to o_1}(o_1=0)=\\sum_{j=2}^N\\mu(o_j=1)\\Pi_{k=2}^{j-1}\\mu(o_k=0)\\rho_j\\)
\u200b \\(\\rho_j=\\int _{a_j}v(a_j)u(a_j)da_j\\)
\u200b More General :[see PAPER]
Bayes Optimal Depth Estimation
Depth \\(D\u2208\\{d_1,...,d_N\\}\\):distance to closest occupied voxel
Optimal depth estimate:
\\(p(D=d_i)\u221d\\mu(o_i=1)\\Pi_{j=1}^{i-1}\\mu(o_j=0)\\rho_i\\) [Derivation see PPT & Paper]
Requires marginal depth distribution \\(p(D)\\) along each ray
Motion field:
Optical flow:
Solution: linearize the brightness constancy assumption
\\(f(x,y)\u2248f(a,b)+ \\frac{\\partial f(a,b)}{\u2202x} (x\u2212a)+ \\frac{\\partial f(a,b)}{\u2202y} (y\u2212b)\\)\u200b
Thus , we have \\(I(x + u(x, y), y + v(x, y), t + 1) \\approx I(x,y,t)+I_x(x,y,t)u(x,y)+I_y(x,y,t)v(x,y)+I_t(x,y,t)\\)
\\(E(u,v) \\approx \\iint [(I_x(x,y,t)u(x,y)+I_y(x,y,t)v(x,y)+I_t(x,y,t))^2+\\lambda(||\\triangledown u(x,y||)^2+||\\triangledown v(x,y||)^2]dxdy\\)
which leads to the following discretized objective:
The text points out the invalidity of two assumptions. Firstly, the assumption of having a smooth probability distribution in the image at object boundaries is invalid because object boundaries typically exhibit discontinuities, whereas Gaussian distributions assume continuity.
Gaussian distributions correspond to quadratic loss functions. This means that using Gaussian distributions to model smoothness is equivalent to using squared terms in the loss function, which is somewhat sensitive to outliers.
Squared loss functions are not sufficiently robust to outliers. This means that in the presence of outliers, using squared loss functions may lead to larger errors, as they heavily influence the process of minimizing the error.
Outliers typically occur at object boundaries, violating the assumptions of smoothness and regularization. Object boundaries usually have sharp transitions, so outliers may occur in these areas.
Outliers may also occur at specular highlights, violating the assumptions of photoconsistency and data terms. Specular highlights often result in very high brightness or intensity in image regions, which may differ significantly from the surrounding areas, thus being considered outliers.
"},{"location":"CV/ML-4360/PGM/lec5/#learning-in-graphical-models","title":"Learning in Graphical Models","text":""},{"location":"CV/ML-4360/PGM/lec5/#conditional-random-fields","title":"Conditional Random Fields","text":"
\\(p(x_1,...,x_{100})= \\frac{1}{\\bold{Z}}exp\\{\\sum_i\u03c8_i(x_i)+\u03bb\\sum_{i\\to j}\u03c8_{ij}(x_i,x_j)\\}\\) * How to estimate the parameters , say \\(\\lambda\\)
"},{"location":"CV/ML-4360/PGM/lec5/#parameter-estimation","title":"Parameter Estimation","text":""},{"location":"CV/ML-4360/Recognition/lec10/#object-detection","title":"Object Detection","text":"
Input: Single RGB Image
Output: A set of detected objects:
Challenges
Apply a CNN to many different crops of the image, CNN classifies each crop as object or background
We should consider all possible sizes and possible positions
Solutions
Region Proposals
Use MultiTask Loss
Bbox : Modify the Region Proposals
\\(L_2\\) regularization encourages leaving proposal unchanged.
Transform encodes relative difference between proposal and output; important since \\(CNN\\) doesn\u2019t see absolute size or position after cropping
"},{"location":"CV/ML-4360/Recognition/lec10/#r-cnn-training","title":"R-CNN Training","text":"Positive: > 0.5 IoU with a GT box
Negative: < 0.3 IoU with all GT boxes
Neutral: between 0.3 and 0.5 IoU with GT boxes
Problem: Object detectors often output many overlapping detections:
Solution: Post-process raw detections using Non-Max Suppression (NMS)
Select next highest-scoring box
Eliminate lower-scoring boxes with IoU > threshold (e.g. 0.7)
If any boxes remain, GOTO 1
Problem: NMS may eliminate \u201dgood\u201d boxes when objects are highly overlapping...
no good solution =(
R-CNN Problem: Very slow! Need to do 2000 forward passes through CNN per image
Idea: Overlapping proposals cause a lot of repeated work: same pixels processed many times. Can we avoid this?
Per-Region network is relatively lightweight
Most of the computation happens in backbone network;
this saves work for overlapping region proposals.
How to crop features?
"},{"location":"CV/ML-4360/Recognition/lec10/#cropping-features-roi-pool","title":"Cropping Features: RoI Pool","text":"Classic idea: build an image pyramid by resizing the image to different scales, then process each image scale independently.
Problem: Expensive! Don\u2019t share any computation between scales.
Simple Idea : Sliding Window
Use Fully Convolutional Network
Dose not have any pooling layers or fully connected layers.
Problem #1: Effective receptive field size is linear in number of conv layers:
Let's say we have L 3x3 convolutional layers. In a CNN, each neuron in a convolutional layer is connected to a small region of neurons from the previous layer, which is its receptive field. For a 3x3 convolutional kernel, the receptive field size is 3x3.
Now, consider L 3x3 convolutional layers. In the first layer, the receptive field of each neuron is 3x3. In the second layer, the receptive field of each neuron relative to the input image will cover a larger area, which is the combination of receptive fields from the previous layer. Similarly, in the third layer, the receptive field relative to the input image will be even larger.
Therefore, as the number of layers increases, the influence of each layer on the input image gradually expands. Assuming each convolutional layer has a stride of 1, and there are no pooling layers or padding, after L 3x3 convolutional layers, the receptive field size relative to the input image for each neuron will be 1 plus twice the number of convolutional layers, denoted as 1+2L.
Problem #2: Convolution on high res images is expensive! Recall ResNet stem aggressively downsamples
"},{"location":"CV/ML-4360/Recognition/lec10/#in-network-upsampling-unpooling","title":"In-Network Upsampling: \u201cUnpooling\u201d","text":"Refer to Slides
Refer to Slides
Learnable Upsampling: Transposed Convolution
Convolution with stride > 1 is \u201cLearnable Downsampling\u201d
Detect all objects in the image, and identify the pixels that belong to each object (Only things!)
Approach: Perform object detection, then predict a segmentation mask for each object!
"},{"location":"CV/ML-4360/SFM/lec3/#structure-form-motion","title":"Structure Form Motion","text":""},{"location":"CV/ML-4360/SFM/lec3/#preliminarynaries","title":"preliminarynaries","text":""},{"location":"CV/ML-4360/SFM/lec3/#point-features","title":"Point Features","text":"
Reference to https://medium.com/@deepanshut041/introduction-to-sift-scale-invariant-feature-transform-65d7f3a72d40 https://www.qixinbo.info/2021/10/26/sift/
\\(L(x,y,\u03c3)=G(x,y,\u03c3)\u2217I(x,y)\\)
Note \uff1a\u5377\u79ef
\u8fde\u7eed\u5f62\u5f0f\uff1a
\\((f\u2217g)(n)=\\int_{-\\infty }^{\\infty}f(\\tau )g(n-\\tau)d\\tau\\)
\u79bb\u6563\u5f62\u5f0f\uff1a
\\((f\u2217g)(n)=\\sum_{\\tau=-\\infty }^{\\infty}f(\\tau)g(n-\\tau)\\)
\u5bf9\u56fe\u50cf\u7684\u5904\u7406\u51fd\u6570\uff08\u5982\u5e73\u6ed1\uff0c\u6216\u8005\u8fb9\u7f18\u63d0\u53d6\uff09\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e00\u4e2ag\u77e9\u9635\u6765\u8868\u793a\uff0c\u5982\uff1a
\\(g=\\begin{bmatrix} &b_{-1,-1} &b_{-1,0} &b_{-1,1}\\\\ &b_{0,-1} &b_{0,0} &b_{0,1} \\\\ &b_{1,-1} &b_{1,0} &b_{1,1} \\end{bmatrix}\\)
\\(f(x,y)=a_{x,y}\\)
\\(g(x,y)=b_{x,y}\\)
\u6309\u5377\u79ef\u7684\u5b9a\u4e49\uff0c\u4e8c\u7ef4\u79bb\u6563\u5f62\u5f0f\u7684\u5377\u79ef\u516c\u5f0f\u5e94\u8be5\u662f\uff1a
\\((f\u2217g)(u,v)=\u2211_i\u2211_jf(i,j)g(u-i,v-j)=\\sum_{i} \\sum_{j} a_{i,j} b_{u-i,v-j}\\)
\u9996\u5148\u6211\u4eec\u5728\u539f\u59cb\u56fe\u50cf\u77e9\u9635\u4e2d\u53d6\u51fa\uff08u,v)\u5904\u7684\u77e9\u9635\uff1a
\\(f=\\begin{bmatrix} &a_{u-1,v-1} &a_{u-1,v} &a_{u-1,v+1}\\\\ &a_{u,v-1} &a_{u,v} &a_{u,v+1} \\\\ &a_{u+1,v-1} &a_{u+1,v} &a_{u+1,v+1} \\end{bmatrix}\\)
\u7136\u540e\u5c06\u56fe\u50cf\u5904\u7406\u77e9\u9635\u7ffb\u8f6c
\\((g^{'}=\\begin{bmatrix} &b_{1,1} &b_{1,0} &b_{1,-1}\\\\ &b_{0,1} &b_{0,0} &b_{0,-1} \\\\ &b_{-1,1} &b_{-1,0} &b_{-1,-1} \\end{bmatrix}\\)
"},{"location":"CV/ML-4360/SFM/lec3/#2construct-the-gaussian-pyramid","title":"(2)Construct the Gaussian Pyramid","text":"Image feature points are composed of local extrema in the DoG (Difference of Gaussians) scale space. To find these extrema in the DoG space, each pixel is compared with all of its neighboring points to determine whether it is larger or smaller than its neighboring points in both the image domain and scale domain. As shown in the figure below, the central detection point is compared with its 8 neighboring points at the same scale and 18 points corresponding to the upper and lower adjacent scales, totaling 26 points to ensure the detection of extrema in both the two-dimensional image space and scale space. If a point is the maximum or minimum value among these 26 points in the DoG scale space at the current layer and the two neighboring layers, it is considered a feature point in the image at that scale.
"},{"location":"CV/ML-4360/SFM/lec3/#4keypoint-localization","title":"\uff084\uff09Keypoint Localization","text":"Key0points generated in the previous step produce a lot of keypoints. Some of them lie along an edge, or they don\u2019t have enough contrast. In both cases, they are not as useful as features. So we get rid of them. The approach is similar to the one used in the Harris Corner Detector for removing edge features. For low contrast features, we simply check their intensities.
They used Taylor series expansion of scale space to get a more accurate location of extrema, and if the intensity at this extrema is less than a threshold value (0.03 as per the paper), it is rejected. DoG has a higher response for edges, so edges also need to be removed. They used a 2x2 Hessian matrix (H) to compute the principal curvature.
Detailed explanation:
Keypoint Filtering: Keypoints generated in the previous step might include features along edges or those with low contrast, which may not be sufficiently prominent or useful. For features with low contrast, their intensities are examined. If the intensity is below a specified threshold (as mentioned in the paper, 0.03), the feature is excluded.
Taylor Series Expansion: Utilizing a Taylor series expansion of the scale space to obtain a more accurate location of extrema, improving the precision of keypoint localization. Intensity Check and Exclusion: For extrema identified through the Taylor series expansion, their intensities are checked. If the intensity falls below the set threshold, they are excluded.
Edge Removal: Due to the higher response of the Difference of Gaussians (DoG) for edges, further removal of edge features is necessary. A 2x2 Hessian matrix is employed to calculate the principal curvature. If the principal curvature is small, indicating the point may lie along an edge, it can be excluded. (\\(f=\\frac{\\lambda_1\\lambda_2}{\\lambda_1+\\lambda_2}\\)) for the Harris operator Which means f is large indicates an edge
Now we have legitimate keypoints. They\u2019ve been tested to be stable. We already know the scale at which the keypoint was detected (it\u2019s the same as the scale of the blurred image). So we have scale invariance. The next thing is to assign an orientation to each keypoint to make it rotation invariance.
A neighborhood is taken around the keypoint location depending on the scale, and the gradient magnitude and direction is calculated in that region. An orientation histogram with 36 bins covering 360 degrees is created. Let's say the gradient direction at a certain point (in the \u201corientation collection region\u201d) is 18.759 degrees, then it will go into the 10\u201319-degree bin. And the \u201camount\u201d that is added to the bin is proportional to the magnitude of the gradient at that point. Once you\u2019ve done this for all pixels around the keypoint, the histogram will have a peak at some point. The highest peak in the histogram is taken and any peak above 80% of it is also considered to calculate the orientation. It creates keypoints with same location and scale, but different directions. It contributes to the stability of matching.
"},{"location":"CV/ML-4360/SFM/lec3/#6keypoint-descriptor","title":"(6)Keypoint descriptor","text":"At this point, each keypoint has a location, scale, orientation. Next is to compute a descriptor for the local image region about each keypoint that is highly distinctive and invariant as possible to variations such as changes in viewpoint and illumination.
To do this, a 16x16 window around the keypoint is taken. It is divided into 16 sub-blocks of 4x4 size. For each sub-block, 8 bin orientation histogram is created.
So 4 X 4 descriptors over 16 X 16 sample array were used in practice. 4 X 4 X 8 directions give 128 bin values. It is represented as a feature vector to form keypoint descriptor. This feature vector introduces a few complications. We need to get rid of them before finalizing the fingerprint.
Let \\(K_i \u2208 R_{3\u00d73}\\)denote the camera matrix of camera \\(i\\).
Let \\(\\widetilde{x}_i\\)= \\(K_i^{-1} x \u0304_i\\)denote the local ray direction of pixel \\(x \u0304_i\\) in camera \\(i\\).
We have:
\\(\\tilde{x}_2 \u221d x_2 = Rx_1+t \u221d R\\tilde{x}_1+st\\)
Taking the cross product of both sides with t we obtain:
\\([t]_\u00d7\\tilde{x}_2 \u221d [t]_\u00d7R\\tilde{x}_1\\)
$\\tilde{x}^\u22a4_2 [t]\u00d7R\\tilde{x}_1 \u221d \\tilde{x}^\u22a4_2 [t]\u00d7\\tilde{x}2 = 0 $
\\(\u21d2 \\tilde{x}^\u22a4_2 [t]\u00d7R \\tilde{x}_1 = 0\\)
\\(\\tilde{x}^\u22a4_2 \\tilde{E}\\tilde{x} _1 = 0\\)
\\(\\widetilde{E} = [ t ] \u00d7 R\\)\u200b
\\(\\widetilde{E}\\) maps a point \\(\\tilde{x}_1\\) in image \\(1\\) to the corresponding epipolar line in image 2 \\(\\widetilde{l}_2=\\widetilde{E} \\tilde{x}_1\\)
Similarly, by transposition, we obtain the epipolar line in image 1:\\(\\widetilde{l}_1 = \\widetilde{E}^\u22a4\\tilde{x}_2\\)
For any point \\(\\tilde{x}_1\\)in the first image, the corresponding epipolar line \\(\\widetilde{l}_2 = \\widetilde{E}\\tilde{x}_1\\) in the second image passes through the so-called epipole \\(\\tilde{e}_2\\) which therefore satisfies
\\(\\widetilde{e}^\u22a4_2 \\ \\widetilde{l}_2 = \\widetilde{e}^\u22a4_2 \\widetilde{E}\\tilde{x}_1 = 0\\) for all \\(\\tilde{x}_1.\\)
It follows that:\\(\\widetilde{e}^\u22a4_2 \\widetilde{E} = 0\\)
Thus, \\(\\tilde{e}^\u22a4_2\\)is the leftnull-space (left singular vector with singular value 0) of\\(\\widetilde{E}\\).
Further:\\(\\widetilde{E}\\tilde{e}_1 = 0\\) Thus,\\(\\tilde{e}_1\\) is the rightnull-space (right singular vector with singular value 0)of \\(\\widetilde{E}.\\)
"},{"location":"CV/ML-4360/SFM/lec3/#recover-r-t","title":"Recover \\(R\\ \\&\\ t\\)","text":"Derivation
\\(\\tilde{x}_{2i}^T \\widetilde{E} \\tilde{x}_{1i}=0\\)
\\(\\widetilde{E}=\\begin{pmatrix} e_{11} & e_{12} & e_{13} \\\\ e_{21} & e_{22} & e_{23} \\\\ e_{31} & e_{32} & e_{33} \\end{pmatrix} \\ \\ \\ \\ \\ \\ \\ \\ E = \\begin{pmatrix} e_{11} \\\\ e_{12} \\\\ e_{13} \\\\ e_{21} \\\\ e_{22} \\\\ e_{23} \\\\ e_{31} \\\\ e_{32} \\\\ e_{33} \\end{pmatrix}\\)\\(\\Rightarrow x_1x_2e_{11} + y_1x_2e_{12} + x_2e_{13}+ x_1y_2e_{21} + y_1y_2e_{22} + y_2e_{23}+ x_1e_{31} + y_1e_{32} + e_{33 }= 0\\)
Rearranging the product, so \\(E\\) is on the left hand side:\\(\\tilde{x}_2^T\\tilde{E}\\tilde{x}_1 =\\begin{pmatrix} x_{11}x_{12} & y_{11}x_{12} & x_{12}&x_{11}y_{12}&y_{11}y_{12}&y_{12}&x_{11}&y_{11}&1 \\\\ \u2026\u2026 \\\\ x_{i1}x_{i2} & y_{i1}x_{i2} & x_{i2}&x_{i1}y_{i2}&y_{i1}y_{i2}&y_{i2}&x_{i1}&y_{i1}&1 \\\\\u2026\u2026 \\\\x_{81}x_{82} & y_{81}x_{82} & x_{82}&x_{81}y_{82}&y_{81}y_{82}&y_{82}&x_{81}&y_{81}&1\\end{pmatrix} \\begin{pmatrix} e_{11} \\\\ e_{12} \\\\ e_{13} \\\\ e_{21} \\\\ e_{22} \\\\ e_{23} \\\\ e_{31} \\\\ e_{32} \\\\ e_{33} \\end{pmatrix} =0\\)\\(\\Rightarrow AE = 0\\)
If there are more than 8 point correspondences, the system is usually over-determined and an exact solution is not possible. Instead, we solve a least squares problem for AE
\u6211\u7684\u7406\u89e3\uff1a\u5e76\u4e0d\u662f\u8bf4\\(E\\) \u7684\u8303\u6570\u5c31\u662f1 \u800c\u662f\u5728\u8ba1\u7b97 \\(min(||AE||)\\)\u65f6\uff0c\u7b49\u540c\u4e8e\u8ba1\u7b97 minimize \\(||AE||\\) subject to \\(||x||^2=1\\)\u200b
\u52d8\u8bef x is the last column of V
Credits to Carnegie Mellon University and Kris Kitani, http://www.cs.cmu.edu/~16385/s17/Slides/11.5_SVD.pdf
"},{"location":"CV/ML-4360/SFM/lec3/#properties-of-the-tildee","title":"Properties of the \\(\\tilde{E}\\)","text":"by readjustment:
\\(\\widetilde{E} =\\begin{bmatrix} u_0&u_1&u_2 \\end{bmatrix}\\begin{bmatrix} s&0&0\\\\0&s&0\\\\0&0&0 \\end{bmatrix} \\begin{bmatrix}v_0^T\\\\v_1^T\\\\v_2^T \\end{bmatrix} =U\\sum V^T\\)
\\(s=\\frac{\\lambda_1+\\lambda_2}{2}\\) \\(\\lambda_1\\ge \\lambda_2\\ge\\lambda_3\\)
Proof
\u5177\u4f53\u8bc1\u660e\u89c1 https://blog.csdn.net/weixin_44387324/article/details/119182590
\\(W=\\begin{bmatrix} 0&-1&0\\\\1&0&0\\\\0&0&1 \\\\ \\end{bmatrix} \\\\ t_x=UW\\sum U^T \\\\ R=UW^{-1}V^T \\\\E=U\\sum V^T\\)
"},{"location":"CV/ML-4360/SFM/lec3/#fundamental-matrix-if-k_i-unknown","title":"Fundamental matrix (if \\(K_i\\) unknown):","text":"Let \\(\\tilde{x}^s_i = \\widetilde{P}_ i \\tilde{x}_w\\) denote the projection of a 3D world point \\(\\tilde{x}_w\\) onto the image of the i\u2019th camera \\(\\tilde{x}^s_i\\).
Consider the cross product \\(x \u0304^s_i \u00d7 \\tilde{P}_i\\tilde{x}_w =0\\).
\\(\\begin{bmatrix}x^s_i\\widetilde{p}_{i3}^T - \\widetilde{p}_{i1}^T \\\\ y^s_i\\widetilde{p}_{i3}^T - \\widetilde{p}_{i2}^T \\end{bmatrix}\\widetilde{x}_w=0\\) \\([\\bar{x}_i^s=(x_i^s,y_i^s,1)]\\)
Stacking \\(N \u2265 2\\) observations of a point, we obtain a linear system \\(A\\tilde{x}_w = 0.\\)
As \\(\\tilde{x}_w\\)\u200b is homogeneous this leads to a constrained least squares problem.
The solution to this problem is the right singular vector corresponding to the smallest singular value of A.
This is the Direct Linear Transformation we are already familiar with from \\(Lecture 2\\).
Direcr Linear Transformation https://medium.com/@insight-in-plain-sight/estimating-the-homography-matrix-with-the-direct-linear-transform-dlt-ec6bbb82ee2b
Proof https://blog.csdn.net/u011178262/article/details/86729887
Reprojection Error Minimization:
While DLT often works well, it is not invariant to perspective transformations.The gold standard is to minimize the reprojection error using numerical methods:
1.Take measurements \\(\\hat{W}\\)
2.Compute SVD $\\hat{W} = U\u03a3V^\u22a4 $and keep the top 3 SVs
3.Define\\(\\hat{R} =U\u03a3^{\\frac{1}{2}}\\) and \\(\\hat{X} =\\sum^{\\frac{1}{2}}V^T\\)
4.Compute \\(QQ^\u22a4\\) and from this Q
5.Compute\\(R=\\hat{R}Q\\)and\\(X=Q^{-1}\\hat{X}\\)
Remarks:
"},{"location":"CV/ML-4360/SFX/lec8/#shape-from-shading","title":"Shape-from-Shading","text":""},{"location":"CV/ML-4360/SFX/lec8/#recap-rendering-equation","title":"Recap: Rendering Equation","text":"
Dropping the dependency on \u03bb and p for notational simplicity, and considering a single point light source located in direction s, the rendering equation simplifies as follows:
\\(L_{out}(v)=BRDF(s,v)\u00b7L_{in}\u00b7(-n^{T}\u00b7s)\\)
Assuming a purely diffuse material with albedo (=diffuse reflectance) BRDF(s, v) = \u03c1 \uff0c further simplifies to the following equation (Lout becomes independent of v): \\(L_{out} = \u03c1 \u00b7 L_{in} \u00b7 (\u2212n^\u22a4\u00b7s)\\)
For simplicity, we further eliminate the minus sign by reversing the orientation (definition) of the light ray s and obtain:
\\(L_{out} = \u03c1 \u00b7 L_{in} \u00b7 n^\u22a4s\\)
"},{"location":"CV/ML-4360/SFX/lec8/#shape-from-shading_1","title":"Shape-from-Shading","text":"Gradient Space Representation
\u200b \\((p,q)=(-\\frac{\\partial z}{\\partial x},-\\frac{\\partial z}{\\partial y})\\)
\u200b \\(\\vec{n}=\\frac{(p,q,1)^T}{\\sqrt{p^2+q^2+1}}\\)
\\(R(n)=n^Ts=\\frac{ps_x+qs_y+s_z}{1+\\sqrt{p^2+q^2+1}}=R(p,q)\\)
Reflectance Map
Shape-from-Shading Formulation
Assumption: image irradiance (=intensity) should equal the reflectance map:\\(I(x, y) = R(f(x, y), g(x, y))\\)
SfS thus minimizes:
\\(E_{image}(f,g)=\\iint (I(x,y)-R(f,g))^2dxdy\\)\u200b
However, as we have seen, this problem is ill-posed (unknowns > observations)
Numerical Shape-from-Shading
To constrain this ill-posed problem, SfS exploits two additional constraints:
Goal: Penalize rapid changes in surface gradients \\(f\\) and \\(g\\)
\u200b \\(E_{smooth}(f,g)=\\iint (f_x^2+f_y^2+g_x^2+g_y^2)dxdy\\) with gradients \\(f_x=\\frac{\\partial f}{\\partial x}\\) \\(f_y=\\frac{\\partial f}{\\partial y}\\)\u200b\u200b
Goal: Constrain normals at occluding boundaries where they are known
\u200b
"},{"location":"CV/ML-4360/SFX/lec8/#surface-integration","title":"Surface Integration","text":"Given the surface gradients (from above methods) \\((p,q)=(-\\frac{\\partial z}{\\partial x},-\\frac{\\partial z}{\\partial y})\\) how can we recover the 3D surface / depth map?
Assuming a smooth surface, we can solve the following variational problem
$E(z)=\\iint[(\\frac{\\partial z}{\\partial x}+p)^2+(\\frac{\\partial z}{\\partial y}+q)^2]dxdy $\u200b\u200b
efficiently using the discrete Fast Fourier Transform (Frankot and Chellappa, 1988).
"},{"location":"CV/ML-4360/SFX/lec8/#photometric-stereo","title":"Photometric Stereo","text":"Instead of smoothness constraints, add more observations per pixel
Take K images of the object from same viewpoint (e.g., with a tripod) but with different (known) point light source each
Per-pixel estimation of normal and albedo or material
Also assumes far camera/light
i.e., if all three light sources \\(s_1, s_2, s_3\\) and the origin \\(p\\) lie on a 3D plane, the linear system becomes rank-deficient and thus there exists no unique solution \\(\\tilde{n} = S^{-1}I\\)\u200b.
Refer to PPT
"},{"location":"CV/ML-4360/SFX/lec8/#volumetric-fusion","title":"Volumetric Fusion","text":""},{"location":"CV/ML-4360/SFX/lec8/#representation","title":"Representation","text":"3 Steps:
As the distance to surface is unknown, approximate it with distance along ray
This approximation is good only in the vincinity of the surface (often suffices)
"},{"location":"CV/ML-4360/SFX/lec8/#volumetric-fusion_1","title":"Volumetric Fusion","text":"After conversion, calculate average of the discrete SDF fields
The implicit surface will be an average of the two original ones
"},{"location":"CV/ML-4360/SR/lec4/#stereo-reconstruction","title":"Stereo Reconstruction","text":""},{"location":"CV/ML-4360/SR/lec4/#preliminaries","title":"Preliminaries","text":""},{"location":"CV/ML-4360/SR/lec4/#epipolar-geometry","title":"Epipolar Geometry","text":"
What if both cameras face exactly the same direction?
Let\\(K_1 = K_2 = R = I\\) and \\(t = (t,0,0)^\u22a4\\)
\\(\\bar{x}_2^T\\tilde{E}\\bar{x}_1=\\bar{x_2}^T \\begin{bmatrix}0&0&0\\\\0&0&-t\\\\0&t&0\\end{bmatrix}\\bar{x1}=ty_1-ty_2=0\\)
Thus \\(y_1=y_2\\)
What if the images are not in the required setup?
How can we make epipolar lines horizontal?
Step1:Estimate\\(\\tilde{E}\\) , decompose into \\(t\\) and \\(R\\)
Step2: Find \\(R_{rect}\\)
Choose \\(OO^\u2032=\\vec{T}=(t_x,t_y,t_z)^T\\)
\\(e_1=\\frac{T}{\u2223\u2223T\u2223\u2223}\\)
\\(e_2=\\frac{1}{\\sqrt{T_x^2+T_y^2}}(-T_y,T_x,0)^T= [(0, 0, 1)^\u22a4]\u00d7r_1\\)
\\(e_3=e_2\\) x \\(e_1\\)
\\(\\Rightarrow R_{rect}=\\begin{bmatrix}e_1^T\\\\e_2^T\\\\e_3^T\\end{bmatrix}\\)
Step3 : Adjust \\(\\tilde{x}_i\\)
Warp pixels in the second image as follows: \\(\\tilde{x}_2^{'}=KRR_{rect}K_2^{-1}x \u0304_2\\)
NOTE \uff1aDifferent coordinate systems result in different perception of \\(R_{rect}\\)
Thus the \\(R_{rect}^{'}=RR_{rect}\\)
\\(K\\) is a shared projection matrix that can be chosen arbitrarily (e.g., \\(K = K_1\\))
In practice, the inverse transformation is used for warping (i.e. query the source)
https://martin-thoma.com/zero-mean-normalized-cross-correlation/
"},{"location":"CV/ML-4360/SR/lec4/#sum-of-squared-differences-ssd","title":"Sum of squared differences (SSD)","text":""},{"location":"CV/ML-4360/SR/lec4/#assumption-violations","title":"Assumption Violations","text":"This is called the fronto-parallel assumption which is often invalid
Slanted surfaces deform perspectively when the viewpoint changes
Effect of Window Size
Small windows lead to matching ambiguities and noise in the disparity maps
Larger windows lead to smoother results, but loss of details and border bleeding
Border Bleeding:
Hinge Loss:\\(L = max(0, m +\\ s_-\\ -\\ s_+)\\)
\\(s_- / s_+\\) is the score of the network for the negative/positive example
The loss is zero when the similarity of the positive example is greater than the similarity of the negative example by at least margin m
The network is trained by minimizing a hinge loss.
The loss is computed by considering pairs of examples centered around the same image position where one example belongs to the positive and one to the negative class.
Let \\(s_+\\)be the output of the network for the positive example, \\(s_-\\) be the output of the network for the negative example
Let m, the margin, be a positive real number.
The hinge loss for that pair of examples is defined as \\(L = max(0, m + s_- - s_+)\\) The loss is zero when the similarity of the positive example is greater than the similarity of the negative example by at least the margin m.
Set the margin to 0.2 in our experiments.
https://www.jmlr.org/papers/volume17/15-535/15-535.pdf
"},{"location":"CV/ML-4360/SR/lec4/#spatial-regularization","title":"Spatial Regularization","text":"Add Pairwise terms: Smoothness between adjacent pixels in addition to matching costs;
Potts: \\(\u03c8_{smooth}(d, d^\u2032) = [d\\ne d^\u2032]\\)
Truncated\\(l_1:\u03c8+{smooth}(d,d^\u2032)=min(|d-d^\u2032|,\u03c4)\\)
Paper
https://dash.harvard.edu/bitstream/handle/1/3637107/Mumford_StatRangeImage.pdf?sequence=3&isAllowed=y
"},{"location":"CV/ML-4360/SR/lec4/#end-to-end-learning","title":"End-to-End Learning","text":"End to End learning in the context of AI and ML is a technique where the model learns all the steps between the initial input phase and the final output result. This is a deep learning process where all of the different parts are simultaneously trained instead of sequentially.
"},{"location":"CV/ML-4360/SR/lec4/#dispnet","title":"DISPNET","text":""},{"location":"CV/ML-4360/SR/lec4/#gcnet","title":"GCNET","text":""},{"location":"CV/ML-4360/SR/lec4/#stereo-mixture-density-networks-smd-nets","title":"STEREO MIXTURE DENSITY NETWORKS (SMD-NETS)","text":""},{"location":"CV/ML-4360/Self/lec/","title":"Self-Supervised Learning","text":""},{"location":"CV/ML-4360/Self/lec/#preliminaries","title":"Preliminaries","text":"
Idea of self-supervision:
Specifically, at training time, we have access to two images \\(I^l\\) and \\(I^r\\) , corresponding to the left and right color images from a calibrated stereo pair, captured at the same moment in time.
Instead of trying to directly predict the depth, we attempt to find the dense correspondence field \\(d_r\\) that, when applied to the left image, would enable us to reconstruct the right image. We will refer to the reconstructed image \\(I^l(d_r)\\) as \\(\\tilde{I}^r\\)\u200b. Similarly, we can also estimate the left image given the right one, \\(\\tilde{I}^l =I^r(d_l)\\).
Assuming that the images are rectified , \\(d\\) corresponds to the image disparity - a scalar value per pixel that our model will learn to predict. Given the baseline distance \\(b\\) between the cameras and the camera focal length \\(f\\), we can then trivially recover the depth \\(\\hat{d}\\) from the predicted disparity, \\(\\hat{d}=bf/d\\)\u200b\u200b\u200b.
Simultaneously infer both disparities (left-to-right and right-to-left), using only the left input image, and obtain better depths by enforcing them to be consistent with each other.
A pretext (adj: pretextual) is an excuse to do something or say something that is not accurate. Pretexts may be based on a half-truth or developed in the context of a misleading fabrication. Pretexts have been used to conceal the true purpose or rationale behind actions and words
"},{"location":"CV/ML-4360/Self/lec/#visual-representation-learning-by-context-prediction","title":"Visual Representation Learning by Context Prediction","text":"Care has to be taken to avoid trivial shortcuts (e.g., edge continuity)
A network can predict the absolute image location of randomly sampled patches In this case, the relative location can be inferred easily. Why is this happening?
Solution: Random dropping of color channels or projection towards gray
"},{"location":"CV/ML-4360/Self/lec/#visual-representation-learning-by-solving-jigsaw-puzzles","title":"Visual Representation Learning by Solving Jigsaw Puzzles","text":"Can we find a more general pretext task?
I Given a chosen score function \\(s(\u00b7, \u00b7)\\), we want to learn an encoder f that yields high score for positive pairs \\((x, x+)\\) and low score for negative pairs \\((x, x\u2212)\\):\\(s(f(x), f(x+)) \u226b s(f(x), f(x\u2212))\\)
"},{"location":"DIP/Basic/DIP/#image-format","title":"Image Format","text":"
Digital image can be represented as a 2D array or matrix.
Grayscale:Each pixel in a grayscale image is represented by a byte (8 bits), which covers 256 degrees by [0..255]
Colour image: Each pixel in a color image is represented by 3 bytes (24 bits), which are for R (red), G (Green) and B (Blue), respectively.
\u76ee\u7684\uff1a\u5b58\u50a8\u56fe\u50cf
\u4fe1\u606f\u56fe\u50cf\u7279\u70b9\uff1a\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\uff0c\u77e9\u5f62\u533a\u57df\u4fe1\u606f\u91cf\u5927
\u6709\u4e9b\u6587\u4ef6\u683c\u5f0f\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u6709\u5173\uff1awindows\u3001unix\u3001mac\u7f16\u7801\u65b9\u5f0f\uff1a\u65e0\u538b\u7f29\u3001\u65e0\u635f\u538b\u7f29\u3001\u6709\u635f\u538b\u7f29
"},{"location":"DIP/Basic/DIP/#1bmp","title":"1.BMP\u7684\u6587\u4ef6\u683c\u5f0f\uff1a","text":"\u200b BMP\uff08Bitmap\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u56fe\u50cf\u6587\u4ef6\u683c\u5f0f\uff0c\u662fWindows\u7cfb\u7edf\u7684\u4e00\u79cd\u6807\u51c6\u6587\u4ef6\u683c\u5f0f\u3002BMP \u4f4d\u56fe\u6587\u4ef6\u9ed8\u8ba4\u7684\u6587\u4ef6\u6269\u5c55\u540d\u662f bmp \u6216\u8005 dib\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cBMP \u56fe\u50cf\u91c7\u7528\u975e\u538b\u7f29\u65b9\u5f0f\uff1b\u7136\u800c\uff0c\u5b83\u4e5f\u652f\u6301\u56fe\u50cf\u538b\u7f29\uff0c\u4f8b\u5982 RLE \u683c\u5f0f\u3002\u4e0b\u9762\u4ecb\u7ecdBMP\u6587\u4ef6\u7684\u5177\u4f53\u6587\u4ef6\u7ed3\u6784\u3002
BMP \u56fe\u50cf\u6bcf\u4e00\u884c\u626b\u63cf\u7531\u8868\u793a\u56fe\u50cf\u50cf\u7d20\u7684\u8fde\u7eed\u7684\u5b57\u8282\u7ec4\u6210\uff0c\u6bcf\u4e00\u884c\u4ee5\u56db\u5b57\u8282\u5bf9\u9f50\uff08\u4ee50\u8865\u9f50\uff09\u3002\u56fe\u50cf\u7684\u626b\u63cf\u884c\u662f\u7531\u5e95\u5411\u4e0a\u5b58\u50a8\u7684\uff0c\u8fd9\u5c31\u662f\u8bf4\uff0c\u9635\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u4f4d\u56fe\u5de6\u4e0b\u89d2\u7684\u50cf\u7d20\uff0c\u800c\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u4f4d\u56fe\u53f3\u4e0a\u89d2\u7684\u50cf\u7d20\u3002
// \u5b9a\u4e49BMP\u6587\u4ef6\u5934\u7684\u5927\u5c0f\u4e3a54\u4e2a\u5b57\u8282\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u56fa\u5b9a\u4e0d\u53d8\n#define HEADERSIZE 54\n\n// \u5b9a\u4e49\u51e0\u79cd\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u522b\u540d\uff0c\u4ee5\u4fbf\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u66f4\u5177\u53ef\u8bfb\u6027\u7684\u540d\u79f0\ntypedef unsigned char byte;\ntypedef unsigned short word;\ntypedef unsigned int dword;\n\n// \u4f7f\u7528#pragma pack(1)\u6307\u4ee4\u8bbe\u7f6e\u7ed3\u6784\u4f53\u7684\u5b57\u8282\u5bf9\u9f50\u65b9\u5f0f\u4e3a1\u5b57\u8282\n#pragma pack(1)\n\n// \u5b9a\u4e49BMP\u6587\u4ef6\u5934\u7684\u7ed3\u6784\u4f53\ntypedef struct {\n word bfType; // BMP\u6587\u4ef6\u7684\u7c7b\u578b\u6807\u8bc6\uff0c\u901a\u5e38\u4e3a\"BM\"\n dword bfSize; // BMP\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\n word reserved1; // \u4fdd\u7559\u5b57\u6bb5\uff0c\u8bbe\u7f6e\u4e3a0\n word reserved2; // \u4fdd\u7559\u5b57\u6bb5\uff0c\u8bbe\u7f6e\u4e3a0\n dword bfOffset; // \u4f4d\u56fe\u6570\u636e\u7684\u504f\u79fb\u91cf\uff0c\u5373\u6587\u4ef6\u5934\u7684\u5927\u5c0f\n} BMPHeader;\n\n// \u5b9a\u4e49BMP\u4fe1\u606f\u5934\u7684\u7ed3\u6784\u4f53\ntypedef struct {\n dword size; // \u4fe1\u606f\u5934\u7684\u5927\u5c0f\uff0c\u901a\u5e38\u4e3a40\u5b57\u8282\n int width; // \u56fe\u50cf\u7684\u5bbd\u5ea6\uff0c\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\n int height; // \u56fe\u50cf\u7684\u9ad8\u5ea6\uff0c\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\n word planes; // \u989c\u8272\u5e73\u9762\u6570\uff0c\u901a\u5e38\u4e3a1\n word bitCount; // \u6bcf\u4e2a\u50cf\u7d20\u7684\u4f4d\u6570\n dword compression; // \u538b\u7f29\u7c7b\u578b\n dword imageSize; // \u4f4d\u56fe\u6570\u636e\u7684\u5927\u5c0f\uff0c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\n int xPixelsPerMeter; // \u6c34\u5e73\u5206\u8fa8\u7387\uff0c\u6bcf\u7c73\u7684\u50cf\u7d20\u6570\n int yPixelsPerMeter; // \u5782\u76f4\u5206\u8fa8\u7387\uff0c\u6bcf\u7c73\u7684\u50cf\u7d20\u6570\n dword colorsUsed; // \u4f7f\u7528\u7684\u989c\u8272\u6570\n dword colorsImportant; // \u91cd\u8981\u989c\u8272\u6570\n} BMPInfoHeader;\n\n// \u5b9a\u4e49\u8c03\u8272\u677f\u4e2d\u7684\u4e00\u4e2a\u989c\u8272\u7684\u7ed3\u6784\u4f53\ntypedef struct {\n byte blue; // \u84dd\u8272\u5206\u91cf\n byte green; // \u7eff\u8272\u5206\u91cf\n byte red; // \u7ea2\u8272\u5206\u91cf\n byte reserved; // \u4fdd\u7559\u5b57\u6bb5\uff0c\u901a\u5e38\u8bbe\u7f6e\u4e3a0\n} pallete;\n\n// \u5b9a\u4e49\u5305\u542bBMP\u6587\u4ef6\u5934\u3001BMP\u4fe1\u606f\u5934\u3001\u8c03\u8272\u677f\u548c\u4f4d\u56fe\u6570\u636e\u7684\u5b8c\u6574BMP\u56fe\u50cf\u7684\u7ed3\u6784\u4f53\ntypedef struct {\n BMPHeader bmph; // BMP\u6587\u4ef6\u5934\n BMPInfoHeader bmpih; // BMP\u4fe1\u606f\u5934\n pallete colours[256]; // \u8c03\u8272\u677f\uff0c\u901a\u5e38\u7528\u4e8e8\u4f4d\u989c\u8272\u7d22\u5f15\u56fe\u50cf\n byte *bitmap; // \u4f4d\u56fe\u6570\u636e\n} BMP;\n
"},{"location":"DIP/Basic/DIP/#1bmp_1","title":"(1)BMP\u6587\u4ef6\u6574\u4f53\u7ed3\u6784\uff1a","text":""},{"location":"DIP/Basic/DIP/#_1","title":"Basic concept and programming introduction","text":"BMP \u6587\u4ef6 \u5927\u4f53\u4e0a\u5206\u4e3a\u56db\u4e2a\u90e8\u5206:\u56fe\u50cf\u6587\u4ef6\u5934\uff08Image file header\uff09\u3001\u56fe\u50cf\u4fe1\u606f\u5934\uff08Image information header\uff09\u3001\u8c03\u8272\u677f\uff08Palette\uff09\u3001\u56fe\u50cf\u6570\u636e\u5b57\u8282\u9635\u5217\uff08Image data\uff09
\u6709\u4e00\u4e2a\u957f\u5bbd\u5404\u4e3a200\u4e2a\u8c61\u7d20\uff0c\u989c\u8272\u6570\u4e3a16\u8272\u7684\u5f69\u8272\u56fe\uff0c\u6bcf\u4e00\u4e2a\u8c61\u7d20\u90fd\u7528R\u3001G\u3001B\u4e09\u4e2a\u5206\u91cf\u8868\u793a
\u56e0\u4e3a\u6bcf\u4e2a\u5206\u91cf\u6709256\u4e2a\u7ea7\u522b\uff0c\u8981\u75288\u4f4d(bit)\uff0c\u5373\u4e00\u4e2a\u5b57\u8282(byte)\u6765\u8868\u793a\uff0c\u6240\u4ee5\u6bcf\u4e2a\u8c61\u7d20\u9700\u8981\u75283\u4e2a\u5b57\u8282\u3002\u6574\u4e2a\u56fe\u8c61\u8981\u7528200\u00d7200\u00d73\uff0c\u7ea6120k\u5b57\u8282\uff0c\u53ef\u4e0d\u662f\u4e00\u4e2a\u5c0f\u6570\u76ee\u5440\uff01\u5982\u679c\u6211\u4eec\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\uff0c\u5c31\u80fd\u7701\u7684\u591a\u3002
\u56e0\u4e3a\u662f\u4e00\u4e2a16\u8272\u56fe\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u5e45\u56fe\u4e2d\u6700\u591a\u53ea\u670916\u79cd\u989c\u8272\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2a\u8868\uff1a\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u8bb0\u5f55\u4e00\u79cd\u989c\u8272\u7684R\u3001G\u3001B\u503c\u3002\u8fd9\u6837\u5f53\u6211\u4eec\u8868\u793a\u4e00\u4e2a\u8c61\u7d20\u7684\u989c\u8272\u65f6\uff0c\u53ea\u9700\u8981\u6307\u51fa\u8be5\u989c\u8272\u662f\u5728\u7b2c\u51e0\u884c\uff0c\u5373\u8be5\u989c\u8272\u5728\u8868\u4e2d\u7684\u7d22\u5f15\u503c\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u8868\u7684\u7b2c0\u884c\u4e3a255\uff0c0\uff0c0(\u7ea2\u8272)\uff0c\u90a3\u4e48\u5f53\u67d0\u4e2a\u8c61\u7d20\u4e3a\u7ea2\u8272\u65f6\uff0c\u53ea\u9700\u8981\u6807\u660e0\u5373\u53ef
\u8fd9\u5f20R\u3001G\u3001B\u7684\u8868\uff0c\u5c31\u662f\u6211\u4eec\u5e38\u8bf4\u7684\u8c03\u8272\u677f(Palette)\uff0c\u53e6\u4e00\u79cd\u53eb\u6cd5\u662f\u989c\u8272\u67e5\u627e\u8868LUT(Look Up Table)\uff0c\u4f3c\u4e4e\u66f4\u786e\u5207\u4e00\u4e9b\u3002Windows\u4f4d\u56fe\u4e2d\u4fbf\u7528\u5230\u4e86\u8c03\u8272\u677f\u6280\u672f\u3002\u5176\u5b9e\u4e0d\u5149\u662fWindows\u4f4d\u56fe\uff0c\u8bb8\u591a\u56fe\u8c61\u6587\u4ef6\u683c\u5f0f\u5982pcx\u3001tif\u3001gif\u7b49\u90fd\u7528\u5230\u4e86\u3002\u6240\u4ee5\u5f88\u597d\u5730\u638c\u63e1\u8c03\u8272\u677f\u7684\u6982\u5ff5\u662f\u5341\u5206\u6709\u7528\u7684\u3002
"},{"location":"DIP/Basic/DIP/#5image-data","title":"(5)\u56fe\u50cf\u6570\u636e\u5b57\u8282\u9635\u5217\uff08Image data\uff09","text":"\u56fe\u50cf\u6570\u636e\u5b57\u8282\u9635\u5217\u5b58\u50a8\u4e86\u8c03\u8272\u677f\u7684\u7d22\u5f15\u53f7\uff0c\u6216\u8005\u53d6\u51b3\u4e8e\u989c\u8272\u6df1\u5ea6\u7684RGB\u503c
\u5176\u5927\u5c0f\u53d6\u51b3\u4e8e\u56fe\u50cf\u5927\u5c0f\u548c\u989c\u8272\u6df1\u5ea6\u3002
(1)\u6bcf\u4e00\u884c\u7684\u5b57\u8282\u6570\u5fc5\u987b\u662f4\u7684\u6574\u500d\u6570\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u5219\u9700\u8981\u8865\u9f50(append zero)
(2)\u4e00\u822c\u6765\u8bf4\uff0c.bMP\u6587\u4ef6\u7684\u6570\u636e\u4ece\u4e0b\u5230\u4e0a\uff0c\u4ece\u5de6\u5230\u53f3\u7684
\u4e5f\u5c31\u662f\u8bf4\uff0c\u4ece\u6587\u4ef6\u4e2d\u6700\u5148\u8bfb\u5230\u7684\u662f\u56fe\u8c61\u6700\u4e0b\u9762\u4e00\u884c\u7684\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u8c61\u7d20\uff0c\u7136\u540e\u662f\u5de6\u8fb9\u7b2c\u4e8c\u4e2a\u8c61\u7d20\u2026\u2026\u63a5\u4e0b\u6765\u662f\u5012\u6570\u7b2c\u4e8c\u884c\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u8c61\u7d20\uff0c\u5de6\u8fb9\u7b2c\u4e8c\u4e2a\u8c61\u7d20\u2026\u2026\u4f9d\u6b21\u7c7b\u63a8 \uff0c\u6700\u540e\u5f97\u5230\u7684\u662f\u6700\u4e0a\u9762\u4e00\u884c\u7684\u6700\u53f3\u4e00\u4e2a\u8c61\u7d20\u3002
You do not need to turn around the rows manually. The API functions which also display the bitmap will do that for you automatically.
\u5229\u7528 C \u8bed\u8a00\u4e2d\u7684 fopen fread fwrite \u8fdb\u884c\u8bfb\u5165\u3001\u5199\u51fa\u5373\u53ef
\u5728\u8bfb\u5165\u4e0e\u5199\u51fa\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f BMP\u6587\u4ef6\u4fe1\u606f\u7684\u8c03\u6574\uff0c\u4f8b\u5982\u753124\u4f4d\u5f69\u8272BMP\u8f6c\u4e3a\u7070\u8272\u56fe\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u8c03\u8272\u677f\u3001\u4f4d\u6df1\u7b49\u7684\u8c03\u6574\u3002
FILE *inputFile = fopen(\"input.bmp\", \"rb\");\n // \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u6253\u5f00\nif (!inputFile) {\n printf(\"Unable to open input BMP file.\\n\"); // \u8f93\u51fa\u9519\u8bef\u4fe1\u606f\n return 0; // \u9000\u51fa\u7a0b\u5e8f\n}\nprintf(\"successfully loaded!\"); // \u6210\u529f\u52a0\u8f7d\u7684\u63d0\u793a\u4fe1\u606f\nBMP bmp; // \u521b\u5efa\u4e00\u4e2aBMP\u7ed3\u6784\u4f53\u53d8\u91cf\u7528\u4e8e\u5b58\u50a8BMP\u6587\u4ef6\u7684\u4fe1\u606f\u548c\u6570\u636e\n// \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6BMP\u6587\u4ef6\u5934\u548c\u4fe1\u606f\u5934\nfread(&(bmp), HEADERSIZE, 1, inputFile);\nint width = bmp.bmpih.width; // \u83b7\u53d6\u56fe\u50cf\u7684\u5bbd\u5ea6\nint height = bmp.bmpih.height; // \u83b7\u53d6\u56fe\u50cf\u7684\u9ad8\u5ea6\n// \u5982\u679c\u56fe\u50cf\u5927\u5c0f\u4e3a0\uff0c\u5219\u8ba1\u7b97\u56fe\u50cf\u6570\u636e\u5927\u5c0f\nif (bmp.bmpih.imageSize == 0) {bmp.bmpih.imageSize = bmp.bmph.bfSize - bmp.bmph.bfOffset;}\n// \u4e3a\u4f4d\u56fe\u6570\u636e\u5206\u914d\u5185\u5b58\nbmp.bitmap = (byte*)malloc(sizeof(byte) * (bmp.bmph.bfSize - HEADERSIZE));\n// \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u4f4d\u56fe\u6570\u636e\nfread(bmp.bitmap, bmp.bmph.bfSize - HEADERSIZE, 1, inputFile);\nfclose(inputFile); // \u5173\u95ed\u6587\u4ef6\n
"},{"location":"DIP/Basic/DIP/#visual-intelligence-and-pattern-analysis","title":"Visual Intelligence and Pattern Analysis","text":""},{"location":"DIP/Basic/DIP/#history","title":"History","text":""},{"location":"DIP/Basic/DIP/#_2","title":"\u9488\u5b54\u6210\u50cf","text":"\u6295\u5f71\u4fbf\u6210\u4e3a\u5012\u7acb\u7684\uff1b\u7269\u8ddd\u8d8a\u8fdc,\u50cf\u8d8a\u5c0f\uff1b\u7269\u8ddd\u8d8a\u8fd1,\u50cf\u8d8a\u5927
\u5982\u524d\u9762\u63d0\u5230\u7684\u60c5\u51b5\uff0c\u5149\u5708\u5b54\u5f84\u5927\u7684\u65f6\u5019\uff0c\u4f1a\u5bfc\u81f4\u6a21\u7cca\uff1b\u90a3\u662f\u4e0d\u662f\u53ef\u4ee5\u5c3d\u91cf\u7f29\u5c0f\u5149\u5708\u7684\u5b54\u5f84\u5462\uff1f
NO.\u5982\u679c\u5b54\u5f84\u592a\u5c0f\uff0c\u53ef\u901a\u8fc7\u7684\u5149\u7ebf\u5c31\u5f88\u5c11\uff0c\u5bfc\u81f4\u5149\u5f3a\u592a\u5f31\uff0c\u540c\u65f6\u5f53\u5b54\u5f84\u5c0f\u5230\u4e00\u5b9a\u7a0b\u5ea6\u65f6\uff0c\u4f1a\u4ea7\u751f\u884d\u5c04\u73b0\u8c61\u3002
"},{"location":"DIP/Basic/DIP/#lenses","title":"Lenses","text":"\u901a\u8fc7\u4f7f\u7528\u900f\u955c\u53ef\u4ee5\u4f1a\u805a\u66f4\u591a\u7684\u5149\u7ebf\u5230\u4e00\u4e2a\u6210\u50cf\u70b9\uff0c\u53ea\u662f\u53ea\u6709\u5728\u7279\u6b8a\u7684\u4f4d\u7f6e\u4e0a\u624d\u80fd\u8fbe\u5230\u8fd9\u4e2a\u76ee\u6807\uff0c\u5373\u5927\u5bb6\u4ee5\u524d\u5c31\u5b66\u4e60\u8fc7\u7684\u900f\u955c\u805a\u7126
\u4f4d\u4e8e\u5408\u9002\u8ddd\u79bb\u7684\u7269\u4f53\u70b9\u53ef\u4ee5\u6210\u6e05\u6670\u50cf\uff0c\u5176\u4ed6\u7684\u70b9\u5219\u4f1a\u4ea7\u751f\u5f25\u6563\u5706 \u901a\u8fc7\u6539\u53d8\u955c\u5934\u7684\u5f62\u72b6\u53ef\u4ee5\u6539\u53d8\u805a\u7126\u65f6\u7684\u7269\u4f53\u70b9\u6240\u9700\u8981\u7684\u8ddd\u79bb
\u666f\u6df1\u968f\u955c\u5934\u7684\u7126\u8ddd\u3001\u5149\u5708\u503c\u3001\u62cd\u6444\u8ddd\u79bb\u800c\u53d8\u5316
(1)\u955c\u5934\u5149\u5708\uff1a\u5149\u5708\u8d8a\u5927\uff0c\u666f\u6df1\u8d8a\u5c0f\uff1b\u5149\u5708\u8d8a\u5c0f\uff0c\u666f\u6df1\u8d8a\u5927
(2)\u955c\u5934\u7126\u8ddd\uff1a\u955c\u5934\u7126\u8ddd\u8d8a\u957f\uff0c\u666f\u6df1\u8d8a\u5c0f\uff1b\u7126\u8ddd\u8d8a\u77ed\uff0c\u666f\u6df1\u8d8a\u5927
(3)\u62cd\u6444\u8ddd\u79bb\uff1a\u8ddd\u79bb\u8d8a\u8fdc\uff0c\u666f\u6df1\u8d8a\u5927\uff1b\u8ddd\u79bb\u8d8a\u8fd1\uff0c\u666f\u6df1\u8d8a\u5c0f
For a given subject framing and camera position, the DOF is controlled by the lens aperture diameter, which is usually specified as the f-number, the ratio of lens focal length to aperture diameter.
Reducing the aperture diameter (increasing the f-number) increases the DOF; however, it also reduces the amount of light transmitted, and increases diffraction, placing a practical limit on the extent to which DOF can be increased by reducing the aperture diameter.
"},{"location":"DIP/Basic/DIP/#digital-camera","title":"Digital camera","text":"\u5f53CCD\u66dd\u5149\u540e\uff0c\u5149\u7535\u4e8c\u6781\u7ba1\u53d7\u5230\u5149\u7ebf\u7684\u6fc0\u53d1\u800c\u91ca\u653e\u51fa\u7535\u8377\uff0c\u751f\u6210\u611f\u5149\u5143\u4ef6\u7684\u7535\u4fe1\u53f7
CCD\u63a7\u5236\u82af\u7247\u5229\u7528\u611f\u5149\u5143\u4ef6\u4e2d\u7684\u63a7\u5236\u4fe1\u53f7\u7ebf\u8def\u5bf9\u53d1\u5149\u4e8c\u6781\u7ba1\u4ea7\u751f\u7684\u7535\u6d41\u8fdb\u884c\u63a7\u5236\uff0c\u7531\u7535\u6d41\u4f20\u8f93\u7535\u8def\u8f93\u51fa\uff0cCCD\u4f1a\u5c06\u4e00\u6b21\u6210\u50cf\u4ea7\u751f\u7684\u7535\u4fe1\u53f7\u6536\u96c6\u8d77\u6765\uff0c\u7edf\u4e00\u8f93\u51fa\u5230\u653e\u5927\u5668\u3002
Colorful view is generated by the interactions between light and objects, e.g., reflection, refraction, scattering, transmission, absorption, and diffraction.
\u8272\u5f69\u573a\u666f\u662f\u7531\u5149\u4e0e\u7269\u4f53\u4e4b\u95f4\u7684\u4ea4\u4e92\u8fc7\u7a0b\u4ea7\u751f\u7684\u3002\u5982\uff1a\u53cd\u5c04\u3001\u6298\u5c04\u3001\u6563\u5c04\u3001\u4f20\u64ad\u3001\u5438\u6536\u4ee5\u53ca\u884d\u5c04\u7b49\u7b49
\u8272\u5f69\u53ef\u4ee5\u5206\u4e3a\u5f69\u8272\uff08chromatic color\uff09\u548c\u6d88\u8272\uff08achromatic color\uff09\u4e24\u5927\u7c7b
\u8272\u89c9(colour vision)\u662f\u6307\u4e0d\u540c\u6ce2\u957f\u7684\u5149\u7ebf\u4f5c\u7528\u4e8e\u89c6\u7f51\u819c\u800c\u5728\u5927\u8111\u4e2d\u5f15\u8d77\u7684\u611f\u89c9
\u4eba\u773c\u53ef\u89c1\u5149\u7ebf\u7684\u6ce2\u957f\u662f390nm\uff5e780nm\uff0c\u4e00\u822c\u53ef\u8fa8\u51fa\u5305\u62ec\u7d2b\u3001\u84dd\u3001\u9752\u3001\u7eff\u3001\u9ec4\u3001\u6a59\u3001\u7ea27\u79cd\u4e3b\u8981\u989c\u8272\u5728\u5185\u7684120\uff5e180\u79cd\u4e0d\u540c\u7684\u989c\u8272
\u89c6\u7f51\u819c\u662f\u4eba\u773c\u4e2d\u6700\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u5c31\u50cf\u76f8\u673a\u91cc\u7684\u611f\u5149\u5e95\u7247\uff0c\u4e13\u95e8\u8d1f\u8d23\u611f\u5149\u6210\u50cf\u3002\u89c6\u7f51\u819c\u4e0a\u5206\u5e03\u7740\u4e24\u79cd\u89c6\u89c9\u7ec6\u80de\uff0c\u4e00\u79cd\u4e3a\u6746\u72b6\u4f53\uff0c\u53e6\u4e00\u79cd\u4e3a\u9525\u72b6\u4f53
\u6746\u72b6\u4f53\u7ec6\u80de\u6bd4\u8f83\u591a\uff0c\u5927\u7ea6\u6709\u4e0a\u4ebf\u4e2a\uff0c\u5b83\u5bf9\u5149\u6781\u4e3a\u7075\u654f\uff0c\u4f46\u6ca1\u6709\u533a\u5206\u8272\u5f69\u7684\u80fd\u529b
\u9525\u72b6\u4f53\u7ec6\u80de\u5219\u53ea\u6709\u516d\u3001\u4e03\u767e\u4e07\u4e2a\uff0c\u5b83\u8981\u5728\u8f83\u5f3a\u7684\u7167\u5ea6\u4e0b\u624d\u80fd\u6fc0\u53d1\uff0c\u5b83\u7684\u5b58\u5728\u4f7f\u6211\u4eec\u80fd\u591f\u8fa8\u522b\u5404\u79cd\u4e0d\u540c\u7684\u989c\u8272
\u89c6\u7f51\u819c\u4e0a\u5b58\u5728\u4e09\u79cd\u5206\u522b\u5bf9\u7ea2\u7eff\u84dd\u5149\u6ce2\u957f\u7279\u522b\u654f\u611f\u7684\u89c6\u9525\u7ec6\u80de\u6216\u76f8\u5e94\u7684\u611f\u5e94\u8272\u7d20\uff0c\u4e0d\u540c\u6ce2\u957f\u7684\u5149\u8fdb\u5165\u773c\u775b\u540e\uff0c\u4e0e\u4e4b\u76f8\u7b26\u6216\u76f8\u8fd1\u7684\u89c6\u9525\u7ec6\u80de\u53d1\u751f\u4e0d\u540c\u7a0b\u5ea6\u7684\u5174\u594b\uff0c\u4e8e\u662f\u5728\u5927\u8111\u4ea7\u751f\u76f8\u5e94\u7684\u8272\u89c9\uff1b\u82e5\u4e09\u79cd\u89c6\u9525\u7ec6\u80de\u53d7\u5230\u540c\u7b49\u7a0b\u5ea6\u7684\u523a\u6fc0\uff0c\u90a3\u4e48\u4ea7\u751f\u6d88\u8272
"},{"location":"DIP/Basic/DIP/#perception-priority-and-sensitivity","title":"Perception priority and sensitivity","text":"Priority: Under the same setting, human notices first the hue (H) (\u8272\u8c03)changes, then the saturation (S) (\u9971\u548c\u5ea6), then the lightness value (V) (\u4eae\u5ea6).
Sensitivity: Human eyes are the most sensitive to the lightness changes and have the best resolution, which is responsible for the eyes\u2019 HDR capacity
\u4eba\u773c\u5bf9\u4e8e\u4eae\u5ea6\u7684\u53d8\u5316\u6700\u4e3a\u654f\u611f\uff0c\u5206\u8fa8\u80fd\u529b\u6700\u5f3a\u3002\u6070\u597d\u4e0e\u4eba\u773c\u7684\u9ad8\u52a8\u6001\u80fd\u529b\u76f8\u5339\u914d\u3002
"},{"location":"DIP/Basic/DIP/#definition-of-color-space","title":"Definition of color space","text":"RGB color model is a unit cube in a Cartesian coordinates system.
The magnitudes of each primary color are equivalent on the main diagonal line, which lead to the white color from darkness to brightness, i.e., grayscale. (0,0,0)-dark, (1,1,1)-bright.
The other 6 corners are respectively red, yellow, cyan, blue and magenta.(\u7ea2\u3001\u9ec4\u3001\u7eff\u3001\u9752\u3001\u84dd\u548c\u54c1\u7ea2)
RGB is a subset of CIE primary color space.
RGB is usually used in Color cathode ray tube and Color raster graphics display (computer , TV).
RGB\u989c\u8272\u6a21\u578b\u901a\u5e38\u7528\u4e8e\u5f69\u8272\u9634\u6781\u5c04\u7ebf\u7ba1\u548c\u5f69\u8272\u5149\u6805\u56fe\u5f62\u663e\u793a\u5668\uff08\u8ba1\u7b97\u673a\u548c\u7535\u89c6\u673a\u91c7\u7528\uff09
"},{"location":"DIP/Basic/DIP/#cmy-color-model","title":"CMY color model","text":"\u5f69\u8272\u5370\u5237\u6216\u8005\u5f69\u8272\u6253\u5370\u7684\u7eb8\u5f20\u4e0d\u80fd\u53d1\u5c04\u5149\u7ebf\uff0c\u56e0\u800c\u5370\u5237\u673a\u6216\u6253\u5370\u673a\u5c31\u53ea\u80fd\u4f7f\u7528\u4e00\u4e9b\u80fd\u591f\u5438\u6536\u7279\u5b9a\u5149\u6ce2\u800c\u53cd\u5c04\u5176\u4ed6\u5149\u6ce2\u7684\u6cb9\u58a8\u6216\u8005\u989c\u8272
\u6cb9\u58a8\u6216\u989c\u6599\u76843\u79cd\u57fa\u8272\u662f\u4ee5\u7ea2\u3001\u7eff\u3001\u84dd\u4e09\u8272\u7684\u8865\u8272 : \u9752\uff08Cyan\uff09\u3001\u54c1\u7ea2\uff08Magenta\uff09\u3001\u9ec4\uff08Yellow\uff09\u4e3a\u57fa\u8272
\u7528CMY\u6a21\u578b\u4ea7\u751f\u7684\u989c\u8272\u79f0\u4e3a\u76f8\u51cf\u8272\uff0c\u662f\u56e0\u4e3a\u5b83\u51cf\u6389\u4e86\u4e3a\u89c6\u89c9\u7cfb\u7edf\u8bc6\u522b\u989c\u8272\u6240\u9700\u8981\u7684\u53cd\u5c04\u5149
HIS/HSV color space is defined based on human visual system.
\u8272\u8c03\uff08Hue)\u3001\u8272\u9971\u548c\u5ea6\uff08Saturation\uff09\u548c\u4eae\u5ea6\uff08Intensity\uff0c\u6216\u8005Value\uff09
Advantages:
Device independent:
XYZ concept is based on the three-primary color theory, which describes human eyes have RGB receptors and all other color are mixture of RBG.
Computed by using functions\\(\\overline{x(\\lambda)},\\overline{y(\\lambda)},\\overline{z(\\lambda)}\\)
\\(Y_{xy}\\)color space given in 1931: Y->luminance, x,y are color coordinates computed based XYZ.
\u5728\u73b0\u4ee3\u5f69\u8272\u7535\u89c6\u7cfb\u7edf\u4e2d\uff0c\u901a\u5e38\u91c7\u7528\u4e09\u7ba1\u5f69\u8272\u6444\u50cf\u673a\u6216\u5f69\u8272CCD\uff08\u7535\u8377\u8026\u5408\u5668\u4ef6\uff09\u6444\u50cf\u673a\uff0c\u5b83\u628a\u6444\u5f97\u7684\u5f69\u8272\u56fe\u50cf\u4fe1\u53f7\uff0c\u7ecf\u5206\u8272\uff0c\u5206\u522b\u653e\u5927\u6821\u6b63\u5f97\u5230RGB\uff0c\u518d\u7ecf\u8fc7\u77e9\u9635\u53d8\u6362\u7535\u8def\u5f97\u5230\u4eae\u5ea6\u4fe1\u53f7Y\u548c\u4e24\u4e2a\u8272\u5dee\u4fe1\u53f7R\uff0dY\u3001B\uff0dY\uff0c\u6700\u540e\u53d1\u9001\u7aef\u5c06\u4eae\u5ea6\u548c\u8272\u5dee\u4e09\u4e2a\u4fe1\u53f7\u5206\u522b\u8fdb\u884c\u7f16\u7801\uff0c\u7528\u540c\u4e00\u4fe1\u9053\u53d1\u9001\u51fa\u53bb,\u8fd9\u5c31\u662f\u6211\u4eec\u5e38\u7528\u7684YUV\u989c\u8272\u7a7a\u95f4
\u91c7\u7528YUV\u989c\u8272\u7a7a\u95f4\u7684\u91cd\u8981\u6027\u662f\u5b83\u7684\u4eae\u5ea6\u4fe1\u53f7Y\u548c\u8272\u5ea6\u4fe1\u53f7U\u3001V\u662f\u5206\u79bb\u7684
\\(RGB->CMY\\\\ C = 255 \u2013 R\\\\ M = 255 \u2013 G\\\\ Y = 255 \u2013 B\\\\\\)
"},{"location":"DIP/Basic/DIP/#rgb-hsv","title":"RGB<->HSV","text":"Search in table:
\\(RGB<->XYZ\\\\HSV<->XYZ\\)
\\(\\begin{gathered}\\begin{bmatrix} X \\\\ Y \\\\ Z\\end{bmatrix}=\\begin{bmatrix} 0.608 & 0.714 & 0.200 \\\\0.299& 0.587&0.133 \\\\0.000 &0.066& 1.112\\end{bmatrix}\\begin{bmatrix} R \\\\ G\\\\ B\\end{bmatrix}\\quad\\end{gathered}\\)
"},{"location":"DIP/Basic/DIP/#cie-xyz-cie-lab","title":"CIE XYZ<->CIE Lab*","text":""},{"location":"DIP/Basic/DIP/#rgb-yuv","title":"RGB \u548c YUV \u7684\u76f8\u4e92\u8f6c\u5316","text":"RGB\uff08\u7ea2\u7eff\u84dd\uff09\u548cYUV\u662f\u4e24\u79cd\u989c\u8272\u8868\u793a\u65b9\u5f0f\uff0c\u5e38\u7528\u4e8e\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u4e2d\u3002RGB\u662f\u4e00\u79cd\u76f4\u63a5\u8868\u793a\u989c\u8272\u7684\u65b9\u5f0f\uff0c\u5176\u4e2d\u7ea2\u8272\uff08R\uff09\u3001\u7eff\u8272\uff08G\uff09\u548c\u84dd\u8272\uff08B\uff09\u5206\u91cf\u7684\u503c\u51b3\u5b9a\u4e86\u989c\u8272\u7684\u5177\u4f53\u5916\u89c2
0.0 ~ 1.0
\u6216\u4ee5\u6574\u6570\u8868\u793a\uff1a\u53d6\u503c\u8303\u56f4\u4e3a 0 ~ 255
\u6216\u8005 00 ~ FF
\u3002RGB \u989c\u8272\u6a21\u578b \u901a\u5e38\u7528\u4e8e\u5f69\u8272\u9634\u6781\u5c04\u7ebf\u7ba1\u548c\u5f69\u8272\u5149\u6805\u56fe\u5f62\u663e\u793a\u5668(\u8ba1\u7b97\u673a\u548c\u7535\u89c6\u673a\u91c7\u7528)\u3002
YUV\u662f\u4e00\u79cd\u989c\u8272\u7a7a\u95f4\uff0c\u57fa\u4e8e YUV
\u7684\u989c\u8272\u7f16\u7801\u662f\u6d41\u5a92\u4f53\u7684\u5e38\u7528\u7f16\u7801\u65b9\u5f0f\uff0c\u8fd9\u79cd\u8868\u8fbe\u65b9\u5f0f\u8d77\u521d\u662f\u4e3a\u4e86\u5f69\u8272\u7535\u89c6\u4e0e\u9ed1\u767d\u7535\u89c6\u4e4b\u95f4\u7684\u4fe1\u53f7\u517c\u5bb9\uff1b\u5176\u4e2d\uff1aY\uff1a\u8868\u793a\u660e\u4eae\u5ea6\uff08Luminance \u6216 Luma\uff09\uff0c\u4e5f\u79f0\u7070\u5ea6\u56fe\u3002U\u3001V\uff1a\u8868\u793a\u8272\u5ea6\uff08Chrominance \u6216 Chroma\uff09\uff0c\u4f5c\u7528\u662f\u63cf\u8ff0\u5f71\u50cf\u7684\u8272\u5f69\u53ca\u9971\u548c\u5ea6\uff0c\u7528\u4e8e\u6307\u5b9a\u50cf\u7d20\u7684\u989c\u8272\u3002
\\(\\begin{gathered}\\begin{bmatrix} Y \\\\ U \\\\ V\\end{bmatrix}=\\begin{bmatrix} 0.299 & 0.587 & 0.114 \\\\ \u22120.147& \u22120.289 &0.435 \\\\0.615 &\u22120.515& \u22120.100\\end{bmatrix}\\begin{bmatrix} R \\\\ G\\\\ B\\end{bmatrix}\\quad\\end{gathered}\\)
\\(\\begin{gathered}\\quad\\quad\\begin{bmatrix} R \\\\ G \\\\ B\\end{bmatrix}=\\begin{bmatrix} 1.0000 &0.0000& 1.3707 \\\\1.0000& \u22120.3376 &-0.6980 \\\\ 1.0000&1.7324& 0.0000\\end{bmatrix}\\begin{bmatrix} Y \\\\ U\\\\ V\\end{bmatrix}\\quad\\end{gathered}\\)
File extension: .JPEG, .JFIF, .JPG, .JPE
Compression format for static image :
As a counterpart of Moving Picture Expert Group.
Lossy encoding method
Allow user to make balance between image quality and image size.
Encoding based on transformation, e.g. Discrete Cosine Transformation (DCT)JPEG2000 is based on wavelet.
Different manipulations for high-frequency signal and low-frequency signal.
Compression strategy: According to the requirement of compression ratio, remove information from high frequency to low frequency.
Advantages:
\u538b\u7f29\u7b56\u7565\uff1a\u6839\u636e\u538b\u7f29\u6bd4\u8981\u6c42\uff0c\u4ece\u9ad8\u9891\u5230\u4f4e\u9891\u9010\u6b65\u524a\u51cf\u4fe1\u606f
More specifically,refer to the PPT(Chapter I)
"},{"location":"DIP/Basic/DIP/#disadvantages","title":"Disadvantages","text":"\u4e0d\u9002\u5408\u7528\u4e8e\u7ebf\u6761\u753b\u3001\u6587\u5b57\u3001\u56fe\u6807\u7b49\uff0c\u5176\u6709\u635f\u538b\u7f29\u4f1a\u5bfc\u81f4\u8fd9\u7c7b\u5bf9\u8c61\u7684\u7455\u75b5\u4e25\u91cd
"},{"location":"DIP/Basic/DIP/#tiff-format","title":"TIFF format","text":""},{"location":"DIP/Basic/DIP/#gif-format","title":"GIF format","text":""},{"location":"DIP/Basic/DIP/#chains","title":"Chains","text":"Chains are used for description of object borders
Chains can be represented using static data structures (e.g., 1D arrays); their size is the longest length of the chain expected.
"},{"location":"DIP/Basic/DIP/#run-length-encoding-rle","title":"Run length encoding (RLE)","text":"RLE is run-length encoding. It is used to encode the location of foreground objects in segmentation. Instead of outputting a mask image, you give a list of start pixels and how many pixels after each of those starts is included in the mask.
ImageNet, Kaggle Facial Expression, Kaggle Multimodal Learning, German Traffic Signs, Handwriting, \u2026.
CNNs are deployed in many practical applications
Image recognition, speech recognition, Google\u2019s and Baidu\u2019s photo taggers
xCNNs are applicable to array data where nearby values are correlated
Images, sound, time-frequency representations, video, volumetric images, RGB-Depth images, \u2026
CNN is one of the few deep models that can be trained in supervised way.
https://towardsdatascience.com/understanding-backpropagation-algorithm-7bb3aa2f95fd
http://galaxy.agh.edu.pl/%7Evlsi/AI/backp_t_en/backprop.html
\u5b66\u4e60\u7387\u4f4e\uff0c\u6536\u655b\u6162\uff0c\u5bb9\u6613\u6389\u5230\u5751\u91cc\u9677\u5165\u5c40\u90e8\u6700\u4f18\uff1b\u5b66\u4e60\u7387\u9ad8\uff0c\u53ef\u80fd\u4f7f\u5f97\u6536\u655b\u8fc7\u7a0b\u4e0d\u7a33\u5b9a\uff0c\u6765\u56de\u9707\u8361\uff0c\u4e00\u76f4\u4e0d\u6536\u655b
idea:
Example:
\u4e3a\u4ec0\u4e48\u5f15\u5165CNN\u53ef\u4ee5\u5927\u89c4\u6a21\u51cf\u5c11\u6743\u6570\u53c2\u6570\u8bad\u7ec3\u91cf\u56e0\u4e3aCNN\u901a\u8fc7
1\uff09 \u5c40\u90e8\u8fde\u63a5\uff08Local Connectivity\uff09
2\uff09 \u6743\u503c\u5171\u4eab\uff08Shared Weights\uff09
3\uff09 \u6c60\u5316\uff08Pooling\uff09
CNN\u6c60\u5316\u53ef\u4ee5\u901a\u8fc7\u6c60\u5316\u5c42\u6765\u964d\u4f4e\u5377\u79ef\u5c42\u8f93\u51fa\u7684\u7279\u5f81\u7ef4\u5ea6\uff0c\u5728\u6709\u6548\u51cf\u5c11\u7f51\u7edc\u53c2\u6570\u7684\u540c\u65f6\u8fd8\u53ef\u4ee5\u9632\u6b62\u8fc7\u62df\u5408\u73b0\u8c61
https://zhuanlan.zhihu.com/p/78760534
"},{"location":"DIP/DL/DL/#cnn-in-detail","title":"CNN in Detail","text":"\u6b65\u957f\uff1f
Refer to the PPT !
Many mathematicians tried to find a way to approximate a complex function by some simple functions for many years. Apart from the theoretical concern, this technique is invaluable for many practical applications.
Before the invention of differential calculus, this problem kept open.
"},{"location":"DIP/Fourier/Fourier/#fourier-transform","title":"Fourier Transform","text":"\\(\\begin{align*}f(x)&=\\frac{1}{2}a_0+\\sum_{n=1}^{\\infty}a_ncos(nx)+\\sum_{n=1}^{\\infty}b_nsin(nx)\\\\ a_0 &= \\frac{1}{\\pi}\\int_{-\\pi}^{\\pi}f(x)dx\\\\a_n &= \\frac{1}{\\pi}\\int_{-\\pi}^{\\pi}f(x)cos(nx)dx\\\\b_n &= \\frac{1}{\\pi}\\int_{-\\pi}^{\\pi}f(x)sin(nx)dx \\end{align*}\\)
Just like Calculus invented by Newton, Fourier analysis is another mathematical tool
Magnitude-Phase\uff08vector\uff09\\(x=|x|e^{j\\phi(x)}\\) where \\(|x|=\\sqrt{a^2+b^2}\\ \\phi(x)=tan^{-1(\\frac{b}{a})}\\)
Basic Properties
\\(xy=|x|e^{j\\phi(x)}|y|e^{j\\phi(y)}=|x||y|e^{j(\\phi(x)+\\phi(y))}\\)
\\(|x|=|x^*|\\)
\\(\\phi(x)=-\\phi(x^*)\\)
\\(xx^*=|x|^2\\)
"},{"location":"DIP/Fourier/Fourier/#image-transform","title":"Image Transform","text":"
Many times, image processing tasks are best performed in transformed domain other than the spatial domain.
Key steps:
(1) Transform the image
(2) Carry out the task(s) in the transformed domain.
(3) Apply inverse transform to return to the spatial domain.
"},{"location":"DIP/Fourier/Fourier/#transformation-kernels","title":"Transformation Kernels","text":""},{"location":"DIP/Fourier/Fourier/#continuous-fourier-transform-ft","title":"Continuous Fourier Transform (FT)","text":"Transforms a signal (i.e., function) from the spatial domain to the frequency domain.
To remove certain frequencies, set their corresponding F(u) coefficients to zero!
Low frequencies correspond to slowly varying information (e.g., continuous surface).
High frequencies correspond to quickly varying information (e.g., edges)
\u4e8c\u7ef4\u79bb\u6563\u5085\u7acb\u53f6\u53d8\u6362\u5b9a\u4e49\u4e3a
\u4e8c\u7ef4\u79bb\u6563\u5085\u7acb\u53f6\u9006\u53d8\u6362\u5b9a\u4e49\u4e3a
\u4e3a\u4e86\u65b9\u4fbf\uff0c\u6211\u4eec\u53ef\u5c06\u4e0a\u8ff0\u4e24\u5f0f\u6539\u4e3a\u5411\u91cf\u5f62\u5f0f
\u5176\u4e2d \u3002
For Images
\\((u,v)\\)-- \u76f8\u4f4d
\\(F(u,v)\\)-- \u5e45\u503c
"},{"location":"DIP/Fourier/Fourier/#fast-fourier-transformfft","title":"Fast Fourier Transform\uff08FFT\uff09","text":"Speed up the discrete Fourier Transform
There are many repetitive work in the computation.
Cooley and Tukey presented in 1965:
HENCE \\(F(k+M)=\\lfloor F_e(k)-F_o(k)W_{2M}^k\\rfloor\\)!!
\u610f\u4e49\uff1a\u5bf9\u4e00\u4e2a\u957f\u5ea6\u4e3a N \u7684\u5e8f\u5217\u8fdb\u884c\u5085\u7acb\u53f6\u53d8\u6362\u53ef\u4ee5\u901a\u8fc7\u5c06\u5176\u5206\u6210\u4e24\u534a\u8ba1\u7b97\uff0c\u5bf9\u7b2c\u4e00\u90e8\u5206\u7684\u8ba1\u7b97\u9700\u8981\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u957f\u5ea6\u4e3a N/2 \u957f\u5ea6\u5e8f\u5217\u7684\u5085\u7acb\u53f6\u53d8\u6362\u5f0f\u8fdb\u884c\uff0c\u7136\u540e\u5229\u7528\u8fd9\u4e24\u4e2a\u957f\u5ea6\u4e3a N/2 \u7684\u5e8f\u5217\u53ef\u4ee5\u5f97\u5230\u7b2c\u4e8c\u90e8\u5206\u7684\u503c
Hint: use inverse DFT to reconstruct the image by magnitude or phase only information
magnitude determines the contribution of each component!(cannot be used to reconstruct a image!)
phase determines which components are present!
PHASE is more important!
"},{"location":"DIP/chap2/chap2/#binary-image-and-morphological-operation","title":"Binary Image and Morphological Operation","text":""},{"location":"DIP/chap2/chap2/#binary-image","title":"Binary image","text":"
\u4f18\u70b9\uff1a\u66f4\u5c0f\u7684\u5185\u5b58\u9700\u6c42 \u8fd0\u884c\u901f\u5ea6\u66f4\u5feb \u4e3a\u4e8c\u503c\u56fe\u50cf\u5f00\u53d1\u7684\u7b97\u6cd5\u5f80\u5f80\u53ef\u4ee5\u7528\u4e8e\u7070\u5ea6\u7ea7\u56fe\u50cf \u66f4\u4fbf\u5b9c \u7f3a\u70b9\uff1a\u5e94\u7528\u8303\u56f4\u6bd5\u7adf\u6709\u9650\uff1b \u66f4\u65e0\u6cd5\u63a8\u5e7f\u5230\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u8868\u73b0\u529b\u6b20\u7f3a\uff0c\u4e0d\u80fd\u8868\u73b0\u7269\u4f53\u5185\u90e8\u7ec6\u8282\u65e0\u6cd5\u63a7\u5236\u5bf9\u6bd4\u5ea6
"},{"location":"DIP/chap2/chap2/#_1","title":"\u56fe\u50cf\u4e8c\u503c\u5316\uff1a","text":""},{"location":"DIP/chap2/chap2/#1","title":"\uff081\uff09\u4e8c\u503c\u56fe\u50cf","text":"\u4e8c\u503c\u56fe\u50cf\uff08Binary Image\uff09\u4e2d\uff0c\u50cf\u7d20\u503c\u9650\u5b9a\u4e3a \\(0\\) \u6216 \\(1\\)\uff08\u7f16\u7a0b\u4e2d\u4e00\u822c\u7528 \\(0\\) \u548c\\(255\\) \u6765\u6784\u9020\u4e8c\u503c\u56fe\u50cf)\u3002
\u5728\u628a\u7070\u5ea6\u56fe\u8f6c\u6362\u4e3a\u4e8c\u503c\u56fe\u50cf\u7684\u65f6\u5019\uff0c\u4e00\u4e2a\u57fa\u672c\u7684\u60f3\u6cd5\u662f\u627e\u5230\u4e00\u4e2a\u5408\u9002\u7684\u9608\u503c\uff08Threshold\uff09\uff0c\u50cf\u7d20\u70b9\u503c\u6bd4\u9608\u503c\u5927\u7684\u8bbe\u4e3a\\(255\\)\uff0c\u6bd4\u9608\u503c\u5c0f\u7684\u8bbe\u4e3a\\(0\\)\u3002\u5373\u4e3a\u5982\u4e0b\uff1a
\\(\\left\\{ \\begin{array}{**lr**} I(x,y) = 0\\ if\\ I(x,y)\\leq Threshold \\\\ I(x,y) = 255\\ if\\ I(x,y)\\geq Threshold \\\\ \\end{array} \\right.\\)
a.\u7edf\u8ba1\u50cf\u7d20\u70b9\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c
b.\u5bf9\u4e8e\u6bcf\u4e2a\u53ef\u80fd\u7684\u9608\u503c\uff0c\u8ba1\u7b97\u5206\u6210\u4e24\u4e2a\u7c7b\u522b\u540e\u7684\u80cc\u666f\u548c\u524d\u666f\u7684\u5185\u90e8\u534f\u65b9\u5dee\u548c\u5916\u90e8\u534f\u65b9\u5dee\u3002
c.\u9009\u62e9\u6700\u5927\u5916\u90e8\u548c\u6700\u5c0f\u5185\u90e8\u534f\u65b9\u5dee\u5bf9\u5e94\u7684 threshold\u4f5c\u4e3a\u4e8c\u503c\u5316\u9608\u503c\u3002
d.\u5e94\u7528\u9009\u62e9\u7684\u9608\u503c\u5c06\u56fe\u50cf\u5206\u5272\u6210\u524d\u666f\u548c\u80cc\u666f\u3002
\u5176\u4e2d\uff0c\u6700\u5927\u5916\u90e8\u65b9\u5dee\u548c\u6700\u5c0f\u5185\u90e8\u65b9\u5dee\u662f\u7b49\u4ef7\u7684\uff0c\u6211\u4eec\u9009\u62e9\u627e\u5230\u6700\u5927\u5916\u90e8\u65b9\u5dee\uff0c\u63a8\u5bfc\u8fc7\u7a0b\u5982\u4e0b\uff1a
\u8bbe\u56fe\u50cf\u5c3a\u5bf8\u4e3a\\(M\\ *\\ N\\)\uff0c\u5176\u4e8c\u503c\u5316\u7684\u6700\u4f73\u9608\u503c\u4e3a\\(T\\)\uff0c\u8be5\u9608\u503c\u5c06\u56fe\u50cf\u5206\u4e3a\u80cc\u666f\u548c\u76ee\u6807\u4e24\u4e2a\u7c7b\u522b\u3002\u5176\u4e2d\u5c5e\u4e8e\u80cc\u666f\u7684\u50cf\u7d20\u70b9\u6570\u91cf\u4e3a\\(N0\\),\u5c5e\u4e8e\u76ee\u6807\u7684\u50cf\u7d20\u70b9\u6570\u91cf\u4e3a\\(N1\\),\u80cc\u666f\u50cf\u7d20\u70b9\u6570\u5360\u6574\u5e45\u56fe\u50cf\u7684\u6bd4\u4f8b\\(\\omega0\\),\u5176\u7070\u5ea6\u5747\u503c\\(\u03bc0\\),\u76ee\u6807\u50cf\u7d20\u70b9\u6570\u5360\u6574\u5e45\u56fe\u50cf\u7684\u6bd4\u4f8b\u4e3a\\(\u03c91\\),\u5176\u7070\u5ea6\u5747\u503c\u4e3a\\(\u03bc1\\) \uff0c\u6574\u5e45\u56fe\u50cf\u7684\u7070\u5ea6\u5747\u503c\u4e3a\\(\\mu\\)\u3002
\\(\\omega_0=\\frac{N0}{M*N}\\\\\\) \\(\\omega_1=\\frac{N1}{M*N}\\) \\(N0+N1=M*N\\\\\\) \\(\\omega0\\ + \\omega1\\ = 1\\\\\\)
\\(\\mu = \\frac{\\mu0*N0+\\mu1*N1}{M*N}=\\mu0\\omega0\\ + \\mu1\\omega1\\\\\\)
\\(\u7c7b\u5185\u65b9\u5dee(Within-class\\ variance)\u516c\u5f0f:\\)
\\(\\sigma_{Withen}^{2}=\\omega_{0} \\sigma_{0}^{2}+\\omega_{1} \\sigma_{1}^{2} \\\\\\)
\\(\u7c7b\u95f4\u65b9\u5dee(Between-class\\ variance)\u7684\u516c\u5f0f:\\sigma_{Between}^{2}=\\sigma^{2}-\\sigma_{Within}^{2}\\\\\\)
\\(\u6709\u6b64\u53ef\u63a8\u5bfc\u5f97\u5230\\)
\\(\\sigma_{Between}^{2}=\\omega_{0}(\\mu_{0}-\\mu)^{2}+\\omega_{1}(\\mu_{1}-\\mu)^{2}\\)
\\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ =\\omega0(\\mu0-\\omega0\\mu0-\\omega1\\mu1)^{2}+\\omega1(\\mu1-\\omega0\\mu0-\\omega1\\mu1)^{2}\\) \\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ =\\omega0(\\omega1\\mu0-\\omega1\\mu1)^{2}+\\omega1(\\omega0\\mu1-\\omega0\\mu0)^{2}\\\\\\) \\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ =(\\omega0\\omega1^{2}+\\omega1\\omega0^{2})(\\mu1-\\mu0)^{2}\\\\\\)
\\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ =\\omega0\\omega1(\\mu0-\\mu1)^{2}\\\\\\)
Step 1: \u786e\u5b9a\u539f\u59cb\u56fe\u50cf\u4e2d\u50cf\u7d20\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c
Step 2: \u6700\u5c0f\u503c\u52a01\u4f5c\u4e3a\u521d\u59cbthreshold\u5bf9\u539f\u59cb\u56fe\u50cf\u8fdb\u884c\u4e8c\u503c\u5316\u64cd\u4f5c
Step 3: \u6839\u636e\u5bf9\u5e94\u5173\u7cfb\u786e\u5b9a\u524d\u666f\u548c\u80cc\u666f\uff0c\u5206\u522b\u8ba1\u7b97\u5f53\u524dthreshold\u4e0b\u7684\u5185\u90e8\u534f\u65b9\u5dee\u548c\u5916\u90e8\u534f\u65b9\u5dee
Step 4: \u56de\u5230Step 2\u76f4\u5230\u8fbe\u5230\u50cf\u7d20\u6700\u5927\u503c
Step 5\uff1a\u627e\u5230\u6700\u5927\u5916\u90e8\u548c\u6700\u5c0f\u5185\u90e8\u534f\u65b9\u5dee\u5bf9\u5e94\u7684threshold.
\u5bf9\u4e8e\u6bcf\u4e00\u7a97\u53e3\u4f4d\u7f6e\uff0c\u786e\u5b9a\u9488\u5bf9\u8be5\u7a97\u53e3\u7684threshold
\u76f8\u6bd4\u5168\u5c40\u5927\u6d25\u6cd5\uff0c\u6211\u4eec\u4f9d\u6b21\u679a\u4e3e\u7070\u5ea6\u56fe\u4e2d\u7684\u6bcf\u4e2a\u50cf\u7d20\\(\uff08i,j)\\)\uff0c\u4ee5\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u4e3a\u8fb9\u957f\uff0c\u5728\\((i - length / 2, j - length / 2, i + length / 2, j + length / 2, i, j)\\)\u8fd9\u4e2a\u6b63\u65b9\u5f62\u4e2d\u8fdb\u884c\u5927\u6d25\u6cd5\uff0c\u5f97\u5230\u9608\u503c\u540e\u4ec5\u5bf9\\((i,j)\\)\u70b9\u8fdb\u884c\u4e8c\u503c\u5316\u5904\u7406\u3002
\u76f8\u6bd4\u5168\u5c40\u5927\u6d25\u6cd5\uff0c\u5c40\u90e8\u5927\u6d25\u6cd5\u8017\u65f6\u663e\u8457\u589e\u52a0\u3002
\u200b \u6570\u5b66\u5f62\u6001\u5b66\uff08\u4e5f\u79f0\u56fe\u50cf\u4ee3\u6570\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5206\u6790\u56fe\u50cf\u5f62\u6001\u548c\u7ed3\u6784\u7684\u5de5\u5177\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u5229\u7528\u7ed3\u6784\u5143\u7d20\u6765\u5ea6\u91cf\u548c\u63d0\u53d6\u5f62\u72b6\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u56fe\u50cf\u7684\u5206\u6790\u548c\u8bc6\u522b\u3002\u56fe\u50cf\u5f62\u6001\u5b66\u64cd\u4f5c\u57fa\u4e8e\u96c6\u5408\u8bba\uff0c\u53ef\u4ee5\u7b80\u5316\u56fe\u50cf\uff0c\u53bb\u9664\u4e0d\u9700\u8981\u7684\u7ed3\u6784\uff0c\u540c\u65f6\u4fdd\u7559\u4e3b\u8981\u7684\u5f62\u72b6\u7279\u5f81\u3002\u5728\u8fd9\u91cc\u6709\u56db\u79cd\u5f62\u6001\u5b66\u64cd\u4f5c\u7b26\uff1a\u81a8\u80c0\u3001\u8150\u8680\u3001\u5f00\u8fd0\u7b97\u548c\u95ed\u8fd0\u7b97\u3002
\u200b \u5728\u4e0b\u9762\u7684\u4ecb\u7ecd\u4e2d\uff0c\u6211\u4eec\u7684 A \u6307\u4e8c\u503c\u56fe\u50cf; B \u6307\u4e8c\u503c\u6a21\u677f\uff0c\u79f0\u4e3a\u7ed3\u6784\u5143(structure element)\u3002
"},{"location":"DIP/chap2/chap2/#_3","title":"\u81a8\u80c0","text":"\u200b \u5f62\u6001\u5b66\u7684\u81a8\u80c0\u64cd\u4f5c\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u79cd\u57fa\u672c\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u589e\u5f3a\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u76ee\u6807\u6216\u7ed3\u6784\uff0c\u4f7f\u5176\u66f4\u52a0\u663e\u8457\u548c\u5bb9\u6613\u68c0\u6d4b\u3002\u81a8\u80c0\u64cd\u4f5c\u7684\u4e3b\u8981\u601d\u60f3\u662f\u5229\u7528\u4e00\u4e2a\u79f0\u4e3a\u7ed3\u6784\u5143\u7d20\u7684\u5c0f\u7a97\u53e3\u6216\u6a21\u677f\uff0c\u6cbf\u7740\u56fe\u50cf\u7684\u5404\u4e2a\u65b9\u5411\u626b\u63cf\uff0c\u5e76\u6839\u636e\u7ed3\u6784\u5143\u7d20\u4e0e\u56fe\u50cf\u7684\u91cd\u53e0\u7a0b\u5ea6\u6765\u6539\u53d8\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u3002
\u200b \u81a8\u80c0\u64cd\u4f5c\u7684\u7ed3\u679c\u901a\u5e38\u662f\u589e\u5f3a\u4e86\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u76ee\u6807\u6216\u7279\u5b9a\u7ed3\u6784\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5c06\u76ee\u6807\u533a\u57df\u6269\u5c55\uff0c\u586b\u5145\u7a7a\u6d1e\uff0c\u4f7f\u8fb9\u7f18\u66f4\u52a0\u6e05\u6670\u3002\u56e0\u6b64\uff0c\u81a8\u80c0\u64cd\u4f5c\u53ef\u7528\u4e8e\u6539\u5584\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u7279\u5f81\uff0c\u4f7f\u5b83\u4eec\u66f4\u5bb9\u6613\u68c0\u6d4b\u548c\u5206\u6790\u3002 $$ A\u2295B={ z|(B)_z \u2229A\\ne\u2205 } $$
Dilation: Bridging gaps in images
\u7531B\u5bf9A\u81a8\u80c0\u6240\u4ea7\u751f\u7684\u4e8c\u503c\u56fe\u8c61D\u662f\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u70b9(x,y)\u7684\u96c6\u5408\uff1a
\u5982\u679cB\u7684\u539f\u70b9\u5e73\u79fb\u5230\u70b9(x,y)\uff0c\u90a3\u4e48\u5b83\u4e0eA\u7684\u4ea4\u96c6\u975e\u7a7a\u3002
"},{"location":"DIP/chap2/chap2/#_4","title":"\u8150\u8680","text":"\u200b \u5f62\u6001\u5b66\u7684\u8150\u8680\u64cd\u4f5c\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u79cd\u57fa\u672c\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u51cf\u5c0f\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u76ee\u6807\u6216\u7ed3\u6784\uff0c\u4ece\u800c\u5f31\u5316\u6216\u6d88\u9664\u4e00\u4e9b\u7279\u5f81\u3002\u8150\u8680\u64cd\u4f5c\u7684\u4e3b\u8981\u601d\u60f3\u662f\u5229\u7528\u4e00\u4e2a\u79f0\u4e3a\u7ed3\u6784\u5143\u7d20\u7684\u5c0f\u7a97\u53e3\u6216\u6a21\u677f\uff0c\u6cbf\u7740\u56fe\u50cf\u7684\u5404\u4e2a\u65b9\u5411\u626b\u63cf\uff0c\u5e76\u6839\u636e\u7ed3\u6784\u5143\u7d20\u4e0e\u56fe\u50cf\u7684\u91cd\u53e0\u7a0b\u5ea6\u6765\u6539\u53d8\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u3002
\u200b \u8150\u8680\u64cd\u4f5c\u7684\u7ed3\u679c\u901a\u5e38\u662f\u51cf\u5c0f\u4e86\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u76ee\u6807\u6216\u7279\u5b9a\u7ed3\u6784\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u53ef\u4ee5\u53bb\u9664\u5c0f\u7684\u7279\u5f81\u3001\u7f29\u5c0f\u76ee\u6807\u533a\u57df\u6216\u6d88\u9664\u566a\u58f0\u3002\u8150\u8680\u64cd\u4f5c\u53ef\u7528\u4e8e\u5f31\u5316\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u7279\u5f81\uff0c\u4f7f\u5176\u66f4\u5bb9\u6613\u68c0\u6d4b\u548c\u5206\u6790\u3002 $$ A\u2296B={(x,y)|(B)_{xy} \u2286A} $$
\u5982\u679cB\u7684\u539f\u70b9\u5e73\u79fb\u5230\u70b9(x,y)\uff0c\u90a3\u4e48B\u5c06\u5b8c\u5168\u5305\u542b\u4e8eA\u4e2d
"},{"location":"DIP/chap2/chap2/#duality","title":"Duality","text":""},{"location":"DIP/chap2/chap2/#application-boundary-extraction","title":"Application: Boundary Extraction","text":""},{"location":"DIP/chap2/chap2/#application-filling-holes","title":"Application: Filling holes\uff01","text":""},{"location":"DIP/chap2/chap2/#_5","title":"\u5f00\u64cd\u4f5c","text":"\u200b \u5f62\u6001\u5b66\u7684\u5f00\u64cd\u4f5c\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5b83\u7ed3\u5408\u4e86\u8150\u8680\uff08erosion\uff09\u548c\u81a8\u80c0\uff08dilation\uff09\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u53bb\u566a\u548c\u5206\u5272\u56fe\u50cf\u4e2d\u7684\u76ee\u6807\u3002\u5f00\u64cd\u4f5c\u7684\u4e3b\u8981\u601d\u60f3\u662f\u9996\u5148\u5bf9\u56fe\u50cf\u6267\u884c\u8150\u8680\u64cd\u4f5c\uff0c\u7136\u540e\u518d\u5bf9\u5176\u6267\u884c\u81a8\u80c0\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u901a\u5e38\u4f7f\u7528\u4e00\u4e2a\u7279\u5b9a\u7684\u7ed3\u6784\u5143\u7d20\u8fdb\u884c\u3002
\u200b \u5f00\u64cd\u4f5c\u5728\u56fe\u50cf\u5904\u7406\u4e2d\u5e38\u7528\u4e8e\u53bb\u9664\u566a\u58f0\u3001\u5206\u5272\u76ee\u6807\u3001\u51cf\u5c0f\u76ee\u6807\u533a\u57df\u7684\u4e0d\u89c4\u5219\u6027\u4ee5\u53ca\u51cf\u5c0f\u76ee\u6807\u4e4b\u95f4\u7684\u5e72\u6270\u3002\u5b83\u4e5f\u6709\u52a9\u4e8e\u6539\u5584\u4e8c\u503c\u5316\u56fe\u50cf\u7684\u8d28\u91cf\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u5206\u6790\u548c\u7279\u5f81\u63d0\u53d6\u3002 $$ A \u25e6 B = (A \u2296 B) \u2295 B $$
\u200b \u5f62\u6001\u5b66\u7684\u5f00\u64cd\u4f5c\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5b83\u7ed3\u5408\u4e86\u8150\u8680\uff08erosion\uff09\u548c\u81a8\u80c0\uff08dilation\uff09\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u53bb\u566a\u548c\u5206\u5272\u56fe\u50cf\u4e2d\u7684\u76ee\u6807\u3002\u5f00\u64cd\u4f5c\u7684\u4e3b\u8981\u601d\u60f3\u662f\u9996\u5148\u5bf9\u56fe\u50cf\u6267\u884c\u8150\u8680\u64cd\u4f5c\uff0c\u7136\u540e\u518d\u5bf9\u5176\u6267\u884c\u81a8\u80c0\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u901a\u5e38\u4f7f\u7528\u4e00\u4e2a\u7279\u5b9a\u7684\u7ed3\u6784\u5143\u7d20\u8fdb\u884c\u3002
\u200b \u95ed\u64cd\u4f5c\u5728\u56fe\u50cf\u5904\u7406\u4e2d\u5e38\u7528\u4e8e\u53bb\u9664\u5c0f\u5b54\u3001\u8fde\u63a5\u65ad\u5f00\u7684\u7ebf\u6761\u3001\u586b\u5145\u4e0d\u89c4\u5219\u76ee\u6807\u7684\u5185\u90e8\u7a7a\u6d1e\u3001\u589e\u5f3a\u56fe\u50cf\u4e2d\u7684\u8fde\u901a\u533a\u57df\uff0c\u5e73\u6ed1\u5176\u8fb9\u754c\u7684\u540c\u65f6\u5e76\u4e0d\u660e\u663e\u6539\u53d8\u5176\u9762\u79ef\u3002\u5b83\u4e5f\u6709\u52a9\u4e8e\u6539\u5584\u4e8c\u503c\u5316\u56fe\u50cf\u7684\u8d28\u91cf\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u5206\u6790\u548c\u7279\u5f81\u63d0\u53d6\u3002 $$ A \u2022 B = (A \u2295 B) \u2296 B $$
"},{"location":"DIP/chap3/chap3/","title":"Basic Operation","text":""},{"location":"DIP/chap3/chap3/#visibility-enhancement","title":"Visibility enhancement","text":""},{"location":"DIP/chap3/chap3/#logarithmic-operation","title":"logarithmic operation","text":"
\u4e3a\u4e86\u589e\u5f3a\u56fe\u50cf\u7684\u53ef\u89c6\u4fe1\u606f\uff0c\u5bf9\u56fe\u50cf\u4e2d\u7684\u50cf\u7d20\u8fdb\u884c\u57fa\u4e8e\u5bf9\u6570\u7684\u64cd\u4f5c \\(L_d = \\frac{log{({L_{w}+1})}}{log{({L_{max}+1})}}\\) \u5176\u4e2d\uff0c\\(L_d\\)\u662f\u663e\u793a\u4eae\u5ea6\uff0c\\(L_w\\)\u662f\u771f\u5b9e\u4e16\u754c\u4eae\u5ea6\uff0c\\(L_{max}\\)\u662f\u573a\u666f\u4e2d\u7684\u6700\u4eae\u503c\u3002
\\(r_k - \u50cf\u7d20\u7684\u7070\u5ea6\u7ea7\\)
\\(n_k - \u5177\u6709\u7070\u5ea6rk\u7684\u50cf\u7d20\u7684\u4e2a\u6570\\)
\\(MN - \u56fe\u50cf\u4e2d\u603b\u7684\u50cf\u7d20\u4e2a\u6570\\)
\\(\\sum_{k=0}^{MN-1}P(r_k)=1\\)
"},{"location":"DIP/chap3/chap3/#b","title":"b.\u5f69\u8272\u76f4\u65b9\u56fe","text":"\u76f4\u65b9\u56fe\u5747\u8861\u5316\uff08Histogram Equalization\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u589e\u5f3a\u6570\u5b57\u56fe\u50cf\u5bf9\u6bd4\u5ea6\u7684\u56fe\u50cf\u5904\u7406\u6280\u672f\u3002\u5b83\u901a\u8fc7\u91cd\u65b0\u5206\u5e03\u56fe\u50cf\u7684\u50cf\u7d20\u503c\uff0c\u4f7f\u76f4\u65b9\u56fe\u53d8\u5f97\u66f4\u5747\u5300\uff0c\u4ece\u800c\u6539\u5584\u56fe\u50cf\u7684\u89c6\u89c9\u8d28\u91cf
\u76f4\u65b9\u56fe\u5747\u8861\u5316\u5e38\u88ab\u7528\u4e8e\u63d0\u9ad8\u56fe\u50cf\u7684\u53ef\u89c6\u6027\u3001\u5206\u6790\u6027\u80fd\u548c\u81ea\u52a8\u56fe\u50cf\u5904\u7406\u5e94\u7528\u4e2d\u3002\u4f8b\u5982\uff0c\u8fc7\u66dd\u5149\u56fe\u50cf\u7684\u7070\u5ea6\u7ea7\u96c6\u4e2d\u5728\u9ad8\u4eae\u5ea6\u8303\u56f4\u5185\uff0c\u800c\u66dd\u5149\u4e0d\u8db3\u5c06\u4f7f\u56fe\u50cf\u7070\u5ea6\u7ea7\u96c6\u4e2d\u5728\u4f4e\u4eae\u5ea6\u8303\u56f4\u5185\u3002\u91c7\u7528\u76f4\u65b9\u56fe\u5747\u8861\u5316\uff0c\u53ef\u4ee5\u628a\u539f\u59cb\u56fe\u50cf\u7684\u76f4\u65b9\u56fe\u53d8\u6362\u4e3a\u5747\u5300\u5206\u5e03(\u5747\u8861)\u7684\u5f62\u5f0f\uff0c\u8fd9\u6837\u5c31\u589e\u52a0\u4e86\u50cf\u7d20\u4e4b\u95f4\u7070\u5ea6\u503c\u5dee\u522b\u7684\u52a8\u6001\u8303\u56f4\uff0c\u4ece\u800c\u8fbe\u5230\u589e\u5f3a\u56fe\u50cf\u6574\u4f53\u5bf9\u6bd4\u5ea6\u7684\u6548\u679c
\u6362\u8a00\u4e4b\uff0c\u76f4\u65b9\u56fe\u5747\u8861\u5316\u7684\u57fa\u672c\u539f\u7406\u662f:\u5bf9\u5728\u56fe\u50cf\u4e2d\u50cf\u7d20\u4e2a\u6570\u591a\u7684\u7070\u5ea6\u503c(\u5373\u5bf9\u753b\u9762\u8d77\u4e3b\u8981\u4f5c\u7528\u7684\u7070\u5ea6\u503c)\u8fdb\u884c\u5c55\u5bbd\uff0c\u800c\u5bf9\u50cf\u7d20\u4e2a\u6570\u5c11\u7684\u7070\u5ea6 \u503c(\u5373\u5bf9\u753b\u9762\u4e0d\u8d77\u4e3b\u8981\u4f5c\u7528\u7684\u7070\u5ea6\u503c)\u8fdb\u884c\u5f52\u5e76\uff0c\u4ece\u800c\u589e\u5927\u5bf9\u6bd4\u5ea6\uff0c\u4f7f\u56fe\u50cf\u6e05\u6670\uff0c\u8fbe\u5230\u589e\u5f3a\u7684\u76ee\u7684
\u5c3d\u7ba1\u76f4\u65b9\u56fe\u5747\u8861\u5316\u5177\u6709\u663e\u8457\u7684\u4f18\u70b9\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u9650\u5236\u3002\u5b83\u53ef\u80fd\u5bfc\u81f4\u56fe\u50cf\u51fa\u73b0\u566a\u70b9\u6216\u8fc7\u5ea6\u589e\u5f3a\u7ec6\u8282\uff0c\u7279\u522b\u662f\u5728\u566a\u97f3\u5b58\u5728\u7684\u60c5\u51b5\u4e0b
\u6b64\u5916\uff0c\u5747\u8861\u5316\u53ef\u80fd\u6539\u53d8\u56fe\u50cf\u7684\u6574\u4f53\u4eae\u5ea6\uff0c\u8fd9\u53ef\u80fd\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u56fe\u50cf
\u8fde\u7eed\u60c5\u51b5\u7684\u8f6c\u6362\\(T\\):
\u8bbe r \u4e3a\u8f6c\u6362\u524d\u7684\u7070\u5ea6\u7ea7\uff0c s \u4e3a\u53d8\u6362\u524d\u540e\u7684\u7070\u5ea6\u7ea7
\\(0 \u2264 r,s \u2264 1\\) [\u5bf9\u5e94\\((0,255)\\)]
P\u00ae \u548c P(s) \u662f\u5bf9\u5e94\u4e8e r \u548c s \u7684\u6982\u7387\u3002\u8f6c\u6362\u89c4\u5219\u4e3a:
T\u00ae\u662f\u5355\u8c03\u9012\u589e\u51fd\u6570\uff0c\\(0\u2264r\u22641\\)\u4e14\\(0\u2264T(r)\u22641\\)
\u9006\u53d8\u6362 r = \\(T^{-1}(s)\\)\u4e5f\u662f\u4e00\u4e2a\u5355\u8c03\u9012\u589e\u51fd\u6570 \u8003\u8651\u5230\u7070\u5ea6\u53d8\u6362\u4e0d\u4f1a\u6539\u53d8\u4f4d\u7f6e\u548c\u50cf\u7d20\u6570
\\(Thus: T(r)=\\int_0^rP(r)dr\\)
\u79bb\u6563\u60c5\u51b5\u7684\u8f6c\u6362 \\(T\\):
\u8bbe\u4e00\u5e45\u56fe\u50cf\u7684\u50cf\u7d20\u603b\u6570\u4e3an\uff0c\u5206L\u4e2a\u7070\u5ea6\u7ea7\uff0c\\(n_k\\)\u4e3a\u7b2ck\u4e2a\u7070\u5ea6\u7ea7\u51fa\u73b0\u7684\u50cf\u7d20\u6570\uff0c\u5219\u7b2c\\(k\\)\u4e2a\u7070\u5ea6\u7ea7\u51fa\u73b0\u7684\u6982\u7387\u4e3a\uff1a $$ P(r_k)= \\frac{n_k}{n}(0\u2264n_k \u22641,k=0,1,2,...,MN\u22121) $$
\u5bf9\u4e8e\u539f\u76f4\u65b9\u56fe\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u7070\u5ea6\u7ea7\\(r_k\\)\uff0c\u53ea\u9700\u5c06\u7070\u5ea6\u7ea7\u4e3a\\([0,r_k]\\)\u4ee5\u5185\u7684\u6240\u6709\u50cf\u7d20\u4e2a\u6570\u7684\u548c\u9664\u4ee5\u56fe\u50cf\u7684\u50cf\u7d20\u603b\u6570\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u8f6c\u6362\u4e4b\u540e\u7684\u5bf9\u5e94\u7070\u5ea6\u7ea7\\(s_k\\)
\u6309\u7167\u5747\u8861\u5316\u7684\u8981\u6c42\uff0c\u5728\u5747\u8861\u5316\u540e\u7684\u7ed3\u679c\u76f4\u65b9\u56fe\u4e2d\uff0c\u5404\u7070\u5ea6\u7ea7\u53d1\u751f\u7684\u6982\u7387\u5e94\u8be5\u662f\u76f8\u540c\u7684\uff0c\u5982\u53f3\u4e0a\u56fe\u6240\u793a\u8fde\u7eed\u7070\u5ea6\u7ea7\u5747\u8861\u5316\u7ed3\u679c\u90a3\u6837\u3002\u4f46\u662f\uff0c\u53f3\u4e0b\u56fe\u4e2d\u79bb\u6563\u7070\u5ea6\u7ea7\u5747\u8861\u5316\u540e\uff0c\u5404\u7070\u5ea6\u7ea7\u51fa\u73b0\u7684\u6982\u7387\u5e76\u4e0d\u5b8c\u5168\u4e00\u6837\u3002\u4e3a\u4ec0\u4e48\uff1f
\u8fd9\u6837\uff0c\u76f8\u90bb\u7684\u591a\u4e2a\\(s_k\\)\u5c31\u53ef\u80fd\u843d\u5165\u540c\u4e00\u4e2a\u7070\u5ea6\u7ea7\uff0c\u9700\u8981\u5728\u6b65\u9aa43\u65f6\u5c06\u5904\u4e8e\u540c\u4e00\u4e2a\u7070\u5ea6\u7ea7\u7684\u50cf\u7d20\u4e2a\u6570\u7d2f\u52a0,\u56e0\u6b64\uff0c\u79bb\u6563\u7070\u5ea6\u76f4\u65b9\u56fe\u5747\u8861\u5316\u64cd\u4f5c\u4ee5\u540e\uff0c\u6bcf\u4e2a\u7070\u5ea6\u7ea7\u5904\u7684\u6982\u7387\u5bc6\u5ea6\uff08\u6216\u50cf\u7d20\u4e2a\u6570\uff09\u5e76\u4e0d\u5b8c\u5168\u4e00\u6837\u3002
"},{"location":"DIP/chap3/chap3/#histogram-fitting","title":"Histogram fitting","text":"\u5728\u6b65\u9aa41\u548c2\u4e2d\uff0c\u5206\u522b\u8ba1\u7b97\u83b7\u5f97\u4e24\u5f20\u8868\uff08\u53c2\u89c1\u76f4\u65b9\u56fe\u5747\u8861\u5316\u4e2d\u7684\u7b97\u4f8b\uff09\uff0c\u4ece\u4e2d\u9009\u53d6\u4e00\u5bf9\\(v_k\u3001s_j\\)\uff0c\u4f7f\\(v_k = s_j\\)\uff0c\u5e76\u4ece\u4e24\u5f20\u8868\u4e2d\u67e5\u51fa\u5bf9\u5e94\u7684\\(z_k\\)\u3001\\(r_j\\)
\u8fd9\u6837\uff0c\u539f\u59cb\u56fe\u50cf\u4e2d\u7070\u5ea6\u7ea7\u4e3a\\(r_j\\)\u7684\u6240\u6709\u50cf\u7d20\u90fd\u6620\u5c04\u6210\u7070\u5ea6\u7ea7\\(z_k\\)\uff0c\u6700\u7ec8\u5f97\u5230\u6240\u671f\u671b\u7684\u56fe\u50cf\u3002
"},{"location":"DIP/chap3/chap3/#histogram-transform","title":"Histogram transform","text":""},{"location":"DIP/chap3/chap3/#linear-histogram-transform","title":"Linear histogram transform","text":"Linear grayscale transform by using a piecewise function, which strengthen the region of interest, weaken the region of no-interest.
\u5229\u7528\u5206\u6bb5\u76f4\u65b9\u56fe\u53d8\u6362\uff0c\u53ef\u4ee5\u5c06\u611f\u5174\u8da3\u7684\u7070\u5ea6\u8303\u56f4\u7ebf\u6027\u6269\u5c55\uff0c\u540c\u65f6\u76f8\u5bf9\u6291\u5236\u4e0d\u611f\u5174\u8da3\u7684\u7070\u5ea6\u533a\u57df
"},{"location":"DIP/chap3/chap3/#nonlinear-histogram-transform","title":"Nonlinear histogram transform","text":"Logarithmic function and Exponential function are two popular nonlinear transform functions.
Logarithmic
Expoential
\u5c06\u56fe\u50cf\u6cbf\u6c34\u5e73\u548c\u7ad6\u76f4\u65b9\u5411\u79fb\u52a8\uff0c\u4ece\u800c\u4ea7\u751f\u65b0\u56fe\u50cf\u7684\u8fc7\u7a0b\u3002
\u5e73\u79fb\u540e\u7684\u666f\u7269\u4e0e\u539f\u56fe\u50cf\u76f8\u540c\uff0c\u4f46\u201c\u753b\u5e03\u201d\u4e00\u5b9a\u662f\u6269\u5927\u4e86\uff0c\u5426\u5219\u5c31\u4f1a\u4e22\u5931\u4fe1\u606f\u3002 * \u77e9\u9635\u8868\u793a \\(\\begin{bmatrix}x^{'}\\\\y^{'}\\\\1\\end{bmatrix}=\\begin{bmatrix}1&0&x_0\\\\0&1&y_0\\\\0&0&1\\end{bmatrix}\\begin{bmatrix}x\\\\y\\\\1\\end{bmatrix}\\)
"},{"location":"DIP/chap3/chap3/#rotation","title":"\u65cb\u8f6c\uff08Rotation\uff09","text":"\u5c06\u56fe\u50cf\u4e58\u4ee5\u4e00\u5b9a\u7cfb\u6570\uff0c\u4ece\u800c\u4ea7\u751f\u65b0\u56fe\u50cf\u7684\u8fc7\u7a0b
\u6cbfy\u8f74\u65b9\u5411\u7f29\u653ed\u500d\uff08d>1\u65f6\u4e3a\u653e\u5927\uff0c0<d<1\u65f6\u4e3a\u7f29\u5c0f\uff09
\u653e\u5927 \uff1a\u65b0\u56fe\u50cf\u51fa\u73b0\u7a7a\u884c\u548c\u7a7a\u5217\uff0c\u53ef\u91c7\u7528\u63d2\u503c\u7684\u65b9\u6cd5\u52a0\u4ee5\u586b\u8865\uff0c\u4f46\u5b58\u5728\u201c\u9a6c\u8d5b\u514b\u201d\u73b0\u8c61\u3002
\u77e9\u9635\u8868\u793a \\(\\begin{bmatrix}x^{'}\\\\y^{'}\\\\1\\end{bmatrix}=\\begin{bmatrix}c&0&0\\\\0&d&0\\\\0&0&1\\end{bmatrix}\\begin{bmatrix}x\\\\y\\\\1\\end{bmatrix}\\)
\u56fe\u50cf\u7684\u9519\u5207\u53d8\u6362\u5b9e\u9645\u4e0a\u662f\u666f\u7269\u5728\u5e73\u9762\u4e0a\u7684\u975e\u5782\u76f4\u6295\u5f71\u6548\u679c
\\(Shear\\ on\\ x-axis:\\left\\{\\begin{aligned}&a(x, y) = x + d_xy \\\\&b(x, y) = y\\end{aligned}\\right.\\) \\(Shear\\ on\\ y-axis:\\left\\{\\begin{aligned}&a(x, y) = x \\\\&b(x, y) = y+d_yx\\end{aligned}\\right.\\)
\u7ed5x\u8f74\u6216y\u8f74\u7ffb\u8f6c\uff0c\u4ece\u800c\u4ea7\u751f\u4e0e\u539f\u56fe\u50cf\u5bf9\u79f0\u7684\u65b0\u56fe\u50cf\u7684\u8fc7\u7a0b * \u77e9\u9635\u8868\u793a \\(\\begin{bmatrix}x^{'}\\\\y^{'}\\\\1\\end{bmatrix}=\\begin{bmatrix}s_x&0&0\\\\0&s_y&0\\\\0&0&1\\end{bmatrix}\\begin{bmatrix}x\\\\y\\\\1\\end{bmatrix}\\)
\u4e3a\u4e86\u8ba1\u7b97\u51e0\u4f55\u53d8\u6362\u540e\u65b0\u56fe\u50cf\u4e2d\u67d0\u4e00\u70b9\\(P\u2019\\)\u5904\u7684\u50cf\u7d20\u503c\uff0c\u53ef\u4ee5\u9996\u5148\u8ba1\u7b97\u8be5\u51e0\u4f55\u53d8\u6362\u7684\u9006\u53d8\u6362\uff0c\u8ba1\u7b97\u51fa \\(P\u2019\\) \u6240\u5bf9\u5e94\u7684\u539f\u56fe\u50cf\u4e2d\u7684\u4f4d\u7f6e \\(P\\)
\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\\(P\\) \u7684\u4f4d\u7f6e\u4e0d\u53ef\u80fd\u6b63\u597d\u5904\u5728\u539f\u56fe\u50cf\u7684\u67d0\u4e00\u4e2a\u50cf\u7d20\u4f4d\u7f6e\u4e0a(\u5373 $P $\u70b9 \u7684\u5750\u6807\u901a\u5e38\u90fd\u4e0d\u4f1a\u6b63\u597d\u662f\u6574\u6570)
\u5bfb\u627e\u4e0e \\(P\\) \u70b9\u6700\u63a5\u8fd1\u7684\u50cf\u7d20\\(Q\\)\uff0c\u628a \\(Q\\) \u70b9\u7684\u50cf\u7d20\u503c\u4f5c\u4e3a\u65b0\u56fe\u50cf\u4e2d \\(P\u2019\\)\u70b9\u7684 \u50cf\u7d20\u503c\u3002
\\(Steps\\) \\((x^{'},y^{'})\\Rightarrow{\\text{Inverse Transtion}}(x,y)\\\\ \\Rightarrow{\\text{rounding operation}}(x_{int},y_{int})\\\\ \\Rightarrow{\\text{assign value}}I_{new}(x^{'},y^{'})=I_{old}(x_{int},y_{int})\\\\\\)
\u5f53\u56fe\u50cf\u4e2d\u5305\u542b\u660e\u663e\u7684\u51e0\u4f55\u7ed3\u6784\u65f6\uff0c\u7ed3\u679c\u5c06\u4e0d\u592a\u5149\u6ed1\u8fde\u7eed\uff0c\u4ece\u800c\u5728\u56fe\u50cf\u4e2d\u4ea7\u751f\u4eba\u4e3a\u7684\u75d5\u8ff9
\u5728\u4e00\u7ef4\u60c5\u51b5\u4e0b\uff0c\u5df2\u77e5x1\u548cx2\u5904\u7684\u7070\u5ea6\u503c\u5206\u522b\u4e3ag1\u548cg2\uff0c\u5219x3\u5904\u7684\u7070\u5ea6\u503cg3\u4e3a\uff1a $$ g_3 = \\frac{g_2-g_1}{x_2-x_1}(x_3-x_1)+g1 $$
\u5728\u4e8c\u7ef4\u60c5\u51b5\u4e0b\uff0c\u79f0\u4e3a\u53cc\u7ebf\u6027\u63d2\u503c
\u6ce8\uff1a\u53ef\u7531\u4e00\u7ef4\u7684\u60c5\u51b5\u63a8\u5bfc\u800c\u6765\uff0c\u5f97\u5230\u53cc\u7ebf\u6027\u65b9\u7a0bg(x,y)\u4e3a $$ g(x,y)=\\frac{(y_1-y)(x_1-x)}{(y_1-y_0)(x_1-x_0)}g(0,0)+\\frac{(y_1-y)(x-x_0)}{(y_1-y_0)(x_1-x_0)}g(1,0)+\\frac{(y-y_0)(x_1-x)}{(y_1-y_0)(x_1-x_0)}g(0,1)+\\frac{(y-y_0)(x-x_0)}{(y_1-y_0)(x_1-x_0)}g(1,1) $$ \u5373\u6ee1\u8db3\\(g(x,y)=ax+by+cxy+d\\)\u7684\u5f62\u5f0f
\u5df2\u77e5\u56fe\u50cf\u7684\u6b63\u65b9\u5f62\u7f51\u683c\u4e0a\u56db\u4e2a\u70b9A\u3001B\u3001C\u3001D\u7684\u7070\u5ea6\uff0c\u6c42P\u70b9\u7684\u7070\u5ea6:
\u5f84\u5411\u57fa\u51fd\u6570\\(\\phi(x)\\)
\u6700\u5e38\u89c1\u4e3a\u9ad8\u65af\u51fd\u6570\\(\u03c6(r)=e^{\\frac{\u2212r^2}{2\u03c3^2}}\\)
Multiquadrics:\\(\\phi(r)=\\sqrt{1+\\frac{r^2}{\\sigma^2}}\\)
Linear: \\(\\phi(r)=r\\)
Cubic:\\(\\phi(r)=r^3\\)
\u2026\u2026
\u63d2\u503c\u51fd\u6570 \\(\\hat{f}(x)=\\sum_{i=1}^Nw_i\\phi(||x-x_i||)\\) where \\(w_i=\\frac{\\phi(|x-c_i|)}{\\sum_{i=1}^n\\phi(|x-c_i|)}\\)
"},{"location":"DIP/feature/feature/#feature-detection","title":"Feature detection","text":""},{"location":"DIP/feature/feature/#feature-detection-the-math","title":"Feature detection: the math","text":"
Consider shifting the window \\(W\\) by \\((u,v)\\)
\\(\\begin{align*}Error(u,v)&=\\sum_{(x,y)\\in W}[I(x+u,y+v)-I(x,y)]^2\\\\ &\\approx\\sum_{(x,y)\\in W}[I(x,y)+\\frac{\\partial I}{\\partial x}u+\\frac{\\partial I}{\\partial y}v-I(x,y)]^2\\\\ &\\approx\\sum_{(x,y)\\in W}[u,v]\\begin{bmatrix}I_x^2&I_xI_y\\\\I_yI_x&I_y^2\\end{bmatrix}\\begin{bmatrix}u\\\\v\\end{bmatrix} \\\\ \\end{align*}\\)
We can find these directions by looking at the eigenvectors of H \\((\\begin{bmatrix}I_x^2&I_xI_y\\\\I_yI_x&I_y^2\\end{bmatrix})\\)
Eigenvalues and eigenvectors of H
Since \\(H\\) is symmetric ,so \\(H=R^{-1}\\begin{bmatrix}\\lambda_1&0\\\\0&\\lambda_2\\end{bmatrix}R\\)
\\(f=\\frac{\\lambda_1\\lambda_2}{\\lambda_1+\\lambda_2}=\\frac{determinant(H)}{trace(H)}\\)
Rotation Invariance
Partial Invariance to additive and multiplicative intensity changes
Not invariance to scaling. !!!
We define the characteristic scale as the scale that produces peak of Laplacian response
Stage 1: Initialization--Multiscale Harris Corner Detection
Stage 2: Scale Selection Based on Laplacian
The Laplacian is an operator used to detect edges and texture variations in an image by computing the second derivative at each point.
In the context of scale selection, the Laplacian serves to measure the changes in the image at different scales.
The optimal scale for a keypoint is where the maximum edge response occurs, indicating that the details of the keypoint are most pronounced at that scale.
For the same corner, even with changes in scale, the corner remains detectable, indicating robustness in terms of repeatability.
The content within the feature scale range of the same corner in images of different sizes should be consistent.
Therefore, the pixel locations within the feature scale range of corners in images of varying scales are proportional to the scale.(\u4e0d\u540c\u5c3a\u5bf8\u56fe\u7247\u4e2d\u7684\u76f8\u540c\u89d2\u70b9\u7684\u7279\u5f81\u5c3a\u5ea6\u8303\u56f4\u4e2d\u7684\u5185\u5bb9\u8981\u76f8\u540c\uff0c\u56e0\u6b64\uff0c\u4e0d\u540c\u5c3a\u5ea6\u7684\u56fe\u7247\u7684\u89d2\u70b9\u7684\u7279\u5f81\u5c3a\u5ea6\u8303\u56f4\u5185\u7684\u50cf\u7d20\u70b9\u4e0e\u5c3a\u5ea6\u6210\u6bd4\u4f8b\u5173\u7cfb\u3002)
"},{"location":"DIP/feature/feature/#local-extrema-detection","title":"Local Extrema Detection","text":"Let's illustrate the construction process of a Gaussian pyramid with a simple example. We assume that each group contains S layers, and the scale differences within each group are achieved through downsampling by a factor of 2 and using different Gaussian blur parameters \u03c3.
This process can be repeated iteratively, and each round of Gaussian blur and downsampling constructs one level of the pyramid. Within each group, we have S images at different scales (determined by different Gaussian blur parameters), and each image, obtained by downsampling, aligns with the corresponding image from the previous group in terms of scale.
This ensures that the construction of the pyramid is \"bottom-up,\" reflecting the features of the image at different scales.
Based on the collected images we detect their interest points with Difference of Gaussian, and extract the SIFT descriptors.
The extracted descriptors are then clustered to generate the vocabulary tree with the hierarchical k-means clustering. In this experiment we generated a visual vocabulary containing 32357 visual words
"},{"location":"DIP/feature/feature/#surf-detectors-and-descriptors","title":"SURF detectors and descriptors","text":"Reference to https://medium.com/@deepanshut041/introduction-to-surf-speeded-up-robust-features-c7396d6e7c4e
Basic Hessian matrix approximation.
"},{"location":"DIP/feature/feature/#aintegral-images","title":"a.Integral images","text":"The Integral Image or Summed-Area Table was introduced in 1984. The Integral Image is used as a quick and effective way of calculating the sum of values (pixel values) in a given image \u2014 or a rectangular subset of a grid (the given image). It can also, or is mainly, used for calculating the average intensity within a given image.
"},{"location":"DIP/feature/feature/#bhessian-matrix-based-interest-points","title":"b.Hessian matrix-based interest points","text":"\\(SURF\\) uses the Hessian matrix because of its good performance in computation time and accuracy. Rather than using a different measure for selecting the location and the scale (Hessian-Laplace detector), surf relies on the determinant of the Hessian matrix for both.
For adapt to any scale, we filtered the image by a Gaussian kernel, so given a point \\(X = (x, y)\\), the Hessian matrix \\(H(x, \u03c3)\\) in x at scale \u03c3 is defined as:
\\(H(\\vec{x},\\sigma)=\\begin{bmatrix}L_{xx}(x,\\sigma)&L_{xy}(x,\\sigma)\\\\ L_{xy}(x,\\sigma)&L_{yy}(x,\\sigma)\\end{bmatrix}\\)
where \\(L_{xx}(x, \u03c3)\\) is the convolution of the Gaussian second order derivative with the image \\(I\\) in point \\(x\\), and similarly for \\(L_{xy} (x, \u03c3)\\) and \\(L_{yy} (x, \u03c3)\\).
Gaussians are optimal for scale-space analysis but in practice, they have to be discretized and cropped. This leads to a loss in repeatability under image rotations around odd multiples of \\(\u03c0 /4\\). This weakness holds for Hessian-based detectors in general. Nevertheless, the detectors still perform well, and the slight decrease in performance does not outweigh the advantage of fast convolutions brought by the discretization and cropping.
After Lowe\u2019s success with LoG approximations(SIFT), SURF pushes the approximation(both convolution and second-order derivative) even further with box filters. These approximate second-order Gaussian derivatives and can be evaluated at a very low computational cost using integral images and independently of size, and this is part of the reason why SURF is fast.
\\(d^2L(x)/dx^2=(L(x+1)-L(x))-(L(x)-L(x-1))=-2*L(x)+L(x+1)+L(x-1)\\)
The \\(9 \u00d7 9\\) box filters in the above images are approximations for Gaussian second order derivatives with \u03c3 = 1.2.
We denote these approximations by \\(D_{xx}, D_{yy}, and D_{xy}\\).
Now we can represent the determinant of the Hessian (approximated) as:\\(det(H_{approx})=D_{xx}D_{yy}-(wD_{xy})^2\\) \\(w=0.9 (Bay\u2019s\\ suggestion)\\)
"},{"location":"DIP/feature/feature/#cscale-space-representation","title":"c.Scale-space representation","text":"SURF can be considered as a faster feature extraction method compared to SIFT.
It processes the original image directly using box filters at different scales, eliminating the need to build multiple layers of a pyramid like SIFT. This makes SURF computationally more efficient, especially in scenarios where large-scale image databases or real-time applications are involved.
If you use a 9x9 box filter on a 36x36 image, and you start from the center of the filter, moving the width of the filter at each step, you would end up with a 4x4 grid, deriving 16 new coordinate detection points. This is because 36 divided by 9 equals 4, thus, you can get 4 coordinate points in each direction.
However, this is just a logical understanding, and the actual results may vary due to the details of filter processing, such as edge handling, stride of the filter, and other factors.
\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u627e\u5230\u7684\u5c40\u90e8\u6700\u5927\u503c\u5982\u679c\u653e\u56de\u539f\u56fe\u7684\u5c3a\u5ea6\uff0c\u5176\u5b9e\u662f\u5bf9\u5e94\u7740\u4e00\u4e2a\u533a\u57df\u7684\uff0c\u90a3\u4e48\u5982\u4f55\u786e\u5b9a\u4e00\u4e2a\u7cbe\u786e\u7684\u5750\u6807\u5462\uff1f
To locate the precise position of feature points, we apply a Taylor expansion to the Hessian matrix at the local maximum points. This approximates a smooth surface depicting the variation of image intensity around the feature points. Then, we identify the peak of this smooth surface. Compared to the original discrete maximum point, the location of this peak is more accurate. Hence, we refer to it as the true location of the feature point.
"},{"location":"DIP/feature/feature/#interpolation","title":"Interpolation","text":"For each local maximum, need to interpolate to get true location (to overcome discretization effects)
"},{"location":"DIP/feature/feature/#2feature-description","title":"(2)Feature Description","text":""},{"location":"DIP/feature/feature/#orientation-assignment","title":"Orientation Assignment","text":"Also, the sampling step is scale dependent and chosen to be s, and the wavelet responses are computed at that current scale s.
Accordingly, at high scales the size of the wavelets is big. Therefore integral images are used again for fast filtering.
Now it\u2019s time to extract the descriptor
The first step consists of constructing a square region centered around the keypoint and oriented along the orientation we already got above. The size of this window is 20s.
Then the region is split up regularly into smaller 4 \u00d7 4 square sub-regions. For each sub-region, we compute a few simple features at 5\u00d75 regularly spaced sample points.
For reasons of simplicity, we call dx the Haar wavelet response in the horizontal direction and dy the Haar wavelet response in the vertical direction (filter size 2s).
To increase the robustness towards geometric deformations and localization errors, the responses dx and dy are first weighted with a Gaussian (\u03c3 = 3.3s) centered at the keypoint.
Then, the wavelet responses dx and dy are summed up over each subregion and form a first set of entries to the feature vector.
In order to bring in information about the polarity of the intensity changes, we also extract the sum of the absolute values of the responses, |dx| and |dy|.
Hence, each sub-region has a four-dimensional descriptor vector \\(\\vec{v}\\) for its underlying intensity structure \\(V = (\u2211 dx, \u2211 dy, \u2211|dx|, \u2211|dy|)\\).
This results in a descriptor vector for all 4\u00d74 sub-regions of length 64.
Procedure
A further refinement of matches.--RANdom SAmple Consensus
In short words, \\(RANSAC\\) fits \\(N\\) models using different random sample S of the whole available data each time. Then, for each model it evaluates its performance P (i.e. number of inliers or outliers) and chooses the best one. Note that RANSAC doesn\u2019t determine what method should you use to find model parameters.(least squares for example)
How many samples are needed?
Suppose \\(w\\) is fraction of inliers (points from line).
\\(n\\) points needed to define hypothesis (2 for lines)
\\(k\\) samples chosen.
Prob. that a single sample of n points is correct \\(w^n\\)
Prob. that all k samples fail is: \\((1-w^n)^k\\)
k-- iterations
RANSAC divides data into inliers and outliers and yields estimate computed from minimal set of inliers.Improve this initial estimate with estimation over all inliers (e.g. with standard least-squares minimization).
But this may change inliers, so alternate fitting with re-classification as inlier/outlier.
A voting strategy, the Hough transform, can handle high percentage of outliers
"},{"location":"DIP/feature/feature/#image-blending","title":"Image Blending","text":""},{"location":"DIP/feature/feature/#image-pyramids","title":"Image Pyramids","text":"An image pyramid can be constructed by repeatedly downsampling (or upsampling) an image and creating a set of images at different resolutions.
The resulting images are referred to as \u201clevels\u201d of the pyramid, with the highest resolution image at the top and the lowest resolution image at the bottom.
This type of pyramid is constructed by repeatedly applying a Gaussian blur filter to an image and downsampling it by a factor of two. The resulting images are smoother and have lower resolution than the original image because Gaussians are low pass filters.
"},{"location":"DIP/feature/feature/#laplacian-pyramid","title":"Laplacian Pyramid","text":"This type of pyramid is constructed by subtracting a downsampled version of an image from the original image.
\u2022 Build Laplacian pyramid for both images: \\(L_A, L_B\\).
\u2022 Build Gaussian pyramid for mask: \\(G\\).
\u2022 Build a combined Laplacian pyramid: \\(L(j) = G(j) LA(j) + (1-G(j)) LB(j).\\)
\u2022 Collapse \\(L\\) to obtain the blended image.
"},{"location":"DIP/filter/filter/","title":"image filter","text":""},{"location":"DIP/filter/filter/#_1","title":"\u6ee4\u6ce2","text":""},{"location":"DIP/filter/filter/#_2","title":"\u5377\u79ef","text":"
\\(L(x,y,\u03c3)=G(x,y,\u03c3)\u2217I(x,y)\\)
\u8fde\u7eed\u5f62\u5f0f\uff1a\\((f\u2217g)(n)=\\int_{-\\infty }^{\\infty}f(\\tau )g(n-\\tau)d\\tau\\)
\u79bb\u6563\u5f62\u5f0f\uff1a\\((f\u2217g)(n)=\\frac{1}{M}\\sum_{\\tau=0 }^{M-1}f(\\tau)g(n-\\tau)\\)
"},{"location":"DIP/filter/filter/#properties","title":"Properties","text":"\\(f(x)*h(x)=h(x)*f(x)\\\\ f*(g+h)=f*g+f*h\\\\f*(g*h)=(f*g)*h\\)
"},{"location":"DIP/filter/filter/#examples","title":"Examples","text":"Covers all the points that \\(h(t)\\) go through during the sliding operation.
In this example, the effective range of x is from 0 to 799.
"},{"location":"DIP/filter/filter/#_3","title":"\u57fa\u672c\u6982\u5ff5","text":"\u6ee4\u6ce2\u5668\u662f\u4e00\u4e2a\u5927\u5c0f\u4e3a \\(M\\times N\\) \u7684\u7a97\u53e3\uff0c\u5176\u4e2d\u7a97\u53e3\u4e2d\u7684\u5143\u7d20\u5bf9\u5e94\u4e8e\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u76f8\u5e94\u50cf\u7d20\u8fdb\u884c\u64cd\u4f5c\uff0c\u7ed3\u679c\u4fdd\u5b58\u4e3a\u65b0\u56fe\u50cf\u4e2d\u7684\u50cf\u7d20
\u6ee4\u6ce2\u4e5f\u88ab\u79f0\u4f5c:\u906e\u7f69 (mask)\u3001\u5185\u6838 (kernel)\u3001\u6a21\u677f (template) \u7b49\u3002\u6ee4\u6ce2\u5668\u4e2d\u7684\u5143\u7d20\u662f\u7cfb\u6570\u800c\u4e0d\u662f\u50cf\u7d20\u503c\uff0c\u5b83\u4eec\u8868\u793a\u5e94\u7528\u4e8e\u539f\u59cb\u56fe\u50cf\u4e2d\u50cf\u7d20\u7684\u6743\u91cd\u3002\u5728\u5f85\u5904\u7406\u56fe\u50cf\u4e2d\u9010\u70b9\u79fb\u52a8\u63a9\u6a21\uff0c\u5728\u6bcf\u4e00\u70b9(x,y)\u5904\uff0c\u6ee4\u6ce2\u5668\u5728\u8be5\u70b9\u7684\u54cd\u5e94\u901a\u8fc7\u5b9e\u73b0\u5b9a\u4e49\u7684\u5173\u7cfb\u6765\u8ba1\u7b97\u3002\u5bf9\u4e8e\u7ebf\u6027\u7a7a\u95f4\u6ee4\u6ce2\uff0c\u5176\u54cd\u5e94\u7531\u6ee4\u6ce2\u5668\u7cfb\u6570\u4e0e\u6ee4\u6ce2\u63a9\u6a21\u626b\u8fc7\u533a\u57df\u7684\u5bf9\u5e94\u50cf\u7d20\u503c\u7684\u4e58\u79ef\u4e4b\u548c\u7ed9\u51fa\u3002
\u901a\u5e38\uff0c\u63a9\u6a21\u7684\u957f\u5bbd\u90fd\u4e3a\u5947\u6570
\u56fe\u50cf\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u4f20\u8f93\u4fe1\u9053\u3001\u91c7\u6837\u7cfb\u7edf\u8d28\u91cf\u8f83\u5dee\uff0c\u6216\u53d7\u5404\u79cd\u5e72\u6270\u7684\u5f71\u54cd\uff0c\u800c\u9020\u6210\u56fe\u50cf\u6bdb\u7cd9\uff0c\u6b64\u65f6\uff0c\u5c31\u9700\u5bf9\u56fe\u50cf\u8fdb\u884c\u5e73\u6ed1\u5904\u7406\u3002\u5e73\u6ed1\u53ef\u4ee5\u6291\u5236\u9ad8\u9891\u6210\u5206\uff0c\u4f46\u4e5f\u4f7f\u56fe\u50cf\u53d8\u5f97\u6a21\u7cca\u3002 \u5e73\u6ed1\u7a7a\u95f4\u6ee4\u6ce2\u5668\u7528\u4e8e\u6a21\u7cca\u5904\u7406\u548c\u51cf\u5c11\u566a\u58f0\u3002\u6a21\u7cca\u5904\u7406\u7ecf\u5e38\u7528\u4e8e\u9884\u5904\u7406\uff0c\u4f8b\u5982\uff0c\u5728\u63d0\u53d6\u5927\u7684\u76ee\u6807\u4e4b\u524d\u53bb\u9664\u56fe\u50cf\u4e2d\u4e00\u4e9b\u7410\u788e\u7684\u7ec6\u8282\uff0c\u6865\u63a5\u76f4\u7ebf\u6216\u66f2\u7ebf\u7684\u7f1d\u9699\u3002
"},{"location":"DIP/filter/filter/#_4","title":"\u5e73\u6ed1\u7ebf\u6027\u7a7a\u95f4\u6ee4\u6ce2\u5668","text":"\u5e73\u6ed1\u7ebf\u6027\u7a7a\u95f4\u6ee4\u6ce2\u5668\u7684\u8f93\u51fa\u662f\u5305\u542b\u5728\u6ee4\u6ce2\u63a9\u6a21\u90bb\u57df\u5185\u50cf\u7d20\u7684\u7b80\u5355\u5e73\u5747\u503c\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u6ee4\u6ce2\u5668\u4e5f\u79f0\u4e3a\u5747\u503c\u6ee4\u6ce2\u5668\u3002
\u7b80\u5355\u5e73\u5747(simple mean)\uff0c\u8868\u793a\u7a97\u53e3\u4e2d\u6bcf\u4e00\u4e2a\u50cf\u7d20\u5bf9\u54cd\u5e94\u7684\u8d21\u732e\u662f\u4e00\u6837\u7684, \u6ee4\u6ce2\u7a97\u53e3\uff1a\\(\\frac{1}{9} \\times \\begin{bmatrix}1&1&1 \\\\ 1&1&1\\\\ 1&1&1\\end{bmatrix}\\)
\u52a0\u6743\u5e73\u5747(weighted mean)\uff0c\u8868\u793a\u7a97\u53e3\u4e2d\u7684\u50cf\u7d20\u5bf9\u76f8\u5e94\u7684\u8d21\u732e\u6709\u5927\u5c0f\u4e4b\u5206\uff0c\u6ee4\u6ce2\u7a97\u53e3\uff1a\\(\\frac{1}{16} \\times \\begin{bmatrix}1&2&1 \\\\ 2&4&2\\\\ 1&2&1\\end{bmatrix}\\)
General Equation
\u5176\u4e2d\uff0c\u6ee4\u6ce2\u5668\u5927\u5c0f\u4e3a\\((2a+1) \u00d7(2b+1)\\)\uff0c\\(w\\)\u4e3a\u6ee4\u6ce2\u5668\uff0c\\(f\\)\u4e3a\u8f93\u5165\u56fe\u50cf\uff0c\\(g\\)\u4e3a\u8f93\u51fa\u56fe\u50cf\u3002
\u5f53\u63a9\u6a21\u6bd4\u8f83\u5c0f\u65f6\uff0c\u53ef\u4ee5\u89c2\u5bdf\u5230\u5728\u6574\u5e45\u56fe\u50cf\u4e2d\u6709\u8f7b\u5fae\u7684\u6a21\u7cca
\u5f53\u63a9\u6a21\u5927\u5c0f\u589e\u52a0\uff0c\u6a21\u7cca\u7a0b\u5ea6\u4e5f\u968f\u4e4b\u589e\u52a0
\u7edf\u8ba1\u6ee4\u6ce2\u5668\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u7684\u7a7a\u95f4\u6ee4\u6ce2\u5668\uff0c\u5b83\u7684\u54cd\u5e94\u662f\u57fa\u4e8e\u7a97\u53e3\u5185\u56fe\u50cf\u533a\u57df\u4e2d\u50cf\u7d20\u503c\u7684\u6392\u5e8f\uff0c\u7531\u7edf\u8ba1\u6392\u5e8f\u7ed3\u679c\u51b3\u5b9a\u7684\u503c\u4ee3\u66ff\u4e2d\u5fc3\u50cf\u7d20\u7684\u503c\u3002
\u5bf9\u5904\u7406\u8109\u51b2\u566a\u58f0\uff08\u4e5f\u79f0\u4e3a\u6912\u76d0\u566a\u58f0\uff09\u975e\u5e38\u6709\u6548\uff0c\u56e0\u4e3a\u8fd9\u79cd\u566a\u58f0\u662f\u4ee5\u9ed1\u767d\u70b9\u53e0\u52a0\u5728\u56fe\u50cf\u4e0a\u7684\u3002
\u4e3a\u4e86\u5bf9\u4e00\u5e45\u56fe\u50cf\u4e0a\u7684\u67d0\u4e2a\u70b9\u4f5c\u4e2d\u503c\u6ee4\u6ce2\u5904\u7406\u3002\u5fc5\u987b\u5148\u5c06\u63a9\u6a21\u5185\u6b32\u6c42\u7684\u50cf\u7d20\u53ca\u5176\u90bb\u57df\u7684\u50cf\u7d20\u503c\u6392\u5e8f\uff0c\u786e\u5b9a\u51fa\u4e2d\u503c\uff0c\u5e76\u5c06\u4e2d\u503c\u8d4b\u4e88\u8be5\u50cf\u7d20\u70b9
\u4e2d\u503c\\(\u03be\\) --\u6570\u503c\u96c6\u5408\u4e2d\uff0c\u6709\u4e00\u534a\u6570\u503c\u5c0f\u4e8e\u6216\u7b49\u4e8e\\(\u03be\\)\uff0c\u8fd8\u6709\u4e00\u534a\u5927\u4e8e\u6216\u7b49\u4e8e\\(\u03be\\)
\u4e3a\u4e86\u5bf9\u4e00\u5e45\u56fe\u50cf\u4e0a\u7684\u67d0\u4e2a\u70b9\u4f5c\u4e2d\u503c\u6ee4\u6ce2\u5904\u7406,\u5fc5\u987b\u5148\u5c06\u63a9\u6a21\u5185\u6b32\u6c42\u7684\u50cf\u7d20\u53ca\u5176\u90bb\u57df\u7684\u50cf\u7d20\u503c\u6392\u5e8f\uff0c\u786e\u5b9a\u51fa\u4e2d\u503c\uff0c\u5e76\u5c06\u4e2d\u503c\u8d4b\u4e88\u8be5\u50cf\u7d20\u70b9
\u62c9\u666e\u62c9\u65af\u9510\u5316\u662f\u4e00\u79cd\u56fe\u50cf\u5904\u7406\u6280\u672f\uff0c\u65e8\u5728\u7a81\u51fa\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\u548c\u7ec6\u8282\uff0c\u4f7f\u56fe\u50cf\u770b\u8d77\u6765\u66f4\u52a0\u9510\u5229\u3002\u8fd9\u79cd\u6280\u672f\u4f7f\u7528\u62c9\u666e\u62c9\u65af\u7b97\u5b50\uff08Laplacian operator\uff09\u6216\u62c9\u666e\u62c9\u65af\u6ee4\u6ce2\u5668\u5bf9\u56fe\u50cf\u8fdb\u884c\u5377\u79ef\u64cd\u4f5c\uff0c\u4ee5\u589e\u5f3a\u56fe\u50cf\u4e2d\u7684\u9ad8\u9891\u4fe1\u606f\u3002
"},{"location":"DIP/filter/filter/#1","title":"(1)\u62c9\u666e\u62c9\u65af\u7b97\u5b50","text":"\u4e00\u9636\u5fae\u5206\u7b97\u5b50:\\(\\frac{\\partial{f}}{\\partial{x}}\\)$= f(x + 1) \u2212 f(x) $
\u4e8c\u9636\u5fae\u5206\u7b97\u5b50:\\(\\frac{\\partial^2{f}}{\\partial{x}^2}\\)\\(= f(x + 1) +f(x-1)\u2212 2f(x)\\)
\u5bf9\u6574\u5e45\u56fe\u50cf\u8ba1\u7b97\u68af\u5ea6\u65f6\uff0c\u8fd0\u7b97\u91cf\u4f1a\u5f88\u5927\uff0c\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u5e38\u7528\u7edd\u5bf9\u503c\u4ee3\u66ff\u5e73\u65b9\u4e0e\u5e73\u65b9\u6839\u8fd0\u7b97\u8fd1\u4f3c\u6c42\u68af\u5ea6\u7684\u6a21\u503c \\(\\triangledown f \\approx |G_x|+|G_y|\\)
\u79bb\u6563\u5f62\u5f0f
\\(\\ \\ \\ \\triangledown^2f=f(x+1,y)+f(x-1,y)+f(x,y+1),F(x,y-1)-4f(x,y)\\)
\u5982\u679c\u8003\u8651\u5bf9\u89d2\u7ebf\u5143\u7d20
\u7531\u4e8e\u62c9\u666e\u62c9\u65af\u7b97\u5b50\u662f\u4e00\u79cd\u5bfc\u6570\u7b97\u5b50\uff0c\u5b83\u7684\u4f7f\u7528\u4f1a\u7a81\u51fa\u663e\u793a\u56fe\u50cf\u4e2d\u7684\u5f3a\u5ea6\u4e0d\u8fde\u7eed\u6027\uff0c\u800c\u4e0d\u5f3a\u8c03\u5177\u6709\u7f13\u6162 \u53d8\u5316\u7684\u5f3a\u5ea6\u7ea7\u522b\u7684\u533a\u57df\u3002\u8fd9\u5c06\u503e\u5411\u4e8e\u751f\u6210\u5177\u6709\u7070\u8272\u8fb9\u7f18\u7ebf\u548c\u5176\u4ed6\u4e0d\u8fde\u7eed\u6027\u7684\u56fe\u50cf\uff0c\u6240\u6709\u8fd9\u4e9b\u56fe\u50cf\u90fd\u53e0\u52a0\u5728\u9ed1\u6697\u3001\u65e0\u7279\u5f81\u7684\u80cc\u666f\u4e0a\u3002\u53ea\u9700\u5c06\u62c9\u666e\u62c9\u65af\u56fe\u50cf\u6dfb\u52a0\u5230\u539f\u59cb\u56fe\u50cf\u4e2d\uff0c\u5373\u53ef\u201c\u6062\u590d\u201d\u80cc\u666f\u7279\u5f81\uff0c\u540c\u65f6\u4ecd\u4fdd\u6301\u62c9 \u666e\u62c9\u65af\u56fe\u50cf\u7684\u9510\u5316\u6548\u679c
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f7f\u7528\u7684\u5b9a\u4e49\u5177\u6709\u8d1f\u4e2d\u5fc3\u7cfb\u6570\uff0c\u5219\u6211\u4eec\u51cf\u53bb\u800c\u4e0d\u662f\u6dfb\u52a0\u62c9\u666e\u62c9\u65af\u56fe\u50cf\u4ee5\u83b7\u5f97\u9510\u5316\u7ed3\u679c
\u56e0\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u62c9\u666e\u62c9\u65af\u51fd\u6570\u8fdb\u884c\u56fe\u50cf\u9510\u5316\u7684\u57fa\u672c\u65b9\u6cd5\u662f:
\\(g(x,y)=\\left\\{\\begin{array}{ll}f(x,y)-\\triangledown^2f(x,y)& if\\ the\\ center\\ of\\ the\\ mask\\ is\\ negative \\\\ f(x,y)+\\triangledown^2f(x,y)& if\\ the\\ center\\ of\\ the\\ mask\\ is\\ positive \\end{array} \\right.\\)
\u9ad8\u65af\u6ee4\u6ce2\u662f\u4e00\u79cd\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u6ee4\u6ce2\u5668\uff0c\u5176\u4e3b\u8981\u76ee\u7684\u662f\u5bf9\u56fe\u50cf\u8fdb\u884c\u5e73\u6ed1\u5904\u7406\u3002\u5b83\u7684\u540d\u79f0\u6765\u6e90\u4e8e\u6240\u4f7f\u7528\u7684\u6ee4\u6ce2\u6838\uff08\u5377\u79ef\u6838\uff09\u662f\u4e00\u4e2a\u4e8c\u7ef4\u9ad8\u65af\u51fd\u6570\u3002\u9ad8\u65af\u6ee4\u6ce2\u5728\u53bb\u9664\u56fe\u50cf\u4e2d\u7684\u566a\u58f0\u3001\u6a21\u7cca\u5904\u7406\u3001\u8fb9\u7f18\u68c0\u6d4b\u7b49\u65b9\u9762\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002
\u9ad8\u65af\u6ee4\u6ce2\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5bf9\u56fe\u50cf\u4e2d\u7684\u6bcf\u4e2a\u50cf\u7d20\u70b9\u8fdb\u884c\u52a0\u6743\u5e73\u5747\uff0c\u5176\u4e2d\u6743\u503c\u662f\u7531\u4e8c\u7ef4\u9ad8\u65af\u51fd\u6570\u786e\u5b9a\u7684\u3002\u8fd9\u610f\u5473\u7740\u79bb\u4e2d\u5fc3\u50cf\u7d20\u8d8a\u8fdc\u7684\u50cf\u7d20\u5bf9\u4e2d\u5fc3\u50cf\u7d20\u7684\u5f71\u54cd\u8d8a\u5c0f\uff0c\u8fd9\u79cd\u6743\u503c\u7684\u5206\u5e03\u7b26\u5408\u9ad8\u65af\u5206\u5e03\u3002\u901a\u8fc7\u8c03\u6574\u9ad8\u65af\u51fd\u6570\u7684\u6807\u51c6\u5dee\\(\u03c3\\)\uff0c\u53ef\u4ee5\u63a7\u5236\u6743\u503c\u7684\u5206\u5e03\u8303\u56f4\uff0c\u4ece\u800c\u8c03\u6574\u6ee4\u6ce2\u6548\u679c\u3002
"},{"location":"DIP/filter/filter/#1_1","title":"(1)\u6570\u5b66\u8868\u8fbe","text":"\u5728\u56fe\u50cf\u6ee4\u6ce2\u4e2d,\u4e00\u822c\u60c5\u51b5\u4e0b\\(\u03bc_x = \u03bc_y = 0\\)\u56e0\u6b64\u4e8c\u7ef4\u9ad8\u65af\u51fd\u6570\u53ef\u8868\u793a\u4e3a\\(f(x,y)=\\frac{1}{2\\pi\\sigma^2}e^{-\\frac{x^2+y^2}{2\\sigma^2}}\\)
"},{"location":"DIP/filter/filter/#2_1","title":"(2)\u6ee4\u6ce2\u8fc7\u7a0b","text":"\u5c06\u5404\u4e2a\u4f4d\u7f6e\u7684\u5750\u6807\u4ee3\u5165\u5230\u9ad8\u65af\u51fd\u6570\u4e2d,\u5f97\u5230\u7684\u503c\u5c31\u662f\u521d\u6b65\u7684\u9ad8\u65af\u6838
\u5229\u7528\u9ad8\u65af\u51fd\u6570\u8fdb\u884c\u5377\u79ef(\u9ad8\u65af\u6ee4\u6ce2)\u7684\u8fc7\u7a0b\u5177\u6709\u53ef\u5206\u79bb\u6027\u3002
"},{"location":"DIP/filter/filter/#_7","title":"\u53cc\u8fb9\u6ee4\u6ce2","text":"\u53cc\u8fb9\u6ee4\u6ce2\uff08Bilateral filter\uff09\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u7684\u6ee4\u6ce2\u65b9\u6cd5\uff0c\u662f\u7ed3\u5408\u56fe\u50cf\u7684\u7a7a\u95f4\u90bb\u8fd1\u5ea6\u548c\u50cf\u7d20\u503c\u76f8\u4f3c\u5ea6\u7684\u4e00\u79cd\u6298\u8877\u5904\u7406\uff0c\u540c\u65f6\u8003\u8651\u7a7a\u57df\u4fe1\u606f\u548c\u7070\u5ea6\u76f8\u4f3c\u6027\uff0c\u8fbe\u5230\u4fdd\u8fb9\u53bb\u566a\u7684\u76ee\u7684\u3002\u5177\u6709\u7b80\u5355\u3001\u975e\u8fed\u4ee3\u3001\u5c40\u90e8\u7684\u7279\u70b9
\u53cc\u8fb9\u6ee4\u6ce2\u5668\u7684\u597d\u5904\u662f\u53ef\u4ee5\u505a\u8fb9\u7f18\u4fdd\u5b58\uff08edge preserving\uff09\uff0c\u4e00\u822c\u7528\u9ad8\u65af\u6ee4\u6ce2\u53bb\u964d\u566a\uff0c\u4f1a\u8f83\u660e\u663e\u5730\u6a21\u7cca\u8fb9\u7f18\uff0c\u5bf9\u4e8e\u9ad8\u9891\u7ec6\u8282\u7684\u4fdd\u62a4\u6548\u679c\u5e76\u4e0d\u660e\u663e\u3002\u53cc\u8fb9\u6ee4\u6ce2\u5668\u987e\u540d\u601d\u4e49\u6bd4\u9ad8\u65af\u6ee4\u6ce2\u591a\u4e86\u4e00\u4e2a\u9ad8\u65af\u65b9\u5deesigma\uff0dd\uff0c\u5b83\u662f\u57fa\u4e8e\u7a7a\u95f4\u5206\u5e03\u7684\u9ad8\u65af\u6ee4\u6ce2\u51fd\u6570\uff0c\u6240\u4ee5\u5728\u8fb9\u7f18\u9644\u8fd1\uff0c\u79bb\u7684\u8f83\u8fdc\u7684\u50cf\u7d20\u4e0d\u4f1a\u592a\u591a\u5f71\u54cd\u5230\u8fb9\u7f18\u4e0a\u7684\u50cf\u7d20\u503c\uff0c\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86\u8fb9\u7f18\u9644\u8fd1\u50cf\u7d20\u503c\u7684\u4fdd\u5b58\u3002\u4f46\u662f\u7531\u4e8e\u4fdd\u5b58\u4e86\u8fc7\u591a\u7684\u9ad8\u9891\u4fe1\u606f\uff0c\u5bf9\u4e8e\u5f69\u8272\u56fe\u50cf\u91cc\u7684\u9ad8\u9891\u566a\u58f0\uff0c\u53cc\u8fb9\u6ee4\u6ce2\u5668\u4e0d\u80fd\u591f\u5e72\u51c0\u7684\u6ee4\u6389\uff0c\u53ea\u80fd\u591f\u5bf9\u4e8e\u4f4e\u9891\u4fe1\u606f\u8fdb\u884c\u8f83\u597d\u7684\u6ee4\u6ce2
Nonlinear
Complex, spatially varying kernels
Cannot be pre-computed
Brute-force implementation is slow > 10min
\\(h(x)=k_r^{\u22121}(x)\u222b^{+\u221e}_{\u2212\u221e}\u222b^{+\u221e}_{\u2212\u221e}f(\u03be)c(\u03be,x)s(f(\u03be),f(x))d\u03be\\)
\\(c(\u03be,x)=\\frac{1}{2\\pi\\sigma_s^2}e^{-\\frac{||\u03be-x||^2}{2\\sigma_s^2}}\\)
\\(s(f(\u03be),f(x))=\\frac{1}{2\\pi\\sigma_r^2}e^{-\\frac{||f(\u03be)-f(x)||^2}{2\\sigma_r^2}}\\)
\\(Which\\) \\(is\\) \\(BF[I]p=\\frac{1}{W_p}\\sum_{q\\in S}G_{\\sigma_s}(||p-q||)G_{\\sigma_r}(|O_p-I_q|)I_q\\) !!!
\\(\\frac{1}{W_p}\\)\u4e3a\u5f52\u4e00\u5316\u56e0\u5b50
\\(\u03c3_s\\) \u8d8a\u5c0f\uff0c\u4e2d\u5fc3\u70b9\u6743\u91cd\u8d8a\u5927\uff0c\u5468\u56f4\u70b9\u6743\u91cd\u8d8a\u5c0f\uff0c\u5bf9\u56fe\u50cf\u7684\u6ee4\u6ce2\u4f5c\u7528\u8d8a\u5c0f\uff0c\u8d8b\u4e8e\u96f6\u65f6\uff0c\u8f93\u51fa\u7b49\u540c\u4e8e\u539f\u56fe.
\\(\u03c3_r\\) \u8d8a\u5c0f\uff0c\u8fb9\u7f18\u8d8a\u6e05\u6670\uff0c\u6781\u9650\u60c5\u51b5\u4e3a \\(\u03c3_r\\) \u65e0\u9650\u63a5\u8fd1 0\uff0c\u503c\u57df\u7cfb\u6570\u9664\u4e86\u4e2d\u5fc3\u4f4d\u7f6e\uff0c\u5176\u4ed6\u8fd1\u4f3c\u4e3a 0(\u63a5\u8fd1 \\(e_\u221e = 0\\))\uff0c\u4e0e\u9ad8\u65af\u6a21\u677f(\u7a7a\u95f4\u57df\u6a21\u677f)\u76f8\u4e58\u8fdb\u884c\u6ee4\u6ce2\u7684\u7ed3\u679c\u7b49\u6548\u4e8e\u539f\u56fe\u50cf.
"},{"location":"DIP/filter/filter/#how-to-set-the-parameters","title":"How to Set the Parameters","text":"Depends on the application. For instance:
e.g., 2% of image diagonal
e.g., mean or median of image gradients
independent of resolution and exposure
Iterating the Bilateral Filter
Small spatial sigma (e.g. 7x7 window)
Adapt intensity sigma to noise level
\u5377\u79ef\u5b9a\u7406\u544a\u8bc9\u6211\u4eec\uff0c\u5728\u9891\u57df\u4e2d\uff0c\u5377\u79ef\u7b49\u4ef7\u4e8e\u76f8\u4e58\u3002\u6240\u4ee5\uff0c\u5728\u9891\u57df\u4e2d\uff0c\u56fe\u50cf F(u,v*) \u4e0e\u6ee4\u6ce2\u6838 Hb(u,v)\u7684\u5377\u79ef\u7ed3\u679c G(u,v)\u53ef\u4ee5\u8868\u793a\u4e3a\uff1a
\\(G(u,v)=F(u,v)\u22c5H_b(u,v)\\)
\u53c2\u8003 https://blog.csdn.net/xijuezhu8128/article/details/111304006
\u5176\u57fa\u672c\u601d\u60f3\u5c31\u662f\u5c06\u975e\u7ebf\u6027\u7684\u53cc\u8fb9\u6ee4\u6ce2\u6539\u6210\u53ef\u5206\u79bb\u7684\u7ebf\u6027\u64cd\u4f5c\u548c\u975e\u7ebf\u6027\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u539f\u6765\u7684\u53cc\u8fb9\u6ee4\u6ce2\u5728\u56fe\u50cf\u4e0d\u540c\u4f4d\u7f6e\u5e94\u7528\u4e0d\u540c\u7684\u6743\u91cd\uff0c\u4e5f\u5c31\u662f\u4f4d\u79fb\u6539\u53d8\u5377\u79ef\uff0c\u4ed6\u4eec\u901a\u8fc7\u589e\u52a0\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u4e5f\u5c31\u662f\u5c06\u7070\u5ea6\u503c\u4f5c\u4e3a\u4e00\u4e2a\u65b0\u7684\u7ef4\u5ea6\uff0c\u5c06\u53cc\u8fb9\u6ee4\u6ce2\u8868\u8fbe\u62103D\u7a7a\u95f4\u4e2d\u7684\u7ebf\u6027\u4f4d\u79fb\u4e0d\u53d8\u5377\u79ef\uff0c\u6700\u540e\u518d\u6267\u884c\u975e\u7ebf\u6027\u7684\u5f52\u4e00\u5316\u64cd\u4f5c\u3002
"},{"location":"DIP/filter/filter/#derivation","title":"Derivation","text":"(1\uff09 \u9996\u5148\u5c06\u539f\u59cb\u53cc\u8fb9\u6ee4\u6ce2\u516c\u5f0f\u7b49\u5f0f\u5de6\u53f3\u7686\u5de6\u4e58\\(W_p^b\\)\uff0c\u5e76\u5c06\u4e24\u4e2a\u516c\u5f0f\u901a\u8fc7\u4e8c\u7ef4\u5411\u91cf\u8868\u8fbe\u6210\u5355\u4e2a\u516c\u5f0f\uff1a
\uff082\uff09\u7b49\u5f0f\u53f3\u4fa7\u4e58\u4ee5\\(W_q\\)\uff0c\\(W_q\\)=1\uff1a
\u4e0a\u56fe\u4e2d\uff0c\u5982\u679c\u5ffd\u7565\u7b49\u5f0f\u53f3\u4fa7\u7684\\(G_{\\sigma_r}(|I_p-I_q|)\\)\u8fd9\u4e00\u9879\uff0c\u90a3\u4e48\u8be5\u7b49\u5f0f\u8868\u8fbe\u7684\u5c31\u662f\u7ecf\u5178\u7684\u9ad8\u65af\u6ee4\u6ce2
\u53ef\u4ee5\u7b80\u5199\u4e3a\u4ee5\u4e0b\u5377\u79ef\u7684\u5f62\u5f0f\uff1a\\(\\begin{pmatrix}W^bI^b\\\\W^b\\end{pmatrix}=G_{\\sigma_r}*\\begin{pmatrix}WI\\\\W\\end{pmatrix}\\)
\uff083\uff09\u589e\u7ef4\uff0c\u589e\u52a0\u5f3a\u5ea6\u7ef4\uff08\u4e5f\u5c31\u662f\u7070\u5ea6\u503c\uff09\uff1a
\u4f7f\u7528\\(Kronecker\\)\u51fd\u6570\uff0c\\(\\delta\\)\u53ea\u5728\\(0\\)\u70b9\u4e3a\\(1\\)\uff0c\u5176\u4ed6\u4e3a0
THUS
\u4e0a\u5f0f\u53ef\u4ee5\u8868\u8fbe\u4e3a\u5728\u70b9\uff08\\(p\\), \\(I_p\\)\uff09\u4f4d\u7f6e\u5904\u4e09\u7ef4\u5377\u79ef\u7684\u5f62\u5f0f\uff1a
\uff085\uff09\u6574\u4e2a\u6d41\u7a0b\uff1a\u7ebf\u6027\u5377\u79ef+\u975e\u7ebf\u6027\u5f52\u4e00\u5316
\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u4e3b\u8981\u7684\u52a0\u901f\u6765\u81ea\u4e8e\u5728\u9891\u57df\u4e2d\u6267\u884c\u5377\u79ef\u64cd\u4f5c\u3002\u9891\u57df\u5377\u79ef\u901a\u5e38\u6bd4\u65f6\u57df\u5377\u79ef\u66f4\u5feb\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u5feb\u901f\u5085\u91cc\u53f6\u53d8\u6362\uff08FFT\uff09\u7b49\u9ad8\u6548\u7b97\u6cd5\u65f6\u3002
"},{"location":"DIP/filter/filter/#guided-filter","title":"Guided Filter","text":"\u5f15\u5165 guided image \\(I\\)
\u4fdd\u6301\u68af\u5ea6\uff0cso \\(q_i(output\\ image)=aI_i(guided\\ image)+b\\)
Use Lagrange multiplier method , we can get \\(a=\\frac{cov(I,p)}{var(I)+\\epsilon} \\ \\ \\ b=\\bar{p}-a\\bar{I}\\)
\u5bf9\u8fb9\u7f18\u7684\u5b9a\u4e49\u4e0d\u6e05\u6dc5\uff0c\u800c\u4e14\u8fb9\u7f18\u662f context-dependent \u7684\u3002\u8089\u773c\u4e2d\u7684\u8fb9\u754c\uff0c\u53ef\u80fd\u4e0d\u88ab\u8ba4\u4e3a\u662f\u8fb9\u754c\uff0c\u6700\u7ec8\u8fd8\u662f\u4f1a\u51fa\u73b0 halo \u7684\u73b0\u8c61\u3002
"},{"location":"DIP/filter/filter/#sparse-norm-filter","title":"Sparse Norm Filter","text":"https://note.hobbitqia.cc/dip/dip9/#core-algorithm
"},{"location":"DIP/filter/filter/#p1","title":"p=1","text":"\\(l^1\\) norm filter is the median filter(\u4e2d\u503c\u6ee4\u6ce2)
\\(l^2\\) norm mean filter(\u5747\u503c\u6ee4\u6ce2)
"},{"location":"DIP/morph/morph/","title":"Image morphing","text":""},{"location":"DIP/morph/morph/#morph","title":"Morph","text":"
Morph is not warp (geometric transformation)Morph is a kind of morphological changing, which makes an image change to another image gradually. Morph handles both the location and the intensity of a pixel.The beginning image and end image are two key frames. Other frames between the two key frames are generated automatically.
\u5bf9\u4e8e\u7070\u5ea6\u56fe\u50cf\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u7b49\u6bd4\u6216\u7b49\u5dee\u7ea7\u6570\u5b9a\u4e49\u6b65\u957f\uff0c\u4f7f\u989c\u8272\u4ece\u539f\u56fe\u53d8\u5230\u76ee\u6807\u56fe\u3002
\u5bf9\u4e8e\u5f69\u8272\u56fe\u50cf\uff0cRGB\u4e09\u8272\u540c\u65f6\u4ece\u539f\u56fe\u53d8\u5230\u76ee\u6807\u56fe\u50cf\u3002\u53ef\u4ee5\u5206\u522b\u53d8\u5316\uff0c\u4e5f\u53ef\u8003\u8651RGB\u7684\u76f8\u5bf9\u6bd4\u4f8b\u5173\u7cfb\u540c\u65f6\u53d8\u5316\u3002
\u9009\u62e9\u4e24\u5e45\u7ed3\u6784\u76f8\u4f3c\u3001\u5927\u5c0f\u76f8\u540c\u7684\u753b\u9762\u4f5c\u4e3a\u8d77\u59cb\u548c\u7ed3\u675f\u5173\u952e\u5e27\uff0c\u8fd9\u6837\u624d\u80fd\u6bd4\u8f83\u5bb9\u6613\u5730\u5b9e\u73b0\u81ea\u7136\u3001\u8fde\u7eed\u7684\u4e2d\u95f4\u53d8\u5f62\u8fc7\u7a0b\u3002
\u5728\u8d77\u59cb\u548c\u7ed3\u675f\u753b\u9762\u4e0a\u786e\u5b9a\u548c\u52fe\u753b\u51fa\u5404\u90e8\u5206\uff08\u4e3b\u8981\u8f6e\u5ed3\uff09\u7684\u7ed3\u6784\u5bf9\u5e94\u5173\u7cfb\uff0c\u4e5f\u5373\u4ece\u8d77\u59cb\u753b\u9762\u4e0a\u7684\u4e00\u4e2a\u70b9\u53d8\u5230\u7ed3\u675f\u753b\u9762\u4e0a\u7684\u53e6\u4e00\u4e2a\u5bf9\u5e94\u70b9\u7684\u4f4d\u7f6e\uff0c\u8fd9\u662f\u53d8\u5f62\u8fd0\u7b97\u6240\u9700\u8981\u7684\u53c2\u6570
\u6839\u636e\u9700\u8981\uff0c\u5bf9\u5e94\u70b9\u7684\u4f4d\u7f6e\u53ef\u4ee5\u4efb\u610f\u79fb\u52a8\u3002\u8c03\u6574\u8d77\u59cb\u5e27\u7684\u5bf9\u5e94\u70b9\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u6a21\u62df\u6444\u50cf\u4e2d\u7684\u955c\u5934\u6e10\u53d8\u6548\u679c
"},{"location":"DIP/morph/morph/#morph-based-on-segment-two-points","title":"Morph based on segment (two points)","text":""},{"location":"DIP/morph/morph/#morph-based-on-grid","title":"Morph based on grid","text":"\u89c6\u70b9\u53d8\u6362\u5c31\u662f\u6c42\u5728\u89c6\u70b9\u79fb\u52a8\u540e\u539f\u59cb\u56fe\u50cf\u53d8\u6362\u751f\u6210\u7684\u65b0\u89c6\u56fe
\u5728\u7528\u76f8\u673a\u62cd\u6444\u7269\u4f53\u65f6\uff0c\u4fdd\u6301\u7269\u4f53\u7684\u4f4d\u7f6e\u4e0d\u52a8\uff0c\u800c\u5c06\u76f8\u673a\u79fb\u79bb\u7269\u4f53\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u89c6\u70b9\u53d8\u6362
\u5982\u679c\u6211\u4eec\u4fdd\u6301\u76f8\u673a\u56fa\u5b9a\u4f4d\u7f6e\uff0c\u5c06\u7269\u4f53\u79fb\u79bb\u76f8\u673a\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u6a21\u578b\u8f6c\u6362\u3002\u89c6\u70b9\u53d8\u5f62\u8981\u6c42\u5bf9\u7269\u4f53\u6709\u4e09\u7ef4\u5750\u6807\u77e5\u8bc6\u3002
Expressive Expression Mapping with Ratio Images
"},{"location":"DIP/morph/morph/#lambertian-model","title":"Lambertian model","text":"Assume there are m point light sources:
\\(I=\\rho\\sum_{1\\le i\\le m}S_iI_in\u00b7l_i=\\rho E(n)\\)
\\(S_i=\\left\\{\\begin{array}{ll}0 & \\text{if the point cannot be seen from light i }\\\\1& \\text{Otherwise}\\end{array}\\right.\\)
\u5176\u4e2d \\(l_i\\) \u7b2c i \u4e2a\u5149\u6e90\u7167\u8fc7\u6765\u7684\u89d2\u5ea6
\\(n\\) \u8868\u793a\u5f53\u524d\u8fd9\u4e2a\u66f2\u9762\u7684\u6cd5\u5411\u91cf\uff0c\u6240\u4ee5\\(n\u00b7l_i\\)\u76f8\u5f53\u4e8e\u4f59\u5f26\u5173\u7cfb
\\(I_i\\) \u8868\u793a\u7b2c i \u4e2a\u5149\u6e90\u7684\u4eae\u5ea6
\\(S_i\\)\u8868\u793a\u66f2\u9762\u80fd\u4e0d\u80fd\u88ab\u7b2c i \u4e2a\u70b9\u5149\u6e90\u7167\u5230
\\(\\rho\\) \u8868\u793a\u66f2\u9762\u7684\u6750\u8d28
"},{"location":"DL_CV/CNN/lec/#convolutional-networks","title":"Convolutional Networks","text":"
torch.nn.Conv1d(in_channels,out_channels,lernel_size,stride=1,padding=0,dilation =1,grounps=1,bias=True,padding_mode='zeros')\n
torch.nn.Conv3d(in_channels,out_channels,kernel_size,stride=1,padding=0.dilation=1,groups=1,bias=True,padding_mode='zeros')\n
Problem: Deep Networks very hard to train!
"},{"location":"DL_CV/CNN/lec/#normalization","title":"Normalization","text":"https://medium.com/techspace-usict/normalization-techniques-in-deep-neural-networks-9121bf100d8
https://zhuanlan.zhihu.com/p/56542480
"},{"location":"DL_CV/CNN/lec/#batch-normalization","title":"Batch Normalization","text":"Why? Helps reduce \u201cinternal covariate shift\u201d, improves optimization
Learnable scale and shift parameters: \\(\\gamma\\ \\beta\\)\u200b
problem : If in two pics,one has a batch of a cat and the other has a batch of dog, we donot expect them to be in different classes,but the batch normalization will cause such problems
Use constant \\(\\mu\\) and \\(\\sigma\\)\u200b !
"},{"location":"DL_CV/Pytorch/lec/#deep-learning-hardware","title":"Deep Learning Hardware","text":"
Tensor
Create
x = torch.empty(3, 4)\nprint(type(x))\nprint(x)\n'''Out:\n<class 'torch.Tensor'>\ntensor([[0., 0., 0., 0.],\n [0., 0., 0., 0.],\n [0., 0., 0., 0.]])\n'''\nzeros = torch.zeros(2, 3)\nones = torch.ones(2, 3)\ntorch.manual_seed(1729)\nrandom = torch.rand(2, 3)\n
_like methods
x = torch.empty(2, 2, 3)\nprint(x.shape)\nprint(x)\n\nempty_like_x = torch.empty_like(x)\nprint(empty_like_x.shape)\nprint(empty_like_x)\n\nzeros_like_x = torch.zeros_like(x)\nprint(zeros_like_x.shape)\nprint(zeros_like_x)\n\nones_like_x = torch.ones_like(x)\nprint(ones_like_x.shape)\nprint(ones_like_x)\n\nrand_like_x = torch.rand_like(x)\nprint(rand_like_x.shape)\nprint(rand_like_x)\n
"},{"location":"DL_CV/Pytorch/lec/#fundamental-concepts","title":"Fundamental Concepts","text":"import torch\ndevice = torch.device('cpu')\n#device = torch.device('cuda:0')\n#device = torch.device('mps')\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in,device=device)\ny = torch.randn(N,D_out,device=device)\nw1 = torch.randn(D_in,H,device=device)\nw2 = torch.randn(H,D_out,device=device)\nlearning_rate=1e-6\nfor t in range(500):\n h = x.mm(w1)\n h_relu = h.clamp(min=0)\n y_pred = h_relu.mm(w2)\n loss = (y_pred-y).pow(2).sum\n\n grad_y_pred = 2.0*(y_pred-y)\n grad_w2 = h_relu.t().mm(grad_y_pred)\n grad_h_relu = grad_y_pred.mm(w2.t())\n grad_h = grad_h_relu.clone()\n grad_h[h<0]=0\n grad_21 = x.t()mm(grad_h)\n\n w1 -= learning_rate * grad_w1\n w2 -= learning_rate * grad_w2\n
a = torch.ones((2, 3), dtype=torch.int16)
rand = torch.rand(2, 4)\ndoubled = rand * (torch.ones(1, 4) * 2)\nprint(rand)\nprint(doubled)\n
Moving to GPU
device = torch.device(\"mps\")\nmodel = ModelName(xxx).to(device)\ndata = torch.Tensor(dataset.x).to(device)\n
Changing dimensions
a = torch.rand(3, 226, 226)\nb = a.unsqueeze(0)\nprint(a.shape)\nprint(b.shape)\n
numpy
import numpy as np\n\nnumpy_array = np.ones((2, 3))\nprint(numpy_array)\n\npytorch_tensor = torch.from_numpy(numpy_array)\nprint(pytorch_tensor)\n
Autograd: Package for building computational graphs out of Tensors, and automatically computing gradients
import torch\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in, H ,requires_grad=True)\nw2 = torch.randn(H,D_out,requires_grad=True)\nlearning_rate=1e-6\nfor t in range(500):\n y_pred = x.mm(w1).clamp(min=0).mm(w2)\n loss = (y_pred-y).pow(2).sum()\n loss.backward()\n with torch.no_grad():\n # Don't do computational graph in this stage [donnot do grad computation in this stage]\n w1 -= learning_rate * w1.grad\n w2 -= learning_rate * w2.grad\n w1.grad.zero_()\n w2.grad.zero_()\n
After backward finishes, gradients are accumulated into \\(w1.grad\\) and \\(w2.grad\\) and the graph is destroyed -- FORGET this is a common bug!
Can define new operations using Python functions
def sigmoid(x):\n return 1.0/(1.0+(-x).exp())\n# y_pred = sigmoid(x.mm(w1)).mm(w2)\n
def sigmoid(x):\n return 1.0/(1.0+(-x).exp())\nimport torch\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in, H ,requires_grad=True)\nw2 = torch.randn(H,D_out,requires_grad=True)\nlearning_rate=1e-6\nfor t in range(500):\n y_pred = sigmoid(x.mm(w1)).mm(w2)\n loss = (y_pred-y).pow(2).sum()\n loss.backward()\n with torch.no_grad():\n # Don't do computational graph in this stage [donnot do grad computation in this stage]\n w1 -= learning_rate * w1.grad\n w2 -= learning_rate * w2.grad\n w1.grad.zero_()\n w2.grad.zero_()\n
Improvement
class Sigmoid(torch.autograd.Function):\n @staticmethod\n def forward(ctx,x):\n y = 1.0/(1.0+(-x).exp())\n ctx.save_for_backward(y)\n return y\n def backward(ctx,grad_y):\n y,=ctx.saved_tensors\n grad_x = grad_y*y*(1.0-y)\n return grad_x\ndef sigmoid(x):\n return SIgmoid.apply(x)\n
Module: A neural network layer ; may store state or learnable weights
nn : Higher-level wrapper for working with neural nets
import torch\nN,D_in,H,D_out = 64,1000,100,10\n\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\n'''\nObject-oriented API: Define model object as sequence of layers objects, each of which holds weight tensors\n'''\nmodel = torch.nn.Sequential(\n torch.nn.Linear(D_in,H),\n torch.nn,ReLU(),\n torch.nn.Linear(H,D_out)\n)\nlearning_rate = 1e-4\noptimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)\nfor t in range(500):\n y_pred = model(x)\n loss = torch.nn.functional.mse_loss(y_pred,y)#torch.nn.functional has useful helpers like loss functions\n loss.backward()\n with torch.no_grad():\n for param in model.parameters():\n param -= learning_rate * param.grad\n model.zero_grad( )\n
import torch\nN,D_in,H,D_out = 64,1000,100,10\n\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\n'''\nObject-oriented API: Define model object as sequence of layers objects, each of which holds weight tensors\n'''\nmodel = torch.nn.Sequential(\n torch.nn.Linear(D_in,H),\n torch.nn,ReLU(),\n torch.nn.Linear(H,D_out)\n)\nlearning_rate = 1e-4\noptimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)\nfor t in range(500):\n y_pred = model(x)\n loss = torch.nn.functional.mse_loss(y_pred,y)#torch.nn.functional has useful helpers like loss functions\n loss.backward()\n optimizer.step()\n optimizer.zero_grad()\n
import torch \nclass TwoLayerNet(torch.nn.Module):\n def __init__(self,D_in,H,D_out):\n super(TwoLayerNet,self).__init__()\n self.linear1 = torch.nn.Linear(D_in,H)\n self.linear2 = torch.nn.Linear(H,D_out)\n def forward(self,x):\n h_relu = self.linear1(x).clamp(min=0)\n y_ored = self.linear2(h_relu)\n return y_pred\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nmodel = TwoLayerNet(D_in,H,D_out)\noptimizer = torch.optim.SGD(model.parameters(),lr=1e-4)\nfor t in range(500):\n y_pred = model(x)\n loss = torch.nn.functional.mse_loss(y_pred,y)\n loss.backward()\n optimizer.step()\n optimizer.zero_grad()\n
import torch\nclass ParallelBlock(torch.nn.Module):\n def __init__(self,D_in,D_out):\n super(ParallelBlock,self)._init__()\n self.linear1 = torch.nn.Linear(D_in,D_out)\n self.linear2 = torch.nn.Linear(D_in,D_out)\n def forward(self,x):\n h1 = self.linear1(x)\n h2 = self.linear2(x)\n return (h1*h2).clamp(min = 0)\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nmodel = torch.nn.Sequential(\n ParallelBlock(D_in,H),\n ParallelBlock(H,H),\n torch.nn.Linear(H,D_out)\n)\noptimizer = torch.optim.Adam(model.parameters(),lr = 1e-4)\nfor t in range(500):\n y_pred = model(x)\n loss = torch.nn.functional.mse_loss(y_pred,y)\n loss.backward()\n optimizer.step()\n optimizer.zero_grad()\n
import torch\nfrom torch.utils.data import TensorDataset,DataLoader\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nloader = DataLoader(TensorDataset(x,y),batch_size = 8)\nmodel = TwoLayerNet(D_in,H,D_out)\noptimizer = torch.optim.SGD(model.parameters(),lr= = 1e-2)\nfor epoch in range(20):\n # Iterate over loader to form minibatches\n for x_batch,y_batch in loader:\n y_pred = model(x_batch)\n loss = loss.nn.functional.mse_loss(y_pred,y_batch)\n loss.backward()\n optimizer.step()\n optimizer.zero_grad()\n
import torch\nimport torchvision\nalexnet = torchvision.models.alexnet(pretrained = True)\nvgg16 = torchvision.models.vgg16(pretrained = True)\nresnet101 = torchvision.models.resnet101(pretrained = True)\n
Note : this model doesn\u2019t makes sense! Just a simple dynamic example
import torch\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in,H,requires_grad = True)\nw2a = torch.randn(H,D_out,requires_grad = True)\nw2b = torch.randn(H,D_out,requires_grad = True)\nlearning_rate=1e-6\nfor t in range(500):\n # Decide which one to use at each layer based on loss at previous iteration\n w2 = w2a if prev_loss <5.0 else w2b\n y_pred = x.mm(w1).clamp(min=0).mm(w2)\n loss = (y_pred - y).pow(2).sum\n loss.backward()\n prev_loss = loss.item()\n
Static Computation Graphs
Step 1: Build computational graph describing our computation (including finding paths for backprop)
import torch \ndef model(x,y,w1,w2a,w2b,prev_loss):\n w2 = w2a if prev_loss <5.0 else w2b\n y_pred = x.mm(w1).clamp(min=0).mm(w2)\n loss = (y_pred - y).pow(2).sum\n return loss\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in,H,requires_grad = True)\nw2a = torch.randn(H,D_out,requires_grad = True)\nw2b = torch.randn(H,D_out,requires_grad = True)\n#Just-In-Time compilation: Introspect the source code of the function, compile it into a graph object.\ngraph = torch.jit.script(model)\nprev_loss = 5.0\nlearning_rate = 1e-6\nfor t in range(500):\n loss = graph(x,y,w1,w2a,w2b,prev_loss)\n loss.backward()\n prev_loss = loss.item()\n
import torch\n@torch.jit.script\ndef model(x,y,w1,w2a,w2b,prev_loss):\n w2 = w2a if prev_loss <5.0 else w2b\n y_pred = x.mm(w1).clamp(min=0).mm(w2)\n loss = (y_pred - y).pow(2).sum\n return loss\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in,H,requires_grad = True)\nw2a = torch.randn(H,D_out,requires_grad = True)\nw2b = torch.randn(H,D_out,requires_grad = True)\nprev_loss = 5.0\nlearning_rate = 1e-6\nfor t in range(500):\n loss = model(x,y,w1,e2a,w2b,prev_loss)\n loss.backward()\n prev_loss = loss.item()\n
Static
With static graphs, framework can optimize the graph for you before it runs!
Once graph is built, can serialize it and run it without the code that built the graph!
e.g. train model in Python, deploy in C++
Lots of indirection between the code you write and the code that runs \u2013 can be hard to debug, benchmark, etc
Dynamic
Graph building and execution are intertwined, so always need to keep code around
The code you write is the code that runs! Easy to reason about, debug, profile, etc
Dynamic Graph Applications
Model structure depends on the input:
TensorFlow 1.0 : Static Graphs
First define computational graph
Then run the graph many times
import numpy as np\nimport tensorflow as tf\nN,D,H = 64,1000,100\nx = tf.placeholder(tf.float32,shape = (N,D))\ny = tf.placeholder(tf.float32,shape = (N,D))\nw1 = tf.placeholder(tf.float32,shape = (D,H))\nw2 = tf.placeholder(tf,float32,shape = (H,D))\n\nh = tf.maximum(tf.matmul(x,w1),0)\ny_pred = tf.matmul(h,w2)\ndiff = y_pred - y\nloss = tf.reduce_mean(tf.refuce_sum(diff ** 2,axis = 1))\ngrad_w1,grad_e2 = tf.grafients(loss,[w1,w2])\n\nwith tf.Session() as sess :\n values = {\n x:np.random.randn(N,D),\n w1:np.random.randn(D,H),\n w2:np.random.randn(H,D),\n y:np.random.randn(N,D),\n }\n out = sess.run(\n [loss,grad_w1,grad_w2],\n feed_dict = values\n )\n loss_val,grad_w1_val,grad_w2_val = out\n
TensorFlow 2.0: Dynamic Graphs
Create TensorFlow Tensors for data and weights
Weights need to be wrapped in tf.Variable so we can mutate them
import tensorflow as tf\nN,Din,H,Dout = 16,1000,100,10\nx = tf.random.noraml((N,Din))\ny = tf.random,normal((N,Dout))\nw1 = tf.Variable(tf.random.normal(Din,H))\nw2 = tf.Variable(tf.random.normal(H,Dout))\nfor t in range(1000):\n #Scope forward pass under a GradientTape to tell TensorFlow to start building a graph\n with tf,GradientTape() as tape :\n h = tf.maximum(tf.matmul(x,w1),0)\n y_pred = tf.matmul(h,w2)\n diff = y_pred - y\n loss = tf.reduce_mean(tf.reduce_sum(diff **2 , axis = 1))\n #Ask the tape to compute gradients\n grad_w1,grad_e2 = tape.gradient(loss,[w1,w2])\n # Gradient descent step, update weights\n w1.assign(w1-learning_rate*grad_w1)\n w2.assign(w2-learning-learning_rate*grad_w2)\nlearning_rate\n
@tf.function\ndef step(x,y,w1,w2):\n with tf,GradientTape() as tape :\n h = tf.maximum(tf.matmul(x,w1),0)\n y_pred = tf.matmul(h,w2)\n diff = y_pred - y\n loss = tf.reduce_mean(tf.reduce_sum(diff **2 , axis = 1))\n #Ask the tape to compute gradients\n grad_w1,grad_e2 = tape.gradient(loss,[w1,w2])\n w1.assign(w1-learning_rate*grad_w1)\n w2.assign(w2-learning-learning_rate*grad_w2)\n return loss\nN,Din,H,Dout = 16,1000,100,10\nx = tf.random.noraml((N,Din))\ny = tf.random,normal((N,Dout))\nw1 = tf.Variable(tf.random.normal(Din,H))\nw2 = tf.Variable(tf.random.normal(H,Dout))\nlearning_rate = 1e-6\nfor t in range(1000):\n loss = step(x,y,w1,w2)\n
import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import InputLayer,Dense\nN,Din,H,Dout = 16,1000,100,10\n\nmodel = Sequential()\nmodel.add(InputLayer(input_shape=(Din,)))\nmodel.add(Dense(units = H,activation = 'relu'))\nmodel.add(Dense(units = Dout))\n\nparams = model.trainable_variables\nloss_fn = tf.keras.losses.MeanSquaredError()\nopt = tf.kears.optimizers.SGD(learning_rate = 1e-6)\nx = tf.random.normal((N,Din))\ny = tf.random.noraml((N,Dout))\ndef step():\n y_pred = model(x)\n loss = loss_fn(y_pred,y)\n return loss\nfor t in range(1000):\n opt.minimize(step,params)\n
"},{"location":"DL_CV/Recognition/lec10/","title":"Recognision","text":""},{"location":"DL_CV/Recognition/lec10/#object-detection","title":"Object Detection","text":"
Input: Single RGB Image
Output: A set of detected objects:
Challenges
Apply a CNN to many different crops of the image, CNN classifies each crop as object or background
We should consider all possible sizes and possible positions
Solutions
Region Proposals
Use MultiTask Loss
Bbox : Modify the Region Proposals
\\(L_2\\) regularization encourages leaving proposal unchanged.
Transform encodes relative difference between proposal and output; important since \\(CNN\\) doesn\u2019t see absolute size or position after cropping
"},{"location":"DL_CV/Recognition/lec10/#r-cnn-training","title":"R-CNN Training","text":"Positive: > 0.5 IoU with a GT box
Negative: < 0.3 IoU with all GT boxes
Neutral: between 0.3 and 0.5 IoU with GT boxes
Problem: Object detectors often output many overlapping detections:
Solution: Post-process raw detections using Non-Max Suppression (NMS)
Select next highest-scoring box
Eliminate lower-scoring boxes with IoU > threshold (e.g. 0.7)
If any boxes remain, GOTO 1
Problem: NMS may eliminate \u201dgood\u201d boxes when objects are highly overlapping...
no good solution =(
R-CNN Problem: Very slow! Need to do 2000 forward passes through CNN per image
Idea: Overlapping proposals cause a lot of repeated work: same pixels processed many times. Can we avoid this?
Per-Region network is relatively lightweight
Most of the computation happens in backbone network;
this saves work for overlapping region proposals.
How to crop features?
"},{"location":"DL_CV/Recognition/lec10/#cropping-features-roi-pool","title":"Cropping Features: RoI Pool","text":"Classic idea: build an image pyramid by resizing the image to different scales, then process each image scale independently.
Problem: Expensive! Don\u2019t share any computation between scales.
Simple Idea : Sliding Window
Use Fully Convolutional Network
Dose not have any pooling layers or fully connected layers.
Problem #1: Effective receptive field size is linear in number of conv layers:
Let's say we have L 3x3 convolutional layers. In a CNN, each neuron in a convolutional layer is connected to a small region of neurons from the previous layer, which is its receptive field. For a 3x3 convolutional kernel, the receptive field size is 3x3.
Now, consider L 3x3 convolutional layers. In the first layer, the receptive field of each neuron is 3x3. In the second layer, the receptive field of each neuron relative to the input image will cover a larger area, which is the combination of receptive fields from the previous layer. Similarly, in the third layer, the receptive field relative to the input image will be even larger.
Therefore, as the number of layers increases, the influence of each layer on the input image gradually expands. Assuming each convolutional layer has a stride of 1, and there are no pooling layers or padding, after L 3x3 convolutional layers, the receptive field size relative to the input image for each neuron will be 1 plus twice the number of convolutional layers, denoted as 1+2L.
Problem #2: Convolution on high res images is expensive! Recall ResNet stem aggressively downsamples
"},{"location":"DL_CV/Recognition/lec10/#in-network-upsampling-unpooling","title":"In-Network Upsampling: \u201cUnpooling\u201d","text":"Refer to Slides
Refer to Slides
Learnable Upsampling: Transposed Convolution
Convolution with stride > 1 is \u201cLearnable Downsampling\u201d
Detect all objects in the image, and identify the pixels that belong to each object (Only things!)
Approach: Perform object detection, then predict a segmentation mask for each object!
"},{"location":"DL_CV/TN/lec/#training-neural-networks","title":"Training Neural Networks","text":""},{"location":"DL_CV/TN/lec/#one-time-setup","title":"One time setup","text":""},{"location":"DL_CV/TN/lec/#activation-functions","title":"Activation Functions","text":""},{"location":"DL_CV/TN/lec/#sigmoid","title":"Sigmoid","text":"
\\(\\sigma(x)=\\frac{1}{1+e^{-x}}\\)
3 problems
What can we say about the gradients on w?
\\(f(x) = max(0,x)\\)\u200b
- Does not saturate (in +region)
- Very computationally efficient
- Converges much faster than sigmoid/tanh in practice (e.g. 6x)
Not zero-centered output
what is the gradient when x < 0?
"},{"location":"DL_CV/TN/lec/#leaky-relu","title":"Leaky ReLU","text":""},{"location":"DL_CV/TN/lec/#exponential-linear-unit-elu","title":"Exponential Linear Unit (ELU)","text":""},{"location":"DL_CV/TN/lec/#scaled-exponential-linear-unit-selu","title":"Scaled Exponential Linear Unit (SELU)","text":"Derivation takes 91 pages of math in appendix...
"},{"location":"DL_CV/TN/lec/#activation-functions-summary","title":"Activation Functions: Summary","text":"Q: What happens if we initialize all W=0, b=0?
A: All outputs are 0, all gradients are the same! No \u201csymmetry breaking\u201d
w = 0.01 * np.random.randn(Din,Dout)\n
Works ~okay for small networks, but problems with deeper networks.
All activations tend to zero for deeper network layers
Q: What do the gradients \\(dL/dW\\) look like? -- All zero, no learning
For CNN : Din will be \\(input\\_channels*kernel\\_size*kernel\\_size\\)\u200b
Remeber?
Derivation:
p = 0.5\nimport numpy as np\ndef train_step(X):\n H1 = np.maximum(0,np.dot(W1,X)+b1)\n U1 = np.random.rand(*H1.shape)<p \n H1 *=U1\n H2 = np.maximum(0,np.dot(W2,H1)+b2)\n U2 = np.random.rand(*H2.shape)<p\n H2* = U2\n out = np.dot(W3,H2)+b3\n
Dropout makes our output random! -- Want to \u201caverage out\u201d the randomness at test-time
\\(y = f(x) = E_z[f(x,z)]=\\int p(z)f(x,z)dz\\)\u200b
But this integral is actually a bit hard
Consider a single neuron
\\(E[a]=\\frac{1}{4}(w_1x+w_2y)+\\frac{1}{4}(w_1x+0y)+\\frac{1}{4}(0x+0y)+\\frac{1}{4}(0x+w_2y)\\\\=\\frac{1}{2}(w_1x+w_2y)\\)
\\(E[a]=w_1x+w_2y\\)
def predict(x):\n H1 = np.maximum(0,np.dot(W1,X)+b1)*p\n H2 = np.maximum(0,np.dot(W2,H1)+b2)*p\n out = np.dot(W3,H2)+b3\n
At test time all neurons are active always.
We must scale the activations so that for each neuron: output at test time = expected output at training time
p = 0.5\ndef train_step(X):\n H1 = np.maximum(0,np.dot(W1,X)+b1)\n U1 = np.random.rand(*H1.shape)<p \n H1 *=U1\n H2 = np.maximum(0,np.dot(W2,H1)+b2)\n U2 = np.random.rand(*H2.shape)<p\n H2* = U2\n out = np.dot(W3,H2)+b3\ndef predict(x):\n H1 = np.maximum(0,np.dot(W1,X)+b1)*p\n H2 = np.maximum(0,np.dot(W2,H1)+b2)*p\n out = np.dot(W3,H2)+b3\n
More common: \u201cInverted dropout\u201d
p = 0.5\ndef train_step(X):\n H1 = np.maximum(0,np.dot(W1,X)+b1)\n U1 = np.random.rand(*H1.shape)<p /p\n H1 *=U1\n H2 = np.maximum(0,np.dot(W2,H1)+b2)\n U2 = np.random.rand(*H2.shape)<p /p\n H2* = U2\n out = np.dot(W3,H2)+b3\ndef predict(x):\n H1 = np.maximum(0,np.dot(W1,X)+b1)\n H2 = np.maximum(0,np.dot(W2,H1)+b2)\n out = np.dot(W3,H2)+b3\n
"},{"location":"DL_CV/TN/lec/#data-augmentation","title":"Data Augmentation","text":""},{"location":"DL_CV/TN/lec/#dropconnect","title":"DropConnect","text":""},{"location":"DL_CV/TN/lec/#fractional-pooling","title":"Fractional Pooling","text":""},{"location":"DL_CV/TN/lec/#stochastic-depth","title":"Stochastic Depth","text":""},{"location":"DL_CV/TN/lec/#mixup","title":"Mixup","text":""},{"location":"DL_CV/class/lec/#image-classifications","title":"Image Classifications","text":"
Challenges :
Fine-Grained Categories(eg.different kinds of cats)
Background Clutter
illumination Changes
Deformation
Occlusion
Machine Learning: Data-Driven Approach
10 classes: Digits 0 to 9 28x28 grayscale images 50k training images 10k test images Results from MNIST often do not hold on more complex datasets!
10 classes
50k training images (5k per class)
10k testing images (1k per class)
32x32 RGB images
100 classes
50k training images (500 per class)
10k testing images (100 per class)
32x32 RGB image
20 superclasses with 5 classes each:
Aquatic mammals: beaver, dolphin, otter, seal, whale
Trees: Maple, oak, palm, pine, willow
1000 classes
\\(\\approx\\) 1.3M training images (~1.3K per class)
50K validation images (50 per class)
100K test images (100 per class)
Performance metric: Top 5 accuracy Algorithm predicts 5 labels for each image
one of them needs to be right
test labels are secret!
Images have variable size, but often resized to 256x256 for training
There is also a 22k category version of ImageNet, but less commonly used
365 classes of different scene types
\\(\\approx\\) 8M training images
18.25K val images (50 per class)
328.5K test images (900 per class)
Images have variable size, often resize to 256x256 for training
1623 categories: characters from 50 different alphabets
20 images per category
Meant to test few shot learning
"},{"location":"DL_CV/class/lec/#first-classifier-nearest-neighbor","title":"First classifier: Nearest Neighbor","text":"def train(imgaes,labels):\n #Machine learning!\n return model\ndef predict(model,test_images):\n #Use model to predict labels\n return test_labels\n
"},{"location":"DL_CV/class/lec/#distance","title":"Distance","text":"import numpy as np\nclass NearestNeightbor:\n def __init__(self):\n pass\n def train(self,X,y):\n '''X is NxD where each row is an example;y is a 1-dimension f size N'''\n self.Xtr = X\n self.ytr = y\n def predict(self,X):\n '''X is NxD where each row is an example we wish to predict label for'''\n num_test = X.shape[0]\n Ypred = np.zeros(num_test,dtype = self.ytr.dtype)\n for i in xrange(num_test):\n distances = np.sum(np.abs(Self.Xtr - X[i,:]),axis = 1)\n min_index = np.argmin(distances)\n Ypred[i]=self.ytr[min_index]\n return Ypred\n
\u8865\uff1anumpy
import numpy as np\n# \u751f\u6210\u793a\u4f8b\u6570\u636e\nN, D = 5, 3\na = np.random.rand(N, D) # \u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a (N, D) \u7684\u968f\u673a\u6570\u7ec4\nb = np.random.rand(D) # \u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a (D,) \u7684\u968f\u673a\u6570\u7ec4\n# \u4f7f\u7528\u5e7f\u64ad\u8fdb\u884c\u51cf\u6cd5\nresult = a - b\n# \u5bf9\u6bcf\u4e00\u884c\u6c42\u548c\uff0c\u5f97\u5230\u5f62\u72b6\u4e3a (N, 1) \u7684\u6570\u7ec4\nsum_result = np.sum(result, axis=1, keepdims=True)\nprint(sum_result)\n
\u200b
\u200b
Distance Metric
\\(L_2\\) (Euclidean) distance : \\(d_2(I_1,I_2)=\\sqrt{\\sum_p(I_1^p-I_2^p)^2}\\)\u200b
With the right choice of distance metric, we can apply K-Nearest Neighbor to any type of data!
hyperparameters: choices about our learning algorithm that we don\u2019t learn from the training data; instead we set them at the start of the learning process
Setting Hyperparameters
"},{"location":"DL_CV/class/lec/#k-nearest-neighbor-universal-approximation","title":"K-Nearest Neighbor: Universal Approximation","text":"Example : Refer to PPT
Curse of dimensionality: For uniform coverage of space, number of training points needed grows exponentially with dimension
Original image is CC0 public domain
K-Nearest Neighbor on raw pixels is seldom used
f(x, W) = Wx (ignore bias)\nf(cx, W) = W(cx) = c * f(x, W)\n
\u6cbf\u7740\u68af\u5ea6\u65b9\u5411 z(which is the score) \u589e\u957f\u6700\u5feb
A loss function(Also called: objective function; cost function) tells how good our current classifier is
Low loss = good classifier
High loss = bad classifier
Given a dataset of examples \\(\\{(x_i,y_i)\\}_{i=1}^N\\) where \\(x_i\\) is image and \\(y_i\\) is (integer) label
Loss for a single example is \\(L_i(f(x_i,W),y_i)\\)
Loss for the dataset is average of per-example losses : \\(L=\\frac{1}{N}\\sum_iL_i(f(x_i,W),y_i)\\)\u200b
\\(L=(2.9+0+12.9)/3\\)\u200b
min - 0
max - infinity
May achieve a \\(L = 1\\) (all are small scores randomly)
All scores be inflatted by a constant one
What if the loss used a mean instead of a sum?
What if we used this loss instead? \\(L_i = \\sum_{j\\ne y_i}max(0,s_y-s_{y_i}+1)^2\\)\u200b
Suppose we found some W with L = 0. Is it unique?
No! 2W is also has L = 0 !
How to Address this problem ? -- Regularization!
"},{"location":"DL_CV/class/lec/#regularization-beyond-training-error","title":"Regularization: Beyond Training Error","text":"\\(L(W) = \\frac{1}{N}\\sum_{i=1}^NL_i(f(x_i,W),y_i)+\\lambda R(W)\\)\u200b
$\\lambda = $\u200b regularization strength (hyperparameter)
Purpose of Regularization:
Express preferences in among models beyond \u201dminimize training error\u201d
Avoid overfitting: Prefer simple models that generalize better
What is the \\(min / max\\) possible loss \\(L_i\\)? A: Min - 0, max - +infinity
If all scores are small random values, what is the loss? A: -log(C) (near log(10) \u2248 2.3 at the beginning stage)
\u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6709\u4e00\u4e2a\u5206\u7c7b\u4efb\u52a1\uff0c\u6a21\u578b\u8f93\u51fa\u7684\u5206\u6570\u7ecf\u8fc7softmax\u51fd\u6570\u8f6c\u6362\u6210\u6982\u7387\u5206\u5e03\u3002\u7531\u4e8e\u6240\u6709\u7684\u5206\u6570\u90fd\u662f\u5c0f\u968f\u673a\u503c\uff0c\u8fd9\u610f\u5473\u7740\u6a21\u578b\u5bf9\u6bcf\u4e2a\u7c7b\u522b\u7684\u9884\u6d4b\u6982\u7387\u90fd\u4f1a\u63a5\u8fd1\u4e8e\u5747\u5300\u5206\u5e03\uff0c\u5373\u6bcf\u4e2a\u7c7b\u522b\u88ab\u9884\u6d4b\u4e3a\u6b63\u7c7b\u7684\u6982\u7387\u90fd\u63a5\u8fd1\u4e8e1/\u7c7b\u522b\u6570\u3002
\u5047\u8bbe\u6709C\u4e2a\u7c7b\u522b\uff0c\u90a3\u4e48\u6bcf\u4e2a\u7c7b\u522b\u88ab\u9884\u6d4b\u4e3a\u6b63\u7c7b\u7684\u6982\u7387\u5927\u7ea6\u662f1/C\u3002\u6839\u636e\u4ea4\u53c9\u71b5\u7684\u5b9a\u4e49\uff0c\u5f53\u771f\u5b9e\u6807\u7b7e\u7684\u6982\u7387\u5206\u5e03\u63a5\u8fd1\u4e8e\u5747\u5300\u5206\u5e03\u65f6\uff0c\u4ea4\u53c9\u71b5\u7684\u503c\u5927\u7ea6\u662f-log(1/C)\u3002\u7531\u4e8e-log(1/C) = log(C)\uff0c\u6240\u4ee5\u4ea4\u53c9\u71b5\u7684\u503c\u5927\u7ea6\u662flog(C)\u3002
log(10)\u662f\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\uff0c\u7ea6\u7b49\u4e8e2.3\u3002\u6240\u4ee5\u5f53\u6240\u6709\u5206\u6570\u90fd\u662f\u5c0f\u968f\u673a\u503c\u65f6\uff0c\u4ea4\u53c9\u71b5\u7684\u503c\u5927\u7ea6\u662f2.3\u3002\u8fd9\u610f\u5473\u7740\u6a21\u578b\u7684\u9884\u6d4b\u4e0e\u771f\u5b9e\u6807\u7b7e\u4e4b\u95f4\u7684\u5dee\u5f02\u6027\u6bd4\u8f83\u5927\uff0c\u56e0\u4e3a\u9884\u6d4b\u7684\u6982\u7387\u5206\u5e03\u63a5\u8fd1\u4e8e\u5747\u5300\u5206\u5e03\uff0c\u800c\u771f\u5b9e\u6807\u7b7e\u7684\u6982\u7387\u5206\u5e03\u662f\u975e\u5747\u5300\u7684\u3002
"},{"location":"DL_CV/class/lec/#compare","title":"Compare","text":"Q: What happens to each loss if I slightly change the scores of the last datapoint?
A: Cross-entropy loss will change; SVM loss will stay the same
Q: What happens to each loss if I double the score of the correct class from 10 to 20?
A: Cross-entropy loss will decrease, SVM loss still 0
"},{"location":"DL_CV/class/lec/#optimization","title":"Optimization","text":"\\(w^* = argmin_wL(w)\\)\u200b
"},{"location":"DL_CV/class/lec/#idea","title":"idea","text":""},{"location":"DL_CV/class/lec/#1-random-search-bad-idea","title":"#1: Random Search (bad idea!)","text":"bestloss = float(\"inf\")\nfor num in xrange(1000):\n W = np.random.randn(10,3073)*0.0001\n loss = L(X_train,Y_train,W )\n if loss < bestloss:\n bestloss = loss\n bestW = W\n print 'in attemp %d the loss was %f , best %f' %(num,loss,bestloss)\n\n# Assume X_test is [3073 x 10000], Y_test [10000 x 1]\nscores = Wbest.dot(Xte_cols) \n# 10X10000,the class scores for all test examples\nYte_predict = np.argmax(scores,axis=0) \n# and calculate accuracy (fraction of predictions that are correst)\nnp.mean(Yte_predict == Yte)\n# returns 0.1555\n
"},{"location":"DL_CV/class/lec/#2-follow-the-slope","title":"#2: Follow the slope","text":""},{"location":"DL_CV/class/lec/#computing-gradients","title":"Computing Gradients","text":"- Numeric gradient: approximate, slow, easy to write
\\(\\frac{df(x)}{dx}=lim_{h\\to 0}\\frac{f(x+h)-f(x)}{h}\\)
- Analytic gradient: exact, fast, error-prone
In practice: Always use analytic gradient, but check implementation with numerical gradient. This is called a gradient check.
def grad_check_sparse(f,x,analytic_grad,num_checks = 10,h=1e-7):\n '''\n sample a few random elements and only return numerical in this dimensions\n '''\n
from pytorch import torch\ntorch.autograd.gradcheck(func,inputs,eps=1e-06,atol = 1e-05,rtol = 0.001,raise_exception = True,check_sparse_nnz=False,nodet_tol = 0.0)\n
"},{"location":"DL_CV/class/lec/#gradient-descent","title":"Gradient Descent","text":"w = initialize_wights()\nfor t in range(num_steps):\n dw = compute_gradient(loss_fn,data,w)\n w-=learning_rate*dw\n
Hyperparameters:
- Weight initialization method - Numberofsteps - Learningrate
Stochastic Gradient Descent
\\(L(W)=E_{(x,y)-p_{data}}[L(x,y,W)]+\\lambda R(W) \\approx \\frac{1}{N}\\sum_{i=1}^NL(x_i,y_i,W)+\\lambda R(W)\\)\u200b
\\(\\triangledown L(W)=\\triangledown_W E_{(x,y)-p_{data}}[L(x,y,W)]+\\lambda \\triangledown _WR(W)\\)\u200b
Problems with SGD
What if the loss function has a local minimum or saddle point? : Zero gradient, gradient descent.
Our gradients come from minibatches so they can be noisy!
- Build up \u201cvelocity\u201d as a running mean of gradients
- Rho gives \u201cfriction\u201d typically rho=0.9 or 0.99
You may see SGD+Momentum formulated different ways, but they are equivalent - give same sequence of x
Nesterov Momentum
\\(v_{t+1} = \\rho v_t -\\alpha \\triangledown f(x_t +\\rho v_t)\\)\u200b
\\(x_{t+1}=x_t + v_{t+1}\\)
Change of variables \\(\\tilde{x_t}=x+t +\\rho v_t\\)\u200b and we get :
$ v_{t+1} = \\rho v_t -\\alpha \\triangledown f(\\tilde{x_t})$\u200b
\\(\\tilde x_{t+1} =\\rho v_t +(1+\\rho )v_{t+1}=\\tilde{x_t}+v_{t+1}+\\rho(v_{t+1}-v_t)\\)\u200b
v = 0\nfor t in range(num_steps):\n dw = compute_gradient(w)\n old_v = v\n v = rho*v -learning_rate*dw\n w -= rho* old_v -(1+rho)*v\n
AdaGrad
grad_squared = 0\nfor t in range(num_steps):\n dw = compute_gradient(w)\n grad_square += dw*dw\n w -= learning_rate * dw / (frad_square.sqrt()+1e-7)\n
Q: What happens with AdaGrad?
Progress along \u201csteep\u201d directions is damped; progress along \u201cflat\u201d directions is accelerated !
Adam(almost):RMSProp + Momentum
# Adam & Momentum\nmoment1 = 0\nmoment2 = 0\nfor t in range(num_steps):\n dw = compute_gradient(w)\n moment1 = beta1*moment1 + (1-beta1)*dw\n moment2 = beta2*moment2+(1-beta2)*dw*dw\n w -= learning_rate*moment1 / (moment2.sqrt()+ 1e-7)\n
Q: What happens at t=0? (Assume \\(beta_2\\) = 0.999)
Bias correction
moment1 = 0\nmoment2 = 0\nfor t in range(num_steps):\n dw = compute_gradient(w)\n moment1 = beta1*moment1 + (1-beta1)*dw\n moment2 = beta2*moment2+(1-beta2)*dw*dw\n moment1_unbias = moment1 / (1-beta1 ** t)\n moment2_unbias = moment2 / (1-beta2 ** t)\n w -= learning_rate*moment1 / (moment2.sqrt()+ 1e-7)\n
"},{"location":"DL_CV/class/lec/#second-order-optimization","title":"Second-Order Optimization","text":""},{"location":"DL_CV/class/lec/#first-order-optimization","title":"First-Order Optimization","text":"Second-Order Taylor Expansion:
\\(L(w)\\approx L(w_0)+(w-w_0)^T\\triangledown _wL(w_0)+\\frac{1}{2}^TH_wL(w_0)(w-w_0)\\)\u200b
Solving for the critical point we obtain the Newton parameter update:
\\(w^* = w_0 -H_wL(w_0)^{-1}\\triangledown_wL(w_0)\\)
Q: Why is this impractical? Hessian has \\(O(N^2)\\) elements Inverting takes \\(O(N^3)\\) with $N = $\u200b(Tens or Hundreds of) Millions
In practice:
"},{"location":"DL_CV/neural/lec/#neural-networks","title":"Neural Networks","text":"
Problem: Linear Classifiers aren\u2019t that powerful
\\(f = W_2 max(0,W_1x)\\) \\(W_2 \\in R^{C\\times H}\\) \\(W_1 \\in R^{H\\times D}\\) \\(x\\in R^D\\)\u200b\u200b
\\(f = W_3 max(0,W_2 max(0,W_1 x))\\)\u200b \\(W_3 \\in E^{C\\times H_2}\\) \\(W_2 \\in R^{H_2\\times H_1}\\) \\(x\\in R^D\\)
"},{"location":"DL_CV/neural/lec/#activation-functions","title":"Activation Functions","text":"Remeber the two-head horse ? (now we can recongnize them !)
import numpy as np\nfrom numpy.random import randn\n# initialize weights and data\nN,Din,H Dout = 64,1000,100,10\nx,y = randn(N,Din),randn(N,Dout)\nw1,w2 = randn(Din,H),randn(H,Dout)\n# compute loss(sigmoid activation)\nfor t in range(10000):\n h = 1.0/(1.0+np.exp(-x.dot(w1)))\n y_pred = h.dot(w2)\n loss = np.square(y_pred-y).sum()\n#Compute gradients\n dy_pred = 2.0 * (y_pred - y)\n dw2 = h.T.dot(dy_pred)\n dh = dy_pred.dot(w2.T)\n dw1 = x.T.dot(dh*h*(1-h))\n#SGD step\n w1 -= 1e-4 * dw1\n w2 -= 1e-4 *dw2\n
"},{"location":"DL_CV/neural/lec/#space-warping","title":"Space Warping","text":""},{"location":"DL_CV/neural/lec/#universal-approximation","title":"Universal Approximation","text":"A neural network with one hidden layer can approximate any function \\(f: R_N \\to R_M\\) with arbitrary precision
Output is a sum of shifted, scaled ReLUs
With 4K hidden units we can build a sum of K bumps
Reality check: Networks don\u2019t really learn bumps!
Universal approximation tells us: Neural nets can represent any function
Universal approximation DOES NOT tell us:
Whether we can actually learn any function with SGD
How much data we need to learn a function
Remember: kNN is also a universal approximator!
"},{"location":"DL_CV/neural/lec/#convex-functions","title":"Convex Functions","text":""},{"location":"DL_CV/neural/lec/#backpropagation","title":"Backpropagation","text":"For the input that is actually the \"max\" the local gradient is 1
the gradient of the other is 0
\\(y_{1,1}=x_{1,1}w_{1,1}+x_{1,2}w_{1,2}+x_{1,3}w_{1,3}\\)
\\(\\frac{dy_{1,1}}{dx_{1,1}}=w_{1,1}\\)\u200b
\\(\\frac{dy_{2,1}}{dx_{1,1}}=0\\)\u200b
\\(\\frac{dL}{dx_{1,1}}=(\\frac{dy}{dx_{1,1}})\u00b7(\\frac{dL}{dy})=(w1,;)\u00b7(\\frac{dL}{dy_1,;})=3*2 + 2*3 +1*(-3)+(-1)*9\\) \u200b
\\(\\frac{dL}{dx_{2,3}}=(\\frac{dy}{dx_{2,3}})\u00b7(\\frac{dL}{dy})=(w3,;)\u00b7(\\frac{dL}{dy_2,;})=3*(-8)+2*1+1*4+(-2)*6\\)\u200b
"},{"location":"DL_CV/neural/lec/#summary","title":"Summary","text":""},{"location":"DL_CV/neural/lec/#higher-order-derivatives","title":"Higher-Order Derivatives","text":""},{"location":"GP/AMAS/AMAS/","title":"Angular Momentum and Spin","text":"
?(12-17\u8bfe\u7a0b\u7ed3\u675f)
The wave function of a particular quantum state of the hydrogen atom can be labeled by a set of quantum numbers \\((n, l, m_l)\\).
The corresponding energy only depends on the principal quantum number \\(n = 1,2,3,....\\)
The orbital quantum number \\(l = 0, 1, 2, . . . , n \u2212 1\\) is a measure of the magnitude of the angular momentum of the quantum state. States with \\(l = 0, 1, 2, 3\\) are called \\(s, p, d, f\\) .
The orbital magnetic quantum number
\\(ml =\u2212l,\u2212l+1,...,l\u22121,l\\) is related to the space orientation of this angular momentum vector.
"},{"location":"GP/AMAS/AMAS/#classical-loop-model-for-electron-orbits","title":"Classical Loop Model for Electron Orbits","text":"Quantized by \\(\\bar{h}\\)
We can measure definite values of a component \\(L_z\\) along a chosen measurement axis(usally taken to be a z axis) as given by \\(L_z=m_l\\bar{h}\\) for \\(m_l=0,\u00b11,\u00b12,\u2026\u2026\u00b1l\\)
In general , if the electron has a definite value of \\(L_z\\) it may not have definite values of \\(L_x\\) and \\(L_y\\)
This is a manifestation of Heisenberg's uncertainty principle.
Therefore, the orbital magnetic dipole moment is also quantized
\\(\\mu_{orb}=|\\gamma|L=\\frac{e}{2m}\\sqrt{l(l+1)}\\bar{h}\\)
\\(\\mu_{orb},z=\\gamma L_z=-m_l\\frac{e\\bar{h}}{2m}=-m_l\\mu_B\\)
\u200b Define the Bohr magneton \\(\\mu_B=\\frac{eh}{4\\pi m}=\\frac{e\\bar{h}}{2m}=9.274*10^{-24}J/T\\)
"},{"location":"GP/AMAS/AMAS/#dynamics-in-a-uniform-magnetic-field","title":"Dynamics in a Uniform Magnetic Field","text":"An external magnetic field B\u20d7 will rotate a magnetic dipole moment \u03bc\u20d7 with a total torque \\(\\vec{\\tau}=\\vec{\\mu}\\times\\vec{B}\\) , or, for an electron with orbital angular momentum \\(\\vec{L}\\) , \\(\\vec{\\tau}=\\frac{d\\vec{L}}{dt}=\\gamma\\vec{L}\\times\\vec{B}\\)
"},{"location":"GP/AMAS/AMAS/#dipole-in-a-nonuniform-magnetic-field","title":"Dipole in a Nonuniform Magnetic Field","text":"\u300cdiphole parallel to the magnetic field.\u300d
"},{"location":"GP/AMAS/AMAS/#the-stern-gerlach-experiment","title":"The Stern-Gerlach Experiment","text":"In the Stern-Gerlach experiment, a narrow beam of silver atoms passes through an electromagnet (with a nonuniform magnetic field) and then lands on a glass detector plate.
An atom in a state with angular momentum equal to one \\((L = 1)\\) would have a magnetic moment with two components relative to the direction of the magnetic field.
In this case the spot on the receiving plate will therefore be split into two, each of them having the same size but half the intensity of the original spot.
The later, or new, quantum theory developed by Heisenberg, Schroedinger, and others, predicted that for an \\(L = 1\\) state the beam should split into three components.
In fact, we now know that a silver atom consists of many electrons ([Kr]4d105s1).
We also know that all those magnetic moments vectorially cancel out except for a single electron \\((5s1)\\), and the orbital dipole moment of that electron is zero.
Therefore, the Stern-Gerlach result posed a serious problem for the new quantum theory.
This was solved when Uhlenbeck and Goudsmit (1925, 1926) proposed that the electron had an intrinsic angular momentum, not associated with its orbital motion.
"},{"location":"GP/AMAS/AMAS/#spin","title":"Spin","text":"The Stern-Gerlach experiment belongs to a class of quantum phenomena involving a quantum degree of freedom called spin, which has no classical counterpart.
Fortunately, one can describe spin and its dynamics without appealing to any mechanical model (such as that of a spinning top), starting with just the observed fact that it is a form of angular momentum.
"},{"location":"GP/AMAS/AMAS/#electron-spin","title":"Electron Spin","text":"Every electron, whether trapped in an atom or free, has a spin angular momentum and a spin magnetic dipole moment that are as intrinsic as its mass and charge. The existence of electron spin was postulated on experimental evidence by George Uhlenbeck and Samuel Goudsmit from their studies of atomic spectra.
For every electron, \\(spin s = 1/2\\) and the electron is said to be a \\(spin-1/2\\) particle. (Protons and neutrons are also \\(spin-1/2\\) particles.)
As with the angular momentum associated with motion, spin angular momentum can have a definite magnitude but does not have a definite direction.
The best we can do is to measure its component along the \\(z\\) axis (or along any axis), and that component can have only the definite values given by \\(S_z =m_s\\bar{h}, for\\ m_s =\u00b1s=\u00b11/2\\).
Here \\(m_s\\) is the spin magnetic quantum number, which can have only two values: \\(m_s = +s = +1/2\\) (the electron is said to be spin up) and \\(m_s = \u2212s = \u22121/2\\) (the electron is said to be spin down).
Proton NMR, which studies the precession of the proton spin in the magnetic field, is a practical medical imaging technique.
Since the Larmor frequency depends on the applied magnetic field, placing a magnetic field gradient across the human body allows you to locate the source of the MRI signal (hence the name Magnetic Resonance Imaging).
"},{"location":"GP/AMAS/AMAS/#spin-orbit-coupling","title":"Spin-Orbit Coupling","text":"Orbital motion cause relative motion of the nucleus to the electron ,creating a magnetic filed(connected with spin)
The spin\u2013orbit interaction Hamiltonian is given by \\(H_{SO}=\\epsilon_{nl}\\vec{L}\\vec{S}\\) where the spin-orbit coupling constant \\(\u03be_{nl}\\), which is positive, is essentially the averaged gradient of the Coulomb interaction.
To solve the Hamiltonian with such an interaction, we need to introduce the total angular momentum \\(\\vec{J}=\\vec{L}+\\vec{S}\\) and and use the expression \\(\\vec{J}\u00b7\\vec{J}=(\\vec{L}+\\vec{S})\u00b7(\\vec{L}+\\vec{S})=\\vec{S}\u00b7\\vec{S}+2\\vec{L}\u00b7\\vec{S}+\\vec{L}\u00b7\\vec{L}\\)
\\(S^2 =s(s+1)\\bar{h} \\\\ L^2 =l(l+1)\\bar{h}^2\\\\J^2=j(j+1)\\bar{h}^2\\)
\\(\\vec{L}\\vec{S}=\\frac{\\bar{h}^2}{2}[j(j+1)-l(l+1)-s(s+1)]\\)
For multiple electrons in the same trap, we must consider the Pauli exclusion principle, named after \\(Wolfgang\\ Pauli\\).
The Pauli principle states that no two electrons confined to the same trap can have the same set of values for their quantum numbers.In other words, there can be two electrons at most at any energy level; they have opposite spins.
This principle applies not only to electrons but also to protons and neutrons, all of which have \\(s = 1/2\\); they are known as fermions.
A remark on fermions and bosons
More generally, fermions are particles with half-integer spin s \\((i.e., s = 1/2, 3/2, etc.)\\). On the other hand, particles with integer s \\((i.e., s = 0, 1, 2, etc.)\\) are called bosons.
Fermions follow the Pauli principle, but bosons do not.
Many particles consisting of matter, such as electrons, protons, and neutrons (s = \u00bd for either of them) are fermions. On the other hand, photons(s = 1) are bosons.
Regarding composite particles consisting of multiple fermions (such as atoms), those with odd number of fermions can behave as fermions while those with even number of fermions as bosons. (\\(^3He\\): \\(2p + 1n + 2e \u2192\\) fermion; \\(^4He: 2p + 2n + 2e\\) \u2192 boson)
"},{"location":"GP/AMAS/AMAS/#the-helium-atom","title":"The Helium Atom","text":"The second simplest atom is the heliuxim atom, which has two 1s electrons (spin up and spin down) in the Coulomb potential of the charge 2e nucleus that obey time-independent Schroedinger\u2019s equation of the form \\((H_1+H_2+V_{12})\u03a8(\\vec{r_1},\\vec{r_2})=E\u03a8(\\vec{r_1},\\vec{r_2})\\) where \\(H_i=-\\frac{\\bar{h^2}}{2m}\\triangledown_i^2\uff08Kinetic\uff09-\\frac{2e^2}{4\\pi\\epsilon_0r_i}\uff08Potential\uff09\\ and\\ V_{12}=\\frac{e^2}{4\\pi\\epsilon_0|\\vec{r_1}-\\vec{r_2}|}\\)
An approximation starts from throwing away the interaction term, which we have not learned to deal with, i.e.,\\((H_1 + H_2) \u03a8_0 = E_0\u03a8_0\\),
The ground state of either \\(H_1\\) or \\(H_2\\) has an energy \\(\u22124E_R\\) (Because of two protons so raidus gets smaller)
and a wave function \\(\u03c8_{100}(\\vec{r})=\\frac{1}{\\pi}(\\frac{2}{a_B})^{3/2}e^{-2r/a_B}\\)
One may verify that \\(\u03a8_0(\\vec{r}_1,\\vec{r}_2)=\u03c8_{100}(\\vec{r_1})\u03c8_{100}(\\vec{r_2})\\) is an exact solution.
The corresponding energy is \\(E_0=-8E_R\\approx -109eV\\)
Others pleae refer to the related PPT
"},{"location":"GP/AMAS/AMAS/#shells-and-subshells","title":"Shells and Subshells","text":""},{"location":"GP/HA/HA/","title":"The Hydrogen Atom","text":""},{"location":"GP/HA/HA/#the-hydrogen-atom","title":"The Hydrogen Atom","text":"
https://www.youtube.com/watch?v=-Y0XL-K0jy0
https://youtube.com/watch?v=acN7E7AUHPk
"},{"location":"GP/HA/HA/#quiz","title":"Quiz","text":"https://www.youtube.com/watch?v=9GOuZAh9Wg0
"},{"location":"GP/HA/HA/#mystery-of-the-hydrogen-atom","title":"Mystery of the Hydrogen Atom","text":"Because the proton\u2019s mass is much greater than the electron\u2019s mass, we shall assume that the proton is fixed in place. So, the atom is a fixed potential trap with the electron moving around inside it.
A hydrogen atom contains an electron that is trapped by the Coulomb force it experiences from the proton, which is the nucleus of the atom.
Under Newtonian laws, the electron would move around the proton, like planets around the Sun, i.e. \\(\\frac{1}{4\\pi\\epsilon_0}\\frac{e^2}{r^2}=m\\frac{v^2}{r}\\)
Multiplying by \u2212r, we obtain \\(E_c=-\\frac{e^2}{4\\pi \\epsilon _0 r}=-mv^2=-2E_k\\)
Alternatively, the total energy of the electron is\\(E=E_k+E_c=\\frac{E_c}{2}=-E_k\\)
However, any charged particle which moves in a curved path will emit electromagnetic radiation, hence losing energy continuously. Why doesn\u2019t the electrical attraction between the electron and the positive charge simply cause the two to collapse together?
Bohr made two bold (and completely unjustified) assumptions:
However, as successful as his theory was on the four visible wavelengths and on why the atom did not simply collapse, it turned out to be quite wrong in almost every other aspect of the atom.
"},{"location":"GP/HA/HA/#physical-meaning-of-the-assumption","title":"Physical meaning of the assumption","text":"The de Broglie wavelength \\(\u03bb\\) of a free particle with momentum p is \\(\u03bb = h/p = h/mv\\).For an electron in a hydrogen atom whose orbital radius \\(r\\), the above equation leads to \\(\u03bb/r = h/rmv = h/L\\). Therefore, we obtain \\(2\u03c0r/\u03bb = L/\\bar{h}\\).
\\(L=n\\bar{h}\\) means that the length of the orbit is an integer multiple of \\(\u03bb\\). Namely, the phase of the electron wave function returns to the initial value by moving for one cycle of the orbit.
"},{"location":"GP/HA/HA/#analysis-based-on-bohrs-model","title":"Analysis based on Bohr\u2019s model","text":"In the Bohr model of the hydrogen atom, the electron\u2019s orbital radius r is quantized and the smallest possible orbital radius (for \\(n = 1\\)) is \\(a_B\\), which is called the Bohr radius.
Can you obtain the length scale, alternatively, from dimension analysis?sIn order that the attraction between electron and nucleus does not simply collapse them together in the Bohr model, the electron should not get any closer to the nucleus than orbital radius aB by taking
The energy of the hydrogen atom, according to the Bohr model, is then \\(E_n=\\frac{1}{2}mv^2-\\frac{1}{4\\pi\\epsilon_0}\\frac{e^2}{r}=-\\frac{E_R}{n^2}\\) where \\(E_R=\\frac{me^4/(4\\pi\\epsilon_0)^2}{2\\bar{h}^2}=13.6eV\\)
The energy of a hydrogen atom (or, equivalently, of its electron) changes when the atom emits or absorbs light. Emission and absorption involve a quantum of light according to \\(\\bar{h}\u03c9_{nm} =E_R(\\frac{1}{n^2} \u2212\\frac{1}{m^2})\\) for integers \\(m > n\\).
The wavelengths of the emitted or absorbed light are given by $ \\frac{1}{\\lambda}=\\frac{E_R}{hc}(\\frac{1}{n^2}-\\frac{1}{m^2})$
The ground-state energy is the lowest energy allowed by Heisenberg\u2019s uncertainty principle.
To find the minimal energy, we solve for \\(\\triangle r\\) \\(\\Rightarrow\\frac{dE}{d(\\triangle r)}=0 \\Rightarrow \\triangle r = \\frac{\\bar{h}^2}{me^2/(4\\pi\\epsilon_0)}=a_B\\) and \\(E=-\\frac{me^4/(4\\pi\\epsilon_0)^2}{2\\bar{h}^2}=-E_R\\)
The energy of the ground state (or any stationary state) is uniquely determined. \u300cThough the uncertainty of \\(t\\ \\&\\ x\\)\u300d
This is because of the energy-time uncertainty principle, \\(\u2206t \u00b7 \u2206E \u2265 \\bar{h}/2\\).
Note, however, both kinetic energy and potential energy have uncertainties, due to the uncertainties of position and momentum.
Hydrogen is a three-dimensional, finite electron trap, with walls that vary in depth with distance.
"},{"location":"GP/HA/HA/#schroedingers-equation-for-the-h-atom","title":"Schroedinger's Equation for the H-atom","text":""},{"location":"GP/HA/HA/#derive","title":"Derive","text":""},{"location":"GP/HA/HA/#ground-state-wave-function","title":"Ground State Wave Function","text":"\\(\u03c8_{100}(\\vec{r})=R_{10}(r)=\\frac{1}{\\sqrt{\\pi}a_B^{3/2}}e^{\\frac{r}{a_B}}\\)
Note that the hydrogen atom in its ground state has zero angular momentum (\\(l = 0\\) more details will be discussed in the next lecture), which is not predicted in the Bohr model.)
The probability that the electron can be detected in any given (infinitesimal) volume element \\(dV\\) located at radius \\(r\\) from the center of the atom is \\(|\u03c8_{100}(\\vec{r})|^2dV.\\)
"},{"location":"GP/Polar/Polarization/#preknowledge","title":"PreKnowledge","text":"
To observe interference of the two waves
In this lecture we consider the direction of \\(\\vec{E}\\)
Light is a transverse electromagnetic wave. Thus far we have considered only light for which the orientation of the electric field is constant, although its magnitude and sign vary in time.
In general, we can consider two such harmonic lightwaves of the same frequency, moving through the same region of space, in the same direction \\(\\hat{z}\\)
\\(\\vec{E}_x(z,t)=\\hat{i}E_{0x}cos(kz\u2212\u03c9t)\\)
\\(\\vec{E}_y(z,t)=\\hat{j}E_{0y}cos(kz\u2212\u03c9t+\\epsilon)\\)
"},{"location":"GP/Polar/Polarization/#polarization-and-its-mathematical-description","title":"Polarization and Its Mathematical Description","text":""},{"location":"GP/Polar/Polarization/#linear-polarization","title":"Linear polarization","text":"If \\(\u03b5\\) is zero or an integral multiple of \\(\u00b12\u03c0\\), the resultant wave is \\(\\vec{E}=(\\hat{i}E_{0x}+\\hat{j}E_{0y})cos(kz-wt)\\)
"},{"location":"GP/Polar/Polarization/#circular-polarization","title":"Circular polarization","text":"When both constituent waves have equal amplitudes and \\(\\epsilon=-\\pi/2+2m\\pi\\)(m is an integer) the resultant wave is \\(\\vec{E}=E_0(\\hat{i}cos(kz-wt)+\\hat{j}sin(kz-wt))\\)
When both constituent waves have equal amplitudes and \\(\\epsilon=\\pi/2+2m\\pi\\)(m is an integer) the resultant wave is \\(\\vec{E}=E_0(\\hat{i}cos(kz-wt)-\\hat{j}sin(kz-wt))\\)
The amplitude is unaffected, but \\(\\vec{E}\\) at a fixed z now rotates counterclockwise, and the wave is left-circularly polarized.
A linearly polarized wave can be synthesized from two oppositely polarized circular waves of equal amplitude.
"},{"location":"GP/Polar/Polarization/#a-math-description-of-polarization","title":"A Math Description of Polarization","text":"\\(|H\u27e9=\\begin{pmatrix}1\\\\0\\end{pmatrix}\\\\|V\u27e9= \\begin{pmatrix}0\\\\1\\end{pmatrix}\\)
\\(|D\u27e9=\\frac{1}{\\sqrt{2}}(|H\u27e9+|V\u27e9)=\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\1\\end{pmatrix}\\)
\\(|A\u27e9=\\frac{1}{\\sqrt{2}}(|H\u27e9\u2212|V\u27e9)=\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\-1\\end{pmatrix}\\)
Note that we are only interested in polarization, so the vectors are normalized, or in one unit length
\\(|R\u27e9=\\frac{1}{\\sqrt{2}}(|H\u27e9\u2212i|V\u27e9)=\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\-i\\end{pmatrix}\\)
\\(|L\u27e9=\\frac{1}{\\sqrt{2}}(|H\u27e9+i|V\u27e9)=\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\i\\end{pmatrix}\\)
"},{"location":"GP/Polar/Polarization/#orthogonal","title":"Orthogonal","text":"Note that in the lecture on interference, we discussed the addition in a two-dimensional real space \\(R_2\\). The space is also equivalent to a one-dimensional complex space \\(C_1\\), or a two-component real vector space.
Now, with polarization, we have generalized the one-dimensional complex space \\(C_1\\) to a two-dimensional complex space \\(C2 = C1 \u2297 C1\\), or a two-component complex vector space (in Jones\u2019 vector representation). The additional \\(C_1\\) space is spanned by the two orthogonal, linearly polarized states \\(|H\u27e9\\) and \\(|V\u27e9\\).
similarly, two complex vectors A\u20d7 and B\u20d7 are said to be orthogonal when \\(\u27e8A|B\u27e9 \u2261 \\vec{A}^{*} \u00b7 \\vec{B} = 0\\).
Notice that\\(\u27e8H|V\u27e9 = \u27e8D|A\u27e9 = \u27e8L|R\u27e9 = 0\\).
As we have seen, any polarization state can be described by a linear combination of the vectors in either one of the orthogonal sets.
These same ideas are of considerable importance in quantum mechanics, where one deals with orthonormal wave functions.
"},{"location":"GP/Polar/Polarization/#monochromatic-light-and-natural-light","title":"Monochromatic Light and Natural Light","text":""},{"location":"GP/Polar/Polarization/#light-trains-and-monochromatic-light","title":"Light Trains and Monochromatic Light","text":"In reality, a non-laser source emits, to the best, quasimonochromatic light trains, whose frequency can be represented by a bell-shaped Gaussian function.
\"\u51c6\u5355\u8272\"\uff08quasi-monochromatic\uff09\u662f\u6307\u5149\u6ce2\u5728\u9891\u7387\u4e0a\u7684\u5206\u5e03\u76f8\u5bf9\u72ed\u7a84\uff0c\u4f46\u5e76\u975e\u5b8c\u5168\u5355\u4e00\u9891\u7387\u7684\u5149\u3002\u51c6\u5355\u8272\u5149\u901a\u5e38\u5177\u6709\u4e00\u4e2a\u4e3b\u5bfc\u7684\u4e2d\u5fc3\u9891\u7387\uff0c\u800c\u5728\u8be5\u4e2d\u5fc3\u9891\u7387\u5468\u56f4\u6709\u4e00\u5b9a\u7684\u9891\u7387\u5bbd\u5ea6\u3002\u8fd9\u79cd\u5149\u7684\u9891\u8c31\u5f62\u72b6\u53ef\u80fd\u7c7b\u4f3c\u4e8e\u9ad8\u65af\u5206\u5e03\u6216\u5176\u4ed6\u8fd1\u4f3c\u5355\u5cf0\u7684\u5206\u5e03\u3002
That is, the irradiance (hence its square root, the amplitude) versus frequency is found to be Gaussian with a width \\(\u2206\u03c9 = 2\u03c0\u2206\u03bd.\\)
Quasimonochromatic light resembles a series of randomly phased finite wave trains.Such a disturbance is nearly sinusoidal,although the frequence dose vary slowly about some mean value.
Moreover,the amplitude fluctuates as well,but this too is a comparatively slow variation.
The average constittuent wavetrain exists roughly for the coherence time \\(\\triangle t_c=\\frac{1}{\\triangle v}\\)
\u4e24\u5217\u6ce2\u80fd\u53d1\u751f\u5e72\u6d89\u7684\u6700\u5927\u5149\u7a0b\u5dee\u53eb\u76f8\u5e72\u957f\u5ea6\\(\\delta_M=\\frac{\\lambda^2}{\\triangle\\lambda}\\) \\(\\lambda\\)\u4e3a\u4e2d\u5fc3\u6ce2\u957f
\u5149\u901a\u8fc7\u76f8\u5e72\u957f\u5ea6\u6240\u9700\u65f6\u95f4\u53eb\u76f8\u5e72\u65f6\u95f4
\u76f8\u5e72\u65f6\u95f4\u7684\u5b9a\u4e49\u662f\u57fa\u4e8e\u5149\u7684\u6ce2\u52a8\u6027\u548c\u5e72\u6d89\u6027\u7684\u3002\u5f53\u4e24\u675f\u5149\u540c\u65f6\u7167\u5c04\u5728\u540c\u4e00\u70b9\uff0c\u5982\u679c\u5b83\u4eec\u7684\u76f8\u4f4d\u5173\u7cfb\u4fdd\u6301\u4e0d\u53d8\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u4f1a\u53d1\u751f\u5e72\u6d89\u3002\u76f8\u5e72\u65f6\u95f4\u5c31\u662f\u63cf\u8ff0\u8fd9\u79cd\u76f8\u4f4d\u5173\u7cfb\u4fdd\u6301\u4e0d\u53d8\u7684\u65f6\u95f4\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5982\u679c\u5149\u7684\u7535\u78c1\u573a\u5728\u4e00\u4e2a\u76f8\u5e72\u65f6\u95f4\u5185\u7684\u5e73\u5747\u503c\u4e0d\u53d8\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u8bf4\u8fd9\u675f\u5149\u5728\u8fd9\u4e2a\u65f6\u95f4\u5185\u662f\u76f8\u5e72\u7684\u3002\u76f8\u5e72\u65f6\u95f4\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\\(T_c=1/\u0394\u03bd\\)\uff0c\u5176\u4e2d\u0394\u03bd\u4e3a\u5149\u7684\u9891\u7387\u5bbd\u5ea6
\u5149\u7684\u5355\u8272\u6027\u597d\uff0c\u76f8\u5e72\u957f\u5ea6\u548c\u76f8\u5e72\u65f6\u95f4\u5c31\u957f\uff0c\u65f6\u95f4\u76f8\u5e72\u6027\u4e5f\u5c31\u597d
An idealized monochromatic plane wave must be depicted as an infinite wavetrain. If this disturbance is resolved into two orthogonal components perpendicular to the direction of propagation, they, in turn, must have the same frequency, be infinite in extent, and therefore be mutually coherent (i.e., \\(\u03b5 = constant\\)).
\\(\\vec{E}_x(z,t)=\\hat{i}E_{0x}cos(kz\u2212\u03c9t)\\)
\\(\\vec{E}_y(z,t)=\\hat{j}E_{0y}cos(kz\u2212\u03c9t+\\epsilon)\\)
A perfectly monochromatic plane wave is always polarized.
The most spectacular of all present-day sources is the laser. Under optimum conditions, with temperature variations and vibrations meticulously suppressed, a laser was actually operated at quite close to its theoretical limit of frequency constancy.
For example, a short-term frequency stability of about 8 parts per \\(10^{14}\\) was attained with a He\u2013Ne continuous gas laser at \\(\u03bb0 = 1153 nm\\) [Jaseja et al., Phys. Rev. Lett. 10, 165 (1963)].
That corresponds to a remarkably narrow bandwidth of about \\(\u2206\u03bd = 20 Hz\\).
"},{"location":"GP/Polar/Polarization/#natural-light","title":"Natural Light","text":"Natural light is composed of a rapidly varying succession (~\\(10^{\u22128}\\) s) of the different polarization states. It is also known as unpolarized or randomly polarized light.
We can mathematically represent natural light in terms of two arbitrary, incoherent, orthogonal, linearly polarized waves of equal amplitude (i.e., waves for which the relative phase difference varies rapidly and randomly).
"},{"location":"GP/Polar/Polarization/#coherence","title":"Coherence","text":"Coherence is a measure of the correlation between the phases measured at different (temporal and spatial) points on a wave.
Temporal coherence is a measure of the correlation of light wave\u2019s phase at different points along the direction of propagation \u2013 it tells us how monochromatic a source is. (Think about the description of quasimonochromatic light.)
Spatial coherence is a measure of the correlation of light wave\u2019s phase at different points transverse to the direction of propagation \u2013 it tells us how uniform the phase of the wavefront is. (Think about Young\u2019s interference experiment.)
The figure illustrates how to prepare a monochromatic wave that is both temporally and spatially coherent from incoherent natural light.
In reality, light is generally neither completely polarized nor completely unpolarized.
More often, the electric-field vector varies in a way that is neither totally regular nor totally irregular, and such an optical disturbance is partially polarized.
One useful way of describing this behavior is to envision it as the result of the superposition of specific amounts of natural and polarized light.
"},{"location":"GP/Polar/Polarization/#polarizing-sheets","title":"Polarizing Sheets","text":"Unpolarized visible light can be transformed into polarized light by sending it through a polarizing sheet,or a Polaroid sheet.
Electric field oscillations of unpolarized light can resolve into two components with equal intensity
For polarized light,obly the component \\(E_y\\)=\\(Ecos\\theta\\) parallel to the polarizing direction of the sheet can be transmitted.Therefore,the intensity of the emerging wave is \\(I_0=I_0cos^2\\theta\\)
One of the most common sources of polarized light is the ubiquitous process of reflection from dielectric media.
Consider a ray of unpolarized light incident on a glass surface. The field \\(\\vec{E}\\) of the incident light can be decomposed into two components of equal magnitude, one perpendicular and another parallel to the plane of incidence.
In general, the reflected light is partially polarized.
When the light is incident at a particular incident angle, called the Brewster angle \\(\u03b8_B\\) , the reflected light is fully polarized.
One finds experimentally that at the incident angle \\(\u03b8_B\\), the reflected and refracted rays are perpendicular to each other: \\(\\theta_B+\\theta_r=\\frac{2}{\\pi}\\)
According to Snell\u2019s law \\(n_i sin\u03b8_B = n_r sin\u03b8_r\\)
we have \\(n_i sin\u03b8_B =n_r sin\u03b8_r =n_r sin(\\frac{\\pi}{2}\u2212\u03b8_B)=n_r cos\u03b8_B\\) or \\(\\theta_B=tan^{-1}\\frac{n_r}{n_i}\\)
If the incident and reflected rays travel in air, we can approximate \\(n_i\\) as unity, so \\(n_r\\)=\\(tan\\theta_B\\)
"},{"location":"GP/QW/QW/","title":"Quantum Wells","text":""},{"location":"GP/QW/QW/#quantum-wells","title":"Quantum Wells","text":"
On a stretched string, we can set up both traveling waves and standing waves.
In other words, confining the wave to a finite region of space leads to quantization of the motion \u2014 to the existence of discrete states for the wave, each state with a sharply defined frequency.
This observation applies to waves of all kinds, including matter waves. For matter waves, however, it is more convenient to deal with the energy E of the associated particle than with the frequency f of the wave.
Consider the matter wave associated with an electron moving in the positive x direction and subject to no net force \u2014 a so-called free particle. The energy of such an electron can have any reasonable value, just as a wave traveling along a stretched string of infinite length can have any reasonable frequency.
Consider next the matter wave associated with an atomic electron, perhaps the valence (least tightly bound) electron.
The electron \u2014 held within the atom by the attractive Coulomb force between it and the positively charged nucleus \u2014 is a bound particle. It can exist only in a set of discrete states, each having a discrete energy E. This sounds much like the discrete states and quantized frequencies that apply to a stretched string of finite length.
For matter waves, then, as for all other kinds of waves, we may state a confinement principle: Confinement of a wave leads to quantization \u2014 that is, to the existence of discrete states with discrete energies.
"},{"location":"GP/QW/QW/#one-dimensional-infinite-potential-well","title":"One-Dimensional Infinite Potential Well","text":"Consider a nonrelativistic electron confined to a one-dimensional electron trap (or a limited region of space).
"},{"location":"GP/QW/QW/#standing-waves-in-a-1d-trap","title":"Standing Waves in a 1D Trap","text":"We examine by analogy with standing waves on a string of finite length, stretched along an x axis and confined between rigid supports.
Because the supports are rigid, the two ends of the string are nodes, or points at which the string is always at rest. The states, or discrete standing wave patterns in which the string can oscillate, are those for which the length L of the string is equal to an integer number of half-wavelengths
That is, the string can occupy only states for which \\(L=\\frac{n\\lambda}{2}\\)
Each value of the integer n identifies a state of the oscillating string. For a given n, the transverse displacement of the string at any position x \\((0 \u2264 x \u2264 L)\\) along the string is given by \\(y_n(x)=Asin(\\frac{n\\pi}{L}x)\\)where A is the amplitude of the standing wave
For the n state \\(\\lambda_n=\\frac{2L}{n}\\) Thus \\(sinkx=sin\\frac{2\\pi}{\\lambda_n}x=sin(\\frac{n\\pi}{L}x)\\)
For the electron in the trap, we promote the transverse displacement to wave function \\(\u03c8_n(x)\\).
"},{"location":"GP/QW/QW/#probability-of-detection","title":"Probability of Detection","text":"Classically, we expect to detect the electron anywhere in the infinite well with a constant probability density.
Quantum mechanically, we find the probability density \\(p_n(x) = |\u03c8_n(x)|^2 = |A|^2 sin^2(\\frac{n\\pi}{L}x)\\) for given n
If don't satisfy \\(L=\\frac{n\\lambda}{2}\\) no stable wave formation!
The constant A (up to a phase) can be determined by the normalization condition
\\(\\int_{-\u221e}^{+\u221e}|\u03c8_n(x)|^2dx =\\int_0^L|\u03c8_n(x)|^2dx = 1\\), so \\(A=\\sqrt{2/L}\\)
"},{"location":"GP/QW/QW/#energies-of-the-trapped-electron","title":"Energies of the Trapped Electron","text":"The de Broglie wavelength \u03bb of the electron is defined as \\(\u03bb=\\frac{h}{p}=\\frac{h}{\\sqrt{2mk}}\\),where \\(K=\\frac{p^2}{2m}\\)is the kinetic enerrgy of the nonrelativistic electron
For an electron moving within the central cylinder, where U = 0, the total (mechanical) energy E is equal to the kinetic energy K.
Narrower well \\((smaller\\ L)\\) \\(\u21d2 E_n \u2197\\).
The positive integer n is the quantum number of the electron's quantum state in the trap
The quantum state with the lowest possible energy level \\(E_1\\) with quantum number n = 1 is called the ground state of the electron.
Why is \\(n = 0\\) not allowed?
Choosing n = 0 would indeed yield a lower energy of zero. However, as we will see below, the corresponding probability density is \\(|\u03c8|^2 = 0\\), which we can interpret only to mean that there is no electron in the well
so \\(n = 0\\) is not a possible quantum number.
It is an important conclusion of quantum physics that confined systems must always have a certain non-zero minimum energy called the zero-point energy.
Electrons can be excited or de-excided bynthe absorption or emission of a photon with energy \\(\\bar{h}w=\\frac{hc}{\\lambda}=\\triangle E=E_{High}-E_{Low}\\)
If we solve time-independent Schroedinger\u2019s equation, as in the previous lecture, for an electron trapped in the 1D infinite well of width L, we could write the solutions as \\(\u03c8_n(x)=exp (i\\frac{n\\pi}{L}x)\\)or\\(\u03c8_n(x)=exp(-i\\frac{n\\pi}{L}x)\\).
The appropriate solutions can only be certain linear combinations of the traveling wave functions, given by \\(\u03c8_n(x)=Asin(\\frac{n\\pi}{L}x)\\) The constant A is to be determined.
For sufficiently large \\(n\\), the probability of detection becomes more and more uniform across the well in the coarse-grained scale. This result is an instance of a general principle called the correspondence principle: At large enough quantum numbers, the predictions of quantum physics merge smoothly with those of classical physics.
"},{"location":"GP/QW/QW/#an-electron-in-a-finite-well","title":"An Electron in a Finite Well","text":"We can picture an electron trapped in a one-dimensional well between infinite-potential walls as being a standing matter wave. The solutions must be zero at the infinite walls.
For finite walls, however, the analogy between waves on a stretched string and matter waves fails. Matter wave nodes no longer exist at \\(x = 0\\) and at \\(x = L\\).
wave function can penetrate the walls into classically forbidden regions.
Constraints
(1)\\(\u03a8(-\\infty)\\rightarrow0\\)
(2)\\(\u03a8(+\\infty)\\rightarrow0\\)
(3)\\(\u03a8(0-)=\u03a8(0+)\\)
(4)\\(\u03a8(L-0)=\u03a8(L+0)\\)
(5)\\(\u03a8'(-0) = \u03a8'(+0)\\)
(6)\\(\u03a8'(L-0) = \u03a8'(L+0)\\)
"},{"location":"GP/QW/QW/#energies-of-the-trapped-electron_1","title":"Energies of the Trapped Electron","text":"Thus, the corresponding energy E \u2248 \\((h/\u03bb)^2/(2m)\\) for an electron in any given state is less in the finite well than in the infinite well.\uff08From what is mentioned above\uff09
An electrons with an energy greater than the well depth\\((E > U_0)\\) has too much energy to be trapped in the finite well.
Thus, there is a continuum of energies beyond the top of potential well ; a high-energy electron is not confined,and its energy is not quantized
"},{"location":"GP/QW/QW/#semiconductor-quantum-wells","title":"Semiconductor Quantum Wells","text":"Semiconductor similar to finite wells
"},{"location":"GP/QW/QW/#schroedingers-equation-in-high-dimensions","title":"Schroedinger\u2019s Equation in High Dimensions","text":"This has the form \\(E = F(x) + G(y)\\), which can only be satisfied when \\(F(x) = E_1\\) and \\(G(y) = E \u2212 E_1\\), i.e., each function must separately be a constant.
As a consequence, separation of variables breaks the multivariate partial differential equation into a set of independent ordinary differential equations \\((ODEs)\\).
We can solve the \\(ODEs\\) for \\(X(x)\\) and \\(Y(y)\\). The wave function for the original equation is simply their product \\(X(x)Y(y)\\).
[In which case \\(\u03a8(x,y)\\) can be written in the form of \\(\u03a8(x,y) = X(x)Y(y\\)), and in which case cannot be?]
It is especially useful in solving equations arising in mathematical physics, such as Laplace\u2019s equation, Helmholtz\u2019s equation, and Schroedinger\u2019s equation.
\\(U (x , y ) = Ux (x ) + Uy (y )\\),or, in a central potential in spherical coordinates,\\(U(r,\u03b8,\u03c6) = V(r).\\)
"},{"location":"GP/QW/QW/#2d-3d-infinite-potential-wells","title":"2D & 3D Infinite Potential Wells","text":""},{"location":"GP/QW/QW/#dirac-delta-function-potential-well","title":"Dirac Delta Function Potential Well","text":""},{"location":"GP/com/com/","title":"Electromagnetic Integration","text":""},{"location":"GP/com/com/#electromagnetic-integration","title":"Electromagnetic Integration","text":""},{"location":"GP/com/com/#faradays-law-of-induction","title":"Faraday\u2019s Law of Induction","text":"
\\(\\Phi_B=\\int\\vec{B}\\cdot d\\vec{A}\\)
\\(\\epsilon = -N\\frac{d\\Phi_B}{dt}\\)
When you move the magnet toward or away from the loop, a magnetic force resists the motion, so Lenz\u2019s law requires your applied force to do positive work.
At the same time, thermal energy is produced in the material of the loop because of the material\u2019s electrical resistance to the induced current.
The energy you transfer to the closed loop-magnet system via your applied force ends up in this thermal energy. (For now, we neglect energy that is radiated away from the loop as electromagnetic waves during the induction.)
\\(\\epsilon= N\\frac{d\\Phi_B}{dt}=BLv\\)
\\(F=F_1=iLB=B^2L^2v/R\\)
\\(P_1=Fv=B^2L^2v^2/R\\)
\\(P_{thermal}=i^2R=(\\frac{BLv}{R})^2R=B^2L^2v^2/R\\)
"},{"location":"GP/com/com/#a-reformulation-of-faradays-law","title":"A Reformulation of Faraday\u2019s Law","text":"We find that an induced emf can be defined without the need of a current or particle: An induced emf is the sum\u2014via integration\u2014of quantities \\(\\vec{E} \u00b7 d\\vec{s}\\) around a closed path, where \\(\\vec{E}\\) is the electric field induced by a changing magnetic flux and \\(d\\vec{s}\\) is a differential length vector along the path.
Rewrite Faraday's Law as \\(\\oint \\vec{E}\\cdot d\\vec{s}=-N\\frac{d\\Phi_B}{dt}=-\\frac{d}{dt}\\int\\vec{B}\\cdot d\\vec{A}\\)
We can convert it to differential form by applying the Stokes\u2019 theorem (or the fundamental theorem for curls)
\\(\\oint\\vec{E}\\cdot d\\vec{s}=\\int_S(\\triangledown \\times \\vec{E})\\cdot d\\vec{A}\\)
Thus, we get : \\(\\triangledown \\times\\vec{E}=-\\frac{\\partial \\vec{B}}{\\partial t}\\)
Electric potential has no meaning for electric fields that are produced by induction
\\(B=\\mu_0in\\)
\\(\\Phi_B=BA=\\mu_0inA\\)
\\(Inductance \\ L \\ =N\\Phi_B/i=\\mu_0n^2lA\\)
L is like a voltage source
\\(E_L = \u2212\\frac{d(N\u03a6_B)}{dt} = \u2212L\\frac{di }{dt}\\)
\\(\\begin{align*}&\\epsilon= iR-(-L\\frac{di}{dt})\\\\ &\\dot{i}+\\frac{R}{L}i-\\frac{\\epsilon}{L}=0\\end{align*}\\)
\\(i=\\frac{\\epsilon}{R}(1-e^{-t/\\tau_L}) \\ \\ \\ (\\tau_L=\\frac{L}{R})\\)
"},{"location":"GP/com/com/#energy","title":"Energy","text":""},{"location":"GP/com/com/#energy-stored-in-a-magnetic-field","title":"Energy Stored in a Magnetic Field","text":"\\(\\epsilon i = Li\\frac{di}{dt} + i^2R.\\)
\\(\\Rightarrow U_B=\\frac{1}{2}Li^2\\)
"},{"location":"GP/com/com/#energy-density-of-a-magnetic-field","title":"Energy Density of a Magnetic Field","text":"\\(\\mu_B=\\frac{U_B}{Ah}=\\frac{Li^2}{2Ah}=\\frac{L}{h}\\frac{i^2}{2A}=\\frac{i^2n^2\\mu_0}{2}=\\frac{B^2}{2\\mu_0}\\)
resembles that in an electric file \\(\\mu_E=\\frac{1}{2}\\epsilon_0E^2\\)
"},{"location":"GP/com/com/#mutual-inductance-of-two-parallel-coils","title":"Mutual Inductance of Two Parallel Coils","text":"\\(M_{21}=\\frac{N_2\\Phi_{21}}{i_1}\\\\M_{12}=\\frac{N_1\\Phi_{12}}{i_2}\\)
\\(\\epsilon_{21}=-M_{21}\\frac{di_1}{dt}\\\\\\epsilon_{12}=-M_{12}\\frac{di_2}{dt}\\)
\\(\\epsilon_1=\\epsilon_{1}+\\epsilon_{12}=-L_{1}\\frac{di_{1}}{dt}-M_{12}\\frac{di_2}{dt}\\)
\\(\\epsilon_2=\\epsilon_{22}+\\epsilon_{21}=-L_{2}\\frac{di_{2}}{dt}-M_{21}\\frac{di_1}{dt}\\)
\\(\\Rightarrow-\\begin{pmatrix}L_1&M_{12}\\\\M_{21}&L_2\\end{pmatrix}\\frac{d}{dt}\\begin{pmatrix}i_1\\\\i_2\\end{pmatrix}=\\begin{pmatrix}\\epsilon_1\\\\\\epsilon_2\\end{pmatrix}\\)
"},{"location":"GP/com/com/#alternating-current-circuits","title":"Alternating-Current Circuits","text":""},{"location":"GP/com/com/#lc-oscillations","title":"LC Oscillations","text":"\\(L\\frac{di}{dt}+\\frac{q}{C}=0\\)
\\(\\ddot{q}+\\frac{1}{LC}q=0\\)
\\(\\Rightarrow q =Acos(w_0t+\\phi)=Qcos(\\frac{1}{\\sqrt{LC}}t+\\phi)\\)
\\(\\Rightarrow Qcos\\phi=0 \\\\-w_0Qsin\\phi=0\\)
"},{"location":"GP/com/com/#the-complex-formalism","title":"The Complex Formalism","text":""},{"location":"GP/com/com/#damped-oscillations-in-an-rlc-circuit","title":"Damped Oscillations in an RLC Circuit","text":"\\(\\ddot{q}+\\frac{R}{L}\\dot{q}+\\frac{1}{LC}q=0\\)
\\(\\Rightarrow q=Qe^{-\\frac{t}{\\tau}}cos(wt+\\phi) \\ where\\ \\tau=2L/R \\ and \\ w=\\sqrt{w_0^2-(1/\\tau)^2} \\ and \\ w_0=\\frac{1}{\\sqrt{LC}}\\)
Note Use ODEs or the method of the below picture
When \\(1/\u03c4 > \u03c9_0\\), one can only find imaginary \u03c9, which means the frictional force is so great that the system cannot oscillate. The circuit is said to be overdamped.
In between, when \\(1/\u03c4 = \u03c9_0\\), the circuit is said to be critically damped. It is worth noting that the critical damping gives the fastest return of the system to its equilibrium position. In engineering design this is often a desirable property.
\\(i(t)=\\tilde{i}e^{iwt}\\)
\\(V(t)=L\\frac{di}{dt}=L(iw)e^{iwt}\\tilde{i}\\)
\\(\\tilde{Z}=iwL\\)
\\(Q(t)=\\tilde{Q}e^{iwt}\\)
\\(\\tilde{V}(t)=\\tilde{Q}/C\\cdot e^{iwt}\\)
\\(\\tilde{i}=\\frac{d\\tilde{Q}}{dt}=iw\\tilde{\\tilde{Q}e^{iwt}}\\)
\\(\\tilde{Z}=\\frac{1}{iwC}=-\\frac{i}{wC}\\)
"},{"location":"GP/com/com/#the-series-rlc-circuit","title":"The Series RLC Circuit","text":""},{"location":"GP/com/com/#resonance","title":"Resonance","text":"When \\(\u03c9_d\\) equals \\(\u03c9_0\\), the circuit is in resonance.
Applying Divergence to Faraday's Law ,we get:\\(\\triangledown\\cdot(\\triangledown \\times \\vec{E})=\\triangledown \\cdot(-\\frac{\\partial\\vec{B}}{\\partial t})=-\\frac{\\partial}{\\partial t}(\\triangledown\\cdot\\vec{B})=0 (Consistency)\\)
However,when applying Divergence to Ampere's Law,we get\\(\\triangledown\\cdot(\\triangledown \\times \\vec{B})=\\triangledown\\cdot(\\mu_0\\vec{J})=\\mu_0(\\triangledown\\cdot\\vec{J})\\)
While \\(\\triangledown \\cdot \\vec{J} = -\\frac{\\partial\\rho}{\\partial t}=-\\frac{\\partial{(\\epsilon_0\\triangledown\\cdot E)}}{\\partial t} =-\\triangledown\\cdot(\\epsilon_0\\frac{\\partial\\vec{E}}{\\partial t}) \\ Which \\ is\\ not\\ necessarily\\ ZERO\\)
\\(\\triangledown\\times\\vec{B}=\\mu_0\\vec{J}+\\mu_0\\vec{J_d}=\\mu_0\\vec{J}+\\mu_0\\epsilon_0\\frac{\\partial\\vec{E}}{\\partial t}\\)
"},{"location":"GP/com/com/#apply","title":"Apply","text":"\\(E=\\frac{\\sigma}{\\epsilon_0}=\\frac{Q}{\\epsilon_0A}\\\\ \\frac{\\partial{E}}{\\partial t}=\\frac{J_d}{\\epsilon_0}=\\frac{I}{\\epsilon_0A}\\)
Choose surface 1 to integral : \\(E=0\\ I_{enc}=I\\)
Choose surface 2 to integral:
\\(I_{enc}=0\\\\\\mu_0\\epsilon_0\\frac{\\partial{E}}{\\partial t}=\\mu_0I/A \\ \\iint \\mu_0\\epsilon_0\\frac{\\partial{E}}{\\partial t}\\cdot d\\vec{A}=\\mu_0I\\)
"},{"location":"GP/com/com/#maxwells-equations","title":"Maxwell\u2019s Equations","text":""},{"location":"GP/com/com/#electromagnetic-waves","title":"Electromagnetic Waves","text":""},{"location":"GP/com/com/#derivation-of-the-wave-equation","title":"Derivation of the Wave Equation","text":"1.Decouple
\\(\\triangledown\\times(\\triangledown\\times\\vec{E})=\\triangledown\\times(-\\frac{\\partial\\vec{B}}{\\partial t})=-\\frac{\\partial}{\\partial t}(\\triangledown\\times\\vec{B})=-\\mu_0\\epsilon_0\\frac{\\partial^2\\vec{E}}{\\partial t^2}\\)
\\(\\triangledown\\times(\\triangledown\\times\\vec{B})=\\triangledown\\times(\\mu_0\\epsilon_0\\frac{\\partial\\vec{E}}{\\partial t})=\\mu_0\\epsilon_0\\frac{\\partial}{\\partial t}(\\triangledown\\times\\vec{E})=-\\mu_0\\epsilon_0\\frac{\\partial^2\\vec{B}}{\\partial t^2}\\)
Another way to solution :
\\(\\vec{A}\\times(\\vec{B}\\times\\vec{C})=\\vec{B}\\cdot(\\vec{A}\\cdot \\vec{C})-\\vec{C}\\cdot(\\vec{A}\\cdot \\vec{B})\\)
\\(\\triangledown\\times(\\triangledown\\times\\vec{C})=\\triangledown\\cdot(\\triangledown\\cdot \\vec{C})-\\triangledown^2\\vec{C}\\)
Thus:
\\(\\triangledown^2\\vec{B}=\\mu_0\\epsilon_0\\frac{\\partial^2\\vec{B}}{\\partial t^2}\\\\ \\triangledown^2\\vec{E}=\\mu_0\\epsilon_0\\frac{\\partial^2\\vec{E}}{\\partial t^2}\\)
\\(\\lambda=\\frac{2\\pi}{k}\\\\ v=\\lambda f=\\lambda/T \\\\w=\\frac{2\\pi}{T}\\)
We have \\(E_m(x)=B_m(x)=0\\)
\\(-k\\hat{x}\\times\\vec{E_m}=-w\\vec{B_m}\\)
When spherical wavefronts spread from an isotropic point source S with power Ps, the energy of the waves is conserved.
The intensity I at the sphere must decrease with r as \\(I=\\frac{P_S}{4\\pi r^2}\\)
"},{"location":"GP/current/current/","title":"Current","text":""},{"location":"GP/current/current/#current","title":"Current","text":""},{"location":"GP/current/current/#resistance-and-capacitance","title":"Resistance and Capacitance","text":"
The resistivity \u03c1 and conductivity \u03c3 of a material are related by \\(\u03c1 = 1/\u03c3 = E/J\\), where \\(E\\) is the magnitude of the applied electric field and \\(J\\) is the magnitude of the current density.
\\(\\vec{a}=-\\frac{e\\vec{E}}{m}\\)
After a typical collision, each electron will \u201close its memory\u201d of its previous drift velocity, starting fresh and moving off in a random direction.
In the average time \\(\u03c4\\) (or mean free time) between collisions, the electron will, on average, acquire a drift velocity
\\(\\vec{v_d}=-\\frac{e\\vec{E}}{m}\\tau \\ \\ \\ Combining \\ with \\vec{J}=-ne\\vec{v_d} \\ We \\ get :\\)
\\(-\\frac{\\vec{J}}{ne}=-\\frac{e\\vec{E}}{m}\\tau \\ \\ \\ which \\ implies \\vec{E}=\\rho\\vec{J} \\ \\ where\\ \\rho=\\frac{m}{ne^2\\tau}\\)
For metals under normal circumstances, both n and \u03c4 can be considered to be a constant. Thus, metals obey Ohm\u2019s law.
Thus,\\(V/I=E\\cdot d/J\\cdot S = \\rho d /S\\)
We have implicitly used the (local) conservation of charge here. Namely, if the total charge in some volume changes, the exactly same amount of charge must have passed in or out through the surface. Otherwise, charge accumulation occurs in the volume enclosed by the surface.
\\(\\frac{d}{dt}\\int_V\\rho(\\vec{r},t)dV = \\int_V \\frac{\\partial{\\rho(\\vec{r},t)}}{\\partial{t}}dV=-\\oint_S\\vec{J}(\\vec{r},t)d\\vec{A}\\)
To be able to move the derivative under the integral sign this way requires that \\(\u2202\u03c1/\u2202t\\) be continuous.
With the divergence theorem, we can rewrite it in the derivative form (note V is arbitrary)\\(\\frac{\\partial \\rho}{\\partial t}=-\\triangledown\\cdot\\vec{J}\\)\uff08By Gauss's Law\uff09
Such an equation of continuity plays an important role in hydrodynamics, hear flow, and diffusion theory, besides electromagnetic theory. It is simply a mathematical expression of a conservation law.
"},{"location":"GP/current/current/#capacitor-and-capacitance","title":"Capacitor and Capacitance","text":"\\(C=\\frac{Q}{U}\\)
"},{"location":"GP/current/current/#capacitance-of-a-parallel-plate-capacitor","title":"Capacitance of a Parallel-Plate Capacitor","text":""},{"location":"GP/current/current/#capacitance-of-a-cylindrical-capacitor","title":"Capacitance of a Cylindrical Capacitor","text":"\\(dW=V'dq'=(\\frac{q'}{C})dq'\\)
\\(W=\\int_0^q(q'/C)dq'=\\frac{q^2}{2C}\\)
\\(U=\\frac{q^2}{2C}=\\frac{CV^2}{2}\\)
"},{"location":"GP/current/current/#dc-circuits","title":"DC Circuits","text":""},{"location":"GP/current/current/#rc-circuits","title":"RC circuits","text":""},{"location":"GP/current/current/#charging-switch-to-a","title":"Charging (switch to a )","text":"\\(\\frac{q}{C}\\frac{dq}{dt}+i^2R=i\\epsilon\\)
\\(\\Rightarrow \\dot{q}+\\frac{q}{RC}=\\frac{\\epsilon}{R}\\)
\\(\\Rightarrow q=C\\epsilon(1-e^{-t/RC})\\)
Note : Derivation see Appendix : ODEs
The equilibrium (final) charge on the then fully charged capacitor is equal to \\(q_0 = CE\\).
\\(\\Rightarrow i = \\frac{dq}{dt}=(\\frac{\\epsilon}{R}e^{-t/RC})\\)
\\(\\Rightarrow V_c = \\frac{q}{C}=\\epsilon(1-e^{-t/RC})\\)
"},{"location":"GP/current/current/#dischargingswitch-to-b-from-a","title":"Discharging(switch to b from a)","text":"\\(\\dot{q}+\\frac{q}{RC}=0\\)
\\(\\Rightarrow q=q_0e^{-t/RC}\\)
\\(\\Rightarrow i = (\\frac{\\epsilon}{R})e^{-t/RC}\\)
"},{"location":"GP/current/current/#energy-density","title":"Energy Density","text":"parallel-plate capacitor:
\\(U=\\frac{1}{2}CV^2=\\frac{1}{2}\\epsilon_0E^2(Ad)\\)
"},{"location":"GP/lec1-3/lec1/#coulombs-law-and-the-electric-field","title":"Coulomb's Law and the Electric Field","text":""},{"location":"GP/lec1-3/lec1/#coulombs-law","title":"Coulomb\u2018s Law","text":"
\\(\\vec{F}=k\\frac{q_1q_2}{r^2}\\)
\\(k = 8.99 * 10^{-12} N*m^2/C^2=\\frac{1}{4\\pi\\epsilon_0}\\\\\\)
\\(\\epsilon_0=8.85*10^{-12}C^2/N*m^2\\)
"},{"location":"GP/lec1-3/lec1/#diphole","title":"Diphole","text":"\u200b \\(\\begin{align*}E&=\\frac{q}{4\\pi\\epsilon_0(z-\\frac{d}{2})^2}- \\frac{q}{4\\pi\\epsilon_0(z+\\frac{d}{2})^2}\\\\ &=\\frac{q\\vec{d}}{2\\pi\\epsilon_0}*\\frac{z}{(z-\\frac{d}{2})^2(z+\\frac{d}{2})^2}\\\\ &=\\frac{\\vec{p}}{2\\pi\\epsilon_0z^3}*\\frac{1}{(1-\\frac{d}{2z})^2(1+\\frac{d}{2z})^2}\\\\ &=\\frac{1}{2\\pi\\epsilon_0}\\frac{p}{z^3} \\ \\ when (z>>d)\\end{align*}\\)
"},{"location":"GP/lec1-3/lec1/#a-diphole-in-a-uniform-electric-filed","title":"A diphole in a uniform electric filed","text":"Note:\\(\\tau=p\\times E\\)
"},{"location":"GP/lec1-3/lec1/#the-electric-field-due-to-a-ring-of-charge","title":"The Electric Field Due to a Ring of Charge","text":"\\(\\begin{align*}dE_z &= \\int \\frac{\\sigma(2\u03c0rdr)}{4\\pi\\epsilon_0(z^2+r^2)}\\frac{z}{\\sqrt{z^2+r^2}}=\\frac{\\sigma z}{2\\epsilon_0}\\int_0^R\\frac{rdr}{(z^2+r^2)^\\frac{3}{2}}\\\\ &= \\frac{\\sigma}{2\\epsilon_0}(1-\\frac{z}{\\sqrt{z^2+R^2}})\\end{align*}\\)
"},{"location":"GP/lec1-3/lec1/#gausss-law","title":"Gauss's Law","text":""},{"location":"GP/lec1-3/lec1/#electric-flux","title":"Electric Flux","text":"\\(\u2206\u03a6 = (E cos\u03b8)\u2206A=\\vec{E}\\cdot{\\vec{A}}\\)
\\(\u03a6=\\int\\vec{E}\\cdot{d\\vec{A}}\\)
"},{"location":"GP/lec1-3/lec1/#gausss-law_1","title":"Gauss's Law","text":"\\(\\epsilon_0 \\oint\\vec{E}\\cdot d\\vec{A}=q_{encore}\\)
"},{"location":"GP/lec1-3/lec1/#applications","title":"Applications","text":""},{"location":"GP/lec1-3/lec1/#1spherical-symmetry","title":"1.Spherical Symmetry","text":"\\(4\\pi r^2\\cdot E = \\frac{q{(\\frac{r}{R})^3}}{\\epsilon_0}\\)
\\(\\Rightarrow \\ \\vec{E}=\\frac{q}{4\\pi\\epsilon_0R^3}\\cdot \\vec{r}\\)
"},{"location":"GP/lec1-3/lec1/#2planar-symmetry","title":"2.Planar Symmetry","text":""},{"location":"GP/lec1-3/lec1/#3cylindrical-symmetry","title":"3.Cylindrical Symmetry","text":"\\(\u03b5_0 \u03a6 = \u03bbh\\)
\\(E=\\frac{\\lambda}{2\\pi r\\epsilon_0}\\)
\\(r_--r_+\\approx dcos\\theta\\)
\\(r_-\\cdot r_+\\approx r^2\\)
\\(V=\\frac{\\vec{p}\\cdot\\vec{r}}{4\\pi\\epsilon_0r^3}\\)
"},{"location":"GP/lec1-3/lec1/#systems-of-charged-particles","title":"Systems of Charged Particles","text":"\\(\\begin{align*} V &= \\int dV = \\int _0^L \\frac{\\lambda dx}{4\\pi\\epsilon_0\\sqrt{x^2+d^2}}\\\\ &=\\int \\frac{\\lambda}{4\\pi\\epsilon_0}\\int_0^L\\frac{dx}{\\sqrt{x^2+d^2}}\\\\ &=\\frac{\\lambda}{4\\pi\\epsilon_0}ln\\frac{L+\\sqrt{L^2+d^2}}{d} \\end{align*}\\)
"},{"location":"GP/lec1-3/lec1/#continuous-charge-distribution-disk","title":"Continuous Charge Distribution: Disk","text":"\\(\\begin{align*}V &= \\int_0^R \\frac{\\sigma(2\u03c0rdr)}{4\\pi\\epsilon_0\\sqrt{z^2+r^2}}\\\\ &= \\frac{\u03c3}{2\\epsilon_0}(\\sqrt{z^2+R^2}\u2212z)\\end{align*}\\)
\\(\\Rightarrow Ez=-\\frac{dV}{dz}\\)
"},{"location":"GP/lec1-3/lec1/#the-triangle-of-electrostatics","title":"The Triangle of Electrostatics","text":""},{"location":"GP/lec1-3/lec1/#preknowledge","title":"PreKnowledge","text":""},{"location":"GP/lec1-3/lec1/#gradient","title":"gradient","text":"\\(\u2207V=\\frac{\\partial{V}}{\\partial{x}}\\hat{x}+\\frac{\\partial{V}}{\\partial{y}}\\hat{y}+\\frac{\\partial{V}}{\\partial{z}}\\hat{z}\\)
"},{"location":"GP/lec1-3/lec1/#curl","title":"curl","text":"\\(\u2207 \\times V = \\begin{bmatrix}\\hat{x}&\\hat{y}&\\hat{z}\\\\\\frac{\\partial{}}{\\partial{x}}&\\frac{\\partial{}}{\\partial{y}}&\\frac{\\partial{}}{\\partial{z}}\\\\ V_x&V_y&V_z\\end{bmatrix}\\)
\\(\u2207 \\cdot V\\)
\\(\\frac{\\partial E_x}{\\partial x}+\\frac{\\partial E_y}{\\partial y}+\\frac{\\partial E_z}{\\partial z}=\\triangledown \\cdot \\vec{E}\\)
"},{"location":"GP/lec1-3/lec1/#e-leftrightarrow-rho","title":"\\(E \\Leftrightarrow \\ \\rho\\)","text":"\\(\\oint\\vec{E}\\cdot d\\vec{l}=\\int\\int(\\triangledown\\times \\vec{E})dS = 0\\) \\(By \\ Stokes\\) -- independent of \\(PATHS\\)
Choose a Gaussian surface to enclose a small cube centered at \\(\\vec{r}\\) with side \\(\\triangledown x\\triangledown y\\triangledown z\\) such that \\(\\triangle V = \\triangle x \\triangle y \\triangle z\\)
For the same conductor with a cavity, is there excess charge on the cavity wall?
If we enclose the conductor in a concentric Gaussian sphere, Gauss\u2019 law tells us that the electric field outside the surface of the conductor looks as if all the excess charge on the shell were concentrated at its center.
"},{"location":"GP/lec1-3/lec1/#electric-field-outside-isolated-conductors","title":"Electric Field Outside Isolated Conductors","text":"There is a charge +q outside a spherical metal shell at a distance\\(r+R\\) from its center,how to decide the image?
In this way \\(V(x,y,z)=\\frac{\\frac{q}{ 4\\pi\\epsilon_0}}{\\sqrt{x^2+y^2+(z-d)^2}}- \\frac{\\frac{q}{ 4\\pi\\epsilon_0}}{\\sqrt{x^2+y^2+(z+d)^2}}\\)
We assert this is the solution for the upper half region.
We can check that\\(V(x,y,z=0)=0\\),and\\(V \u21920\\)for\\(x^2 +y^2 +z^2 \u226bd2.\\)
The only charge in the region z > 0 is q at (0,0,d).
According to Gauss\u2019 law, the surface charge density \u03c3 induced on the conductor is:
\\(E=-\\epsilon_0\\frac{\\partial V}{\\partial z}|_{z=0}=\\frac{-qd}{2\\pi\\epsilon_0(x^2+y^2+d^2)^\\frac{3}{2}}\\hat{z}\\)
\\(\\sigma=\\epsilon_0\\cdot E =\\frac{-qd}{2\\pi(x^2+y^2+d^2)^\\frac{3}{2}}\\)
Derive as little circles
\\(V=\\int_0^\\infty\\frac{qd\\cdot 2\\pi r dr}{2\\pi(r^2+d^2)^{3/2}}\\cdot\\frac{q}{4\\pi\\epsilon_0(r^2+d^2)^{1/2}}=\\frac{q^2d}{8\\pi\\epsilon_0}\\int_0^{\\infty}\\frac{d(r^2)}{r^2+d^2}=-\\frac{q^2}{8\\pi\\epsilon_od}\\)
which is half of the energy for bringing two point charges to a distance 2d with no conductor.
"},{"location":"GP/lec1-3/lec1/#positive-charge-q-and-a-conducting-sphere","title":"positive charge +Q and a conducting sphere","text":"Solve the electro-static potential function \\(V(x,y,z)\\) and the electrostatic field \\(E(x,y,z)\\) in space, with a positive charge \\(+Q\\) located at \\((\u2212D, 0, 0)\\)and a conducting sphere of radius R (not grounded) at origin \\((R<D)\\).
Pre - knowledge :
Image : \\(r'=\\frac{R^2}{D}\\) \\(q'=-qR/D\\)
Derivation :
\\(\\begin{align*}V(\\theta)&=\\frac{q'}{4\\pi\\epsilon_0}\\cdot\\frac{1}{\\sqrt{R^2+r'^2-2Rr'cos\\theta}}+\\frac{Q}{4\\pi\\epsilon_0}\\cdot\\frac{1}{\\sqrt{R^2+D^2-2RDcos\\theta}}=0\\end{align*}\\)
\\(\\Rightarrow \\frac{1}{4\\pi\\epsilon_0}[\\frac{\\frac{q'}{\\sqrt{Rr'}}}{\\sqrt{R/r'+r'/R-2cos\\theta}}+\\frac{\\frac{Q}{\\sqrt{RD}}}{\\sqrt{R/D+D/R-2cos\\theta}}]=0\\)
\\(\\begin{align*}\\Rightarrow &\\frac{q'}{\\sqrt{Rr'}}+\\frac{Q}{\\sqrt{RD}}=0\\\\ &\\frac{R}{r'}+\\frac{r'}{R}=\\frac{R}{D}+\\frac{D}{R}\\end{align*}\\)
\\(\\Rightarrow R/r'=D/R \\Rightarrow r'=R^2/D \\Rightarrow q'=-\\frac{qR}{D}\\)
* Also See https://galileoandeinstein.phys.virginia.edu/Elec_Mag/2022_Lectures/EM_08_Images.html
"},{"location":"GP/lec1-3/lec1/#appendix","title":"Appendix \uff1a","text":""},{"location":"GP/lec1-3/lec1/#cross-product","title":"Cross product","text":"\\(dA=r^2sin\\theta d\\theta d\\Phi\\)
\\(Solid \\ Angle\\ d\\Omega = \\frac{dA}{r^2}=sin\\theta d\\theta d\\Phi\\)
\\(dV=r^2sin\\theta d\\theta d\\Phi dr\\)
"},{"location":"GP/light/inteference/","title":"interference & refraction","text":""},{"location":"GP/light/inteference/#interference","title":"Interference","text":""},{"location":"GP/light/inteference/#the-superposition-of-waves","title":"The Superposition of Waves","text":""},{"location":"GP/light/inteference/#wave-equation","title":"Wave Equation","text":"
Recall that each field component of an electromagnetic wave$ (Ex, Ey, Ez, Bx, By, and Bz) $satisfies the scalar 3D differential wave equation
\\(\\frac{1}{v^2}\\frac{\\partial^2{\\phi}}{\\partial t^2}=\\frac{\\partial^2{\\phi}}{\\partial x^2}+\\frac{\\partial^2{\\phi}}{\\partial y^2}+\\frac{\\partial^2{\\phi}}{\\partial z^2}\\)
This equation is linear \\(\\vec{\\phi}(r,t)\\) and its derivatives appear only to the first power.
Consequently, if \\(\\vec{\\phi}(r,t)\\) are solutions, any linear combination of them \\(\\vec{\\phi}(r,t)=\\sum_{i=1}^nC_i\\vec{\\phi}_i(r,t)\\) will be a solution as well.
\\(cos(\u03b1 + \u03b2) = cos \u03b1 cos \u03b2 \u2212 sin \u03b1 sin \u03b2\\)
\\(cos(\u03b1 \u2212 \u03b2) = cos \u03b1 cos \u03b2 + sin \u03b1 sin \u03b2\\)
\\(cos \u03b1 + cos \u03b2 = 2 cos \\frac{(\u03b1 + \u03b2)}{2} cos\\frac{(\u03b1 - \u03b2)}{2}\\)
\\(cos \u03b1 \u2212 cos \u03b2 = -2 sin \\frac{(\u03b1 + \u03b2)}{2} sin\\frac{(\u03b1 - \u03b2)}{2}\\)
"},{"location":"GP/light/inteference/#the-algebraic-method-of-adding-waves","title":"The Algebraic Method of Adding Waves","text":"\\(E_1 = E_{01}cos(\u03b1_1 \u2212 \u03c9t)\\)
\\(E_2 = E_{02}cos(\u03b1_2 \u2212 \u03c9t)\\)
\\(\u03b1_i = kx_i + \u03c6_i\\) with \\(x_i\\) being the distance from the source \\(s_i\\) of the waveto the point of observation.
The linear combination of the waves is \\(E \u2261 E_0 cos(\u03b1 \u2212 \u03c9t) = E_1 + E_2\\)
We get :
\\(E_0cos\u03b1=E_{01}cos\u03b1_1 +E_{02}cos\u03b1_2\\)
\\(E_0sin\u03b1=E_{01}sin\u03b1_1 +E_{02}sin\u03b1_2\\)
\\(\\Rightarrow \\vec{E_0}=\\vec{E_{01}}+\\vec{E_{02}}\\)
\\(E_0^2=E_{01}^2+E_{02}^2+2E_{01}E_{02}cos(\\alpha_2-\\alpha_1)\\)
The resultant intensity is not simply the sum of the component intensity
there is an additional contribution \\(2E_{01}E_{02}cos(\u03b1_2 \u2212 \u03b1_1)\\), known as the interference term.
The phase difference may arise from a difference in path length traversed by the two waves, as well as a difference in the initial phase angle \\(\u03b4 =\\alpha_2-\\alpha_1= \\frac{2\u03c0}{\\lambda}(x_2 \u2212x_1)+(\u03c6_2 \u2212\u03c6_1).\\)
"},{"location":"GP/light/inteference/#the-complex-method","title":"The Complex Method","text":"It is often mathematically convenient to make use of the complex representation when dealing with the superposition of harmonic waves.
We now redo the calculation of adding two waves.
The wave function \\(E_1 =E_{01}cos(\u03b1_1 \u2212\u03c9t)=E_{01}cos(kx_1 \u2212\u03c9t+\u03c6_1)\\) can be written as \\(\\tilde{E}_1 = E_{01}e^{i(\u03b1_1\u2212\u03c9t)} = E_{01}e^{i\u03b1_1}e^{\u2212i\u03c9t}\\) if we are interested only in the real part.
Now we discuss a graphical technique that uses vector additions instead of trigonometric additions.
We can map a wave, which has an amplitude and a phase, to a vector, known as a phasor, in a two-dimensional plane, such that
"},{"location":"GP/light/inteference/#summary","title":"Summary","text":"Initial phase difference can exist between sources, as long as it remains constant; the two sources are said to be coherent.
Until the advant of laser, no two individual sources can maintain a constant relative phase long enough for an observable interference pattern.
Each point on a wavefront may be regarded as a source of waves expanding from that point.
If waves strike a barrier with a small opening, the waves may be seen to expand from the opening. Notice the wavelength is larger than the opening in this case.
"},{"location":"GP/light/inteference/#youngs-interference-experiment","title":"Young\u2019s Interference Experiment","text":"In other words, the light leaving the slits is in phase.
However, the electric field components of these waves at point P are not in phase and vary with time as
\\(E_1 =E_0cos(kr_1 \u2212\u03c9t)=E_0cos(kL+\u03b2\u2212\u03c9t)\\)
\\(E_2 =E_0cos(kr_2 \u2212\u03c9t)=E_0cos(kL\u2212\u03b2\u2212\u03c9t)\\)
Where the phase difference \\([L = (r_1 + r_2)/2 = \\sqrt{D^2+y^2}]\\) \\([\u03b4_2 = 2\u03b2 = k\u2206L = \\frac{2\u03c0d}{\\lambda} sin\u03b8.]\\)
Therefore, a bright fringe appears when \\(\u2206L = d sin \u03b8 = m\u03bb\\) where m is an integer.
On the other hand, a dark fringe appears when \\(\u2206L=dsin\u03b8= (m+\\frac{1}{2}) \u03bb\\) where m is an integer.
We can then find the angle \u03b8 to any fringe and thus use the values of m to label the fringes.
So, reflecting off higher index, ray \\(r_1\\) has an additionalreflection phase shift 0.5 wavelength. There is no such shift for \\(r_2\\)
In addition, the light waves of rays \\(r_1\\) and \\(r_2\\) has a path difference \\(2L\\), which occurs in index \\(n_2\\).Notice that the wavelength in the medium is \\(\u03bb_2=\\frac{v_2}{f}=\\frac{c}{n_2}\\frac{1}{f}=\\frac{\\lambda}{n_2}\\)
Therefore, rays are in phase if \\(2L=(m+\\frac{1}{2})\\frac{\\lambda}{n_2}\\) , for integer m.
They produce an interference maximum and the nearby region on the film is bright to observers.
Similarly, if they are exactly out of phase \\(2L=m\\frac{\\lambda}{n_2}\\) they produce an interference minimum and the nearby region is dark, even though it is illuminated.
"},{"location":"GP/light/inteference/#negligible-film-thickness","title":"Negligible Film Thickness","text":"Newton\u2019s rings are interference patterns formed by light incident on the thin film of air between a convex lens and a flat (or between two suitable lenses).
"},{"location":"GP/light/light/","title":"Light Waves","text":""},{"location":"GP/light/light/#light","title":"Light","text":""},{"location":"GP/light/light/#light-as-electromagnetic-waves","title":"Light as Electromagnetic Waves","text":""},{"location":"GP/light/light/#plane-wave","title":"Plane Wave","text":""},{"location":"GP/light/light/#transmission-of-light-in-matter","title":"Transmission of Light in Matter","text":"
In dielectric materials, the electric field is altered by a factor \\(\u03b5_r\\) , the relative permittivity (also called the dielectric constant \\(\u03ba\\)).
In magnetic materials (therefore, not in glass or plastic), the magnetic field is altered by a factor \\(\u03bc_r\\) , the relative permeability.
Therefore, a light wave propagating through any substantive medium travels at a speed \\(v=\\frac{c}{n}=\\frac{1}{\\sqrt{\\epsilon_r\\mu_r}}\\frac{1}{\\sqrt{\\epsilon_o\\mu_o}}\\) where the index of refraction \\(n=\\sqrt{\\epsilon_r\\mu_r}\\)
\\(Derivation\\)
\\(t(x)=\\frac{\\bar{SO}}{v_i}+\\frac{\\bar{OP}}{v_t}=\\frac{\\sqrt{x^2+h^2}}{\\frac{c}{n_i}}+\\frac{\\sqrt{(a-x)^2+b^2}}{\\frac{c}{n_t}}\\)
\\(\\frac{dt}{dx}=\\frac{n_i}{c}\\frac{x}{\\bar{SO}}-\\frac{n_t}{c}\\frac{a-x}{\\bar{OP}}=0\\)
Thus \\(n_isin\\theta_i=n_tsin\\theta_t\\)
"},{"location":"GP/light/light/#huygens-principle","title":"Huygens\u2019 Principle","text":"Huygens\u2019 principle is: All points on a wavefront serve as point sources of spherical secondary wavelets. After a time \\(t\\), the new position of the wavefront will be that of a surface tangent to these secondary wavelets.
Thus , We get Law of reflection.
\\(\\triangle t = \\frac{\\triangle l\\ tan\\theta_1}{v_1}\\)
\\(R=v_2 \\cdot \\triangle t\\)
\\(v_i=\\frac{c}{n_i}\\)
\\(\\Rightarrow R=\\frac{n_1}{n_2}\\triangle l \\cdot tan\\theta_1\\)
\\(sin\\theta_2=\\frac{R}{\\frac{\\triangle l}{cons\\theta_1}}\\)
\\(\\Rightarrow n_1sin\\theta_1=n_2sin\\theta_2\\)
Thus We get Law of refraction.
"},{"location":"GP/light/light/#the-electromagnetic-approach","title":"The Electromagnetic Approach","text":"\\(\\vec{E}_i = \\vec{E}_{0i}cos(\\vec{k}_i \\cdot\\vec{r}\u2212\u03c9_it)\\)
\\(\\vec{E}_r = \\vec{E}_{0r}cos(\\vec{k}_r \\cdot\\vec{r}\u2212\u03c9_rt+\\phi_r)\\)
\\(\\vec{E}_t = \\vec{E}_{0t}cos(\\vec{k}_t \\cdot\\vec{r}\u2212\u03c9_tt+\\phi_t)\\)
\\(Boundary\\ conditions\\)
\\(\\hat{u}_n \\times (\\vec{E}_i+\\vec{E}_r)-\\hat{u}_n \\times \\vec{E}_t=0\\) which is satisfied for all values of time and at any point on the interface.
Thus, we have :
\\(\\begin{align*}&\\hat{u}_n \\times \\vec{E}_{0i}cos(\\vec{k}_i \\cdot\\vec{r}\u2212\u03c9_it)+\\hat{u}_n \\times \\vec{E}_{0r}cos(\\vec{k}_r \\cdot\\vec{r}\u2212\u03c9_rt+\\phi_r)\\\\ &= \\hat{u}_n \\times \\vec{E}_{0t}cos(\\vec{k}_t \\cdot\\vec{r}\u2212\u03c9_tt+\\phi_t) \\end{align*}\\)
\\((\\vec{k_i}\\cdot \\vec{r})|_{y=b}=(\\vec{k_r}\\cdot \\vec{r}+\\phi_r)|_{y=b}=(\\vec{k_t}\\cdot \\vec{r}+\\phi_t)|_{y=b}\\)
Thus,We have \\((\\vec{k_i}-\\vec{k_r})|_{y=b}=\\phi_r\\) or \\((\\vec{k}_i-\\vec{k_r})\\cdot(\\vec{r_1}-\\vec{r_2})=0\\) for any pair of \\(\\vec{r_1}\\) and \\(\\vec{r_2}\\) terminating on the interface.
For $\\hat{u_n}\\cdot (\\vec{r_1}-\\vec{r_2})=0 $,we know \\((\\vec{k_i}-\\vec{k_r})\\) is parallel to \\(\\hat{u_n}\\) or \\(k_isin\\theta_i=k_rsin\\theta_r\\)\u300cREFLECTION LAW\u300d
Since the incident and reflected waves are in the same medium, \\(k_i = k_r\\) , so, finally, \\(\u03b8_i = \u03b8_r\\) (the law of reflection).
\\(\\vec{k_i}\\times\\hat{u_n}=\\vec{k_t}\\times\\hat{u_n}\\)
Or. \\(n_i(\\hat{k_i}\\times\\hat{u_n})=n_t(\\hat{k_t}\\times\\hat{u_n})\\)\u300cREFRACTION LAW\u300d
Note that the law of reflection and the law of refraction only rely on the phase relationship that exists among the phases of \\(\\vec{E}_{0i}\\), \\(\\vec{E}_{0r}\\) and \\(\\vec{E}_{0t}\\) at the boundary.
There is still an interdependence shared by the amplitudes \\(\\vec{E}_{0i}\\), \\(\\vec{E}_{0r}\\) and \\(\\vec{E}_{0t}\\). The additional constraint can be used to calculate the amplitude of the reflected wave and the transmitted wave (the Fresnel equations). This will lead to the phenomenon of polarization by reflection (to be discussed in the lecture on polarization).
\\(Proof\\)
"},{"location":"GP/light/light/#images-from-spherical-mirrors","title":"Images from Spherical Mirrors","text":"\\(\\frac{1}{p}+\\frac{1}{i}=\\frac{1}{f}\\)
\\(Proof\\)
When \\(p<f\\) \\(i\\) is negative,indicating an imaginary image.
When \\(i\\) > 0 : an upsidedown real image
When \\(i\\) <0 : an erect imaginary image
\\(Proof\\)
"},{"location":"GP/light/light/#spherical-refraction","title":"Spherical Refraction","text":"\\(Proof\\)
Thus \\(f\\) for convex lens is positive , for concave lens is negative
\\(Proof\\)
It can also be applied for any number of lenses.
The overall (or net) lateral magnification M of a system of lenses (or lenses and a mirror) is the product of the individual lateral magnifications as given by \\(m = \u2212i/p\\).
"},{"location":"GP/light/light/#application","title":"Application","text":"The normal human eye can focus a sharp image of an object on the retina if the object is located beyond the near point (\u223c 25 cm from the eye).
To see better, we want to generate an magnified image of an object at the near point, instead of putting the object there.
Explain eg \\(p2\\) : \\(\\frac{1}{p_2}+\\frac{1}{i_2}=\\frac{1}{f}\\) but \\(i_2<0\\),Thus if we want \\(|i_2|\\)to be large ,\\(p_2\\) has to be relatively large, but also \\(p_2\\)has to be smaller than \\(f_{ey}\\) , thus \\(p2 <\\approx f_{ey}\\).
This leaves the distance between the two lenses \\(f_{ob} + s + f_{ey}\\) the only parameter to tune. The longer the distance, the larger the magnification
\\(M = m_1m_2 \\approx -\\frac{f_{ob}+s}{f_{ob}}\\frac{25}{f_{ey}}\\)
A plane wave is a type of wave whose wavefronts are flat, or nearly flat. This means that the wave crests and troughs are aligned along parallel lines. Plane waves are an important concept in physics and are often used to describe various types of waves, including electromagnetic waves and sound waves.
Key characteristics of a plane wave include:
Constant Phase Front: In a plane wave, all points on a wavefront have the same phase. The phase of a wave is a measure of the position of a point in its cycle relative to a reference point.
Parallel Propagation: The wavefronts of a plane wave propagate in parallel lines. This is in contrast to spherical waves, where the wavefronts are spheres centered around the source.
Mathematical Representation: Mathematically, a one-dimensional plane wave can be represented as a function of the form:
\\(Acos\u2061(kx\u2212\u03c9t+\u03d5)\\)
where:
Plane waves are useful in physics because they simplify the mathematical description of wave phenomena, making it easier to analyze and solve problems. They are often used as a starting point for understanding more complex wave behavior. In the context of electromagnetic waves, for example, plane waves are fundamental to the study of optics and electromagnetic field theory.
"},{"location":"GP/light/df/diffraction/#single-slit-diffraction","title":"Single-Slit Diffraction","text":""},{"location":"GP/light/df/diffraction/#electric-field-and-intensity","title":"Electric Field and Intensity","text":"To find an expression for the intensity at an arbitrary point P on the viewing screen, corresponding to a particular small angle \u03b8, we need to divide the slit into N zones of equal widths \\(\u2206x = a/N\\) small enough that we can assume each zone acts as a source of Huygens wavelets.
We then add the phasors for the wavelets, which form a geometric series(notice. \\(r_{i+1}\u2212r_i =\u2206xsin\u03b8\\)):
\\(\\widetilde{E}_\\theta =\\frac{E_0}{N}e^{-iwt}e^{ikr+1}\\times [1+e^{ik(r_2-r_1)}+e^{ik(r_3-r_1)}+\u2026+e^{ik(r_N-r_1)}]\\)
note \\(\\Phi\\)=\\(\\frac{asin\\theta}{\\lambda}\\times2\\pi\\)
Summary
\\(\\begin{align*}\\widetilde{E}_\\theta &=\\frac{E_0}{N}e^{-iwt}e^{ikr+1}\\times [1+e^{ik(r_2-r_1)}+e^{ik(r_3-r_1)}+\u2026+e^{ik(r_N-r_1)}]\\\\&=\\frac{E_0 \\triangle x}{a}e^{-iwt}[e^{ikr_1}+e^{ikr_2}+\u2026+e^{ikr_N}]\\end{align*}\\)
\\(\\Rightarrow_{N\\to+\\infty} \\widetilde{E}_\\theta = \\frac{E_0}{a} e^{-iwt}\\int_0^ae^{ik\uff08sin\\theta x+r_1\uff09}dx\\) ~ \\(\\int_{-a/2}^{a/2}e^{ik_xx}dx\\) \\((k_x=ksin\\theta)\\)
"},{"location":"GP/light/df/diffraction/#fourier-transform","title":"Fourier Transform","text":"Thus \\(\\widetilde{E}_{sq}(k_x)= E_0a\\frac{2sin(k_xa/2)}{k_x}=E_0a\\frac{sin\\alpha}{\\alpha}(\\alpha=k_xa/2=\\frac{sin\\theta a}{2})\\)
The key message is that the field distribution in the Fraunhofer diffraction pattern is the Fourier transform of the field distribution across the aperture.
"},{"location":"GP/light/df/diffraction/#diffraction-by-a-circular-aperture","title":"Diffraction by a Circular Aperture","text":"We are essentially collecting only a fraction of the incident wavefront and therefore cannot hope to form a perfect image. The image is related to the Fourier transform of a disk and is known as the Airy pattern.
The analysis of such patterns shows that the first minimum for the diffraction pattern of a circular aperture of diameter a is located by \\(sin\\theta = 1.22\\frac{\\lambda}{a}\\) in contrast to \\(sin \u03b8 = \u03bb/a\\) in the slit case.
"},{"location":"GP/light/df/diffraction/#resolvability","title":"Resolvability","text":"Two objects cannot be distinguished from a single point object, if their diffraction patterns (mainly their central maxima) overlap. Rayleigh\u2019s criterion for resolvability states that the two point objects are barely resolved if their angular separation is such that the central maximum of the diffraction pattern of one source is centered on the first minimum of the diffraction pattern of the other, i.e.\\(\u03b8_R = sin^{\u22121} \\frac{1.22\u03bb}{a} \u2248 1.22\\frac{\\lambda}{a}\\)
Formally, with diffraction effects taken into account, the intensity of a double-slit interference pattern (shown later with the Fourier method) is
\\(I(\\theta)=I_{max}(\\frac{sin\\alpha}{\\alpha})^2cos^2\\beta\\) where \\(\\beta = \\frac{\\delta_2}{2}=\\frac{\\pi}{\\lambda}dsin\\theta\\) \\(\\alpha = \\frac{\\pi}{\\lambda}asin\\theta\\)
The interference pattern can be understood by a convolution theorem for the Fourier transformation: The transform\uff08Fourier\uff09 of the convolution of two functions \\((f*g)\\) is the product of their transforms.
\\(Proof\\)
\\((f*g)(x)=\\int dx\\int dx'f(x')g(x-x')e^{ikx}\\\\=\\int f(x')e^{ikx'}d x'\\int dx\\ g(x-x')e^{ik(x-x')}\\)
Thus
The single-slit aperture function :\\(F[E_{sq}(x)]=\\widetilde{E}_{sq}(k_x)=\\int_{-a/2}^{a/2}E_0e^{ikx}dx= E_0a\\frac{2sin(k_xa/2)}{k_x}=E_0a\\frac{sin\\alpha}{\\alpha}(\\alpha=k_xa/2=\\frac{sin\\theta a}{2})\\)
Young\u2019s double-slit interference result\uff1a
Young's double-slit interference result is related to the interference pattern formed by two narrow slits. The function h(x) describes the distribution of intensity along the screen where the interference pattern is observed. In this case, h(x) is represented as a sum of two delta functions:
\u200b \\(h(x) = \u03b4(x + d/2) + \u03b4(x \u2212 d/2)\\)
Here, d is the separation between the two slits, and \u03b4(x) is the Dirac delta function. The Dirac delta function is a mathematical concept often used in physics to model idealized distributions. It is not a traditional function but rather a distribution that satisfies certain properties. In this context, \u03b4(x) can be thought of as an infinitely tall and narrow spike located at x=0.
\u200b \\(F[h(x)]=\\int_{-\\infty}^{+\\infty} h(x)e^{ik_xx}dx= e^{-ik_xd/2}+e^{ik_xd/2}=2cos(k_xd/2)\\)
"},{"location":"GP/light/df/diffraction/#interference-vs-diffraction","title":"Interference vs Diffraction","text":"If we let \\(a\u21920\\), then \\(\u03b1\u21920\\) and\\(\\frac{sin\\alpha}{\\alpha}\u21921\\). Our result then reduces, as it must, to an equation describing the interference pattern for a pair of vanishingly narrow slits with slit separation d.
Similarly, putting \\(d \u2192 0\\) is equivalent physically to causing the two slits to merge into a single slit of width a. Then we have \\(\u03b2\u21920\\) and \\(cos2\u03b2\u21921\\). Our result reduces,as it must,to an equation describing the diffraction pattern for a single slit of width a.
If the combining waves originate from a small number of elementary coherent sources \u2014 as in a double-slit experiment with a \u226a \u03bb \u2014 we call the process interference.
If the combining waves originate in a single wavefront \u2014 as in a single-slit experiment \u2014 we call the process diffraction.
Note that both are superposition effects and usually both are present simultaneously.
"},{"location":"GP/light/df/diffraction/#diffraction-gratings","title":"Diffraction Gratings","text":"In double-slit interference with a \u226a \u03bb \\(I(\u03b8) = I_{max}cos^2 [\\frac{\u03c0d}{\\lambda} sin\u03b8]\\)
The bright fringes due to different wavelengths overlap too much to be distinguished.
A useful tool in the study of light and of objects that emit and absorb light is the diffraction grating, which has a much greater number N of slits, often called rulings, perhaps as many as several thousand per millimeter.
"},{"location":"GP/light/df/diffraction/#multiple-slits-with-monochromatic-light","title":"Multiple Slits with Monochromatic Light","text":"Figure below shows the simplest case of multiple-slit interference, with three slits, or \ud835\udc41=3. The spacing between slits is d, and the path length difference between adjacent slits is \ud835\udc51sin\ud835\udf03, same as the case for the double slit. What is new is that the path length difference for the first and the third slits is 2\ud835\udc51sin\ud835\udf03. The condition for constructive interference is the same as for the double slit, that is \\(dsin\\theta=m\\lambda\\)
When this condition is met, 2\ud835\udc51sin\ud835\udf03 is automatically a multiple of \ud835\udf06, so all three rays combine constructively, and the bright fringes that occur here are called principal maxima. But what happens when the path length difference between adjacent slits is only \ud835\udf06/2? We can think of the first and second rays as interfering destructively, but the third ray remains unaltered. Instead of obtaining a dark fringe, or a minimum, as we did for the double slit, we see a secondary maximum with intensity lower than the principal maxima.
In general, for N slits, these secondary maxima occur whenever an unpaired ray is present that does not go away due to destructive interference. This occurs at (\ud835\udc41\u22122) evenly spaced positions between the principal maxima.
The amplitude of the electromagnetic wave is correspondingly diminished to 1/\ud835\udc41 of the wave at the principal maxima, and the light intensity, being proportional to the square of the wave amplitude, is diminished to \\(\\frac{1}{N^2}\\) of the intensity compared to the principal maxima.
As figure below shows, a dark fringe is located between every maximum (principal or secondary). As N grows larger and the number of bright and dark fringes increase, the widths of the maxima become narrower due to the closely located neighboring dark fringes. Because the total amount of light energy remains unaltered, narrower maxima require that each maximum reaches a correspondingly higher intensity.
\\(Phase\\ difference\\)
\\(\\delta_N=\\frac{2\\pi}{\\lambda}dsin\\theta\\)
Consider the example below when N equals 4
A grating\u2019s ability to resolve (separate) lines of different wavelengths depends on the linewidth. The half-width of the central line \\(\u2206\u03b8_{hw}\\) is determined by the first minimum in intensity, at which the N rays from the N slits of the grating cancel one another.
The first minimum occurs where the phase difference between the adjacent slits is (from the path length difference between the top and bottom rays)
\\(\\delta_N=\\frac{2\\pi}{\\lambda}dsin\\triangle\\theta_{hw}=\\frac{2\\pi}{N}\\)
Or \\(\\triangle\\theta_{hw}\\approx sin\\triangle\\theta_{hw}=\\frac{\\lambda}{Nd}\\)
"},{"location":"GP/light/df/diffraction/#atomic-grating","title":"Atomic Grating","text":"A crystalline solid, which consists of a regular array of atoms, resembles a diffraction grating with separation d on the atomic scale (\u223c \\(10^{\u221210}\\) m).
Waves can be diffracted as if they were reflected by a family of parallel planes, with angles measured relative to the planes (not to a normal as in optics).
NOTE : The distance difference is now approximately \\(2dsin\\theta\\)
Suppose we would like to use the visible light \\((\u03bb \u2243 5.5 \u00d7 10^{\u22127} m)\\) to study the diffraction. The first-order maximum (m = 1) would occur at \\(sin \u03b8 = \\frac{m\u03bb}{2d} = 2750 \u226b 1.\\)
This means that we would not observe the first-order maxima. Therefore, we need waves with much shorter wavelength \\((\u03bb \u2248 d)\\), that is, X rays.
"},{"location":"GP/light/df/diffraction/#x-ray-diffraction","title":"X-Ray Diffraction","text":"Wilhelm Roentgen discovered X rays in 1895. He realized that these rays could be used to pass through, e.g., his wife\u2019s hand and to create an image of internal bones.
The invention of X rays helped revolutionize modern medicine, evolving into what we use today, called the digital X-ray.
The maxima turns out to be in directions as if the x rays were reflected by a family of crystal planes that extend through the atoms within the crystal and that contain regular arrays of the atoms.
Bragg\u2019s law states that the intensity maxima for X-ray diffraction is \\(2d sin\u03b8 = m\u03bb\\) where \\(m = 1,2,3,...\\) is the order number of an intensity maximum. A monochromatic X-ray beam can be used to determine the geometrical structure of a crystal.
"},{"location":"GP/light/df/diffraction/#crystallography-miller-indices","title":"Crystallography: Miller Indices","text":"Miller Indices are a symbolic vector representation for the orientation of an atomic plane in a crystal lattice and are defined as the reciprocals of the fractional intercepts which the plane makes with the crystallographic axes.
The method by which indices are determined is best shown by example. Recall, that there are three axes in crystallographic systems (except sometimes in the hexagonal system adopts a convention where there are four axes). Miller indices are represented by a set of 3 integer numbers.
(2,0,0) half of (1,0,0) Thus,\\(\\theta\\) is different
"},{"location":"GP/light/df/diffraction/#applications","title":"Applications","text":"X-ray diffraction from fibrous DNA (by Rosalind Franklin and her student R. G. Gosling) tipped off Watson and Crick to double helix. Watson and Crick also knew from the work of Erwin Chargaff that adenine and thymine bases and guanine and cytosine bases were always present in approximately equal amounts.
"},{"location":"GP/light/quantum%20light/quantum/","title":"The Quantum Nature of Light","text":""},{"location":"GP/light/quantum%20light/quantum/#the-photoelectric-effect","title":"The Photoelectric Effect","text":"Experiments show that if you direct a beam of light of short enough wavelength onto a clean metal surface, the light will eject the electrons from the surface.
We adjust the potential difference V by moving the sliding contact so that the potential of collector C is slightly negative with respect to target T.
At the stopping potential \\(V = V_{stop}\\), the reading of meter A has just dropped to zero, the most energetic ejected electrons are turned back just before reaching the collector.
"},{"location":"GP/light/quantum%20light/quantum/#puzzle-1-for-classical-physics","title":"Puzzle 1 for Classical Physics","text":"Measurements show that for light of a given frequency, \\(K_{max}\\) does not depend on the intensity of the light source.
However,in the classical theory, however, if we increase the amplitude of the wave and its oscillating electric field, the electron should get a more energetic \u201ckick\u201d.
Why Strange?The incident light is a sinusoidally oscillating electromagnetic wave.
An electron in the target should oscillate sinusoidally due to the oscillating electric force on it from the wave\u2019s electric field.
If the amplitude of the electron\u2019s oscillation is great enough, the electron should break free of the target\u2019s surface \u2013 that is, be ejected from the target.
Experiments show that the photoelectric effect does not occur if the frequency is below a certain cutoff value \\(f_0\\) ,no matter how intense the incident light is.
"},{"location":"GP/light/quantum%20light/quantum/#sum","title":"Sum","text":"In the classical theory, light is viewed as an electromagnetic wave.
One expects that no matter how low the frequency, electrons can always be ejected by light if they are supplied with enough energy \u2014 that is, if one uses a light source that is bright enough.
In experiments, however, electrons can escape only if the light frequency exceeds a certain value, and the escaping electron\u2019s kinetic energy is greater for a greater light frequency.
Einstein proposed that electromagnetic radiation (or simply light) is quantized and exists in elementary amounts (quanta) that we now call photons. According to his proposal, the quantum of a light wave of frequency f has the energy \\(E = hf = \\bar{h}\u03c9\\), where \\(h = 2\u03c0\\bar{h} = 6.63 \u00d7 10^{\u221234} J\u00b7s\\) is the Planck constant, and \u03c9 is the angular frequency. The total energy of a light wave of frequency f must be an integer multiple of \\(hf\\) , with the smallest amount being \\(hf\\) ,the energy of a single photon.
Einstein further proposed that when light is absorbed or emitted by an object (matter), the absorption or emission event occurs in the atoms of the object.
In the absorption event, the energy \\(hf\\) of one photon is transferred from the light to the atom; the photon vanishes and the atom is said to absorb it.
For an object consisting of many atoms, there can be many photon absorptions (such as with sunglasses) or photon emissions (such as with lamps).
In classical physics, such events involve so much light that we had no need of quantum physics.
"},{"location":"GP/light/quantum%20light/quantum/#photon-the-quantum-of-light","title":"Photon, the Quantum of Light","text":"Back to the Photoelectric Effect\uff0cthe electrons within the target are held by electric forces. To just escape from the target, an electron must pick up a certain minimum energy \\(W\\) , where \\(W\\) is a property of the target material called its work function.
The energy that can be transferred from the incident light to an electron in the target is that of a single photon \\(hf\\) . According the conservation of energy, the kinetic energy K acquired by the electron satisfies. \\(hf = K + W\\) .
In the most favorable circumstance, the electron can escape through the surface without losing any of this kinetic energy in the process. (i.e. \\(K_{max} = hf \u2212 W\\) ).
Increasing the light intensity increases the number of photons in the light, not the photon energy, so the energy transferred to the kinetic energy of an electron is also unchanged. This solves puzzle #1.
If the energy hf transferred to an electron by a photon exceeds the work function of the material (if \\(hf > W\\) ), the electron can escape the target. If the energy transferred does not exceed the work function (that is, if \\(hf < W\\) ), the electron cannot escape. This solves puzzle #2.
"},{"location":"GP/light/quantum%20light/quantum/#photon-momentum-and-compton-scattering","title":"Photon Momentum and Compton Scattering","text":"A photon, or a light quantum, is a particle with energy \\(E = hf\\) . It has a velocity of the speed of light c, but no mass (m = 0). In 1916, Einstein extended his concept of photons by proposing that a quantum of light has linear momentum.
According to the theory of relativity, \\(E^2 \u2212 c^2p^2 = m^2c^4 = 0\\) the magnitude of the photon momentum is. \\(p = \\frac{hf}{c} = \\frac{h}{\\lambda} = \\bar{h}k\\)
"},{"location":"GP/light/quantum%20light/quantum/#compton-scattering","title":"Compton Scattering","text":"When a photon interacts with matter, energy and momentum are transferred, as if there were a collision between the photon and matter in the classical sense.
To demonstrate, Arthur Compton measured the wavelengths and intensities of a beam of X rays that were scattered in various directions from a carbon target.
Compton found that although there is only a single wavelength \\((\u03bb = 71.1 pm)\\) in the incident X-ray beam, the scattered X rays contain a range of wavelengths with two prominent intensity peaks.
In classical physics, an electron in the carbon target undergoes forced oscillations in the sinusoidally oscillating electromagnetic wave. Hence, the electron should send out scattered waves at the same frequency.
With quantum physics and relativity, the energy and momentum conservation becomes \u300cImportant\u300d
\\(\\begin{align*} \\frac{hc}{\\lambda}+mc^2&=\\frac{hc}{\\lambda^{'}}+\\gamma mc^2 \\ (energy) \\\\ \\frac{h}{\\lambda}&=\\frac{h}{\\lambda'}cos\\phi+\\gamma mvcos\\theta\\ (momentun\\ in\\ x) \\\\ 0&=\\frac{h}{\\lambda'}sin\\phi-\\gamma mvcos\\theta\\ (momentun\\ in\\ y)\\end{align*}\\)
where \\(\\gamma =\\sqrt{\\frac{1}{1-\\frac{v^2}{c^2}}}\\)
The quantity \\(h/mc\\) is a constant called the Compton wavelength. Its value depends on the mass m of the particle from which the X rays scatter.
Strictly speaking, the particle can be a loosely boundelectron, or a carbon atom (with tightly bound electrons).
For an electron, the Compton wavelength is \\(\\frac{h}{mc}= \\frac{hc}{mc^2} =\\frac{12400eV\u00b7A\u030a}{511000eV}=2.426pm\\)
For a carbon atom, the Compton wavelength is \\(12 \u00d7 m_u /m_e \u2248 12 \u00d7 1836 \u2248 22, 000\\) times smaller and, hence, can be neglected. Therefore, there is a peak at the incident wavelength at any angle.
According to the quantum-mechanical description, a photon also has an intrinsic spin angular momentum, which is either \\(\u2212\\bar{h}\\) or \\(+\\bar{h}\\), where the signs indicate right- or left-handedness, respectively.
Whenever a charged particle emits or absorbs electromagnetic radiation, along with changes in its energy and linear momentum, it will undergo a change of \\(\u00b1\\bar{h}\\) in its angular momentum.
The energy transferred to a target by an incident monochromatic electromagnetic wave can be envisaged as being transported in the form of a stream of identical photons.
A beam of linearly polarized light will interact with matter as if it were composed, at that instant, of equal numbers of right- and left-handed photons. There is a subtle point. Strictly speaking, we cannot say that the beam is actually made up of precisely equal amounts of well-defined right- and left-handed photons; the photons are all identical.
Rather, each individual photon exists in either spin state with equal likelihood.
\\(|H\u27e9 =\\frac{|R\u27e9+|L\u27e9}{\\sqrt{2}} = \\frac{1}{\\sqrt{2}}[\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\-i\\end{pmatrix} +\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\i\\end{pmatrix}]\\)
"},{"location":"GP/light/quantum%20light/quantum/#appendix-21a-quantum-key-distribution","title":"Appendix 21A: Quantum Key Distribution","text":"Goal: To send encrypted messages that cannot be understood by anyone but the designated recipient.
Message is a whole number \\(m\\) (e.g., represented by the dots and dashes of Morse code as ones and zeros.)
An encryption is a function \\(f : m \u2192 f (m)\\), agreed on between Alice (sender) and Bob (recipient) but unknown to Eve (a possible eavesdropper).
Problem: If the same encryption is used many times, Eve can usually deduce the nature of the encryption and read the messages (by, e.g., frequency analysis).
Classical solution: Let the encryption depend on a frequently changed key, which can be regarded as another whole number \\(k\\). The encrypted message is now \\(f (m, k)\\). New problem: Alice and Bob must frequently exchange messages to establish new keys, and these new messages too may be intercepted by Eve. Quantum solution: It is not possible to measure any quantity without changing an unknown state vector to one in which that quantity has some definite value.
"},{"location":"GP/light/quantum%20light/quantum/#the-bb84-protocol-bennet-brassard-1984","title":"The BB84 Protocol (Bennet & Brassard, 1984)","text":"A protocol to establish a secured communication channel. More concretely, a scheme to enables us to detect the presence of an eavesdropper using polarized photons.
What Can Eve Do?
What Eve really wants is that Alice and Bob should establish a key that Eve knows, so that she can secretly read the messages sent from Alice to Bob.
So Eve can intercept the photons sent by Alice, measure their polarizations, and then send substitute photons with these polarizations on to Bob.
But Eve, like Bob, does not know the mode that Alice is using in choosing each photon polarization. So there is only a 50% chance that the substitute photon sent by Eve to Bob will have the same polarization that it had when it was sent by Alice.
Can Alice and Bob Detect Eve?
When Alice and Bob compare notes, they identity the photons that had been sent when Alice and Bob had by chance being used the same modes.
Eve too may learn this information, but by then it is too late. There is only a 50% chance that Eve had used the same mode that Alice (and Bob) had used.\u300c\u6d4b\u4e0d\u51c6\u300d
If Eve had used a different mode, there is still a 50% chance that Bob would have observed the same polarization that had been sent by Alice.
Overall, Alice and Bob had 25% of the binary digits in the key that do not match; thus, they can detect Eve\u2019s intervention by comparing a part of the key.
After all, polarization is a classical concept. Which part of the BB84 depends crucially on the quantum concept and can be defeated in the classical world?
Quantum no-cloning theorem: It is impossible to create an identical copy of an arbitrary unknown quantum state. Where is it needed?
"},{"location":"GP/mag/mag/#the-magnetic-field","title":"The Magnetic Field","text":"
If the velocity of a charged particle has a component parallel to the uniform magnetic field, such that :
\\(v_{||}=vcos\\phi\\)
\\(v_{\u22a5}=vsin\\phi\\)
\\(d\\vec{B}= \\frac{\\mu_0}{4\\pi}\\frac{id\\vec{s}\\times\\vec{r}}{r^3}\\)
Where the constant \\(\\mu_0=4\\pi \\cdot 10^{-7(T\\cdot m/A)}\\)is called the permeability constant
"},{"location":"GP/mag/mag/#example","title":"Example","text":""},{"location":"GP/mag/mag/#a-long-straight-wire","title":"A Long Straight Wire","text":"\\(\\begin{align*}d\\vec{B}&=\\frac{\\mu_0}{4\\pi}\\frac{id\\vec{s}\\times\\vec{r}}{r^3}\\\\ &=\\frac{\\mu_0}{4\\pi}\\frac{id\\vec{s}\\times\\vec{R}}{r^3} \\\\B&=\\frac{\\mu_0i}{4\\pi R}\\int_{-\\infty}^{+\\infty}\\frac{R^2dS}{r^3} (sin\\theta=R/r \\ cos\\theta=-s/r \\ dr/ds=s/r)\\\\ &=\\frac{\\mu_0i}{4\\pi R}\\int_{0}^{\\pi}sin\\theta d\\theta \\ (cos\\theta d\\theta = d(sin\\theta)=-\\frac{R^2dr}{r^2ds}ds=-\\frac{R^2s}{r^2r}ds=cos\\theta \\frac{Rds}{r^2})\\\\ &= \\frac{\\mu_0 i}{2\\pi R}\\end{align*}\\)
"},{"location":"GP/mag/mag/#force-between-two-parallel-wires","title":"Force Between Two Parallel Wires","text":""},{"location":"GP/mag/mag/#magnetic-field-circulation","title":"Magnetic Field Circulation","text":"\\(Circulation =\\oint \\vec{B}\\cdot d\\vec{s} = \\mu_0 i_{enc}\\) (Amperian Loop)
In the limiting case of an ideal solenoid, which is infinitely long and consists of tightly packed (close-packed) turns of square wire, the field inside the coil is uniform and parallel to the solenoid axis. The magnetic field outside the solenoid is zero.
The direction of the magnetic field along the solenoid axis is given by a curled-straight right-hand rule: Grasp the solenoid with your right hand so that your fingers follow the direction of the current in the windings; your extended right thumb then points in the direction of the axial magnetic field.
A solenoid thus provides a practical way to set up a known
uniform magnetic field for experimentation, just as a parallel-plate capacitor provides a practical way to set up a known uniform electric field.
"},{"location":"GP/mag/mag/#magnetic-field-of-a-toroid","title":"Magnetic Field of a Toroid","text":"In contrast to the situation for a solenoid, B is not constant over the cross section of a toroid.
One can show, with Ampere\u2019s law, that B = 0 for points outside an ideal toroid (as if the toroid were made from an ideal solenoid).
"},{"location":"GP/mag/mag/#the-curl-of-vecb","title":"The Curl of \\(\\vec{B}\\)","text":"For volume currents, the Biot-Savart law becomes
\\(\\vec{B}(x,y,z)=\\frac{\\mu_0}{4\\pi}\\iiint\\frac{\\vec{J}(x',y',z')\\times \\vec{r}}{r^3}dx'dy'dz'\\)
The length element \\(id\\vec{s}\\) is replace by the volume element \\(JdV'= \\vec{J}(x',y',z')dx'dy'dz'\\) and \\(r =(x \u2212 x' ) \\hat{x} + ( y \u2212 y' )\\hat{y} + ( z \u2212 z') \\hat{z}\\)
Applying the divergence, we obtain:
\\(\\triangledown \\cdot \\vec{B}=\\frac{\\mu_0}{4\\pi}\\int \\triangledown (\\frac{\\vec{J}\\times \\vec{r}}{r^3})dV'=-\\frac{\\mu_0}{4\\pi}\\int \\vec{J} (\\triangledown \\times \\frac{\\vec{r}}{r^3})dV'\\)
It does not twists around; it only spreads out. Its curl is zero (as known in electrostatics).
Thus \\(\\triangledown \\cdot \\vec{B} = 0\\)
Constructing a closed Gaussian surface,we have:
\\(\\oint\\vec{B}\\cdot d\\vec{A}=\\int(\\triangledown\\cdot\\vec{B})dV=0\\)
The law asserts that the net magnetic flux \\(\\Phi _B\\) through any closed Gaussian surface is zero.
This is a formal way of saying that magnetic monopoles do not exist. The simplest magnetic structure that can exist is a magnetic dipole.
\\(\\vec{\\mu}=Ni\\vec{A}\\)
\\(\\tau = \\vec{\\mu} \\times \\vec{B}=-\\mu Bsin\\theta=-\\frac{\\partial}{\\partial \\theta}(-\\mu Bcos\\theta)\\)
\\(U_B=-\\vec{\\mu}\\cdot\\vec{B} = -\\mu Bcos\\theta\\)
"},{"location":"GP/mag/mag/#magnetic-field-of-a-circular-arc-of-wire","title":"Magnetic Field of a Circular Arc of Wire","text":""},{"location":"GP/mag/mag/#at-the-center","title":"At the center","text":"Which means \\(B(z)=\\frac{\\mu_0}{2\\pi}\\frac{\\mu}{r^3}\\) Just like the electric dipole
"},{"location":"GP/mag/mag/#magnetic-materials","title":"Magnetic Materials","text":""},{"location":"GP/mag/mag/#paramagnetism","title":"Paramagnetism","text":"Paramagnetism occurs in materials whose atoms have permanent magnetic dipole moments \\(\\vec{\\mu}\\)
Explain:
"},{"location":"GP/mag/mag/#diamagnetism","title":"Diamagnetism","text":"Diamagnetism occurs in all materials, but the weak effect is only observable in materials having atomic dipole moments of zero.
Such a material can be modeled by equal numbers of electrons orbiting counterclockwise or clockwise. An external magnetic field will either accelerate or decelerate these electrons, leading to a net magnetic dipole moment
"},{"location":"GP/mag/mag/#ferromagnetism","title":"Ferromagnetism","text":"A ferromagnet has strong, permanent magnetism. What distinguishes ferromagnets from paramagnets is that there is a strong interaction between neighboring atoms.
The interaction keeps the dipole moments of atoms aligned even when the magnetic field is removed.
"},{"location":"GP/matter%20wave/matter/","title":"matter wave","text":""},{"location":"GP/matter%20wave/matter/#matter-waves","title":"Matter Waves","text":""},{"location":"GP/matter%20wave/matter/#two-versions-of-interference","title":"Two Versions of Interference","text":""},{"location":"GP/matter%20wave/matter/#standard-version-of-the-double-slit-experiment","title":"standard version of the double-slit experiment","text":"
In the standard version of the double-slit experiment(Thomas Young, 1801), light shines on screen B, which contains two narrow parallel slits.
The light waves emerging from the two slits spread out by diffraction and overlap on screen C where, by interference, they form a pattern of alternating intensity maxima and minima.
The point of this thought experiment is as follows.
We know that the intensity \\(I\\) of a light wave at any point is proportional to the square of \\(E_m\\). Thus, the probability(per unit time interval) that a photon will be detected in any small volume centered on a given point in a light wave is proportional to the square of the amplitude of the wave\u2019s electric field vector at that point.
"},{"location":"GP/matter%20wave/matter/#single-photon-version","title":"single-photon version","text":"The light source is so extremely feeble that it emits only one photon at a time, at random intervals. Astonishingly, interference fringes still build up on screen C if the experiment runs long enough
Because an interference pattern eventually builds up on the screen, we can only speculate that each photon travels from source to screen as a wave that fills up the space between source and screen.
We can predict the probability that an energy transfer will occur at any given point on the screen.
Thus, we can say that the wave traveling from the source is a probability wave, which produces a pattern of \u201cprobability fringes\u201d on the screen.
"},{"location":"GP/matter%20wave/matter/#light-as-a-probability-wave","title":"Light as a Probability wave","text":"We see that the double-slit experiments tell us that
The probability density of detecting a photon at some point \\(P\\) in space depends on the irradiance \\(I \u221d E_0^2\\) at that point. Thus, the net \\(E_0\\) at \\(P\\) can be interpreted as the probability amplitude. (Probability amplitude is a quantity whose square gives the probability density.)
To go further, one will need quantum electrodynamics (QED), the quantum theory of the interaction of light and matter.
"},{"location":"GP/matter%20wave/matter/#extendtions-to-not-only-light","title":"Extendtions to not only light","text":""},{"location":"GP/matter%20wave/matter/#de-broglie-hypothesis","title":"De Broglie Hypothesis","text":"A beam of light is a wave, but it transfers energy and momentum to matter only at points, via photons. Electron is a particle with energy and momentum. Why can\u2019t we think of a beam of moving electron \u2014 or any other particle \u2014 as a matter wave?
In 1924, Louis de Broglie proposed that one could assign a wavelength \\(\u03bb\\) to a particle with momentum of magnitude \\(p\\). Like that of photons, we define. \\(\u03bb = h/p\\), which is known as the de Broglie wavelength of the moving particle.
"},{"location":"GP/matter%20wave/matter/#electron-diffraction","title":"Electron Diffraction","text":"Electron diffraction and neutron diffraction are used to study the atomic structures of solids and liquids, and electron diffraction is often used to study the atomic.
"},{"location":"GP/matter%20wave/matter/#the-interference-of-electrons","title":"The Interference of Electrons","text":"In a more recent experiment, an interference pattern was built up when electrons were sent, one by one, through a double-slit apparatus. When an electron hit the viewing screen, it caused a flash of light whose position was recorded.
Do particles interfere with each other?
Perhaps they collide . . . .
In Newtonian physics: a particle is only aware of the slit through which it goes, it has no idea how many other slits are open or closed or even exist. Therefore, when both slits are open, \\(P_{12} = P_1 + P_2\\).
Consider matter waves:The electrons arrive in lumps, like particles, and the probability of arrival of these lumps is distributed like the distribution of intensity of a wave.
Introduce:
Add Amplitude \\(A\\)
Introduce the probability amplitude \\(\u03c8\\) which is a complex number.
The probability of an event in an ideal experiment is then given by \\(|\u03c8|^2 = \u03c8^\u2217\\times\u03c8\\).
When an event can occur in several alternative ways, the probability amplitude for the event is the sum of the probability amplitudes for each way considered separately
\\(\u03c8 = \u03c8_1 + \u03c8_2 + \u00b7 \u00b7 \u00b7\\).
The probability for the event is, then,\\(P=|\u03c8|^2 =|\u03c81|^2+|\u03c82|^2+2R(\u03c81^\u2217\u03c82)+\u00b7\u00b7\u00b7.\\)\uff08note:\u6a21\uff09
The interference term \\(2R(\u03c8_1^\u2217\u03c8_2)\\) is responsible for the rapid oscillations of \\(P\\) observed in the experiment.
If an experiment is performed which is capable of determining whether one or another alternative is actually taken, the interference is lost.
The experiment tells us that the probability of the event in the which-way experiment is the sum of the probabilities for each alternative, \\(P = |\u03c8|^2 = |\u03c8_1|^2 + |\u03c8_2|^2\\),just as what happens in the classical case.
Thus an electron acts like it went through one particular slit if we see it doing that, and acts like it did not have a specific path (through a specific slit) when it is not seen.
To see an electron with a resolution comparable to slit separation \\(d\\), (so we know which slit it took) requires light with \\(\u03bb < d\\), this is just standard wave theory.
But, the light is made of photons each with momentum \\(p>\\frac{h}{d}\\). So, measuring the position of the electron has made us disturb its momentum.
The amount of momentum transferred to the electron in the act of observation is indefinite.
This is a manifestation of Heisenberg\u2019s uncertainty principle, which says you cannot measure the momentum and coordinate of a particle simultaneously to arbitrary accuracy.
"},{"location":"GP/matter%20wave/matter/#heisenbergs-uncertainty-principle","title":"Heisenberg\u2019s Uncertainty Principle","text":"Heisenberg\u2019s uncertainty principle is not an effect caused by the measurement but a consequence of intrinsic wave nature of matter.
The probabilistic nature of quantum physics places an important limitation on detecting a particle\u2019s position and momentum. That is, it is not possible to measure the position \\(\\vec{r}\\) and the momentum \\(\\vec{p}\\) of a particle simultaneously with unlimited precision.
The uncertainties in the components of these quantities are given by (Werner Heisenberg, 1927)
\\(\u2206x\u00b7\u2206p_x \u2265\\bar{h}\\)
\\(\u2206y\u00b7\u2206p_y \u2265\\bar{h}\\)
\\(\u2206z\u00b7\u2206p_z \u2265\\bar{h}\\)
They are due to the fact that electrons and other particles are matter waves and that repeated measurements of their positions and momenta involve probabilities, not certainties.
In the statistics of such measurements, we can view, say, \\(\u2206x\\) and \\(\u2206p_x\\) as the spread (actually, the standard deviations) in the measurements. Couldn\u2019t we very precisely measure \\(p_x\\) and then next very precisely measure x wherever the electron happens to show up?
Doesn\u2019t that mean that we have measured both \\(p_x\\) and \\(x\\) simultaneously and very precisely?
No, the flaw is that although the first measurement can give us a precise value for \\(p_x\\) , the second measurement necessarily alters that value. Puzzled?
Consider an electron with a certain value of \\(k\\), which, by the de Broglie relationship, means a certain momentum \\(p_x = \\bar{h}k\\). Thus, \\(\u2206p_x = 0\\). By Heisenberg\u2019s uncertainty principle, that means that \\(\u2206x \u2192 \u221e\\).
What form does the electron wave function have? The naive guess could be \\(sinkx\\), or \\(coskx\\).
But they have spatial variance that is inconsistent with our intuition.
To represent the electron by a wave, we need a function, which is called wave function, with a wavelength \\(\u03bb = 2\u03c0/k\\), but its square should not show any variation in x\u300cNote: Definite Momentum\u300di.e. \\(\u03c8(x,t) = e^{i(kx\u2212\u03c9t)}\\).
To calculate the square of the wave function \\(\u2223\u03c8\u2223^2\\), we obtain \\(\u2223\u03c8\u2223^2=e^{i(kx\u2212\u03c9t)}\u22c5e^{\u2212i(kx\u2212\u03c9t)}\\).
we then have \\(\u2223\u03c8\u2223^2=1\\).
This implies that at a given time t, the square of the wave function is a constant in the x direction, with no variation concerning x.
Now, where is the electron? Why complex?
but the particle exists anywhere with the same probability, so \\(\u2206x = \u221e\\). ?Question
Heisenberg\u2019s uncertainty principle is not violated.
Now, is the momentum still \\(p_x = \\bar{h}k\\)?
The Fourier transform of the \u03b4-function tells us \\(\u03c8 \u0304(p) = F(\u03c8(x)) = constant\\).
In this case, \\(\u2206x = 0\\) but \\(\u2206p = \u221e\\). Again, Heisenberg\u2019s uncertainty principle is not violated.
"},{"location":"GP/matter%20wave/matter/#fourier-transform","title":"Fourier Transform","text":"https://www.youtube.com/watch?v=spUNpyF58BY&list=RDCMUCYO_jab_esuFRV4b17AJtAw&index=3
https://youtube.com/watch?v=MBnnXbOM5S4
"},{"location":"GP/sch/sch/","title":"Schroedinger's Equation","text":""},{"location":"GP/sch/sch/#motivation","title":"Motivation","text":"
A classical wave \\(\u03c8(x, y, z, t)\\) satisfies the wave equation \\(\\frac{1}{v^2}\\frac{\\partial^2\u03c8}{\\partial^2t}=\\triangledown^2\u03c8\\) \\(v \\ is\\ speed\\)
In the quantum theory, a microscopic particle is described by a probability amplitude \\(\u03c8(x, y, z, t)\\), and the probability of finding it is proportional to \\(P(x, y, z, t) = |\u03c8(x, y, z, t)|^2\\).
Find the equation that governs the quantum behavior of a free particle of mass \\(m\\) represented by a wave \\(\u03c8(x,t) = e^{i(kx\u2212\u03c9t)}\\).
\\(E=\\frac{p^2}{2m}\\)
According to the de Broglie\u2019s hypothesis
\\(p=\\frac{h}{\\lambda}=\\bar{h}k=\u2212i\\bar{h}\\frac{1}{\u03c8(x,t)} \\frac{\u2202\u03c8(x,t)}{\\partial x}\\)
d\\(p^2=\\bar{h}^2k^2=-\\bar{h}^2\\frac{1}{\u03c8(x,t)} \\frac{\u2202^2\u03c8(x,t)}{\\partial^2 x}\\)
\\(E=hv=\\bar{h}w=\\frac{1}{\u03c8(x,t)} \\frac{\u2202\u03c8(x,t)}{\\partial t}\\)
using wave function, the energy-momentum relation is
In the presence of potential, e.g. a harmonic potential \\(U(x) = ax^2/2\\) , the classical relation is modified to \\(E=\\frac{p^2}{2m} +U(x)\\) where \\(E\\) is a constant of motion, but \\(p\\) is not. In other words, a plane wave is not a solution any more.
In this expression, p is the momentum of the particle, m is the mass of the particle, \\(U(x)\\) is the potential energy associated with the position x, and E is the total energy of the system, which remains constant.
In the conventional case where there is no potential energy in the system, momentum p would be a conserved quantity, implying the total momentum of the system remains constant. However, in the presence of potential energy, the interaction between kinetic and potential energy leads to a non-conservation of momentum. In such cases, simple waveforms like plane waves are no longer solutions, as the interaction between kinetic and potential energy requires a more complex description.
Schroedinger's equation is both homogenous and linear!!!
Linear Partial Differential Equation (PDE):
A partial differential equation is termed linear if it is linear in terms of the unknown function and its partial derivatives. It can be expressed in the form:
\\(L(u)=a_n(x,y)u_{xx}+a_{n\u22121}(x,y)u_x+\u2026+a_1(x,y)u_y+a_0(x,y)u+F(x,y)=0\\)
Here, \\(u\\) is the unknown function, \\(u_x, u_y, u_{xx}\\), and so on represent the corresponding partial derivatives
Crucially, all coefficients \\(a_n(x,y), a_{n\u22121}(x,y), \u2026\u2026, a_0(x,y)\\)as well as \\(F(x,y)\\), do not depend on the values of the unknown function \\(u\\) or its derivatives.
Homogeneous Partial Differential Equation (PDE):
A partial differential equation is considered homogeneous if all terms in the equation are homogeneous polynomials of the unknown function and its partial derivatives.
NOTE\uff1ahomogeneous without \u201dconstant term\u201d
"},{"location":"GP/sch/sch/#time-independent-schroedingers-equation","title":"time-independent Schroedinger\u2019s equation","text":"In most cases we discuss, the potential energy \\(U = U(x)\\) is independent of time.
We can solve the stationary solution of the Schroedinger equation by an ansatz: \\(\u03c8(x, t) = \\Phi(x)e^{\\frac{-iEt}{\\bar{h}}}\\).
Substituting the above ansatz of \\(\u03c8(x,t)\\), we obtain the following equation for \\(\u03c6(x)\\) \\(\\frac{\\partial^2\\phi(x)}{\\partial^2x}+\\frac{2m}{\\bar{h}^2}[E-U(x)]\\phi(x)=0\\)
By solving this equation, we can obtain the stationary solution \u03c6\\((x)\\) under the time-independent external potential \\(U(x)\\).
In free space, \\(U(x) = 0\\).
The general form of a second-order homogeneous linear ordinary differential equation (ODE) with constant coefficients is given by:
\\(a\\frac{d^y}{dx^2}+b\\frac{dy}{dx}+cy=0\\)
The general solution for such equations can be found by assuming the solution has the form \\(y=e^{rx}\\) where r is a constant to be determined. Substituting this form into the equation yields the characteristic equation:\\(ar^2+br+c=0\\)
Solving this quadratic equation for rr determines the values for r, which in turn determine the form of the general solution. There are typically three cases:
The general solution is: \\(y=C_1e^{r_1x}+C_2e^{r_2x}\\)
The general solution is: \\(y=(C_1+C_2x)e^{rx}\\)
The general solution is: \\(y=e^{\u03b1x}(C_1cos\u2061(\u03b2x)+C_2sin\u2061(\u03b2x))\\)
"},{"location":"GP/sch/sch/#the-complete-time-dependent-wave-function","title":"The complete time-dependent wave function","text":"\\(\u03c8(x,t)=Ae^{i(kx\u2212\u03c9t)} +Be^{\u2212i(kx+\u03c9t)}\\) where \\(w=\\frac{E}{\\bar{h}}\\)
Consider the right-moving wave \\(\u03c8(x,t) = Ae^{i(kx\u2212\u03c9t)}\\), the probability density is uniform:\\(|\u03c8(x, t)|^2 = \u03c8^\u2217(x, t)\u03c8(x, t) = |A|^2\\). That means that if we make a measurement to locate the particle, the location could turn out to be at any x value.
"},{"location":"GP/sch/sch/#wave-packets","title":"Wave Packets","text":"What is the speed of a free quantum mechanical particle?
\\(\u03c8(x,t)=Ae^{i(kx\u2212\\frac{\\bar{h}k^2}{2m}t)} +Be^{\u2212i(kx+\\frac{\\bar{h}k^2}{2m}t)}\\) where \\(w=\\frac{E}{\\bar{h}}\\) and \\(\\sqrt{2mE} /\\bar{h}\\)
\\(\\Rightarrow v_{ph}=\\frac{\\bar{h}k}{2m}=\\sqrt{\\frac{E}{2m}}\\)
On the other hand, the classical speed of a free particle with energy E is given by \\(v_{cl} = \\sqrt{\\frac{2E}{m}}= 2v_{ph}\\).
Problem #1:
The quantum mechanical wave function travels at half the speed of the particle it is supposed to represent!
Problem #2:
How to normalize the wave function of the free particle, say, represented by \\(Ae^{ikx}\\) ?
\\(\\int_{-\\infty}^{+\\infty}|\u03c8(x)|^2dx=|A|^2\\int_{-\\infty}^{+\\infty}1dx=|A|^2\\infty\\)
This wave function is not normalizable!
In fact, the stationary (separable) solutions do not represent physically realizable states for a free particle; there is no such thing as a free particle with a definite energy.
"},{"location":"GP/sch/sch/#the-realistic-solution-to-the-schroedinger-equation-for-a-free-particle","title":"the realistic solution to the Schroedinger equation for a free particle","text":"In quantum theory, a localized particle is modeled by a linear superposition of these stationary free-particle (or plane-wave) states.
In general, we can construct a linear combination (integral over continuous \\(k\\))
\\(\u03a8(x,t)=\\frac{1}{\\sqrt{2\\pi}}\\int_{-\\infty}^{+\\infty}\\)\u03c6\\((k)e^{i(kx\u2212\\frac{\\bar{h}k^2}{2m}t)}dk\\)
This wave function can be normalized for appropriate \u03c6\\((k)\\), typically Gaussian. We call it a wave packet, which carries a range of k and, hence, a range of energies and speeds. In a general quantum problem, we are given \\(\u03a8(x, 0)\\) and needed to find \\(\u03a8(x, t)\\).
The particle can be better localized (\u2206x can be decreased) if more plane-wave states of different wavelengths or momenta are added together in the right way (\u2206p is increased).
According to Heisenberg, these uncertainties obey \\(\u2206x\u2206p \u2265 \\bar{h}/2\\).
It turns out that the group velocity of the wave packet, not the phase velocity of the stationary states, matches the classical particle velocity.
"},{"location":"GP/sch/sch/#some-exercises","title":"Some Exercises","text":""},{"location":"GP/sch/sch/#reflection-from-a-potential-step","title":"Reflection from a Potential Step","text":"Consider a beam of nonrelativistic electrons, each of total energy E, along an x axis through a narrow tube. They experience a negative electric potential step of height \\(V_b <0\\) at \\(x=0\\).(Note that the charge q of the electron is negative)
"},{"location":"GP/sch/sch/#e-qv_b","title":"\\(E > qV_b\\).","text":"Classically, electrons should all pass through the boundary. Their total energy should be conserved, so their kinetic energy, hence speed, decreases when their potential energy increases.
(at a given time?)
We can first set \\(D = 0\\), because there is no electron source off to the right, and there can be no electrons moving to the left in region 2.
We now consider boundary conditions at \\(x = 0\\):
\\(A + B = C\\) (matching of values)
\\(Ak \u2212 Bk = Ck_b\\) (matching of slopes) \u300cfirst derivation\u300d
We should be able to solve \\(B/A\\) and \\(C/A\\), but not \\(A\\), \\(B\\), and \\(C\\).
Note that the absolute values are not important for our purpose (it can be related to the beam intensities, though).
\\(R_{reflected}=\\frac{|A|^2}{|B|^2}=|\\frac{k-k_b}{k+k_b}|^2\\)
\\(T_{transmition}=1-R=\\frac{4kR(k_b)}{|k+k_b|^2}\\)
Quantum mechanically, electrons are reflected from the boundary, but only with a probability.
What inspires us to define T in this way?
Well, one can consider an alternative quantity \\(\\frac{|C|^2}{|A|^2}=\\frac{4k^2}{|k+k_b|^2}=\\frac{k}{R(k_b)}T=\\frac{k}{k_b}T for\\ real\\ k_b\\)(which means\\(E > qV_b\\). )
Why coefficient?
\\(T = \\frac{|C|^2k_b}{|A|^2k} = \\frac{|C|^2q(\\bar{h}k_b/m)}{|A|^2q(\\bar{h}k/m)} =\\frac{n_1qv_1}{n_2qv_2}= \\frac{J_{transmitted}}{J_{incident}}\\).
One can also write
\\(R=\\frac{J_{reflected}}{J_{incident}}\\)
Therefore, \\(T = 1 \u2212 R\\) is nothing but the conservation of current \\(J_{transmitted} = J_{incident} \u2212 J_{reflected}.\\)
"},{"location":"GP/sch/sch/#e-qv_b_1","title":"\\(E < qV_b\\).","text":"Now consider a potential energy barrier, which is a region of thickness L where the electric potential is \\(V_b(<0)\\) and the barrier height is \\(U_b=qV_b\\)
Separate the space into three regions and solve Schroedinger's equation in each region \\((3\\times2-1=5\\ unknowns)\\)
Apply boundary conditions at the two boundaries\\((2\\times2=4)\\)
Calculate the tunneling coefficient.
"},{"location":"GP/sch/sch/#general-results","title":"General Results","text":"The oscillations occur because these two waves, traveling in opposite directions, interfere with each other, setting up a standing wave pattern.
Within the barrier \\((for\\ 0 < x < L)\\) the probability density decreases exponentially with x. However, if L is small, the probability density is not quite zero at x = L..
To the right of the barrier (for x > L), the probability density plot describes a transmitted wave (through the barrier) with low but constant amplitude.
We can assign a transmission coeffient T to the incident matter wave and the barrier. The transmission coefficient T is approximately \\(T\\approx e^{-2kL}\\) where \\(k=\\frac{\\sqrt{2m(qV_b-E)}}{\\bar{h}}\\) Try Derive (imaginary k' actually) \\(T\\) is sensitive to L,m and \\(U_b-E\\)
"},{"location":"GP/sch/sch/#scanning-tunneling-microscope-stm","title":"Scanning Tunneling Microscope (STM)","text":"The size of details that can be seen in an optical microscope is limited by the wavelength of the light the microscope uses (about 300 nm for ultraviolet light). We use electron matter waves (tunneling through potential barriers) to create images on the atomic scale.
The piezoelectric effect refers to the phenomenon where certain crystals exhibit charge separation when subjected to mechanical stress, resulting in the generation of voltage, or conversely, when an electric field is applied, causing the crystal to undergo mechanical deformation. This phenomenon is known as the piezoelectric effect, and the crystals that exhibit it are referred to as piezoelectric crystals.
Specifically, the piezoelectric effect can be categorized into two main types:
A fine metallic tip, mounted on quartz rods, is placed close to the surface to be examined. The space between the surface and the tip forms a potential energy barrier.
Quartz shows the piezoelectricity.
The position of the tip can be controlled by applying the electric field on the quartz. The horizontal location of the tip is controlled by the electric field in x- and y-directions. The vertical distance between the surface and the tip is controlled by the electric field in z-direction.
Transmission coefficient \\(T\\) ~ \\(e^{-2kL}\\) is sensitive to the barrier width.
\\(\u2192\\) Tunneling current is sensitive to the distance between the surface and the tip.
An STM not only can provide an image of a static surface, it can also be used to manipulate atoms and molecules on a surface.
"},{"location":"GP/sch/sch/#s-matrix","title":"S-Matrix","text":"The expression \\(|r|^2+|t|^2=1\\) is a consequence of probability conservation in quantum mechanics. Let's break down the meaning of this expression in the context of quantum mechanics and scattering theory.
In quantum mechanics, when a particle encounters a potential barrier or undergoes scattering, the probabilities of reflection\\(|r|^2\\) and transmission\\(t|^2\\)must add up to unity. Here's why:
Mathematically, this is expressed as \\(|r|^2+|t|^2=1\\)
The normalization condition for probability amplitudes requires that the sum of the probabilities over all possible outcomes is equal to 1.
Mathematically, this normalization condition is expressed as \\(\u222b\u2223\u03a8(x)\u2223^2\u2009dx=1\\), where \\(\u03a8(x)\\) is the wave function.
"},{"location":"GP/sch/sch/#example","title":"Example","text":"\\(Explain\\ for\\ the\\ below\\ Pic\\)
\\(t_1\\) and \\(t_2\\): These are the transmission coefficients at the first and second barriers, respectively. They represent the fraction of the wave that gets transmitted through the barriers.
\\(r_1\\) and \\(r_2\\): These are the reflection coefficients at the first and second barriers, respectively. They represent the fraction of the wave that gets reflected at the barriers.
\\(e^{i\u03b8}\\): This term introduces a phase shift (\u03b8) to the wave.
The term is a complex exponential that represents a phase change in the wave. It's common in wave optics and quantum mechanics to introduce phase factors.
Now, let's interpret the terms in the sum:
The first term \\(t_1e^{i\u03b8}t_2\\) represents the wave being transmitted through the first barrier (\\(t_1\\)), undergoing a phase change (\\(e^{i\\theta}\\)), and then being transmitted through the second barrier \\((t_2)\\)
The second term \\(t_1e^{i\\theta}r_2e^{i\\theta}r_1e^{i\\theta}t_2\\) represents a more complex scenario.
The wave is transmitted through the first barrier (\\(t_1\\)), undergoes a phase change (\\(e^{i\\theta}\\)), gets partially reflected \\(r_2\\), undergoes another phase change\\(e^{i\\theta}\\), gets reflected again\\((r_1)\\), undergoes yet another phase change\\((e^{i\\theta})\\), and finally gets transmitted through the second barrier \\(t_2\\).
\\(b_4b_4^\u2217=\\frac{t_{1}^2t_2^2}{1\u22122r_1r_2cos\u2061(2\u03b8)+r_1^2r_2^2}\\)
"},{"location":"GP/sch/sch/#two-particle-system","title":"Two Particle System","text":"https://www.youtube.com/watch?v=M7VJa5eim5o
https://phys.libretexts.org/Bookshelves/Quantum_Mechanics/Introductory_Quantum_Mechanics_(Fitzpatrick)/05%3A_Multi-Particle_Systems/5.03%3A_Two-Particle_Systems
\\(i\\bar{h}\\frac{\\partial}{\\partial t}\u03c8(x_1,x_2,t)=[-\\frac{\\bar{h}^2}{2m_1}\\frac{\\partial^2}{\\partial x_1^2}-\\frac{\\bar{h}^2}{2m_2}\\frac{\\partial^2}{\\partial x_2^2}+V(x_1,x_2)]\u03c8(x_1,x_2,t)\\)
"},{"location":"NA/Chap1/Chap1/","title":"Mathematical Preliminaries","text":""},{"location":"NA/Chap1/Chap1/#roundoff-errors-and-computer-arithmetic","title":"Roundoff Errors and Computer Arithmetic","text":"Truncation Error: the error involved in using a truncated, or finite, summation to approximate the sum of an infinite series.
Roundoff Error: the error produced when performing real number calculations. It occurs because the arithmetic performed in a machine involves numbers with only a finite number of digits.
If \\(p^*\\) is an approximation to p, the absolute error is | \\(p\\) \u2013 \\(p^*\\) |, and the relative error is | \\(p\\) \u2013 \\(p^*\\) | / | p |, provided that p \\(\\ne\\) 0.
The number \\(p^*\\) is said to approximate \\(p\\) to t significant digits (or figures) if \\(t\\) is the largest nonnegative integer for which \\(\\frac{|p-p^*|}{|p|}<5\\times10^{-t}\\)
To achieve \\(To\\ t\\ significant\\ digits\\) Both Chopping & Rounding has to approximate at \\(k = t\\)
Subtraction of nearly equal numbers will cause a cancellation of significant digits.
"},{"location":"NA/Chap1/Chap1/#algorithms-and-convergence","title":"Algorithms and Convergence","text":"An algorithm that satisfies that small changes in the initial data produce correspondingly small changes in the final results is called stable; otherwise it is unstable.
An algorithm is called conditionally stable if it is stable only for certain choices of initial data.
Suppose that \\(E_0 > 0\\) denotes an initial error and \\(E_n\\) represents the magnitude of an error after n subsequent operations. If \\(E_n \\approx CnE_0\\), where C is a constant independent of n, then the growth of error is said to be linear. If \\(E_n \\approx C^n E_0\\), for some C > 1, then the growth of error is called exponential.
"},{"location":"NA/Chap2/Chap2/","title":"Solutions of Equations in one variable","text":""},{"location":"NA/Chap2/Chap2/#the-bisection-method","title":"The Bisection Method","text":"Algorithm: Bisection\nTo find a solution to f (x) = 0 given the continuous function f on the interval [ a, b ], where f (a) and f (b) have opposite signs.\nInput: endpoints a, b; tolerance TOL; maximum number of iterations Nmax.\nOutput: approximate solution p or message of failure.\n\nStep 1 Set i = 1;\n FA = f (a) ;\nStep 2 While( i<=Nmax) do steps 3-6\n Step 3 Set p = a + ( b \u2013 a ) / 2 ; /* compute pi */\n FP = f (p) ; \n Step 4 If ( FP == 0 ) or ( b - a ) / 2 < TOL then Output (p); \n STOP; /* successful */\n Step 5 Set i ++;\n Step 6 If sign(FA) \u00b7 sign(FP) > 0 then set a = p ; FA = FP ;\n Else set b = p ; /* update ai, bi */ \nStep 7 Output (Method failed after Nmax iterations); /* unsuccessful */\n STOP.\n
Other Stopping Procedures
\\(|p_N-p_{N-1}|<\\epsilon\\)
\\(\\frac{|p_N-p_{N-1}|}{|p_N|}<\\epsilon\\)
\\(|f(p_N)|<\\epsilon\\)
"},{"location":"NA/Chap2/Chap2/#therom","title":"Therom","text":"Suppose that \\(f\\in C[a,b]\\) and \\(f(a)f(b)<0\\) The Bisection method generates a seqence \\(\\{p_n\\}_{n=1}^{\\infty}\\) approximating a zero p of f with \\(|p_n-p|\\le\\frac{b-a}{2^n}\\) when \\(n \\ge1\\)
"},{"location":"NA/Chap2/Chap2/#fixed-point-iteration","title":"Fixed-Point Iteration","text":""},{"location":"NA/Chap2/Chap2/#theorem-fixed-point-theorem","title":"Theorem: (Fixed-Point Theorem)","text":"Let \\(g \\in C[a, b]\\) be such that \\(g(x) \\in [a, b]\\), for all x in \\([a, b]\\).
\\(g\\) has a fixed point in \\([a,b]\\)
Suppose, in addition, that \\(g\u2019\\) exists on \\((a, b)\\) and that a constant \\(0 < k < 1\\) exists with\\(|g\u2019(x)| \\le k\\) for all x in (a, b). Then:
(1) The fixed point is unique \\(Proof \\ P57\\) (By Contradiction)
(2)For any number \\(p_0\\) in [a, b], the sequence defined by \\(p_n = g( p_{n \u2013 1} )\\), \\(n \\ge 1\\), converges to the unique fixed point \\(p\\) in \\([a, b]\\). \\(Proof \\ P61-62\\)
"},{"location":"NA/Chap2/Chap2/#corollary","title":"Corollary","text":"If \\(g\\) satisfies the hypotheses of the Fixed-Point Theorem, then bounds for the error involved in using \\(p_n\\) to approximate \\(p\\) are given by (for all n \\(\\ge\\) 1)
\\(|p_n-p|\\le k^n max\\{p_0-a,b-p_0\\}\\)
\\(|p_n-p|\\le\\frac{1}{1-k}|p_{n+1}-p_n|\\)
\\(|p_n-p|\\le \\frac{k^n}{1-k}|p_1-p_0|\\)
\\(Proof\\ P62\\)
Algorithm: Fixed-Point Iteration\nFind a solution to p = g(p) given an initial approximation p0.\nInput: initial approximation p0; tolerance TOL; maximum number of iterations Nmax.\nOutput: approximate solution p or message of failure.\nStep 1 Set i = 1;\nStep 2 While ( i <= Nmax) do steps 3-6\n Step 3 Set p = g(p0); /* compute pi */\n Step 4 If | p - p0 | < TOL then Output (p); /* successful */\n STOP;\n Step 5 Set i ++;\n Step 6 Set p0 = p ; /* update p0 */\nStep 7 Output (The method failed after Nmax iterations); /* unsuccessful */\n STOP.\n
"},{"location":"NA/Chap2/Chap2/#newtons-problem","title":"Newton's Problem","text":"Linearize a nonlinear function using Taylor\u2019s expansion.
"},{"location":"NA/Chap2/Chap2/#therom_1","title":"Therom","text":""},{"location":"NA/Chap2/Chap2/#secant-method","title":"Secant Method","text":""},{"location":"NA/Chap2/Chap2/#method-of-false-position","title":"Method of False Position","text":"P73 Just Modify to always bracket the root.
"},{"location":"NA/Chap2/Chap2/#error-analysis-for-iterative-methods","title":"Error Analysis for Iterative Methods","text":""},{"location":"NA/Chap2/Chap2/#therom-27","title":"Therom 2.7","text":"Let \\(g \\in C[a,b]\\) be such that \\(g(x)\\in [a,b]\\) for all \\(x\\in [a,b]\\).Suppose, in addition,that is continuous on \\((a,b)\\) and a positive constant \\(k<1\\) exists with \\(|g'(x)|\\le k\\) for all \\(x\\in (a,b)\\)
\\(p_{n+1}-p=g(p_n)-g(p)=g'(\\epsilon_n)(p_n-p)\\)
"},{"location":"NA/Chap2/Chap2/#therom_2","title":"Therom","text":"\\(f\\in C[a,b]\\) has a simple zero at \\(p\\) in \\((a,b)\\) if and only if \\(f(p)=0\\) but \\(f'(p)\\ne 0\\)
\\(f\\in C^m[a,b]\\) has a zero of multiplicity m at \\(p\\) in \\((a,b)\\) if and only if \\(0=f(p)=f'(p)=\u2026\u2026=f^{m-1}(p)\\) but \\(f^m(p)\\ne 0\\)
"},{"location":"NA/Chap2/Chap2/#accelarating-convergence","title":"Accelarating Convergence","text":"Algorithm: Steffensen\u2019s Acceleration\nFind a solution to x = g(x) given an initial approximation p0.\nInput: initial approximation p0; tolerance TOL; maximum number of iterations Nmax.\nOutput: approximate solution x or message of failure.\nStep 1 Set i = 1;\nStep 2 While ( i <= Nmax) do steps 3-6\n Step 3 Set p1 = g(p0) ; \n p2 = g(p1) ; \n p = p0 -( p1 - p0 )2 / ( p2 -2 p1 + p0 ) ;\n Step 4 If | p - p0 | < TOL then Output (p); /* successful */\n STOP;\n Step 5 Set i ++;\n Step 6 Set p0 = p ; /* update p0 */\nStep 7 Output (The method failed after Nmax iterations); /* unsuccessful */\n STOP.\n
"},{"location":"NA/Chap3/Chap3/","title":"Interpolation and Polynomial Approximation","text":""},{"location":"NA/Chap3/Chap3/#interpolation-and-the-lagrange-polynomial","title":"Interpolation and the Lagrange Polynomial","text":""},{"location":"NA/Chap3/Chap3/#therom-32","title":"Therom 3.2","text":"\\(Specific\\ Proof\\ P112\\)
"},{"location":"NA/Chap3/Chap3/#def","title":"Def","text":"Let \\(f\\) be a function defined at $x_0, x_1 \u2026\u2026 x_n $ and suppose that $m_0, m_1 \u2026\u2026 m_n $ are k distince integers . The Lagrange polynomial that agrees with \\(f(x)\\) at k points \\(x_{m1},x_{m2}\u2026\u2026 x_{m_k}\\) is denoted by \\(P_{m_1,m_2\u2026\u2026,m_n}(x)\\)
"},{"location":"NA/Chap3/Chap3/#nevilles-method","title":"Neville\u2019s Method","text":""},{"location":"NA/Chap3/Chap3/#therom-35","title":"Therom 3.5","text":""},{"location":"NA/Chap3/Chap3/#peseudo-code","title":"Peseudo code","text":""},{"location":"NA/Chap3/Chap3/#divided-difference","title":"Divided Difference","text":""},{"location":"NA/Chap3/Chap3/#newtons-interpolation","title":"Newton\u2019s Interpolation","text":"\\(P_n(x)=f[x_0]+\\sum_{k=1}^nf[x_0,x_1,\u2026\u2026x_k](x-x_0)\u2026\u2026(x-x_{k-1})\\)
Newton's Forward Difference Interpolation and Newton's Backward Difference Interpolation are both methods for constructing polynomial interpolants, but they differ in terms of the direction in which they compute the differences.
In both cases, the differences \\(f[x_i,x_{i\u22121},\u2026,x_0]\\) are computed using the divided difference approach. The primary difference between the two methods lies in the direction in which these differences are calculated.
Key Points:
\\(Pseudocode\\ P124-125\\)
How can we make a smooth interpolation without asking too much from \\(f\\) ?Headache \u2026
"},{"location":"NA/Chap3/Chap3/#further","title":"Further","text":"Suppose \\(S_j(x)=a_j+b_j(x-x_j)+c_j(x-x_j)^2+d_j(x-x_j)^3\\)
For notation \\(h_j=x_{j+1}-x_j\\) \\(a_{j+1}=a_j+b_jh_j+c_jh_j^2+d_jh_j^3\\) \\((1)\\)
\\(b_{j+1}=b_j+2c_jh_j+3d_jh_j^2\\) \\((2)\\)
\\(c_{j+1}=c_j+3d_jh_j\\) \\((3)\\)
\\(\\Rightarrow h_{j-1}c_{j-1}+2(h_{j-1}+h_j)c_j+h_jc_{j+1}=\\frac{3}{h_j}(a_{j+1}-a_j)-\\frac{3}{h_{j-1}}(a_{j+1}-a_j)\\)
"},{"location":"NA/Chap3/Chap3/#natural-cubic-spline","title":"Natural Cubic Spline","text":""},{"location":"NA/Chap3/Chap3/#clamped-boundary","title":"Clamped Boundary","text":""},{"location":"NA/Chap4/Chap4/","title":"Numerical Differentiation and Integration","text":"Refer to the Book .\uff08This page LACKS many contents\uff09
"},{"location":"NA/Chap4/Chap4/#numerical-differentiation","title":"Numerical Differentiation","text":""},{"location":"NA/Chap4/Chap4/#richardsons-extrapolation","title":"Richardson\u2019s Extrapolation","text":""},{"location":"NA/Chap4/Chap4/#elements-of-numerical-integration","title":"Elements of Numerical Integration","text":"Approximate \\(I=\\int_a^b f(x)dx\\)
all step size, the accuracy of approximation increases.
"},{"location":"NA/Chap5/Chap5/#error-analysis","title":"Error Analysis","text":"Refer to the BOOK
"},{"location":"NA/Chap5/Chap5/#other-euler-methods","title":"Other Euler Methods","text":""},{"location":"NA/Chap5/Chap5/#implicit-euler-method","title":"Implicit Euler Method","text":"\\(y'(t_0)\\approx\\frac{t(t_0)-y(t_0-h)}{h}\\Rightarrow y(t_1)\\approx y(t_0)+hy'(t_1)=\\alpha+hf(t_1,y(t_1))\\)
\\(w_0=\\alpha ; w_{i+1}=w_i+hf(t_{i+1},w_{i+1})(i=0,\u2026\u2026\uff0cn-1)\\)
\\(\\tau_{i+1}=\\frac{y_{i+1}-w_{i+1}}{h}==\\frac{h}{2}y''(\\epsilon_i)=o(h)\\)
"},{"location":"NA/Chap5/Chap5/#trapezoidal-method","title":"Trapezoidal Method","text":"\\(w_{i+1}=w_i+\\frac{h}{2}[f(t_i,w_i)+f(t_{i+1},w_{i+1})]\\)
\\(y'(t_0)=\\frac{1}{2h}[y(t_0+h)-y(t_0-h)]-\\frac{h^2}{6}y^{(3)}(\\epsilon_1)\\)
\\(\\Rightarrow y(t_2)\\approx y(t_0)+2hf(t_1,y(t_1))\\)
\\(w_0=\\alpha\\\\ w_{i+1}=w_{i-1}+2hf(t_i,w_i)\\)
Modified Euler's method, also known as the Improved Euler method or Heun's method, is a numerical technique used for approximating solutions to ordinary differential equations (ODEs). It is an extension of the basic Euler's method and provides better accuracy by incorporating a simple correction.
The standard Euler's method is a first-order numerical method that uses a linear approximation to update the solution from one time step to the next. However, it can have limited accuracy, especially for ODEs with rapidly changing behavior.
The Modified Euler's method improves upon this by using a two-step process:
Use the current information to make a preliminary estimate of the solution at the next time step.
Predicted Value \\((P)\\):\\(\\tilde{y}_{i+1}=y_{i}+h\u22c5f(t_i,y_i)\\)
Here, \\(y_i\\) is the current approximation, h is the step size, and \\(f(t_i,y_i)\\)represents the derivative of yy with respect to \\(t\\) at the current point.
Use the predicted value to compute a more accurate estimate by incorporating the derivative at the predicted point.
Corrected Value \\((C)\\):\\(y_{i+1}=y_i+\\frac{h}{2}[f(t_i,y_i)+f(t_{i+1},\\tilde{y}_{i+1})]\\)
In this step, \\(f(t_i,y_i)\\)represents the derivative at the initial point, and \\(f(t_{i+1},y_{i+1})\\) represents the derivative at the predicted point.
Modified Euler's method has a local truncation error of \\(O(h^3)\\), which is an improvement over the \\(O(h^2)\\) local truncation error of the basic Euler method. This makes it more accurate for a wide range of ODEs, and it is still relatively simple to implement.
"},{"location":"NA/Chap5/Chap5/#higher-order-taylor-methods","title":"Higher-order Taylor Methods","text":""},{"location":"NA/Chap5/Chap5/#runge-kutta-methods","title":"Runge-Kutta Methods","text":"Runge-Kutta methods are a family of iterative numerical techniques used for solving ordinary differential equations (ODEs) or systems of ODEs. These methods provide an approximation of the solution at discrete points in the domain by iteratively updating the solution from one point to the next.
Single-Step Method:
In a single-step method, the solution at the next time step \\((t_{i+1},w_{i+1})\\) is determined based on the information available at the current time step \\((t_{i},w_{i})\\). This implies that we calculate the next point of the solution through one iteration rather than computing the entire solution curve at once.
We can improve the result by finding a better slope.
Generalize the modified Euler\u2019s method
"},{"location":"NA/Chap5/Chap5/#multistep-methods","title":"Multistep Methods","text":"Use a linear combination of y and y at several mesh points to better approximate \\(y(t_{i+1})\\)
\\(w_{i+1}=a_{m-1}w_i+a_{m-2}w_{i-1}+\u2026\u2026+h[b_mf_{i+1}+b_{m-1}f_i+\u2026\u2026+b_0f_{i+1-m}]\\)
Newton's Forward Difference Interpolation and Newton's Backward Difference Interpolation are both methods for constructing polynomial interpolants, but they differ in terms of the direction in which they compute the differences.
In both cases, the differences \\(f[x_i,x_{i\u22121},\u2026,x_0]\\) are computed using the divided difference approach. The primary difference between the two methods lies in the direction in which these differences are calculated.
Key Points:
Derive from integration
1.Use the Newton backward-difference formula to interpolate f on \\((t_i,f_i),(t_{i-1},f_{i-1})\u2026\u2026(t_{i+1-m},f_{i+1-m})\\) and obtain \\(P_{m-1}(t)\\)
2.Or Let \\(t=t_i+sh\\ s\\in[0,1]\\) we have
\\(\\int_{t_{i}}^{t_{i+1}}f(t,y(t))dt=h\\int_0^1P_{m-1}(t_i+sh)ds+h\\int_0^1R_{m-1(t_i+sh)}ds\\)
\\(\\Rightarrow w_{i+1}=w_i+h\\int_0^1P_{m-1}(t_i+sh)ds\\)
3. The local truncation error for a multistep method is \\(\\tau_{i+1}(h)=y_{i+1}-(a_{m-1}y_i+\u2026\u2026+a_0y_{i+1-m})/h-[b_mf_{i+1}+\u2026\u2026+b_0f_{i+1-m}]\\) for each \\(i=m-1,m\u2026\u2026,n-1\\)
Example
derive the Adams-Bashforth two-step explicit method.
Note \uff1a \\(h\\) is step length \\(h=\\triangle x/n\\)
Step 1 Compute the first m initial values by Runge-Kutta method.
Step 2 Predict by Adams-Bashforth explicit method.
Step 3 Correct by Adams-Moulton implicit method.
Consider a simple first-order ordinary differential equation given by:
\\(dy/dt=\u2212y\\)
We will use the Adams predictor-corrector method to numerically solve this equation.
"},{"location":"NA/Chap5/Chap5/#step-1-compute-the-first-m-initial-values-by-runge-kutta-method","title":"Step 1: Compute the First m Initial Values by Runge-Kutta Method","text":"First, we use the Runge-Kutta method to calculate the initial values. For the given equation, the Runge-Kutta scheme is:
\\(k_1=\u2212y_n\\)
\\(k_2=\u2212(y_n+0.5hk_1)\\)
\\(y_{n+1}=y_n+hk_2\\)
Here, \\(h\\) is the time step.
"},{"location":"NA/Chap5/Chap5/#step-2-predict-by-adams-bashforth-explicit-method","title":"Step 2: Predict by Adams-Bashforth Explicit Method","text":"The Adams-Bashforth method for a second-order equation is:
\\(y_{n+1}^{(p)}=y_n+h(\u2212y_n+\\frac{3}{2}y_{n\u22121})\\)
"},{"location":"NA/Chap5/Chap5/#step-3-correct-by-adams-moulton-implicit-method","title":"Step 3: Correct by Adams-Moulton Implicit Method","text":"The Adams-Moulton method for a second-order equation is:
\\[y_{n+1}=y_n+h(\u2212\\frac{1}{2}y_{n+1}+\\frac{3}{2}y_{n})\\]This equation needs to be solved iteratively as \\(y_{n+1}\\) appears on both sides of the equation.
This is a simple example, and in practice, the Adams predictor-corrector method can be applied to higher-order differential equations. The specific steps and coefficients will vary based on the chosen order. In real-world applications, computational tools or programming languages may be used to perform these calculations.
The most popularly used system is based on the 4th-order Adams-Bashforth method as predictor and one iteration of the Adams-Moulton method as corrector, with the starting values obtained from the 4th-order Runge-Kutta method.
"},{"location":"NA/Chap5/Chap5/#derive-from-taylor-expansion","title":"Derive from Taylor expansion","text":"For multistep methods it is also required that for \\(i=1,2,\u2026\u2026m-1\\)
\u200b \\(lim_{h\\rightarrow 0}|w_i-y_i|=0\\)
A one-step difference equation method is said to be convergent with respect to the differential equation it approximates if \\(lim_{h\\rightarrow 0}max_{1\\le i\\le n}|w_i-y_i|=0\\)
Apply a particular method to a simple test equation,y\u2018 =\u03bby, y(0) =\u03b1, where Re(\u03bb ) < 0.Assume that roundoff error is introduced only at the initial point. If this initial error will decrease for a certain step size h, then this
method is said to be absolutely stable with respect to H =\u03bbh. The set of all such H forms the region of absolute stability.
Method A is said to be more stable than method B if the region of
absolute stability of A is larger than that of B.
"},{"location":"NA/Chap5/Chap5/#def_1","title":"Def","text":""},{"location":"NA/Chap6/Chap6/","title":"Direct Methods for Solving Linear Systems","text":""},{"location":"NA/Chap6/Chap6/#pivoting-strategies","title":"Pivoting Strategies","text":"Place the element in the pivot position that is largest relative to the entries in its row.
Step 1: define a scale factor \\(s_i\\) for each row as \\(s_i=max_{1\\le j\\le n}|a_{ij}|\\)
Step 2:Determine the smallest \\(p\\ge k\\) such that \\(\\frac{|a_{pk}^{(k)}|}{s_p}=max_{k\\le i\\le n}\\frac{|a_{ik}^{(k)}|}{s_i}\\) (If already at the kth row)
A strictly diagonally dominant matrix A is nonsingular. Moreover, Gaussian elimination can be performed without row or column interchanges, and the computations will be stable with respect to the growth of roundoff errors.
A matrix A is positive definite if it is symmetric and if \\(\\vec{x^t}A\\vec{x}\\) > 0 for every n-dimensional vector\\(\\vec{x}\\ne 0\\).
Matrix Norms
"},{"location":"NA/Chap7/Chap7/#eigenvalues-and-eigenvectors","title":"Eigenvalues and Eigenvectors","text":""},{"location":"NA/Chap8/Chap8/","title":"Chap8","text":"a
"},{"location":"OS/Summary/Summary/","title":"Summary","text":""},{"location":"OS/Summary/Summary/#interrupt","title":"Interrupt","text":""},{"location":"OS/lec1/lec1/#lecture-1-introduction-to-operating-systems","title":"Lecture 1 - Introduction to Operating Systems","text":""},{"location":"OS/lec1/lec1/#10-a-bit-of-history","title":"1.0 A bit of history","text":"
An operating system is a software that acts as an intermediary between the computer hardware and the user. It provides an environment in which a user can execute programs conveniently and efficiently.
------------------------------------\n| APP --> Unprivileged instructions |\n------------------------------------\n| OS --> Privileged instructions |\n------------------------------------\n| Hardware |\n------------------------------------\n
"},{"location":"OS/lec1/lec1/#os-events","title":"OS Events","text":"An event stops execution, changes mode, and changes context
When a user program need sto do something privileged, it makes a system call
The timer interrupts the computer regularly
The OS is responsible for :
"},{"location":"OS/lec2/lec2/#lecture-2-os-structure","title":"Lecture 2 - OS Structure","text":""},{"location":"OS/lec2/lec2/#system-calls","title":"System Calls","text":"
printf()
function is called, then write()
function is called. In the write()
function, there is a system call to the kernel. [with syscall number $0x1]kernel_entry code
will be called -- Saved all user space registers 2) calls write syscall handler
-- Get from syscall_table, which is an arraySYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, size_t, count)\n{\n return ksys_write(fd, buf, count);\n}\n
ret_to_user
System-call interface
maintains a table indexed according to numbers assigned to each system call. Each entry in the table points to the entry point of the system call.
strace
to trace system callsstrace cp file1 file2
strace -c cp file1 file2
-- Count the number of system callsstrace cp main.c main.copy 2>&1 |wc -l #175 lines\n
2>&1
-- Redirect stderr to stdout|wc -l
-- Count the number of linestime ap start_kernel
-- Time spent in system callstime
count multiple pid and sum them up, so it seems that the time spent in system calls is more than the actual time spent in the system calls.
time grep -Rn start_kernel
Three general methods used to pass parameters to the OS
.data
section.rodata
sectionWhy unintialized in .bss
instead of .data
? -- To save space, as .bss
is not stored in the file
readelf -h main
0x7f 0x45 0x4c 0x46
-- ELF : Linux defines the format of the file using magic numberreadelf -p .interp main\n
.interp
section of the ELF file, which contains the path of the dynamic linkerprintf()
ld-linux-aarch64.so.1
readelf
we see that entry of the main.static
is 0x400640
0x400640
is the address of the start
function in the main.static
binaryobjdump-d a.out
regs->pc = pc
here pc
is the address of the start
function -- elf_entry
interp_elf_ex -> e_entry
ld.so
-- Loader resolves the library callsstart
functionSystem calls are different -- name / number
Apps can be multi-operating system * Written in interpreted language like Python, Ruby, and interpreter available on multiple operating systems * App written in language that includes a VM containing the running app (like Java) * Use standard language (like C), compile separately on each operating system to run on each
Application Binary Interface (ABI) is architecture equivalent of API, defines how different components of binary code can interface for a given operating system on a given architecture, CPU, etc
"},{"location":"OS/lec2/lec2/#operating-system-design-and-implementation","title":"Operating-System Design and Implementation","text":""},{"location":"OS/lec2/lec2/#operating-system-structure","title":"Operating System Structure","text":"General-purpose OS is very large program
Various ways to structure ones
Microkernel \u2013Mach
Good Helper Website
"},{"location":"OS/lec3/lec3/#lecture-3-process","title":"Lecture 3 - Process","text":""},{"location":"OS/lec3/lec3/#process-concept","title":"Process Concept","text":"
Process\uff1aa unit of resource allocation and protection.
Process = code (also called the text)\n initially stored on disk in an executable file\n data section\n global variables (.bssand .data in x86 assembly)\n program counter\n points to the next instruction to execute (i.e., an address in the code)\n contents of the processor's registers\n a stack\n a heap\n----------------------------------------------\n| Stack |\n|--------------------------------------------|\n| | |\n| v |\n| ... |\n| ... |\n| ^ |\n| | |\n|--------------------------------------------|\n| Heap | \n|--------------------------------------------|\n| Data |\n|--------------------------------------------|\n| Code |\n----------------------------------------------\n
"},{"location":"OS/lec3/lec3/#runtime-stack","title":"Runtime Stack","text":"Each process has and only has a PCB * Allocate a PCB on new process creation * Free the PCB on process termination
Represented by the C structure task_struct
"},{"location":"OS/lec3/lec3/#process-state","title":"Process State","text":"fork()
System Call","text":"fork()
creates a new processThe child is is a copy of the parent, but... 1. It has a different pid(and thus ppid) 2. Its resource utilization (so far) is set to 0
fork()
returns the child\u2019s pid to the parent, and 0 to the child
getpid()
call, and its ppidwith the getppid()
callfork()
pid=fork();\nif (pid<0) {\n fprintf(stdout, \"Error: can\u2019t fork()\\n\");\n perror(\u201cfork()\u201d);\n}\nif(pid!=0) {\n fprintf(stdout, \"I am parent and my child has pid%d\\n\",pid);while(1);\n} else{\n fprintf(stdout, \"I am child, and my pidis %d\\n\", getpid());while(1) ;\n}\n
if(pid!=0)
, but for child, pid
is 0, so the child will not enter the if
blockint a = 12\nif(pid = fork()){// PARENT\n // ask the OS to put me in waiting\n sleep(10); // 10 seconds\n fprintf(stdout,\"a =%d\\n\",a);\n}\nelse{//CHILD\n a += 3;\n while(1);\n}\n
a = 12
2. The child process cannot modify the parent's memory spaceHow many times will the following code print \"Hello\"?
pid1 = fork();\nprintf(\"Hello\\n\");\npid2 = fork();\nprintf(\"Hello\\n\");\n
6 times
"},{"location":"OS/lec3/lec3/#unix-examples","title":"UNIX examples","text":"
fork()
system call creates new processexecve()
system call used after a fork()
to replace the process\u2019memory space with a new programwait()
for the child to terminateDemo of how the system do ls
\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528 exec \u7cfb\u5217\u51fd\u6570\uff08\u4f8b\u5982 execl()\u3001execv() \u7b49\uff09\u65f6\uff0c\u5b83\u4f1a\u7528\u4e00\u4e2a\u65b0\u7684\u7a0b\u5e8f\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b\u7684\u5185\u5bb9\u3002\u8c03\u7528\u6210\u529f\u540e\uff0c\u65e7\u7684\u7a0b\u5e8f\u4ee3\u7801\u3001\u6570\u636e\u548c\u6808\u90fd\u4f1a\u88ab\u65b0\u7a0b\u5e8f\u7684\u5185\u5bb9\u53d6\u4ee3\uff0c\u4f46\u8fdb\u7a0bID\uff08PID\uff09\u4fdd\u6301\u4e0d\u53d8\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u7a0b\u5e8f\u7ee7\u7eed\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u8fd0\u884c\u3002\u6b64\u65f6\uff0c\u5f53\u524d\u8fdb\u7a0b\u7684\u6240\u6709\u6267\u884c\u72b6\u6001\u90fd\u4f1a\u88ab\u65b0\u7a0b\u5e8f\u7684\u72b6\u6001\u66ff\u4ee3\uff0c\u539f\u8fdb\u7a0b\u7684\u4ee3\u7801\u4e0d\u4f1a\u518d\u6267\u884c\u3002
"},{"location":"OS/lec3/lec3/#pros-and-cons-of-fork","title":"pros and cons offork()
","text":"fork()
, the child process do execve()
, so the child process will not print \"Will I be executed?\"; it will call another programwait()
call 1. blocks until any child completes 2. returns the pidof the completed child and the child\u2019s exit codewaitpid()
call 1. blocks until a specific child completes 2. can be made non-blocking with WNOHANG options\uf06cRead the man pages (\u201cman 2waitpid\u201d)Question: what resources cannot be deallocated by the child process? * PCB (Process Control Block)
wait()
Z
statewait()
to the parent process to avoid zombie process or waitpid(pid)
if(pid<0){\n fprintf(stdout, \"Error: can\u2019t fork()\\n\");\n perror(\u201cfork()\u201d);\n}\nif(pid!=0){\n fprintf(stdout, \"I am parent and my child has pid%d\\n\",pid);\n while(1);\n //did not call wait() not properly\n} else{ //I am the child\n pid = fork();\n if(pid<0){\n fprintf(stdder,\"Error: can't fork()\\n\");\n perror(\"fork()\");\n exit(1);\n }\n if(pid==0){\n fprintf(stdder,\"I am the grandchild, and my pid is %d\\n\",getpid());\n sleep(10);\n exit(0);\n }\n else{\n sleep(1);\n // parent exits without waiting for the child\n // grandchild becomes an orphan -- not a zombie\n exit(0);\n }\n}\n
"},{"location":"OS/lec3/lec3/#process-scheduling","title":"Process Scheduling","text":"Processes migrate among the various queues.
\u5f53\u6211\u4eec\u60f3\u8981\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u65f6\uff0c\u76f4\u63a5\u901a\u8fc7\u53cc\u5411\u94fe\u8868\u63a5\u4e0a\u5373\u53ef\u3002
Context Switch is Pure Overhead 1. Should be minimized.
Code
x8
points to the task_struct of the process to be switched inswitch_to()
set ?task_struct
is on the heap.fork()
can return two different values in parent and child processes?Because fork()
, then, two set of context values.
see https://note.hobbitqia.cc/OS/chap02/#context-switch
"},{"location":"OS/lec5/Lecture5/#segmentation","title":"Segmentation","text":""},{"location":"OS/lec5/Lecture5/#segmentation-fault","title":"Segmentation Fault:","text":"
Segmentation -- Not fixed size, can grow or shrink as needed.
Still have External Fragmentation -- Segments are of varying sizes, so memory may not be used efficiently.
"},{"location":"PPL/lec1/lec1/#lec-1-lambda-calculus","title":"Lec 1 \\(\\lambda\\)-Calculus","text":"
Lambda calculus is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution.
"},{"location":"PPL/lec1/lec1/#definition","title":"Definition","text":"CANNOT CHANGE NAME OF FREE VARIABLE
CAN CHANGE NAME OF BOUND VARIABLE * \\(BV(x) = \\emptyset\\) * \\(BV(\\lambda x.t') = BV(t') \\cup \\{x\\}\\) * \\(BV(t_1 t_2) = BV(t_1) \\cup BV(t_2)\\)
"},{"location":"PPL/lec1/lec1/#example2","title":"Example2","text":"True: \\(\\lambda x.\\lambda y.x\\)
False: \\(\\lambda x.\\lambda y.y\\)
Example: (in steps):
not
and
or
xor
Let's walk through a concrete example of the multiplication function in lambda calculus, using Church numerals to represent numbers. The Church numeral for a natural number \\( n \\) is defined as a function that applies a given function \\( f \\) to an argument \\( x \\), \\( n \\)-times. Here\u2019s a quick refresher on the Church numerals:
Multiplication Function: The multiplication function is: [ mul = \\lambda m.\\lambda n.\\lambda f. m (n f) ] This means \\( m \\) is applied \\( n \\)-times to a function \\( f \\).
Example: Multiply 2 and 3 To demonstrate, we'll compute \\( 2 \\times 3 \\) using Church numerals for 2 and 3.
Now, let\u2019s compute \\( mul(2)(3) \\):
Thus, \\( mul\\ 2\\ 3 = \\lambda f. \\lambda x. f(f(f(f(f(f(x)))))) \\), which is the Church numeral for 6.
Conclusion: This result shows that applying the multiplication function to the Church numerals for 2 and 3 correctly gives the Church numeral for 6, as expected.
In lambda calculus, exponentiation (exp
) can be defined as repeated application of multiplication. Specifically, exponentiation can be thought of as applying a number \\( n \\) (the exponent) to a function \\( m \\) (the base) a number of times. This means we can define exponentiation in terms of function application.
Explanation: - \\( m \\) is the base, which is a Church numeral. - \\( n \\) is the exponent, which is also a Church numeral. - In lambda calculus, \\( n\\ m \\) means applying \\( m \\), \\( n \\)-times. Since a Church numeral represents the number of times to apply a function, this effectively results in exponentiation.
This function takes two Church numerals \\( m \\) and \\( n \\), and applies the base \\( m \\), \\( n \\)-times. The result is \\( m^n \\), which is the Church numeral representing the result of exponentiation.
Example: \\( 2^3 \\) Let\u2019s compute \\( 2^3 \\) using this definition.
Church Numerals: - \\( 2 = \\lambda f. \\lambda x. f(f(x)) \\) - \\( 3 = \\lambda f. \\lambda x. f(f(f(x))) \\)
Exponentiation Function:
Expanding this, each application of \\( 2 \\) adds another layer of function application:
\\[ \\begin{align*} &= \\lambda x. 2(2(f(f(x)))\\\\ &= \\lambda x. 2(f(f(f(f(x))))\\\\ &= \\lambda x. f(f(f(f(f(f(f(f(x)))))))\\\\ \\end{align*} \\]This result corresponds to the Church numeral for \\( 8 \\), which is \\( 2^3 \\).
"},{"location":"PPL/lec2/lec2/","title":"Lecture 2 - Abstract Syntax Trees","text":""},{"location":"PPL/lec2/lec2/#lec-2-abstract-syntax-trees","title":"Lec 2 Abstract Syntax Trees","text":""},{"location":"PPL/lec2/lec2/#definition","title":"Definition","text":"
Operand and arity
num[n]
\\(n\\in \\mathbb{N}\\)plus
,times
,\u6bcf\u4e2a\u53c2\u6570\u90fd\u662fExp\u7684astplus (num[2] ;times( num[3]; x])
EST can be drawn as:\n plus\n / \\\n num[2] times\n / \\\n num[3] x\n
"},{"location":"PT/PT/#probability-theory","title":"Probability Theory","text":""},{"location":"PT/PT/#_1","title":"\u968f\u673a\u53d8\u91cf\u4e0e\u5206\u5e03\u51fd\u6570","text":""},{"location":"PT/PT/#_2","title":"\u968f\u673a\u53d8\u91cf","text":""},{"location":"PT/PT/#_3","title":"\u5206\u5e03\u51fd\u6570","text":"
\\(F (x) = P (\u03be \u2a7d x), \u2212\u221e < x < +\u221e\\)
(1) \\(a < b, F (a) \u2a7d F (b)\\)
(2). \\(lim_{x\\rightarrow-\\infty} F(x)=0\\\\ lim_{x\\rightarrow +\\infty} F(x)=1\\)
(3) \\(\u2203F(x\u22120)= lim_{h\u21920+}F(x\u2212h)\\) -- \u5904\u5904\u5de6\u6781\u9650\u5b58\u5728 \\(F(x+0)= lim_{h\u21920+} F(x+h)=F(x)\\) -- \u53f3\u8fde\u7eed
\u6ce8\u610f\uff0c\u5982\u679c\u4fee\u6539\u5206\u5e03\u51fd\u6570\u5b9a\u4e49\u4e3a\\(F (x) = P (\u03be < x), \u2212\u221e < x < +\u221e\\)\u90a3\u4e48 (3) \u5e94\u8be5\u4fee\u6539\u4e3a\u5904\u5904\u53f3\u6781\u9650\u5b58\u5728\uff0c\u5de6\u8fde\u7eed\u3002
"},{"location":"PT/PT/#_4","title":"\u5bc6\u5ea6\u51fd\u6570","text":"\u82e5\u968f\u673a\u53d8\u91cf\\(\\epsilon\\)\u53ef\u53d6\u67d0\u4e2a\u533a\u95f4\u4e2d\u7684\u4e00\u5207\u503c\uff0c\u5e76\u4e14\u5b58\u5728\u67d0\u4e2a\u975e\u8d1f\u7684\u53ef\u79ef\u51fd\u6570\\(p(x)\\) , \u4f7f\u5206\u5e03\u51fd\u6570\\(F(x)\\)\u6ee1\u8db3\\(F(x)=\\int_{-\\infty}^xp(y)dy\\) \u5219\u79f0\\(\\epsilon\\) \u4e3a\u8fde\u7eed\u6027\u968f\u673a\u53d8\u91cf\uff0c\u79f0p(x)\u4e3a\\(\\epsilon\\)\u7684\u6982\u7387\u5bc6\u5ea6\u51fd\u6570
1.\u5206\u5e03\u51fd\u6570
2.\u5bc6\u5ea6\u51fd\u6570
\uff081\uff09\u57fa\u672c\u540c\u968f\u673a\u53d8\u91cf\u7684\u60c5\u51b5
\uff082\uff09\\(\\frac{\\partial^nF(x_1,x_2,\u2026\u2026,X_n)}{\\partial x_1\u2026\u2026\\partial x_n}=p(x_1,x_2,\u2026\u2026,x_n)\\)
3.\u8fb9\u9645\u5bc6\u5ea6
"},{"location":"PT/PT/#_6","title":"\u72ec\u7acb\u6027","text":"\u5bf9\u4e8e\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\uff0c\u7528\u5206\u5e03\u51fd\u6570\u5b9a\u4e49\u7684\u968f\u673a\u53d8\u91cf\u7684\u72ec\u7acb\u6027\u6761\u4ef6\\(F(x,y) = F_\u03be(x)F_\u03b7(y)\\) \u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u5f52\u7eb3\u63a8\u51fa\u5176\u7279\u6709\u72ec\u7acb\u6027\u6761\u4ef6\\(p_{ij} = p_i\u00b7 p_j\\) \u540c\u7406\uff0c\u5f88\u65b9\u4fbf\u5730\u53ef\u4ee5\u7528\u540e\u8005\u53cd\u63a8\u51fa\u524d\u8005\u3002\u56e0\u6b64\u5728\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4e24\u79cd\u5b9a\u4e49\u662f\u7b49\u4ef7\u7684
\u8bbe \\(p(x, y)\\)\u4e0e \\(p_\u03be (x), p_\u03b7 (y)\\) \u5206\u522b\u4e3a\u8fde\u7eed\u578b\u968f\u673a\u5411\u91cf \\((\u03be, \u03b7)\\)\u7684\u8054\u5408\u5bc6\u5ea6\u548c\u8fb9\u9645\u5bc6\u5ea6, \u5219 \\(\u03be, \u03b7\\)\u76f8\u4e92\u72ec\u7acb\u7684\u5145\u8981\u6761\u4ef6\u662f\\(p(x, y) = p_\u03be (x)p_\u03b7 (y)\\)
"},{"location":"PT/PT/#_7","title":"\u6761\u4ef6\u5206\u5e03","text":"\u79bb\u6563\u578b
\u8fde\u7eed\u578b
\u6761\u4ef6\u5206\u5e03\u51fd\u6570 \\(P(\\eta \\le y | \\epsilon =x)=\\int_{-\\infty}^{y}\\frac{p(x,v)}{p_\\epsilon(x)}dv\\)
\u6761\u4ef6\u5bc6\u5ea6\u51fd\u6570 \\(p_{\\eta | \\epsilon}(y|x)=\\frac{p(x,y)}{p_\\epsilon(x)}\\)
\\(P(A)=P(A,-\\infty<x<+\\infty)=\\int_{-\\infty}^{+\\infty} P(A|X=x)p_X(x)dx=\\int_{-\\infty}^{+\\infty}g(x)P_X(x)dx\\)
BOOK p79 2.24
"},{"location":"PT/PT/#_8","title":"\u968f\u673a\u53d8\u91cf\u7684\u51fd\u6570\u53ca\u5176\u5206\u5e03","text":"Therom 1
\u5047\u8bbef(x)\u4e25\u683c\u5355\u8c03\uff0c\u53cd\u51fd\u6570\u6709\u8fde\u7eed\u5bfc\u6570\uff0c\u5219\\(\\eta = f(\\epsilon)\\)\u4e5f\u662f\u8fde\u7eed\u6027\u968f\u673a\u53d8\u91cf\uff0c\u5176\u5bc6\u5ea6\u51fd\u6570\u4e3a
\\(g(y)=\\left\\{\\begin{array}{**lr**}p(f^{-1}(y)|f^{-1}|(y))'| y\\in f(x)\u7684\u503c\u57df\\\\0\\ \\ \\ \\ \u5176\u4ed6\\end{array}\\right.\\)
note \u5728\u4e0d\u91cd\u53e0\u7684\u533a\u95f4\u4e0a\u9010\u6bb5\u4e25\u683c\u5355\u8c03\uff0c\u5404\u6bb5\u7684\u53cd\u51fd\u6570\u90fd\u6709\u8fde\u7eed\u5012\u6570
\\(g(y)=\\left\\{\\begin{array}{**lr**}\\sum p(h_i(y)|h'_i|(y)|) y\\in \u5404h_i(y)\u7684\u5b9a\u4e49\u57df\\\\0\\ \\ \\ \\ \u5176\u4ed6\\end{array}\\right.\\)
Therom 2
\u8bbe\\(\\epsilon\\) \u6709\u8fde\u7eed\u7684\u5206\u5e03\u51fd\u6570\\(F(x)\\),\u6c42\\(\\theta=F(\\epsilon)\\)\u7684\u5206\u5e03 \\(P_{\\theta}(y)=y\\) \u670d\u4ece[0,1]\u4e0a\u7684\u5747\u5300\u5206\u5e03
Therom 3
\u82e5 \\(\\theta\\) \u670d\u4ece[0,1]\u4e0a\u7684\u5747\u5300\u5206\u5e03,F(x)\u6ee1\u8db3\u5206\u5e03\u51fd\u6570\u7684\u4e09\u4e2a\u6027\u8d28\uff0c\\(\\epsilon=F^{-1}(\\theta)\\) \u5219 \\(P_{\\epsilon}(x)=F(x)\\)
Therom 1\u5377\u79ef\u516c\u5f0f
Therom 2 \u82e5\\((\\epsilon_1 \\epsilon_2)\\)\u662f\u8fde\u7eed\u6027\u968f\u673a\u5411\u91cf\uff0c\u5219\\(\\eta=\\frac{\\epsilon_1}{\\epsilon_2}\\)\u662f\u8fde\u7eed\u578b\u968f\u673a\u53d8\u91cf\uff0c\u5176\u5bc6\u5ea6\u51fd\u6570\u4e3a
\\(p_{\\eta}(z)=\\int_{-\\infty}^{+\\infty}p(zx,x)|x|dx\\)
\\(F_{\\eta}(y)=\\int_{-\\infty}^yp_{\\eta}(z)dz\\)
\u6b21\u5e8f\u7edf\u8ba1\u8ba1\u91cf\u5206\u5e03
P89-93
\u8bbe\u968f\u673a\u53d8\u91cfX\u548cY\u76f8\u4e92\u72ec\u7acb\uff0c\u5e76\u4e14Z\u4ec5\u662fX\u7684\u51fd\u6570\uff0cW\u4ec5\u662fY\u7684\u51fd\u6570\uff1bZ=g(X) W=h(Y) \u5176\u4e2dg\u548ch\u90fd\u662f\u6ce2\u96f7\u5c14\u53ef\u6d4b\u51fd\u6570\uff0c\u90a3\u4e48Z\u548cW\u4f9d\u65e7\u72ec\u7acb
"},{"location":"PT/PT/#_9","title":"\u5e38\u89c1\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\u5206\u5e03","text":"1.\u9000\u5316\u5206\u5e03 \\(P(\\epsilon = c)=1\\)
2.\u4e24\u70b9\u5206\u5e03 \\(\\begin{bmatrix}x1&x2\\\\p&q\\end{bmatrix}\\) \\(p+q=1 p,q>0\\)
3.\u5e15\u65af\u5361\u5206\u5e03 \\(P(\\epsilon=k)=\\begin{pmatrix}k-1\\\\r-1\\end{pmatrix}p^rq^{k-r}\\)
4.\u4e8c\u9879\u5206\u5e03
\u82e5\u5b58\u5728\u6b63\u5e38\u6570\\(\\lambda\\) \u5f53\\(n \\rightarrow+\\infty\\)\u65f6\uff0c\u6709\\(bp_n \\rightarrow +\\lambda\\) ,\u5219
\\(lim_{n \\rightarrow+\\infty} b(k,n,p)=\\frac{\\lambda^k}{k!}e^{-\\lambda}\\)
\u901a\u5e38\uff0cn\u4e0ep\u65e0\u5173\uff0c\u4f46\u662fn\u5f88\u5927\uff0cp\u5f88\u5c0f\uff0cnp\u4e0d\u662f\u5f88\u5927\u7684\u65f6\u5019\uff0c\u53ef\u8fd1\u4f3c\u5730\u53d6\\(\\lambda=np\\)
\\(P(\\epsilon_n=j)\\approx \\frac{1}{\\sqrt{2\\pi npq}}e^{-x^2/2} \\ \\ x=\\frac{j-np}{\\sqrt{npq}}\\)
4.\u6cca\u677e\u5206\u5e03
\\(P(\\epsilon=k)=\\frac{\\lambda^k}{k!}e^{-\\lambda}\\)
5.\u51e0\u4f55\u5206\u5e03\u300c\u65e0\u8bb0\u5fc6\u6027\u300d
\\(P(\\epsilon=k)=pq^{k-1}\\)
6.\u8d85\u51e0\u4f55\u5206\u5e03
\u300c\u4fee\u6b63\uff1aM+N-n \u6709\u5927\u5c0f\u5199\u9519\u8bef\u300d
"},{"location":"PT/PT/#_10","title":"\u5e38\u89c1\u8fde\u7eed\u578b\u968f\u673a\u53d8\u91cf\u5206\u5e03","text":"1.\u5747\u5300\u5206\u5e03
2.\u6b63\u6001\u5206\u5e03
\\(Proof\\)
\\(\\begin{align*}(\\frac{1}{\\sqrt{2\\pi}\\sigma}\\int_{-\\infty}^{+\\infty} e^{-\\frac{(t-a)^2}{2\\sigma^2}}dt)^2 &= (\\frac{1}{\\sqrt{2\\pi}}\\int_{-\\infty}^{+\\infty} e^{-\\frac{t^2}{2}}dt)^2\\\\&=\\frac{1}{2\\pi}\\int_{-\\infty}^{+\\infty} \\int_{-\\infty}^{+\\infty} e^{\\frac{t^2+s^2}{2}}dtds\\\\ &=\\frac{1}{2\\pi} \\int_{0}^{2\\pi}d\\theta\\int_{0}^{+\\infty} re^{-\\frac{r^2}{2}}dr\\\\ &=1\\end{align*}\\)
2.2 n\u7ef4\u6b63\u6001\u5206\u5e03
\u8bbe \\(B = (b_{ij} )\\) \u4e3a n \u7ef4\u6b63\u5b9a\u5bf9\u79f0\u77e9\u9635, \\(|B|\\) \u4e3a\u5176\u884c\u5217\u5f0f, \\(B^{\u22121}\\) \u4e3a\u5176\u9006,
\u53c8\u8bbe \\(x = (x1, x2, \u00b7 \u00b7 \u00b7 , xn)^T\\) , \\(a = (a1, a2, \u00b7 \u00b7 \u00b7 , an)^T\\) , \u5219\u79f0
\\(p(\\vec{x})=\\frac{1}{(2\\pi)^{n/2}|B|^{1/2}} exp(-\\frac{1}{2}(x-a)^TB^{-1}(x-a))\\) \u4e3an\u7ef4\u6b63\u6001\u5bc6\u5ea6\u51fd\u6570
2.3 \u4e8c\u7ef4\u7684\u60c5\u51b5
\\(B=\\begin{pmatrix}\\sigma_1^2 & r\\sigma_1\\sigma_2\\\\ r\\sigma_1\\sigma_2&\\sigma_2^2\\end{pmatrix}. B^{-1}=\\begin{pmatrix}\\sigma_2^2 & -r\\sigma_1\\sigma_2\\\\ -r\\sigma_1\\sigma_2&\\sigma_1^2\\end{pmatrix}\\)
\\(p(x,y)=\\frac{1}{2\\pi\\sigma_1\\sigma_2\\sqrt{1-r^2}}exp(-\\frac{1}{2(1-r^2)}\\times[\\frac{(x-a)^2}{\\sigma_1^2} - \\frac{2r(x-a)(y-b)}{\\sigma_1\\sigma_2} + \\frac{(y-b)^2}{\\sigma_2^2}])\\)
\u7b80\u8bb0\u4f5c \\((\\epsilon,\\eta)\\) ~ \\(N(a,b,\\sigma_1^2,\\sigma_2^2,r)\\)
2.4 \u6761\u4ef6\u5206\u5e03
2.5 \u6b63\u6001\u5206\u5e03\u7684\u51fd\u6570
\\(\\epsilon\\)~ \\(N(a_1,\\sigma_1^2)\\) \\(\\eta\\)~\\(N(a_2,\\sigma_2^2)\\) \\(\\eta + \\epsilon\\)~\\(N(a_1+a_2,\\sigma_1^2+\\sigma_2^2)\\)
\\(\\epsilon\\) ~ \\(N(0,\\ 1) \\ \\eta = \\epsilon^2\\)
\u82e5\\((X,Y)\\)~\\(N(\\mu_1,\\mu_2,\\sigma_1^2,\\sigma_2^2,r)\\) \u5219 \\(C_1X+C_2Y\\)~\\(N(C_1\\mu_1+C_2\\mu_2,C_1^2\\sigma_1^2+C_2^2\\sigma_2^2+2C_1C_2r\\sigma_1\\sigma_2)\\)
2.6 \u591a\u7ef4\u6b63\u6001\u5206\u5e03 P93
\\(\\vec{\\eta}=C\\vec{\\epsilon}+a\\) \u5219 \\(\\vec{\\eta}\\) ~ \\(N(C\\vec{\\mu}+a,C\\Sigma C^T)\\)
3.\u6307\u6570\u5206\u5e03
\\(p(x)=\\left\\{\\begin{array}{**lr**}\\lambda e^{-\\lambda x}\\ x\\ge 0 \\\\0 \\ \\ \\ \\ \\ \\ \\ \\ \\ x < 0\\end{array}\\right.\\)
\\(F(x)=\\left\\{\\begin{array}{**lr**}1-e^{-\\lambda x}\\ x\\ge 0 \\\\0 \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ x < 0\\end{array}\\right.\\)
4.T\u5206\u5e03
5.\u5a01\u5e03\u5c14\u5206\u5e03
6.\u5e15\u7d2f\u6258\u5206\u5e03
7.\\(\\beta\\)\u5206\u5e03
8.\u67ef\u897f\u5206\u5e03
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/","title":"\u6570\u5b57\u7279\u5f81\u4e0e\u7279\u5f81\u51fd\u6570","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_1","title":"\u6570\u5b66\u671f\u671b","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_2","title":"\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\u7684\u6570\u5b66\u671f\u671b","text":"
\u8bbe\u7f6e\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\\(\\epsilon\\) \u7684\u5206\u5e03\u5217\u4e3a
\\(\\begin{bmatrix}x_1&x_2&\u2026\u2026&x_k&\u2026\u2026\\\\p_1&p_2&\u2026\u2026&p_k&\u2026\u2026\\end{bmatrix}\\)
\u5982\u679c\u7ea7\u6570\\(\\sum_kx_kp_k\\)\u7edd\u5bf9\u6536\u655b\uff0c\u5219\u79f0\u6b64\u7ea7\u6570\u7684\u548c\u4e3a\\(\\epsilon\\) \u7684\u6570\u5b66\u671f\u671b\u6216\u5747\u503c\\((mean)\\)\uff0c\u8ba1\u4f5c\\(E\\epsilon=\\sum_kx_kp_k\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_3","title":"\u8fde\u7eed\u578b\u968f\u673a\u53d8\u91cf\u7684\u6570\u5b66\u671f\u671b","text":"\u8bbe\\(\\epsilon\\)\u4e3a\u8fde\u7eed\u578b\u968f\u673a\u53d8\u91cf\uff0c\u6709\u5bc6\u5ea6\u51fd\u6570\\(p(x)\\)\u5f53\\(\\int_{-\\infty}^{+\\infty}|x|p(x)dx<\\infty\u65f6\\) \u79f0\\(E\\epsilon=\\int_{-\\infty}^{+\\infty}xp(x)dx\\)\u4e3a\\(\\epsilon\\)\u7684\u6570\u5b66\u671f\u671b
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_4","title":"\u4e00\u822c\u5b9a\u4e49","text":"\u8bbe\u968f\u673a\u53d8\u91cf\\(\\epsilon\\)\u6709\u5206\u5e03\u51fd\u6570\\(F(x)\\)\uff0c\u82e5\\(\\int_{-\\infty}^{+\\infty}|x|d F(x)<\\infty\\) \u79f0\\(E\\epsilon=\\int_{-\\infty}^{+\\infty}xd F(x)\\)\u4e3a\\(\\epsilon\\)\u7684\u6570\u5b66\u671f\u671b
\u8bbe\\(\\epsilon\\ \\eta\\)\u4e3a\u968f\u673a\u53d8\u91cf\u5206\u5e03\u51fd\u6570\u5206\u522b\u4e3a\\(F_{\\epsilon}(x)\\ F_{\\eta}(x)\\) \uff1b\\(f(x)\\)\u662f\u4e00\u5143\u6ce2\u96f7\u5c14\u51fd\u6570\uff0c\u8ba1\\(\\eta=f(\\epsilon)\\) \u5219 \\(E_{\\eta}=\\int_{-\\infty}^{+\\infty}xdF_{\\eta}(x)=\\int_{-\\infty}^{+\\infty}f(x)dF_{\\epsilon}(x)\\)
(Stein \u5f15\u7406) \\(P119\\)
\u82e5\\(a\\le \\epsilon \\le b\\)\u5219 \\(E\\epsilon\\) \u5b58\u5728\u4e14\\(a\\le E\\epsilon\\le b\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#1_1","title":"\u6027\u8d281\u2018","text":"\u82e5\\(|\\epsilon| < \\eta\\) \u4e14\\(E\\eta\\) \u5b58\u5728 \u5219\\(E\\epsilon\\)\u5b58\u5728 \u4e14\\(|E\\epsilon|\\le E|\\epsilon|\\le E\\eta\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#2","title":"\u6027\u8d282","text":"\\(E_{\\epsilon_1},E_{\\epsilon_2},E_{\\epsilon_3}\u2026\u2026 E_{\\epsilon_n}\\)\u5b58\u5728\uff0c\u5219\u5bf9\u4efb\u610f\u5e38\u6570\\(c_1,c_2,\u2026\u2026c_n\\)\u548cb \\(E(\\sum_{i=1}^nc_i\\epsilon_i+b)\\)\u5b58\u5728\uff0c\u4e14\\(E(\\sum_{i=1}^nc_i\\epsilon_i+b)=\\sum_{i=1}^nc_iE\\epsilon_i+b\\)
\u7279\u522b\u5730\uff1a\\(E(\\sum_{i=1}^n\\epsilon_i)=\\sum_{i=1}^nE\\epsilon_i\\) \\(E(c\\epsilon)=cE\\epsilon\\)
\u82e5\\(\\epsilon_1,\\epsilon_2\u2026\u2026\\epsilon_n\\)\u76f8\u4e92\u72ec\u7acb\uff0c\u5404\\(E_{\\epsilon_i}\\)\u5b58\u5728\uff0c\u5219\\(E(\\epsilon_1,\\epsilon_2\u2026\u2026,\\epsilon_n)=E\\epsilon_1E\\epsilon_2\u2026\u2026E\\epsilon_n\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#4","title":"\u6027\u8d284(\u6709\u754c\u6536\u655b\u5b9a\u7406)","text":"\u5047\u8bbe\u5bf9\u4efb\u610f\\(\\omega\\in\\Omega\\) \u6709\\(\\lim_{n\\rightarrow\\infty}\\epsilon(\\omega)=\\epsilon(\\omega)\\)\u5e76\u4e14\uff0c\u5bf9\u4e00\u5207\u7684\\(n >=1\\) \\(|\\epsilon_n|\\le M\\) M\u4e3a\u5e38\u6570\uff0c\u5219\\(\\lim_{n\\rightarrow\\infty}E\\epsilon_n=E\\epsilon\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_7","title":"\u6761\u4ef6\u671f\u671b","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_8","title":"\u6761\u4ef6\u671f\u671b","text":"\\(E(\\eta|\\epsilon=x)=\\int_{-\\infty}^{+\\infty}ydF_{\\eta|\\epsilon}(y|x)\\)
\\(E(\\eta|\\epsilon=x)=\\int_{-\\infty}^{+\\infty}yp_{\\eta|\\epsilon}(y|x)dy\\)
\\(E(\\eta|\\epsilon=x)=\\sum yp_{\\eta|\\epsilon}(y|x)dy\\)
\u518d\u5bf9\\(E(\\eta|\\epsilon=x)\\)\u6c42\u671f\u671b\uff0c\u4f1a\u5f97\u5230\\(E[E(\\eta|\\epsilon=x)]=E\\eta\\)
\u8fde\u7eed\u578b
\u79bb\u6563\u578b
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_9","title":"\u5168\u671f\u671b\u516c\u5f0f","text":"\\(\\epsilon\\) \u662f\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\u65f6\uff0c\u8ba1\\(p_i=P(\\epsilon=x_i) \\Rightarrow E\\eta=\\sum_i p_iE(\\eta|\\epsilon=x_i)=\\sum_iE(\\eta|\\epsilon=x_i)P(\\epsilon=x_i)\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_10","title":"\u6761\u4ef6\u671f\u671b\u7684\u6027\u8d28","text":"\\(Var\\epsilon=E(\\epsilon-E\\epsilon)^2=E\\epsilon^2-(E\\epsilon)^2\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#chebyshev","title":"Chebyshev \u4e0d\u7b49\u5f0f","text":"\\(P(|\u03be\u2212E\u03be|\u2265\u03b5)\u2264 Var\u03be\\)
\\(P(|\u03be \u2212 E\u03be| \u2265 \u03b5) =\\int_{|x-E\u03be|>=\\epsilon}dF(x)\\le \\int_{|x-E\u03be|>=\\epsilon}\\frac{(x-E\u03be)^2}{\\epsilon^2}dF(x)\\\\ \\le \\frac{1}{\\epsilon^2}\\int_ {-\\infty}^{+\\infty}(x-E\u03be)^2dF(x) =\\frac{Var\u03be}{\\epsilon^2}\\)
\\(Var\u03be=0 \\Leftrightarrow P(\u03be=c)=1\\) (c\u662f\u5e38\u6570)
\\(Var(c\u03be+b)=c^2Var\u03be\\)
\u82e5 \\(c \\ne E\u03be\\ \\ \\ var\u03be<E(\u03be-c)^2\\)
\\(p130\\)\u6700\u4f73\u9884\u6d4b\u95ee\u9898
\\(Var (\\sum\u03be_i )= \\sum Var\u03be_i +2\\sum_{1\\le i<j\\le n} E(\u03be_i \u2212E\u03be_i)(\u03be_j \u2212E\u03be_j)\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_13","title":"\u534f\u65b9\u5dee","text":"\u8bbe\\(\\epsilon_i \\epsilon_j\\)\u7684\u8054\u5408\u5206\u5e03\u51fd\u6570\u4e3a\\(F_{ij}(x,y)\\)\u82e5\\(E|(\\epsilon_i-E\\epsilon_i)(\\epsilon_j-E\\epsilon_j)|<\\infty\\) \u79f0 \\(E(\\epsilon_i-E\\epsilon_i)(\\epsilon_j-E\\epsilon_j)=\\int_{-\\infty}^{+\\infty}\\int_{-\\infty}^{+\\infty}(x-E\\epsilon_i)(y-E\\epsilon_j)dF_{ij}(x,y)\\)\u4e3a\\(\\epsilon_i\\)\u548c\\(\\epsilon_j\\)\u7684\u534f\u65b9\u5dee\uff0c\u8ba1\u4f5c\\(Cov(\\epsilon_i\\ \\epsilon_j)\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_14","title":"\u6027\u8d28","text":"\u4ee4\\(\\epsilon^*=(\\epsilon-E\\epsilon)/\\sqrt{Var\\epsilon} \\\\ \\eta^*=(\\eta-E\\eta)/\\sqrt{Var\\eta}\\)
\u79f0\\(r_{\\epsilon\\eta}=Cov(\\epsilon^*,\\eta^*)=\\frac{E(\\epsilon-E\\epsilon)(\\eta-E\\eta)}{\\sqrt{Var\\epsilon Var\\eta}}\\) \u4e3a\\(\\epsilon \\ \\eta\\)\u7684\u76f8\u5173\u7cfb\u6570 \\(E\\epsilon^* \\eta^*\\) instead of \\(Cov(\\epsilon^*,\\eta^*)\\)?
\\(r_{\\epsilon\\eta}=1\\)\u6210\u7acb\\(iff\\ P(\\frac{\\epsilon-E\\epsilon}{\\sqrt{Var\\epsilon}}=\\frac{\\eta-E\\eta}{\\sqrt{Var\\eta}})=1\\)
\\(r_{\\epsilon\\eta}=-1\\)\u6210\u7acb\\(iff\\ P(\\frac{\\epsilon-E\\epsilon}{\\sqrt{Var\\epsilon}}=-\\frac{\\eta-E\\eta}{\\sqrt{Var\\eta}})=1\\)
\\((1) Cov(\\epsilon,\\eta)=0\\)
\\((2) \\epsilon \\ \\eta\\)\u4e0d\u76f8\u5173
\\((3) E\\epsilon\\eta=E\\epsilon E\\eta\\)
\\((4)Var{\\epsilon+\\eta}=Var\\epsilon+Var\\eta\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_17","title":"\u77e9","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_18","title":"\u7279\u5f81\u51fd\u6570","text":"\\(f(t)=Ee^{it\\epsilon}\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#example","title":"Example","text":"\\(f(t)\\)\u4e3a\u7279\u5f81\u51fd\u6570\\(\\Leftrightarrow f(t)\\)\u975e\u8d1f\u5b9a\uff0c\u8fde\u7eed\u4e14\\(f(0)=1\\) * \\(\\epsilon_1\u2026\u2026\\epsilon_n\\)\u76f8\u4e92\u72ec\u7acb\uff0c\u7279\u5f81\u51fd\u6570\u5206\u522b\u4e3a\\(f_1(t)\u2026\u2026f_n(t)\\) \u8bb0\\(\\eta=\\epsilon_1+\u2026\u2026+\\epsilon_n\\) \u5219\\(\\eta\\)\u7684\u7279\u5f81\u51fd\u6570\\(f_\\eta(t)=f_1(t)f_2(t)\u2026\u2026f_n(t)\\) * \u82e5\\(E\\epsilon^n\\)\u5b58\u5728\uff0c\u5219\\(f(t)\\)n\u6b21\u53ef\u5fae\uff0c\u8fdb\u800c \\(k\\le n\\)\u65f6\uff0c\\(f^{k}(t)=i^k\\int_{-\\infty}^{+\\infty}x^ke^{itx}dF(x),f^{(k)}(0)=i^kE\\epsilon^k\\)\u7279\u522b\u5730 \\(E\\epsilon^2\\)\u5b58\u5728\u65f6 \\(E\\epsilon=-if'(0),E\\epsilon^2=-f''(0),Var\\epsilon=-f''(0)+[f'(0)]^2\\)
\u53cd\u8fc7\u6765\uff0c\u82e5n\u4e3a\u5076\u6570\uff0c\u4e14\\(f^{(n)}(0)\\)\u5b58\u5728\uff0c\u5219\\(E\\epsilon^n\\)\u5b58\u5728 * \\(\\eta=a\\epsilon+b, f_\\eta(t)=e^{ibt}f(at)\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_19","title":"\u9006\u8f6c\u516c\u5f0f","text":"\u8bbe\u5206\u5e03\u51fd\u6570\\(F(x)\\)\u7684\u7279\u5f81\u51fd\u6570\u4e3a\\(f(t)\\) \u53e6x1\uff0cx2\u4e3a\\(F(x)\\)\u7684\u8fde\u7eed\u70b9\uff0c\u5219\\(F(x_2)-F(x_1)=lim_{T\\rightarrow \\infty}\\frac{e^{-itx_1}=e^{-itx^2}}{it}f(t)dt\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_20","title":"\u552f\u4e00\u6027\u5b9a\u7406","text":"\u5206\u5e03\u51fd\u6570\u53ef\u7531\u7279\u5f81\u51fd\u6570\u552f\u4e00\u786e\u5b9a
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_21","title":"\u9006\u5085\u7acb\u53f6\u53d8\u6362","text":"\u8bbe\\(f(t)\\)\u662f\u7279\u5f81\u51fd\u6570\u4e14\\(\\int_{=\\infty}^{+\\infty}|f(t)|dt<\\infty\\)\u5219\u5206\u5e03\u51fd\u6570F(x)\u8fde\u7eed\uff0c\u6b64\u65f6 \\(F'(x)=\\frac{1}{2\\pi}\\int_{-\\infty}^{+\\infty}e^{-itx}f(t)dt\\) * \u82e5f(t)\u662f\u67d0\u968f\u673a\u53d8\u91cf\u7684\u7279\u5f81\u51fd\u6570\uff0c\u5219\\(\\bar{f(t)},|f(t)|^2\\)\u4e5f\u662f\u7279\u5f81\u51fd\u6570 \\(f(-t)=\\bar{f(t)}\\)\u662f\\(-\\epsilon\\)\u7684\u7279\u5f81\u51fd\u6570 \\(\\epsilon_1-\\epsilon_2\\)\uff08\u72ec\u7acb\u540c\u5206\u5e03\uff09\u7684\u7279\u5f81\u51fd\u6570\u4e3a\\(f(t)\\bar{f(t)}=|f(t)|^2\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_22","title":"\u5206\u5e03\u51fd\u6570\u7684\u53ef\u52a0\u6027","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_23","title":"\u591a\u5143\u7279\u5f81\u51fd\u6570","text":"\u8bbe\u968f\u673a\u5411\u91cf\\(\\vec{\\epsilon}=(\\epsilon_1,\u2026\u2026,\\epsilon_n)'\\)\u7684\u5206\u5e03\u51fd\u6570\u4e3a\\(F(x_1\u2026\u2026,x_n)\\)\u79f0\\(f(t_1,\u2026\u2026t_n)=\\int_{-\\infty}^{+\\infty}\u2026\u2026\\int_{-\\infty}^{+\\infty}e^{i(t_1x_1+\u2026\u2026+t_nx_n)}dF(x_1\u2026\u2026\uff0cx_n)\\)\u4e3a\u4ed6\u7684\u7279\u5f81\u51fd\u6570 * \\(\\eta=a_1\\epsilon_1\u2026\u2026+a_n\\epsilon_n\\\\f_\\eta(t)=Ee^{it\\sum_{k=1}^na_k\\epsilon_k}=f(a_1t,\u2026\u2026a_nt)\\) \u5176\u4ed6\u8be6\u7ec6\u89c1P48
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_24","title":"\u591a\u5143\u6b63\u6001\u5206\u5e03","text":"P149-P154
"},{"location":"PT/%E6%9E%81%E9%99%90%E5%AE%9A%E7%90%86/","title":"\u6781\u9650\u5b9a\u7406","text":""},{"location":"PT/%E6%9E%81%E9%99%90%E5%AE%9A%E7%90%86/#_1","title":"\u4f9d\u5206\u5e03\u6536\u655b\u4e0e\u4e2d\u5fc3\u6781\u9650\u5b9a\u7406","text":""},{"location":"Papers/3DGS/paper/","title":"3DGS","text":""},{"location":"Papers/3DGS/paper/#3d-gaussian-splatting-for-real-time-radiance-field-rendering","title":"3D Gaussian Splatting for Real-Time Radiance Field Rendering","text":""},{"location":"Papers/3DGS/paper/#differentiable-3d-gaussian-splatting","title":"Differentiable 3D Gaussian Splatting","text":""},{"location":"Papers/3DGS/paper/#3d-gaussian","title":"3D Gaussian","text":"
3D Gaussian : \\(G(x) = \\frac{1}{(2\\pi)^{3/2}\\sigma^3}e^{-\\frac{||x-\\mu||^2}{2\\sigma^2}}\\)
reference : 3D\u9ad8\u65af
Project the 3D Gaussian to 2D Gaussian and then render it.
"},{"location":"Papers/3DGS/paper/#projection","title":"Projection :","text":"Conclusion
\\(\\Sigma^{'} = JW\\Sigma W^TJ^T\\) where W is a view transformation and J is the Jacobian of the affine approximation of the projective transformation
Derivation
"},{"location":"Papers/3DGS/paper/#the-viewing-transformation","title":"The viewing transformation:","text":"Denote the Gaussian reconstruction kernels in object space by \\(r_{k}''(\\mathbf{t})=\\mathcal{G}_{\\mathbf{V''}}(\\mathbf{t}-\\mathbf{t_k})\\), where \\(\\mathbf{t_k}\\) are the voxel positions of center of kernel.
Denote camera coordinates by a vector \\(\\mathbf{u}=(u_0,u_1,u_2)^{T}\\). Object coordinates are transformed to camera coordinates using an af\ufb01ne mapping \\(\\mathbf{u}=\\varphi(\\mathbf{t})=\\mathbf{Wt+d}\\), called viewing tranformation.
Now we can transform the reconstruction kernels \\(\\mathcal{G}_{\\mathbf{V''}}(\\mathbf{t}-\\mathbf{t_k})\\) to camera space:
\\(\\(\\mathcal{G}_{\\mathbf{V''}}(\\varphi^{-1}(\\mathbf{u})-\\mathbf{t_k})=\\frac{1}{|\\mathbf{W}^{-1}|}\\mathcal{G}_{\\mathbf{V'}_{k}}(\\mathbf{u}-\\mathbf{u_k})=r_{k}'(\\mathbf{u})\\)\\)
where \\(\\mathbf{u_k}=\\varphi(\\mathbf{t_k})\\) is the center of the Gaussian in camera coordinates and \\(\\mathbf{V'}_{k}=\\mathbf{W}\\mathbf{V''}_{k}\\mathbf{W}^{T}\\) is the variance matrix in camera coordinates.
"},{"location":"Papers/3DGS/paper/#the-projective-transformation","title":"The Projective Transformation","text":"In camera space, The ray intersecting the center of projection and the point \\((x_0, x_1)\\) on the projection plane is called a viewing ray.
To facilitate analiytical integration of volumn function, we need to transform the camera space to ray space such that the viewing rays are parallel to a coordinate axis. The projective transformation converts camera coordinates to ray coordinates.
Camera space is de\ufb01ned such that the origin of the camera coordinate system is at the center of projection and the projection plane is the plane \\(u_{2}=1\\). Camera space and ray space are related by the mapping \\(\\mathbf{x=m(u)}\\).
\\[ \\begin{pmatrix} x_0\\\\ x_1\\\\ x_2\\\\ \\end{pmatrix} =\\mathbf{m(u)}= \\begin{pmatrix} u_{0}/u_{2}\\\\ u_{1}/u_{2}\\\\ \\Vert(u_{0},u_{1},u_{2})^{T}\\Vert\\\\ \\end{pmatrix}\\\\\\\\ ~\\\\\\\\ \\begin{pmatrix} u_0\\\\ u_1\\\\ u_2\\\\ \\end{pmatrix} =\\mathbf{m^{-1}(u)}= \\begin{pmatrix} x_{0}/l\\cdot x_2\\\\ x_{1}/l\\cdot x_2\\\\ 1/l\\cdot x_2\\\\ \\end{pmatrix} \\]where \\(l=\\Vert(x_{0},x_{1},1)^{T}\\Vert\\).
Unfortunately, these mappings are not affine. The Gaussian after the transformation may not still Gaussian. To solve this problem, we introduce the local affine approximation \\(m_{uk}\\) of the projective transformation. It is defined by the first two terms of the Taylor expansion of \\(\\mathbf{m}\\) at the point \\(\\mathbf{u}_k\\):
\\[ \\mathbf{m_{u_k}(u)=x_k+J_{u_k}\\cdot (u-u_k)}\\\\\\\\ ~\\\\ \\mathbf{J_{u_k}}={\\frac{\\partial \\mathbf{m}}{\\partial \\mathbf{u}}}(\\mathbf{u}_k) \\]where \\(\\mathbf{x}_k=\\mathbf{m(u_k)}\\) is the center of a Gaussian in ray space and the Jacobian \\(\\mathbf{J_{u_k}}\\) is given by the partial derivatives of \\(\\mathbf{m}\\) at the point \\(\\mathbf{u}_k\\).
This yields the local affine approximation of reconstruction kernels to ray space:
\\[ \\begin{aligned} r_{k}(\\mathbf{x})&=\\frac{1}{|\\mathbf{W}^{-1}|}\\mathcal{G}_{\\mathbf{V'}_{k}}(\\mathbf{m^{-1}(x)}-\\mathbf{u_k})\\\\ &=\\frac{1}{|\\mathbf{W}^{-1}||\\mathbf{J}^{-1}|}\\mathcal{G}_{\\mathbf{V}_{k}}(\\mathbf{x}-\\mathbf{x_k}) \\end{aligned} \\]where \\(\\mathbf{V}_k\\) is the variance matrix in ray coordinates:
\\[ \\begin{aligned} \\mathbf{V}_{k}&=\\mathbf{J}\\mathbf{V'}_{k}\\mathbf{J}^{T}\\\\ &=\\mathbf{JW}\\mathbf{V''}_{k}\\mathbf{W}^{T}\\mathbf{J}^{T} \\end{aligned} \\]"},{"location":"Papers/3DGS/paper/#rasterizer","title":"Rasterizer","text":"http://www.yindaheng98.top/%E5%9B%BE%E5%BD%A2%E5%AD%A6/%E7%90%83%E8%B0%90%E7%B3%BB%E6%95%B0.html#%E7%90%83%E9%9D%A2%E9%AB%98%E6%96%AF%EF%BC%88spherical-gaussian%EF%BC%89
"},{"location":"Papers/3DGS/paper/#optimization-with-adaptive-density-control-of-3d-gaussians","title":"Optimization With Adaptive Density Control of 3D Gaussians","text":""},{"location":"Papers/3DGS/paper/#gradient-caculation","title":"Gradient Caculation","text":"Reference : grads\u63a8\u5bfc
\u56db\u5143\u6570 : \u56db\u5143\u6570
// Compute loss gradient w.r.t. matrix M\n// dSigma_dM = 2 * M\nglm::mat3 dL_dM = 2.0f * M * dL_dSigma;\n
"},{"location":"Papers/3DGS/paper/#adaptive-control-of-gaussians","title":"Adaptive Control of Gaussians","text":"\u201c\u6b20\u91cd\u6784\u533a\u201d(under-reconstruction)\uff1a\u4f4d\u7f6e\u68af\u5ea6\u5f88\u5927\u4f46\u503c\u5f88\u5c0f\u7684Gaussian\u70b9 -- \u7f3a\u5c11Gaussian\u70b9\u7684\u533a\u57df \u590d\u5236\u8fd9\u4e2aGaussian\u70b9\uff0c\u518d\u6cbf\u4f4d\u7f6e\u68af\u5ea6\u8fdb\u884c\u79fb\u52a8
\u201c\u8fc7\u91cd\u6784\u533a\u201d(over-reconstruction)\uff1a\u4f4d\u7f6e\u68af\u5ea6\u5f88\u5927\u4e14\u503c\u5f88\u5927\u7684Gaussian\u70b9 -- \u6bcf\u4e2aGaussian\u70b9\u8986\u76d6\u5f88\u5927\u8303\u56f4\u7684\u533a\u57df
\u51cf\u5c0f\u8fd9\u4e2aGaussian\u70b9\u7684\u503c\uff0c\u518d\u590d\u5236\u8fd9\u4e2aGaussian\u70b9\uff0c\u518d\u6cbf\u4f4d\u7f6e\u68af\u5ea6\u8fdb\u884c\u79fb\u52a8
"},{"location":"Papers/3DGS/paper/#fast-differentiable-rasterization-for-gaussians","title":"Fast Differentiable Rasterization For Gaussians","text":""},{"location":"Papers/3DGS/paper/#pipeline","title":"Pipeline","text":"Only keep the splats that have 99% confidence of intersecting the view frustum.
\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2asplat\u7684\u4e2d\u5fc3\u4f4d\u4e8e\u89c6\u9525\u4f53\u4e4b\u5916\uff0c\u6211\u4eec\u5c31\u4e0d\u4f1a\u8003\u8651\u5b83\u3002
\u5047\u8bbe\u4e00\u4e2asplat\u4e0e5\u4e2a\u74e6\u7247\u76f8\u4ea4\uff0c\u6211\u4eec\u4e3a\u8fd9\u4e2asplat\u521b\u5efa5\u4e2a\u5b9e\u4f8b
\u6bcf\u4e2a\u5b9e\u4f8b\u6839\u636e\u5176\u5728\u89c6\u7a7a\u95f4\u4e2d\u7684\u6df1\u5ea6\u548c\u74e6\u7247ID\u83b7\u5f97\u4e00\u4e2a\u552f\u4e00\u7684\u952e
\u4f7f\u7528GPU Radix\u6392\u5e8f\u7b97\u6cd5\uff0c\u6211\u4eec\u6839\u636e\u8fd9\u4e9b\u952e\u5bf9\u6240\u6709\u5b9e\u4f8b\u8fdb\u884c\u6392\u5e8f 5. Rasterization
\u5bf9\u4e8e\u6bcf\u4e2a\u74e6\u7247\uff0c\u6211\u4eec\u786e\u5b9a\u6392\u5e8f\u540e\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5f71\u54cd\u8be5\u74e6\u7247\u7684splat\u5b9e\u4f8b
\u5047\u8bbe\u74e6\u7247(5,5)\u9996\u5148\u52a0\u8f7d\u4e86\u5f71\u54cd\u5b83\u7684splat\u5b9e\u4f8b\u5217\u8868\u5230\u5171\u4eab\u5185\u5b58
\u5bf9\u4e8e\u74e6\u7247(5,5)\u4e2d\u7684\u50cf\u7d20(5,5)\uff0c\u6211\u4eec\u4ece\u5217\u8868\u7684\u5f00\u59cb\u5230\u7ed3\u675f\u904d\u5386splat\u5b9e\u4f8b\uff0c\u7d2f\u79ef\u989c\u8272\u548c\u03b1\u503c
\u5f53\u50cf\u7d20(5,5)\u7684\u03b1\u503c\u8fbe\u52301\u65f6\uff0c\u6211\u4eec\u77e5\u9053\u5b83\u5df2\u7ecf\u9971\u548c\uff0c\u4e0d\u518d\u9700\u8981\u66f4\u591a\u7684splat\u5b9e\u4f8b\u6765\u5f71\u54cd\u5b83\u7684\u989c\u8272
Compared to the state-of-the-art NeRF, 3DGS achieves much faster rendering speed with comparable visual quality (But the memory consumption is higher).
Details see the paper
"},{"location":"Papers/4DGS/paper/","title":"2023.10-ICLR","text":""},{"location":"Papers/4DGS/paper/#real-time-photorrealistic-dynamic-scene-representation-and-rendering-with-4d-gaussian-splatting","title":"Real-Time Photorrealistic Dynamic Scene Representation And Rendering With 4D Gaussian Splatting","text":""},{"location":"Papers/4DGS/paper/#problem-formulation-and-4d-gaussian-splatting","title":"Problem Formulation and 4D Gaussian Splatting","text":""},{"location":"Papers/4DGS/paper/#problem-formulation","title":"Problem Formulation","text":"\\[ \\mathbb{I}(\\mu,v,t)=\\sum_{i=1}^{N} p_i(\\mu,v,t) \\alpha_ic_i(d)\\prod_{j=1}^{i-1} (1-p_j(\\mu, v,t)\\alpha_j) \\]
Helper Proof see Appendix
"},{"location":"Papers/4DGS/paper/#representaion-of-4d-gaussian","title":"Representaion of 4D Gaussian","text":""},{"location":"Papers/4DGS/paper/#4d-gaussian","title":"4D Gaussian","text":"4D Gaussian : In the same way as in 3D Gaussian, we can define decomposition of 4D Gaussian as \\(\\Sigma = R\\Lambda R^T\\) where \\(\\Lambda\\) is a diagonal matrix and \\(U\\) is an orthogonal matrix. Also if we use \\(S = (\\Lambda)^{1/2}\\), then \\(\\Sigma = RSS^TR\\)
S is a 4x4 scaling matrix (diagonal matrix) so it can be represented as \\(S = diag(s_x, s_y, s_z, s_t)\\)
R is a 4x4 rotation matrix so it can be decomposed into 2 isotropic 2 rotations, each of which represented by a quaternion. So \\(R = L(q_l)R(q_r)\\) where \\(q_l\\) and \\(q_r\\) are left and right quaternions respectively.
\\(q_l = (a, b, c, d)\\) and \\(q_r = (p, q, r, s)\\)
\\[ R(q_l)= \\begin{pmatrix} a&-b&-c&-d\\\\ b&a&-d&c\\\\ c&d&a&-b\\\\ d&-c&b&a \\end{pmatrix} \\] \\[ R(q_r)= \\begin{pmatrix} p&-q&-r&-s\\\\ q&p&s&-r\\\\ r&-s&p&q\\\\ s&r&-q&p \\end{pmatrix} \\]the mean of the Gaussian is represented by a 4D vector \\(\\mu = (\\mu_x, \\mu_y, \\mu_z, \\mu_t)\\)
"},{"location":"Papers/4DGS/paper/#derivation-of-the-conditional-3d-gaussian-and-marginal-1d-gaussian","title":"Derivation of the conditional 3D Gaussian and marginal 1D Gaussian","text":""},{"location":"Papers/4DGS/paper/#conditional-3d-gaussian","title":"Conditional 3D Gaussian","text":"The conditional 3D Gaussian is the Gaussian distribution of the first 3 dimensions given the 4th dimension. It can be derived by the following formula:
\\[ \\begin{aligned} \\Sigma_{3|1} &= \\Sigma_{1,2,3} - \\Sigma_{1,2,4}\\Sigma_{4}^{-1}\\Sigma_{1,2,4}^T\\\\ \\mu_{3|1} &= \\mu_{1,2,3} - \\Sigma_{1,2,3}\\Sigma_{4}^{-1}(r - \\mu_{4}) \\end{aligned} \\] \\[ \\Sigma = \\begin{pmatrix} \\Sigma_{1,2,3} & \\Sigma_{1,2,4} \\\\ \\Sigma_{1,2,4}^T & \\Sigma_4 \\end{pmatrix} \\]"},{"location":"Papers/4DGS/paper/#marginal-1d-gaussian","title":"Marginal 1D Gaussian","text":"The marginal 1D Gaussian is the Gaussian distribution of the 4th dimension. It can be derived by the following formula:
\\[ \\begin{aligned} \\Sigma_{4} &= \\Sigma_{4}\\\\ \\mu_{4} &= \\mu_{4} \\end{aligned} \\]"},{"location":"Papers/4DGS/paper/#4d-spherindrical-harmonics","title":"4D spherindrical harmonics","text":""},{"location":"Papers/4DGS/paper/#spherindrical-harmonics","title":"spherindrical harmonics","text":"\\[ \\begin{aligned} Y_{l}^m(\\theta, \\phi) &= \\sqrt{\\frac{2l+1}{4\\pi}\\frac{(l-|m|)!}{(l+|m|)!}}P_l^m(\\cos(\\theta))e^{im\\phi}\\\\ P_l^m(x) &= (1-x^2)^{|m|/2}\\frac{d^{|m|}}{dx^{|m|}}P_l(x)\\\\ P_l(x) &= \\frac{1}{2^ll!}\\frac{d^l}{dx^l}(x^2-1)^l \\end{aligned} \\]\\(l\\): the degree (non-negative integer)
\\(m\\): the order (integer such that \\(\u2212l\\leq m\\leq l\\))
\\(c_{l}^{m}\\): SH coefficients
"},{"location":"Papers/DM/DDIM/#ddim-denoising-diffusion-implicit-models","title":"DDIM: Denoising Diffusion Implicit Models","text":""},{"location":"Papers/DM/DDIM/#goal","title":"Goal","text":"
"},{"location":"Papers/DM/DDPM/#ddpm-denoising-diffusion-probabilistic-models","title":"DDPM: Denoising Diffusion Probabilistic Models","text":""},{"location":"Papers/DM/DDPM/#mathematical-formulation","title":"Mathematical Formulation","text":""},{"location":"Papers/DM/DDPM/#forward-diffusion-process","title":"Forward Diffusion Process","text":"
The distribution \\(q\\) in the forward diffusion process is defined as Markov Chain given by $$ q(x_1,...x_T|x_0) = \\Pi_{t=1}^T q(x_t|x_{t-1}) (1) $$ $$ q(x_t|x_{t-1}) = \\mathcal{N}(x_t;\\sqrt{1-\\beta_t}x_{t-1},\\beta_t I) (2) $$
From (2) we can sample \\(x_t\\) from \\(q(x_t|x_{t-1})\\) by $$ x_t = \\sqrt{1-\\beta_t}x_{t-1} + \\sqrt{\\beta_t}z_t (3) $$ where \\(z_t \\sim \\mathcal{N}(0,I)\\).
We want to sample at any time \\(t\\) from the distribution \\(q(x_t|x_0)\\) without knowing the previous samples \\(x_{t-1},...,x_1\\).
$$\\alpha_t = 1-\\beta_t (4)\\ $$ \\(\\(\\bar{\\alpha}_t = \\Pi_{i=1}^t \\alpha_i \\ \\ (5)\\\\\\)\\) $$ q(x_t|x_0) = \\mathcal{N}(x_t;\\sqrt{\\bar{\\alpha}_t}x_0,(1-\\bar{\\alpha}_t) I) (6)$$
The reverse process is a Markov chain where a neural network predicts the parameters for the reverse diffusion kernel at each timestep.
maximum likelihood estimation (MLE) is used to train the neural network.
Maximize the lower bound of the log-likelihood of the data given the model.
After some math -- our goal is to minimize the noise difference between the predicted noise and the true noise. \\(\\(||(\\epsilon_{\\theta}-\\epsilon)^2_2||\\)\\)
what are diffusion models
An In-Depth Guide to Denoising Diffusion Probabilistic Models DDPM \u2013 Theory to Implementation
Youtube: Denoising Diffusion Probabilistic Models | DDPM Explained
"},{"location":"Papers/DM/SD/#high-resolution-image-synthesis-with-latent-diffusion-models","title":"High-Resolution Image Synthesis with Latent Diffusion Models","text":""},{"location":"Papers/DM/SD/#architecture","title":"Architecture","text":""},{"location":"Papers/DM/SD/#perceptual-image-compression","title":"Perceptual Image Compression","text":"
"},{"location":"Papers/NeRF/Theory/#nerf-representing-scenes-as-neural-radiance-fields-for-view-synthesis","title":"NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis","text":""},{"location":"Papers/NeRF/Theory/#introduction","title":"Introduction","text":"
Overfit a single neural network for a particular scene to train an overfitted nerual network which represents the scene.
Input : \\((x,y,z,\\theta,\\phi)\\): \\((x,y,z)\\) is the space location and \\((\\theta,\\phi)\\) is the viewing direction
Output : (colour, density)
Therefore, when the network is well-overfitted, it can give you results depending on the given viewing directions -- Catch Transparency and Colour.
"},{"location":"Papers/NeRF/Theory/#overview","title":"Overview","text":"a. Sample 5D coordinates along camera rays.
b. Feed those locations into an MLP to produce a color and volume density.
\\(F_{\\Theta}\\) :
Process the input 3D coordinate with 8 fully-connected layers (with ReLU activations and 256 channels per layer)
Output \\(\\sigma\\) and a 256-dimensional feature vector.
Concatenate the feature vector with the camera ray's viewing direction and passed to one additional fully-connected layer (using ReLU activation and 128 channels.)
From above we can see that, NeRF encourages the representation to be multivies consistent by restricting the network to predict the volume density \\(\\sigma\\) as a function of ONLY the location, while we allow the RGB colour to be predicted as a fucntion of both location and viewing direction.
c. Use volume rendering techniques to composite these values into an image
d. The rendering function is differentiable, we optimize the scene representation by minimizing the residual between synthesized and ground truth observed images.
The weight is the scene
"},{"location":"Papers/NeRF/Theory/#detailed-parts","title":"Detailed Parts","text":""},{"location":"Papers/NeRF/Theory/#neural-radiance-field-scene-representation","title":"Neural Radiance Field Scene Representation","text":"Volume density \\(\\sigma(x)\\) : differential probability of a ray terminating at an infinitesimal particle at location x
\\(C(r)\\) : The expected color of ray \\(r(t) = o+td\\) with near and far bound \\(t_n\\) and \\(t_f\\)\u200b
\\(T(t)\\) : accumulated transmittance along the ray from \\(t_n\\) to \\(t\\) ,i.e., the probability that the ray travels from \\(t_n\\) to \\(t\\) without hitting any other particle. And therefore \\(T(t)=exp(-\\int_{t_n}^{t}\\sigma(r(s))ds)\\)\u200b
Thus , \\(C(r) = \\int_{t_n}^{t_f} T(t) \\sigma(r(t))c(r(t),d)dt\\)\u200b : Clearly color depends on both position and direction.
Numerically estimate this continuous integral using quadrature and finally we get:
"},{"location":"Papers/NeRF/Theory/#optimizing-a-neural-radiance-field","title":"Optimizing a Neural Radiance Field","text":""},{"location":"Papers/NeRF/Theory/#positional-encoding","title":"Positional Encoding","text":"Better Performace at representing high-frequency variation
Reform \\(F_{\\theta}\\) as a composition of two functions \\(F_{\\theta}=F_{\\theta}'*\\gamma\\) one learned and one not.
Implement two networks: Use the result of the coarse network to determine where to sample in the fine network.
To do this, we rewrite the alpha composited color from thecoarse network \\(\\hat{C}_c(r)\\) as a weighted sum of all sampled colors \\(c_i\\)\u200b along the ray.
Normalize the wights as \\(\\hat{w}_i=\\frac{w_i}{\\sum_{j=1}^{N_c}w_j}\\) , we sample a second set of \\(N_f\\) locations from the distribution using inverse transform sampling (https://en.wikipedia.org/wiki/Inverse_transform_sampling)
Compute the final result using all \\(N_c + N_f\\)\u200b samples(Training Time), at test time use ONLY fine network.
"},{"location":"Papers/NeRF/Theory/#implementation-details","title":"Implementation Details","text":""},{"location":"assembly/%E4%B8%AD%E6%96%AD/","title":"\u4e2d\u65ad","text":""},{"location":"assembly/%E4%B8%AD%E6%96%AD/#int16","title":"int16","text":"int 16h ax=\u6240\u6572\u952e\u7684\u7f16\u7801
again: [\u5237\u65b0\u6e38\u620f\u7684\u753b\u9762]\nmov ah,1\nint 16h \u68c0\u67e5\u952e\u76d8\u7f13\u51b2\u533a\u662f\u5426\u4e3a\u7a7a\uff0c\u82e5\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56dezf=1,\u5426\u5219\u8fd4\u56dezf=0 (ZF=1 jz\u8df3\u8f6c)\njz no_key\nhas_key:\n mov ah,0\n int 16h \u4ece\u952e\u76d8\u7684\u7f13\u51b2\u533a\u8bfb\u53d6\u4e00\u4e2a\u952e\uff0c\u6839\u636e\u6240\u6572\u7684\u952e\u6267\u884c\u4e0d\u540c\u5206\u652f\nno_key:\n jmp again\n
\u4f8b\u5b50\uff1a
data segment\nx dw 0\ny dw 0\ndata ends\ncode segment\nassume cs:code,ds:data\nmain:\n mov ax,data\n mov ds,ax\n mov ah,0\n mov al,3\n mov ax,0B800h\n mov es,ax\ndraw:\n ;bx=(y*80+x)*2\n mov ax,[y]\n mov bp,80\n mul bp \n; dx(\u9ad816\u4f4d):ax\uff08\u4f4e16\u4f4d\uff09=ax*bp \n; \u5176\u4e2ddx=0 mul\u7684\u4e58\u6570\u5bbd\u5ea616 \u5219\u88ab\u4e58\u6570\u4e3aax\n; 24x79\u603b\u4e4b\u4e0d\u8d85\u8fc716\u4f4d \n add ax,[x]\n add ax,ax\n mov bx,ax\n mov es:[bx],1720h\n check_key:\n mov ah,1\n int 16h\n jnz has_key\n jmp check_key\n has_key:\n mov ah,0\n int 16h\n cmp ax,4800h\n je is_up\n cmp ax,5000h\n je is_down\n cmp ax,4B00h\n je is_left\n cmp ax,4D00h\n je is_right\n jmp exit\nis_up:\n cmp [y],0\n je check_key\n dec [y]\n jmp draw\nis_down:\n cmp [y],24\n je check_key\n inc [y]\n jmp draw\nis_left:\n cmp [x],0\n je check_key\n dec [x]\n jmp draw\nis_right:\n cmp [x],79\n je check_key\n inc [x]\n jmp draw\nexit:\n mov ah,4Ch\n int 21h\ncode ends\nend main\n
"},{"location":"assembly/%E4%B8%AD%E6%96%AD/#int-21h","title":"int 21h","text":"0:84 78h\n0:85 56h\n0:86 34h\n0:87 12h\nxor ax,ax\nmov es,ax\nmov bx,84h\nlds si,es:[bx]\n;ds=1234h si=5678h\n
\u663e\u7136, int 00h\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[0];\nint 01h\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[4];\nint 03h\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[0Ch];\nint 08h\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[20h];\nint 0FFh\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[3FCh];\n
* BIOS\u4f1a\u5b8c\u6210\u90e8\u5206\u4e2d\u65ad\u5411\u91cf\u7684\u586b\u5199, \u5982int 10h\u3001int 16h\u3001int 13h\u8fd9\u51e0\u4e2aBIOS\u4e2d\u65ad\u7684\u5411\u91cf\u5728DOS\u542f\u52a8\u524d\u5c31\u5df2\u7ecf\u586b\u597d\u4e86 * DOS\u542f\u52a8\u5b8c\u6210\u540e\uff0c\u4f1a\u586b\u5165int 21h\u7684\u4e2d\u65ad\u5411\u91cf\u3002 1000:2000 mov ah, 2\n1000:2002 mov dl, 'A'\n1000:2004 int 21h\n1000:2006 mov ah,4Ch\n
* \u4e0a\u9762\u8fd9\u6761int 21h\u6307\u4ee4\u6267\u884c\u65f6, cpu\u505a\u4e86\u4ee5\u4e0b4\u4ef6\u4e8b: IF=0\nTF=0 -\u9632\u6b62\u4e2d\u65ad\u91cc\u518d\u6b21\u4ea7\u751f\u4e2d\u65ad\npushf\npush cs\u53731000h\npush \u4e0b\u6761\u6307\u4ee4\u7684\u504f\u79fb\u5730\u5740\u53732006h\njmp dword ptr 0:[84h]; jmp 1234h:5678h\n\n\u4e0a\u9762\u8fd9\u6761jmp\u4f1a\u8df3\u8f6c\u5230int 21h\u7684\u4e2d\u65ad\u670d\u52a1\u51fd\u6570\u5185\u90e8:\n1234:5678 ...\n ...\n1234:56FF iret; \u4e2d\u65ad\u8fd4\u56de\niret\u5728\u6267\u884c\u65f6, cpu\u505a\u4e86\u4ee5\u4e0b3\u4ef6\u4e8b\u60c5:\npop ip\u5373ip=2006h\npop cs\u5373cs=1000h\npopf\n\n1000:2000 call 1234:5678; \u8fdc\u8c03\u7528\n;\u6b64\u65f6\u4f1apush 1000h \u518d push 2005h\n;\u6700\u540ejmp 1234:5678\n1000:2005 mov ah, 4Ch\n1000:2007 int 21h\n...\n1234:5678 ...\n retf; \u6b64\u65f6\u6267\u884cpop ip\u518dpop cs\n
\u4e2d\u65ad\u7a0b\u5e8f\u4f8b\u5b50
code segment\nassume cs:code\nold_80h dw 0, 0\nmain:\n xor ax, ax\n mov es, ax\n mov bx, 80h*4; mov bx, 200h\n mov ax, es:[bx]\n mov old_80h[0], ax\n mov ax, es:[bx+2]\n mov old_80h[2], ax\n\n mov word ptr es:[bx], offset int_80h\n mov es:[bx+2], cs\n\n mov ah, 1\n int 80h; AL=\u952e\u76d8\u8f93\u5165\u7684ASCII\u7801\nnext:\n mov ah, 2\n mov dl, al\n int 80h\n\n mov ax, old_80h[0]\n mov es:[bx], ax\n mov ax, old_80h[2]\n mov es:[bx+2], ax\n\n mov ah, 4Ch\n int 21h\nint_80h: ; ISR(Interrupt Service Routine)\n ; \u4e2d\u65ad\u670d\u52a1\u51fd\u6570\n cmp ah, 1\n je is_1\nis_2:\n push es\n push bx\n push ax\n mov bx, 0B800h\n mov es, bx\n mov byte ptr es:[160], dl\n mov byte ptr es:[161], 17h\n pop ax\n pop bx\n pop es\n jmp done\nis_1:\n int 21h\ndone:\n iret\n ;\u5982\u679c\u60f3\u63a5\u8fde\u6267\u884cold int80\uff1ajmp dword ptr cs:[old_80h]\n\ncode ends\nend main\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/","title":"\u51fd\u6570","text":""},{"location":"assembly/%E5%87%BD%E6%95%B0/#_1","title":"\u6c47\u7f16\u8bed\u8a00\u4e2d\u7684\u4e09\u79cd\u53c2\u6570\u4f20\u9012\u65b9\u5f0f","text":""},{"location":"assembly/%E5%87%BD%E6%95%B0/#_2","title":"\u5bc4\u5b58\u5668\u4f20\u9012","text":"f:\n add ax, ax; ax=2*ax\n ret ; \u8fd4\u56de\u65f6ax\u5c31\u662f\u51fd\u6570\u503c\nmain:\n mov ax, 3; ax\u5c31\u662ff()\u7684\u53c2\u6570\n call f\nnext:\n mov ah, 4Ch\n int 21h\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/#_3","title":"\u53d8\u91cf\u4f20\u9012","text":"f:\n mov ax, var\n add ax, ax; ax\u5c31\u662f\u51fd\u6570\u503c\n ret\nmain:\n mov var, 3\n ; var\u662f\u4e00\u4e2a16\u4f4d\u7684\u53d8\u91cf, \u7528\u4f5c\u53c2\u6570\uff0cvar\u5fc5\u987b\u662f\u5168\u5c40\u53d8\u91cf\n call f\n
* \u5728\u6c47\u7f16\u8bed\u8a00\u4e2d\uff0c\u7528\u00a0db\u3001dw\u7b49\u5173\u952e\u8bcd\u5b9a\u4e49\u7684\u53d8\u91cf\u5747\u4e3a\u5168\u5c40\u53d8\u91cf\u00a0\u3002\u5728\u5806\u6808\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u624d\u662f\u5c40\u90e8\u53d8\u91cf\u3002 * var\u4e0d\u80fd\u7528\u4e8e\u9012\u5f52\u51fd\u6570\u5185--\u6bcf\u4e00\u5c42\u53d8\u91cf\u5730\u5740\u5404\u4e0d\u76f8\u540c\uff0c\u5168\u5c40\u53d8\u91cf\u7684\u8bdd\u6c38\u8fdc\u662f\u540c\u4e00\u4e2a\u5730\u5740\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528\u6765\u4f20\u9012\u9012\u5f52\u51fd\u6570\u7684\u53c2\u6570"},{"location":"assembly/%E5%87%BD%E6%95%B0/#_4","title":"\u5806\u6808\u4f20\u9012","text":"f:\n push bp\n mov bp, sp\n mov ax, [bp+4]; \u4ece\u5806\u6808\u4e2d\u53d6\u5f97\u53c2\u6570\n add ax, ax\n pop bp\n ret\nmain:\n mov ax, 3\n push ax; \u53c2\u6570\u538b\u5165\u5230\u5806\u6808\n call f\n add sp, 2\n
* C\u8bed\u8a00\u51fd\u6570\u8c03\u7528y=f(2,3)\u6c42\u4e24\u6570\u4e4b\u548c\u8f6c\u5316\u6210\u6c47\u7f16\u8bed\u8a00 * \u8bbess=1000h sp=2000h f:\n push bp;(4)\n mov bp, sp\n mov ax, [bp+4]\n add ax, [bp+6]\n pop bp; (5)\n ret; (6) pop ip\nmain:\n mov ax, 3\n push ax; (1)\n mov ax, 2\n push ax; (2)\n call f; (3) \n ;call\u4f1a\u628af\u6307\u4ee4\u7684\u4e0b\u4e00\u6761\u7684\u5730\u5740\uff08here\uff09\u538b\u5165\u5806\u6808 \u7136\u540e\u518djmpf\nhere:\n add sp, 4;(7);\u5806\u6808\u6062\u590d\u5e73\u8861\n
* \u4e0a\u8ff0\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5806\u6808\u5e03\u5c40\u5982\u4e0b:stack frame ss:1FF8 old bp <- bp (4)\nss:1FFA here <- (3)(5)\nss:1FFC 02 <- (2)(6)\nss:1FFE 03 <- (1)\nss:2000 ?? <- (7)\n
* printf\u51fd\u6570\u539f\u578b #include <stdio.h>\ndouble f(char *s, ...)\n{ double y=0;\n char *p = (char *)&s; /* p = bp+4 */\n p += sizeof(s); /* p = bp+6 */\n while(*s != '\\0')\n {\n if(*s == 'i')\n {\n y += *(int *)p;\n p += sizeof(int);\n }\n else if(*s == 'l')\n {\n y += *(long*)p;\n p += sizeof(long);\n }\n else if(*s == 'd')\n {\n y += *(double *)p;\n p += sizeof(double);\n }\n s++;\n }\n return y;\n}\nmain()\n{\n double y;\n y = f(\"ild\", 10, 20L, 3.14);\n printf(\"y=%lf\\n\", y);\n}\n
* call far ptr dest retf 1000:2000 call 1234:5678 ;\u8de8\u6bb5\u8df3\u8fdc\ncpu\u4f1a\u505a\u4e00\u4e0b\u52a8\u4f5c\uff1a\n[1]push cs\n[2]push 2005h\n[3]jmp 1234:5678h\n;\u4e00\u5b9a\u5148\u538b\u5165\u6bb5\u5730\u5740\uff0c\u518d\u538b\u5165\u504f\u79fb\u5730\u5740 WHY?5.18\u6700\u540e\u4e00\u6bb5\n1000:2005 mov ah,4Ch\n1234:5678 ...\n1234:5688 retf\ncpu \u5728retf\u65f6\u4f1a\u505a\u4ee5\u4e0b\u52a8\u4f5c\n[1]pop ip;\n[2]pop cs;\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/#3","title":"\u7528\u5806\u6808\u4f20\u9012\u53c2\u6570\u67093\u79cd\u65b9\u5f0f:","text":""},{"location":"assembly/%E5%87%BD%E6%95%B0/#__cdecl","title":"__cdecl","text":"__cdecl\u7684\u4f8b\u5b50:
f:\npush bp; (4)\nmov bp, sp\nmov ax, [bp+4]; arg0\nadd ax, [bp+6]; arg1\npop bp; (5)\nret; (6)\nmain:\nmov ax, 20; arg1\npush ax; (1)\nmov ax, 10; arg0\npush ax; (2)\ncall f; (3)\nhere:\nadd sp, 4; (7)\n
ss:1FF8 old bp -> bp (4)\nss:1FFA here -> (3)(5)\nss:1FFC 10 -> (2)(6)\nss:1FFE 20 -> (1)\nss:2000 -> (7)\n
__pascal\u7684\u4f8b\u5b50\uff1a
f:\npush bp; (4)\nmov bp, sp\nmov ax, [bp+6]; arg0\nadd ax, [bp+4]; arg1\npop bp; (5)\nret 4; (6)\nmain: \nmov ax, 10\npush ax; (1) arg0\nmov ax, 20\npush ax; (2) arg1\ncall f; (3)\nhere: \n
ss:1FF8 old bp<- bp (4)\nss:1FFA here <- (3)(5)\nss:1FFC 20 <- (2)\nss:1FFE 10 <- (1)\nss:2000 <-(6)\n
ret 4: [1]pop ip [2]sp=sp+4
__stdcall\u7684\u4f8b\u5b50\uff1a
f:\npush bp; (4)\nmov bp, sp\nmov ax, [bp+4]; arg0\nadd ax, [bp+6]; arg1\npop bp; (5)\nret 4; (6)\nmain:\nmov ax, 20\npush ax; (1) arg1\nmov ax, 10\npush ax; (2) arg0\ncall f; (3)\nhere: \n
__stdcall\u7684\u5806\u6808\u5e03\u5c40:
ss:1FF8 old bp<- bp (4)\nss:1FFA here <- (3)(5)\nss:1FFC 10 <- (2)\nss:1FFE 20 <- (1)\nss:2000 <-(6)\n
* 32\u4f4d__stdcall\u7684\u4f8b\u5b50: f:\npush ebp;(4)\nmov ebp, esp\nmov eax,[ebp+8];arg0\nadd eax,[ebp+0Ch];arg1\npop ebp;(5)\nret 8;(6)\nmain:\npush 20;(1) arg1\npush 10;(2) arg0\ncall f;(3)\nhere:\n
* 32\u4f4d__stdcall\u7684\u5806\u6808\u5e03\u5c40: ss:1FF0 old ebp<- ebp (4)\nss:1FF4 here <- (3)(5)\nss:1FF8 10 <- (2)\nss:1FFC 20 <- (1)\nss:2000 <-(6)\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/#_5","title":"\u52a8\u6001\u53d8\u91cf","text":"int f(int a, int b)\n{\n int c; /* c\u662f\u5c40\u90e8\u52a8\u6001\u53d8\u91cf */\n c = a+b;\n return c;\n}\n
\u4e0a\u8ff0C\u8bed\u8a00\u51fd\u6570\u53ef\u7ffb\u8bd1\u6210\u4ee5\u4e0b\u6c47\u7f16\u4ee3\u7801:
f:\npush bp; (4)\nmov bp, sp\nsub sp, 2; (5) \u8fd9\u91cc\u6316\u7684\u5751\u5c31\u662f\u7ed9\u53d8\u91cfc\u7684\nmov ax, [bp+4]\nadd ax, [bp+6]\nmov [bp-2], ax\nmov ax, [bp-2]\nmov sp, bp; (6)\u6b64\u65f6\u53d8\u91cfc\u6b7b\u4ea1\npop bp; (7)\nret; (8)\nmain:\nmov ax, 20\npush ax; (1)\nmov ax, 10\npush ax; (2)\ncall f; (3)\nhere:\nadd sp, 4;(9)\u6b64\u65f6\u53c2\u6570a,b\u6b7b\u4ea1\n
\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6, \u5806\u6808\u5e03\u5c40\u5982\u4e0b: ss:1FF6 [30] (5) \u53d8\u91cfc\nss:1FF8 old bp<- bp(4)(6)\nss:1FFA here <- (3)(7)\nss:1FFC 10 <- (2)(8)\nss:1FFE 20 <- (1)\nss:2000 <-(9)\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/#cbp-bx-si-di","title":"C\u8bed\u8a00\u51fd\u6570\u4e2d\u9700\u8981\u4fdd\u62a4bp, bx, si, di","text":"f:\npush bp\nmov bp, sp\nsub sp, n; \u5176\u4e2dn\u4e00\u4e2a\u5e38\u6570,\u7528\u6765\u4e3a\u52a8\u6001\u53d8\u91cf\u5206\u914d\u7a7a\u95f4\n \u5148\u6316\u5751\u518dpush\npush bx\npush si\npush di\n...\npop di\npop si\npop bx\nmov sp, bp\npop bp\nret\n
int f(int n)\n{\n if(n==1)\n return 1;\n return n+f(n-1);\n}\n
\u4e0a\u8ff0C\u8bed\u8a00\u9012\u5f52\u51fd\u6570\u53ef\u7ffb\u8bd1\u6210\u4ee5\u4e0b\u6c47\u7f16\u4ee3\u7801:
f:\n push bp;(3)(6)(9)\n mov bp, sp\n mov ax, [bp+4]\n cmp ax, 1\n je done\n dec ax\n push ax;(4)(7)\n call f;(5)(8)\nthere:\n add sp, 2;(12)(15)\n add ax, [bp+4]\ndone:\n pop bp;(10)(13)(16)\n ret;(11)(14)(17)\nmain:\n mov ax, 3\n push ax;(1)\n call f;(2)\n here:\n add sp, 2;(18)\n
\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\u7684\u5806\u6808\u5e03\u5c40\u5982\u4e0b: ss:1FEE oldbp<-bp(9)\nss:1FF0 there<-(8)(10)\nss:1FF2 1<-(7)(11)\nss:1FF4 oldbp<-bp(6)12\nss:1FF6 there<-(5)(13)\nss:1FF8 2<-(4)(14)\nss:1FFA oldbp<-bp(3)(15)\nss:1FFC here <-(2)(16)\nss:1FFE 3 <-(1)(17)\nss:2000 <-(18)\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/","title":"\u57fa\u7840\u6307\u4ee4","text":""},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#_1","title":"\u903b\u8f91\u8fd0\u7b97\u548c\u79fb\u4f4d\u6307\u4ee4","text":""},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#andorxornottest","title":"\u903b\u8f91\u8fd0\u7b97\u6307\u4ee4\uff1aAND\uff0cOR\uff0cXOR\uff0cNOT\uff0cTEST","text":"mov ax, 9234h\ntest ax, 8000h; ZF=0, AX=9234h\njnz msb_is_one; most significant bit\u6700\u9ad8\u4f4d\n;test\u548cand\u7684\u5173\u7cfb\u76f8\u5f53\u4e8ecmp\u548csub\u7684\u5173\u7cfb\u3002\n
test cl, cl\nor cl, cl\nand cl, cl\nor cl, 0\ncmp cl, 0\n
mov ah, 1011 0110B\nrol ah, 1; AH=0110 1101, CF=1\n;sal: shift arithmetic left \u7b97\u672f\u5de6\u79fb\nmov ah,11111100B;AH=-4\nsar ah,2;AH=11111111 AH=-2\n
mov ah,0B6h\nclc ;CF=0\nrcl ah,1 ;CF=\u539f\u6765\u6700\u9ad8\u4f4d\u7684\u503c ah\u6700\u5730\u4f4d\u4f1a\u53d8\u6210\u539fCF\u7684\u503c\n ;ah=01101100 CF=1\n\nmov ah, 0B6h\nstc ; CF=1\nrcl ah, 1 ; CF=1 AH=1011 0110 \u79fb\u4f4d\u524d\n ; CF=1 AH=0110 1101 \u79fb\u4f4d\u540e\n\nmov ah, 0B6h\nstc ; CF=1\nrcr ah, 1 ; AH=1011 0110 CF=1\u79fb\u4f4d\u524d\n ; AH=1101 1011 CF=0\u79fb\u4f4d\u540e\n\nmov ah,0B6h\nstc\nrcl ah, 1; CF=1 AH=0110 1101\n ;dx ax\n
\u5047\u5b9a\u8981\u628aAX\u4e2d\u768416\u4f4d\u503c\u8f6c\u5316\u6210\u4e8c\u8fdb\u5236\u8f93\u51fa: * \u89e3\u6cd51:
mov cx, 16\nnext:\n shl ax, 1\n jc is_1\n\nis_0:\n mov dl, '0'\n jmp output\n\nis_1:\n mov dl, '1'\n\noutput:\npush ax\nmov ah, 2\nint 21h\npop ax\ndec cx\njnz next\n
* \u89e3\u6cd52: mov cx, 16\nnext:\nshl ax, 1\nmov dl, '0'\nadc dl, 0\noutput:\npush ax\nmov ah, 2\nint 21h\npop ax\ndec cx\njnz next\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#_3","title":"\u5b57\u7b26\u4e32\u64cd\u4f5c\u6307\u4ee4","text":""},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#movsbmovswmovsd","title":"\u5b57\u7b26\u4e32\u4f20\u9001\u6307\u4ee4\uff1aMOVSB\uff0cMOVSW\uff0cMOVSD","text":"rep movsb : \u5176\u4e2drep\u8868\u793arepeat\uff0cs\u8868\u793astring\uff0cb\u8868\u793abyte
\u5728\u6267\u884c\u6b64\u6307\u4ee4\u524d\u8981\u505a\u4ee5\u4e0b\u51c6\u5907\u5de5\u4f5c\uff1a
rep movsb\u6240\u505a\u7684\u64cd\u4f5c\u5982\u4e0b:
again:\nif(cx == 0)\n goto done;\nbyte ptr es:[di] = byte ptr ds:[si]\nif(df==0)\n{si++; di++;}\nelse\n{si--; di--;}\ncx--\ngoto again\ndone:\n
\u4f8b\u5b50: \u8981\u628a\u4ee5\u4e0b\u5de6\u4fa74\u4e2a\u5b57\u8282\u590d\u5236\u5230\u53f3\u4fa7
1000:0000 'A' 2000:0000 'A'\n1000:0001 'B' 2000:0001 'B'\n1000:0002 'C' 2000:0002 'C'\n1000:0003 00 2000:0003 00\n
mov ax, 1000h\nmov ds, ax\nmov si, 0 ; mov si, 3\nmov ax, 2000h\nmov es, ax\nmov di, 0 ; mov di, 3\nmov cx, 4\ncld ; std\nrep movsb\n
again:\nif(cx == 0)\n goto done;\nword ptr es:[di] = word ptr ds:[si]\nif(df==0)\n{si+=2; di+=2;}\nelse\n{si-=2; di-=2;}\ncx--\ngoto again\ndone:\n
rep movsd\u7684\u64cd\u4f5c\u8fc7\u7a0b:[dword]
again:\nif(cx == 0)\n goto done;\ndword ptr es:[di] = dword ptr ds:[si]\nif(df==0)\n{si+=4; di+=4;}\nelse\n{si-=4; di-=4;}\ncx--\ngoto again\ndone:\n
\u572832\u4f4d\u7cfb\u7edf\u4e0b, \u5047\u5b9ads:esi->\u6e90\u5185\u5b58\u5757
, es:edi->\u76ee\u6807\u5757
, DF=0
, \u5219\u5f53\u8981\u590d\u5236\u7684\u5b57\u8282\u6570ecx\u4e0d\u662f4\u7684\u500d\u6570\u65f6\uff0c\u53ef\u4ee5\u505a\u5982\u4e0b\u5904\u7406:
push ecx\nshr ecx, 2\nrep movsd\npop ecx\nand ecx, 3; \u76f8\u5f53\u4e8eecx = ecx % 4\nrep movsb\n
cmpsb [jz je]
repe cmpsb
again:\nif(cx == 0) goto done;\ntemp=byte ptr ds:[si]-byte ptr es:[di]\nold_fl=FL\n\u5f53DF=0\u65f6\uff0csi++\uff0cdi++\n\u5f53DF=1\u65f6\uff0csi--\uff0cdi--\ncx--\nFL=old_fl\nif (ZF==1) goto again\ndone:\n
* cx!=0 \u4e5f\u53ef\u80fd\u63d0\u524d\u7ed3\u675f\u5faa\u73af * repne cmpsb * \u82e5\u672c\u6b21\u6bd4\u8f83\u4e0d\u7b49\u5219\u7ee7\u7eed\u6bd4\u8f83\u4e0b\u4e00\u4e2a(repeat if not equal) again:\nif(cx == 0) goto done;\ntemp=byte ptr ds:[si]-byte ptr es:[di]\nold_fl=FL\n\u5f53DF=0\u65f6\uff0csi++\uff0cdi++\n\u5f53DF=1\u65f6\uff0csi--\uff0cdi--\ncx--\nFL=old_fl\nif (ZF==0) goto again\ndone:\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#scasbscasw-scasd","title":"\u5b57\u7b26\u4e32\u626b\u63cf\u6307\u4ee4\uff1ascasb\uff0cscasw, scasd","text":"cmp al, es:[di]\ndi++; (\u5f53DF=1\u65f6\uff0c\u4e3adi--)\n
repne scasb:
next:\n if(cx == 0) goto done;\n cmp al, es:[di]\n di++; \u5f53DF=1\u65f6\uff0c\u4e3adi--\n cx--\n je done\n goto next\ndone:\n
\u4f8b\u5b50: \u5047\u5b9a\u4ece\u5730\u57401000:2000\u5f00\u59cb\u5b58\u653e\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8bf7\u8ba1\u7b97\u8be5\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u5e76\u5b58\u653e\u5230CX\u4e2d\u3002\u5047\u5b9a\u5b57\u7b26\u4e32\u4ee5ASCII\u78010\u7ed3\u675f\uff0c\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u5305\u62ec0\u3002
mov ax, 1000h\nmov es, ax\nmov di, 2000h; ES:DI->\u76ee\u6807\u4e32\nmov cx, 0FFFFh; CX=\u6700\u591a\u627eFFFF\u6b21\nmov al, 0; AL=\u5f85\u627e\u7684\u5b57\u7b26\ncld ; DF=0\uff0c\u8868\u793a\u6b63\u65b9\u5411\nrepne scasb; again:\nnot cx ; \u76f8\u5f53\u4e8ecx=FFFF-cx\ndec cx\n;\u4e0a\u8ff0\u4e24\u6761\u6307\u4ee4\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u4ee5\u4e0b\u4e24\u6761\u6307\u4ee4:\n;inc cx\n;not cx\n;not cx\u76f8\u5f53\u4e8ecx=FFFF-cx\u00a0\n
mov al, '#'\ncld\nrepe scasb\ndec di; ES:DI->\"ABC\"\ninc cx; CX=4\npush es\npop ds; DS=ES\npush di\npop si; SI=DI\nmov ax, 2000h\nmov es, ax\nmov di, 0\nrep movsb\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#_4","title":"\u5b57\u7b26\u4e32\u64cd\u4f5c\u6307\u4ee4","text":"es:[di] = AL\ndi++; DF=1\u65f6\u4e3adi--\n
again:\nif(cx == 0) goto done;\nes:[di] = al\ndi++; \u5f53DF=1\u65f6, \u4e3aDI--\ncx--\ngoto again;\ndone:\n
memset(void *t,int value,int n);\ndouble x[100];\nmemset(&x,0,sizeof(x))\n
al=ds:[si]\nSI++;\u5f53DF=1\u65f6, \u4e3aSI--\n
cld\nagain:\n lodsb ; AL=DS:[SI], SI++\n ; mov al, ds:[si]\n ; inc si\n cmp al, '#'\n je next\n stosb ; ES:[DI]=AL, DI++\n ; mov es:[di], al\n ; inc di\nnext:\n dec cx\n jnz again\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#c","title":"C\u8bed\u8a00\u5b57\u7b26\u4e32\u51fd\u6570\u8f6c\u6362","text":"strlen:\n push bp\n mov bp,sp\n push di\n push ds\n\n\n mov di,[bp+4]\n mov ax,data\n mov ds,ax\n mov es,ax\n mov cx,0FFFFh\n xor al,al\n cld\n repne scasb\n inc cx\n not cx\n mov ax,cx\n\n pop ds\n pop di\n mov sp,bp\n pop bp\n ret\n_memcpy:\n push bp\n mov bp,sp\n push es\n push ds\n push si\n push di\n mov ax,data\n mov es,ax\n mov ds,ax\n mov si,word ptr [bp+6]\n mov di,word ptr [bp+4]\n mov cx,word ptr [bp+8]\n cld\n rep movsb\n pop di\n pop si\n pop ds\n pop es\n mov sp,bp\n pop bp\n ret\n_strncpy:\n push bp\n mov bp,sp\n push es\n push ds\n push si\n push di\n mov si, word ptr [bp+6]\n mov di,word ptr [bp+4]\n push si\n call _strlen\n pop cx\n cmp ax,[bp+8]\n jl tricky_form\n mov cx,word ptr [bp+8]\n cld\n rep movsb\n tricky_form:\n mov dx,[bp+8]\n sub dx,ax\n mov cx,ax\n cld\n rep movsb\n mov cx,dx\n mov ax,0\n cld\n rep stosb\n pop di\n pop si\n pop ds\n pop es\n mov sp,bp\n pop bp\n ret\n_memset:\n push bp\n mov bp,sp\n push es\n push di\n mov ax,data\n mov es,ax\n mov di,[bp+4]\n mov ax,[bp+6]\n mov cx,[bp+8]\n cld\n rep stosb\n pop di\n pop es\n mov sp,bp\n pop bp\n ret\n\n_clrscr:\n push es\n push di\n mov ax,0B800h\n mov es,ax\n mov di,0\n mov ax,0000h\n mov cx,2000\nagain:\n mov es:[di],ax\n add di,2\n sub cx,1\n jnz again\n pop di\n pop es\n ret\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#_5","title":"\u63a7\u5236\u8f6c\u79fb\u6307\u4ee4","text":"jc jnc jo jno js jns jz jnz ja jb jae jbe jg jge jl jle jp jnp
\u5730\u5740 \u673a\u5668\u7801 \u6c47\u7f16\u6307\u4ee4 \n 1D3E:0090 ...\n 1D3E:00F0\n 1D3E:0100 EB06 jmp 0108h\n 1D3E:0102 B402 mov ah\uff0c2\n 1D3E:0104 B241 mov dl, 41h\n 1D3E:0106 CD21 int 21h\n 1D3E:0108 B44C mov ah\uff0c4Ch\n 1D3E:010A CD21 int 21h\n
cmp ax, bx\n\uff08je equal; jump out of range\uff09\njne not_equal\njmp equal; \u8fd1\u8df3\nnot_equal:\n...; \u5047\u5b9a\u8fd9\u91cc\u7701\u7565\u6307\u4ee4\u7684\u673a\u5668\u7801\u603b\u957f\u5ea6\u8d85\u8fc77Fh\u5b57\u8282\nequal:\n...\n
jmp \u504f\u79fb\u5730\u5740\u6216\u6807\u53f7 ; \u5982jmp 1000h\njmp 16\u4f4d\u5bc4\u5b58\u5668 ; \u5982jmp bx\njmp 16\u4f4d\u53d8\u91cf ; \u5982jmp word ptr [addr]\n
jmp 1234h:5678h
; \u673a\u5668\u7801\u4e3a0EAh,78h,56h,34h,12h
db 0EAh\ndw 5678h\ndw 1234h\ndb 0EAh\ndd 0FFFF0000h\n;\u6216 dw 0000h\n;dw 0FFFFh\n;\u4e0a\u8ff03\u884c\u5b9a\u4e49\u5408\u5728\u4e00\u8d77\u8868\u793ajmp 1234h:5678h\n
mov word ptr ds:[bx],1234h\nmov word ptr ds:[bx+2],5678h\njmp dword ptr ds:[bx] ;jmp 5678h:1234h\n
data segment\naddr dw 0000h, 0FFFFh \n;\u6216\u5199\u6210addr dd 0FFFF0000h\ndata ends\ncode segment\nassume cs:code, ds:data\nmain:\nmov ax, data\nmov ds, ax\njmp dword ptr [addr] \n;\u76f8\u5f53\u4e8ejmp FFFF:0000\ncode ends\nend main\n
CX = CX - 1 ; \u5faa\u73af\u6b21\u6570\u51cf1\nif(CX != 0) ; \u82e5CX\u4e0d\u7b49\u4e8e0\uff0c\u5219\n goto dest ; \u8df3\u8f6c\u81f3dest\n
mov cx, 3\nnext:\nadd ax, cx; ax +3, +2, +1\nloop next; cx=2, 1, 0\n ; dec cx\n ; jnz next \u5148-1\u518d\u5224\u65ad\n ;--cx\u521d\u59cb\u503c=0\u65f6\u80fd\u8fbe\u5230\u6700\u5927\u5faa\u73af\u6b21\u6570 10000h\u6b21\n\nmov ax, 0\nmov cx, 0\njcxz done \u8fd9\u6761\u6307\u4ee4\u53ef\u4ee5\u9632\u6b62cx\u4e3a0\u65f6\u8fdb\u5165\u5faa\u73af\nnext:\nadd ax, cx\nloop next; \u5faa\u73af10000h\u6b21\ndone:\n
\u8fd1\u8fd4\u56de\uff1aretn\uff0c\u5728\u591a\u6570\u60c5\u51b5\u4e0b\u53ef\u5199\u6210ret\n\u8fdc\u8c03\u7528\uff1acall far ptr dest\n\u8fdc\u8fd4\u56de\uff1aretf\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#movpushpopxchg","title":"\u901a\u7528\u6570\u636e\u4f20\u9001\u6307\u4ee4\uff1aMOV\uff0cPUSH\uff0cPOP\uff0cXCHG","text":"mov al, es:[di]\nmov ds:[bx], al\n
eax=12345678h\nebx=89ABCDEFh\nss=1000h\nsp=2000h\n\u73b0\u6267\u884c\u4ee5\u4e0b\u6307\u4ee4:\npush eax;(1)\npush ebx;(2)\npop ebx;(3)\npop eax;(4)\n
mov ax,1\nmov bx,2\nxchg ax,bx\nxchg ax,ds:[bx] \u53ef\u4ee5\u4ea4\u6362\u5bc4\u5b58\u5668\u548c\u53d8\u91cf \u4e0d\u5141\u8bb8\u5bf9\u4e24\u4e2a\u53d8\u91cf\u8fdb\u884c\u4ea4\u6362\n
stk segment stack \u5806\u6808\u53ea\u80fd\u5b9a\u4e49\u4e00\u4e2a\ns db 200h dup ('S');\u4e5f\u53ef\u4ee5\u4e0d\u5b9a\u4e49\u6570\u7ec4\u540d\ndb 200h dup('S')\nstk ends\n
* assume cs:code,ds:data,ss:stk * \\(mov\\ ah,s[1]\\) \u7f16\u8bd1\u540e\u53d8\u6210 \\(mov\\ ah,stk:[s+1]\\)\u518d\u8f6c\u5316\u6210$mov ah,ss:[1] $ * \u5982\u679c\u5f15\u7528\u6570\u7ec4s\u5fc5\u987b\u52a0\u4e0aassume ss\uff1astk"},{"location":"assembly/%E5%A0%86%E6%A0%88/#dos","title":"\u7a0b\u5e8f\u8f7d\u5165\u5185\u5b58\u540e\uff0c\u64cd\u4f5c\u7cfb\u7edfdos\u4f1a\u5bf9\u4e00\u4e0b\u5bc4\u5b58\u5668\u4f5c\u521d\u59cb\u5316\u8d4b\u503c\uff1a","text":"psp\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a100h\u5b57\u8282\u7684\u5185\u5b58\u5757\uff0c\u4f4d\u4e8e\u5f53\u524d\u7a0b\u5e8f\u9996\u6bb5\u7684\u524d\u9762 psp\u7531\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9\u5f53\u524d\u7a0b\u5e8f\uff0c\u91cc\u9762\u5b58\u653e\u4e86\u4e0e\u8be5exe\u76f8\u5173\u7684\u4e00\u4e9b\u4fe1\u606f\u5982\u547d\u4ee4\u884c\u53c2\u6570\u5373main\uff08\uff09\u7684\u53c2\u6570
int main(int argc,char *argv[]){ }\nmain.exe 123 xyz\nargv[0] [1] [2] \n
\u6e90\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u5b9a\u4e49\u5806\u6808\u6bb5\u65f6 ss=\u9996\u6bb5\u7684\u6bb5\u5730\u5740=1000h sp=0 sp-2=FFFE\uff081000\uff1aFFFE\uff09\u2014\u2014\u8fbe\u5230\u6700\u5927\u5806\u6808 \u5c0f\u7aef \u5373push a[0] (1234h)\u5728\u5806\u6808\u91cc\u4ec0\u4e48\u6837\u5b50 34 12 * 9000\uff1aFFFF\u662f\u7528\u6237\u53ef\u7528\u7684last \u5982\u679cpush\u592a\u591a\u4e86 \u53ef\u80fd\u8fdb\u5165code\u6bb5\u672b\u5c3e\uff0c\u5806\u6808\u6ea2\u51fa
"},{"location":"assembly/%E5%AF%84%E5%AD%98%E5%99%A8/","title":"\u5bc4\u5b58\u5668","text":""},{"location":"assembly/%E5%AF%84%E5%AD%98%E5%99%A8/#_1","title":"\u5bc4\u5b58\u5668\u8be6\u89e3","text":"\u5982\u679c\u672a\u5b9a\u4e49\u5806\u6808\uff0c\u90a3\u4e48ss\u548csp\uff1f
ss=\u9996\u6bb5\u7684\u6bb5\u5730\u5740=1000h \nsp=0 \nsp-2=FFFE\uff081000\uff1aFFFE\uff09\u2014\u2014\u8fbe\u5230\u6700\u5927\u5806\u6808 \n9000\uff1aFFFF\u662f\u7528\u6237\u53ef\u7528\u7684last \n\u5982\u679cpush\u592a\u591a\u4e86 \u53ef\u80fd\u8fdb\u5165code\u6bb5\u672b\u5c3e\uff0c\u5806\u6808\u6ea2\u51fa\n
11 10 9 8 7 6 4 2 0
O DF IF TF SF ZF AF PF CF
"},{"location":"assembly/%E5%AF%84%E5%AD%98%E5%99%A8/#cf-zf-sf-of-af-pf","title":"CF ZF SF OF AF PF: \u72b6\u6001\u6807\u5fd7","text":"CF: \u8fdb\u4f4d\u6807\u5fd7(carry flag)
mov ah, 0FFh ;\nadd ah, 1; ah=0, CF=1\u4ea7\u751f\u4e86\u8fdb\u4f4d \nadd ah, 2; ah=2, CF=0 \nsub ah, 3; ah=0FFh, CF=1\u4ea7\u751f\u4e86\u8fdb\u4f4d \n
mov cl,2 shr ah,cl;\u53f3\u79fb\u4e24\u4f4d\uff0ccf=1 \n
\u4e0eCF\u76f8\u5173\u7684\u8df3\u8f6c\u6307\u4ee4:
data segment\nabc dw 32767\ndata ends\ncode segment\nassume cs:code,ds:data\nmain:\n mov ax,data\n mov ds,ax\n mov ax,[abc]\n mov cx,16\nagain:\n shl ax,1\n jc is_one\nis_zero:\n mov dl,'0'\n jmp out_put\nis_one:\n mov dl,'1'\nout_put:\n push ax\n mov ah,2\n int 21h\n pop ax\n sub cx,1\n jnz again\nmov ah,4Ch\nint 21h\ncode ends\nend main\n
ZF: \u96f6\u6807\u5fd7(zero flag)
sub ax, ax; AX=0, ZF=1 \nadd ax, 1; AX=1, ZF=0 \nadd ax, 0FFFFh; AX=0, ZF=1, CF=1\n
SF: \u7b26\u53f7\u6807\u5fd7(sign flag)\u8fd0\u7b97\u7ed3\u679c\u7684\u6700\u9ad8\u4f4d
mov ah, 7Fh add ah, 1; AH=80h=1000 0000B, SF=1\u2014\u2014\u8fd0\u7b97\u7ed3\u679c\u8d1f\u6570 \nsub ah, 1; AH=7Fh=0111 1111B, SF=0\u2014\u2014\u8fd0\u7b97\u7ed3\u679c\u6b63\u6570\n
OF: \u6ea2\u51fa\u6807\u5fd7(overflow flag)
mov ah, 7Fh \nadd ah, 1 ;AH=80h, OF=1, ZF=0, CF=0, SF=1 \nmov ah, 80h add ah, 0FFh; AH=7Fh, OF=1, ZF=0, CF=1, SF=0 \nmov ah, 80h sub ah, 1; AH=7Fh, OF=1, ZF=0, CF=0, SF=0 \n
mov ab,81h ;ah=10000001B \nshl ah,1 ah=00000010B;0F=1,CF=1 \nonly one-bit shift\u4f1a\u5f71\u54cdOF\n
PF(Parity Flag)\u5947\u5076\u6807\u5fd7
mov ah, 4 add ah, 1; AH=0000 0101B, PF=1\u8868\u793a\u6709\u5076\u6570\u4e2a1 \nmov ax, 0101h add ax, 0004h; AX=0105h=0000 0001 0000 0101B \n; PF=1\u53ea\u7edf\u8ba1\u4f4e8\u4f4d\u4e2d1\u7684\u4e2a\u6570 \u8981\u662f\u4f4e8\u4f4d\u4e2d1\u7684\u4e2a\u6570\u662f\u5947\u6570\u65f6\uff0cPF=0 \n
\u5047\u5b9a\u8981\u53d1\u9001\u5b57\u7b26\u2019C\u2019=0100 0011B, \u73b0\u5047\u5b9a\u4f4e7\u4f4d\u4e3a\u6570\u636e\u4f4d \u6700\u9ad8\u4f4d\u4e3a\u6821\u9a8c\u4f4d\u3002\u90a3\u4e48\u6821\u9a8c\u4f4d\u7684\u8ba1\u7b97\u65b9\u6cd5\u67092\u79cd:
(1) \u5947\u6821\u9a8c: \u6570\u636e\u4f4d+\u6821\u9a8c\u4f4d\u5408\u8d77\u6765\uff0c1\u7684\u4e2a\u6570\u5fc5\u987b\u662f\u5947\u6570
(2) \u5076\u6821\u9a8c: \u6570\u636e\u4f4d+\u6821\u9a8c\u4f4d\u5408\u8d77\u6765\uff0c1\u7684\u4e2a\u6570\u5fc5\u987b\u662f\u5076\u6570
\u73b0\u5728\u6211\u4eec\u91c7\u7528\u5076\u6821\u9a8c\u6765\u53d1\u9001\u2019C\u2019,\u90a3\u4e48\u6821\u9a8c\u4f4d\u5fc5\u987b\u7b49\u4e8e1,\u5373\u5b9e\u9645\u8981\u53d1\u9001\u76848\u4f4d\u4e8c\u8fdb\u5236\u503c\u4e3a1100 0011B \u5bf9\u65b9\u63a5\u6536\u8fd98\u4f4d\u503c\u5e76\u4fdd\u5b58\u5728\u5bc4\u5b58\u5668AL\u4e2d, \u63a5\u4e0b\u53bb\u53ef\u4ee5\u6267\u884c\u5982\u4e0b\u4ee3\u7801\u6765\u9a8c\u8bc1AL\u4e2d\u7684\u503c\u662f\u5426\u6709\u9519:
or al, al\njnp error; if(PF==0) goto error\n good:\n ...\n error:\n ...\n
AF(Auxiliary Flag) \u8f85\u52a9\u8fdb\u4f4d\u6807\u5fd7
\u4f4e4\u4f4d\u5411\u9ad84\u4f4d\u4ea7\u751f\u8fdb\u4f4d\u6216\u501f\u4f4d
mov ah, 1Fh ; 0001 1111\nadd ah, 1 ; ah=20h, AF=1\n
AF\u8ddfBCD(Binary Coded Decimal)\u7801\u6709\u5173
DF:direction flag
TF:trace/trap flag
IF:interrupt flag
DF(Direction Flag)\u65b9\u5411\u6807\u5fd7: \u63a7\u5236\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u65b9\u5411
data segment:\ns db \"ABC123xyz\",0\nt db 10 dup(0)\ndata ends\ncode segment:\nassume cs:code,ds:data\nmain:\n mov ax,data\n mov ds,ax\n mov es,ax\n mov si,offset s+9\n mov di,offset t+9\n mov cx,10\n std\n rep movsb ;memcpy(es:di,ds:si,cx)\ndone:\n mov ah,4Ch\n int 21h\ncode ends\nend main\n\n\u6ce8\uff1arep movsb\nagain:\n cmp cx,0\n je done\n mov al,ds[si]\n mov es:[di],al\n inc si/dec si\n inc di/dec di\n dec cx\n jmp again\n
IF\uff08Interrupt Flag\uff09\u4e2d\u65ad\u6807\u5fd7
mov ax, 0 \nmov bx, 1 \nadd ax, bx \n
\u65f6\u949f\u6bcf\u96941/18\u5999\u4f1a\u4ea7\u751f\u4e00\u4e2a\u4e2d\u65ad\u8bf7\u6c42 \u82e5\u7a0b\u5e8f\u5df2\u8fd0\u884c\u4e861/18\u79d2,\u5219cpu\u4f1a\u5728\u6b64\u5904\u63d2\u5165\u4e00\u6761int 8h\u6307\u4ee4\u5e76\u6267\u884c\uff08count++\uff09
TF\uff08Trace/Trap Flag\uff09\u8ddf\u8e2a/\u9677\u9631\u6807\u5fd7
\u5229\u7528\u5355\u6b65\u6a21\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u53cd\u8c03\u8bd5,\u6f14\u793a\u4ee3\u7801\u89c1\u4ee5\u4e0b\u94fe\u63a5: http://cc.zju.edu.cn/bhh/antidbg.zip
bp\u7684\u9690\u542b\u6bb5\u5730\u5740\u662fss\u300c\u7528\u4ee5\u4ee3\u66ffsp\uff0c\u53ef\u83b7\u5f97\u5806\u6808\u4e2d\u7684\u5185\u5bb9\u300f
code segment\nassume cs:code,ds:data\nmain:\n jmp begin\nold1h dw 0,0\nprev_addr dw offset first,code \n[\u6bb5\u5730\u5740+\u504f\u79fb\u5730\u5740\uff1a2x16\u4f4d]\n[\u5982\uff1aA0 00:00 01 \u662f\u4e00\u4e2a\u5730\u5740]\nbegin:\n push cs\n pop ds\n xor ax,ax\n mov es,ax ;es=0\n mov bx,4 ;bx=4 es:[bx]->int1h \u7684\u4e2d\u65ad\u5411\u91cf(\u8be5\u51fd\u6570\u7684\u9996\u5730\u5740) \u5176\u4e2d\u524d16\u4f4d\u5b58\u653e\u504f\u79fb\u5730\u57400:[4]\uff0c\u540e16\u4f4d\u5b58\u653e\u6bb5\u5730\u57400:[6]\n push es:[bx]\n pop old1h[0]\n push es:[bx+2]\n pop old1h[2]\n mov word ptr es:[bx],offset int1h\n mov word ptr es:[bx+2],cs\n pushf [save old FL \u628aflag\u538b\u5165\u5806\u6808]\n pushf [\u4e3a\u4e86\u4e4b\u540e\u518d\u628aTF\u6539\u62100][and ax,0FEFFh]\n pop ax [\u4e0d\u53ef\u7528mov ax,fl \u5373fl\u548cip\u552f\u4e8c\u4e0d\u80fd\u88ab\u5f15\u7528\u7684\u5bc4\u5b58\u5668]\n or ax,100h [\u5c06TF\u6539\u62101]\uff01TF=1\n push ax\n popf [FL=AX]\nfirst:\n nop \n \u5f53\u67d0\u6307\u4ee4\u6267\u884c\u524dTF=1 \u5219\u8be5\u6307\u4ee4\u6267\u884c\u4e4b\u540e\u624d\u4f1a\u81ea\u52a8\u6267\u884cint01h\u5355\u6b65\u4e2d\u65ad\nsingle\u2014\u2014step_begin:\n\n[first int 1h] \nint1h\u53d1\u751f\u7684\u65f6\u5019 cpu\u4f1a\u505a\u4ee5\u4e0b\u7684\u52a8\u4f5c\uff1a\npushf \u5c06flag\u538b\u5165\u5806\u6808\npush cs \u5c06\u4e0b\u4e00\u6761\u6307\u4ee4\u7684\u6bb5\u5730\u5740\u538b\u5165\u5806\u6808\npush offset back \u5c06\u4e0b\u4e00\u6761\u6307\u4ee4\u7684\u504f\u79fb\u5730\u5740\u538b\u5165\u5806\u6808 <- ss:sp\njmp dword ptr 0:[4]\u8fdb\u5165\u51fd\u6570int1h\n\nback:\n xor ax,ax\n mov cx,3\nnext\uff1a\n add ax,cx ;\u5df2\u89e3\u5bc6\n nop\n loop next\n popf\n nop\nsingle_step_end:\n push old1h[0]\n pop es:[bx]\n push old1h[2]\n pop es:[bx+2]\n mov ah,4Ch\n int 21h\nint1h:\n push bp\n mov bp,sp\n push bx\n push es\n mov bx,cs:prev_addr[0]\n mov es,cs:prev_addr[2]\n inc byte ptr es:[bx]\n mov bx,[bp+2]\n mov es,[bp+4]\n dec byte ptr es:[bx]\n mov cs:prev_addr[0],bx\n mov cs:prev_addr[2],es \n pop es\n pop bx\n pop bp\n iret[\u4e2d\u65ad\u8fd4\u56de cpu\u4f1a\u6267\u884cpop ip \uff0cpop cs\uff0cpopf]\ncode ends\nend main\n
"},{"location":"assembly/%E6%98%BE%E5%8D%A1%E8%BE%93%E5%87%BA/","title":"\u663e\u5361\u8f93\u51fa","text":""},{"location":"assembly/%E6%98%BE%E5%8D%A1%E8%BE%93%E5%87%BA/#_1","title":"\u663e\u5361\u8f93\u51fa","text":"0000\u2014\u2014black\n0001-blue\n0010-green\n0100-red\n0011(BLue and green)\n
code segment\nassume cs:code\nmain:\n mov ax,0B800h\n mov ds,ax\n mov di,0\n mov al,'A'\n mov ah,17h\n mov cx,2000\nagain:\n mov ds:[di],ax\n mov bx,800h\n\nwait_wait:\n mov dx,0\nwait_a_while:\n sub dx,1\n jnz wait_a_while\n sub bx,1\n jnz wait_a_while;1000h\u4e58\u4ee5800h\n\n mov word ptr ds:[di],0020h ;00\u9ed1\u8272\n add di,2\n sub cx,1\n jnz again\n\n mov ah,1\n int 21h (\u7528\u6237\u6572\u952e\u76d8\u518d\u7ed3\u675f)\n mov ah ,4Ch\n int 21h\n\ncode ends\nend main\n
"},{"location":"assembly/%E6%98%BE%E5%8D%A1%E8%BE%93%E5%87%BA/#_2","title":"\u5185\u5b58\u6620\u5c04","text":"dos\u53ca\u7528\u6237\u7a0b\u5e8f\u5360\u7528\u8fd9\u5757\u5185\u5b58\uff0c\u603b\u957f\u4e3a640KB
1000:0000~1000:FFFF\n\u2026\u2026\n9000:0000~9000:FFFF\n
A000:0000~A000:FFFF
mov ah,0 set video mode\nmov al,13h 13\u53f7 320*200\nint 10h\n
al=12h \u5207\u6362\u5230640 * 480 16 \u8272\u56fe\u5f62\u6a21\u5f0f > 640 480 \u8de8\u6bb5\uff0c\u8fd9\u4e2a\u65f6\u5019\u4e0d\u662f\u7528\u4e00\u4e2a\u5b57\u8282\u63a7\u5236\u4e00\u4e2a\u70b9\uff0c\u800c\u662f\u4e00\u4e2abit\u63a7\u5236\u4e00\u4e2a\u70b9 \uff0c\u7528\u56db\u4e2a\u5e73\u9762\u53e0\u8d77\u6765\uff0c\u63a7\u523616\u79cd\u989c\u8272
code segment\nassume cs:code\nmain:\n jmp begin\ni dw 0\nbegin:\n mov ah,00h\n mov al,13h\n int 10h\n mov ax,0A000h\n mov es,ax\n mov di,(100-20)*320+(160-20)\n mov [i],41 \u4e00\u517141\u884c\nnext_row:\n ;push cx\n push di\n mov al,4 ;color=red\n mov cx,41\nnext_dot:\n mov es:[di],al\n add di,1\n sub cx,1\n jnz next_dot\n pop di\n add di,320\n sub [i],1\n jnz next_row\n mov ah,1\n int 21h\n\n mov ah,00h\n mov al,03h;80*25\u7684\u6587\u672c\u6a21\u5f0f\n int 10h\n mov ah ,4Ch\n int 21h\ncode ends\nend main\n
Applications
\u7a0b\u5e8f \u753b\u6c49\u5b57\n\ndata segment:\nhz db 04h,08h,0Eh,0A0h,78h,80h,08h,90h,\u2026\u202610h,0Ch\ndata ends\ncode segment:\nassume cs:code,ds:data\nmain:\n mov ax,data\n mov ds,ax\n mov ax,0A000h\n mov es,ax\n mov di,0\n mov ax,0013h\n int 10h\n mov dx,16\n mov si,0\nnext_row:\n mov ah,hz[si]\n mov al,hz[si+1]\n add si,2\n mov cx,16\ncheck_next_dot:\n shl ax,1\n jnc no_dot\nis_dot:\n mov byte ptr es:[di],0Ch\nno_dot:\n add di,1\n sub cx,1\n jnz check_next_dot\n sub di,16\n add di,320\n sub dx,1\n jnz next_row\n mov ah,1\n int 21h\n mov ax,0003h\n int 10h;\u56de\u523080*25\u6587\u672c\u6a21\u5f0f\n mov ah,4Ch\n int 21h\ncode ends\nend main\n
CPU <\u2014> \u7aef\u53e3(port) <\u2014> I/O\u8bbe\u5907
\u7aef\u53e3\u7f16\u53f7\u5c31\u662f\u7aef\u53e3\u5730\u5740\u3002\u7aef\u53e3\u5730\u5740\u7684\u8303\u56f4\u662f\uff1a[0000h, 0FFFFh]\uff0c\u517165536\u4e2a\u7aef\u53e3
\u5bf9\u7aef\u53e3\u64cd\u4f5c\u4f7f\u7528\u6307\u4ee4in\u4e0eout\u5b9e\u73b0\u3002
in al, 60h
\u4ece\u7aef\u53e360h\u8bfb\u53d6\u4e00\u4e2a\u5b57\u8282\u5e76\u5b58\u653e\u5230AL\u4e2dmov al,2\nout 70h,al \u544a\u8bc9cmos\u63a5\u4e0b\u6765\u8981\u53bb\u8bbf\u95ee\u5b83\u76842\u53f7\u5185\u5b58\u5355\u5143\nin al,71h \u8bfb\u53d6cmos\u4e2d2\u53f7\u5355\u5143\u7684\u503c\uff08\u5206\u949f\uff09\nmov al, 4\nout 70h, al\nmov al, 23h\nout 71h, al; \u628acmos4\u53f7\u5355\u5143\u5373\u5c0f\u65f6\u7684\u503c\u6539\u621023\u70b9\n
"},{"location":"note/basicII/","title":"Basic data Structures II","text":""},{"location":"note/basicII/#graphs","title":"Graphs","text":""},{"location":"note/basicII/#some-defs","title":"Some defs","text":"
\\(Undirected \\ V=n \\ \\ \\ \\ E=C_n^2=\\frac{n(n-1)}{2}\\)
\\(Directed \\ V=n \\ \\ \\ \\ E=2*C_n^2=n(n-1)\\)
\\(Undirected \\ : (v_i,v_j)\\ is \\ incident \\ on\\ v_i\\ and\\ v_j\\)
\\(Directed \\ : v_i \\ is\\ adjacent\\ to \\ v_j \\ \\ \\ \\ \\ v_j \\ is\\ adjacent\\ from \\ v_i\\)
Subgraph
Simple Path
Connected
\\(Undirected:\\) An undirected graph G is connected if every pair of distinct \\(v_i\\) and \\(v_j\\) are connected
\\(Directed:\\)
Strongly connected directed graph G = for every pair of \\(v_i\\) and \\(v_j\\) in V( G ), there exist directed paths from \\(v_i\\) to \\(v_j\\) and from \\(v_j\\) to \\(v_i\\).
If the graph is connected without direction to the edges, then it is said to be weakly connected
Strongly connected component : the maximal subgraph that is strongly connected
For a directed G, we have in-degree and out-degree.
\\(r=v-e+2\\)
\\(adj_- mat[i][j]=\\left\\{\\begin{array}{l} 1 \\ if (v_i,v_j) \\ or <v_i,v_j> \\in E(G)\\\\0\\ \\ otherwise \\end{array}\\right.\\)
If G is undirected the matrix is symmetric,thus sorting only half of the matrix
The trick is to store the matrix as a 1-D array: adj_mat [ $n(n+1)/2 $] = \\({ a_{11}, a_{21}, a_{22}, ..., a_{n1}, ..., a_{nn} }\\)
The index for \\(a_{ij}\\) is \\(i( i - 1 )/2+j\\).
\\(\\begin{align*}degree(i) &= \\sum_{j=0}^{n-1}adj_-mat[i][j] \\ (If\\ G\\ is\\ undirected)\\\\ & \\ \\ +\\sum_{j=0}^{n-1}adj_-mat[j][i]\\ (If\\ G\\ is\\ directed)\\end{align*}\\)
"},{"location":"note/basicII/#adjacency-lists","title":"Adjacency Lists","text":"Degree( \\(i\\) ) = number of nodes in graph[ \\(i\\) ] (if \\(G\\) is undirected).
T of examine (whether complete) E(G) = O( n + e )
A. Add inverse adjacency lists
B.Multilists
"},{"location":"note/basicII/#adjacency-multilist","title":"Adjacency Multilist","text":"Digraph G in which V( G ) represents activities ( e.g. the courses ) and E( G ) represents precedence relations
If the precedence relation is reflexive, then there must be an i such that i is a predecessor of i.
That is, i must be done before i is started. Therefore if a project is feasible, it must be irreflexive.
A topological order is a linear ordering of the vertices of a graph such that, for any two vertices, i, j, if i is a predecessor of j in the network then i precedes j in the linear ordering.
Test an AOV for feasibility, and generate a topological order if possible.
Method One \\(T=O(|V|^2)\\)
void Topsort( Graph G )\n{ int Counter;\n Vertex V, W;\n for ( Counter = 0; Counter < NumVertex; Counter ++ ) {\n V = FindNewVertexOfDegreeZero( );\n if ( V == NotAVertex ) {\n Error ( \u201cGraph has a cycle\u201d ); break; }\n TopNum[ V ] = Counter; /* or output V */\n for ( each W adjacent to V )\n Indegree[ W ] \u2013 \u2013 ;\n }\n}\n
void Topsort( Graph G )\n{ Queue Q;\n int Counter = 0;\n Vertex V, W;\n Q = CreateQueue( NumVertex ); MakeEmpty( Q );\n for ( each vertex V )\n if ( Indegree[ V ] == 0 ) Enqueue( V, Q );\n while ( !IsEmpty( Q ) ) {\n V = Dequeue( Q );\n TopNum[ V ] = ++ Counter; /* assign next */\n for ( each W adjacent to V )\n if ( \u2013 \u2013 Indegree[ W ] == 0 ) Enqueue( W, Q );\n } /* end-while */\n if ( Counter != NumVertex )\n Error( \u201cGraph has a cycle\u201d );\n DisposeQueue( Q ); /* free memory */\n}\n
"},{"location":"note/basicII/#midterm-review","title":"Midterm Review","text":"Which of the following statements is TRUE about topological sorting? (5\u5206)
3 is true
"},{"location":"note/basicII/#shortest-path-problem","title":"Shortest Path Problem","text":""},{"location":"note/basicII/#1single-source-shortest-path-problem","title":"1.Single-Source Shortest Path Problem","text":"void unweighted(Table T){\n int CurrDist;\n Vertex V,W;\n for(CurrDist=0;CurrDist<NumVertex;CurrDist++){\n for(each vertex V){ \n if(!T[V].Known&&T[V].Dust==CurrDist){\n R[V].Known=true;\n for(each W adjacent to V){\n if(T[W].Dist==infinity){\n T[W].Dist=CurrDist+1;\n T[E].Path=V;\n }\n }\n }\n }\n }\n}\n
But the time complexity is \\(O(|V|^2)\\)
Improvement
void unweighted (Table T){\n Queue Q;\n Vertex V,W;\n Q=CreateQueue(NumVertex);MakeEmpty(Q);\n Enqueue(S,Q);\n while(!isEmpty(Q)){\n V=Dequeue(Q);\n T[V].known=true;//not really necessary\n for(each W adjacent to V){\n if(T[W].Dist==Infinity){\n T[W].Dist=T[V].Dist+1;\n T[W].Path=V;\n ENqueue(W,Q)\n }\n }\n }\n DisposeQueue(Q);\n}\n
void Dijkstra(Table T){\n //T is initialized by Figure 9.30 on p 303\n Vertex V,W;\n for(;;){\n V=smallest unknown distance vertex;\n if(V==NotAVertex){break;}\n T[V].known=true;\n for(each W adjacent to V){\n if(!T[W].Known){\n if(T[V].Dist+Cvw<T[W].Dist){\n Decrease(T[W].Dist to T[V].Dist+Cvw);\n T[W].Path=V\n }\n }\n }\n }\n}\n
\\(T = O( |V|^2 + |E| )\\)
Initialization: The initialization phase involves traversing all vertices, setting their distances to infinity, and setting the initial vertex's distance to 0. The time complexity of this step is O(V), where V is the number of vertices.
Main Loop: The number of iterations in the main loop depends on the number of vertices. In each iteration, the algorithm selects the smallest unknown distance vertex V and then traverses all vertices W adjacent to V. For each W, it checks if there is a shorter path through V to W, and if so, it updates the distance of W.
The time complexity of this step is \\(O(V^2)\\), as, for each vertex V, all vertices adjacent to V are considered.
Finding the Minimum Distance Vertex: In the main loop, the algorithm needs to find the smallest unknown distance vertex V. The time complexity of this step is O(V^2), as it needs to check the distance of each vertex.
In summary, the time complexity of the Dijkstra algorithm is \\(O(V^2)\\).
Implementation 2
V = smallest unknown distance vertex: Keep distances in a priority queue and call DeleteMin \u2013 \\(O(log|V|)\\)
Decrease( T[ W ].Dist to T[ V ].Dist + Cvw )
\\(T = O( |V|log|V|+|E|log|V|)=O(|E|log|V|)\\) ----Good if the graph is sparse
Must keep doing DeleteMin until an unknown vertex emerges
\\(T = O(|E| log|V| )\\) but requires \\(|E|\\) DeleteMin with \\(|E|\\) space
void WeightedNegative( Table T )\n{ Queue Q;\n Vertex V, W;\n Q = CreateQueue (NumVertex ); MakeEmpty( Q );\n Enqueue( S, Q ); /* Enqueue the source vertex */\n while (!IsEmpty(Q)){\n V=Dequeue(Q);/* each vertex can dequeue at most |V| times */\n for(each W adjacent to V){\n if ( T[ V ].Dist + Cvw < T[ W ].Dist ){/* no longer once per edge */\n T[ W ].Dist = T[ V ].Dist + Cvw;\n T[ W ].Path = V;\n if(W is not already in Q){Enqueue(W,Q)}\n }/* end-if update */ \n }\n }/* end-while */\n DisposeQueue( Q ); /* free memory */\n}\n
"},{"location":"note/basicII/#acyclic-graphs","title":"Acyclic Graphs","text":"If the graph is acyclic, vertices may be selected in topological order since when a vertex is selected, its distance can no longer be lowered without any incoming edges from unknown nodes.
\\(T=O(|E| + |V|)\\) and no priority queue is needed.
Application: AOE ( Activity On Edge ) Networks \u2014\u2014 scheduling a project
"},{"location":"note/basicII/#all-pairs-shortest-path-problem","title":"All-pairs Shortest path problem","text":""},{"location":"note/basicII/#network-flow-problem","title":"Network Flow Problem","text":""},{"location":"note/basicII/#ford-fulkerson-algorithm","title":"Ford-Fulkerson Algorithm","text":"If the edge capabilities are rational numbers, this algorithm always terminate with a maximum flow.
The algorithm works for G with cycles as well.
Analysis
"},{"location":"note/basicII/#1an-augmenting-path-can-be-found-by-an-unweighted-shortest-path-algorithm","title":"1.An augmenting path can be found by an unweighted shortest path algorithm.","text":"\\(T = O( f\\cdot|E| )\\) where f is the maximum flow.
"},{"location":"note/basicII/#2always-choose-the-augmenting-path-that-allows-the-largest-increase-in-flow","title":"2.always choose the augmenting path that allows the largest increase in flow.","text":"\\(\u300cmodify\\ Dijkstra\u2019s\\ algorithm\\ \u300d\\)
\\(\\begin{align*}T&=T_{argmentation}*T_{find\\ a\\ path}\\\\ &=O(E)log(cap_{max})*O(|E|log|V|)\\\\ &= O( |E|^2log|V|).\\end{align*}\\) if capmax is a small integer
"},{"location":"note/basicII/#3always-choose-the-augmenting-path-that-has-the-least-number-of-edges","title":"3.Always choose the augmenting path that has the least number of edges.","text":"Simple \\(BSF\\) Unweighted shortest path algorithm
\\(\\begin{align*}T&=T_{argmentation}*T_{find\\ a\\ path}\\\\ &=O(E)*O(|E||V|)\\\\ &= O( |E|^2|V|).\\end{align*}\\)
(1) we must use only edges within the graph
(2) we must use exactly |V| -1 edges
(3) we may not use edges that would produce a cycle.
"},{"location":"note/basicII/#1prims-algorithm-grow-a-tree","title":"1.Prim\u2019s Algorithm \u2013 grow a tree","text":""},{"location":"note/basicII/#2krukals-algorithm-grow-a-tree","title":"2.Krukal\u2019s Algorithm \u2013 grow a tree","text":"void Kruskal ( Graph G )\n{ T = { } ;\n while ( T contains less than |V| - 1 edges \n && E is not empty ) {\n choose a least cost edge (v, w) from E ; /* Delete Min */\n delete (v, w) from E ;\n if ( (v, w) does not create a cycle in T ) /Union Find/\n add (v, w) to T ;*\n else \n discard (v, w) ;\n }\n if ( T contains fewer than |V| - 1 edges )\n Error ( \u201cNo spanning tree\u201d ) ;\n}\n
"},{"location":"note/basicII/#applications-of-depth-first-search","title":"Applications of Depth-First Search","text":"void DFS ( Vertex V ) /* this is only a template */\n{ visited[ V ] = true; /* mark this vertex to avoid cycles */\n for ( each W adjacent to V )\n if ( !visited[ W ] )\n DFS( W );\n} \n/* T = O( |E| + |V| ) as long as adjacency lists are used */\n
"},{"location":"note/basicII/#biconnectivity","title":"Biconnectivity","text":"Refert to https://www.baeldung.com/cs/scc-tarjans-algorithm
A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph.
There is no back edge from one SCC to another (There can be cross edges, but cross edges will not be used while processing the graph).
Case1 (Tree Edge): If node v is not visited already, then after the DFS of v is complete, a minimum of low[u] and low[v] will be updated to low[u]. low[u] = min(low[u], low[v])
#include <iostream>\n#include <cstring>\nusing namespace std;\nconst int maxn = 1005; // \u5047\u8bbe\u6700\u5927\u8282\u70b9\u6570\u4e3a1005\nint G[maxn][maxn]; // \u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\nint deg[maxn]; // \u8282\u70b9\u7684\u5ea6\nint ans[maxn]; // \u5b58\u50a8\u7ed3\u679c\u7684\u6570\u7ec4\nint ansi = 0; // \u7ed3\u679c\u6570\u7ec4\u7684\u7d22\u5f15\nbool visited[maxn]; // \u6807\u8bb0\u8282\u70b9\u662f\u5426\u88ab\u8bbf\u95ee\u8fc7\nvoid dfs(int x) {\n for (int y = 1; y <= maxn; ++y) {\n if (G[x][y]) {\n G[x][y] = G[y][x] = 0;\n dfs(y);\n }\n }\n ans[++ansi] = x;\n}\nint main() {\n // ... \u8bfb\u53d6\u8f93\u5165\uff0c\u521d\u59cb\u5316 G \u548c deg\n int cnt = 0, root = 0;\n for (int i = 1; i <= maxn; ++i) {\n if (deg[i] % 2) {\n cnt++;\n if (!root) root = i;\n }\n }\n if (!root) {\n for (int i = 1; i <= maxn; ++i) {\n if (deg[i]) {\n root = i; break;\n }\n }\n }\n if (cnt && cnt != 2) {\n cout << \"No Solution\\n\";\n return 0;\n }\n dfs(root);\n // \u8f93\u51fa\u7ed3\u679c\n for (int i = ansi; i > 0; --i) {\n cout << ans[i] << ' ';}\n cout << '\\n';\n return 0;\n}\n
"},{"location":"note/basicII/#hamilton-cycle","title":"Hamilton cycle","text":""},{"location":"note/hash/","title":"HASH","text":""},{"location":"note/hash/#hashing","title":"HASHING","text":""},{"location":"note/hash/#interpolation-search","title":"Interpolation Search","text":"
Interpolation Search is a searching algorithm used to find a specific element in a sorted array. Similar to binary search, it employs a divide-and-conquer strategy, but it differs by estimating the probable position of the target value based on the distribution of the search key and array elements at each step.
The algorithm proceeds as follows:
Assume the array is sorted.
Calculate the interpolation position using the formula:
\\(position=low+\u230a\\frac{(high\u2212low)\u00d7(key\u2212arr[low])}{arr[high]\u2212arr[low]\u230b}\\)
Here, \\(arr\\) is the array, and \\(low\\) and \\(high\\) are the array indices, while \\(key\\) is the element to be searched.
If \\(arr[position]=key\\), the target element is found, and the position positionposition is returned.
If \\(arr[position]<key\\), continue the search in the right half by updating \\(low=position+1\\)
If \\(arr[position]>key\\), continue the search in the left half by updating \\(high=position\u22121\\)
Repeat steps 2 to 5 until the target element is found or \\(low>high\\).
Interpolation Search excels when dealing with uniformly distributed sorted arrays, as it efficiently estimates the target element's position, reducing the number of search iterations. However, its effectiveness relies on the assumption of a roughly uniform distribution of array elements. In cases of uneven data distribution, other search algorithms may outperform Interpolation Search. Binary search, for instance, could be more stable as it is less sensitive to the distribution of array elements.
"},{"location":"note/hash/#hash-tables","title":"Hash Tables","text":"https://www.geeksforgeeks.org/hashing-data-structure/#introduction
For numbers\uff1a
\uff081\uff09\u6c42\u4f59\u6cd5
\uff082\uff09\u5e73\u65b9\u53d6\u4e2d
\uff083\uff09\u6298\u53e0\u6cd5
\uff084\uff09\u6570\u5b57\u5206\u6790\u6cd5
\\(f(x)=(\\sum x[N-i-1]*32^i)\\%TableSize\\)
Index Hash3( const char *x, int TableSize ) {\n unsigned int HashVal = 0; \n/* 1*/ while( *x != '\\0' ) \n/* 2*/ HashVal = ( HashVal << 5 ) + *x++; \n/* 3*/ return HashVal % TableSize; \n} \n
struct ListNode; \ntypedef struct ListNode *Position; \nstruct HashTbl; \ntypedef struct HashTbl *HashTable; \nstruct ListNode { \n ElementType Element; \n Position Next; \n}; \ntypedef Position List; \n/* List *TheList will be an array of lists, allocated later */ \n/* The lists use headers (for simplicity), */ \n/* though this wastes space */ \nstruct HashTbl { \n int TableSize; \n List *TheLists; \n};\n
"},{"location":"note/hash/#create-an-empty-table","title":"Create an empty table","text":"HashTable InitializeTable( int TableSize ) \n{ HashTable H; \n int i; \n if ( TableSize < MinTableSize ) { \n Error( \"Table size too small\" ); return NULL; \n } \n H = malloc( sizeof( struct HashTbl ) ); /* Allocate table */\n if ( H == NULL ) FatalError( \"Out of space!!!\" ); \n H->TableSize = NextPrime( TableSize ); /* Better be prime */\n H->TheLists = malloc( sizeof( List ) * H->TableSize ); /*Array of lists*/\n if ( H->TheLists == NULL ) FatalError( \"Out of space!!!\" ); \n for( i = 0; i < H->TableSize; i++ ) { /* Allocate list headers */\n H->TheLists[ i ] = malloc( sizeof( struct ListNode ) ); /* Slow! */\n if ( H->TheLists[ i ] == NULL ) FatalError( \"Out of space!!!\" ); \n else H->TheLists[ i ]->Next = NULL;\n } \n return H; \n} \n
"},{"location":"note/hash/#find-a-key-from-a-hash-table","title":"Find a key from a hash table","text":"Position Find ( ElementType Key, HashTable H ) \n{ \n Position P; \n List L; \n L = H->TheLists[ Hash( Key, H->TableSize ) ]; \n\n P = L->Next; \n while( P != NULL && P->Element != Key ) /* Probably need strcmp */ \n P = P->Next; \n return P; \n} \n
"},{"location":"note/hash/#insert-a-key-into-a-hash-table","title":"Insert a key into a hash table","text":"void Insert ( ElementType Key, HashTable H ) \n{ \n Position Pos, NewCell; \n List L; \n Pos = Find( Key, H ); \n if ( Pos == NULL ) { /* Key is not found, then insert */\n NewCell = malloc( sizeof( struct ListNode ) ); \n if ( NewCell == NULL ) FatalError( \"Out of space!!!\" ); \n else { \n L = H->TheLists[ Hash( Key, H->TableSize ) ]; \n NewCell->Next = L->Next; \n NewCell->Element = Key; /* Probably need strcpy! */ \n L->Next = NewCell; \n } \n } \n} \n
"},{"location":"note/hash/#open-addressing","title":"Open Addressing","text":"Solve Collision
Algorithm: insert key into an array of hash table\n{\n index = hash(key);\n initialize i = 0 ------ the counter of probing;\n while ( collision at index ) {\n index = ( hash(key) + f(i) ) % TableSize;\n if ( table is full ) break;\n else i ++;\n }\n if ( table is full ) ERROR (\u201cNo space left\u201d);\n else insert key at index;\n}\n
"},{"location":"note/hash/#linear-probing","title":"Linear Probing","text":"\\(f(i)=i\\)
\\(f(i)=i^2\\)
Position Find ( ElementType Key, HashTable H ) \n{ Position CurrentPos; \n int CollisionNum; \n CollisionNum = 0; \n CurrentPos = Hash( Key, H->TableSize ); \n while( H->TheCells[ CurrentPos ].Info != Empty && H->TheCells[ CurrentPos ].Element != Key) { \n CurrentPos += 2 * (++CollisionNum)-1; \n if ( CurrentPos >= H->TableSize ) CurrentPos \uf02d = H->TableSize; \n } \n return CurrentPos; \n} \n
void Insert ( ElementType Key, HashTable H ){ \n Position Pos; \n Pos = Find( Key, H ); \n if ( H->TheCells[ Pos ].Info != Legitimate ) { /* OK to insert here */ \n H->TheCells[ Pos ].Info = Legitimate; \n H->TheCells[ Pos ].Element = Key; /* Probably need strcpy */ \n } \n} \n
Insertion will be seriously slowed down if there are too many deletions intermixed with insertions.
Although primary clustering is solved, secondary clustering occurs \u2013 that is, keys that hash to the same position will probe the same alternative cells.
"},{"location":"note/hash/#double-hashing","title":"Double Hashing","text":"\\(f ( i ) = i * hash_2( x )\\)
// Reverse a linked list\nq=h;\np=NUll;\nwhile(q){\n t = q->next;\n q->next = p;\n p=q;\n q=t;\n}\nh = p;\n\nLNode *reverse( LNode *head ){\n LNode *p0,*head1;\n head1=NULL;\n for(p0=head->next;p0;){\n LNode *temp=p0->next;//important!!!\n p0->next =head1;\n head1=p0;\n p0=temp;\n }\n return head1;\n}\n
* multilist * sparse matrix representation"},{"location":"note/sort/","title":"Sort","text":""},{"location":"note/sort/#simple-sort","title":"Simple Sort","text":""},{"location":"note/sort/#algorithm","title":"Algorithm","text":""},{"location":"note/sort/#bubble-sort","title":"Bubble sort","text":"
Basic
void bubbleSort(int arr[], int n) {\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < n - i - 1; j++) {\n if (arr[j] > arr[j + 1]) {\n int temp = arr[j];\n arr[j] = arr[j + 1];\n arr[j + 1] = temp;\n }\n }\n }\n}\n
Improvement
void bubbleSort_recursive(int a[],int begin,int end,int n){\n if(end>begin){\n int lastswap=-1;\n for(int i=begin;i<end;i++){\n if(a[i]>a[i+1]){\n int m=a[i];\n a[i]=a[i+1];\n a[i+1]=m; \n lastswap=i;\n }\n }\n end=lastswap;\n sort(a,begin,end,n);\n}\n\nvoid bubbleSort_iter (int a[], int n){\n int lastswap, end;\n for (int i=0;i<n-1;i++) {\n lastswap = -1;\n for (int j=0;j<end;j++) {\n if (a[j] > a[j + 1]) {\n exchange(&a[j], &a[j + 1]);\n lastswap = j;\n }\n }\n end = lastswap;\n }\n}\n
"},{"location":"note/sort/#selection-sort","title":"Selection sort","text":"void selectionSort(int arr[], int n) {\n int i, j, min_idx;\n // \u5916\u5c42\u5faa\u73af\u904d\u5386\u672a\u6392\u5e8f\u90e8\u5206\n for (i = 0; i <n-1;i++) {\n // \u5047\u8bbe\u5f53\u524d\u672a\u6392\u5e8f\u90e8\u5206\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e3a\u6700\u5c0f\n min_idx = i;\n // \u5185\u5c42\u5faa\u73af\u5728\u672a\u6392\u5e8f\u90e8\u5206\u4e2d\u627e\u5230\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n for (j=i+1;j<n;j++) {\n if (arr[j] < arr[min_idx]) {\n min_idx = j;\n }\n }\n // \u5c06\u627e\u5230\u7684\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u90e8\u5206\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4ea4\u6362\u4f4d\u7f6e\n swap(&arr[min_idx], &arr[i]);\n }\n}\n
"},{"location":"note/sort/#insertion-sort","title":"Insertion Sort","text":"void InsertionSort ( ElementType A[ ], int N ){\n int j,P;\n ElementType Tmp;\n for(P=1;P<N;P++){\n Tmp=A[p];\n for(j=P;j>0&&A[j-1]>Tmp;j--){\n A[j]=A[j-1];\n }\n A[j]=Tmp;\n }\n}\n
The worst case:Input \\(A[ \\ \\ ]\\) is in reverse order. \\(T( N ) = O( N^2 )\\)
"},{"location":"note/sort/#analysis","title":"Analysis","text":"\\(T ( N, I )\\) = \\(O( I+N )\\) where \\(I\\) is the number of inversions in the original array.
The average number of inversions in an array of N distinct numbers is \\(N ( N +1 ) / 4\\).
Original Shell Sequence: The original sequence suggested by Donald Shell was to use gaps of n/2
, n/4
, n/8
, ..., 1
. This is a simple and effective sequence, often called the \"original Shell sequence.\" It is easy to implement and works well in practice.
for (int gap = n/2; gap > 0; gap /= 2) {\n // Shell sort logic\n}\n
Knuth's Sequence: Donald Knuth suggested a formula for computing gaps, which is 3^k - 1
for k = 0, 1, 2, ...
. This sequence often performs well in practice.
for (int gap = 1; gap < n/3; gap = 3 * gap + 1) {\n // Shell sort logic\n}\n
Sedgewick's Sequence: Robert Sedgewick proposed a sequence that interleaves powers of 2 and 3: 1, 5, 19, 41, 109, 209, 505, ...
. It combines the simplicity of the original Shell sequence with the effectiveness of the Knuth sequence.
int sedgewick[] = {1, 5, 19, 41, 109, 209, 505, ...}; // Extend as needed\nint sedgewickIndex = 0;\nfor (int gap = sedgewick[sedgewickIndex]; gap > 0; gap = sedgewick[++sedgewickIndex]) {\n // Shell sort logic\n}\n
the terms are either of the form \\(9\\times 4^i -9\\times 2^i + 1\\) or \\(4^i -3\\times 2^i + 1\\). \\(T_{avg} ( N ) = O ( N^{7/6} )\\) and \\(T_{worst} ( N ) = O ( N^{4/3} )\\). void shellSort(int arr[], int n) {\n // ps:we can choose any ideal sequence suitable for given cases\n for (int gap = n/2; gap > 0; gap /= 2) {\n for (int i = gap; i < n; i++) {\n int temp = arr[i];\n int j;\n for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {\n arr[j] = arr[j - gap];\n }\n arr[j] = temp;\n }\n }\n}\n
"},{"location":"note/sort/#worst-case-analysis","title":"Worst-Case Analysis:","text":"The worst-case running time of Shellsort, using Shell's increments, is \\(O(N^2)\\). * example : 1 9 2 10 3 11 4 12 5 13 6 14
\\(h_k = 2^k -1\\) ---- consecutive increments have no common factors. The worst-case running time of Shellsort, using Hibbard's increments, is \\(O( N^{3/2})\\).
"},{"location":"note/sort/#heap-sort","title":"Heap Sort","text":""},{"location":"note/sort/#algorithm-1","title":"Algorithm 1:","text":"Algorithm 1:\n{\n BuildHeap( H ); //O(N)\n for (int i=0; i<N; i++ ){\n TmpH[ i ] = DeleteMin( H );O(logN)\n }\n for ( i=0; i<N; i++ ){\n H[ i ] = TmpH[ i ];\n }\n}\n
\\(T ( N ) = O ( N log N )\\) but space needed is doubled."},{"location":"note/sort/#algorithm-2","title":"Algorithm 2:","text":"Review
void PercolateDown( int p, PriorityQueue H ){ \n int temp=H->Elements[p];\n int parent,son;\n for(parent=p;parent*2<=H->Size;parent=son){\n son=parent*2;\n if(son<H->Size&&H->Elements[son]<H->Elements[son+1]){\n son++; \n }\n if(temp>=H->Elements[son]){break;} \n else{H->Elements[parent]=H->Elements[son];}\n }\n H->Elements[parent]=temp;\n}\n
void Heapsort( ElementType A[ ], int N ) \n{ int i; \n for ( i = N / 2; i >= 0; i - - ) /* BuildHeap */ {PercDown( A, i, N ); }\n for ( i = N - 1; i > 0; i - - ) { \n Swap( &A[ 0 ], &A[ i ] ); /* DeleteMax */ \n PercDown( A, 0, i ); \n } \n}\n
The average number of comparisons used to heapsort a random permutation of N distinct items is \\(2N log N - O( N log log N )\\). Although Heapsort gives the best average time, in practice it is slower than a version of Shellsort that uses Sedgewick's increment sequence.
"},{"location":"note/sort/#mergesort","title":"Mergesort","text":"void Mergesort( ElementType A[ ], int N );\nvoid MSort( ElementType A[ ], ElementType TmpArray[ ],int Left,int Right);\nvoid Merge( ElementType A[ ], ElementType TmpArray[ ],int Lpos,int Rpos, int RightEnd);\n\nvoid Mergesort( ElementType A[ ], int N ) {\n ElementType *TmpArray; /* need O(N) extra space */\n TmpArray = malloc( N * sizeof( ElementType ) ); \n if ( TmpArray != NULL ) { \n MSort( A, TmpArray, 0, N - 1 ); \n free( TmpArray ); \n } \n else FatalError( \"No space for tmp array!!!\" ); \n}\n\nvoid MSort( ElementType A[ ], ElementType TmpArray[ ],int Left, int Right){\n int Center; \n if ( Left < Right ) { /* if there are elements to be sorted */\n Center = ( Left + Right ) / 2; \n MSort( A, TmpArray, Left, Center ); /* T( N / 2 ) */\n MSort( A, TmpArray, Center + 1, Right ); /* T( N / 2 ) */\n Merge( A, TmpArray, Left, Center + 1, Right ); /* O( N ) */\n } \n} \n\n/* Lpos = start of left half, Rpos = start of right half */ \nvoid Merge( ElementType A[ ], ElementType TmpArray[ ], int Lpos, int Rpos, int RightEnd ) {\n int i, LeftEnd, NumElements, TmpPos; \n LeftEnd = Rpos - 1; \n TmpPos = Lpos; \n NumElements = RightEnd - Lpos + 1; \n while(Lpos<=LeftEnd && Rpos<=RightEnd){\n if(A[Lpos]<=A[Rpos]){TmpArray[TmpPos++]=A[Lpos++];}\n else{TmpArray[TmpPos++]=A[Rpos++];}\n }\n while(Lpos<=LeftEnd){TmpArray[ TmpPos++ ] = A[ Lpos++ ];}\n while(Rpos<=RightEnd){TmpArray[ TmpPos++ ] = A[ Rpos++ ];}\n for( i = 0; i < NumElements; i++, RightEnd - - ) \n /* Copy TmpArray back */ \n A[ RightEnd ] = TmpArray[ RightEnd ]; \n}\n/** version2 **/\nvoid Merge2( ElementType A[ ], ElementType TmpArray[ ], int Lpos, int Rpos, int RightEnd ) {\n int i, LeftEnd, NumElements, TmpPos; \n LeftEnd = Rpos - 1; \n TmpPos = Lpos; \n NumElements = RightEnd - Lpos + 1; \n for(int i=0;i<NumElements;i++){\n if((Lpos<=LeftEnd)&&(Rpos>RightEnd||A[Lpos]<=A[Rpos])){TmpArray[i]=A[Lpos++];}\n else{TmpArray[TmpPos++]=A[Rpos++];}\n }\n}\n/** version3 **/\nvoid CopyArray(int B[],int iBegin,int iEnd,int A[]);\nvoid Merge (int A[],int ileft,int iright,int iend,int B[]);\nvoid BottomupMergeSort(int A[],int B[],int n){\n for(int width=1;width<n;width=width*2){\n for(int i=0;i<n;i=i+2*width){\n Merge(A,i,min(i+width,n),min(i+2*width,n),B);\n }\n CopyArray(B,0,n,A);\n }\n}\n// (iBegin,iEnd]\nvoid Merge (int A[],int ileft,int iright,int iend,int B[]){\n int i=ileft,j=iright;\n for(int k=ileft;k<iend;k++){\n if(i<iright&&(j>=iend||A[i]<=A[j])){\n B[k]=A[i++];\n }\n else{B[k]=A[j++];}\n }\n}\n
"},{"location":"note/sort/#complexity","title":"Complexity","text":"\\(T(n)=2T(n/2)+O(n)\\) \u2192 \\(O(nlogn)\\) Note: Mergesort requires linear extra memory, and copying an array is slow. It is hardly ever used for internal sorting, but is quite useful for external sorting.
"},{"location":"note/sort/#quick-sort","title":"Quick Sort","text":""},{"location":"note/sort/#picking-the-pivot","title":"Picking the Pivot","text":"A Safe Maneuver: \\(Pivot = random\\ select\\ from\\ A[\\ \\ ]\\)
Median-of-Three Partitioning : \\(Pivot\\ = median ( left, center, right )\\)
Eliminates the bad case for sorted input and actually reduces the running time by about 5%.
"},{"location":"note/sort/#small-arrays","title":"Small Arrays","text":"Problem: Quicksort is slower than insertion sort for small N ( \\(\\le\\)20 ).
Solution : Cutoff when N gets small ( e.g. N = 10 ) and use other efficient algorithms (such as insertion sort).
"},{"location":"note/sort/#implementation","title":"Implementation","text":"void Quicksort( ElementType A[ ], int N ) \n{ \n Qsort( A, 0, N - 1 ); \n /* A: the array */\n /* 0: Left index */\n /* N \u2013 1: Right index */\n}\n\n/* Return median of Left, Center, and Right */ \n/* Order these and hide the pivot */ \nElementType Median3( ElementType A[ ], int Left, int Right ) \n{ \n int Center = ( Left + Right ) / 2; \n if ( A[ Left ] > A[ Center ] ) \n Swap( &A[ Left ], &A[ Center ] ); \n if ( A[ Left ] > A[ Right ] ) \n Swap( &A[ Left ], &A[ Right ] ); \n if ( A[ Center ] > A[ Right ] ) \n Swap( &A[ Center ], &A[ Right ] ); \n /* Invariant: A[ Left ] <= A[ Center ] <= A[ Right ] */ \n Swap( &A[ Center ], &A[ Right - 1 ] ); /* Hide pivot */ \n /* only need to sort A[ Left + 1 ] \u2026 A[ Right \u2013 2 ] */\n return A[ Right - 1 ]; /* Return pivot */ \n}\n\nvoid Qsort( ElementType A[ ], int Left, int Right ) \n{ int i, j; \n ElementType Pivot; \n if( Left + Cutoff <= Right ){ /* if the sequence is not too short */\n Pivot = Median3( A, Left, Right ); /* select pivot */\n i = Left; \n j = Right \u2013 1; /* why not set Left+1 and Right-2? */\n for( ; ; ) { \n while ( A[ ++i ] < Pivot ) { } /* scan from left */\n while ( A[ \u2013\u2013j ] > Pivot ) { } /* scan from right */\n if ( i < j ) {Swap( &A[ i ], &A[ j ] ); } /* adjust partition */\n else {break; } /* partition done */\n } \n Swap( &A[ i ], &A[ Right - 1 ] ); /* restore pivot */ \n Qsort( A, Left, i - 1 ); /* recursively sort left part */\n Qsort( A, i + 1, Right ); /* recursively sort right part */\n } /* end if - the sequence is long */\n\n else /* do an insertion sort on the short subarray */ \n {InsertionSort( A + Left, Right - Left + 1 );}\n}\n
"},{"location":"note/sort/#sorting-large-structures","title":"Sorting Large Structures","text":"Swapping large structures can be very much expensive.
Any algorithm that sorts by comparisons only must have a worst case computing time of \\(\\Omega( N log N )\\).
When sorting N distinct elements, there are \\(N!\\) (N factorial) different possible sorting outcomes. This arises from the numerous ways of arranging the elements, with \\(N\\) choices for the first element, \\(N-1\\) choices for the second, and so forth, yielding a total of \\(N!\\).
In the context of decision trees representing the decision-making process of comparison-based sorting algorithms, the number of leaves in the tree is at least \\(N!\\). Each leaf node signifies a distinct sorting outcome.
The relationship between the height of the decision tree \\((k)\\) and complete binary trees is emphasized. For a complete binary tree with \\(k\\) levels, it can have at most \\(2^{k-1}\\) leaves. Combining this with the earlier discussion, the inequality \\(N! \u2264 2^{k-1}\\) is established, where k is the height of the decision tree.
Expressing the relationship between height and N! using logarithms, we find that \\(k \u2265 log(N!) + 1\\).
These insights delineate theoretical limits for comparison-based sorting algorithms and underscore the significance of N log N as the theoretical lower bound for their time complexity.
"},{"location":"note/sort/#bucket-sort-and-radix-sort","title":"Bucket Sort and Radix Sort","text":""},{"location":"note/sort/#bucket-sort-tnmomn","title":"Bucket Sort \\(T(N,M)=O(M+N)\\)","text":"Algorithm{\n initialize count[ ];\n while (read in a student\u2019s record)\n insert to list count[stdnt.grade];\n for (i=0; i<M; i++) {\n if (count[i])\n output list count[i];\n }\n}\n
* What if \\(M >> N\\) ?"},{"location":"note/sort/#radix-sort","title":"Radix Sort","text":""},{"location":"note/sort/#stableness","title":"Stableness","text":""},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#sparse-matrix","title":"SPARSE MATRIX","text":"
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).
#include <iostream>\nusing namespace std;\n\nint main()\n{\n // Assume 4x5 sparse matrix\n int sparseMatrix[4][5] =\n {\n {0 , 0 , 3 , 0 , 4 },\n {0 , 0 , 5 , 7 , 0 },\n {0 , 0 , 0 , 0 , 0 },\n {0 , 2 , 6 , 0 , 0 }\n };\n\n int size = 0;\n for (int i = 0; i < 4; i++)\n for (int j = 0; j < 5; j++)\n if (sparseMatrix[i][j] != 0)\n size++;\n\n // number of columns in compactMatrix (size) must be\n // equal to number of non - zero elements in\n // sparseMatrix\n int compactMatrix[3][size];\n\n // Making of new matrix\n int k = 0;\n for (int i = 0; i < 4; i++)\n for (int j = 0; j < 5; j++)\n if (sparseMatrix[i][j] != 0)\n {\n compactMatrix[0][k] = i;\n compactMatrix[1][k] = j;\n compactMatrix[2][k] = sparseMatrix[i][j];\n k++;\n } \n\n for (int i=0; i<3; i++)\n {\n for (int j=0; j<size; j++)\n cout <<\" \"<< compactMatrix[i][j];\n cout <<\"\\n\";\n }\n return 0;\n}\n
#include<stdio.h>\n#include<stdlib.h>\n\n// Node to represent sparse matrix\nstruct Node\n{\n int value;\n int row_position;\n int column_postion;\n struct Node *next;\n};\n\n// Function to create new node\nvoid create_new_node(struct Node** start, int non_zero_element,int row_index, int column_index )\n{\n struct Node *temp, *r;\n temp = *start;\n if (temp == NULL)\n {\n // Create new node dynamically\n temp = (struct Node *) malloc (sizeof(struct Node));\n temp->value = non_zero_element;\n temp->row_position = row_index;\n temp->column_postion = column_index;\n temp->next = NULL;\n *start = temp;\n }\n else\n {\n while (temp->next != NULL)\n temp = temp->next;\n // Create new node dynamically\n r = (struct Node *) malloc (sizeof(struct Node));\n r->value = non_zero_element;\n r->row_position = row_index;\n r->column_postion = column_index;\n r->next = NULL;\n temp->next = r;\n }\n}\n// This function prints contents of linked list\n// starting from start\nvoid PrintList(struct Node* start)\n{\n struct Node *temp, *r, *s;\n temp = r = s = start;\n\n printf(\"row_position: \");\n while(temp != NULL){\n printf(\"%d \", temp->row_position);\n temp = temp->next;\n }\n printf(\"\\n\");\n printf(\"column_postion: \");\n while(r != NULL){\n printf(\"%d \", r->column_postion);\n r = r->next;\n }\n printf(\"\\n\");\n printf(\"Value: \");\n while(s != NULL){\n printf(\"%d \", s->value);\n s = s->next;\n }\n printf(\"\\n\");\n}\n// Driver of the program\nint main(){\n// Assume 4x5 sparse matrix\n int sparseMatric[4][5] =\n {\n {0 , 0 , 3 , 0 , 4 },\n {0 , 0 , 5 , 7 , 0 },\n {0 , 0 , 0 , 0 , 0 },\n {0 , 2 , 6 , 0 , 0 }\n };\n /* Start with the empty list */\n struct Node* start = NULL;\n for (int i = 0; i < 4; i++)\n for (int j = 0; j < 5; j++)\n // Pass only those values which are non - zero\n if (sparseMatric[i][j] != 0)\n create_new_node(&start, sparseMatric[i][j], i, j);\n PrintList(start);\n return 0;\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#stack","title":"STACK","text":""},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#1adt","title":"1.ADT","text":""},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#2implementations","title":"2.Implementations","text":"Algorithm {\n Make an empty stack S;\n while (read in a character c) {\n if (c is an opening symbol)\n Push(c, S);\n else if (c is a closing symbol) {\n if (S is empty) { ERROR; exit; }\n else { /* stack is okay */\n if (Top(S) doesn\u2019t match c) { ERROR, exit; }\n else Pop(S);\n } /* end else-stack is okay */\n } /* end else-if-closing symbol */\n } /* end while-loop */ \n if (S is not empty) ERROR;\n}\n
#include <stdio.h>\n#include <stdbool.h>\n#include <string.h>\n\n#define STACKSIZE 30 \nchar stack[STACKSIZE]; // Global, but let's go with it\nint top = 0;\n\nvoid push(char x) {\n if (top == STACKSIZE)\n puts(\"Stack Full\");\n else\n stack[top++] = x;\n}\n\nbool popExpect(char c) { // compare expected char on top with passed char\n return top && c == stack[--top];\n}\n\nbool chk(const char *str) {\n char *cp, pairs[] = \"(){}[]\"; // three important pairs\n bool isGood = true; // optimism\n\n for (int i = 0; isGood && str[i]; i++)\n // is this char one of the \"special\" ones?\n if ((cp = strchr(pairs, str[i])) != NULL) {\n int off = cp - pairs;\n // because \"paired\" 0,2,4 are open, 1,3,5 are close\n if (off % 2 == 0) // opening\n push(cp[1]); // push the close that matches this open\n else // special closing\n isGood = popExpect(str[i]); // does this close match?\n }\n\n return isGood && top == 0;\n}\n\nint main() {\n const char *s1 = \"(foobar)({}){bar}[[[(foo)]]]\"; // balanced\n const char *s2 = \"(foobar)({}){ { bar}[[[(foo)]]]\"; // unbalanced open\n const char *s3 = \"(foobar)({}){ ] bar}[[[(foo)]]]\"; // unbalanced close\n\n puts(chk(s1) ? \"Balanced\" : \"Unbalanced\");\n puts(chk(s2) ? \"Balanced\" : \"Unbalanced\");\n puts(chk(s3) ? \"Balanced\" : \"Unbalanced\");\n\n return 0;\n}\n
Calculation
Prefix
Infix\u300cConvert To Postfix\u300d
#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n// Function to return precedence of operators\nint prec(char c) {\n if (c == '^')\n return 3;\n else if (c == '/' || c == '*')\n return 2;\n else if (c == '+' || c == '-')\n return 1;\n else\n return -1;\n}\n\n// Function to return associativity of operators\nchar associativity(char c) {\n if (c == '^')\n return 'R';\n return 'L'; // Default to left-associative\n}\n\n// Structure to represent stack\nstruct Stack {\n int top;\n unsigned capacity;\n char* array;\n};\n\n// Function to initialize a stack\nstruct Stack* createStack(unsigned capacity) {\n struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));\n stack->capacity = capacity;\n stack->top = -1;\n stack->array = (char*)malloc(stack->capacity * sizeof(char));\n return stack;\n}\n\n// Function to check if the stack is full\nint isFull(struct Stack* stack) {\n return stack->top == stack->capacity - 1;\n}\n\n// Function to check if the stack is empty\nint isEmpty(struct Stack* stack) {\n return stack->top == -1;\n}\n\n// Function to push an element to the stack\nvoid push(struct Stack* stack, char item) {\n if (isFull(stack)) {\n printf(\"Stack Full\\n\");\n return;\n }\n stack->array[++stack->top] = item;\n}\n\n// Function to pop an element from the stack\nchar pop(struct Stack* stack) {\n if (isEmpty(stack)) {\n return '$'; // Assuming '$' as an invalid character\n }\n return stack->array[stack->top--];\n}\n\n// The main function to convert infix expression\n// to postfix expression\nvoid infixToPostfix(char* exp) {\n struct Stack* stack = createStack(strlen(exp));\n char result[strlen(exp)];\n int resultIndex = 0;\n\n for (int i = 0; exp[i] != '\\0'; i++) {\n char c = exp[i];\n\n // If the scanned character is\n // an operand, add it to the output string.\n if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))\n result[resultIndex++] = c;\n\n // If the scanned character is an\n // \u2018(\u2018, push it to the stack.\n else if (c == '(')\n push(stack, '(');\n\n // If the scanned character is an \u2018)\u2019,\n // pop and add to the output string from the stack\n // until an \u2018(\u2018 is encountered.\n else if (c == ')') {\n while (stack->top != -1 && stack->array[stack->top] != '(') {\n result[resultIndex++] = pop(stack);\n }\n pop(stack); // Pop '('\n }\n\n // If an operator is scanned\n else {\n while (!isEmpty(stack) && prec(exp[i]) < prec(stack->array[stack->top]) ||\n !isEmpty(stack) && prec(exp[i]) == prec(stack->array[stack->top]) &&\n associativity(exp[i]) == 'L') {\n result[resultIndex++] = pop(stack);\n }\n push(stack, c);\n }\n }\n\n // Pop all the remaining elements from the stack\n while (!isEmpty(stack)) {\n result[resultIndex++] = pop(stack);\n }\n\n result[resultIndex] = '\\0'; // Null-terminate the result string\n\n printf(\"%s\\n\", result);\n}\n\n// Driver code\nint main() {\n char exp[] = \"a+b*(c^d-e)^(f+g*h)-i\";\n // Function call\n infixToPostfix(exp);\n return 0;\n}\n
Note: a \u2013 b \u2013 c will be converted to a b \u2013 c \u2013. However, \\(2^{2^{3}}\\)must be converted to 2 2 3 ^ ^ , not 2 2 ^ 3 ^ since exponentiation associates right to left.
Observe that when ' ( ' is not in the stack, its precedence is the highest; but when it is in the stack, its precedence is the lowest. Define in-stack precedence and incoming precedence for symbols, and each time use the corresponding precedence for comparison.
Note: a \u2013 b \u2013 c will be converted to a b \u2013 c \u2013. However, \\(2^{2^{3}}\\)must be converted to 2 2 3 ^ ^ , not 2 2 ^ 3 ^ since exponentiation associates right to left.
Postfix \u300cEasier To Calculate\u300d
ps:Tail Recursion! -- compliler will remove the recursion.
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#the-queue-adt","title":"The Queue ADT","text":"A queue is a First-In-First-Out (FIFO) list, that is, an ordered list in which insertions take place at one end and deletions take place at the opposite end.
Linked list implementation
Array Implementation
struct QueueRecord {\n int Capacity ; /* max size of queue */\n int Front; /* the front pointer */\n int Rear; /* the rear pointer */\n int Size; /* Optional - the current size of queue */\n ElementType *Array; /* array for queue elements */\n } ; \n
FirstChild-NextSibling \u8868\u793a\u6cd5
\u8bb0\u5f55\u7b2c\u4e00\u4e2a\u5b50\u8282\u70b9\u548c\u4e0b\u4e00\u4e2a\u5144\u5f1f\u8282\u70b9
\u56e0\u4e3a\u4e00\u68f5\u6811\u7684\u513f\u5b50\u987a\u5e8f\u4e0d\u5b9a\uff0c\u6240\u4ee5\u4e00\u68f5\u6811\u7684\u8868\u793a\u65b9\u5f0f\u4e0d\u552f\u4e00
struct TreeNode {\n ElementType Element;\n PtrToNode FirstChild;\n PtrToNode NextSibling;FirstChildFirfsads\n};\ntypedef struct TreeNode *PtrToNode;\n
https://blog.csdn.net/m0_73070900/article/details/130227715
Preorder\uff1aroot-left-right
Postorder\uff1aleft-right-root
inorder:left-root-right
level order:
void levelorder(tree_ptr tree) {\n enqueue(tree);\n while (queue is not empty) {\n visit(T = dequeue());\n for (each child C of T) \n enqueue(C);\n }\n}\n
// \u4e8c\u53c9\u6811\u7684\u5b9e\u73b0\uff08C\u8bed\u8a00\uff09\n// \u94fe\u8868\uff0c\u9012\u5f52\u5b9e\u73b0\ntypedef char Elementtype; // \u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u6839\u636e\u9700\u8981\u81ea\u884c\u5b9a\u5236\ntypedef struct TreeNode * Node; // Node\u76f8\u5f53\u4e8estruct treeNode *\n// \u5b9a\u4e49\u6570\u8282\u70b9\u7ed3\u6784\ntypedef struct TreeNode {\n Elementtype Element;\n Node left; // \u6811\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\n Node right; // \u6811\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\n}TREE,*PTREE;\n\nvoid CreatTree(PTREE *); // \u6811\u7684\u5148\u5e8f\u521b\u5efa\u51fd\u6570\nvoid PreOrderTree(PTREE ); // \u6811\u7684\u524d\u5e8f\u904d\u5386\u51fd\u6570\nvoid InOrderTree(PTREE ); // \u6811\u7684\u4e2d\u5e8f\u904d\u5386\nvoid PostOrderTree(PTREE ); // \u6811\u7684\u540e\u5e8f\u904d\u5386\nvoid LeafOfTree(PTREE ); // \u6253\u5370\u6811\u7684\u53f6\u5b50\u8282\u70b9\u51fd\u6570\nint Get_Leaf_Num(PTREE ); // \u83b7\u53d6\u6811\u53f6\u5b50\u8282\u70b9\u4e2a\u6570\nint Get_Height(PTREE ); // \u83b7\u53d6\u6811\u7684\u9ad8\u5ea6\n\nint main() {\n PTREE Root; \n printf(\"\u8bf7\u5148\u5e8f\u8f93\u5165\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u636e\uff1a \");\n CreatTree(&Root); \n printf(\"\\n\u524d\u5e8f\u904d\u5386\u7ed3\u679c\u4e3a\uff1a\");\n PreOrderTree(Root); \n printf(\"\\n\u4e2d\u5e8f\u904d\u5386\u7ed3\u679c\u4e3a\uff1a\");\n InOrderTree(Root);\n printf(\"\\n\u540e\u5e8f\u904d\u5386\u7ed3\u679c\u4e3a\uff1a\");\n PostOrderTree(Root);\n printf(\"\\n\u6253\u5370\u53f6\u5b50\u8282\u70b9\u4e3a\uff1a\");\n LeafOfTree(Root);\n printf(\"\\n\u53f6\u5b50\u8282\u70b9\u4e2a\u6570\u4e3a\uff1a%d\", Get_Leaf_Num(Root));\n printf(\"\\n\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\u4e3a\uff1a%d\", Get_Height(Root));\n printf(\"\\n\");\n return 0;\n}\n// \u5b9a\u4e49\u6811\u5148\u5e8f\u521b\u5efa\u51fd\u6570\nvoid CreatTree(PTREE *ROOT){\n char val = 0;\n val = getchar();\n if(wal=='*'){*ROOT=NULL;}\n else{\n (*Root)=(PTREE)malloc(sizeof(TREE));\n if ((*Root) == NULL) {\n printf(\"\u521b\u5efa\u8282\u70b9\u5931\u8d25\uff0c\u65e0\u6cd5\u5206\u914d\u53ef\u7528\u5185\u5b58...\");\n exit(-1);\n }\n else{\n (*Root)->Element =val;\n CreatTree(&(*Root)->left);\n CreatTree(&(*Root)->right); \n }\n }\n\n}\n\n// \u6811\u7684\u524d\u5e8f\u904d\u5386\u51fd\u6570\u5b9a\u4e49\nvoid PreOrderTree(PTREE Root) {\n if (Root == NULL)\n return;\n else {\n putchar(Root->Element);\n PreOrderTree(Root->left);\n PreOrderTree(Root->right);\n }\n}\n\n// \u6811\u7684\u4e2d\u5e8f\u904d\u5386\u51fd\u6570\u5b9a\u4e49\nvoid InOrderTree(PTREE Root) {\n if (Root == NULL)\n return;\n else {\n InOrderTree(Root->left);\n putchar(Root->Element);\n InOrderTree(Root->right);\n }\n}\n// \u6811\u7684\u540e\u5e8f\u904d\u5386\u51fd\u6570\u5b9a\u4e49\nvoid PostOrderTree(PTREE Root) {\n if (Root==NULL) \n return ;\n else{\n PostOrderTree(Root->left);\n PostOrderTree(Root->right);\n putchar( Root->Element);\n }\n}\n//\u6811\u7684\u540e\u5e8f\u904d\u5386\u7684\u5faa\u73af\u5f62\u5f0f\nIterative Program \nvoid iter_postorder\uff08tree_ptr_tree\uff09{\n stack S = CreateStack(MAX_SIZE);\n for(;;){\n\n }\n}\n// \u6253\u5370\u6811\u7684\u53f6\u5b50\u8282\u70b9\u51fd\u6570\u5b9a\u4e49\nvoid LeafOfTree(PTREE Tree) {\n if (Tree == NULL) \n return ;\n else {\n if (Tree->left == NULL&&Tree->right == NULL)\n putchar(Tree->Element);\n else {\n LeafOfTree(Tree->left);\n LeafOfTree(Tree->right);\n }\n } \n}\n// \u83b7\u53d6\u6811\u7684\u53f6\u5b50\u8282\u70b9\u4e2a\u6570\u51fd\u6570\u5b9a\u4e49\nint Get_Leaf_Num(PTREE Tree) {\n if (Tree == NULL)\n return 0;\n if (Tree->left == NULL&&Tree->right == NULL)\n return 1;\n //\u9012\u5f52\u6574\u4e2a\u6811\u7684\u53f6\u5b50\u8282\u70b9\u4e2a\u6570 = \u5de6\u5b50\u6811\u53f6\u5b50\u8282\u70b9\u7684\u4e2a\u6570 + \u53f3\u5b50\u6811\u53f6\u5b50\u8282\u70b9\u7684\u4e2a\u6570\n return Get_Leaf_Num(Tree->left) + Get_Leaf_Num(Tree->right);\n}\n// \u83b7\u53d6\u6811\u9ad8\u7684\u51fd\u6570\u5b9a\u4e49\nint Get_Height(PTREE Tree) {\n int Height = 0;\n if (Tree == NULL)\n return 0;\n //\u6811\u7684\u9ad8\u5ea6 = max(\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6) + 1\n else\n {\n int L_Height = Get_Height(Tree->left);\n int R_Height = Get_Height(Tree->right);\n Height = L_Height >= R_Height ? L_Height + 1 : R_Height + 1;\n }\n return Height;\n}\n
#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n\n// \u5b9a\u4e49\u4e8c\u53c9\u6811\u7ed3\u70b9\nstruct TreeNode {\n int val;\n struct TreeNode* left;\n struct TreeNode* right;\n};\n\n// \u521b\u5efa\u4e00\u4e2a\u4e8c\u53c9\u6811\u7ed3\u70b9\nstruct TreeNode* createNode(int val) {\n struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));\n newNode->val = val;\n newNode->left = NULL;\n newNode->right = NULL;\n return newNode;\n}\n// \u7ed3\u6784\u4f53\u6a21\u62df\u6808\nstruct TreeNodeStack {\n struct TreeNode* data;\n struct TreeNodeStack* next;\n};\n\nstruct TreeNodeStack* createStackNode(struct TreeNode* node) {\n struct TreeNodeStack* stackNode = (struct TreeNodeStack*)malloc(sizeof(struct TreeNodeStack));\n stackNode->data = node;\n stackNode->next = NULL;\n return stackNode;\n}\n\nvoid push(struct TreeNodeStack** stack, struct TreeNode* node) {\n struct TreeNodeStack* stackNode = createStackNode(node);\n stackNode->next = *stack;\n *stack = stackNode;\n}\n\nstruct TreeNode* pop(struct TreeNodeStack** stack) {\n if (*stack == NULL) {\n return NULL;\n }\n struct TreeNodeStack* temp = *stack;\n *stack = (*stack)->next;\n struct TreeNode* node = temp->data;\n free(temp);\n return node;\n}\n\nbool isEmpty(struct TreeNodeStack* stack) {\n return stack == NULL;\n}\n\n// \u4e2d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u5e76\u8fd4\u56de\u7ed3\u679c\u4f5c\u4e3a\u6570\u7ec4\nint* inorderTraversal(struct TreeNode* root, int* returnSize) {\n struct TreeNodeStack* stack = NULL;\n int* result = (int*)malloc(sizeof(int));\n int resultSize = 0;\n struct TreeNode* current = root;\n while (current != NULL || !isEmpty(stack)) {\n while (current != NULL) {\n push(&stack, current);\n current = current->left;\n }\n current = pop(&stack);\n result = (int*)realloc(result, (resultSize + 1) * sizeof(int));\n result[resultSize] = current->val;\n resultSize++;\n current = current->right;\n }\n\n *returnSize = resultSize;\n return result;\n}\n\n// \u4e3b\u51fd\u6570\nint main() {\n struct TreeNode* root = createNode(1);\n root->right = createNode(2);\n root->right->left = createNode(3);\n\n printf(\"Inorder Traversal: \");\n int resultSize;\n int* result = inorderTraversal(root, &resultSize);\n for (int i = 0; i < resultSize; i++) {\n printf(\"%d \", result[i]);\n }\n free(result);\n return 0;\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#tree_1","title":"\u521b\u5efaTREE","text":"BiTree* postInTree(int post[],int in[],int n) {\n if (n<=0) return nullptr;\n int i=0;\n while (post[n-1]!=in[i]) i++; //i\u4e5f\u6b63\u597d\u662f\u5de6\u5b50\u6811\u8282\u70b9\u6570\n BiTree* nw=new BiTree;\n nw->data=in[i];\n nw->lc=postInTree(post,in,i); //\u5efa\u5de6\u5b50\u6811\n nw->rc=postInTree(post+i,in+i+1,n-i-1); //\u5efa\u53f3\u5b50\u6811\n return nw;\n}\n
BiTree* preInTree2(int pre[],int in[],int n) { //\u8fd9\u662f\u7b14\u8bb0\u4e0a\u7684\u6211\u7684\u65b9\u6cd5,n\u662f\u5f53\u524d\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\n if (n<=0) return nullptr;\n int i=0;\n while (in[i]!=pre[0]) i++; //\u6b64\u65f6i\u6b63\u597d\u662f\u5de6\u5b50\u6811\u8282\u70b9\u6570\u3002\u5148\u5e8f\u904d\u5386\u7684\u9996\u5143\u7d20\u4e00\u5b9a\u662f\u6839\u8282\u70b9\n BiTree* nw=new BiTree;\n nw->data=in[i];\n nw->lc=preInTree2(pre+1,in,i); //\u5efa\u5de6\u5b50\u6811\uff0c\u5de6\u5b50\u6811\u8282\u70b9i\u4e2a\n nw->rc=preInTree2(pre+i+1,in+i+1,n-i-1); //\u5efa\u53f3\u5b50\u6811\uff0c\u53f3\u5b50\u6811\u8282\u70b9n-i-1\u4e2a\n return nw;\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#application","title":"Application","text":"static void ListDir ( DirOrFile D, int Depth )\n{\n if ( D is a legitimate entry ) {\n PrintName (D, Depth );\n if ( D is a directory )\n for (each child C of D )\n ListDir ( C, Depth + 1 );\n }\n}\n
Calculating the size of the directory\u300cTypical Postorder Traversal\u300d
\u7ebf\u7d22\u4e8c\u53c9\u6811 Threader Binary Tree Inorder
Rule 1: If Tree->Left is null, replace it with a pointer to the inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a threaded binary tree must have a head node of which the left child points to the first node.
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#binary-search-tree","title":"Binary Search Tree","text":"SearchTree Delete(ElementType X, SearchTree T) {\n Position TmpCell;\n if (T == NULL) Error(\"not found\");\n else if (X < T->Element) T->Left = Delete(X, T->Left);\n else if (x > T->Element) T->Right = Delete(X, T->Right);\n else {\n if (T->Left && T->Right) {\n TmpCell = FindMin(T->Right);\n T->Element = TmpCell->Element;\n T->Right = Delete(T->Element, T->Right);\n } else {\n TmpCell = T;\n if (T->Left == NULL) T = T->Right;\n else if (T->Right == NULL) T = T->Left;\n free(TmpCell);\n }\n }\n return T;\n}\n
\\(C_{n}=\\sum_{i=1}^{n} C_{i}C{n-i-1}\\)
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#complete-binary-tree","title":"\u5b8c\u5168\u4e8c\u53c9\u6811\uff08complete binary tree\uff09","text":""},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#basic-properties_2","title":"Basic Properties","text":"\u6240\u6709\u53f6\u8282\u70b9\u90fd\u5728\u76f8\u90bb\u7684\u4e24\u5c42\u4e0a\u7684\u4e8c\u53c9\u6811
Basic Properties:
\u9664\u4e86\u6700\u540e\u4e00\u5c42\uff0c\u6bcf\u4e00\u5c42\u90fd\u662f\u6ee1\u7684
\u6700\u540e\u4e00\u5c42\u7684\u8282\u70b9\u90fd\u9760\u5de6\u6392\u5217
\\(2^{h}\\) ~ \\(2^{h+1}-1\\) nodes\\((0,1,2,3- height = 3)\\)
\\(h=O(log(n))\\)
void Insert(ElementType X, MaxHeap H) {\n if (IsFull(H)) {\n Error(\"Full Heap\");\n return;\n }\n int i = ++H->Size;\n for (; H->Elements[i/2] < X; i /= 2) {\n H->Elements[i] = H->Elements[i/2];\n }\n H->Elements[i] = X;\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#delete","title":"DELETE","text":"ElementType DeleteMax(MaxHeap H) {\n if (IsEmpty(H)) {\n Error(\"Empty Heap\");\n return H->Elements[0];\n }\n int i, Child;\n ElementType MaxElement, LastElement;\n MaxElement = H->Elements[1];\n LastElement = H->Elements[H->Size--];\n for (i = 1; i * 2 <= H->size; i = Child) {\n Child = i * 2;\n if (Child != H->Size && H->Elements[Child + 1] > H->Elements[Child]) {\n Child++;\n }\n if (LastElement < H->Elements[Child]) {\n H->Elements[i] = H->Elements[Child];\n } else {\n break;\n }\n }\n H->Elements[i] = LastElement;\n return MaxElement;\n}\n
Question: how to make it a natural stop without using \\(Child != H->size\\)
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#buildheap-h","title":"BuildHeap ( H )","text":"For the perfect binary tree of height h containing \\(2^{h+1}-1\\) nodes, the sum of the heights of the nodes is \\(2^{h+1}-1 -(h + 1)\\).
\\(T ( N ) = O ( N )\\)
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#the-dynamic-equivalence-problem","title":"The Dynamic Equivalence Problem","text":"A dynamic graph generally refers to a graph that keeps on changing its configuration. Let\u2019s deep dive into it using an example:
NOTE:UNION is the union of the head of one head to another(thus for a random element x,always find the \"head\" element Find(x) and modify s[Find(x)]
void SetUnion ( DisjSet S, \n SetType Rt1, \n SetType Rt2 )\n{ S [ Rt2 ] = Rt1 ; }\n
SetType Find ( ElementType X, DisjSet S )\n{ for ( ; S[X] > 0; X = S[X] ) ;\n return X ;\n}\n
Algorithm using union-find operations\n{ Initialize Si = { i } for i = 1, ..., 12 ;\n for ( k = 1; k <= 9; k++ ) { /* for each pair i \u7b49\u4ef7j \u5373\u5982\u679c\u7ed9\u51fa\u4e5d\u7ec4\u7b49\u4ef7\u5173\u7cfb*/\n if ( Find( i ) != Find( j ) )\n SetUnion( Find( i ), Find( j ) );\n }\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#smart-union-algorithms","title":"Smart Union Algorithms","text":"The rank of a node generally refers to the distance (the number of nodes including the leaf node) between the furthest leaf node and the current node. Basically rank includes all the nodes beneath the current node.
void make_set(int v) {\n parent[v] = v;\n rank[v] = 0;\n}\nvoid union_sets(int a, int b) {\n a = find_set(a);\n b = find_set(b);\n if (a != b) {\n if (rank[a] < rank[b])\n swap(a, b);\n parent[b] = a;\n if (rank[a] == rank[b])\n rank[a]++;\n }\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#union-by-size","title":"Union by size","text":"void make_set(int v) {\n parent[v] = v;\n size[v] = 1;\n}\nvoid union_sets(int a, int b) {\n a = find_set(a);\n b = find_set(b);\n if (a != b) {\n if (size[a] < size[b])\n swap(a, b);\n parent[b] = a;\n size[a] += size[b];\n }\n}\n
Let T be a tree created by union-by-size with N nodes, then \\((Height(T) \\le \\lfloor log_2N \\rfloor + 1)\\)
Proof: By induction. (Each element can have its set name changed at most. \\(log_2N\\)
times.\uff09\u300cUnion by size\u300d
First , all note are at depth 0 at the very beginning
Every time,we do Union:the node was set on a new tree whose size is at least twice its size
So a node from either set can only incrase the depth by \\(1\\)
For n nodes ,every node can have its set name changed at most \\(log_2N\\) times.
Thus,Time complexity for Find is \\(O (l o g N )\\)
The base case K=0 is easy, since a tree of height 0 always has just 1 node
Suppose the claim is true for h=k. Now consider a union-by-height tree of height k+1.
There must have been a union that brought two trees together and increased the height of one of them from k to k+1.
Let those two trees (at the time of that union) be T1 and T2. We know that both T1 and T2 were of height k before the union.
Now we can apply the induction hypothesis: the trees T1 and T2 each have at least \\(2^k\\) nodes. Thus, the unioned tree has at least \\(2^k + 2^k = 2^{k+1}\\)nodes.
Time complexity of \\(N\\) Union and \\(M\\) Find operations is now. \\(O( N + M log_2N)\\)
We can set \\(S [ Root ] = \u2013 size\\), thus we don't need the array size[ ]
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#path-compression","title":"Path Compression","text":"Basically, connecting each node in a particular path to its ultimate parent refers to path compression. Let\u2019s understand it using the following illustration
SetType Find ( ElementType X, DisjSet S )\n{\n if ( S[ X ] <= 0 ) return X;\n else return S[ X ] = Find( S[ X ], S );//IMPORTANT\uff01\uff01\uff01\n}\n//The simple implementation does what was intended: first find the representative of the set (root vertex), and then in the process of stack unwinding the visited nodes are attached directly to the representative.\nSetType Find ( ElementType X, DisjSet S )\n{ ElementType root, trail, lead;\n for ( root = X; S[ root ] > 0; root = S[ root ] )\n ; /* find the root */\n for ( trail = X; trail != root; trail = lead ) {\n lead = S[ trail ] ; \n S[ trail ] = root ; \n } /* collapsing */\n return root ;\n}\n
NOTE
#include <stdio.h>\n#include <stdlib.h>\nstruct DisjointSet {\n int *rank, *parent, *size;\n int n;\n};\nstruct DisjointSet* createDisjointSet(int n) {\n struct DisjointSet* ds = (struct DisjointSet*)malloc(sizeof(struct DisjointSet));\n ds->n = n;\n ds->rank = (int*)malloc((n + 1) * sizeof(int));\n ds->parent = (int*)malloc((n + 1) * sizeof(int));\n ds->size = (int*)malloc((n + 1) * sizeof(int));\n for (int i = 0; i <= n; i++) {\n ds->rank[i] = 0;\n ds->parent[i] = i;\n ds->size[i] = 1;\n }\n return ds;\n}\nint findUPar(struct DisjointSet* ds, int node) {\n if (node == ds->parent[node])\n return node;\n ds->parent[node] = findUPar(ds, ds->parent[node]);//PATH COMPRESSION\n return ds->parent[node];\n}\nvoid unionByRank(struct DisjointSet* ds, int u, int v) {\n int ulp_u = findUPar(ds, u);\n int ulp_v = findUPar(ds, v);\n if (ulp_u == ulp_v)\n return;\n if (ds->rank[ulp_u] < ds->rank[ulp_v]) {\n ds->parent[ulp_u] = ulp_v;\n } else if (ds->rank[ulp_v] < ds->rank[ulp_u]) {\n ds->parent[ulp_v] = ulp_u;\n } else {\n ds->parent[ulp_v] = ulp_u;\n ds->rank[ulp_u]++;\n }\n}\nvoid unionBySize(struct DisjointSet* ds, int u, int v) {\n int ulp_u = findUPar(ds, u);\n int ulp_v = findUPar(ds, v);\n if (ulp_u == ulp_v)\n return;\n if (ds->size[ulp_u] < ds->size[ulp_v]) {\n ds->parent[ulp_u] = ulp_v;\n ds->size[ulp_v] += ds->size[ulp_u];\n } else {\n ds->parent[ulp_v] = ulp_u;\n ds->size[ulp_u] += ds->size[ulp_v];\n }\n}\nint main() {\n struct DisjointSet* ds = createDisjointSet(7);\n unionBySize(ds, 1, 2);\n unionBySize(ds, 2, 3);\n unionBySize(ds, 4, 5);\n unionBySize(ds, 6, 7);\n unionBySize(ds, 5, 6);\n if (findUPar(ds, 3) == findUPar(ds, 7)) {\n printf(\"Same\\n\");\n } else {\n printf(\"Not same\\n\");\n }\n unionBySize(ds, 3, 7);\n if (findUPar(ds, 3) == findUPar(ds, 7)) {\n printf(\"Same\\n\");\n } else {\n printf(\"Not same\\n\");\n }\n free(ds->rank);\n free(ds->parent);\n free(ds->size);\n free(ds);\n return 0;\n}\n
"},{"location":"note/%E6%9C%80%E5%A4%A7%E5%AD%90%E5%88%97%E5%92%8C/","title":"Maximum Subsequence Sum Problem","text":""},{"location":"note/%E6%9C%80%E5%A4%A7%E5%AD%90%E5%88%97%E5%92%8C/#maximum-subsequence-sum-problem","title":"Maximum Subsequence Sum Problem","text":"
O(\\(N^3\\)) \u300c\u57fa\u672c\u601d\u8def\uff0c\u9010\u4e2a\u5217\u4e3e\uff0c\u7565\u300d
O(\\(N^2\\)) \u300c\u4f18\u5316\u4e00\u4e0b\u5217\u4e3e\uff0c\u7565\u300d
divide & conquer O(N logN)
static int\nMaxSubSum(const int A[],int left,int right){\n int MaxLeftSum, MaxRightSum;\n int MaxLeftBorderSum, MaxRightBorderSum; \n int LeftBorderSum, RightBorderSum;\n int Center, i;\n\n if( left == right ){\n if(A[left]>0){\n return A[left];\n }\n else{\n return 0;\n }\n }\n Center = ( Left +Right ) / 2;\n MaxLeftSum = MaxSubSum( A, Left, Center ) \n MaxRightSum = MaxSubSum( A, Center + 1, Right );\n\n MaxLeftBorderSum = 0; LeftBorderSum = 0;\n\n for(i=Cente+1;i>=left;i--){\n LeftBorderSum += A[i];\n if(leftBorderSum>MaxleftBorderSum){\n MaxleftBorderSum = LeftBorderSum;\n }\n }\n\n MaxRightBorderSum = 0; \n RightBorderSum = 0;\n for(i=Center;i<=Right;i++){\n RightBorderSum += A[i];\n if(RightBorderSum > MaxRightBorderSum){\n MaxRightBOrderSum = RightBorderSum\n }\n }\n return Max(MaxLeftSum,MaxRightSum,MaxLeftBorderSum+MaxRightBorderSum);\n\n}\nint MaxsubsequenceSum( const int A[],intN) {\n return MaxSubSum( A, 0, N - 1 );\n }\n
int MaxSubsequenceSum( const int A[ ], int N ) \n{ \n int ThisSum, MaxSum, j; \n ThisSum = MaxSum = 0; \n for ( j = 0; j < N; j++ ){ \n ThisSum += A[ j ]; \n if( ThisSum > MaxSum ){\n MaxSum = ThisSum; \n }\n else if( ThisSum < 0 ){\n ThisSum = 0;\n }\n } \n return MaxSum; \n} \n
int maxSubArray(int* nums, int numsSize) {\n int pre = 0, maxAns = nums[0];\n for (int i = 0; i < numsSize; i++) {\n pre = fmax(pre + nums[i], nums[i]);\n maxAns = fmax(maxAns, pre);\n }\n return maxAns;\n}\n
"},{"location":"oop/Basic/Basic/","title":"Itro & Class","text":""},{"location":"oop/Basic/Basic/#in-out","title":"In & Out","text":"
#include<iostream>\nusing namespace std;\nint main()\n{\n int age;\n cin >> age >> name;\n return 0;\n}\n
cout
\u6807\u51c6\u8f93\u51fa <<
Inserter
cout
\u4e3a cout << \"Hello World\"
\u7684\u7ed3\u679c
#include<string>
#include<iostream>\n#include<string>\nusing namespace std;\nint main()\n{\n int age;\n string name;\n cin >> age >> name;\n cout << \"Hello World!\" << name << \" is \" << age << \" today!\" << endl;\n cout << name.length() << endl;\n return 0;\n}\n
\\0
string place(\"Hangzhou\").
int a(8)
#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n string originalString = \"Hello, World!\";\n // Using substr to extract a substring\n int startPos = 7; // Starting position\n int length = 5; // Length of the substring to extract\n string extractedSubstring = originalString.substr(startPos, length);\n // Displaying the result\n cout << \"Original String: \" << originalString << endl;\n cout << \"Substring from position \" << startPos << \" with length \" << length << \": \" << extractedSubstring << endl;\n return 0;\n}\n
"},{"location":"oop/Basic/Basic/#alter-string","title":"Alter-string","text":"#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n\n // Example 1: Assign the value of another string\n string anotherString = \"Goodbye\";\n myString.assign(anotherString);\n cout << \"After assign(const string& str): \" << myString << endl;\n\n // Example 2: Assign a substring of another string\n string sourceString = \"123456789\";\n myString.assign(sourceString, 2, 5); //Starting from position 2,take 5 characters\n cout << \"After assign(const string& str, size_t subpos, size_t sublen): \" << myString << endl;\n\n // Example 3: Assign the value of a C-string\n const char* cString = \"C-Style String\";\n myString.assign(cString);\n cout << \"After assign(const char* s): \" << myString << endl;\n\n // Example 4: Assign the first n characters of a C-string\n const char* cStringWithLength = \"ABCDE\";\n size_t n = 3;\n myString.assign(cStringWithLength, n);\n cout << \"After assign(const char* s, size_t n): \" << myString << endl;\n\n // Example 5: Assign a string consisting of n copies of character c\n char characterToRepeat = 'X';\n size_t numberOfCopies = 4;\n myString.assign(numberOfCopies, characterToRepeat);\n cout << \"After assign(size_t n, char c): \" << myString << endl;\n\n return 0;\n}\n
After assign(const string& str): Goodbye\nAfter assign(const string& str, size_t subpos, size_t sublen): 34567\nAfter assign(const char* s): C-Style String\nAfter assign(const char* s, size_t n): ABC\nAfter assign(size_t n, char c): XXXX\n
size_t
is a data type in C++ that is commonly employed for expressing the size of objects or the count of elements. It belongs to the category of unsigned integer types and is designed to have a size large enough to handle the maximum possible size of objects on a given platform. The definition of size_t
can be found in the standard library header <cstddef>
(or <stddef.h>
in C), and it is typically introduced using typedef
or using
.
#include <iostream>\n#include <string>\n\nusing namespace std;\n\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n\n // Example 1: Insert another string at a specific position\n string insertString = \"Beautiful \";\n size_t insertPosition = 7; // Before the 'W'\n myString.insert(insertPosition, insertString);\n cout << \"After insert(size_t pos, const string& str): \" << myString << endl;\n\n // Example 2: Insert a substring of another string at a specific position\n string sourceString = \"12345\";\n size_t subpos = 2;\n size_t sublen = 3;\n myString.insert(12, sourceString, subpos, sublen); // Insert at position 12\n cout << \"After insert(size_t pos, const string& str, size_t subpos, size_t sublen): \" << myString << endl;\n\n // Example 3: Insert a C-string at a specific position\n const char* cString = \"XYZ\";\n myString.insert(6, cString); // Insert at position 6\n cout << \"After insert(size_t pos, const char* s): \" << myString << endl;\n\n // Example 4: Insert the first n characters of a C-string at a specific position\n const char* cStringWithLength = \"ABCDE\";\n size_t n = 3;\n myString.insert(3, cStringWithLength, n); // Insert at position 3\n cout << \"After insert(size_t pos, const char* s, size_t n): \" << myString << endl;\n\n // Example 5: Insert n copies of character c at a specific position\n char characterToInsert = '!';\n size_t numberOfCopies = 4;\n myString.insert(0, numberOfCopies, characterToInsert); // Insert at the beginning\n cout << \"After insert(size_t pos, size_t n, char c): \" << myString << endl;\n\n return 0;\n}\n
After insert(size_t pos, const string& str): Hello, Beautiful World!\nAfter insert(size_t pos, const string& str, size_t subpos, size_t sublen): Hello, Beaut345iful World!\nAfter insert(size_t pos, const char* s): Hello,XYZ Beaut345iful World!\nAfter insert(size_t pos, const char* s, size_t n): HelABClo,XYZ Beaut345iful World!\nAfter insert(size_t pos, size_t n, char c): !!!!HelABClo,XYZ Beaut345iful World!\n
#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n // Example 1: Erase a portion of the string\n size_t erasePosition = 7; // Start erasing from position 7\n size_t eraseLength = 5; // Erase 5 characters\n myString.erase(erasePosition, eraseLength);\n cout << \"After erase(size_t pos, size_t len): \" << myString << endl;\n // Example 2: Erase the entire string (using default arguments)\n myString.erase();\n cout << \"After erase(): \" << myString << endl;\n return 0;\n}\n
After erase(size_t pos, size_t len): Hello, !\nAfter erase(): \n
#include <iostream>\n#include <string>\n\nusing namespace std;\n\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n\n // Example 1: Replace a portion of the string with another string\n size_t replacePosition = 7; // Start replacing from position 7\n size_t replaceLength = 5; // Replace 5 characters\n string replacementString = \"Universe\";\n myString.replace(replacePosition, replaceLength, replacementString);\n cout << \"After replace(size_t pos, size_t len, const string& str): \" << myString << endl;\n\n // Example 2: Replace a portion of the string with a substring of another string\n string sourceString = \"12345\";\n size_t subpos = 2;\n size_t sublen = 3;\n myString.replace(12, 5, sourceString, subpos, sublen); // Replace at position 12\n cout << \"After replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen): \" << myString << endl;\n\n // Example 3: Replace a portion of the string with a C-string\n const char* cString = \"XYZ\";\n myString.replace(6, 5, cString); // Replace at position 6\n cout << \"After replace(size_t pos, size_t len, const char* s): \" << myString << endl;\n\n // Example 4: Replace a portion of the string with the first n characters of a C-string\n const char* cStringWithLength = \"ABCDE\";\n size_t n = 3;\n myString.replace(3, 5, cStringWithLength, n); // Replace at position 3\n cout << \"After replace(size_t pos, size_t len, const char* s, size_t n): \" << myString << endl;\n\n // Example 5: Replace a portion of the string with n copies of character c\n char characterToReplace = '!';\n size_t numberOfCopies = 4;\n myString.replace(0, 1, numberOfCopies, characterToReplace); // Replace at the beginning\n cout << \"After replace(size_t pos, size_t len, size_t n, char c): \" << myString << endl;\n\n return 0;\n}\n
After replace(size_t pos, size_t len, const string& str): Hello, Universe!\nAfter replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen): Hello, Unive345\nAfter replace(size_t pos, size_t len, const char* s): Hello,XYZe345\nAfter replace(size_t pos, size_t len, const char* s, size_t n): HelABCZe345\nAfter replace(size_t pos, size_t len, size_t n, char c): !!!!elABCZe345\n
#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n // Example 1: Find the position of a substring\n string searchString = \"World\";\n size_t foundPosition = myString.find(searchString);\n cout << \"Position of '\" << searchString << \"': \" << foundPosition << endl;\n // Example 2: Find the position of a substring starting from a specific position\n size_t startPosition = 7;\n foundPosition = myString.find(searchString, startPosition);\n cout << \"Position of '\" << searchString << \"' starting from position \" << startPosition << \": \" << foundPosition << endl;\n return 0;\n}\n
Position of 'World': 7\nPosition of 'World' starting from position 7: 7\n
"},{"location":"oop/Basic/Basic/#dynamic-memory-allocation","title":"Dynamic memory allocation","text":"new
and delete
new int;\nnew int[10];\nnew Stash;\ndelete p;\ndelete []p; //\u6307\u9488\u6307\u5411\u7684\u662f\u591a\u4e2a\u5bf9\u8c61\n
// Allocate an array of integers\nint* p = new int[5];\n// Use the allocated memory\n// Deallocate the memory using delete []\ndelete [] p;\n
New -- \u7533\u8bf7\u7a7a\u95f4\uff0c\u540c\u65f6\u521d\u59cb\u5316\u5bf9\u8c61
It is safe to delete a Null
malloc
returns null
indicating not enough space
new
just alert error.
char c;\nchar& r=c;\n
For local or global variables , the initial value of the bending is required.
In parameters lists and member varaibles , not necessary
void f(int& x);\nf(y); //initialized when function is called \n
Restrictions
#include <iostream>\n#include <string>\nusing namespace std;\ntypedef struct point {\n float x;\n float y;\n /*version1*/\n void print(){\n cout << x << \" \" << y << endl;\n }\n /*version 2*/\n void move(int dx, int dy);\n} Point;\nvoid Point::move(int dx, int dy){\n x += dx;\n y += dy;\n}\n/*void print(const Point *p)\n{\n printf(\"%d %d\\n\", p->x, p->y); // const means that the function will not change the value of the variable \n}\nvoid move(Point* p,int dx, int dy)\n{\n p->x += dx;\n p->y += dy;\n}*/\nint main()\n{\n Point a;\n a.x = 1;\n a.y = 2;\n a.print();\n a.move(3, 4);\n}\n
// Class\n#include <iostream>\n#include <string>\nusing namespace std;\nclass Point{\n public:\n void init(int x,int y);\n void move(int dx,int dy);\n void print()const;\n private:\n int x;\n int y;\n};\nvoid Point::init(int ix,int iy){\n x=ix;\n y=iy;\n}\nvoid Point::move(int dx,int dy){\n x+=dx;\n y+=dy;\n}\nvoid Point::print()const{\n cout<<\"(\"<<x<<\",\"<<y<<\")\"<<endl;\n}\nint main()\n{\n Point a;\n a.init(10,20);\n a.print();\n}\n
void Point::print()const{\n cout << this << endl\n cout<<\"(\"<<this->x<<\",\"<<this->y<<\")\"<<endl;\n}\n
this
-- Point* this
--hidden parameter
Another example
void Point::init(int x,int y){\n this->x=x;\n this->y=y; \n}\n
"},{"location":"oop/Basic/Basic/#resolver","title":"Resolver","text":"void S::f() {\n ::f(); // Would be recursive otherwise!\n ::a++; // Select the global a\n a--; // The a at class scope\n}\n
"},{"location":"oop/Basic/Basic/#object-oriented","title":"Object-Oriented","text":""},{"location":"oop/Basic/Basic/#constructer","title":"Constructer","text":"Point c = {10,20}
#include <iostream>\n#include <string>\nusing namespace std;\nclass Point{\n public:\n Point(int deep); //one argument\n Point(int x,int y); //two arguments\n Point(){x = 13,y=31;}; // no argument\n void move(int dx,int dy);\n void print()const;\n private:\n int x;\n int y;\n};\nPoint::Point(int ix,int iy){\n x=ix;\n y=iy;\n}\nPoint::Point(int deep){\n x=y=deep;\n}\nvoid Point::move(int dx,int dy){\n x+=dx;\n y+=dy;\n}\nvoid Point::print()const{\n cout<<\"(\"<<x<<\",\"<<y<<\")\"<<endl;\n}\nint main()\n{\n Point a(10,20);\n Point c(10);//Point c = 10\n Point d;\n d.print();\n a.print();\n}\n
Student::Student(string s):name(s){} //initialization before constructor\n// Prefered\n
\u8fd9\u610f\u5473\u7740\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u4e2d\u5bf9 name \u6210\u5458\u53d8\u91cf\u8fdb\u884c\u4e86\u521d\u59cb\u5316 \u5728\u6784\u9020\u51fd\u6570\u4f53\u5185\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9 name \u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\uff0c\u800c\u662f\u5728\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u4e2d\u76f4\u63a5\u5c06\u4f20\u9012\u7ed9\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570 s \u8d4b\u503c\u7ed9\u4e86 name,\u8fd9\u6837\u7684\u64cd\u4f5c\u662f\u5728\u6784\u9020\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\uff0c\u5373\u5728\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5c31\u5bf9 name \u8fdb\u884c\u4e86\u521d\u59cb\u5316 \u56e0\u6b64\uff0c\u53ef\u4ee5\u8bf4\u8fd9\u662f\u201c\u521d\u59cb\u5316\uff08Initialization\uff09\u5728\u6784\u9020\u51fd\u6570\uff08Constructor\uff09\u4e4b\u524d\uff08Before\uff09\u201d\u6267\u884c\u7684\uff0c\u5373\u201cinitialization before constructor\u201d
Assignment
Student::Student(string s) {name=s;}\n
Student::Student(sring s){\n name = s;\n} //assignment\n// For more constructors\n
void func() {\n int x; // \u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u7684\u53d8\u91cf\n // \u7a7a\u95f4\u5728\u51fd\u6570\u6267\u884c\u65f6\u5206\u914d\n}\nvoid func() {\n static int x; // \u9759\u6001\u53d8\u91cf\n // \u7a7a\u95f4\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5206\u914d\n}\n
#include <iostream>\n#include <string>\n\nclass Student {\nprivate:\n std::string name;\n\npublic:\n // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9 name \u8fdb\u884c\u521d\u59cb\u5316\n Student(std::string s) : name(s) {\n std::cout << \"Default constructor called with name: \" << name << std::endl;\n }\n // \u8f93\u51fa\u5b66\u751f\u59d3\u540d\u7684\u65b9\u6cd5\n void display() {\n std::cout << \"Student's name: \" << name << std::endl;\n }\n};\nint main() {\n // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \"Alice\" \u7684\u5b66\u751f\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\n Student student1(\"Alice\");\n // \u8c03\u7528 display \u65b9\u6cd5\u663e\u793a\u5b66\u751f\u7684\u59d3\u540d\n student1.display();\n return 0;\n}\n
const
\u6210\u5458\u53d8\u91cf\u4e0d\u80fd\u88ab\u8d4b\u503c\uff0c\u53ea\u80fd\u5728\u521d\u59cb\u5316\u65f6\u8d4b\u503c\u3002\u8fd9\u91cc\u8be6\u7ec6\u89e3\u91ca\u4e00\u4e0b\u5173\u4e8e const
\u6210\u5458\u53d8\u91cf\u521d\u59cb\u5316\u7684\u4e24\u79cd\u65b9\u5f0f\uff1a\u7c7b\u5185\u76f4\u63a5\u521d\u59cb\u5316\uff1a - \u53ef\u4ee5\u5728\u58f0\u660e const
\u6210\u5458\u53d8\u91cf\u65f6\u76f4\u63a5\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f8b\u5982 const float x = 1.0
\u3002\u4f46\u662f\uff0c\u8fd9\u6837\u505a\u7684\u7ed3\u679c\u662f\u6240\u6709\u5bf9\u8c61\u7684\u8be5 const
\u6210\u5458\u53d8\u91cf\u90fd\u5c06\u62e5\u6709\u76f8\u540c\u7684\u503c\uff0c\u65e0\u6cd5\u9488\u5bf9\u6bcf\u4e2a\u5bf9\u8c61\u5355\u72ec\u521d\u59cb\u5316\u3002 - \u793a\u4f8b\uff1a
class Point {\nprivate:\n const float x = 1.0;\n const float y = 1.0;\npublic:\n Point() {}\n};\n
\u8fd9\u79cd\u65b9\u5f0f\u7684\u9650\u5236\u662f\u6240\u6709 Point
\u5bf9\u8c61\u7684 x
\u548c y
\u90fd\u662f 1.0\uff0c\u65e0\u6cd5\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u8d4b\u4e88\u4e0d\u540c\u7684\u503c\u3002 \u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5217\u8868\uff1a - \u53e6\u4e00\u79cd\u521d\u59cb\u5316 const
\u6210\u5458\u53d8\u91cf\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u4e3a const
\u6210\u5458\u53d8\u91cf\u8d4b\u4e88\u4e0d\u540c\u7684\u503c\u3002 - \u793a\u4f8b\uff1a
class Point {\nprivate:\n const float x, y;\npublic:\n Point(float xa = 0.0, float ya = 0.0) : x(xa), y(ya) {}\n};\n
\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cPoint
\u7c7b\u6709\u4e24\u4e2a const
\u6210\u5458\u53d8\u91cf x
\u548c y
\u3002\u6784\u9020\u51fd\u6570\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\u8fd9\u4e24\u4e2a const
\u53d8\u91cf\u3002\u8fd9\u6837\uff0c\u521b\u5efa\u5bf9\u8c61\u65f6\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u7684 x
\u548c y
\u8d4b\u4e88\u4e0d\u540c\u7684\u503c\u3002 Point p1(1.0, 2.0); // p1 \u7684 x \u662f 1.0\uff0cy \u662f 2.0\nPoint p2(3.0, 4.0); // p2 \u7684 x \u662f 3.0\uff0cy \u662f 4.0\n
\u8be6\u7ec6\u89e3\u91ca\uff1a
\u5728\u4e0a\u8ff0 Point
\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a const
\u6210\u5458\u53d8\u91cf x
\u548c y
\u3002\u7531\u4e8e\u5b83\u4eec\u662f const
\u7684\uff0c\u6240\u4ee5\u4e00\u65e6\u88ab\u521d\u59cb\u5316\u540e\uff0c\u5c31\u4e0d\u80fd\u518d\u4fee\u6539\u5176\u503c\u3002\u4e3a\u4e86\u80fd\u591f\u7075\u6d3b\u5730\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u7684 x
\u548c y
\u8d4b\u503c\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\uff1a
Point(float xa = 0.0, float ya = 0.0)
\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53c2\u6570 xa
\u548c ya
\uff0c\u5e76\u4e14\u7ed9\u5b83\u4eec\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c 0.0
\u3002: x(xa), y(ya)
\u5728\u6784\u9020\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\u5c06\u53c2\u6570 xa
\u548c ya
\u7684\u503c\u5206\u522b\u8d4b\u7ed9 const
\u6210\u5458\u53d8\u91cf x
\u548c y
\u3002x
\u548c y
\u503c\uff0c\u800c\u4e0d\u662f\u6240\u6709\u5bf9\u8c61\u90fd\u5177\u6709\u76f8\u540c\u7684 const
\u6210\u5458\u503c\u3002Destructor is called automatically by the compiler when the object goes out of scope
The order of destruction is the reverse of construction.
Scope is delimited by curly braces{ }
.
Upon entering a function, space for all local variables is allocated,but constructors are not invoked until the specific constructor line is executed.
Similarly, when entering a switch case
statement, space for objects is generated but not constructed, which may lead to issues during destruction.
void f(int i){\n if(i<10){\n goto jump1; //Error: goto bypasses init\n }\n X x1; //Constructor\n jump1:\n switch(i){\n case 1:\n X x2;//Constructor\n case 2 : //Error: case bypasses init\n X x3;//Constructor\n break;\n }\n}\n
x1
(x2
) is allocated , but not initialized , so destruction will fail.h
and .cpp
files are used to define one class..h
).All the bodies of these functions are in the source file (.cpp
)
Standard header file sturcture
#ifnedf HEADER_FLAG\n#define HEADER_FLAG\n//Type declaration here...\n#endif\n
#include
is to insert the included file into the .cpp
file at where the #include
statement is. #include \"xx.h\"
: search in the current directory firstly, then the directories declared somewhere#include <xx.h>
: search in the specified directories#include <xx>
: same as #include <xx.h>
NumberDisplay.h
#ifndef _NUMBER_DISPLAY_HEAD_\n#define _NUMBER_DISPLAY_HEAD_\n#include<string>\nclass NumberDisplay{\n private:\n int limit;\n int value;\n public:\n NumberDisplay(int limit);\n int increase();\n std::string toString();\n}\n#endif\n
NumberDisplay.cpp
#include \"NumberDisplay.h\"\n#include<string>\n#include<iostream>\n\nNumberDisplay::NumberDisplay(int limit){\n value = 0;\n this->limit = limit;\n}\n/**\n * @return 1 for turn over\n*/\nint NumberDisplay::increase():{\n value++;\n if(value == limit){\n value = 0;\n return 1;\n }\n return 0;\n}\nstd::string NumberDisplay::toString(){\n if(value<10){\n return \"0\"+value;\n }\n else{\n return \"\"+value;\n }\n}\n#ifdef _TEST_ND_\n#include<iostream>\nusing namespace std;\nint main(){\n NumberDisplay d(10);\n for(int i=9;i<20;i++){\n d.increase();\n cout << d.tostring()<<endl; \n }\n\n}\n#endif\n
clock.h
#ifndef _CLOCK_HEAD_\n#define _CLOCK_HEAD_\n#include \"NumberDisplay.h\"\nclass Clock{\n private:\n NumberDisplay hour;\n NumberDisplay minute;\n public:\n Clock();\n void dida();\n}\n
clock.cpp
#include \"clock.h\"\n#include<iostream>\nusing namespace std;\nClock::Clock():\n hour(24),minute(60) //initialization list\n{\n\n}\nvoid Clock::dida(){\n if(minute.increase()){\n hour.increase\n }\n cout << hour.toString();\n cout << \":\";\n cout << minute.toString();\n cout << endl;\n}\n
* What if ? clock.h
#ifndef _CLOCK_HEAD_\n#define _CLOCK_HEAD_\n//#include\"NumberDisplay.h\"\nclass NumberDisplay\nclass Clock{\n private:\n NumberDisplay *hour;\n NumberDisplay *minute;\n public:\n Clock();\n void dida();\n}\n
Reference
\u5f15\u7528\u548c\u6307\u9488\u662fC++\u4e2d\u4e24\u79cd\u4e0d\u540c\u7684\u6982\u5ff5\uff0c\u5b83\u4eec\u6709\u7740\u76f8\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5728\u8bed\u4e49\u548c\u4f7f\u7528\u65b9\u5f0f\u4e0a\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u533a\u522b\u3002
"},{"location":"oop/Basic/Basic/#_1","title":"\u533a\u522b\u4e00\uff1a\u8bed\u6cd5\u548c\u58f0\u660e","text":"\u5f15\u7528\uff1a\u4f7f\u7528 &
\u7b26\u53f7\u6765\u58f0\u660e\uff0c\u5fc5\u987b\u5728\u521d\u59cb\u5316\u65f6\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u53d8\u91cf\u6216\u5bf9\u8c61\u3002\u4e00\u65e6\u521d\u59cb\u5316\uff0c\u5f15\u7528\u5c06\u4e00\u76f4\u6307\u5411\u8be5\u53d8\u91cf\u6216\u5bf9\u8c61\uff0c\u5e76\u4e14\u4e0d\u80fd\u91cd\u65b0\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\u3002
\u6307\u9488\uff1a\u4f7f\u7528 *
\u7b26\u53f7\u6765\u58f0\u660e\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u6307\u5411\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u6307\u5411\u7a7a\u503c\uff08nullptr\uff09\u3002\u6307\u9488\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u88ab\u91cd\u65b0\u8d4b\u503c\uff0c\u6307\u5411\u4e0d\u540c\u7684\u5bf9\u8c61\u6216\u7a7a\u503c\u3002
int num = 10;\nint& ref = num; // \u5f15\u7528\nint* ptr = # // \u6307\u9488\n
"},{"location":"oop/Basic/Basic/#_2","title":"\u533a\u522b\u4e8c\uff1a\u7a7a\u503c","text":"\u5f15\u7528\uff1a\u5f15\u7528\u4e0d\u80fd\u6307\u5411\u7a7a\u503c\uff0c\u5fc5\u987b\u5728\u521d\u59cb\u5316\u65f6\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5bf9\u8c61\u3002
\u6307\u9488\uff1a\u6307\u9488\u53ef\u4ee5\u6307\u5411\u7a7a\u503c\uff08nullptr\uff09\uff0c\u8868\u793a\u4e0d\u6307\u5411\u4efb\u4f55\u5bf9\u8c61\u3002
int& ref = null; // \u9519\u8bef\uff0c\u5f15\u7528\u4e0d\u80fd\u6307\u5411\u7a7a\u503c\nint* ptr = nullptr; // \u6307\u9488\u6307\u5411\u7a7a\u503c\n
"},{"location":"oop/Basic/Basic/#_3","title":"\u533a\u522b\u4e09\uff1a\u64cd\u4f5c\u7b26","text":"\u5f15\u7528\uff1a\u4f7f\u7528\u5f15\u7528\u65f6\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\uff08*
\uff09\uff0c\u76f4\u63a5\u4f7f\u7528\u5f15\u7528\u672c\u8eab\u5373\u53ef\u8bbf\u95ee\u76ee\u6807\u5bf9\u8c61\u3002
\u6307\u9488\uff1a\u9700\u8981\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\uff08*
\uff09\u6765\u8bbf\u95ee\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61\u3002
int num = 10;\nint& ref = num; // \u5f15\u7528\nint* ptr = # // \u6307\u9488\n\nint value1 = ref; // \u76f4\u63a5\u4f7f\u7528\u5f15\u7528\nint value2 = *ptr; // \u4f7f\u7528\u6307\u9488\u9700\u8981\u89e3\u5f15\u7528\n
"},{"location":"oop/Basic/Basic/#_4","title":"\u533a\u522b\u56db\uff1a\u7a7a\u95f4\u5360\u7528","text":"\u5f15\u7528\uff1a\u5728\u5185\u5b58\u4e2d\u4e0d\u4f1a\u5206\u914d\u989d\u5916\u7684\u7a7a\u95f4\uff0c\u5b83\u53ea\u662f\u539f\u53d8\u91cf\u7684\u522b\u540d\u3002
\u6307\u9488\uff1a\u5728\u5185\u5b58\u4e2d\u5360\u7528\u989d\u5916\u7684\u7a7a\u95f4\u6765\u5b58\u50a8\u6307\u9488\u53d8\u91cf\u672c\u8eab\u7684\u5730\u5740\u3002
\u5f15\u7528\u548c\u6307\u9488\u5728C++\u4e2d\u90fd\u6709\u5176\u72ec\u7279\u7684\u7528\u9014\u548c\u4f18\u52bf\u3002\u5f15\u7528\u66f4\u5b89\u5168\u3001\u66f4\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u522b\u540d\u548c\u4f20\u9012\u53c2\u6570\u3002\u6307\u9488\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u6307\u5411\u591a\u4e2a\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8fdb\u884c\u7a7a\u503c\u68c0\u67e5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u5206\u914d\u5185\u5b58\u6216\u8ddf\u8e2a\u5bf9\u8c61\u5730\u5740\u7684\u60c5\u51b5\u3002\u9009\u62e9\u4f7f\u7528\u5f15\u7528\u8fd8\u662f\u6307\u9488\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u8bbe\u8ba1\u8003\u8651\u3002
"},{"location":"oop/Container/Container/","title":"Container & Function","text":"
Reference : https://note.hobbitqia.cc/OOP/oop3/#for-each-loop
"},{"location":"oop/Container/Container/#container","title":"Container","text":"Collection objects are objects that can store an arbitrary number of other objects.
#include <iostream>\nusing namespace std;\n#include<vector>\nint main(){\n vector<int> x;\n for (int a=0;a<1000;a++){\n x.push_back(a);\n }\n vector<int>::iterator p;\n for(p=x.begin();p<x.end();p++)\n cout << *p << \" \";\n return 0; \n}\n
int main(){\n vector<int> x;\n for(int a=0;a<100;a++){\n x.push_back(a);\n }\n cout << x.size()<<endl\n vector<int>::iterator p;\n for(auto k : x){\n cout << k << \" \";\n }\n cout << endl;\n return x;\n}\n
iterator : class inside vector
generic classes vector<string> notes;
Have to specify two types
It is able to increase its internal capacity as required: as more items are added, it simply makes enough room for them
It keeps its own private count of how many items it is currently storing. Its size method returns the number of objects currently stored in it
It maintains the order of items you insert into it.You can later retrieve them in the same order.
"},{"location":"oop/Container/Container/#basic-vector-operations","title":"Basic Vector Operations","text":"vector<Elem>c\nvector<Elem>c1(c2)\n
V.size()\nV.empty()\nV.swap(v2)\n
I.begin()\nI.end()\n
V.at(index)\nV[index]\nV.front()\nV.back()\n
V.push_back(e)\nV.pop_back(e)\nV.insert(pos,e)\nV.erase(pos)\nV.clear()\nV.find(first,last,item)\n
int main(){\n vector<Student> ss //Student is a class\n Student s(1);\n ss.push_back(s);\n s.x = 10; // does not change ss[0]\n}\n
int main(){\n vector<Student*> ss \n Student s(1);\n ss.push_back(&s);\n s.x = 10; //will change(obviously)\n}\n
"},{"location":"oop/Container/Container/#operations","title":"Operations","text":"<vector>
\u53ef\u4ee5\u5411\u4efb\u610f\u4e0b\u6807\u8d4b\u503c\uff0c\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f46\u662f\u8fd9\u4e0d\u4f1a\u6539\u53d8.size() .back()
\u7b49 \uff0c\u6240\u4ee5\u5b9e\u9645\u4e0a\u6211\u4eec\u8981\u7528push_back()
\u7b49\u53bbinsertp!=s.end()
#include <iostream>\nusing namespace std;\n#include<list>\nint main(){\n list <int> L;\n for(int i=0;i<5;i++){\n L.push_back(i);\n }\n L.erase(++L.begin());\n copy(L.begin(),L.end(),ostream_iterator<int>(cout,\",\"));\n}\n
\u901a\u5e38\u7528vector \uff1a \u66f4\u8282\u7ea6\u7a7a\u95f4,\u9664\u975e\u9700\u8981\u5927\u91cf\u589e\u52a0\uff0c\u5220\u9664
"},{"location":"oop/Container/Container/#maps","title":"Maps","text":"Hash
#include <map>\nmap<long,int> root;\nroot[4] = 2;\nroot[1000000] = 1000;\nlong l;\ncin >> l;\nif (root.count(l))\n cout<<root[l]\nelse cout<<\u201cNot perfect square\u201d;\n
"},{"location":"oop/Container/Container/#pitfalls","title":"Pitfalls","text":"if ( my_list.count() == 0 ) { ... } // Slow\nif ( my_list.empty() ) {...} // Fast\n
list<int> L;\nlist<int>::iterator li;\nli = L.begin();\nL.erase(li);\n++li; // WRONG\n// Use return value of erase to advance\nli = L.erase(li); // RIGHT\n
map<>
if (foo[\"bob\"]==1)\n//silently created entry \u201cbob\u201d\n//Solutions: Use count() to check for a key without creating a new entry. if ( foo.count(\"bob\") )\n
"},{"location":"oop/Container/Container/#function","title":"Function","text":""},{"location":"oop/Container/Container/#function-overloading","title":"Function Overloading","text":"void print(char * str, int width); // #1 \nvoid print(double d, int width); // #2 \nvoid print(long l, int width); // #3 \nvoid print(int i, int width); // #4 \nvoid print(char *str); // #5 \nprint(\"Pancakes\", 15); \nprint(\"Syrup\"); \nprint(1999.0, 10); \nprint(1999, 12); \nprint(1999L, 15);\n
A default argument is a value given in the declaration that the compiler automatically inserts if you don\u02bct provide a value in the function call.
int harpo(int n, int m = 4, int j = 5);\nint chico(int n, int m = 6, int j); // illegal\n//To define a function with an argument list, defaults must be added from right to left.\nint groucho(int k = 1, int m = 2, int n = 3);\nbeeps = harpo(2);\nbeeps = harpo(1,8);\nbeeps = harpo(8,7,6);\n
void f(int i, int j = 10);\nint main()\n{ ...\n}\nvoid f(int i, int j){\n ...\n}\n// USE IN THIS WAY\n
"},{"location":"oop/Container/Container/#friend-access-control","title":"Friend [Access Control]","text":"struct X{\n private:\n int x;\n public :\n void initialize();\n friend void g(X*,int);\n friend void Y::f(X*);\n friend struct Z;\n friend void h();\n};\n
"},{"location":"oop/Container/Container/#overhead-for-a-function-call","title":"Overhead for a function call","text":""},{"location":"oop/Container/Container/#inline-functions","title":"Inline Functions","text":"inline int f(int i) {\n return i*2;\n}\nmain() {\n int a=4;\n int b = f(a); // become b = a * 2;\n}\n
// .h File!!!\nclass Point{\n ...\n public:\n inline void print(string & msg = \"\");\n};\ninline void Point::print(string & msg = \"\"){\n ...\n}\n
class Cup {\n int color;\npublic:\n int getColor() { return color; }\n void setColor(int color) {\n this->color = color;\n }\n};\n
const int a = 6
-- not changable variable
const int bufsize = 1024;
Unless you make an explicit extern declaration , like extern const int bufsize;
Compiler to make sure that it will not be changed.
Connot be optimized : \uff08remain variables\uff09
const int x = 123; // const, literal\nx = 27; // illegal!\nx++; // illegal!\nint y = x; // ok, copy const to non-const\ny = x; // ok, same thing\nconst int z = y; // ok, const is safer\n
const int i[] = { 1, 2, 3, 4 };\nfloat f[i[3]]; // Illegal(in C++98)\nstruct S { int i, j; };\nconst S s[] = { { 1, 2 }, { 3, 4 } };\ndouble d[s[1].j]; // Illegal\n
const
for aggregates, but storage will be allocated. In these situations,const
means \"a piece of storage that cannot be changed.\" However,the value cannot be used at compile time because the compiler is not required to know the contents of the storage at compile time.
"},{"location":"oop/Container/Container/#pointer","title":"Pointer","text":"char * const q = \"abc\"; // q is const\n*q = 'c'; // ERROR\n// char a[] = \"abc\"; *q = 'c' is ok.\nq++; // ERROR\nconst char *p = \"ABCD\"; // (*p) is a const char\n*p = 'b'; // ERROR! (*p) is the const\n
string p1(\"Fred\");\nconst string* p = &p1; // p1 cannot be changed through p.\nstring const* p = &p1; // like the first one.\nstring *const p = &p1; // p cannot be changed.only point to p1.\n
"},{"location":"oop/Container/Container/#string-literals","title":"String Literals","text":"char *s = \"Hello\"; //can be compile but bus error because actually const char *s\n// Add const compile will not pass.\ns[0] = 'K';\nprintf(\"%s\\n\",s);\n// Write in an array\nchar s[] = \"Hello, world!\";\n
"},{"location":"oop/Container/Container/#const-object","title":"Const Object","text":"const Currency the_raise(42, 38);\n
Compiler Will ERROR:
Thus,when we write all the member functions,SPECIFY CONST! For Compiler to know if it will change the value of member variables!
int Date::set_day(int d) {\n //...error check d here...\n day = d; // ok, non-const so can modify\n}\nint Date::get_day() const {\n day++; // ERROR modifies data member\n set_day(12); // ERROR calls non-const member\n return day; // ok\n}\n
const
\u5b9a\u4e49\u7684\u5bf9\u8c61\uff0c\u53ea\u80fd\u8c03\u7528\u5e26 const
\u5c5e\u6027\u7684\u6210\u5458\u51fd\u6570\u3002const
\u5fc5\u987b\u4fdd\u8bc1\u6210\u5458\u53d8\u91cf\u6709\u521d\u59cb\u503c\uff01\u9ed8\u8ba4\u6784\u9020\u51fd\u6570.
Overload (const can distinguish the two functions.)\uff0c\u4f1a\u6839\u636e\u5bf9\u8c61\u8c03\u7528\u65f6\u662f\u5426 const
\u6765\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u6210\u5458\u51fd\u6570
\u4e8b\u5b9e\u4e0a const
\u540e\u7684\u6210\u5458\u51fd\u6570\u53c2\u6570\uff0c\u76f8\u5f53\u4e8e const A *this
, \u800c\u4e0d\u52a0 const \u5c31\u53ea\u662f A *this
.
const
\u4f5c\u7528\u5728\u9690\u85cf\u53c2\u6570 A* this
void f() const;\nvoid f();\n
\u53ea\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee[\u53ea\u5728\u7b2c\u4e00\u6b21\u88ab\u6267\u884c\u5230\u7684\u65f6\u5019]\u65f6\u88ab\u6784\u9020
\u51fa\u73b0\u5728\u5168\u5c40\u53d8\u91cf/\u51fd\u6570\u524d\uff0c\u8868\u793a\u8bbf\u95ee\u9650\u5236\uff0c\u53ea\u6709\u5f53\u524d\u6587\u4ef6\u53ef\u4ee5\u8bbf\u95ee
\u5728\u7a0b\u5e8f\u7ed3\u675f\u540e\u6790\u6784
\u5168\u5c40\u53d8\u91cf\u7684\u6784\u9020\u53d1\u751f\u5728 main()
\u4e4b\u524d\uff0c\u5728 main()
\u6216\u8005 exit()
\u4e4b\u540e\u6790\u6784 \u4f46\u662f\u4e0d\u540c\u7f16\u8bd1\u5355\u5143\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5982\u679c\u5b58\u5728\u4f9d\u8d56\u5173\u7cfb\uff0c\u4f46\u8fd9\u662f\u65e0\u6cd5\u4fdd\u8bc1\u7684
\u6700\u597d\u4e0d\u5728C++\u4f7f\u7528global variable
\u8bbf\u95ee\u53d7\u9650\uff0c\u9650\u4e8e\u7c7b\u5185\u90e8\uff0c\u5b9e\u9645\u4e0a\u662f\u5168\u5c40\u53d8\u91cf\u3002
\u5728\u8fd9\u4e2a\u7c7b\u5185\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u7ef4\u6301\u76f8\u540c\u7684\u503c\uff0c\u5bf9\u8c61 A \u4fee\u6539\u4e86\u90a3\u4e48\u5bf9\u8c61 B \u7684\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\u4e5f\u4f1a\u6539\u53d8
\u53ef\u4ee5\u5728\u6ca1\u6709\u521b\u5efa\u7c7b\u7684\u5bf9\u8c61\u7684\u65f6\u5019\u5c31\u80fd\u8c03\u7528\u9759\u6001\u6210\u5458\u51fd\u6570
#ifndef _STAT_MEM_\n#define _STAT_MEM_\n\nclass StatMem{\npublic:\n int getHeight(){return m_h;}\n void setHeight(int i){m_h = i}\n int getWeight(){return m_w;}\n void setWeight(int i){m_w = i;}\n\n static int m_h;\n int m_w;\n}\n#endif \n
#include\"StatMem.h\"\nint StatMem::m_h\n
\u200b \u4e14\u4e0d\u80fd\u518d\u52a0static
Example
void main() {\n using MyLib::foo;\n using MyLib::Cat;\n foo();\n Cat c;\n c.Meow();\n}\n
// Mylib.h\nnamespace XLib {\n void x();\n void y();\n}\nnamespace YLib {\n void y();\n void z();\n}\n\n//Main\nvoid main() {\n using namespace XLib;\n using namespace YLib;\n x(); // OK\n y(); // Error: ambiguous\n XLib::y(); // OK, resolves to XLib\n z(); // OK\n}\n
"},{"location":"oop/Container/Container/#namespace-aliase","title":"Namespace aliase","text":"namespace supercalifragilistic{\n void f();\n}\nnamespace short1 = supercalifragilistic;\nshort1::f();\n
"},{"location":"oop/Container/Container/#namespace-composition","title":"Namespace composition","text":"namespace first {\n void x();\n void y();\n}\nnamespace second {\n void y();\n void z();\n}\nnamespace mine {\n using namespace first;\n using namespace second;\n using first::y(); // resolve clashes to first::x()\n void mystuff();\n // ...\n}\n
"},{"location":"oop/Container/Container/#namespaces-are-open","title":"Namespaces are open","text":"//header1.h\nnamespace X {\n void f();\n}\n// header2.h\nnamespace X {\n void g(); // X now has f() and g();\n}\n
"},{"location":"oop/Container/Container/#namespace-selection","title":"Namespace selection","text":"namespace mine {\n using orig::Cat; // use Cat class from orig\n void x();\n void y();\n}\n
"},{"location":"oop/Container/Container/#appendix","title":"Appendix","text":"C++
\u94fe\u63a5\u5668\u4e0d\u53bb\u505a\u53c2\u6570\u7c7b\u578b\u7684\u5224\u65ad\uff0c\u90a3\u4e48\u600e\u4e48\u505aoverload , namespace\uff1ff(int a,float b)
will turn into _f_int_double_()
namespace func(void f();) _func_f_()
C
\u7f16\u8bd1\u4ea7\u751f\u7684\u662f\u4e0d\u5e26\u4e0b\u5212\u7ebf\u7684 solution\uff1aexetern \"C\"{\n #include \"oldc.h\"\n}\n
"},{"location":"oop/Exceptions/lec/","title":"Exception & File","text":""},{"location":"oop/Exceptions/lec/#exceptions","title":"Exceptions","text":"
Exceptions are a mechanism for handling errors in C++ programs. They are used to signal that an error has occurred and to specify what should be done to handle the error.
The significant benefit of exceptions is that they clean up error handling code.
It separates the code that describes what you want to do from the code that is executed.
\u53ef\u4ee5\u9884\u89c1\u7684\u9519\u8bef\uff1a
\u53ef\u4ee5 fseek \u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u518d ftell \u672b\u5c3e\u7684\u503c\u3002
errorCodeType readFile { initialize errorCode = 0;\n open the file;\n if ( theFilesOpen ) { \n determine its size;\n if ( gotTheFileLength ) { \n allocate that much memory;\n if ( gotEnoughMemory ) { \n read the file into memory;\n if ( readFailed ) { \n errorCode = -1;\n }\n }else {\n errorCode = -2;\n } \n } else {\n errorCode = -3;\n }\n close the file;\n if ( theFILEDidntClose && errorCode == 0 ) { \n errorCode = -4;\n }else {\n errorCode = errorCode and -4;\n } \n } else {\n errorCode = -5;\n }\n return errorCode;\n}\n
try {\n open the file;\n determine its size;\n allocate that much memory;\n read the file into memory;\n close the file; \n} \ncatch ( fileOpenFailed ) { doSomething; } \ncatch ( sizeDeterminationFailed ) { doSomething; } \ncatch ( memoryAllocationFailed ) { doSomething; } \ncatch ( readFailed ) { doSomething; } \ncatch ( fileCloseFailed ) { doSomething; }\n
template <class T> class Vector { \nprivate: \n T* m_elements; \n int m_size; \npublic: \n Vector (int size = 0) : \n m_size(size) ... \n ~Vector () { delete [] m_elements; } \n void length(int); \n int length() { return m_size; } \n T& operator[](int); \n};\n
\u95ee\u9898\uff1a\u5f53\u7528\u6237\u8bd5\u56fe\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u65f6\uff0c\u5e94\u8be5\u600e\u4e48\u5904\u7406\uff1f
T& Vector::operator[](int index) { \n if (index < 0 || index >= m_size){\n throw \"Index out of range\"; \n }\n return m_elements[index]; \n}\n
Assertion vs. Exception
try{}
, \u6211\u4eec\u5c31\u5224\u65ad\u5339\u914d\u4e00\u4e2a\u5f02\u5e38\u7c7bclass VectorIndexError { \n public: \n VectorIndexError(int v) : m_badValue(v) { } \n ~VectorIndexError() { } \n void diagnostic() { cerr << \"index \" << m_ badValue << \"out of range!\"; } \n private: \n int m_badValue; \n};\ntemplate <class T> \nT& Vector::operator[](int index) { \n if (index < 0 || index >= m_size) \n throw VectorIndexError(index); \n return m_elements[index]; \n}\n
void outer2() { \nString err(\"exception caught\"); \ntry {\n func(); \n} catch (VectorIndexError) { \n cout << err; \n throw; // propagate the exception \n} \n}\n
Two forms
catch (SomeType v) { // handler code } catch (...) { // handler code }
new
does NOT returned 0 on failure. new
raises a bad_alloc() exception.void abc():throw (MathErr);
","text":"Printer::print(Document&) : throw(PrinterOffLine, BadDocument) { ... }\nPrintManager::print(Document&) : throw (BadDocument) { ... }// raises or doesn\u2019t handle BadDocument \nvoid goodguy() : throw () { }// handles all exceptions \nvoid average() { } // no spec, no checking,\n
f() {\n A *p = new A();\n ...\n delete p;\n}\n
delete this
:\u5fc5\u987b\u662f\u4e00\u4e2a\u5c40\u90e8\u5bf9\u8c61\uff0c\u4e0d\u80fd\u662f\u4e00\u4e2a\u5168\u5c40\u5bf9\u8c61\u3002c++ Error *p = new Error(); ... catch (Error e) { delete e; delete this; }
int get()\nwhile((ch=cin.get())!=EOF) {\n cout.put(ch);\n}\n
"},{"location":"oop/Smart/lec/","title":"Smart Pointer","text":""},{"location":"oop/Smart/lec/#smart-pointers","title":"Smart Pointers","text":"String abc(\"abcdefg\");
p->decrement(); // p's count will decrease \np = q; \nq->increment(); // q/p's count will increase\n
"},{"location":"oop/Smart/lec/#ucobject","title":"UCObject","text":"#ifndef UCObject_H_\n#define UCObject_H_\n#include <assert.h> \nclass UCObject { \npublic: \n UCObject() : m_refCount(0){ } \n /**\n * Only destruct when refCount is 0\n * assert, \u56e0\u4e3a\u4e0d\u662f\u5bf9\u8c61\u7684\u95ee\u9898\uff0c\u662f\u5916\u90e8\u7684\u95ee\u9898\u3002\n */\n virtual ~UCObject() { assert(m_refCount == 0);}; \n /**\n * \u4e0d\u62f7\u8d1d refcount \n */ \n UCObject(const UCObject&) : m_refCount(0) { } \n void incr() { m_refCount++; } \n void decr(); \n int references() { return m_refCount; } \nprivate: \n int m_refCount; \n};\ninline void UCObject::decr() { \n m_refCount -= 1; \n if (m_refCount == 0) { \n delete this; // goto UCObject::~UCObject()\n } \n} \n#endif\n
#ifndef UCPointer_H_\n#define UCPointer_H_\ntemplate <class T> \nclass UCPointer { \nprivate: \n T* m_pObj; \n void increment() { if (m_pObj) m_pObj->incr(); } \n void decrement() { if (m_pObj) m_pObj->decr(); } \npublic: \n UCPointer(T* r = 0): m_pObj(r) { increment();} \n ~UCPointer() { decrement(); }; \n UCPointer(const UCPointer<T> & p); \n UCPointer& operator=(const UCPointer<T> &); \n T* operator->() const; \n T& operator*() const { return *m_pObj; }; \n}\ntemplate <class T> \nUCPointer<T>::UCPointer(const UCPointer<T> & p){ \n m_pObj = p.m_pObj; \n increment(); \n}\ntemplate <class T> \nUCPointer<T>& UCPointer<T>::operator=(const UCPointer<T>& p){ \n if (m_pObj != p.m_pObj){ \n decrement(); \n m_pObj = p.m_pObj; \n increment(); \n } \n return *this; \n}\ntemplate<class T> \nT* UCPointer<T>::operator->() const { \n return m_pObj; \n}\ntemplate <class T>\nT& UCPointer<T>::operator*() const { \n return *m_pObj; \n}\n#endif\n
Ellipse elly(200F, 300F); \nUCPointer<Shape> p(&elly); \np->render(); // calls Ellipse::render() on elly!\n
p->render();
\u4f1a\u8c03\u7528 m_pObj->render()
abc = \"Hello World\"\n
#ifndef StringRep_H_\n#define StringRep_H_\n#include <cstring.h>\n#include \"UCObject.h\" \nclass StringRep : public UCObject { \npublic: \n StringRep(const char *); \n ~StringRep(); \n StringRep(const StringRep&); \n int length() const{ return strlen(m_pChars); } \n int equal(const StringRep&) const; \nprivate: \n char *m_pChars; \n // reference semantics -- no assignment op! \n // \u79c1\u6709\uff0c\u5916\u754c\u4e0d\u80fd\u505a StringRep \u7684\u8d4b\u503c\n void operator=(const StringRep&) {}\n};\n/**\n * 1. \u4f20\u5165\u7684\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff0c\u90a3\u4e48\u53ea\u6709\u4e00\u4e2a \\0 \n */\nStringRep::StringRep(const char *s) { \n if (s) { \n int len = strlen(s) + 1; \n m_pChars = new char[len]; \n strcpy(m_pChars , s); \n } \n else { \n m_pChars = new char[1]; \n *m_pChars = '\\0'; \n } \n} \nStringRep::~StringRep() { \n delete [] m_pChars ; \n}\nStringRep::StringRep(const StringRep& sr) { \n int len = sr.length(); \n m_pChars = new char[len + 1]; \n strcpy(m_pChars , sr.m_pChars ); \n} \nint StringRep::equal(const StringRep& sp) const { \n return (strcmp(m_pChars, sp.m_pChars) == 0); \n}\n#endif\n
class String { \npublic: \n String(const char *); \n ~String(); \n String(const String&); \n String& operator=(const String&); \n int operator==(const String&) const; \n String operator+(const String&) const; \n int length() const; \n operator const char*() const; \nprivate: \n UCPointer<StringRep> m_rep; \n};\nString::String(const char *s) : m_rep(0) { \n m_rep = new StringRep(s); \n} \nString::~String() {} \n// Again, note constructor for rep in list. \nString::String(const String& s) : m_rep(s.m_rep) { \n // \u505a UCPointer<StringRep> \u7684\u62f7\u8d1d\u6784\u9020\n} \nString& String::operator=(const String& s) { \n m_rep = s.m_rep; // let smart pointer do work! \n return *this; \n}\nint String::operator==(const String& s) const { \n // overloaded -> forwards to StringRep \n return m_rep->equal(*s.m_rep); // smart ptr * \n} \nint String::length() const { \n return m_rep->length(); \n}\n
* \u8fd9\u91cc m_rep(0) \u5b9e\u9645\u4e0a\u662f\u7528 0 \u53bb\u505a UCP \u7684\u6784\u9020(\u4e5f\u53ef\u4ee5\u4e0d\u5199\uff0c\u56e0\u4e3a\u9ed8\u8ba4 0). * new \u8fd4\u56de\u7684\u662f StringRep *, \u800c\u5de6\u8fb9\u662f UCP, \u8fd9\u91cc\u8d4b\u503c\u65f6\u5c31\u4f1a\u628a StringRep * \u8f6c\u5316\u4e3a UCP, \u518d\u8d4b\u503c"},{"location":"oop/Smart/lec/#main","title":"Main","text":"#include<iostream>\nusing namespace std;\n#include \"String.h\"\nint main(){\n cout << \"Hello\\n\";\n /**\n * 1. Construct a StringRep object with \"Hello\"\n * 2. Construct a UCP<StringRep> object with the StringRep object\n * 3. overlaoded '=' operator for UCP<StringRep> object\n * (m_rep = new StringRep(\"Hello\");)\n * 4. Destructor for UCP<StringRep> object\n */\n String a = \"Hello\";\n /**\n * Copy constructor for String object\n * 1.String::String(const String& s) : m_rep(s.m_rep) { //\u505a UCPointer<StringRep> \u7684\u62f7\u8d1d\u6784\u9020} \n * 2.Copy constructor for UCP<StringRep> object\n * (1) m_pObj = p.m_pObj; \n * (2)increment(); \n */\n String b=a;\n /**\n * Overloaded '=' operator for String object\n * 1.Overloaded '=' operator for UCP<StringRep> object\n * So actually nothing beacuse b and a are pointing to the same object\n */\n b = a;\n b = 'bye';\n /**\n * Overloaded '=' operator for String object\n * 1.Overloaded '=' operator for UCP<StringRep> object\n */\n b = a;\n cout(a==b)<<endl;\n}\n
\u5728UCPointer\u505aincrease
\u548cdecrease
\u5728UCObject\u5224\u65ad\u662f\u5426ref=0
\u4e4b\u540e\u5220\u9664
\u5728 C++ \u4e2d\uff0c\u6709\u56db\u79cd\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\uff1astatic_cast
\u3001dynamic_cast
\u3001const_cast
\u548c reinterpret_cast
\u3002\u5b83\u4eec\u5404\u81ea\u6709\u4e0d\u540c\u7684\u7528\u9014\u548c\u9650\u5236\u3002\u4ee5\u4e0b\u662f\u5bf9\u5b83\u4eec\u7684\u8be6\u7ec6\u89e3\u91ca\uff1a
static_cast
","text":"static_cast
\u7528\u4e8e\u5728\u76f8\u5173\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u663e\u5f0f\u8f6c\u6362\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a
int
\u5230 float
\u3002void*
\u6307\u9488\u8f6c\u6362\u56de\u539f\u7c7b\u578b\u3002#include <iostream>\nusing namespace std;\n\nclass Base {};\nclass Derived : public Base {};\n\nint main() {\n int a = 10;\n double b = static_cast<double>(a); // \u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\n cout << b << endl;\n\n Derived d;\n Base* basePtr = static_cast<Base*>(&d); // \u5411\u4e0a\u8f6c\u6362\n Derived* derivedPtr = static_cast<Derived*>(basePtr); // \u5411\u4e0b\u8f6c\u6362\n\n void* voidPtr = static_cast<void*>(&d); // \u8f6c\u6362\u4e3a void*\n Derived* derivedPtr2 = static_cast<Derived*>(voidPtr); // \u8f6c\u6362\u56de\u539f\u7c7b\u578b\n}\n
"},{"location":"oop/Smart/lec/#2-dynamic_cast","title":"2. dynamic_cast
","text":"dynamic_cast
\u7528\u4e8e\u5728\u7c7b\u5c42\u6b21\u7ed3\u6784\u4e2d\u8fdb\u884c\u5b89\u5168\u7684\u5411\u4e0b\u8f6c\u6362\uff08\u4ece\u57fa\u7c7b\u5230\u6d3e\u751f\u7c7b\uff09\uff0c\u5e76\u4e14\u53ea\u80fd\u7528\u4e8e\u6709\u865a\u51fd\u6570\u7684\u591a\u6001\u7c7b\u578b\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u6307\u9488\u7c7b\u578b\u4f1a\u8fd4\u56de nullptr
\uff0c\u5f15\u7528\u7c7b\u578b\u4f1a\u629b\u51fa std::bad_cast
\u5f02\u5e38\u3002
#include <iostream>\nusing namespace std;\n\nclass Base {\npublic:\n virtual ~Base() {}\n};\n\nclass Derived : public Base {};\n\nint main() {\n Base* basePtr = new Derived();\n Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);\n if (derivedPtr) {\n cout << \"dynamic_cast \u6210\u529f\" << endl;\n } else {\n cout << \"dynamic_cast \u5931\u8d25\" << endl;\n }\n\n delete basePtr;\n}\n
"},{"location":"oop/Smart/lec/#3-const_cast","title":"3. const_cast
","text":"const_cast
\u7528\u4e8e\u5728\u76f8\u540c\u7c7b\u578b\u4e4b\u95f4\u53bb\u6389\u6216\u6dfb\u52a0 const
\u6216 volatile
\u5c5e\u6027\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u53bb\u6389 const
\u5c5e\u6027\uff0c\u4ee5\u4fbf\u4fee\u6539\u539f\u672c\u662f const
\u7684\u6570\u636e\u3002
#include <iostream>\nusing namespace std;\n\nvoid modify(const int* p) {\n int* modifiable = const_cast<int*>(p);\n *modifiable = 20;\n}\n\nint main() {\n const int a = 10;\n cout << \"\u4fee\u6539\u524d: \" << a << endl;\n modify(&a);\n cout << \"\u4fee\u6539\u540e: \" << a << endl; // \u672a\u5b9a\u4e49\u884c\u4e3a\n}\n
"},{"location":"oop/Smart/lec/#4-reinterpret_cast","title":"4. reinterpret_cast
","text":"reinterpret_cast
\u7528\u4e8e\u5728\u4e0d\u540c\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u4f4e\u7ea7\u522b\u7684\u3001\u53ef\u80fd\u4e0d\u5b89\u5168\u7684\u8f6c\u6362\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u6307\u9488\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u5b83\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u5b9e\u9645\u7684\u6570\u636e\u8f6c\u6362\uff0c\u53ea\u662f\u7b80\u5355\u5730\u91cd\u65b0\u89e3\u91ca\u4f4d\u6a21\u5f0f\u3002
#include <iostream>\nusing namespace std;\n\nint main() {\n int a = 42;\n void* ptr = &a;\n int* intPtr = reinterpret_cast<int*>(ptr);\n cout << *intPtr << endl;\n\n // \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u6307\u9488\n intptr_t intVal = 0x12345678;\n void* voidPtr = reinterpret_cast<void*>(intVal);\n cout << voidPtr << endl;\n\n // \u5c06\u6307\u9488\u8f6c\u6362\u4e3a\u6574\u6570\n intVal = reinterpret_cast<intptr_t>(voidPtr);\n cout << std::hex << intVal << endl;\n}\n
"},{"location":"oop/Smart/lec/#_1","title":"\u603b\u7ed3","text":"static_cast
\uff1a\u7528\u4e8e\u76f8\u5173\u7c7b\u578b\u4e4b\u95f4\u7684\u663e\u5f0f\u8f6c\u6362\uff0c\u7f16\u8bd1\u65f6\u68c0\u67e5\uff0c\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002dynamic_cast
\uff1a\u7528\u4e8e\u5b89\u5168\u7684\u5411\u4e0b\u8f6c\u6362\uff0c\u591a\u6001\u7c7b\u578b\u4e2d\u4f7f\u7528\uff0c\u8fd0\u884c\u65f6\u68c0\u67e5\uff0c\u8f6c\u6362\u5931\u8d25\u65f6\u8fd4\u56de nullptr
\u6216\u629b\u51fa\u5f02\u5e38\u3002const_cast
\uff1a\u7528\u4e8e\u6dfb\u52a0\u6216\u53bb\u6389 const
\u6216 volatile
\u5c5e\u6027\uff0c\u4e0d\u6539\u53d8\u5e95\u5c42\u6570\u636e\u3002reinterpret_cast
\uff1a\u7528\u4e8e\u5728\u4e0d\u540c\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u4f4e\u7ea7\u522b\u7684\u8f6c\u6362\uff0c\u4e0d\u5b89\u5168\uff0c\u53ea\u662f\u91cd\u65b0\u89e3\u91ca\u4f4d\u6a21\u5f0f\u3002void f(){\n Stash students();\n}\n
Stash students();
is a function prototype, while students
is a function that returns a Stash
object.#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(){cout<<\"A()\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n};\nvoid f(A aa){\n cout << \"aa: \"<<&aa << endl;\n cout << aa.getVal() << endl;\n}\nint main(){\n A a;\n a.setVal(10);\n cout << \"a: \"<<&a << endl;\n cout << \"-------------------\\n\";\n f(a);\n cout << \"-------------------\\n\";\n\n return 0;\n}\n
A()\na: 0x16dc9f088\n-------------------\naa: 0x16dc9f068\n10\n~A()\n-------------------\n~A()\n
Only one time of constructor is called.
#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i){cout<<i<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n};\nvoid f(A aa){\n cout << \"aa: \"<<&aa << endl;\n cout << aa.getVal() << endl;\n}\nint main(){\n A a(10);\n a.setVal(10);\n cout << \"a: \"<<&a << endl;\n cout << \"-------------------\\n\";\n f(a);\n cout << \"-------------------\\n\";\n\n return 0;\n}\n
Without default constructor, still one time of constructor is called.
#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i){cout<<i<<endl;}\n A(const A& r):i(r.i){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n};\nvoid f(A aa){\n cout << \"aa: \"<<&aa << endl;\n cout << aa.getVal() << endl;\n}\nint main(){\n A a(10);\n a.setVal(10);\n cout << \"a: \"<<&a << endl;\n cout << \"-------------------\\n\";\n f(a);\n cout << \"-------------------\\n\";\n\n return 0;\n}\n
10\na: 0x16b763088\n-------------------\nA(const A&)\naa: 0x16b763068\n10\n~A()\n-------------------\n~A()\n
in f(), A(const A&)
is called.
A(const A&)
is a copy constructor.Person baby_a(\"Fred\");\n// these use the copy ctor\nPerson baby_b = baby_a; // not an assignment\nPerson baby_c( baby_a ); // not an assignment\n
\u5982\u679c\u6709\u6210\u5458\u53d8\u91cf\u662f\u6307\u9488\uff0c\u4f1a\u548c\u539f\u6765\u5bf9\u8c61\u4e00\u6837\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58! \u5982\u679c\u6709\u4e00\u4e2a\u5bf9\u8c61\u88ab\u6790\u6784\uff0c\u90a3\u4e48\u8fd9\u5757\u5185\u5b58\u5c31\u88ab delete, \u8fd9\u5c31\u53d8\u6210\u4e86\u65e0\u6548\u5185\u5b58!
#include<iostream>\nusing namespace std;\nclass A{\n int i;\n string s;\npublic:\n A(int i){cout<<i<<endl;}\n A(const A& r):i(r.i),s(r.s){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n};\nA f(A aa){\n cout << \"------------\"<<endl;\n A bb(20);\n cout << \"aa: \"<<&aa << endl;\n cout << aa.getVal() << endl;\n cout << \"Destructor for aa\"<<endl;\n return bb;\n}\nint main(){\n A a(10);\n a.setVal(10);\n cout << \"a: \"<<&a << endl;\n cout << \"-------------------\\n\";\n A d = f(a);\n cout << \"-------------------\\n\";\n d.setVal(30);\n return 0;\n}\n
10\na: 0x16b247190\n-------------------\nA(const A&)\n------------\n20\naa: 0x16b247130\n10\nDestructor for aa\n~A()\n-------------------\n~A()\n~A()\n
\u7f16\u8bd1\u5668\u5728return\u5904\u4f18\u5316\u4e86\uff0c\u4e5f\u5c31\u662f\u76f4\u63a5\u628abb\u653e\u5728\u8981\u8fd4\u56de\u7684\u5730\u65b9\uff0c\u800c\u4e0d\u662f\u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u62f7\u8d1d\u5230\u8fd4\u56de\u7684\u5730\u65b9
C++ \u4f1a\u505a\u62f7\u8d1d\u6784\u9020\uff0c\u5982\u679c\u7c7b\u4e2d\u5168\u90e8\u90fd\u662f\u5bf9\u8c61\uff0c\u90a3\u4e48\u662fok\u7684\uff0c\u4f46\u662f\u5982\u679c\u6709\u6307\u9488\uff1a
way in
void f(Student i)
;void f(Student *p);
void f(Student& i);
way out
Student f()
;Student* f()
;Student& f()
;char *foo() {\n char *p; \n p = new char[10];\n strcpy(p, \"something\");\n return p;\n}\nvoid bar() {\n char *p = foo();\n printf(\"%s\", p);\n delete p;\n}\n
delete p
? 1. \u5982\u679c\u4e0d\u505a\uff0c\u51fa\u4e86\u51fd\u6570\u5c31\u627e\u4e0d\u5230\u8fd9\u4e2a\u7a7a\u95f4\u4e86\uff0c\u5185\u5b58\u6cc4\u6f0f:\u5982\u679c\u4e00\u4e2a\u4e00\u76f4\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u4e2d\u6709\u5185\u5b58\u6cc4\u6f0f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u8d8a\u6765\u8d8a\u6162\uff0c\u56e0\u4e3a\u5185\u5b58\u8d8a\u6765\u8d8a\u5c11\uff01 2. \u90a3\u6211\u600e\u4e48\u77e5\u9053\u6211\u5e94\u8be5delete\u5462\uff1f\u5982\u679c\u662fnew\u51fa\u6765\u7684\uff0c\u90a3\u4e48\u5c31\u8981delete\uff0c\u5982\u679c\u662f\u6307\u5411\u5168\u5c40\u7684\uff0c\u90a3\u4e48\u5c31\u4e0d\u7528deletewhile(){\n Person p;\n p.read();\n s.put(&p);\n}\n
C++\u5bf9\u672c\u5730\u53d8\u91cf\u4f1a\u81ea\u52a8\u56de\u6536\uff0c\u4f46\u662f\u5bf9new\u51fa\u6765\u7684\u4e0d\u4f1a\u81ea\u52a8\u56de\u6536\uff0c\u6240\u4ee5\u5199\u7684\u65f6\u5019\u5c3d\u91cf\u591a\u7528\u672c\u5730\u53d8\u91cf \u8fd4\u56de\u5bf9\u8c61\u7684\u8bdd\uff0c\u8981\u4e0d\u62f7\u8d1d\u6784\u9020\uff0c\u8981\u4e0d\u79fb\u52a8\u6784\u9020\uff0c\u603b\u4f1a\u88ab\u56de\u6536\uff0c\u4e0d\u7528\u62c5\u5fc3\u5185\u5b58\u6cc4\u6f0f
"},{"location":"oop/copy/lec/#move-ctor","title":"Move Ctor","text":""},{"location":"oop/copy/lec/#left-value-and-right-value","title":"Left Value and Right Value","text":"*
\u3001 []
\u8fd0\u7b97\u7684\u7ed3\u679c&&
\u6210\u4e3a\u53f3\u503c\u5f15\u7528 int x=20; // \u5de6\u503c\nint&& rx = x * 2; // x*2\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u53f3\u503c\uff0crx\u5ef6\u957f\u5176\u2f63\u547d\u5468\u671f\nint y = rx + 2; // \u56e0\u6b64\u4f60\u53ef\u4ee5\u91cd\u7528\u5b83:42\nrx = 100; // \u4e00\u65e6\u4f60\u521d\u59cb\u5316\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u5c31\u6210\u4e3a\u4e86\u4e00\u4e2a\u5de6\u503c\uff0c\u53ef\u4ee5\u88ab\u8d4b\u503c\nint&& rrx1 = x; // \u975e\u6cd5:\u53f3\u503c\u5f15\u7528\u65e0\u6cd5\u88ab\u5de6\u503c\u521d\u59cb\u5316\nconst int&& rrx2 = x; // \u975e\u6cd5:\u53f3\u503c\u5f15\u7528\u65e0\u6cd5\u88ab\u5de6\u503c\u521d\u59cb\u5316\n
x
\u4e0d\u5b58\u5728\u4e86\uff0c\u53f3\u503c\u5f15\u7528\u4f9d\u7136\u53ef\u4ee5\u4f7f\u7528int x = 10;\nint &&a = x + 2;\nint y = a + 2;\ncout << y << endl;\nx = 5;\ncout << y << endl;\na = 40;\ncout << y << endl;\ncout << a << endl;\nint &&b = x; // ERR: \u53f3\u503c\u5f15\u7528\u4e0d\u80fd\u7ed1\u5de6\u503c\uff08\u4e3a\u4e86\u533a\u5206\nint &&b = x+0; // ok\n
c++11
:\u63a5\u53d7const\u5de6\u503c\u7684\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u53f3\u503c void fun(int &lref) {\n cout << \"l-value\" << endl;\n}\nvoid fun(int &&rref) {\n cout << \"r-value\" << endl;\n}\nint main() {\n int x = 10;\n fun(x); // l value\n fun(10); // r value\n}\n
Move ctor is a special constructor that takes an rvalue reference to an object of the same class and moves the resource owned by the rvalue reference to the object being constructed.
\u5982\u679c\u6709\u4e00\u4e2a\u5bf9\u8c61\uff0c\u91cc\u9762\u6709\u6307\u9488\u6307\u5411\u4e00\u5757\u5185\u5b58
(nullptr)
.DynamicArray(DynamicArray&& rhs) : m_size{rhs.m_size}, m_array{rhs.m_array}\n{\n rhs.m_size = 0;\n rhs.m_array = nullptr;\n cout << \"Move constructor: dynamic array is moved!\\n\";\n}\n
f(P p);
: \u4f20\u5165\u4e00\u4e2a\u53f3\u503c\uff0c\u4f1a\u8c03\u7528\u53f3\u503c\u5f15\u7528\u7684\u6784\u9020\u51fd\u6570f(P &&p);
: \u5982\u679c\u63d0\u4f9b\u4e86\u79fb\u52a8\u6784\u9020\uff0c\u53ef\u4ee5\u4f20\u4e00\u4e2a\u53f3\u503cf(const P &p);
f
\u5185\u7684\u672c\u5730\u53d8\u91cfstd::move
","text":"vector<int> v1{1, 2, 3, 4};\nvector<int> v2 = v1;\nvector<int> v3 = std::move(v1);// \u6b64\u65f6\u8c03\u7528\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\n
std::move
\u5c06 v1 \u8f6c\u5316\u4e3a\u53f3\u503c\uff0c\u4ece\u2f7d\u6fc0\u53d1 v3 \u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0c\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49//\u5c0f\u62ec\u53f7\u521d\u59cb\u5316\nstring str(\"hello\");\n//\u7b49\u53f7\u521d\u59cb\u5316\nstring str = \"hello\";\n//\u5927\u62ec\u53f7\u521d\u59cb\u5316\nstruct Studnet{\n char *name;\n int age;\n};\nStudnet s = {\"dablelv\", 18};//Plain of Data\u7c7b\u578b\u5bf9\u8c61\nStudnet sArr[] = {{\"dablelv\", 18}, {\"tommy\", 19}}; //POD\u6570\u7ec4\n
"},{"location":"oop/copy/lec/#_2","title":"\u5217\u8868\u521d\u59cb\u5316","text":"class Test{\n int a;\n int b;\n public:\n Test(int i, int j);\n};\nTest t{0, 0}; //C++11 only\uff0c\u76f8\u5f53\u4e8e Test t(0,0);\nTest *pT = new Test{1, 2}; //C++11 only\uff0c\u76f8\u5f53\u4e8e Test* pT=new Test(1,2);\nint *a = new int[3]{1, 2, 0}; //C++11 only\n
"},{"location":"oop/copy/lec/#delegating-ctor","title":"Delegating Ctor","text":"class class_c{\npublic:\n int max;\n int min;\n int middle;\n class c(int my_max){\n max = my_max>0?my_max:10;\n }\n class_c(int my_max, int my_min):class_c(my_max){\n min = my_min>0&&my_min<max?my_min:1;\n }\n class_c(int my_max, int my_min, int my_middle):class_c(my_max, my_min){\n middle = my_middle>min&&my_middle<max?my_middle:(min+max)/2;\n }\n};\nint main(){\n class_c c(10, 5, 7);\n}\n
"},{"location":"oop/copy/lec/#overloaded-operators","title":"Overloaded Operators","text":"Types that cannot be overloaded:
::
.
*
?
:
sizeof
typeid
new
delete
new[]
delete[]
static_cast
dynamic_cast
const_cast
reinterpret_cast
Only existing operators can be overloaded.
operator
followed by the operator to be overloadedclass A {\npublic:\n A(int ii):i(ii){}\n int get() {return i;}\n /* \u8fd4\u56de\u7684\u4e00\u5b9a\u662f A \u7684\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61 */\n const A operator+(const A &that) const {\n A c(this->i+that.i); /* \u8fd9\u91cc\u53ef\u4ee5\u8bbf\u95ee that. \u79c1\u6709\u662f\u9488\u5bf9\u7c7b\u7684\uff0c\u4e0d\u662f\u9488\u5bf9\u5bf9\u8c61\u7684\u3002 */\n return c;\n }\n //!\u8fd4\u56de\u7684\u5e94\u8be5\u662f\u53f3\u503c\uff0c\u6240\u4ee5const\uff01\n private:\n int i;\n}\nint main() {\n A a = 6;\n A b = 7;\n A c = a + b; /* a + 9 \u4e5f\u662f\u53ef\u4ee5\u7684\uff1b\u4f46 9 + a \u4e0d\u884c */\n cout << c.get() << endl; /* \u8f93\u51fa 13 */\n}\n
const
?const
member functions can be called on const
objectsconst
objects can only call const
member functionsconst
member functions can't change the objectconst
member functions can't call non-const member functionsoperator+
#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i):i(i){cout<<\"A()\"<<i<<endl;}\n A(const A& r):i(r.i){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n A operator+(const A& r){\n cout << \"operator+\" << endl;\n A a(this->i+r.i);\n return a;\n }\n};\nint main(){\n A a(10);\n A b(20);\n A c = a+b;\n}\n/*\nA()10\nA()20\noperator+\nA()30\n~A()\n~A()\n~A()\n */\n
A c = a+b;
is equivalent to A c = a.operator+(b);
A c = a+3;
is equivalent to A c = a.operator+(3);
And when using '3' as the second operand\uff1a we construct a temporary object of A(3) and pass it to the operator+ function.
#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i):i(i){cout<<\"A()\"<<i<<endl;}\n A(const A& r):i(r.i){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n A operator+(const A& r){\n cout << \"operator+\" << endl;\n A a(this->i+r.i);\n return a;\n }\n};\nint main(){\n A a(10);\n A b(20);\n A c = a+3;\n}\n/*\nA()10\nA()20\nA()3\noperator+\nA()13\n~A()\n~A()\n~A()\n~A()\n*/\n
However, if we want to use 3+a
, member function can't be used!!!
friend
of the class#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i):i(i){cout<<\"A()\"<<i<<endl;}\n A(const A& r):i(r.i){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal()const{return i;}\n void setVal(int i){this->i=i;}\n};\nA operator+(const A& r,const A&l){\n cout << \"+outside\" << endl;\n A temp(r.getVal()+l.getVal());\n return temp;\n}\nint main(){\n A a(10);\n A b(20);\n A c = 3+a;\n}\n/*\nA()10\nA()20\nA()3\n+outside\nA()13\n~A()\n~A()\n~A()\n~A()\n*/\n
=
,()
,[]
,->
and ->*
must be members.Note : What if A class don't have getVal()
function? * We can use friend
to access private members of class A.
friend const A operator+(const A& r,const A&l); \n
"},{"location":"oop/copy/lec/#the-prototype-of-operators","title":"The Prototype of operators","text":""},{"location":"oop/copy/lec/#-","title":"+
-
*
/
%
^
&
|
~
:","text":"const T operatorX(const T& l, const T& r);
!
&&
||
==
!=
<
>
<=
>=
:","text":"bool operatorX(const T& l);
==
!=
<
>
<=
>=
!=
in terms of ==
>
>=
<
in terms of <
class Integer { \n public: \n ... \n bool operator==( const Integer& rhs ) const; \n bool operator!=( const Integer& rhs ) const; \n bool operator<( const Integer& rhs ) const; \n bool operator>( const Integer& rhs ) const; \n bool operator<=( const Integer& rhs ) const; \n bool operator>=( const Integer& rhs ) const; \n }\nbool Integer::operator==( const Integer& rhs ) const { \n return i == rhs.i; \n} \n// implement lhs != rhs in terms of !(lhs == rhs) \nbool Integer::operator!=( const Integer& rhs ) const { \n return !(*this == rhs); \n} \nbool Integer::operator<( const Integer& rhs ) const { \n return i < rhs.i; \n}\n// implement lhs > rhs in terms of lhs < rhs \nbool Integer::operator>( const Integer& rhs ) const { \n return rhs < *this; \n} \n// implement lhs <= rhs in terms of !(rhs < lhs) \nbool Integer::operator<=( const Integer& rhs ) const { \n return !(rhs < *this); \n} \n// implement lhs >= rhs in terms of !(lhs < rhs) \nbool Integer::operator>=( const Integer& rhs ) const { \n return !(*this < rhs); \n}\n
[ ]
","text":"a[6]=7
E& T::operator[](int index);
#include <iostream>\nclass T {\nprivate:\n int data[10];\npublic:\n // \u91cd\u8f7d operator[]\n int& operator[](int index) {\n return data[index];\n }\n};\nint main() {\n T obj;\n // \u4f7f\u7528 operator[] \u4fee\u6539\u5bf9\u8c61\u7684\u503c\n obj[6] = 7;\n std::cout << obj[6] << std::endl; // \u8f93\u51fa 7\n return 0;\n}\n
class A {\npublic:\n A(int s):size(s){\n buf = new int[s];\n }\n int& operator [] (int idx){\n return buf[index];\n }\n virtual ~A(){\n delete [] buf;\n }\nprivate:\n int size;\n int *buf;\n}\n
++
and --
","text":"const Integer& operator++(); // prefix
++a.f()
,++a=5
\u8fd9\u6837\u7684\u64cd\u4f5c class Integer{\npublic:\n const Integer& operator++(); // prefix \n const Integer operator++(int); // postfix\n const Integer& operator--(); // prefix\n const Integer operator--(int); // postfix\n};\nconst Integer& Integer::operator++() { \n *this += 1; // increment have to overload +=\n return *this; // fetch \n} \n// int argument not used so leave unnamed so \n// won't get compiler warnings \nconst Integer Integer::operator++( int ){ \n Integer old(*this); // fetch \n ++(*this); // increment \u8c03\u7528\u4e86\u521a\u521a\u7684\u51fd\u6570\n return old; // return \n}\n\nint main(){\n ++x; // calls x.operator++(); \n x++; // calls x.operator++(0); \n --x; // calls x.operator--(); \n x--; // calls x.operator--(0); \n\n}\n
friend ostream& operator<<(ostream& os, const A& a);
ostream& operator <<(ostream & os, const A &a)\n{\n cout << a.size() << endl;\n return os;\n}\n
ostream& manip(ostream& out) { \n ... \n return out; \n} \nostream& tab ( ostream& out ) { \n return out << '\\t'; \n} \ncout << \"Hello\" << tab << \"World!\" << endl;\n
operator=
\uff09\u65f6\u901a\u5e38\u8fd4\u56de\u4e00\u4e2a\u5bf9\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u8fd9\u79cd\u505a\u6cd5\u6709\u51e0\u4e2a\u91cd\u8981\u7684\u539f\u56e0\uff1a","text":"\u652f\u6301\u94fe\u5f0f\u8d4b\u503c\uff1a \u901a\u8fc7\u8fd4\u56de\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u53ef\u4ee5\u652f\u6301\u94fe\u5f0f\u8d4b\u503c\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a
a = b = c;\n
\u5982\u679c\u8d4b\u503c\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u662f\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\uff0cb = c
\u4f1a\u8fd4\u56de b
\uff0c\u7136\u540e a = b
\u5c31\u53ef\u4ee5\u6b63\u5e38\u8fdb\u884c\u3002 \u6548\u7387\u8003\u8651\uff1a \u8fd4\u56de\u5f15\u7528\u907f\u514d\u4e86\u8fd4\u56de\u5bf9\u8c61\u65f6\u7684\u62f7\u8d1d\u5f00\u9500\u3002\u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\u4f1a\u5bfc\u81f4\u5bf9\u8c61\u7684\u590d\u5236\uff0c\u4ece\u800c\u589e\u52a0\u4e0d\u5fc5\u8981\u7684\u5f00\u9500\u3002\u800c\u8fd4\u56de\u5f15\u7528\u53ea\u9700\u8fd4\u56de\u4e00\u4e2a\u5730\u5740\uff0c\u6ca1\u6709\u989d\u5916\u7684\u6027\u80fd\u635f\u8017\u3002
\u4e0e\u5185\u7f6e\u7c7b\u578b\u7684\u884c\u4e3a\u4e00\u81f4\uff1a \u5185\u7f6e\u7c7b\u578b\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e5f\u8fd4\u56de\u5de6\u503c\uff08\u5373\u5f53\u524d\u5bf9\u8c61\u672c\u8eab\uff09\uff0c\u901a\u8fc7\u8ba9\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u884c\u4e3a\u4e00\u81f4\uff0c\u4ee3\u7801\u7684\u884c\u4e3a\u548c\u9884\u671f\u4e00\u81f4\u3002
\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u6b63\u786e\u5730\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1a
#include <iostream>\n#include <cstring>\n\nclass STRING {\n char *m_pStr;\n int m_len;\npublic:\n // \u6784\u9020\u51fd\u6570\n STRING(char *str = NULL) {\n if (str != NULL) {\n m_len = strlen(str) + 1;\n m_pStr = new char[m_len];\n strcpy(m_pStr, str);\n } else {\n m_len = 0;\n m_pStr = NULL;\n }\n }\n\n // \u62f7\u8d1d\u6784\u9020\u51fd\u6570\n STRING(const STRING& other) {\n m_len = other.m_len;\n if (m_len > 0) {\n m_pStr = new char[m_len];\n strcpy(m_pStr, other.m_pStr);\n } else {\n m_pStr = NULL;\n }\n }\n\n // \u6790\u6784\u51fd\u6570\n ~STRING() {\n delete[] m_pStr;\n }\n\n // \u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d\n STRING& operator=(const STRING& other) {\n if (this != &other) { // \u9632\u6b62\u81ea\u6211\u8d4b\u503c\n delete[] m_pStr;\n m_len = other.m_len;\n if (m_len > 0) {\n m_pStr = new char[m_len];\n strcpy(m_pStr, other.m_pStr);\n } else {\n m_pStr = NULL;\n }\n }\n return *this; // \u8fd4\u56de\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\n }\n\n // \u53cb\u5143\u51fd\u6570\uff0c\u7528\u4e8e\u8f93\u51fa\u5b57\u7b26\u4e32\n friend std::ostream& operator<<(std::ostream& out, const STRING& s);\n};\n\nstd::ostream& operator<<(std::ostream& out, const STRING& s) {\n if (s.m_pStr != NULL) {\n out << s.m_pStr;\n }\n return out;\n}\n\nint main() {\n STRING s1((char *)\"Hello\"), s2, s3;\n s3 = s2 = s1; // \u94fe\u5f0f\u8d4b\u503c\n std::cout << \"s1: \" << s1 << std::endl;\n std::cout << \"s2: \" << s2 << std::endl;\n std::cout << \"s3: \" << s3 << std::endl;\n return 0;\n}\n
"},{"location":"oop/copy/lec/#copying-vs-initialization","title":"Copying vs Initialization","text":""},{"location":"oop/copy/lec/#assignment-operator","title":"Assignment Operator","text":"\u5148\u91ca\u653e\u4e86\u539f\u6709\u7684\u5185\u5b58\uff0c\u7136\u540e\u53c8\u8bd5\u56fe\u4f7f\u7528\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u8fdb\u884c\u8d4b\u503c\uff0c\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f
\u5728\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u9632\u6b62\u81ea\u8d4b\u503c\u7684\u60c5\u51b5\u53d1\u751f!! \u5982\u679c\u4e0d\u52a0\u4ee5\u5904\u7406\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u6216\u5185\u5b58\u6cc4\u6f0f\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u4f1a\u5728\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u5b9e\u73b0\u4e2d\u6dfb\u52a0\u81ea\u8d4b\u503c\u68c0\u67e5\uff0c\u786e\u4fdd\u5728\u81ea\u8d4b\u503c\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u5bf9\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\u3002
Be sure to assign to all data members
*this
// \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\n MyClass& operator=(const MyClass& other) {\n if (this != &other) { // \u68c0\u67e5\u662f\u5426\u81ea\u8d4b\u503c\n delete data; // \u91ca\u653e\u539f\u6709\u5185\u5b58\n data = new int(*other.data); // \u6df1\u62f7\u8d1d\u6570\u636e\n }\n return *this; // \u8fd4\u56de\u5f15\u7528\n }\n
class PathName {\n string name;\npublic:\n // or could be multi-argument with defaults\n // Or Type Conversion\n PathName(const string&);\n ~ PathName();\n};\n...\nstring abc(\"abc\");\nPathName xyz(abc); // OK!\nxyz = abc; // OK abc => PathName Type Conversion\n
\u5728\u6784\u9020\u51fd\u6570\u524d\u9762\u52a0\u4e0a explicit
\u5173\u952e\u5b57:
explicit PathName(const string&);\n
\u8fd9\u65f6\u6211\u4eec\u7684\u6784\u9020\u51fd\u6570\u53ea\u80fd\u7528\u6765\u505a\u6784\u9020\uff01\uff01\uff01
Compiler will use it as a type conversion from
class Rational {\npublic:\n ...\n operator double() const; // Rational to double\n}\nRational::operator double() const { \n return numerator_/(double)denominator_;\n}\nRational r(1,3); \ndouble d = r; // r=>double\n
\u4e0d\u9700\u8981\u5199\u8fd4\u56de\u7c7b\u578b\u3002 \u5982\u679c\u6211\u4eec\u5728\u91cd\u8f7d\u7684\u8fd0\u7b97\u7b26\u524d\u9762\u52a0\u4e0a explicit, \u90a3\u4e48\u6211\u4eec\u5c31\u5fc5\u987b\u5199\u4f5c double d = (double)r;
operator C()
\u7684\u91cd\u8f7d\u3002\u5982\u679c\u4e24\u4e2a\u90fd\u6709\uff0c\u7f16\u8bd1\u5668\u4f1a\u51fa\u9519\u3002&&
,||
,,
\u6700\u597d\u4e0d\u8981\u91cd\u8f7dInheritance is the ability to define the behavior or implementation of one class as a superset of another class.
Take a closer look at an example.
class Database {\n vector<CD> cds;\n vector<DVD> dvds;\n public:\n void addCD(CD &aCD);\n void addDVD(DVD &aDVD);\n void list() {\n for (auto x:cds) { cd.print(); }\n for (auto x:dvds) { x.print(); }\n }\n}\n
Solution
"},{"location":"oop/inheritance/lec/#basic-declaration","title":"Basic Declaration","text":"use std : avoid include a lot of header files.
#include<string>\nusing namespace std;\n\n//or\n\nstd::string\n
::
operator.class Employee{\n public:\n Employee(const std::string& name,const std::string& ssn);\n const std :: string & get_name() const;\n const string& getName()const{return name;}\n const string& getSSN()const{return ssn;}\n void print()const;\n //void print(std::ostream& out)const;\n void print(const std::string & msg)const;\n protected: // Self and descendant can access\n std::string m_name;\n std::string m_mssn;\n};\nEmployee::Employee( const string& name, const string& ssn ):m_name(name),m_ssn( ssn){// initializer list sets up the values!}\n\ninline const std::string& Employee::get_name()const{\n return m_name;\n}\ninline void Employee::print(std::ostream& out)const{\n out << m_name<<endl;\n out << m_ssn<<endl;\n}\ninline void Employee::print(std::ostream& out,const std::string& msg)const{\n out << msg << endl;\n print(out);\n}\nclass Manager : public Employee {\n public:\n Manager(const std::string& _name, const std::string& _ssn, const std::string& _title);\n const std::string& get_title() const;\n void print() const;\n private:\n std::string m_title;\n};\nManager::Manager(const string& _name, const string& _ssn, const string& _title = \"\" ):Employee(_name, _ssn), m_title( _title ) {}\n
Better to initialize the parent class by calling its constructor instead of
Manager(const std::string& name, const std::string& ssn, const std::string&title):name(_name),ssn(_ssn)mtitle(_title){}\n
void Manager::print()const{\n Employee::print();\n cout << title << endl;\n}\n
#include \"employee.h\"\n#include \"manager.h\"\nint main()\n{\n Employee p1(\"John Smith\",\"123123\");\n Manager p2(\"Tom Jordan\", \"3267\",\"Mr.\");\n p1.print(); \n p2.print();\n p1.print(\"Welcome:\");\n //p2.print(\"Welcome:\"); \u62a5\u9519\n\n\n Employee *p = &p1;\n p->print();\n p = &p2; \n p->print(); // \u6309 Employee \u91cc\u9762\u7684\u51fd\u6570\u8f93\u51fa\uff0c\u800c\u4e0d\u662f Manager\n}\n
cout<<name<<endl //supose name(private) is from parent class, ERROR\n
protected
: only descandents can access.\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u4e86\u7236\u7c7b\u7684\u67d0\u4e2a\u51fd\u6570\uff0c\u5c31\u4f1a\u628a\u5176\u4ed6 overload \u7684\u51fd\u6570\u8986\u76d6\u6389\uff08C++ \u72ec\u6709\uff0c\u65e0\u6cd5\u89e3\u91ca\uff09\uff0c\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d, p2.print(\"welcome\")
\u4f1a\u62a5\u9519
\u8981\u8c03\u7528\u7236\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u8981 Employee::print()
.
Failed to inherit:
class Derived : public Base ...\nclass Derived : protected Base ...\nclass Derived : private Base ..\n
"},{"location":"oop/inheritance/lec/#polymorphism","title":"Polymorphism","text":"class A public B{\n\n};\nclass A protected B{\n\n};\nclass A private B{\n\n};\n
public
: A is a Bprotected
: A is a B and A's member is protected(for A's descandent)private
: A is a B and A's member is private(for A's descandent)Public Inheritance should imply substitution
If B is a A, you can use a B any where an A can be used. if B is a A, then everything that is true for A is also true of B.
Be careful if the substitution is not valid!
#include <iostream>\nusing namespace std;\nclass A{\n public:\n int i;\n void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;}\n};\nclass B:public A{\n public:\n int i;\n void f(){cout<<\"B::f()\"<<endl;}\n B(){i=20;cout<<\"B()\"<<i<<endl;}\n};\nint main(){\n B b;\n cout << \"finished creating b\"<<endl;\n cout << endl;\n\n A *p=&b;\n b.f();\n cout << endl;\n cout << \"which f() is called by A* p=&b; ?\"<<endl;\n p->f(); // A::f()\n cout << endl;\n cout<< sizeof(*p)<<endl;\n int *pi = (int*)p;\n cout << endl;\n\n cout<< pi[0] <<\",\"<<pi[1]<<endl;\n cout << p->i <<endl;\n cout << b.i<<endl;\n}\n
A()10\nB()20\nfinished creating b\n\nB::f()\n\nwhich f() is called by A* p=&b; ?\nA::f()\n\n4\n\n10,20\n10\n20\n
f()
function using pointer to ites parent class, it will call the parent class's function.<without virtual>
#include <iostream>\nusing namespace std;\nclass A{\n public:\n int i;\n virtual void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;}\n};\nclass B:public A{\n public:\n int i;\n void f(){cout<<\"B::f()\"<<endl;}\n B(){i=20;cout<<\"B()\"<<i<<endl;}\n};\nint main(){ \n B b;\n A *p=&b;\n B *q=&b;\n cout << sizeof(b)<<endl; \n cout << sizeof(A)<<endl; \n\n b.f();\n cout << \"When virtual is added it will call the derived class function f() else it will call the base class function f()\"<<endl;\n p->f();\n cout<< sizeof(*p)<<endl;\n cout<< sizeof(*q)<<endl;\n int *pi = (int*)p;\n int *qi = (int*)q;\n cout<< pi[0] <<\",\"<<pi[1]<<','<<pi[2]<<','<<pi[3]<<endl;\n cout<< qi[0] <<\",\"<<qi[1]<<','<<qi[2]<<','<<qi[3]<<endl;\n\n cout << \"p->i is still A's i\"<<endl;\n cout << p->i <<endl;\n cout << b.i<<endl;\n}\n
virtual
keyword, it will call the child class's function.A()10\nB()20\n16\n16\nB::f()\nWhen virtual is added it will call the derived class function f() else it will call the base class function f()\nB::f()\n16\n16\n12550336,1,10,20\n12550336,1,10,20\np->i is still A's i\n10\n20\n
"},{"location":"oop/inheritance/lec/#example-drawing-program","title":"Example : Drawing Program","text":"class XYPos{}\nclass Shape {\npublic: \n Shape();\n virtual ~Shape();\n virtual void render();\n void move (const XYPos&);\n virtual void resize();\nprotected:\n XYPos center;\n}\n
virtual
is used to define a function that can be overwritten in a derived class.class Ellipse : public Shape {\npublic:\n Ellipse(float maj, float minr);\n virtual void render(); // will define own\nprotected:\n float major_axis, minor_axis;\n};\nclass Circle : public Ellipse {\npublic:\n Circle(float radius) : Ellipse(radius, radius){}\n virtual void render();\n};\n
.render()
: static binding->render()
: dynamic binding&\u5f15\u7528\u8bbf\u95ee
: dynamic bindingvoid render(Shape* p) {\n p->render(); // calls correct render function\n} // for given Shape! void func() {\nEllipse ell(10, 20);\nell.render(); // static -- Ellipse::render();\nCircle circ(40);\ncirc.render(); // static -- Circle::render();\nrender(&ell); // dynamic -- Ellipse::render();\nrender(&circ); // dynamic -- Circle::render()\n
void render(Shape* p) {\n p->render(); // calls correct render function\n} \n
Shape *p
: polymofphic varaibleV table The V table is a table of the addresses of the virtual functions for that class.
Note : Pointer size is 8 bytes in 64-bit system. And We have to consider alignment.
Vtable is created at compile time. SO it is static.
int main(){ \n B b;\n A *p=&b;\n cout << sizeof(b)<<endl; \n cout << sizeof(A)<<endl; \n b.f();\n long long **vp = (long long**)p;\n void (*pf) () = (void (*)())(*(*vp));\n cout << \"-------------\"<<endl;\n pf();\n}\n
A()10\nB()20\n16\n16\nB::f()\n-------------\nB::f()\n
class A{\n public:\n int i;\n virtual void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;}\n void g(){f();}//this->f()\n};\n
p->g();\n
B::f()\n
Add
class A{\n public:\n int i;\n virtual void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;f();}\n void g(){f();}//this->f()\n};\n
While in constructor, it will call the parent class's function. A::f()
How dose vptr
come?
When the object is created, space will be allocated and the constructor will be called. When the constructor is called, the vptr will be set to the vtable
of the class!!!
Think about the order of the constructor:
When a descendent class is created, the parent class's constructor will be called first. So the vptr will be set to the parent class's vtable first. Then the descendent class's constructor will be called and the vptr will be set to the descendent class's vtable.
"},{"location":"oop/inheritance/lec/#what-happens-if","title":"What happens if ?","text":"Ellipse ell(10, 20);\nCircle circ(40);\nelly = circ; \n
elly
's vptr.a = b;\np = &a;\np -> f();\n
A::f()
p->f()
will call the parent class's function.What about?
Ellipse *elly = new Ellipse(20F,40F);\nCircle *circ = new Circle(60F);\nelly = circ;\n
elly
is lost.elly ->render();//Circle::render()
What about ?
void func(Ellipse &elly){\n elly.render()\n}\nCircle circ(60F);\nfunc(circ);\n
#include <iostream>\nusing namespace std;\nclass A{\n public:\n int i;\n virtual void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;f();}\n void g(){f();}//this->f()\n};\nclass B:public A{\n public:\n int i;\n void f(){cout<<\"B::f()\"<<endl;}\n B(){i=20;cout<<\"B()\"<<i<<endl;}\n};\nint main(){ \n cout << \"Creating B object\"<<endl;\n B b;\n cout << \"Creating A object\"<<endl;\n A a;\n\n A *p=&b;\n cout << \"----------\"<<endl;\n\n p->f();\n cout << sizeof(b)<<endl; \n cout << sizeof(A)<<endl; \n\n int *pi = (int*)p;\n cout<< long(pi[0]) <<\",\"<<pi[2]<<\",\"<<pi[3]<<endl;\n\n long long **vp = (long long**)(p);\n void (*pf) () = (void (*)())(*(*vp));\n pf();\n p->g();\n\n cout << \"-------------\"<<endl;\n a = b;\n p = &a;\n cout << \"This time p is pointing to A object and a=b is sliced from b to a(meaning that the B part of b is copied to a and original A is preserved)\"<<endl;\n\n pi = (int*)p;\n cout<< long(pi[0]) <<\",\"<<pi[2]<<endl;\n\n long long **vp1 = (long long**)(p);\n void (*pf1) () = (void (*)())(*(*vp1));\n pf1();\n\n cout<<\"-----------\"<<endl;\n b.f();\n a.f();\n p -> f();\n cout <<\"----------\"<<endl;\n\n A *x1 = new A();\n B *x2 = new B();\n\n x1 = x2; \n cout << \"x1 = x2 and x1 is pointing to B object now\"<<endl;\n cout << \"-----------\"<<endl;\n x1->f(); \n}\n
"},{"location":"oop/inheritance/lec/#virtual-destructors","title":"Virtual Destructors","text":"Shape *p = new Ellipse(100.0F, 200.0F); ...\ndelete p;\n
p \u7684\u9759\u6001\u7c7b\u578b\u662f Shape, \u5982\u679c\u4e0d\u5b9a\u4e49\u865a\u51fd\u6570\uff0c\u90a3\u4e48 p \u53ea\u4f1a\u53d1\u751f\u9759\u6001\u7ed1\u5b9a\uff0c\u5373\u8c03\u7528 Shape\u7684\u6790\u6784\u51fd\u6570\uff0c\u65e0\u6cd5\u8c03\u7528Ellipse\u7684\u6790\u6784\u51fd\u6570! virtual
\u5173\u952e\u5b57\uff0c\u9ed8\u8ba4\u662f virtual \u7684\uff0c\u4f46\u662f\u63a8\u8350\u52a0\uff0c\u4e3a\u4e86\u5b59\u7c7b\u3002class Expr {\npublic: \n virtual Expr* newExpr();\n virtual Expr& clone();\n virtual Expr self();\n}\nclass BinaryExpr : public Expr {\npublic:\n virtual BinaryExpr* newExpr(); // ok\n virtual BinaryExpr& clone(); // ok\n virtual BinaryExpr self(); // Error!\n}\n
\u6307\u5411\u5b50\u7c7b\u7684\u5bf9\u8c61\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u4e00\u4e2a\u6307\u5411\u7236\u7c7b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\u5b50\u7c7b\u7684\u5bf9\u8c61\u548c\u7236\u7c7b\u7684\u5bf9\u8c61\u662f\u4e0d\u540c\u7684
void Derived::func() {\n cout << \"In Derived::func!\";\n Base::func(); // call to base class\n}\n
class Base {\npublic:\n virtual void func();\n virtual void func(int);\n};\n
\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u4f60\u5728\u6d3e\u751f\u7c7b\u4e2d\u53ea\u91cd\u5199\u4e86 func()
\uff0c\u800c\u6ca1\u6709\u91cd\u5199 func(int)
\uff0c\u90a3\u4e48 func(int)
\u5c31\u4f1a\u5728\u6d3e\u751f\u7c7b\u4e2d\u4e0d\u53ef\u89c1\uff0c\u56e0\u4e3a\u57fa\u7c7b\u4e2d\u7684 func(int)
\u88ab\u9690\u85cf\u4e86\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4f60\u5728\u6d3e\u751f\u7c7b\u4e2d\u65e0\u6cd5\u8c03\u7528 func(int)
\uff0c\u9664\u975e\u4f60\u4f7f\u7528\u4e86\u663e\u5f0f\u7684\u4f5c\u7528\u57df\u89e3\u6790\u7b26\u6765\u6307\u5b9a\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u3002
#include <iostream>\nusing namespace std;\nclass A {\n public:\n A() { f(); }\n virtual void f() { cout << \"A::f()\"; }\n};\nclass B : public A {\n public:\n B() { f(); }\n void f() { cout << \"B::f()\"; }\n};\nint main() {\n B b;\n}\n//A::f()B::f()\n
virtual void render() = 0;\n
class XYPos{ ... }; // x,y point\nclass Shape {\npublic:\n Shape();\n virtual void render() = 0; \n void move(const XYPos&);\n virtual void resize();\nprotected:\n XYPos center;\n};\n
"},{"location":"oop/inheritance/lec/#multiple-inheritance","title":"Multiple Inheritance","text":"class B1 { int m_i; };\nclass D1 : public B1 {};\nclass D2 : public B1 {};\nclass M : public D1, public D2 {};\nvoid main() {\n M m; //OK\n B1* p = new M; // ERROR: which B1\n B1* p2 = dynamic_cast<D1*>(new M); // OK\n}\n
Say No to Multiple Inheritance
\u51fd\u6570\u6a21\u677f\uff1a\u7528\u4e8e\u5236\u9020\u51fd\u6570\u7684\u6a21\u677f!\u4e5f\u5c31\u662f\u8bf4\u5b83\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u4e00\u4e2a\u51fd\u6570\u7684\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u53c2\u6570\u7c7b\u578b\u751f\u6210\u5bf9\u5e94\u7684\u51fd\u6570\u3002
\u5982\u679c\u8fd9\u4e2a\u65f6\u5019\u5df2\u7ecf\u6709\u4e86\u67d0\u4e00\u4e2a\u51fd\u6570\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u4f1a\u4f18\u5148\u8c03\u7528\u5df2\u6709\u7684\u51fd\u6570\u3002
void swap( int& x, int& y ) { \n T temp = x; \n x = y; \n y = temp; \n}\n
template < class T > \nvoid swap( T& x, T& y ) { \n T temp = x; \n x = y; \n y = temp; \n}\n
The class T
specifies a parameterized type name
\u53ef\u4ee5\u7406\u89e3\u4e3a\uff0c\u6a21\u677f\u9ad8\u901f\u7f16\u8bd1\u5668\u8fd9\u4e2a\u51fd\u6570\u4f1a\u6709\u5f88\u591a\u91cd\u8f7d\u7248\u672c\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u53c2\u6570\u7c7b\u578b\u751f\u6210\u5bf9\u5e94\u7684\u51fd\u6570\u3002
int main() { \n int a = 1, b = 2; \n swap( a, b ); \n cout << a << \" \" << b << endl; \n return 0; \n}\n
int i = 3; int j = 4; \nswap(i, j); // use explicit int swap \nfloat k = 4.5; float m = 3.7; \nswap(k, m); // instanstiate float swap \nstd::string s(\"Hello\"); \nstd::string t(\"World\"); \nswap(s, t); // std::string swap \n
\u8fd9\u91cc swap(k,m)
\u4f1a\u8c03\u7528\u51fd\u6570\u6a21\u677f\uff0c\u968f\u540e\u751f\u6210 float swap
, \u7f16\u8bd1\u5668\u4f1a\u63d2\u5165\u51fd\u6570\uff0c\u8c03\u7528\u3002
\u5fc5\u987b\u7c7b\u578b\u76f8\u540c\uff01
swap(int, int); // ok \nswap(double, double); // ok \nswap(int, double); // error!\n
Even implicit conversions are ignored!
void swap(&int, &int);\ntemplate <class T> void swap(&T, &T);\nswap(1, 2); //Wrong!:should be left value reference\nint a = 1, b = 2;\nswap(a, b); //OK\n
template <class T>\nvoid f(int i){\n T a;\n a = i;\n}\n//Use in this way:\nf<int>(3);\nf<double>(1.0);\n
template <class T> \nclass Vector { \npublic: \n Vector(int); \n ~Vector(); \n Vector(const Vector&); \n Vector& operator=(const Vector&); \n T& operator[](int); \nprivate: \n T* m_elements; \n int m_size; \n};\n
template<class T>\nT& vector<T>::operator[](int i){ //Note this <T>!!!\n return m_elements[i];\n}\n
Vector<int> v1(100); \nVector<Complex> v2(256); \nv1[20] = 10; \nv2[20] = v1[20]; // ok if int->Complex define\n
A simple sort function
operator<
\u91cd\u8f7d\u7c7b\u6a21\u677f\u7684\u51fd\u6570\u662f\u58f0\u660e\u800c\u4e0d\u662f\u5b9a\u4e49\uff0c\u6ca1\u6709\u5206\u79bb\u7684 .h \u6587\u4ef6\u3002\uff08\u4e0d\u9700\u8981 inline \u5173\u952e\u5b57\uff09
template< class Key, class Value> \nclass HashTable { \nconst Value& lookup(const Key&) const; \nvoid install(const Key&, const Value&); \n... \n}; \n
Templates nest \u2014 they\u2019re just new types!
Vector< Vector< double *> > // note space > >\n
Vector< int (*)(Vector<double>&, int)>\n
\u5728C++\u4e2d\uff0c\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6307\u9488\uff0c\u5b83\u6307\u5411\u51fd\u6570\u800c\u4e0d\u662f\u6307\u5411\u53d8\u91cf\u3002\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u6307\u5411\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002
\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u9700\u8981\u6307\u5b9a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u548c\u53c2\u6570\u7c7b\u578b\uff0c\u5e76\u4f7f\u7528\u6307\u9488\u58f0\u660e\u7b26 *
\u6765\u58f0\u660e\u6307\u9488\u3002\u51fd\u6570\u6307\u9488\u7684\u4e00\u822c\u5f62\u5f0f\u5982\u4e0b\uff1a
return_type (*pointer_name)(arg_type1, arg_type2, ...);\n
\u5176\u4e2d\uff1a - return_type
\u662f\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\uff1b - pointer_name
\u662f\u51fd\u6570\u6307\u9488\u7684\u540d\u79f0\uff1b - arg_type1, arg_type2, ...
\u662f\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u3002
\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u51fd\u6570 int add(int a, int b)
\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6307\u5411\u8be5\u51fd\u6570\u7684\u6307\u9488\u5982\u4e0b\uff1a
int (*ptr)(int, int); // \u5b9a\u4e49\u4e00\u4e2a\u6307\u5411\u8fd4\u56de\u7c7b\u578b\u4e3a int\uff0c\u63a5\u53d7\u4e24\u4e2a int \u7c7b\u578b\u53c2\u6570\u7684\u51fd\u6570\u6307\u9488\n
\u7136\u540e\uff0c\u53ef\u4ee5\u5c06\u51fd\u6570 add
\u7684\u5730\u5740\u8d4b\u7ed9\u6307\u9488 ptr
\uff0c\u4f7f\u5176\u6307\u5411 add
\u51fd\u6570\uff1a
ptr = add;\n
\u73b0\u5728\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8c03\u7528\u51fd\u6570 add
\uff1a
int result = (*ptr)(2, 3); // \u7b49\u4ef7\u4e8e int result = add(2, 3);\n
\u6216\u8005\u53ef\u4ee5\u7b80\u5316\u4e3a\uff1a
int result = ptr(2, 3); // \u4e5f\u7b49\u4ef7\u4e8e int result = add(2, 3);\n
\u8fd9\u6837\uff0c\u51fd\u6570\u6307\u9488\u5c31\u53ef\u4ee5\u7528\u6765\u52a8\u6001\u5730\u6307\u5411\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u5e76\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u3002\u5728\u793a\u4f8b\u4e2d\uff0cVector<int (*)(Vector<double>&, int)>
\u5c31\u662f\u4e00\u4e2a\u5411\u91cf\uff0c\u5176\u5143\u7d20\u662f\u6307\u5411\u63a5\u53d7 Vector<double>
\u548c int
\u53c2\u6570\u7684\u51fd\u6570\u7684\u6307\u9488\uff0c\u5e76\u8fd4\u56de int
\u7c7b\u578b\u503c\u3002
template <class T, int bound=100>\nclass FixedVector {\npublic:\n FixedVector();\n FixedVector(int);\n FixedVector(const FixedVector&);\n FixedVector& operator=(const FixedVector&);\n T& operator[](int);\nprivate:\n T m_elements[bound];\n int m_size;\n};\n
FixedVector<int, 200> v1;\nFixedVector<int> v2; // bound=100\n
template <class T>\nclass Derived : public Base<T> {\npublic:\n Derived();\n Derived(const Derived&);\n Derived& operator=(const Derived&);\n T& operator[](int);\n};\n
template <class T>\nclass Derived : public Base<T> {\npublic:\n Derived();\n Derived(const Derived&);\n Derived& operator=(const Derived&);\n T& operator[](int);\n};\n
template <class T>\nclass Base {\npublic:\n Base();\n Base(const Base&);\n Base& operator=(const Base&);\n T& operator[](int);\n};\nclass Derived : public Base<int> {\npublic:\n Derived();\n Derived(const Derived&);\n Derived& operator=(const Derived&);\n int& operator[](int);\n};\n
weak
\u5173\u952e\u5b57\u89e3\u51b3?"},{"location":"optimization/lec1/lec/#lecture-1-introduction-to-optimization","title":"Lecture 1: Introduction to Optimization","text":""},{"location":"optimization/lec1/lec/#relaxation-and-optimization","title":"Relaxation and Optimization","text":""},{"location":"optimization/lec1/lec/#classes-of-differentiable-functions","title":"Classes of differentiable functions","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/","title":"Chapter 1 Digital Systems and Information","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#breif-intro","title":"Breif Intro","text":"
The most commonly used two-valued information is an electrical signal - voltage or current typically two discrete values represented by the voltage range of values.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#digital-systems","title":"Digital Systems","text":"Takes a set of discrete information inputs and discrete internal information (system state) and generates a set of discrete information outputs.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#types-of-digital-systems","title":"Types of Digital Systems","text":"Combinational Logic System:No state present , one input just decide one output(one to one , no state)
Sequential System
State present
State updated at discrete times -- Synchronous Sequential System [Periodic]
State updated att any time -- Asychronous Sequential System
State function : \\(f_{state}=Function(State,Input)\\)
Output function : \\(f_{output}=Function(State)\\) or \\(f_{output}=Function(State,Input)\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#organization-of-computer","title":"Organization of Computer","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#embedded-systems","title":"Embedded Systems","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#number-systems-codes","title":"Number Systems & Codes","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#covert","title":"Covert","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#decimal-to-binary","title":"Decimal To Binary","text":"\u6574\u6570\u2014\u2014\u6743\u5c55\u5f00\u5f0f\u96642\uff0c\u4f59\u6570\u6784\u6210\u6700\u4f4e\u4f4d\u2014\u2014\u96642\u53d6\u4f59
\u5c0f\u6570\u2014\u2014\u6743\u5c55\u5f00\u5f0f\u4e582\uff0c\u6574\u6570\u6784\u6210\u6700\u9ad8\u4f4d\u2014\u2014\u4e582\u53d6\u6574
Given n binary digits (called bits), a binary code is a mapping from a set of represented elements to a subset of the \\(2^n\\) binary numbers.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#number-of-elements-represented","title":"Number of Elements Represented","text":"You can represent 4 elements in radix r = 2 with n = 2 digits: (00, 01, 10, 11).
You can represent 4 elements in radix \\(r=2\\) with \\(n=4\\) digits (0001,0010,0100,1000) \\(One\\ Hot\\ Code\\)
Other Examples
Add Six
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#ascii","title":"ASCII","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#parity-bit-error-detection-codes","title":"Parity Bit Error-Detection Codes","text":"Redundancy (e.g. extra information), in the form of extra bits, can be incorporated into binary code words to detect and correct errors.
A simple form of redundancy is parity, an extra bit appended onto the code word to make the number of 1\u2019s odd or even.
Parity can detect all single-bit errors and some multiple-bit errors.
A code word has even parity if the number of 1\u2019s in the code word is even.
A code word has odd parity if the number of 1\u2019s in the code word is odd.
Suppose we want to know the angle shifted by the machine.
If Use Binary Code-- \u5982\u679c\u4e09\u4e2a\u4f20\u611f\u5668\u4f4d\u7f6e\u4e0d\u7edf\u4e00\uff0c\u5728\u4ece000-111\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\uff1a000-100-110-111 -- \u4e2d\u95f4\u8fc7\u7a0b\u7578\u53d8
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/","title":"Chapter 2 Combinational Logic","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#binary-logic-and-gates","title":"Binary Logic and Gates","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#gate-delay","title":"Gate Delay","text":"On the other hand, when an nMOS transistor receives a voltage at around 0 volts, the connection from the source to the drain will be broken, which is referred to as an open circuit.
Whereas the nMOS will form a closed circuit with the source when the voltage is non-negligible, the pMOS will form an open circuit with the source when the voltage is non-negligible.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#tristate-gate","title":"Tristate gate","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#common-logic-gate-circuit","title":"Common logic gate circuit","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#nand-operation","title":"NAND operation","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#nor-operation","title":"NOR operation","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#and-or-invert-operation","title":"AND-OR-INVERT operation","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#exclusive-or","title":"Exclusive OR","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#exclusive-nor","title":"Exclusive NOR","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#basic-concepts-of-boolean-algebra","title":"Basic concepts of Boolean algebra","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#the-xor-identities","title":"The XOR identities","text":"Note the following two points:
The holding operation priority unchanged, if necessary, add brackets indicate.
Within converting, public non-operation remains unchanged for several variables
If F\u2018 is the F Duality, then F is also F\u2019 of Duality. F and F\u2018 is mutually Duality formula .
If the two logical functions F and G are equal, then the Duality formula F' and G' are also equal.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#substitution-rules","title":"Substitution rules","text":"Any logical equation that contains a variable A, and if all occurrences of A's position** are replaced with a logical function F, the equation still holds.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#shannon-formula","title":"Shannon formula","text":"\u2013 Assuming: Function F contained variables \\(x\\)\u3001\\(\\bar{x}\\), at \"x AND F\" operation, variable \\(x\\) may be replaced by 1 , variable \\(\\bar{x}\\) can be replaced by 0.
\u2013 At \"x AND F\" operation, \\(x\\) can be \"0\", \\(\\bar{x}\\) can be replaced with \"1\" .
\\(xf(x,\\overline{x},y...)=xf(1,0,y\u2026)\\)
\\(\\overline{x}f(x,\\overline{x},y...)=\\overline{x} f(0,1,y...)\\)
Similarly
\\(x+f(x,\\bar{x},y\u2026\u2026z)=x+f(0,1,y\u2026\u2026z)\\)
\\(\\bar{x}+f(x,\\bar{x},y\u2026\u2026z)=\\bar{x}+f(1,0,y\u2026\u2026,z)\\)
If the function F contains the both of the variables \\(x,\\bar{x}\\) may be follow:
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#simplification","title":"Simplification","text":"Minterms are AND terms with every variable present in either true or complemented form.
Given that each binary variable may appear normal (e.g., x) or complemented (e.g., x ), there are \\(2^n\\) minterms for n variables. denoted as \\(m_i\\)
Maxterms are OR terms with every variable in true or complemented form.
Given that each binary variable may appear normal (e.g., x) or complemented (e.g., x), there are \\(2^n\\) maxterms for n variables. denoted as \\(M_i\\)
For a logical function SOP or POS is the optimized\uff08\u4e24\u7ea7\u95e8\uff0c\u95e8\u7ea7\u6570\u6700\u5c0f\uff09
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#karnaugh-map-of-functio","title":"Karnaugh map of Functio","text":"\u7565.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#multi-level-circuit-optimization","title":"Multi-level circuit optimization","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#cost","title":"COST","text":"Literal Cost : Just Literal Appearances
Gate Input Cost \\(G\\) : Add AND OR term but exclude one-literal term
AB+CD+E -- 7
BD+ABC+ACD --11
A gate that can be described using a single primitive operation type (AND or OR) plus an optional inversions.
The same as a connection -- improve circuit voltage levels and increase the speed of circuit operation.(decrease propagation delay)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#complex-gates","title":"Complex Gates","text":"Requires more than one primitive operation type for its description.
XOR -- Odd Functions
Use inversions -- Even Functions
P -- Generate the Parity bit
E--Decide whether the translated inf is Error
Three-state logic adds a third logic value, Hi-Impedance (\\(Hi-Z\\)), giving three states: \\(0, 1, and\\ Hi-Z\\) on the outputs.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#the-3-state-buffer","title":"The 3-State Buffer","text":"Connection of two 3-state buffer outputs, B1 and B0, to a wire, OUT:
We can neglect the inversion of input iterals ; but the intermediate varaible\u2019s inversions cannot be neglected
For the first -- 3\u7ea7 but high cost
For the latter-- 4\u7ea7 but low cost
Basic Form : \\((2^3)\\) 8-3-input AND gates.
Split to 2-to-4-line decoder and 1-to-2-line decoder. [Simplify]
Another Example
Basic \\(128*7\\)
3-to-8 & 4-to-16. -- \\(128*2 + 8*3+16*4\\)
Theoratically realize all logic functions \\((SOM)\\)
To decrease count of the pins
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#encoding","title":"Encoding","text":"If two signals are high simultaneously ? -- Meaningless
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#priority-encoder","title":"Priority Encoder","text":"If more than one input value is \\(1\\), then the encoder just designed does not work.
One encoder that can accept all possible combinations of input values and produce a meaningful result is a priority encoder
To process \u4e2d\u65ad\u4e8b\u4ef6
Could use a K-map to get equations
Also can be read directly from table and manually optimized if careful
\\(A_2=D_4\\)
\\(A_1=\\bar{D_4}D_3+\\bar{D}_4\\bar{D}_3D_2=\\bar{D_4}F_1. F_1=(D_3+D_2)\\)
\\(A_0=\\bar{D}_4D_3+\\bar{D_4}\\bar{D_3}\\bar{D_2}D_1=\\bar{D_4(D_3+\\bar{D_2}D_1)}\\)
\\(V=D_4+F_1+D_1+D_0\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#multiplexers","title":"Multiplexers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#multiplexer-width-expansion","title":"Multiplexer Width Expansion","text":"GN(22)
Three State can connect all output to one line. --GN=18 (a)
GN=14 (b)
Disadvantages : Takes much time .
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#example","title":"Example","text":"Refer to \\(PPT\\)
Note that the multiplexer with fixed inputs is identical to a ROM with 3-bit addresses and 2-bit data!
The most COMMON:
\\(X\\oplus Y\\) only different from \\(X+Y\\) when \\(XY=1\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#binary-adders","title":"Binary Adders","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#carry-lookhead","title":"Carry Lookhead","text":"\\(P_i=A_i\\oplus B_i\\ \\ \\ G_i=A_iB_i\\\\S_i=P_i\\oplus C_i\\ \\ \\ C_{i+1}=G_i+P_iC_i\\)
Thus,we have
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#group-carry-lookahead-logic","title":"Group Carry Lookahead Logic","text":"Disadvantages :
So Simple copy the CLA to more bits is not practical.
Overflow V = \\(C_n\\oplus C_{n-1}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#incrementing-decrementing","title":"Incrementing & Decrementing","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#incrementing","title":"Incrementing","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#multiplicationdivision-by-2n","title":"Multiplication/Division by \\(2^n\\)","text":"Copies the MSB of the operand into the new positions
Positive operand example - 01110101 extended to 16 bits:0000000001110101
Negative operand example - 11110101 extended to 16 bits:1111111111110101
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#arithmetic-logic-unitalu-implementation","title":"Arithmetic Logic Unit(ALU) implementation","text":"\\(Y_i=B_iS_0+\\bar{B_i}S_1\\)
The Above Part -- (Basic ALU like above)
The below Part -- AND OR XOR NOT (one bit)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#combinational-shifter-parameters","title":"Combinational Shifter Parameters","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/","title":"Chapter 4 Sequential Logic","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#storage-elements-and-sequential-circuit-analysis","title":"Storage Elements and Sequential Circuit Analysis","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#types-of-sequential-circuits","title":"Types of Sequential Circuits","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#synchronous","title":"Synchronous","text":"Behavior defined from knowledge of inputs an any instant of time and the order in continuous time in which inputs change
If clock just regarded as another input, all circuits are asynchronous!
Nevertheless, the synchronous abstraction makes complex designs tractable!
While \\(C=1\\) \\(S\\) \\(and/or\\) \\(R\\) are permited to change.
The master latch sets to \\(1\\)
A \\(1\\) is transferred to the slave
The master latch sets and then resets
A \\(0\\) is transferred to the slave
Master-slave - Equal to the width of the triggering pulse (Because of the 1's Catching the triggering pulse once arrives is forbidden to change)
Edge-triggered - Equal to a time interval that is generally much less than the width of the the triggering pulse
\\(t_h\\) Hold Time -- often equal to zero
\\(t_w\\) Clock Pulse Width
\\(t_{px}\\)
Same parameters as for gates except that measured from clock edge that triggers the output change to the output change.
More Specifically
pos
or neg
)\uff1bOutputs are a function ONLY of states
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#mealy-models","title":"Mealy Models","text":"Outputs are a function of inputs AND states
Details"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#equivalent-state","title":"Equivalent State","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#circuit-and-system-level-timing","title":"Circuit and System Level Timing","text":"periodic clock
flip-flop input
setup time on a path : Must be greater than or equal to zero on all paths for correct operation
\\(t_p=t_{slack}+(t_{pd,FF}+t_{pd,COMB}+t_s)\\)
\\(t_{stack}\\) must be greater than or equal to zero so \\(t_{p} \\ge max(t_{pd,FF}+t_{pd,COMB}+t_s)\\)
from all paths from flip-flop output to flip-flop input
Can be calculated more precisely by using \\(t_{PHL}\\) and \\(t_{PLH}\\) values instead of \\(t_{pd}\\) values, but requires consideration of inversions on paths
Calculation[Refer to PPT]
The task of state assignment involves determining the encoding length and finding an optimal (often challenging) or near-optimal state assignment scheme, leading to different expressions for output functions and activation functions, and consequently varying circuit complexities. The basic principles of state assignment are:
The fundamental principles of assignment are as follows:
Generally, the first principle is of greater importance and should be prioritized. Subsequently, consideration should be given to the frequency of occurrence of state pairs resulting from the first three principles, with a preference for assigning adjacent binary codes to state pairs with higher frequencies.
EXAMPLE -- Refer to the PPT
Design a sequential modulo 3 accumulator for 2-bit operands
Same as S-R flip-flop with J analogous to S and K analogous to R
Has a single input T
Same as a J-K flip-flop with J = K = T
Cannot be initialized to a known state using the T input
To avoid 1s catching behavior,one solution used is to use an edge-triggered D as the core of the flip-flop
Eg.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#flip-flop-behavior-example","title":"Flip-Flop Behavior Example","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/DHI/Digital%20Hardware%20Implementation/","title":"Chapter 5 Digital Hardware Implementation","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/DHI/Digital%20Hardware%20Implementation/#programmable-logic","title":"Programmable Logic","text":"SIngle-bit-storge element
\u200b Reasable
\u200b Eletrically erasable
\u200b Flash
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/DHI/Digital%20Hardware%20Implementation/#read-only-memoryrom","title":"Read Only Memory(ROM)","text":"\u53ef\u5c06\u9996\u6b21\u4e0e\u6216\u5f53\u4f5c\u8f93\u5165\u56de\u5230AND gates input \u53ef\u5b9e\u73b0\u591a\u53d8\u91cf\u4e0e\u6216.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/DHI/Digital%20Hardware%20Implementation/#programmable-logic-array-pla","title":"Programmable Logic Array (PLA)","text":"\\(And\\) Gates limited
\u9002\u5f53\u53d6\u53cd\uff0c\u5c06\u9700\u89816\u4e2a\u4e0e\u9879\u2192 \u9700\u89814\u4e2a\u4e0e\u9879
65ns -- 'read speed'--is critical for the whole speed.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#ram","title":"RAM","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#types-of-random-access-memory","title":"Types of Random Access Memory","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#static-ram-cell","title":"Static RAM -- Cell","text":"Information stored in Latches
Select = 0 HOLD
Select = 1 OUTPUT DATA. \u300cINPUT depends on \\(B\\) and \\(\\bar{B}\\)\u300d
Bit select = 0 : NO WRITING or READING -- Bit Slice Disabled
Word select : only one allowed to be ONE
Bit select = 1:
Chip Select : When CS=0 Data Output \u9ad8\u963b\u6001
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#cell-arrays-and-coincident-selection","title":"Cell Arrays and Coincident Selection","text":"16x1
8x2
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#making-larger-memories-word-extension","title":"Making Larger Memories: Word extension","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#making-wider-memories-bit-extension","title":"Making Wider Memories: Bit extension","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#dynamic-ram","title":"Dynamic RAM","text":"information stored as electrical charges
Transfers to and from the DRAM are synchronize with a clock
Synchronous registers appear on:
Column address counter
Example: Memory data path width: 1 word = 4 bytes
SDRAM burst time-- burst size=4
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#double-data-rate-sdram-ddr-sdram","title":"Double Data Rate SDRAM (DDR SDRAM)","text":"Transfers data on both edges of the clock
Provides a transfer rate of 2 data words perclock cycle
Example: Same as for synchronous DRAM
Read cycle time = 60 ns
Memory Bandwidth: (2 x 32)/(60 x 10-9) = 1.066 Mbytes/sec
SPEED UP!
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#rambus-dram-rdram","title":"RAMBUS\u00ae DRAM (RDRAM)","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#dependence-on-power-supply","title":"Dependence on Power Supply","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#volatile","title":"Volatile","text":"Loses stored information when power turned off
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#non-volatile","title":"Non-volatile","text":"Retains information when power turned off
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/","title":"Registers and Register Transfers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#registers","title":"Registers","text":"Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#storage-and-load-enable","title":"Storage and Load Enable","text":"Load = 0 : Store
Load = 1 : Load
The Problem is : Clock Skew \u300cAnd Gate Delay\u300d
\u52d8\u8bef Load = 0 \u65f6 \u8109\u51b2\u6d88\u5931\uff0c\u5bc4\u5b58\u5668\u65e0\u6cd5\u88ab\u8f7d\u5165
Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#register-transfer","title":"Register Transfer","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#microoperations","title":"Microoperations","text":"
Logical Groupings:
\\(if\\ \\ (K_1=1)\\ then\\ \\ (R_2\u2190R_1)\\) \u8868\u793a\u4e3a \\(K_1 : R_2\u2190R_1\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#register-transfer-structures","title":"Register Transfer structures","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#multiplexer-based-transfers","title":"Multiplexer-Based Transfers","text":"note : clocks are omitted for clarity
\\(K_1 : R_0\u2190R_1\\)
\\(K2\\bar{K1}: R_0\u2190R_2\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#register-cell-design","title":"Register Cell Design","text":"
Example 1 Not encoded \u300cone hot code\u300d
Example 2 Encoded. \u300cBinary or Gray\u300d
EG1: Example
Register A (m-bits) Sepecification:
Data input B
Control input \\(C_x \\ C_y\\) with combinations\\((0,0)\\ (0,1)\\ (1,0)\\)
Register Transfers:
\\(C_x : A \\leftarrow A\\ or\\ B\\)
\\(C_y\\) : \\(A \\leftarrow A\\ xor\\ B\\)
Hold State : \\((0,0)\\)
Multiplexer Approach
Thus , we have :
\\(Load = C_x + C_y\\)
Cost : 6+3+2(or)+8(xor) = 19
Sequential Circuit Design Approach
Similar to using a MUX, the MUX in this case is no longer dedicated logic for a specific register but is shared logic used by three registers.
The MUX in the bus selects one input among all registers and outputs it to all registers. The bus controls which (or how many) registers receive this signal by manipulating the Load signal.
Advantages:
Disadvantages:
Gate Cost. (increase linearly)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#three-state-bus","title":"Three-State Bus","text":"The 3-input MUX can be replaced by a 3-state node (bus) and 3-state buffers.
Gate Cost Only 2n (suppose cost for three-state is 2)
Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#shift-registers","title":"Shift Registers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#_1","title":"\u4e32\u884c\u5b9e\u73b0","text":"
\u5047\u8bbe\u73b0\u5728\u6211\u4eec\u6709 4 \u4e2a FF
\u9996\u5c3e\u76f8\u8fde\uff0c\u73b0\u5728\u4e32\u884c\u8f93\u5165 1001
\u8fd9 4bits \u7684\u6570\u636e\uff08\u5f53\u7136\uff0c\u5b8c\u5168\u8f93\u5165\u9700\u8981 4 \u4e2a\u65f6\u949f\u5468\u671f\uff09\u3002
\u5047\u8bbe\u7b2c 1 \u4e2a\u5468\u671f\u7ed3\u675f\u540e\uff0c1001
\u6700\u53f3\u4fa7\u7684 1
\u88ab\u5b58\u5728\u4e86\u7b2c\u4e00\u4e2a FF\uff0c\u5219\uff1a
100
1
? ? ? 1???
2 10
0
1
? ? 01??
3 1
0
0
1
? 001?
4 1
0
0
1
1001
5 ? 1
0
0
?100
6 ? ? 1
0
??10
7 ? ? ? 1
???1
\u8fd9\u91cc\u7684 SO \u6307\u7684\u662f\u4ece\u5f53\u524d\u8f6e\u6b21\u5f00\u59cb\uff0c\u8bfb\u5165 4bits \u6570\u636e\u5f97\u5230\u7684\u4e32\u884c\u8f93\u51fa\u7ed3\u679c\uff0c\u8fd9\u91cc\u7684 4bits \u5f53\u7136\u662f\u5bf9\u5e94\u8f93\u5165\u7684 4bits\u3002
\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728\u7b2c\u56db\u8f6e\u524d\u540e\u7684\u7ed3\u679c\u5206\u522b\u662f\u5de6\u79fb\u548c\u53f3\u79fb\u7684\u7ed3\u679c\uff0c\u5f53\u7136\uff0c\u5982\u679c\u662f\u8981\u8003\u8651\u5177\u6709\u5b9e\u9645\u8ba1\u7b97\u4ef7\u503c\u7684\u4f4d\u79fb\uff0c\u6211\u4eec\u5f53\u7136\u9700\u8981\u586b\u5145 ?
\u7684\u503c\uff0c\u4e0d\u8fc7\u8fd9\u90fd\u662f\u5c0f\u4e8b\u60c5\u3002
Internal details => Incrementer
Internal Logic
Count Enable
Carry Out
Added as part of incrementer
Connect to Count Enable of additional 4-bit counters to form larger counters
\u4e3a\u4e86\u5b9e\u73b0\u9006\u5411\u8ba1\u6570\u5668\uff0c\u6211\u4eec\u5219\u9700\u8981\u5c06\u81ea\u589e\u5668\u6362\u4e3a\u81ea\u51cf\u5668\u3002\u505a\u6cd5\u53ea\u4e0d\u8fc7\u662f\u5c06\u5f02\u6216\u95e8\u7684\u63a7\u5236\u4fe1\u53f7\u53d6\u53cd
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#counter-with-parallel-load","title":"Counter with parallel load","text":"count Load Operation 0 0 Hold 0 1 Load Parallel data 1 x count"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#design-example","title":"Design Example","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#synchronous-bcd","title":"Synchronous BCD","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#counting-modulo-n","title":"Counting Modulo N","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#serial-transforms-and-microoperations","title":"Serial Transforms and Microoperations","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/","title":"Registers and Register Transfers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#registers","title":"Registers","text":"Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#storage-and-load-enable","title":"Storage and Load Enable","text":"Load = 0 : Store
Load = 1 : Load
The Problem is : Clock Skew \u300cAnd Gate Delay\u300d
\u52d8\u8bef Load = 0 \u65f6 \u8109\u51b2\u6d88\u5931\uff0c\u5bc4\u5b58\u5668\u65e0\u6cd5\u88ab\u8f7d\u5165
Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#register-transfer","title":"Register Transfer","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#microoperations","title":"Microoperations","text":"
Logical Groupings:
\\(if\\ \\ (K_1=1)\\ then\\ \\ (R_2\u2190R_1)\\) \u8868\u793a\u4e3a \\(K_1 : R_2\u2190R_1\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#register-transfer-structures","title":"Register Transfer structures","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#multiplexer-based-transfers","title":"Multiplexer-Based Transfers","text":"note : clocks are omitted for clarity
\\(K_1 : R_0\u2190R_1\\)
\\(K2\\bar{K1}: R_0\u2190R_2\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#register-cell-design","title":"Register Cell Design","text":"
Example 1 Not encoded \u300cone hot code\u300d
Example 2 Encoded. \u300cBinary or Gray\u300d
EG1: Example
Register A (m-bits) Sepecification:
Data input B
Control input \\(C_x \\ C_y\\) with combinations\\((0,0)\\ (0,1)\\ (1,0)\\)
Register Transfers:
\\(C_x : A \\leftarrow A\\ or\\ B\\)
\\(C_y\\) : \\(A \\leftarrow A\\ xor\\ B\\)
Hold State : \\((0,0)\\)
Multiplexer Approach
Thus , we have :
\\(Load = C_x + C_y\\)
Cost : 6+3+2(or)+8(xor) = 19
Sequential Circuit Design Approach
Similar to using a MUX, the MUX in this case is no longer dedicated logic for a specific register but is shared logic used by three registers.
The MUX in the bus selects one input among all registers and outputs it to all registers. The bus controls which (or how many) registers receive this signal by manipulating the Load signal.
Advantages:
Disadvantages:
Gate Cost. (increase linearly)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#three-state-bus","title":"Three-State Bus","text":"The 3-input MUX can be replaced by a 3-state node (bus) and 3-state buffers.
Gate Cost Only 2n (suppose cost for three-state is 2)
Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#shift-registers","title":"Shift Registers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#_1","title":"\u4e32\u884c\u5b9e\u73b0","text":"
\u5047\u8bbe\u73b0\u5728\u6211\u4eec\u6709 4 \u4e2a FF
\u9996\u5c3e\u76f8\u8fde\uff0c\u73b0\u5728\u4e32\u884c\u8f93\u5165 1001
\u8fd9 4bits \u7684\u6570\u636e\uff08\u5f53\u7136\uff0c\u5b8c\u5168\u8f93\u5165\u9700\u8981 4 \u4e2a\u65f6\u949f\u5468\u671f\uff09\u3002
\u5047\u8bbe\u7b2c 1 \u4e2a\u5468\u671f\u7ed3\u675f\u540e\uff0c1001
\u6700\u53f3\u4fa7\u7684 1
\u88ab\u5b58\u5728\u4e86\u7b2c\u4e00\u4e2a FF\uff0c\u5219\uff1a
100
1
? ? ? 1???
2 10
0
1
? ? 01??
3 1
0
0
1
? 001?
4 1
0
0
1
1001
5 ? 1
0
0
?100
6 ? ? 1
0
??10
7 ? ? ? 1
???1
\u8fd9\u91cc\u7684 SO \u6307\u7684\u662f\u4ece\u5f53\u524d\u8f6e\u6b21\u5f00\u59cb\uff0c\u8bfb\u5165 4bits \u6570\u636e\u5f97\u5230\u7684\u4e32\u884c\u8f93\u51fa\u7ed3\u679c\uff0c\u8fd9\u91cc\u7684 4bits \u5f53\u7136\u662f\u5bf9\u5e94\u8f93\u5165\u7684 4bits\u3002
\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728\u7b2c\u56db\u8f6e\u524d\u540e\u7684\u7ed3\u679c\u5206\u522b\u662f\u5de6\u79fb\u548c\u53f3\u79fb\u7684\u7ed3\u679c\uff0c\u5f53\u7136\uff0c\u5982\u679c\u662f\u8981\u8003\u8651\u5177\u6709\u5b9e\u9645\u8ba1\u7b97\u4ef7\u503c\u7684\u4f4d\u79fb\uff0c\u6211\u4eec\u5f53\u7136\u9700\u8981\u586b\u5145 ?
\u7684\u503c\uff0c\u4e0d\u8fc7\u8fd9\u90fd\u662f\u5c0f\u4e8b\u60c5\u3002
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#lecture-1-finite-automata","title":"Lecture 1 - Finite Automata","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#example","title":"Example :","text":"
Given a weighted graph G :
Given a string w, is \\(w\\in L=\\{encode(G,k) | G\\ is\\ a\\ graph,that\\ has\\ a\\ ST\\ with\\ weight\\ at\\ most\\ k\\}\\)?
A finite automaton is a 5-tuple \\((K,\\Sigma,\\delta,s,F)\\) where:
A configuration of an FA is a pair \\((q,w)\\) where \\(q\\in Q\\) and \\(w\\in\\Sigma^*\\) where \\(q\\) is the current state and \\(w\\) is the remaining input.
M accepts a language L if (two conditions):
A language is regular if it is accepted by a finite automaton.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#theorem","title":"Theorem","text":"If A and B are regular languages, so is \\(A\\cup B\\). Proof:
A NFA is a 5-tuple \\((K,\\Sigma,\\Delta,s,F)\\) where:
Acceptance of a string by a NFA:
Example: Construct a NFA that accepts the language \\(\\{w | w \\in \\{a,b\\}^*\\),the second symbol from the end of w is b\\(\\}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#theorem_1","title":"Theorem","text":"Given a DFA M, there exists a NFA M' such that \\(L(M) = L(M')\\).
DFA M simulates \"tree-like\" computation of NFA M.
A NFA \\(M = (K,\\Sigma,\\Delta,s,F)\\) can be simulated by a DFA \\(M' = (K',\\Sigma,\\delta,s',F')\\) where:
\\(2^K = \\{q_1,q_2,\\cdots,q_n,\\{q_1\\},\\{q_2\\},\\cdots,\\{q_n\\},\\{q_1,q_2\\},\\cdots,\\{q_1,q_2,\\cdots,q_n\\}\\}\\)
\\(F' = \\{q\\in K' | q\\cap F \\neq \\emptyset\\}\\)
\\(\\delta(q,a) = \\bigcup_{p\\in q}\\Delta(p,a)\\) for any \\(q\\in K'\\) and \\(a\\in\\Sigma\\)
\\(s' = E(s)\\) where \\(\\forall q \\in K, E(q)=\\{p \\in K:(q,e)\\vdash_M^*(p,e)\\}\\)
\\(\\delta(Q,a) = \\bigcup_{q\\in Q}\\bigcup_{p:(p,a,q)\\in\\Delta}E(p)\\) for any \\(Q\\subseteq K'\\) and \\(a\\in\\Sigma\\)
A language is regular if and only if it is accepted by a NFA.
Let \\(M_A\\) and \\(M_B\\) be FAs that accept A and B respectively.
\\(M_A = (K_A,\\Sigma,\\delta_A,s_A,F_A)\\)
\\(M_B = (K_B,\\Sigma,\\delta_B,s_B,F_B)\\)
Construct a new FA \\(M\\) that accepts \\(A\\cdot B\\):
\\(M = (K,\\Sigma,\\delta,s,F)\\) where
\\(K = K_A\\bigcup K_B\\)
\\(s = s_A\\)
\\(F = F_B\\)
\\(\\delta(q,a) = \\delta_A(q,a)\\bigcup\\delta_B(q,a)\\bigcup\\{(q,e,s_B):q\\in F_A\\}\\)
Proof:
The set of all boolean functions(mapping \\(\\{0,1\\}^*\\rightarrow\\{0,1\\}\\)) is uncountable. * Proof see book page 99
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#equivalent-conditions-for-countability","title":"Equivalent conditions for countability","text":"Lemma: Suppose that \\(E: O \\rightarrow \\{0,1\\}^*\\) is a one-to-one function. Then there exists a function \\(D:\\{0,1\\}^*\\rightarrow O\\) such that \\(D(E(o)) = o\\) for all \\(o\\in O\\).
Proof: Let \\(o_0\\) be an arbitrary object in O. Let \\(w_0 = E(o_0)\\). Let \\(w_0 = w_1w_2\\cdots w_n\\) be the string representation of \\(w_0\\). Let \\(o_1 = D(w_1)\\), \\(o_2 = D(w_1w_2)\\), \\(\\cdots\\), \\(o_n = D(w_1w_2\\cdots w_n)\\). Then \\(D(w_0) = o_n\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#finite-representations","title":"Finite Representations","text":"if \\(O\\) is finite, then we can represent every object in \\(O\\) as a string of length at most some number \\(n\\) if and only if \\(|O|\\leq 2^{n+1}-1\\).
Lemma: For every two finite sets S and T, there exists a one-to-one function \\(E:S\\rightarrow T\\) if and only if \\(|S|\\leq|T|\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec2/Lecture2/#lecture-2-regular-expressions","title":"Lecture 2 - Regular Expressions","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec2/Lecture2/#definition","title":"Definition :","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec2/Lecture2/#regular-expression","title":"Regular Expression","text":"
\\(a(a\\cup b)^nb\\) is a regular expression over \\(\\{a,b\\}\\)
\\(L(R) = \\{w \\in \\{a,b\\}^* | w\\ is\\ a\\ string\\ that\\ starts\\ with\\ a\\ and\\ ends\\ with\\ b\\}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec2/Lecture2/#automic-regular-expression","title":"Automic Regular Expression","text":"\\(RE \\rightarrow NPA\\)
\\(NPA \\rightarrow RE\\)
state elimination
and dynamic programming
.Let \\(L\\) be a regular language,there must exist an integer \\(p\\geq 1\\) such that every string \\(w\\in L\\) with \\(|w|\\geq p\\) can be written as \\(w = xyz\\) with
Here \\(p\\) is called the pumping length.
Proof
If \\(L\\) is a regular language and it is finite, then we can set \\(p\\) to be the maximum length +1 of the strings in \\(L\\).
If \\(L\\) is a regular language and it is infinite, there exits a NFA \\(N\\) that accepts \\(L\\). Let \\(N = (Q,\\Sigma,\\delta,q_0,F)\\), where \\(Q = \\{q_0,q_1,\\cdots,q_n\\}\\).
Example
Assume \\(L\\) is a regular language, then \\(L \\cap 0^*1^* = \\{0^n1^n | n\\geq 0\\}\\) is a regular language, which is a contradiction.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec3/Lecture3/","title":"Lecture 3 - Context-Free Language","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec3/Lecture3/#lecture-3-context-free-grammar-cfg","title":"Lecture 3 - Context-Free Grammar CFG","text":"
A context-free grammar (CFG) is a set of recursive rules used to generate patterns of strings. A CFG \\(G = (V, \\Sigma, R, S)\\) consists of:
Define in one step \\(\\Rightarrow\\).
For any \\(x,y,u \\in V^*\\), for any \\(A \\in V - \\Sigma\\), \\(xAy \\Rightarrow xuy\\) if \\((A,u) \\in R\\).
Define in multiple steps \\(\\Rightarrow^*\\).
For any \\(x,y,u \\in V^*\\), for any \\(A \\in V - \\Sigma\\), \\(w \\Rightarrow^* u\\) if \\(w=u\\) or \\(w \\Rightarrow u_1 \\Rightarrow u_2 \\Rightarrow \\cdots \\Rightarrow u_k \\Rightarrow u\\).
G generates \\(w\\in \\Sigma^*\\) if \\(S \\Rightarrow^* w\\). * L(G) = {w \\(\\in \\Sigma ^*\\)| G generates w} is a context-free language.
Example:
If L is a context-free language, then there exists a positive integer p such that any string \\(s \\in L\\) with \\(|s| \\geq p\\) can be written as \\(s = uvxyz\\) such that:
If L is a context-free language, then there exists a positive integer p such that any string \\(s \\in L\\) with \\(|s| \\geq p\\) can be written as \\(s = uvxyz\\) such that:
From 2. and 1., we have \\(vxy\\) contains at most two types of symbols from \\(\\{a,b,c\\}\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#lecture-4-pushdown-automata","title":"Lecture 4 - Pushdown Automata","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#pushdown-automata","title":"Pushdown Automata","text":"
A PDA is a 6-tuple \\(P = (K, \\Sigma, \\Gamma, \\Delta, s, F)\\), where
\\(\\Delta\\) is a \"finite\" subset of \\(K \\times ((\\Sigma \\cup \\{\\epsilon\\}) \\times \\Gamma^*) \\times (K \\times \\Gamma^*)\\). where
A configuration of PDA is a member of \\(K \\times \\Sigma^* \\times \\Gamma^*\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#yeild-in-one-step","title":"yeild in one step","text":"\\((q, x, \\alpha) \\vdash_P (p, y, \\beta)\\) if \\(\\exists( (q, a, r), (p, b) ) \\in \\Delta\\) such that \\(x = ay\\) and \\(\\alpha = r\\tau\\) and \\(\\beta = b\\tau\\). for some \\(\\tau \\in \\Gamma^*\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#yeild-in-multiple-steps","title":"yeild in multiple steps","text":"\\((q, x, \\alpha) \\vdash_P^* (p, y, \\beta)\\) if \\((q, x, \\alpha) \\vdash_P (p_1, y_1, \\beta_1) \\vdash_P \\cdots \\vdash_P (p, y, \\beta)\\) or \\((q, x, \\alpha) = (p, y, \\beta)\\).
PDA accepts a string \\(x\\) if \\((s, x, \\epsilon) \\vdash_P^* (p, \\epsilon, \\epsilon)\\) for some \\(p \\in F\\).
Language of PDA \\(P\\) is \\(L(P) = \\{x \\in \\Sigma^* | (s, x, \\epsilon) \\vdash_P^* (p, \\epsilon, \\epsilon) \\text{ for some } p \\in F\\}\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#example","title":"Example","text":"Construct a PDA that accepts the language \\(\\{w \\in \\{0, 1\\}^* | \\#0's = \\#1's\\}\\).
\\(CFG \\Rightarrow PDA\\). and \\(PDA \\Rightarrow CFG\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#cfg-rightarrow-pda","title":"CFG \\(\\Rightarrow\\) PDA","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#only-give-an-example","title":"Only Give an Example","text":"Given a CFG with \\(R = \\{S \\rightarrow aSb | \\epsilon\\}\\), construct a PDA \\(P\\) that accepts \\(L(G)\\).
The idea is to first convert the PDA to a simple PDA, then convert the simple PDA to a CFG.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#theorem","title":"Theorem","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#theorem-1","title":"Theorem 1","text":"
Every Regular Language is a Context-Free Language.
The family of context-free languages is closed under \\(\\cup, \\cdot, *\\). but not closed under \\(\\cap, -\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#cup","title":"\\(\\cup\\)","text":"Proof: \\(A \\cap B = \\overline{\\bar{A} \\cup \\bar{B}}\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#examples","title":"Examples","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#example-1","title":"Example 1","text":"Construct a PDA that accepts the language \\(\\{w \\in \\{a, b\\}^* | \\#a's = 2\\#b's\\}\\).
Idea: Stack \\(\\Rightarrow\\) Unary Counter.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#lecture-5-turing-machine","title":"Lecture 5 - Turing Machine","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#turing-machine","title":"Turing Machine","text":"
A Turing machine is a mathematical model of computation that defines an abstract machine. It was invented by Alan Turing in 1936. A Turing machine can simulate the logic of any computer algorithm, and is therefore the theoretical foundation of all modern computers.
A Turing machine is a 5-tuple \\((K, \\Sigma, \\delta, s, H)\\), where:
The transition function \\(\\delta\\) satisfies the following properties:
leftend \\(\\triangleright\\) is a special symbol that is used to indicate the left end of the tape.
blank symbol \\(\\cup\\) is a special symbol that is used to indicate the blank symbol.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#configuration","title":"configuration","text":"A configuration of a Turing machine is a member of
\\[(K \\times \\triangle(\\Sigma-\\{\\triangleright\\})^*) \\times ((\\Sigma-\\{\\triangleright\\})^* (\\Sigma - \\{\\triangleright,\\cup\\})\\cup \\{e\\})\\]We say \\((q_1,\\triangleright w_1a_1u_1) \\vdash_M (q_2,\\triangleright w_2a_2u_2)\\) if
writing : \\(\\delta(q_1,a_1) = (q_2,a_2) and\\ a_2 \\in \\Sigma - \\{\\triangleright\\}\\) and \\(w_2 = w_1\\) and \\(u_2 = u_1\\).
moving left : \\(\\delta(q_1,u_1) = (q_2,L)\\) and \\(w_1=w_2a_2\\) and \\(u_2 = a_1u_1\\).
M halts if it reaches a halting configuration
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#acceptance-and-rejection","title":"Acceptance and Rejection","text":"A Turing machine M accepts a string w if \\((s,\\triangleright\\cup w) \\vdash^* (yes,\\triangleright\\cup aw)\\)
A Turing machine M rejects a string w if \\((s,\\triangleright\\cup w) \\vdash^* (no,\\triangleright\\cup aw)\\)
Given a Turing machine M, we can define the language accepted by M as \\(L(M) = \\{w \\in \\Sigma^* | M \\text{ accepts w}\\}\\).
M decides a language \\(L\\) if M accepts all strings in L and rejects all strings not in L.
M semi-decides a language \\(L\\) if M accepts all strings in L and may loop (or reject) on strings not in L.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#reccursive-language","title":"Reccursive Language","text":"A language \\(L\\) is recursive if there exists a Turing machine that decides \\(L\\).
Every recursive language is recursively enumerable.
A multi-tape Turing machine is a Turing machine with multiple tapes. Each tape has its own head and can move independently.
\\[\\delta: (K - H) \\times \\Sigma^k \\rightarrow K \\times ((\\Sigma -\\triangleright)\\cup \\{L,R\\})^k.\\]"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#two-way-infinite-tape","title":"Two-way Infinite Tape","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#multi-head-turing-machine","title":"Multi-head Turing Machine","text":"A multi-head Turing machine is a Turing machine with multiple heads on a single tape.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#2d-tape-turing-machine","title":"2D-Tape Turing Machine","text":"Here is a high-level description of a Turing machine that can decide \\( L \\):
Check if the string consists of only \\( a \\)'s, \\( b \\)'s, and \\( c \\)'s: - The TM scans the input tape to ensure that it only contains \\( a \\)'s, \\( b \\)'s, and \\( c \\)'s in that order. If any other character is found, the machine rejects the string.
Match \\( a \\)'s, \\( b \\)'s, and \\( c \\)'s: - Start by marking the leftmost \\( a \\) and replace it with a special symbol (say \\( X \\)). - Then, scan the tape to find the leftmost \\( b \\). If one is found, mark it with a different special symbol (say \\( Y \\)). - Next, scan to find the leftmost \\( c \\), and mark it with another symbol (say \\( Z \\)). - Now, go back to the left end of the tape and repeat the process, continuing to match one \\( a \\), one \\( b \\), and one \\( c \\) at a time, until either:
Verify the end condition: - Once all the symbols have been marked, check the tape:
Empty string check: - The empty string \\( \\epsilon \\) is trivially in \\( L \\), as it satisfies the condition \\( a^n b^n c^n \\) where \\( n = 0 \\).
M decides a language \\(L\\) if
M semi-decides a language \\(L\\) if for any \\(w \\in L\\):
Let \\(C = {100,110,1000,...}\\) C is composed of all binary numbers that are not prime. -- Construct a NTM that semi-decides C.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#theorem","title":"Theorem","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#theorem-1","title":"Theorem 1","text":"Every NTM can be simulated by a DTM.
A NTM semi-decides a language \\(L\\) if and only if there exists a DTM that semi-decides \\(L\\).
A NTM semi-decides a language \\(L\\) \\(\\Rightarrow\\) There exists a DTM that semi-decides \\(L\\).
Use a three-tape DTM to simulate a NTM.
A Turing machine can be described by high-level pseudocode.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#fact","title":"Fact","text":"M on input \\(G\\):
Input: \\(\\langle D,w \\rangle\\), where \\(D\\) is a DFA and \\(w\\) is a string.
Output: Accept if \\(D\\) accepts \\(w\\); reject otherwise.
Solution:Construct a Turing machine that simulates the DFA \\(D\\) on input \\(w\\).
\\(M_{R_1}\\) = on input \\(\\langle D,w \\rangle\\):
Input: \\(\\langle B,w \\rangle\\), where \\(B\\) is a NFA and \\(w\\) is a string.
Output: Accept if \\(B\\) accepts \\(w\\); reject otherwise.
Solution: Construct a Turing machine that simulates the NFA \\(B\\) on input \\(w\\).
\\(M_{R_2}\\) = on input \\(\\langle B,w \\rangle\\):
This involves the process of reduction.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#a_rex","title":"\\(A_{REX}\\)","text":"Input: \\(\\langle R,w \\rangle\\), where \\(R\\) is a regular expression and \\(w\\) is a string.
Output: Accept if \\(R\\) accepts \\(w\\); reject otherwise.
Solution: Construct a Turing machine that simulates the NFA \\(B\\) on input \\(w\\).
\\(M_{R_3}\\) = on input \\(\\langle R,w \\rangle\\):
Input: \\(\\langle B \\rangle\\), where \\(B\\) is a DFA.
Output: Accept if \\(L(B) = \\emptyset\\); reject otherwise.
Solution: Construct a Turing machine that simulates the DFA \\(B\\) on input \\(w\\).
\\(M_{R_4}\\) = on input \\(\\langle B \\rangle\\):
Input: \\(\\langle B_1,B_2 \\rangle\\), where \\(B_1\\) and \\(B_2\\) are DFAs.
Output: Accept if \\(L(B_1) = L(B_2)\\); reject otherwise.
Solution: Construct a Turing machine that simulates the DFA \\(B\\) on input \\(w\\).
\\(M_{R_5}\\) = on input \\(\\langle B_1,B_2 \\rangle\\):
Input: \\(\\langle G,w \\rangle\\), where \\(G\\) is a CFG and \\(w\\) is a string.
Output: Accept if \\(G\\) generates \\(w\\); reject otherwise.
\\(A_{CFG} = \\{ \\langle G,w \\rangle | G \\text{ is a CFG and G generates w}\\}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#chonsky-normal-form","title":"Chonsky Normal Form","text":"A CFG \\(G\\) is in Chonsky Normal Form if every rule is of the form:
So if final \\(w\\) has length \\(n\\), the number of steps of subtitution is \\(2n-1\\).
\\(M_{C1}\\) = on input \\(\\langle G,w \\rangle\\): 1. Convert CFG \\(G\\) to Chonsky Normal Form \\(G'\\). 2. Enumerate all derivations of length at most \\(|R'|^{2n-1}\\). 3. Acscept if any derivation generates \\(w\\); otherwise, reject.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#a_pda","title":"\\(A_{PDA}\\)","text":"Input: \\(\\langle P,w \\rangle\\), where \\(P\\) is a PDA and \\(w\\) is a string.
Output: Accept if \\(P\\) accepts \\(w\\); reject otherwise.
\\(A_{PDA} = \\{ \\langle P,w \\rangle | P \\text{ is a PDA and P accepts w}\\}\\)
\\(M_{C_2}\\) = on input \\(\\langle P,w \\rangle\\): 1. Convert PDA \\(P\\) to a CFG \\(G\\). 2. Run \\(M_{C_1}\\) on input \\(\\langle G,w \\rangle\\). 3. If \\(M_{C_1}\\) accepts, accept; otherwise, reject.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#e_cfg","title":"\\(E_{CFG}\\)","text":"Input: \\(\\langle G \\rangle\\), where \\(G\\) is a CFG.
Output: Accept if \\(L(G) = \\emptyset\\); reject otherwise.
\\(E_{CFG} = \\{ \\langle G \\rangle | L(G) = \\emptyset\\}\\)
\\(M_{C_3}\\) = on input \\(\\langle G \\rangle\\): 1. Mark all terminals and \\(e\\). 2. Look through all rules and if there is a rule that all symbols on the right side are marked , mark the left side. 3. Repeat step 2 until no new symbols are marked. 4. If \\(S\\) is marked, reject; otherwise, accept.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#e_pda","title":"\\(E_{PDA}\\)","text":"Input: \\(\\langle P \\rangle\\), where \\(P\\) is a PDA.
Output: Accept if \\(L(P) = \\emptyset\\); reject otherwise.
\\(E_{PDA} = \\{ \\langle P \\rangle | L(P) = \\emptyset\\}\\)
\\(M_{C_4}\\) = on input \\(\\langle P \\rangle\\): 1. Convert PDA \\(P\\) to a CFG \\(G\\). 2. Run \\(M_{C_3}\\) on input \\(\\langle G \\rangle\\). 3. If \\(M_{C_3}\\) accepts, accept; otherwise, reject.
\\(A_{DFA}\\) is recursive \\(\\rightarrow\\) \\(L(D)\\) is recursive.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/","title":"Lecture 6 - Containments properties of languages","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#containments-properties-of-languages","title":"Containments properties of languages","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#summary","title":"Summary","text":"
\\(\\text{regular languages}\\in \\text{context-free languages} \\in \\text{recursively languages} \\in \\text{recursively enumerable languages}(semi-decidable)\\)
A set A is countable if it is finite or \\(\\exists\\) a bijection between A and \\(\\mathbb{N}\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#lemma-1","title":"Lemma 1","text":"A set A is countable if and only if there is an injection from A to \\(\\mathbb{N}\\).
injection: \\(f:A \\rightarrow \\mathbb{N}\\) is an injection if \\(f(a) = f(b) \\Rightarrow a = b\\).
(\\(\\Leftarrow\\)) If bijection exists, then injection exists.
Any subset of a countable set A is countable.
Proof:
Any language \\(\\Sigma^*\\) is countable.
Proof:
\\(\\{M: \\text{M is a Turing Machine}\\}\\) is countable.
Let \\(\\Sigma\\) be an alphabet. Let \\(L\\) be a language over \\(\\Sigma\\). Then, \\(L\\) is uncountable.
See link
Another Proof:
\\(A_{TM} = \\{<M,w>|M \\text{ is a TM and M accepts w}\\}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem1","title":"Theorem1","text":"\\(A_{TM}\\) is recursively enumerable.
Proof:
Suppose \\(A_{TM}\\) is recursively enumerable. Then, there exists a TM \\(E\\) that semi-decides \\(A_{TM}\\).
E = On input : 1. Simulate M on w. 2. If M accepts w, accept; if M rejects w, reject. 3. Loop if M loops on w. 4. If M rejects w, reject."},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem2","title":"Theorem2","text":"
\\(A_{TM}\\) is not recursive.
Proof:
Suppose \\(A_{TM}\\) is recursive. Then, there exists a TM \\(H\\) that decides \\(A_{TM}\\).
H = On input : 1. Run M on w. 2. If M accepts w, accept; if M rejects w, reject.
Consider the following TM D:
D = On input : 1. Run H on <M,\\. 2. If H accepts, reject; if H rejects, accept.
In this condition, it means that D can decide D so D is recursive. -- \\(A_{TM}\\) is recursive \\(\\Rightarrow\\) \\(D (A_d)\\) is recursive.
Consider what happens when we run D on \\<D>.
So, \\(A_{TM}\\) is not recursive.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#ad","title":"Ad","text":"\\(A_d = \\{<\"M\">|M \\text{ is a TM that does not accept \"M\"}\\}\\) -- See Above.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem3","title":"Theorem3","text":"\\(A_d\\) is not recursively enumerable.
Proof:
Suppose \\(A_d\\) is recursively enumerable. Then, there exists a TM \\(D\\) that semi-decides \\(A_d\\).
D = On input : 1. Accept if \\(M \\in A_d\\). (M rejects \"M\") 2. Reject or Loop if \\(M \\notin A_d\\).\" (M accepts \"M\")
Consider what happens when we run D on \\<D>.
\\(A_{TM}\\) is recursively enumerable but not recursive. \\(A_d\\) is not recursively enumerable.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem4","title":"Theorem4","text":"If \\(L\\) and \\(\\overline{L}\\) are both recursively enumerable, then \\(L\\) is recursive.
Proof:
D = On input w: 1. Run \\(M_1\\) and \\(M_2\\) parallelly on w. 2. If \\(M_1\\) accepts w, accept; if \\(M_2\\) accepts w, reject.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem5","title":"Theorem5","text":"\\(\\overline{A_{TM}}\\) is not recursively enumerable.
Proof:
Suppose \\(\\overline{A_{TM}}\\) is recursively enumerable. Then, there exists a TM \\(D\\) that semi-decides \\(\\overline{A_{TM}}\\).
Also we know that \\(A_{TM}\\) is recursively enumerable. Then, there exists a TM \\(E\\) that semi-decides \\(A_{TM}\\).
But \\(A_{TM}\\) is not recursive. So, \\(\\overline{A_{TM}}\\) is not recursively enumerable.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/","title":"Lecture 7 - Decidability","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#problems-that-are-not-recursive","title":"Problems That Are Not Recursive","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#examples","title":"Examples","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#1-the-halting-problem","title":"1. The Halting Problem","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#proof-1","title":"Proof 1","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#proof-2","title":"Proof 2","text":"
\\(A_{TM} = \\{<M, w>|M\\text{ is a TM and M accepts w}\\}\\) * \\(A_{TM}\\) is not recursive
Construct a TM \\(M^*\\ H_{TM}\\) to solve \\(A_{TM}\\):
\\(H_{TM} = {<M, w>|M\\text{ is a TM and M halts on w}}\\)
$M^* = $ On input x: 1. run M on w 2. If M accepts w, accept 3. If M rejects w, reject 4. If M loops on w, \\(M^*\\) is looping on w
Suppose \\(M^*\\) is a recursive TM, then we can construct a TM \\(M_{H}\\) to decide \\(H_{TM}\\):
$M_{A} = $ On input : 1. Construct a TM \\(M^*\\) as above using M 2. Run \\(M_{H}\\) on \\(M^*\\), w 1. If \\(M_{H}\\) accepts, accept 2. If \\(M_{H}\\) rejects, reject
If all this is true, then \\(M_A\\) can decide \\(A_{TM}\\) * But \\(A_{TM}\\) is not recursive * So \\(M^*\\) is not recursive * So the halting problem is not recursive
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#1-l-mtextm-is-a-tm-that-accepts-e-is-not-recursive","title":"1. \\(L = \\{\"M\"|\\text{M is a TM that accepts e}\\}\\) is not recursive","text":"A Turing machine \\(M\\) accepts \\(w\\) if and only if \\(M^*\\) accepts \\(e\\)
$M^* = $ On input x: 1. Run M on w 2. If M accepts w, accept 3. If M rejects w, reject
If M accepts w, then \\(M^*\\) accepts all inputs, including e
If M rejects or loops on w, then \\(M^*\\) rejects all inputs, including e
Suppose \\(L\\) is recursive, then we can construct a TM \\(M_{L}\\) to decide \\(L\\):
$M_{A} = $ On input : 1. Construct a TM \\(M^*\\) as above using M 2. Run \\(M_{L}\\) on \\(M^*\\), w 1. If \\(M_{L}\\) accepts, accept 2. If \\(M_{L}\\) rejects, reject
If all this is true, then \\(M_A\\) can decide \\(A_{TM}\\) * But \\(A_{TM}\\) is not recursive * So \\(M_L\\) is not recursive * So \\(L\\) is not recursive
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#2-l_2-mtextm-is-a-tm-that-accepts-all-strings-is-not-recursive","title":"2. \\(L_2 = \\{\"M\"|\\text{M is a TM that accepts all strings}\\}\\) is not recursive","text":"A Turing Machine accepts w if and only if \\(M^*\\) accepts all strings
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#3-l_3-m1-m2textm1-and-m2-accept-the-same-set-of-strings-is-not-recursive","title":"3. \\(L_3 = \\{\"M1, M2\"|\\text{M1 and M2 accept the same set of strings}\\}\\) is not recursive","text":"Suppose that \\(L_3\\) is recursive, \\(\\exist M_3\\) that decides \\(L_3\\)
Use \\(M_3\\) to construct \\(M_2\\) that deides \\(L_2\\)
\\(M_2\\) = on input \"M\" 1. construct \\(M_E\\) as follows: \\(M_E\\) = on input x 1. Accept 2. Run \\(M_3\\) on \"\\(M\\)\",\"\\(M_E\\)\" -- check whether \\(M\\) and \\(M_E\\) accepts the same set of strings 3. If \\(M_3\\) accepts, accepts \\(M\\) 4. If \\(M_3\\) rejects, rejects \\(M\\)
So \\(M_2\\) decides \\(L_2\\) but \\(L_2\\) is not recursive
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#4-l_4-mtextm-is-a-tm-with-lm-is-regular-is-not-recursive","title":"4. \\(L_4 = \\{\"M\"|\\text{M is a TM with L(M) is regular}\\}\\) is not recursive","text":"Proof: \\(L_4\\) is not recursive
Suppose that \\(L_4\\) is recursive, \\(\\exist M_4\\) that decides \\(L_4\\)
Universal Turing Machine U : On input : 1. Run M on w 2. If M accepts w, accept 3. else reject
Language of \\( M^* \\): The language \\( L(M^*) \\) is constructed so that: - \\( L(M^*) \\) is non-empty if \\( M \\) accepts \\( w \\). - \\( L(M^*) \\) = \\(L(U)\\) = \\(A_{TM}\\) Not regular/Not Context-Free /Not Recursive - \\( L(M^*) = \\emptyset \\) if \\( M \\) does not accept \\( w \\). -- Regular/Contex-Free/Recursive
Conclusion: - If \\( L(M^*) \\) is non-regular, it implies that \\( M \\) accepts \\( w \\). - If \\( L(M^*) \\) is regular, then \\( M \\) does not accept \\( w \\). - Since determining whether \\( L(M^*) \\) is regular would solve the Halting Problem, it follows that \\( R_{TM} \\) is undecidable.
The undecidability of \\( R_{TM} = \\{ \\langle M \\rangle : M \\text{ is a Turing machine with } L(M) \\text{ regular} \\} \\) (i.e., determining if a Turing machine \\( M \\) has a regular language \\( L(M) \\)) stems from the fact that if we could decide \\( R_{TM} \\), we could also decide the Halting Problem, which is known to be undecidable.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#heres-a-step-by-step-explanation-of-why-this-leads-to-undecidability","title":"Here\u2019s a step-by-step explanation of why this leads to undecidability:","text":"Assume \\( R_{TM} \\) is Decidable: Suppose there exists a Turing machine \\( D \\) that decides \\( R_{TM} \\), meaning that \\( D \\) can take as input any Turing machine \\( M \\) and decide if \\( L(M) \\) is regular.
Reduction from the Halting Problem: We know that the Halting Problem (determining whether a Turing machine \\( M \\) halts on an input \\( w \\)) is undecidable. To show that \\( R_{TM} \\) is also undecidable, we construct a new Turing machine \\( M^* \\) based on \\( M \\) and \\( w \\) and show that if we could decide whether \\( L(M^*) \\) is regular, we could solve the Halting Problem.
Construction of \\( M^* \\): Define \\( M^* \\) as a Turing machine that: - First simulates \\( M \\) on \\( w \\). - If \\( M \\) halts on \\( w \\), \\( M^* \\) then simulates a universal Turing machine \\( U \\) on any additional input \\( x \\). - If \\( M \\) does not halt on \\( w \\), \\( M^* \\) simply rejects all inputs or does not accept any string beyond a certain structure.
Behavior of \\( L(M^*) \\): - If \\( M \\) halts on \\( w \\), then \\( L(M^*) \\) becomes non-regular (since it depends on \\( L(U) \\), the language of a universal Turing machine, which is non-regular). - If \\( M \\) does not halt on \\( w \\), \\( L(M^*) \\) is regular (e.g., it could be empty or finite, depending on the construction).
Implication: If we had a way to determine whether \\( L(M^*) \\) is regular using the hypothetical decider \\( D \\) for \\( R_{TM} \\), we could answer whether \\( M \\) halts on \\( w \\): - If \\( D \\) tells us that \\( L(M^*) \\) is regular, then \\( M \\) does not halt on \\( w \\). - If \\( D \\) tells us that \\( L(M^*) \\) is non-regular, then \\( M \\) halts on \\( w \\).
Contradiction: Since this method would solve the Halting Problem (which is known to be undecidable), the assumption that \\( R_{TM} \\) is decidable must be incorrect. Therefore, \\( R_{TM} \\) is undecidable.
In short, if we could decide whether \\( L(M) \\) is regular for any Turing machine \\( M \\), we could also solve the Halting Problem by constructing \\( M^* \\) as described. This leads to a contradiction, proving that \\( R_{TM} \\) is undecidable.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#5-l_5-mtextm-is-a-tm-with-lm-is-context-free-is-not-recursive","title":"5. \\(L_5 = \\{\"M\"|\\text{M is a TM with L(M) is context-free}\\}\\) is not recursive","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#6-l_6-mtextm-is-a-tm-with-lm-is-recursive-is-not-recursive","title":"6. \\(L_6 = \\{\"M\"|\\text{M is a TM with L(M) is recursive}\\}\\) is not recursive","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#rices-theorem","title":"Rice's Theorem","text":"\\(All_{PDA} = \\{<M>|M\\text{ is a PDA and }L(M) = \\Sigma^*\\}\\)
Construct a TM \\(M\\) to solve \\(NOTAll_{PDA}\\): * M halts on w if and only if \\(L(P) \\neq \\Sigma^*\\)
Construct P accepts all strings that are not computing history of M that halts on w.
If \\(NOTAll_{PDA}\\) is recursive, then \\(H_{TM}\\) is recursive, which is not true.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#reduction","title":"Reduction","text":"Let A, B be two languages, A reduces to B if there is a computable function f \\(\\Sigma^* \\to \\Sigma^*\\) such that:
\\(x \\in A \\Leftrightarrow f(x) \\in B\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#lemma-1","title":"Lemma 1","text":"Suppose \\(\\exist f\\) that reduces A to B:
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#chap2language-of-the-computerrisc-v","title":"Chap2:Language of the Computer(RISC V)","text":"Summary"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#instruction-characteristics","title":"Instruction Characteristics","text":"
\u64cd\u4f5c\u6570\u4f4d\u5bbd\u53ef\u4ee5\u4e0d\u540c\uff0c\u53ef\u4ee5\u662f\u7acb\u5373\u6570/\u5bc4\u5b58\u5668/\u5185\u5b58
Load/Store \u7ed3\u6784
\u6307\u4ee4\u5206\u7c7b\uff1aLoad/Store\u7ed3\u6784\u5c06\u6307\u4ee4\u5206\u4e3a\u4e24\u7c7b\uff1a\u52a0\u8f7d\uff08Load\uff09\u6307\u4ee4\u548c\u5b58\u50a8\uff08Store\uff09\u6307\u4ee4\u3002\u8fd9\u4e24\u7c7b\u6307\u4ee4\u5206\u522b\u7528\u4e8e\u4ece\u5185\u5b58\u4e2d\u52a0\u8f7d\u6570\u636e\u5230\u5bc4\u5b58\u5668\u6216\u5c06\u5bc4\u5b58\u5668\u4e2d\u7684\u6570\u636e\u5b58\u50a8\u5230\u5185\u5b58\u4e2d
\u6570\u636e\u4f20\u8f93\uff1a\u5728Load/Store\u7ed3\u6784\u4e2d\uff0c\u53ea\u6709Load\u548cStore\u6307\u4ee4\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\u3002\u5176\u4ed6\u6307\u4ee4\uff0c\u5982\u7b97\u672f\u8fd0\u7b97\u6307\u4ee4\u6216\u903b\u8f91\u6307\u4ee4\uff0c\u5fc5\u987b\u9996\u5148\u5c06\u6570\u636e\u52a0\u8f7d\u5230\u5bc4\u5b58\u5668\u4e2d\uff0c\u7136\u540e\u6267\u884c\u64cd\u4f5c\uff0c\u6700\u540e\u518d\u5c06\u7ed3\u679c\u5b58\u56de\u5185\u5b58
Example
f = (g + h) - (i + j);\n
add t0, g, h\nadd t1, i, j\nsub f, t0, t1\n
;Not good for parallel opeartions\nadd t0, g, h\nsub t0, t0, i\nsub f, t0, j\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#regisers","title":"Regisers","text":"load
\u6307\u4ee4\u53ef\u4ee5\u8bbf\u95ee\u7684\u8303\u56f4\u6709\u8fd9\u4e48\u5927\u3002 x0
: \u56e0\u4e3a\u7ecf\u5e38\u6709 0 \u53c2\u4e0e\u8ba1\u7b97\uff0c\u5c06\u5176\u5b58\u5728\u4e00\u4e2a\u5bc4\u5b58\u5668\u4e2d\uff0c\u4fbf\u4e8e\u8ba1\u7b97add x5,x20,x21\nadd x6,x22,x23\nsub x19,x5,x6\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#memory-operands","title":"Memory Operands","text":"Data transfer instructions * Load: Load values from memory to register * Store: Store result from register to memory; store doubleword
Memory is byte addressed.
RISC-V is Little Endian
RISC-V dose not require words to be aligned in memory : To Save Memory
words align: A word is 4 byte
\u6211\u4eec\u8981\u6c42\u5b57\u7684\u8d77\u59cb\u5730\u5740\u4e00\u5b9a\u8981\u662f 4 \u7684\u500d\u6570 : \u8bbf\u5b58\u66f4\u52a0\u65b9\u4fbf
LW t0,[0,4,8,...] \nLD t0,[0,8,16,32...]\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#data-transfer-instruction","title":"Data Transfer instruction","text":"g = h + A[i]\n//assume g,h,i - x18,x19,x20 base address of A -x22\n
0(x5)
x5
is for \u504f\u79fbadd x5,x20,x20\nadd x5,x5,x5\nadd,x5,x5,x5\nadd,x5,x5,x22\nld,x6,0(x5) \nadd x18,x19,x6\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#registers-vs-memory","title":"Registers vs. Memory","text":"Immediate: Other method for adding constant
Avoids the load instruction
Offer versions of the instruction e.g. addi x22, x22, 4
load byte / load half
e.g. addi
, ld
rs1: source or base address register number
immediate: constant operand, or offset added to base address \u5c06 rs2, funct7 \u5408\u5e76\u4e86\uff0c\u5f97\u5230 12 \u4f4d\u7acb\u5373\u6570
Stored Program Computer
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#logical-operations","title":"Logical Operations","text":"Operation C Java RISC-V Shift left << <<
slli
Shift right >> >>> srli
Bit-by-by AND & & and, andi
Bit-by-by OR | | or, ori
Bit-by-by XOR ^ ^ xor, xori
Bit-by-by NOT ~ ~ - 0(x5)
x5
is for \u504f\u79fbfunct6
: \u79fb\u4f4d\u4e0d\u9700\u8981\u8fd9\u4e48\u591a\u7acb\u5373\u6570\uff0c\u53ea\u8981\u516d\u4f4d (\\(2^6=64\\)) \u5373\u53ef\u3002not--xor 1111111...
beq register1, register2, L1\n bne register1, register2, L1\n
RISC-V assembly code
bne x22, x23, ELSE ; go to ELSE if i != j\nadd x19, x20, x21 ; f = g + h ( skipped if i not equals j)\nbeq x0, x0, EXIT ; as jump (unconditional branch)\nELSE: sub x19, x20, x21; f = g - h ( skipped if i equals j )\nEXIT:\n
bne \nbeq\nblt rs1, rs2, L1;if (rs1 < rs2) branch to instruction labeled L1\nbge rs1, rs2, L1;if (rs1 >= rs2) branch to instruction labeled L1\n
Example "},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#_1","title":"Chapter2","text":"
Signed integer\nslt \uff1aSet on less than\nslti \uff1aSet on less than immediate\nUnsigned integer\nsltu \uff1a Set on less than\nsltiu \uff1a Set on less than immediate \n
Slt\u8fd8\u6709\u4e0e\u7acb\u5373\u6570\u7684\u6bd4\u8f83
Slt, slti\u9488\u5bf9\u6709\u7b26\u53f7\u6570\uff0csltu sltiu\u9488\u5bf9\u65e0\u7b26\u53f7\u6570\u4e0d\u540c\u7684\u6bd4\u8f83\u6307\u4ee4\u8fd0\u884c\u5230\u540c\u4e00\u4e2a\u64cd\u4f5c\u6570\u4e0a\uff0c\u7ed3\u679c\u4e5f\u662f\u4e0d\u4e00\u6837\u7684\u5c06\u6709\u7b26\u53f7\u6570\u4f5c\u4e3a\u65e0\u7b26\u53f7\u6570\u6765\u5904\u7406\uff0c\u662f\u68c0\u9a8c0<=x<y\u7684\u4f4e\u5f00\u9500\u65b9\u6cd5\uff0c\u5e38\u7528\u4e8e\u68c0\u67e5\u6570\u7ec4\u7684\u4e0b\u6807\u662f\u5426\u8d8a\u754c\u3002
slt
\u6839\u636e\u6bd4\u8f83\u7ed3\u679c\u8bbe\u7f6e\u5bc4\u5b58\u5668\uff0c\u7136\u540e\u7528beq
\u6216\u8005bne
\u6765\u8fdb\u884c\u5206\u652f\u5224\u65ad\uff0cblt
, bge
\u8fd9\u4e9b\u6307\u4ee4\u662f\u4f5c\u4e3a\u4f2a\u6307\u4ee4slt x5, x6, x7 ; x5 = 1 if x6 < x7 ( a < b) \nbne x5, x0, Less ; go to Less if x5 != 0 (that is, if a < b)\n \u2026\u2026 \nLess:\n
Reduce an index-out-of-bounds check
\u5c06\u6709\u7b26\u53f7\u6570\u5f53\u4f5c\u65e0\u7b26\u53f7\u6570\u5904\u7406\uff0c\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f4e\u6210\u672c\u7684\u65b9\u5f0f\u68c0\u67e5\u662f\u54260<=x<y
\u5e38\u7528\u4e8e\u68c0\u67e5\u6570\u7ec4\u4e0b\u6807\u662f\u5426\u8d8a\u754c
x<y
\uff0c\u5728\u68c0\u6d4bx<y
\u7684\u540c\u65f6\uff0c\u4e5f\u68c0\u6d4b\u4e86x
\u662f\u5426\u4e3a\u8d1f\u6570bgeu x20, x11, IndexOutofBounds
sltu $t0, $a1, $t2 ; x20 < x11 \nbeq $t0, $zero, IndexOutofBounds \n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#loop-statements","title":"Loop statements","text":"Example"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#caseswitch","title":"Case/Switch","text":"\u5b9e\u73b0switch\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u501f\u52a9\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad\u5e8f\u5217,\u628aswitch\u8f6c\u5316\u4e3a\u4e00\u7cfb\u5217\u7684 if-then-else
\u53e6\u4e00\u79cd\u66f4\u6709\u6548\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u7f16\u7801\u5f62\u6210\u4e00\u4e2a\u8f6c\u79fb\u5730\u5740\u8868\uff0c\u5c31\u662f\u5206\u652f\u6307\u4ee4\u5e8f\u5217\u5730\u5740\u8868\uff1b\u7a0b\u5e8f\u901a\u8fc7\u67e5\u627e\u8f6c\u79fb\u5730\u5740\u8868\u6765\u83b7\u53d6\u76ee\u6807\u5730\u5740\uff0c\u5e76\u8df3\u8f6c\u5230\u76f8\u5e94\u7684\u5206\u652f\u6307\u4ee4\u5e8f\u5217
\u8f6c\u79fb\u5730\u5740\u8868\u5c31\u662f\u4e00\u4e2a\u5b57\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5bf9\u5e94\u4e8e\u4ee3\u7801\u4e2d\u5404\u4e2a\u6807\u53f7\u7684\u5730\u5740
\u4e3a\u652f\u6301\u8fd9\u79cd\u60c5\u51b5\uff0c\u6709\u4e00\u6761\u95f4\u63a5\u8df3\u8f6c\u6307\u4ee4\uff0cjalr
: \u65e0\u6761\u4ef6\u5730\u8f6c\u79fb\u5230\u67d0\u4e2a\u5bc4\u5b58\u5668\u6307\u5b9a\u7684\u5730\u5740.
A basic block is a sequence of instructions with
A compiler identifies basic blocks for optimization.
An advanced processor can accelerate execution of basic blocks.
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#supporting-procedures-in-computer-hardware","title":"Supporting Procedures in Computer Hardware","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#procedure-call-instructions","title":"Procedure Call Instructions","text":"Procedure call: jump and link
jal x1
, ProcedureLabe
\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u8fc7\u7a0b\u7684\u6807\u7b7e\u4e3a MyProcedure
\uff0c\u5219 jal x1, MyProcedure
\u5c06\u4f1a\u5c06\u5f53\u524d\u5730\u5740\u4fdd\u5b58\u5230\u5bc4\u5b58\u5668 x1
\u4e2d\uff0c\u5e76\u8df3\u8f6c\u5230 MyProcedure
\u6807\u7b7e\u6240\u4ee3\u8868\u7684\u4ee3\u7801\u5904\u6267\u884c
Procedure return: jump and link register
jalr x0, 0(x1)
x0
as rd
(x0 cannot be changed)\u65e0\u6761\u4ef6\u8df3\u8f6c\uff1a jal x0, Label
\uff0c\u56e0\u4e3ax0\u786c\u8fde\u7ebf\u52300\uff0c\u6548\u679c\u7b49\u540c\u4e8e\u4e22\u5f03\u8fd4\u56de\u5730\u5740
\u4f7f\u7528 x0
\u5bc4\u5b58\u5668\u4f5c\u4e3a\u76ee\u6807\u5bc4\u5b58\u5668\uff08rd
\uff09\uff0c\u5373\u8fd4\u56de\u5730\u5740\u5c06\u88ab\u5b58\u50a8\u5728 x0
\u4e2d\u3002\u7531\u4e8e x0
\u5bc4\u5b58\u5668\u7684\u503c\u6c38\u8fdc\u662f 0
\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u64cd\u4f5c\u53ef\u4ee5\u89c6\u4e3a\u5c06\u8fd4\u56de\u5730\u5740\u4e22\u5f03
Registers for procedure calling
x10~ x17
: 8 argument registers to pass parameters or return values
x1
: one return address register to return to origin point.
\u6808\u662f\u5185\u5b58\u91cc\u7684\u4e00\u5757\u5b58\u50a8\u7ed3\u6784\uff0c\u6808\u5e95\u662f\u9ad8\u5730\u5740\uff0c\u6808\u589e\u957f\u662f\u5730\u5740\u4ece\u9ad8\u5230\u4f4e\uff0cpush\u7684\u65f6\u5019\uff0csp\u51cf\u5c0f\uff0cpop, sp\u589e\u5927
C code
long long int leaf_example (\n long long int g, long long int h,\n long long int i, long long int j) {\n long long int f;\n f = (g + h) - (i + j);\n return f;\n}\n
Arguments g, \u2026, j
in x10, \u2026, x13
.
f
in x20
temporaries .
x5
, x6
.
Need to save x5
, x6
, x20
on stack.
Assembly
leaf_example:\n ;Save x5, x6, x20 on stack\n addi sp,sp,-24\n sd x5,16(sp)\n sd x6,8(sp)\n sd x20,0(sp)\n\n add x5,x10,x11 ;x5 = g + h\n add x6,x12,x1 ;x6 = i + j\n sub x20,x5,x6 ;f = x5 \u2013 x6\n addi x10,x20,0 ;copy f to return register\n\n ;Restore x5, x6, x20 from stack\n ld x20,0(sp)\n ld x6,8(sp)\n ld x5,16(sp)\n addi sp,sp,24\n jalr x0,0(x1)\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#non-leaf-procedures","title":"Non-Leaf Procedures","text":"Caller save\uff1a
Callee save: any S
registers used for local variables
x5 \u2013 x7, x28 \u2013 x31
: temporary registers. Not preserved by the callee.
x8 \u2013 x9, x18 \u2013 x27:
saved registers. If used, the callee saves and restores them.
long long fact ( long long n ) {\n if ( n < 1 ) return ( 1 ) ;\n else return ( n * fact ( n - 1 ) ) ;\n} \n
Main:\n....\n;n is reg x28\nadd x10,x0,x28\nJal fact\n\n;Nonleaf : save return address\nfact: addi sp, sp,-16 ;adjust stack for 2 items\n sd x1, 8(sp) ;save the return address\n sd x10, 0(sp) ;save the argument n\n addi x5, x10, -1 ;x5 = n - 1\n bge x5, x0, L1 ;if n >= 1, go to L1(else)\n addi x10, x0, 1 ;return 1 if n <1\n addi sp, sp, 16 ;Recover sp (Why not recover x1and x10 ?)\n jalr x0, 0(x1) ;return to caller\n\nL1: addi x10, x10, -1 ;n >= 1: argument gets ( n - 1 )\n jal x1, fact ;call fact with ( n - 1 )\n\n add x6, x10, x0 ;x6 = fact(n-1)\n ld x10, 0(sp) ;restore argument n\n ld x1, 8(sp) ;restore the return address\n addi sp, sp, 16 ;adjust stack pointer to pop 2 items\n\n mul x10, x10, x6 ;return n*fact ( n - 1 )\n jalr x0, 0(x1) ;return to the caller\n
X10-x17
returenjal
instruction is used to jump to the procedure and save the current PC+4 into the return address register X0
$a0-$a3
return values in $v0-$v1
\u6808\u8fd8\u9700\u8981\u5b58\u50a8\u4e00\u4e9b\u5c40\u90e8\u53d8\u91cf\uff0c\u4f8b\u5982\u5c40\u90e8\u6570\u7ec4\u6216\u7ed3\u6784, \u8fd9\u4e9b\u53d8\u91cf\u65f6\u5c40\u90e8\u4e8e\u8fc7\u7a0b\uff0c\u4e0d\u9002\u7528\u4e8e\u5bc4\u5b58\u5668\uff0c\u800c\u662f\u4fdd\u5b58\u5728\u6808\u4e2d
\u6808\u4e2d\u5305\u542b\u8fc7\u7a0b\u4fdd\u5b58\u7684\u5bc4\u5b58\u5668\u548c\u5c40\u90e8\u53d8\u91cf\u7684\u6bb5\u79f0\u4e3a\u8fc7\u7a0b\u5e27(frame)\u6216\u8005\u6d3b\u52a8\u8bb0\u5f55(activation record)
\u5728\u8fc7\u7a0b\u4e2d\u6808\u6307\u9488\u53ef\u80fd\u6539\u53d8\uff0c\u5982\u679c\u5229\u7528\u6808\u6307\u9488\u548c\u5730\u5740\u8fd0\u7b97\u6765\u5b8c\u6210\u5bf9\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u975e\u5e38\u4e0d\u65b9\u4fbf,\u56e0\u6b64\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5e27\u6307\u9488fp(x8)
\uff0c\u6307\u5411\u8fc7\u7a0b\u5e27\u7684\u7b2c\u4e00\u4e2a\u53cc\u5b57\uff0c\u8fd9\u6837\u5e27\u6307\u9488\u5c31\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7a33\u5b9a\u7684\u57fa\u5bc4\u5b58\u5668\uff0c\u5bf9\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\u5c31\u53d8\u5f97\u975e\u5e38\u7b80\u5355
\u5982\u679c\u8fc7\u7a0b\u4e2d\u6808\u5185\u6ca1\u6709\u5c40\u90e8\u53d8\u91cf\uff0c\u7f16\u8bd1\u5668\u4e00\u822c\u4e0d\u8bbe\u7f6e\u548c\u4e0d\u6062\u590d\u5e27\u6307\u9488\u4ee5\u8282\u7701\u65f6\u95f4,\u5f53\u4f7f\u7528\u5e27\u6307\u9488\u65f6\uff0c\u5728\u8c03\u7528\u4e2d\u4f7f\u7528sp\u6765\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4e14\u53ef\u4ee5\u4f7f\u7528fp
\u6062\u590d sp
.
\u6ce8\u610f, \u4e0d\u7ba1\u662f\u5426\u4f7f\u7528\u663e\u5f0f\u7684\u5e27\u6307\u9488\uff0c\u6808\u4e0a\u90fd\u4f1a\u663e\u793a\u4e00\u6761\u6d3b\u52a8\u8bb0\u5f55\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ef4\u62a4\u7a33\u5b9a\u7684sp\u6765\u51cf\u5c11\u5bf9fp\u7684\u4f7f\u7528
\u9664\u4e86\u52a8\u6001\u53d8\u91cf\uff0cC\u7a0b\u5e8f\u5458\u8fd8\u9700\u8981\u4e3a\u9759\u6001\u53d8\u91cf\u548c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u5206\u914d\u5185\u5b58\u7a7a\u95f4
\u8fd9\u4e2a\u56fe\u5c55\u793a\u4e86\u8fd0\u884clinux OS\u65f6\u7a0b\u5e8f\u548c\u6570\u636e\u5728RISC-V\u5185\u5b58\u5206\u914d\u7684\u7ea6\u5b9a,\u8fd9\u4e9b\u5730\u5740\u53ea\u662f\u4e00\u79cd\u8f6f\u4ef6\u89c4\u5b9a\uff0c\u5e76\u4e0d\u662fRISC-V\u4f53\u7cfb\u7ed3\u6784\u7684\u4e00\u90e8\u5206\u3002
\u6808\u7531\u5185\u5b58\u9ad8\u5730\u5740\u5f00\u59cb\uff0c\u5411\u4e0b\u589e\u957f:\u5185\u5b58\u4f4e\u5730\u5740\u6bb5\u5148\u662f\u4fdd\u7559\u533a\u57df\uff0c\u7136\u540e\u65f6\u673a\u5668\u4ee3\u7801\u7684\u5b58\u50a8\u533a\u57df\uff0c\u79f0\u4e3a\u6b63\u6587\u6bb5\uff0c\u4e4b\u4e0a\u662f\u9759\u6001\u6570\u636e\u6bb5\uff0c\u7528\u4e8e\u5b58\u50a8\u5e38\u91cf\u548c\u5176\u4ed6\u9759\u6001\u53d8\u91cf\u7684\u7a7a\u95f4\u3002\u7c7b\u4f3c\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\u901a\u5e38\u4f1a\u968f\u7740\u751f\u547d\u671f\u589e\u957f\u6216\u8005\u7f29\u77ed\uff0c\u8fd9\u7c7b\u6570\u636e\u7ed3\u6784\u5bf9\u5e94\u7684\u6bb5\u79f0\u4e3a\u5806(heap),\u5b83\u653e\u5728\u5185\u5b58\u4e2d,\u4f4d\u4e8e\u9759\u6001\u6570\u636e\u4e4b\u540e
\u6808\u548c\u5806\u76f8\u4e92\u589e\u957f\uff0c\u5728\u4e24\u4e2a\u6bb5\u6b64\u6d88\u5f7c\u957f\u7684\u8fc7\u7a0b\u4e2d\u8fbe\u5230\u5185\u5b58\u7684\u9ad8\u6548\u4f7f\u7528,C\u8bed\u8a00\u901a\u8fc7\u663e\u793a\u51fd\u6570malloc(), free()\u5206\u914d\u548c\u91ca\u653e\u7a7a\u95f4\uff0c\u5bb9\u6613\u5bfc\u81f4\u6f0f\u6d1e\u3002\u5fd8\u8bb0\u91ca\u653e\u4f1a\u5bfc\u81f4\u201c\u5185\u5b58\u6cc4\u6f0f\u201d\uff08\u6d88\u8017\u6389\u592a\u591a\u5185\u5b58\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u5d29\u6e83\uff09\uff0c\u8fc7\u65e9\u7684\u91ca\u653e\u4f1a\u5bfc\u81f4\u201c\u60ac\u7a7a\u6307\u9488\u201d\uff08\u6307\u9488\u6307\u5411\u7a0b\u5e8f\u4ece\u672a\u8bbf\u95ee\u7684\u4f4d\u7f6e\uff09
Jave\u4f7f\u7528\u81ea\u52a8\u5185\u5b58\u5206\u914d\u548c\u5783\u573e\u56de\u6536\u673a\u5236\u6765\u907f\u514d\u8fd9\u7c7b\u9519\u8bef
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#communication-with-people","title":"communication with people","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#bytehalfwordword-operations","title":"Byte/Halfword/Word Operations","text":"Load byte/halfword/word: Sign extend to 64 bits in rd \u2022 lb rd, offset(rs1) \u2022 lh rd, offset(rs1) \u2022 lw rd, offset(rs1) Load byte/halfword/word unsigned: Zero extend to 64 bits in rd \u2022 lbu rd, offset(rs1) \u2022 lhu rd, offset(rs1) \u2022 lwu rd, offset(rs1) Store byte/halfword/word: Store rightmost 8/16/32 bits \u2022 sb rs2, offset(rs1) \u2022 sh rs2, offset(rs1) \u2022 sw rs2, offset(rs1)
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#string","title":"String","text":"Three choices for representing a string * Place the length of the string in the first position * An accompanying variable has the length * A character in the last position to mark the end of a string
void strcpy ( char x[ ] , char y[ ] )\n {\n size_t i ;\n i = 0 ;\n while ( ( x[ i ] = y[ i ] ) != \u2018\\ 0\u2019 ) /* copy and test byte */\n i += 1 ;\n }\n
Assume : i--x19, x's base--x10, y's base--x11 strcpy :\n addi sp,sp,-8\n sd x19,0(sp)\n add x19,x0,x0\nL1:\n add x5,x11,x19 ;x5 = &y[i]\n lbu x6,0(x5) ;x6 = y[i]\n add x7,x10,x19 ;x7 = &x[i]\n sb x6,0(x7) ;x[i] = y[i]\n beq x6,x0,Exit ;if y[i] == 0, exit\n addi x19,x19,1 ;i += 1\n jal x0,L1\nExit:\n ld x19,0(sp)\n addi sp,sp,8\n jalr x0,0(x1)\n
For a leaf procedure * The compiler exhausts all temporary registers * Then use the registers it must save x5 \u2013 x7, x28 \u2013 x31: temporary registers : Not preserved by the callee
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#risc-v-addressing-for-wide-immediate-addresses","title":"RISC-V Addressing for Wide Immediate & Addresses","text":"For occasional 32-bit constant lui rd,constant
* copy the 20-bit constant to the upper 20 bits of rd * Extends the constant to 64 bits by setting the lower 12 bits to 0 Better: * lui rd,constant[31:12]
* ori rd,rd,constant[11:0]
More specifically beq x0,x0 Loop
OP + func3 \u2192 beq rs1 \u2192 x0 rs2 \u2192 x0 offset = -20 = 111111101100 \u622a\u65ad\u6700\u540e\u4e00\u4f4d\uff0c\u5f97\u5230 11111110110 im[12] + im[10:5] + im[4:1] + im[11] + 0 rd/offset -> im[4:1]+im[11] im[12] : sign bit
Offset > 12 bits ? Rewrite it to offer a much greater branching distance:
bne x10, x0, L2\njal x0, L1\nL2:...\n
JAL
instruction: 20-bit offset How to know the offset? Label Table
Object File: Linking Object modules Dynamic Linking Only link/load the needed library routines when it is called * Requires procedure code to be relocatable * Avoids image bloat caused by static linking of all (transitively) referenced libraries * Automatically picks up new library versions
Lazy Linkage Second time the link is called,much faster.
\u6267\u884c\u6587\u4ef6 * \u5728\u786c\u76d8\u4e0a\uff0c\u975e\u6267\u884c\u6001\uff1a\u5982\u75c5\u6bd2\u6837\u672c \u8fdb\u7a0b * \u8f6c\u8f7d\u5230\u5185\u5b58 * \u53ef\u4ee5\u7ec6\u5206\u4e3a\u591a\u4e2a\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u7684\u7ebf\u7a0b\uff1a\u5982\u6fc0\u6d3b\u6001\u75c5\u6bd2 * \u5982\u4f55\u770b\u7ebf\u7a0b: \u4efb\u52a1\u7ba1\u7406\u5668
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#example","title":"Example","text":"void swap ( long long v[ ] , size_t k )\n {\n long lon temp ;\n temp = v[ k ] ;\n v[ k ] = v[ k + 1 ] ;\n v[ k + 1 ] = temp ;\n }\n
swap:\n slli x6,x11,3 //x6 = k*8\n add x6,x10,x6 //x6 = &v[k]\n ld x5,0(x6) //x5 = v[k]\n ld x7,8(x6) //x7 = v[k+1]\n sd x7,0(x6) //v[k] = v[k+1]\n sd x5,8(x6) //v[k+1] = v[k]\n jalr x0,0(x1) //return\n
void sort (long long v[ ] , size_t n )\n {\n size_t i , j ;\n for ( i = 0 ; i < n ; i + = 1 ) {\n for ( j = i - 1 ; j >= 0 && v[j] > v[j+1] ; j -= 1 )\n swap ( v , j ) ;\n }\n }\n
sort:\n addi sp,sp,-40\n sd x1 ,32(sp)\n sd x22,24(sp)\n sd x21,16(sp)\n sd x20,8(sp)\n sd x19,0(sp)\nfor1tst:\n bge x19,x20,exit1\n ....\nexit2:\n addi x19,x19,1\n j for1tst\nexit1:\n ld x19,0(sp)\n ld x20,8(sp)\n ld x21,16(sp)\n ld x22,24(sp)\n ld x1,32(sp)\n addi sp,sp,40\n jalr x0,0(x1)\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/","title":"Chapter3","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#chap3arithmetic-for-computer","title":"Chap3.Arithmetic for computer","text":"Introduction"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#numbers","title":"Numbers","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#signed-and-unsigned-numbers-possible-representations","title":"Signed and Unsigned Numbers Possible Representations","text":"Example
To know a negative num's definite value : also invert and plus one
Biased notation
1000 0000
\\(=\\) minimal negative value(\\(-2^7\\)) 0111 1111
\\(=\\) maximal positive value (\\(2^7-1\\))Sign Extention lbu
vs. lb
Signed integer
slt
Set when less than
slti
Set when less than immediate
Unsigned integer
sltu
Set when less than
sltiu
Set when less than immediate
\"sltu\"\u548c\"sltiu\"\u90fd\u662fMIPS\u6c47\u7f16\u8bed\u8a00\u4e2d\u7684\u6307\u4ee4\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u533a\u522b\uff1a
sltu\uff1a
sltiu\uff1a
New RISC V instructions
lbu
load byte unsigned : Loads a byte into the lowest 8 bit of a register
Fills the remaining bits with 0
Lb
load byte (signed)Loads a byte into the lowest 8 bit of a register
Extends the highest bit into the remaining 24 bits
sltu
: set on less than unsignedslti
: set on less than immediatesltiu
: set on less than unsigned immediateLogical Operations
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#arithmetic","title":"Arithmetic","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#addition-subtraction","title":"Addition & subtraction","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#overflow","title":"Overflow","text":"^
Carry bit^
\\(Carry] and \\ Operation_{add\\ or\\ sub}\\)addadd immediate (addi)
subtract (sub)
add unsigned (addu)
add immediate unsigned (addiu)
Subtract unsigned (subu)
\\(S=X\\oplus Y\\\\ C=XY\\)
\\(Sum = X\\oplus Y\\oplus Carry_{in}\\)\u200b
\\(Carry_{out} = XY +(X+Y)Carry_{in}\\)\u200b
\\(X\\oplus Y\\) only different from \\(X+Y\\) when \\(XY=1\\)
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#overflow-detection","title":"Overflow Detection","text":"module alu(A, B, ALU_operation, res, zero, overflow );\n input [31:0] A, B;\n input [2:0] ALU_operation;\n output [31:0] res;\n output zero, overflow ;\n wire [31:0] res_and,res_or,res_add,res_sub,res_nor,res_slt;\n reg [31:0] res;\n parameter one = 32'h00000001, zero_0 = 32'h00000000;\n assign res_and = A&B;\n assign res_or = A|B;\n assign res_add = A+B;\n assign res_sub = A-B;\n assign res_slt =(A < B) ? one : zero_0;\n always @ (A or B or ALU_operation)\n case (ALU_operation)\n 3'b000: res=res_and; \n 3'b001: res=res_or; \n 3'b010: res=res_add; \n 3'b110: res=res_sub; \n 3'b100: res=~(A | B);\n 3'b111: res=res_slt;\n default: res=32'hx;\n endcase\n assign zero = (res==0)? 1: 0;\nendmodule\n
\\(P_i = A_i\\oplus B_i \\ \\ \\ \\ G_i = A_iB_i\\)\u200b
\\(S_i = P_i\\oplus C_i\\ \\ \\ \\ C_{i+1} = G_i + P_iC_i\\)\u200b\u200b
Improvement -- Reduce FAN-OUT"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#carry-skip-adder","title":"Carry Skip Adder","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#carry-select-adder-csa","title":"Carry Select Adder (CSA)","text":"
Already caclulate(parallel) different situations,once the \\(C_0\\)\u200b is delivered, the result can be output.
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#multiplexer","title":"Multiplexer","text":"Look at current bit position
Multiplexter V1
Multiplexter V2
Multiplexter V3
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#booths-algorithm","title":"Booth's Algorithm","text":"
Idea:
1s
subtract at first '1'
in multiplier'1s'
'1\u2018
Action
1
0
: subtract multiplicand from left1
1
no arithmetic operation0
1
add multiplicand to left half0
0
no arithmetic operation\\(Bit_{-1} = 0\\)
Arithmetic shift right:
4.1 \u5df2\u7ecf\u7ed3\u675f\u4e86\u9664\u6cd5\u64cd\u4f5c\uff0c\u6b64\u65f6\u7684\u9ad8\u4f4d\u5c31\u662f\u6211\u4eec\u7684\u4f59\u6570\uff0c\u4f46\u662f\u8fd9\u6700\u540e\u4e00\u6b21\u7684\u7ed3\u679c\u8fd8\u6ca1\u6709\u653e\u56de\u5230 Reminder \u4e2d\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u518d\u5f80\u5de6\u79fb\u4e00\u4f4d\u4e3a\u5546\u7559\u51fa\u7a7a\u95f4\uff0c\u653e\u5165\u540e\uff0c\u518d\u628a\u9ad8\u4f4d\u4f59\u6570\u5f80\u53f3\u79fb\u52a8\u4ee5\u62b5\u6d88\u5f71\u54cd
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#signed-division","title":"Signed division","text":"Standardized format IEEE 754
8 bit exp, 23 bit significant
11 bit exp, 52 bit significant
Leading '1' bit of significand is implicit
M: \u5c3e\u6570. \u5373\u9ed8\u8ba4.xxx
\u662f1.xxx
(\u56e0\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u6ca1\u67090.xxx
)
Exponent is biased(\u79fb\u7801)
Bias 127 for single precision
Bias 1023 for double precision
Have to be transfered back,but treated like unsigned inside
NOTE :\\((-1)^{sign} \u2022 (1 + significand) \u2022 2^{exponent - bias}\\)\u200b
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#limitations","title":"Limitations","text":"Overflow: &. Underflow
EXAMPLE"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#floating-point-addition","title":"Floating Point Addition","text":"
Algorithm
Add exponents - bias.
Multiply the significands.
Normalize.
Over- underflow.
Rounding.
Sign.
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#float-devision","title":"float devision","text":"
\u4e3a\u4e86\u4fdd\u8bc1\u56db\u820d\u4e94\u5165\u7684\u7cbe\u5ea6,\u7ed3\u679c\u6ca1\u6709,\u53ea\u5728\u8fd0\u7b97\u7684\u8fc7\u7a0b\u4e2d\u4fdd\u7559
EXAMPLE
A bit used in rounding in addition to guard and round that is set whenever there are nonzero bits to the right of the round bit.
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#round-modes","title":"Round Modes","text":"EXAMPLE"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#parallelism-and-computer-arithmetic-associativity","title":"Parallelism and Computer Arithmetic: Associativity","text":"
if \\(x + (y+ z) = (x + y) + z ?\\)
\u5df2\u77e5\\(F(n) = \u03a32^i = 2^{n+1} -1 = 111\u20261B\\)
int f1\uff08 unsigned n){ \n int sum = 1, power = 1;\n for ( unsigned i=0; i<=n-1; i++ ){ \n power * = 2;\n sum += power;\n }\n return sum;\n}\n
\u5047\u8bbeunsigned\u548cint\u578b\u6570\u636e\u90fd\u536032\u4f4d\uff0cfloat\u91c7\u7528IEEE 754\u5355\u7cbe\u5ea6\u6807\u51c6(IEEE 754\u91c7\u7528\u7684\u662f\u6700\u8fd1\u820d\u5165round to nearest
\u7684\u65b9\u5f0f) , \u56de\u7b54\u4e00\u4e0b\u95ee\u9898\uff1a
\u5f53n=0\u65f6\uff0c\\(f1\\)\u4f1a\u51fa\u73b0\u6b7b\u5faa\u73af\uff0c\u4e3a\u4ec0\u4e48\uff1f\u82e5\u5c06\\(f1\\)\u4e2d\u53d8\u91cf\\(i\\)\u548c\\(n\\)\u90fd\u5b9a\u4e49\u4e3aint\u578b\uff0c\u5219\\(f1\\)\u200b\u662f\u5426\u8fd8\u4f1a\u51fa\u73b0\u6b7b\u5faa\u73af\uff1f\u4e3a\u4ec0\u4e48\uff1f
unsigned 0 -1
-- \u5927\u6570\uff0ci<=n-1
\u6c38\u771f\uff0c\u6240\u4ee5\u6b7b\u5faa\u73af.
\u82e5i\u548cn\u6539\u4e3aint\u7c7b\u578b\uff0c\u4e0d\u4f1a\u6b7b\u5faa\u73af:n=0
\u65f6\uff0cn-1
\u7684\u503c\u4e3a-1
\uff0c\u6545i<=n-1
\u4e0d\u6210\u7acb\uff0c\u9000\u51fa\u5faa\u73af
f1(23)\u548cf2(23) \u7684\u8fd4\u56de\u503c\u662f\u5426\u76f8\u7b49\uff1f\u673a\u5668\u6570\u5404\u662f\u4ec0\u4e48\uff08\u7528\u5341\u516d\u8fdb\u5236\u8868\u793a)
Signle presion bias = 127
\\(f1(23)=00FFFFFFh\\)
\\(f2(23)=0_{sign}10010110_{exponent(bias)}....=0100\\ 1011\\ 0111\\ 1111\\ 1111\\ 1111\\ 1111\\ 1111=47BFFFFFH\\)
\\(f1(24)\\)\u548c\\(f2(24)\\)\u7684\u8fd4\u56de\u503c\u5206\u522b\u4e3a\\(33554431\\)\u548c\\(33554432.0\\)\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f8\u7b49\uff1f
\u5f53\\(n=24\\)\u65f6\uff0c\\(f(24)=11\u2026.1B\\),float\u53ea\u670924\u4f4d\u6709\u6548\u4f4d\uff0c\u820d\u5165\u540e\u6570\u503c\u589e\u5927\uff0c\u6240\u4ee5\\(f2(24)\\)\u6bd4\\(f1(24)\\)\u5927\\(1\\).
\\(f(31) = 2^{32} -1\\), \u800c\\(f1(31)\\)\u7684\u8fd4\u56de\u503c\u5374\u4e3a\\(-1\\)\uff0c\u4e3a\u4ec0\u4e48\uff1f\u82e5\u4f7f\\(f1(n)\\)\u7684\u8fd4\u56de\u503c\u4e0e\\(f(n)\\)\u200b\u76f8\u7b49\uff0c\u6700\u5927\u7684n\u662f\u591a\u5c11\uff1f
\\(F(31)\\)\u8d85\u51faint\u6570\u636e\u7684\u8868\u793a\u8303\u56f4\uff0c\u7528\\(f1(31)\\)\u5b9e\u73b0\u65f6\u5f97\u5230\u673a\u5668\u6570\u4e3a32\u4e2a1\uff0c\u4f5c\u4e3aint\u89e3\u91ca\u65f6\u5176\u503c\u4e3a-1
\u56e0\u4e3aint
\u6700\u5927\u53ef\u8868\u793a\u6570\u4e3a0\u540e\u8ddf31\u4e2a1\uff0c\u6240\u4ee5\\(f1(n)\\)\u7684\u8fd4\u56de\u503c\u4e0e\\(f(n)\\)\u76f8\u7b49\u7684\u6700\u5927\\(n\\)\u503c\u662f30\u3002
\\(f2(127)\\)\u7684\u673a\u5668\u6570\u4e3a\\(7F80 0000H\\)\uff0c\u5bf9\u5e94\u7684\u503c\u662f\u4ec0\u4e48\uff1f\u82e5\u4f7f\\(f2(n)\\)\u7684\u7ed3\u679c\u4e0d\u6ea2\u51fa\uff0c\u5219\u6700\u5927\u7684n\u662f\u591a\u5c11\uff1f\u82e5\u4f7f\\(f2(n)\\)\u7684\u7ed3\u679c\u7cbe\u786e\uff08\u65e0\u820d\u5165\uff09\uff0c\u5219\u6700\u5927\u7684\\(n\\)\u200b\u662f\u591a\u5c11\uff1f
\u9636\u7801\u51681,\u5c3e\u6570\u4e3a0
\u8868\u793a\u65e0\u7a77\u5927F2\u8fd4\u56de\u503c\u4e3afloat\uff0c\u673a\u5668\u6570\u4e3a\\(7F80 0000H\\)\u5bf9\u5e94\u7684\u503c\u662f\\(+\u221e\\)
n=126
\u65f6\uff0c\u5bf9\u5e94\u9636\u7801\u4e3a253\uff0c\u5c3e\u6570\u90e8\u5206\u820d\u5165\u540e\u9636\u7801\u52a01\uff0c\u6700\u7ec8\u9636\u7801\u4e3a254\uff0c\u4e0d\u6ea2\u51fa\u7684\u6700\u5927n\u503c\u4e3a126
.N=23
\u65f6\uff0cf(23)\u4e3a24\u4f4d1\uff0cfloat\u670924\u4f4d\u6709\u6548\u4f4d\uff0c\u6240\u4ee5\u4e0d\u9700\u820d\u5165\uff0c\u7ed3\u679c\u7cbe\u786e
\u6545\u4f7ff2\u83b7\u7cbe\u786e\u7ed3\u679c\u7684\u6700\u5927n\u503c\u4e3a 23.
In RISC-V: Supervisor Exception Program Counter (SEPC) (P316, 7th line)
In RISC-V: Supervisor Exception Cause Register (SCAUSE) 64 bits, but most bits unused
Assume at 0000 0000 1C09 0000hex Entry address in a special register :Suptervisor Trap Vector (STVEC), which can be loaded by OS.
Close interrupts * Read cause, and transfer to relevant handler * Determine action required * If restartable \u2013 Take corrective action \u2013 use SEPC to return to program (mret) * Otherwise \u2013 Terminate program \u2013 Report error using SEPC, SCAUSE, ... \u2013 OS make the choice to transfer to another ready process
"},{"location":"%E8%AE%A1%E7%BB%84/Chap4/lec/#csr","title":"CSR","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap4/lec/#8-important-csr-for-exception-handling","title":"8 important CSR for exception handling","text":"mtvec
* ( Machine Trap Vector): jump to this address when exception mepc
* (Machine Exception PC): the instruction raise the exception * When Interruption or Exception, the current PC will be saved in mepc
mcause
* (Machine Cause): the cause of the exception,which kindmie
* (Machine Interrupt Enable): enable or disable the interruptmip
* (Machine Interrupt Pending): the interrupt is pending or notmtval
* (Machine Trap Value): the value of the exception * error address , illegal instruction, or 0mscrach
* (Machine Scratch): a temporary registermstatus
* (Machine Status): the status of the machine * MPIE: Machine Previous Interrupt Enable Save the previous status of the interrupt * Exceptions \u7acb\u523b\u5904\u7406\uff0c\u6ca1\u6709exception \u624d\u8003\u8651 interrupts
mstatus.MIE = 1 && mie[i] = 1 && mip [i]= 1
CPI \u8868\u793a\u7684\u662f \"Clocks Per Instruction\"\uff0c\u5373\u6bcf\u6761\u6307\u4ee4\u7684\u65f6\u949f\u5468\u671f\u6570\u3002\u5b83\u662f\u8861\u91cf\u8ba1\u7b97\u673a\u5904\u7406\u5668\u6027\u80fd\u7684\u91cd\u8981\u6307\u6807\u4e4b\u4e00\u3002CPI \u6570\u503c\u8d8a\u4f4e\uff0c\u8868\u793a\u5904\u7406\u5668\u5728\u6267\u884c\u6307\u4ee4\u65f6\u9700\u8981\u7684\u65f6\u949f\u5468\u671f\u8d8a\u5c11\uff0c\u6027\u80fd\u8d8a\u597d\u3002
\u5176\u4e2d\uff0c\"Total Clock Cycles\" \u8868\u793a\u6267\u884c\u6240\u6709\u6307\u4ee4\u6240\u9700\u7684\u603b\u65f6\u949f\u5468\u671f\u6570\uff0c\"Total Instructions Executed\" \u8868\u793a\u6267\u884c\u7684\u603b\u6307\u4ee4\u6570\u3002
\u901a\u8fc7\u76d1\u6d4b\u548c\u4f18\u5316(\u51cf\u5c11) CPI\uff0c\u53ef\u4ee5\u6539\u5584\u5904\u7406\u5668\u7684\u6027\u80fd\uff0c\u4f8b\u5982\u51cf\u5c11\u6307\u4ee4\u6267\u884c\u65f6\u95f4\uff0c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\uff0c\u4ee5\u53ca\u964d\u4f4e\u80fd\u8017\u3002
"},{"location":"%E8%AE%A1%E7%BB%84/Intro/intro/#cpu-time","title":"CPU Time","text":"Clock cycle: This refers to the basic unit of time used by a computer's CPU (Central Processing Unit) in executing instructions. It's the time it takes for one complete pulse of the CPU's clock signal. All operations in a computer, such as fetching instructions, decoding them, executing them, and storing results, are synchronized by this clock signal. Clock cycles are typically measured in units like milliseconds (ms), microseconds (\u03bcs), or nanoseconds (ns), depending on the speed of the processor.
Clock rate: Also known as clock speed or clock frequency, this is the rate at which a CPU executes instructions, measured in cycles per second (Hz). For example, a CPU with a clock rate of 2 gigahertz (GHz) performs 2 billion clock cycles per second. Higher clock rates generally mean faster processing speeds, assuming other factors remain constant.
CPU time: This represents the total time taken by a CPU to complete a task or process. It's typically measured in seconds. CPU time is influenced by factors such as the clock rate, the number of clock cycles required to execute each instruction, and the total number of instructions executed.
Performance improved by * Reducing number of clock cycles * Increasing clock rate * Hardware designer must often trade off clock rate against cycle count
"}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Welcome to Lily's Notebook!","text":""},{"location":"#about-me","title":"About me","text":""},{"location":"#my-github","title":"My GitHub","text":""},{"location":"#my-gmail-and-my-zju-email","title":"My Gmail and My ZJU Email","text":""},{"location":"#_1","title":"Welcome to Lily's Notebook!","text":""},{"location":"invisible/","title":"Invisible","text":"\\(\\hspace{1cm}\\) \u4eca\u5929\u65e9\u4e0a\u8d77\u6765\uff0c\u5929\u7a7a\u7070\u9eef\u9eef\u7684\uff0c\u5f53\u7136\uff0c\u65f6\u65f6\u662f\u8fd9\u6837\uff0c\u4e5f\u6b63\u662f\u8fd9\u6837\uff0c\u624d\u65f6\u65f6\u662f\u5371\u9669\u7684\u3002
\\(\\hspace{1cm}\\) \u6211\u68b3\u4e86\u4e00\u4e2a\u9a6c\u5c3e\u8fab\u3002
\\(\\hspace{1cm}\\) \u6253\u5f00\u7535\u89c6\uff0c\u55e1\u55e1\u7684\u58f0\u97f3\u50cf\u662f\u4e00\u4e0b\u4ece\u8fdc\u65b9\u62bd\u5438\u8fdb\u6765\uff0c\u6a21\u7cca\u51fa\u4e00\u7247\u6e05\u6f88\u3002\u201c\u6839\u636e\u8fd1\u4e9b\u5e74\u6765\u79d1\u5b66\u5bb6\u7814\u7a76\u7edf\u8ba1\uff0c\u5168\u7403\u660e\u7ea4\u5269\u4f59\u91cf\u5927\u7ea6\u4e3a0.03\u6839/\u5e73\u65b9\u5343\u7c73\uff0c\u5168\u7403\u5404\u56fd\u5fd7\u613f\u8005\u4e5f\u90fd\u5728\u4e3a\u626b\u9664\u660e\u7ea4\u5949\u732e\u7740\u9c9c\u8840\u548c\u6c57\u6c34\uff0c\u636e\u4f30\u8ba1\uff0c\u4ee5\u76ee\u524d\u8fdb\u7a0b\u5168\u7403\u57fa\u672c\u4e0a\u53ef\u4ee5\u5728X\u5e74\u57fa\u672c\u5f7b\u5e95\u6e05\u9664\u660e\u7ea4\u3002\u7136\u800c\u4e13\u5bb6\u4ecd\u7136\u5efa\u8bae\u5e7f\u5927\u7fa4\u4f17\u5728\u6b63\u5e38\u751f\u6d3b\u4e4b\u4f59\u505a\u597d\u660e\u7ea4\u65e5\u5e38\u9632\u62a4\u3002\u4e0b\u9762\u64ad\u653e\u51e0\u4e2a\u660e\u7ea4\u9632\u62a4\u65e5\u5e38\u8d34\u58eb\uff1a1.\u65e5\u5e38\u5e94\u8be5\u4fdd\u6301\u653e\u677e\u5fc3\u6001\uff0c\u4e0d\u8981\u8fc7\u4e8e\u7126\u8651\u660e\u7ea4\u95ee\u9898\u2026\u2026\u201d
\\(\\hspace{1cm}\\) \u8001\u751f\u5e38\u8c08\uff0c\u6211\u6362\u4e86\u53f0\u3002
\\(\\hspace{1cm}\\) \u201c\u636e\u672c\u53f0\u8bb0\u8005\u62a5\u9053\uff0c\u672c\u5e02\u4e8e\u6628\u65e5\u53d1\u751f\u4e00\u8d77\u2018\u62a2\u53d1\u6848\u2019 \uff0c\u53d7\u5bb3\u4ebaX\u5148\u751f\u88ab\u952f\u65ad\u8fd120cm\u5934\u53d1\uff0c\u5e02\u503c\u4f30\u8ba1\u572820000\u5143\u5de6\u53f3\uff0c\u73b0\u6b63\u7d27\u6025\u8c03\u67e5\u6d89\u5acc\u4eba\u5458\uff0c\u8bf7\u5e7f\u5927\u5e02\u6c11\u6ce8\u610f\u597d\u81ea\u8eab\u5b89\u5168\u4fdd\u62a4\u2026\u2026\u201d
\\(\\hspace{1cm}\\) \u6211\u4e0d\u7531\u5f97\u6478\u4e86\u6478\u81ea\u5df1\u7684\u5934\u53d1\uff1a\u6211\u5f88\u559c\u6b22\u6211\u7684\u5934\u53d1\uff0c\u9ed1\u9ed1\u7684\uff0c\u4eae\u4eae\u7684\uff0c\u67d4\u987a\u7684\uff0c\u5e02\u4ef7\u5e94\u8be5\u5f88\u9ad8\uff0c\u6240\u4ee5\u6211\u4e00\u76f4\u6ca1\u6709\u5f88\u52aa\u529b\u5de5\u4f5c\uff0c\u5b9e\u5728\u4e0d\u884c\u4e86\uff0c\u6211\u5c31\u53bb\u628a\u5934\u53d1\u5356\u4e86\uff0c\u6211\u4e5f\u4e0d\u6015\u6b7b\uff0c\u4f46\u6211\u70ed\u7231\u751f\u6d3b\u3002
\\(\\hspace{1cm}\\) \u81f3\u4e8e\u4e3a\u4ec0\u4e48\u73b0\u5728\u5934\u53d1\uff0c\u6216\u8005\u8bf4\u9ad8\u8d28\u91cf\u5047\u53d1\uff0c\u7adf\u7136\u4ef7\u683c\u5982\u6b64\u9ad8\u6602\uff0c\u662f\u4ece\u660e\u7ea4\u7684\u53d1\u73b0\u5f00\u59cb\u7684\u3002\u5927\u6982\u5728\u5341\u51e0\u5e74\u524d\uff0c\u5728Stephen\u5c0f\u9547\uff0c\u9065\u8fdc\u7684\u5317\u6b27\uff0c\u5728\u90a3\u7247\u5b81\u9759\uff0c\u6f2b\u957f\u4e0e\u5bd2\u51b7\u4e4b\u95f4\uff0c\u7a81\u7136\u6709\u4e86\u70ed\u8840\u6cb8\u817e\uff1a\u4e00\u4e2a\u4e0b\u5348\uff0c\u9633\u5149\u80fd\u6620\u51fa\u7a7a\u4e2d\u7684\u9648\u7c92\uff1b\u8857\u4e0a\uff0c\u884c\u4eba\u4e09\u4e24\uff1b\u7a81\u7136\u5c31\u6709\u4e86\u4e00\u9053\u8840\u5149\u3002\u5f53\u65f6\u8def\u4eba\u5927\u6982\u90fd\u60ca\u5446\u4e86\uff0c\u51bb\u5728\u90a3\u7247\u5bd2\u51b7\u7684\u8840\u8272\u91cc\u3002\u662f\u4e00\u4e2a\u884c\u4eba\uff0c\u4e00\u4e2a\u8001\u7237\u7237\uff0c\u5a46\u5a11\u7740\u80cc\uff0c\u6162\u6162\u5730\u8d70\u7740\uff0c\u7136\u540e\u7a81\u7136\u62e6\u8170\u65ad\u4e86\uff0c\u5012\u4e0b\uff0c\u5929\u8fb9\u4e00\u7247\u7ea2\u3002\u5728\u4e00\u6bb5\u53ef\u6016\u7684\u9759\u6b62\u548c\u6c89\u9ed8\u4e4b\u540e\uff0c\u6709\u4eba\u4e0a\u524d\u67e5\u770b\uff0c\u53c8\u5012\u4e0b\u4e86\u3002
\\(\\hspace{1cm}\\) \u5bc2\u9759\u4e4b\u58f0\u3002
\\(\\hspace{1cm}\\) \u540e\u6765\u7684\u540e\u6765\uff0c\u7ec8\u4e8e\u6709\u4eba\u53d1\u73b0\uff0c\u5728\u90a3\u5929\u707f\u70c2\u7684\u9633\u5149\u4e0b\uff0c\u90a3\u91cc\u98d8\u7740\u4e00\u6bb5\u5c0f\u5c0f\u7684\u7ea4\u7ef4\u72b6\u7684\u7ec6\u4e1d\uff0c\u5f88\u7ec6\uff0c\u80fd\u53d1\u73b0\u53ea\u662f\u56e0\u4e3a\u5728\u9633\u5149\u4e0b\u90a3\u7ec6\u4e1d\u6709\u4e9b\u5fae\u7684\u95ea\u70c1\u53d1\u5149,\u6240\u4ee5\uff0c\u5c31\u53eb\u505a\u660e\u7ea4\uff0c\u591a\u7f8e\u4e3d\u7684\u540d\u5b57\uff0c\u7ea4\u7ec6\u800c\u8106\u5f31\u3002
\\(\\hspace{1cm}\\) \u6ca1\u6709\u4eba\u77e5\u9053\u660e\u7ea4\u662f\u54ea\u91cc\u6765\u7684\uff0c\u6709\u4eba\u8bf4\u662f\u4e00\u6b21\u9ad8\u7ea7\u6b66\u5668\u7684\u6ee5\u7528\uff0c\u67d0\u79cd\u5316\u5b66\u7269\u8d28\uff1b\u4e5f\u6709\u4eba\u8bf4\u662f\u67d0\u4e2a\u6050\u6016\u7ec4\u7ec7\u5455\u5c3d\u5fc3\u8840\u5728\u5168\u7403\u5e03\u7f6e\u4e0b\u51e0\u5904\u4ee5\u5f15\u8d77\u6050\u614c\u3002\u4e0d\u7ba1\u600e\u4e48\u6837\uff0c\u6050\u614c\u662f\u6709\u7684\uff1a\u636e\u76ee\u524d\u6240\u77e5\uff0c\u660e\u7ea4\u5206\u5e03\u4e0d\u5e7f\uff0c\u4f46\u6740\u4f24\u529b\u6781\u5927\u3002\u53d1\u73b0\u660e\u7ea4\u540e\uff0c\u4e5f\u53ea\u80fd\u5efa\u7acb\u7981\u884c\u533a\uff0c\u800c\u505a\u4e0d\u5230\u5f7b\u5e95\u7684\u201c\u6e05\u695a\u201d\u2014\u2014\u4e0d\u8fc7\u7528\u4ec0\u4e48\u60f3\u8981\u6e05\u695a\u660e\u7ea4\uff0c\u660e\u7ea4\u603b\u80fd\u5207\u65ad\uff0c\u4eba\u6216\u8005\u91d1\u5c5e\u3002
\\(\\hspace{1cm}\\) \u636e\u8bf4\u88ab\u660e\u7ea4\u5207\u65ad\u7684\u4e00\u77ac\u95f4\u5e76\u6ca1\u6709\u4ec0\u4e48\u5177\u4f53\u611f\u53d7\uff0c\u5176\u5b9e\u8fd9\u662f\u4e00\u79cd\u63a8\u65ad\uff0c\u56e0\u4e3a\u5f88\u591a\u884c\u8fdb\u5f88\u6162\u7684\u4eba\uff0c\u5c31\u50cf\u7b2c\u4e00\u4f8b\u53d7\u5bb3\u8005\u8001\u7237\u7237\uff0c\u90fd\u6b7b\u4e8e\u8eaf\u4f53\u7684\u5b8c\u5168\u5206\u9694\u3002
\\(\\hspace{1cm}\\) \u603b\u4e4b\uff0c\u5728\u90a3\u4e4b\u540e\uff0c\u4eba\u4eec\u90fd\u6709\u4e9b\u4e0d\u6562\u51fa\u95e8\u4e86\uff0c\u5728\u8857\u4e0a\u8d70\u4e5f\u5f88\u6162\u3002
\\(\\hspace{1cm}\\) \u51e0\u5e74\u524d\u6709\u4e00\u4e2a\u5e78\u8fd0\u7684\u5973\u5b69\u5b50\u9047\u5230\u660e\u7ea4\u5374\u6beb\u53d1\u672a\u635f\u2014\u2014\u786e\u5207\u7684\u8bf4\uff0c\u53ea\u635f\u6beb\u53d1\uff1a\u5973\u5b69\u8d70\u8def\u65f6\u5f88\u7231\u7529\u5934\u53d1\uff0c\u53d1\u95f4\u5411\u5de6\u6643\u65f6\u4e00\u4e0b\u88ab\u5207\u65ad\u4e86\u4e00\u622a\uff0c\u98d8\u98d8\u6d12\u6d12\u3002\u540e\u6765\uff0c\u7537\u5b50\u4e5f\u5f00\u59cb\u84c4\u957f\u53d1\uff0c\u4f20\u8bf4\u4e2d\u6f02\u6d6e\u5ea6\u8303\u56f4\u5f88\u5e7f\u7684\u9ad8\u8d28\u91cf\u5047\u53d1\u4e5f\u5f00\u59cb\u5728\u5e02\u573a\u4e0a\u5d1b\u8d77\u4e86\u3002
\\(\\hspace{1cm}\\) \u6211\u4e5f\u559c\u6b22\u8d70\u8def\u7684\u65f6\u5019\u9ad8\u9ad8\u5730\u7529\u5934\u53d1\uff0c\u6211\u89c9\u5f97\u771f\u5f88\u7f8e\uff0c\u5973\u5b69\u5b50\u7684\u9752\u6625\u7684\u7f8e\uff0c\u4e0d\u8fc7\uff0c\u4e5f\u8bb8\u662f\u56e0\u4e3a\u6211\u5728\u5fc3\u91cc\u7684\u4e00\u89d2\u6697\u81ea\u745f\u7f29\u5427\u3002
\\(\\hspace{1cm}\\) \u601d\u7eea\u53c8\u548c\u7535\u89c6\u7684\u58f0\u97f3\u76f8\u9047\u4e86\u3002
\\(\\hspace{1cm}\\) \u201c\u4e0b\u9762\u672c\u53f0\u4e3a\u60a8\u5e26\u6765\u5fd7\u613f\u6e05\u626b\u660e\u7ea4\u73b0\u573a\uff0c\u8ba9\u6211\u4eec\u4e3a\u5728\u573a\u7684\u6bcf\u4e00\u4f4d\u82f1\u96c4\u9f13\u638c\u559d\u5f69\uff01\u201d\u955c\u5934\u8f6c\u6362\uff0c\u4eba\u7fa4\u7199\u6518\uff0c\u6211\u770b\u89c1\u4e86\u5e74\u8f7b\u7684\u7537\u5973\uff0c\u4e2d\u5e74\u7684\u76b1\u7eb9\uff0c\u82b1\u767d\u7684\u5934\u53d1\uff0c\u4e73\u7259\uff0c\u9152\u7a9d\uff0c\u8033\u6d1e\u2026\u2026\u6240\u6709\u7684\u4e00\u5207\u90fd\u5728\u70ed\u70c8\u7684\u6c14\u6c1b\u4e2d\u6a21\u7cca\u6b86\u5c3d\uff0c\u8fd8\u6709\u9065\u9065\u7684\u70ed\u8840\u97f3\u4e50\u5728\u7a7a\u6c14\u4e2d\u6643\u8361\u3002
\\(\\hspace{1cm}\\) \u5927\u6982\u662f\u5ba3\u5e03\u4e86\u5f00\u59cb\uff0c\u4eba\u7fa4\u52a8\u8d77\u6765\u4e86\uff0c\u8dd1\u8d77\u6765\u4e86\uff0c\u5374\u4e00\u76f4\u662f\u62e5\u6324\u7684\u53c8\u6a21\u7cca\u8d77\u6765\u4e86\u3002\u6211\u77e5\u9053\u4ed6\u4eec\u4e3a\u4ec0\u4e48\u5954\u8dd1\uff0c\u56e0\u4e3a\u636e\u8bf4\u5feb\u901f\u901a\u8fc7\u660e\u7ea4\u800c\u6b7b\u4ea1\u6ca1\u6709\u4efb\u4f55\u75db\u82e6\u3002\u6211\u4e5f\u77e5\u9053\u4ed6\u4eec\u4e3a\u4ec0\u4e48\u62e5\u6324\uff0c\u56e0\u4e3a\u53ea\u6709\u62e5\u5728\u4e00\u8d77\uff0c\u624d\u80fd\u4fdd\u8bc1\u7ecf\u8fc7\u7684\u6bcf\u4e00\u5904\u5730\u65b9\u90fd\u6ca1\u6709\u660e\u7ea4\u9003\u8fc7\u3002
\\(\\hspace{1cm}\\) \u6211\u5374\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u662f\u4eba\u3002\u4e3a\u4ec0\u4e48\u4e0d\u662f\u8f66\u6216\u8005\u673a\u5668\u6216\u8005\u6280\u672f\u2026\u2026\u4e5f\u6709\u5f88\u591a\u8bf4\u6cd5\uff0c\u6709\u4eba\u8bf4\u660e\u7ea4\u5206\u5e03\u5b9e\u5728\u7a00\u5c11\u4e0d\u5fc5\u4e3a\u6b64\u6295\u5165\u592a\u591a\u7cbe\u529b\uff0c\u786e\u5b9e\uff0c\u6bcf\u5e74\u636e\u62a5\u9053\u5927\u6982\u53ea\u67092-3\u4f8b\u53d7\u5bb3\u8005\u3002\u4e5f\u6709\u4eba\u8bf4\u8fd9\u6837\u7684\u6d3b\u52a8\u4ece\u6765\u6ca1\u6709\u8fc7\u727a\u7272\uff0c\u8fd8\u53ef\u4ee5\u57f9\u517b\u7fa4\u4f17\u7684\u56e2\u7ed3\u5408\u4f5c\u80fd\u529b\uff0c\u8fd8\u6709\u4eba\u8bf4\u8fd9\u662f\u4e2a\u9634\u8c0b\u8bba\u3002
\\(\\hspace{1cm}\\) \u8fd8\u662f\u4e0d\u7ba1\u600e\u6837\uff0c\u603b\u6709\u5730\u65b9\u5728\u62db\u52df\u5fd7\u613f\u8005\uff0c\u4e5f\u603b\u6709\u8fd9\u6837\u7684\u5e74\u8f7b\u7f8e\u4e3d\uff0c\u70ed\u8840\u6f8e\u6e43\u7684\u5fd7\u613f\u8005\u81ea\u544a\u594b\u52c7\u3002
\\(\\hspace{1cm}\\) \u4e00\u9635\u6c89\u90c1\u7ffb\u6d8c\uff0c\u6211\u5173\u4e0a\u4e86\u7535\u89c6\u3002
\\(\\hspace{1cm}\\) \u6211\u6700\u6700\u4eb2\u7231\u7684\u8bfb\u8005\uff0c\u6211\u591a\u4e48\u96be\u5e0c\u671b\u548c\u4f60\u4eec\u4e00\u6837\u628a\u8fd9\u5f53\u6210\u4e00\u4e2a\u8981\u7231\u62a4\u5934\u53d1\u7684\u5c0f\u6545\u4e8b\uff0c\u53ef\u662f\u6211\u4e0d\u884c\uff0c\u6211\u9762\u5bf9\u7684\u4e0d\u662f\u865a\u7a7a\uff0c\u662f\u771f\u5b9e\u3002
\\(\\hspace{1cm}\\) \u6574\u7406\u4e86\u4e0b\u5934\u53d1\uff0c\u6211\u51fa\u4e86\u5bb6\u95e8\uff0c\u62ac\u5934\uff0c\u53ea\u770b\u89c1\u90a3\u6c89\u6c89\u7684\u5929\u5728\u884c\u8272\u5306\u5306\u4e2d\u9759\u9ed8\u4e0d\u8a00\u3002
"},{"location":"%E5%A4%A7%E4%BE%A0/","title":"\u5927\u4fa0","text":"\\(\\hspace{1cm}\\)\u5f88\u5c0f\u7684\u65f6\u5019\uff0c\u6211\u5c31\u77e5\u9053\uff0c\u6211\u4ee5\u540e\u8981\u505a\u4e00\u4ee3\u5927\u4fa0\uff0c\u884c\u8d70\u7eff\u6797\uff0c\u6d6a\u8361\u6c5f\u6e56\uff0c\u4e00\u8eab\u5251\u6c14\uff0c\u4e00\u8def\u6f47\u6d12\u3002
\\(\\hspace{1cm}\\)\u6c5f\u6e56\u4e0a\u7684\u4e8b\u60c5\u6211\u4eec\u5927\u591a\u90fd\u6709\u8033\u95fb\uff1a\u8981\u6709\u6b66\u529f\uff0c\u8981\u6709\u809d\u80c6\uff0c\u8981\u4e00\u8eab\u6e05\u98ce\u6717\u6717\uff0c\u6063\u610f\u5e73\u751f\uff0c\u4e0e\u4e16\u5043\u4ef0\u3002
\\(\\hspace{1cm}\\)\u4e8e\u662f\u6211\u4e0d\u65ad\u5730\u52aa\u529b\u7740\uff1a\u8d70\u5c71\u8d70\u6c34\uff0c\u95ef\u8361\u5929\u5730\uff0c\u56db\u5904\u8bbf\u5e08\uff0c\u56db\u5904\u5b66\u827a\u3002\u6768\u8fc7\u5927\u4fa0\u90a3\u6bb5\u4e0e\u6df1\u5c71\u4e3a\u4f34\u7684\u5c81\u6708\uff0c\u6211\u4e5f\u66fe\u76f8\u4f3c\u4eb2\u5386\u3002\u6211\u4ee5\u4e3a\u6211\u5c1d\u5c3d\u4e86\u6210\u957f\u4e3a\u4e00\u4ee3\u5927\u4fa0\u8fc7\u7a0b\u4e2d\u5e94\u6709\u7684\uff0c\u6240\u6709\u7684\u54c0\u621a\u4e0e\u5e78\u798f\uff0c\u6211\u4e3a\u6211\u7684\u4e00\u5207\u52aa\u529b\u800c\u611f\u5230\u5feb\u4e50\u4e0e\u6210\u5c31\u3002
\\(\\hspace{1cm}\\)\u6bcf\u6b21\u6e38\u5386\u56de\u5bb6\u65f6\uff0c\u5988\u5988\u5e38\u7528\u7565\u5e26\u5fe7\u4f24\u7684\u773c\u795e\u770b\u7740\u6211\uff0c\u6211\u77e5\u9053\uff0c\u5979\u662f\u592a\u62c5\u5fc3\u6211\uff0c\u5bb3\u6015\u6c5f\u6e56\u4e0a\u7684\u9ed1\u6697\u4f1a\u5c06\u6211\u541e\u6ca1\uff0c\u4e0d\u5269\u9aa8\u5934\uff1b\u6211\u4e5f\u77e5\u9053\uff0c\u5979\u4ecd\u662f\u65e5\u65e5\u591c\u591c\u76fc\u671b\u6211\u6210\u529f\uff0c\u5e26\u7740\u8346\u68d8\u7684\u5fc3\u548c\u96be\u8a00\u7684\u60c6\u6005\u3002\u603b\u5f97\u6765\u8bf4\uff0c\u5988\u5988\u662f\u5f88\u652f\u6301\u6211\u7684\uff0c\u5c31\u50cf\u7ae5\u5e74\u7684\u7761\u524d\u6545\u4e8b\u2014\u2014\u90a3\u4e9b\u6ee1\u662f\u4fa0\u8005\u98ce\u8303\u7684\u5feb\u610f\u6c5f\u6e56\u3002
\\(\\hspace{1cm}\\)\u90a3\u5929\uff0c\u6211\u6210\u5e74\u4e86\uff0c\u88f9\u4e86\u4e00\u4e2a\u5c0f\u5305\u88b1\uff1a\u4e00\u4e2a\u5988\u5988\u7ee3\u7684\u8377\u5305\uff0c\u51e0\u4e24\u94f6\u94b1\uff0c\u968f\u8eab\u8863\u7269\u3002\u7136\u540e\u6211\u5c31\u4e0a\u8def\u4e86\u2014\u2014\u6211\u51b3\u5b9a\u8981\u5f00\u542f\u81ea\u5df1\u7684\u4eba\u751f\uff1a\u5927\u4fa0\u7684\u4eba\u751f\u3002
\\(\\hspace{1cm}\\)\u7b2c\u4e00\u5929\uff0c\u6211\u6551\u4e86\u4e00\u4e2a\u843d\u96be\u7684\u5973\u5b50\u3002\u5979\u8877\u5fc3\u5730\u611f\u8c22\u6211\uff0c\u7136\u540e\u53c8\u5e26\u7740\u5fe7\u4f24\u5730\uff0c\u621a\u621a\u5730\u770b\u7740\u6211\uff0c\u55eb\u5685\u7740\uff1a\u53ef\u662f\u4f60\u5e2e\u4e0d\u4e86\u6211\u2014\u2014\u90a3\u5fe7\u4f24\u548c\u5988\u5988\u7684\u5fe7\u4f24\u6709\u4e9b\u76f8\u4f3c\u3002\u6211\u628a\u968f\u8eab\u7684\u94f6\u4e24\u90fd\u585e\u7ed9\u4e86\u5979\uff0c\u5979\u6ca1\u6709\u6536\uff0c\u8885\u5a1c\u7740\u8d70\u4e86\u3002
\\(\\hspace{1cm}\\)\u4e00\u4e9b\u8ff7\u832b\uff0c\u4e00\u4e9b\u611f\u52a8\uff0c\u548c\u4e00\u4e9b\u5fe7\u6101\u3002
\\(\\hspace{1cm}\\)\u6211\u6ca1\u6709\u6e38\u79fb\u3002\u6211\u8fd8\u662f\u884c\u8d70\u7740\uff0c\u53c8\u89c1\u4e86\u4e00\u4e9b\u4e8b\uff0c\u6551\u4e86\u4e00\u4e9b\u4eba\uff0c\u8fd8\u662f\u8d70\u8fc7\u5c71\u5c71\u6c34\u6c34\u3002\u4ed6\u4eec\u90fd\u53d8\u6210\u4e86\u6211\u3002
\\(\\hspace{1cm}\\)\u90a3\u5929\u6211\u4e8c\u5341\u4e94\u5c81\uff0c\u6211\u7ec8\u4e8e\u6765\u5230\u4e86\u6211\u4ece\u5c0f\u68a6\u91cc\u7684\u6bbf\u5802\uff1a\u4fa0\u4e4b\u95e8\u3002\u6211\u77e5\u9053\u6211\u4e0d\u662f\u540d\u95e8\u6b63\u6d3e\uff0c\u4f46\u6211\u60f3\uff0c\u7ecf\u5386\u4e86\u8fd9\u8bb8\u591a\uff0c\u6211\u4e00\u5b9a\u4f1a\u88ab\u8ba4\u53ef\u2014\u2014\u6b66\u529f\u4e5f\u597d\uff0c\u809d\u80c6\u4e5f\u7f62\uff0c\u4e0d\u81f3\u4e8e\u540d\u5782\u9752\u53f2\uff0c\u81f3\u5c11\u8db3\u591f\u6177\u6168\u3002
\\(\\hspace{1cm}\\)\u6211\u8fdb\u4e86\u95e8\uff0c\u81ea\u62a5\u5bb6\u95e8\uff0c\u5b88\u95e8\u4eba\u5f15\u6211\u5411\u524d\uff0c\u5230\u4e86\u4e00\u5f20\u53e4\u6734\u7684\u684c\u5b50\uff0c\u684c\u524d\u5750\u7740\u4e00\u4f4d\u5782\u5782\u8001\u8005\u3002\u6211\u8840\u8109\u55b7\u6d8c\u2014\u2014\u50cf\u662f\u68a6\u91cc\u3002
\\(\\hspace{1cm}\\)\u201c\u8981\u6210\u4e3a\u5927\u4fa0\uff1f\u201d
\\(\\hspace{1cm}\\)\u201c\u662f\u7684\uff0c\u662f\u7684\u3002\u201d
\\(\\hspace{1cm}\\)\u201c\u591a\u5927\uff1f\u201d
\\(\\hspace{1cm}\\)\u201c\u4e8c\u5341\u4e94\u4e86\u3002\u201d
\\(\\hspace{1cm}\\)\u6cdb\u9ec4\u7684\u7eb8\u9875\uff0c\u76d6\u4e86\u4e2a\u9c9c\u7ea2\u7684\u5370\u7ae0\uff0c\u5199\u7740\u4e9b\u4ec0\u4e48\u4fa0\u4e4b\u5927\u8005\uff0c\u63a8\u5411\u6211\u3002
\\(\\hspace{1cm}\\)\u201c\u8d44\u683c\u8bc1\uff0c\u5927\u4fa0\u3002\u201d
\\(\\hspace{1cm}\\)\u4e8e\u662f\uff0c\u6211\u591a\u4e86\u4e00\u7eb8\u8584\u8584\u7684\u4e66\u9875\uff0c\u8d70\u51fa\u4e86\u5c71\u95e8\u3002
\\(\\hspace{1cm}\\)\u6211\u7a81\u7136\u60f3\u8d77\u5f88\u591a\u5e74\u524d\uff0c\u5728\u5988\u5988\u8fd8\u5728\u4e3a\u6211\u8bb2\u7761\u524d\u6545\u4e8b\u7684\u65f6\u5019\uff1a\u6211\u5e74\u7eaa\u5c0f\uff0c\u603b\u662f\u542c\u5230\u4e00\u534a\u5c31\u660f\u660f\u5165\u7761\uff0c\u4f46\u5988\u5988\u603b\u4f1a\u8bb2\u5b8c\u5979\u7684\u6545\u4e8b\u3002
\\(\\hspace{1cm}\\)\u6211\u60f3\u8d77\u591a\u5e74\u524d\u7684\u90a3\u4e2a\u591c\u665a\uff0c\u661f\u661f\u5929\u771f\u5730\u5728\u591c\u7a7a\u6d82\u9e26\uff0c\u5988\u5988\u770b\u7740\u6211\u7761\u773c\u60fa\u5fea\uff0c\u8f7b\u58f0\u5730\u8bb2\u7740\u6545\u4e8b\u7684\u7ed3\u5c3e\uff1a\u4ed6\u6700\u7ec8\u6ca1\u80fd\u6210\u4e3a\u5927\u4fa0\uff0c\u4f46\u4ed6\u52c7\u6562\uff0c\u575a\u5b9a\uff0c\u5766\u8361\u800c\u6f84\u6f88\uff0c\u4ed6\u770b\u8fc7\u5c71\u7684\u9ad8\u5927\uff0c\u770b\u8fc7\u6d77\u7684\u8fbd\u9614\uff0c\u4eba\u4eec\u8bf4\u4ed6\u4e00\u4e8b\u65e0\u6210\uff0c\u53ea\u662f\u6d6a\u8361\u6c5f\u6e56\uff0c\u4ed6\u4e0d\u540e\u6094\u3002
"},{"location":"ADS/D%26C/lec/","title":"Divide and Conquer","text":""},{"location":"ADS/D%26C/lec/#divide-and-conquer","title":"Divide and Conquer","text":"
Main Idea
General recurrence: \\(T(N) = aT(N/b) + f(N)\\)
"},{"location":"ADS/D%26C/lec/#example","title":"Example","text":""},{"location":"ADS/D%26C/lec/#maximum-subsequence-sum-problem","title":"Maximum Subsequence Sum Problem","text":"static int MaxSubSum(const int A[],int left,int right){\n int MaxLeftSum, MaxRightSum;\n int MaxLeftBorderSum, MaxRightBorderSum; \n int LeftBorderSum, RightBorderSum;\n int Center, i;\n if( left == right ){\n if(A[left]>0){return A[left];}\n else{return 0;}\n }\n Center = ( Left +Right ) / 2;\n MaxLeftSum = MaxSubSum( A, Left, Center ) \n MaxRightSum = MaxSubSum( A, Center + 1, Right );\n MaxLeftBorderSum = 0; LeftBorderSum = 0;\n for(i=Center+1;i>=left;i--){\n LeftBorderSum += A[i];\n if(leftBorderSum>MaxleftBorderSum){\n MaxleftBorderSum = LeftBorderSum;\n }\n }\n MaxRightBorderSum = 0; \n RightBorderSum = 0;\n for(i=Center;i<=Right;i++){\n RightBorderSum += A[i];\n if(RightBorderSum > MaxRightBorderSum){\n MaxRightBOrderSum = RightBorderSum\n }\n }\n return Max(MaxLeftSum,MaxRightSum,MaxLeftBorderSum+MaxRightBorderSum);\n}\nint MaxsubsequenceSum( const int A[],intN) {\n return MaxSubSum( A, 0, N - 1 );}\n
"},{"location":"ADS/D%26C/lec/#tree-traversal","title":"Tree Traversal","text":"Given a postorder traversal of a binary tree, and an inorder traversal of the same tree, we can reconstruct the tree.
Postorder : DEBFCA\nInorder : DBEACF\n
void BuildTree(int postL, int postR, int inL, int inR){\n if(postL>postR){return;}\n int root = postorder[postR];\n int k;\n for(k=inL;k<=inR;k++){\n if(inorder[k]==root){break;}\n }\n int numLeft = k-inL;\n BuildTree(postL,postL+numLeft-1,inL,k-1);\n BuildTree(postL+numLeft,postR-1,k+1,inR);\n}\n
We are given an array of n points in the plane, and the problem is to find out the closest pair of points in the array. This problem arises in a number of applications. For example, in air-traffic control, you may want to monitor planes that come too close together, since this may indicate a possible collision.
The Brute force solution is \\(O(n^2)\\) , compute the distance between each pair and return the smallest. We can calculate the smallest distance in \\(O(nlogn)\\) time using Divide and Conquer strategy.
"},{"location":"ADS/D%26C/lec/#on-logn2-approach","title":"\\(O(n (logn)^2)\\)\u200b approach","text":"Input: An array of n points P[ ]
Output: The smallest distance between two points in the given array.
As a pre-processing step, the input array is sorted according to x coordinates.
1) Find the middle point in the sorted array, we can take P[n/2]
as middle point.
2) Divide the given array in two halves. The first subarray contains points from P[0]
to P[n/2]
. The second subarray contains points from P[n/2+1]
to P[n-1]
.
3) Recursively find the smallest distances in both subarrays. Let the distances be \\(d_l\\) and \\(d_r\\). Find the minimum of \\(d_l\\) and \\(d_r\\). Let the minimum be \\(d\\)\u200b\u200b.
4) From the above 3 steps, we have an upper bound \\(d\\) of minimum distance. Now we need to consider the pairs such that one point in pair is from the left half and the other is from the right half. Consider the vertical line passing through P[n/2]
and find all points whose x coordinate is closer than \\(d\\)\u200b to the middle vertical line. Build an array strip[ ]
of all such points.
5) Sort the array strip[ ]
according to \\(y\\) coordinates.
6) Find the smallest distance in strip[ ]
. This is tricky. From the first look, it seems to be a \\(O(n^2)\\) step, but it is actually \\(O(n)\\)\u200b\u200b. It can be proved geometrically that for every point in the strip, we only need to check at most 7 points after it (note that strip is sorted according to Y coordinate). See this for more analysis.
7) Finally return the minimum of d and distance calculated in the above step (step 6)
// A divide and conquer program in C/C++ to find the smallest distance from a\n// given set of points.\n\n#include <stdio.h>\n#include <float.h>\n#include <stdlib.h>\n#include <math.h>\n\n// A structure to represent a Point in 2D plane\nstruct Point{\n int x, y;\n};\n\n// Needed to sort array of points according to X coordinate\nint compareX(const void* a, const void* b){\n Point *p1 = (Point *)a, *p2 = (Point *)b;\n return (p1->x - p2->x);\n}\n// Needed to sort array of points according to Y coordinate\nint compareY(const void* a, const void* b){\n Point *p1 = (Point *)a, *p2 = (Point *)b;\n return (p1->y - p2->y);\n}\n\n// A utility function to find the distance between two points\nfloat dist(Point p1, Point p2){\n return sqrt( (p1.x - p2.x)*(p1.x - p2.x) +\n (p1.y - p2.y)*(p1.y - p2.y)\n );\n}\n// A Brute Force method to return the smallest distance between two points\n// in P[] of size n\nfloat bruteForce(Point P[], int n){\n float min = FLT_MAX;\n for (int i = 0; i < n; ++i)\n for (int j = i+1; j < n; ++j)\n if (dist(P[i], P[j]) < min)\n min = dist(P[i], P[j]);\n return min;\n}\n\n// A utility function to find a minimum of two float values\nfloat min(float x, float y)\n{\n return (x < y)? x : y;\n}\n\n// A utility function to find the distance between the closest points of\n// strip of a given size. All points in strip[] are sorted according to\n// y coordinate. They all have an upper bound on minimum distance as d.\n// Note that this method seems to be a O(n^2) method, but it's a O(n)\n// method as the inner loop runs at most 6 times\nfloat stripClosest(Point strip[], int size, float d)\n{\n float min = d; // Initialize the minimum distance as d\n\n qsort(strip, size, sizeof(Point), compareY); \n\n // Pick all points one by one and try the next points till the difference\n // between y coordinates is smaller than d.\n // This is a proven fact that this loop runs at most 6 times\n for (int i = 0; i < size; ++i)\n for (int j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j)\n if (dist(strip[i],strip[j]) < min)\n min = dist(strip[i], strip[j]);\n\n return min;\n}\n\n// A recursive function to find the smallest distance. The array P contains\n// all points sorted according to x coordinate\nfloat closestUtil(Point P[], int n)\n{\n // If there are 2 or 3 points, then use brute force\n if (n <= 3)\n return bruteForce(P, n);\n // Find the middle point\n int mid = n/2;\n Point midPoint = P[mid];\n // Consider the vertical line passing through the middle point\n // calculate the smallest distance dl on left of middle point and\n // dr on right side\n float dl = closestUtil(P, mid);\n float dr = closestUtil(P + mid, n-mid);\n // Find the smaller of two distances\n float d = min(dl, dr);\n // Build an array strip[] that contains points close (closer than d)\n // to the line passing through the middle point\n Point strip[n];\n int j = 0;\n for (int i = 0; i < n; i++)\n if (abs(P[i].x - midPoint.x) < d)\n strip[j] = P[i], j++;\n\n // Find the closest points in strip. Return the minimum of d and closest\n // distance is strip[]\n return min(d, stripClosest(strip, j, d) );\n}\n\n// The main function that finds the smallest distance\n// This method mainly uses closestUtil()\nfloat closest(Point P[], int n)\n{\n qsort(P, n, sizeof(Point), compareX);\n\n // Use recursive function closestUtil() to find the smallest distance\n return closestUtil(P, n);\n}\n\n// Driver program to test above functions\nint main(){\n Point P[] = { {2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};\n int n = sizeof(P) / sizeof(P[0]);\n printf(\"The smallest distance is %f \", closest(P, n));\n return 0;\n}\n
"},{"location":"ADS/D%26C/lec/#onlogn-approach","title":"\\(O(nlogn)\\)\u200b approach","text":""},{"location":"ADS/D%26C/lec/#solving-recurrences","title":"Solving Recurrences","text":""},{"location":"ADS/D%26C/lec/#substitution-method-guess-and-prove-by-induction","title":"Substitution method : Guess and prove by induction","text":"Makeing a good guess
"},{"location":"ADS/D%26C/lec/#recursion-tree-method","title":"Recursion-tree method","text":"Case 1 If \\(f(N) = O(N^{\\log_ba-\\epsilon})\\) for some \\(\\epsilon > 0\\), then \\(T(N) = \\Theta(N^{\\log_ba})\\)
Proof:(Refer to Slides)
\\[\\begin{align*}\\sum_{i=0}^{\\log_bN-1}a^if(N/b^i) &= O(N^{log_b{a-\\epsilon} }\\frac{b^{\\epsilon log_bN}-1}{b^\\epsilon-1})\\\\&=O(N^{\\log_ba-\\epsilon}N^{\\epsilon})\\\\&=O(N^{\\log_ba})\\end{align*}\\]Therefore : \\(T(N) = \\Theta(N^{\\log_ba})+O(N^{\\log_ba}) =\\Theta(N^{\\log_ba})\\)
Case 2 If \\(f(N) = \\Theta(N^{\\log_ba})\\), then \\(T(N) = \\Theta(N^{\\log_ba}\\log N)\\)
Proof
\\[\\begin{align*}\\sum_{i=0}^{\\log_bN-1}a^if(N/b^i) &= \\sum_{i=0}^{\\log_bN-1}a^i\\Theta((\\frac{N}{b^i})^{\\log_ba})\\\\&=\\Theta(N^{\\log_ba}\\sum_{i=0}^{\\log_bN-1}(\\frac{a}{b^{log_ba} })^i)\\\\&=\\Theta(N^{\\log_ba}\\sum_{i=0}^{\\log_bN-1}(1)\\\\&=\\Theta(N^{\\log_ba}\\log N)\\end{align*}\\]Therefore : \\(T(N) = \\Theta(N^{\\log_ba}\\log N) + O(N^{\\log_ba}) = \\Theta(N^{\\log_ba}\\log N)\\) Case 3 If \\(f(N) = \\Omega(N^{\\log_ba+\\epsilon})\\) for some \\(\\epsilon > 0\\), if \\(af(N/b) \\leq kf(N)\\) for some \\(k<1\\) and sufficiently large \\(N\\), then \\(T(N) = \\Theta(f(N))\\)
Proof
\\[\\begin{align*}\\sum_{i=0}^{\\log_bN-1}a^if(N/b^i) &= \\Omega(N^{\\log_ba+\\epsilon}\\frac{b^{\\epsilon log_bN}-1}{b^\\epsilon-1})\\\\&=\\Omega(N^{\\log_ba+\\epsilon}N^{\\epsilon})\\\\&=\\Omega(N^{\\log_ba+\\epsilon})\\end{align*}\\]Therefore : \\(T(N) = \\Theta(f(N))\\)
"},{"location":"ADS/Heap/lec/","title":"Heap","text":"
The null path length, \\(Npl(X)\\), of any node \\(X\\) is the length of the shortest path from \\(X\\) to a node without two children. Define \\(Npl(NULL) = \u20131\\).
"},{"location":"ADS/Heap/lec/#def2","title":"Def.2","text":"The leftist heap property is that for every node \\(X\\)\u200b in the heap, the null path length of the left child is at least as large as that of the right child.
"},{"location":"ADS/Heap/lec/#theorem1","title":"Theorem.1","text":"A leftist tree with \\(r\\) nodes on the right path must have at least \\(2^r \u2013 1\\)\u200b nodes.
For the tree,left path actually all equals to its roots' NPL
\\(r=1\\) clearly holds
If \\(NLP(X)=r\\le k\\) holds then if \\(NLP(X)=r=k+1:\\)\u200b
Its right subtree must have \\(NLP(X)=k\\)
\\(\\because when\\ NLP(X)= k \\ right \\ path \\ has \\ at\\ least\\ 2^k-1\\ nodes\\)\u200b
So all the right part at least \\(2^k-1\\)
\\(r=1\\) clearly holds
If \\(NLP(X)\\le k\\) holds then if \\(NLP(X)=k+1:\\)
Its left subtree's right path must be \\(\\ge k\\)\u200b
So at least right path is k, so at least \\(2^k-1\\) for the left part
Thus \\(2^k-1+2^k-1+1=2^{k+1}-1\\)\u200b
Induction concludes.
PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2 ){ \n if ( H1 == NULL ) return H2; \n if ( H2 == NULL ) return H1; \n if ( H1->Element < H2->Element ){return Merge1( H1, H2 );} \n else {return Merge1( H2, H1 );}\n}\nstatic PriorityQueue\nMerge1(PriorityQueue H1,PriorityQueue H2){\n if(H1->Left == NULL){H1->Left = H2;}\n else{\n H1->Right = Merge(H1->Right,H2);\n if(H1->Left->Npl<H1->Right->Npl){\n swapChildren(H1);\n }\n H1->Npl = H1->Right->Npl+1;\n }\n return H1;\n}\n
\\(T_p=O(logN)\\)\u200b
Iterative Version
For SORTING \\(loglogN\\) \u2192 \\(logN * loglogN\\)
Only have to maintain two pointers.
Any \\(M\\) consecutive operations take at most \\(O(M log N)\\) time.
Always swap the left and right children except that the largest of all the nodes on the right paths does not have its children swapped. \\(No Npl.\\)
Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children.
It is an open problem to determine precisely the expected right path length of both leftist and skew heaps.
"},{"location":"ADS/Heap/lec/#amortized-analysis-for-skew-heaps","title":"Amortized Analysis for Skew Heaps","text":"\\(D_i =\\) the root of the resulting tree.
\\(\\Phi(D_i)=\\)\u200b\u200b\u200b number of heavy nodes.
\\(\\Phi(D_i) =\\)number of right nodes?
No! Think of only operation where num of right nodes will decrease: will not happen because the largest of all the nodes on the right paths does not have its children swapped
So this \\(\\Phi(D_i)\\) will always be increasing.
"},{"location":"ADS/Heap/lec/#def","title":"Def:","text":"A node \\(p\\) is heavy if the number of descendants of \\(p\\)\u2019s right subtree is at least half of the number of descendants of \\(p\\), and light otherwise.
Note that the number of descendants of a node includes the node itself.
\\(T_{worst}=\\)all right nodes \\(=l_1+h_1+l_2+h_2\\)
All heavy points at the right path will ALWAYS turn into light points.
Original light points at most can ALL turn into heavy nodes.
Remember: also need to insert into left subtrees.
h is heavy nodes on the left paths,will note change,cause its descandents will never exchange! .
Therefore Credits = \\(l_1+l_2-h_1-h_2\\)\u200b\u200b\u200b.
\\(T_{amotized}= T_{worst}+credits \\le2(l_1+l_2)\\)\u200b
\\(l_{right}=O(logN)\\)\u200b\u200b ? -- Actually if we want \\(l_{right}\\) to be large ,best almost balanced.
\u5047\u5982root\u7684\u5de6subtree \u5f88\u591a\uff0c\u628a\u591a\u51fa\u6765\u7684\u7ed9\u53f3\u8fb9\u6765\u589e\u52a0\u53f3\u94fe\u957f\uff0c\u8fd9\u6837\u5de6\u53f3node\u4e00\u6837\uff0c\u4e14\u53f3node\u7684heavy nodes\u81f3\u5c11\u4e0d\u4f1a\u5c11.
"},{"location":"ADS/Heap/lec/#amortized-analysis","title":"Amortized Analysis","text":"\\(\\therefore\\) A priority queue of any size can be uniquely represented by a collection of binomial trees.
Example : \\(13=2^0+2^2+2^3=1101_2\\)
"},{"location":"ADS/Heap/lec/#operations_1","title":"Operations","text":""},{"location":"ADS/Heap/lec/#findmin","title":"FindMin","text":"The minimum key is in one of the roots.
There are at most \\(\u300clogN\\) roots, hence \\(T_p = O( logN)\\)\u200b\u200b.
\u4e8c\u8fdb\u5236\u5206\u89e3\uff0c\u6240\u4ee5\\(logN\\)
Then this operation will take \\(O(1)\\)\u200b.
"},{"location":"ADS/Heap/lec/#merge_1","title":"Merge","text":"To combine two trees \\(O(1)\\)
At most \\(O(logN)\\) times
! Must keep the trees in the binomial queue sorted by height.
"},{"location":"ADS/Heap/lec/#insert","title":"Insert","text":""},{"location":"ADS/Heap/lec/#deletemin","title":"DeleteMin","text":""},{"location":"ADS/Heap/lec/#implementation","title":"Implementation","text":"typedef struct BinNode *Position;\ntypedef struct Collection *BinQueue;\ntypedef struct BinNode *BinTree; /* missing from p.176 */\n\nstruct BinNode \n{ \n ElementType Element;\n Position LeftChild;\n Position NextSibling;\n} ;\n\nstruct Collection \n{ \n int CurrentSize; /* total number of nodes */\n BinTree TheTrees[ MaxTrees ];\n} ;\n
BinTree CombineTrees( BinTree T1, BinTree T2 )\n{ /* merge equal-sized T1 and T2 */\n if ( T1->Element > T2->Element )\n /* attach the larger one to the smaller one */\n return CombineTrees( T2, T1 );\n /* insert T2 to the front of the children list of T1 */\n T2->NextSibling = T1->LeftChild;\n T1->LeftChild = T2;\n return T1;\n}\n
\\(T_p = O( 1 )\\)\u200b
BinQueue Merge( BinQueue H1, BinQueue H2 )\n{ BinTree T1, T2, Carry = NULL; \n int i, j;\n if ( H1->CurrentSize + H2-> CurrentSize > Capacity ) ErrorMessage();\n H1->CurrentSize += H2-> CurrentSize;\n for ( i=0, j=1; j<= H1->CurrentSize; i++, j*=2 ) {\n T1 = H1->TheTrees[i]; T2 = H2->TheTrees[i]; /*current trees */\n switch( 4*!!Carry + 2*!!T2 + !!T1 ) { \n case 0: /* 000 */ /* Carry T1 T2 */\n case 1: /* 001 */ break; \n case 2: /* 010 */ H1->TheTrees[i] = T2; H2->TheTrees[i] = NULL; break;\n case 4: /* 100 */ H1->TheTrees[i] = Carry; Carry = NULL; break;\n case 3: /* 011 */ Carry = CombineTrees( T1, T2 );\n H1->TheTrees[i] = H2->TheTrees[i] = NULL; break;\n case 5: /* 101 */ Carry = CombineTrees( T1, Carry );\n H1->TheTrees[i] = NULL; break;\n case 6: /* 110 */ Carry = CombineTrees( T2, Carry );\n H2->TheTrees[i] = NULL; break;\n case 7: /* 111 */ H1->TheTrees[i] = Carry; \n Carry = CombineTrees( T1, T2 ); \n H2->TheTrees[i] = NULL; break;\n } /* end switch */\n } /* end for-loop */\n return H1;\n}\n
BinQueue Merge( BinQueue H1, BinQueue H2 )\n{ BinTree T1, T2, Carry = NULL; \n int i, j;\n if ( H1->CurrentSize + H2-> CurrentSize > Capacity ) ErrorMessage();\n H1->CurrentSize += H2-> CurrentSize;\n for ( i=0, j=1; j<= H1->CurrentSize; i++, j*=2 ) {\n T1 = H1->TheTrees[i]; T2 = H2->TheTrees[i]; /*current trees */\n switch( 4*!!Carry + 2*!!T2 + !!T1 ) { \n case 0: /* 000 */\n case 1: /* 001 */ break; \n case 2: /* 010 */ H1->TheTrees[i] = T2; H2->TheTrees[i] = NULL; break;\n case 4: /* 100 */ H1->TheTrees[i] = Carry; Carry = NULL; break;\n case 3: /* 011 */ Carry = CombineTrees( T1, T2 );\n H1->TheTrees[i] = H2->TheTrees[i] = NULL; break;\n case 5: /* 101 */ Carry = CombineTrees( T1, Carry );\n H1->TheTrees[i] = NULL; break;\n case 6: /* 110 */ Carry = CombineTrees( T2, Carry );\n H2->TheTrees[i] = NULL; break;\n case 7: /* 111 */ H1->TheTrees[i] = Carry; \n Carry = CombineTrees( T1, T2 ); \n H2->TheTrees[i] = NULL; break;\n } /* end switch */\n } /* end for-loop */\n return H1;\n}\n
\u54e8\u5175
BinQueue Merge( BinQueue H1, BinQueue H2 ){\n\n}\n
ElementType DeleteMin( BinQueue H ){\n BinQueue DeletedQueue; \n Position DeletedTree, OldRoot;\n ElementType MinItem = Infinity; /* the minimum item to be returned */ \n int i, j, MinTree; /* MinTree is the index of the tree with the minimum item */\n if ( IsEmpty( H ) ) { PrintErrorMessage(); return \u2013Infinity; }\n for ( i = 0; i < MaxTrees; i++) { /* Step 1: find the minimum item */\n if( H->TheTrees[i] && H->TheTrees[i]->Element < MinItem ){ \n MinItem = H->TheTrees[i]->Element; MinTree = i; \n } /* end if */\n } /* end for-i-loop */\n\n DeletedTree = H->TheTrees[MinTree]; \n H->TheTrees[MinTree] = NULL; /* Step 2: remove the MinTree from H => H\u2019 */ \n OldRoot = DeletedTree; /* Step 3.1: remove the root */ \n DeletedTree = DeletedTree->LeftChild; free(OldRoot);\n DeletedQueue = Initialize(); /* Step 3.2: create H\u201d */ \n DeletedQueue->CurrentSize = (1<<MinTree ) \u2013 1; /* 2MinTree \u2013 1 */\n for ( j = MinTree \u2013 1; j >= 0; j \u2013 \u2013 ) { \n DeletedQueue->TheTrees[j] = DeletedTree;\n DeletedTree = DeletedTree->NextSibling;\n DeletedQueue->TheTrees[j]->NextSibling = NULL;\n } /* end for-j-loop */\n\n H->CurrentSize \u2013 = DeletedQueue->CurrentSize + 1;\n H = Merge( H, DeletedQueue ); /* Step 4: merge H\u2019 and H\u201d */ \n return MinItem;\n}\n
In aggregate analysis, we show that for all n, a sequence of n operations takes worst-case time \\(T(n)\\) in total. In the worst case, the average cost, or amortized cost, per operation is therefore \\(T(n)/n\\). Note that this amortized cost applies to each operation, even when there are several types of operations in the sequence.
Stack operations
Let us analyze a sequence of n \\(PUSH, POP\\), and \\(MULTIPOP\\)\u200b operations on an initially empty stack
The worst-case time of any stack operation is therefore \\(O(n)\\), and hence a sequence of n operations costs \\(O(n^2)\\)
We can pop each object from the stack at most once for each time we have pushed it onto the stack.
Therefore, the number of times that \\(POP\\) can be called on a nonempty stack, including calls within \\(MULTIPOP\\), is at most the number of PUSH operations, which is at most n.
For any value of n, any sequence of n \\(PUSH, POP\\), and \\(MULTIPOP\\) operations takes a total of \\(O(n)\\) time. The average cost of an operation is \\(O(n)/n = O(1)\\)\u200b
We actually showed a worst-case bound of \\(O(n)\\) on a sequence of \\(n\\) operations. Dividing this total cost by \\(n\\)\u200b yielded the average cost per operation, or the amortized cost.
Incrementing a binary counter
Consider the problem of implementing a k-bit binary counter that counts upward from 0.
We use an array \\(A[0..k-1]\\) of bits, where \\(A.length=k\\), as the counter.
A binary number x that is stored in the counter has its lowest-order bit in \\(A[0]\\) and its highest-order bit in \\(A[k-1]\\)so that \\(x = \\sum_{i=0}^{k-1}A[i]\u00b72^i\\) Initially, \\(x=0\\) and thus \\(A[i]=0\\) for \\(i=0,1,... k-1\\)
To add \\(1\\)(modulo \\(2^k\\)\u200b) to the value in the counter, we use the following procedure.
Thus, a sequence of n INCREMENT operations on an initially zero counter takes time \\(O(nk)\\) in the worst case.
Similarly, bit \\(A[2]\\) flibs only every fourth time or \\(n/4\u300d\\) times in a sequence.
\\(\\sum_{i=0}^{k-1}\\frac{n}{2^i}\u300d<n\\sum_{i=0}^{\\infty}\\frac{1}{2^i}=2n\\)
1. If the set of stack operations included a MULTIPUSH operation, which pushes \\(k\\) items onto the stack, would the \\(O(1)\\)\u200b bound on the amortized cost of stack operations continue to hold?
Since one MULTIPUSH needs \\(\\Theta(k)\\) time, performing n MULTIPUSH operations, each with k elements, would take\\(\u0398(kn)\\) time, leading to amortized cost of \\(\\Theta(k)\\)\u200b.
Show that if a DECREMENT operation were included in the k-bit counter example, n operations could cost as much as \\(\\Theta(nk)\\)\u200b time.
2. Suppose we perform a sequence of \\(n\\) operations on a data structure in which the i th operation costs i if i is an exact power of 2, and 1 otherwise.
Use aggregate analysis to determine the amortized cost per operation.
"},{"location":"ADS/Heap/lec/#the-accounting-method","title":"The accounting method","text":"In the accounting method of amortized analysis, we assign differing charges to different operations, with some operations charged more or less than they actually cost. We call the amount we charge an operation its amortized cost. When an operation\u2019s amortized cost exceeds its actual cost, we assign the difference to specific objects in the data structure as credit.
Credit can help pay for later operations whose amortized cost is less than their actual cost.
If we want to show that in the worst case the average cost per operation is small by analyzing with amortized costs, we must ensure that the total amortized cost of a sequence of operations provides an upper bound on the total actual cost of the sequence.
this relationship must hold for all sequences of operations. Which means \\(\\sum \\hat{c}_i\\ge\\sum c_i\\) for all sequences of n operations.
Instead of representing prepaid work as credit stored with specific objects in the data structure, the potential method of amortized analysis represents the prepaid work as \u201cpotential energy,\u201d or just \u201cpotential,\u201d which can be released to pay for future operations. We associate the potential with the data structure as a whole rather than with specific objects within the data structure.
We will perform n operations, starting with an initial data structure \\(D_0\\)
For each \\(i=1,2,...n\\), we let \\(c_i\\) be the actual cost of the ith operation and \\(D_i\\) be the data structure that results after applying the ith operation to data structure \\(D_{i-1}\\).
A potential function \\(\\Phi\\) maps each data structure \\(D_i\\) to a real number \\(\\Phi(D_i)\\), which is the potential associated with data structure \\(D_i\\) .
The amortized cost \\(\\hat{c}_i\\) of the ith operation with respect to potential function \\(\\Phi\\) is defined by \\(\\hat{c}_i=c_i+\\Phi(D_i)-\\Phi(D_{i-1})\\)
the total amortized cost of the n operations is \\(\\sum\\hat{c}_i=\\sum c_i+\\Phi(D_n)-\\Phi(D_0)\\)
Define a potential function so that \\(\\Phi(D_i)\\ge \\Phi(D_0)\\) for all i!
Stack operations * See Previous Part.
Incrementing a binary counter we define the potential of the counter after the ith INCREMENT operation to be \\(b_i\\) , the number of 1s in the counter after the i th operation. * If the counter starts at zero, then \\(\\Phi(D_0)=0\\) Since \\(\\Phi(D_i)\\ge 0\\) for all i , the total amortized cost of a sequence of n INCREMENT operations is an upper bound on the total actual cost, and so the worst-case cost of n INCREMENT operations is \\(O(n)\\).
"},{"location":"ADS/NP/lec/#np-completeness","title":"NP - Completeness","text":""},{"location":"ADS/NP/lec/#turing-machind","title":"Turing Machind","text":"
NP: Nondeterministic polynomial-time * The problem is NP if we can prove any solution is true in polynomial time.
Example Hamilton cycle problem: Find a single cycle that contains every vertex \u2013 does this simple circuit include all the vertices?
Note: Not all decidable problems are in NP. For example, consider the problem of determining whether a graph does not have a Hamiltonian cycle.
There is a polynomial time algorithm that can solve the problem. \\(P \\subseteq NP\\)
The most difficult problems in NP.
EXAMPLE Suppose that we already know that the Hamiltonian cycle problem is NP-complete
. Prove that the traveling salesman problem is NP-complete as well. * Hamiltonian cycle problem: Given a graph \\(G=(V, E)\\), is there a simple cycle that visits all vertices? * Traveling salesman problem: Given a complete graph \\(G=(V, E)\\), with edge costs, and an integer K, is there a simple cycle that visits all vertices and has total cost \\(\\le K\\)?
The first problem that was proven to be NP-complete was the Satisfiability problem (Circuit-SAT): Input a boolean expression and ask if it has an assignment to the variables that gives the expression a value of 1. Cook showed in 1971 that all the problems in NP could be polynomially transformed to Satisfiability. He proved it by solving this problem on a nondeterministic Turing machine in polynomial time.
"},{"location":"ADS/NP/lec/#co-np","title":"co-NP","text":""},{"location":"ADS/NP/lec/#abstract-problem","title":"Abstract Problem","text":"An abstract problem \\(Q\\) is a binary relation on a set \\(I\\) of problem instances and a set \\(S\\) of problem solutions.
"},{"location":"ADS/NP/lec/#formal-language-theory-for-decision-problem","title":"Formal-language Theory \u2014 for decision problem","text":"x\u662f\u89e3\u7684\u5b9e\u4f8b
x
and the other is a binary string y
called a certificate. x
if there exists a certificate y
such that A(x, y) = 1
. L = { x \u2208 {0, 1}*
: there exists y \u2208 {0, 1}*
such that A(x, y) = 1}
. co-NP : \u8be5\u95ee\u9898\u548c\u5b83\u7684\u8865\u90fd\u5c5e\u4e8eNP
Some probles can only reach requirement2 but satisfy 1 -- NP-Hard
0-1 backpack problem
https://zhuanlan.zhihu.com/p/93857890
"},{"location":"ADS/Tree/lec/#avl-tree","title":"AVL Tree","text":"
An empty binary tree is height balanced.
If T is nonempty binary tree with \\(T_L\\) and \\(T_R\\) as its left and right subtrees,then \\(T\\) is height balanced iff
(1)\\(T_L\\) and \\(T_R\\) are height balanced
(2)\\(|h_L-h_R|\\le 1\\) where \\(h_l\\) and \\(h_R\\) are the heights of \\(T_L\\) and \\(T_R\\)\u200b\u200b
In an AVL Tree
, \\(BF(node)\\) = \\(-1,0,or\\ 1\\)
RR Rotation
LL Rotation
RL Rotation
LR Rotation
Details refer to slides.
\u52d8\u8bef \uff1a \\(n_h \\approx \\frac{1}{\\sqrt{5}}(\\frac{1+\\sqrt{5}}{2})^{h+3} - 1\\)\u200b
"},{"location":"ADS/Tree/lec/#splay-trees","title":"Splay Trees","text":"Target : Any \\(M\\) consecutive tree operations starting from an empty tree take at most \\(O(M log N)\\) time. * For any nonroot node \\(X\\) , denote its parent by \\(P\\) and grandparent by \\(G\\)
worst case bound > amortized bound > average case bound
"},{"location":"ADS/Tree/lec/#aggregate-analysis","title":"Aggregate analysis","text":"Show that for all n, a sequence of n operations takes worst-case time \\(T(n)\\) in total.
In the worst case, the average cost, or amortized cost, per operation is therefore \\(T(n)/n\\).
"},{"location":"ADS/Tree/lec/#accounting-method","title":"Accounting method","text":"When an operation\u2019s amortized cost \\(\\hat{c}_i\\) exceeds its actual cost \\(c_i\\) , we assign the difference to specific objects in the data structure as credit. Credit can help pay for later operations whose amortized cost is less than their actual cost.
"},{"location":"ADS/Tree/lec/#potential-method","title":"Potential Method","text":"Black-Height
The number of black nodes from the node to its descandent leaves (without counting NIL & itself)
"},{"location":"ADS/Tree/lec/#lemma","title":"Lemma","text":"A red-black tree with N internal nodes has height at most \\(2ln(N +1)\\)\u200b.
"},{"location":"ADS/Tree/lec/#operations","title":"Operations","text":""},{"location":"ADS/Tree/lec/#insert","title":"Insert","text":"Sketch : Insert & Colour red ; Pass Error to Root
Refer to \u7b97\u6cd5\u5bfc\u8bba !
Loop ends when z.p is black
Color the root black after the loop
Case 3 : To have a red far nephew.
Case 4 : (1) change colour of ? and w (2) far nephew change black (3) make brother new root
Beacuse property 5 holds so descants of w have no black node
A B+ tree of order M is a tree with the following structural properties:
Root can have less children like when constructing.
Btree Insert ( ElementType X, Btree T ) \n{ \n Search from root to leaf for X and find the proper leaf node;\n Insert X;\n while ( this node has M+1 keys ) {\n split it into 2 nodes with \u300c(M+1)/2 and \u300c(M+1)/2 keys, respectively;\n if (this node is the root)\n create a new root with two children;\n check its parent;\n // Every iteration O(M)\n }\n} \n
\\(Depth = O(log_{M/2}N)\\)\u200b
\\(T_{insert} = O(M* Depth)=O(logN*M/logM)\\)\u200b
\\(Y_{find} = O(logN)\\)\u200b
"},{"location":"ADS/approx/lec/","title":"Approximation","text":""},{"location":"ADS/approx/lec/#approximation","title":"Approximation","text":"
If an algorithm achieves an approximation ratio of \\(\\rho(n)\\), we call it a\\(\\rho(n)\\)-approximation algorithm.
Approximate Bin Packing Given N items of sizes \\(S_1 , S_2 , \u2026, S_N\\) , such that \\(0 < S_i \\le 1\\) for all \\(1 \\le i \\le N\\) . Pack these items in the fewest number of bins, each of which has unit capacity. Example : N = 7; \\(S_i\\) = 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8
void NextFit ( )\n{ read item1;\n while ( read item2 ) {\n if ( item2 can be packed in the same bin as item1 )\n place item2 in the bin;\n else\n create a new bin for item2;\n item1 = item2;\n } /* end-while */\n}\n
* Therefore the NextFit algorithm is a 2-approximation algorithm for the bin packing problem.[approximation ratio = 2] void FirstFit ( )\n{ while ( read item ) {\n scan for the first bin that is large enough for item;\n if ( found )\n place item in that bin;\n else\n create a new bin for item;\n } /* end-while */\n}\n
* Time complexity of FirstFit is \\(O(nlogn)\\) * But the approximation ratio of FirstFit is 2. On-line Algorithms Place an item before processing the next one, and can NOT change decision.
Off-line Algorithms View the entire item list before producing an answer.
"},{"location":"ADS/approx/lec/#the-knapsack-problem-fractional-version","title":"The Knapsack Problem \u2014 fractional version","text":"A knapsack with a capacity \\(M\\) is to be packed. Given \\(N\\) items. Each item \\(i\\) has a weight \\(w_i\\) and a profit \\(p_i\\) . If \\(x_i\\) is the percentage of the item \\(i\\) being packed, then the packed profit will be \\(p_ix_i\\) . * An optimal packing is a feasible one with maximum profit. That is, we are supposed to find the values of \\(x_i\\) such that \\(\\sum\\limits_{i=1}^n p_ix_i\\) obtains its maximum under the constrains: $\\sum\\limits_{i=1}^{n}w_ix_i \\le M $ and \\(x\\in [0,1] \\ for\\ 1\\le i \\le n\\)
"},{"location":"ADS/approx/lec/#the-knapsack-problem-0-1-version","title":"The Knapsack Problem \u2014 0-1 version","text":"NPC and NP-hard
\\(p_{opt}\\) is the optimal solution for the frac version
Refer to Slides.
"},{"location":"ADS/back/lec/","title":"backtracking","text":""},{"location":"ADS/back/lec/#backtracking","title":"Backtracking","text":"
The basic idea is that suppose we have a partial solution \\(( x_1, ... , x_i )\\) where each \\(x_k \\in S_k\\) for \\(1 \\le k \\le i < n\\). First we add \\(x_{i+1} \\in S_{i+1}\\) and check if \\(( x_1, ... , x_i, x_{i+1} )\\) satisfies the constrains. If the answer is \\(\u201cyes\u201d\\) we continue to add the next \\(x\\), else we delete \\(x_i\\) and backtrack to the previous partial solution \\(( x_1, ... , x_{i-1} )\\)\u200b.
"},{"location":"ADS/back/lec/#example","title":"Example","text":""},{"location":"ADS/back/lec/#eight-queens","title":"Eight Queens","text":""},{"location":"ADS/back/lec/#the-turnpike-reconstruction-problem","title":"The Turnpike Reconstruction Problem","text":"Given \\(N\\) points on the x-axis with coordinates \\(x_1 < x_2 < \u2026< x_N\\) . Assume that \\(x_1 = 0\\). There are \\(N ( N \u2013 1 ) / 2\\) distances between every pair of points.
Given \\(N ( N \u2013 1 ) / 2\\) distances. Reconstruct a point set from the distances.
Example
Complexity : \\(2^n\\)
bool Reconstruct ( DistType X[ ], DistSet D, int N, int left, int right )\n{ /* X[1]...X[left-1] and X[right+1]...X[N] are solved */\n bool Found = false;\n if ( Is_Empty( D ) )\n return true; /* solved */\n D_max = Find_Max( D );\n /* option 1\uff1aX[right] = D_max */\n /* check if |D_max-X[i]|\uf0ceD is true for all X[i]\u2019s that have been solved */\n OK = Check( D_max, N, left, right ); /* pruning */\n if ( OK ) { /* add X[right] and update D */\n X[right] = D_max;\n for ( i=1; i<left; i++ ) Delete( |X[right]-X[i]|, D);\n for ( i=right+1; i<=N; i++ ) Delete( |X[right]-X[i]|, D);\n Found = Reconstruct ( X, D, N, left, right-1 );\n if ( !Found ) { /* if does not work, undo */\n for ( i=1; i<left; i++ ) Insert( |X[right]-X[i]|, D);\n for ( i=right+1; i<=N; i++ ) Insert( |X[right]-X[i]|, D);\n }\n }\n /* finish checking option 1 */\n if ( !Found ) { /* if option 1 does not work */\n /* option 2: X[left] = X[N]-D_max */\n OK = Check( X[N]-D_max, N, left, right );\n if ( OK ) {\n X[left] = X[N] \u2013 D_max;\n for ( i=1; i<left; i++ ) Delete( |X[left]-X[i]|, D);\n for ( i=right+1; i<=N; i++ ) Delete( |X[left]-X[i]|, D);\n Found = Reconstruct (X, D, N, left+1, right );\n if ( !Found ) {\n for ( i=1; i<left; i++ ) Insert( |X[left]-X[i]|, D);\n for ( i=right+1; i<=N; i++ ) Insert( |X[left]-X[i]|,D);\n }\n }\n /* finish checking option 2 */\n } /* finish checking all the options */\n\n return Found;\n}\n
bool Backtracking ( int i )\n{ Found = false;\n if ( i > N )\n return true; /* solved with (x1, \u2026, xN) */\n for ( each xi in Si ) { \n /* check if satisfies the restriction R */\n OK = Check((x1, \u2026, xi) , R ); /* pruning */\n if ( OK ) {\n Count xi in;\n Found = Backtracking( i+1 );\n if ( !Found )\n Undo( i ); /* recover to (x1, \u2026, xi-1) */\n }\n if ( Found ) break; \n }\n return Found;\n}\n
\u6ce8\uff1a\u56de\u6eaf\u7684\u6548\u7387\u8ddfS\u7684\u89c4\u6a21\u3001\u7ea6\u675f\u51fd\u6570\u7684\u590d\u6742\u6027\u3001\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u7ed3\u70b9\u6570\u76f8\u5173\u3002\u7ea6\u675f\u51fd\u6570\u51b3\u5b9a\u4e86\u526a\u679d\u7684\u6548\u7387\uff0c\u4f46\u662f\u5982\u679c\u51fd\u6570\u672c\u8eab\u592a\u590d\u6742\u4e5f\u672a\u5fc5\u5408\u7b97\u3002\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u7ed3\u70b9\u6570\u6700\u96be\u4f30\u8ba1\uff0c\u4f7f\u5f97\u590d\u6742\u5ea6\u5206\u6790\u5f88\u96be\u5b8c\u6210\u3002
"},{"location":"ADS/back/lec/#different-construction-methods","title":"Different Construction Methods","text":"In practice , we use the first tree more.
"},{"location":"ADS/back/lec/#stick-problem","title":"Stick Problem","text":"George had a number of small sticks of the same length,whcih he cut into random sections. He wanted to know if he could reconstruct the original sticks from the sections.Given the lengths of the sections,help him to find the minimum possible length of the original sticks.
Example Sample Input
9\n5 2 1 5 2 1 5 2 1\n
Sample Output 6\n
Idea: 1.Enumerate the length of the original sticks. 2.Check by enumerate a permutation by backtracking. Everytime devcide a stick section should belong to which original stick.
Example:Tic-tac-toe
"},{"location":"ADS/dy/lec/#dynamic-programming","title":"Dynamic Programming","text":""},{"location":"ADS/dy/lec/#example","title":"Example:","text":""},{"location":"ADS/dy/lec/#1-fibonacci-numbers","title":"1. Fibonacci numbers","text":"
int fib(int n) {\n if (n <= 1) return n;\n return fib(n-1) + fib(n-2);\n}\n
If we use dynamic programming, we can reduce the time complexity to \\(O(n)\\).
int fib(int n) {\n int f[n+1];\n f[0] = 0;\n f[1] = 1;\n for (int i = 2; i <= n; i++) {\n f[i] = f[i-1] + f[i-2];\n }\n return f[n];\n}\n
"},{"location":"ADS/dy/lec/#2-ordering-matrix-multiplications","title":"2. Ordering Matrix Multiplications","text":"* In which order can we compute the product of n matrices with minimal computing time?
Total time complexity: \\(O(n^3)\\) -- compute all m[i][j]
void OptMatrix( const long r[ ], int N, TwoDimArray M ) \n{ int i, j, k, L; \n long ThisM; \n for( i = 1; i <= N; i++ ) M[ i ][ i ] = 0; \n for( k = 1; k < N; k++ ) /* k = j - i */ \n for( i = 1; i <= N - k; i++ ) { /* For each position */ \n j = i + k; M[ i ][ j ] = Infinity; \n for( L = i; L < j; L++ ) { \n ThisM = M[ i ][ L ] + M[ L + 1 ][ j ] \n + r[ i - 1 ] * r[ L ] * r[ j ]; \n if ( ThisM < M[ i ][ j ] ) /* Update min */ \n M[ i ][ j ] = ThisM; \n } /* end for-L */\n } /* end for-Left */\n}\n
\\(F[N][i] = min_{k}(F[k-i][i]+F[N-k-i][k+1]+r_{k-i-1} \\times r_i \\times r_{N-i})\\) ?
Given N words \\(w_1, w_2, \\cdots, w_N\\) and their search probabilities \\(p_1, p_2, \\cdots, p_N\\) . We want to arrange these words in a binary search tree in a way that minimize the expected total access time : \\(\\sum_{i=1}^{N} (d_i+1) \\times p_i\\) , where \\(d_i\\) is the depth of the node containing \\(w_i\\) in the binary search tree.
Method 1: Use single source shortest path algorithm \\(N\\) times.
Method 2: Floyd-Warshall Algorithm Define \\(D^k[i][j]\\)= \\(min\\{ length\\ of\\ path\\ i \\to {l \\le k }\\to j\\}\\) and \\(D^{-1}[ i ] [ j ] = Cost [ i ] [ j ]\\).
Then the length of the shortest path from i to j is \\(D^{N-1}[ i ] [ j ]\\).
/* A[ ] contains the adjacency matrix with A[ i ][ i ] = 0 */ \n/* D[ ] contains the values of the shortest path */ \n/* N is the number of vertices */ \n/* A negative cycle exists iff D[ i ][ i ] < 0 */ \nvoid AllPairs( TwoDimArray A, TwoDimArray D, int N ) \n{ int i, j, k; \n for ( i = 0; i < N; i++ ) /* Initialize D */ \n for( j = 0; j < N; j++ )\n D[ i ][ j ] = A[ i ][ j ]; \n for( k = 0; k < N; k++ ) /* add one vertex k into the path */\n for( i = 0; i < N; i++ ) \n for( j = 0; j < N; j++ ) \n if( D[ i ][ k ] + D[ k ][ j ] < D[ i ][ j ] ) \n /* Update shortest path */ \n D[ i ][ j ] = D[ i ][ k ] + D[ k ][ j ]; \n}\n
\\(T(N) = O(N^3)\\), but faster in a dense graph.
How to design a DP method? 1. Characterize the structure of an optimal solution. 2. Recursively define the value of an optimal solution. 3. Compute the value of an optimal solution in some order. 4. Reconstruct an optimal solution from computed information.
"},{"location":"ADS/dy/lec/#5-product-assembly","title":"5. Product Assembly","text":""},{"location":"ADS/greedy/lec/#greedy-algorithm","title":"Greedy Algorithm","text":""},{"location":"ADS/greedy/lec/#activities-arrangements","title":"Activities Arrangements","text":"
Given a set of activities \\(S = \\{ a_1, a_2, \u2026, a_n \\}\\) that wish to use a resource (e.g. a classroom).
Each \\(a_i\\) takes place during a time interval \\([s_i, f_i)\\).Activities ai and aj are compatible if \\(s_i \\ge f_j\\) or \\(s_j \\ge f_i\\)\u200b\u200b (i.e. their time intervals do not overlap).
"},{"location":"ADS/greedy/lec/#dynamic-programming","title":"Dynamic Programming","text":"c[i][k] : i is start activity j is end activity and i j not included.
//\u8bb0\u5fc6\u4fdd\u5b58\u641c\u7d22\u7ed3\u679c\nif S[i][j] == NULL : return 0;\nfor k\n if (c[i][k]==-1) c[i][k] = dfs(i,k)\n
Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve.
Prove that there is always an optimal solution to the original problem that makes the greedy choice, so that the greedy choice is always safe.
First choice can always be included !
Greedy algorithm works only if the local optimum is equal to the global optimum.
"},{"location":"ADS/greedy/lec/#change","title":"Change","text":""},{"location":"ADS/greedy/lec/#huffman-codes","title":"Huffman Codes","text":"The trick is:No code is a prefix of another!!
Or it will cause ambiguity when decoding!
"},{"location":"ADS/greedy/lec/#huffmans-algorithm","title":"Huffman's algorithm","text":"void Huffman ( PriorityQueue heap[ ], int C )\n{ consider the C characters as C single node binary trees,\n and initialize them into a min heap;\n for ( i = 1; i < C; i++ ) { \n create a new node;\n /* be greedy here */\n delete root from min heap and attach it to left_child of node;\n delete root from min heap and attach it to right_child of node;\n weight of node = sum of weights of its children;\n /* weight of a tree = sum of the frequencies of its leaves */\n insert node into min heap;\n }\n}\n/*T = O(ClogC)*/\n
"},{"location":"ADS/rand/lec/#randomized-algorithms","title":"Randomized Algorithms","text":""},{"location":"ADS/rand/lec/#example-the-hiring-problem","title":"[Example] The Hiring Problem","text":"
Assume M people are hired.Total Cost: \\(C = O(NC_i + MC_h)\\)
"},{"location":"ADS/rand/lec/#naive-solution","title":"Na\u00efve Solution","text":"int Hiring ( EventType C[ ], int N )\n{ /* candidate 0 is a least-qualified dummy candidate */\n int Best = 0;\n int BestQ = the quality of candidate 0;\n for ( i=1; i<=N; i++ ) {\n Qi = interview( i ); /* Ci */\n if ( Qi > BestQ ) {\n BestQ = Qi;\n Best = i;\n hire( i ); /* Ch */\n }\n }\n return Best;\n}\n
Worst Case: \\(O(N)\\) interviews, \\(O(N)\\) hires, \\(O(N)\\) comparisons Assume candidates are randomly ordered. * X = number of hires. E[X] = \\(\\sum\\limits_{i=1}^{N} i\u00b7Pr[X=i]\\) * $ X_i = \\begin{array}{ll} 1 if i is hired \\ 0 otherwise \\end{array}$ * \\(Pr[X_i = 1] = \\frac{1}{i}\\) * E[X] = Expected number of hires = \\(E[\\sum\\limits_{i=1}^{N} X_i] = \\sum\\limits_{i=1}^{N} E[X_i] = \\sum\\limits_{i=1}^{N} \\frac{1}{i} = O(\\log N) +O(1)\\) * \\(O(C_h \\log N + NC_i)\\)
"},{"location":"ADS/rand/lec/#randomized-solution","title":"Randomized Solution","text":"int RandomizedHiring ( EventType C[ ], int N )\n{ /* candidate 0 is a least-qualified dummy candidate */\n int Best = 0;\n int BestQ = the quality of candidate 0;\n\n randomly permute the list of candidates;\n\n for ( i=1; i<=N; i++ ) {\n Qi = interview( i ); /* Ci */\n if ( Qi > BestQ ) {\n BestQ = Qi;\n Best = i;\n hire( i ); /* Ch */\n }\n }\n}\n
"},{"location":"ADS/rand/lec/#radomized-permutation-algorithm","title":"Radomized Permutation Algorithm","text":"Assign each element A[ i ] a random priority P[ i ],and sort
void PermuteBySorting ( ElemType A[ ], int N )\n{\n for ( i=1; i<=N; i++ )\n A[i].P = 1 + rand()%(N3); \n /* makes it more likely that all priorities are unique */\n Sort A, using P as the sort keys;\n}\n
int OnlineHiring ( EventType C[ ], int N, int k )\n{\n int Best = N;\n int BestQ = - infinity ;\n for ( i=1; i<=k; i++ ) {\n Qi = interview( i );\n if ( Qi > BestQ ) BestQ = Qi;\n }\n for ( i=k+1; i<=N; i++ ) {\n Qi = interview( i );\n if ( Qi > BestQ ) {\n Best = i;\n break;\n }\n }\n return Best;\n}\n
"},{"location":"ADS/rand/lec/#example-quicksort","title":"[Example] Quicksort","text":"https://leovan.me/cn/2018/08/cnn/
"},{"location":"AI/net/#recurrent-neural-network-rnn","title":"Recurrent Neural Network \uff08RNN\uff09","text":"\\(O_t=g(V\\cdot S_t)\\)
\\(S_t=\\Phi(U\\cdot X_t+W\\cdot S_{t-1})\\) \\(\\Phi\\)\uff08\u6fc0\u6d3b\u51fd\u6570\uff09
"},{"location":"AI/net/#back-propagation","title":"Back propagation","text":""},{"location":"AI/net/#intro","title":"Intro","text":"\\(Cost=H(W_{11},W_{12},\u2026\u2026,W_{mn})\\)
\\(\\triangledown H=\\frac{\\partial H}{\\partial W_{11}}e_{11}+\\frac{\\partial H}{\\partial W_{12}}e_{12}+\u2026\u2026+\\frac{\\partial H}{\\partial W_{mn}}e_{mn}\\)
https://zhuanlan.zhihu.com/p/115571464
https://towardsdatascience.com/understanding-backpropagation-algorithm-7bb3aa2f95fd
\u53cd\u5411\u4f20\u64ad\uff08Backpropagation\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u7684\u4f18\u5316\u7b97\u6cd5\uff0c\u901a\u8fc7\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u5173\u4e8e\u7f51\u7edc\u53c2\u6570\u7684\u68af\u5ea6\uff0c\u5e76\u5229\u7528\u8fd9\u4e9b\u68af\u5ea6\u6765\u66f4\u65b0\u53c2\u6570\uff0c\u4ee5\u6700\u5c0f\u5316\u635f\u5931\u51fd\u6570\u3002
\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u5bf9\u7f51\u7edc\u8f93\u51fa\u7684\u68af\u5ea6\uff08\u5373\u635f\u5931\u51fd\u6570\u5173\u4e8e\u8f93\u51fa\u7684\u5bfc\u6570\uff09\u3002
\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u901a\u8fc7\u53cd\u5411\u4f20\u64ad\u548c\u68af\u5ea6\u4e0b\u964d\u7b97\u6cd5\u7684\u7ed3\u5408\u6765\u5b9e\u73b0\u7684\uff0c\u4ee5\u6700\u5c0f\u5316\u635f\u5931\u51fd\u6570\u5e76\u4f18\u5316\u795e\u7ecf\u7f51\u7edc\u7684\u53c2\u6570\u3002
"},{"location":"AI/net/#_2","title":"\u53c2\u6570\u66f4\u65b0","text":"\\(\u03b8_{new}=\u03b8_{old}\u2212\u03b1\u2207L(\u03b8_{old})\\)
\u5176\u4e2d\uff1a
\u8fd9\u4e2a\u66f4\u65b0\u89c4\u5219\u7684\u76f4\u89c2\u89e3\u91ca\u662f\uff0c\u6211\u4eec\u6cbf\u7740\u635f\u5931\u51fd\u6570\u4e0b\u964d\u6700\u5feb\u7684\u65b9\u5411\u66f4\u65b0\u53c2\u6570\u3002\u68af\u5ea6\u544a\u8bc9\u6211\u4eec\u635f\u5931\u51fd\u6570\u5728\u5f53\u524d\u70b9\u4e0a\u5347\u6700\u5feb\u7684\u65b9\u5411\uff0c\u6211\u4eec\u6cbf\u7740\u68af\u5ea6\u7684\u53cd\u65b9\u5411\u524d\u8fdb\uff0c\u4ee5\u964d\u4f4e\u635f\u5931\u3002
\u5b66\u4e60\u7387\u7684\u9009\u62e9\u662f\u5173\u952e\u7684\u3002\u5982\u679c\u5b66\u4e60\u7387\u592a\u5c0f\uff0c\u6536\u655b\u901f\u5ea6\u4f1a\u5f88\u6162\uff0c\u800c\u5982\u679c\u5b66\u4e60\u7387\u592a\u5927\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u8df3\u8fc7\u635f\u5931\u51fd\u6570\u7684\u6700\u5c0f\u503c\u3002\u56e0\u6b64\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u5b66\u4e60\u7387\u5bf9\u4e8e\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u81f3\u5173\u91cd\u8981\u3002
\u901a\u5e38\uff0c\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u5b66\u4e60\u7387\u53ef\u80fd\u4f1a\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\u800c\u53d8\u5316\uff0c\u8fd9\u88ab\u79f0\u4e3a\u5b66\u4e60\u7387\u8c03\u5ea6\uff08learning rate scheduling\uff09\u3002\u4f8b\u5982\uff0c\u521d\u59cb\u65f6\u53ef\u4ee5\u4f7f\u7528\u8f83\u5927\u7684\u5b66\u4e60\u7387\u4ee5\u5feb\u901f\u6536\u655b\uff0c\u7136\u540e\u968f\u7740\u8bad\u7ec3\u7684\u8fdb\u884c\u9010\u6e10\u51cf\u5c0f\u5b66\u4e60\u7387\uff0c\u4ee5\u63d0\u9ad8\u6536\u655b\u7684\u7cbe\u5ea6\u3002
\u603b\u7684\u6765\u8bf4\uff0c\u5b66\u4e60\u7387\u662f\u4e00\u4e2a\u5e73\u8861\u8bad\u7ec3\u901f\u5ea6\u548c\u6027\u80fd\u7684\u5173\u952e\u56e0\u7d20\u3002\u4e0d\u540c\u7684\u95ee\u9898\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u5b66\u4e60\u7387\uff0c\u56e0\u6b64\u5b83\u901a\u5e38\u9700\u8981\u901a\u8fc7\u5b9e\u9a8c\u6765\u8c03\u6574\u3002
"},{"location":"AI/net/#back-propagation-in-cnn","title":"Back propagation in CNN","text":"https://mmuratarat.github.io/2019-02-07/bptt-of-rnn
"},{"location":"AI/net/#long-short-term-memory","title":"Long Short-Term Memory","text":"https://easyai.tech/ai-definition/rnn/
https://blog.csdn.net/u012328159/article/details/87567358
"},{"location":"AI/net/#attention","title":"Attention","text":"https://www.cnblogs.com/gczr/p/14693829.html
https://zhuanlan.zhihu.com/p/379722366
"},{"location":"AI/pca/","title":"\u6570\u636e\u7684\u5411\u91cf\u8868\u793a\u53ca\u964d\u7ef4\u95ee\u9898","text":"\u6ce8\u660e\uff1a\u6765\u6e90
\u4e0b\u9762\u6211\u4eec\u7ee7\u7eed\u5728\u4e8c\u7ef4\u7a7a\u95f4\u5185\u8ba8\u8bba\u5411\u91cf\u3002\u4e0a\u6587\u8bf4\u8fc7\uff0c\u4e00\u4e2a\u4e8c\u7ef4\u5411\u91cf\u53ef\u4ee5\u5bf9\u5e94\u4e8c\u7ef4\u7b1b\u5361\u5c14\u76f4\u89d2\u5750\u6807\u7cfb\u4e2d\u4ece\u539f\u70b9\u51fa\u53d1\u7684\u4e00\u4e2a\u6709\u5411\u7ebf\u6bb5\u3002\u4f8b\u5982\u4e0b\u9762\u8fd9\u4e2a\u5411\u91cf\uff1a
\u5728\u4ee3\u6570\u8868\u793a\u65b9\u9762\uff0c\u6211\u4eec\u7ecf\u5e38\u7528\u7ebf\u6bb5\u7ec8\u70b9\u7684\u70b9\u5750\u6807\u8868\u793a\u5411\u91cf\uff0c\u4f8b\u5982\u4e0a\u9762\u7684\u5411\u91cf\u53ef\u4ee5\u8868\u793a\u4e3a(3,2)\uff0c\u8fd9\u662f\u6211\u4eec\u518d\u719f\u6089\u4e0d\u8fc7\u7684\u5411\u91cf\u8868\u793a\u3002
\u4e0d\u8fc7\u6211\u4eec\u5e38\u5e38\u5ffd\u7565\uff0c\u53ea\u6709\u4e00\u4e2a(3,2)\u672c\u8eab\u662f\u4e0d\u80fd\u591f\u7cbe\u786e\u8868\u793a\u4e00\u4e2a\u5411\u91cf\u7684\u3002\u6211\u4eec\u4ed4\u7ec6\u770b\u4e00\u4e0b\uff0c\u8fd9\u91cc\u76843\u5b9e\u9645\u8868\u793a\u7684\u662f\u5411\u91cf\u5728x\u8f74\u4e0a\u7684\u6295\u5f71\u503c\u662f3\uff0c\u5728y\u8f74\u4e0a\u7684\u6295\u5f71\u503c\u662f2\u3002\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u5176\u5b9e\u9690\u5f0f\u5f15\u5165\u4e86\u4e00\u4e2a\u5b9a\u4e49\uff1a\u4ee5x\u8f74\u548cy\u8f74\u4e0a\u6b63\u65b9\u5411\u957f\u5ea6\u4e3a1\u7684\u5411\u91cf\u4e3a\u6807\u51c6\u3002\u90a3\u4e48\u4e00\u4e2a\u5411\u91cf(3,2)\u5b9e\u9645\u662f\u8bf4\u5728x\u8f74\u6295\u5f71\u4e3a3\u800cy\u8f74\u7684\u6295\u5f71\u4e3a2\u3002\u6ce8\u610f\u6295\u5f71\u662f\u4e00\u4e2a\u77e2\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u4e3a\u8d1f\u3002
\u66f4\u6b63\u5f0f\u7684\u8bf4\uff0c\u5411\u91cf(x,y)\u5b9e\u9645\u4e0a\u8868\u793a\u7ebf\u6027\u7ec4\u5408\uff1a
\\[x(1,0)^\\mathsf{T}+y(0,1)^\\mathsf{T}\\]\u4e0d\u96be\u8bc1\u660e\u6240\u6709\u4e8c\u7ef4\u5411\u91cf\u90fd\u53ef\u4ee5\u8868\u793a\u4e3a\u8fd9\u6837\u7684\u7ebf\u6027\u7ec4\u5408\u3002\u6b64\u5904(1,0)\u548c(0,1)\u53eb\u505a\u4e8c\u7ef4\u7a7a\u95f4\u4e2d\u7684\u4e00\u7ec4\u57fa\u3002
\u6240\u4ee5\uff0c\u8981\u51c6\u786e\u63cf\u8ff0\u5411\u91cf\uff0c\u9996\u5148\u8981\u786e\u5b9a\u4e00\u7ec4\u57fa\uff0c\u7136\u540e\u7ed9\u51fa\u5728\u57fa\u6240\u5728\u7684\u5404\u4e2a\u76f4\u7ebf\u4e0a\u7684\u6295\u5f71\u503c\uff0c\u5c31\u53ef\u4ee5\u4e86\u3002\u53ea\u4e0d\u8fc7\u6211\u4eec\u7ecf\u5e38\u7701\u7565\u7b2c\u4e00\u6b65\uff0c\u800c\u9ed8\u8ba4\u4ee5(1,0)\u548c(0,1)\u4e3a\u57fa\u3002
\u6211\u4eec\u4e4b\u6240\u4ee5\u9ed8\u8ba4\u9009\u62e9(1,0)\u548c(0,1)\u4e3a\u57fa\uff0c\u5f53\u7136\u662f\u6bd4\u8f83\u65b9\u4fbf\uff0c\u56e0\u4e3a\u5b83\u4eec\u5206\u522b\u662fx\u548cy\u8f74\u6b63\u65b9\u5411\u4e0a\u7684\u5355\u4f4d\u5411\u91cf\uff0c\u56e0\u6b64\u5c31\u4f7f\u5f97\u4e8c\u7ef4\u5e73\u9762\u4e0a\u70b9\u5750\u6807\u548c\u5411\u91cf\u4e00\u4e00\u5bf9\u5e94\uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u4f46\u5b9e\u9645\u4e0a\u4efb\u4f55\u4e24\u4e2a\u7ebf\u6027\u65e0\u5173\u7684\u4e8c\u7ef4\u5411\u91cf\u90fd\u53ef\u4ee5\u6210\u4e3a\u4e00\u7ec4\u57fa\uff0c\u6240\u8c13\u7ebf\u6027\u65e0\u5173\u5728\u4e8c\u7ef4\u5e73\u9762\u5185\u53ef\u4ee5\u76f4\u89c2\u8ba4\u4e3a\u662f\u4e24\u4e2a\u4e0d\u5728\u4e00\u6761\u76f4\u7ebf\u4e0a\u7684\u5411\u91cf\u3002
\u4f8b\u5982\uff0c(1,1)\u548c(-1,1)\u4e5f\u53ef\u4ee5\u6210\u4e3a\u4e00\u7ec4\u57fa\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u5e0c\u671b\u57fa\u7684\u6a21\u662f1\uff0c\u56e0\u4e3a\u4ece\u5185\u79ef\u7684\u610f\u4e49\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u57fa\u7684\u6a21\u662f1\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u7528\u5411\u91cf\u70b9\u4e58\u57fa\u800c\u76f4\u63a5\u83b7\u5f97\u5176\u5728\u65b0\u57fa\u4e0a\u7684\u5750\u6807\u4e86\uff01\u5b9e\u9645\u4e0a\uff0c\u5bf9\u5e94\u4efb\u4f55\u4e00\u4e2a\u5411\u91cf\u6211\u4eec\u603b\u53ef\u4ee5\u627e\u5230\u5176\u540c\u65b9\u5411\u4e0a\u6a21\u4e3a1\u7684\u5411\u91cf\uff0c\u53ea\u8981\u8ba9\u4e24\u4e2a\u5206\u91cf\u5206\u522b\u9664\u4ee5\u6a21\u5c31\u597d\u4e86\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u57fa\u53ef\u4ee5\u53d8\u4e3a\\((\\frac{1}{\\sqrt{2}},\\frac{1}{\\sqrt{2}})\\)\u548c\\((-\\frac{1}{\\sqrt{2}},\\frac{1}{\\sqrt{2}})\\)\u3002
\u73b0\u5728\uff0c\u6211\u4eec\u60f3\u83b7\u5f97(3,2)\u5728\u65b0\u57fa\u4e0a\u7684\u5750\u6807\uff0c\u5373\u5728\u4e24\u4e2a\u65b9\u5411\u4e0a\u7684\u6295\u5f71\u77e2\u91cf\u503c\uff0c\u90a3\u4e48\u6839\u636e\u5185\u79ef\u7684\u51e0\u4f55\u610f\u4e49\uff0c\u6211\u4eec\u53ea\u8981\u5206\u522b\u8ba1\u7b97(3,2)\u548c\u4e24\u4e2a\u57fa\u7684\u5185\u79ef\uff0c\u4e0d\u96be\u5f97\u5230\u65b0\u7684\u5750\u6807\u4e3a\\((\\frac{5}{\\sqrt{2}},-\\frac{1}{\\sqrt{2}})\\)\u3002\u4e0b\u56fe\u7ed9\u51fa\u4e86\u65b0\u7684\u57fa\u4ee5\u53ca(3,2)\u5728\u65b0\u57fa\u4e0a\u5750\u6807\u503c\u7684\u793a\u610f\u56fe\uff1a
\u53e6\u5916\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u5217\u4e3e\u7684\u4f8b\u5b50\u4e2d\u57fa\u662f\u6b63\u4ea4\u7684\uff08\u5373\u5185\u79ef\u4e3a0\uff0c\u6216\u76f4\u89c2\u8bf4\u76f8\u4e92\u5782\u76f4\uff09\uff0c\u4f46\u53ef\u4ee5\u6210\u4e3a\u4e00\u7ec4\u57fa\u7684\u552f\u4e00\u8981\u6c42\u5c31\u662f\u7ebf\u6027\u65e0\u5173\uff0c\u975e\u6b63\u4ea4\u7684\u57fa\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u4e0d\u8fc7\u56e0\u4e3a\u6b63\u4ea4\u57fa\u6709\u8f83\u597d\u7684\u6027\u8d28\uff0c\u6240\u4ee5\u4e00\u822c\u4f7f\u7528\u7684\u57fa\u90fd\u662f\u6b63\u4ea4\u7684\u3002
\u4e0b\u9762\u6211\u4eec\u627e\u4e00\u79cd\u7b80\u4fbf\u7684\u65b9\u5f0f\u6765\u8868\u793a\u57fa\u53d8\u6362\u3002\u8fd8\u662f\u62ff\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u60f3\u4e00\u4e0b\uff0c\u5c06(3,2)\u53d8\u6362\u4e3a\u65b0\u57fa\u4e0a\u7684\u5750\u6807\uff0c\u5c31\u662f\u7528(3,2)\u4e0e\u7b2c\u4e00\u4e2a\u57fa\u505a\u5185\u79ef\u8fd0\u7b97\uff0c\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u65b0\u7684\u5750\u6807\u5206\u91cf\uff0c\u7136\u540e\u7528(3,2)\u4e0e\u7b2c\u4e8c\u4e2a\u57fa\u505a\u5185\u79ef\u8fd0\u7b97\uff0c\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u65b0\u5750\u6807\u7684\u5206\u91cf\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u77e9\u9635\u76f8\u4e58\u7684\u5f62\u5f0f\u7b80\u6d01\u7684\u8868\u793a\u8fd9\u4e2a\u53d8\u6362\uff1a
\\[\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\\\ -1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix} \\begin{pmatrix} 3 \\\\ 2 \\end{pmatrix} = \\begin{pmatrix} 5/\\sqrt{2} \\\\ -1/\\sqrt{2} \\end{pmatrix}\\]\u592a\u6f02\u4eae\u4e86\uff01\u5176\u4e2d\u77e9\u9635\u7684\u4e24\u884c\u5206\u522b\u4e3a\u4e24\u4e2a\u57fa\uff0c\u4e58\u4ee5\u539f\u5411\u91cf\uff0c\u5176\u7ed3\u679c\u521a\u597d\u4e3a\u65b0\u57fa\u7684\u5750\u6807\u3002\u53ef\u4ee5\u7a0d\u5fae\u63a8\u5e7f\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u6709m\u4e2a\u4e8c\u7ef4\u5411\u91cf\uff0c\u53ea\u8981\u5c06\u4e8c\u7ef4\u5411\u91cf\u6309\u5217\u6392\u6210\u4e00\u4e2a\u4e24\u884cm\u5217\u77e9\u9635\uff0c\u7136\u540e\u7528\u201c\u57fa\u77e9\u9635\u201d\u4e58\u4ee5\u8fd9\u4e2a\u77e9\u9635\uff0c\u5c31\u5f97\u5230\u4e86\u6240\u6709\u8fd9\u4e9b\u5411\u91cf\u5728\u65b0\u57fa\u4e0b\u7684\u503c\u3002\u4f8b\u5982(1,1)\uff0c(2,2)\uff0c(3,3)\uff0c\u60f3\u53d8\u6362\u5230\u521a\u624d\u90a3\u7ec4\u57fa\u4e0a\uff0c\u5219\u53ef\u4ee5\u8fd9\u6837\u8868\u793a\uff1a
\\[\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\\\ -1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix} \\begin{pmatrix} 1 & 2 & 3 \\\\ 1 & 2 & 3 \\end{pmatrix} = \\begin{pmatrix} 2/\\sqrt{2} & 4/\\sqrt{2} & 6/\\sqrt{2} \\\\ 0 & 0 & 0 \\end{pmatrix}\\]\u4e8e\u662f\u4e00\u7ec4\u5411\u91cf\u7684\u57fa\u53d8\u6362\u88ab\u5e72\u51c0\u7684\u8868\u793a\u4e3a\u77e9\u9635\u7684\u76f8\u4e58\u3002
\u4e00\u822c\u7684\uff0c\u5982\u679c\u6211\u4eec\u6709M\u4e2aN\u7ef4\u5411\u91cf\uff0c\u60f3\u5c06\u5176\u53d8\u6362\u4e3a\u7531R\u4e2aN\u7ef4\u5411\u91cf\u8868\u793a\u7684\u65b0\u7a7a\u95f4\u4e2d\uff0c\u90a3\u4e48\u9996\u5148\u5c06R\u4e2a\u57fa\u6309\u884c\u7ec4\u6210\u77e9\u9635A\uff0c\u7136\u540e\u5c06\u5411\u91cf\u6309\u5217\u7ec4\u6210\u77e9\u9635B\uff0c\u90a3\u4e48\u4e24\u77e9\u9635\u7684\u4e58\u79efAB\u5c31\u662f\u53d8\u6362\u7ed3\u679c\uff0c\u5176\u4e2dAB\u7684\u7b2cm\u5217\u4e3aA\u4e2d\u7b2cm\u5217\u53d8\u6362\u540e\u7684\u7ed3\u679c\u3002
\u6570\u5b66\u8868\u793a\u4e3a\uff1a
\\[\\begin{pmatrix} p_1 \\\\ p_2 \\\\ \\vdots \\\\ p_R \\end{pmatrix} \\begin{pmatrix} a_1 & a_2 & \\cdots & a_M \\end{pmatrix} = \\begin{pmatrix} p_1a_1 & p_1a_2 & \\cdots & p_1a_M \\\\ p_2a_1 & p_2a_2 & \\cdots & p_2a_M \\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\ p_Ra_1 & p_Ra_2 & \\cdots & p_Ra_M \\end{pmatrix}\\]\u5176\u4e2d\\(p_i\\)\u662f\u4e00\u4e2a\u884c\u5411\u91cf\uff0c\u8868\u793a\u7b2ci\u4e2a\u57fa\uff0c\\(a_j\\)\u662f\u4e00\u4e2a\u5217\u5411\u91cf\uff0c\u8868\u793a\u7b2cj\u4e2a\u539f\u59cb\u6570\u636e\u8bb0\u5f55\u3002
\u7279\u522b\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91ccR\u53ef\u4ee5\u5c0f\u4e8eN\uff0c\u800cR\u51b3\u5b9a\u4e86\u53d8\u6362\u540e\u6570\u636e\u7684\u7ef4\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00N\u7ef4\u6570\u636e\u53d8\u6362\u5230\u66f4\u4f4e\u7ef4\u5ea6\u7684\u7a7a\u95f4\u4e2d\u53bb\uff0c\u53d8\u6362\u540e\u7684\u7ef4\u5ea6\u53d6\u51b3\u4e8e\u57fa\u7684\u6570\u91cf\u3002\u56e0\u6b64\u8fd9\u79cd\u77e9\u9635\u76f8\u4e58\u7684\u8868\u793a\u4e5f\u53ef\u4ee5\u8868\u793a\u964d\u7ef4\u53d8\u6362\u3002
\u6700\u540e\uff0c\u4e0a\u8ff0\u5206\u6790\u540c\u65f6\u7ed9\u77e9\u9635\u76f8\u4e58\u627e\u5230\u4e86\u4e00\u79cd\u7269\u7406\u89e3\u91ca\uff1a\u4e24\u4e2a\u77e9\u9635\u76f8\u4e58\u7684\u610f\u4e49\u662f\u5c06\u53f3\u8fb9\u77e9\u9635\u4e2d\u7684\u6bcf\u4e00\u5217\u5217\u5411\u91cf\u53d8\u6362\u5230\u5de6\u8fb9\u77e9\u9635\u4e2d\u6bcf\u4e00\u884c\u884c\u5411\u91cf\u4e3a\u57fa\u6240\u8868\u793a\u7684\u7a7a\u95f4\u4e2d\u53bb\u3002\u66f4\u62bd\u8c61\u7684\u8bf4\uff0c\u4e00\u4e2a\u77e9\u9635\u53ef\u4ee5\u8868\u793a\u4e00\u79cd\u7ebf\u6027\u53d8\u6362\u3002\u5f88\u591a\u540c\u5b66\u5728\u5b66\u7ebf\u6027\u4ee3\u6570\u65f6\u5bf9\u77e9\u9635\u76f8\u4e58\u7684\u65b9\u6cd5\u611f\u5230\u5947\u602a\uff0c\u4f46\u662f\u5982\u679c\u660e\u767d\u4e86\u77e9\u9635\u76f8\u4e58\u7684\u7269\u7406\u610f\u4e49\uff0c\u5176\u5408\u7406\u6027\u5c31\u4e00\u76ee\u4e86\u7136\u4e86\u3002
"},{"location":"AI/pca/#_2","title":"\u534f\u65b9\u5dee\u77e9\u9635\u53ca\u4f18\u5316\u76ee\u6807","text":"\u4e0a\u9762\u6211\u4eec\u8ba8\u8bba\u4e86\u9009\u62e9\u4e0d\u540c\u7684\u57fa\u53ef\u4ee5\u5bf9\u540c\u6837\u4e00\u7ec4\u6570\u636e\u7ed9\u51fa\u4e0d\u540c\u7684\u8868\u793a\uff0c\u800c\u4e14\u5982\u679c\u57fa\u7684\u6570\u91cf\u5c11\u4e8e\u5411\u91cf\u672c\u8eab\u7684\u7ef4\u6570\uff0c\u5219\u53ef\u4ee5\u8fbe\u5230\u964d\u7ef4\u7684\u6548\u679c\u3002\u4f46\u662f\u6211\u4eec\u8fd8\u6ca1\u6709\u56de\u7b54\u4e00\u4e2a\u6700\u6700\u5173\u952e\u7684\u95ee\u9898\uff1a\u5982\u4f55\u9009\u62e9\u57fa\u624d\u662f\u6700\u4f18\u7684\u3002\u6216\u8005\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u7ec4N\u7ef4\u5411\u91cf\uff0c\u73b0\u5728\u8981\u5c06\u5176\u964d\u5230K\u7ef4\uff08K\u5c0f\u4e8eN\uff09\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u9009\u62e9K\u4e2a\u57fa\u624d\u80fd\u6700\u5927\u7a0b\u5ea6\u4fdd\u7559\u539f\u6709\u7684\u4fe1\u606f\uff1f
\u8981\u5b8c\u5168\u6570\u5b66\u5316\u8fd9\u4e2a\u95ee\u9898\u975e\u5e38\u7e41\u6742\uff0c\u8fd9\u91cc\u6211\u4eec\u7528\u4e00\u79cd\u975e\u5f62\u5f0f\u5316\u7684\u76f4\u89c2\u65b9\u6cd5\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002
\u4e3a\u4e86\u907f\u514d\u8fc7\u4e8e\u62bd\u8c61\u7684\u8ba8\u8bba\uff0c\u6211\u4eec\u4ecd\u4ee5\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u5c55\u5f00\u3002\u5047\u8bbe\u6211\u4eec\u7684\u6570\u636e\u7531\u4e94\u6761\u8bb0\u5f55\u7ec4\u6210\uff0c\u5c06\u5b83\u4eec\u8868\u793a\u6210\u77e9\u9635\u5f62\u5f0f\uff1a
\\[\\begin{pmatrix} 1 & 1 & 2 & 4 & 2 \\\\ 1 & 3 & 3 & 4 & 4 \\end{pmatrix}\\]\u5176\u4e2d\u6bcf\u4e00\u5217\u4e3a\u4e00\u6761\u6570\u636e\u8bb0\u5f55\uff0c\u800c\u4e00\u884c\u4e3a\u4e00\u4e2a\u5b57\u6bb5\u3002\u4e3a\u4e86\u540e\u7eed\u5904\u7406\u65b9\u4fbf\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6bcf\u4e2a\u5b57\u6bb5\u5185\u6240\u6709\u503c\u90fd\u51cf\u53bb\u5b57\u6bb5\u5747\u503c\uff0c\u5176\u7ed3\u679c\u662f\u5c06\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u53d8\u4e3a\u5747\u503c\u4e3a0\uff08\u8fd9\u6837\u505a\u7684\u9053\u7406\u548c\u597d\u5904\u540e\u9762\u4f1a\u770b\u5230\uff09\u3002
\u6211\u4eec\u770b\u4e0a\u9762\u7684\u6570\u636e\uff0c\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u5747\u503c\u4e3a2\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u5747\u503c\u4e3a3\uff0c\u6240\u4ee5\u53d8\u6362\u540e\uff1a
\\[\\begin{pmatrix} -1 & -1 & 0 & 2 & 0 \\\\ -2 & 0 & 0 & 1 & 1 \\end{pmatrix}\\]\u6211\u4eec\u53ef\u4ee5\u770b\u4e0b\u4e94\u6761\u6570\u636e\u5728\u5e73\u9762\u76f4\u89d2\u5750\u6807\u7cfb\u5185\u7684\u6837\u5b50\uff1a
\u73b0\u5728\u95ee\u9898\u6765\u4e86\uff1a\u5982\u679c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u4e00\u7ef4\u6765\u8868\u793a\u8fd9\u4e9b\u6570\u636e\uff0c\u53c8\u5e0c\u671b\u5c3d\u91cf\u4fdd\u7559\u539f\u59cb\u7684\u4fe1\u606f\uff0c\u4f60\u8981\u5982\u4f55\u9009\u62e9\uff1f
\u901a\u8fc7\u4e0a\u4e00\u8282\u5bf9\u57fa\u53d8\u6362\u7684\u8ba8\u8bba\u6211\u4eec\u77e5\u9053\uff0c\u8fd9\u4e2a\u95ee\u9898\u5b9e\u9645\u4e0a\u662f\u8981\u5728\u4e8c\u7ef4\u5e73\u9762\u4e2d\u9009\u62e9\u4e00\u4e2a\u65b9\u5411\uff0c\u5c06\u6240\u6709\u6570\u636e\u90fd\u6295\u5f71\u5230\u8fd9\u4e2a\u65b9\u5411\u6240\u5728\u76f4\u7ebf\u4e0a\uff0c\u7528\u6295\u5f71\u503c\u8868\u793a\u539f\u59cb\u8bb0\u5f55\u3002\u8fd9\u662f\u4e00\u4e2a\u5b9e\u9645\u7684\u4e8c\u7ef4\u964d\u5230\u4e00\u7ef4\u7684\u95ee\u9898\u3002
\u90a3\u4e48\u5982\u4f55\u9009\u62e9\u8fd9\u4e2a\u65b9\u5411\uff08\u6216\u8005\u8bf4\u57fa\uff09\u624d\u80fd\u5c3d\u91cf\u4fdd\u7559\u6700\u591a\u7684\u539f\u59cb\u4fe1\u606f\u5462\uff1f\u4e00\u79cd\u76f4\u89c2\u7684\u770b\u6cd5\u662f\uff1a\u5e0c\u671b\u6295\u5f71\u540e\u7684\u6295\u5f71\u503c\u5c3d\u53ef\u80fd\u5206\u6563\u3002
\u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u770b\u51fa\u5982\u679c\u5411x\u8f74\u6295\u5f71\uff0c\u90a3\u4e48\u6700\u5de6\u8fb9\u7684\u4e24\u4e2a\u70b9\u4f1a\u91cd\u53e0\u5728\u4e00\u8d77\uff0c\u4e2d\u95f4\u7684\u4e24\u4e2a\u70b9\u4e5f\u4f1a\u91cd\u53e0\u5728\u4e00\u8d77\uff0c\u4e8e\u662f\u672c\u8eab\u56db\u4e2a\u5404\u4e0d\u76f8\u540c\u7684\u4e8c\u7ef4\u70b9\u6295\u5f71\u540e\u53ea\u5269\u4e0b\u4e24\u4e2a\u4e0d\u540c\u7684\u503c\u4e86\uff0c\u8fd9\u662f\u4e00\u79cd\u4e25\u91cd\u7684\u4fe1\u606f\u4e22\u5931\uff0c\u540c\u7406\uff0c\u5982\u679c\u5411y\u8f74\u6295\u5f71\u6700\u4e0a\u9762\u7684\u4e24\u4e2a\u70b9\u548c\u5206\u5e03\u5728x\u8f74\u4e0a\u7684\u4e24\u4e2a\u70b9\u4e5f\u4f1a\u91cd\u53e0\u3002\u6240\u4ee5\u770b\u6765x\u548cy\u8f74\u90fd\u4e0d\u662f\u6700\u597d\u7684\u6295\u5f71\u9009\u62e9\u3002\u6211\u4eec\u76f4\u89c2\u76ee\u6d4b\uff0c\u5982\u679c\u5411\u901a\u8fc7\u7b2c\u4e00\u8c61\u9650\u548c\u7b2c\u4e09\u8c61\u9650\u7684\u659c\u7ebf\u6295\u5f71\uff0c\u5219\u4e94\u4e2a\u70b9\u5728\u6295\u5f71\u540e\u8fd8\u662f\u53ef\u4ee5\u533a\u5206\u7684\u3002
\u4e0b\u9762\uff0c\u6211\u4eec\u7528\u6570\u5b66\u65b9\u6cd5\u8868\u8ff0\u8fd9\u4e2a\u95ee\u9898\u3002
"},{"location":"AI/pca/#_3","title":"\u65b9\u5dee","text":"\u4e0a\u6587\u8bf4\u5230\uff0c\u6211\u4eec\u5e0c\u671b\u6295\u5f71\u540e\u6295\u5f71\u503c\u5c3d\u53ef\u80fd\u5206\u6563\uff0c\u800c\u8fd9\u79cd\u5206\u6563\u7a0b\u5ea6\uff0c\u53ef\u4ee5\u7528\u6570\u5b66\u4e0a\u7684\u65b9\u5dee\u6765\u8868\u8ff0\u3002\u6b64\u5904\uff0c\u4e00\u4e2a\u5b57\u6bb5\u7684\u65b9\u5dee\u53ef\u4ee5\u770b\u505a\u662f\u6bcf\u4e2a\u5143\u7d20\u4e0e\u5b57\u6bb5\u5747\u503c\u7684\u5dee\u7684\u5e73\u65b9\u548c\u7684\u5747\u503c\uff0c\u5373\uff1a
\\[Var(a)=\\frac{1}{m}\\sum_{i=1}^m{(a_i-\\mu)^2}\\]\u7531\u4e8e\u4e0a\u9762\u6211\u4eec\u5df2\u7ecf\u5c06\u6bcf\u4e2a\u5b57\u6bb5\u7684\u5747\u503c\u90fd\u5316\u4e3a0\u4e86\uff0c\u56e0\u6b64\u65b9\u5dee\u53ef\u4ee5\u76f4\u63a5\u7528\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u548c\u9664\u4ee5\u5143\u7d20\u4e2a\u6570\u8868\u793a\uff1a
\\[Var(a)=\\frac{1}{m}\\sum_{i=1}^m{a_i^2}\\]\u4e8e\u662f\u4e0a\u9762\u7684\u95ee\u9898\u88ab\u5f62\u5f0f\u5316\u8868\u8ff0\u4e3a\uff1a\u5bfb\u627e\u4e00\u4e2a\u4e00\u7ef4\u57fa\uff0c\u4f7f\u5f97\u6240\u6709\u6570\u636e\u53d8\u6362\u4e3a\u8fd9\u4e2a\u57fa\u4e0a\u7684\u5750\u6807\u8868\u793a\u540e\uff0c\u65b9\u5dee\u503c\u6700\u5927\u3002
"},{"location":"AI/pca/#_4","title":"\u534f\u65b9\u5dee","text":"\u5bf9\u4e8e\u4e0a\u9762\u4e8c\u7ef4\u964d\u6210\u4e00\u7ef4\u7684\u95ee\u9898\u6765\u8bf4\uff0c\u627e\u5230\u90a3\u4e2a\u4f7f\u5f97\u65b9\u5dee\u6700\u5927\u7684\u65b9\u5411\u5c31\u53ef\u4ee5\u4e86\u3002\u4e0d\u8fc7\u5bf9\u4e8e\u66f4\u9ad8\u7ef4\uff0c\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\u9700\u8981\u89e3\u51b3\u3002\u8003\u8651\u4e09\u7ef4\u964d\u5230\u4e8c\u7ef4\u95ee\u9898\u3002\u4e0e\u4e4b\u524d\u76f8\u540c\uff0c\u9996\u5148\u6211\u4eec\u5e0c\u671b\u627e\u5230\u4e00\u4e2a\u65b9\u5411\u4f7f\u5f97\u6295\u5f71\u540e\u65b9\u5dee\u6700\u5927\uff0c\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u7b2c\u4e00\u4e2a\u65b9\u5411\u7684\u9009\u62e9\uff0c\u7ee7\u800c\u6211\u4eec\u9009\u62e9\u7b2c\u4e8c\u4e2a\u6295\u5f71\u65b9\u5411\u3002
\u5982\u679c\u6211\u4eec\u8fd8\u662f\u5355\u7eaf\u53ea\u9009\u62e9\u65b9\u5dee\u6700\u5927\u7684\u65b9\u5411\uff0c\u5f88\u660e\u663e\uff0c\u8fd9\u4e2a\u65b9\u5411\u4e0e\u7b2c\u4e00\u4e2a\u65b9\u5411\u5e94\u8be5\u662f\u201c\u51e0\u4e4e\u91cd\u5408\u5728\u4e00\u8d77\u201d\uff0c\u663e\u7136\u8fd9\u6837\u7684\u7ef4\u5ea6\u662f\u6ca1\u6709\u7528\u7684\uff0c\u56e0\u6b64\uff0c\u5e94\u8be5\u6709\u5176\u4ed6\u7ea6\u675f\u6761\u4ef6\u3002\u4ece\u76f4\u89c2\u4e0a\u8bf4\uff0c\u8ba9\u4e24\u4e2a\u5b57\u6bb5\u5c3d\u53ef\u80fd\u8868\u793a\u66f4\u591a\u7684\u539f\u59cb\u4fe1\u606f\uff0c\u6211\u4eec\u662f\u4e0d\u5e0c\u671b\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\uff08\u7ebf\u6027\uff09\u76f8\u5173\u6027\u7684\uff0c\u56e0\u4e3a\u76f8\u5173\u6027\u610f\u5473\u7740\u4e24\u4e2a\u5b57\u6bb5\u4e0d\u662f\u5b8c\u5168\u72ec\u7acb\uff0c\u5fc5\u7136\u5b58\u5728\u91cd\u590d\u8868\u793a\u7684\u4fe1\u606f\u3002
\u6570\u5b66\u4e0a\u53ef\u4ee5\u7528\u4e24\u4e2a\u5b57\u6bb5\u7684\u534f\u65b9\u5dee\u8868\u793a\u5176\u76f8\u5173\u6027\uff0c\u7531\u4e8e\u5df2\u7ecf\u8ba9\u6bcf\u4e2a\u5b57\u6bb5\u5747\u503c\u4e3a0\uff0c\u5219\uff1a
\\[Cov(a,b)=\\frac{1}{m}\\sum_{i=1}^m{a_ib_i}\\]\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u5b57\u6bb5\u5747\u503c\u4e3a0\u7684\u60c5\u51b5\u4e0b\uff0c\u4e24\u4e2a\u5b57\u6bb5\u7684\u534f\u65b9\u5dee\u7b80\u6d01\u7684\u8868\u793a\u4e3a\u5176\u5185\u79ef\u9664\u4ee5\u5143\u7d20\u6570m\u3002
\u5f53\u534f\u65b9\u5dee\u4e3a0\u65f6\uff0c\u8868\u793a\u4e24\u4e2a\u5b57\u6bb5\u5b8c\u5168\u72ec\u7acb\u3002\u4e3a\u4e86\u8ba9\u534f\u65b9\u5dee\u4e3a0\uff0c\u6211\u4eec\u9009\u62e9\u7b2c\u4e8c\u4e2a\u57fa\u65f6\u53ea\u80fd\u5728\u4e0e\u7b2c\u4e00\u4e2a\u57fa\u6b63\u4ea4\u7684\u65b9\u5411\u4e0a\u9009\u62e9\u3002\u56e0\u6b64\u6700\u7ec8\u9009\u62e9\u7684\u4e24\u4e2a\u65b9\u5411\u4e00\u5b9a\u662f\u6b63\u4ea4\u7684\u3002
\u81f3\u6b64\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u964d\u7ef4\u95ee\u9898\u7684\u4f18\u5316\u76ee\u6807\uff1a\u5c06\u4e00\u7ec4N\u7ef4\u5411\u91cf\u964d\u4e3aK\u7ef4\uff08K\u5927\u4e8e0\uff0c\u5c0f\u4e8eN\uff09\uff0c\u5176\u76ee\u6807\u662f\u9009\u62e9K\u4e2a\u5355\u4f4d\uff08\u6a21\u4e3a1\uff09\u6b63\u4ea4\u57fa\uff0c\u4f7f\u5f97\u539f\u59cb\u6570\u636e\u53d8\u6362\u5230\u8fd9\u7ec4\u57fa\u4e0a\u540e\uff0c\u5404\u5b57\u6bb5\u4e24\u4e24\u95f4\u534f\u65b9\u5dee\u4e3a0\uff0c\u800c\u5b57\u6bb5\u7684\u65b9\u5dee\u5219\u5c3d\u53ef\u80fd\u5927\uff08\u5728\u6b63\u4ea4\u7684\u7ea6\u675f\u4e0b\uff0c\u53d6\u6700\u5927\u7684K\u4e2a\u65b9\u5dee\uff09\u3002
"},{"location":"AI/pca/#_5","title":"\u534f\u65b9\u5dee\u77e9\u9635","text":"\u4e0a\u9762\u6211\u4eec\u5bfc\u51fa\u4e86\u4f18\u5316\u76ee\u6807\uff0c\u4f46\u662f\u8fd9\u4e2a\u76ee\u6807\u4f3c\u4e4e\u4e0d\u80fd\u76f4\u63a5\u4f5c\u4e3a\u64cd\u4f5c\u6307\u5357\uff08\u6216\u8005\u8bf4\u7b97\u6cd5\uff09\uff0c\u56e0\u4e3a\u5b83\u53ea\u8bf4\u8981\u4ec0\u4e48\uff0c\u4f46\u6839\u672c\u6ca1\u6709\u8bf4\u600e\u4e48\u505a\u3002\u6240\u4ee5\u6211\u4eec\u8981\u7ee7\u7eed\u5728\u6570\u5b66\u4e0a\u7814\u7a76\u8ba1\u7b97\u65b9\u6848\u3002
\u6211\u4eec\u770b\u5230\uff0c\u6700\u7ec8\u8981\u8fbe\u5230\u7684\u76ee\u7684\u4e0e\u5b57\u6bb5\u5185\u65b9\u5dee\u53ca\u5b57\u6bb5\u95f4\u534f\u65b9\u5dee\u6709\u5bc6\u5207\u5173\u7cfb\u3002\u56e0\u6b64\u6211\u4eec\u5e0c\u671b\u80fd\u5c06\u4e24\u8005\u7edf\u4e00\u8868\u793a\uff0c\u4ed4\u7ec6\u89c2\u5bdf\u53d1\u73b0\uff0c\u4e24\u8005\u5747\u53ef\u4ee5\u8868\u793a\u4e3a\u5185\u79ef\u7684\u5f62\u5f0f\uff0c\u800c\u5185\u79ef\u53c8\u4e0e\u77e9\u9635\u76f8\u4e58\u5bc6\u5207\u76f8\u5173\u3002\u4e8e\u662f\u6211\u4eec\u6765\u4e86\u7075\u611f\uff1a
\u5047\u8bbe\u6211\u4eec\u53ea\u6709a\u548cb\u4e24\u4e2a\u5b57\u6bb5\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u5b83\u4eec\u6309\u884c\u7ec4\u6210\u77e9\u9635X\uff1a
\\[X=\\begin{pmatrix} a_1 & a_2 & \\cdots & a_m \\\\ b_1 & b_2 & \\cdots & b_m \\end{pmatrix}\\]\u7136\u540e\u6211\u4eec\u7528X\u4e58\u4ee5X\u7684\u8f6c\u7f6e\uff0c\u5e76\u4e58\u4e0a\u7cfb\u65701/m\uff1a
\\[\\frac{1}{m}XX^\\mathsf{T}=\\begin{pmatrix} \\frac{1}{m}\\sum_{i=1}^m{a_i^2} & \\frac{1}{m}\\sum_{i=1}^m{a_ib_i} \\\\ \\frac{1}{m}\\sum_{i=1}^m{a_ib_i} & \\frac{1}{m}\\sum_{i=1}^m{b_i^2} \\end{pmatrix}\\]\u5947\u8ff9\u51fa\u73b0\u4e86\uff01\u8fd9\u4e2a\u77e9\u9635\u5bf9\u89d2\u7ebf\u4e0a\u7684\u4e24\u4e2a\u5143\u7d20\u5206\u522b\u662f\u4e24\u4e2a\u5b57\u6bb5\u7684\u65b9\u5dee\uff0c\u800c\u5176\u5b83\u5143\u7d20\u662fa\u548cb\u7684\u534f\u65b9\u5dee\u3002\u4e24\u8005\u88ab\u7edf\u4e00\u5230\u4e86\u4e00\u4e2a\u77e9\u9635\u7684\u3002
\u6839\u636e\u77e9\u9635\u76f8\u4e58\u7684\u8fd0\u7b97\u6cd5\u5219\uff0c\u8fd9\u4e2a\u7ed3\u8bba\u5f88\u5bb9\u6613\u88ab\u63a8\u5e7f\u5230\u4e00\u822c\u60c5\u51b5\uff1a
\u8bbe\u6211\u4eec\u6709m\u4e2an\u7ef4\u6570\u636e\u8bb0\u5f55\uff0c\u5c06\u5176\u6309\u5217\u6392\u6210n\u4e58m\u7684\u77e9\u9635X\uff0c\u8bbe\\(C=\\frac{1}{m}XX^\\mathsf{T}\\)\uff0c\u5219C\u662f\u4e00\u4e2a\u5bf9\u79f0\u77e9\u9635\uff0c\u5176\u5bf9\u89d2\u7ebf\u5206\u522b\u4e2a\u5404\u4e2a\u5b57\u6bb5\u7684\u65b9\u5dee\uff0c\u800c\u7b2ci\u884cj\u5217\u548cj\u884ci\u5217\u5143\u7d20\u76f8\u540c\uff0c\u8868\u793ai\u548cj\u4e24\u4e2a\u5b57\u6bb5\u7684\u534f\u65b9\u5dee\u3002
"},{"location":"AI/pca/#_6","title":"\u534f\u65b9\u5dee\u77e9\u9635\u5bf9\u89d2\u5316","text":"\u6839\u636e\u4e0a\u8ff0\u63a8\u5bfc\uff0c\u6211\u4eec\u53d1\u73b0\u8981\u8fbe\u5230\u4f18\u5316\u76ee\u524d\uff0c\u7b49\u4ef7\u4e8e\u5c06\u534f\u65b9\u5dee\u77e9\u9635\u5bf9\u89d2\u5316\uff1a\u5373\u9664\u5bf9\u89d2\u7ebf\u5916\u7684\u5176\u5b83\u5143\u7d20\u5316\u4e3a0\uff0c\u5e76\u4e14\u5728\u5bf9\u89d2\u7ebf\u4e0a\u5c06\u5143\u7d20\u6309\u5927\u5c0f\u4ece\u4e0a\u5230\u4e0b\u6392\u5217\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u8fbe\u5230\u4e86\u4f18\u5316\u76ee\u7684\u3002\u8fd9\u6837\u8bf4\u53ef\u80fd\u8fd8\u4e0d\u662f\u5f88\u660e\u6670\uff0c\u6211\u4eec\u8fdb\u4e00\u6b65\u770b\u4e0b\u539f\u77e9\u9635\u4e0e\u57fa\u53d8\u6362\u540e\u77e9\u9635\u534f\u65b9\u5dee\u77e9\u9635\u7684\u5173\u7cfb\uff1a
\u8bbe\u539f\u59cb\u6570\u636e\u77e9\u9635X\u5bf9\u5e94\u7684\u534f\u65b9\u5dee\u77e9\u9635\u4e3aC\uff0c\u800cP\u662f\u4e00\u7ec4\u57fa\u6309\u884c\u7ec4\u6210\u7684\u77e9\u9635\uff0c\u8bbeY=PX\uff0c\u5219Y\u4e3aX\u5bf9P\u505a\u57fa\u53d8\u6362\u540e\u7684\u6570\u636e\u3002\u8bbeY\u7684\u534f\u65b9\u5dee\u77e9\u9635\u4e3aD\uff0c\u6211\u4eec\u63a8\u5bfc\u4e00\u4e0bD\u4e0eC\u7684\u5173\u7cfb\uff1a
\\[\\begin{array}{l l l} D & = & \\frac{1}{m}YY^\\mathsf{T} \\\\ & = & \\frac{1}{m}(PX)(PX)^\\mathsf{T} \\\\ & = & \\frac{1}{m}PXX^\\mathsf{T}P^\\mathsf{T} \\\\ & = & P(\\frac{1}{m}XX^\\mathsf{T})P^\\mathsf{T} \\\\ & = & PCP^\\mathsf{T} \\end{array}\\]\u73b0\u5728\u4e8b\u60c5\u5f88\u660e\u767d\u4e86\uff01\u6211\u4eec\u8981\u627e\u7684P\u4e0d\u662f\u522b\u7684\uff0c\u800c\u662f\u80fd\u8ba9\u539f\u59cb\u534f\u65b9\u5dee\u77e9\u9635\u5bf9\u89d2\u5316\u7684P\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f18\u5316\u76ee\u6807\u53d8\u6210\u4e86\u5bfb\u627e\u4e00\u4e2a\u77e9\u9635P\uff0c\u6ee1\u8db3\\(PCP^\\mathsf{T}\\)\u662f\u4e00\u4e2a\u5bf9\u89d2\u77e9\u9635\uff0c\u5e76\u4e14\u5bf9\u89d2\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u4f9d\u6b21\u6392\u5217\uff0c\u90a3\u4e48P\u7684\u524dK\u884c\u5c31\u662f\u8981\u5bfb\u627e\u7684\u57fa\uff0c\u7528P\u7684\u524dK\u884c\u7ec4\u6210\u7684\u77e9\u9635\u4e58\u4ee5X\u5c31\u4f7f\u5f97X\u4eceN\u7ef4\u964d\u5230\u4e86K\u7ef4\u5e76\u6ee1\u8db3\u4e0a\u8ff0\u4f18\u5316\u6761\u4ef6\u3002
\u81f3\u6b64\uff0c\u6211\u4eec\u79bb\u201c\u53d1\u660e\u201dPCA\u8fd8\u6709\u4ec5\u4e00\u6b65\u4e4b\u9065\uff01
\u73b0\u5728\u6240\u6709\u7126\u70b9\u90fd\u805a\u7126\u5728\u4e86\u534f\u65b9\u5dee\u77e9\u9635\u5bf9\u89d2\u5316\u95ee\u9898\u4e0a\uff0c\u6709\u65f6\uff0c\u6211\u4eec\u771f\u5e94\u8be5\u611f\u8c22\u6570\u5b66\u5bb6\u7684\u5148\u884c\uff0c\u56e0\u4e3a\u77e9\u9635\u5bf9\u89d2\u5316\u5728\u7ebf\u6027\u4ee3\u6570\u9886\u57df\u5df2\u7ecf\u5c5e\u4e8e\u88ab\u73a9\u70c2\u4e86\u7684\u4e1c\u897f\uff0c\u6240\u4ee5\u8fd9\u5728\u6570\u5b66\u4e0a\u6839\u672c\u4e0d\u662f\u95ee\u9898\u3002
\u7531\u4e0a\u6587\u77e5\u9053\uff0c\u534f\u65b9\u5dee\u77e9\u9635C\u662f\u4e00\u4e2a\u662f\u5bf9\u79f0\u77e9\u9635\uff0c\u5728\u7ebf\u6027\u4ee3\u6570\u4e0a\uff0c\u5b9e\u5bf9\u79f0\u77e9\u9635\u6709\u4e00\u7cfb\u5217\u975e\u5e38\u597d\u7684\u6027\u8d28\uff1a
1\uff09\u5b9e\u5bf9\u79f0\u77e9\u9635\u4e0d\u540c\u7279\u5f81\u503c\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u5fc5\u7136\u6b63\u4ea4\u3002
2\uff09\u8bbe\u7279\u5f81\u5411\u91cf\\(\\lambda\\)\u91cd\u6570\u4e3ar\uff0c\u5219\u5fc5\u7136\u5b58\u5728r\u4e2a\u7ebf\u6027\u65e0\u5173\u7684\u7279\u5f81\u5411\u91cf\u5bf9\u5e94\u4e8e\\(\\lambda\\)\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c06\u8fd9r\u4e2a\u7279\u5f81\u5411\u91cf\u5355\u4f4d\u6b63\u4ea4\u5316\u3002
\u7531\u4e0a\u9762\u4e24\u6761\u53ef\u77e5\uff0c\u4e00\u4e2an\u884cn\u5217\u7684\u5b9e\u5bf9\u79f0\u77e9\u9635\u4e00\u5b9a\u53ef\u4ee5\u627e\u5230n\u4e2a\u5355\u4f4d\u6b63\u4ea4\u7279\u5f81\u5411\u91cf\uff0c\u8bbe\u8fd9n\u4e2a\u7279\u5f81\u5411\u91cf\u4e3a\\(e_1,e_2,\\cdots,e_n\\)\uff0c\u6211\u4eec\u5c06\u5176\u6309\u5217\u7ec4\u6210\u77e9\u9635\uff1a
\\[E=\\begin{pmatrix} e_1 & e_2 & \\cdots & e_n \\end{pmatrix}\\]\u5219\u5bf9\u534f\u65b9\u5dee\u77e9\u9635C\u6709\u5982\u4e0b\u7ed3\u8bba\uff1a
\\[E^\\mathsf{T}CE=\\Lambda=\\begin{pmatrix} \\lambda_1 & & & \\\\ & \\lambda_2 & & \\\\ & & \\ddots & \\\\ & & & \\lambda_n \\end{pmatrix}\\]\u5176\u4e2d\\(\\Lambda\\)\u4e3a\u5bf9\u89d2\u77e9\u9635\uff0c\u5176\u5bf9\u89d2\u5143\u7d20\u4e3a\u5404\u7279\u5f81\u5411\u91cf\u5bf9\u5e94\u7684\u7279\u5f81\u503c\uff08\u53ef\u80fd\u6709\u91cd\u590d\uff09\u3002
\u4ee5\u4e0a\u7ed3\u8bba\u4e0d\u518d\u7ed9\u51fa\u4e25\u683c\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u5bf9\u8bc1\u660e\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u7ebf\u6027\u4ee3\u6570\u4e66\u7c4d\u5173\u4e8e\u201c\u5b9e\u5bf9\u79f0\u77e9\u9635\u5bf9\u89d2\u5316\u201d\u7684\u5185\u5bb9\u3002
\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u53d1\u73b0\u6211\u4eec\u5df2\u7ecf\u627e\u5230\u4e86\u9700\u8981\u7684\u77e9\u9635P\uff1a
\\[P=E^\\mathsf{T}\\]P\u662f\u534f\u65b9\u5dee\u77e9\u9635\u7684\u7279\u5f81\u5411\u91cf\u5355\u4f4d\u5316\u540e\u6309\u884c\u6392\u5217\u51fa\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u90fd\u662fC\u7684\u4e00\u4e2a\u7279\u5f81\u5411\u91cf\u3002\u5982\u679c\u8bbeP\u6309\u7167\\(\\Lambda\\)\u4e2d\u7279\u5f81\u503c\u7684\u4ece\u5927\u5230\u5c0f\uff0c\u5c06\u7279\u5f81\u5411\u91cf\u4ece\u4e0a\u5230\u4e0b\u6392\u5217\uff0c\u5219\u7528P\u7684\u524dK\u884c\u7ec4\u6210\u7684\u77e9\u9635\u4e58\u4ee5\u539f\u59cb\u6570\u636e\u77e9\u9635X\uff0c\u5c31\u5f97\u5230\u4e86\u6211\u4eec\u9700\u8981\u7684\u964d\u7ef4\u540e\u7684\u6570\u636e\u77e9\u9635Y\u3002
\u81f3\u6b64\u6211\u4eec\u5b8c\u6210\u4e86\u6574\u4e2aPCA\u7684\u6570\u5b66\u539f\u7406\u8ba8\u8bba\u3002\u5728\u4e0b\u9762\u7684\u4e00\u8282\uff0c\u6211\u4eec\u5c06\u7ed9\u51faPCA\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002
"},{"location":"AI/pca/#pca","title":"PCA\u7b97\u6cd5","text":"\u603b\u7ed3\u4e00\u4e0bPCA\u7684\u7b97\u6cd5\u6b65\u9aa4\uff1a
\u8bbe\u6709m\u6761n\u7ef4\u6570\u636e\u3002
1\uff09\u5c06\u539f\u59cb\u6570\u636e\u6309\u5217\u7ec4\u6210n\u884cm\u5217\u77e9\u9635X
2\uff09\u5c06X\u7684\u6bcf\u4e00\u884c\uff08\u4ee3\u8868\u4e00\u4e2a\u5c5e\u6027\u5b57\u6bb5\uff09\u8fdb\u884c\u96f6\u5747\u503c\u5316\uff0c\u5373\u51cf\u53bb\u8fd9\u4e00\u884c\u7684\u5747\u503c
3\uff09\u6c42\u51fa\u534f\u65b9\u5dee\u77e9\u9635\\(C=\\frac{1}{m}XX^\\mathsf{T}\\)
4\uff09\u6c42\u51fa\u534f\u65b9\u5dee\u77e9\u9635\u7684\u7279\u5f81\u503c\u53ca\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf
5\uff09\u5c06\u7279\u5f81\u5411\u91cf\u6309\u5bf9\u5e94\u7279\u5f81\u503c\u5927\u5c0f\u4ece\u4e0a\u5230\u4e0b\u6309\u884c\u6392\u5217\u6210\u77e9\u9635\uff0c\u53d6\u524dk\u884c\u7ec4\u6210\u77e9\u9635P
6\uff09\\(Y=PX\\)\u5373\u4e3a\u964d\u7ef4\u5230k\u7ef4\u540e\u7684\u6570\u636e
"},{"location":"AI/pca/#_7","title":"\u5b9e\u4f8b","text":"\u8fd9\u91cc\u4ee5\u4e0a\u6587\u63d0\u5230\u7684
\\[\\begin{pmatrix} -1 & -1 & 0 & 2 & 0 \\\\ -2 & 0 & 0 & 1 & 1 \\end{pmatrix}\\]\u4e3a\u4f8b\uff0c\u6211\u4eec\u7528PCA\u65b9\u6cd5\u5c06\u8fd9\u7ec4\u4e8c\u7ef4\u6570\u636e\u5176\u964d\u5230\u4e00\u7ef4\u3002
\u56e0\u4e3a\u8fd9\u4e2a\u77e9\u9635\u7684\u6bcf\u884c\u5df2\u7ecf\u662f\u96f6\u5747\u503c\uff0c\u8fd9\u91cc\u6211\u4eec\u76f4\u63a5\u6c42\u534f\u65b9\u5dee\u77e9\u9635\uff1a
\\[C=\\frac{1}{5}\\begin{pmatrix} -1 & -1 & 0 & 2 & 0 \\\\ -2 & 0 & 0 & 1 & 1 \\end{pmatrix}\\begin{pmatrix} -1 & -2 \\\\ -1 & 0 \\\\ 0 & 0 \\\\ 2 & 1 \\\\ 0 & 1 \\end{pmatrix}=\\begin{pmatrix} \\frac{6}{5} & \\frac{4}{5} \\\\ \\frac{4}{5} & \\frac{6}{5} \\end{pmatrix}\\]\u7136\u540e\u6c42\u5176\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\uff0c\u5177\u4f53\u6c42\u89e3\u65b9\u6cd5\u4e0d\u518d\u8be6\u8ff0\uff0c\u53ef\u4ee5\u53c2\u8003\u76f8\u5173\u8d44\u6599\u3002\u6c42\u89e3\u540e\u7279\u5f81\u503c\u4e3a\uff1a
\\[\\lambda_1=2,\\lambda_2=2/5\\]\u5176\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u5206\u522b\u662f\uff1a
\\[c_1\\begin{pmatrix} 1 \\\\ 1 \\end{pmatrix},c_2\\begin{pmatrix} -1 \\\\ 1 \\end{pmatrix}\\]\u5176\u4e2d\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u5206\u522b\u662f\u4e00\u4e2a\u901a\u89e3\uff0c\\(c_1\\)\u548c\\(c_2\\)\u53ef\u53d6\u4efb\u610f\u5b9e\u6570\u3002\u90a3\u4e48\u6807\u51c6\u5316\u540e\u7684\u7279\u5f81\u5411\u91cf\u4e3a\uff1a
\\[\\begin{pmatrix} 1/\\sqrt{2} \\\\ 1/\\sqrt{2} \\end{pmatrix},\\begin{pmatrix} -1/\\sqrt{2} \\\\ 1/\\sqrt{2} \\end{pmatrix}\\]\u56e0\u6b64\u6211\u4eec\u7684\u77e9\u9635P\u662f\uff1a
\\[P=\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\\\ -1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix}\\]\u53ef\u4ee5\u9a8c\u8bc1\u534f\u65b9\u5dee\u77e9\u9635C\u7684\u5bf9\u89d2\u5316\uff1a
\\[PCP^\\mathsf{T}=\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\\\ -1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix}\\begin{pmatrix} 6/5 & 4/5 \\\\ 4/5 & 6/5 \\end{pmatrix}\\begin{pmatrix} 1/\\sqrt{2} & -1/\\sqrt{2} \\\\ 1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix}=\\begin{pmatrix} 2 & 0 \\\\ 0 & 2/5 \\end{pmatrix}\\]\u6700\u540e\u6211\u4eec\u7528P\u7684\u7b2c\u4e00\u884c\u4e58\u4ee5\u6570\u636e\u77e9\u9635\uff0c\u5c31\u5f97\u5230\u4e86\u964d\u7ef4\u540e\u7684\u8868\u793a\uff1a
\\[Y=\\begin{pmatrix} 1/\\sqrt{2} & 1/\\sqrt{2} \\end{pmatrix}\\begin{pmatrix} -1 & -1 & 0 & 2 & 0 \\\\ -2 & 0 & 0 & 1 & 1 \\end{pmatrix}=\\begin{pmatrix} -3/\\sqrt{2} & -1/\\sqrt{2} & 0 & 3/\\sqrt{2} & -1/\\sqrt{2} \\end{pmatrix}\\]\u964d\u7ef4\u6295\u5f71\u7ed3\u679c\u5982\u4e0b\u56fe\uff1a
"},{"location":"AI/pca/#_8","title":"\u8fdb\u4e00\u6b65\u8ba8\u8bba","text":"\u6839\u636e\u4e0a\u9762\u5bf9PCA\u7684\u6570\u5b66\u539f\u7406\u7684\u89e3\u91ca\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230\u4e00\u4e9bPCA\u7684\u80fd\u529b\u548c\u9650\u5236\u3002PCA\u672c\u8d28\u4e0a\u662f\u5c06\u65b9\u5dee\u6700\u5927\u7684\u65b9\u5411\u4f5c\u4e3a\u4e3b\u8981\u7279\u5f81\uff0c\u5e76\u4e14\u5728\u5404\u4e2a\u6b63\u4ea4\u65b9\u5411\u4e0a\u5c06\u6570\u636e\u201c\u79bb\u76f8\u5173\u201d\uff0c\u4e5f\u5c31\u662f\u8ba9\u5b83\u4eec\u5728\u4e0d\u540c\u6b63\u4ea4\u65b9\u5411\u4e0a\u6ca1\u6709\u76f8\u5173\u6027\u3002
\u56e0\u6b64\uff0cPCA\u4e5f\u5b58\u5728\u4e00\u4e9b\u9650\u5236\uff0c\u4f8b\u5982\u5b83\u53ef\u4ee5\u5f88\u597d\u7684\u89e3\u9664\u7ebf\u6027\u76f8\u5173\uff0c\u4f46\u662f\u5bf9\u4e8e\u9ad8\u9636\u76f8\u5173\u6027\u5c31\u6ca1\u6709\u529e\u6cd5\u4e86\uff0c\u5bf9\u4e8e\u5b58\u5728\u9ad8\u9636\u76f8\u5173\u6027\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651Kernel PCA\uff0c\u901a\u8fc7Kernel\u51fd\u6570\u5c06\u975e\u7ebf\u6027\u76f8\u5173\u8f6c\u4e3a\u7ebf\u6027\u76f8\u5173\uff0c\u5173\u4e8e\u8fd9\u70b9\u5c31\u4e0d\u5c55\u5f00\u8ba8\u8bba\u4e86\u3002\u53e6\u5916\uff0cPCA\u5047\u8bbe\u6570\u636e\u5404\u4e3b\u7279\u5f81\u662f\u5206\u5e03\u5728\u6b63\u4ea4\u65b9\u5411\u4e0a\uff0c\u5982\u679c\u5728\u975e\u6b63\u4ea4\u65b9\u5411\u4e0a\u5b58\u5728\u51e0\u4e2a\u65b9\u5dee\u8f83\u5927\u7684\u65b9\u5411\uff0cPCA\u7684\u6548\u679c\u5c31\u5927\u6253\u6298\u6263\u4e86\u3002
\u6700\u540e\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0cPCA\u662f\u4e00\u79cd\u65e0\u53c2\u6570\u6280\u672f\uff0c\u4e5f\u5c31\u662f\u8bf4\u9762\u5bf9\u540c\u6837\u7684\u6570\u636e\uff0c\u5982\u679c\u4e0d\u8003\u8651\u6e05\u6d17\uff0c\u8c01\u6765\u505a\u7ed3\u679c\u90fd\u4e00\u6837\uff0c\u6ca1\u6709\u4e3b\u89c2\u53c2\u6570\u7684\u4ecb\u5165\uff0c\u6240\u4ee5PCA\u4fbf\u4e8e\u901a\u7528\u5b9e\u73b0\uff0c\u4f46\u662f\u672c\u8eab\u65e0\u6cd5\u4e2a\u6027\u5316\u7684\u4f18\u5316\u3002
\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u670b\u53cb\u4eec\u4e86\u89e3PCA\u7684\u6570\u5b66\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u73b0\u539f\u7406\uff0c\u501f\u6b64\u4e86\u89e3PCA\u7684\u9002\u7528\u573a\u666f\u548c\u9650\u5236\uff0c\u4ece\u800c\u66f4\u597d\u7684\u4f7f\u7528\u8fd9\u4e2a\u7b97\u6cd5\u3002
"},{"location":"AI/pca/#dla","title":"DLA","text":"\\(or \\Rightarrow max Tr(w^TS_bS_w^{-1}w) constrained to\\ \\ ww^T=1\\)
\\(m1 \\ m2 \u90fd\u662f\u5e73\u5747\u503c\\)
https://zhuanlan.zhihu.com/p/32658341
"},{"location":"CG/lec1/lec1/","title":"Lecture 1 - 2D Graphics","text":""},{"location":"CG/lec1/lec1/#lecture-1-2d-graphics","title":"Lecture 1 - 2D Graphics","text":""},{"location":"CG/lec1/lec1/#rasterization","title":"Rasterization","text":""},{"location":"CG/lec1/lec1/#scan-conversion","title":"Scan Conversion","text":""},{"location":"CG/lec1/lec1/#scan-converting-a-line-segment","title":"Scan converting a line segment","text":"
Bresenham's Line Drawing Algorithm
Algorithm
void Bresenham(int x1, int y1, int x2, int y2) {\n int b = 0; //or a given value\n int dx = x2 - x1;\n int dy = y2 - y1;\n int x = x1, y = y1;\n int P0 = 2 * dy * x1 - 2 * dx * y1 + 2 * dy + (2 * b - 1) * dx;\n for (int i = 0; i < dx; i++) {\n if (P0 > 0) {\n y++;\n P0 += 2 * dy - 2 * dx;\n } else {\n P0 += 2 * dy;\n }\n x++;\n plot(x, y);\n }\n\n}\n
"},{"location":"CG/lec1/lec1/#scan-converting-a-circle","title":"Scan converting a circle","text":"Method 1: Even-Odd Test
Method II Scan Line Polygon Filling Algorithm
"},{"location":"CMU%2015-445/Chap1/Chap1/#relational","title":"Relational","text":""},{"location":"CMU%2015-445/Chap1/Chap1/#relational-model","title":"Relational Model","text":"
Store database in simple data structures (relations).
Physical storage left up to the DBMS implementation.
Access data through high-level language, DBMS figures out best execution strategy.
A relation is an unordered set that contain the relationship of attributes that represent entities.
A tuple is a set of attribute values (also known as its domain) in the relation.
n-ary Relation :Table with n columns
"},{"location":"CMU%2015-445/Chap1/Chap1/#keys","title":"Keys","text":"Primaray Keys
A relation's primary key uniquely identifies a single tuple
Foriegn Keys
A foreign key specifies that an attribute from one relation has to map to a tuple in another relation.
"},{"location":"CMU%2015-445/Chap1/Chap1/#data-manipulation-languages-dml","title":"Data Manipulation Languages (DML)","text":"Methods to store and retrieve information from a database.
"},{"location":"CMU%2015-445/Chap1/Chap1/#relational-algebra","title":"Relational Algebra","text":""},{"location":"CMU%2015-445/Chap1/Chap1/#fundamental-operations","title":"Fundamental Operations","text":"\\(\\sigma -- Select\\)\u200b\u200b
\\(\\pi -- Projection\\)\u200b
\\(\\cup--Union\\)\u200b
\\(\\cap -- Intersection\\)\u200b
\\(- \\ --Difference\\)\u200b
\\(\\times --Product\\)\u200b
\\(\u22c8-- Join\\)\u200b
Example : Refer to ZJU course slides
A better approach is to state the high-level answer that you want the DBMS to compute.
"},{"location":"CMU%2015-445/Chap11/lec/#join-algorithms","title":"Join Algorithms","text":""},{"location":"CMU%2015-445/Chap11/lec/#nested-loop-join","title":"Nested Loop Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#naive-nested-loop-join","title":"Naive Nested Loop Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#block-nested-loop-join","title":"Block Nested Loop Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#index-nested-loop-join","title":"Index Nested Loop Join","text":"
sort R,S on join keys\ncursorR \u2190 Rsorted, cursorS \u2190 Ssorted\nwhile cursorR and cursorS:\n if cursorR > cursorS:\n increment cursorS\n if cursorR < cursorS:\n increment cursorR (and possibly backtrack cursors)\n elif cursorR and cursorS match:\n emit\n increment cursorS\n
Sort cost(R):\\(2M\u2219 (1 + \u2308log_{B-1}\u2308M/ B\u2309\u2309)\\) Sort cost(S):\\(2N\u2219 (1 + \u2308log_{B-1}\u2308N/ B\u2309\u2309)\\) Merge cost:\\((M + N)\\) Total cost:\\(2M\u2219 (1 + \u2308log_{B-1}\u2308M/ B\u2309\u2309) + 2N\u2219 (1 + \u2308log_{B-1}\u2308N/ B\u2309\u2309) + (M + N)\\)
The worst case for the merging phase is when the join attribute of all the tuples in both relations contains the same value
Cost: (M \u2219 N) + (sort cost)
WHEN IS SORT-MERGE JOIN USEFUL? * One or both tables are already sorted on join key. * Output must be sorted on join key. * The input relations may be sorted either by an explicit sort operator, or by scanning the relation using an index on the join key.
"},{"location":"CMU%2015-445/Chap11/lec/#hash-join","title":"Hash Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#simple-hash-join","title":"Simple Hash Join","text":""},{"location":"CMU%2015-445/Chap11/lec/#optimizationprobe-filter","title":"Optimization:Probe Filter","text":"Create a probe filter.
First look in Bloom filter then check hash(because bloom filter is in cache,so it's faster!)
Partition Phase: Hash both tables on the join attribute into partitions Probe Phase: Compares tuples in corresponding partitions for each table
"},{"location":"CMU%2015-445/Chap11/lec/#partitioned-hash-join-edge-cases","title":"Partitioned HASH JOIN EDGE CASES","text":"If a partition does not fit in memory,recursively partition it with a different hash function * Repeat as needed * Eventually hash join the corresponding (sub-)partitions
If a single join key has so many matching records that they don\u2019t fit in memory, use a block nested loop join for that key * Worst case: attribute are all the same then recursive hash is not useful. * A relation does not need recursive partitioning if \\(M > n_h + 1\\), or equivalently \\(M > (b_s/M) + 1\\), which simplifies (approximately) to \\(M > \\sqrt{b}_s\\).
"},{"location":"CMU%2015-445/Chap11/lec/#hybrid-hash-join","title":"Hybrid Hash Join","text":""},{"location":"CMU%2015-445/Chap2/Chap2/","title":"Chapter 2","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#pre-knoledge","title":"Pre-Knoledge","text":"
Reference to https://runoob.com/sql/sql-update.html
"},{"location":"CMU%2015-445/Chap2/Chap2/#fundamentals","title":"Fundamentals","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#create-table-construct","title":"Create Table Construct","text":"create table instructor(\n ID char(5)\n name varchar(20) not null,\n dept_name varchar(20)\uff0c\n salary numeric(8,2)default 0\uff0c\n primary key(ID),\n foreign key(dept_name)references department\n)\uff1b\n
foreign key
\u8fd9\u610f\u5473\u7740\"dept_name\"\u5217\u4e2d\u7684\u503c\u5fc5\u987b\u5b58\u5728\u4e8e\"department\"\u8868\u4e2d\u7684\u5173\u8054\u5217\u4e2d\uff0c\u5426\u5219\u63d2\u5165\u6216\u66f4\u65b0\u64cd\u4f5c\u5c06\u88ab\u62d2\u7edd.foreign key (dept_name) references department) \n on delete cascade |set null |restrict|set default\n on update cascade |set null |restrict |set default,\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#drop-and-alter-table-constructs","title":"Drop and Alter Table Constructs","text":"drop table student.
: Deletes the table and its contents.
delete from student
: Deletes all contents of table, but retains table.
alter table
:
alter table r add A D
alter table student add resume varchar(256);\n
alter table r drop A
SELECT column1, column2, ...\n FROM table_name;\nSELECT column1, column2, ...\n FROM table_name;\nSELECT DISTINCT column1, column2, ...\n FROM table_name;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#sql-where","title":"SQL WHERE","text":"SELECT column1, column2, ...\n FROM table_name WHERE condition;\nSELECT column1, column2, ...\n FROM table_name\n ORDER BY column1, column2, ... ASC|DESC;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#sql-insert","title":"SQL INSERT","text":"INSERT INTO table_name (column1,column2,column3,...)\nVALUES (value1,value2,value3,...);\n
INSERT INTO student\nSELECT ID,name,dept_name,0\nFROM instructor\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#sql-update","title":"SQL Update","text":"UPDATE table_name\nSET column1 = value1, column2 = value2, ...\nWHERE condition;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#sql-delete","title":"SQL DELETE","text":"DELETE FROM table_name WHERE condition;\nDELETE FROM table_name;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#natural-join","title":"Natural Join","text":"select name, course_id\nfrom instructor, teaches\nwhere instructor.ID = teaches.ID;\n\nselect name, course_id\nfrom instructor natural join teaches;\n
course(course_id,title, dept_name,credits\nteaches(ID, course_id,sec_id,semester, year)\ninstructor(ID, name, dept_name,salary\uff09 \nDepartment has different meanings.\nselect name, title from (instructor natural join teaches\uff09join course using(course_id); \u5373\u89c4\u5b9a\u8fde\u63a5\u7684\u5c5e\u6027\uff0c\u5bf9\u5e94\u4e8eFind students who takes courses across his/her department.\nor:\nselect distinct student.id\n from (student natural join takes)join course using (course_id\uff09 \n where student.dept_name <> course.dept_name\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#rename","title":"Rename","text":"select ID, name, salary/12 as monthly_salary\nfrom instructor\n\nselect distinct T. name\nfrom instructor as T, instructor as S\nwhere T.salary > S.salary and S.dept_name = \u2018Comp. Sci.\u2019\n
instructor as T \u2261 instructor T\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#string-operations","title":"String operations","text":"like \u2018100 \\%' escape '\\' \nlike \u2018100 \\%' \nlike \u2018100 #%' escape \u2018#' \n
"},{"location":"CMU%2015-445/Chap2/Chap2/#order","title":"Order","text":"order by name desc\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#limit","title":"Limit","text":" select name\n from instructor\n order by salary desc\n limit 3\uff1b // limit 0,3 \n
"},{"location":"CMU%2015-445/Chap2/Chap2/#set-operations","title":"Set Operations","text":"Each of the above operations automatically eliminates duplicates
To retain all duplicates use the corresponding multiset versions union all, intersect all and except all.
Suppose a tuple occurs m times in r and n times in s, then, it occurs:
m + n times in r union all s
min(m,n) times in r intersect all s
max(0, m \u2013 n) times in r except all s
null signifies an unknown value or that a value does not exist.
The result of any arithmetic expression involving null is nul
5 + null returns null
Find all instructors whose salary is null.
select name \nfrom instructor \nwhere salary is null\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#aggregations-group-by","title":"Aggregations + Group By","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#aggregations","title":"Aggregations","text":"the SELECT output list.
SELECT COUNT(DISTINCT login)\n FROM student WHERE login LIKE \"%@cs\"\n
SELECT AVG(gpq),COUNT(sid)\n FROM student WHERE login LIKE '%@cs'\n
undefined.
Cause ERROR
SELECT AVG(s.gpa), e.cid\n FROM enrolled AS e JOIN student AS s\n ON e.sid = s.sid\n
SELECT AVG(s.gpa), e.cid\n FROM enrolled AS e JOIN student AS s\n ON e.sid = s.sid\n GROUP BY e.cid\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#having","title":"Having","text":"* We cannot use Aggregations to FILTER tuples because we have not computed it yet
"},{"location":"CMU%2015-445/Chap2/Chap2/#examples","title":"Examples","text":"select dept_name\nfrom student\ngroup by dept_name\nhaving count(distinct name)= count(id)\n
select dept_name\nfrom student\ngroup by dept_name\nhaving 1-count(distinct name)/ count(id)<0.001 ;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#nested-subqueries","title":"Nested Subqueries","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#set-membership","title":"Set Membership","text":"select distinct course_id\nfrom section\nwhere semester = \"Fall\" and year = 2009 and\n course_id in (select course_id from section\n where semester = 'Spring' and year = 2010);\n
select distinct course_id\nfrom section\nwhere semester = \u2019Fall\u2019 and year= 2009 and \u000b course_id not in (select course_id\n from section\n where semester = \u2019Spring\u2019 and year= 2010);\n
select count(distinct ID)\nfrom takes\nwhere (couse_id,sec_id,semester,year)in\n(select course_id,sec_id,semester,year from teaches \n where teaches.ID = '10101')\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#set-comparison","title":"Set Comparison","text":"select distinct T.name\nfrom instructor as T,instructor as S\nwhere T.salary > S.salary and S.dept_name = 'Biology'\n
select name\nfrom instructor\nwhere salary > some (select salary\n from instructor\n where dept_name = \u2019Biology\u2019);\n
select name\nfrom instructor\nwhere salary > all (select salary\n from instructor\n where dept_name = \u2019Biology\u2019);\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#test-for-empty-relations","title":"Test for Empty Relations","text":"select course_id\nfrom section as S\nwhere = 'Fall' and year = 2009 and\nexists(select * from section as T\n where semester = 'Spring' and year = 2010\n and S.course_id = T.course_id)\n
\\(Note\\ that\\ X \u2013 Y = \\emptyset \\Rightarrow X\\subset Y\\)
select distinct S.ID,S.name\nfrom student as S\nwhere not exists(\n (select course_id from course where dept_name = 'Biology')\n except\n (select T.course_id from takes as T\n where S.ID = T.ID)\n)\n
The unique construct tests whether a subquery has any duplicate tuples in its result.
Find all courses that were offered at most once in 2009
select T.couse_id\nfrom course as T\nwhere unique(select R.course_id \n from section as R\n where T.course_id = R.course_id and R.year = 2009)\n
select T.course_id\nfrom courses as T\nwhere unique(select R.course_id\n from section as R\n where T.course_id = R.course_id \n and R.year = 2009)\n and exists(select R.course_id\n from section as R\n where T.course_id = R.course_id \n and R.year = 2009)\n
and course_id in (select course_id from section\n where year = 2009) ;\n
select T.course_id\nfrom course as T\nwhere unique(select R.course_id,year,semester\n from section as R\n where T.course_id = R.course_id)\n and exists(select R.course_id,year,semester\n from section as R\n where T.course_id = R.course_id)\n
group by count(*) > 1
\u5b9e\u73b0The with clause provides a way of defining a temporary view whose definition is available only to the query in which the with clause occurs.
Find all departments with the maximum budget
with max_budget(value) as\n (select max(budget\n from department)\n select dept_name\n from department,max_budget\n where department.budget = max_budget.value)\n\nselect dept_name\nfrom department\nwhere budget = (select (max(budget) from department))\n
Complex Queries using With Clause
with dept_total(dept_name,value)as\n(select dept_name,sum(salary)from instructor\ngroup by dept_name),\n deot_total_avg(value)as\n(select avg(value)from dept_total)\nselect dept_name\nfrom dept_total,dept_total_avg\nwhere dept_total.value >= dept_total_ang.value;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#modification-of-the-database","title":"Modification of the Database","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#delete","title":"Delete","text":"delete from instructor\nwhere dept_name in (select dept_name from department\n where building = \u2019Watson\u2019);\n\ndelete from instructor\nwhere salary< (select avg (salary) from instructor);\n'''\nProblem: as we delete tuples from deposit, the average salary changes\nSolution used in SQL:\n1. First, compute avg salary and find all tuples to delete\n2. Next, delete all tuples found above (without ecomputing avg or retesting the tuples)\n'''\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#insert","title":"Insert","text":"insert into course\nvalues (\u2019CS-437\u2019, \u2019Database Systems\u2019, \u2019Comp. Sci.\u2019, 4);\ninsert into course (course_id, title, dept_name, credits)\nvalues (\u2019CS-437\u2019, \u2019Database Systems\u2019, \u2019Comp. Sci.\u2019, 4);\ninsert into student\nvalues (\u20193003\u2019, \u2019Green\u2019, \u2019Finance\u2019, null);\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#update","title":"Update","text":" update instructor\n set salary = salary * 1.03\n where salary > 100000;\n update instructor\n set salary = salary * 1.05\n where salary <= 100000;\n
The order is importantCan be done better using the case statement (next slide)
Case Statement for Conditional Updates
update instructor\nset salary = case when salary <= 100000 then salary * 1.05\nelse salary * 1.03 \nend\n
LIKE is used for string matching.
String-matching operators >%
matches any sequence of characters, including zero characters. Any substring. In other words, %
can match any string of any length, including an empty string. Any character.
'15%'
matches any string starting with \"15\", '%15'
matches any string ending with \"15\", and '%15%'
matches any string containing \"15\" anywhere.SELECT * FROM enrolled AS e\n WHERE e.cid LIKE '15_%'\n
_
matches any single character. It's used to specify that at a particular position, any character must match, but it doesn't matter which character. For example, '15_'
matches \"150\", \"151\", \"152\", etc., but not \"15\" etc.
SELECT * FROM student AS s\n WHERE s.login LIKE '%@c_'\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#substring","title":"SUBSTRING","text":"SELECT SUBSTRING(name,1,5) AS abbrv_name\n FROM student WHERE sid = 53688\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#upper","title":"UPPER","text":"SELECT * FROM student AS s\n WHERE UPPER(s.name) LIKE 'KAN%'\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#_1","title":"||
","text":"SQL-92
SELECT name FROM student\n WHERE login = LOWER(name) || '@cs'\n
MSSQL
SELECT name FROM student\n WHERE login = LOWER(name) + '@cs'\n
MySQL
SELECT name FROM student\n WHERE login = CONCAT(LOWER(name), '@cs')\n
'Li''ly'
will automatically concatenate SELECT NOW();\n SELECT CURRENT_TIMESTAMP();\n SELECT CURRENT_TIMESTAMP();\n SELECT EXTRACT(DAY FROM DATE('2018-08-29'));\n //SELECT DATE('2018-08-29')-DATE('2018-01-01');\n SELECT ROUND((UNIX_TIMESTAMP(DATE('2018-08-29'))-UNIX_TIMESTAMP(DATE('2018-01-01')))/(60*60*24),0) AS days;\n SELECT DATEDIFF(DATE('2018-08-29'),DATE('2018-01-01')) AS days;\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#output-control-redirection","title":"Output Control + Redirection","text":""},{"location":"CMU%2015-445/Chap2/Chap2/#output-redirection","title":"Output Redirection","text":"Store query results in another table: \u2192 Table must not already be defined. \u2192 Table will have the same # of columns with the same types as the input.
CREATE TABLE CourseIds (\nSELECT DISTINCT cid FROM enrolled);\n
Insert tuples from query into another table: \u2192 Inner SELECTmust generate the same columns as the
target table.
\u2192 DBMSs have different options/syntax on what to do with
integrity violations (e.g., invalid duplicates).
INSERT INTO CourseIds\n(SELECT DISTINCT cid FROM enrolled);\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#output-control","title":"Output Control","text":"SELECT sid FROM enrolled\nWHERE cid = '15-721'\nORDER BY grade DESC,1,sid ASC\n
offset -- skip
-- should combine with oredered by
clause SELECT sid, name FROM student\nWHERE login LIKE '%@cs'\nLIMIT 20 OFFSET 10 \n
select name from student\n Where sid in(\n select sid from enrolled\n where cid = '14-445 '\n )\n
SELECT name FROM student\nWHERE sid = ANY(\nSELECT sid FROM enrolled\nWHERE cid = '15-445'\n)\n
SELECT (SELECT S.name from student as S where S.sid = E = sid) as sname\nFROM enrolled as E\nwhere cid = '15-445'\n
Find student record with the highest id that is enrolled in at least one course.
SELECT sid,name FROM student\nWHERE sid IN(\nSELECT MAX(sid)FROM enrolled\n)\n
SELECT sid, name FROM student\nWHERE sid IN (\nSELECT sid FROM enrolled\nORDER BY sid DESC LIMIT 1\n)\n
SELECT student.sid, name\n FROM student\n JOIN (SELECT MAX(sid) AS sid\n FROM enrolled) AS max_e\n ON student.sid = max_e.sid;\n
Find all courses that have no students enrolled in it.
SELECT * FROM course\n WHERE NOT EXISTS(\n SELECT * FROM enrolled\n WHERE course.cid = enrolled.cid\n )\n
"},{"location":"CMU%2015-445/Chap2/Chap2/#window-functions","title":"Window Functions","text":"Provides a way to write auxiliary statements for use in a larger query.
Think of it like a temp table just for one query.
Alternative to nested queries and views.
WITH cteName AS (\nSELECT 1\n)\nSELECT * FROM cteName\n
WITH cteSource (maxId) AS (\nSELECT MAX(sid) FROM enrolled\n)\nSELECT name FROM student, cteSource\nWHERE student.sid = cteSource.maxId\n
At each iteration, that SELECT produces a row with a new value one greater than the value of n from the previous row set. The first iteration operates on the initial row set (1) and produces 1+1=2; the second iteration operates on the first iteration\u2019s row set (2) and produces 2+1=3; and so forth. This continues until recursion ends, which occurs when n is no longer less than 5.
https://blog.csdn.net/yiyelanxin/article/details/86593473
Join operations take two relations and return as a result another relation.
Join condition defines which tuples in the two relations match, and what attributes are present in the result of the join.
Join type defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated.
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#examples","title":"Examples","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#types","title":"Types.","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#built-in-data-types-in-sql","title":"Built-in Data Types in SQL","text":"date \u20182005-7-27\u2019
time \u201809:00:30.75\u2019
timestamp \u20182005-7-27 09:00:30.75\u2019
interval \u20181\u2019 day
Subtracting a date/time/timestamp value from another gives an interval value
Interval values can be added to date/time/timestamp values
current_date(), current_time()
year(x), month(x), day(x), hour(x), minute(x), second(x)
create type Dollars as numeric (12,2) final\ncreate table department(\n dept_name varchar (20),\n building varchar (15),\n budget Dollars);\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#domains","title":"Domains","text":"create domain person_name char(20) not null\n
Domains can have constraints, such as not null, specified on them.
create domain degree_level varchar(10)\nconstraint degree_level_test\ncheck (value in (\u2019Bachelors\u2019, \u2019Masters\u2019, \u2019Doctorate\u2019));\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#large-object-types","title":"Large-Object Types","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#integrity-constraints","title":"Integrity Constraints","text":"Integrity constraints guard against accidental damage to the database, by ensuring that authorized changes to the database do not result in a loss of data consistency.
not null
name varchar(20) not null\nbudget numeric(12,2) not null\n
primary key
unique
check (P), where P is a predicate
create table section (\n course_id varchar (8),\n sec_id varchar (8),\n semester varchar (6),\n year numeric (4,0),\n building varchar (15),\n room_number varchar (7),\n time slot id varchar (4), \n primary key (course_id, sec_id, semester, year),\n check (semester in (\u2019Fall\u2019, \u2019Winter\u2019, \u2019Spring\u2019, \u2019Summer\u2019))\n);\n
check (semester in (\u2019Fall\u2019, \u2019Winter\u2019, \u2019Spring\u2019, \u2019Summer\u2019))\n
foreign key
Cascading Actions
create table course (\ncourse_id char(5) primary key,\ntitle varchar(20),\ndept_name varchar(20) references department\n)\ncreate table course (\n ...\ndept_name varchar(20),\nforeign key (dept_name) references department\n on delete cascade,\n on update cascade,\n ...)\n
create table person (\n ID char(10),\n name char(40),\n mother char(10),\n father char(10),\n primary key (ID),\n foreign key (father) references person,\n foreign key (mother) references person);\n
How to insert a tuple without causing constraint violation ?
Assertion
create assertion <assertion-name> check <predicate>;\ncreate assertion credits_earned_constraint check\n(not exists \n (select ID \n from student\n where tot_cred <> (\n select sum(credits)\n from takes natural join course\n where student.ID=takes.ID\n and grade is not null \n and grade<>\u2019F\u2019)))\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#view","title":"View","text":"create view faculty as \nselect ID, name, dept_name\nfrom instructor\n
select name\nfrom faculty\nwhere dept_name = \u2018Biology\u2019\n
create view departments_total_salary(dept_name, total_salary) as\nselect dept_name, sum (salary)\nfrom instructor\ngroup by dept_name;\n
create view physics_fall_2009 as\nselect course.course_id, sec_id, building, room_number\nfrom course, section\nwhere course.course_id = section.course_id\nand course.dept_name = \u2019Physics\u2019\nand section.semester = \u2019Fall\u2019\nand section.year = \u20192009\u2019;\n
create view physics_fall_2009_watson as\nselect course_id, room_number\nfrom physics_fall_2009\nwhere building= \u2019Watson\u2019;\n
create view faculty as\nselect ID, name, dept_name\nfrom instructor\n\ninsert into faculty values (\u201930765\u2019, \u2019Green\u2019, \u2019Music\u2019);\n
This insertion must be represented by the insertion of the tuple(\u201930765\u2019, \u2019Green\u2019, \u2019Music\u2019, null)
into the instructor relation insert into instructor values (\u201930765\u2019, \u2019Green\u2019, \u2019Music\u2019, null);
Some Updates cannot be Translated Uniquely
create view instructor_info a\nselect ID, name, building\nfrom instructor, department\nwhere instructor.dept_name= department.dept_name;\ninsert into instructor_info values (\u201969987\u2019, \u2019White\u2019, \u2019Taylor\u2019);\n
which department, if multiple departments in Taylor?
Most SQL implementations allow updates only on simple views(updatable views)
The from clause has only one database relation.
The select clause contains only attribute names of the relation, and does not have any expressions, aggregates, or distinct specification.
Any attribute not listed in the select clause can be set to null.
The query does not have a group by or having clause.
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#materialized-view","title":"Materialized View","text":"create a physical table containing all the tuples in the result of the query defining the view
If relations used in the query are updated, the materialized view result becomes out of date
create materialized view departments_total_salary(dept_name, total_salary) as\n select dept_name, sum (salary)\n from instructor\n group by dept_name;\nselect dept_name\nfrom departments_total_salary\nwhere total_salary > (select avg(total_salary) from departments_total_salary );\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#view-and-logical-data-indepencence","title":"View and Logical Data Indepencence","text":"If relation \\(S(a, b, c)\\) is split into two sub relations \\(S_1(a,b)\\) and \\(S_2(a,c)\\)\u200b. How to realize the logical data independence?
create table S1...;\ncreate table S2...;\ninsert into S1 select a,b from S;\ninsert into S2 select a,c from S;\ndrop table S;\ncreate view S(a,b,c)as select a,b,c from Sq natural join S2;\n
create view S(a,b,c)as select a,b,c from Sq natural join S2;
\u5b9e\u73b0\u5bf9\u539f\u8868\u7684\u67e5\u8be2\uff0c\u5bf9\u7528\u6237\u6765\u8bf4\u65e0\u533a\u522b.create table student\n( ID varchar (5),\n name varchar (20) not null,\n dept_name varchar (20),\n tot_cred numeric (3,0) default 0,\n primary key (ID) \n)\ncreate index studentID_index on student(ID)\n
select *\n from student\n where ID = \u201812345\u2019\n
AUTOCOMMIT=1;
-- Everytime auto commitSET AUTOCOMMIT=0;\nUPDATE account SET balance=balance -100 WHERE ano=\u20181001\u2019;\nUPDATE account SET balance=balance+100 WHERE ano=\u20181002\u2019;\nCOMMIT;\n
UPDATE account SET balance=balance -200 WHERE ano=\u20181003\u2019;\nUPDATE account SET balance=balance+200 WHERE ano=\u20181004\u2019; COMMIT;\n
UPDATE account SET balance=balance+balance*2.5%;\nCOMMIT;\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#acid-properties","title":"ACID Properties","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#authorization","title":"Authorization","text":"grant <privilege list> // privilege\uff1a\u6743\u9650\non <relation name or view name> to <user list>\n
a user-id
public, which allows all valid users the privilege granted
A role (more on this later)
grant select on instructor to U1, U2, U3\ngrant select on department to public\ngrant update (budget) on department to U1,U2\ngrant all privileges on department to U1\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#revoking-authorization-in-sql","title":"Revoking Authorization in SQL","text":"revoke <privilege list>\non <relation name or view name> \nfrom <user list>\nrevoke select on branch from U1, U2, U3\n
There are two approaches to accessing database from a general-purpose programming language.
API (application-program interface) for a program to interact with a database server
Embedded SQL -- provides a means by which a program can interact with a database server.
At runtime, these function calls connect to the database using an API that provides dynamic SQL facilities.
ODBC (Open Database Connectivity) works with C, C++, C#
public static void JDBCexample(String dbid, String userid, String passwd){ \n try { \n Connection conn = DriverManager.getConnection( \n \"jdbc:oracle:thin:@db.yale.edu:2000:univdb\", userid, passwd); \n Statement stmt = conn.createStatement(); \n ... Do Actual Work ....\n stmt.close();\n conn.close();\n }\n catch (SQLException sqle) { \n System.out.println(\"SQLException : \" + sqle);\n }\n }\n
* Update to database try {\n stmt.executeUpdate(\n \"insert into instructor values(\u201977987\u2019, \u2019Kim\u2019, \u2019Physics\u2019, 98000)\"\n );\n} \ncatch (SQLException sqle){\n System.out.println(\"Could not insert tuple. \" + sqle);\n}\n
* Execute query and fetch and print results ResultSet rset = stmt.executeQuery(\n \"select dept_name, avg (salary)\n from instructor\n group by dept_name\");\nwhile (rset.next()){\n System.out.println(rset.getString(\"dept_name\") + \" \" +rset.getFloat(2));\n}\n
rset.getString(\u201cdept_name\u201d)
and rset.getString(1)
equivalent if dept_name is the first argument of select result.
int a = rset.getInt(\u201ca\u201d); if (rset.wasNull()) Systems.out.println(\u201cGot null value\u201d);
PreparedStatement pStmt = conn.prepareStatement( \"insert into instructor values(?,?,?,?)\");\npStmt.setString(1, \"88877\"); \npStmt.setString(2, \"Perry\");\npStmt.setString(3, \"Finance\"); \npStmt.setInt(4, 125000);\n\npStmt.executeUpdate(); \n\npStmt.setString(1, \"88878\");\npStmt.executeUpdate();\n
WARNING: always use prepared statements when taking an input from the user and adding it to a query\"insert into instructor values(\u2019 \" + ID + \" \u2019, \u2019 \" + name + \" \u2019, \" + \n \" \u2019 \" + dept name + \" \u2019, \" + salary + \")\"\n
ResultSetMetaData rsmd = rs.getMetaData();\n for(int i = 1; i <= rsmd.getColumnCount(); i++) {\n System.out.println(rsmd.getColumnName(i));\n System.out.println(rsmd.getColumnTypeName(i));\n}\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#transaction-control-in-jdbc","title":"Transaction Control in JDBC","text":"bad idea for transactions with multiple updates
conn.setAutoCommit(false);\n
conn.commit(); \nconn.rollback();\n
conn.setAutoCommit(true)
turns on automatic commit.#sql iterator deptInfoIter ( String dept name, int avgSal);\n deptInfoIter iter = null;\n #sql iter = { select dept_name, avg(salary) as avgSal from instructor group by dept name };\n while (iter.next()) {\n String deptName = iter.dept_name();\n int avgSal = iter.avgSal();\n System.out.println(deptName + \" \" + avgSal);\n }\n iter.close();\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#odbc","title":"ODBC","text":"Each database system supporting ODBC provides a \"driver\" library that must be linked with the client program.
When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action, and fetch results.
ODBC program first allocates an SQL environment, then a database connection handle.
Opens database connection using SQLConnect( ). Parameters for SQLConnect:
Must also specify types of arguments:
int ODBCexample()\n{\n RETCODE error;\n HENV env; /* environment */ \n HDBC conn; /* database connection */ \n SQLAllocEnv(&env);\n SQLAllocConnect(env, &conn);\n SQLConnect(conn, 'db.yale.edu', SQL_NTS, 'avi', SQL_NTS,'avipasswd',SQL_NTS); \n { .... Do actual work ... }\n SQLDisconnect(conn); \n SQLFreeConnect(conn); \n SQLFreeEnv(env); \n}\n
Program sends SQL commands to database by using SQLExecDirect
Result tuples are fetched using SQLFetch()
SQLBindCol()
binds C language variables to attributes of the query result When a tuple is fetched, its attribute values are automatically stored in corresponding C variables.
Arguments to SQLBindCol()
ODBC stmt variable, attribute position in query result
The type conversion from SQL to C.
The address of the variable.
For variable-length types like character arrays,
Good programming requires checking results of every function call for errors; we have omitted most checks for brevity.
char deptname[80];\nfloat salary;\nint lenOut1, lenOut2;\n\nHSTMT stmt;\nchar * sqlquery = \"select dept_name, sum (salary) from instructor group by dept_name\";\nSQLAllocStmt(conn, &stmt);\nerror = SQLExecDirect(stmt, sqlquery, SQL_NTS);\n\nif (error == SQL SUCCESS) {\n SQLBindCol(stmt, 1, SQL_C_CHAR, deptname , 80, &lenOut1);\n SQLBindCol(stmt, 2, SQL_C_FLOAT, &salary, 0 , &lenOut2);\n while (SQLFetch(stmt) == SQL_SUCCESS) {\n printf (\" %s %g\\n\", deptname, salary);\n }\n}\nSQLFreeStmt(stmt, SQL_DROP);\n//\u5b9a\u4e49\u6570\u7ec4\u9700\u8981\u591a\u4e00\u4e2a\uff0c\u5426\u5219\u4f1a\u6709\u622a\u65ad\u3002\u5982 char deptname[11]; \u624d\u80fd\u5b9a\u4e49\u5341\u4e2a\u5143\u7ec4\u3002\n//\u5982\u679c\u7ed3\u679c\u4e3a\u7a7a\uff0c\u5219 lenOut \u4e3a -1.\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#odbc-prepared-statements","title":"ODBC Prepared Statements","text":"SQLPrepare(stmt, <SQL String>);\nSQLBindParameter(stmt, <parameter#>,\n \u2026 type information and value omitted for simplicity..)\n retcode = SQLExecute( stmt); \n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#embedded-sql","title":"Embedded SQL","text":"EXEC-SQL connect to server user user-name using password;\n
They are preceded by a colon (:) to distinguish from SQL variables (e.g., :credit_amount )
EXEC-SQL BEGIN DECLARE SECTION\n int credit-amount ;\nEXEC-SQL END DECLARE SECTION;\n
declare c cursor for <SQL query> \n
EXEC SQL\n declare c cursor for\n select ID, name\n from student\n where tot_cred > :credit_amount;\n
main( )\n{ EXEC SQL INCLUDE SQLCA; //\u58f0\u660e\u6bb5\u5f00\u59cb\n EXEC SQL BEGIN DECLARE SECTION;\n char account_no [11]; //host variables(\u5bbf\u4e3b\u53d8\u91cf)\u58f0\u660e\n char branch_name [16];\n int balance; \n EXEC SQL END DECLARE SECTION;//\u58f0\u660e\u6bb5\u7ed3\u675f\n EXEC SQL CONNECT TO bank_db USER Adam Using Eve; \n scanf (\u201c%s %s %d\u201d, account_no, branch_name, balance);\n EXEC SQL insert into account values (:account_no, :branch_name, :balance);\n If (SQLCA.sqlcode ! = 0){printf ( \u201cError!\\n\u201d);}\n else {printf (\u201cSuccess!\\n\u201d);}\u00e5\n
This statement causes the database system to execute the query and to save the results within a temporary relation.
The query uses the value of the host-language variable credit-amount at the time the open statement is executed.
EXEC SQL open c ;\n
EXEC SQL fetch c into :si, :sn\uff1b\n
A variable called SQLSTATE in the SQL communication area (SQLCA) gets set to '02000' to indicate no more data is available
The close statement causes the database system to delete the temporary relation that holds the result of the query.
EXEC SQL close c ;\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#updates-through-embedded-sql","title":"Updates Through Embedded SQL","text":" EXEC SQL \ndeclare c cursor for\nselect *\nfrom instructor\nwhere dept_name = 'Music'\nfor update\n
We then iterate through the tuples by performing fetch operations on the cursor , and after fetching each tuple we execute the following code:
update instructor\n set salary = salary + 1000\n where current of c\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#procedural-constructs-in-sql","title":"Procedural Constructs in SQL","text":""},{"location":"CMU%2015-445/Chap2_inclass/lec/#sql-functions","title":"SQL Functions","text":" create function dept_count (dept_name varchar(20))\n returns integer\n begin\n declare d_count integer;\n select count (* ) into d_count\n from instructor\n where instructor.dept_name = dept_name\n\n return d_count;\n end\n
select dept_name, budget\nfrom department\nwhere dept_count (dept_name ) > 1\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#table-functions","title":"Table Functions","text":"create function instructors_of (dept_name char(20) )\nreturns table ( ID varchar(5),\n name varchar(20),\n dept_name varchar(20),\n salary numeric(8,2))\nreturn table\n(select ID, name, dept_name, salary\n from instructor\n where instructor.dept_name = instructors_of.dept_name)\n
select *\nfrom table (instructors_of (\u2018Music\u2019))\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#sql-procedures","title":"SQL Procedures","text":"create procedure dept_count_proc (in dept_name varchar(20), out d_count integer)\nbegin\n select count(*) into d_count\n from instructor\n where instructor.dept_name = dept_count_proc.dept_name\nend\n
declare d_count integer;\ncall dept_count_proc( \u2018Physics\u2019, d_count)\n
While and repeat statements
declare n integer default 0;\nwhile n < 10 do\n set n = n + 1\nend while \n
repeat\n set n = n \u2013 1\n until n = 0\nend repeat\n
For loop
declare n integer default 0;\n for r as\n select budget from department\n where dept_name = \u2018Music\u2019\n do\n set n = n - r.budget\n end for\n
* Conditional statements (if-then-else) if boolean expression \n then statement or compound statement \nelseif boolean expression \n then statement or compound statement \n else statement or compound statement \nend if\n
* SQL:1999 also supports a case statement similar to C case statement * EXAMPLE CREATE FUNCTION registerStudent(\n IN s_id VARCHAR(5),\n IN s_courseid VARCHAR(8),\n IN s_secud VARCHAR(8),\n IN s_semester VARCHAR(6),\n in s_year NUMERIC(4.0),\n OUT errorMsg VARCHAR(100)\n)\nRETURNS INTEGER\nBEGIN\n DECLARE currEnrol INT;\n SELECT COUNT(*) INTO currEnrol\n FROM takes\n WHERE course_id = s_courseid AND sec_id = s_secid AND semester = s_semester AND year = s_year;\n DECLARE LIMIT INT;\n SELECT capacity INTO limit\n FROM classroom NATURAL JOIN section\n WHERE course_id = s_courseid AND sec_id = s_secid AND semester = s_semester AND year = s_year;\n IF(currEnrol<limit)\n BEGIN\n INSET INTO takes VALUES\n (s_id,s_course,s_secid,s_semester,s_year,null);\n RETURN(0);\n END\n SET errorMsg = 'Enrollment limit reached for course'||'s_course_id'||'section'||s_secid;\n RETURN(-1);\nEND;\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#external-language-functionsprocedures","title":"External Language Functions/Procedures","text":"create procedure dept_count_proc(in dept_name varchar(20),out count integer)\nlanguage C\nexternal name '/usr/avi/bin/dept_count_proc'\ncreate function dept_count(dept_name varchar(20))\nreturns integer\nlanguage C\nexternal name '/usr/avi/bin/dept_count'\n
* Benefits of external language functions/procedures: more efficient for many operations, and more expressive power. * Drawbacks Code to implement function may need to be loaded into database system and executed in the database system\u2019s address space. * risk of accidental corruption of database structures * security risk, allowing users access to unauthorized data There are alternatives, which give good security at the cost of potentially worse performance. Direct execution in the database system\u2019s space is used when efficiency is more important than security."},{"location":"CMU%2015-445/Chap2_inclass/lec/#triggers","title":"Triggers","text":"CREATE TRIGGER account_trigger AFTER UPDATE of account ON balance \nreferencing NEW ROW AS nrow \nreferencing OLD ROW AS orow \nFOR EACH ROW\n WHEN nrow.balance - orow.balance > =200000 OR\n orow.balance - nrow.balance >=50000 \n BEGIN\n INSERT INTO account_log VALUES (nrow.account-number, nrow.balance-orow.balance , current_time() )\nEND\n
create trigger timeslot_check1 after insert on section\nreferencing new row as nrow\nfor each row\nwhen (nrow.time_slot_id not in (\n select time_slot_id\n from time_slot)) /* time_slot_id not present in time_slot */\nbegin\n rollback\nend;\n
create trigger timeslot_check2 after delete on timeslot\nreferencing old row as orow\nfor each row\nwhen (orow.time_slot_id not in (select time_slot_id from time_slot) /* last tuple for time slot id deleted from time slot */\nand orow.time_slot_id in (select time_slot_id from section))/* and time_slot_id still referenced from section*/\nbegin\n rollback\nend;\n
create trigger credits_earned after update of takes on grade\nreferencing new row as nrow\nreferencing old row as orow\nfor each row\nwhen nrow.grade <> \u2019F\u2019 and nrow.grade is not null\n and (orow.grade = \u2019F\u2019 or orow.grade is null)\nbegin atomic\n update student\n set tot_cred= tot_cred + \n (select credits\n from course\n where course.course_id= nrow.course_id)\n where student.id = nrow.id;\nend;\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#statement-level-triggers","title":"Statement Level Triggers","text":"Instead of executing a separate action for each affected row, a single action can be executed for all rows affected by a transaction.
for each statement
instead of for each row
create trigger grade_trigger after update of takes on grade\n referencing new table as new_table \n for each statement\n when exists(select avg(grade)\n from new_table\n group by course_id, sec_id, semester, year\n having avg(grade)< 60 )\n begin \n rollback\n end\n
"},{"location":"CMU%2015-445/Chap2_inclass/lec/#recursive-queries","title":"Recursive Queries","text":"with recursive rec_prereq(course_id, prereq_id) as (\n select course_id, prereq_id\n from prereq\n union\n select rec_prereq.course_id, prereq.prereq_id\n from rec_prereq, prereq\n where rec_prereq.prereq_id = prereq.course_id)\nselect \u2217from rec_prereq;\n
"},{"location":"CMU%2015-445/Chap3/Chap3/#database-storage","title":"Database Storage","text":"
Non-Volatile : Persistent when loose power. Sequential Access Block Addressable Memory.
In the course, we will focus on the DRAM
Goal : Allow the DBMS to manage databases that exceed the amount of memory available
Virtual Memory : A technique that allows an operating system to provide more memory to a process than is physically available.
But also multitable clustering file organization.
Self-contained file : A file that contains all the information needed to manage the data it contains.
While not entirely true, as the DBMS may store some information to manage the data in other files.
Different DBMSs manage pages in files on disk in different ways.
like for SQLite,Only 4KB is atomatic read/write!--Hardware can only guarantee atomicity at 4KB.
If store tuples one by one , do not guarantee that the tuples are stored in that order.
As demonstrated in Heap File Organization
, the tradeoff between read and write is a common problem in database storage.
Every page contains a header of metadata about the page's contents. \u2192 Page Size \u2192 Checksum \u2192 DBMS Version \u2192 Transaction Visibility \u2192 Compression Information * Some systems require pages to be self-contained (e.g., Oracle)
"},{"location":"CMU%2015-445/Chap3/Chap3/#tuple-oriented-layout","title":"Tuple-oriented Layout","text":"When the page gets full, the DBMS writes it out disk and starts filling up the next page with records.
"},{"location":"CMU%2015-445/Chap3/Chap3/#log-structured-compaction","title":"Log-Structured Compaction","text":"
Compaction coalesces larger log files into smaller files by removing unnecessary records. Log-structured storage managers are more common today. This is partly due to the proliferation of RocksDB. Downsides: * Write - Amplification * Compaction is expensive
"},{"location":"CMU%2015-445/Chap3/Chap3/#tuple-layout","title":"Tuple Layout","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#denormalized-tuple-data","title":"Denormalized Tuple Data","text":"Details"},{"location":"CMU%2015-445/Chap3/Chap3/#tuple-storage","title":"Tuple Storage","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#data-representation","title":"Data Representation","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#variable-precision-numbers-fixed-precision-numbers","title":"Variable Precision Numbers & Fixed Precision Numbers","text":"Variable Precision Numbers : Typically faster than arbitrary precision numbers but can have rounding errors...
"},{"location":"CMU%2015-445/Chap3/Chap3/#large-values","title":"Large Values","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#external-value-storage","title":"External Value Storage","text":""},{"location":"CMU%2015-445/Chap3/Chap3/#system-catalog","title":"System Catalog","text":"See Slides.
"},{"location":"CMU%2015-445/Chap6/lec/","title":"Problem #2: How the DBMS manages its memory and move data back-and-forth from disk.","text":"Memory region organized as an array of fixed-size pages. An array entry is called a frame. * When the DBMS requests a page, an exact copy is placed into one of these frames. Dirty pages are buffered and notwritten to disk immediately * \u2192 Write-Back Cache
"},{"location":"CMU%2015-445/Chap6/lec/#page-table","title":"Page Table","text":"Hash Table
See Slides
"},{"location":"CMU%2015-445/Chap6/lec/#scan-sharing","title":"Scan Sharing","text":"If a query wants to scan a table and another query is already doing this, then the DBMS will attach the second query's cursor to the existing cursor.
See Slides and Lectures
"},{"location":"CMU%2015-445/Chap6/lec/#buffer-pool-bypass","title":"Buffer Pool Bypass","text":"See Slides
"},{"location":"CMU%2015-445/Chap6/lec/#os-page-cache","title":"OS Page Cache","text":"Most disk operations go through the OS API. Unless you tell it not to, the OS maintains its own filesystem cache (i.e., the page cache)
"},{"location":"CMU%2015-445/Chap6/lec/#replacement-policies","title":"Replacement Policies","text":"When the DBMS needs to free up a frame to make room for a new page, it must decide which page to evict from the buffer pool.
"},{"location":"CMU%2015-445/Chap6/lec/#lru-least-recently-used","title":"LRU : Least Recently Used","text":"Maintain a single timestamp of when each page was last accessed. When the DBMS needs to evict a page, select the one with the oldest timestamp.
LRU and CLOCK replacement policies are susceptible to sequential flooding.
Track the history of last \\(K\\) references to each page as timestamps and compute the interval between subsequent accesses. The DBMS then uses this history to estimate the next time that page is going to be accessed.
"},{"location":"CMU%2015-445/Chap6/lec/#better-policies-localization","title":"Better Policies : Localization","text":"The DBMS chooses which pages to evict on a per txn/query basis. This minimizes the pollution of the buffer pool from each query.
Example: Postgres maintains a small ring buffer that is private to the query.
"},{"location":"CMU%2015-445/Chap6/lec/#better-policies-proority-hints","title":"Better Policies : Proority Hints","text":""},{"location":"CMU%2015-445/Chap6/lec/#dirty-pages","title":"Dirty Pages","text":"Fast Path: If a page in the buffer pool is not dirty, then the DBMS can simply \"drop\" it. Slow Path: If a page is dirty, then the DBMS must write back to disk to ensure that its changes are persisted.
The DBMS can periodically walk through the page table and write dirty pages to disk. When a dirty page is safely written, the DBMS can either evict the page or just unset the dirty flag. Need to be careful that the system doesn't write dirty pages before their log records are written...
"},{"location":"CMU%2015-445/Chap6/lec/#other-memory-pools","title":"Other Memory Pools","text":""},{"location":"CMU%2015-445/Chap8/Chap8/","title":"Chapter 8","text":"Index Types see Inclass Slide.(Chapter 14)
"},{"location":"CMU%2015-445/Chap8/Chap8/#b-tree-indexes","title":"B+ Tree Indexes","text":"A B+Tree is a self-balancing, ordered tree data structure that allows searches, sequential access,Insertions, and deletions in O(log n).
A B+Tree is an M-way search tree with the following properties:
Root can have only 2 branches.
"},{"location":"CMU%2015-445/Chap9/Chap9/","title":"Chap9","text":"a
"},{"location":"CMU%2015-445/ER/lec/","title":"E-R Model","text":""},{"location":"CMU%2015-445/ER/lec/#entity-relationship-model","title":"Entity-Relationship Model","text":""},{"location":"CMU%2015-445/ER/lec/#modeling","title":"Modeling","text":"
\u53cc\u6a2a\u7ebf\u8868\u793a\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u5fc5\u987b\u53c2\u4e0e\u5173\u7cfb\uff0c\u800c\u5355\u6a2a\u7ebf\u5219\u8868\u793a\u5bf9\u8c61\u53ef\u4ee5\u4e0d\u53c2\u4e0e\u5173\u7cfb
-----
====
<--->
one to one
relationship
\u5e26\u5c5e\u6027. : \u5982 takes
\u4e0a\u7684 grade
.
A database can be modeled as:
An entity is an object that exists and is distinguishable from other objects.
An entity set is a set of entities of the same type that share the same
properties.
"},{"location":"CMU%2015-445/ER/lec/#relationship","title":"Relationship","text":""},{"location":"CMU%2015-445/ER/lec/#attributes","title":"Attributes","text":"Entities have attributes.
Attribute types:
Derived(\u6d3e\u751f) attributes
date_of_birth
{phone_number}
multiple phone numbers.
If implemented , have to import using another new entity.
"},{"location":"CMU%2015-445/ER/lec/#mapping-cardinality-constraints","title":"Mapping Cardinality Constraints","text":"We express cardinality constraints by drawing either a directed line \\((\\to)\\), signifying \u201cone,\u201d or an undirected line \\((\u2014)\\), signifying \u201cmany,\u201d between the relationship set and the entity set.
"},{"location":"CMU%2015-445/ER/lec/#one-to-one","title":"One to one","text":""},{"location":"CMU%2015-445/ER/lec/#one-to-many","title":"One to many","text":""},{"location":"CMU%2015-445/ER/lec/#many-to-one","title":"Many to one","text":""},{"location":"CMU%2015-445/ER/lec/#many-to-many","title":"Many to many","text":"proj_guide
to instructor indicates each student has at most one guide for a projectFor Entity Sets.
For Relationship Sets.
An entity set that does not have a primary key is referred to as a weak entity set.
The existence of a weak entity set depends on the existence of a identifying entity set(\u6807\u8bc6\u6027\u5b9e\u4f53\u96c6)
Note: the primary key of the strong entity set is not explicitly stored with the weak entity set, since it is implicit in the identifying relationship.
If course_id
were explicitly stored, section could be made a strong entity, but then the relationship between section and course would be duplicated by an implicit relationship defined by the attribute course_id
common to course and section.
Suppose we have entity sets:
We model the fact that each student has an associated department using a relationship set stud_dept
The attribute dept_name in student below replicates information present in the relationship and is therefore redundant and needs to be removed.
BUT: when converting back to tables, in some cases the attribute gets reintroduced, as we will see later.
"},{"location":"CMU%2015-445/ER/lec/#reduction-to-relation-schemas","title":"Reduction to Relation Schemas","text":"inst_dept(id,name,salary,dept_name, building, budget)\n
instructor(id,name,salary,dept_name)\ndepartment(dept_name,building,budget)\n
instructor(id,name,salary,dept_name,building,budget)
, then dept_name
would be a candidate key.In inst_dept
, dept_name
is not a candidate key.The building
and budget
of a department are not determined by the department name alone,may have to be repeated.
Lossy Decomposition: If we decompose a relation into smaller relations, we may lose information.
Goal : Devise a Theory for the Following
A constraint is a condition that must be true for any instance of the database.
Functional Dependencies are constraints on the set of legal relations.
Let R be a relation schema \\(\\alpha \\in R\\) and \\(\\beta \\in R\\). The functional dependency \\(\\alpha\\to\\beta\\) holds on R if and only if:
A functional dependency is trivial if \\(\\beta\\) is a subset of \\(\\alpha\\).
Given a set F of functional dependencies, there are certain other functional dependencies that are logically implied by F.
Example:
Armstrong\u2019s Axioms
Armstrong\u2019s Axioms are a set of rules that allow us to infer all the functional dependencies that hold on a relation.
The axioms are:
Transitivity: If \\(\\alpha\\) \\(\\to\\) \\(\\beta\\) and \\(\\beta\\) \\(\\to\\) \\(\\gamma\\), then \\(\\alpha\\) \\(\\to\\) \\(\\gamma\\).
These rules are:
Additional Rules (can be derived from Armstrong\u2019s Axioms)
Union: If \\(\\alpha\\) \\(\\to\\) \\(\\beta\\) and \\(\\alpha\\) \\(\\to\\) \\(\\gamma\\), then \\(\\alpha\\) \\(\\to\\) \\(\\beta\\gamma\\).
Decomposition: If \\(\\alpha\\) \\(\\to\\) \\(\\beta\\gamma\\), then \\(\\alpha\\) \\(\\to\\) \\(\\beta\\) and \\(\\alpha\\) \\(\\to\\) \\(\\gamma\\).
Pseudotransitivity: If \\(\\alpha\\) \\(\\to\\) \\(\\beta\\) and \\(\\gamma\\beta\\) \\(\\to\\) \\(\\delta\\), then \\(\\alpha\\gamma\\) \\(\\to\\) \\(\\delta\\).
Given a set of attributes X, the closure of X under F, denoted X+, is the set of all attributes that are functionally determined by X.
Example:
result := alpha;\nwhile (changes to result) do\n for each beta -> gamma in F do\n begin\n if beta in result then result := result Union gamma \n end\n
Intuitively, a canonical cover of F is a \u201cminimal\u201d set of functional dependencies equivalent to F, having no redundant dependencies or redundant parts of dependencies
"},{"location":"CMU%2015-445/Relational/lec/#extraneous-attributes","title":"Extraneous Attributes","text":"Constraints, including functional dependencies, are costly to check in practice unless they pertain to only one relation. If it is sufficient to test only those dependencies on each individual relation of a decomposition in order to ensure that all functional dependencies hold, then that decomposition is dependency preserving * Because it is not always possible to achieve both BCNF and dependency preservation, we consider a weaker normal form, known as third normal form.
Let R be a relation schemma with a set of attributes that are partitioned into 3 nonempty subsets Y,Z,W.
We say that Y->->Z if and only if for all possible relations r(R) \\(\\in\\) r and \\(\\in\\) r then we have \\(\\in\\) r and \\(\\in\\) r * Note:we can also get Y->->W
"},{"location":"CMU%2015-445/Relational/lec/#fourth-normal-forth","title":"Fourth Normal Forth","text":"Example
"},{"location":"CS231n/RNN/lec/","title":"Lecture 8: Recurrent Neural Networks","text":""},{"location":"CS231n/RNN/lec/#vanilla-recurrent-neural-network","title":"(Vanilla) Recurrent Neural Network","text":"The state consists of a single \u201chidden\u201d vector h:
\\(h_t = f_W(h_{t-1},x_t)\\)
\\(h_t = tanh(W_{hh}h_{t-1}+W_{xh}x_t)\\)
\\(y_t = W_{hy}h_t\\)\u200b
So Backward time, we need to sum over because \\(f_W\\) is repeatedly used many times.
"},{"location":"CS231n/RNN/lec/#many-to-many","title":"Many to Many","text":""},{"location":"CS231n/RNN/lec/#many-to-one","title":"Many to One","text":""},{"location":"CS231n/RNN/lec/#one-to-many","title":"One to Many","text":""},{"location":"CS231n/RNN/lec/#sequence-to-sequence-many-to-one-one-to-many","title":"Sequence to Sequence: Many-to-one + one-to-many","text":"Vocabulary: [h,e,l,o]
Example training sequence: \u201chello\u201d
At test-time sample characters one at a time, feed back to model
112
lines withoud using pytorch.\"\"\"\nMinimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy)\nBSD License\n\"\"\"\nimport numpy as np\n\n# data I/O\ndata = open('input.txt', 'r').read() # should be simple plain text file\nchars = list(set(data))\ndata_size, vocab_size = len(data), len(chars)\nprint 'data has %d characters, %d unique.' % (data_size, vocab_size)\nchar_to_ix = { ch:i for i,ch in enumerate(chars) }\nix_to_char = { i:ch for i,ch in enumerate(chars) }\n\n# hyperparameters\nhidden_size = 100 # size of hidden layer of neurons\nseq_length = 25 # number of steps to unroll the RNN for\nlearning_rate = 1e-1\n\n# model parameters\nWxh = np.random.randn(hidden_size, vocab_size)*0.01 # input to hidden\nWhh = np.random.randn(hidden_size, hidden_size)*0.01 # hidden to hidden\nWhy = np.random.randn(vocab_size, hidden_size)*0.01 # hidden to output\nbh = np.zeros((hidden_size, 1)) # hidden bias\nby = np.zeros((vocab_size, 1)) # output bias\n\ndef lossFun(inputs, targets, hprev):\n \"\"\"\n inputs,targets are both list of integers.\n hprev is Hx1 array of initial hidden state\n returns the loss, gradients on model parameters, and last hidden state\n \"\"\"\n xs, hs, ys, ps = {}, {}, {}, {}\n hs[-1] = np.copy(hprev)\n loss = 0\n # forward pass\n for t in xrange(len(inputs)):\n xs[t] = np.zeros((vocab_size,1)) # encode in 1-of-k representation\n xs[t][inputs[t]] = 1\n hs[t] = np.tanh(np.dot(Wxh, xs[t]) + np.dot(Whh, hs[t-1]) + bh) # hidden state\n ys[t] = np.dot(Why, hs[t]) + by # unnormalized log probabilities for next chars\n ps[t] = np.exp(ys[t]) / np.sum(np.exp(ys[t])) # probabilities for next chars\n loss += -np.log(ps[t][targets[t],0]) # softmax (cross-entropy loss)\n # backward pass: compute gradients going backwards\n dWxh, dWhh, dWhy = np.zeros_like(Wxh), np.zeros_like(Whh), np.zeros_like(Why)\n dbh, dby = np.zeros_like(bh), np.zeros_like(by)\n dhnext = np.zeros_like(hs[0])\n for t in reversed(xrange(len(inputs))):\n dy = np.copy(ps[t])\n dy[targets[t]] -= 1 # backprop into y. see http://cs231n.github.io/neural-networks-case-study/#grad if confused here\n dWhy += np.dot(dy, hs[t].T)\n dby += dy\n dh = np.dot(Why.T, dy) + dhnext # backprop into h\n dhraw = (1 - hs[t] * hs[t]) * dh # backprop through tanh nonlinearity\n dbh += dhraw\n dWxh += np.dot(dhraw, xs[t].T)\n dWhh += np.dot(dhraw, hs[t-1].T)\n dhnext = np.dot(Whh.T, dhraw)\n for dparam in [dWxh, dWhh, dWhy, dbh, dby]:\n np.clip(dparam, -5, 5, out=dparam) # clip to mitigate exploding gradients\n return loss, dWxh, dWhh, dWhy, dbh, dby, hs[len(inputs)-1]\n\ndef sample(h, seed_ix, n):\n \"\"\" \n sample a sequence of integers from the model \n h is memory state, seed_ix is seed letter for first time step\n \"\"\"\n x = np.zeros((vocab_size, 1))\n x[seed_ix] = 1\n ixes = []\n for t in xrange(n):\n h = np.tanh(np.dot(Wxh, x) + np.dot(Whh, h) + bh)\n y = np.dot(Why, h) + by\n p = np.exp(y) / np.sum(np.exp(y))\n ix = np.random.choice(range(vocab_size), p=p.ravel())\n x = np.zeros((vocab_size, 1))\n x[ix] = 1\n ixes.append(ix)\n return ixes\n\nn, p = 0, 0\nmWxh, mWhh, mWhy = np.zeros_like(Wxh), np.zeros_like(Whh), np.zeros_like(Why)\nmbh, mby = np.zeros_like(bh), np.zeros_like(by) # memory variables for Adagrad\nsmooth_loss = -np.log(1.0/vocab_size)*seq_length # loss at iteration 0\nwhile True:\n # prepare inputs (we're sweeping from left to right in steps seq_length long)\n if p+seq_length+1 >= len(data) or n == 0: \n hprev = np.zeros((hidden_size,1)) # reset RNN memory\n p = 0 # go from start of data\n inputs = [char_to_ix[ch] for ch in data[p:p+seq_length]]\n targets = [char_to_ix[ch] for ch in data[p+1:p+seq_length+1]]\n\n # sample from the model now and then\n if n % 100 == 0:\n sample_ix = sample(hprev, inputs[0], 200)\n txt = ''.join(ix_to_char[ix] for ix in sample_ix)\n print '----\\n %s \\n----' % (txt, )\n\n # forward seq_length characters through the net and fetch gradient\n loss, dWxh, dWhh, dWhy, dbh, dby, hprev = lossFun(inputs, targets, hprev)\n smooth_loss = smooth_loss * 0.999 + loss * 0.001\n if n % 100 == 0: print 'iter %d, loss: %f' % (n, smooth_loss) # print progress\n\n # perform parameter update with Adagrad\n for param, dparam, mem in zip([Wxh, Whh, Why, bh, by], \n [dWxh, dWhh, dWhy, dbh, dby], \n [mWxh, mWhh, mWhy, mbh, mby]):\n mem += dparam * dparam\n param += -learning_rate * dparam / np.sqrt(mem + 1e-8) # adagrad update\n\n p += seq_length # move data pointer\n n += 1 # iteration counter \n
"},{"location":"CS231n/RNN/lec/#rnn-tradeoffs","title":"RNN Tradeoffs","text":"RNN Advantages:
RNN Disadvantages:
Multiply \\(W_{hh}\\)\u200b many times ! -- Really Bad !
Largest singular value > 1:Exploding gradients
Gradient Clipping : scale it if the norm is too big -- exploding gradients
grad_norm = np.sum(grad*grad)\nif grad_norm > threshold:\n grad*=(threshold/gram_norm)\n
Change RNN architecture !
"},{"location":"CS231n/RNN/lec/#long-short-term-memory-lstm_1","title":"Long Short Term Memory (LSTM)","text":""},{"location":"CS231n/RNN/lec/#gradient-flow","title":"Gradient Flow","text":"Do LSTMs solve the vanishing gradient problem?
The LSTM architecture makes it easier for the RNN to preserve information over many timesteps
- e.g. if the f = 1 and the i = 0, then the information of that cell is preserved indefinitely.
- By contrast, it\u2019s harder for vanilla RNN to learn a recurrent weight matrix \\(W_h\\) that preserves info in hidden state
LSTM doesn\u2019t guarantee that there is no vanishing/exploding gradient, but it does provide an easier way for the model to learn long-distance dependencies
"},{"location":"CS231n/lec10/lec/","title":"Video Understanding","text":""},{"location":"CS231n/lec10/lec/#late-fusion-approach","title":"Late Fusion Approach","text":"FC layers may result in overfitting
"},{"location":"CS231n/lec10/lec/#early-fusion-approach","title":"Early Fusion Approach","text":"
* One layer of temporal processing may not be enough!
"},{"location":"CS231n/lec10/lec/#comparison","title":"Comparison","text":""},{"location":"CS231n/lec10/lec/#general-comparison","title":"General Comparison","text":""},{"location":"CS231n/lec10/lec/#2d-conv-early-fusion-vs-3d-conv-3d-cnn","title":"2D Conv (Early Fusion) vs 3D Conv (3D CNN)","text":"Details see Slides. * 2D : perceptive of features of time at once and slide over the space feature. No temporal shift-invariance! Needs to learn separate filters for the same motion at different times in the clip.
* Sometimes don\u2019t backprop to CNN to save memory; pretrain and use it as a feature extractor : Like using pretrained C3D as a feature extractor.
"},{"location":"CS231n/lec10/lec/#recurrent-convolutional-network","title":"Recurrent Convolutional Network","text":"Actually not too much used because of the time complexity.(Not good for parallelization) Sequential processing is not good for parallelization.
"},{"location":"CS231n/lec10/lec/#spatio-temporal-self-attention-non-local-block","title":"Spatio-Temporal Self-Attention (Non Local Block)","text":"Trick:Add non-local blocks to 3D CNNs , initilize the weights of the non-local block with all zeros, and fine-tune the network.
"},{"location":"CS231n/lec10/lec/#inflating-2d-networks-to-3d-i3d","title":"Inflating 2D Networks to 3D (I3D)","text":"Refer to the slides for details. Trick: Pretrain 2D CNNs and inflate them to 3D CNNs by repeating the weights along the temporal dimension.
"},{"location":"CS231n/lec12/lec/","title":"Visualizing and Understanding","text":""},{"location":"CS231n/lec12/lec/#visualizing-what-models-have-learned","title":"Visualizing what models have learned:","text":""},{"location":"CS231n/lec12/lec/#visualizing-filters","title":"Visualizing filters","text":""},{"location":"CS231n/lec12/lec/#visualizing-final-layer-features","title":"Visualizing final layer features","text":"PCA
t-SNE
Like Train A Best Image
\\(I^* = argmax_I f(I)+R(I)\\)
\\(argmax_IS_c(I)-\\lambda||I||_2^2\\)
Simple regularizer: Penalize L2 norm of generated image
Better regularizer: Penalize L2 norm of image , also during optimization periodically
(1) Gaussian blur image
(2) Clip pixels with small values to 0
(3) Clip pixels with small gradients to 0
"},{"location":"CS231n/lec12/lec/#adversarial-perturbations","title":"Adversarial perturbations","text":""},{"location":"CS231n/lec12/lec/#style-transfer","title":"Style transfer","text":""},{"location":"CS231n/lec12/lec/#features-inversion","title":"Features inversion","text":"Given a CNN feature vector for an image, find a new image that:
https://github.com/google/deepdream
"},{"location":"CS231n/lec12/lec/#texture-synthesis","title":"Texture synthesis","text":"See Slides
"},{"location":"CS231n/lec12/lec/#neural-style-transfe","title":"Neural style transfe","text":"Average over all pairs of vectors, giving Gram matrix of shape C x C
Efficient to compute; reshape features from CxHxW to =CxHW then compute G = FFT
Details see Slides.
Problem: Style transfer requires many forward / backward passes through VGG; very slow!
Solution: Train another neural network to perform style transfer for us!
https://github.com/jcjohnson/fast-neural-style
Instance Normalization was developed for style transfer!
Network only for a single style.
a
"},{"location":"CS231n/lec14/lec/","title":"Lecture 14 Robot Learning","text":"a
"},{"location":"CS231n/lec15/lec/","title":"Generative Models","text":"Supervised vs Unsupervised Learning
Refer to Slides.
"},{"location":"CS231n/lec15/lec/#discriminative-vs-generative-models","title":"Discriminative vs Generative Models","text":"Details see slides.
Discriminative Model: Learn a probability distribution \\(p(y|x)\\)
Assign labels to data
Feature learning (with labels)
Shortcomes
Generative Model: Learn a probability distribution \\(p(x)\\)\u200b
Detect outliers
Feature learning (without labels)
Sample to generate new data
Conditional Generative Model: Learn \\(p(x|y)\\)
Assign labels, while rejecting outliers!
Generate new data conditioned on input labels
\\(p(x|y)=\\frac{p(y|x)}{p(y)}p(x)\\)
We can build a conditional generative model from other components!
\u300a\u7edf\u8ba1\u5b66\u4e60\u65b9\u6cd5\u300b
,\u5c31\u662f\u4f18\u5316\u6781\u5927\u4f3c\u7136\u51fd\u6570.\\(\\begin{align*}p(x) &= p(x_1,x_2,x_3,...x_T)\\\\ &=p(x_1)p(x_2|x_1)p(x_3|x_1,x_2)....\\\\&=\\Pi_{t=1}^Tp(x_t|x_1,...x_{t-1})\\end{align*}\\)
https://arxiv.org/pdf/1601.06759.pdf
Diagnal Order
.Pros:
Can explicitly compute likelihood \\(p(x)\\)
Explicit likelihood of training data gives good evaluation metric
Good samples
Con:
Improving PixelCNN performance
Variational Autoencoders (VAE) define an intractable density that we cannot explicitly compute or optimize.
But we will be able to directly optimize a lower bound on the density !
"},{"location":"CS231n/lec15/lec/#regular-non-variational-autoencoders","title":"(Regular, non-variational) Autoencoders","text":"Unsupervised method for learning feature vectors from raw data x, without any labels
Problem: How can we learn this feature transform from raw data?
Idea: Use the features to reconstruct the input data with a decoder \u201cAutoencoding\u201d = encoding itself
Somehow Compress the input data.
After training, throw away decoder.
Encoder can be used to initialize a supervised model.
Autoencoders learn latent features for data without any labels! Can use features to initialize a supervised model
z for each x
, then could train a conditional generative model p(x|z)
Basic idea: maximize likelihood of data
Problem: Impossible to integrate over all z!
Problem: No way to compute this! \\(p_{\\theta}(z|x)\\)
Solution: Train another network (encoder) that learns \\(q_{\\Phi}(z|x)\\approx p_{\\theta}(z|x)\\)
Use encoder to compute \\(q_{\\Phi}(z|x)\\approx p_{\\theta}(z|x)\\)
"},{"location":"CS231n/lec9/lec/","title":"Attention and Transformers","text":"Log on probability : Preserve precision :
Problem: Input sequence bottlenecked through fixed-sized vector. What if T=1000?
"},{"location":"CS231n/lec9/lec/#attention","title":"Attention","text":""},{"location":"CS231n/lec9/lec/#sequence-to-sequence-with-rnns-and-attention","title":"Sequence to Sequence with RNNs and Attention","text":"Use \\(s_1\\) to compute new context vector \\(c_2\\)
Use \\(c_2\\) to compute \\(s_2\\), \\(y_2\\)
"},{"location":"CS231n/lec9/lec/#example","title":"Example","text":"Input: Image I
Output: Sequence y = \\(y_1, y_2,..., y_T\\)
Encoder: \\(h_0\\) = \\(f_W(z)\\) where z is spatial CNN features \\(f_W(.)\\) is an MLP
Decoder: \\(y_t = g_V(y_{t-1},h_{t-1}, c)\\) where context vector c is often \\(c = h_0\\)
"},{"location":"CS231n/lec9/lec/#image-captioning-with-rnns-and-attention","title":"Image Captioning with RNNs and Attention","text":"Attention idea: New context vector at every time step.Each context vector will attend to different image regions
This entire process is differentiable:
Each timestep of decoder uses a different context vector that looks at different parts of the input image.
"},{"location":"CS231n/lec9/lec/#attention-layer","title":"Attention Layer","text":"Attention operation is permutation invariant. - Doesn't care about ordering of the features - Stretch H x W = N into N vectors
Change \\(f_{att}\\) to a simple dot product - only works well with key & value transformation trick (will mention in a few slides)
Change \\(f_{att}(.)\\) to a scaled simple dot product - Larger dimensions means more terms in the dot product sum. - So, the variance of the logits is higher. Large magnitude vectors will produce much higher logits. - So, the post-softmax distribution has lower-entropy, assuming logits are IID. - Ultimately, these large magnitude vectors will cause softmax to peak and assign very little weight to all others - Divide by \u221aD to reduce effect of large magnitude vectors
Notice that the input vectors are used for both the alignment as well as the attention calculations. - We can add more expressivity to the layer by adding a different FC layer before each of the two steps. * Self Attention Layer does not CARE about the order of the input vectors! * Use positional encoding to give the model some notion of order.
Desiderata of pos(.) :
"},{"location":"CV/ML-4360/BS/lec2/#lec2-for-ml-4360","title":"Lec2 for ML-4360","text":""},{"location":"CV/ML-4360/BS/lec2/#21-primitives-transformations","title":"2.1 Primitives & Transformations","text":""},{"location":"CV/ML-4360/BS/lec2/#homogeneous","title":"\\(Homogeneous\\)","text":"
\\(\\begin{bmatrix}I&t\\\\0^{T}&1\\end{bmatrix}\\)
"},{"location":"CV/ML-4360/BS/lec2/#euclidean-3dof","title":"Euclidean 3DOF","text":"\\(RR^T=I \\ der(R)=1\\)\u200b
\\(\\begin{bmatrix}R&t\\\\0^{T}&1\\end{bmatrix}\\)
\u6b63\u4ea4\u77e9\u9635\\(A^TA=I\\)
\\(\\begin{bmatrix}sR&t\\\\0^{T}&1\\end{bmatrix}\\)\u200b
\\(RR^{T}=I\\)
"},{"location":"CV/ML-4360/BS/lec2/#affine-6dof","title":"Affine: 6DOF","text":"\\(\\begin{bmatrix}A&t\\\\0^{T}&1\\end{bmatrix}\\)
arbitrary \\(2\u00d72\\) matrix
Parallels Remain!
An orthographic projection simply drops the z component of the 3D point in camera
coordinates \\(x_c\\) to obtain the corresponding 2D point on the image plane (= screen) \\(x_s\\)
\\(\\bar{x_s}=\\begin{bmatrix}s&0&0&0\\\\0&s&0&0\\\\0&0&0&1\\end{bmatrix}\\).
"},{"location":"CV/ML-4360/BS/lec2/#perspective-projection","title":"Perspective projection","text":""},{"location":"CV/ML-4360/BS/lec2/#chaining-transformations","title":"Chaining Transformations","text":"\\(\\tilde{x}_s=K[R \\ t]\\bar{x}_w\\)
\u6211\u7684\u7406\u89e3\uff1a\\(\\bar{x}_s\\)\u662f\u771f\u5b9e\u7684\u76f8\u673a\u5750\u6807\u7cfb\u4e0b\u7684\u5750\u6807\uff0c\u4e0e\\(\\tilde{x}_s\\)\u5dee\u4e00\u4e2a\u7cfb\u6570\uff0c\u5982\u679c\u6211\u4eec\u77e5\u9053\u8fd9\u4e2a\u7cfb\u6570\uff0c\u5c31\u53ef\u4ee5\u6062\u590d\u51fa\\(\\tilde{x}_s\\)\u5e76\u6839\u636e\\(\\tilde{x}_s\\)\u7684\u8ba1\u7b97\u65b9\u6cd5\u6062\u590d\u51fa\u4e16\u754c\u5750\u6807\u7cfb\u4e0b\u7684\u5750\u6807\\(\\bar{x}_c\\)
"},{"location":"CV/ML-4360/BS/lec2/#23-photometric-image-formation","title":"2.3 Photometric Image Formation","text":""},{"location":"CV/ML-4360/BS/lec2/#rendering-equation","title":"\\(Rendering\\ Equation\\)","text":"\\(dw=\\sin\\theta\\)\\(d\\theta\\)\\(d\\phi\\)
E(x)=d\\(\\Phi(x)\\)/dA
\\(L(p,w)=d^2\\Phi(p,\\omega)\\)/\\(d\\omega\\)\\(dAcos\\theta\\)
\\(L_r(p,w_r)=\\int_{H^2}f_r(p,w_i\\rightarrow w_r)L_i(p,w_i)cos\\theta_id_{w_i}\\)
"},{"location":"CV/ML-4360/BS/lec2/#back-to-cameras","title":"Back To Cameras","text":""},{"location":"CV/ML-4360/CBN/lec9/","title":"Coordinate-Based Networks","text":""},{"location":"CV/ML-4360/CBN/lec9/#implicit-neural-representations","title":"Implicit Neural Representations","text":""},{"location":"CV/ML-4360/CBN/lec9/#what-is-a-good-output-representation","title":"What is a good output representation?","text":"
Flat surface only in the canonical directions
No surface
#### Occupancy Networks
Also possible: signed distance field [Park et al., 2019]
\\(L(\\theta,\u03c8) = \\sum_{j=1}^KBCE(f_\u03b8(p_{ij},z_{i}),o_{ij})+KL[q_\u03c8(z|(p_{ij},o_{ij})_{j=1:K})\u2225p0(z)]\\)\u200b
Conditional Surface Light Field
Given any light & view direction, we want to know the color
"},{"location":"CV/ML-4360/CBN/lec9/#representing-motion","title":"Representing Motion","text":"Problems for occupancy network
Implicit models work well for simple objects but poorly on complex scenes
"},{"location":"CV/ML-4360/CBN/lec9/#convolutional-occupancy-networks","title":"Convolutional Occupancy Networks","text":""},{"location":"CV/ML-4360/CBN/lec9/#differentiable-volumetric-rendering","title":"Differentiable Volumetric Rendering","text":""},{"location":"CV/ML-4360/CBN/lec9/#learning-from-images","title":"Learning from Images","text":"Secant Method : A finite-difference approximation of Newton\u2019s method.
"},{"location":"CV/ML-4360/CBN/lec9/#backward-passdifferentiation","title":"Backward Pass(Differentiation)","text":"Comes to Backward Pass
Image Observation I
Loss \\(L(\\hat{I},I) = \\sum_u||\\hat{I}_u-I_u||\\)\u200b
Gradient of Loss Function
\\(\\frac{\\partial{L}}{\\partial{\\theta}} = \\sum_u \\frac{\\partial L}{\\partial\\hat{I_u}}\u00b7\\frac{\\partial{\\hat{I_u}}}{\\partial\\theta}\\)\u200b
\\(\\frac{\\partial{\\hat{I_u}}}{\\partial\\theta}=\\frac{\\partial{t_{\\theta}(\\hat{p})}}{\\partial\\theta} +\\frac{\\partial{t_{\\theta}(\\hat{p})}}{\\partial\\hat{p}}\u00b7\\frac{\\partial{\\hat{p}}}{\\partial\\theta}\\)\u200b
Note : \\(\\hat{p}\\) (The Surface Intersection) also depends on \\(\\theta\\)\u200b !!
So we have to apply total derivative !!
Differentiation of \\(f_{\\theta}(\\hat{p}=\\tau)\\) yields :
\\(\\frac{\\partial\\hat{p}}{\\partial\\theta}=-w(\\frac{\\partial f_{\\theta}(\\hat{p})}{\\partial\\hat{p}}\u00b7w)^{-1}\\frac{\\partial f_{\\theta}(\\hat{p})}{\\partial\\theta}\\)\u200b\u200b
Derivation :
Note \\(\\bold{w}\\) is a vector!
"},{"location":"CV/ML-4360/CBN/lec9/#neural-radiance-fields","title":"Neural Radiance Fields","text":"Task: Given a set of images of a scene, render image from novel viewpoint
\\(min_{\\theta}=\\sum_i||render_i(F_{\\theta})-I_i||^2\\)\u200b
Shoot ray, render ray to pixel, minimize reconstruction error via backpropagation
From the Paper :
We encourage the representation to be multiview consistent by restricting the network to predict the volume density \u03c3 as a function of only the location x, while allowing the RGB color c to be predicted as a function of both location and viewing direction.
To accomplish this, the MLP \\(F_\u0398\\) first processes the input 3D coordinate x with 8 fully-connected layers (using ReLU activations and 256 channels per layer), and outputs \u03c3 and a 256-dimensional feature vector.
This feature vector is then concatenated with the camera ray\u2019s viewing direction and passed to one additional fully-connected layer (using a ReLU activation and 128 channels) that output the view-dependent RGB color.
"},{"location":"CV/ML-4360/CBN/lec9/#volume-rendering-with-radiance-fields","title":"Volume Rendering with Radiance Fields","text":"From the Paper :
The volume density \\(\u03c3(x)\\) can be interpreted as the differential probability of a ray terminating at an infinitesimal particle at location \\(x\\)\u200b.
The expected color \\(C(r)\\) of camera ray \\(r(t) = o + td\\) with near and far bounds \\(t_n\\) and \\(t_f\\) is:
Having the network \\(F_\u0398\\) directly operate on \\(xyz\u03b8\u03c6\\)\u200b input coordinates results in renderings that perform poorly at representing high-frequency variation in color and geometry
(Recap Transformers)
"},{"location":"CV/ML-4360/PGM/lec5/#probabilistic-graphical-models","title":"Probabilistic Graphical Models","text":""},{"location":"CV/ML-4360/PGM/lec5/#structured-prediction","title":"Structured Prediction","text":""},{"location":"CV/ML-4360/PGM/lec5/#spatial-regularization","title":"Spatial regularization","text":"
\\(p(D)\\propto e^{-\\sum_i\\phi_{data}(d_i)-\\lambda\\sum_{(i\uff0cj)\\in S}\\phi_{smooth}(d_i,d_j)}\\)
\\(i\\)~\\(j\\) neighbouring pixels (on a 4-connected grid).
$\\phi_{smooth} $ is a regularization term that encourages neighboring pixels to have similar disparities.
where \\(I\\)and \\(J\\) are the image pairs. \\(\u03c3\\) and \\(\u03c4\\) are truncation thresholds.
Probabilistic graphical models encode local dependencies of the problem
Deep neural netwoks with image-based outputs (stereo, flow, semantics)
"},{"location":"CV/ML-4360/PGM/lec5/#markov-random-fields","title":"Markov Random Fields","text":""},{"location":"CV/ML-4360/PGM/lec5/#undirected-graphical-models-ugm","title":"Undirected graphical models (UGM)","text":"Refers to fully connected subgraphs in a graphical model, particularly in models like Markov Random Fields or Conditional Random Fields.
In this context, a clique is a group of nodes in a graph where every pair of nodes is directly connected.
"},{"location":"CV/ML-4360/PGM/lec5/#potential","title":"potential","text":"\\(P(x_1\u2026\u2026x_n)=\\frac{1}{Z}\\prod_{c\\in C}\\phi_c(x_c)\\)
\\(Z = \\sum_{x_1\u2026\u2026x_n}\\prod_{c\\in C}\\phi_c(x_c)\\)
"},{"location":"CV/ML-4360/PGM/lec5/#defination-of-markov-random-field","title":"Defination of Markov Random Field","text":"\\(p(X)=\\frac{1}{Z}\\prod_{k=1}^K\\phi_k(X_k)\\)
Examples\uff1a
"},{"location":"CV/ML-4360/PGM/lec5/#properties","title":"Properties","text":"\\(Condition \\ One\\)
"},{"location":"CV/ML-4360/PGM/lec5/#marginalizing-over-c-makes-a-and-b-dependent","title":"Marginalizing over c makes a and b dependent","text":"\\(Proof\\)
\\(Proof\\)
"},{"location":"CV/ML-4360/PGM/lec5/#global-markov-property","title":"Global Markov Property","text":""},{"location":"CV/ML-4360/PGM/lec5/#local-markov-property","title":"Local Markov Property","text":"A probability distribution that has a strictly positive mass or density satisfies the Markov properties with respect to an undirected graph G if and only if it is a Gibbs random field,
i.e. its density can be factorized over the (maximal) cliques of the graph.
"},{"location":"CV/ML-4360/PGM/lec5/#factor-graphs","title":"Factor Graphs","text":"\\(p(X) = \\frac{1}{Z}\\prod_{k=1}^Kf_k(X_k)_{k=1}^K\\)\u200b
"},{"location":"CV/ML-4360/PGM/lec5/#example","title":"Example","text":""},{"location":"CV/ML-4360/PGM/lec5/#belief-propagation","title":"Belief Propagation","text":""},{"location":"CV/ML-4360/PGM/lec5/#inference-in-chain-structured-factor-graphs","title":"Inference in Chain Structured Factor Graphs","text":"\\(p(a, b, c, d) = \\frac{1}{Z}f_1(a, b)f_2(b, c)f_3(c, d)f4(d)\\)
\\(p(a,b,c) = \\sum_{d}p(a,b,c,d)\\)
\\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ = \\frac{1}{Z}f_1(a,b)f_2(b,c)\\underbrace{\\sum_{d}f_3(c,d)f_4(d)}_{\u03bc_{d\u2192c}(c)}\\)
\\(p(a,b) = \\sum_{c}p(a,b,c)\\)
\\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ = \\frac{1}{Z}f_1(a,b)\\underbrace{\\sum_{c}f_2(b,c)\u03bc_{d\u2192c}(c)}_{\u03bc_{c\u2192b}(b)}\\)
\\(\u2026\u2026\\)
Factor-to-Variable Messages
Variable-to-Factor Messages
"},{"location":"CV/ML-4360/PGM/lec5/#sum-product-algorithm","title":"Sum-Product Algorithm","text":""},{"location":"CV/ML-4360/PGM/lec5/#belief-propagation_1","title":"Belief Propagation:","text":"\\(\\begin{align*}\\underset{\\text{a,b,c,d}}{\\text{max}}p(a,b,c,d)&= \\underset{\\text{a,b,c,d}}{\\text{max}}f_1(a,b)f2(b,c)f_3(c,d)\\\\&=\\underset{\\text{a,b,c}}{\\text{max}}f_1(a,b)f2(b,c)\\underbrace{\\underset{\\text{d}}{\\text{max}}f_3(c,d)}_{\\mu_{d->c}(c)}\\\\&=\u2026\u2026\\\\ &=\\underset{\\text{a}}{\\text{max}}\\mu_{b->a}(a)\\end{align*}\\)
"},{"location":"CV/ML-4360/PGM/lec5/#loopy-belief-propagation","title":"Loopy Belief Propagation","text":"Max-Product Belief Propagation on chain structured Markov Random Fields for Vehicle Localization
Let's consider an autonomous vehicle driving on a highway and tracking a vehicle in front in order to initiate an overtaking maneuver. Let \\(x_t\\in\\{1,2,3\\}\\) denote the lane the vehicle in front is driving on at time \\(t\\in\\{1,\\dots,10\\}\\). Unfortunately, the sensor readings are noisy as depicted below.
Selecting the most likely lane at each time \\(t\\) independently (green) leads to wrong estimates for \\(t=\\{3,7,10\\}\\). To solve this problem, and recover the correct situation depicted below
we can integrate prior knowledge and infer the most likely situation using max-product belief propagation. A sensible prior would favor staying on the same lane over changing one lane at a time over changing two lanes at a time. This prior can be integrated via a pairwise, chain-structured Markov Random Field (also called: Hidden Markov Model or HMM) where pairwise factors between adjacent frames modulate transition likelihoods:
\\[g_\\theta(x_i,x_{i+1}) = \\begin{bmatrix}0.8 & 0.2 & 0.0\\\\ 0.2 & 0.6 & 0.2 \\\\ 0.0 & 0.2 & 0.8\\end{bmatrix}\\] \\[ p_\\theta({\\bf x}) \\propto \\prod_{1}^{10} f_i(x_i)\\prod_{1}^{9}g_{\\theta}(x_i, x_{i+1})\\]You are given a noisy binary image (\\(10 \\times 10\\) pixels) which you want to denoise.
Make use of the Ising model for that purpose where neighboring pixels are encouraged to take the same value: \\(\\(p(x_1,\\dots,x_{100}) \\propto \\exp \\left\\{\\sum_{i=1}^{100} \\psi_i(x_i) + \\sum_{i\\sim j} \\psi_{ij} (x_i,x_j) \\right\\}\\)\\)
Here, \\(i\\) is the pixel index and \\(i\\sim j\\) are neighboring pixels on a 4-connected grid. The unary term \\(\\psi_i(x_i) = [x_i = o_i]\\) models the observation at pixel \\(i\\), and the pairwise term is the Ising prior \\(\\psi_{ij}(x_i,x_j) = \\alpha \\cdot [x_i = x_j]\\), where \\(\\alpha\\) controls the strength of the interaction/smoothing.
Because we have large number of variables in this exercise, we use logarithm factors to avoid potential numerical underflow issues.
Inputs: * num_vars
, num_states
, factors
, msg_fv
, msg_vf
, ne_var
Outputs: * max_marginals
: num_vars
x num_states
array of estimated max-marginals * map_est
: array comprising the estimated MAP state of each variable
Algorithm Pseudocode:
N=30
iterations do:Update all variable-to-factor messages: \\(\\lambda_{x\\rightarrow f}(x) = \\sum_{g\\in\\{ ne(x)\\setminus f\\}}\\lambda_{g\\rightarrow x}(x)\\)
Calculate Max-Marginals: \\(\\gamma_x(x) = \\sum_{g\\in\\{ ne(x)\\}}\\lambda_{g\\rightarrow x}(x)\\)
To minimize the ENERGY
Add Truncated penalty
\\(\\lambda\\)\u200b -- \"tradeoff\"
Very LOCAL pairwise terms cannot deal with some cases (like reflections)
Block matching suffers from ambiguities
Choosing window size is problematic (tradeoff)
Incorporating smoothness constraints can resolve some of the ambiguities and allows for choosing small windows (no bleeding artifacts)
Can be formulated as MAP inference in a discrete MRF
Representation
Voxel occupancy: This concept describes whether a voxel is occupied by a solid entity. In many tasks such as 3D reconstruction, object detection, SLAM (Simultaneous Localization and Mapping), etc., voxel occupancy is crucial. Typically, a voxel is either occupied by a solid entity or empty (occupied by other objects or background in space). In representations of voxel occupancy, a common method is binary, where a voxel is considered occupied when it's 1 and empty otherwise.
Voxel appearance: This concept describes the visual properties of a voxel, i.e., how it appears in images or voxel grids. It may include color, texture, brightness, etc.
Image Formation Process
Probabilistic Model
Joint Distribution: \\(p(\\bold{O},\\bold{A})=\\frac{1}{Z}\\Pi_{v\\in\\bold{V}}\u03c6_v(o_v)\\Pi_{r\\in\\bold{R}}\u03c8_r(o_r, a_r)\\)\u200b
Unary Potentails : \\(\u03c6_v(o_v) = \u03b3^{o_v} (1 \u2212 \u03b3)^{(1\u2212o_v)}\\)
Most voxels are empty \u21d2 \\(\u03b3\\)\u200b < 0.5
Ray Potentials :
If first occupied voxel \\(a_i^r\\) similar to the corresponding pixel , \\(\u03c8(o_r,a_r)\\) will increase
Depth Distribution For Single Ray
Message from and to the unary factors \\(\\mu_{\u03c6_i\\to o_i}\\) \\(\\mu_{o_i\\to \u03c6_i}\\) and \\(\\mu_{o_i\\to \u03c8_i}\\) can be easilly computed.
Occupancy message
\u200b Thus, resulting in [DERIVATION SEE PAPER BELOW]
\u200b \\(\\mu_{\u03c8\\to o_1}(o_1=1)=\\int_{a_1}v(a_1)\\mu(a_1)da_1\\)\u200b
\u200b \\(\\mu_{\u03c8\\to o_1}(o_1=0)=\\sum_{j=2}^N\\mu(o_j=1)\\Pi_{k=2}^{j-1}\\mu(o_k=0)\\rho_j\\)
\u200b \\(\\rho_j=\\int _{a_j}v(a_j)u(a_j)da_j\\)
\u200b More General :[see PAPER]
Bayes Optimal Depth Estimation
Depth \\(D\u2208\\{d_1,...,d_N\\}\\):distance to closest occupied voxel
Optimal depth estimate:
\\(p(D=d_i)\u221d\\mu(o_i=1)\\Pi_{j=1}^{i-1}\\mu(o_j=0)\\rho_i\\) [Derivation see PPT & Paper]
Requires marginal depth distribution \\(p(D)\\) along each ray
Motion field:
Optical flow:
Solution: linearize the brightness constancy assumption
\\(f(x,y)\u2248f(a,b)+ \\frac{\\partial f(a,b)}{\u2202x} (x\u2212a)+ \\frac{\\partial f(a,b)}{\u2202y} (y\u2212b)\\)\u200b
Thus , we have \\(I(x + u(x, y), y + v(x, y), t + 1) \\approx I(x,y,t)+I_x(x,y,t)u(x,y)+I_y(x,y,t)v(x,y)+I_t(x,y,t)\\)
\\(E(u,v) \\approx \\iint [(I_x(x,y,t)u(x,y)+I_y(x,y,t)v(x,y)+I_t(x,y,t))^2+\\lambda(||\\triangledown u(x,y||)^2+||\\triangledown v(x,y||)^2]dxdy\\)
which leads to the following discretized objective:
The text points out the invalidity of two assumptions. Firstly, the assumption of having a smooth probability distribution in the image at object boundaries is invalid because object boundaries typically exhibit discontinuities, whereas Gaussian distributions assume continuity.
Gaussian distributions correspond to quadratic loss functions. This means that using Gaussian distributions to model smoothness is equivalent to using squared terms in the loss function, which is somewhat sensitive to outliers.
Squared loss functions are not sufficiently robust to outliers. This means that in the presence of outliers, using squared loss functions may lead to larger errors, as they heavily influence the process of minimizing the error.
Outliers typically occur at object boundaries, violating the assumptions of smoothness and regularization. Object boundaries usually have sharp transitions, so outliers may occur in these areas.
Outliers may also occur at specular highlights, violating the assumptions of photoconsistency and data terms. Specular highlights often result in very high brightness or intensity in image regions, which may differ significantly from the surrounding areas, thus being considered outliers.
"},{"location":"CV/ML-4360/PGM/lec5/#learning-in-graphical-models","title":"Learning in Graphical Models","text":""},{"location":"CV/ML-4360/PGM/lec5/#conditional-random-fields","title":"Conditional Random Fields","text":"
\\(p(x_1,...,x_{100})= \\frac{1}{\\bold{Z}}exp\\{\\sum_i\u03c8_i(x_i)+\u03bb\\sum_{i\\to j}\u03c8_{ij}(x_i,x_j)\\}\\) * How to estimate the parameters , say \\(\\lambda\\)
"},{"location":"CV/ML-4360/PGM/lec5/#parameter-estimation","title":"Parameter Estimation","text":""},{"location":"CV/ML-4360/Recognition/lec10/#object-detection","title":"Object Detection","text":"
Input: Single RGB Image
Output: A set of detected objects:
Challenges
Apply a CNN to many different crops of the image, CNN classifies each crop as object or background
We should consider all possible sizes and possible positions
Solutions
Region Proposals
Use MultiTask Loss
Bbox : Modify the Region Proposals
\\(L_2\\) regularization encourages leaving proposal unchanged.
Transform encodes relative difference between proposal and output; important since \\(CNN\\) doesn\u2019t see absolute size or position after cropping
"},{"location":"CV/ML-4360/Recognition/lec10/#r-cnn-training","title":"R-CNN Training","text":"Positive: > 0.5 IoU with a GT box
Negative: < 0.3 IoU with all GT boxes
Neutral: between 0.3 and 0.5 IoU with GT boxes
Problem: Object detectors often output many overlapping detections:
Solution: Post-process raw detections using Non-Max Suppression (NMS)
Select next highest-scoring box
Eliminate lower-scoring boxes with IoU > threshold (e.g. 0.7)
If any boxes remain, GOTO 1
Problem: NMS may eliminate \u201dgood\u201d boxes when objects are highly overlapping...
no good solution =(
R-CNN Problem: Very slow! Need to do 2000 forward passes through CNN per image
Idea: Overlapping proposals cause a lot of repeated work: same pixels processed many times. Can we avoid this?
Per-Region network is relatively lightweight
Most of the computation happens in backbone network;
this saves work for overlapping region proposals.
How to crop features?
"},{"location":"CV/ML-4360/Recognition/lec10/#cropping-features-roi-pool","title":"Cropping Features: RoI Pool","text":"Classic idea: build an image pyramid by resizing the image to different scales, then process each image scale independently.
Problem: Expensive! Don\u2019t share any computation between scales.
Simple Idea : Sliding Window
Use Fully Convolutional Network
Dose not have any pooling layers or fully connected layers.
Problem #1: Effective receptive field size is linear in number of conv layers:
Let's say we have L 3x3 convolutional layers. In a CNN, each neuron in a convolutional layer is connected to a small region of neurons from the previous layer, which is its receptive field. For a 3x3 convolutional kernel, the receptive field size is 3x3.
Now, consider L 3x3 convolutional layers. In the first layer, the receptive field of each neuron is 3x3. In the second layer, the receptive field of each neuron relative to the input image will cover a larger area, which is the combination of receptive fields from the previous layer. Similarly, in the third layer, the receptive field relative to the input image will be even larger.
Therefore, as the number of layers increases, the influence of each layer on the input image gradually expands. Assuming each convolutional layer has a stride of 1, and there are no pooling layers or padding, after L 3x3 convolutional layers, the receptive field size relative to the input image for each neuron will be 1 plus twice the number of convolutional layers, denoted as 1+2L.
Problem #2: Convolution on high res images is expensive! Recall ResNet stem aggressively downsamples
"},{"location":"CV/ML-4360/Recognition/lec10/#in-network-upsampling-unpooling","title":"In-Network Upsampling: \u201cUnpooling\u201d","text":"Refer to Slides
Refer to Slides
Learnable Upsampling: Transposed Convolution
Convolution with stride > 1 is \u201cLearnable Downsampling\u201d
Detect all objects in the image, and identify the pixels that belong to each object (Only things!)
Approach: Perform object detection, then predict a segmentation mask for each object!
"},{"location":"CV/ML-4360/SFM/lec3/#structure-form-motion","title":"Structure Form Motion","text":""},{"location":"CV/ML-4360/SFM/lec3/#preliminarynaries","title":"preliminarynaries","text":""},{"location":"CV/ML-4360/SFM/lec3/#point-features","title":"Point Features","text":"
Reference to https://medium.com/@deepanshut041/introduction-to-sift-scale-invariant-feature-transform-65d7f3a72d40 https://www.qixinbo.info/2021/10/26/sift/
\\(L(x,y,\u03c3)=G(x,y,\u03c3)\u2217I(x,y)\\)
Note \uff1a\u5377\u79ef
\u8fde\u7eed\u5f62\u5f0f\uff1a
\\((f\u2217g)(n)=\\int_{-\\infty }^{\\infty}f(\\tau )g(n-\\tau)d\\tau\\)
\u79bb\u6563\u5f62\u5f0f\uff1a
\\((f\u2217g)(n)=\\sum_{\\tau=-\\infty }^{\\infty}f(\\tau)g(n-\\tau)\\)
\u5bf9\u56fe\u50cf\u7684\u5904\u7406\u51fd\u6570\uff08\u5982\u5e73\u6ed1\uff0c\u6216\u8005\u8fb9\u7f18\u63d0\u53d6\uff09\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e00\u4e2ag\u77e9\u9635\u6765\u8868\u793a\uff0c\u5982\uff1a
\\(g=\\begin{bmatrix} &b_{-1,-1} &b_{-1,0} &b_{-1,1}\\\\ &b_{0,-1} &b_{0,0} &b_{0,1} \\\\ &b_{1,-1} &b_{1,0} &b_{1,1} \\end{bmatrix}\\)
\\(f(x,y)=a_{x,y}\\)
\\(g(x,y)=b_{x,y}\\)
\u6309\u5377\u79ef\u7684\u5b9a\u4e49\uff0c\u4e8c\u7ef4\u79bb\u6563\u5f62\u5f0f\u7684\u5377\u79ef\u516c\u5f0f\u5e94\u8be5\u662f\uff1a
\\((f\u2217g)(u,v)=\u2211_i\u2211_jf(i,j)g(u-i,v-j)=\\sum_{i} \\sum_{j} a_{i,j} b_{u-i,v-j}\\)
\u9996\u5148\u6211\u4eec\u5728\u539f\u59cb\u56fe\u50cf\u77e9\u9635\u4e2d\u53d6\u51fa\uff08u,v)\u5904\u7684\u77e9\u9635\uff1a
\\(f=\\begin{bmatrix} &a_{u-1,v-1} &a_{u-1,v} &a_{u-1,v+1}\\\\ &a_{u,v-1} &a_{u,v} &a_{u,v+1} \\\\ &a_{u+1,v-1} &a_{u+1,v} &a_{u+1,v+1} \\end{bmatrix}\\)
\u7136\u540e\u5c06\u56fe\u50cf\u5904\u7406\u77e9\u9635\u7ffb\u8f6c
\\((g^{'}=\\begin{bmatrix} &b_{1,1} &b_{1,0} &b_{1,-1}\\\\ &b_{0,1} &b_{0,0} &b_{0,-1} \\\\ &b_{-1,1} &b_{-1,0} &b_{-1,-1} \\end{bmatrix}\\)
"},{"location":"CV/ML-4360/SFM/lec3/#2construct-the-gaussian-pyramid","title":"(2)Construct the Gaussian Pyramid","text":"Image feature points are composed of local extrema in the DoG (Difference of Gaussians) scale space. To find these extrema in the DoG space, each pixel is compared with all of its neighboring points to determine whether it is larger or smaller than its neighboring points in both the image domain and scale domain. As shown in the figure below, the central detection point is compared with its 8 neighboring points at the same scale and 18 points corresponding to the upper and lower adjacent scales, totaling 26 points to ensure the detection of extrema in both the two-dimensional image space and scale space. If a point is the maximum or minimum value among these 26 points in the DoG scale space at the current layer and the two neighboring layers, it is considered a feature point in the image at that scale.
"},{"location":"CV/ML-4360/SFM/lec3/#4keypoint-localization","title":"\uff084\uff09Keypoint Localization","text":"Key0points generated in the previous step produce a lot of keypoints. Some of them lie along an edge, or they don\u2019t have enough contrast. In both cases, they are not as useful as features. So we get rid of them. The approach is similar to the one used in the Harris Corner Detector for removing edge features. For low contrast features, we simply check their intensities.
They used Taylor series expansion of scale space to get a more accurate location of extrema, and if the intensity at this extrema is less than a threshold value (0.03 as per the paper), it is rejected. DoG has a higher response for edges, so edges also need to be removed. They used a 2x2 Hessian matrix (H) to compute the principal curvature.
Detailed explanation:
Keypoint Filtering: Keypoints generated in the previous step might include features along edges or those with low contrast, which may not be sufficiently prominent or useful. For features with low contrast, their intensities are examined. If the intensity is below a specified threshold (as mentioned in the paper, 0.03), the feature is excluded.
Taylor Series Expansion: Utilizing a Taylor series expansion of the scale space to obtain a more accurate location of extrema, improving the precision of keypoint localization. Intensity Check and Exclusion: For extrema identified through the Taylor series expansion, their intensities are checked. If the intensity falls below the set threshold, they are excluded.
Edge Removal: Due to the higher response of the Difference of Gaussians (DoG) for edges, further removal of edge features is necessary. A 2x2 Hessian matrix is employed to calculate the principal curvature. If the principal curvature is small, indicating the point may lie along an edge, it can be excluded. (\\(f=\\frac{\\lambda_1\\lambda_2}{\\lambda_1+\\lambda_2}\\)) for the Harris operator Which means f is large indicates an edge
Now we have legitimate keypoints. They\u2019ve been tested to be stable. We already know the scale at which the keypoint was detected (it\u2019s the same as the scale of the blurred image). So we have scale invariance. The next thing is to assign an orientation to each keypoint to make it rotation invariance.
A neighborhood is taken around the keypoint location depending on the scale, and the gradient magnitude and direction is calculated in that region. An orientation histogram with 36 bins covering 360 degrees is created. Let's say the gradient direction at a certain point (in the \u201corientation collection region\u201d) is 18.759 degrees, then it will go into the 10\u201319-degree bin. And the \u201camount\u201d that is added to the bin is proportional to the magnitude of the gradient at that point. Once you\u2019ve done this for all pixels around the keypoint, the histogram will have a peak at some point. The highest peak in the histogram is taken and any peak above 80% of it is also considered to calculate the orientation. It creates keypoints with same location and scale, but different directions. It contributes to the stability of matching.
"},{"location":"CV/ML-4360/SFM/lec3/#6keypoint-descriptor","title":"(6)Keypoint descriptor","text":"At this point, each keypoint has a location, scale, orientation. Next is to compute a descriptor for the local image region about each keypoint that is highly distinctive and invariant as possible to variations such as changes in viewpoint and illumination.
To do this, a 16x16 window around the keypoint is taken. It is divided into 16 sub-blocks of 4x4 size. For each sub-block, 8 bin orientation histogram is created.
So 4 X 4 descriptors over 16 X 16 sample array were used in practice. 4 X 4 X 8 directions give 128 bin values. It is represented as a feature vector to form keypoint descriptor. This feature vector introduces a few complications. We need to get rid of them before finalizing the fingerprint.
Let \\(K_i \u2208 R_{3\u00d73}\\)denote the camera matrix of camera \\(i\\).
Let \\(\\widetilde{x}_i\\)= \\(K_i^{-1} x \u0304_i\\)denote the local ray direction of pixel \\(x \u0304_i\\) in camera \\(i\\).
We have:
\\(\\tilde{x}_2 \u221d x_2 = Rx_1+t \u221d R\\tilde{x}_1+st\\)
Taking the cross product of both sides with t we obtain:
\\([t]_\u00d7\\tilde{x}_2 \u221d [t]_\u00d7R\\tilde{x}_1\\)
$\\tilde{x}^\u22a4_2 [t]\u00d7R\\tilde{x}_1 \u221d \\tilde{x}^\u22a4_2 [t]\u00d7\\tilde{x}2 = 0 $
\\(\u21d2 \\tilde{x}^\u22a4_2 [t]\u00d7R \\tilde{x}_1 = 0\\)
\\(\\tilde{x}^\u22a4_2 \\tilde{E}\\tilde{x} _1 = 0\\)
\\(\\widetilde{E} = [ t ] \u00d7 R\\)\u200b
\\(\\widetilde{E}\\) maps a point \\(\\tilde{x}_1\\) in image \\(1\\) to the corresponding epipolar line in image 2 \\(\\widetilde{l}_2=\\widetilde{E} \\tilde{x}_1\\)
Similarly, by transposition, we obtain the epipolar line in image 1:\\(\\widetilde{l}_1 = \\widetilde{E}^\u22a4\\tilde{x}_2\\)
For any point \\(\\tilde{x}_1\\)in the first image, the corresponding epipolar line \\(\\widetilde{l}_2 = \\widetilde{E}\\tilde{x}_1\\) in the second image passes through the so-called epipole \\(\\tilde{e}_2\\) which therefore satisfies
\\(\\widetilde{e}^\u22a4_2 \\ \\widetilde{l}_2 = \\widetilde{e}^\u22a4_2 \\widetilde{E}\\tilde{x}_1 = 0\\) for all \\(\\tilde{x}_1.\\)
It follows that:\\(\\widetilde{e}^\u22a4_2 \\widetilde{E} = 0\\)
Thus, \\(\\tilde{e}^\u22a4_2\\)is the leftnull-space (left singular vector with singular value 0) of\\(\\widetilde{E}\\).
Further:\\(\\widetilde{E}\\tilde{e}_1 = 0\\) Thus,\\(\\tilde{e}_1\\) is the rightnull-space (right singular vector with singular value 0)of \\(\\widetilde{E}.\\)
"},{"location":"CV/ML-4360/SFM/lec3/#recover-r-t","title":"Recover \\(R\\ \\&\\ t\\)","text":"Derivation
\\(\\tilde{x}_{2i}^T \\widetilde{E} \\tilde{x}_{1i}=0\\)
\\(\\widetilde{E}=\\begin{pmatrix} e_{11} & e_{12} & e_{13} \\\\ e_{21} & e_{22} & e_{23} \\\\ e_{31} & e_{32} & e_{33} \\end{pmatrix} \\ \\ \\ \\ \\ \\ \\ \\ E = \\begin{pmatrix} e_{11} \\\\ e_{12} \\\\ e_{13} \\\\ e_{21} \\\\ e_{22} \\\\ e_{23} \\\\ e_{31} \\\\ e_{32} \\\\ e_{33} \\end{pmatrix}\\)\\(\\Rightarrow x_1x_2e_{11} + y_1x_2e_{12} + x_2e_{13}+ x_1y_2e_{21} + y_1y_2e_{22} + y_2e_{23}+ x_1e_{31} + y_1e_{32} + e_{33 }= 0\\)
Rearranging the product, so \\(E\\) is on the left hand side:\\(\\tilde{x}_2^T\\tilde{E}\\tilde{x}_1 =\\begin{pmatrix} x_{11}x_{12} & y_{11}x_{12} & x_{12}&x_{11}y_{12}&y_{11}y_{12}&y_{12}&x_{11}&y_{11}&1 \\\\ \u2026\u2026 \\\\ x_{i1}x_{i2} & y_{i1}x_{i2} & x_{i2}&x_{i1}y_{i2}&y_{i1}y_{i2}&y_{i2}&x_{i1}&y_{i1}&1 \\\\\u2026\u2026 \\\\x_{81}x_{82} & y_{81}x_{82} & x_{82}&x_{81}y_{82}&y_{81}y_{82}&y_{82}&x_{81}&y_{81}&1\\end{pmatrix} \\begin{pmatrix} e_{11} \\\\ e_{12} \\\\ e_{13} \\\\ e_{21} \\\\ e_{22} \\\\ e_{23} \\\\ e_{31} \\\\ e_{32} \\\\ e_{33} \\end{pmatrix} =0\\)\\(\\Rightarrow AE = 0\\)
If there are more than 8 point correspondences, the system is usually over-determined and an exact solution is not possible. Instead, we solve a least squares problem for AE
\u6211\u7684\u7406\u89e3\uff1a\u5e76\u4e0d\u662f\u8bf4\\(E\\) \u7684\u8303\u6570\u5c31\u662f1 \u800c\u662f\u5728\u8ba1\u7b97 \\(min(||AE||)\\)\u65f6\uff0c\u7b49\u540c\u4e8e\u8ba1\u7b97 minimize \\(||AE||\\) subject to \\(||x||^2=1\\)\u200b
\u52d8\u8bef x is the last column of V
Credits to Carnegie Mellon University and Kris Kitani, http://www.cs.cmu.edu/~16385/s17/Slides/11.5_SVD.pdf
"},{"location":"CV/ML-4360/SFM/lec3/#properties-of-the-tildee","title":"Properties of the \\(\\tilde{E}\\)","text":"by readjustment:
\\(\\widetilde{E} =\\begin{bmatrix} u_0&u_1&u_2 \\end{bmatrix}\\begin{bmatrix} s&0&0\\\\0&s&0\\\\0&0&0 \\end{bmatrix} \\begin{bmatrix}v_0^T\\\\v_1^T\\\\v_2^T \\end{bmatrix} =U\\sum V^T\\)
\\(s=\\frac{\\lambda_1+\\lambda_2}{2}\\) \\(\\lambda_1\\ge \\lambda_2\\ge\\lambda_3\\)
Proof
\u5177\u4f53\u8bc1\u660e\u89c1 https://blog.csdn.net/weixin_44387324/article/details/119182590
\\(W=\\begin{bmatrix} 0&-1&0\\\\1&0&0\\\\0&0&1 \\\\ \\end{bmatrix} \\\\ t_x=UW\\sum U^T \\\\ R=UW^{-1}V^T \\\\E=U\\sum V^T\\)
"},{"location":"CV/ML-4360/SFM/lec3/#fundamental-matrix-if-k_i-unknown","title":"Fundamental matrix (if \\(K_i\\) unknown):","text":"Let \\(\\tilde{x}^s_i = \\widetilde{P}_ i \\tilde{x}_w\\) denote the projection of a 3D world point \\(\\tilde{x}_w\\) onto the image of the i\u2019th camera \\(\\tilde{x}^s_i\\).
Consider the cross product \\(x \u0304^s_i \u00d7 \\tilde{P}_i\\tilde{x}_w =0\\).
\\(\\begin{bmatrix}x^s_i\\widetilde{p}_{i3}^T - \\widetilde{p}_{i1}^T \\\\ y^s_i\\widetilde{p}_{i3}^T - \\widetilde{p}_{i2}^T \\end{bmatrix}\\widetilde{x}_w=0\\) \\([\\bar{x}_i^s=(x_i^s,y_i^s,1)]\\)
Stacking \\(N \u2265 2\\) observations of a point, we obtain a linear system \\(A\\tilde{x}_w = 0.\\)
As \\(\\tilde{x}_w\\)\u200b is homogeneous this leads to a constrained least squares problem.
The solution to this problem is the right singular vector corresponding to the smallest singular value of A.
This is the Direct Linear Transformation we are already familiar with from \\(Lecture 2\\).
Direcr Linear Transformation https://medium.com/@insight-in-plain-sight/estimating-the-homography-matrix-with-the-direct-linear-transform-dlt-ec6bbb82ee2b
Proof https://blog.csdn.net/u011178262/article/details/86729887
Reprojection Error Minimization:
While DLT often works well, it is not invariant to perspective transformations.The gold standard is to minimize the reprojection error using numerical methods:
1.Take measurements \\(\\hat{W}\\)
2.Compute SVD $\\hat{W} = U\u03a3V^\u22a4 $and keep the top 3 SVs
3.Define\\(\\hat{R} =U\u03a3^{\\frac{1}{2}}\\) and \\(\\hat{X} =\\sum^{\\frac{1}{2}}V^T\\)
4.Compute \\(QQ^\u22a4\\) and from this Q
5.Compute\\(R=\\hat{R}Q\\)and\\(X=Q^{-1}\\hat{X}\\)
Remarks:
"},{"location":"CV/ML-4360/SFX/lec8/#shape-from-shading","title":"Shape-from-Shading","text":""},{"location":"CV/ML-4360/SFX/lec8/#recap-rendering-equation","title":"Recap: Rendering Equation","text":"
Dropping the dependency on \u03bb and p for notational simplicity, and considering a single point light source located in direction s, the rendering equation simplifies as follows:
\\(L_{out}(v)=BRDF(s,v)\u00b7L_{in}\u00b7(-n^{T}\u00b7s)\\)
Assuming a purely diffuse material with albedo (=diffuse reflectance) BRDF(s, v) = \u03c1 \uff0c further simplifies to the following equation (Lout becomes independent of v): \\(L_{out} = \u03c1 \u00b7 L_{in} \u00b7 (\u2212n^\u22a4\u00b7s)\\)
For simplicity, we further eliminate the minus sign by reversing the orientation (definition) of the light ray s and obtain:
\\(L_{out} = \u03c1 \u00b7 L_{in} \u00b7 n^\u22a4s\\)
"},{"location":"CV/ML-4360/SFX/lec8/#shape-from-shading_1","title":"Shape-from-Shading","text":"Gradient Space Representation
\u200b \\((p,q)=(-\\frac{\\partial z}{\\partial x},-\\frac{\\partial z}{\\partial y})\\)
\u200b \\(\\vec{n}=\\frac{(p,q,1)^T}{\\sqrt{p^2+q^2+1}}\\)
\\(R(n)=n^Ts=\\frac{ps_x+qs_y+s_z}{1+\\sqrt{p^2+q^2+1}}=R(p,q)\\)
Reflectance Map
Shape-from-Shading Formulation
Assumption: image irradiance (=intensity) should equal the reflectance map:\\(I(x, y) = R(f(x, y), g(x, y))\\)
SfS thus minimizes:
\\(E_{image}(f,g)=\\iint (I(x,y)-R(f,g))^2dxdy\\)\u200b
However, as we have seen, this problem is ill-posed (unknowns > observations)
Numerical Shape-from-Shading
To constrain this ill-posed problem, SfS exploits two additional constraints:
Goal: Penalize rapid changes in surface gradients \\(f\\) and \\(g\\)
\u200b \\(E_{smooth}(f,g)=\\iint (f_x^2+f_y^2+g_x^2+g_y^2)dxdy\\) with gradients \\(f_x=\\frac{\\partial f}{\\partial x}\\) \\(f_y=\\frac{\\partial f}{\\partial y}\\)\u200b\u200b
Goal: Constrain normals at occluding boundaries where they are known
\u200b
"},{"location":"CV/ML-4360/SFX/lec8/#surface-integration","title":"Surface Integration","text":"Given the surface gradients (from above methods) \\((p,q)=(-\\frac{\\partial z}{\\partial x},-\\frac{\\partial z}{\\partial y})\\) how can we recover the 3D surface / depth map?
Assuming a smooth surface, we can solve the following variational problem
$E(z)=\\iint[(\\frac{\\partial z}{\\partial x}+p)^2+(\\frac{\\partial z}{\\partial y}+q)^2]dxdy $\u200b\u200b
efficiently using the discrete Fast Fourier Transform (Frankot and Chellappa, 1988).
"},{"location":"CV/ML-4360/SFX/lec8/#photometric-stereo","title":"Photometric Stereo","text":"Instead of smoothness constraints, add more observations per pixel
Take K images of the object from same viewpoint (e.g., with a tripod) but with different (known) point light source each
Per-pixel estimation of normal and albedo or material
Also assumes far camera/light
i.e., if all three light sources \\(s_1, s_2, s_3\\) and the origin \\(p\\) lie on a 3D plane, the linear system becomes rank-deficient and thus there exists no unique solution \\(\\tilde{n} = S^{-1}I\\)\u200b.
Refer to PPT
"},{"location":"CV/ML-4360/SFX/lec8/#volumetric-fusion","title":"Volumetric Fusion","text":""},{"location":"CV/ML-4360/SFX/lec8/#representation","title":"Representation","text":"3 Steps:
As the distance to surface is unknown, approximate it with distance along ray
This approximation is good only in the vincinity of the surface (often suffices)
"},{"location":"CV/ML-4360/SFX/lec8/#volumetric-fusion_1","title":"Volumetric Fusion","text":"After conversion, calculate average of the discrete SDF fields
The implicit surface will be an average of the two original ones
"},{"location":"CV/ML-4360/SR/lec4/#stereo-reconstruction","title":"Stereo Reconstruction","text":""},{"location":"CV/ML-4360/SR/lec4/#preliminaries","title":"Preliminaries","text":""},{"location":"CV/ML-4360/SR/lec4/#epipolar-geometry","title":"Epipolar Geometry","text":"
What if both cameras face exactly the same direction?
Let\\(K_1 = K_2 = R = I\\) and \\(t = (t,0,0)^\u22a4\\)
\\(\\bar{x}_2^T\\tilde{E}\\bar{x}_1=\\bar{x_2}^T \\begin{bmatrix}0&0&0\\\\0&0&-t\\\\0&t&0\\end{bmatrix}\\bar{x1}=ty_1-ty_2=0\\)
Thus \\(y_1=y_2\\)
What if the images are not in the required setup?
How can we make epipolar lines horizontal?
Step1:Estimate\\(\\tilde{E}\\) , decompose into \\(t\\) and \\(R\\)
Step2: Find \\(R_{rect}\\)
Choose \\(OO^\u2032=\\vec{T}=(t_x,t_y,t_z)^T\\)
\\(e_1=\\frac{T}{\u2223\u2223T\u2223\u2223}\\)
\\(e_2=\\frac{1}{\\sqrt{T_x^2+T_y^2}}(-T_y,T_x,0)^T= [(0, 0, 1)^\u22a4]\u00d7r_1\\)
\\(e_3=e_2\\) x \\(e_1\\)
\\(\\Rightarrow R_{rect}=\\begin{bmatrix}e_1^T\\\\e_2^T\\\\e_3^T\\end{bmatrix}\\)
Step3 : Adjust \\(\\tilde{x}_i\\)
Warp pixels in the second image as follows: \\(\\tilde{x}_2^{'}=KRR_{rect}K_2^{-1}x \u0304_2\\)
NOTE \uff1aDifferent coordinate systems result in different perception of \\(R_{rect}\\)
Thus the \\(R_{rect}^{'}=RR_{rect}\\)
\\(K\\) is a shared projection matrix that can be chosen arbitrarily (e.g., \\(K = K_1\\))
In practice, the inverse transformation is used for warping (i.e. query the source)
https://martin-thoma.com/zero-mean-normalized-cross-correlation/
"},{"location":"CV/ML-4360/SR/lec4/#sum-of-squared-differences-ssd","title":"Sum of squared differences (SSD)","text":""},{"location":"CV/ML-4360/SR/lec4/#assumption-violations","title":"Assumption Violations","text":"This is called the fronto-parallel assumption which is often invalid
Slanted surfaces deform perspectively when the viewpoint changes
Effect of Window Size
Small windows lead to matching ambiguities and noise in the disparity maps
Larger windows lead to smoother results, but loss of details and border bleeding
Border Bleeding:
Hinge Loss:\\(L = max(0, m +\\ s_-\\ -\\ s_+)\\)
\\(s_- / s_+\\) is the score of the network for the negative/positive example
The loss is zero when the similarity of the positive example is greater than the similarity of the negative example by at least margin m
The network is trained by minimizing a hinge loss.
The loss is computed by considering pairs of examples centered around the same image position where one example belongs to the positive and one to the negative class.
Let \\(s_+\\)be the output of the network for the positive example, \\(s_-\\) be the output of the network for the negative example
Let m, the margin, be a positive real number.
The hinge loss for that pair of examples is defined as \\(L = max(0, m + s_- - s_+)\\) The loss is zero when the similarity of the positive example is greater than the similarity of the negative example by at least the margin m.
Set the margin to 0.2 in our experiments.
https://www.jmlr.org/papers/volume17/15-535/15-535.pdf
"},{"location":"CV/ML-4360/SR/lec4/#spatial-regularization","title":"Spatial Regularization","text":"Add Pairwise terms: Smoothness between adjacent pixels in addition to matching costs;
Potts: \\(\u03c8_{smooth}(d, d^\u2032) = [d\\ne d^\u2032]\\)
Truncated\\(l_1:\u03c8+{smooth}(d,d^\u2032)=min(|d-d^\u2032|,\u03c4)\\)
Paper
https://dash.harvard.edu/bitstream/handle/1/3637107/Mumford_StatRangeImage.pdf?sequence=3&isAllowed=y
"},{"location":"CV/ML-4360/SR/lec4/#end-to-end-learning","title":"End-to-End Learning","text":"End to End learning in the context of AI and ML is a technique where the model learns all the steps between the initial input phase and the final output result. This is a deep learning process where all of the different parts are simultaneously trained instead of sequentially.
"},{"location":"CV/ML-4360/SR/lec4/#dispnet","title":"DISPNET","text":""},{"location":"CV/ML-4360/SR/lec4/#gcnet","title":"GCNET","text":""},{"location":"CV/ML-4360/SR/lec4/#stereo-mixture-density-networks-smd-nets","title":"STEREO MIXTURE DENSITY NETWORKS (SMD-NETS)","text":""},{"location":"CV/ML-4360/Self/lec/","title":"Self-Supervised Learning","text":""},{"location":"CV/ML-4360/Self/lec/#preliminaries","title":"Preliminaries","text":"
Idea of self-supervision:
Specifically, at training time, we have access to two images \\(I^l\\) and \\(I^r\\) , corresponding to the left and right color images from a calibrated stereo pair, captured at the same moment in time.
Instead of trying to directly predict the depth, we attempt to find the dense correspondence field \\(d_r\\) that, when applied to the left image, would enable us to reconstruct the right image. We will refer to the reconstructed image \\(I^l(d_r)\\) as \\(\\tilde{I}^r\\)\u200b. Similarly, we can also estimate the left image given the right one, \\(\\tilde{I}^l =I^r(d_l)\\).
Assuming that the images are rectified , \\(d\\) corresponds to the image disparity - a scalar value per pixel that our model will learn to predict. Given the baseline distance \\(b\\) between the cameras and the camera focal length \\(f\\), we can then trivially recover the depth \\(\\hat{d}\\) from the predicted disparity, \\(\\hat{d}=bf/d\\)\u200b\u200b\u200b.
Simultaneously infer both disparities (left-to-right and right-to-left), using only the left input image, and obtain better depths by enforcing them to be consistent with each other.
A pretext (adj: pretextual) is an excuse to do something or say something that is not accurate. Pretexts may be based on a half-truth or developed in the context of a misleading fabrication. Pretexts have been used to conceal the true purpose or rationale behind actions and words
"},{"location":"CV/ML-4360/Self/lec/#visual-representation-learning-by-context-prediction","title":"Visual Representation Learning by Context Prediction","text":"Care has to be taken to avoid trivial shortcuts (e.g., edge continuity)
A network can predict the absolute image location of randomly sampled patches In this case, the relative location can be inferred easily. Why is this happening?
Solution: Random dropping of color channels or projection towards gray
"},{"location":"CV/ML-4360/Self/lec/#visual-representation-learning-by-solving-jigsaw-puzzles","title":"Visual Representation Learning by Solving Jigsaw Puzzles","text":"Can we find a more general pretext task?
I Given a chosen score function \\(s(\u00b7, \u00b7)\\), we want to learn an encoder f that yields high score for positive pairs \\((x, x+)\\) and low score for negative pairs \\((x, x\u2212)\\):\\(s(f(x), f(x+)) \u226b s(f(x), f(x\u2212))\\)
"},{"location":"DIP/Basic/DIP/#image-format","title":"Image Format","text":"
Digital image can be represented as a 2D array or matrix.
Grayscale:Each pixel in a grayscale image is represented by a byte (8 bits), which covers 256 degrees by [0..255]
Colour image: Each pixel in a color image is represented by 3 bytes (24 bits), which are for R (red), G (Green) and B (Blue), respectively.
\u76ee\u7684\uff1a\u5b58\u50a8\u56fe\u50cf
\u4fe1\u606f\u56fe\u50cf\u7279\u70b9\uff1a\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\uff0c\u77e9\u5f62\u533a\u57df\u4fe1\u606f\u91cf\u5927
\u6709\u4e9b\u6587\u4ef6\u683c\u5f0f\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u6709\u5173\uff1awindows\u3001unix\u3001mac\u7f16\u7801\u65b9\u5f0f\uff1a\u65e0\u538b\u7f29\u3001\u65e0\u635f\u538b\u7f29\u3001\u6709\u635f\u538b\u7f29
"},{"location":"DIP/Basic/DIP/#1bmp","title":"1.BMP\u7684\u6587\u4ef6\u683c\u5f0f\uff1a","text":"\u200b BMP\uff08Bitmap\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u56fe\u50cf\u6587\u4ef6\u683c\u5f0f\uff0c\u662fWindows\u7cfb\u7edf\u7684\u4e00\u79cd\u6807\u51c6\u6587\u4ef6\u683c\u5f0f\u3002BMP \u4f4d\u56fe\u6587\u4ef6\u9ed8\u8ba4\u7684\u6587\u4ef6\u6269\u5c55\u540d\u662f bmp \u6216\u8005 dib\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cBMP \u56fe\u50cf\u91c7\u7528\u975e\u538b\u7f29\u65b9\u5f0f\uff1b\u7136\u800c\uff0c\u5b83\u4e5f\u652f\u6301\u56fe\u50cf\u538b\u7f29\uff0c\u4f8b\u5982 RLE \u683c\u5f0f\u3002\u4e0b\u9762\u4ecb\u7ecdBMP\u6587\u4ef6\u7684\u5177\u4f53\u6587\u4ef6\u7ed3\u6784\u3002
BMP \u56fe\u50cf\u6bcf\u4e00\u884c\u626b\u63cf\u7531\u8868\u793a\u56fe\u50cf\u50cf\u7d20\u7684\u8fde\u7eed\u7684\u5b57\u8282\u7ec4\u6210\uff0c\u6bcf\u4e00\u884c\u4ee5\u56db\u5b57\u8282\u5bf9\u9f50\uff08\u4ee50\u8865\u9f50\uff09\u3002\u56fe\u50cf\u7684\u626b\u63cf\u884c\u662f\u7531\u5e95\u5411\u4e0a\u5b58\u50a8\u7684\uff0c\u8fd9\u5c31\u662f\u8bf4\uff0c\u9635\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u4f4d\u56fe\u5de6\u4e0b\u89d2\u7684\u50cf\u7d20\uff0c\u800c\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u4f4d\u56fe\u53f3\u4e0a\u89d2\u7684\u50cf\u7d20\u3002
// \u5b9a\u4e49BMP\u6587\u4ef6\u5934\u7684\u5927\u5c0f\u4e3a54\u4e2a\u5b57\u8282\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u56fa\u5b9a\u4e0d\u53d8\n#define HEADERSIZE 54\n\n// \u5b9a\u4e49\u51e0\u79cd\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u522b\u540d\uff0c\u4ee5\u4fbf\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u66f4\u5177\u53ef\u8bfb\u6027\u7684\u540d\u79f0\ntypedef unsigned char byte;\ntypedef unsigned short word;\ntypedef unsigned int dword;\n\n// \u4f7f\u7528#pragma pack(1)\u6307\u4ee4\u8bbe\u7f6e\u7ed3\u6784\u4f53\u7684\u5b57\u8282\u5bf9\u9f50\u65b9\u5f0f\u4e3a1\u5b57\u8282\n#pragma pack(1)\n\n// \u5b9a\u4e49BMP\u6587\u4ef6\u5934\u7684\u7ed3\u6784\u4f53\ntypedef struct {\n word bfType; // BMP\u6587\u4ef6\u7684\u7c7b\u578b\u6807\u8bc6\uff0c\u901a\u5e38\u4e3a\"BM\"\n dword bfSize; // BMP\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\n word reserved1; // \u4fdd\u7559\u5b57\u6bb5\uff0c\u8bbe\u7f6e\u4e3a0\n word reserved2; // \u4fdd\u7559\u5b57\u6bb5\uff0c\u8bbe\u7f6e\u4e3a0\n dword bfOffset; // \u4f4d\u56fe\u6570\u636e\u7684\u504f\u79fb\u91cf\uff0c\u5373\u6587\u4ef6\u5934\u7684\u5927\u5c0f\n} BMPHeader;\n\n// \u5b9a\u4e49BMP\u4fe1\u606f\u5934\u7684\u7ed3\u6784\u4f53\ntypedef struct {\n dword size; // \u4fe1\u606f\u5934\u7684\u5927\u5c0f\uff0c\u901a\u5e38\u4e3a40\u5b57\u8282\n int width; // \u56fe\u50cf\u7684\u5bbd\u5ea6\uff0c\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\n int height; // \u56fe\u50cf\u7684\u9ad8\u5ea6\uff0c\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\n word planes; // \u989c\u8272\u5e73\u9762\u6570\uff0c\u901a\u5e38\u4e3a1\n word bitCount; // \u6bcf\u4e2a\u50cf\u7d20\u7684\u4f4d\u6570\n dword compression; // \u538b\u7f29\u7c7b\u578b\n dword imageSize; // \u4f4d\u56fe\u6570\u636e\u7684\u5927\u5c0f\uff0c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\n int xPixelsPerMeter; // \u6c34\u5e73\u5206\u8fa8\u7387\uff0c\u6bcf\u7c73\u7684\u50cf\u7d20\u6570\n int yPixelsPerMeter; // \u5782\u76f4\u5206\u8fa8\u7387\uff0c\u6bcf\u7c73\u7684\u50cf\u7d20\u6570\n dword colorsUsed; // \u4f7f\u7528\u7684\u989c\u8272\u6570\n dword colorsImportant; // \u91cd\u8981\u989c\u8272\u6570\n} BMPInfoHeader;\n\n// \u5b9a\u4e49\u8c03\u8272\u677f\u4e2d\u7684\u4e00\u4e2a\u989c\u8272\u7684\u7ed3\u6784\u4f53\ntypedef struct {\n byte blue; // \u84dd\u8272\u5206\u91cf\n byte green; // \u7eff\u8272\u5206\u91cf\n byte red; // \u7ea2\u8272\u5206\u91cf\n byte reserved; // \u4fdd\u7559\u5b57\u6bb5\uff0c\u901a\u5e38\u8bbe\u7f6e\u4e3a0\n} pallete;\n\n// \u5b9a\u4e49\u5305\u542bBMP\u6587\u4ef6\u5934\u3001BMP\u4fe1\u606f\u5934\u3001\u8c03\u8272\u677f\u548c\u4f4d\u56fe\u6570\u636e\u7684\u5b8c\u6574BMP\u56fe\u50cf\u7684\u7ed3\u6784\u4f53\ntypedef struct {\n BMPHeader bmph; // BMP\u6587\u4ef6\u5934\n BMPInfoHeader bmpih; // BMP\u4fe1\u606f\u5934\n pallete colours[256]; // \u8c03\u8272\u677f\uff0c\u901a\u5e38\u7528\u4e8e8\u4f4d\u989c\u8272\u7d22\u5f15\u56fe\u50cf\n byte *bitmap; // \u4f4d\u56fe\u6570\u636e\n} BMP;\n
"},{"location":"DIP/Basic/DIP/#1bmp_1","title":"(1)BMP\u6587\u4ef6\u6574\u4f53\u7ed3\u6784\uff1a","text":""},{"location":"DIP/Basic/DIP/#_1","title":"Basic concept and programming introduction","text":"BMP \u6587\u4ef6 \u5927\u4f53\u4e0a\u5206\u4e3a\u56db\u4e2a\u90e8\u5206:\u56fe\u50cf\u6587\u4ef6\u5934\uff08Image file header\uff09\u3001\u56fe\u50cf\u4fe1\u606f\u5934\uff08Image information header\uff09\u3001\u8c03\u8272\u677f\uff08Palette\uff09\u3001\u56fe\u50cf\u6570\u636e\u5b57\u8282\u9635\u5217\uff08Image data\uff09
\u6709\u4e00\u4e2a\u957f\u5bbd\u5404\u4e3a200\u4e2a\u8c61\u7d20\uff0c\u989c\u8272\u6570\u4e3a16\u8272\u7684\u5f69\u8272\u56fe\uff0c\u6bcf\u4e00\u4e2a\u8c61\u7d20\u90fd\u7528R\u3001G\u3001B\u4e09\u4e2a\u5206\u91cf\u8868\u793a
\u56e0\u4e3a\u6bcf\u4e2a\u5206\u91cf\u6709256\u4e2a\u7ea7\u522b\uff0c\u8981\u75288\u4f4d(bit)\uff0c\u5373\u4e00\u4e2a\u5b57\u8282(byte)\u6765\u8868\u793a\uff0c\u6240\u4ee5\u6bcf\u4e2a\u8c61\u7d20\u9700\u8981\u75283\u4e2a\u5b57\u8282\u3002\u6574\u4e2a\u56fe\u8c61\u8981\u7528200\u00d7200\u00d73\uff0c\u7ea6120k\u5b57\u8282\uff0c\u53ef\u4e0d\u662f\u4e00\u4e2a\u5c0f\u6570\u76ee\u5440\uff01\u5982\u679c\u6211\u4eec\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\uff0c\u5c31\u80fd\u7701\u7684\u591a\u3002
\u56e0\u4e3a\u662f\u4e00\u4e2a16\u8272\u56fe\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u5e45\u56fe\u4e2d\u6700\u591a\u53ea\u670916\u79cd\u989c\u8272\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2a\u8868\uff1a\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u8bb0\u5f55\u4e00\u79cd\u989c\u8272\u7684R\u3001G\u3001B\u503c\u3002\u8fd9\u6837\u5f53\u6211\u4eec\u8868\u793a\u4e00\u4e2a\u8c61\u7d20\u7684\u989c\u8272\u65f6\uff0c\u53ea\u9700\u8981\u6307\u51fa\u8be5\u989c\u8272\u662f\u5728\u7b2c\u51e0\u884c\uff0c\u5373\u8be5\u989c\u8272\u5728\u8868\u4e2d\u7684\u7d22\u5f15\u503c\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u8868\u7684\u7b2c0\u884c\u4e3a255\uff0c0\uff0c0(\u7ea2\u8272)\uff0c\u90a3\u4e48\u5f53\u67d0\u4e2a\u8c61\u7d20\u4e3a\u7ea2\u8272\u65f6\uff0c\u53ea\u9700\u8981\u6807\u660e0\u5373\u53ef
\u8fd9\u5f20R\u3001G\u3001B\u7684\u8868\uff0c\u5c31\u662f\u6211\u4eec\u5e38\u8bf4\u7684\u8c03\u8272\u677f(Palette)\uff0c\u53e6\u4e00\u79cd\u53eb\u6cd5\u662f\u989c\u8272\u67e5\u627e\u8868LUT(Look Up Table)\uff0c\u4f3c\u4e4e\u66f4\u786e\u5207\u4e00\u4e9b\u3002Windows\u4f4d\u56fe\u4e2d\u4fbf\u7528\u5230\u4e86\u8c03\u8272\u677f\u6280\u672f\u3002\u5176\u5b9e\u4e0d\u5149\u662fWindows\u4f4d\u56fe\uff0c\u8bb8\u591a\u56fe\u8c61\u6587\u4ef6\u683c\u5f0f\u5982pcx\u3001tif\u3001gif\u7b49\u90fd\u7528\u5230\u4e86\u3002\u6240\u4ee5\u5f88\u597d\u5730\u638c\u63e1\u8c03\u8272\u677f\u7684\u6982\u5ff5\u662f\u5341\u5206\u6709\u7528\u7684\u3002
"},{"location":"DIP/Basic/DIP/#5image-data","title":"(5)\u56fe\u50cf\u6570\u636e\u5b57\u8282\u9635\u5217\uff08Image data\uff09","text":"\u56fe\u50cf\u6570\u636e\u5b57\u8282\u9635\u5217\u5b58\u50a8\u4e86\u8c03\u8272\u677f\u7684\u7d22\u5f15\u53f7\uff0c\u6216\u8005\u53d6\u51b3\u4e8e\u989c\u8272\u6df1\u5ea6\u7684RGB\u503c
\u5176\u5927\u5c0f\u53d6\u51b3\u4e8e\u56fe\u50cf\u5927\u5c0f\u548c\u989c\u8272\u6df1\u5ea6\u3002
(1)\u6bcf\u4e00\u884c\u7684\u5b57\u8282\u6570\u5fc5\u987b\u662f4\u7684\u6574\u500d\u6570\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u5219\u9700\u8981\u8865\u9f50(append zero)
(2)\u4e00\u822c\u6765\u8bf4\uff0c.bMP\u6587\u4ef6\u7684\u6570\u636e\u4ece\u4e0b\u5230\u4e0a\uff0c\u4ece\u5de6\u5230\u53f3\u7684
\u4e5f\u5c31\u662f\u8bf4\uff0c\u4ece\u6587\u4ef6\u4e2d\u6700\u5148\u8bfb\u5230\u7684\u662f\u56fe\u8c61\u6700\u4e0b\u9762\u4e00\u884c\u7684\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u8c61\u7d20\uff0c\u7136\u540e\u662f\u5de6\u8fb9\u7b2c\u4e8c\u4e2a\u8c61\u7d20\u2026\u2026\u63a5\u4e0b\u6765\u662f\u5012\u6570\u7b2c\u4e8c\u884c\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u8c61\u7d20\uff0c\u5de6\u8fb9\u7b2c\u4e8c\u4e2a\u8c61\u7d20\u2026\u2026\u4f9d\u6b21\u7c7b\u63a8 \uff0c\u6700\u540e\u5f97\u5230\u7684\u662f\u6700\u4e0a\u9762\u4e00\u884c\u7684\u6700\u53f3\u4e00\u4e2a\u8c61\u7d20\u3002
You do not need to turn around the rows manually. The API functions which also display the bitmap will do that for you automatically.
\u5229\u7528 C \u8bed\u8a00\u4e2d\u7684 fopen fread fwrite \u8fdb\u884c\u8bfb\u5165\u3001\u5199\u51fa\u5373\u53ef
\u5728\u8bfb\u5165\u4e0e\u5199\u51fa\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f BMP\u6587\u4ef6\u4fe1\u606f\u7684\u8c03\u6574\uff0c\u4f8b\u5982\u753124\u4f4d\u5f69\u8272BMP\u8f6c\u4e3a\u7070\u8272\u56fe\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u8c03\u8272\u677f\u3001\u4f4d\u6df1\u7b49\u7684\u8c03\u6574\u3002
FILE *inputFile = fopen(\"input.bmp\", \"rb\");\n // \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u6253\u5f00\nif (!inputFile) {\n printf(\"Unable to open input BMP file.\\n\"); // \u8f93\u51fa\u9519\u8bef\u4fe1\u606f\n return 0; // \u9000\u51fa\u7a0b\u5e8f\n}\nprintf(\"successfully loaded!\"); // \u6210\u529f\u52a0\u8f7d\u7684\u63d0\u793a\u4fe1\u606f\nBMP bmp; // \u521b\u5efa\u4e00\u4e2aBMP\u7ed3\u6784\u4f53\u53d8\u91cf\u7528\u4e8e\u5b58\u50a8BMP\u6587\u4ef6\u7684\u4fe1\u606f\u548c\u6570\u636e\n// \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6BMP\u6587\u4ef6\u5934\u548c\u4fe1\u606f\u5934\nfread(&(bmp), HEADERSIZE, 1, inputFile);\nint width = bmp.bmpih.width; // \u83b7\u53d6\u56fe\u50cf\u7684\u5bbd\u5ea6\nint height = bmp.bmpih.height; // \u83b7\u53d6\u56fe\u50cf\u7684\u9ad8\u5ea6\n// \u5982\u679c\u56fe\u50cf\u5927\u5c0f\u4e3a0\uff0c\u5219\u8ba1\u7b97\u56fe\u50cf\u6570\u636e\u5927\u5c0f\nif (bmp.bmpih.imageSize == 0) {bmp.bmpih.imageSize = bmp.bmph.bfSize - bmp.bmph.bfOffset;}\n// \u4e3a\u4f4d\u56fe\u6570\u636e\u5206\u914d\u5185\u5b58\nbmp.bitmap = (byte*)malloc(sizeof(byte) * (bmp.bmph.bfSize - HEADERSIZE));\n// \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u4f4d\u56fe\u6570\u636e\nfread(bmp.bitmap, bmp.bmph.bfSize - HEADERSIZE, 1, inputFile);\nfclose(inputFile); // \u5173\u95ed\u6587\u4ef6\n
"},{"location":"DIP/Basic/DIP/#visual-intelligence-and-pattern-analysis","title":"Visual Intelligence and Pattern Analysis","text":""},{"location":"DIP/Basic/DIP/#history","title":"History","text":""},{"location":"DIP/Basic/DIP/#_2","title":"\u9488\u5b54\u6210\u50cf","text":"\u6295\u5f71\u4fbf\u6210\u4e3a\u5012\u7acb\u7684\uff1b\u7269\u8ddd\u8d8a\u8fdc,\u50cf\u8d8a\u5c0f\uff1b\u7269\u8ddd\u8d8a\u8fd1,\u50cf\u8d8a\u5927
\u5982\u524d\u9762\u63d0\u5230\u7684\u60c5\u51b5\uff0c\u5149\u5708\u5b54\u5f84\u5927\u7684\u65f6\u5019\uff0c\u4f1a\u5bfc\u81f4\u6a21\u7cca\uff1b\u90a3\u662f\u4e0d\u662f\u53ef\u4ee5\u5c3d\u91cf\u7f29\u5c0f\u5149\u5708\u7684\u5b54\u5f84\u5462\uff1f
NO.\u5982\u679c\u5b54\u5f84\u592a\u5c0f\uff0c\u53ef\u901a\u8fc7\u7684\u5149\u7ebf\u5c31\u5f88\u5c11\uff0c\u5bfc\u81f4\u5149\u5f3a\u592a\u5f31\uff0c\u540c\u65f6\u5f53\u5b54\u5f84\u5c0f\u5230\u4e00\u5b9a\u7a0b\u5ea6\u65f6\uff0c\u4f1a\u4ea7\u751f\u884d\u5c04\u73b0\u8c61\u3002
"},{"location":"DIP/Basic/DIP/#lenses","title":"Lenses","text":"\u901a\u8fc7\u4f7f\u7528\u900f\u955c\u53ef\u4ee5\u4f1a\u805a\u66f4\u591a\u7684\u5149\u7ebf\u5230\u4e00\u4e2a\u6210\u50cf\u70b9\uff0c\u53ea\u662f\u53ea\u6709\u5728\u7279\u6b8a\u7684\u4f4d\u7f6e\u4e0a\u624d\u80fd\u8fbe\u5230\u8fd9\u4e2a\u76ee\u6807\uff0c\u5373\u5927\u5bb6\u4ee5\u524d\u5c31\u5b66\u4e60\u8fc7\u7684\u900f\u955c\u805a\u7126
\u4f4d\u4e8e\u5408\u9002\u8ddd\u79bb\u7684\u7269\u4f53\u70b9\u53ef\u4ee5\u6210\u6e05\u6670\u50cf\uff0c\u5176\u4ed6\u7684\u70b9\u5219\u4f1a\u4ea7\u751f\u5f25\u6563\u5706 \u901a\u8fc7\u6539\u53d8\u955c\u5934\u7684\u5f62\u72b6\u53ef\u4ee5\u6539\u53d8\u805a\u7126\u65f6\u7684\u7269\u4f53\u70b9\u6240\u9700\u8981\u7684\u8ddd\u79bb
\u666f\u6df1\u968f\u955c\u5934\u7684\u7126\u8ddd\u3001\u5149\u5708\u503c\u3001\u62cd\u6444\u8ddd\u79bb\u800c\u53d8\u5316
(1)\u955c\u5934\u5149\u5708\uff1a\u5149\u5708\u8d8a\u5927\uff0c\u666f\u6df1\u8d8a\u5c0f\uff1b\u5149\u5708\u8d8a\u5c0f\uff0c\u666f\u6df1\u8d8a\u5927
(2)\u955c\u5934\u7126\u8ddd\uff1a\u955c\u5934\u7126\u8ddd\u8d8a\u957f\uff0c\u666f\u6df1\u8d8a\u5c0f\uff1b\u7126\u8ddd\u8d8a\u77ed\uff0c\u666f\u6df1\u8d8a\u5927
(3)\u62cd\u6444\u8ddd\u79bb\uff1a\u8ddd\u79bb\u8d8a\u8fdc\uff0c\u666f\u6df1\u8d8a\u5927\uff1b\u8ddd\u79bb\u8d8a\u8fd1\uff0c\u666f\u6df1\u8d8a\u5c0f
For a given subject framing and camera position, the DOF is controlled by the lens aperture diameter, which is usually specified as the f-number, the ratio of lens focal length to aperture diameter.
Reducing the aperture diameter (increasing the f-number) increases the DOF; however, it also reduces the amount of light transmitted, and increases diffraction, placing a practical limit on the extent to which DOF can be increased by reducing the aperture diameter.
"},{"location":"DIP/Basic/DIP/#digital-camera","title":"Digital camera","text":"\u5f53CCD\u66dd\u5149\u540e\uff0c\u5149\u7535\u4e8c\u6781\u7ba1\u53d7\u5230\u5149\u7ebf\u7684\u6fc0\u53d1\u800c\u91ca\u653e\u51fa\u7535\u8377\uff0c\u751f\u6210\u611f\u5149\u5143\u4ef6\u7684\u7535\u4fe1\u53f7
CCD\u63a7\u5236\u82af\u7247\u5229\u7528\u611f\u5149\u5143\u4ef6\u4e2d\u7684\u63a7\u5236\u4fe1\u53f7\u7ebf\u8def\u5bf9\u53d1\u5149\u4e8c\u6781\u7ba1\u4ea7\u751f\u7684\u7535\u6d41\u8fdb\u884c\u63a7\u5236\uff0c\u7531\u7535\u6d41\u4f20\u8f93\u7535\u8def\u8f93\u51fa\uff0cCCD\u4f1a\u5c06\u4e00\u6b21\u6210\u50cf\u4ea7\u751f\u7684\u7535\u4fe1\u53f7\u6536\u96c6\u8d77\u6765\uff0c\u7edf\u4e00\u8f93\u51fa\u5230\u653e\u5927\u5668\u3002
Colorful view is generated by the interactions between light and objects, e.g., reflection, refraction, scattering, transmission, absorption, and diffraction.
\u8272\u5f69\u573a\u666f\u662f\u7531\u5149\u4e0e\u7269\u4f53\u4e4b\u95f4\u7684\u4ea4\u4e92\u8fc7\u7a0b\u4ea7\u751f\u7684\u3002\u5982\uff1a\u53cd\u5c04\u3001\u6298\u5c04\u3001\u6563\u5c04\u3001\u4f20\u64ad\u3001\u5438\u6536\u4ee5\u53ca\u884d\u5c04\u7b49\u7b49
\u8272\u5f69\u53ef\u4ee5\u5206\u4e3a\u5f69\u8272\uff08chromatic color\uff09\u548c\u6d88\u8272\uff08achromatic color\uff09\u4e24\u5927\u7c7b
\u8272\u89c9(colour vision)\u662f\u6307\u4e0d\u540c\u6ce2\u957f\u7684\u5149\u7ebf\u4f5c\u7528\u4e8e\u89c6\u7f51\u819c\u800c\u5728\u5927\u8111\u4e2d\u5f15\u8d77\u7684\u611f\u89c9
\u4eba\u773c\u53ef\u89c1\u5149\u7ebf\u7684\u6ce2\u957f\u662f390nm\uff5e780nm\uff0c\u4e00\u822c\u53ef\u8fa8\u51fa\u5305\u62ec\u7d2b\u3001\u84dd\u3001\u9752\u3001\u7eff\u3001\u9ec4\u3001\u6a59\u3001\u7ea27\u79cd\u4e3b\u8981\u989c\u8272\u5728\u5185\u7684120\uff5e180\u79cd\u4e0d\u540c\u7684\u989c\u8272
\u89c6\u7f51\u819c\u662f\u4eba\u773c\u4e2d\u6700\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u5c31\u50cf\u76f8\u673a\u91cc\u7684\u611f\u5149\u5e95\u7247\uff0c\u4e13\u95e8\u8d1f\u8d23\u611f\u5149\u6210\u50cf\u3002\u89c6\u7f51\u819c\u4e0a\u5206\u5e03\u7740\u4e24\u79cd\u89c6\u89c9\u7ec6\u80de\uff0c\u4e00\u79cd\u4e3a\u6746\u72b6\u4f53\uff0c\u53e6\u4e00\u79cd\u4e3a\u9525\u72b6\u4f53
\u6746\u72b6\u4f53\u7ec6\u80de\u6bd4\u8f83\u591a\uff0c\u5927\u7ea6\u6709\u4e0a\u4ebf\u4e2a\uff0c\u5b83\u5bf9\u5149\u6781\u4e3a\u7075\u654f\uff0c\u4f46\u6ca1\u6709\u533a\u5206\u8272\u5f69\u7684\u80fd\u529b
\u9525\u72b6\u4f53\u7ec6\u80de\u5219\u53ea\u6709\u516d\u3001\u4e03\u767e\u4e07\u4e2a\uff0c\u5b83\u8981\u5728\u8f83\u5f3a\u7684\u7167\u5ea6\u4e0b\u624d\u80fd\u6fc0\u53d1\uff0c\u5b83\u7684\u5b58\u5728\u4f7f\u6211\u4eec\u80fd\u591f\u8fa8\u522b\u5404\u79cd\u4e0d\u540c\u7684\u989c\u8272
\u89c6\u7f51\u819c\u4e0a\u5b58\u5728\u4e09\u79cd\u5206\u522b\u5bf9\u7ea2\u7eff\u84dd\u5149\u6ce2\u957f\u7279\u522b\u654f\u611f\u7684\u89c6\u9525\u7ec6\u80de\u6216\u76f8\u5e94\u7684\u611f\u5e94\u8272\u7d20\uff0c\u4e0d\u540c\u6ce2\u957f\u7684\u5149\u8fdb\u5165\u773c\u775b\u540e\uff0c\u4e0e\u4e4b\u76f8\u7b26\u6216\u76f8\u8fd1\u7684\u89c6\u9525\u7ec6\u80de\u53d1\u751f\u4e0d\u540c\u7a0b\u5ea6\u7684\u5174\u594b\uff0c\u4e8e\u662f\u5728\u5927\u8111\u4ea7\u751f\u76f8\u5e94\u7684\u8272\u89c9\uff1b\u82e5\u4e09\u79cd\u89c6\u9525\u7ec6\u80de\u53d7\u5230\u540c\u7b49\u7a0b\u5ea6\u7684\u523a\u6fc0\uff0c\u90a3\u4e48\u4ea7\u751f\u6d88\u8272
"},{"location":"DIP/Basic/DIP/#perception-priority-and-sensitivity","title":"Perception priority and sensitivity","text":"Priority: Under the same setting, human notices first the hue (H) (\u8272\u8c03)changes, then the saturation (S) (\u9971\u548c\u5ea6), then the lightness value (V) (\u4eae\u5ea6).
Sensitivity: Human eyes are the most sensitive to the lightness changes and have the best resolution, which is responsible for the eyes\u2019 HDR capacity
\u4eba\u773c\u5bf9\u4e8e\u4eae\u5ea6\u7684\u53d8\u5316\u6700\u4e3a\u654f\u611f\uff0c\u5206\u8fa8\u80fd\u529b\u6700\u5f3a\u3002\u6070\u597d\u4e0e\u4eba\u773c\u7684\u9ad8\u52a8\u6001\u80fd\u529b\u76f8\u5339\u914d\u3002
"},{"location":"DIP/Basic/DIP/#definition-of-color-space","title":"Definition of color space","text":"RGB color model is a unit cube in a Cartesian coordinates system.
The magnitudes of each primary color are equivalent on the main diagonal line, which lead to the white color from darkness to brightness, i.e., grayscale. (0,0,0)-dark, (1,1,1)-bright.
The other 6 corners are respectively red, yellow, cyan, blue and magenta.(\u7ea2\u3001\u9ec4\u3001\u7eff\u3001\u9752\u3001\u84dd\u548c\u54c1\u7ea2)
RGB is a subset of CIE primary color space.
RGB is usually used in Color cathode ray tube and Color raster graphics display (computer , TV).
RGB\u989c\u8272\u6a21\u578b\u901a\u5e38\u7528\u4e8e\u5f69\u8272\u9634\u6781\u5c04\u7ebf\u7ba1\u548c\u5f69\u8272\u5149\u6805\u56fe\u5f62\u663e\u793a\u5668\uff08\u8ba1\u7b97\u673a\u548c\u7535\u89c6\u673a\u91c7\u7528\uff09
"},{"location":"DIP/Basic/DIP/#cmy-color-model","title":"CMY color model","text":"\u5f69\u8272\u5370\u5237\u6216\u8005\u5f69\u8272\u6253\u5370\u7684\u7eb8\u5f20\u4e0d\u80fd\u53d1\u5c04\u5149\u7ebf\uff0c\u56e0\u800c\u5370\u5237\u673a\u6216\u6253\u5370\u673a\u5c31\u53ea\u80fd\u4f7f\u7528\u4e00\u4e9b\u80fd\u591f\u5438\u6536\u7279\u5b9a\u5149\u6ce2\u800c\u53cd\u5c04\u5176\u4ed6\u5149\u6ce2\u7684\u6cb9\u58a8\u6216\u8005\u989c\u8272
\u6cb9\u58a8\u6216\u989c\u6599\u76843\u79cd\u57fa\u8272\u662f\u4ee5\u7ea2\u3001\u7eff\u3001\u84dd\u4e09\u8272\u7684\u8865\u8272 : \u9752\uff08Cyan\uff09\u3001\u54c1\u7ea2\uff08Magenta\uff09\u3001\u9ec4\uff08Yellow\uff09\u4e3a\u57fa\u8272
\u7528CMY\u6a21\u578b\u4ea7\u751f\u7684\u989c\u8272\u79f0\u4e3a\u76f8\u51cf\u8272\uff0c\u662f\u56e0\u4e3a\u5b83\u51cf\u6389\u4e86\u4e3a\u89c6\u89c9\u7cfb\u7edf\u8bc6\u522b\u989c\u8272\u6240\u9700\u8981\u7684\u53cd\u5c04\u5149
HIS/HSV color space is defined based on human visual system.
\u8272\u8c03\uff08Hue)\u3001\u8272\u9971\u548c\u5ea6\uff08Saturation\uff09\u548c\u4eae\u5ea6\uff08Intensity\uff0c\u6216\u8005Value\uff09
Advantages:
Device independent:
XYZ concept is based on the three-primary color theory, which describes human eyes have RGB receptors and all other color are mixture of RBG.
Computed by using functions\\(\\overline{x(\\lambda)},\\overline{y(\\lambda)},\\overline{z(\\lambda)}\\)
\\(Y_{xy}\\)color space given in 1931: Y->luminance, x,y are color coordinates computed based XYZ.
\u5728\u73b0\u4ee3\u5f69\u8272\u7535\u89c6\u7cfb\u7edf\u4e2d\uff0c\u901a\u5e38\u91c7\u7528\u4e09\u7ba1\u5f69\u8272\u6444\u50cf\u673a\u6216\u5f69\u8272CCD\uff08\u7535\u8377\u8026\u5408\u5668\u4ef6\uff09\u6444\u50cf\u673a\uff0c\u5b83\u628a\u6444\u5f97\u7684\u5f69\u8272\u56fe\u50cf\u4fe1\u53f7\uff0c\u7ecf\u5206\u8272\uff0c\u5206\u522b\u653e\u5927\u6821\u6b63\u5f97\u5230RGB\uff0c\u518d\u7ecf\u8fc7\u77e9\u9635\u53d8\u6362\u7535\u8def\u5f97\u5230\u4eae\u5ea6\u4fe1\u53f7Y\u548c\u4e24\u4e2a\u8272\u5dee\u4fe1\u53f7R\uff0dY\u3001B\uff0dY\uff0c\u6700\u540e\u53d1\u9001\u7aef\u5c06\u4eae\u5ea6\u548c\u8272\u5dee\u4e09\u4e2a\u4fe1\u53f7\u5206\u522b\u8fdb\u884c\u7f16\u7801\uff0c\u7528\u540c\u4e00\u4fe1\u9053\u53d1\u9001\u51fa\u53bb,\u8fd9\u5c31\u662f\u6211\u4eec\u5e38\u7528\u7684YUV\u989c\u8272\u7a7a\u95f4
\u91c7\u7528YUV\u989c\u8272\u7a7a\u95f4\u7684\u91cd\u8981\u6027\u662f\u5b83\u7684\u4eae\u5ea6\u4fe1\u53f7Y\u548c\u8272\u5ea6\u4fe1\u53f7U\u3001V\u662f\u5206\u79bb\u7684
\\(RGB->CMY\\\\ C = 255 \u2013 R\\\\ M = 255 \u2013 G\\\\ Y = 255 \u2013 B\\\\\\)
"},{"location":"DIP/Basic/DIP/#rgb-hsv","title":"RGB<->HSV","text":"Search in table:
\\(RGB<->XYZ\\\\HSV<->XYZ\\)
\\(\\begin{gathered}\\begin{bmatrix} X \\\\ Y \\\\ Z\\end{bmatrix}=\\begin{bmatrix} 0.608 & 0.714 & 0.200 \\\\0.299& 0.587&0.133 \\\\0.000 &0.066& 1.112\\end{bmatrix}\\begin{bmatrix} R \\\\ G\\\\ B\\end{bmatrix}\\quad\\end{gathered}\\)
"},{"location":"DIP/Basic/DIP/#cie-xyz-cie-lab","title":"CIE XYZ<->CIE Lab*","text":""},{"location":"DIP/Basic/DIP/#rgb-yuv","title":"RGB \u548c YUV \u7684\u76f8\u4e92\u8f6c\u5316","text":"RGB\uff08\u7ea2\u7eff\u84dd\uff09\u548cYUV\u662f\u4e24\u79cd\u989c\u8272\u8868\u793a\u65b9\u5f0f\uff0c\u5e38\u7528\u4e8e\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u4e2d\u3002RGB\u662f\u4e00\u79cd\u76f4\u63a5\u8868\u793a\u989c\u8272\u7684\u65b9\u5f0f\uff0c\u5176\u4e2d\u7ea2\u8272\uff08R\uff09\u3001\u7eff\u8272\uff08G\uff09\u548c\u84dd\u8272\uff08B\uff09\u5206\u91cf\u7684\u503c\u51b3\u5b9a\u4e86\u989c\u8272\u7684\u5177\u4f53\u5916\u89c2
0.0 ~ 1.0
\u6216\u4ee5\u6574\u6570\u8868\u793a\uff1a\u53d6\u503c\u8303\u56f4\u4e3a 0 ~ 255
\u6216\u8005 00 ~ FF
\u3002RGB \u989c\u8272\u6a21\u578b \u901a\u5e38\u7528\u4e8e\u5f69\u8272\u9634\u6781\u5c04\u7ebf\u7ba1\u548c\u5f69\u8272\u5149\u6805\u56fe\u5f62\u663e\u793a\u5668(\u8ba1\u7b97\u673a\u548c\u7535\u89c6\u673a\u91c7\u7528)\u3002
YUV\u662f\u4e00\u79cd\u989c\u8272\u7a7a\u95f4\uff0c\u57fa\u4e8e YUV
\u7684\u989c\u8272\u7f16\u7801\u662f\u6d41\u5a92\u4f53\u7684\u5e38\u7528\u7f16\u7801\u65b9\u5f0f\uff0c\u8fd9\u79cd\u8868\u8fbe\u65b9\u5f0f\u8d77\u521d\u662f\u4e3a\u4e86\u5f69\u8272\u7535\u89c6\u4e0e\u9ed1\u767d\u7535\u89c6\u4e4b\u95f4\u7684\u4fe1\u53f7\u517c\u5bb9\uff1b\u5176\u4e2d\uff1aY\uff1a\u8868\u793a\u660e\u4eae\u5ea6\uff08Luminance \u6216 Luma\uff09\uff0c\u4e5f\u79f0\u7070\u5ea6\u56fe\u3002U\u3001V\uff1a\u8868\u793a\u8272\u5ea6\uff08Chrominance \u6216 Chroma\uff09\uff0c\u4f5c\u7528\u662f\u63cf\u8ff0\u5f71\u50cf\u7684\u8272\u5f69\u53ca\u9971\u548c\u5ea6\uff0c\u7528\u4e8e\u6307\u5b9a\u50cf\u7d20\u7684\u989c\u8272\u3002
\\(\\begin{gathered}\\begin{bmatrix} Y \\\\ U \\\\ V\\end{bmatrix}=\\begin{bmatrix} 0.299 & 0.587 & 0.114 \\\\ \u22120.147& \u22120.289 &0.435 \\\\0.615 &\u22120.515& \u22120.100\\end{bmatrix}\\begin{bmatrix} R \\\\ G\\\\ B\\end{bmatrix}\\quad\\end{gathered}\\)
\\(\\begin{gathered}\\quad\\quad\\begin{bmatrix} R \\\\ G \\\\ B\\end{bmatrix}=\\begin{bmatrix} 1.0000 &0.0000& 1.3707 \\\\1.0000& \u22120.3376 &-0.6980 \\\\ 1.0000&1.7324& 0.0000\\end{bmatrix}\\begin{bmatrix} Y \\\\ U\\\\ V\\end{bmatrix}\\quad\\end{gathered}\\)
File extension: .JPEG, .JFIF, .JPG, .JPE
Compression format for static image :
As a counterpart of Moving Picture Expert Group.
Lossy encoding method
Allow user to make balance between image quality and image size.
Encoding based on transformation, e.g. Discrete Cosine Transformation (DCT)JPEG2000 is based on wavelet.
Different manipulations for high-frequency signal and low-frequency signal.
Compression strategy: According to the requirement of compression ratio, remove information from high frequency to low frequency.
Advantages:
\u538b\u7f29\u7b56\u7565\uff1a\u6839\u636e\u538b\u7f29\u6bd4\u8981\u6c42\uff0c\u4ece\u9ad8\u9891\u5230\u4f4e\u9891\u9010\u6b65\u524a\u51cf\u4fe1\u606f
More specifically,refer to the PPT(Chapter I)
"},{"location":"DIP/Basic/DIP/#disadvantages","title":"Disadvantages","text":"\u4e0d\u9002\u5408\u7528\u4e8e\u7ebf\u6761\u753b\u3001\u6587\u5b57\u3001\u56fe\u6807\u7b49\uff0c\u5176\u6709\u635f\u538b\u7f29\u4f1a\u5bfc\u81f4\u8fd9\u7c7b\u5bf9\u8c61\u7684\u7455\u75b5\u4e25\u91cd
"},{"location":"DIP/Basic/DIP/#tiff-format","title":"TIFF format","text":""},{"location":"DIP/Basic/DIP/#gif-format","title":"GIF format","text":""},{"location":"DIP/Basic/DIP/#chains","title":"Chains","text":"Chains are used for description of object borders
Chains can be represented using static data structures (e.g., 1D arrays); their size is the longest length of the chain expected.
"},{"location":"DIP/Basic/DIP/#run-length-encoding-rle","title":"Run length encoding (RLE)","text":"RLE is run-length encoding. It is used to encode the location of foreground objects in segmentation. Instead of outputting a mask image, you give a list of start pixels and how many pixels after each of those starts is included in the mask.
ImageNet, Kaggle Facial Expression, Kaggle Multimodal Learning, German Traffic Signs, Handwriting, \u2026.
CNNs are deployed in many practical applications
Image recognition, speech recognition, Google\u2019s and Baidu\u2019s photo taggers
xCNNs are applicable to array data where nearby values are correlated
Images, sound, time-frequency representations, video, volumetric images, RGB-Depth images, \u2026
CNN is one of the few deep models that can be trained in supervised way.
https://towardsdatascience.com/understanding-backpropagation-algorithm-7bb3aa2f95fd
http://galaxy.agh.edu.pl/%7Evlsi/AI/backp_t_en/backprop.html
\u5b66\u4e60\u7387\u4f4e\uff0c\u6536\u655b\u6162\uff0c\u5bb9\u6613\u6389\u5230\u5751\u91cc\u9677\u5165\u5c40\u90e8\u6700\u4f18\uff1b\u5b66\u4e60\u7387\u9ad8\uff0c\u53ef\u80fd\u4f7f\u5f97\u6536\u655b\u8fc7\u7a0b\u4e0d\u7a33\u5b9a\uff0c\u6765\u56de\u9707\u8361\uff0c\u4e00\u76f4\u4e0d\u6536\u655b
idea:
Example:
\u4e3a\u4ec0\u4e48\u5f15\u5165CNN\u53ef\u4ee5\u5927\u89c4\u6a21\u51cf\u5c11\u6743\u6570\u53c2\u6570\u8bad\u7ec3\u91cf\u56e0\u4e3aCNN\u901a\u8fc7
1\uff09 \u5c40\u90e8\u8fde\u63a5\uff08Local Connectivity\uff09
2\uff09 \u6743\u503c\u5171\u4eab\uff08Shared Weights\uff09
3\uff09 \u6c60\u5316\uff08Pooling\uff09
CNN\u6c60\u5316\u53ef\u4ee5\u901a\u8fc7\u6c60\u5316\u5c42\u6765\u964d\u4f4e\u5377\u79ef\u5c42\u8f93\u51fa\u7684\u7279\u5f81\u7ef4\u5ea6\uff0c\u5728\u6709\u6548\u51cf\u5c11\u7f51\u7edc\u53c2\u6570\u7684\u540c\u65f6\u8fd8\u53ef\u4ee5\u9632\u6b62\u8fc7\u62df\u5408\u73b0\u8c61
https://zhuanlan.zhihu.com/p/78760534
"},{"location":"DIP/DL/DL/#cnn-in-detail","title":"CNN in Detail","text":"\u6b65\u957f\uff1f
Refer to the PPT !
Many mathematicians tried to find a way to approximate a complex function by some simple functions for many years. Apart from the theoretical concern, this technique is invaluable for many practical applications.
Before the invention of differential calculus, this problem kept open.
"},{"location":"DIP/Fourier/Fourier/#fourier-transform","title":"Fourier Transform","text":"\\(\\begin{align*}f(x)&=\\frac{1}{2}a_0+\\sum_{n=1}^{\\infty}a_ncos(nx)+\\sum_{n=1}^{\\infty}b_nsin(nx)\\\\ a_0 &= \\frac{1}{\\pi}\\int_{-\\pi}^{\\pi}f(x)dx\\\\a_n &= \\frac{1}{\\pi}\\int_{-\\pi}^{\\pi}f(x)cos(nx)dx\\\\b_n &= \\frac{1}{\\pi}\\int_{-\\pi}^{\\pi}f(x)sin(nx)dx \\end{align*}\\)
Just like Calculus invented by Newton, Fourier analysis is another mathematical tool
Magnitude-Phase\uff08vector\uff09\\(x=|x|e^{j\\phi(x)}\\) where \\(|x|=\\sqrt{a^2+b^2}\\ \\phi(x)=tan^{-1(\\frac{b}{a})}\\)
Basic Properties
\\(xy=|x|e^{j\\phi(x)}|y|e^{j\\phi(y)}=|x||y|e^{j(\\phi(x)+\\phi(y))}\\)
\\(|x|=|x^*|\\)
\\(\\phi(x)=-\\phi(x^*)\\)
\\(xx^*=|x|^2\\)
"},{"location":"DIP/Fourier/Fourier/#image-transform","title":"Image Transform","text":"
Many times, image processing tasks are best performed in transformed domain other than the spatial domain.
Key steps:
(1) Transform the image
(2) Carry out the task(s) in the transformed domain.
(3) Apply inverse transform to return to the spatial domain.
"},{"location":"DIP/Fourier/Fourier/#transformation-kernels","title":"Transformation Kernels","text":""},{"location":"DIP/Fourier/Fourier/#continuous-fourier-transform-ft","title":"Continuous Fourier Transform (FT)","text":"Transforms a signal (i.e., function) from the spatial domain to the frequency domain.
To remove certain frequencies, set their corresponding F(u) coefficients to zero!
Low frequencies correspond to slowly varying information (e.g., continuous surface).
High frequencies correspond to quickly varying information (e.g., edges)
\u4e8c\u7ef4\u79bb\u6563\u5085\u7acb\u53f6\u53d8\u6362\u5b9a\u4e49\u4e3a
\u4e8c\u7ef4\u79bb\u6563\u5085\u7acb\u53f6\u9006\u53d8\u6362\u5b9a\u4e49\u4e3a
\u4e3a\u4e86\u65b9\u4fbf\uff0c\u6211\u4eec\u53ef\u5c06\u4e0a\u8ff0\u4e24\u5f0f\u6539\u4e3a\u5411\u91cf\u5f62\u5f0f
\u5176\u4e2d \u3002
For Images
\\((u,v)\\)-- \u76f8\u4f4d
\\(F(u,v)\\)-- \u5e45\u503c
"},{"location":"DIP/Fourier/Fourier/#fast-fourier-transformfft","title":"Fast Fourier Transform\uff08FFT\uff09","text":"Speed up the discrete Fourier Transform
There are many repetitive work in the computation.
Cooley and Tukey presented in 1965:
HENCE \\(F(k+M)=\\lfloor F_e(k)-F_o(k)W_{2M}^k\\rfloor\\)!!
\u610f\u4e49\uff1a\u5bf9\u4e00\u4e2a\u957f\u5ea6\u4e3a N \u7684\u5e8f\u5217\u8fdb\u884c\u5085\u7acb\u53f6\u53d8\u6362\u53ef\u4ee5\u901a\u8fc7\u5c06\u5176\u5206\u6210\u4e24\u534a\u8ba1\u7b97\uff0c\u5bf9\u7b2c\u4e00\u90e8\u5206\u7684\u8ba1\u7b97\u9700\u8981\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u957f\u5ea6\u4e3a N/2 \u957f\u5ea6\u5e8f\u5217\u7684\u5085\u7acb\u53f6\u53d8\u6362\u5f0f\u8fdb\u884c\uff0c\u7136\u540e\u5229\u7528\u8fd9\u4e24\u4e2a\u957f\u5ea6\u4e3a N/2 \u7684\u5e8f\u5217\u53ef\u4ee5\u5f97\u5230\u7b2c\u4e8c\u90e8\u5206\u7684\u503c
Hint: use inverse DFT to reconstruct the image by magnitude or phase only information
magnitude determines the contribution of each component!(cannot be used to reconstruct a image!)
phase determines which components are present!
PHASE is more important!
"},{"location":"DIP/chap2/chap2/#binary-image-and-morphological-operation","title":"Binary Image and Morphological Operation","text":""},{"location":"DIP/chap2/chap2/#binary-image","title":"Binary image","text":"
\u4f18\u70b9\uff1a\u66f4\u5c0f\u7684\u5185\u5b58\u9700\u6c42 \u8fd0\u884c\u901f\u5ea6\u66f4\u5feb \u4e3a\u4e8c\u503c\u56fe\u50cf\u5f00\u53d1\u7684\u7b97\u6cd5\u5f80\u5f80\u53ef\u4ee5\u7528\u4e8e\u7070\u5ea6\u7ea7\u56fe\u50cf \u66f4\u4fbf\u5b9c \u7f3a\u70b9\uff1a\u5e94\u7528\u8303\u56f4\u6bd5\u7adf\u6709\u9650\uff1b \u66f4\u65e0\u6cd5\u63a8\u5e7f\u5230\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u8868\u73b0\u529b\u6b20\u7f3a\uff0c\u4e0d\u80fd\u8868\u73b0\u7269\u4f53\u5185\u90e8\u7ec6\u8282\u65e0\u6cd5\u63a7\u5236\u5bf9\u6bd4\u5ea6
"},{"location":"DIP/chap2/chap2/#_1","title":"\u56fe\u50cf\u4e8c\u503c\u5316\uff1a","text":""},{"location":"DIP/chap2/chap2/#1","title":"\uff081\uff09\u4e8c\u503c\u56fe\u50cf","text":"\u4e8c\u503c\u56fe\u50cf\uff08Binary Image\uff09\u4e2d\uff0c\u50cf\u7d20\u503c\u9650\u5b9a\u4e3a \\(0\\) \u6216 \\(1\\)\uff08\u7f16\u7a0b\u4e2d\u4e00\u822c\u7528 \\(0\\) \u548c\\(255\\) \u6765\u6784\u9020\u4e8c\u503c\u56fe\u50cf)\u3002
\u5728\u628a\u7070\u5ea6\u56fe\u8f6c\u6362\u4e3a\u4e8c\u503c\u56fe\u50cf\u7684\u65f6\u5019\uff0c\u4e00\u4e2a\u57fa\u672c\u7684\u60f3\u6cd5\u662f\u627e\u5230\u4e00\u4e2a\u5408\u9002\u7684\u9608\u503c\uff08Threshold\uff09\uff0c\u50cf\u7d20\u70b9\u503c\u6bd4\u9608\u503c\u5927\u7684\u8bbe\u4e3a\\(255\\)\uff0c\u6bd4\u9608\u503c\u5c0f\u7684\u8bbe\u4e3a\\(0\\)\u3002\u5373\u4e3a\u5982\u4e0b\uff1a
\\(\\left\\{ \\begin{array}{**lr**} I(x,y) = 0\\ if\\ I(x,y)\\leq Threshold \\\\ I(x,y) = 255\\ if\\ I(x,y)\\geq Threshold \\\\ \\end{array} \\right.\\)
a.\u7edf\u8ba1\u50cf\u7d20\u70b9\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c
b.\u5bf9\u4e8e\u6bcf\u4e2a\u53ef\u80fd\u7684\u9608\u503c\uff0c\u8ba1\u7b97\u5206\u6210\u4e24\u4e2a\u7c7b\u522b\u540e\u7684\u80cc\u666f\u548c\u524d\u666f\u7684\u5185\u90e8\u534f\u65b9\u5dee\u548c\u5916\u90e8\u534f\u65b9\u5dee\u3002
c.\u9009\u62e9\u6700\u5927\u5916\u90e8\u548c\u6700\u5c0f\u5185\u90e8\u534f\u65b9\u5dee\u5bf9\u5e94\u7684 threshold\u4f5c\u4e3a\u4e8c\u503c\u5316\u9608\u503c\u3002
d.\u5e94\u7528\u9009\u62e9\u7684\u9608\u503c\u5c06\u56fe\u50cf\u5206\u5272\u6210\u524d\u666f\u548c\u80cc\u666f\u3002
\u5176\u4e2d\uff0c\u6700\u5927\u5916\u90e8\u65b9\u5dee\u548c\u6700\u5c0f\u5185\u90e8\u65b9\u5dee\u662f\u7b49\u4ef7\u7684\uff0c\u6211\u4eec\u9009\u62e9\u627e\u5230\u6700\u5927\u5916\u90e8\u65b9\u5dee\uff0c\u63a8\u5bfc\u8fc7\u7a0b\u5982\u4e0b\uff1a
\u8bbe\u56fe\u50cf\u5c3a\u5bf8\u4e3a\\(M\\ *\\ N\\)\uff0c\u5176\u4e8c\u503c\u5316\u7684\u6700\u4f73\u9608\u503c\u4e3a\\(T\\)\uff0c\u8be5\u9608\u503c\u5c06\u56fe\u50cf\u5206\u4e3a\u80cc\u666f\u548c\u76ee\u6807\u4e24\u4e2a\u7c7b\u522b\u3002\u5176\u4e2d\u5c5e\u4e8e\u80cc\u666f\u7684\u50cf\u7d20\u70b9\u6570\u91cf\u4e3a\\(N0\\),\u5c5e\u4e8e\u76ee\u6807\u7684\u50cf\u7d20\u70b9\u6570\u91cf\u4e3a\\(N1\\),\u80cc\u666f\u50cf\u7d20\u70b9\u6570\u5360\u6574\u5e45\u56fe\u50cf\u7684\u6bd4\u4f8b\\(\\omega0\\),\u5176\u7070\u5ea6\u5747\u503c\\(\u03bc0\\),\u76ee\u6807\u50cf\u7d20\u70b9\u6570\u5360\u6574\u5e45\u56fe\u50cf\u7684\u6bd4\u4f8b\u4e3a\\(\u03c91\\),\u5176\u7070\u5ea6\u5747\u503c\u4e3a\\(\u03bc1\\) \uff0c\u6574\u5e45\u56fe\u50cf\u7684\u7070\u5ea6\u5747\u503c\u4e3a\\(\\mu\\)\u3002
\\(\\omega_0=\\frac{N0}{M*N}\\\\\\) \\(\\omega_1=\\frac{N1}{M*N}\\) \\(N0+N1=M*N\\\\\\) \\(\\omega0\\ + \\omega1\\ = 1\\\\\\)
\\(\\mu = \\frac{\\mu0*N0+\\mu1*N1}{M*N}=\\mu0\\omega0\\ + \\mu1\\omega1\\\\\\)
\\(\u7c7b\u5185\u65b9\u5dee(Within-class\\ variance)\u516c\u5f0f:\\)
\\(\\sigma_{Withen}^{2}=\\omega_{0} \\sigma_{0}^{2}+\\omega_{1} \\sigma_{1}^{2} \\\\\\)
\\(\u7c7b\u95f4\u65b9\u5dee(Between-class\\ variance)\u7684\u516c\u5f0f:\\sigma_{Between}^{2}=\\sigma^{2}-\\sigma_{Within}^{2}\\\\\\)
\\(\u6709\u6b64\u53ef\u63a8\u5bfc\u5f97\u5230\\)
\\(\\sigma_{Between}^{2}=\\omega_{0}(\\mu_{0}-\\mu)^{2}+\\omega_{1}(\\mu_{1}-\\mu)^{2}\\)
\\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ =\\omega0(\\mu0-\\omega0\\mu0-\\omega1\\mu1)^{2}+\\omega1(\\mu1-\\omega0\\mu0-\\omega1\\mu1)^{2}\\) \\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ =\\omega0(\\omega1\\mu0-\\omega1\\mu1)^{2}+\\omega1(\\omega0\\mu1-\\omega0\\mu0)^{2}\\\\\\) \\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ =(\\omega0\\omega1^{2}+\\omega1\\omega0^{2})(\\mu1-\\mu0)^{2}\\\\\\)
\\(\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ =\\omega0\\omega1(\\mu0-\\mu1)^{2}\\\\\\)
Step 1: \u786e\u5b9a\u539f\u59cb\u56fe\u50cf\u4e2d\u50cf\u7d20\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c
Step 2: \u6700\u5c0f\u503c\u52a01\u4f5c\u4e3a\u521d\u59cbthreshold\u5bf9\u539f\u59cb\u56fe\u50cf\u8fdb\u884c\u4e8c\u503c\u5316\u64cd\u4f5c
Step 3: \u6839\u636e\u5bf9\u5e94\u5173\u7cfb\u786e\u5b9a\u524d\u666f\u548c\u80cc\u666f\uff0c\u5206\u522b\u8ba1\u7b97\u5f53\u524dthreshold\u4e0b\u7684\u5185\u90e8\u534f\u65b9\u5dee\u548c\u5916\u90e8\u534f\u65b9\u5dee
Step 4: \u56de\u5230Step 2\u76f4\u5230\u8fbe\u5230\u50cf\u7d20\u6700\u5927\u503c
Step 5\uff1a\u627e\u5230\u6700\u5927\u5916\u90e8\u548c\u6700\u5c0f\u5185\u90e8\u534f\u65b9\u5dee\u5bf9\u5e94\u7684threshold.
\u5bf9\u4e8e\u6bcf\u4e00\u7a97\u53e3\u4f4d\u7f6e\uff0c\u786e\u5b9a\u9488\u5bf9\u8be5\u7a97\u53e3\u7684threshold
\u76f8\u6bd4\u5168\u5c40\u5927\u6d25\u6cd5\uff0c\u6211\u4eec\u4f9d\u6b21\u679a\u4e3e\u7070\u5ea6\u56fe\u4e2d\u7684\u6bcf\u4e2a\u50cf\u7d20\\(\uff08i,j)\\)\uff0c\u4ee5\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u4e3a\u8fb9\u957f\uff0c\u5728\\((i - length / 2, j - length / 2, i + length / 2, j + length / 2, i, j)\\)\u8fd9\u4e2a\u6b63\u65b9\u5f62\u4e2d\u8fdb\u884c\u5927\u6d25\u6cd5\uff0c\u5f97\u5230\u9608\u503c\u540e\u4ec5\u5bf9\\((i,j)\\)\u70b9\u8fdb\u884c\u4e8c\u503c\u5316\u5904\u7406\u3002
\u76f8\u6bd4\u5168\u5c40\u5927\u6d25\u6cd5\uff0c\u5c40\u90e8\u5927\u6d25\u6cd5\u8017\u65f6\u663e\u8457\u589e\u52a0\u3002
\u200b \u6570\u5b66\u5f62\u6001\u5b66\uff08\u4e5f\u79f0\u56fe\u50cf\u4ee3\u6570\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5206\u6790\u56fe\u50cf\u5f62\u6001\u548c\u7ed3\u6784\u7684\u5de5\u5177\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u5229\u7528\u7ed3\u6784\u5143\u7d20\u6765\u5ea6\u91cf\u548c\u63d0\u53d6\u5f62\u72b6\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u56fe\u50cf\u7684\u5206\u6790\u548c\u8bc6\u522b\u3002\u56fe\u50cf\u5f62\u6001\u5b66\u64cd\u4f5c\u57fa\u4e8e\u96c6\u5408\u8bba\uff0c\u53ef\u4ee5\u7b80\u5316\u56fe\u50cf\uff0c\u53bb\u9664\u4e0d\u9700\u8981\u7684\u7ed3\u6784\uff0c\u540c\u65f6\u4fdd\u7559\u4e3b\u8981\u7684\u5f62\u72b6\u7279\u5f81\u3002\u5728\u8fd9\u91cc\u6709\u56db\u79cd\u5f62\u6001\u5b66\u64cd\u4f5c\u7b26\uff1a\u81a8\u80c0\u3001\u8150\u8680\u3001\u5f00\u8fd0\u7b97\u548c\u95ed\u8fd0\u7b97\u3002
\u200b \u5728\u4e0b\u9762\u7684\u4ecb\u7ecd\u4e2d\uff0c\u6211\u4eec\u7684 A \u6307\u4e8c\u503c\u56fe\u50cf; B \u6307\u4e8c\u503c\u6a21\u677f\uff0c\u79f0\u4e3a\u7ed3\u6784\u5143(structure element)\u3002
"},{"location":"DIP/chap2/chap2/#_3","title":"\u81a8\u80c0","text":"\u200b \u5f62\u6001\u5b66\u7684\u81a8\u80c0\u64cd\u4f5c\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u79cd\u57fa\u672c\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u589e\u5f3a\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u76ee\u6807\u6216\u7ed3\u6784\uff0c\u4f7f\u5176\u66f4\u52a0\u663e\u8457\u548c\u5bb9\u6613\u68c0\u6d4b\u3002\u81a8\u80c0\u64cd\u4f5c\u7684\u4e3b\u8981\u601d\u60f3\u662f\u5229\u7528\u4e00\u4e2a\u79f0\u4e3a\u7ed3\u6784\u5143\u7d20\u7684\u5c0f\u7a97\u53e3\u6216\u6a21\u677f\uff0c\u6cbf\u7740\u56fe\u50cf\u7684\u5404\u4e2a\u65b9\u5411\u626b\u63cf\uff0c\u5e76\u6839\u636e\u7ed3\u6784\u5143\u7d20\u4e0e\u56fe\u50cf\u7684\u91cd\u53e0\u7a0b\u5ea6\u6765\u6539\u53d8\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u3002
\u200b \u81a8\u80c0\u64cd\u4f5c\u7684\u7ed3\u679c\u901a\u5e38\u662f\u589e\u5f3a\u4e86\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u76ee\u6807\u6216\u7279\u5b9a\u7ed3\u6784\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5c06\u76ee\u6807\u533a\u57df\u6269\u5c55\uff0c\u586b\u5145\u7a7a\u6d1e\uff0c\u4f7f\u8fb9\u7f18\u66f4\u52a0\u6e05\u6670\u3002\u56e0\u6b64\uff0c\u81a8\u80c0\u64cd\u4f5c\u53ef\u7528\u4e8e\u6539\u5584\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u7279\u5f81\uff0c\u4f7f\u5b83\u4eec\u66f4\u5bb9\u6613\u68c0\u6d4b\u548c\u5206\u6790\u3002 $$ A\u2295B={ z|(B)_z \u2229A\\ne\u2205 } $$
Dilation: Bridging gaps in images
\u7531B\u5bf9A\u81a8\u80c0\u6240\u4ea7\u751f\u7684\u4e8c\u503c\u56fe\u8c61D\u662f\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684\u70b9(x,y)\u7684\u96c6\u5408\uff1a
\u5982\u679cB\u7684\u539f\u70b9\u5e73\u79fb\u5230\u70b9(x,y)\uff0c\u90a3\u4e48\u5b83\u4e0eA\u7684\u4ea4\u96c6\u975e\u7a7a\u3002
"},{"location":"DIP/chap2/chap2/#_4","title":"\u8150\u8680","text":"\u200b \u5f62\u6001\u5b66\u7684\u8150\u8680\u64cd\u4f5c\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u79cd\u57fa\u672c\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u51cf\u5c0f\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u76ee\u6807\u6216\u7ed3\u6784\uff0c\u4ece\u800c\u5f31\u5316\u6216\u6d88\u9664\u4e00\u4e9b\u7279\u5f81\u3002\u8150\u8680\u64cd\u4f5c\u7684\u4e3b\u8981\u601d\u60f3\u662f\u5229\u7528\u4e00\u4e2a\u79f0\u4e3a\u7ed3\u6784\u5143\u7d20\u7684\u5c0f\u7a97\u53e3\u6216\u6a21\u677f\uff0c\u6cbf\u7740\u56fe\u50cf\u7684\u5404\u4e2a\u65b9\u5411\u626b\u63cf\uff0c\u5e76\u6839\u636e\u7ed3\u6784\u5143\u7d20\u4e0e\u56fe\u50cf\u7684\u91cd\u53e0\u7a0b\u5ea6\u6765\u6539\u53d8\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u3002
\u200b \u8150\u8680\u64cd\u4f5c\u7684\u7ed3\u679c\u901a\u5e38\u662f\u51cf\u5c0f\u4e86\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u76ee\u6807\u6216\u7279\u5b9a\u7ed3\u6784\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u53ef\u4ee5\u53bb\u9664\u5c0f\u7684\u7279\u5f81\u3001\u7f29\u5c0f\u76ee\u6807\u533a\u57df\u6216\u6d88\u9664\u566a\u58f0\u3002\u8150\u8680\u64cd\u4f5c\u53ef\u7528\u4e8e\u5f31\u5316\u56fe\u50cf\u4e2d\u7684\u7279\u5b9a\u7279\u5f81\uff0c\u4f7f\u5176\u66f4\u5bb9\u6613\u68c0\u6d4b\u548c\u5206\u6790\u3002 $$ A\u2296B={(x,y)|(B)_{xy} \u2286A} $$
\u5982\u679cB\u7684\u539f\u70b9\u5e73\u79fb\u5230\u70b9(x,y)\uff0c\u90a3\u4e48B\u5c06\u5b8c\u5168\u5305\u542b\u4e8eA\u4e2d
"},{"location":"DIP/chap2/chap2/#duality","title":"Duality","text":""},{"location":"DIP/chap2/chap2/#application-boundary-extraction","title":"Application: Boundary Extraction","text":""},{"location":"DIP/chap2/chap2/#application-filling-holes","title":"Application: Filling holes\uff01","text":""},{"location":"DIP/chap2/chap2/#_5","title":"\u5f00\u64cd\u4f5c","text":"\u200b \u5f62\u6001\u5b66\u7684\u5f00\u64cd\u4f5c\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5b83\u7ed3\u5408\u4e86\u8150\u8680\uff08erosion\uff09\u548c\u81a8\u80c0\uff08dilation\uff09\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u53bb\u566a\u548c\u5206\u5272\u56fe\u50cf\u4e2d\u7684\u76ee\u6807\u3002\u5f00\u64cd\u4f5c\u7684\u4e3b\u8981\u601d\u60f3\u662f\u9996\u5148\u5bf9\u56fe\u50cf\u6267\u884c\u8150\u8680\u64cd\u4f5c\uff0c\u7136\u540e\u518d\u5bf9\u5176\u6267\u884c\u81a8\u80c0\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u901a\u5e38\u4f7f\u7528\u4e00\u4e2a\u7279\u5b9a\u7684\u7ed3\u6784\u5143\u7d20\u8fdb\u884c\u3002
\u200b \u5f00\u64cd\u4f5c\u5728\u56fe\u50cf\u5904\u7406\u4e2d\u5e38\u7528\u4e8e\u53bb\u9664\u566a\u58f0\u3001\u5206\u5272\u76ee\u6807\u3001\u51cf\u5c0f\u76ee\u6807\u533a\u57df\u7684\u4e0d\u89c4\u5219\u6027\u4ee5\u53ca\u51cf\u5c0f\u76ee\u6807\u4e4b\u95f4\u7684\u5e72\u6270\u3002\u5b83\u4e5f\u6709\u52a9\u4e8e\u6539\u5584\u4e8c\u503c\u5316\u56fe\u50cf\u7684\u8d28\u91cf\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u5206\u6790\u548c\u7279\u5f81\u63d0\u53d6\u3002 $$ A \u25e6 B = (A \u2296 B) \u2295 B $$
\u200b \u5f62\u6001\u5b66\u7684\u5f00\u64cd\u4f5c\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5b83\u7ed3\u5408\u4e86\u8150\u8680\uff08erosion\uff09\u548c\u81a8\u80c0\uff08dilation\uff09\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u53bb\u566a\u548c\u5206\u5272\u56fe\u50cf\u4e2d\u7684\u76ee\u6807\u3002\u5f00\u64cd\u4f5c\u7684\u4e3b\u8981\u601d\u60f3\u662f\u9996\u5148\u5bf9\u56fe\u50cf\u6267\u884c\u8150\u8680\u64cd\u4f5c\uff0c\u7136\u540e\u518d\u5bf9\u5176\u6267\u884c\u81a8\u80c0\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u901a\u5e38\u4f7f\u7528\u4e00\u4e2a\u7279\u5b9a\u7684\u7ed3\u6784\u5143\u7d20\u8fdb\u884c\u3002
\u200b \u95ed\u64cd\u4f5c\u5728\u56fe\u50cf\u5904\u7406\u4e2d\u5e38\u7528\u4e8e\u53bb\u9664\u5c0f\u5b54\u3001\u8fde\u63a5\u65ad\u5f00\u7684\u7ebf\u6761\u3001\u586b\u5145\u4e0d\u89c4\u5219\u76ee\u6807\u7684\u5185\u90e8\u7a7a\u6d1e\u3001\u589e\u5f3a\u56fe\u50cf\u4e2d\u7684\u8fde\u901a\u533a\u57df\uff0c\u5e73\u6ed1\u5176\u8fb9\u754c\u7684\u540c\u65f6\u5e76\u4e0d\u660e\u663e\u6539\u53d8\u5176\u9762\u79ef\u3002\u5b83\u4e5f\u6709\u52a9\u4e8e\u6539\u5584\u4e8c\u503c\u5316\u56fe\u50cf\u7684\u8d28\u91cf\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u5206\u6790\u548c\u7279\u5f81\u63d0\u53d6\u3002 $$ A \u2022 B = (A \u2295 B) \u2296 B $$
"},{"location":"DIP/chap3/chap3/","title":"Basic Operation","text":""},{"location":"DIP/chap3/chap3/#visibility-enhancement","title":"Visibility enhancement","text":""},{"location":"DIP/chap3/chap3/#logarithmic-operation","title":"logarithmic operation","text":"
\u4e3a\u4e86\u589e\u5f3a\u56fe\u50cf\u7684\u53ef\u89c6\u4fe1\u606f\uff0c\u5bf9\u56fe\u50cf\u4e2d\u7684\u50cf\u7d20\u8fdb\u884c\u57fa\u4e8e\u5bf9\u6570\u7684\u64cd\u4f5c \\(L_d = \\frac{log{({L_{w}+1})}}{log{({L_{max}+1})}}\\) \u5176\u4e2d\uff0c\\(L_d\\)\u662f\u663e\u793a\u4eae\u5ea6\uff0c\\(L_w\\)\u662f\u771f\u5b9e\u4e16\u754c\u4eae\u5ea6\uff0c\\(L_{max}\\)\u662f\u573a\u666f\u4e2d\u7684\u6700\u4eae\u503c\u3002
\\(r_k - \u50cf\u7d20\u7684\u7070\u5ea6\u7ea7\\)
\\(n_k - \u5177\u6709\u7070\u5ea6rk\u7684\u50cf\u7d20\u7684\u4e2a\u6570\\)
\\(MN - \u56fe\u50cf\u4e2d\u603b\u7684\u50cf\u7d20\u4e2a\u6570\\)
\\(\\sum_{k=0}^{MN-1}P(r_k)=1\\)
"},{"location":"DIP/chap3/chap3/#b","title":"b.\u5f69\u8272\u76f4\u65b9\u56fe","text":"\u76f4\u65b9\u56fe\u5747\u8861\u5316\uff08Histogram Equalization\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u589e\u5f3a\u6570\u5b57\u56fe\u50cf\u5bf9\u6bd4\u5ea6\u7684\u56fe\u50cf\u5904\u7406\u6280\u672f\u3002\u5b83\u901a\u8fc7\u91cd\u65b0\u5206\u5e03\u56fe\u50cf\u7684\u50cf\u7d20\u503c\uff0c\u4f7f\u76f4\u65b9\u56fe\u53d8\u5f97\u66f4\u5747\u5300\uff0c\u4ece\u800c\u6539\u5584\u56fe\u50cf\u7684\u89c6\u89c9\u8d28\u91cf
\u76f4\u65b9\u56fe\u5747\u8861\u5316\u5e38\u88ab\u7528\u4e8e\u63d0\u9ad8\u56fe\u50cf\u7684\u53ef\u89c6\u6027\u3001\u5206\u6790\u6027\u80fd\u548c\u81ea\u52a8\u56fe\u50cf\u5904\u7406\u5e94\u7528\u4e2d\u3002\u4f8b\u5982\uff0c\u8fc7\u66dd\u5149\u56fe\u50cf\u7684\u7070\u5ea6\u7ea7\u96c6\u4e2d\u5728\u9ad8\u4eae\u5ea6\u8303\u56f4\u5185\uff0c\u800c\u66dd\u5149\u4e0d\u8db3\u5c06\u4f7f\u56fe\u50cf\u7070\u5ea6\u7ea7\u96c6\u4e2d\u5728\u4f4e\u4eae\u5ea6\u8303\u56f4\u5185\u3002\u91c7\u7528\u76f4\u65b9\u56fe\u5747\u8861\u5316\uff0c\u53ef\u4ee5\u628a\u539f\u59cb\u56fe\u50cf\u7684\u76f4\u65b9\u56fe\u53d8\u6362\u4e3a\u5747\u5300\u5206\u5e03(\u5747\u8861)\u7684\u5f62\u5f0f\uff0c\u8fd9\u6837\u5c31\u589e\u52a0\u4e86\u50cf\u7d20\u4e4b\u95f4\u7070\u5ea6\u503c\u5dee\u522b\u7684\u52a8\u6001\u8303\u56f4\uff0c\u4ece\u800c\u8fbe\u5230\u589e\u5f3a\u56fe\u50cf\u6574\u4f53\u5bf9\u6bd4\u5ea6\u7684\u6548\u679c
\u6362\u8a00\u4e4b\uff0c\u76f4\u65b9\u56fe\u5747\u8861\u5316\u7684\u57fa\u672c\u539f\u7406\u662f:\u5bf9\u5728\u56fe\u50cf\u4e2d\u50cf\u7d20\u4e2a\u6570\u591a\u7684\u7070\u5ea6\u503c(\u5373\u5bf9\u753b\u9762\u8d77\u4e3b\u8981\u4f5c\u7528\u7684\u7070\u5ea6\u503c)\u8fdb\u884c\u5c55\u5bbd\uff0c\u800c\u5bf9\u50cf\u7d20\u4e2a\u6570\u5c11\u7684\u7070\u5ea6 \u503c(\u5373\u5bf9\u753b\u9762\u4e0d\u8d77\u4e3b\u8981\u4f5c\u7528\u7684\u7070\u5ea6\u503c)\u8fdb\u884c\u5f52\u5e76\uff0c\u4ece\u800c\u589e\u5927\u5bf9\u6bd4\u5ea6\uff0c\u4f7f\u56fe\u50cf\u6e05\u6670\uff0c\u8fbe\u5230\u589e\u5f3a\u7684\u76ee\u7684
\u5c3d\u7ba1\u76f4\u65b9\u56fe\u5747\u8861\u5316\u5177\u6709\u663e\u8457\u7684\u4f18\u70b9\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u9650\u5236\u3002\u5b83\u53ef\u80fd\u5bfc\u81f4\u56fe\u50cf\u51fa\u73b0\u566a\u70b9\u6216\u8fc7\u5ea6\u589e\u5f3a\u7ec6\u8282\uff0c\u7279\u522b\u662f\u5728\u566a\u97f3\u5b58\u5728\u7684\u60c5\u51b5\u4e0b
\u6b64\u5916\uff0c\u5747\u8861\u5316\u53ef\u80fd\u6539\u53d8\u56fe\u50cf\u7684\u6574\u4f53\u4eae\u5ea6\uff0c\u8fd9\u53ef\u80fd\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u56fe\u50cf
\u8fde\u7eed\u60c5\u51b5\u7684\u8f6c\u6362\\(T\\):
\u8bbe r \u4e3a\u8f6c\u6362\u524d\u7684\u7070\u5ea6\u7ea7\uff0c s \u4e3a\u53d8\u6362\u524d\u540e\u7684\u7070\u5ea6\u7ea7
\\(0 \u2264 r,s \u2264 1\\) [\u5bf9\u5e94\\((0,255)\\)]
P\u00ae \u548c P(s) \u662f\u5bf9\u5e94\u4e8e r \u548c s \u7684\u6982\u7387\u3002\u8f6c\u6362\u89c4\u5219\u4e3a:
T\u00ae\u662f\u5355\u8c03\u9012\u589e\u51fd\u6570\uff0c\\(0\u2264r\u22641\\)\u4e14\\(0\u2264T(r)\u22641\\)
\u9006\u53d8\u6362 r = \\(T^{-1}(s)\\)\u4e5f\u662f\u4e00\u4e2a\u5355\u8c03\u9012\u589e\u51fd\u6570 \u8003\u8651\u5230\u7070\u5ea6\u53d8\u6362\u4e0d\u4f1a\u6539\u53d8\u4f4d\u7f6e\u548c\u50cf\u7d20\u6570
\\(Thus: T(r)=\\int_0^rP(r)dr\\)
\u79bb\u6563\u60c5\u51b5\u7684\u8f6c\u6362 \\(T\\):
\u8bbe\u4e00\u5e45\u56fe\u50cf\u7684\u50cf\u7d20\u603b\u6570\u4e3an\uff0c\u5206L\u4e2a\u7070\u5ea6\u7ea7\uff0c\\(n_k\\)\u4e3a\u7b2ck\u4e2a\u7070\u5ea6\u7ea7\u51fa\u73b0\u7684\u50cf\u7d20\u6570\uff0c\u5219\u7b2c\\(k\\)\u4e2a\u7070\u5ea6\u7ea7\u51fa\u73b0\u7684\u6982\u7387\u4e3a\uff1a $$ P(r_k)= \\frac{n_k}{n}(0\u2264n_k \u22641,k=0,1,2,...,MN\u22121) $$
\u5bf9\u4e8e\u539f\u76f4\u65b9\u56fe\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u7070\u5ea6\u7ea7\\(r_k\\)\uff0c\u53ea\u9700\u5c06\u7070\u5ea6\u7ea7\u4e3a\\([0,r_k]\\)\u4ee5\u5185\u7684\u6240\u6709\u50cf\u7d20\u4e2a\u6570\u7684\u548c\u9664\u4ee5\u56fe\u50cf\u7684\u50cf\u7d20\u603b\u6570\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u8f6c\u6362\u4e4b\u540e\u7684\u5bf9\u5e94\u7070\u5ea6\u7ea7\\(s_k\\)
\u6309\u7167\u5747\u8861\u5316\u7684\u8981\u6c42\uff0c\u5728\u5747\u8861\u5316\u540e\u7684\u7ed3\u679c\u76f4\u65b9\u56fe\u4e2d\uff0c\u5404\u7070\u5ea6\u7ea7\u53d1\u751f\u7684\u6982\u7387\u5e94\u8be5\u662f\u76f8\u540c\u7684\uff0c\u5982\u53f3\u4e0a\u56fe\u6240\u793a\u8fde\u7eed\u7070\u5ea6\u7ea7\u5747\u8861\u5316\u7ed3\u679c\u90a3\u6837\u3002\u4f46\u662f\uff0c\u53f3\u4e0b\u56fe\u4e2d\u79bb\u6563\u7070\u5ea6\u7ea7\u5747\u8861\u5316\u540e\uff0c\u5404\u7070\u5ea6\u7ea7\u51fa\u73b0\u7684\u6982\u7387\u5e76\u4e0d\u5b8c\u5168\u4e00\u6837\u3002\u4e3a\u4ec0\u4e48\uff1f
\u8fd9\u6837\uff0c\u76f8\u90bb\u7684\u591a\u4e2a\\(s_k\\)\u5c31\u53ef\u80fd\u843d\u5165\u540c\u4e00\u4e2a\u7070\u5ea6\u7ea7\uff0c\u9700\u8981\u5728\u6b65\u9aa43\u65f6\u5c06\u5904\u4e8e\u540c\u4e00\u4e2a\u7070\u5ea6\u7ea7\u7684\u50cf\u7d20\u4e2a\u6570\u7d2f\u52a0,\u56e0\u6b64\uff0c\u79bb\u6563\u7070\u5ea6\u76f4\u65b9\u56fe\u5747\u8861\u5316\u64cd\u4f5c\u4ee5\u540e\uff0c\u6bcf\u4e2a\u7070\u5ea6\u7ea7\u5904\u7684\u6982\u7387\u5bc6\u5ea6\uff08\u6216\u50cf\u7d20\u4e2a\u6570\uff09\u5e76\u4e0d\u5b8c\u5168\u4e00\u6837\u3002
"},{"location":"DIP/chap3/chap3/#histogram-fitting","title":"Histogram fitting","text":"\u5728\u6b65\u9aa41\u548c2\u4e2d\uff0c\u5206\u522b\u8ba1\u7b97\u83b7\u5f97\u4e24\u5f20\u8868\uff08\u53c2\u89c1\u76f4\u65b9\u56fe\u5747\u8861\u5316\u4e2d\u7684\u7b97\u4f8b\uff09\uff0c\u4ece\u4e2d\u9009\u53d6\u4e00\u5bf9\\(v_k\u3001s_j\\)\uff0c\u4f7f\\(v_k = s_j\\)\uff0c\u5e76\u4ece\u4e24\u5f20\u8868\u4e2d\u67e5\u51fa\u5bf9\u5e94\u7684\\(z_k\\)\u3001\\(r_j\\)
\u8fd9\u6837\uff0c\u539f\u59cb\u56fe\u50cf\u4e2d\u7070\u5ea6\u7ea7\u4e3a\\(r_j\\)\u7684\u6240\u6709\u50cf\u7d20\u90fd\u6620\u5c04\u6210\u7070\u5ea6\u7ea7\\(z_k\\)\uff0c\u6700\u7ec8\u5f97\u5230\u6240\u671f\u671b\u7684\u56fe\u50cf\u3002
"},{"location":"DIP/chap3/chap3/#histogram-transform","title":"Histogram transform","text":""},{"location":"DIP/chap3/chap3/#linear-histogram-transform","title":"Linear histogram transform","text":"Linear grayscale transform by using a piecewise function, which strengthen the region of interest, weaken the region of no-interest.
\u5229\u7528\u5206\u6bb5\u76f4\u65b9\u56fe\u53d8\u6362\uff0c\u53ef\u4ee5\u5c06\u611f\u5174\u8da3\u7684\u7070\u5ea6\u8303\u56f4\u7ebf\u6027\u6269\u5c55\uff0c\u540c\u65f6\u76f8\u5bf9\u6291\u5236\u4e0d\u611f\u5174\u8da3\u7684\u7070\u5ea6\u533a\u57df
"},{"location":"DIP/chap3/chap3/#nonlinear-histogram-transform","title":"Nonlinear histogram transform","text":"Logarithmic function and Exponential function are two popular nonlinear transform functions.
Logarithmic
Expoential
\u5c06\u56fe\u50cf\u6cbf\u6c34\u5e73\u548c\u7ad6\u76f4\u65b9\u5411\u79fb\u52a8\uff0c\u4ece\u800c\u4ea7\u751f\u65b0\u56fe\u50cf\u7684\u8fc7\u7a0b\u3002
\u5e73\u79fb\u540e\u7684\u666f\u7269\u4e0e\u539f\u56fe\u50cf\u76f8\u540c\uff0c\u4f46\u201c\u753b\u5e03\u201d\u4e00\u5b9a\u662f\u6269\u5927\u4e86\uff0c\u5426\u5219\u5c31\u4f1a\u4e22\u5931\u4fe1\u606f\u3002 * \u77e9\u9635\u8868\u793a \\(\\begin{bmatrix}x^{'}\\\\y^{'}\\\\1\\end{bmatrix}=\\begin{bmatrix}1&0&x_0\\\\0&1&y_0\\\\0&0&1\\end{bmatrix}\\begin{bmatrix}x\\\\y\\\\1\\end{bmatrix}\\)
"},{"location":"DIP/chap3/chap3/#rotation","title":"\u65cb\u8f6c\uff08Rotation\uff09","text":"\u5c06\u56fe\u50cf\u4e58\u4ee5\u4e00\u5b9a\u7cfb\u6570\uff0c\u4ece\u800c\u4ea7\u751f\u65b0\u56fe\u50cf\u7684\u8fc7\u7a0b
\u6cbfy\u8f74\u65b9\u5411\u7f29\u653ed\u500d\uff08d>1\u65f6\u4e3a\u653e\u5927\uff0c0<d<1\u65f6\u4e3a\u7f29\u5c0f\uff09
\u653e\u5927 \uff1a\u65b0\u56fe\u50cf\u51fa\u73b0\u7a7a\u884c\u548c\u7a7a\u5217\uff0c\u53ef\u91c7\u7528\u63d2\u503c\u7684\u65b9\u6cd5\u52a0\u4ee5\u586b\u8865\uff0c\u4f46\u5b58\u5728\u201c\u9a6c\u8d5b\u514b\u201d\u73b0\u8c61\u3002
\u77e9\u9635\u8868\u793a \\(\\begin{bmatrix}x^{'}\\\\y^{'}\\\\1\\end{bmatrix}=\\begin{bmatrix}c&0&0\\\\0&d&0\\\\0&0&1\\end{bmatrix}\\begin{bmatrix}x\\\\y\\\\1\\end{bmatrix}\\)
\u56fe\u50cf\u7684\u9519\u5207\u53d8\u6362\u5b9e\u9645\u4e0a\u662f\u666f\u7269\u5728\u5e73\u9762\u4e0a\u7684\u975e\u5782\u76f4\u6295\u5f71\u6548\u679c
\\(Shear\\ on\\ x-axis:\\left\\{\\begin{aligned}&a(x, y) = x + d_xy \\\\&b(x, y) = y\\end{aligned}\\right.\\) \\(Shear\\ on\\ y-axis:\\left\\{\\begin{aligned}&a(x, y) = x \\\\&b(x, y) = y+d_yx\\end{aligned}\\right.\\)
\u7ed5x\u8f74\u6216y\u8f74\u7ffb\u8f6c\uff0c\u4ece\u800c\u4ea7\u751f\u4e0e\u539f\u56fe\u50cf\u5bf9\u79f0\u7684\u65b0\u56fe\u50cf\u7684\u8fc7\u7a0b * \u77e9\u9635\u8868\u793a \\(\\begin{bmatrix}x^{'}\\\\y^{'}\\\\1\\end{bmatrix}=\\begin{bmatrix}s_x&0&0\\\\0&s_y&0\\\\0&0&1\\end{bmatrix}\\begin{bmatrix}x\\\\y\\\\1\\end{bmatrix}\\)
\u4e3a\u4e86\u8ba1\u7b97\u51e0\u4f55\u53d8\u6362\u540e\u65b0\u56fe\u50cf\u4e2d\u67d0\u4e00\u70b9\\(P\u2019\\)\u5904\u7684\u50cf\u7d20\u503c\uff0c\u53ef\u4ee5\u9996\u5148\u8ba1\u7b97\u8be5\u51e0\u4f55\u53d8\u6362\u7684\u9006\u53d8\u6362\uff0c\u8ba1\u7b97\u51fa \\(P\u2019\\) \u6240\u5bf9\u5e94\u7684\u539f\u56fe\u50cf\u4e2d\u7684\u4f4d\u7f6e \\(P\\)
\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\\(P\\) \u7684\u4f4d\u7f6e\u4e0d\u53ef\u80fd\u6b63\u597d\u5904\u5728\u539f\u56fe\u50cf\u7684\u67d0\u4e00\u4e2a\u50cf\u7d20\u4f4d\u7f6e\u4e0a(\u5373 $P $\u70b9 \u7684\u5750\u6807\u901a\u5e38\u90fd\u4e0d\u4f1a\u6b63\u597d\u662f\u6574\u6570)
\u5bfb\u627e\u4e0e \\(P\\) \u70b9\u6700\u63a5\u8fd1\u7684\u50cf\u7d20\\(Q\\)\uff0c\u628a \\(Q\\) \u70b9\u7684\u50cf\u7d20\u503c\u4f5c\u4e3a\u65b0\u56fe\u50cf\u4e2d \\(P\u2019\\)\u70b9\u7684 \u50cf\u7d20\u503c\u3002
\\(Steps\\) \\((x^{'},y^{'})\\Rightarrow{\\text{Inverse Transtion}}(x,y)\\\\ \\Rightarrow{\\text{rounding operation}}(x_{int},y_{int})\\\\ \\Rightarrow{\\text{assign value}}I_{new}(x^{'},y^{'})=I_{old}(x_{int},y_{int})\\\\\\)
\u5f53\u56fe\u50cf\u4e2d\u5305\u542b\u660e\u663e\u7684\u51e0\u4f55\u7ed3\u6784\u65f6\uff0c\u7ed3\u679c\u5c06\u4e0d\u592a\u5149\u6ed1\u8fde\u7eed\uff0c\u4ece\u800c\u5728\u56fe\u50cf\u4e2d\u4ea7\u751f\u4eba\u4e3a\u7684\u75d5\u8ff9
\u5728\u4e00\u7ef4\u60c5\u51b5\u4e0b\uff0c\u5df2\u77e5x1\u548cx2\u5904\u7684\u7070\u5ea6\u503c\u5206\u522b\u4e3ag1\u548cg2\uff0c\u5219x3\u5904\u7684\u7070\u5ea6\u503cg3\u4e3a\uff1a $$ g_3 = \\frac{g_2-g_1}{x_2-x_1}(x_3-x_1)+g1 $$
\u5728\u4e8c\u7ef4\u60c5\u51b5\u4e0b\uff0c\u79f0\u4e3a\u53cc\u7ebf\u6027\u63d2\u503c
\u6ce8\uff1a\u53ef\u7531\u4e00\u7ef4\u7684\u60c5\u51b5\u63a8\u5bfc\u800c\u6765\uff0c\u5f97\u5230\u53cc\u7ebf\u6027\u65b9\u7a0bg(x,y)\u4e3a $$ g(x,y)=\\frac{(y_1-y)(x_1-x)}{(y_1-y_0)(x_1-x_0)}g(0,0)+\\frac{(y_1-y)(x-x_0)}{(y_1-y_0)(x_1-x_0)}g(1,0)+\\frac{(y-y_0)(x_1-x)}{(y_1-y_0)(x_1-x_0)}g(0,1)+\\frac{(y-y_0)(x-x_0)}{(y_1-y_0)(x_1-x_0)}g(1,1) $$ \u5373\u6ee1\u8db3\\(g(x,y)=ax+by+cxy+d\\)\u7684\u5f62\u5f0f
\u5df2\u77e5\u56fe\u50cf\u7684\u6b63\u65b9\u5f62\u7f51\u683c\u4e0a\u56db\u4e2a\u70b9A\u3001B\u3001C\u3001D\u7684\u7070\u5ea6\uff0c\u6c42P\u70b9\u7684\u7070\u5ea6:
\u5f84\u5411\u57fa\u51fd\u6570\\(\\phi(x)\\)
\u6700\u5e38\u89c1\u4e3a\u9ad8\u65af\u51fd\u6570\\(\u03c6(r)=e^{\\frac{\u2212r^2}{2\u03c3^2}}\\)
Multiquadrics:\\(\\phi(r)=\\sqrt{1+\\frac{r^2}{\\sigma^2}}\\)
Linear: \\(\\phi(r)=r\\)
Cubic:\\(\\phi(r)=r^3\\)
\u2026\u2026
\u63d2\u503c\u51fd\u6570 \\(\\hat{f}(x)=\\sum_{i=1}^Nw_i\\phi(||x-x_i||)\\) where \\(w_i=\\frac{\\phi(|x-c_i|)}{\\sum_{i=1}^n\\phi(|x-c_i|)}\\)
"},{"location":"DIP/feature/feature/#feature-detection","title":"Feature detection","text":""},{"location":"DIP/feature/feature/#feature-detection-the-math","title":"Feature detection: the math","text":"
Consider shifting the window \\(W\\) by \\((u,v)\\)
\\(\\begin{align*}Error(u,v)&=\\sum_{(x,y)\\in W}[I(x+u,y+v)-I(x,y)]^2\\\\ &\\approx\\sum_{(x,y)\\in W}[I(x,y)+\\frac{\\partial I}{\\partial x}u+\\frac{\\partial I}{\\partial y}v-I(x,y)]^2\\\\ &\\approx\\sum_{(x,y)\\in W}[u,v]\\begin{bmatrix}I_x^2&I_xI_y\\\\I_yI_x&I_y^2\\end{bmatrix}\\begin{bmatrix}u\\\\v\\end{bmatrix} \\\\ \\end{align*}\\)
We can find these directions by looking at the eigenvectors of H \\((\\begin{bmatrix}I_x^2&I_xI_y\\\\I_yI_x&I_y^2\\end{bmatrix})\\)
Eigenvalues and eigenvectors of H
Since \\(H\\) is symmetric ,so \\(H=R^{-1}\\begin{bmatrix}\\lambda_1&0\\\\0&\\lambda_2\\end{bmatrix}R\\)
\\(f=\\frac{\\lambda_1\\lambda_2}{\\lambda_1+\\lambda_2}=\\frac{determinant(H)}{trace(H)}\\)
Rotation Invariance
Partial Invariance to additive and multiplicative intensity changes
Not invariance to scaling. !!!
We define the characteristic scale as the scale that produces peak of Laplacian response
Stage 1: Initialization--Multiscale Harris Corner Detection
Stage 2: Scale Selection Based on Laplacian
The Laplacian is an operator used to detect edges and texture variations in an image by computing the second derivative at each point.
In the context of scale selection, the Laplacian serves to measure the changes in the image at different scales.
The optimal scale for a keypoint is where the maximum edge response occurs, indicating that the details of the keypoint are most pronounced at that scale.
For the same corner, even with changes in scale, the corner remains detectable, indicating robustness in terms of repeatability.
The content within the feature scale range of the same corner in images of different sizes should be consistent.
Therefore, the pixel locations within the feature scale range of corners in images of varying scales are proportional to the scale.(\u4e0d\u540c\u5c3a\u5bf8\u56fe\u7247\u4e2d\u7684\u76f8\u540c\u89d2\u70b9\u7684\u7279\u5f81\u5c3a\u5ea6\u8303\u56f4\u4e2d\u7684\u5185\u5bb9\u8981\u76f8\u540c\uff0c\u56e0\u6b64\uff0c\u4e0d\u540c\u5c3a\u5ea6\u7684\u56fe\u7247\u7684\u89d2\u70b9\u7684\u7279\u5f81\u5c3a\u5ea6\u8303\u56f4\u5185\u7684\u50cf\u7d20\u70b9\u4e0e\u5c3a\u5ea6\u6210\u6bd4\u4f8b\u5173\u7cfb\u3002)
"},{"location":"DIP/feature/feature/#local-extrema-detection","title":"Local Extrema Detection","text":"Let's illustrate the construction process of a Gaussian pyramid with a simple example. We assume that each group contains S layers, and the scale differences within each group are achieved through downsampling by a factor of 2 and using different Gaussian blur parameters \u03c3.
This process can be repeated iteratively, and each round of Gaussian blur and downsampling constructs one level of the pyramid. Within each group, we have S images at different scales (determined by different Gaussian blur parameters), and each image, obtained by downsampling, aligns with the corresponding image from the previous group in terms of scale.
This ensures that the construction of the pyramid is \"bottom-up,\" reflecting the features of the image at different scales.
Based on the collected images we detect their interest points with Difference of Gaussian, and extract the SIFT descriptors.
The extracted descriptors are then clustered to generate the vocabulary tree with the hierarchical k-means clustering. In this experiment we generated a visual vocabulary containing 32357 visual words
"},{"location":"DIP/feature/feature/#surf-detectors-and-descriptors","title":"SURF detectors and descriptors","text":"Reference to https://medium.com/@deepanshut041/introduction-to-surf-speeded-up-robust-features-c7396d6e7c4e
Basic Hessian matrix approximation.
"},{"location":"DIP/feature/feature/#aintegral-images","title":"a.Integral images","text":"The Integral Image or Summed-Area Table was introduced in 1984. The Integral Image is used as a quick and effective way of calculating the sum of values (pixel values) in a given image \u2014 or a rectangular subset of a grid (the given image). It can also, or is mainly, used for calculating the average intensity within a given image.
"},{"location":"DIP/feature/feature/#bhessian-matrix-based-interest-points","title":"b.Hessian matrix-based interest points","text":"\\(SURF\\) uses the Hessian matrix because of its good performance in computation time and accuracy. Rather than using a different measure for selecting the location and the scale (Hessian-Laplace detector), surf relies on the determinant of the Hessian matrix for both.
For adapt to any scale, we filtered the image by a Gaussian kernel, so given a point \\(X = (x, y)\\), the Hessian matrix \\(H(x, \u03c3)\\) in x at scale \u03c3 is defined as:
\\(H(\\vec{x},\\sigma)=\\begin{bmatrix}L_{xx}(x,\\sigma)&L_{xy}(x,\\sigma)\\\\ L_{xy}(x,\\sigma)&L_{yy}(x,\\sigma)\\end{bmatrix}\\)
where \\(L_{xx}(x, \u03c3)\\) is the convolution of the Gaussian second order derivative with the image \\(I\\) in point \\(x\\), and similarly for \\(L_{xy} (x, \u03c3)\\) and \\(L_{yy} (x, \u03c3)\\).
Gaussians are optimal for scale-space analysis but in practice, they have to be discretized and cropped. This leads to a loss in repeatability under image rotations around odd multiples of \\(\u03c0 /4\\). This weakness holds for Hessian-based detectors in general. Nevertheless, the detectors still perform well, and the slight decrease in performance does not outweigh the advantage of fast convolutions brought by the discretization and cropping.
After Lowe\u2019s success with LoG approximations(SIFT), SURF pushes the approximation(both convolution and second-order derivative) even further with box filters. These approximate second-order Gaussian derivatives and can be evaluated at a very low computational cost using integral images and independently of size, and this is part of the reason why SURF is fast.
\\(d^2L(x)/dx^2=(L(x+1)-L(x))-(L(x)-L(x-1))=-2*L(x)+L(x+1)+L(x-1)\\)
The \\(9 \u00d7 9\\) box filters in the above images are approximations for Gaussian second order derivatives with \u03c3 = 1.2.
We denote these approximations by \\(D_{xx}, D_{yy}, and D_{xy}\\).
Now we can represent the determinant of the Hessian (approximated) as:\\(det(H_{approx})=D_{xx}D_{yy}-(wD_{xy})^2\\) \\(w=0.9 (Bay\u2019s\\ suggestion)\\)
"},{"location":"DIP/feature/feature/#cscale-space-representation","title":"c.Scale-space representation","text":"SURF can be considered as a faster feature extraction method compared to SIFT.
It processes the original image directly using box filters at different scales, eliminating the need to build multiple layers of a pyramid like SIFT. This makes SURF computationally more efficient, especially in scenarios where large-scale image databases or real-time applications are involved.
If you use a 9x9 box filter on a 36x36 image, and you start from the center of the filter, moving the width of the filter at each step, you would end up with a 4x4 grid, deriving 16 new coordinate detection points. This is because 36 divided by 9 equals 4, thus, you can get 4 coordinate points in each direction.
However, this is just a logical understanding, and the actual results may vary due to the details of filter processing, such as edge handling, stride of the filter, and other factors.
\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u627e\u5230\u7684\u5c40\u90e8\u6700\u5927\u503c\u5982\u679c\u653e\u56de\u539f\u56fe\u7684\u5c3a\u5ea6\uff0c\u5176\u5b9e\u662f\u5bf9\u5e94\u7740\u4e00\u4e2a\u533a\u57df\u7684\uff0c\u90a3\u4e48\u5982\u4f55\u786e\u5b9a\u4e00\u4e2a\u7cbe\u786e\u7684\u5750\u6807\u5462\uff1f
To locate the precise position of feature points, we apply a Taylor expansion to the Hessian matrix at the local maximum points. This approximates a smooth surface depicting the variation of image intensity around the feature points. Then, we identify the peak of this smooth surface. Compared to the original discrete maximum point, the location of this peak is more accurate. Hence, we refer to it as the true location of the feature point.
"},{"location":"DIP/feature/feature/#interpolation","title":"Interpolation","text":"For each local maximum, need to interpolate to get true location (to overcome discretization effects)
"},{"location":"DIP/feature/feature/#2feature-description","title":"(2)Feature Description","text":""},{"location":"DIP/feature/feature/#orientation-assignment","title":"Orientation Assignment","text":"Also, the sampling step is scale dependent and chosen to be s, and the wavelet responses are computed at that current scale s.
Accordingly, at high scales the size of the wavelets is big. Therefore integral images are used again for fast filtering.
Now it\u2019s time to extract the descriptor
The first step consists of constructing a square region centered around the keypoint and oriented along the orientation we already got above. The size of this window is 20s.
Then the region is split up regularly into smaller 4 \u00d7 4 square sub-regions. For each sub-region, we compute a few simple features at 5\u00d75 regularly spaced sample points.
For reasons of simplicity, we call dx the Haar wavelet response in the horizontal direction and dy the Haar wavelet response in the vertical direction (filter size 2s).
To increase the robustness towards geometric deformations and localization errors, the responses dx and dy are first weighted with a Gaussian (\u03c3 = 3.3s) centered at the keypoint.
Then, the wavelet responses dx and dy are summed up over each subregion and form a first set of entries to the feature vector.
In order to bring in information about the polarity of the intensity changes, we also extract the sum of the absolute values of the responses, |dx| and |dy|.
Hence, each sub-region has a four-dimensional descriptor vector \\(\\vec{v}\\) for its underlying intensity structure \\(V = (\u2211 dx, \u2211 dy, \u2211|dx|, \u2211|dy|)\\).
This results in a descriptor vector for all 4\u00d74 sub-regions of length 64.
Procedure
A further refinement of matches.--RANdom SAmple Consensus
In short words, \\(RANSAC\\) fits \\(N\\) models using different random sample S of the whole available data each time. Then, for each model it evaluates its performance P (i.e. number of inliers or outliers) and chooses the best one. Note that RANSAC doesn\u2019t determine what method should you use to find model parameters.(least squares for example)
How many samples are needed?
Suppose \\(w\\) is fraction of inliers (points from line).
\\(n\\) points needed to define hypothesis (2 for lines)
\\(k\\) samples chosen.
Prob. that a single sample of n points is correct \\(w^n\\)
Prob. that all k samples fail is: \\((1-w^n)^k\\)
k-- iterations
RANSAC divides data into inliers and outliers and yields estimate computed from minimal set of inliers.Improve this initial estimate with estimation over all inliers (e.g. with standard least-squares minimization).
But this may change inliers, so alternate fitting with re-classification as inlier/outlier.
A voting strategy, the Hough transform, can handle high percentage of outliers
"},{"location":"DIP/feature/feature/#image-blending","title":"Image Blending","text":""},{"location":"DIP/feature/feature/#image-pyramids","title":"Image Pyramids","text":"An image pyramid can be constructed by repeatedly downsampling (or upsampling) an image and creating a set of images at different resolutions.
The resulting images are referred to as \u201clevels\u201d of the pyramid, with the highest resolution image at the top and the lowest resolution image at the bottom.
This type of pyramid is constructed by repeatedly applying a Gaussian blur filter to an image and downsampling it by a factor of two. The resulting images are smoother and have lower resolution than the original image because Gaussians are low pass filters.
"},{"location":"DIP/feature/feature/#laplacian-pyramid","title":"Laplacian Pyramid","text":"This type of pyramid is constructed by subtracting a downsampled version of an image from the original image.
\u2022 Build Laplacian pyramid for both images: \\(L_A, L_B\\).
\u2022 Build Gaussian pyramid for mask: \\(G\\).
\u2022 Build a combined Laplacian pyramid: \\(L(j) = G(j) LA(j) + (1-G(j)) LB(j).\\)
\u2022 Collapse \\(L\\) to obtain the blended image.
"},{"location":"DIP/filter/filter/","title":"image filter","text":""},{"location":"DIP/filter/filter/#_1","title":"\u6ee4\u6ce2","text":""},{"location":"DIP/filter/filter/#_2","title":"\u5377\u79ef","text":"
\\(L(x,y,\u03c3)=G(x,y,\u03c3)\u2217I(x,y)\\)
\u8fde\u7eed\u5f62\u5f0f\uff1a\\((f\u2217g)(n)=\\int_{-\\infty }^{\\infty}f(\\tau )g(n-\\tau)d\\tau\\)
\u79bb\u6563\u5f62\u5f0f\uff1a\\((f\u2217g)(n)=\\frac{1}{M}\\sum_{\\tau=0 }^{M-1}f(\\tau)g(n-\\tau)\\)
"},{"location":"DIP/filter/filter/#properties","title":"Properties","text":"\\(f(x)*h(x)=h(x)*f(x)\\\\ f*(g+h)=f*g+f*h\\\\f*(g*h)=(f*g)*h\\)
"},{"location":"DIP/filter/filter/#examples","title":"Examples","text":"Covers all the points that \\(h(t)\\) go through during the sliding operation.
In this example, the effective range of x is from 0 to 799.
"},{"location":"DIP/filter/filter/#_3","title":"\u57fa\u672c\u6982\u5ff5","text":"\u6ee4\u6ce2\u5668\u662f\u4e00\u4e2a\u5927\u5c0f\u4e3a \\(M\\times N\\) \u7684\u7a97\u53e3\uff0c\u5176\u4e2d\u7a97\u53e3\u4e2d\u7684\u5143\u7d20\u5bf9\u5e94\u4e8e\u539f\u59cb\u56fe\u50cf\u4e2d\u7684\u76f8\u5e94\u50cf\u7d20\u8fdb\u884c\u64cd\u4f5c\uff0c\u7ed3\u679c\u4fdd\u5b58\u4e3a\u65b0\u56fe\u50cf\u4e2d\u7684\u50cf\u7d20
\u6ee4\u6ce2\u4e5f\u88ab\u79f0\u4f5c:\u906e\u7f69 (mask)\u3001\u5185\u6838 (kernel)\u3001\u6a21\u677f (template) \u7b49\u3002\u6ee4\u6ce2\u5668\u4e2d\u7684\u5143\u7d20\u662f\u7cfb\u6570\u800c\u4e0d\u662f\u50cf\u7d20\u503c\uff0c\u5b83\u4eec\u8868\u793a\u5e94\u7528\u4e8e\u539f\u59cb\u56fe\u50cf\u4e2d\u50cf\u7d20\u7684\u6743\u91cd\u3002\u5728\u5f85\u5904\u7406\u56fe\u50cf\u4e2d\u9010\u70b9\u79fb\u52a8\u63a9\u6a21\uff0c\u5728\u6bcf\u4e00\u70b9(x,y)\u5904\uff0c\u6ee4\u6ce2\u5668\u5728\u8be5\u70b9\u7684\u54cd\u5e94\u901a\u8fc7\u5b9e\u73b0\u5b9a\u4e49\u7684\u5173\u7cfb\u6765\u8ba1\u7b97\u3002\u5bf9\u4e8e\u7ebf\u6027\u7a7a\u95f4\u6ee4\u6ce2\uff0c\u5176\u54cd\u5e94\u7531\u6ee4\u6ce2\u5668\u7cfb\u6570\u4e0e\u6ee4\u6ce2\u63a9\u6a21\u626b\u8fc7\u533a\u57df\u7684\u5bf9\u5e94\u50cf\u7d20\u503c\u7684\u4e58\u79ef\u4e4b\u548c\u7ed9\u51fa\u3002
\u901a\u5e38\uff0c\u63a9\u6a21\u7684\u957f\u5bbd\u90fd\u4e3a\u5947\u6570
\u56fe\u50cf\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u4f20\u8f93\u4fe1\u9053\u3001\u91c7\u6837\u7cfb\u7edf\u8d28\u91cf\u8f83\u5dee\uff0c\u6216\u53d7\u5404\u79cd\u5e72\u6270\u7684\u5f71\u54cd\uff0c\u800c\u9020\u6210\u56fe\u50cf\u6bdb\u7cd9\uff0c\u6b64\u65f6\uff0c\u5c31\u9700\u5bf9\u56fe\u50cf\u8fdb\u884c\u5e73\u6ed1\u5904\u7406\u3002\u5e73\u6ed1\u53ef\u4ee5\u6291\u5236\u9ad8\u9891\u6210\u5206\uff0c\u4f46\u4e5f\u4f7f\u56fe\u50cf\u53d8\u5f97\u6a21\u7cca\u3002 \u5e73\u6ed1\u7a7a\u95f4\u6ee4\u6ce2\u5668\u7528\u4e8e\u6a21\u7cca\u5904\u7406\u548c\u51cf\u5c11\u566a\u58f0\u3002\u6a21\u7cca\u5904\u7406\u7ecf\u5e38\u7528\u4e8e\u9884\u5904\u7406\uff0c\u4f8b\u5982\uff0c\u5728\u63d0\u53d6\u5927\u7684\u76ee\u6807\u4e4b\u524d\u53bb\u9664\u56fe\u50cf\u4e2d\u4e00\u4e9b\u7410\u788e\u7684\u7ec6\u8282\uff0c\u6865\u63a5\u76f4\u7ebf\u6216\u66f2\u7ebf\u7684\u7f1d\u9699\u3002
"},{"location":"DIP/filter/filter/#_4","title":"\u5e73\u6ed1\u7ebf\u6027\u7a7a\u95f4\u6ee4\u6ce2\u5668","text":"\u5e73\u6ed1\u7ebf\u6027\u7a7a\u95f4\u6ee4\u6ce2\u5668\u7684\u8f93\u51fa\u662f\u5305\u542b\u5728\u6ee4\u6ce2\u63a9\u6a21\u90bb\u57df\u5185\u50cf\u7d20\u7684\u7b80\u5355\u5e73\u5747\u503c\u3002\u56e0\u6b64\uff0c\u8fd9\u4e9b\u6ee4\u6ce2\u5668\u4e5f\u79f0\u4e3a\u5747\u503c\u6ee4\u6ce2\u5668\u3002
\u7b80\u5355\u5e73\u5747(simple mean)\uff0c\u8868\u793a\u7a97\u53e3\u4e2d\u6bcf\u4e00\u4e2a\u50cf\u7d20\u5bf9\u54cd\u5e94\u7684\u8d21\u732e\u662f\u4e00\u6837\u7684, \u6ee4\u6ce2\u7a97\u53e3\uff1a\\(\\frac{1}{9} \\times \\begin{bmatrix}1&1&1 \\\\ 1&1&1\\\\ 1&1&1\\end{bmatrix}\\)
\u52a0\u6743\u5e73\u5747(weighted mean)\uff0c\u8868\u793a\u7a97\u53e3\u4e2d\u7684\u50cf\u7d20\u5bf9\u76f8\u5e94\u7684\u8d21\u732e\u6709\u5927\u5c0f\u4e4b\u5206\uff0c\u6ee4\u6ce2\u7a97\u53e3\uff1a\\(\\frac{1}{16} \\times \\begin{bmatrix}1&2&1 \\\\ 2&4&2\\\\ 1&2&1\\end{bmatrix}\\)
General Equation
\u5176\u4e2d\uff0c\u6ee4\u6ce2\u5668\u5927\u5c0f\u4e3a\\((2a+1) \u00d7(2b+1)\\)\uff0c\\(w\\)\u4e3a\u6ee4\u6ce2\u5668\uff0c\\(f\\)\u4e3a\u8f93\u5165\u56fe\u50cf\uff0c\\(g\\)\u4e3a\u8f93\u51fa\u56fe\u50cf\u3002
\u5f53\u63a9\u6a21\u6bd4\u8f83\u5c0f\u65f6\uff0c\u53ef\u4ee5\u89c2\u5bdf\u5230\u5728\u6574\u5e45\u56fe\u50cf\u4e2d\u6709\u8f7b\u5fae\u7684\u6a21\u7cca
\u5f53\u63a9\u6a21\u5927\u5c0f\u589e\u52a0\uff0c\u6a21\u7cca\u7a0b\u5ea6\u4e5f\u968f\u4e4b\u589e\u52a0
\u7edf\u8ba1\u6ee4\u6ce2\u5668\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u7684\u7a7a\u95f4\u6ee4\u6ce2\u5668\uff0c\u5b83\u7684\u54cd\u5e94\u662f\u57fa\u4e8e\u7a97\u53e3\u5185\u56fe\u50cf\u533a\u57df\u4e2d\u50cf\u7d20\u503c\u7684\u6392\u5e8f\uff0c\u7531\u7edf\u8ba1\u6392\u5e8f\u7ed3\u679c\u51b3\u5b9a\u7684\u503c\u4ee3\u66ff\u4e2d\u5fc3\u50cf\u7d20\u7684\u503c\u3002
\u5bf9\u5904\u7406\u8109\u51b2\u566a\u58f0\uff08\u4e5f\u79f0\u4e3a\u6912\u76d0\u566a\u58f0\uff09\u975e\u5e38\u6709\u6548\uff0c\u56e0\u4e3a\u8fd9\u79cd\u566a\u58f0\u662f\u4ee5\u9ed1\u767d\u70b9\u53e0\u52a0\u5728\u56fe\u50cf\u4e0a\u7684\u3002
\u4e3a\u4e86\u5bf9\u4e00\u5e45\u56fe\u50cf\u4e0a\u7684\u67d0\u4e2a\u70b9\u4f5c\u4e2d\u503c\u6ee4\u6ce2\u5904\u7406\u3002\u5fc5\u987b\u5148\u5c06\u63a9\u6a21\u5185\u6b32\u6c42\u7684\u50cf\u7d20\u53ca\u5176\u90bb\u57df\u7684\u50cf\u7d20\u503c\u6392\u5e8f\uff0c\u786e\u5b9a\u51fa\u4e2d\u503c\uff0c\u5e76\u5c06\u4e2d\u503c\u8d4b\u4e88\u8be5\u50cf\u7d20\u70b9
\u4e2d\u503c\\(\u03be\\) --\u6570\u503c\u96c6\u5408\u4e2d\uff0c\u6709\u4e00\u534a\u6570\u503c\u5c0f\u4e8e\u6216\u7b49\u4e8e\\(\u03be\\)\uff0c\u8fd8\u6709\u4e00\u534a\u5927\u4e8e\u6216\u7b49\u4e8e\\(\u03be\\)
\u4e3a\u4e86\u5bf9\u4e00\u5e45\u56fe\u50cf\u4e0a\u7684\u67d0\u4e2a\u70b9\u4f5c\u4e2d\u503c\u6ee4\u6ce2\u5904\u7406,\u5fc5\u987b\u5148\u5c06\u63a9\u6a21\u5185\u6b32\u6c42\u7684\u50cf\u7d20\u53ca\u5176\u90bb\u57df\u7684\u50cf\u7d20\u503c\u6392\u5e8f\uff0c\u786e\u5b9a\u51fa\u4e2d\u503c\uff0c\u5e76\u5c06\u4e2d\u503c\u8d4b\u4e88\u8be5\u50cf\u7d20\u70b9
\u62c9\u666e\u62c9\u65af\u9510\u5316\u662f\u4e00\u79cd\u56fe\u50cf\u5904\u7406\u6280\u672f\uff0c\u65e8\u5728\u7a81\u51fa\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\u548c\u7ec6\u8282\uff0c\u4f7f\u56fe\u50cf\u770b\u8d77\u6765\u66f4\u52a0\u9510\u5229\u3002\u8fd9\u79cd\u6280\u672f\u4f7f\u7528\u62c9\u666e\u62c9\u65af\u7b97\u5b50\uff08Laplacian operator\uff09\u6216\u62c9\u666e\u62c9\u65af\u6ee4\u6ce2\u5668\u5bf9\u56fe\u50cf\u8fdb\u884c\u5377\u79ef\u64cd\u4f5c\uff0c\u4ee5\u589e\u5f3a\u56fe\u50cf\u4e2d\u7684\u9ad8\u9891\u4fe1\u606f\u3002
"},{"location":"DIP/filter/filter/#1","title":"(1)\u62c9\u666e\u62c9\u65af\u7b97\u5b50","text":"\u4e00\u9636\u5fae\u5206\u7b97\u5b50:\\(\\frac{\\partial{f}}{\\partial{x}}\\)$= f(x + 1) \u2212 f(x) $
\u4e8c\u9636\u5fae\u5206\u7b97\u5b50:\\(\\frac{\\partial^2{f}}{\\partial{x}^2}\\)\\(= f(x + 1) +f(x-1)\u2212 2f(x)\\)
\u5bf9\u6574\u5e45\u56fe\u50cf\u8ba1\u7b97\u68af\u5ea6\u65f6\uff0c\u8fd0\u7b97\u91cf\u4f1a\u5f88\u5927\uff0c\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u5e38\u7528\u7edd\u5bf9\u503c\u4ee3\u66ff\u5e73\u65b9\u4e0e\u5e73\u65b9\u6839\u8fd0\u7b97\u8fd1\u4f3c\u6c42\u68af\u5ea6\u7684\u6a21\u503c \\(\\triangledown f \\approx |G_x|+|G_y|\\)
\u79bb\u6563\u5f62\u5f0f
\\(\\ \\ \\ \\triangledown^2f=f(x+1,y)+f(x-1,y)+f(x,y+1),F(x,y-1)-4f(x,y)\\)
\u5982\u679c\u8003\u8651\u5bf9\u89d2\u7ebf\u5143\u7d20
\u7531\u4e8e\u62c9\u666e\u62c9\u65af\u7b97\u5b50\u662f\u4e00\u79cd\u5bfc\u6570\u7b97\u5b50\uff0c\u5b83\u7684\u4f7f\u7528\u4f1a\u7a81\u51fa\u663e\u793a\u56fe\u50cf\u4e2d\u7684\u5f3a\u5ea6\u4e0d\u8fde\u7eed\u6027\uff0c\u800c\u4e0d\u5f3a\u8c03\u5177\u6709\u7f13\u6162 \u53d8\u5316\u7684\u5f3a\u5ea6\u7ea7\u522b\u7684\u533a\u57df\u3002\u8fd9\u5c06\u503e\u5411\u4e8e\u751f\u6210\u5177\u6709\u7070\u8272\u8fb9\u7f18\u7ebf\u548c\u5176\u4ed6\u4e0d\u8fde\u7eed\u6027\u7684\u56fe\u50cf\uff0c\u6240\u6709\u8fd9\u4e9b\u56fe\u50cf\u90fd\u53e0\u52a0\u5728\u9ed1\u6697\u3001\u65e0\u7279\u5f81\u7684\u80cc\u666f\u4e0a\u3002\u53ea\u9700\u5c06\u62c9\u666e\u62c9\u65af\u56fe\u50cf\u6dfb\u52a0\u5230\u539f\u59cb\u56fe\u50cf\u4e2d\uff0c\u5373\u53ef\u201c\u6062\u590d\u201d\u80cc\u666f\u7279\u5f81\uff0c\u540c\u65f6\u4ecd\u4fdd\u6301\u62c9 \u666e\u62c9\u65af\u56fe\u50cf\u7684\u9510\u5316\u6548\u679c
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f7f\u7528\u7684\u5b9a\u4e49\u5177\u6709\u8d1f\u4e2d\u5fc3\u7cfb\u6570\uff0c\u5219\u6211\u4eec\u51cf\u53bb\u800c\u4e0d\u662f\u6dfb\u52a0\u62c9\u666e\u62c9\u65af\u56fe\u50cf\u4ee5\u83b7\u5f97\u9510\u5316\u7ed3\u679c
\u56e0\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u62c9\u666e\u62c9\u65af\u51fd\u6570\u8fdb\u884c\u56fe\u50cf\u9510\u5316\u7684\u57fa\u672c\u65b9\u6cd5\u662f:
\\(g(x,y)=\\left\\{\\begin{array}{ll}f(x,y)-\\triangledown^2f(x,y)& if\\ the\\ center\\ of\\ the\\ mask\\ is\\ negative \\\\ f(x,y)+\\triangledown^2f(x,y)& if\\ the\\ center\\ of\\ the\\ mask\\ is\\ positive \\end{array} \\right.\\)
\u9ad8\u65af\u6ee4\u6ce2\u662f\u4e00\u79cd\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u6ee4\u6ce2\u5668\uff0c\u5176\u4e3b\u8981\u76ee\u7684\u662f\u5bf9\u56fe\u50cf\u8fdb\u884c\u5e73\u6ed1\u5904\u7406\u3002\u5b83\u7684\u540d\u79f0\u6765\u6e90\u4e8e\u6240\u4f7f\u7528\u7684\u6ee4\u6ce2\u6838\uff08\u5377\u79ef\u6838\uff09\u662f\u4e00\u4e2a\u4e8c\u7ef4\u9ad8\u65af\u51fd\u6570\u3002\u9ad8\u65af\u6ee4\u6ce2\u5728\u53bb\u9664\u56fe\u50cf\u4e2d\u7684\u566a\u58f0\u3001\u6a21\u7cca\u5904\u7406\u3001\u8fb9\u7f18\u68c0\u6d4b\u7b49\u65b9\u9762\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002
\u9ad8\u65af\u6ee4\u6ce2\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5bf9\u56fe\u50cf\u4e2d\u7684\u6bcf\u4e2a\u50cf\u7d20\u70b9\u8fdb\u884c\u52a0\u6743\u5e73\u5747\uff0c\u5176\u4e2d\u6743\u503c\u662f\u7531\u4e8c\u7ef4\u9ad8\u65af\u51fd\u6570\u786e\u5b9a\u7684\u3002\u8fd9\u610f\u5473\u7740\u79bb\u4e2d\u5fc3\u50cf\u7d20\u8d8a\u8fdc\u7684\u50cf\u7d20\u5bf9\u4e2d\u5fc3\u50cf\u7d20\u7684\u5f71\u54cd\u8d8a\u5c0f\uff0c\u8fd9\u79cd\u6743\u503c\u7684\u5206\u5e03\u7b26\u5408\u9ad8\u65af\u5206\u5e03\u3002\u901a\u8fc7\u8c03\u6574\u9ad8\u65af\u51fd\u6570\u7684\u6807\u51c6\u5dee\\(\u03c3\\)\uff0c\u53ef\u4ee5\u63a7\u5236\u6743\u503c\u7684\u5206\u5e03\u8303\u56f4\uff0c\u4ece\u800c\u8c03\u6574\u6ee4\u6ce2\u6548\u679c\u3002
"},{"location":"DIP/filter/filter/#1_1","title":"(1)\u6570\u5b66\u8868\u8fbe","text":"\u5728\u56fe\u50cf\u6ee4\u6ce2\u4e2d,\u4e00\u822c\u60c5\u51b5\u4e0b\\(\u03bc_x = \u03bc_y = 0\\)\u56e0\u6b64\u4e8c\u7ef4\u9ad8\u65af\u51fd\u6570\u53ef\u8868\u793a\u4e3a\\(f(x,y)=\\frac{1}{2\\pi\\sigma^2}e^{-\\frac{x^2+y^2}{2\\sigma^2}}\\)
"},{"location":"DIP/filter/filter/#2_1","title":"(2)\u6ee4\u6ce2\u8fc7\u7a0b","text":"\u5c06\u5404\u4e2a\u4f4d\u7f6e\u7684\u5750\u6807\u4ee3\u5165\u5230\u9ad8\u65af\u51fd\u6570\u4e2d,\u5f97\u5230\u7684\u503c\u5c31\u662f\u521d\u6b65\u7684\u9ad8\u65af\u6838
\u5229\u7528\u9ad8\u65af\u51fd\u6570\u8fdb\u884c\u5377\u79ef(\u9ad8\u65af\u6ee4\u6ce2)\u7684\u8fc7\u7a0b\u5177\u6709\u53ef\u5206\u79bb\u6027\u3002
"},{"location":"DIP/filter/filter/#_7","title":"\u53cc\u8fb9\u6ee4\u6ce2","text":"\u53cc\u8fb9\u6ee4\u6ce2\uff08Bilateral filter\uff09\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u7684\u6ee4\u6ce2\u65b9\u6cd5\uff0c\u662f\u7ed3\u5408\u56fe\u50cf\u7684\u7a7a\u95f4\u90bb\u8fd1\u5ea6\u548c\u50cf\u7d20\u503c\u76f8\u4f3c\u5ea6\u7684\u4e00\u79cd\u6298\u8877\u5904\u7406\uff0c\u540c\u65f6\u8003\u8651\u7a7a\u57df\u4fe1\u606f\u548c\u7070\u5ea6\u76f8\u4f3c\u6027\uff0c\u8fbe\u5230\u4fdd\u8fb9\u53bb\u566a\u7684\u76ee\u7684\u3002\u5177\u6709\u7b80\u5355\u3001\u975e\u8fed\u4ee3\u3001\u5c40\u90e8\u7684\u7279\u70b9
\u53cc\u8fb9\u6ee4\u6ce2\u5668\u7684\u597d\u5904\u662f\u53ef\u4ee5\u505a\u8fb9\u7f18\u4fdd\u5b58\uff08edge preserving\uff09\uff0c\u4e00\u822c\u7528\u9ad8\u65af\u6ee4\u6ce2\u53bb\u964d\u566a\uff0c\u4f1a\u8f83\u660e\u663e\u5730\u6a21\u7cca\u8fb9\u7f18\uff0c\u5bf9\u4e8e\u9ad8\u9891\u7ec6\u8282\u7684\u4fdd\u62a4\u6548\u679c\u5e76\u4e0d\u660e\u663e\u3002\u53cc\u8fb9\u6ee4\u6ce2\u5668\u987e\u540d\u601d\u4e49\u6bd4\u9ad8\u65af\u6ee4\u6ce2\u591a\u4e86\u4e00\u4e2a\u9ad8\u65af\u65b9\u5deesigma\uff0dd\uff0c\u5b83\u662f\u57fa\u4e8e\u7a7a\u95f4\u5206\u5e03\u7684\u9ad8\u65af\u6ee4\u6ce2\u51fd\u6570\uff0c\u6240\u4ee5\u5728\u8fb9\u7f18\u9644\u8fd1\uff0c\u79bb\u7684\u8f83\u8fdc\u7684\u50cf\u7d20\u4e0d\u4f1a\u592a\u591a\u5f71\u54cd\u5230\u8fb9\u7f18\u4e0a\u7684\u50cf\u7d20\u503c\uff0c\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86\u8fb9\u7f18\u9644\u8fd1\u50cf\u7d20\u503c\u7684\u4fdd\u5b58\u3002\u4f46\u662f\u7531\u4e8e\u4fdd\u5b58\u4e86\u8fc7\u591a\u7684\u9ad8\u9891\u4fe1\u606f\uff0c\u5bf9\u4e8e\u5f69\u8272\u56fe\u50cf\u91cc\u7684\u9ad8\u9891\u566a\u58f0\uff0c\u53cc\u8fb9\u6ee4\u6ce2\u5668\u4e0d\u80fd\u591f\u5e72\u51c0\u7684\u6ee4\u6389\uff0c\u53ea\u80fd\u591f\u5bf9\u4e8e\u4f4e\u9891\u4fe1\u606f\u8fdb\u884c\u8f83\u597d\u7684\u6ee4\u6ce2
Nonlinear
Complex, spatially varying kernels
Cannot be pre-computed
Brute-force implementation is slow > 10min
\\(h(x)=k_r^{\u22121}(x)\u222b^{+\u221e}_{\u2212\u221e}\u222b^{+\u221e}_{\u2212\u221e}f(\u03be)c(\u03be,x)s(f(\u03be),f(x))d\u03be\\)
\\(c(\u03be,x)=\\frac{1}{2\\pi\\sigma_s^2}e^{-\\frac{||\u03be-x||^2}{2\\sigma_s^2}}\\)
\\(s(f(\u03be),f(x))=\\frac{1}{2\\pi\\sigma_r^2}e^{-\\frac{||f(\u03be)-f(x)||^2}{2\\sigma_r^2}}\\)
\\(Which\\) \\(is\\) \\(BF[I]p=\\frac{1}{W_p}\\sum_{q\\in S}G_{\\sigma_s}(||p-q||)G_{\\sigma_r}(|O_p-I_q|)I_q\\) !!!
\\(\\frac{1}{W_p}\\)\u4e3a\u5f52\u4e00\u5316\u56e0\u5b50
\\(\u03c3_s\\) \u8d8a\u5c0f\uff0c\u4e2d\u5fc3\u70b9\u6743\u91cd\u8d8a\u5927\uff0c\u5468\u56f4\u70b9\u6743\u91cd\u8d8a\u5c0f\uff0c\u5bf9\u56fe\u50cf\u7684\u6ee4\u6ce2\u4f5c\u7528\u8d8a\u5c0f\uff0c\u8d8b\u4e8e\u96f6\u65f6\uff0c\u8f93\u51fa\u7b49\u540c\u4e8e\u539f\u56fe.
\\(\u03c3_r\\) \u8d8a\u5c0f\uff0c\u8fb9\u7f18\u8d8a\u6e05\u6670\uff0c\u6781\u9650\u60c5\u51b5\u4e3a \\(\u03c3_r\\) \u65e0\u9650\u63a5\u8fd1 0\uff0c\u503c\u57df\u7cfb\u6570\u9664\u4e86\u4e2d\u5fc3\u4f4d\u7f6e\uff0c\u5176\u4ed6\u8fd1\u4f3c\u4e3a 0(\u63a5\u8fd1 \\(e_\u221e = 0\\))\uff0c\u4e0e\u9ad8\u65af\u6a21\u677f(\u7a7a\u95f4\u57df\u6a21\u677f)\u76f8\u4e58\u8fdb\u884c\u6ee4\u6ce2\u7684\u7ed3\u679c\u7b49\u6548\u4e8e\u539f\u56fe\u50cf.
"},{"location":"DIP/filter/filter/#how-to-set-the-parameters","title":"How to Set the Parameters","text":"Depends on the application. For instance:
e.g., 2% of image diagonal
e.g., mean or median of image gradients
independent of resolution and exposure
Iterating the Bilateral Filter
Small spatial sigma (e.g. 7x7 window)
Adapt intensity sigma to noise level
\u5377\u79ef\u5b9a\u7406\u544a\u8bc9\u6211\u4eec\uff0c\u5728\u9891\u57df\u4e2d\uff0c\u5377\u79ef\u7b49\u4ef7\u4e8e\u76f8\u4e58\u3002\u6240\u4ee5\uff0c\u5728\u9891\u57df\u4e2d\uff0c\u56fe\u50cf F(u,v*) \u4e0e\u6ee4\u6ce2\u6838 Hb(u,v)\u7684\u5377\u79ef\u7ed3\u679c G(u,v)\u53ef\u4ee5\u8868\u793a\u4e3a\uff1a
\\(G(u,v)=F(u,v)\u22c5H_b(u,v)\\)
\u53c2\u8003 https://blog.csdn.net/xijuezhu8128/article/details/111304006
\u5176\u57fa\u672c\u601d\u60f3\u5c31\u662f\u5c06\u975e\u7ebf\u6027\u7684\u53cc\u8fb9\u6ee4\u6ce2\u6539\u6210\u53ef\u5206\u79bb\u7684\u7ebf\u6027\u64cd\u4f5c\u548c\u975e\u7ebf\u6027\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u539f\u6765\u7684\u53cc\u8fb9\u6ee4\u6ce2\u5728\u56fe\u50cf\u4e0d\u540c\u4f4d\u7f6e\u5e94\u7528\u4e0d\u540c\u7684\u6743\u91cd\uff0c\u4e5f\u5c31\u662f\u4f4d\u79fb\u6539\u53d8\u5377\u79ef\uff0c\u4ed6\u4eec\u901a\u8fc7\u589e\u52a0\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u4e5f\u5c31\u662f\u5c06\u7070\u5ea6\u503c\u4f5c\u4e3a\u4e00\u4e2a\u65b0\u7684\u7ef4\u5ea6\uff0c\u5c06\u53cc\u8fb9\u6ee4\u6ce2\u8868\u8fbe\u62103D\u7a7a\u95f4\u4e2d\u7684\u7ebf\u6027\u4f4d\u79fb\u4e0d\u53d8\u5377\u79ef\uff0c\u6700\u540e\u518d\u6267\u884c\u975e\u7ebf\u6027\u7684\u5f52\u4e00\u5316\u64cd\u4f5c\u3002
"},{"location":"DIP/filter/filter/#derivation","title":"Derivation","text":"(1\uff09 \u9996\u5148\u5c06\u539f\u59cb\u53cc\u8fb9\u6ee4\u6ce2\u516c\u5f0f\u7b49\u5f0f\u5de6\u53f3\u7686\u5de6\u4e58\\(W_p^b\\)\uff0c\u5e76\u5c06\u4e24\u4e2a\u516c\u5f0f\u901a\u8fc7\u4e8c\u7ef4\u5411\u91cf\u8868\u8fbe\u6210\u5355\u4e2a\u516c\u5f0f\uff1a
\uff082\uff09\u7b49\u5f0f\u53f3\u4fa7\u4e58\u4ee5\\(W_q\\)\uff0c\\(W_q\\)=1\uff1a
\u4e0a\u56fe\u4e2d\uff0c\u5982\u679c\u5ffd\u7565\u7b49\u5f0f\u53f3\u4fa7\u7684\\(G_{\\sigma_r}(|I_p-I_q|)\\)\u8fd9\u4e00\u9879\uff0c\u90a3\u4e48\u8be5\u7b49\u5f0f\u8868\u8fbe\u7684\u5c31\u662f\u7ecf\u5178\u7684\u9ad8\u65af\u6ee4\u6ce2
\u53ef\u4ee5\u7b80\u5199\u4e3a\u4ee5\u4e0b\u5377\u79ef\u7684\u5f62\u5f0f\uff1a\\(\\begin{pmatrix}W^bI^b\\\\W^b\\end{pmatrix}=G_{\\sigma_r}*\\begin{pmatrix}WI\\\\W\\end{pmatrix}\\)
\uff083\uff09\u589e\u7ef4\uff0c\u589e\u52a0\u5f3a\u5ea6\u7ef4\uff08\u4e5f\u5c31\u662f\u7070\u5ea6\u503c\uff09\uff1a
\u4f7f\u7528\\(Kronecker\\)\u51fd\u6570\uff0c\\(\\delta\\)\u53ea\u5728\\(0\\)\u70b9\u4e3a\\(1\\)\uff0c\u5176\u4ed6\u4e3a0
THUS
\u4e0a\u5f0f\u53ef\u4ee5\u8868\u8fbe\u4e3a\u5728\u70b9\uff08\\(p\\), \\(I_p\\)\uff09\u4f4d\u7f6e\u5904\u4e09\u7ef4\u5377\u79ef\u7684\u5f62\u5f0f\uff1a
\uff085\uff09\u6574\u4e2a\u6d41\u7a0b\uff1a\u7ebf\u6027\u5377\u79ef+\u975e\u7ebf\u6027\u5f52\u4e00\u5316
\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u4e3b\u8981\u7684\u52a0\u901f\u6765\u81ea\u4e8e\u5728\u9891\u57df\u4e2d\u6267\u884c\u5377\u79ef\u64cd\u4f5c\u3002\u9891\u57df\u5377\u79ef\u901a\u5e38\u6bd4\u65f6\u57df\u5377\u79ef\u66f4\u5feb\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u5feb\u901f\u5085\u91cc\u53f6\u53d8\u6362\uff08FFT\uff09\u7b49\u9ad8\u6548\u7b97\u6cd5\u65f6\u3002
"},{"location":"DIP/filter/filter/#guided-filter","title":"Guided Filter","text":"\u5f15\u5165 guided image \\(I\\)
\u4fdd\u6301\u68af\u5ea6\uff0cso \\(q_i(output\\ image)=aI_i(guided\\ image)+b\\)
Use Lagrange multiplier method , we can get \\(a=\\frac{cov(I,p)}{var(I)+\\epsilon} \\ \\ \\ b=\\bar{p}-a\\bar{I}\\)
\u5bf9\u8fb9\u7f18\u7684\u5b9a\u4e49\u4e0d\u6e05\u6dc5\uff0c\u800c\u4e14\u8fb9\u7f18\u662f context-dependent \u7684\u3002\u8089\u773c\u4e2d\u7684\u8fb9\u754c\uff0c\u53ef\u80fd\u4e0d\u88ab\u8ba4\u4e3a\u662f\u8fb9\u754c\uff0c\u6700\u7ec8\u8fd8\u662f\u4f1a\u51fa\u73b0 halo \u7684\u73b0\u8c61\u3002
"},{"location":"DIP/filter/filter/#sparse-norm-filter","title":"Sparse Norm Filter","text":"https://note.hobbitqia.cc/dip/dip9/#core-algorithm
"},{"location":"DIP/filter/filter/#p1","title":"p=1","text":"\\(l^1\\) norm filter is the median filter(\u4e2d\u503c\u6ee4\u6ce2)
\\(l^2\\) norm mean filter(\u5747\u503c\u6ee4\u6ce2)
"},{"location":"DIP/morph/morph/","title":"Image morphing","text":""},{"location":"DIP/morph/morph/#morph","title":"Morph","text":"
Morph is not warp (geometric transformation)Morph is a kind of morphological changing, which makes an image change to another image gradually. Morph handles both the location and the intensity of a pixel.The beginning image and end image are two key frames. Other frames between the two key frames are generated automatically.
\u5bf9\u4e8e\u7070\u5ea6\u56fe\u50cf\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u7b49\u6bd4\u6216\u7b49\u5dee\u7ea7\u6570\u5b9a\u4e49\u6b65\u957f\uff0c\u4f7f\u989c\u8272\u4ece\u539f\u56fe\u53d8\u5230\u76ee\u6807\u56fe\u3002
\u5bf9\u4e8e\u5f69\u8272\u56fe\u50cf\uff0cRGB\u4e09\u8272\u540c\u65f6\u4ece\u539f\u56fe\u53d8\u5230\u76ee\u6807\u56fe\u50cf\u3002\u53ef\u4ee5\u5206\u522b\u53d8\u5316\uff0c\u4e5f\u53ef\u8003\u8651RGB\u7684\u76f8\u5bf9\u6bd4\u4f8b\u5173\u7cfb\u540c\u65f6\u53d8\u5316\u3002
\u9009\u62e9\u4e24\u5e45\u7ed3\u6784\u76f8\u4f3c\u3001\u5927\u5c0f\u76f8\u540c\u7684\u753b\u9762\u4f5c\u4e3a\u8d77\u59cb\u548c\u7ed3\u675f\u5173\u952e\u5e27\uff0c\u8fd9\u6837\u624d\u80fd\u6bd4\u8f83\u5bb9\u6613\u5730\u5b9e\u73b0\u81ea\u7136\u3001\u8fde\u7eed\u7684\u4e2d\u95f4\u53d8\u5f62\u8fc7\u7a0b\u3002
\u5728\u8d77\u59cb\u548c\u7ed3\u675f\u753b\u9762\u4e0a\u786e\u5b9a\u548c\u52fe\u753b\u51fa\u5404\u90e8\u5206\uff08\u4e3b\u8981\u8f6e\u5ed3\uff09\u7684\u7ed3\u6784\u5bf9\u5e94\u5173\u7cfb\uff0c\u4e5f\u5373\u4ece\u8d77\u59cb\u753b\u9762\u4e0a\u7684\u4e00\u4e2a\u70b9\u53d8\u5230\u7ed3\u675f\u753b\u9762\u4e0a\u7684\u53e6\u4e00\u4e2a\u5bf9\u5e94\u70b9\u7684\u4f4d\u7f6e\uff0c\u8fd9\u662f\u53d8\u5f62\u8fd0\u7b97\u6240\u9700\u8981\u7684\u53c2\u6570
\u6839\u636e\u9700\u8981\uff0c\u5bf9\u5e94\u70b9\u7684\u4f4d\u7f6e\u53ef\u4ee5\u4efb\u610f\u79fb\u52a8\u3002\u8c03\u6574\u8d77\u59cb\u5e27\u7684\u5bf9\u5e94\u70b9\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u6a21\u62df\u6444\u50cf\u4e2d\u7684\u955c\u5934\u6e10\u53d8\u6548\u679c
"},{"location":"DIP/morph/morph/#morph-based-on-segment-two-points","title":"Morph based on segment (two points)","text":""},{"location":"DIP/morph/morph/#morph-based-on-grid","title":"Morph based on grid","text":"\u89c6\u70b9\u53d8\u6362\u5c31\u662f\u6c42\u5728\u89c6\u70b9\u79fb\u52a8\u540e\u539f\u59cb\u56fe\u50cf\u53d8\u6362\u751f\u6210\u7684\u65b0\u89c6\u56fe
\u5728\u7528\u76f8\u673a\u62cd\u6444\u7269\u4f53\u65f6\uff0c\u4fdd\u6301\u7269\u4f53\u7684\u4f4d\u7f6e\u4e0d\u52a8\uff0c\u800c\u5c06\u76f8\u673a\u79fb\u79bb\u7269\u4f53\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u89c6\u70b9\u53d8\u6362
\u5982\u679c\u6211\u4eec\u4fdd\u6301\u76f8\u673a\u56fa\u5b9a\u4f4d\u7f6e\uff0c\u5c06\u7269\u4f53\u79fb\u79bb\u76f8\u673a\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u6a21\u578b\u8f6c\u6362\u3002\u89c6\u70b9\u53d8\u5f62\u8981\u6c42\u5bf9\u7269\u4f53\u6709\u4e09\u7ef4\u5750\u6807\u77e5\u8bc6\u3002
Expressive Expression Mapping with Ratio Images
"},{"location":"DIP/morph/morph/#lambertian-model","title":"Lambertian model","text":"Assume there are m point light sources:
\\(I=\\rho\\sum_{1\\le i\\le m}S_iI_in\u00b7l_i=\\rho E(n)\\)
\\(S_i=\\left\\{\\begin{array}{ll}0 & \\text{if the point cannot be seen from light i }\\\\1& \\text{Otherwise}\\end{array}\\right.\\)
\u5176\u4e2d \\(l_i\\) \u7b2c i \u4e2a\u5149\u6e90\u7167\u8fc7\u6765\u7684\u89d2\u5ea6
\\(n\\) \u8868\u793a\u5f53\u524d\u8fd9\u4e2a\u66f2\u9762\u7684\u6cd5\u5411\u91cf\uff0c\u6240\u4ee5\\(n\u00b7l_i\\)\u76f8\u5f53\u4e8e\u4f59\u5f26\u5173\u7cfb
\\(I_i\\) \u8868\u793a\u7b2c i \u4e2a\u5149\u6e90\u7684\u4eae\u5ea6
\\(S_i\\)\u8868\u793a\u66f2\u9762\u80fd\u4e0d\u80fd\u88ab\u7b2c i \u4e2a\u70b9\u5149\u6e90\u7167\u5230
\\(\\rho\\) \u8868\u793a\u66f2\u9762\u7684\u6750\u8d28
"},{"location":"DL_CV/CNN/lec/#convolutional-networks","title":"Convolutional Networks","text":"
torch.nn.Conv1d(in_channels,out_channels,lernel_size,stride=1,padding=0,dilation =1,grounps=1,bias=True,padding_mode='zeros')\n
torch.nn.Conv3d(in_channels,out_channels,kernel_size,stride=1,padding=0.dilation=1,groups=1,bias=True,padding_mode='zeros')\n
Problem: Deep Networks very hard to train!
"},{"location":"DL_CV/CNN/lec/#normalization","title":"Normalization","text":"https://medium.com/techspace-usict/normalization-techniques-in-deep-neural-networks-9121bf100d8
https://zhuanlan.zhihu.com/p/56542480
"},{"location":"DL_CV/CNN/lec/#batch-normalization","title":"Batch Normalization","text":"Why? Helps reduce \u201cinternal covariate shift\u201d, improves optimization
Learnable scale and shift parameters: \\(\\gamma\\ \\beta\\)\u200b
problem : If in two pics,one has a batch of a cat and the other has a batch of dog, we donot expect them to be in different classes,but the batch normalization will cause such problems
Use constant \\(\\mu\\) and \\(\\sigma\\)\u200b !
"},{"location":"DL_CV/Pytorch/lec/#deep-learning-hardware","title":"Deep Learning Hardware","text":"
Tensor
Create
x = torch.empty(3, 4)\nprint(type(x))\nprint(x)\n'''Out:\n<class 'torch.Tensor'>\ntensor([[0., 0., 0., 0.],\n [0., 0., 0., 0.],\n [0., 0., 0., 0.]])\n'''\nzeros = torch.zeros(2, 3)\nones = torch.ones(2, 3)\ntorch.manual_seed(1729)\nrandom = torch.rand(2, 3)\n
_like methods
x = torch.empty(2, 2, 3)\nprint(x.shape)\nprint(x)\n\nempty_like_x = torch.empty_like(x)\nprint(empty_like_x.shape)\nprint(empty_like_x)\n\nzeros_like_x = torch.zeros_like(x)\nprint(zeros_like_x.shape)\nprint(zeros_like_x)\n\nones_like_x = torch.ones_like(x)\nprint(ones_like_x.shape)\nprint(ones_like_x)\n\nrand_like_x = torch.rand_like(x)\nprint(rand_like_x.shape)\nprint(rand_like_x)\n
"},{"location":"DL_CV/Pytorch/lec/#fundamental-concepts","title":"Fundamental Concepts","text":"import torch\ndevice = torch.device('cpu')\n#device = torch.device('cuda:0')\n#device = torch.device('mps')\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in,device=device)\ny = torch.randn(N,D_out,device=device)\nw1 = torch.randn(D_in,H,device=device)\nw2 = torch.randn(H,D_out,device=device)\nlearning_rate=1e-6\nfor t in range(500):\n h = x.mm(w1)\n h_relu = h.clamp(min=0)\n y_pred = h_relu.mm(w2)\n loss = (y_pred-y).pow(2).sum\n\n grad_y_pred = 2.0*(y_pred-y)\n grad_w2 = h_relu.t().mm(grad_y_pred)\n grad_h_relu = grad_y_pred.mm(w2.t())\n grad_h = grad_h_relu.clone()\n grad_h[h<0]=0\n grad_21 = x.t()mm(grad_h)\n\n w1 -= learning_rate * grad_w1\n w2 -= learning_rate * grad_w2\n
a = torch.ones((2, 3), dtype=torch.int16)
rand = torch.rand(2, 4)\ndoubled = rand * (torch.ones(1, 4) * 2)\nprint(rand)\nprint(doubled)\n
Moving to GPU
device = torch.device(\"mps\")\nmodel = ModelName(xxx).to(device)\ndata = torch.Tensor(dataset.x).to(device)\n
Changing dimensions
a = torch.rand(3, 226, 226)\nb = a.unsqueeze(0)\nprint(a.shape)\nprint(b.shape)\n
numpy
import numpy as np\n\nnumpy_array = np.ones((2, 3))\nprint(numpy_array)\n\npytorch_tensor = torch.from_numpy(numpy_array)\nprint(pytorch_tensor)\n
Autograd: Package for building computational graphs out of Tensors, and automatically computing gradients
import torch\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in, H ,requires_grad=True)\nw2 = torch.randn(H,D_out,requires_grad=True)\nlearning_rate=1e-6\nfor t in range(500):\n y_pred = x.mm(w1).clamp(min=0).mm(w2)\n loss = (y_pred-y).pow(2).sum()\n loss.backward()\n with torch.no_grad():\n # Don't do computational graph in this stage [donnot do grad computation in this stage]\n w1 -= learning_rate * w1.grad\n w2 -= learning_rate * w2.grad\n w1.grad.zero_()\n w2.grad.zero_()\n
After backward finishes, gradients are accumulated into \\(w1.grad\\) and \\(w2.grad\\) and the graph is destroyed -- FORGET this is a common bug!
Can define new operations using Python functions
def sigmoid(x):\n return 1.0/(1.0+(-x).exp())\n# y_pred = sigmoid(x.mm(w1)).mm(w2)\n
def sigmoid(x):\n return 1.0/(1.0+(-x).exp())\nimport torch\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in, H ,requires_grad=True)\nw2 = torch.randn(H,D_out,requires_grad=True)\nlearning_rate=1e-6\nfor t in range(500):\n y_pred = sigmoid(x.mm(w1)).mm(w2)\n loss = (y_pred-y).pow(2).sum()\n loss.backward()\n with torch.no_grad():\n # Don't do computational graph in this stage [donnot do grad computation in this stage]\n w1 -= learning_rate * w1.grad\n w2 -= learning_rate * w2.grad\n w1.grad.zero_()\n w2.grad.zero_()\n
Improvement
class Sigmoid(torch.autograd.Function):\n @staticmethod\n def forward(ctx,x):\n y = 1.0/(1.0+(-x).exp())\n ctx.save_for_backward(y)\n return y\n def backward(ctx,grad_y):\n y,=ctx.saved_tensors\n grad_x = grad_y*y*(1.0-y)\n return grad_x\ndef sigmoid(x):\n return SIgmoid.apply(x)\n
Module: A neural network layer ; may store state or learnable weights
nn : Higher-level wrapper for working with neural nets
import torch\nN,D_in,H,D_out = 64,1000,100,10\n\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\n'''\nObject-oriented API: Define model object as sequence of layers objects, each of which holds weight tensors\n'''\nmodel = torch.nn.Sequential(\n torch.nn.Linear(D_in,H),\n torch.nn,ReLU(),\n torch.nn.Linear(H,D_out)\n)\nlearning_rate = 1e-4\noptimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)\nfor t in range(500):\n y_pred = model(x)\n loss = torch.nn.functional.mse_loss(y_pred,y)#torch.nn.functional has useful helpers like loss functions\n loss.backward()\n with torch.no_grad():\n for param in model.parameters():\n param -= learning_rate * param.grad\n model.zero_grad( )\n
import torch\nN,D_in,H,D_out = 64,1000,100,10\n\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\n'''\nObject-oriented API: Define model object as sequence of layers objects, each of which holds weight tensors\n'''\nmodel = torch.nn.Sequential(\n torch.nn.Linear(D_in,H),\n torch.nn,ReLU(),\n torch.nn.Linear(H,D_out)\n)\nlearning_rate = 1e-4\noptimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)\nfor t in range(500):\n y_pred = model(x)\n loss = torch.nn.functional.mse_loss(y_pred,y)#torch.nn.functional has useful helpers like loss functions\n loss.backward()\n optimizer.step()\n optimizer.zero_grad()\n
import torch \nclass TwoLayerNet(torch.nn.Module):\n def __init__(self,D_in,H,D_out):\n super(TwoLayerNet,self).__init__()\n self.linear1 = torch.nn.Linear(D_in,H)\n self.linear2 = torch.nn.Linear(H,D_out)\n def forward(self,x):\n h_relu = self.linear1(x).clamp(min=0)\n y_ored = self.linear2(h_relu)\n return y_pred\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nmodel = TwoLayerNet(D_in,H,D_out)\noptimizer = torch.optim.SGD(model.parameters(),lr=1e-4)\nfor t in range(500):\n y_pred = model(x)\n loss = torch.nn.functional.mse_loss(y_pred,y)\n loss.backward()\n optimizer.step()\n optimizer.zero_grad()\n
import torch\nclass ParallelBlock(torch.nn.Module):\n def __init__(self,D_in,D_out):\n super(ParallelBlock,self)._init__()\n self.linear1 = torch.nn.Linear(D_in,D_out)\n self.linear2 = torch.nn.Linear(D_in,D_out)\n def forward(self,x):\n h1 = self.linear1(x)\n h2 = self.linear2(x)\n return (h1*h2).clamp(min = 0)\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nmodel = torch.nn.Sequential(\n ParallelBlock(D_in,H),\n ParallelBlock(H,H),\n torch.nn.Linear(H,D_out)\n)\noptimizer = torch.optim.Adam(model.parameters(),lr = 1e-4)\nfor t in range(500):\n y_pred = model(x)\n loss = torch.nn.functional.mse_loss(y_pred,y)\n loss.backward()\n optimizer.step()\n optimizer.zero_grad()\n
import torch\nfrom torch.utils.data import TensorDataset,DataLoader\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nloader = DataLoader(TensorDataset(x,y),batch_size = 8)\nmodel = TwoLayerNet(D_in,H,D_out)\noptimizer = torch.optim.SGD(model.parameters(),lr= = 1e-2)\nfor epoch in range(20):\n # Iterate over loader to form minibatches\n for x_batch,y_batch in loader:\n y_pred = model(x_batch)\n loss = loss.nn.functional.mse_loss(y_pred,y_batch)\n loss.backward()\n optimizer.step()\n optimizer.zero_grad()\n
import torch\nimport torchvision\nalexnet = torchvision.models.alexnet(pretrained = True)\nvgg16 = torchvision.models.vgg16(pretrained = True)\nresnet101 = torchvision.models.resnet101(pretrained = True)\n
Note : this model doesn\u2019t makes sense! Just a simple dynamic example
import torch\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in,H,requires_grad = True)\nw2a = torch.randn(H,D_out,requires_grad = True)\nw2b = torch.randn(H,D_out,requires_grad = True)\nlearning_rate=1e-6\nfor t in range(500):\n # Decide which one to use at each layer based on loss at previous iteration\n w2 = w2a if prev_loss <5.0 else w2b\n y_pred = x.mm(w1).clamp(min=0).mm(w2)\n loss = (y_pred - y).pow(2).sum\n loss.backward()\n prev_loss = loss.item()\n
Static Computation Graphs
Step 1: Build computational graph describing our computation (including finding paths for backprop)
import torch \ndef model(x,y,w1,w2a,w2b,prev_loss):\n w2 = w2a if prev_loss <5.0 else w2b\n y_pred = x.mm(w1).clamp(min=0).mm(w2)\n loss = (y_pred - y).pow(2).sum\n return loss\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in,H,requires_grad = True)\nw2a = torch.randn(H,D_out,requires_grad = True)\nw2b = torch.randn(H,D_out,requires_grad = True)\n#Just-In-Time compilation: Introspect the source code of the function, compile it into a graph object.\ngraph = torch.jit.script(model)\nprev_loss = 5.0\nlearning_rate = 1e-6\nfor t in range(500):\n loss = graph(x,y,w1,w2a,w2b,prev_loss)\n loss.backward()\n prev_loss = loss.item()\n
import torch\n@torch.jit.script\ndef model(x,y,w1,w2a,w2b,prev_loss):\n w2 = w2a if prev_loss <5.0 else w2b\n y_pred = x.mm(w1).clamp(min=0).mm(w2)\n loss = (y_pred - y).pow(2).sum\n return loss\nN,D_in,H,D_out = 64,1000,100,10\nx = torch.randn(N,D_in)\ny = torch.randn(N,D_out)\nw1 = torch.randn(D_in,H,requires_grad = True)\nw2a = torch.randn(H,D_out,requires_grad = True)\nw2b = torch.randn(H,D_out,requires_grad = True)\nprev_loss = 5.0\nlearning_rate = 1e-6\nfor t in range(500):\n loss = model(x,y,w1,e2a,w2b,prev_loss)\n loss.backward()\n prev_loss = loss.item()\n
Static
With static graphs, framework can optimize the graph for you before it runs!
Once graph is built, can serialize it and run it without the code that built the graph!
e.g. train model in Python, deploy in C++
Lots of indirection between the code you write and the code that runs \u2013 can be hard to debug, benchmark, etc
Dynamic
Graph building and execution are intertwined, so always need to keep code around
The code you write is the code that runs! Easy to reason about, debug, profile, etc
Dynamic Graph Applications
Model structure depends on the input:
TensorFlow 1.0 : Static Graphs
First define computational graph
Then run the graph many times
import numpy as np\nimport tensorflow as tf\nN,D,H = 64,1000,100\nx = tf.placeholder(tf.float32,shape = (N,D))\ny = tf.placeholder(tf.float32,shape = (N,D))\nw1 = tf.placeholder(tf.float32,shape = (D,H))\nw2 = tf.placeholder(tf,float32,shape = (H,D))\n\nh = tf.maximum(tf.matmul(x,w1),0)\ny_pred = tf.matmul(h,w2)\ndiff = y_pred - y\nloss = tf.reduce_mean(tf.refuce_sum(diff ** 2,axis = 1))\ngrad_w1,grad_e2 = tf.grafients(loss,[w1,w2])\n\nwith tf.Session() as sess :\n values = {\n x:np.random.randn(N,D),\n w1:np.random.randn(D,H),\n w2:np.random.randn(H,D),\n y:np.random.randn(N,D),\n }\n out = sess.run(\n [loss,grad_w1,grad_w2],\n feed_dict = values\n )\n loss_val,grad_w1_val,grad_w2_val = out\n
TensorFlow 2.0: Dynamic Graphs
Create TensorFlow Tensors for data and weights
Weights need to be wrapped in tf.Variable so we can mutate them
import tensorflow as tf\nN,Din,H,Dout = 16,1000,100,10\nx = tf.random.noraml((N,Din))\ny = tf.random,normal((N,Dout))\nw1 = tf.Variable(tf.random.normal(Din,H))\nw2 = tf.Variable(tf.random.normal(H,Dout))\nfor t in range(1000):\n #Scope forward pass under a GradientTape to tell TensorFlow to start building a graph\n with tf,GradientTape() as tape :\n h = tf.maximum(tf.matmul(x,w1),0)\n y_pred = tf.matmul(h,w2)\n diff = y_pred - y\n loss = tf.reduce_mean(tf.reduce_sum(diff **2 , axis = 1))\n #Ask the tape to compute gradients\n grad_w1,grad_e2 = tape.gradient(loss,[w1,w2])\n # Gradient descent step, update weights\n w1.assign(w1-learning_rate*grad_w1)\n w2.assign(w2-learning-learning_rate*grad_w2)\nlearning_rate\n
@tf.function\ndef step(x,y,w1,w2):\n with tf,GradientTape() as tape :\n h = tf.maximum(tf.matmul(x,w1),0)\n y_pred = tf.matmul(h,w2)\n diff = y_pred - y\n loss = tf.reduce_mean(tf.reduce_sum(diff **2 , axis = 1))\n #Ask the tape to compute gradients\n grad_w1,grad_e2 = tape.gradient(loss,[w1,w2])\n w1.assign(w1-learning_rate*grad_w1)\n w2.assign(w2-learning-learning_rate*grad_w2)\n return loss\nN,Din,H,Dout = 16,1000,100,10\nx = tf.random.noraml((N,Din))\ny = tf.random,normal((N,Dout))\nw1 = tf.Variable(tf.random.normal(Din,H))\nw2 = tf.Variable(tf.random.normal(H,Dout))\nlearning_rate = 1e-6\nfor t in range(1000):\n loss = step(x,y,w1,w2)\n
import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import InputLayer,Dense\nN,Din,H,Dout = 16,1000,100,10\n\nmodel = Sequential()\nmodel.add(InputLayer(input_shape=(Din,)))\nmodel.add(Dense(units = H,activation = 'relu'))\nmodel.add(Dense(units = Dout))\n\nparams = model.trainable_variables\nloss_fn = tf.keras.losses.MeanSquaredError()\nopt = tf.kears.optimizers.SGD(learning_rate = 1e-6)\nx = tf.random.normal((N,Din))\ny = tf.random.noraml((N,Dout))\ndef step():\n y_pred = model(x)\n loss = loss_fn(y_pred,y)\n return loss\nfor t in range(1000):\n opt.minimize(step,params)\n
"},{"location":"DL_CV/Recognition/lec10/","title":"Recognision","text":""},{"location":"DL_CV/Recognition/lec10/#object-detection","title":"Object Detection","text":"
Input: Single RGB Image
Output: A set of detected objects:
Challenges
Apply a CNN to many different crops of the image, CNN classifies each crop as object or background
We should consider all possible sizes and possible positions
Solutions
Region Proposals
Use MultiTask Loss
Bbox : Modify the Region Proposals
\\(L_2\\) regularization encourages leaving proposal unchanged.
Transform encodes relative difference between proposal and output; important since \\(CNN\\) doesn\u2019t see absolute size or position after cropping
"},{"location":"DL_CV/Recognition/lec10/#r-cnn-training","title":"R-CNN Training","text":"Positive: > 0.5 IoU with a GT box
Negative: < 0.3 IoU with all GT boxes
Neutral: between 0.3 and 0.5 IoU with GT boxes
Problem: Object detectors often output many overlapping detections:
Solution: Post-process raw detections using Non-Max Suppression (NMS)
Select next highest-scoring box
Eliminate lower-scoring boxes with IoU > threshold (e.g. 0.7)
If any boxes remain, GOTO 1
Problem: NMS may eliminate \u201dgood\u201d boxes when objects are highly overlapping...
no good solution =(
R-CNN Problem: Very slow! Need to do 2000 forward passes through CNN per image
Idea: Overlapping proposals cause a lot of repeated work: same pixels processed many times. Can we avoid this?
Per-Region network is relatively lightweight
Most of the computation happens in backbone network;
this saves work for overlapping region proposals.
How to crop features?
"},{"location":"DL_CV/Recognition/lec10/#cropping-features-roi-pool","title":"Cropping Features: RoI Pool","text":"Classic idea: build an image pyramid by resizing the image to different scales, then process each image scale independently.
Problem: Expensive! Don\u2019t share any computation between scales.
Simple Idea : Sliding Window
Use Fully Convolutional Network
Dose not have any pooling layers or fully connected layers.
Problem #1: Effective receptive field size is linear in number of conv layers:
Let's say we have L 3x3 convolutional layers. In a CNN, each neuron in a convolutional layer is connected to a small region of neurons from the previous layer, which is its receptive field. For a 3x3 convolutional kernel, the receptive field size is 3x3.
Now, consider L 3x3 convolutional layers. In the first layer, the receptive field of each neuron is 3x3. In the second layer, the receptive field of each neuron relative to the input image will cover a larger area, which is the combination of receptive fields from the previous layer. Similarly, in the third layer, the receptive field relative to the input image will be even larger.
Therefore, as the number of layers increases, the influence of each layer on the input image gradually expands. Assuming each convolutional layer has a stride of 1, and there are no pooling layers or padding, after L 3x3 convolutional layers, the receptive field size relative to the input image for each neuron will be 1 plus twice the number of convolutional layers, denoted as 1+2L.
Problem #2: Convolution on high res images is expensive! Recall ResNet stem aggressively downsamples
"},{"location":"DL_CV/Recognition/lec10/#in-network-upsampling-unpooling","title":"In-Network Upsampling: \u201cUnpooling\u201d","text":"Refer to Slides
Refer to Slides
Learnable Upsampling: Transposed Convolution
Convolution with stride > 1 is \u201cLearnable Downsampling\u201d
Detect all objects in the image, and identify the pixels that belong to each object (Only things!)
Approach: Perform object detection, then predict a segmentation mask for each object!
"},{"location":"DL_CV/TN/lec/#training-neural-networks","title":"Training Neural Networks","text":""},{"location":"DL_CV/TN/lec/#one-time-setup","title":"One time setup","text":""},{"location":"DL_CV/TN/lec/#activation-functions","title":"Activation Functions","text":""},{"location":"DL_CV/TN/lec/#sigmoid","title":"Sigmoid","text":"
\\(\\sigma(x)=\\frac{1}{1+e^{-x}}\\)
3 problems
What can we say about the gradients on w?
\\(f(x) = max(0,x)\\)\u200b
- Does not saturate (in +region)
- Very computationally efficient
- Converges much faster than sigmoid/tanh in practice (e.g. 6x)
Not zero-centered output
what is the gradient when x < 0?
"},{"location":"DL_CV/TN/lec/#leaky-relu","title":"Leaky ReLU","text":""},{"location":"DL_CV/TN/lec/#exponential-linear-unit-elu","title":"Exponential Linear Unit (ELU)","text":""},{"location":"DL_CV/TN/lec/#scaled-exponential-linear-unit-selu","title":"Scaled Exponential Linear Unit (SELU)","text":"Derivation takes 91 pages of math in appendix...
"},{"location":"DL_CV/TN/lec/#activation-functions-summary","title":"Activation Functions: Summary","text":"Q: What happens if we initialize all W=0, b=0?
A: All outputs are 0, all gradients are the same! No \u201csymmetry breaking\u201d
w = 0.01 * np.random.randn(Din,Dout)\n
Works ~okay for small networks, but problems with deeper networks.
All activations tend to zero for deeper network layers
Q: What do the gradients \\(dL/dW\\) look like? -- All zero, no learning
For CNN : Din will be \\(input\\_channels*kernel\\_size*kernel\\_size\\)\u200b
Remeber?
Derivation:
p = 0.5\nimport numpy as np\ndef train_step(X):\n H1 = np.maximum(0,np.dot(W1,X)+b1)\n U1 = np.random.rand(*H1.shape)<p \n H1 *=U1\n H2 = np.maximum(0,np.dot(W2,H1)+b2)\n U2 = np.random.rand(*H2.shape)<p\n H2* = U2\n out = np.dot(W3,H2)+b3\n
Dropout makes our output random! -- Want to \u201caverage out\u201d the randomness at test-time
\\(y = f(x) = E_z[f(x,z)]=\\int p(z)f(x,z)dz\\)\u200b
But this integral is actually a bit hard
Consider a single neuron
\\(E[a]=\\frac{1}{4}(w_1x+w_2y)+\\frac{1}{4}(w_1x+0y)+\\frac{1}{4}(0x+0y)+\\frac{1}{4}(0x+w_2y)\\\\=\\frac{1}{2}(w_1x+w_2y)\\)
\\(E[a]=w_1x+w_2y\\)
def predict(x):\n H1 = np.maximum(0,np.dot(W1,X)+b1)*p\n H2 = np.maximum(0,np.dot(W2,H1)+b2)*p\n out = np.dot(W3,H2)+b3\n
At test time all neurons are active always.
We must scale the activations so that for each neuron: output at test time = expected output at training time
p = 0.5\ndef train_step(X):\n H1 = np.maximum(0,np.dot(W1,X)+b1)\n U1 = np.random.rand(*H1.shape)<p \n H1 *=U1\n H2 = np.maximum(0,np.dot(W2,H1)+b2)\n U2 = np.random.rand(*H2.shape)<p\n H2* = U2\n out = np.dot(W3,H2)+b3\ndef predict(x):\n H1 = np.maximum(0,np.dot(W1,X)+b1)*p\n H2 = np.maximum(0,np.dot(W2,H1)+b2)*p\n out = np.dot(W3,H2)+b3\n
More common: \u201cInverted dropout\u201d
p = 0.5\ndef train_step(X):\n H1 = np.maximum(0,np.dot(W1,X)+b1)\n U1 = np.random.rand(*H1.shape)<p /p\n H1 *=U1\n H2 = np.maximum(0,np.dot(W2,H1)+b2)\n U2 = np.random.rand(*H2.shape)<p /p\n H2* = U2\n out = np.dot(W3,H2)+b3\ndef predict(x):\n H1 = np.maximum(0,np.dot(W1,X)+b1)\n H2 = np.maximum(0,np.dot(W2,H1)+b2)\n out = np.dot(W3,H2)+b3\n
"},{"location":"DL_CV/TN/lec/#data-augmentation","title":"Data Augmentation","text":""},{"location":"DL_CV/TN/lec/#dropconnect","title":"DropConnect","text":""},{"location":"DL_CV/TN/lec/#fractional-pooling","title":"Fractional Pooling","text":""},{"location":"DL_CV/TN/lec/#stochastic-depth","title":"Stochastic Depth","text":""},{"location":"DL_CV/TN/lec/#mixup","title":"Mixup","text":""},{"location":"DL_CV/class/lec/#image-classifications","title":"Image Classifications","text":"
Challenges :
Fine-Grained Categories(eg.different kinds of cats)
Background Clutter
illumination Changes
Deformation
Occlusion
Machine Learning: Data-Driven Approach
10 classes: Digits 0 to 9 28x28 grayscale images 50k training images 10k test images Results from MNIST often do not hold on more complex datasets!
10 classes
50k training images (5k per class)
10k testing images (1k per class)
32x32 RGB images
100 classes
50k training images (500 per class)
10k testing images (100 per class)
32x32 RGB image
20 superclasses with 5 classes each:
Aquatic mammals: beaver, dolphin, otter, seal, whale
Trees: Maple, oak, palm, pine, willow
1000 classes
\\(\\approx\\) 1.3M training images (~1.3K per class)
50K validation images (50 per class)
100K test images (100 per class)
Performance metric: Top 5 accuracy Algorithm predicts 5 labels for each image
one of them needs to be right
test labels are secret!
Images have variable size, but often resized to 256x256 for training
There is also a 22k category version of ImageNet, but less commonly used
365 classes of different scene types
\\(\\approx\\) 8M training images
18.25K val images (50 per class)
328.5K test images (900 per class)
Images have variable size, often resize to 256x256 for training
1623 categories: characters from 50 different alphabets
20 images per category
Meant to test few shot learning
"},{"location":"DL_CV/class/lec/#first-classifier-nearest-neighbor","title":"First classifier: Nearest Neighbor","text":"def train(imgaes,labels):\n #Machine learning!\n return model\ndef predict(model,test_images):\n #Use model to predict labels\n return test_labels\n
"},{"location":"DL_CV/class/lec/#distance","title":"Distance","text":"import numpy as np\nclass NearestNeightbor:\n def __init__(self):\n pass\n def train(self,X,y):\n '''X is NxD where each row is an example;y is a 1-dimension f size N'''\n self.Xtr = X\n self.ytr = y\n def predict(self,X):\n '''X is NxD where each row is an example we wish to predict label for'''\n num_test = X.shape[0]\n Ypred = np.zeros(num_test,dtype = self.ytr.dtype)\n for i in xrange(num_test):\n distances = np.sum(np.abs(Self.Xtr - X[i,:]),axis = 1)\n min_index = np.argmin(distances)\n Ypred[i]=self.ytr[min_index]\n return Ypred\n
\u8865\uff1anumpy
import numpy as np\n# \u751f\u6210\u793a\u4f8b\u6570\u636e\nN, D = 5, 3\na = np.random.rand(N, D) # \u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a (N, D) \u7684\u968f\u673a\u6570\u7ec4\nb = np.random.rand(D) # \u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a (D,) \u7684\u968f\u673a\u6570\u7ec4\n# \u4f7f\u7528\u5e7f\u64ad\u8fdb\u884c\u51cf\u6cd5\nresult = a - b\n# \u5bf9\u6bcf\u4e00\u884c\u6c42\u548c\uff0c\u5f97\u5230\u5f62\u72b6\u4e3a (N, 1) \u7684\u6570\u7ec4\nsum_result = np.sum(result, axis=1, keepdims=True)\nprint(sum_result)\n
\u200b
\u200b
Distance Metric
\\(L_2\\) (Euclidean) distance : \\(d_2(I_1,I_2)=\\sqrt{\\sum_p(I_1^p-I_2^p)^2}\\)\u200b
With the right choice of distance metric, we can apply K-Nearest Neighbor to any type of data!
hyperparameters: choices about our learning algorithm that we don\u2019t learn from the training data; instead we set them at the start of the learning process
Setting Hyperparameters
"},{"location":"DL_CV/class/lec/#k-nearest-neighbor-universal-approximation","title":"K-Nearest Neighbor: Universal Approximation","text":"Example : Refer to PPT
Curse of dimensionality: For uniform coverage of space, number of training points needed grows exponentially with dimension
Original image is CC0 public domain
K-Nearest Neighbor on raw pixels is seldom used
f(x, W) = Wx (ignore bias)\nf(cx, W) = W(cx) = c * f(x, W)\n
\u6cbf\u7740\u68af\u5ea6\u65b9\u5411 z(which is the score) \u589e\u957f\u6700\u5feb
A loss function(Also called: objective function; cost function) tells how good our current classifier is
Low loss = good classifier
High loss = bad classifier
Given a dataset of examples \\(\\{(x_i,y_i)\\}_{i=1}^N\\) where \\(x_i\\) is image and \\(y_i\\) is (integer) label
Loss for a single example is \\(L_i(f(x_i,W),y_i)\\)
Loss for the dataset is average of per-example losses : \\(L=\\frac{1}{N}\\sum_iL_i(f(x_i,W),y_i)\\)\u200b
\\(L=(2.9+0+12.9)/3\\)\u200b
min - 0
max - infinity
May achieve a \\(L = 1\\) (all are small scores randomly)
All scores be inflatted by a constant one
What if the loss used a mean instead of a sum?
What if we used this loss instead? \\(L_i = \\sum_{j\\ne y_i}max(0,s_y-s_{y_i}+1)^2\\)\u200b
Suppose we found some W with L = 0. Is it unique?
No! 2W is also has L = 0 !
How to Address this problem ? -- Regularization!
"},{"location":"DL_CV/class/lec/#regularization-beyond-training-error","title":"Regularization: Beyond Training Error","text":"\\(L(W) = \\frac{1}{N}\\sum_{i=1}^NL_i(f(x_i,W),y_i)+\\lambda R(W)\\)\u200b
$\\lambda = $\u200b regularization strength (hyperparameter)
Purpose of Regularization:
Express preferences in among models beyond \u201dminimize training error\u201d
Avoid overfitting: Prefer simple models that generalize better
What is the \\(min / max\\) possible loss \\(L_i\\)? A: Min - 0, max - +infinity
If all scores are small random values, what is the loss? A: -log(C) (near log(10) \u2248 2.3 at the beginning stage)
\u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6709\u4e00\u4e2a\u5206\u7c7b\u4efb\u52a1\uff0c\u6a21\u578b\u8f93\u51fa\u7684\u5206\u6570\u7ecf\u8fc7softmax\u51fd\u6570\u8f6c\u6362\u6210\u6982\u7387\u5206\u5e03\u3002\u7531\u4e8e\u6240\u6709\u7684\u5206\u6570\u90fd\u662f\u5c0f\u968f\u673a\u503c\uff0c\u8fd9\u610f\u5473\u7740\u6a21\u578b\u5bf9\u6bcf\u4e2a\u7c7b\u522b\u7684\u9884\u6d4b\u6982\u7387\u90fd\u4f1a\u63a5\u8fd1\u4e8e\u5747\u5300\u5206\u5e03\uff0c\u5373\u6bcf\u4e2a\u7c7b\u522b\u88ab\u9884\u6d4b\u4e3a\u6b63\u7c7b\u7684\u6982\u7387\u90fd\u63a5\u8fd1\u4e8e1/\u7c7b\u522b\u6570\u3002
\u5047\u8bbe\u6709C\u4e2a\u7c7b\u522b\uff0c\u90a3\u4e48\u6bcf\u4e2a\u7c7b\u522b\u88ab\u9884\u6d4b\u4e3a\u6b63\u7c7b\u7684\u6982\u7387\u5927\u7ea6\u662f1/C\u3002\u6839\u636e\u4ea4\u53c9\u71b5\u7684\u5b9a\u4e49\uff0c\u5f53\u771f\u5b9e\u6807\u7b7e\u7684\u6982\u7387\u5206\u5e03\u63a5\u8fd1\u4e8e\u5747\u5300\u5206\u5e03\u65f6\uff0c\u4ea4\u53c9\u71b5\u7684\u503c\u5927\u7ea6\u662f-log(1/C)\u3002\u7531\u4e8e-log(1/C) = log(C)\uff0c\u6240\u4ee5\u4ea4\u53c9\u71b5\u7684\u503c\u5927\u7ea6\u662flog(C)\u3002
log(10)\u662f\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\uff0c\u7ea6\u7b49\u4e8e2.3\u3002\u6240\u4ee5\u5f53\u6240\u6709\u5206\u6570\u90fd\u662f\u5c0f\u968f\u673a\u503c\u65f6\uff0c\u4ea4\u53c9\u71b5\u7684\u503c\u5927\u7ea6\u662f2.3\u3002\u8fd9\u610f\u5473\u7740\u6a21\u578b\u7684\u9884\u6d4b\u4e0e\u771f\u5b9e\u6807\u7b7e\u4e4b\u95f4\u7684\u5dee\u5f02\u6027\u6bd4\u8f83\u5927\uff0c\u56e0\u4e3a\u9884\u6d4b\u7684\u6982\u7387\u5206\u5e03\u63a5\u8fd1\u4e8e\u5747\u5300\u5206\u5e03\uff0c\u800c\u771f\u5b9e\u6807\u7b7e\u7684\u6982\u7387\u5206\u5e03\u662f\u975e\u5747\u5300\u7684\u3002
"},{"location":"DL_CV/class/lec/#compare","title":"Compare","text":"Q: What happens to each loss if I slightly change the scores of the last datapoint?
A: Cross-entropy loss will change; SVM loss will stay the same
Q: What happens to each loss if I double the score of the correct class from 10 to 20?
A: Cross-entropy loss will decrease, SVM loss still 0
"},{"location":"DL_CV/class/lec/#optimization","title":"Optimization","text":"\\(w^* = argmin_wL(w)\\)\u200b
"},{"location":"DL_CV/class/lec/#idea","title":"idea","text":""},{"location":"DL_CV/class/lec/#1-random-search-bad-idea","title":"#1: Random Search (bad idea!)","text":"bestloss = float(\"inf\")\nfor num in xrange(1000):\n W = np.random.randn(10,3073)*0.0001\n loss = L(X_train,Y_train,W )\n if loss < bestloss:\n bestloss = loss\n bestW = W\n print 'in attemp %d the loss was %f , best %f' %(num,loss,bestloss)\n\n# Assume X_test is [3073 x 10000], Y_test [10000 x 1]\nscores = Wbest.dot(Xte_cols) \n# 10X10000,the class scores for all test examples\nYte_predict = np.argmax(scores,axis=0) \n# and calculate accuracy (fraction of predictions that are correst)\nnp.mean(Yte_predict == Yte)\n# returns 0.1555\n
"},{"location":"DL_CV/class/lec/#2-follow-the-slope","title":"#2: Follow the slope","text":""},{"location":"DL_CV/class/lec/#computing-gradients","title":"Computing Gradients","text":"- Numeric gradient: approximate, slow, easy to write
\\(\\frac{df(x)}{dx}=lim_{h\\to 0}\\frac{f(x+h)-f(x)}{h}\\)
- Analytic gradient: exact, fast, error-prone
In practice: Always use analytic gradient, but check implementation with numerical gradient. This is called a gradient check.
def grad_check_sparse(f,x,analytic_grad,num_checks = 10,h=1e-7):\n '''\n sample a few random elements and only return numerical in this dimensions\n '''\n
from pytorch import torch\ntorch.autograd.gradcheck(func,inputs,eps=1e-06,atol = 1e-05,rtol = 0.001,raise_exception = True,check_sparse_nnz=False,nodet_tol = 0.0)\n
"},{"location":"DL_CV/class/lec/#gradient-descent","title":"Gradient Descent","text":"w = initialize_wights()\nfor t in range(num_steps):\n dw = compute_gradient(loss_fn,data,w)\n w-=learning_rate*dw\n
Hyperparameters:
- Weight initialization method - Numberofsteps - Learningrate
Stochastic Gradient Descent
\\(L(W)=E_{(x,y)-p_{data}}[L(x,y,W)]+\\lambda R(W) \\approx \\frac{1}{N}\\sum_{i=1}^NL(x_i,y_i,W)+\\lambda R(W)\\)\u200b
\\(\\triangledown L(W)=\\triangledown_W E_{(x,y)-p_{data}}[L(x,y,W)]+\\lambda \\triangledown _WR(W)\\)\u200b
Problems with SGD
What if the loss function has a local minimum or saddle point? : Zero gradient, gradient descent.
Our gradients come from minibatches so they can be noisy!
- Build up \u201cvelocity\u201d as a running mean of gradients
- Rho gives \u201cfriction\u201d typically rho=0.9 or 0.99
You may see SGD+Momentum formulated different ways, but they are equivalent - give same sequence of x
Nesterov Momentum
\\(v_{t+1} = \\rho v_t -\\alpha \\triangledown f(x_t +\\rho v_t)\\)\u200b
\\(x_{t+1}=x_t + v_{t+1}\\)
Change of variables \\(\\tilde{x_t}=x+t +\\rho v_t\\)\u200b and we get :
$ v_{t+1} = \\rho v_t -\\alpha \\triangledown f(\\tilde{x_t})$\u200b
\\(\\tilde x_{t+1} =\\rho v_t +(1+\\rho )v_{t+1}=\\tilde{x_t}+v_{t+1}+\\rho(v_{t+1}-v_t)\\)\u200b
v = 0\nfor t in range(num_steps):\n dw = compute_gradient(w)\n old_v = v\n v = rho*v -learning_rate*dw\n w -= rho* old_v -(1+rho)*v\n
AdaGrad
grad_squared = 0\nfor t in range(num_steps):\n dw = compute_gradient(w)\n grad_square += dw*dw\n w -= learning_rate * dw / (frad_square.sqrt()+1e-7)\n
Q: What happens with AdaGrad?
Progress along \u201csteep\u201d directions is damped; progress along \u201cflat\u201d directions is accelerated !
Adam(almost):RMSProp + Momentum
# Adam & Momentum\nmoment1 = 0\nmoment2 = 0\nfor t in range(num_steps):\n dw = compute_gradient(w)\n moment1 = beta1*moment1 + (1-beta1)*dw\n moment2 = beta2*moment2+(1-beta2)*dw*dw\n w -= learning_rate*moment1 / (moment2.sqrt()+ 1e-7)\n
Q: What happens at t=0? (Assume \\(beta_2\\) = 0.999)
Bias correction
moment1 = 0\nmoment2 = 0\nfor t in range(num_steps):\n dw = compute_gradient(w)\n moment1 = beta1*moment1 + (1-beta1)*dw\n moment2 = beta2*moment2+(1-beta2)*dw*dw\n moment1_unbias = moment1 / (1-beta1 ** t)\n moment2_unbias = moment2 / (1-beta2 ** t)\n w -= learning_rate*moment1 / (moment2.sqrt()+ 1e-7)\n
"},{"location":"DL_CV/class/lec/#second-order-optimization","title":"Second-Order Optimization","text":""},{"location":"DL_CV/class/lec/#first-order-optimization","title":"First-Order Optimization","text":"Second-Order Taylor Expansion:
\\(L(w)\\approx L(w_0)+(w-w_0)^T\\triangledown _wL(w_0)+\\frac{1}{2}^TH_wL(w_0)(w-w_0)\\)\u200b
Solving for the critical point we obtain the Newton parameter update:
\\(w^* = w_0 -H_wL(w_0)^{-1}\\triangledown_wL(w_0)\\)
Q: Why is this impractical? Hessian has \\(O(N^2)\\) elements Inverting takes \\(O(N^3)\\) with $N = $\u200b(Tens or Hundreds of) Millions
In practice:
"},{"location":"DL_CV/neural/lec/#neural-networks","title":"Neural Networks","text":"
Problem: Linear Classifiers aren\u2019t that powerful
\\(f = W_2 max(0,W_1x)\\) \\(W_2 \\in R^{C\\times H}\\) \\(W_1 \\in R^{H\\times D}\\) \\(x\\in R^D\\)\u200b\u200b
\\(f = W_3 max(0,W_2 max(0,W_1 x))\\)\u200b \\(W_3 \\in E^{C\\times H_2}\\) \\(W_2 \\in R^{H_2\\times H_1}\\) \\(x\\in R^D\\)
"},{"location":"DL_CV/neural/lec/#activation-functions","title":"Activation Functions","text":"Remeber the two-head horse ? (now we can recongnize them !)
import numpy as np\nfrom numpy.random import randn\n# initialize weights and data\nN,Din,H Dout = 64,1000,100,10\nx,y = randn(N,Din),randn(N,Dout)\nw1,w2 = randn(Din,H),randn(H,Dout)\n# compute loss(sigmoid activation)\nfor t in range(10000):\n h = 1.0/(1.0+np.exp(-x.dot(w1)))\n y_pred = h.dot(w2)\n loss = np.square(y_pred-y).sum()\n#Compute gradients\n dy_pred = 2.0 * (y_pred - y)\n dw2 = h.T.dot(dy_pred)\n dh = dy_pred.dot(w2.T)\n dw1 = x.T.dot(dh*h*(1-h))\n#SGD step\n w1 -= 1e-4 * dw1\n w2 -= 1e-4 *dw2\n
"},{"location":"DL_CV/neural/lec/#space-warping","title":"Space Warping","text":""},{"location":"DL_CV/neural/lec/#universal-approximation","title":"Universal Approximation","text":"A neural network with one hidden layer can approximate any function \\(f: R_N \\to R_M\\) with arbitrary precision
Output is a sum of shifted, scaled ReLUs
With 4K hidden units we can build a sum of K bumps
Reality check: Networks don\u2019t really learn bumps!
Universal approximation tells us: Neural nets can represent any function
Universal approximation DOES NOT tell us:
Whether we can actually learn any function with SGD
How much data we need to learn a function
Remember: kNN is also a universal approximator!
"},{"location":"DL_CV/neural/lec/#convex-functions","title":"Convex Functions","text":""},{"location":"DL_CV/neural/lec/#backpropagation","title":"Backpropagation","text":"For the input that is actually the \"max\" the local gradient is 1
the gradient of the other is 0
\\(y_{1,1}=x_{1,1}w_{1,1}+x_{1,2}w_{1,2}+x_{1,3}w_{1,3}\\)
\\(\\frac{dy_{1,1}}{dx_{1,1}}=w_{1,1}\\)\u200b
\\(\\frac{dy_{2,1}}{dx_{1,1}}=0\\)\u200b
\\(\\frac{dL}{dx_{1,1}}=(\\frac{dy}{dx_{1,1}})\u00b7(\\frac{dL}{dy})=(w1,;)\u00b7(\\frac{dL}{dy_1,;})=3*2 + 2*3 +1*(-3)+(-1)*9\\) \u200b
\\(\\frac{dL}{dx_{2,3}}=(\\frac{dy}{dx_{2,3}})\u00b7(\\frac{dL}{dy})=(w3,;)\u00b7(\\frac{dL}{dy_2,;})=3*(-8)+2*1+1*4+(-2)*6\\)\u200b
"},{"location":"DL_CV/neural/lec/#summary","title":"Summary","text":""},{"location":"DL_CV/neural/lec/#higher-order-derivatives","title":"Higher-Order Derivatives","text":""},{"location":"GP/AMAS/AMAS/","title":"Angular Momentum and Spin","text":"
?(12-17\u8bfe\u7a0b\u7ed3\u675f)
The wave function of a particular quantum state of the hydrogen atom can be labeled by a set of quantum numbers \\((n, l, m_l)\\).
The corresponding energy only depends on the principal quantum number \\(n = 1,2,3,....\\)
The orbital quantum number \\(l = 0, 1, 2, . . . , n \u2212 1\\) is a measure of the magnitude of the angular momentum of the quantum state. States with \\(l = 0, 1, 2, 3\\) are called \\(s, p, d, f\\) .
The orbital magnetic quantum number
\\(ml =\u2212l,\u2212l+1,...,l\u22121,l\\) is related to the space orientation of this angular momentum vector.
"},{"location":"GP/AMAS/AMAS/#classical-loop-model-for-electron-orbits","title":"Classical Loop Model for Electron Orbits","text":"Quantized by \\(\\bar{h}\\)
We can measure definite values of a component \\(L_z\\) along a chosen measurement axis(usally taken to be a z axis) as given by \\(L_z=m_l\\bar{h}\\) for \\(m_l=0,\u00b11,\u00b12,\u2026\u2026\u00b1l\\)
In general , if the electron has a definite value of \\(L_z\\) it may not have definite values of \\(L_x\\) and \\(L_y\\)
This is a manifestation of Heisenberg's uncertainty principle.
Therefore, the orbital magnetic dipole moment is also quantized
\\(\\mu_{orb}=|\\gamma|L=\\frac{e}{2m}\\sqrt{l(l+1)}\\bar{h}\\)
\\(\\mu_{orb},z=\\gamma L_z=-m_l\\frac{e\\bar{h}}{2m}=-m_l\\mu_B\\)
\u200b Define the Bohr magneton \\(\\mu_B=\\frac{eh}{4\\pi m}=\\frac{e\\bar{h}}{2m}=9.274*10^{-24}J/T\\)
"},{"location":"GP/AMAS/AMAS/#dynamics-in-a-uniform-magnetic-field","title":"Dynamics in a Uniform Magnetic Field","text":"An external magnetic field B\u20d7 will rotate a magnetic dipole moment \u03bc\u20d7 with a total torque \\(\\vec{\\tau}=\\vec{\\mu}\\times\\vec{B}\\) , or, for an electron with orbital angular momentum \\(\\vec{L}\\) , \\(\\vec{\\tau}=\\frac{d\\vec{L}}{dt}=\\gamma\\vec{L}\\times\\vec{B}\\)
"},{"location":"GP/AMAS/AMAS/#dipole-in-a-nonuniform-magnetic-field","title":"Dipole in a Nonuniform Magnetic Field","text":"\u300cdiphole parallel to the magnetic field.\u300d
"},{"location":"GP/AMAS/AMAS/#the-stern-gerlach-experiment","title":"The Stern-Gerlach Experiment","text":"In the Stern-Gerlach experiment, a narrow beam of silver atoms passes through an electromagnet (with a nonuniform magnetic field) and then lands on a glass detector plate.
An atom in a state with angular momentum equal to one \\((L = 1)\\) would have a magnetic moment with two components relative to the direction of the magnetic field.
In this case the spot on the receiving plate will therefore be split into two, each of them having the same size but half the intensity of the original spot.
The later, or new, quantum theory developed by Heisenberg, Schroedinger, and others, predicted that for an \\(L = 1\\) state the beam should split into three components.
In fact, we now know that a silver atom consists of many electrons ([Kr]4d105s1).
We also know that all those magnetic moments vectorially cancel out except for a single electron \\((5s1)\\), and the orbital dipole moment of that electron is zero.
Therefore, the Stern-Gerlach result posed a serious problem for the new quantum theory.
This was solved when Uhlenbeck and Goudsmit (1925, 1926) proposed that the electron had an intrinsic angular momentum, not associated with its orbital motion.
"},{"location":"GP/AMAS/AMAS/#spin","title":"Spin","text":"The Stern-Gerlach experiment belongs to a class of quantum phenomena involving a quantum degree of freedom called spin, which has no classical counterpart.
Fortunately, one can describe spin and its dynamics without appealing to any mechanical model (such as that of a spinning top), starting with just the observed fact that it is a form of angular momentum.
"},{"location":"GP/AMAS/AMAS/#electron-spin","title":"Electron Spin","text":"Every electron, whether trapped in an atom or free, has a spin angular momentum and a spin magnetic dipole moment that are as intrinsic as its mass and charge. The existence of electron spin was postulated on experimental evidence by George Uhlenbeck and Samuel Goudsmit from their studies of atomic spectra.
For every electron, \\(spin s = 1/2\\) and the electron is said to be a \\(spin-1/2\\) particle. (Protons and neutrons are also \\(spin-1/2\\) particles.)
As with the angular momentum associated with motion, spin angular momentum can have a definite magnitude but does not have a definite direction.
The best we can do is to measure its component along the \\(z\\) axis (or along any axis), and that component can have only the definite values given by \\(S_z =m_s\\bar{h}, for\\ m_s =\u00b1s=\u00b11/2\\).
Here \\(m_s\\) is the spin magnetic quantum number, which can have only two values: \\(m_s = +s = +1/2\\) (the electron is said to be spin up) and \\(m_s = \u2212s = \u22121/2\\) (the electron is said to be spin down).
Proton NMR, which studies the precession of the proton spin in the magnetic field, is a practical medical imaging technique.
Since the Larmor frequency depends on the applied magnetic field, placing a magnetic field gradient across the human body allows you to locate the source of the MRI signal (hence the name Magnetic Resonance Imaging).
"},{"location":"GP/AMAS/AMAS/#spin-orbit-coupling","title":"Spin-Orbit Coupling","text":"Orbital motion cause relative motion of the nucleus to the electron ,creating a magnetic filed(connected with spin)
The spin\u2013orbit interaction Hamiltonian is given by \\(H_{SO}=\\epsilon_{nl}\\vec{L}\\vec{S}\\) where the spin-orbit coupling constant \\(\u03be_{nl}\\), which is positive, is essentially the averaged gradient of the Coulomb interaction.
To solve the Hamiltonian with such an interaction, we need to introduce the total angular momentum \\(\\vec{J}=\\vec{L}+\\vec{S}\\) and and use the expression \\(\\vec{J}\u00b7\\vec{J}=(\\vec{L}+\\vec{S})\u00b7(\\vec{L}+\\vec{S})=\\vec{S}\u00b7\\vec{S}+2\\vec{L}\u00b7\\vec{S}+\\vec{L}\u00b7\\vec{L}\\)
\\(S^2 =s(s+1)\\bar{h} \\\\ L^2 =l(l+1)\\bar{h}^2\\\\J^2=j(j+1)\\bar{h}^2\\)
\\(\\vec{L}\\vec{S}=\\frac{\\bar{h}^2}{2}[j(j+1)-l(l+1)-s(s+1)]\\)
For multiple electrons in the same trap, we must consider the Pauli exclusion principle, named after \\(Wolfgang\\ Pauli\\).
The Pauli principle states that no two electrons confined to the same trap can have the same set of values for their quantum numbers.In other words, there can be two electrons at most at any energy level; they have opposite spins.
This principle applies not only to electrons but also to protons and neutrons, all of which have \\(s = 1/2\\); they are known as fermions.
A remark on fermions and bosons
More generally, fermions are particles with half-integer spin s \\((i.e., s = 1/2, 3/2, etc.)\\). On the other hand, particles with integer s \\((i.e., s = 0, 1, 2, etc.)\\) are called bosons.
Fermions follow the Pauli principle, but bosons do not.
Many particles consisting of matter, such as electrons, protons, and neutrons (s = \u00bd for either of them) are fermions. On the other hand, photons(s = 1) are bosons.
Regarding composite particles consisting of multiple fermions (such as atoms), those with odd number of fermions can behave as fermions while those with even number of fermions as bosons. (\\(^3He\\): \\(2p + 1n + 2e \u2192\\) fermion; \\(^4He: 2p + 2n + 2e\\) \u2192 boson)
"},{"location":"GP/AMAS/AMAS/#the-helium-atom","title":"The Helium Atom","text":"The second simplest atom is the heliuxim atom, which has two 1s electrons (spin up and spin down) in the Coulomb potential of the charge 2e nucleus that obey time-independent Schroedinger\u2019s equation of the form \\((H_1+H_2+V_{12})\u03a8(\\vec{r_1},\\vec{r_2})=E\u03a8(\\vec{r_1},\\vec{r_2})\\) where \\(H_i=-\\frac{\\bar{h^2}}{2m}\\triangledown_i^2\uff08Kinetic\uff09-\\frac{2e^2}{4\\pi\\epsilon_0r_i}\uff08Potential\uff09\\ and\\ V_{12}=\\frac{e^2}{4\\pi\\epsilon_0|\\vec{r_1}-\\vec{r_2}|}\\)
An approximation starts from throwing away the interaction term, which we have not learned to deal with, i.e.,\\((H_1 + H_2) \u03a8_0 = E_0\u03a8_0\\),
The ground state of either \\(H_1\\) or \\(H_2\\) has an energy \\(\u22124E_R\\) (Because of two protons so raidus gets smaller)
and a wave function \\(\u03c8_{100}(\\vec{r})=\\frac{1}{\\pi}(\\frac{2}{a_B})^{3/2}e^{-2r/a_B}\\)
One may verify that \\(\u03a8_0(\\vec{r}_1,\\vec{r}_2)=\u03c8_{100}(\\vec{r_1})\u03c8_{100}(\\vec{r_2})\\) is an exact solution.
The corresponding energy is \\(E_0=-8E_R\\approx -109eV\\)
Others pleae refer to the related PPT
"},{"location":"GP/AMAS/AMAS/#shells-and-subshells","title":"Shells and Subshells","text":""},{"location":"GP/HA/HA/","title":"The Hydrogen Atom","text":""},{"location":"GP/HA/HA/#the-hydrogen-atom","title":"The Hydrogen Atom","text":"
https://www.youtube.com/watch?v=-Y0XL-K0jy0
https://youtube.com/watch?v=acN7E7AUHPk
"},{"location":"GP/HA/HA/#quiz","title":"Quiz","text":"https://www.youtube.com/watch?v=9GOuZAh9Wg0
"},{"location":"GP/HA/HA/#mystery-of-the-hydrogen-atom","title":"Mystery of the Hydrogen Atom","text":"Because the proton\u2019s mass is much greater than the electron\u2019s mass, we shall assume that the proton is fixed in place. So, the atom is a fixed potential trap with the electron moving around inside it.
A hydrogen atom contains an electron that is trapped by the Coulomb force it experiences from the proton, which is the nucleus of the atom.
Under Newtonian laws, the electron would move around the proton, like planets around the Sun, i.e. \\(\\frac{1}{4\\pi\\epsilon_0}\\frac{e^2}{r^2}=m\\frac{v^2}{r}\\)
Multiplying by \u2212r, we obtain \\(E_c=-\\frac{e^2}{4\\pi \\epsilon _0 r}=-mv^2=-2E_k\\)
Alternatively, the total energy of the electron is\\(E=E_k+E_c=\\frac{E_c}{2}=-E_k\\)
However, any charged particle which moves in a curved path will emit electromagnetic radiation, hence losing energy continuously. Why doesn\u2019t the electrical attraction between the electron and the positive charge simply cause the two to collapse together?
Bohr made two bold (and completely unjustified) assumptions:
However, as successful as his theory was on the four visible wavelengths and on why the atom did not simply collapse, it turned out to be quite wrong in almost every other aspect of the atom.
"},{"location":"GP/HA/HA/#physical-meaning-of-the-assumption","title":"Physical meaning of the assumption","text":"The de Broglie wavelength \\(\u03bb\\) of a free particle with momentum p is \\(\u03bb = h/p = h/mv\\).For an electron in a hydrogen atom whose orbital radius \\(r\\), the above equation leads to \\(\u03bb/r = h/rmv = h/L\\). Therefore, we obtain \\(2\u03c0r/\u03bb = L/\\bar{h}\\).
\\(L=n\\bar{h}\\) means that the length of the orbit is an integer multiple of \\(\u03bb\\). Namely, the phase of the electron wave function returns to the initial value by moving for one cycle of the orbit.
"},{"location":"GP/HA/HA/#analysis-based-on-bohrs-model","title":"Analysis based on Bohr\u2019s model","text":"In the Bohr model of the hydrogen atom, the electron\u2019s orbital radius r is quantized and the smallest possible orbital radius (for \\(n = 1\\)) is \\(a_B\\), which is called the Bohr radius.
Can you obtain the length scale, alternatively, from dimension analysis?sIn order that the attraction between electron and nucleus does not simply collapse them together in the Bohr model, the electron should not get any closer to the nucleus than orbital radius aB by taking
The energy of the hydrogen atom, according to the Bohr model, is then \\(E_n=\\frac{1}{2}mv^2-\\frac{1}{4\\pi\\epsilon_0}\\frac{e^2}{r}=-\\frac{E_R}{n^2}\\) where \\(E_R=\\frac{me^4/(4\\pi\\epsilon_0)^2}{2\\bar{h}^2}=13.6eV\\)
The energy of a hydrogen atom (or, equivalently, of its electron) changes when the atom emits or absorbs light. Emission and absorption involve a quantum of light according to \\(\\bar{h}\u03c9_{nm} =E_R(\\frac{1}{n^2} \u2212\\frac{1}{m^2})\\) for integers \\(m > n\\).
The wavelengths of the emitted or absorbed light are given by $ \\frac{1}{\\lambda}=\\frac{E_R}{hc}(\\frac{1}{n^2}-\\frac{1}{m^2})$
The ground-state energy is the lowest energy allowed by Heisenberg\u2019s uncertainty principle.
To find the minimal energy, we solve for \\(\\triangle r\\) \\(\\Rightarrow\\frac{dE}{d(\\triangle r)}=0 \\Rightarrow \\triangle r = \\frac{\\bar{h}^2}{me^2/(4\\pi\\epsilon_0)}=a_B\\) and \\(E=-\\frac{me^4/(4\\pi\\epsilon_0)^2}{2\\bar{h}^2}=-E_R\\)
The energy of the ground state (or any stationary state) is uniquely determined. \u300cThough the uncertainty of \\(t\\ \\&\\ x\\)\u300d
This is because of the energy-time uncertainty principle, \\(\u2206t \u00b7 \u2206E \u2265 \\bar{h}/2\\).
Note, however, both kinetic energy and potential energy have uncertainties, due to the uncertainties of position and momentum.
Hydrogen is a three-dimensional, finite electron trap, with walls that vary in depth with distance.
"},{"location":"GP/HA/HA/#schroedingers-equation-for-the-h-atom","title":"Schroedinger's Equation for the H-atom","text":""},{"location":"GP/HA/HA/#derive","title":"Derive","text":""},{"location":"GP/HA/HA/#ground-state-wave-function","title":"Ground State Wave Function","text":"\\(\u03c8_{100}(\\vec{r})=R_{10}(r)=\\frac{1}{\\sqrt{\\pi}a_B^{3/2}}e^{\\frac{r}{a_B}}\\)
Note that the hydrogen atom in its ground state has zero angular momentum (\\(l = 0\\) more details will be discussed in the next lecture), which is not predicted in the Bohr model.)
The probability that the electron can be detected in any given (infinitesimal) volume element \\(dV\\) located at radius \\(r\\) from the center of the atom is \\(|\u03c8_{100}(\\vec{r})|^2dV.\\)
"},{"location":"GP/Polar/Polarization/#preknowledge","title":"PreKnowledge","text":"
To observe interference of the two waves
In this lecture we consider the direction of \\(\\vec{E}\\)
Light is a transverse electromagnetic wave. Thus far we have considered only light for which the orientation of the electric field is constant, although its magnitude and sign vary in time.
In general, we can consider two such harmonic lightwaves of the same frequency, moving through the same region of space, in the same direction \\(\\hat{z}\\)
\\(\\vec{E}_x(z,t)=\\hat{i}E_{0x}cos(kz\u2212\u03c9t)\\)
\\(\\vec{E}_y(z,t)=\\hat{j}E_{0y}cos(kz\u2212\u03c9t+\\epsilon)\\)
"},{"location":"GP/Polar/Polarization/#polarization-and-its-mathematical-description","title":"Polarization and Its Mathematical Description","text":""},{"location":"GP/Polar/Polarization/#linear-polarization","title":"Linear polarization","text":"If \\(\u03b5\\) is zero or an integral multiple of \\(\u00b12\u03c0\\), the resultant wave is \\(\\vec{E}=(\\hat{i}E_{0x}+\\hat{j}E_{0y})cos(kz-wt)\\)
"},{"location":"GP/Polar/Polarization/#circular-polarization","title":"Circular polarization","text":"When both constituent waves have equal amplitudes and \\(\\epsilon=-\\pi/2+2m\\pi\\)(m is an integer) the resultant wave is \\(\\vec{E}=E_0(\\hat{i}cos(kz-wt)+\\hat{j}sin(kz-wt))\\)
When both constituent waves have equal amplitudes and \\(\\epsilon=\\pi/2+2m\\pi\\)(m is an integer) the resultant wave is \\(\\vec{E}=E_0(\\hat{i}cos(kz-wt)-\\hat{j}sin(kz-wt))\\)
The amplitude is unaffected, but \\(\\vec{E}\\) at a fixed z now rotates counterclockwise, and the wave is left-circularly polarized.
A linearly polarized wave can be synthesized from two oppositely polarized circular waves of equal amplitude.
"},{"location":"GP/Polar/Polarization/#a-math-description-of-polarization","title":"A Math Description of Polarization","text":"\\(|H\u27e9=\\begin{pmatrix}1\\\\0\\end{pmatrix}\\\\|V\u27e9= \\begin{pmatrix}0\\\\1\\end{pmatrix}\\)
\\(|D\u27e9=\\frac{1}{\\sqrt{2}}(|H\u27e9+|V\u27e9)=\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\1\\end{pmatrix}\\)
\\(|A\u27e9=\\frac{1}{\\sqrt{2}}(|H\u27e9\u2212|V\u27e9)=\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\-1\\end{pmatrix}\\)
Note that we are only interested in polarization, so the vectors are normalized, or in one unit length
\\(|R\u27e9=\\frac{1}{\\sqrt{2}}(|H\u27e9\u2212i|V\u27e9)=\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\-i\\end{pmatrix}\\)
\\(|L\u27e9=\\frac{1}{\\sqrt{2}}(|H\u27e9+i|V\u27e9)=\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\i\\end{pmatrix}\\)
"},{"location":"GP/Polar/Polarization/#orthogonal","title":"Orthogonal","text":"Note that in the lecture on interference, we discussed the addition in a two-dimensional real space \\(R_2\\). The space is also equivalent to a one-dimensional complex space \\(C_1\\), or a two-component real vector space.
Now, with polarization, we have generalized the one-dimensional complex space \\(C_1\\) to a two-dimensional complex space \\(C2 = C1 \u2297 C1\\), or a two-component complex vector space (in Jones\u2019 vector representation). The additional \\(C_1\\) space is spanned by the two orthogonal, linearly polarized states \\(|H\u27e9\\) and \\(|V\u27e9\\).
similarly, two complex vectors A\u20d7 and B\u20d7 are said to be orthogonal when \\(\u27e8A|B\u27e9 \u2261 \\vec{A}^{*} \u00b7 \\vec{B} = 0\\).
Notice that\\(\u27e8H|V\u27e9 = \u27e8D|A\u27e9 = \u27e8L|R\u27e9 = 0\\).
As we have seen, any polarization state can be described by a linear combination of the vectors in either one of the orthogonal sets.
These same ideas are of considerable importance in quantum mechanics, where one deals with orthonormal wave functions.
"},{"location":"GP/Polar/Polarization/#monochromatic-light-and-natural-light","title":"Monochromatic Light and Natural Light","text":""},{"location":"GP/Polar/Polarization/#light-trains-and-monochromatic-light","title":"Light Trains and Monochromatic Light","text":"In reality, a non-laser source emits, to the best, quasimonochromatic light trains, whose frequency can be represented by a bell-shaped Gaussian function.
\"\u51c6\u5355\u8272\"\uff08quasi-monochromatic\uff09\u662f\u6307\u5149\u6ce2\u5728\u9891\u7387\u4e0a\u7684\u5206\u5e03\u76f8\u5bf9\u72ed\u7a84\uff0c\u4f46\u5e76\u975e\u5b8c\u5168\u5355\u4e00\u9891\u7387\u7684\u5149\u3002\u51c6\u5355\u8272\u5149\u901a\u5e38\u5177\u6709\u4e00\u4e2a\u4e3b\u5bfc\u7684\u4e2d\u5fc3\u9891\u7387\uff0c\u800c\u5728\u8be5\u4e2d\u5fc3\u9891\u7387\u5468\u56f4\u6709\u4e00\u5b9a\u7684\u9891\u7387\u5bbd\u5ea6\u3002\u8fd9\u79cd\u5149\u7684\u9891\u8c31\u5f62\u72b6\u53ef\u80fd\u7c7b\u4f3c\u4e8e\u9ad8\u65af\u5206\u5e03\u6216\u5176\u4ed6\u8fd1\u4f3c\u5355\u5cf0\u7684\u5206\u5e03\u3002
That is, the irradiance (hence its square root, the amplitude) versus frequency is found to be Gaussian with a width \\(\u2206\u03c9 = 2\u03c0\u2206\u03bd.\\)
Quasimonochromatic light resembles a series of randomly phased finite wave trains.Such a disturbance is nearly sinusoidal,although the frequence dose vary slowly about some mean value.
Moreover,the amplitude fluctuates as well,but this too is a comparatively slow variation.
The average constittuent wavetrain exists roughly for the coherence time \\(\\triangle t_c=\\frac{1}{\\triangle v}\\)
\u4e24\u5217\u6ce2\u80fd\u53d1\u751f\u5e72\u6d89\u7684\u6700\u5927\u5149\u7a0b\u5dee\u53eb\u76f8\u5e72\u957f\u5ea6\\(\\delta_M=\\frac{\\lambda^2}{\\triangle\\lambda}\\) \\(\\lambda\\)\u4e3a\u4e2d\u5fc3\u6ce2\u957f
\u5149\u901a\u8fc7\u76f8\u5e72\u957f\u5ea6\u6240\u9700\u65f6\u95f4\u53eb\u76f8\u5e72\u65f6\u95f4
\u76f8\u5e72\u65f6\u95f4\u7684\u5b9a\u4e49\u662f\u57fa\u4e8e\u5149\u7684\u6ce2\u52a8\u6027\u548c\u5e72\u6d89\u6027\u7684\u3002\u5f53\u4e24\u675f\u5149\u540c\u65f6\u7167\u5c04\u5728\u540c\u4e00\u70b9\uff0c\u5982\u679c\u5b83\u4eec\u7684\u76f8\u4f4d\u5173\u7cfb\u4fdd\u6301\u4e0d\u53d8\uff0c\u90a3\u4e48\u5b83\u4eec\u5c31\u4f1a\u53d1\u751f\u5e72\u6d89\u3002\u76f8\u5e72\u65f6\u95f4\u5c31\u662f\u63cf\u8ff0\u8fd9\u79cd\u76f8\u4f4d\u5173\u7cfb\u4fdd\u6301\u4e0d\u53d8\u7684\u65f6\u95f4\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5982\u679c\u5149\u7684\u7535\u78c1\u573a\u5728\u4e00\u4e2a\u76f8\u5e72\u65f6\u95f4\u5185\u7684\u5e73\u5747\u503c\u4e0d\u53d8\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u8bf4\u8fd9\u675f\u5149\u5728\u8fd9\u4e2a\u65f6\u95f4\u5185\u662f\u76f8\u5e72\u7684\u3002\u76f8\u5e72\u65f6\u95f4\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\\(T_c=1/\u0394\u03bd\\)\uff0c\u5176\u4e2d\u0394\u03bd\u4e3a\u5149\u7684\u9891\u7387\u5bbd\u5ea6
\u5149\u7684\u5355\u8272\u6027\u597d\uff0c\u76f8\u5e72\u957f\u5ea6\u548c\u76f8\u5e72\u65f6\u95f4\u5c31\u957f\uff0c\u65f6\u95f4\u76f8\u5e72\u6027\u4e5f\u5c31\u597d
An idealized monochromatic plane wave must be depicted as an infinite wavetrain. If this disturbance is resolved into two orthogonal components perpendicular to the direction of propagation, they, in turn, must have the same frequency, be infinite in extent, and therefore be mutually coherent (i.e., \\(\u03b5 = constant\\)).
\\(\\vec{E}_x(z,t)=\\hat{i}E_{0x}cos(kz\u2212\u03c9t)\\)
\\(\\vec{E}_y(z,t)=\\hat{j}E_{0y}cos(kz\u2212\u03c9t+\\epsilon)\\)
A perfectly monochromatic plane wave is always polarized.
The most spectacular of all present-day sources is the laser. Under optimum conditions, with temperature variations and vibrations meticulously suppressed, a laser was actually operated at quite close to its theoretical limit of frequency constancy.
For example, a short-term frequency stability of about 8 parts per \\(10^{14}\\) was attained with a He\u2013Ne continuous gas laser at \\(\u03bb0 = 1153 nm\\) [Jaseja et al., Phys. Rev. Lett. 10, 165 (1963)].
That corresponds to a remarkably narrow bandwidth of about \\(\u2206\u03bd = 20 Hz\\).
"},{"location":"GP/Polar/Polarization/#natural-light","title":"Natural Light","text":"Natural light is composed of a rapidly varying succession (~\\(10^{\u22128}\\) s) of the different polarization states. It is also known as unpolarized or randomly polarized light.
We can mathematically represent natural light in terms of two arbitrary, incoherent, orthogonal, linearly polarized waves of equal amplitude (i.e., waves for which the relative phase difference varies rapidly and randomly).
"},{"location":"GP/Polar/Polarization/#coherence","title":"Coherence","text":"Coherence is a measure of the correlation between the phases measured at different (temporal and spatial) points on a wave.
Temporal coherence is a measure of the correlation of light wave\u2019s phase at different points along the direction of propagation \u2013 it tells us how monochromatic a source is. (Think about the description of quasimonochromatic light.)
Spatial coherence is a measure of the correlation of light wave\u2019s phase at different points transverse to the direction of propagation \u2013 it tells us how uniform the phase of the wavefront is. (Think about Young\u2019s interference experiment.)
The figure illustrates how to prepare a monochromatic wave that is both temporally and spatially coherent from incoherent natural light.
In reality, light is generally neither completely polarized nor completely unpolarized.
More often, the electric-field vector varies in a way that is neither totally regular nor totally irregular, and such an optical disturbance is partially polarized.
One useful way of describing this behavior is to envision it as the result of the superposition of specific amounts of natural and polarized light.
"},{"location":"GP/Polar/Polarization/#polarizing-sheets","title":"Polarizing Sheets","text":"Unpolarized visible light can be transformed into polarized light by sending it through a polarizing sheet,or a Polaroid sheet.
Electric field oscillations of unpolarized light can resolve into two components with equal intensity
For polarized light,obly the component \\(E_y\\)=\\(Ecos\\theta\\) parallel to the polarizing direction of the sheet can be transmitted.Therefore,the intensity of the emerging wave is \\(I_0=I_0cos^2\\theta\\)
One of the most common sources of polarized light is the ubiquitous process of reflection from dielectric media.
Consider a ray of unpolarized light incident on a glass surface. The field \\(\\vec{E}\\) of the incident light can be decomposed into two components of equal magnitude, one perpendicular and another parallel to the plane of incidence.
In general, the reflected light is partially polarized.
When the light is incident at a particular incident angle, called the Brewster angle \\(\u03b8_B\\) , the reflected light is fully polarized.
One finds experimentally that at the incident angle \\(\u03b8_B\\), the reflected and refracted rays are perpendicular to each other: \\(\\theta_B+\\theta_r=\\frac{2}{\\pi}\\)
According to Snell\u2019s law \\(n_i sin\u03b8_B = n_r sin\u03b8_r\\)
we have \\(n_i sin\u03b8_B =n_r sin\u03b8_r =n_r sin(\\frac{\\pi}{2}\u2212\u03b8_B)=n_r cos\u03b8_B\\) or \\(\\theta_B=tan^{-1}\\frac{n_r}{n_i}\\)
If the incident and reflected rays travel in air, we can approximate \\(n_i\\) as unity, so \\(n_r\\)=\\(tan\\theta_B\\)
"},{"location":"GP/QW/QW/","title":"Quantum Wells","text":""},{"location":"GP/QW/QW/#quantum-wells","title":"Quantum Wells","text":"
On a stretched string, we can set up both traveling waves and standing waves.
In other words, confining the wave to a finite region of space leads to quantization of the motion \u2014 to the existence of discrete states for the wave, each state with a sharply defined frequency.
This observation applies to waves of all kinds, including matter waves. For matter waves, however, it is more convenient to deal with the energy E of the associated particle than with the frequency f of the wave.
Consider the matter wave associated with an electron moving in the positive x direction and subject to no net force \u2014 a so-called free particle. The energy of such an electron can have any reasonable value, just as a wave traveling along a stretched string of infinite length can have any reasonable frequency.
Consider next the matter wave associated with an atomic electron, perhaps the valence (least tightly bound) electron.
The electron \u2014 held within the atom by the attractive Coulomb force between it and the positively charged nucleus \u2014 is a bound particle. It can exist only in a set of discrete states, each having a discrete energy E. This sounds much like the discrete states and quantized frequencies that apply to a stretched string of finite length.
For matter waves, then, as for all other kinds of waves, we may state a confinement principle: Confinement of a wave leads to quantization \u2014 that is, to the existence of discrete states with discrete energies.
"},{"location":"GP/QW/QW/#one-dimensional-infinite-potential-well","title":"One-Dimensional Infinite Potential Well","text":"Consider a nonrelativistic electron confined to a one-dimensional electron trap (or a limited region of space).
"},{"location":"GP/QW/QW/#standing-waves-in-a-1d-trap","title":"Standing Waves in a 1D Trap","text":"We examine by analogy with standing waves on a string of finite length, stretched along an x axis and confined between rigid supports.
Because the supports are rigid, the two ends of the string are nodes, or points at which the string is always at rest. The states, or discrete standing wave patterns in which the string can oscillate, are those for which the length L of the string is equal to an integer number of half-wavelengths
That is, the string can occupy only states for which \\(L=\\frac{n\\lambda}{2}\\)
Each value of the integer n identifies a state of the oscillating string. For a given n, the transverse displacement of the string at any position x \\((0 \u2264 x \u2264 L)\\) along the string is given by \\(y_n(x)=Asin(\\frac{n\\pi}{L}x)\\)where A is the amplitude of the standing wave
For the n state \\(\\lambda_n=\\frac{2L}{n}\\) Thus \\(sinkx=sin\\frac{2\\pi}{\\lambda_n}x=sin(\\frac{n\\pi}{L}x)\\)
For the electron in the trap, we promote the transverse displacement to wave function \\(\u03c8_n(x)\\).
"},{"location":"GP/QW/QW/#probability-of-detection","title":"Probability of Detection","text":"Classically, we expect to detect the electron anywhere in the infinite well with a constant probability density.
Quantum mechanically, we find the probability density \\(p_n(x) = |\u03c8_n(x)|^2 = |A|^2 sin^2(\\frac{n\\pi}{L}x)\\) for given n
If don't satisfy \\(L=\\frac{n\\lambda}{2}\\) no stable wave formation!
The constant A (up to a phase) can be determined by the normalization condition
\\(\\int_{-\u221e}^{+\u221e}|\u03c8_n(x)|^2dx =\\int_0^L|\u03c8_n(x)|^2dx = 1\\), so \\(A=\\sqrt{2/L}\\)
"},{"location":"GP/QW/QW/#energies-of-the-trapped-electron","title":"Energies of the Trapped Electron","text":"The de Broglie wavelength \u03bb of the electron is defined as \\(\u03bb=\\frac{h}{p}=\\frac{h}{\\sqrt{2mk}}\\),where \\(K=\\frac{p^2}{2m}\\)is the kinetic enerrgy of the nonrelativistic electron
For an electron moving within the central cylinder, where U = 0, the total (mechanical) energy E is equal to the kinetic energy K.
Narrower well \\((smaller\\ L)\\) \\(\u21d2 E_n \u2197\\).
The positive integer n is the quantum number of the electron's quantum state in the trap
The quantum state with the lowest possible energy level \\(E_1\\) with quantum number n = 1 is called the ground state of the electron.
Why is \\(n = 0\\) not allowed?
Choosing n = 0 would indeed yield a lower energy of zero. However, as we will see below, the corresponding probability density is \\(|\u03c8|^2 = 0\\), which we can interpret only to mean that there is no electron in the well
so \\(n = 0\\) is not a possible quantum number.
It is an important conclusion of quantum physics that confined systems must always have a certain non-zero minimum energy called the zero-point energy.
Electrons can be excited or de-excided bynthe absorption or emission of a photon with energy \\(\\bar{h}w=\\frac{hc}{\\lambda}=\\triangle E=E_{High}-E_{Low}\\)
If we solve time-independent Schroedinger\u2019s equation, as in the previous lecture, for an electron trapped in the 1D infinite well of width L, we could write the solutions as \\(\u03c8_n(x)=exp (i\\frac{n\\pi}{L}x)\\)or\\(\u03c8_n(x)=exp(-i\\frac{n\\pi}{L}x)\\).
The appropriate solutions can only be certain linear combinations of the traveling wave functions, given by \\(\u03c8_n(x)=Asin(\\frac{n\\pi}{L}x)\\) The constant A is to be determined.
For sufficiently large \\(n\\), the probability of detection becomes more and more uniform across the well in the coarse-grained scale. This result is an instance of a general principle called the correspondence principle: At large enough quantum numbers, the predictions of quantum physics merge smoothly with those of classical physics.
"},{"location":"GP/QW/QW/#an-electron-in-a-finite-well","title":"An Electron in a Finite Well","text":"We can picture an electron trapped in a one-dimensional well between infinite-potential walls as being a standing matter wave. The solutions must be zero at the infinite walls.
For finite walls, however, the analogy between waves on a stretched string and matter waves fails. Matter wave nodes no longer exist at \\(x = 0\\) and at \\(x = L\\).
wave function can penetrate the walls into classically forbidden regions.
Constraints
(1)\\(\u03a8(-\\infty)\\rightarrow0\\)
(2)\\(\u03a8(+\\infty)\\rightarrow0\\)
(3)\\(\u03a8(0-)=\u03a8(0+)\\)
(4)\\(\u03a8(L-0)=\u03a8(L+0)\\)
(5)\\(\u03a8'(-0) = \u03a8'(+0)\\)
(6)\\(\u03a8'(L-0) = \u03a8'(L+0)\\)
"},{"location":"GP/QW/QW/#energies-of-the-trapped-electron_1","title":"Energies of the Trapped Electron","text":"Thus, the corresponding energy E \u2248 \\((h/\u03bb)^2/(2m)\\) for an electron in any given state is less in the finite well than in the infinite well.\uff08From what is mentioned above\uff09
An electrons with an energy greater than the well depth\\((E > U_0)\\) has too much energy to be trapped in the finite well.
Thus, there is a continuum of energies beyond the top of potential well ; a high-energy electron is not confined,and its energy is not quantized
"},{"location":"GP/QW/QW/#semiconductor-quantum-wells","title":"Semiconductor Quantum Wells","text":"Semiconductor similar to finite wells
"},{"location":"GP/QW/QW/#schroedingers-equation-in-high-dimensions","title":"Schroedinger\u2019s Equation in High Dimensions","text":"This has the form \\(E = F(x) + G(y)\\), which can only be satisfied when \\(F(x) = E_1\\) and \\(G(y) = E \u2212 E_1\\), i.e., each function must separately be a constant.
As a consequence, separation of variables breaks the multivariate partial differential equation into a set of independent ordinary differential equations \\((ODEs)\\).
We can solve the \\(ODEs\\) for \\(X(x)\\) and \\(Y(y)\\). The wave function for the original equation is simply their product \\(X(x)Y(y)\\).
[In which case \\(\u03a8(x,y)\\) can be written in the form of \\(\u03a8(x,y) = X(x)Y(y\\)), and in which case cannot be?]
It is especially useful in solving equations arising in mathematical physics, such as Laplace\u2019s equation, Helmholtz\u2019s equation, and Schroedinger\u2019s equation.
\\(U (x , y ) = Ux (x ) + Uy (y )\\),or, in a central potential in spherical coordinates,\\(U(r,\u03b8,\u03c6) = V(r).\\)
"},{"location":"GP/QW/QW/#2d-3d-infinite-potential-wells","title":"2D & 3D Infinite Potential Wells","text":""},{"location":"GP/QW/QW/#dirac-delta-function-potential-well","title":"Dirac Delta Function Potential Well","text":""},{"location":"GP/com/com/","title":"Electromagnetic Integration","text":""},{"location":"GP/com/com/#electromagnetic-integration","title":"Electromagnetic Integration","text":""},{"location":"GP/com/com/#faradays-law-of-induction","title":"Faraday\u2019s Law of Induction","text":"
\\(\\Phi_B=\\int\\vec{B}\\cdot d\\vec{A}\\)
\\(\\epsilon = -N\\frac{d\\Phi_B}{dt}\\)
When you move the magnet toward or away from the loop, a magnetic force resists the motion, so Lenz\u2019s law requires your applied force to do positive work.
At the same time, thermal energy is produced in the material of the loop because of the material\u2019s electrical resistance to the induced current.
The energy you transfer to the closed loop-magnet system via your applied force ends up in this thermal energy. (For now, we neglect energy that is radiated away from the loop as electromagnetic waves during the induction.)
\\(\\epsilon= N\\frac{d\\Phi_B}{dt}=BLv\\)
\\(F=F_1=iLB=B^2L^2v/R\\)
\\(P_1=Fv=B^2L^2v^2/R\\)
\\(P_{thermal}=i^2R=(\\frac{BLv}{R})^2R=B^2L^2v^2/R\\)
"},{"location":"GP/com/com/#a-reformulation-of-faradays-law","title":"A Reformulation of Faraday\u2019s Law","text":"We find that an induced emf can be defined without the need of a current or particle: An induced emf is the sum\u2014via integration\u2014of quantities \\(\\vec{E} \u00b7 d\\vec{s}\\) around a closed path, where \\(\\vec{E}\\) is the electric field induced by a changing magnetic flux and \\(d\\vec{s}\\) is a differential length vector along the path.
Rewrite Faraday's Law as \\(\\oint \\vec{E}\\cdot d\\vec{s}=-N\\frac{d\\Phi_B}{dt}=-\\frac{d}{dt}\\int\\vec{B}\\cdot d\\vec{A}\\)
We can convert it to differential form by applying the Stokes\u2019 theorem (or the fundamental theorem for curls)
\\(\\oint\\vec{E}\\cdot d\\vec{s}=\\int_S(\\triangledown \\times \\vec{E})\\cdot d\\vec{A}\\)
Thus, we get : \\(\\triangledown \\times\\vec{E}=-\\frac{\\partial \\vec{B}}{\\partial t}\\)
Electric potential has no meaning for electric fields that are produced by induction
\\(B=\\mu_0in\\)
\\(\\Phi_B=BA=\\mu_0inA\\)
\\(Inductance \\ L \\ =N\\Phi_B/i=\\mu_0n^2lA\\)
L is like a voltage source
\\(E_L = \u2212\\frac{d(N\u03a6_B)}{dt} = \u2212L\\frac{di }{dt}\\)
\\(\\begin{align*}&\\epsilon= iR-(-L\\frac{di}{dt})\\\\ &\\dot{i}+\\frac{R}{L}i-\\frac{\\epsilon}{L}=0\\end{align*}\\)
\\(i=\\frac{\\epsilon}{R}(1-e^{-t/\\tau_L}) \\ \\ \\ (\\tau_L=\\frac{L}{R})\\)
"},{"location":"GP/com/com/#energy","title":"Energy","text":""},{"location":"GP/com/com/#energy-stored-in-a-magnetic-field","title":"Energy Stored in a Magnetic Field","text":"\\(\\epsilon i = Li\\frac{di}{dt} + i^2R.\\)
\\(\\Rightarrow U_B=\\frac{1}{2}Li^2\\)
"},{"location":"GP/com/com/#energy-density-of-a-magnetic-field","title":"Energy Density of a Magnetic Field","text":"\\(\\mu_B=\\frac{U_B}{Ah}=\\frac{Li^2}{2Ah}=\\frac{L}{h}\\frac{i^2}{2A}=\\frac{i^2n^2\\mu_0}{2}=\\frac{B^2}{2\\mu_0}\\)
resembles that in an electric file \\(\\mu_E=\\frac{1}{2}\\epsilon_0E^2\\)
"},{"location":"GP/com/com/#mutual-inductance-of-two-parallel-coils","title":"Mutual Inductance of Two Parallel Coils","text":"\\(M_{21}=\\frac{N_2\\Phi_{21}}{i_1}\\\\M_{12}=\\frac{N_1\\Phi_{12}}{i_2}\\)
\\(\\epsilon_{21}=-M_{21}\\frac{di_1}{dt}\\\\\\epsilon_{12}=-M_{12}\\frac{di_2}{dt}\\)
\\(\\epsilon_1=\\epsilon_{1}+\\epsilon_{12}=-L_{1}\\frac{di_{1}}{dt}-M_{12}\\frac{di_2}{dt}\\)
\\(\\epsilon_2=\\epsilon_{22}+\\epsilon_{21}=-L_{2}\\frac{di_{2}}{dt}-M_{21}\\frac{di_1}{dt}\\)
\\(\\Rightarrow-\\begin{pmatrix}L_1&M_{12}\\\\M_{21}&L_2\\end{pmatrix}\\frac{d}{dt}\\begin{pmatrix}i_1\\\\i_2\\end{pmatrix}=\\begin{pmatrix}\\epsilon_1\\\\\\epsilon_2\\end{pmatrix}\\)
"},{"location":"GP/com/com/#alternating-current-circuits","title":"Alternating-Current Circuits","text":""},{"location":"GP/com/com/#lc-oscillations","title":"LC Oscillations","text":"\\(L\\frac{di}{dt}+\\frac{q}{C}=0\\)
\\(\\ddot{q}+\\frac{1}{LC}q=0\\)
\\(\\Rightarrow q =Acos(w_0t+\\phi)=Qcos(\\frac{1}{\\sqrt{LC}}t+\\phi)\\)
\\(\\Rightarrow Qcos\\phi=0 \\\\-w_0Qsin\\phi=0\\)
"},{"location":"GP/com/com/#the-complex-formalism","title":"The Complex Formalism","text":""},{"location":"GP/com/com/#damped-oscillations-in-an-rlc-circuit","title":"Damped Oscillations in an RLC Circuit","text":"\\(\\ddot{q}+\\frac{R}{L}\\dot{q}+\\frac{1}{LC}q=0\\)
\\(\\Rightarrow q=Qe^{-\\frac{t}{\\tau}}cos(wt+\\phi) \\ where\\ \\tau=2L/R \\ and \\ w=\\sqrt{w_0^2-(1/\\tau)^2} \\ and \\ w_0=\\frac{1}{\\sqrt{LC}}\\)
Note Use ODEs or the method of the below picture
When \\(1/\u03c4 > \u03c9_0\\), one can only find imaginary \u03c9, which means the frictional force is so great that the system cannot oscillate. The circuit is said to be overdamped.
In between, when \\(1/\u03c4 = \u03c9_0\\), the circuit is said to be critically damped. It is worth noting that the critical damping gives the fastest return of the system to its equilibrium position. In engineering design this is often a desirable property.
\\(i(t)=\\tilde{i}e^{iwt}\\)
\\(V(t)=L\\frac{di}{dt}=L(iw)e^{iwt}\\tilde{i}\\)
\\(\\tilde{Z}=iwL\\)
\\(Q(t)=\\tilde{Q}e^{iwt}\\)
\\(\\tilde{V}(t)=\\tilde{Q}/C\\cdot e^{iwt}\\)
\\(\\tilde{i}=\\frac{d\\tilde{Q}}{dt}=iw\\tilde{\\tilde{Q}e^{iwt}}\\)
\\(\\tilde{Z}=\\frac{1}{iwC}=-\\frac{i}{wC}\\)
"},{"location":"GP/com/com/#the-series-rlc-circuit","title":"The Series RLC Circuit","text":""},{"location":"GP/com/com/#resonance","title":"Resonance","text":"When \\(\u03c9_d\\) equals \\(\u03c9_0\\), the circuit is in resonance.
Applying Divergence to Faraday's Law ,we get:\\(\\triangledown\\cdot(\\triangledown \\times \\vec{E})=\\triangledown \\cdot(-\\frac{\\partial\\vec{B}}{\\partial t})=-\\frac{\\partial}{\\partial t}(\\triangledown\\cdot\\vec{B})=0 (Consistency)\\)
However,when applying Divergence to Ampere's Law,we get\\(\\triangledown\\cdot(\\triangledown \\times \\vec{B})=\\triangledown\\cdot(\\mu_0\\vec{J})=\\mu_0(\\triangledown\\cdot\\vec{J})\\)
While \\(\\triangledown \\cdot \\vec{J} = -\\frac{\\partial\\rho}{\\partial t}=-\\frac{\\partial{(\\epsilon_0\\triangledown\\cdot E)}}{\\partial t} =-\\triangledown\\cdot(\\epsilon_0\\frac{\\partial\\vec{E}}{\\partial t}) \\ Which \\ is\\ not\\ necessarily\\ ZERO\\)
\\(\\triangledown\\times\\vec{B}=\\mu_0\\vec{J}+\\mu_0\\vec{J_d}=\\mu_0\\vec{J}+\\mu_0\\epsilon_0\\frac{\\partial\\vec{E}}{\\partial t}\\)
"},{"location":"GP/com/com/#apply","title":"Apply","text":"\\(E=\\frac{\\sigma}{\\epsilon_0}=\\frac{Q}{\\epsilon_0A}\\\\ \\frac{\\partial{E}}{\\partial t}=\\frac{J_d}{\\epsilon_0}=\\frac{I}{\\epsilon_0A}\\)
Choose surface 1 to integral : \\(E=0\\ I_{enc}=I\\)
Choose surface 2 to integral:
\\(I_{enc}=0\\\\\\mu_0\\epsilon_0\\frac{\\partial{E}}{\\partial t}=\\mu_0I/A \\ \\iint \\mu_0\\epsilon_0\\frac{\\partial{E}}{\\partial t}\\cdot d\\vec{A}=\\mu_0I\\)
"},{"location":"GP/com/com/#maxwells-equations","title":"Maxwell\u2019s Equations","text":""},{"location":"GP/com/com/#electromagnetic-waves","title":"Electromagnetic Waves","text":""},{"location":"GP/com/com/#derivation-of-the-wave-equation","title":"Derivation of the Wave Equation","text":"1.Decouple
\\(\\triangledown\\times(\\triangledown\\times\\vec{E})=\\triangledown\\times(-\\frac{\\partial\\vec{B}}{\\partial t})=-\\frac{\\partial}{\\partial t}(\\triangledown\\times\\vec{B})=-\\mu_0\\epsilon_0\\frac{\\partial^2\\vec{E}}{\\partial t^2}\\)
\\(\\triangledown\\times(\\triangledown\\times\\vec{B})=\\triangledown\\times(\\mu_0\\epsilon_0\\frac{\\partial\\vec{E}}{\\partial t})=\\mu_0\\epsilon_0\\frac{\\partial}{\\partial t}(\\triangledown\\times\\vec{E})=-\\mu_0\\epsilon_0\\frac{\\partial^2\\vec{B}}{\\partial t^2}\\)
Another way to solution :
\\(\\vec{A}\\times(\\vec{B}\\times\\vec{C})=\\vec{B}\\cdot(\\vec{A}\\cdot \\vec{C})-\\vec{C}\\cdot(\\vec{A}\\cdot \\vec{B})\\)
\\(\\triangledown\\times(\\triangledown\\times\\vec{C})=\\triangledown\\cdot(\\triangledown\\cdot \\vec{C})-\\triangledown^2\\vec{C}\\)
Thus:
\\(\\triangledown^2\\vec{B}=\\mu_0\\epsilon_0\\frac{\\partial^2\\vec{B}}{\\partial t^2}\\\\ \\triangledown^2\\vec{E}=\\mu_0\\epsilon_0\\frac{\\partial^2\\vec{E}}{\\partial t^2}\\)
\\(\\lambda=\\frac{2\\pi}{k}\\\\ v=\\lambda f=\\lambda/T \\\\w=\\frac{2\\pi}{T}\\)
We have \\(E_m(x)=B_m(x)=0\\)
\\(-k\\hat{x}\\times\\vec{E_m}=-w\\vec{B_m}\\)
When spherical wavefronts spread from an isotropic point source S with power Ps, the energy of the waves is conserved.
The intensity I at the sphere must decrease with r as \\(I=\\frac{P_S}{4\\pi r^2}\\)
"},{"location":"GP/current/current/","title":"Current","text":""},{"location":"GP/current/current/#current","title":"Current","text":""},{"location":"GP/current/current/#resistance-and-capacitance","title":"Resistance and Capacitance","text":"
The resistivity \u03c1 and conductivity \u03c3 of a material are related by \\(\u03c1 = 1/\u03c3 = E/J\\), where \\(E\\) is the magnitude of the applied electric field and \\(J\\) is the magnitude of the current density.
\\(\\vec{a}=-\\frac{e\\vec{E}}{m}\\)
After a typical collision, each electron will \u201close its memory\u201d of its previous drift velocity, starting fresh and moving off in a random direction.
In the average time \\(\u03c4\\) (or mean free time) between collisions, the electron will, on average, acquire a drift velocity
\\(\\vec{v_d}=-\\frac{e\\vec{E}}{m}\\tau \\ \\ \\ Combining \\ with \\vec{J}=-ne\\vec{v_d} \\ We \\ get :\\)
\\(-\\frac{\\vec{J}}{ne}=-\\frac{e\\vec{E}}{m}\\tau \\ \\ \\ which \\ implies \\vec{E}=\\rho\\vec{J} \\ \\ where\\ \\rho=\\frac{m}{ne^2\\tau}\\)
For metals under normal circumstances, both n and \u03c4 can be considered to be a constant. Thus, metals obey Ohm\u2019s law.
Thus,\\(V/I=E\\cdot d/J\\cdot S = \\rho d /S\\)
We have implicitly used the (local) conservation of charge here. Namely, if the total charge in some volume changes, the exactly same amount of charge must have passed in or out through the surface. Otherwise, charge accumulation occurs in the volume enclosed by the surface.
\\(\\frac{d}{dt}\\int_V\\rho(\\vec{r},t)dV = \\int_V \\frac{\\partial{\\rho(\\vec{r},t)}}{\\partial{t}}dV=-\\oint_S\\vec{J}(\\vec{r},t)d\\vec{A}\\)
To be able to move the derivative under the integral sign this way requires that \\(\u2202\u03c1/\u2202t\\) be continuous.
With the divergence theorem, we can rewrite it in the derivative form (note V is arbitrary)\\(\\frac{\\partial \\rho}{\\partial t}=-\\triangledown\\cdot\\vec{J}\\)\uff08By Gauss's Law\uff09
Such an equation of continuity plays an important role in hydrodynamics, hear flow, and diffusion theory, besides electromagnetic theory. It is simply a mathematical expression of a conservation law.
"},{"location":"GP/current/current/#capacitor-and-capacitance","title":"Capacitor and Capacitance","text":"\\(C=\\frac{Q}{U}\\)
"},{"location":"GP/current/current/#capacitance-of-a-parallel-plate-capacitor","title":"Capacitance of a Parallel-Plate Capacitor","text":""},{"location":"GP/current/current/#capacitance-of-a-cylindrical-capacitor","title":"Capacitance of a Cylindrical Capacitor","text":"\\(dW=V'dq'=(\\frac{q'}{C})dq'\\)
\\(W=\\int_0^q(q'/C)dq'=\\frac{q^2}{2C}\\)
\\(U=\\frac{q^2}{2C}=\\frac{CV^2}{2}\\)
"},{"location":"GP/current/current/#dc-circuits","title":"DC Circuits","text":""},{"location":"GP/current/current/#rc-circuits","title":"RC circuits","text":""},{"location":"GP/current/current/#charging-switch-to-a","title":"Charging (switch to a )","text":"\\(\\frac{q}{C}\\frac{dq}{dt}+i^2R=i\\epsilon\\)
\\(\\Rightarrow \\dot{q}+\\frac{q}{RC}=\\frac{\\epsilon}{R}\\)
\\(\\Rightarrow q=C\\epsilon(1-e^{-t/RC})\\)
Note : Derivation see Appendix : ODEs
The equilibrium (final) charge on the then fully charged capacitor is equal to \\(q_0 = CE\\).
\\(\\Rightarrow i = \\frac{dq}{dt}=(\\frac{\\epsilon}{R}e^{-t/RC})\\)
\\(\\Rightarrow V_c = \\frac{q}{C}=\\epsilon(1-e^{-t/RC})\\)
"},{"location":"GP/current/current/#dischargingswitch-to-b-from-a","title":"Discharging(switch to b from a)","text":"\\(\\dot{q}+\\frac{q}{RC}=0\\)
\\(\\Rightarrow q=q_0e^{-t/RC}\\)
\\(\\Rightarrow i = (\\frac{\\epsilon}{R})e^{-t/RC}\\)
"},{"location":"GP/current/current/#energy-density","title":"Energy Density","text":"parallel-plate capacitor:
\\(U=\\frac{1}{2}CV^2=\\frac{1}{2}\\epsilon_0E^2(Ad)\\)
"},{"location":"GP/lec1-3/lec1/#coulombs-law-and-the-electric-field","title":"Coulomb's Law and the Electric Field","text":""},{"location":"GP/lec1-3/lec1/#coulombs-law","title":"Coulomb\u2018s Law","text":"
\\(\\vec{F}=k\\frac{q_1q_2}{r^2}\\)
\\(k = 8.99 * 10^{-12} N*m^2/C^2=\\frac{1}{4\\pi\\epsilon_0}\\\\\\)
\\(\\epsilon_0=8.85*10^{-12}C^2/N*m^2\\)
"},{"location":"GP/lec1-3/lec1/#diphole","title":"Diphole","text":"\u200b \\(\\begin{align*}E&=\\frac{q}{4\\pi\\epsilon_0(z-\\frac{d}{2})^2}- \\frac{q}{4\\pi\\epsilon_0(z+\\frac{d}{2})^2}\\\\ &=\\frac{q\\vec{d}}{2\\pi\\epsilon_0}*\\frac{z}{(z-\\frac{d}{2})^2(z+\\frac{d}{2})^2}\\\\ &=\\frac{\\vec{p}}{2\\pi\\epsilon_0z^3}*\\frac{1}{(1-\\frac{d}{2z})^2(1+\\frac{d}{2z})^2}\\\\ &=\\frac{1}{2\\pi\\epsilon_0}\\frac{p}{z^3} \\ \\ when (z>>d)\\end{align*}\\)
"},{"location":"GP/lec1-3/lec1/#a-diphole-in-a-uniform-electric-filed","title":"A diphole in a uniform electric filed","text":"Note:\\(\\tau=p\\times E\\)
"},{"location":"GP/lec1-3/lec1/#the-electric-field-due-to-a-ring-of-charge","title":"The Electric Field Due to a Ring of Charge","text":"\\(\\begin{align*}dE_z &= \\int \\frac{\\sigma(2\u03c0rdr)}{4\\pi\\epsilon_0(z^2+r^2)}\\frac{z}{\\sqrt{z^2+r^2}}=\\frac{\\sigma z}{2\\epsilon_0}\\int_0^R\\frac{rdr}{(z^2+r^2)^\\frac{3}{2}}\\\\ &= \\frac{\\sigma}{2\\epsilon_0}(1-\\frac{z}{\\sqrt{z^2+R^2}})\\end{align*}\\)
"},{"location":"GP/lec1-3/lec1/#gausss-law","title":"Gauss's Law","text":""},{"location":"GP/lec1-3/lec1/#electric-flux","title":"Electric Flux","text":"\\(\u2206\u03a6 = (E cos\u03b8)\u2206A=\\vec{E}\\cdot{\\vec{A}}\\)
\\(\u03a6=\\int\\vec{E}\\cdot{d\\vec{A}}\\)
"},{"location":"GP/lec1-3/lec1/#gausss-law_1","title":"Gauss's Law","text":"\\(\\epsilon_0 \\oint\\vec{E}\\cdot d\\vec{A}=q_{encore}\\)
"},{"location":"GP/lec1-3/lec1/#applications","title":"Applications","text":""},{"location":"GP/lec1-3/lec1/#1spherical-symmetry","title":"1.Spherical Symmetry","text":"\\(4\\pi r^2\\cdot E = \\frac{q{(\\frac{r}{R})^3}}{\\epsilon_0}\\)
\\(\\Rightarrow \\ \\vec{E}=\\frac{q}{4\\pi\\epsilon_0R^3}\\cdot \\vec{r}\\)
"},{"location":"GP/lec1-3/lec1/#2planar-symmetry","title":"2.Planar Symmetry","text":""},{"location":"GP/lec1-3/lec1/#3cylindrical-symmetry","title":"3.Cylindrical Symmetry","text":"\\(\u03b5_0 \u03a6 = \u03bbh\\)
\\(E=\\frac{\\lambda}{2\\pi r\\epsilon_0}\\)
\\(r_--r_+\\approx dcos\\theta\\)
\\(r_-\\cdot r_+\\approx r^2\\)
\\(V=\\frac{\\vec{p}\\cdot\\vec{r}}{4\\pi\\epsilon_0r^3}\\)
"},{"location":"GP/lec1-3/lec1/#systems-of-charged-particles","title":"Systems of Charged Particles","text":"\\(\\begin{align*} V &= \\int dV = \\int _0^L \\frac{\\lambda dx}{4\\pi\\epsilon_0\\sqrt{x^2+d^2}}\\\\ &=\\int \\frac{\\lambda}{4\\pi\\epsilon_0}\\int_0^L\\frac{dx}{\\sqrt{x^2+d^2}}\\\\ &=\\frac{\\lambda}{4\\pi\\epsilon_0}ln\\frac{L+\\sqrt{L^2+d^2}}{d} \\end{align*}\\)
"},{"location":"GP/lec1-3/lec1/#continuous-charge-distribution-disk","title":"Continuous Charge Distribution: Disk","text":"\\(\\begin{align*}V &= \\int_0^R \\frac{\\sigma(2\u03c0rdr)}{4\\pi\\epsilon_0\\sqrt{z^2+r^2}}\\\\ &= \\frac{\u03c3}{2\\epsilon_0}(\\sqrt{z^2+R^2}\u2212z)\\end{align*}\\)
\\(\\Rightarrow Ez=-\\frac{dV}{dz}\\)
"},{"location":"GP/lec1-3/lec1/#the-triangle-of-electrostatics","title":"The Triangle of Electrostatics","text":""},{"location":"GP/lec1-3/lec1/#preknowledge","title":"PreKnowledge","text":""},{"location":"GP/lec1-3/lec1/#gradient","title":"gradient","text":"\\(\u2207V=\\frac{\\partial{V}}{\\partial{x}}\\hat{x}+\\frac{\\partial{V}}{\\partial{y}}\\hat{y}+\\frac{\\partial{V}}{\\partial{z}}\\hat{z}\\)
"},{"location":"GP/lec1-3/lec1/#curl","title":"curl","text":"\\(\u2207 \\times V = \\begin{bmatrix}\\hat{x}&\\hat{y}&\\hat{z}\\\\\\frac{\\partial{}}{\\partial{x}}&\\frac{\\partial{}}{\\partial{y}}&\\frac{\\partial{}}{\\partial{z}}\\\\ V_x&V_y&V_z\\end{bmatrix}\\)
\\(\u2207 \\cdot V\\)
\\(\\frac{\\partial E_x}{\\partial x}+\\frac{\\partial E_y}{\\partial y}+\\frac{\\partial E_z}{\\partial z}=\\triangledown \\cdot \\vec{E}\\)
"},{"location":"GP/lec1-3/lec1/#e-leftrightarrow-rho","title":"\\(E \\Leftrightarrow \\ \\rho\\)","text":"\\(\\oint\\vec{E}\\cdot d\\vec{l}=\\int\\int(\\triangledown\\times \\vec{E})dS = 0\\) \\(By \\ Stokes\\) -- independent of \\(PATHS\\)
Choose a Gaussian surface to enclose a small cube centered at \\(\\vec{r}\\) with side \\(\\triangledown x\\triangledown y\\triangledown z\\) such that \\(\\triangle V = \\triangle x \\triangle y \\triangle z\\)
For the same conductor with a cavity, is there excess charge on the cavity wall?
If we enclose the conductor in a concentric Gaussian sphere, Gauss\u2019 law tells us that the electric field outside the surface of the conductor looks as if all the excess charge on the shell were concentrated at its center.
"},{"location":"GP/lec1-3/lec1/#electric-field-outside-isolated-conductors","title":"Electric Field Outside Isolated Conductors","text":"There is a charge +q outside a spherical metal shell at a distance\\(r+R\\) from its center,how to decide the image?
In this way \\(V(x,y,z)=\\frac{\\frac{q}{ 4\\pi\\epsilon_0}}{\\sqrt{x^2+y^2+(z-d)^2}}- \\frac{\\frac{q}{ 4\\pi\\epsilon_0}}{\\sqrt{x^2+y^2+(z+d)^2}}\\)
We assert this is the solution for the upper half region.
We can check that\\(V(x,y,z=0)=0\\),and\\(V \u21920\\)for\\(x^2 +y^2 +z^2 \u226bd2.\\)
The only charge in the region z > 0 is q at (0,0,d).
According to Gauss\u2019 law, the surface charge density \u03c3 induced on the conductor is:
\\(E=-\\epsilon_0\\frac{\\partial V}{\\partial z}|_{z=0}=\\frac{-qd}{2\\pi\\epsilon_0(x^2+y^2+d^2)^\\frac{3}{2}}\\hat{z}\\)
\\(\\sigma=\\epsilon_0\\cdot E =\\frac{-qd}{2\\pi(x^2+y^2+d^2)^\\frac{3}{2}}\\)
Derive as little circles
\\(V=\\int_0^\\infty\\frac{qd\\cdot 2\\pi r dr}{2\\pi(r^2+d^2)^{3/2}}\\cdot\\frac{q}{4\\pi\\epsilon_0(r^2+d^2)^{1/2}}=\\frac{q^2d}{8\\pi\\epsilon_0}\\int_0^{\\infty}\\frac{d(r^2)}{r^2+d^2}=-\\frac{q^2}{8\\pi\\epsilon_od}\\)
which is half of the energy for bringing two point charges to a distance 2d with no conductor.
"},{"location":"GP/lec1-3/lec1/#positive-charge-q-and-a-conducting-sphere","title":"positive charge +Q and a conducting sphere","text":"Solve the electro-static potential function \\(V(x,y,z)\\) and the electrostatic field \\(E(x,y,z)\\) in space, with a positive charge \\(+Q\\) located at \\((\u2212D, 0, 0)\\)and a conducting sphere of radius R (not grounded) at origin \\((R<D)\\).
Pre - knowledge :
Image : \\(r'=\\frac{R^2}{D}\\) \\(q'=-qR/D\\)
Derivation :
\\(\\begin{align*}V(\\theta)&=\\frac{q'}{4\\pi\\epsilon_0}\\cdot\\frac{1}{\\sqrt{R^2+r'^2-2Rr'cos\\theta}}+\\frac{Q}{4\\pi\\epsilon_0}\\cdot\\frac{1}{\\sqrt{R^2+D^2-2RDcos\\theta}}=0\\end{align*}\\)
\\(\\Rightarrow \\frac{1}{4\\pi\\epsilon_0}[\\frac{\\frac{q'}{\\sqrt{Rr'}}}{\\sqrt{R/r'+r'/R-2cos\\theta}}+\\frac{\\frac{Q}{\\sqrt{RD}}}{\\sqrt{R/D+D/R-2cos\\theta}}]=0\\)
\\(\\begin{align*}\\Rightarrow &\\frac{q'}{\\sqrt{Rr'}}+\\frac{Q}{\\sqrt{RD}}=0\\\\ &\\frac{R}{r'}+\\frac{r'}{R}=\\frac{R}{D}+\\frac{D}{R}\\end{align*}\\)
\\(\\Rightarrow R/r'=D/R \\Rightarrow r'=R^2/D \\Rightarrow q'=-\\frac{qR}{D}\\)
* Also See https://galileoandeinstein.phys.virginia.edu/Elec_Mag/2022_Lectures/EM_08_Images.html
"},{"location":"GP/lec1-3/lec1/#appendix","title":"Appendix \uff1a","text":""},{"location":"GP/lec1-3/lec1/#cross-product","title":"Cross product","text":"\\(dA=r^2sin\\theta d\\theta d\\Phi\\)
\\(Solid \\ Angle\\ d\\Omega = \\frac{dA}{r^2}=sin\\theta d\\theta d\\Phi\\)
\\(dV=r^2sin\\theta d\\theta d\\Phi dr\\)
"},{"location":"GP/light/inteference/","title":"interference & refraction","text":""},{"location":"GP/light/inteference/#interference","title":"Interference","text":""},{"location":"GP/light/inteference/#the-superposition-of-waves","title":"The Superposition of Waves","text":""},{"location":"GP/light/inteference/#wave-equation","title":"Wave Equation","text":"
Recall that each field component of an electromagnetic wave$ (Ex, Ey, Ez, Bx, By, and Bz) $satisfies the scalar 3D differential wave equation
\\(\\frac{1}{v^2}\\frac{\\partial^2{\\phi}}{\\partial t^2}=\\frac{\\partial^2{\\phi}}{\\partial x^2}+\\frac{\\partial^2{\\phi}}{\\partial y^2}+\\frac{\\partial^2{\\phi}}{\\partial z^2}\\)
This equation is linear \\(\\vec{\\phi}(r,t)\\) and its derivatives appear only to the first power.
Consequently, if \\(\\vec{\\phi}(r,t)\\) are solutions, any linear combination of them \\(\\vec{\\phi}(r,t)=\\sum_{i=1}^nC_i\\vec{\\phi}_i(r,t)\\) will be a solution as well.
\\(cos(\u03b1 + \u03b2) = cos \u03b1 cos \u03b2 \u2212 sin \u03b1 sin \u03b2\\)
\\(cos(\u03b1 \u2212 \u03b2) = cos \u03b1 cos \u03b2 + sin \u03b1 sin \u03b2\\)
\\(cos \u03b1 + cos \u03b2 = 2 cos \\frac{(\u03b1 + \u03b2)}{2} cos\\frac{(\u03b1 - \u03b2)}{2}\\)
\\(cos \u03b1 \u2212 cos \u03b2 = -2 sin \\frac{(\u03b1 + \u03b2)}{2} sin\\frac{(\u03b1 - \u03b2)}{2}\\)
"},{"location":"GP/light/inteference/#the-algebraic-method-of-adding-waves","title":"The Algebraic Method of Adding Waves","text":"\\(E_1 = E_{01}cos(\u03b1_1 \u2212 \u03c9t)\\)
\\(E_2 = E_{02}cos(\u03b1_2 \u2212 \u03c9t)\\)
\\(\u03b1_i = kx_i + \u03c6_i\\) with \\(x_i\\) being the distance from the source \\(s_i\\) of the waveto the point of observation.
The linear combination of the waves is \\(E \u2261 E_0 cos(\u03b1 \u2212 \u03c9t) = E_1 + E_2\\)
We get :
\\(E_0cos\u03b1=E_{01}cos\u03b1_1 +E_{02}cos\u03b1_2\\)
\\(E_0sin\u03b1=E_{01}sin\u03b1_1 +E_{02}sin\u03b1_2\\)
\\(\\Rightarrow \\vec{E_0}=\\vec{E_{01}}+\\vec{E_{02}}\\)
\\(E_0^2=E_{01}^2+E_{02}^2+2E_{01}E_{02}cos(\\alpha_2-\\alpha_1)\\)
The resultant intensity is not simply the sum of the component intensity
there is an additional contribution \\(2E_{01}E_{02}cos(\u03b1_2 \u2212 \u03b1_1)\\), known as the interference term.
The phase difference may arise from a difference in path length traversed by the two waves, as well as a difference in the initial phase angle \\(\u03b4 =\\alpha_2-\\alpha_1= \\frac{2\u03c0}{\\lambda}(x_2 \u2212x_1)+(\u03c6_2 \u2212\u03c6_1).\\)
"},{"location":"GP/light/inteference/#the-complex-method","title":"The Complex Method","text":"It is often mathematically convenient to make use of the complex representation when dealing with the superposition of harmonic waves.
We now redo the calculation of adding two waves.
The wave function \\(E_1 =E_{01}cos(\u03b1_1 \u2212\u03c9t)=E_{01}cos(kx_1 \u2212\u03c9t+\u03c6_1)\\) can be written as \\(\\tilde{E}_1 = E_{01}e^{i(\u03b1_1\u2212\u03c9t)} = E_{01}e^{i\u03b1_1}e^{\u2212i\u03c9t}\\) if we are interested only in the real part.
Now we discuss a graphical technique that uses vector additions instead of trigonometric additions.
We can map a wave, which has an amplitude and a phase, to a vector, known as a phasor, in a two-dimensional plane, such that
"},{"location":"GP/light/inteference/#summary","title":"Summary","text":"Initial phase difference can exist between sources, as long as it remains constant; the two sources are said to be coherent.
Until the advant of laser, no two individual sources can maintain a constant relative phase long enough for an observable interference pattern.
Each point on a wavefront may be regarded as a source of waves expanding from that point.
If waves strike a barrier with a small opening, the waves may be seen to expand from the opening. Notice the wavelength is larger than the opening in this case.
"},{"location":"GP/light/inteference/#youngs-interference-experiment","title":"Young\u2019s Interference Experiment","text":"In other words, the light leaving the slits is in phase.
However, the electric field components of these waves at point P are not in phase and vary with time as
\\(E_1 =E_0cos(kr_1 \u2212\u03c9t)=E_0cos(kL+\u03b2\u2212\u03c9t)\\)
\\(E_2 =E_0cos(kr_2 \u2212\u03c9t)=E_0cos(kL\u2212\u03b2\u2212\u03c9t)\\)
Where the phase difference \\([L = (r_1 + r_2)/2 = \\sqrt{D^2+y^2}]\\) \\([\u03b4_2 = 2\u03b2 = k\u2206L = \\frac{2\u03c0d}{\\lambda} sin\u03b8.]\\)
Therefore, a bright fringe appears when \\(\u2206L = d sin \u03b8 = m\u03bb\\) where m is an integer.
On the other hand, a dark fringe appears when \\(\u2206L=dsin\u03b8= (m+\\frac{1}{2}) \u03bb\\) where m is an integer.
We can then find the angle \u03b8 to any fringe and thus use the values of m to label the fringes.
So, reflecting off higher index, ray \\(r_1\\) has an additionalreflection phase shift 0.5 wavelength. There is no such shift for \\(r_2\\)
In addition, the light waves of rays \\(r_1\\) and \\(r_2\\) has a path difference \\(2L\\), which occurs in index \\(n_2\\).Notice that the wavelength in the medium is \\(\u03bb_2=\\frac{v_2}{f}=\\frac{c}{n_2}\\frac{1}{f}=\\frac{\\lambda}{n_2}\\)
Therefore, rays are in phase if \\(2L=(m+\\frac{1}{2})\\frac{\\lambda}{n_2}\\) , for integer m.
They produce an interference maximum and the nearby region on the film is bright to observers.
Similarly, if they are exactly out of phase \\(2L=m\\frac{\\lambda}{n_2}\\) they produce an interference minimum and the nearby region is dark, even though it is illuminated.
"},{"location":"GP/light/inteference/#negligible-film-thickness","title":"Negligible Film Thickness","text":"Newton\u2019s rings are interference patterns formed by light incident on the thin film of air between a convex lens and a flat (or between two suitable lenses).
"},{"location":"GP/light/light/","title":"Light Waves","text":""},{"location":"GP/light/light/#light","title":"Light","text":""},{"location":"GP/light/light/#light-as-electromagnetic-waves","title":"Light as Electromagnetic Waves","text":""},{"location":"GP/light/light/#plane-wave","title":"Plane Wave","text":""},{"location":"GP/light/light/#transmission-of-light-in-matter","title":"Transmission of Light in Matter","text":"
In dielectric materials, the electric field is altered by a factor \\(\u03b5_r\\) , the relative permittivity (also called the dielectric constant \\(\u03ba\\)).
In magnetic materials (therefore, not in glass or plastic), the magnetic field is altered by a factor \\(\u03bc_r\\) , the relative permeability.
Therefore, a light wave propagating through any substantive medium travels at a speed \\(v=\\frac{c}{n}=\\frac{1}{\\sqrt{\\epsilon_r\\mu_r}}\\frac{1}{\\sqrt{\\epsilon_o\\mu_o}}\\) where the index of refraction \\(n=\\sqrt{\\epsilon_r\\mu_r}\\)
\\(Derivation\\)
\\(t(x)=\\frac{\\bar{SO}}{v_i}+\\frac{\\bar{OP}}{v_t}=\\frac{\\sqrt{x^2+h^2}}{\\frac{c}{n_i}}+\\frac{\\sqrt{(a-x)^2+b^2}}{\\frac{c}{n_t}}\\)
\\(\\frac{dt}{dx}=\\frac{n_i}{c}\\frac{x}{\\bar{SO}}-\\frac{n_t}{c}\\frac{a-x}{\\bar{OP}}=0\\)
Thus \\(n_isin\\theta_i=n_tsin\\theta_t\\)
"},{"location":"GP/light/light/#huygens-principle","title":"Huygens\u2019 Principle","text":"Huygens\u2019 principle is: All points on a wavefront serve as point sources of spherical secondary wavelets. After a time \\(t\\), the new position of the wavefront will be that of a surface tangent to these secondary wavelets.
Thus , We get Law of reflection.
\\(\\triangle t = \\frac{\\triangle l\\ tan\\theta_1}{v_1}\\)
\\(R=v_2 \\cdot \\triangle t\\)
\\(v_i=\\frac{c}{n_i}\\)
\\(\\Rightarrow R=\\frac{n_1}{n_2}\\triangle l \\cdot tan\\theta_1\\)
\\(sin\\theta_2=\\frac{R}{\\frac{\\triangle l}{cons\\theta_1}}\\)
\\(\\Rightarrow n_1sin\\theta_1=n_2sin\\theta_2\\)
Thus We get Law of refraction.
"},{"location":"GP/light/light/#the-electromagnetic-approach","title":"The Electromagnetic Approach","text":"\\(\\vec{E}_i = \\vec{E}_{0i}cos(\\vec{k}_i \\cdot\\vec{r}\u2212\u03c9_it)\\)
\\(\\vec{E}_r = \\vec{E}_{0r}cos(\\vec{k}_r \\cdot\\vec{r}\u2212\u03c9_rt+\\phi_r)\\)
\\(\\vec{E}_t = \\vec{E}_{0t}cos(\\vec{k}_t \\cdot\\vec{r}\u2212\u03c9_tt+\\phi_t)\\)
\\(Boundary\\ conditions\\)
\\(\\hat{u}_n \\times (\\vec{E}_i+\\vec{E}_r)-\\hat{u}_n \\times \\vec{E}_t=0\\) which is satisfied for all values of time and at any point on the interface.
Thus, we have :
\\(\\begin{align*}&\\hat{u}_n \\times \\vec{E}_{0i}cos(\\vec{k}_i \\cdot\\vec{r}\u2212\u03c9_it)+\\hat{u}_n \\times \\vec{E}_{0r}cos(\\vec{k}_r \\cdot\\vec{r}\u2212\u03c9_rt+\\phi_r)\\\\ &= \\hat{u}_n \\times \\vec{E}_{0t}cos(\\vec{k}_t \\cdot\\vec{r}\u2212\u03c9_tt+\\phi_t) \\end{align*}\\)
\\((\\vec{k_i}\\cdot \\vec{r})|_{y=b}=(\\vec{k_r}\\cdot \\vec{r}+\\phi_r)|_{y=b}=(\\vec{k_t}\\cdot \\vec{r}+\\phi_t)|_{y=b}\\)
Thus,We have \\((\\vec{k_i}-\\vec{k_r})|_{y=b}=\\phi_r\\) or \\((\\vec{k}_i-\\vec{k_r})\\cdot(\\vec{r_1}-\\vec{r_2})=0\\) for any pair of \\(\\vec{r_1}\\) and \\(\\vec{r_2}\\) terminating on the interface.
For $\\hat{u_n}\\cdot (\\vec{r_1}-\\vec{r_2})=0 $,we know \\((\\vec{k_i}-\\vec{k_r})\\) is parallel to \\(\\hat{u_n}\\) or \\(k_isin\\theta_i=k_rsin\\theta_r\\)\u300cREFLECTION LAW\u300d
Since the incident and reflected waves are in the same medium, \\(k_i = k_r\\) , so, finally, \\(\u03b8_i = \u03b8_r\\) (the law of reflection).
\\(\\vec{k_i}\\times\\hat{u_n}=\\vec{k_t}\\times\\hat{u_n}\\)
Or. \\(n_i(\\hat{k_i}\\times\\hat{u_n})=n_t(\\hat{k_t}\\times\\hat{u_n})\\)\u300cREFRACTION LAW\u300d
Note that the law of reflection and the law of refraction only rely on the phase relationship that exists among the phases of \\(\\vec{E}_{0i}\\), \\(\\vec{E}_{0r}\\) and \\(\\vec{E}_{0t}\\) at the boundary.
There is still an interdependence shared by the amplitudes \\(\\vec{E}_{0i}\\), \\(\\vec{E}_{0r}\\) and \\(\\vec{E}_{0t}\\). The additional constraint can be used to calculate the amplitude of the reflected wave and the transmitted wave (the Fresnel equations). This will lead to the phenomenon of polarization by reflection (to be discussed in the lecture on polarization).
\\(Proof\\)
"},{"location":"GP/light/light/#images-from-spherical-mirrors","title":"Images from Spherical Mirrors","text":"\\(\\frac{1}{p}+\\frac{1}{i}=\\frac{1}{f}\\)
\\(Proof\\)
When \\(p<f\\) \\(i\\) is negative,indicating an imaginary image.
When \\(i\\) > 0 : an upsidedown real image
When \\(i\\) <0 : an erect imaginary image
\\(Proof\\)
"},{"location":"GP/light/light/#spherical-refraction","title":"Spherical Refraction","text":"\\(Proof\\)
Thus \\(f\\) for convex lens is positive , for concave lens is negative
\\(Proof\\)
It can also be applied for any number of lenses.
The overall (or net) lateral magnification M of a system of lenses (or lenses and a mirror) is the product of the individual lateral magnifications as given by \\(m = \u2212i/p\\).
"},{"location":"GP/light/light/#application","title":"Application","text":"The normal human eye can focus a sharp image of an object on the retina if the object is located beyond the near point (\u223c 25 cm from the eye).
To see better, we want to generate an magnified image of an object at the near point, instead of putting the object there.
Explain eg \\(p2\\) : \\(\\frac{1}{p_2}+\\frac{1}{i_2}=\\frac{1}{f}\\) but \\(i_2<0\\),Thus if we want \\(|i_2|\\)to be large ,\\(p_2\\) has to be relatively large, but also \\(p_2\\)has to be smaller than \\(f_{ey}\\) , thus \\(p2 <\\approx f_{ey}\\).
This leaves the distance between the two lenses \\(f_{ob} + s + f_{ey}\\) the only parameter to tune. The longer the distance, the larger the magnification
\\(M = m_1m_2 \\approx -\\frac{f_{ob}+s}{f_{ob}}\\frac{25}{f_{ey}}\\)
A plane wave is a type of wave whose wavefronts are flat, or nearly flat. This means that the wave crests and troughs are aligned along parallel lines. Plane waves are an important concept in physics and are often used to describe various types of waves, including electromagnetic waves and sound waves.
Key characteristics of a plane wave include:
Constant Phase Front: In a plane wave, all points on a wavefront have the same phase. The phase of a wave is a measure of the position of a point in its cycle relative to a reference point.
Parallel Propagation: The wavefronts of a plane wave propagate in parallel lines. This is in contrast to spherical waves, where the wavefronts are spheres centered around the source.
Mathematical Representation: Mathematically, a one-dimensional plane wave can be represented as a function of the form:
\\(Acos\u2061(kx\u2212\u03c9t+\u03d5)\\)
where:
Plane waves are useful in physics because they simplify the mathematical description of wave phenomena, making it easier to analyze and solve problems. They are often used as a starting point for understanding more complex wave behavior. In the context of electromagnetic waves, for example, plane waves are fundamental to the study of optics and electromagnetic field theory.
"},{"location":"GP/light/df/diffraction/#single-slit-diffraction","title":"Single-Slit Diffraction","text":""},{"location":"GP/light/df/diffraction/#electric-field-and-intensity","title":"Electric Field and Intensity","text":"To find an expression for the intensity at an arbitrary point P on the viewing screen, corresponding to a particular small angle \u03b8, we need to divide the slit into N zones of equal widths \\(\u2206x = a/N\\) small enough that we can assume each zone acts as a source of Huygens wavelets.
We then add the phasors for the wavelets, which form a geometric series(notice. \\(r_{i+1}\u2212r_i =\u2206xsin\u03b8\\)):
\\(\\widetilde{E}_\\theta =\\frac{E_0}{N}e^{-iwt}e^{ikr+1}\\times [1+e^{ik(r_2-r_1)}+e^{ik(r_3-r_1)}+\u2026+e^{ik(r_N-r_1)}]\\)
note \\(\\Phi\\)=\\(\\frac{asin\\theta}{\\lambda}\\times2\\pi\\)
Summary
\\(\\begin{align*}\\widetilde{E}_\\theta &=\\frac{E_0}{N}e^{-iwt}e^{ikr+1}\\times [1+e^{ik(r_2-r_1)}+e^{ik(r_3-r_1)}+\u2026+e^{ik(r_N-r_1)}]\\\\&=\\frac{E_0 \\triangle x}{a}e^{-iwt}[e^{ikr_1}+e^{ikr_2}+\u2026+e^{ikr_N}]\\end{align*}\\)
\\(\\Rightarrow_{N\\to+\\infty} \\widetilde{E}_\\theta = \\frac{E_0}{a} e^{-iwt}\\int_0^ae^{ik\uff08sin\\theta x+r_1\uff09}dx\\) ~ \\(\\int_{-a/2}^{a/2}e^{ik_xx}dx\\) \\((k_x=ksin\\theta)\\)
"},{"location":"GP/light/df/diffraction/#fourier-transform","title":"Fourier Transform","text":"Thus \\(\\widetilde{E}_{sq}(k_x)= E_0a\\frac{2sin(k_xa/2)}{k_x}=E_0a\\frac{sin\\alpha}{\\alpha}(\\alpha=k_xa/2=\\frac{sin\\theta a}{2})\\)
The key message is that the field distribution in the Fraunhofer diffraction pattern is the Fourier transform of the field distribution across the aperture.
"},{"location":"GP/light/df/diffraction/#diffraction-by-a-circular-aperture","title":"Diffraction by a Circular Aperture","text":"We are essentially collecting only a fraction of the incident wavefront and therefore cannot hope to form a perfect image. The image is related to the Fourier transform of a disk and is known as the Airy pattern.
The analysis of such patterns shows that the first minimum for the diffraction pattern of a circular aperture of diameter a is located by \\(sin\\theta = 1.22\\frac{\\lambda}{a}\\) in contrast to \\(sin \u03b8 = \u03bb/a\\) in the slit case.
"},{"location":"GP/light/df/diffraction/#resolvability","title":"Resolvability","text":"Two objects cannot be distinguished from a single point object, if their diffraction patterns (mainly their central maxima) overlap. Rayleigh\u2019s criterion for resolvability states that the two point objects are barely resolved if their angular separation is such that the central maximum of the diffraction pattern of one source is centered on the first minimum of the diffraction pattern of the other, i.e.\\(\u03b8_R = sin^{\u22121} \\frac{1.22\u03bb}{a} \u2248 1.22\\frac{\\lambda}{a}\\)
Formally, with diffraction effects taken into account, the intensity of a double-slit interference pattern (shown later with the Fourier method) is
\\(I(\\theta)=I_{max}(\\frac{sin\\alpha}{\\alpha})^2cos^2\\beta\\) where \\(\\beta = \\frac{\\delta_2}{2}=\\frac{\\pi}{\\lambda}dsin\\theta\\) \\(\\alpha = \\frac{\\pi}{\\lambda}asin\\theta\\)
The interference pattern can be understood by a convolution theorem for the Fourier transformation: The transform\uff08Fourier\uff09 of the convolution of two functions \\((f*g)\\) is the product of their transforms.
\\(Proof\\)
\\((f*g)(x)=\\int dx\\int dx'f(x')g(x-x')e^{ikx}\\\\=\\int f(x')e^{ikx'}d x'\\int dx\\ g(x-x')e^{ik(x-x')}\\)
Thus
The single-slit aperture function :\\(F[E_{sq}(x)]=\\widetilde{E}_{sq}(k_x)=\\int_{-a/2}^{a/2}E_0e^{ikx}dx= E_0a\\frac{2sin(k_xa/2)}{k_x}=E_0a\\frac{sin\\alpha}{\\alpha}(\\alpha=k_xa/2=\\frac{sin\\theta a}{2})\\)
Young\u2019s double-slit interference result\uff1a
Young's double-slit interference result is related to the interference pattern formed by two narrow slits. The function h(x) describes the distribution of intensity along the screen where the interference pattern is observed. In this case, h(x) is represented as a sum of two delta functions:
\u200b \\(h(x) = \u03b4(x + d/2) + \u03b4(x \u2212 d/2)\\)
Here, d is the separation between the two slits, and \u03b4(x) is the Dirac delta function. The Dirac delta function is a mathematical concept often used in physics to model idealized distributions. It is not a traditional function but rather a distribution that satisfies certain properties. In this context, \u03b4(x) can be thought of as an infinitely tall and narrow spike located at x=0.
\u200b \\(F[h(x)]=\\int_{-\\infty}^{+\\infty} h(x)e^{ik_xx}dx= e^{-ik_xd/2}+e^{ik_xd/2}=2cos(k_xd/2)\\)
"},{"location":"GP/light/df/diffraction/#interference-vs-diffraction","title":"Interference vs Diffraction","text":"If we let \\(a\u21920\\), then \\(\u03b1\u21920\\) and\\(\\frac{sin\\alpha}{\\alpha}\u21921\\). Our result then reduces, as it must, to an equation describing the interference pattern for a pair of vanishingly narrow slits with slit separation d.
Similarly, putting \\(d \u2192 0\\) is equivalent physically to causing the two slits to merge into a single slit of width a. Then we have \\(\u03b2\u21920\\) and \\(cos2\u03b2\u21921\\). Our result reduces,as it must,to an equation describing the diffraction pattern for a single slit of width a.
If the combining waves originate from a small number of elementary coherent sources \u2014 as in a double-slit experiment with a \u226a \u03bb \u2014 we call the process interference.
If the combining waves originate in a single wavefront \u2014 as in a single-slit experiment \u2014 we call the process diffraction.
Note that both are superposition effects and usually both are present simultaneously.
"},{"location":"GP/light/df/diffraction/#diffraction-gratings","title":"Diffraction Gratings","text":"In double-slit interference with a \u226a \u03bb \\(I(\u03b8) = I_{max}cos^2 [\\frac{\u03c0d}{\\lambda} sin\u03b8]\\)
The bright fringes due to different wavelengths overlap too much to be distinguished.
A useful tool in the study of light and of objects that emit and absorb light is the diffraction grating, which has a much greater number N of slits, often called rulings, perhaps as many as several thousand per millimeter.
"},{"location":"GP/light/df/diffraction/#multiple-slits-with-monochromatic-light","title":"Multiple Slits with Monochromatic Light","text":"Figure below shows the simplest case of multiple-slit interference, with three slits, or \ud835\udc41=3. The spacing between slits is d, and the path length difference between adjacent slits is \ud835\udc51sin\ud835\udf03, same as the case for the double slit. What is new is that the path length difference for the first and the third slits is 2\ud835\udc51sin\ud835\udf03. The condition for constructive interference is the same as for the double slit, that is \\(dsin\\theta=m\\lambda\\)
When this condition is met, 2\ud835\udc51sin\ud835\udf03 is automatically a multiple of \ud835\udf06, so all three rays combine constructively, and the bright fringes that occur here are called principal maxima. But what happens when the path length difference between adjacent slits is only \ud835\udf06/2? We can think of the first and second rays as interfering destructively, but the third ray remains unaltered. Instead of obtaining a dark fringe, or a minimum, as we did for the double slit, we see a secondary maximum with intensity lower than the principal maxima.
In general, for N slits, these secondary maxima occur whenever an unpaired ray is present that does not go away due to destructive interference. This occurs at (\ud835\udc41\u22122) evenly spaced positions between the principal maxima.
The amplitude of the electromagnetic wave is correspondingly diminished to 1/\ud835\udc41 of the wave at the principal maxima, and the light intensity, being proportional to the square of the wave amplitude, is diminished to \\(\\frac{1}{N^2}\\) of the intensity compared to the principal maxima.
As figure below shows, a dark fringe is located between every maximum (principal or secondary). As N grows larger and the number of bright and dark fringes increase, the widths of the maxima become narrower due to the closely located neighboring dark fringes. Because the total amount of light energy remains unaltered, narrower maxima require that each maximum reaches a correspondingly higher intensity.
\\(Phase\\ difference\\)
\\(\\delta_N=\\frac{2\\pi}{\\lambda}dsin\\theta\\)
Consider the example below when N equals 4
A grating\u2019s ability to resolve (separate) lines of different wavelengths depends on the linewidth. The half-width of the central line \\(\u2206\u03b8_{hw}\\) is determined by the first minimum in intensity, at which the N rays from the N slits of the grating cancel one another.
The first minimum occurs where the phase difference between the adjacent slits is (from the path length difference between the top and bottom rays)
\\(\\delta_N=\\frac{2\\pi}{\\lambda}dsin\\triangle\\theta_{hw}=\\frac{2\\pi}{N}\\)
Or \\(\\triangle\\theta_{hw}\\approx sin\\triangle\\theta_{hw}=\\frac{\\lambda}{Nd}\\)
"},{"location":"GP/light/df/diffraction/#atomic-grating","title":"Atomic Grating","text":"A crystalline solid, which consists of a regular array of atoms, resembles a diffraction grating with separation d on the atomic scale (\u223c \\(10^{\u221210}\\) m).
Waves can be diffracted as if they were reflected by a family of parallel planes, with angles measured relative to the planes (not to a normal as in optics).
NOTE : The distance difference is now approximately \\(2dsin\\theta\\)
Suppose we would like to use the visible light \\((\u03bb \u2243 5.5 \u00d7 10^{\u22127} m)\\) to study the diffraction. The first-order maximum (m = 1) would occur at \\(sin \u03b8 = \\frac{m\u03bb}{2d} = 2750 \u226b 1.\\)
This means that we would not observe the first-order maxima. Therefore, we need waves with much shorter wavelength \\((\u03bb \u2248 d)\\), that is, X rays.
"},{"location":"GP/light/df/diffraction/#x-ray-diffraction","title":"X-Ray Diffraction","text":"Wilhelm Roentgen discovered X rays in 1895. He realized that these rays could be used to pass through, e.g., his wife\u2019s hand and to create an image of internal bones.
The invention of X rays helped revolutionize modern medicine, evolving into what we use today, called the digital X-ray.
The maxima turns out to be in directions as if the x rays were reflected by a family of crystal planes that extend through the atoms within the crystal and that contain regular arrays of the atoms.
Bragg\u2019s law states that the intensity maxima for X-ray diffraction is \\(2d sin\u03b8 = m\u03bb\\) where \\(m = 1,2,3,...\\) is the order number of an intensity maximum. A monochromatic X-ray beam can be used to determine the geometrical structure of a crystal.
"},{"location":"GP/light/df/diffraction/#crystallography-miller-indices","title":"Crystallography: Miller Indices","text":"Miller Indices are a symbolic vector representation for the orientation of an atomic plane in a crystal lattice and are defined as the reciprocals of the fractional intercepts which the plane makes with the crystallographic axes.
The method by which indices are determined is best shown by example. Recall, that there are three axes in crystallographic systems (except sometimes in the hexagonal system adopts a convention where there are four axes). Miller indices are represented by a set of 3 integer numbers.
(2,0,0) half of (1,0,0) Thus,\\(\\theta\\) is different
"},{"location":"GP/light/df/diffraction/#applications","title":"Applications","text":"X-ray diffraction from fibrous DNA (by Rosalind Franklin and her student R. G. Gosling) tipped off Watson and Crick to double helix. Watson and Crick also knew from the work of Erwin Chargaff that adenine and thymine bases and guanine and cytosine bases were always present in approximately equal amounts.
"},{"location":"GP/light/quantum%20light/quantum/","title":"The Quantum Nature of Light","text":""},{"location":"GP/light/quantum%20light/quantum/#the-photoelectric-effect","title":"The Photoelectric Effect","text":"Experiments show that if you direct a beam of light of short enough wavelength onto a clean metal surface, the light will eject the electrons from the surface.
We adjust the potential difference V by moving the sliding contact so that the potential of collector C is slightly negative with respect to target T.
At the stopping potential \\(V = V_{stop}\\), the reading of meter A has just dropped to zero, the most energetic ejected electrons are turned back just before reaching the collector.
"},{"location":"GP/light/quantum%20light/quantum/#puzzle-1-for-classical-physics","title":"Puzzle 1 for Classical Physics","text":"Measurements show that for light of a given frequency, \\(K_{max}\\) does not depend on the intensity of the light source.
However,in the classical theory, however, if we increase the amplitude of the wave and its oscillating electric field, the electron should get a more energetic \u201ckick\u201d.
Why Strange?The incident light is a sinusoidally oscillating electromagnetic wave.
An electron in the target should oscillate sinusoidally due to the oscillating electric force on it from the wave\u2019s electric field.
If the amplitude of the electron\u2019s oscillation is great enough, the electron should break free of the target\u2019s surface \u2013 that is, be ejected from the target.
Experiments show that the photoelectric effect does not occur if the frequency is below a certain cutoff value \\(f_0\\) ,no matter how intense the incident light is.
"},{"location":"GP/light/quantum%20light/quantum/#sum","title":"Sum","text":"In the classical theory, light is viewed as an electromagnetic wave.
One expects that no matter how low the frequency, electrons can always be ejected by light if they are supplied with enough energy \u2014 that is, if one uses a light source that is bright enough.
In experiments, however, electrons can escape only if the light frequency exceeds a certain value, and the escaping electron\u2019s kinetic energy is greater for a greater light frequency.
Einstein proposed that electromagnetic radiation (or simply light) is quantized and exists in elementary amounts (quanta) that we now call photons. According to his proposal, the quantum of a light wave of frequency f has the energy \\(E = hf = \\bar{h}\u03c9\\), where \\(h = 2\u03c0\\bar{h} = 6.63 \u00d7 10^{\u221234} J\u00b7s\\) is the Planck constant, and \u03c9 is the angular frequency. The total energy of a light wave of frequency f must be an integer multiple of \\(hf\\) , with the smallest amount being \\(hf\\) ,the energy of a single photon.
Einstein further proposed that when light is absorbed or emitted by an object (matter), the absorption or emission event occurs in the atoms of the object.
In the absorption event, the energy \\(hf\\) of one photon is transferred from the light to the atom; the photon vanishes and the atom is said to absorb it.
For an object consisting of many atoms, there can be many photon absorptions (such as with sunglasses) or photon emissions (such as with lamps).
In classical physics, such events involve so much light that we had no need of quantum physics.
"},{"location":"GP/light/quantum%20light/quantum/#photon-the-quantum-of-light","title":"Photon, the Quantum of Light","text":"Back to the Photoelectric Effect\uff0cthe electrons within the target are held by electric forces. To just escape from the target, an electron must pick up a certain minimum energy \\(W\\) , where \\(W\\) is a property of the target material called its work function.
The energy that can be transferred from the incident light to an electron in the target is that of a single photon \\(hf\\) . According the conservation of energy, the kinetic energy K acquired by the electron satisfies. \\(hf = K + W\\) .
In the most favorable circumstance, the electron can escape through the surface without losing any of this kinetic energy in the process. (i.e. \\(K_{max} = hf \u2212 W\\) ).
Increasing the light intensity increases the number of photons in the light, not the photon energy, so the energy transferred to the kinetic energy of an electron is also unchanged. This solves puzzle #1.
If the energy hf transferred to an electron by a photon exceeds the work function of the material (if \\(hf > W\\) ), the electron can escape the target. If the energy transferred does not exceed the work function (that is, if \\(hf < W\\) ), the electron cannot escape. This solves puzzle #2.
"},{"location":"GP/light/quantum%20light/quantum/#photon-momentum-and-compton-scattering","title":"Photon Momentum and Compton Scattering","text":"A photon, or a light quantum, is a particle with energy \\(E = hf\\) . It has a velocity of the speed of light c, but no mass (m = 0). In 1916, Einstein extended his concept of photons by proposing that a quantum of light has linear momentum.
According to the theory of relativity, \\(E^2 \u2212 c^2p^2 = m^2c^4 = 0\\) the magnitude of the photon momentum is. \\(p = \\frac{hf}{c} = \\frac{h}{\\lambda} = \\bar{h}k\\)
"},{"location":"GP/light/quantum%20light/quantum/#compton-scattering","title":"Compton Scattering","text":"When a photon interacts with matter, energy and momentum are transferred, as if there were a collision between the photon and matter in the classical sense.
To demonstrate, Arthur Compton measured the wavelengths and intensities of a beam of X rays that were scattered in various directions from a carbon target.
Compton found that although there is only a single wavelength \\((\u03bb = 71.1 pm)\\) in the incident X-ray beam, the scattered X rays contain a range of wavelengths with two prominent intensity peaks.
In classical physics, an electron in the carbon target undergoes forced oscillations in the sinusoidally oscillating electromagnetic wave. Hence, the electron should send out scattered waves at the same frequency.
With quantum physics and relativity, the energy and momentum conservation becomes \u300cImportant\u300d
\\(\\begin{align*} \\frac{hc}{\\lambda}+mc^2&=\\frac{hc}{\\lambda^{'}}+\\gamma mc^2 \\ (energy) \\\\ \\frac{h}{\\lambda}&=\\frac{h}{\\lambda'}cos\\phi+\\gamma mvcos\\theta\\ (momentun\\ in\\ x) \\\\ 0&=\\frac{h}{\\lambda'}sin\\phi-\\gamma mvcos\\theta\\ (momentun\\ in\\ y)\\end{align*}\\)
where \\(\\gamma =\\sqrt{\\frac{1}{1-\\frac{v^2}{c^2}}}\\)
The quantity \\(h/mc\\) is a constant called the Compton wavelength. Its value depends on the mass m of the particle from which the X rays scatter.
Strictly speaking, the particle can be a loosely boundelectron, or a carbon atom (with tightly bound electrons).
For an electron, the Compton wavelength is \\(\\frac{h}{mc}= \\frac{hc}{mc^2} =\\frac{12400eV\u00b7A\u030a}{511000eV}=2.426pm\\)
For a carbon atom, the Compton wavelength is \\(12 \u00d7 m_u /m_e \u2248 12 \u00d7 1836 \u2248 22, 000\\) times smaller and, hence, can be neglected. Therefore, there is a peak at the incident wavelength at any angle.
According to the quantum-mechanical description, a photon also has an intrinsic spin angular momentum, which is either \\(\u2212\\bar{h}\\) or \\(+\\bar{h}\\), where the signs indicate right- or left-handedness, respectively.
Whenever a charged particle emits or absorbs electromagnetic radiation, along with changes in its energy and linear momentum, it will undergo a change of \\(\u00b1\\bar{h}\\) in its angular momentum.
The energy transferred to a target by an incident monochromatic electromagnetic wave can be envisaged as being transported in the form of a stream of identical photons.
A beam of linearly polarized light will interact with matter as if it were composed, at that instant, of equal numbers of right- and left-handed photons. There is a subtle point. Strictly speaking, we cannot say that the beam is actually made up of precisely equal amounts of well-defined right- and left-handed photons; the photons are all identical.
Rather, each individual photon exists in either spin state with equal likelihood.
\\(|H\u27e9 =\\frac{|R\u27e9+|L\u27e9}{\\sqrt{2}} = \\frac{1}{\\sqrt{2}}[\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\-i\\end{pmatrix} +\\frac{1}{\\sqrt{2}}\\begin{pmatrix}1\\\\i\\end{pmatrix}]\\)
"},{"location":"GP/light/quantum%20light/quantum/#appendix-21a-quantum-key-distribution","title":"Appendix 21A: Quantum Key Distribution","text":"Goal: To send encrypted messages that cannot be understood by anyone but the designated recipient.
Message is a whole number \\(m\\) (e.g., represented by the dots and dashes of Morse code as ones and zeros.)
An encryption is a function \\(f : m \u2192 f (m)\\), agreed on between Alice (sender) and Bob (recipient) but unknown to Eve (a possible eavesdropper).
Problem: If the same encryption is used many times, Eve can usually deduce the nature of the encryption and read the messages (by, e.g., frequency analysis).
Classical solution: Let the encryption depend on a frequently changed key, which can be regarded as another whole number \\(k\\). The encrypted message is now \\(f (m, k)\\). New problem: Alice and Bob must frequently exchange messages to establish new keys, and these new messages too may be intercepted by Eve. Quantum solution: It is not possible to measure any quantity without changing an unknown state vector to one in which that quantity has some definite value.
"},{"location":"GP/light/quantum%20light/quantum/#the-bb84-protocol-bennet-brassard-1984","title":"The BB84 Protocol (Bennet & Brassard, 1984)","text":"A protocol to establish a secured communication channel. More concretely, a scheme to enables us to detect the presence of an eavesdropper using polarized photons.
What Can Eve Do?
What Eve really wants is that Alice and Bob should establish a key that Eve knows, so that she can secretly read the messages sent from Alice to Bob.
So Eve can intercept the photons sent by Alice, measure their polarizations, and then send substitute photons with these polarizations on to Bob.
But Eve, like Bob, does not know the mode that Alice is using in choosing each photon polarization. So there is only a 50% chance that the substitute photon sent by Eve to Bob will have the same polarization that it had when it was sent by Alice.
Can Alice and Bob Detect Eve?
When Alice and Bob compare notes, they identity the photons that had been sent when Alice and Bob had by chance being used the same modes.
Eve too may learn this information, but by then it is too late. There is only a 50% chance that Eve had used the same mode that Alice (and Bob) had used.\u300c\u6d4b\u4e0d\u51c6\u300d
If Eve had used a different mode, there is still a 50% chance that Bob would have observed the same polarization that had been sent by Alice.
Overall, Alice and Bob had 25% of the binary digits in the key that do not match; thus, they can detect Eve\u2019s intervention by comparing a part of the key.
After all, polarization is a classical concept. Which part of the BB84 depends crucially on the quantum concept and can be defeated in the classical world?
Quantum no-cloning theorem: It is impossible to create an identical copy of an arbitrary unknown quantum state. Where is it needed?
"},{"location":"GP/mag/mag/#the-magnetic-field","title":"The Magnetic Field","text":"
If the velocity of a charged particle has a component parallel to the uniform magnetic field, such that :
\\(v_{||}=vcos\\phi\\)
\\(v_{\u22a5}=vsin\\phi\\)
\\(d\\vec{B}= \\frac{\\mu_0}{4\\pi}\\frac{id\\vec{s}\\times\\vec{r}}{r^3}\\)
Where the constant \\(\\mu_0=4\\pi \\cdot 10^{-7(T\\cdot m/A)}\\)is called the permeability constant
"},{"location":"GP/mag/mag/#example","title":"Example","text":""},{"location":"GP/mag/mag/#a-long-straight-wire","title":"A Long Straight Wire","text":"\\(\\begin{align*}d\\vec{B}&=\\frac{\\mu_0}{4\\pi}\\frac{id\\vec{s}\\times\\vec{r}}{r^3}\\\\ &=\\frac{\\mu_0}{4\\pi}\\frac{id\\vec{s}\\times\\vec{R}}{r^3} \\\\B&=\\frac{\\mu_0i}{4\\pi R}\\int_{-\\infty}^{+\\infty}\\frac{R^2dS}{r^3} (sin\\theta=R/r \\ cos\\theta=-s/r \\ dr/ds=s/r)\\\\ &=\\frac{\\mu_0i}{4\\pi R}\\int_{0}^{\\pi}sin\\theta d\\theta \\ (cos\\theta d\\theta = d(sin\\theta)=-\\frac{R^2dr}{r^2ds}ds=-\\frac{R^2s}{r^2r}ds=cos\\theta \\frac{Rds}{r^2})\\\\ &= \\frac{\\mu_0 i}{2\\pi R}\\end{align*}\\)
"},{"location":"GP/mag/mag/#force-between-two-parallel-wires","title":"Force Between Two Parallel Wires","text":""},{"location":"GP/mag/mag/#magnetic-field-circulation","title":"Magnetic Field Circulation","text":"\\(Circulation =\\oint \\vec{B}\\cdot d\\vec{s} = \\mu_0 i_{enc}\\) (Amperian Loop)
In the limiting case of an ideal solenoid, which is infinitely long and consists of tightly packed (close-packed) turns of square wire, the field inside the coil is uniform and parallel to the solenoid axis. The magnetic field outside the solenoid is zero.
The direction of the magnetic field along the solenoid axis is given by a curled-straight right-hand rule: Grasp the solenoid with your right hand so that your fingers follow the direction of the current in the windings; your extended right thumb then points in the direction of the axial magnetic field.
A solenoid thus provides a practical way to set up a known
uniform magnetic field for experimentation, just as a parallel-plate capacitor provides a practical way to set up a known uniform electric field.
"},{"location":"GP/mag/mag/#magnetic-field-of-a-toroid","title":"Magnetic Field of a Toroid","text":"In contrast to the situation for a solenoid, B is not constant over the cross section of a toroid.
One can show, with Ampere\u2019s law, that B = 0 for points outside an ideal toroid (as if the toroid were made from an ideal solenoid).
"},{"location":"GP/mag/mag/#the-curl-of-vecb","title":"The Curl of \\(\\vec{B}\\)","text":"For volume currents, the Biot-Savart law becomes
\\(\\vec{B}(x,y,z)=\\frac{\\mu_0}{4\\pi}\\iiint\\frac{\\vec{J}(x',y',z')\\times \\vec{r}}{r^3}dx'dy'dz'\\)
The length element \\(id\\vec{s}\\) is replace by the volume element \\(JdV'= \\vec{J}(x',y',z')dx'dy'dz'\\) and \\(r =(x \u2212 x' ) \\hat{x} + ( y \u2212 y' )\\hat{y} + ( z \u2212 z') \\hat{z}\\)
Applying the divergence, we obtain:
\\(\\triangledown \\cdot \\vec{B}=\\frac{\\mu_0}{4\\pi}\\int \\triangledown (\\frac{\\vec{J}\\times \\vec{r}}{r^3})dV'=-\\frac{\\mu_0}{4\\pi}\\int \\vec{J} (\\triangledown \\times \\frac{\\vec{r}}{r^3})dV'\\)
It does not twists around; it only spreads out. Its curl is zero (as known in electrostatics).
Thus \\(\\triangledown \\cdot \\vec{B} = 0\\)
Constructing a closed Gaussian surface,we have:
\\(\\oint\\vec{B}\\cdot d\\vec{A}=\\int(\\triangledown\\cdot\\vec{B})dV=0\\)
The law asserts that the net magnetic flux \\(\\Phi _B\\) through any closed Gaussian surface is zero.
This is a formal way of saying that magnetic monopoles do not exist. The simplest magnetic structure that can exist is a magnetic dipole.
\\(\\vec{\\mu}=Ni\\vec{A}\\)
\\(\\tau = \\vec{\\mu} \\times \\vec{B}=-\\mu Bsin\\theta=-\\frac{\\partial}{\\partial \\theta}(-\\mu Bcos\\theta)\\)
\\(U_B=-\\vec{\\mu}\\cdot\\vec{B} = -\\mu Bcos\\theta\\)
"},{"location":"GP/mag/mag/#magnetic-field-of-a-circular-arc-of-wire","title":"Magnetic Field of a Circular Arc of Wire","text":""},{"location":"GP/mag/mag/#at-the-center","title":"At the center","text":"Which means \\(B(z)=\\frac{\\mu_0}{2\\pi}\\frac{\\mu}{r^3}\\) Just like the electric dipole
"},{"location":"GP/mag/mag/#magnetic-materials","title":"Magnetic Materials","text":""},{"location":"GP/mag/mag/#paramagnetism","title":"Paramagnetism","text":"Paramagnetism occurs in materials whose atoms have permanent magnetic dipole moments \\(\\vec{\\mu}\\)
Explain:
"},{"location":"GP/mag/mag/#diamagnetism","title":"Diamagnetism","text":"Diamagnetism occurs in all materials, but the weak effect is only observable in materials having atomic dipole moments of zero.
Such a material can be modeled by equal numbers of electrons orbiting counterclockwise or clockwise. An external magnetic field will either accelerate or decelerate these electrons, leading to a net magnetic dipole moment
"},{"location":"GP/mag/mag/#ferromagnetism","title":"Ferromagnetism","text":"A ferromagnet has strong, permanent magnetism. What distinguishes ferromagnets from paramagnets is that there is a strong interaction between neighboring atoms.
The interaction keeps the dipole moments of atoms aligned even when the magnetic field is removed.
"},{"location":"GP/matter%20wave/matter/","title":"matter wave","text":""},{"location":"GP/matter%20wave/matter/#matter-waves","title":"Matter Waves","text":""},{"location":"GP/matter%20wave/matter/#two-versions-of-interference","title":"Two Versions of Interference","text":""},{"location":"GP/matter%20wave/matter/#standard-version-of-the-double-slit-experiment","title":"standard version of the double-slit experiment","text":"
In the standard version of the double-slit experiment(Thomas Young, 1801), light shines on screen B, which contains two narrow parallel slits.
The light waves emerging from the two slits spread out by diffraction and overlap on screen C where, by interference, they form a pattern of alternating intensity maxima and minima.
The point of this thought experiment is as follows.
We know that the intensity \\(I\\) of a light wave at any point is proportional to the square of \\(E_m\\). Thus, the probability(per unit time interval) that a photon will be detected in any small volume centered on a given point in a light wave is proportional to the square of the amplitude of the wave\u2019s electric field vector at that point.
"},{"location":"GP/matter%20wave/matter/#single-photon-version","title":"single-photon version","text":"The light source is so extremely feeble that it emits only one photon at a time, at random intervals. Astonishingly, interference fringes still build up on screen C if the experiment runs long enough
Because an interference pattern eventually builds up on the screen, we can only speculate that each photon travels from source to screen as a wave that fills up the space between source and screen.
We can predict the probability that an energy transfer will occur at any given point on the screen.
Thus, we can say that the wave traveling from the source is a probability wave, which produces a pattern of \u201cprobability fringes\u201d on the screen.
"},{"location":"GP/matter%20wave/matter/#light-as-a-probability-wave","title":"Light as a Probability wave","text":"We see that the double-slit experiments tell us that
The probability density of detecting a photon at some point \\(P\\) in space depends on the irradiance \\(I \u221d E_0^2\\) at that point. Thus, the net \\(E_0\\) at \\(P\\) can be interpreted as the probability amplitude. (Probability amplitude is a quantity whose square gives the probability density.)
To go further, one will need quantum electrodynamics (QED), the quantum theory of the interaction of light and matter.
"},{"location":"GP/matter%20wave/matter/#extendtions-to-not-only-light","title":"Extendtions to not only light","text":""},{"location":"GP/matter%20wave/matter/#de-broglie-hypothesis","title":"De Broglie Hypothesis","text":"A beam of light is a wave, but it transfers energy and momentum to matter only at points, via photons. Electron is a particle with energy and momentum. Why can\u2019t we think of a beam of moving electron \u2014 or any other particle \u2014 as a matter wave?
In 1924, Louis de Broglie proposed that one could assign a wavelength \\(\u03bb\\) to a particle with momentum of magnitude \\(p\\). Like that of photons, we define. \\(\u03bb = h/p\\), which is known as the de Broglie wavelength of the moving particle.
"},{"location":"GP/matter%20wave/matter/#electron-diffraction","title":"Electron Diffraction","text":"Electron diffraction and neutron diffraction are used to study the atomic structures of solids and liquids, and electron diffraction is often used to study the atomic.
"},{"location":"GP/matter%20wave/matter/#the-interference-of-electrons","title":"The Interference of Electrons","text":"In a more recent experiment, an interference pattern was built up when electrons were sent, one by one, through a double-slit apparatus. When an electron hit the viewing screen, it caused a flash of light whose position was recorded.
Do particles interfere with each other?
Perhaps they collide . . . .
In Newtonian physics: a particle is only aware of the slit through which it goes, it has no idea how many other slits are open or closed or even exist. Therefore, when both slits are open, \\(P_{12} = P_1 + P_2\\).
Consider matter waves:The electrons arrive in lumps, like particles, and the probability of arrival of these lumps is distributed like the distribution of intensity of a wave.
Introduce:
Add Amplitude \\(A\\)
Introduce the probability amplitude \\(\u03c8\\) which is a complex number.
The probability of an event in an ideal experiment is then given by \\(|\u03c8|^2 = \u03c8^\u2217\\times\u03c8\\).
When an event can occur in several alternative ways, the probability amplitude for the event is the sum of the probability amplitudes for each way considered separately
\\(\u03c8 = \u03c8_1 + \u03c8_2 + \u00b7 \u00b7 \u00b7\\).
The probability for the event is, then,\\(P=|\u03c8|^2 =|\u03c81|^2+|\u03c82|^2+2R(\u03c81^\u2217\u03c82)+\u00b7\u00b7\u00b7.\\)\uff08note:\u6a21\uff09
The interference term \\(2R(\u03c8_1^\u2217\u03c8_2)\\) is responsible for the rapid oscillations of \\(P\\) observed in the experiment.
If an experiment is performed which is capable of determining whether one or another alternative is actually taken, the interference is lost.
The experiment tells us that the probability of the event in the which-way experiment is the sum of the probabilities for each alternative, \\(P = |\u03c8|^2 = |\u03c8_1|^2 + |\u03c8_2|^2\\),just as what happens in the classical case.
Thus an electron acts like it went through one particular slit if we see it doing that, and acts like it did not have a specific path (through a specific slit) when it is not seen.
To see an electron with a resolution comparable to slit separation \\(d\\), (so we know which slit it took) requires light with \\(\u03bb < d\\), this is just standard wave theory.
But, the light is made of photons each with momentum \\(p>\\frac{h}{d}\\). So, measuring the position of the electron has made us disturb its momentum.
The amount of momentum transferred to the electron in the act of observation is indefinite.
This is a manifestation of Heisenberg\u2019s uncertainty principle, which says you cannot measure the momentum and coordinate of a particle simultaneously to arbitrary accuracy.
"},{"location":"GP/matter%20wave/matter/#heisenbergs-uncertainty-principle","title":"Heisenberg\u2019s Uncertainty Principle","text":"Heisenberg\u2019s uncertainty principle is not an effect caused by the measurement but a consequence of intrinsic wave nature of matter.
The probabilistic nature of quantum physics places an important limitation on detecting a particle\u2019s position and momentum. That is, it is not possible to measure the position \\(\\vec{r}\\) and the momentum \\(\\vec{p}\\) of a particle simultaneously with unlimited precision.
The uncertainties in the components of these quantities are given by (Werner Heisenberg, 1927)
\\(\u2206x\u00b7\u2206p_x \u2265\\bar{h}\\)
\\(\u2206y\u00b7\u2206p_y \u2265\\bar{h}\\)
\\(\u2206z\u00b7\u2206p_z \u2265\\bar{h}\\)
They are due to the fact that electrons and other particles are matter waves and that repeated measurements of their positions and momenta involve probabilities, not certainties.
In the statistics of such measurements, we can view, say, \\(\u2206x\\) and \\(\u2206p_x\\) as the spread (actually, the standard deviations) in the measurements. Couldn\u2019t we very precisely measure \\(p_x\\) and then next very precisely measure x wherever the electron happens to show up?
Doesn\u2019t that mean that we have measured both \\(p_x\\) and \\(x\\) simultaneously and very precisely?
No, the flaw is that although the first measurement can give us a precise value for \\(p_x\\) , the second measurement necessarily alters that value. Puzzled?
Consider an electron with a certain value of \\(k\\), which, by the de Broglie relationship, means a certain momentum \\(p_x = \\bar{h}k\\). Thus, \\(\u2206p_x = 0\\). By Heisenberg\u2019s uncertainty principle, that means that \\(\u2206x \u2192 \u221e\\).
What form does the electron wave function have? The naive guess could be \\(sinkx\\), or \\(coskx\\).
But they have spatial variance that is inconsistent with our intuition.
To represent the electron by a wave, we need a function, which is called wave function, with a wavelength \\(\u03bb = 2\u03c0/k\\), but its square should not show any variation in x\u300cNote: Definite Momentum\u300di.e. \\(\u03c8(x,t) = e^{i(kx\u2212\u03c9t)}\\).
To calculate the square of the wave function \\(\u2223\u03c8\u2223^2\\), we obtain \\(\u2223\u03c8\u2223^2=e^{i(kx\u2212\u03c9t)}\u22c5e^{\u2212i(kx\u2212\u03c9t)}\\).
we then have \\(\u2223\u03c8\u2223^2=1\\).
This implies that at a given time t, the square of the wave function is a constant in the x direction, with no variation concerning x.
Now, where is the electron? Why complex?
but the particle exists anywhere with the same probability, so \\(\u2206x = \u221e\\). ?Question
Heisenberg\u2019s uncertainty principle is not violated.
Now, is the momentum still \\(p_x = \\bar{h}k\\)?
The Fourier transform of the \u03b4-function tells us \\(\u03c8 \u0304(p) = F(\u03c8(x)) = constant\\).
In this case, \\(\u2206x = 0\\) but \\(\u2206p = \u221e\\). Again, Heisenberg\u2019s uncertainty principle is not violated.
"},{"location":"GP/matter%20wave/matter/#fourier-transform","title":"Fourier Transform","text":"https://www.youtube.com/watch?v=spUNpyF58BY&list=RDCMUCYO_jab_esuFRV4b17AJtAw&index=3
https://youtube.com/watch?v=MBnnXbOM5S4
"},{"location":"GP/sch/sch/","title":"Schroedinger's Equation","text":""},{"location":"GP/sch/sch/#motivation","title":"Motivation","text":"
A classical wave \\(\u03c8(x, y, z, t)\\) satisfies the wave equation \\(\\frac{1}{v^2}\\frac{\\partial^2\u03c8}{\\partial^2t}=\\triangledown^2\u03c8\\) \\(v \\ is\\ speed\\)
In the quantum theory, a microscopic particle is described by a probability amplitude \\(\u03c8(x, y, z, t)\\), and the probability of finding it is proportional to \\(P(x, y, z, t) = |\u03c8(x, y, z, t)|^2\\).
Find the equation that governs the quantum behavior of a free particle of mass \\(m\\) represented by a wave \\(\u03c8(x,t) = e^{i(kx\u2212\u03c9t)}\\).
\\(E=\\frac{p^2}{2m}\\)
According to the de Broglie\u2019s hypothesis
\\(p=\\frac{h}{\\lambda}=\\bar{h}k=\u2212i\\bar{h}\\frac{1}{\u03c8(x,t)} \\frac{\u2202\u03c8(x,t)}{\\partial x}\\)
d\\(p^2=\\bar{h}^2k^2=-\\bar{h}^2\\frac{1}{\u03c8(x,t)} \\frac{\u2202^2\u03c8(x,t)}{\\partial^2 x}\\)
\\(E=hv=\\bar{h}w=\\frac{1}{\u03c8(x,t)} \\frac{\u2202\u03c8(x,t)}{\\partial t}\\)
using wave function, the energy-momentum relation is
In the presence of potential, e.g. a harmonic potential \\(U(x) = ax^2/2\\) , the classical relation is modified to \\(E=\\frac{p^2}{2m} +U(x)\\) where \\(E\\) is a constant of motion, but \\(p\\) is not. In other words, a plane wave is not a solution any more.
In this expression, p is the momentum of the particle, m is the mass of the particle, \\(U(x)\\) is the potential energy associated with the position x, and E is the total energy of the system, which remains constant.
In the conventional case where there is no potential energy in the system, momentum p would be a conserved quantity, implying the total momentum of the system remains constant. However, in the presence of potential energy, the interaction between kinetic and potential energy leads to a non-conservation of momentum. In such cases, simple waveforms like plane waves are no longer solutions, as the interaction between kinetic and potential energy requires a more complex description.
Schroedinger's equation is both homogenous and linear!!!
Linear Partial Differential Equation (PDE):
A partial differential equation is termed linear if it is linear in terms of the unknown function and its partial derivatives. It can be expressed in the form:
\\(L(u)=a_n(x,y)u_{xx}+a_{n\u22121}(x,y)u_x+\u2026+a_1(x,y)u_y+a_0(x,y)u+F(x,y)=0\\)
Here, \\(u\\) is the unknown function, \\(u_x, u_y, u_{xx}\\), and so on represent the corresponding partial derivatives
Crucially, all coefficients \\(a_n(x,y), a_{n\u22121}(x,y), \u2026\u2026, a_0(x,y)\\)as well as \\(F(x,y)\\), do not depend on the values of the unknown function \\(u\\) or its derivatives.
Homogeneous Partial Differential Equation (PDE):
A partial differential equation is considered homogeneous if all terms in the equation are homogeneous polynomials of the unknown function and its partial derivatives.
NOTE\uff1ahomogeneous without \u201dconstant term\u201d
"},{"location":"GP/sch/sch/#time-independent-schroedingers-equation","title":"time-independent Schroedinger\u2019s equation","text":"In most cases we discuss, the potential energy \\(U = U(x)\\) is independent of time.
We can solve the stationary solution of the Schroedinger equation by an ansatz: \\(\u03c8(x, t) = \\Phi(x)e^{\\frac{-iEt}{\\bar{h}}}\\).
Substituting the above ansatz of \\(\u03c8(x,t)\\), we obtain the following equation for \\(\u03c6(x)\\) \\(\\frac{\\partial^2\\phi(x)}{\\partial^2x}+\\frac{2m}{\\bar{h}^2}[E-U(x)]\\phi(x)=0\\)
By solving this equation, we can obtain the stationary solution \u03c6\\((x)\\) under the time-independent external potential \\(U(x)\\).
In free space, \\(U(x) = 0\\).
The general form of a second-order homogeneous linear ordinary differential equation (ODE) with constant coefficients is given by:
\\(a\\frac{d^y}{dx^2}+b\\frac{dy}{dx}+cy=0\\)
The general solution for such equations can be found by assuming the solution has the form \\(y=e^{rx}\\) where r is a constant to be determined. Substituting this form into the equation yields the characteristic equation:\\(ar^2+br+c=0\\)
Solving this quadratic equation for rr determines the values for r, which in turn determine the form of the general solution. There are typically three cases:
The general solution is: \\(y=C_1e^{r_1x}+C_2e^{r_2x}\\)
The general solution is: \\(y=(C_1+C_2x)e^{rx}\\)
The general solution is: \\(y=e^{\u03b1x}(C_1cos\u2061(\u03b2x)+C_2sin\u2061(\u03b2x))\\)
"},{"location":"GP/sch/sch/#the-complete-time-dependent-wave-function","title":"The complete time-dependent wave function","text":"\\(\u03c8(x,t)=Ae^{i(kx\u2212\u03c9t)} +Be^{\u2212i(kx+\u03c9t)}\\) where \\(w=\\frac{E}{\\bar{h}}\\)
Consider the right-moving wave \\(\u03c8(x,t) = Ae^{i(kx\u2212\u03c9t)}\\), the probability density is uniform:\\(|\u03c8(x, t)|^2 = \u03c8^\u2217(x, t)\u03c8(x, t) = |A|^2\\). That means that if we make a measurement to locate the particle, the location could turn out to be at any x value.
"},{"location":"GP/sch/sch/#wave-packets","title":"Wave Packets","text":"What is the speed of a free quantum mechanical particle?
\\(\u03c8(x,t)=Ae^{i(kx\u2212\\frac{\\bar{h}k^2}{2m}t)} +Be^{\u2212i(kx+\\frac{\\bar{h}k^2}{2m}t)}\\) where \\(w=\\frac{E}{\\bar{h}}\\) and \\(\\sqrt{2mE} /\\bar{h}\\)
\\(\\Rightarrow v_{ph}=\\frac{\\bar{h}k}{2m}=\\sqrt{\\frac{E}{2m}}\\)
On the other hand, the classical speed of a free particle with energy E is given by \\(v_{cl} = \\sqrt{\\frac{2E}{m}}= 2v_{ph}\\).
Problem #1:
The quantum mechanical wave function travels at half the speed of the particle it is supposed to represent!
Problem #2:
How to normalize the wave function of the free particle, say, represented by \\(Ae^{ikx}\\) ?
\\(\\int_{-\\infty}^{+\\infty}|\u03c8(x)|^2dx=|A|^2\\int_{-\\infty}^{+\\infty}1dx=|A|^2\\infty\\)
This wave function is not normalizable!
In fact, the stationary (separable) solutions do not represent physically realizable states for a free particle; there is no such thing as a free particle with a definite energy.
"},{"location":"GP/sch/sch/#the-realistic-solution-to-the-schroedinger-equation-for-a-free-particle","title":"the realistic solution to the Schroedinger equation for a free particle","text":"In quantum theory, a localized particle is modeled by a linear superposition of these stationary free-particle (or plane-wave) states.
In general, we can construct a linear combination (integral over continuous \\(k\\))
\\(\u03a8(x,t)=\\frac{1}{\\sqrt{2\\pi}}\\int_{-\\infty}^{+\\infty}\\)\u03c6\\((k)e^{i(kx\u2212\\frac{\\bar{h}k^2}{2m}t)}dk\\)
This wave function can be normalized for appropriate \u03c6\\((k)\\), typically Gaussian. We call it a wave packet, which carries a range of k and, hence, a range of energies and speeds. In a general quantum problem, we are given \\(\u03a8(x, 0)\\) and needed to find \\(\u03a8(x, t)\\).
The particle can be better localized (\u2206x can be decreased) if more plane-wave states of different wavelengths or momenta are added together in the right way (\u2206p is increased).
According to Heisenberg, these uncertainties obey \\(\u2206x\u2206p \u2265 \\bar{h}/2\\).
It turns out that the group velocity of the wave packet, not the phase velocity of the stationary states, matches the classical particle velocity.
"},{"location":"GP/sch/sch/#some-exercises","title":"Some Exercises","text":""},{"location":"GP/sch/sch/#reflection-from-a-potential-step","title":"Reflection from a Potential Step","text":"Consider a beam of nonrelativistic electrons, each of total energy E, along an x axis through a narrow tube. They experience a negative electric potential step of height \\(V_b <0\\) at \\(x=0\\).(Note that the charge q of the electron is negative)
"},{"location":"GP/sch/sch/#e-qv_b","title":"\\(E > qV_b\\).","text":"Classically, electrons should all pass through the boundary. Their total energy should be conserved, so their kinetic energy, hence speed, decreases when their potential energy increases.
(at a given time?)
We can first set \\(D = 0\\), because there is no electron source off to the right, and there can be no electrons moving to the left in region 2.
We now consider boundary conditions at \\(x = 0\\):
\\(A + B = C\\) (matching of values)
\\(Ak \u2212 Bk = Ck_b\\) (matching of slopes) \u300cfirst derivation\u300d
We should be able to solve \\(B/A\\) and \\(C/A\\), but not \\(A\\), \\(B\\), and \\(C\\).
Note that the absolute values are not important for our purpose (it can be related to the beam intensities, though).
\\(R_{reflected}=\\frac{|A|^2}{|B|^2}=|\\frac{k-k_b}{k+k_b}|^2\\)
\\(T_{transmition}=1-R=\\frac{4kR(k_b)}{|k+k_b|^2}\\)
Quantum mechanically, electrons are reflected from the boundary, but only with a probability.
What inspires us to define T in this way?
Well, one can consider an alternative quantity \\(\\frac{|C|^2}{|A|^2}=\\frac{4k^2}{|k+k_b|^2}=\\frac{k}{R(k_b)}T=\\frac{k}{k_b}T for\\ real\\ k_b\\)(which means\\(E > qV_b\\). )
Why coefficient?
\\(T = \\frac{|C|^2k_b}{|A|^2k} = \\frac{|C|^2q(\\bar{h}k_b/m)}{|A|^2q(\\bar{h}k/m)} =\\frac{n_1qv_1}{n_2qv_2}= \\frac{J_{transmitted}}{J_{incident}}\\).
One can also write
\\(R=\\frac{J_{reflected}}{J_{incident}}\\)
Therefore, \\(T = 1 \u2212 R\\) is nothing but the conservation of current \\(J_{transmitted} = J_{incident} \u2212 J_{reflected}.\\)
"},{"location":"GP/sch/sch/#e-qv_b_1","title":"\\(E < qV_b\\).","text":"Now consider a potential energy barrier, which is a region of thickness L where the electric potential is \\(V_b(<0)\\) and the barrier height is \\(U_b=qV_b\\)
Separate the space into three regions and solve Schroedinger's equation in each region \\((3\\times2-1=5\\ unknowns)\\)
Apply boundary conditions at the two boundaries\\((2\\times2=4)\\)
Calculate the tunneling coefficient.
"},{"location":"GP/sch/sch/#general-results","title":"General Results","text":"The oscillations occur because these two waves, traveling in opposite directions, interfere with each other, setting up a standing wave pattern.
Within the barrier \\((for\\ 0 < x < L)\\) the probability density decreases exponentially with x. However, if L is small, the probability density is not quite zero at x = L..
To the right of the barrier (for x > L), the probability density plot describes a transmitted wave (through the barrier) with low but constant amplitude.
We can assign a transmission coeffient T to the incident matter wave and the barrier. The transmission coefficient T is approximately \\(T\\approx e^{-2kL}\\) where \\(k=\\frac{\\sqrt{2m(qV_b-E)}}{\\bar{h}}\\) Try Derive (imaginary k' actually) \\(T\\) is sensitive to L,m and \\(U_b-E\\)
"},{"location":"GP/sch/sch/#scanning-tunneling-microscope-stm","title":"Scanning Tunneling Microscope (STM)","text":"The size of details that can be seen in an optical microscope is limited by the wavelength of the light the microscope uses (about 300 nm for ultraviolet light). We use electron matter waves (tunneling through potential barriers) to create images on the atomic scale.
The piezoelectric effect refers to the phenomenon where certain crystals exhibit charge separation when subjected to mechanical stress, resulting in the generation of voltage, or conversely, when an electric field is applied, causing the crystal to undergo mechanical deformation. This phenomenon is known as the piezoelectric effect, and the crystals that exhibit it are referred to as piezoelectric crystals.
Specifically, the piezoelectric effect can be categorized into two main types:
A fine metallic tip, mounted on quartz rods, is placed close to the surface to be examined. The space between the surface and the tip forms a potential energy barrier.
Quartz shows the piezoelectricity.
The position of the tip can be controlled by applying the electric field on the quartz. The horizontal location of the tip is controlled by the electric field in x- and y-directions. The vertical distance between the surface and the tip is controlled by the electric field in z-direction.
Transmission coefficient \\(T\\) ~ \\(e^{-2kL}\\) is sensitive to the barrier width.
\\(\u2192\\) Tunneling current is sensitive to the distance between the surface and the tip.
An STM not only can provide an image of a static surface, it can also be used to manipulate atoms and molecules on a surface.
"},{"location":"GP/sch/sch/#s-matrix","title":"S-Matrix","text":"The expression \\(|r|^2+|t|^2=1\\) is a consequence of probability conservation in quantum mechanics. Let's break down the meaning of this expression in the context of quantum mechanics and scattering theory.
In quantum mechanics, when a particle encounters a potential barrier or undergoes scattering, the probabilities of reflection\\(|r|^2\\) and transmission\\(t|^2\\)must add up to unity. Here's why:
Mathematically, this is expressed as \\(|r|^2+|t|^2=1\\)
The normalization condition for probability amplitudes requires that the sum of the probabilities over all possible outcomes is equal to 1.
Mathematically, this normalization condition is expressed as \\(\u222b\u2223\u03a8(x)\u2223^2\u2009dx=1\\), where \\(\u03a8(x)\\) is the wave function.
"},{"location":"GP/sch/sch/#example","title":"Example","text":"\\(Explain\\ for\\ the\\ below\\ Pic\\)
\\(t_1\\) and \\(t_2\\): These are the transmission coefficients at the first and second barriers, respectively. They represent the fraction of the wave that gets transmitted through the barriers.
\\(r_1\\) and \\(r_2\\): These are the reflection coefficients at the first and second barriers, respectively. They represent the fraction of the wave that gets reflected at the barriers.
\\(e^{i\u03b8}\\): This term introduces a phase shift (\u03b8) to the wave.
The term is a complex exponential that represents a phase change in the wave. It's common in wave optics and quantum mechanics to introduce phase factors.
Now, let's interpret the terms in the sum:
The first term \\(t_1e^{i\u03b8}t_2\\) represents the wave being transmitted through the first barrier (\\(t_1\\)), undergoing a phase change (\\(e^{i\\theta}\\)), and then being transmitted through the second barrier \\((t_2)\\)
The second term \\(t_1e^{i\\theta}r_2e^{i\\theta}r_1e^{i\\theta}t_2\\) represents a more complex scenario.
The wave is transmitted through the first barrier (\\(t_1\\)), undergoes a phase change (\\(e^{i\\theta}\\)), gets partially reflected \\(r_2\\), undergoes another phase change\\(e^{i\\theta}\\), gets reflected again\\((r_1)\\), undergoes yet another phase change\\((e^{i\\theta})\\), and finally gets transmitted through the second barrier \\(t_2\\).
\\(b_4b_4^\u2217=\\frac{t_{1}^2t_2^2}{1\u22122r_1r_2cos\u2061(2\u03b8)+r_1^2r_2^2}\\)
"},{"location":"GP/sch/sch/#two-particle-system","title":"Two Particle System","text":"https://www.youtube.com/watch?v=M7VJa5eim5o
https://phys.libretexts.org/Bookshelves/Quantum_Mechanics/Introductory_Quantum_Mechanics_(Fitzpatrick)/05%3A_Multi-Particle_Systems/5.03%3A_Two-Particle_Systems
\\(i\\bar{h}\\frac{\\partial}{\\partial t}\u03c8(x_1,x_2,t)=[-\\frac{\\bar{h}^2}{2m_1}\\frac{\\partial^2}{\\partial x_1^2}-\\frac{\\bar{h}^2}{2m_2}\\frac{\\partial^2}{\\partial x_2^2}+V(x_1,x_2)]\u03c8(x_1,x_2,t)\\)
"},{"location":"NA/Chap1/Chap1/","title":"Mathematical Preliminaries","text":""},{"location":"NA/Chap1/Chap1/#roundoff-errors-and-computer-arithmetic","title":"Roundoff Errors and Computer Arithmetic","text":"Truncation Error: the error involved in using a truncated, or finite, summation to approximate the sum of an infinite series.
Roundoff Error: the error produced when performing real number calculations. It occurs because the arithmetic performed in a machine involves numbers with only a finite number of digits.
If \\(p^*\\) is an approximation to p, the absolute error is | \\(p\\) \u2013 \\(p^*\\) |, and the relative error is | \\(p\\) \u2013 \\(p^*\\) | / | p |, provided that p \\(\\ne\\) 0.
The number \\(p^*\\) is said to approximate \\(p\\) to t significant digits (or figures) if \\(t\\) is the largest nonnegative integer for which \\(\\frac{|p-p^*|}{|p|}<5\\times10^{-t}\\)
To achieve \\(To\\ t\\ significant\\ digits\\) Both Chopping & Rounding has to approximate at \\(k = t\\)
Subtraction of nearly equal numbers will cause a cancellation of significant digits.
"},{"location":"NA/Chap1/Chap1/#algorithms-and-convergence","title":"Algorithms and Convergence","text":"An algorithm that satisfies that small changes in the initial data produce correspondingly small changes in the final results is called stable; otherwise it is unstable.
An algorithm is called conditionally stable if it is stable only for certain choices of initial data.
Suppose that \\(E_0 > 0\\) denotes an initial error and \\(E_n\\) represents the magnitude of an error after n subsequent operations. If \\(E_n \\approx CnE_0\\), where C is a constant independent of n, then the growth of error is said to be linear. If \\(E_n \\approx C^n E_0\\), for some C > 1, then the growth of error is called exponential.
"},{"location":"NA/Chap2/Chap2/","title":"Solutions of Equations in one variable","text":""},{"location":"NA/Chap2/Chap2/#the-bisection-method","title":"The Bisection Method","text":"Algorithm: Bisection\nTo find a solution to f (x) = 0 given the continuous function f on the interval [ a, b ], where f (a) and f (b) have opposite signs.\nInput: endpoints a, b; tolerance TOL; maximum number of iterations Nmax.\nOutput: approximate solution p or message of failure.\n\nStep 1 Set i = 1;\n FA = f (a) ;\nStep 2 While( i<=Nmax) do steps 3-6\n Step 3 Set p = a + ( b \u2013 a ) / 2 ; /* compute pi */\n FP = f (p) ; \n Step 4 If ( FP == 0 ) or ( b - a ) / 2 < TOL then Output (p); \n STOP; /* successful */\n Step 5 Set i ++;\n Step 6 If sign(FA) \u00b7 sign(FP) > 0 then set a = p ; FA = FP ;\n Else set b = p ; /* update ai, bi */ \nStep 7 Output (Method failed after Nmax iterations); /* unsuccessful */\n STOP.\n
Other Stopping Procedures
\\(|p_N-p_{N-1}|<\\epsilon\\)
\\(\\frac{|p_N-p_{N-1}|}{|p_N|}<\\epsilon\\)
\\(|f(p_N)|<\\epsilon\\)
"},{"location":"NA/Chap2/Chap2/#therom","title":"Therom","text":"Suppose that \\(f\\in C[a,b]\\) and \\(f(a)f(b)<0\\) The Bisection method generates a seqence \\(\\{p_n\\}_{n=1}^{\\infty}\\) approximating a zero p of f with \\(|p_n-p|\\le\\frac{b-a}{2^n}\\) when \\(n \\ge1\\)
"},{"location":"NA/Chap2/Chap2/#fixed-point-iteration","title":"Fixed-Point Iteration","text":""},{"location":"NA/Chap2/Chap2/#theorem-fixed-point-theorem","title":"Theorem: (Fixed-Point Theorem)","text":"Let \\(g \\in C[a, b]\\) be such that \\(g(x) \\in [a, b]\\), for all x in \\([a, b]\\).
\\(g\\) has a fixed point in \\([a,b]\\)
Suppose, in addition, that \\(g\u2019\\) exists on \\((a, b)\\) and that a constant \\(0 < k < 1\\) exists with\\(|g\u2019(x)| \\le k\\) for all x in (a, b). Then:
(1) The fixed point is unique \\(Proof \\ P57\\) (By Contradiction)
(2)For any number \\(p_0\\) in [a, b], the sequence defined by \\(p_n = g( p_{n \u2013 1} )\\), \\(n \\ge 1\\), converges to the unique fixed point \\(p\\) in \\([a, b]\\). \\(Proof \\ P61-62\\)
"},{"location":"NA/Chap2/Chap2/#corollary","title":"Corollary","text":"If \\(g\\) satisfies the hypotheses of the Fixed-Point Theorem, then bounds for the error involved in using \\(p_n\\) to approximate \\(p\\) are given by (for all n \\(\\ge\\) 1)
\\(|p_n-p|\\le k^n max\\{p_0-a,b-p_0\\}\\)
\\(|p_n-p|\\le\\frac{1}{1-k}|p_{n+1}-p_n|\\)
\\(|p_n-p|\\le \\frac{k^n}{1-k}|p_1-p_0|\\)
\\(Proof\\ P62\\)
Algorithm: Fixed-Point Iteration\nFind a solution to p = g(p) given an initial approximation p0.\nInput: initial approximation p0; tolerance TOL; maximum number of iterations Nmax.\nOutput: approximate solution p or message of failure.\nStep 1 Set i = 1;\nStep 2 While ( i <= Nmax) do steps 3-6\n Step 3 Set p = g(p0); /* compute pi */\n Step 4 If | p - p0 | < TOL then Output (p); /* successful */\n STOP;\n Step 5 Set i ++;\n Step 6 Set p0 = p ; /* update p0 */\nStep 7 Output (The method failed after Nmax iterations); /* unsuccessful */\n STOP.\n
"},{"location":"NA/Chap2/Chap2/#newtons-problem","title":"Newton's Problem","text":"Linearize a nonlinear function using Taylor\u2019s expansion.
"},{"location":"NA/Chap2/Chap2/#therom_1","title":"Therom","text":""},{"location":"NA/Chap2/Chap2/#secant-method","title":"Secant Method","text":""},{"location":"NA/Chap2/Chap2/#method-of-false-position","title":"Method of False Position","text":"P73 Just Modify to always bracket the root.
"},{"location":"NA/Chap2/Chap2/#error-analysis-for-iterative-methods","title":"Error Analysis for Iterative Methods","text":""},{"location":"NA/Chap2/Chap2/#therom-27","title":"Therom 2.7","text":"Let \\(g \\in C[a,b]\\) be such that \\(g(x)\\in [a,b]\\) for all \\(x\\in [a,b]\\).Suppose, in addition,that is continuous on \\((a,b)\\) and a positive constant \\(k<1\\) exists with \\(|g'(x)|\\le k\\) for all \\(x\\in (a,b)\\)
\\(p_{n+1}-p=g(p_n)-g(p)=g'(\\epsilon_n)(p_n-p)\\)
"},{"location":"NA/Chap2/Chap2/#therom_2","title":"Therom","text":"\\(f\\in C[a,b]\\) has a simple zero at \\(p\\) in \\((a,b)\\) if and only if \\(f(p)=0\\) but \\(f'(p)\\ne 0\\)
\\(f\\in C^m[a,b]\\) has a zero of multiplicity m at \\(p\\) in \\((a,b)\\) if and only if \\(0=f(p)=f'(p)=\u2026\u2026=f^{m-1}(p)\\) but \\(f^m(p)\\ne 0\\)
"},{"location":"NA/Chap2/Chap2/#accelarating-convergence","title":"Accelarating Convergence","text":"Algorithm: Steffensen\u2019s Acceleration\nFind a solution to x = g(x) given an initial approximation p0.\nInput: initial approximation p0; tolerance TOL; maximum number of iterations Nmax.\nOutput: approximate solution x or message of failure.\nStep 1 Set i = 1;\nStep 2 While ( i <= Nmax) do steps 3-6\n Step 3 Set p1 = g(p0) ; \n p2 = g(p1) ; \n p = p0 -( p1 - p0 )2 / ( p2 -2 p1 + p0 ) ;\n Step 4 If | p - p0 | < TOL then Output (p); /* successful */\n STOP;\n Step 5 Set i ++;\n Step 6 Set p0 = p ; /* update p0 */\nStep 7 Output (The method failed after Nmax iterations); /* unsuccessful */\n STOP.\n
"},{"location":"NA/Chap3/Chap3/","title":"Interpolation and Polynomial Approximation","text":""},{"location":"NA/Chap3/Chap3/#interpolation-and-the-lagrange-polynomial","title":"Interpolation and the Lagrange Polynomial","text":""},{"location":"NA/Chap3/Chap3/#therom-32","title":"Therom 3.2","text":"\\(Specific\\ Proof\\ P112\\)
"},{"location":"NA/Chap3/Chap3/#def","title":"Def","text":"Let \\(f\\) be a function defined at $x_0, x_1 \u2026\u2026 x_n $ and suppose that $m_0, m_1 \u2026\u2026 m_n $ are k distince integers . The Lagrange polynomial that agrees with \\(f(x)\\) at k points \\(x_{m1},x_{m2}\u2026\u2026 x_{m_k}\\) is denoted by \\(P_{m_1,m_2\u2026\u2026,m_n}(x)\\)
"},{"location":"NA/Chap3/Chap3/#nevilles-method","title":"Neville\u2019s Method","text":""},{"location":"NA/Chap3/Chap3/#therom-35","title":"Therom 3.5","text":""},{"location":"NA/Chap3/Chap3/#peseudo-code","title":"Peseudo code","text":""},{"location":"NA/Chap3/Chap3/#divided-difference","title":"Divided Difference","text":""},{"location":"NA/Chap3/Chap3/#newtons-interpolation","title":"Newton\u2019s Interpolation","text":"\\(P_n(x)=f[x_0]+\\sum_{k=1}^nf[x_0,x_1,\u2026\u2026x_k](x-x_0)\u2026\u2026(x-x_{k-1})\\)
Newton's Forward Difference Interpolation and Newton's Backward Difference Interpolation are both methods for constructing polynomial interpolants, but they differ in terms of the direction in which they compute the differences.
In both cases, the differences \\(f[x_i,x_{i\u22121},\u2026,x_0]\\) are computed using the divided difference approach. The primary difference between the two methods lies in the direction in which these differences are calculated.
Key Points:
\\(Pseudocode\\ P124-125\\)
How can we make a smooth interpolation without asking too much from \\(f\\) ?Headache \u2026
"},{"location":"NA/Chap3/Chap3/#further","title":"Further","text":"Suppose \\(S_j(x)=a_j+b_j(x-x_j)+c_j(x-x_j)^2+d_j(x-x_j)^3\\)
For notation \\(h_j=x_{j+1}-x_j\\) \\(a_{j+1}=a_j+b_jh_j+c_jh_j^2+d_jh_j^3\\) \\((1)\\)
\\(b_{j+1}=b_j+2c_jh_j+3d_jh_j^2\\) \\((2)\\)
\\(c_{j+1}=c_j+3d_jh_j\\) \\((3)\\)
\\(\\Rightarrow h_{j-1}c_{j-1}+2(h_{j-1}+h_j)c_j+h_jc_{j+1}=\\frac{3}{h_j}(a_{j+1}-a_j)-\\frac{3}{h_{j-1}}(a_{j+1}-a_j)\\)
"},{"location":"NA/Chap3/Chap3/#natural-cubic-spline","title":"Natural Cubic Spline","text":""},{"location":"NA/Chap3/Chap3/#clamped-boundary","title":"Clamped Boundary","text":""},{"location":"NA/Chap4/Chap4/","title":"Numerical Differentiation and Integration","text":"Refer to the Book .\uff08This page LACKS many contents\uff09
"},{"location":"NA/Chap4/Chap4/#numerical-differentiation","title":"Numerical Differentiation","text":""},{"location":"NA/Chap4/Chap4/#richardsons-extrapolation","title":"Richardson\u2019s Extrapolation","text":""},{"location":"NA/Chap4/Chap4/#elements-of-numerical-integration","title":"Elements of Numerical Integration","text":"Approximate \\(I=\\int_a^b f(x)dx\\)
all step size, the accuracy of approximation increases.
"},{"location":"NA/Chap5/Chap5/#error-analysis","title":"Error Analysis","text":"Refer to the BOOK
"},{"location":"NA/Chap5/Chap5/#other-euler-methods","title":"Other Euler Methods","text":""},{"location":"NA/Chap5/Chap5/#implicit-euler-method","title":"Implicit Euler Method","text":"\\(y'(t_0)\\approx\\frac{t(t_0)-y(t_0-h)}{h}\\Rightarrow y(t_1)\\approx y(t_0)+hy'(t_1)=\\alpha+hf(t_1,y(t_1))\\)
\\(w_0=\\alpha ; w_{i+1}=w_i+hf(t_{i+1},w_{i+1})(i=0,\u2026\u2026\uff0cn-1)\\)
\\(\\tau_{i+1}=\\frac{y_{i+1}-w_{i+1}}{h}==\\frac{h}{2}y''(\\epsilon_i)=o(h)\\)
"},{"location":"NA/Chap5/Chap5/#trapezoidal-method","title":"Trapezoidal Method","text":"\\(w_{i+1}=w_i+\\frac{h}{2}[f(t_i,w_i)+f(t_{i+1},w_{i+1})]\\)
\\(y'(t_0)=\\frac{1}{2h}[y(t_0+h)-y(t_0-h)]-\\frac{h^2}{6}y^{(3)}(\\epsilon_1)\\)
\\(\\Rightarrow y(t_2)\\approx y(t_0)+2hf(t_1,y(t_1))\\)
\\(w_0=\\alpha\\\\ w_{i+1}=w_{i-1}+2hf(t_i,w_i)\\)
Modified Euler's method, also known as the Improved Euler method or Heun's method, is a numerical technique used for approximating solutions to ordinary differential equations (ODEs). It is an extension of the basic Euler's method and provides better accuracy by incorporating a simple correction.
The standard Euler's method is a first-order numerical method that uses a linear approximation to update the solution from one time step to the next. However, it can have limited accuracy, especially for ODEs with rapidly changing behavior.
The Modified Euler's method improves upon this by using a two-step process:
Use the current information to make a preliminary estimate of the solution at the next time step.
Predicted Value \\((P)\\):\\(\\tilde{y}_{i+1}=y_{i}+h\u22c5f(t_i,y_i)\\)
Here, \\(y_i\\) is the current approximation, h is the step size, and \\(f(t_i,y_i)\\)represents the derivative of yy with respect to \\(t\\) at the current point.
Use the predicted value to compute a more accurate estimate by incorporating the derivative at the predicted point.
Corrected Value \\((C)\\):\\(y_{i+1}=y_i+\\frac{h}{2}[f(t_i,y_i)+f(t_{i+1},\\tilde{y}_{i+1})]\\)
In this step, \\(f(t_i,y_i)\\)represents the derivative at the initial point, and \\(f(t_{i+1},y_{i+1})\\) represents the derivative at the predicted point.
Modified Euler's method has a local truncation error of \\(O(h^3)\\), which is an improvement over the \\(O(h^2)\\) local truncation error of the basic Euler method. This makes it more accurate for a wide range of ODEs, and it is still relatively simple to implement.
"},{"location":"NA/Chap5/Chap5/#higher-order-taylor-methods","title":"Higher-order Taylor Methods","text":""},{"location":"NA/Chap5/Chap5/#runge-kutta-methods","title":"Runge-Kutta Methods","text":"Runge-Kutta methods are a family of iterative numerical techniques used for solving ordinary differential equations (ODEs) or systems of ODEs. These methods provide an approximation of the solution at discrete points in the domain by iteratively updating the solution from one point to the next.
Single-Step Method:
In a single-step method, the solution at the next time step \\((t_{i+1},w_{i+1})\\) is determined based on the information available at the current time step \\((t_{i},w_{i})\\). This implies that we calculate the next point of the solution through one iteration rather than computing the entire solution curve at once.
We can improve the result by finding a better slope.
Generalize the modified Euler\u2019s method
"},{"location":"NA/Chap5/Chap5/#multistep-methods","title":"Multistep Methods","text":"Use a linear combination of y and y at several mesh points to better approximate \\(y(t_{i+1})\\)
\\(w_{i+1}=a_{m-1}w_i+a_{m-2}w_{i-1}+\u2026\u2026+h[b_mf_{i+1}+b_{m-1}f_i+\u2026\u2026+b_0f_{i+1-m}]\\)
Newton's Forward Difference Interpolation and Newton's Backward Difference Interpolation are both methods for constructing polynomial interpolants, but they differ in terms of the direction in which they compute the differences.
In both cases, the differences \\(f[x_i,x_{i\u22121},\u2026,x_0]\\) are computed using the divided difference approach. The primary difference between the two methods lies in the direction in which these differences are calculated.
Key Points:
Derive from integration
1.Use the Newton backward-difference formula to interpolate f on \\((t_i,f_i),(t_{i-1},f_{i-1})\u2026\u2026(t_{i+1-m},f_{i+1-m})\\) and obtain \\(P_{m-1}(t)\\)
2.Or Let \\(t=t_i+sh\\ s\\in[0,1]\\) we have
\\(\\int_{t_{i}}^{t_{i+1}}f(t,y(t))dt=h\\int_0^1P_{m-1}(t_i+sh)ds+h\\int_0^1R_{m-1(t_i+sh)}ds\\)
\\(\\Rightarrow w_{i+1}=w_i+h\\int_0^1P_{m-1}(t_i+sh)ds\\)
3. The local truncation error for a multistep method is \\(\\tau_{i+1}(h)=y_{i+1}-(a_{m-1}y_i+\u2026\u2026+a_0y_{i+1-m})/h-[b_mf_{i+1}+\u2026\u2026+b_0f_{i+1-m}]\\) for each \\(i=m-1,m\u2026\u2026,n-1\\)
Example
derive the Adams-Bashforth two-step explicit method.
Note \uff1a \\(h\\) is step length \\(h=\\triangle x/n\\)
Step 1 Compute the first m initial values by Runge-Kutta method.
Step 2 Predict by Adams-Bashforth explicit method.
Step 3 Correct by Adams-Moulton implicit method.
Consider a simple first-order ordinary differential equation given by:
\\(dy/dt=\u2212y\\)
We will use the Adams predictor-corrector method to numerically solve this equation.
"},{"location":"NA/Chap5/Chap5/#step-1-compute-the-first-m-initial-values-by-runge-kutta-method","title":"Step 1: Compute the First m Initial Values by Runge-Kutta Method","text":"First, we use the Runge-Kutta method to calculate the initial values. For the given equation, the Runge-Kutta scheme is:
\\(k_1=\u2212y_n\\)
\\(k_2=\u2212(y_n+0.5hk_1)\\)
\\(y_{n+1}=y_n+hk_2\\)
Here, \\(h\\) is the time step.
"},{"location":"NA/Chap5/Chap5/#step-2-predict-by-adams-bashforth-explicit-method","title":"Step 2: Predict by Adams-Bashforth Explicit Method","text":"The Adams-Bashforth method for a second-order equation is:
\\(y_{n+1}^{(p)}=y_n+h(\u2212y_n+\\frac{3}{2}y_{n\u22121})\\)
"},{"location":"NA/Chap5/Chap5/#step-3-correct-by-adams-moulton-implicit-method","title":"Step 3: Correct by Adams-Moulton Implicit Method","text":"The Adams-Moulton method for a second-order equation is:
\\[y_{n+1}=y_n+h(\u2212\\frac{1}{2}y_{n+1}+\\frac{3}{2}y_{n})\\]This equation needs to be solved iteratively as \\(y_{n+1}\\) appears on both sides of the equation.
This is a simple example, and in practice, the Adams predictor-corrector method can be applied to higher-order differential equations. The specific steps and coefficients will vary based on the chosen order. In real-world applications, computational tools or programming languages may be used to perform these calculations.
The most popularly used system is based on the 4th-order Adams-Bashforth method as predictor and one iteration of the Adams-Moulton method as corrector, with the starting values obtained from the 4th-order Runge-Kutta method.
"},{"location":"NA/Chap5/Chap5/#derive-from-taylor-expansion","title":"Derive from Taylor expansion","text":"For multistep methods it is also required that for \\(i=1,2,\u2026\u2026m-1\\)
\u200b \\(lim_{h\\rightarrow 0}|w_i-y_i|=0\\)
A one-step difference equation method is said to be convergent with respect to the differential equation it approximates if \\(lim_{h\\rightarrow 0}max_{1\\le i\\le n}|w_i-y_i|=0\\)
Apply a particular method to a simple test equation,y\u2018 =\u03bby, y(0) =\u03b1, where Re(\u03bb ) < 0.Assume that roundoff error is introduced only at the initial point. If this initial error will decrease for a certain step size h, then this
method is said to be absolutely stable with respect to H =\u03bbh. The set of all such H forms the region of absolute stability.
Method A is said to be more stable than method B if the region of
absolute stability of A is larger than that of B.
"},{"location":"NA/Chap5/Chap5/#def_1","title":"Def","text":""},{"location":"NA/Chap6/Chap6/","title":"Direct Methods for Solving Linear Systems","text":""},{"location":"NA/Chap6/Chap6/#pivoting-strategies","title":"Pivoting Strategies","text":"Place the element in the pivot position that is largest relative to the entries in its row.
Step 1: define a scale factor \\(s_i\\) for each row as \\(s_i=max_{1\\le j\\le n}|a_{ij}|\\)
Step 2:Determine the smallest \\(p\\ge k\\) such that \\(\\frac{|a_{pk}^{(k)}|}{s_p}=max_{k\\le i\\le n}\\frac{|a_{ik}^{(k)}|}{s_i}\\) (If already at the kth row)
A strictly diagonally dominant matrix A is nonsingular. Moreover, Gaussian elimination can be performed without row or column interchanges, and the computations will be stable with respect to the growth of roundoff errors.
A matrix A is positive definite if it is symmetric and if \\(\\vec{x^t}A\\vec{x}\\) > 0 for every n-dimensional vector\\(\\vec{x}\\ne 0\\).
Matrix Norms
"},{"location":"NA/Chap7/Chap7/#eigenvalues-and-eigenvectors","title":"Eigenvalues and Eigenvectors","text":""},{"location":"NA/Chap8/Chap8/","title":"Chap8","text":"a
"},{"location":"OS/Summary/Summary/","title":"Summary","text":""},{"location":"OS/Summary/Summary/#interrupt","title":"Interrupt","text":""},{"location":"OS/lec1/lec1/#lecture-1-introduction-to-operating-systems","title":"Lecture 1 - Introduction to Operating Systems","text":""},{"location":"OS/lec1/lec1/#10-a-bit-of-history","title":"1.0 A bit of history","text":"
An operating system is a software that acts as an intermediary between the computer hardware and the user. It provides an environment in which a user can execute programs conveniently and efficiently.
------------------------------------\n| APP --> Unprivileged instructions |\n------------------------------------\n| OS --> Privileged instructions |\n------------------------------------\n| Hardware |\n------------------------------------\n
"},{"location":"OS/lec1/lec1/#os-events","title":"OS Events","text":"An event stops execution, changes mode, and changes context
When a user program need sto do something privileged, it makes a system call
The timer interrupts the computer regularly
The OS is responsible for :
"},{"location":"OS/lec2/lec2/#lecture-2-os-structure","title":"Lecture 2 - OS Structure","text":""},{"location":"OS/lec2/lec2/#system-calls","title":"System Calls","text":"
printf()
function is called, then write()
function is called. In the write()
function, there is a system call to the kernel. [with syscall number $0x1]kernel_entry code
will be called -- Saved all user space registers 2) calls write syscall handler
-- Get from syscall_table, which is an arraySYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, size_t, count)\n{\n return ksys_write(fd, buf, count);\n}\n
ret_to_user
System-call interface
maintains a table indexed according to numbers assigned to each system call. Each entry in the table points to the entry point of the system call.
strace
to trace system callsstrace cp file1 file2
strace -c cp file1 file2
-- Count the number of system callsstrace cp main.c main.copy 2>&1 |wc -l #175 lines\n
2>&1
-- Redirect stderr to stdout|wc -l
-- Count the number of linestime ap start_kernel
-- Time spent in system callstime
count multiple pid and sum them up, so it seems that the time spent in system calls is more than the actual time spent in the system calls.
time grep -Rn start_kernel
Three general methods used to pass parameters to the OS
.data
section.rodata
sectionWhy unintialized in .bss
instead of .data
? -- To save space, as .bss
is not stored in the file
readelf -h main
0x7f 0x45 0x4c 0x46
-- ELF : Linux defines the format of the file using magic numberreadelf -p .interp main\n
.interp
section of the ELF file, which contains the path of the dynamic linkerprintf()
ld-linux-aarch64.so.1
readelf
we see that entry of the main.static
is 0x400640
0x400640
is the address of the start
function in the main.static
binaryobjdump-d a.out
regs->pc = pc
here pc
is the address of the start
function -- elf_entry
interp_elf_ex -> e_entry
ld.so
-- Loader resolves the library callsstart
functionSystem calls are different -- name / number
Apps can be multi-operating system * Written in interpreted language like Python, Ruby, and interpreter available on multiple operating systems * App written in language that includes a VM containing the running app (like Java) * Use standard language (like C), compile separately on each operating system to run on each
Application Binary Interface (ABI) is architecture equivalent of API, defines how different components of binary code can interface for a given operating system on a given architecture, CPU, etc
"},{"location":"OS/lec2/lec2/#operating-system-design-and-implementation","title":"Operating-System Design and Implementation","text":""},{"location":"OS/lec2/lec2/#operating-system-structure","title":"Operating System Structure","text":"General-purpose OS is very large program
Various ways to structure ones
Microkernel \u2013Mach
Good Helper Website
"},{"location":"OS/lec3/lec3/#lecture-3-process","title":"Lecture 3 - Process","text":""},{"location":"OS/lec3/lec3/#process-concept","title":"Process Concept","text":"
Process\uff1aa unit of resource allocation and protection.
Process = code (also called the text)\n initially stored on disk in an executable file\n data section\n global variables (.bssand .data in x86 assembly)\n program counter\n points to the next instruction to execute (i.e., an address in the code)\n contents of the processor's registers\n a stack\n a heap\n----------------------------------------------\n| Stack |\n|--------------------------------------------|\n| | |\n| v |\n| ... |\n| ... |\n| ^ |\n| | |\n|--------------------------------------------|\n| Heap | \n|--------------------------------------------|\n| Data |\n|--------------------------------------------|\n| Code |\n----------------------------------------------\n
"},{"location":"OS/lec3/lec3/#runtime-stack","title":"Runtime Stack","text":"Each process has and only has a PCB * Allocate a PCB on new process creation * Free the PCB on process termination
Represented by the C structure task_struct
"},{"location":"OS/lec3/lec3/#process-state","title":"Process State","text":"fork()
System Call","text":"fork()
creates a new processThe child is is a copy of the parent, but... 1. It has a different pid(and thus ppid) 2. Its resource utilization (so far) is set to 0
fork()
returns the child\u2019s pid to the parent, and 0 to the child
getpid()
call, and its ppidwith the getppid()
callfork()
pid=fork();\nif (pid<0) {\n fprintf(stdout, \"Error: can\u2019t fork()\\n\");\n perror(\u201cfork()\u201d);\n}\nif(pid!=0) {\n fprintf(stdout, \"I am parent and my child has pid%d\\n\",pid);while(1);\n} else{\n fprintf(stdout, \"I am child, and my pidis %d\\n\", getpid());while(1) ;\n}\n
if(pid!=0)
, but for child, pid
is 0, so the child will not enter the if
blockint a = 12\nif(pid = fork()){// PARENT\n // ask the OS to put me in waiting\n sleep(10); // 10 seconds\n fprintf(stdout,\"a =%d\\n\",a);\n}\nelse{//CHILD\n a += 3;\n while(1);\n}\n
a = 12
2. The child process cannot modify the parent's memory spaceHow many times will the following code print \"Hello\"?
pid1 = fork();\nprintf(\"Hello\\n\");\npid2 = fork();\nprintf(\"Hello\\n\");\n
6 times
"},{"location":"OS/lec3/lec3/#unix-examples","title":"UNIX examples","text":"
fork()
system call creates new processexecve()
system call used after a fork()
to replace the process\u2019memory space with a new programwait()
for the child to terminateDemo of how the system do ls
\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528 exec \u7cfb\u5217\u51fd\u6570\uff08\u4f8b\u5982 execl()\u3001execv() \u7b49\uff09\u65f6\uff0c\u5b83\u4f1a\u7528\u4e00\u4e2a\u65b0\u7684\u7a0b\u5e8f\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b\u7684\u5185\u5bb9\u3002\u8c03\u7528\u6210\u529f\u540e\uff0c\u65e7\u7684\u7a0b\u5e8f\u4ee3\u7801\u3001\u6570\u636e\u548c\u6808\u90fd\u4f1a\u88ab\u65b0\u7a0b\u5e8f\u7684\u5185\u5bb9\u53d6\u4ee3\uff0c\u4f46\u8fdb\u7a0bID\uff08PID\uff09\u4fdd\u6301\u4e0d\u53d8\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u7a0b\u5e8f\u7ee7\u7eed\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u8fd0\u884c\u3002\u6b64\u65f6\uff0c\u5f53\u524d\u8fdb\u7a0b\u7684\u6240\u6709\u6267\u884c\u72b6\u6001\u90fd\u4f1a\u88ab\u65b0\u7a0b\u5e8f\u7684\u72b6\u6001\u66ff\u4ee3\uff0c\u539f\u8fdb\u7a0b\u7684\u4ee3\u7801\u4e0d\u4f1a\u518d\u6267\u884c\u3002
"},{"location":"OS/lec3/lec3/#pros-and-cons-of-fork","title":"pros and cons offork()
","text":"fork()
, the child process do execve()
, so the child process will not print \"Will I be executed?\"; it will call another programwait()
call 1. blocks until any child completes 2. returns the pidof the completed child and the child\u2019s exit codewaitpid()
call 1. blocks until a specific child completes 2. can be made non-blocking with WNOHANG options\uf06cRead the man pages (\u201cman 2waitpid\u201d)Question: what resources cannot be deallocated by the child process? * PCB (Process Control Block)
wait()
Z
statewait()
to the parent process to avoid zombie process or waitpid(pid)
if(pid<0){\n fprintf(stdout, \"Error: can\u2019t fork()\\n\");\n perror(\u201cfork()\u201d);\n}\nif(pid!=0){\n fprintf(stdout, \"I am parent and my child has pid%d\\n\",pid);\n while(1);\n //did not call wait() not properly\n} else{ //I am the child\n pid = fork();\n if(pid<0){\n fprintf(stdder,\"Error: can't fork()\\n\");\n perror(\"fork()\");\n exit(1);\n }\n if(pid==0){\n fprintf(stdder,\"I am the grandchild, and my pid is %d\\n\",getpid());\n sleep(10);\n exit(0);\n }\n else{\n sleep(1);\n // parent exits without waiting for the child\n // grandchild becomes an orphan -- not a zombie\n exit(0);\n }\n}\n
"},{"location":"OS/lec3/lec3/#process-scheduling","title":"Process Scheduling","text":"Processes migrate among the various queues.
\u5f53\u6211\u4eec\u60f3\u8981\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u65f6\uff0c\u76f4\u63a5\u901a\u8fc7\u53cc\u5411\u94fe\u8868\u63a5\u4e0a\u5373\u53ef\u3002
Context Switch is Pure Overhead 1. Should be minimized.
Code
x8
points to the task_struct of the process to be switched inswitch_to()
set ?task_struct
is on the heap.fork()
can return two different values in parent and child processes?Because fork()
, then, two set of context values.
see https://note.hobbitqia.cc/OS/chap02/#context-switch
"},{"location":"OS/lec5/Lecture5/#segmentation","title":"Segmentation","text":""},{"location":"OS/lec5/Lecture5/#segmentation-fault","title":"Segmentation Fault:","text":"
Segmentation -- Not fixed size, can grow or shrink as needed.
Still have External Fragmentation -- Segments are of varying sizes, so memory may not be used efficiently.
"},{"location":"PPL/lec1/lec1/#lec-1-lambda-calculus","title":"Lec 1 \\(\\lambda\\)-Calculus","text":"
Lambda calculus is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution.
"},{"location":"PPL/lec1/lec1/#definition","title":"Definition","text":"CANNOT CHANGE NAME OF FREE VARIABLE
CAN CHANGE NAME OF BOUND VARIABLE * \\(BV(x) = \\emptyset\\) * \\(BV(\\lambda x.t') = BV(t') \\cup \\{x\\}\\) * \\(BV(t_1 t_2) = BV(t_1) \\cup BV(t_2)\\)
"},{"location":"PPL/lec1/lec1/#example2","title":"Example2","text":"True: \\(\\lambda x.\\lambda y.x\\)
False: \\(\\lambda x.\\lambda y.y\\)
Example: (in steps):
not
and
or
xor
Let's walk through a concrete example of the multiplication function in lambda calculus, using Church numerals to represent numbers. The Church numeral for a natural number \\( n \\) is defined as a function that applies a given function \\( f \\) to an argument \\( x \\), \\( n \\)-times. Here\u2019s a quick refresher on the Church numerals:
Multiplication Function: The multiplication function is: [ mul = \\lambda m.\\lambda n.\\lambda f. m (n f) ] This means \\( m \\) is applied \\( n \\)-times to a function \\( f \\).
Example: Multiply 2 and 3 To demonstrate, we'll compute \\( 2 \\times 3 \\) using Church numerals for 2 and 3.
Now, let\u2019s compute \\( mul(2)(3) \\):
Thus, \\( mul\\ 2\\ 3 = \\lambda f. \\lambda x. f(f(f(f(f(f(x)))))) \\), which is the Church numeral for 6.
Conclusion: This result shows that applying the multiplication function to the Church numerals for 2 and 3 correctly gives the Church numeral for 6, as expected.
In lambda calculus, exponentiation (exp
) can be defined as repeated application of multiplication. Specifically, exponentiation can be thought of as applying a number \\( n \\) (the exponent) to a function \\( m \\) (the base) a number of times. This means we can define exponentiation in terms of function application.
Explanation: - \\( m \\) is the base, which is a Church numeral. - \\( n \\) is the exponent, which is also a Church numeral. - In lambda calculus, \\( n\\ m \\) means applying \\( m \\), \\( n \\)-times. Since a Church numeral represents the number of times to apply a function, this effectively results in exponentiation.
This function takes two Church numerals \\( m \\) and \\( n \\), and applies the base \\( m \\), \\( n \\)-times. The result is \\( m^n \\), which is the Church numeral representing the result of exponentiation.
Example: \\( 2^3 \\) Let\u2019s compute \\( 2^3 \\) using this definition.
Church Numerals: - \\( 2 = \\lambda f. \\lambda x. f(f(x)) \\) - \\( 3 = \\lambda f. \\lambda x. f(f(f(x))) \\)
Exponentiation Function:
Expanding this, each application of \\( 2 \\) adds another layer of function application:
\\[ \\begin{align*} &= \\lambda x. 2(2(f(f(x)))\\\\ &= \\lambda x. 2(f(f(f(f(x))))\\\\ &= \\lambda x. f(f(f(f(f(f(f(f(x)))))))\\\\ \\end{align*} \\]This result corresponds to the Church numeral for \\( 8 \\), which is \\( 2^3 \\).
"},{"location":"PPL/lec2/lec2/","title":"Lecture 2 - Abstract Syntax Trees","text":""},{"location":"PPL/lec2/lec2/#lec-2-abstract-syntax-trees","title":"Lec 2 Abstract Syntax Trees","text":""},{"location":"PPL/lec2/lec2/#definition","title":"Definition","text":"
Operand and arity
num[n]
\\(n\\in \\mathbb{N}\\)plus
,times
,\u6bcf\u4e2a\u53c2\u6570\u90fd\u662fExp\u7684astplus (num[2] ;times( num[3]; x])
EST can be drawn as:\n plus\n / \\\n num[2] times\n / \\\n num[3] x\n
"},{"location":"PT/PT/#probability-theory","title":"Probability Theory","text":""},{"location":"PT/PT/#_1","title":"\u968f\u673a\u53d8\u91cf\u4e0e\u5206\u5e03\u51fd\u6570","text":""},{"location":"PT/PT/#_2","title":"\u968f\u673a\u53d8\u91cf","text":""},{"location":"PT/PT/#_3","title":"\u5206\u5e03\u51fd\u6570","text":"
\\(F (x) = P (\u03be \u2a7d x), \u2212\u221e < x < +\u221e\\)
(1) \\(a < b, F (a) \u2a7d F (b)\\)
(2). \\(lim_{x\\rightarrow-\\infty} F(x)=0\\\\ lim_{x\\rightarrow +\\infty} F(x)=1\\)
(3) \\(\u2203F(x\u22120)= lim_{h\u21920+}F(x\u2212h)\\) -- \u5904\u5904\u5de6\u6781\u9650\u5b58\u5728 \\(F(x+0)= lim_{h\u21920+} F(x+h)=F(x)\\) -- \u53f3\u8fde\u7eed
\u6ce8\u610f\uff0c\u5982\u679c\u4fee\u6539\u5206\u5e03\u51fd\u6570\u5b9a\u4e49\u4e3a\\(F (x) = P (\u03be < x), \u2212\u221e < x < +\u221e\\)\u90a3\u4e48 (3) \u5e94\u8be5\u4fee\u6539\u4e3a\u5904\u5904\u53f3\u6781\u9650\u5b58\u5728\uff0c\u5de6\u8fde\u7eed\u3002
"},{"location":"PT/PT/#_4","title":"\u5bc6\u5ea6\u51fd\u6570","text":"\u82e5\u968f\u673a\u53d8\u91cf\\(\\epsilon\\)\u53ef\u53d6\u67d0\u4e2a\u533a\u95f4\u4e2d\u7684\u4e00\u5207\u503c\uff0c\u5e76\u4e14\u5b58\u5728\u67d0\u4e2a\u975e\u8d1f\u7684\u53ef\u79ef\u51fd\u6570\\(p(x)\\) , \u4f7f\u5206\u5e03\u51fd\u6570\\(F(x)\\)\u6ee1\u8db3\\(F(x)=\\int_{-\\infty}^xp(y)dy\\) \u5219\u79f0\\(\\epsilon\\) \u4e3a\u8fde\u7eed\u6027\u968f\u673a\u53d8\u91cf\uff0c\u79f0p(x)\u4e3a\\(\\epsilon\\)\u7684\u6982\u7387\u5bc6\u5ea6\u51fd\u6570
1.\u5206\u5e03\u51fd\u6570
2.\u5bc6\u5ea6\u51fd\u6570
\uff081\uff09\u57fa\u672c\u540c\u968f\u673a\u53d8\u91cf\u7684\u60c5\u51b5
\uff082\uff09\\(\\frac{\\partial^nF(x_1,x_2,\u2026\u2026,X_n)}{\\partial x_1\u2026\u2026\\partial x_n}=p(x_1,x_2,\u2026\u2026,x_n)\\)
3.\u8fb9\u9645\u5bc6\u5ea6
"},{"location":"PT/PT/#_6","title":"\u72ec\u7acb\u6027","text":"\u5bf9\u4e8e\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\uff0c\u7528\u5206\u5e03\u51fd\u6570\u5b9a\u4e49\u7684\u968f\u673a\u53d8\u91cf\u7684\u72ec\u7acb\u6027\u6761\u4ef6\\(F(x,y) = F_\u03be(x)F_\u03b7(y)\\) \u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u5f52\u7eb3\u63a8\u51fa\u5176\u7279\u6709\u72ec\u7acb\u6027\u6761\u4ef6\\(p_{ij} = p_i\u00b7 p_j\\) \u540c\u7406\uff0c\u5f88\u65b9\u4fbf\u5730\u53ef\u4ee5\u7528\u540e\u8005\u53cd\u63a8\u51fa\u524d\u8005\u3002\u56e0\u6b64\u5728\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4e24\u79cd\u5b9a\u4e49\u662f\u7b49\u4ef7\u7684
\u8bbe \\(p(x, y)\\)\u4e0e \\(p_\u03be (x), p_\u03b7 (y)\\) \u5206\u522b\u4e3a\u8fde\u7eed\u578b\u968f\u673a\u5411\u91cf \\((\u03be, \u03b7)\\)\u7684\u8054\u5408\u5bc6\u5ea6\u548c\u8fb9\u9645\u5bc6\u5ea6, \u5219 \\(\u03be, \u03b7\\)\u76f8\u4e92\u72ec\u7acb\u7684\u5145\u8981\u6761\u4ef6\u662f\\(p(x, y) = p_\u03be (x)p_\u03b7 (y)\\)
"},{"location":"PT/PT/#_7","title":"\u6761\u4ef6\u5206\u5e03","text":"\u79bb\u6563\u578b
\u8fde\u7eed\u578b
\u6761\u4ef6\u5206\u5e03\u51fd\u6570 \\(P(\\eta \\le y | \\epsilon =x)=\\int_{-\\infty}^{y}\\frac{p(x,v)}{p_\\epsilon(x)}dv\\)
\u6761\u4ef6\u5bc6\u5ea6\u51fd\u6570 \\(p_{\\eta | \\epsilon}(y|x)=\\frac{p(x,y)}{p_\\epsilon(x)}\\)
\\(P(A)=P(A,-\\infty<x<+\\infty)=\\int_{-\\infty}^{+\\infty} P(A|X=x)p_X(x)dx=\\int_{-\\infty}^{+\\infty}g(x)P_X(x)dx\\)
BOOK p79 2.24
"},{"location":"PT/PT/#_8","title":"\u968f\u673a\u53d8\u91cf\u7684\u51fd\u6570\u53ca\u5176\u5206\u5e03","text":"Therom 1
\u5047\u8bbef(x)\u4e25\u683c\u5355\u8c03\uff0c\u53cd\u51fd\u6570\u6709\u8fde\u7eed\u5bfc\u6570\uff0c\u5219\\(\\eta = f(\\epsilon)\\)\u4e5f\u662f\u8fde\u7eed\u6027\u968f\u673a\u53d8\u91cf\uff0c\u5176\u5bc6\u5ea6\u51fd\u6570\u4e3a
\\(g(y)=\\left\\{\\begin{array}{**lr**}p(f^{-1}(y)|f^{-1}|(y))'| y\\in f(x)\u7684\u503c\u57df\\\\0\\ \\ \\ \\ \u5176\u4ed6\\end{array}\\right.\\)
note \u5728\u4e0d\u91cd\u53e0\u7684\u533a\u95f4\u4e0a\u9010\u6bb5\u4e25\u683c\u5355\u8c03\uff0c\u5404\u6bb5\u7684\u53cd\u51fd\u6570\u90fd\u6709\u8fde\u7eed\u5012\u6570
\\(g(y)=\\left\\{\\begin{array}{**lr**}\\sum p(h_i(y)|h'_i|(y)|) y\\in \u5404h_i(y)\u7684\u5b9a\u4e49\u57df\\\\0\\ \\ \\ \\ \u5176\u4ed6\\end{array}\\right.\\)
Therom 2
\u8bbe\\(\\epsilon\\) \u6709\u8fde\u7eed\u7684\u5206\u5e03\u51fd\u6570\\(F(x)\\),\u6c42\\(\\theta=F(\\epsilon)\\)\u7684\u5206\u5e03 \\(P_{\\theta}(y)=y\\) \u670d\u4ece[0,1]\u4e0a\u7684\u5747\u5300\u5206\u5e03
Therom 3
\u82e5 \\(\\theta\\) \u670d\u4ece[0,1]\u4e0a\u7684\u5747\u5300\u5206\u5e03,F(x)\u6ee1\u8db3\u5206\u5e03\u51fd\u6570\u7684\u4e09\u4e2a\u6027\u8d28\uff0c\\(\\epsilon=F^{-1}(\\theta)\\) \u5219 \\(P_{\\epsilon}(x)=F(x)\\)
Therom 1\u5377\u79ef\u516c\u5f0f
Therom 2 \u82e5\\((\\epsilon_1 \\epsilon_2)\\)\u662f\u8fde\u7eed\u6027\u968f\u673a\u5411\u91cf\uff0c\u5219\\(\\eta=\\frac{\\epsilon_1}{\\epsilon_2}\\)\u662f\u8fde\u7eed\u578b\u968f\u673a\u53d8\u91cf\uff0c\u5176\u5bc6\u5ea6\u51fd\u6570\u4e3a
\\(p_{\\eta}(z)=\\int_{-\\infty}^{+\\infty}p(zx,x)|x|dx\\)
\\(F_{\\eta}(y)=\\int_{-\\infty}^yp_{\\eta}(z)dz\\)
\u6b21\u5e8f\u7edf\u8ba1\u8ba1\u91cf\u5206\u5e03
P89-93
\u8bbe\u968f\u673a\u53d8\u91cfX\u548cY\u76f8\u4e92\u72ec\u7acb\uff0c\u5e76\u4e14Z\u4ec5\u662fX\u7684\u51fd\u6570\uff0cW\u4ec5\u662fY\u7684\u51fd\u6570\uff1bZ=g(X) W=h(Y) \u5176\u4e2dg\u548ch\u90fd\u662f\u6ce2\u96f7\u5c14\u53ef\u6d4b\u51fd\u6570\uff0c\u90a3\u4e48Z\u548cW\u4f9d\u65e7\u72ec\u7acb
"},{"location":"PT/PT/#_9","title":"\u5e38\u89c1\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\u5206\u5e03","text":"1.\u9000\u5316\u5206\u5e03 \\(P(\\epsilon = c)=1\\)
2.\u4e24\u70b9\u5206\u5e03 \\(\\begin{bmatrix}x1&x2\\\\p&q\\end{bmatrix}\\) \\(p+q=1 p,q>0\\)
3.\u5e15\u65af\u5361\u5206\u5e03 \\(P(\\epsilon=k)=\\begin{pmatrix}k-1\\\\r-1\\end{pmatrix}p^rq^{k-r}\\)
4.\u4e8c\u9879\u5206\u5e03
\u82e5\u5b58\u5728\u6b63\u5e38\u6570\\(\\lambda\\) \u5f53\\(n \\rightarrow+\\infty\\)\u65f6\uff0c\u6709\\(bp_n \\rightarrow +\\lambda\\) ,\u5219
\\(lim_{n \\rightarrow+\\infty} b(k,n,p)=\\frac{\\lambda^k}{k!}e^{-\\lambda}\\)
\u901a\u5e38\uff0cn\u4e0ep\u65e0\u5173\uff0c\u4f46\u662fn\u5f88\u5927\uff0cp\u5f88\u5c0f\uff0cnp\u4e0d\u662f\u5f88\u5927\u7684\u65f6\u5019\uff0c\u53ef\u8fd1\u4f3c\u5730\u53d6\\(\\lambda=np\\)
\\(P(\\epsilon_n=j)\\approx \\frac{1}{\\sqrt{2\\pi npq}}e^{-x^2/2} \\ \\ x=\\frac{j-np}{\\sqrt{npq}}\\)
4.\u6cca\u677e\u5206\u5e03
\\(P(\\epsilon=k)=\\frac{\\lambda^k}{k!}e^{-\\lambda}\\)
5.\u51e0\u4f55\u5206\u5e03\u300c\u65e0\u8bb0\u5fc6\u6027\u300d
\\(P(\\epsilon=k)=pq^{k-1}\\)
6.\u8d85\u51e0\u4f55\u5206\u5e03
\u300c\u4fee\u6b63\uff1aM+N-n \u6709\u5927\u5c0f\u5199\u9519\u8bef\u300d
"},{"location":"PT/PT/#_10","title":"\u5e38\u89c1\u8fde\u7eed\u578b\u968f\u673a\u53d8\u91cf\u5206\u5e03","text":"1.\u5747\u5300\u5206\u5e03
2.\u6b63\u6001\u5206\u5e03
\\(Proof\\)
\\(\\begin{align*}(\\frac{1}{\\sqrt{2\\pi}\\sigma}\\int_{-\\infty}^{+\\infty} e^{-\\frac{(t-a)^2}{2\\sigma^2}}dt)^2 &= (\\frac{1}{\\sqrt{2\\pi}}\\int_{-\\infty}^{+\\infty} e^{-\\frac{t^2}{2}}dt)^2\\\\&=\\frac{1}{2\\pi}\\int_{-\\infty}^{+\\infty} \\int_{-\\infty}^{+\\infty} e^{\\frac{t^2+s^2}{2}}dtds\\\\ &=\\frac{1}{2\\pi} \\int_{0}^{2\\pi}d\\theta\\int_{0}^{+\\infty} re^{-\\frac{r^2}{2}}dr\\\\ &=1\\end{align*}\\)
2.2 n\u7ef4\u6b63\u6001\u5206\u5e03
\u8bbe \\(B = (b_{ij} )\\) \u4e3a n \u7ef4\u6b63\u5b9a\u5bf9\u79f0\u77e9\u9635, \\(|B|\\) \u4e3a\u5176\u884c\u5217\u5f0f, \\(B^{\u22121}\\) \u4e3a\u5176\u9006,
\u53c8\u8bbe \\(x = (x1, x2, \u00b7 \u00b7 \u00b7 , xn)^T\\) , \\(a = (a1, a2, \u00b7 \u00b7 \u00b7 , an)^T\\) , \u5219\u79f0
\\(p(\\vec{x})=\\frac{1}{(2\\pi)^{n/2}|B|^{1/2}} exp(-\\frac{1}{2}(x-a)^TB^{-1}(x-a))\\) \u4e3an\u7ef4\u6b63\u6001\u5bc6\u5ea6\u51fd\u6570
2.3 \u4e8c\u7ef4\u7684\u60c5\u51b5
\\(B=\\begin{pmatrix}\\sigma_1^2 & r\\sigma_1\\sigma_2\\\\ r\\sigma_1\\sigma_2&\\sigma_2^2\\end{pmatrix}. B^{-1}=\\begin{pmatrix}\\sigma_2^2 & -r\\sigma_1\\sigma_2\\\\ -r\\sigma_1\\sigma_2&\\sigma_1^2\\end{pmatrix}\\)
\\(p(x,y)=\\frac{1}{2\\pi\\sigma_1\\sigma_2\\sqrt{1-r^2}}exp(-\\frac{1}{2(1-r^2)}\\times[\\frac{(x-a)^2}{\\sigma_1^2} - \\frac{2r(x-a)(y-b)}{\\sigma_1\\sigma_2} + \\frac{(y-b)^2}{\\sigma_2^2}])\\)
\u7b80\u8bb0\u4f5c \\((\\epsilon,\\eta)\\) ~ \\(N(a,b,\\sigma_1^2,\\sigma_2^2,r)\\)
2.4 \u6761\u4ef6\u5206\u5e03
2.5 \u6b63\u6001\u5206\u5e03\u7684\u51fd\u6570
\\(\\epsilon\\)~ \\(N(a_1,\\sigma_1^2)\\) \\(\\eta\\)~\\(N(a_2,\\sigma_2^2)\\) \\(\\eta + \\epsilon\\)~\\(N(a_1+a_2,\\sigma_1^2+\\sigma_2^2)\\)
\\(\\epsilon\\) ~ \\(N(0,\\ 1) \\ \\eta = \\epsilon^2\\)
\u82e5\\((X,Y)\\)~\\(N(\\mu_1,\\mu_2,\\sigma_1^2,\\sigma_2^2,r)\\) \u5219 \\(C_1X+C_2Y\\)~\\(N(C_1\\mu_1+C_2\\mu_2,C_1^2\\sigma_1^2+C_2^2\\sigma_2^2+2C_1C_2r\\sigma_1\\sigma_2)\\)
2.6 \u591a\u7ef4\u6b63\u6001\u5206\u5e03 P93
\\(\\vec{\\eta}=C\\vec{\\epsilon}+a\\) \u5219 \\(\\vec{\\eta}\\) ~ \\(N(C\\vec{\\mu}+a,C\\Sigma C^T)\\)
3.\u6307\u6570\u5206\u5e03
\\(p(x)=\\left\\{\\begin{array}{**lr**}\\lambda e^{-\\lambda x}\\ x\\ge 0 \\\\0 \\ \\ \\ \\ \\ \\ \\ \\ \\ x < 0\\end{array}\\right.\\)
\\(F(x)=\\left\\{\\begin{array}{**lr**}1-e^{-\\lambda x}\\ x\\ge 0 \\\\0 \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ x < 0\\end{array}\\right.\\)
4.T\u5206\u5e03
5.\u5a01\u5e03\u5c14\u5206\u5e03
6.\u5e15\u7d2f\u6258\u5206\u5e03
7.\\(\\beta\\)\u5206\u5e03
8.\u67ef\u897f\u5206\u5e03
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/","title":"\u6570\u5b57\u7279\u5f81\u4e0e\u7279\u5f81\u51fd\u6570","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_1","title":"\u6570\u5b66\u671f\u671b","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_2","title":"\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\u7684\u6570\u5b66\u671f\u671b","text":"
\u8bbe\u7f6e\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\\(\\epsilon\\) \u7684\u5206\u5e03\u5217\u4e3a
\\(\\begin{bmatrix}x_1&x_2&\u2026\u2026&x_k&\u2026\u2026\\\\p_1&p_2&\u2026\u2026&p_k&\u2026\u2026\\end{bmatrix}\\)
\u5982\u679c\u7ea7\u6570\\(\\sum_kx_kp_k\\)\u7edd\u5bf9\u6536\u655b\uff0c\u5219\u79f0\u6b64\u7ea7\u6570\u7684\u548c\u4e3a\\(\\epsilon\\) \u7684\u6570\u5b66\u671f\u671b\u6216\u5747\u503c\\((mean)\\)\uff0c\u8ba1\u4f5c\\(E\\epsilon=\\sum_kx_kp_k\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_3","title":"\u8fde\u7eed\u578b\u968f\u673a\u53d8\u91cf\u7684\u6570\u5b66\u671f\u671b","text":"\u8bbe\\(\\epsilon\\)\u4e3a\u8fde\u7eed\u578b\u968f\u673a\u53d8\u91cf\uff0c\u6709\u5bc6\u5ea6\u51fd\u6570\\(p(x)\\)\u5f53\\(\\int_{-\\infty}^{+\\infty}|x|p(x)dx<\\infty\u65f6\\) \u79f0\\(E\\epsilon=\\int_{-\\infty}^{+\\infty}xp(x)dx\\)\u4e3a\\(\\epsilon\\)\u7684\u6570\u5b66\u671f\u671b
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_4","title":"\u4e00\u822c\u5b9a\u4e49","text":"\u8bbe\u968f\u673a\u53d8\u91cf\\(\\epsilon\\)\u6709\u5206\u5e03\u51fd\u6570\\(F(x)\\)\uff0c\u82e5\\(\\int_{-\\infty}^{+\\infty}|x|d F(x)<\\infty\\) \u79f0\\(E\\epsilon=\\int_{-\\infty}^{+\\infty}xd F(x)\\)\u4e3a\\(\\epsilon\\)\u7684\u6570\u5b66\u671f\u671b
\u8bbe\\(\\epsilon\\ \\eta\\)\u4e3a\u968f\u673a\u53d8\u91cf\u5206\u5e03\u51fd\u6570\u5206\u522b\u4e3a\\(F_{\\epsilon}(x)\\ F_{\\eta}(x)\\) \uff1b\\(f(x)\\)\u662f\u4e00\u5143\u6ce2\u96f7\u5c14\u51fd\u6570\uff0c\u8ba1\\(\\eta=f(\\epsilon)\\) \u5219 \\(E_{\\eta}=\\int_{-\\infty}^{+\\infty}xdF_{\\eta}(x)=\\int_{-\\infty}^{+\\infty}f(x)dF_{\\epsilon}(x)\\)
(Stein \u5f15\u7406) \\(P119\\)
\u82e5\\(a\\le \\epsilon \\le b\\)\u5219 \\(E\\epsilon\\) \u5b58\u5728\u4e14\\(a\\le E\\epsilon\\le b\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#1_1","title":"\u6027\u8d281\u2018","text":"\u82e5\\(|\\epsilon| < \\eta\\) \u4e14\\(E\\eta\\) \u5b58\u5728 \u5219\\(E\\epsilon\\)\u5b58\u5728 \u4e14\\(|E\\epsilon|\\le E|\\epsilon|\\le E\\eta\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#2","title":"\u6027\u8d282","text":"\\(E_{\\epsilon_1},E_{\\epsilon_2},E_{\\epsilon_3}\u2026\u2026 E_{\\epsilon_n}\\)\u5b58\u5728\uff0c\u5219\u5bf9\u4efb\u610f\u5e38\u6570\\(c_1,c_2,\u2026\u2026c_n\\)\u548cb \\(E(\\sum_{i=1}^nc_i\\epsilon_i+b)\\)\u5b58\u5728\uff0c\u4e14\\(E(\\sum_{i=1}^nc_i\\epsilon_i+b)=\\sum_{i=1}^nc_iE\\epsilon_i+b\\)
\u7279\u522b\u5730\uff1a\\(E(\\sum_{i=1}^n\\epsilon_i)=\\sum_{i=1}^nE\\epsilon_i\\) \\(E(c\\epsilon)=cE\\epsilon\\)
\u82e5\\(\\epsilon_1,\\epsilon_2\u2026\u2026\\epsilon_n\\)\u76f8\u4e92\u72ec\u7acb\uff0c\u5404\\(E_{\\epsilon_i}\\)\u5b58\u5728\uff0c\u5219\\(E(\\epsilon_1,\\epsilon_2\u2026\u2026,\\epsilon_n)=E\\epsilon_1E\\epsilon_2\u2026\u2026E\\epsilon_n\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#4","title":"\u6027\u8d284(\u6709\u754c\u6536\u655b\u5b9a\u7406)","text":"\u5047\u8bbe\u5bf9\u4efb\u610f\\(\\omega\\in\\Omega\\) \u6709\\(\\lim_{n\\rightarrow\\infty}\\epsilon(\\omega)=\\epsilon(\\omega)\\)\u5e76\u4e14\uff0c\u5bf9\u4e00\u5207\u7684\\(n >=1\\) \\(|\\epsilon_n|\\le M\\) M\u4e3a\u5e38\u6570\uff0c\u5219\\(\\lim_{n\\rightarrow\\infty}E\\epsilon_n=E\\epsilon\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_7","title":"\u6761\u4ef6\u671f\u671b","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_8","title":"\u6761\u4ef6\u671f\u671b","text":"\\(E(\\eta|\\epsilon=x)=\\int_{-\\infty}^{+\\infty}ydF_{\\eta|\\epsilon}(y|x)\\)
\\(E(\\eta|\\epsilon=x)=\\int_{-\\infty}^{+\\infty}yp_{\\eta|\\epsilon}(y|x)dy\\)
\\(E(\\eta|\\epsilon=x)=\\sum yp_{\\eta|\\epsilon}(y|x)dy\\)
\u518d\u5bf9\\(E(\\eta|\\epsilon=x)\\)\u6c42\u671f\u671b\uff0c\u4f1a\u5f97\u5230\\(E[E(\\eta|\\epsilon=x)]=E\\eta\\)
\u8fde\u7eed\u578b
\u79bb\u6563\u578b
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_9","title":"\u5168\u671f\u671b\u516c\u5f0f","text":"\\(\\epsilon\\) \u662f\u79bb\u6563\u578b\u968f\u673a\u53d8\u91cf\u65f6\uff0c\u8ba1\\(p_i=P(\\epsilon=x_i) \\Rightarrow E\\eta=\\sum_i p_iE(\\eta|\\epsilon=x_i)=\\sum_iE(\\eta|\\epsilon=x_i)P(\\epsilon=x_i)\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_10","title":"\u6761\u4ef6\u671f\u671b\u7684\u6027\u8d28","text":"\\(Var\\epsilon=E(\\epsilon-E\\epsilon)^2=E\\epsilon^2-(E\\epsilon)^2\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#chebyshev","title":"Chebyshev \u4e0d\u7b49\u5f0f","text":"\\(P(|\u03be\u2212E\u03be|\u2265\u03b5)\u2264 Var\u03be\\)
\\(P(|\u03be \u2212 E\u03be| \u2265 \u03b5) =\\int_{|x-E\u03be|>=\\epsilon}dF(x)\\le \\int_{|x-E\u03be|>=\\epsilon}\\frac{(x-E\u03be)^2}{\\epsilon^2}dF(x)\\\\ \\le \\frac{1}{\\epsilon^2}\\int_ {-\\infty}^{+\\infty}(x-E\u03be)^2dF(x) =\\frac{Var\u03be}{\\epsilon^2}\\)
\\(Var\u03be=0 \\Leftrightarrow P(\u03be=c)=1\\) (c\u662f\u5e38\u6570)
\\(Var(c\u03be+b)=c^2Var\u03be\\)
\u82e5 \\(c \\ne E\u03be\\ \\ \\ var\u03be<E(\u03be-c)^2\\)
\\(p130\\)\u6700\u4f73\u9884\u6d4b\u95ee\u9898
\\(Var (\\sum\u03be_i )= \\sum Var\u03be_i +2\\sum_{1\\le i<j\\le n} E(\u03be_i \u2212E\u03be_i)(\u03be_j \u2212E\u03be_j)\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_13","title":"\u534f\u65b9\u5dee","text":"\u8bbe\\(\\epsilon_i \\epsilon_j\\)\u7684\u8054\u5408\u5206\u5e03\u51fd\u6570\u4e3a\\(F_{ij}(x,y)\\)\u82e5\\(E|(\\epsilon_i-E\\epsilon_i)(\\epsilon_j-E\\epsilon_j)|<\\infty\\) \u79f0 \\(E(\\epsilon_i-E\\epsilon_i)(\\epsilon_j-E\\epsilon_j)=\\int_{-\\infty}^{+\\infty}\\int_{-\\infty}^{+\\infty}(x-E\\epsilon_i)(y-E\\epsilon_j)dF_{ij}(x,y)\\)\u4e3a\\(\\epsilon_i\\)\u548c\\(\\epsilon_j\\)\u7684\u534f\u65b9\u5dee\uff0c\u8ba1\u4f5c\\(Cov(\\epsilon_i\\ \\epsilon_j)\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_14","title":"\u6027\u8d28","text":"\u4ee4\\(\\epsilon^*=(\\epsilon-E\\epsilon)/\\sqrt{Var\\epsilon} \\\\ \\eta^*=(\\eta-E\\eta)/\\sqrt{Var\\eta}\\)
\u79f0\\(r_{\\epsilon\\eta}=Cov(\\epsilon^*,\\eta^*)=\\frac{E(\\epsilon-E\\epsilon)(\\eta-E\\eta)}{\\sqrt{Var\\epsilon Var\\eta}}\\) \u4e3a\\(\\epsilon \\ \\eta\\)\u7684\u76f8\u5173\u7cfb\u6570 \\(E\\epsilon^* \\eta^*\\) instead of \\(Cov(\\epsilon^*,\\eta^*)\\)?
\\(r_{\\epsilon\\eta}=1\\)\u6210\u7acb\\(iff\\ P(\\frac{\\epsilon-E\\epsilon}{\\sqrt{Var\\epsilon}}=\\frac{\\eta-E\\eta}{\\sqrt{Var\\eta}})=1\\)
\\(r_{\\epsilon\\eta}=-1\\)\u6210\u7acb\\(iff\\ P(\\frac{\\epsilon-E\\epsilon}{\\sqrt{Var\\epsilon}}=-\\frac{\\eta-E\\eta}{\\sqrt{Var\\eta}})=1\\)
\\((1) Cov(\\epsilon,\\eta)=0\\)
\\((2) \\epsilon \\ \\eta\\)\u4e0d\u76f8\u5173
\\((3) E\\epsilon\\eta=E\\epsilon E\\eta\\)
\\((4)Var{\\epsilon+\\eta}=Var\\epsilon+Var\\eta\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_17","title":"\u77e9","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_18","title":"\u7279\u5f81\u51fd\u6570","text":"\\(f(t)=Ee^{it\\epsilon}\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#example","title":"Example","text":"\\(f(t)\\)\u4e3a\u7279\u5f81\u51fd\u6570\\(\\Leftrightarrow f(t)\\)\u975e\u8d1f\u5b9a\uff0c\u8fde\u7eed\u4e14\\(f(0)=1\\) * \\(\\epsilon_1\u2026\u2026\\epsilon_n\\)\u76f8\u4e92\u72ec\u7acb\uff0c\u7279\u5f81\u51fd\u6570\u5206\u522b\u4e3a\\(f_1(t)\u2026\u2026f_n(t)\\) \u8bb0\\(\\eta=\\epsilon_1+\u2026\u2026+\\epsilon_n\\) \u5219\\(\\eta\\)\u7684\u7279\u5f81\u51fd\u6570\\(f_\\eta(t)=f_1(t)f_2(t)\u2026\u2026f_n(t)\\) * \u82e5\\(E\\epsilon^n\\)\u5b58\u5728\uff0c\u5219\\(f(t)\\)n\u6b21\u53ef\u5fae\uff0c\u8fdb\u800c \\(k\\le n\\)\u65f6\uff0c\\(f^{k}(t)=i^k\\int_{-\\infty}^{+\\infty}x^ke^{itx}dF(x),f^{(k)}(0)=i^kE\\epsilon^k\\)\u7279\u522b\u5730 \\(E\\epsilon^2\\)\u5b58\u5728\u65f6 \\(E\\epsilon=-if'(0),E\\epsilon^2=-f''(0),Var\\epsilon=-f''(0)+[f'(0)]^2\\)
\u53cd\u8fc7\u6765\uff0c\u82e5n\u4e3a\u5076\u6570\uff0c\u4e14\\(f^{(n)}(0)\\)\u5b58\u5728\uff0c\u5219\\(E\\epsilon^n\\)\u5b58\u5728 * \\(\\eta=a\\epsilon+b, f_\\eta(t)=e^{ibt}f(at)\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_19","title":"\u9006\u8f6c\u516c\u5f0f","text":"\u8bbe\u5206\u5e03\u51fd\u6570\\(F(x)\\)\u7684\u7279\u5f81\u51fd\u6570\u4e3a\\(f(t)\\) \u53e6x1\uff0cx2\u4e3a\\(F(x)\\)\u7684\u8fde\u7eed\u70b9\uff0c\u5219\\(F(x_2)-F(x_1)=lim_{T\\rightarrow \\infty}\\frac{e^{-itx_1}=e^{-itx^2}}{it}f(t)dt\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_20","title":"\u552f\u4e00\u6027\u5b9a\u7406","text":"\u5206\u5e03\u51fd\u6570\u53ef\u7531\u7279\u5f81\u51fd\u6570\u552f\u4e00\u786e\u5b9a
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_21","title":"\u9006\u5085\u7acb\u53f6\u53d8\u6362","text":"\u8bbe\\(f(t)\\)\u662f\u7279\u5f81\u51fd\u6570\u4e14\\(\\int_{=\\infty}^{+\\infty}|f(t)|dt<\\infty\\)\u5219\u5206\u5e03\u51fd\u6570F(x)\u8fde\u7eed\uff0c\u6b64\u65f6 \\(F'(x)=\\frac{1}{2\\pi}\\int_{-\\infty}^{+\\infty}e^{-itx}f(t)dt\\) * \u82e5f(t)\u662f\u67d0\u968f\u673a\u53d8\u91cf\u7684\u7279\u5f81\u51fd\u6570\uff0c\u5219\\(\\bar{f(t)},|f(t)|^2\\)\u4e5f\u662f\u7279\u5f81\u51fd\u6570 \\(f(-t)=\\bar{f(t)}\\)\u662f\\(-\\epsilon\\)\u7684\u7279\u5f81\u51fd\u6570 \\(\\epsilon_1-\\epsilon_2\\)\uff08\u72ec\u7acb\u540c\u5206\u5e03\uff09\u7684\u7279\u5f81\u51fd\u6570\u4e3a\\(f(t)\\bar{f(t)}=|f(t)|^2\\)
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_22","title":"\u5206\u5e03\u51fd\u6570\u7684\u53ef\u52a0\u6027","text":""},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_23","title":"\u591a\u5143\u7279\u5f81\u51fd\u6570","text":"\u8bbe\u968f\u673a\u5411\u91cf\\(\\vec{\\epsilon}=(\\epsilon_1,\u2026\u2026,\\epsilon_n)'\\)\u7684\u5206\u5e03\u51fd\u6570\u4e3a\\(F(x_1\u2026\u2026,x_n)\\)\u79f0\\(f(t_1,\u2026\u2026t_n)=\\int_{-\\infty}^{+\\infty}\u2026\u2026\\int_{-\\infty}^{+\\infty}e^{i(t_1x_1+\u2026\u2026+t_nx_n)}dF(x_1\u2026\u2026\uff0cx_n)\\)\u4e3a\u4ed6\u7684\u7279\u5f81\u51fd\u6570 * \\(\\eta=a_1\\epsilon_1\u2026\u2026+a_n\\epsilon_n\\\\f_\\eta(t)=Ee^{it\\sum_{k=1}^na_k\\epsilon_k}=f(a_1t,\u2026\u2026a_nt)\\) \u5176\u4ed6\u8be6\u7ec6\u89c1P48
"},{"location":"PT/%E6%95%B0%E5%AD%97%E7%89%B9%E5%BE%81%E4%B8%8E%E7%89%B9%E5%BE%81%E5%87%BD%E6%95%B0/#_24","title":"\u591a\u5143\u6b63\u6001\u5206\u5e03","text":"P149-P154
"},{"location":"PT/%E6%9E%81%E9%99%90%E5%AE%9A%E7%90%86/","title":"\u6781\u9650\u5b9a\u7406","text":""},{"location":"PT/%E6%9E%81%E9%99%90%E5%AE%9A%E7%90%86/#_1","title":"\u4f9d\u5206\u5e03\u6536\u655b\u4e0e\u4e2d\u5fc3\u6781\u9650\u5b9a\u7406","text":""},{"location":"Papers/3DGS/paper/","title":"3DGS","text":""},{"location":"Papers/3DGS/paper/#3d-gaussian-splatting-for-real-time-radiance-field-rendering","title":"3D Gaussian Splatting for Real-Time Radiance Field Rendering","text":""},{"location":"Papers/3DGS/paper/#differentiable-3d-gaussian-splatting","title":"Differentiable 3D Gaussian Splatting","text":""},{"location":"Papers/3DGS/paper/#3d-gaussian","title":"3D Gaussian","text":"
3D Gaussian : \\(G(x) = \\frac{1}{(2\\pi)^{3/2}\\sigma^3}e^{-\\frac{||x-\\mu||^2}{2\\sigma^2}}\\)
reference : 3D\u9ad8\u65af
Project the 3D Gaussian to 2D Gaussian and then render it.
"},{"location":"Papers/3DGS/paper/#projection","title":"Projection :","text":"Conclusion
\\(\\Sigma^{'} = JW\\Sigma W^TJ^T\\) where W is a view transformation and J is the Jacobian of the affine approximation of the projective transformation
Derivation
"},{"location":"Papers/3DGS/paper/#the-viewing-transformation","title":"The viewing transformation:","text":"Denote the Gaussian reconstruction kernels in object space by \\(r_{k}''(\\mathbf{t})=\\mathcal{G}_{\\mathbf{V''}}(\\mathbf{t}-\\mathbf{t_k})\\), where \\(\\mathbf{t_k}\\) are the voxel positions of center of kernel.
Denote camera coordinates by a vector \\(\\mathbf{u}=(u_0,u_1,u_2)^{T}\\). Object coordinates are transformed to camera coordinates using an af\ufb01ne mapping \\(\\mathbf{u}=\\varphi(\\mathbf{t})=\\mathbf{Wt+d}\\), called viewing tranformation.
Now we can transform the reconstruction kernels \\(\\mathcal{G}_{\\mathbf{V''}}(\\mathbf{t}-\\mathbf{t_k})\\) to camera space:
\\(\\(\\mathcal{G}_{\\mathbf{V''}}(\\varphi^{-1}(\\mathbf{u})-\\mathbf{t_k})=\\frac{1}{|\\mathbf{W}^{-1}|}\\mathcal{G}_{\\mathbf{V'}_{k}}(\\mathbf{u}-\\mathbf{u_k})=r_{k}'(\\mathbf{u})\\)\\)
where \\(\\mathbf{u_k}=\\varphi(\\mathbf{t_k})\\) is the center of the Gaussian in camera coordinates and \\(\\mathbf{V'}_{k}=\\mathbf{W}\\mathbf{V''}_{k}\\mathbf{W}^{T}\\) is the variance matrix in camera coordinates.
"},{"location":"Papers/3DGS/paper/#the-projective-transformation","title":"The Projective Transformation","text":"In camera space, The ray intersecting the center of projection and the point \\((x_0, x_1)\\) on the projection plane is called a viewing ray.
To facilitate analiytical integration of volumn function, we need to transform the camera space to ray space such that the viewing rays are parallel to a coordinate axis. The projective transformation converts camera coordinates to ray coordinates.
Camera space is de\ufb01ned such that the origin of the camera coordinate system is at the center of projection and the projection plane is the plane \\(u_{2}=1\\). Camera space and ray space are related by the mapping \\(\\mathbf{x=m(u)}\\).
\\[ \\begin{pmatrix} x_0\\\\ x_1\\\\ x_2\\\\ \\end{pmatrix} =\\mathbf{m(u)}= \\begin{pmatrix} u_{0}/u_{2}\\\\ u_{1}/u_{2}\\\\ \\Vert(u_{0},u_{1},u_{2})^{T}\\Vert\\\\ \\end{pmatrix}\\\\\\\\ ~\\\\\\\\ \\begin{pmatrix} u_0\\\\ u_1\\\\ u_2\\\\ \\end{pmatrix} =\\mathbf{m^{-1}(u)}= \\begin{pmatrix} x_{0}/l\\cdot x_2\\\\ x_{1}/l\\cdot x_2\\\\ 1/l\\cdot x_2\\\\ \\end{pmatrix} \\]where \\(l=\\Vert(x_{0},x_{1},1)^{T}\\Vert\\).
Unfortunately, these mappings are not affine. The Gaussian after the transformation may not still Gaussian. To solve this problem, we introduce the local affine approximation \\(m_{uk}\\) of the projective transformation. It is defined by the first two terms of the Taylor expansion of \\(\\mathbf{m}\\) at the point \\(\\mathbf{u}_k\\):
\\[ \\mathbf{m_{u_k}(u)=x_k+J_{u_k}\\cdot (u-u_k)}\\\\\\\\ ~\\\\ \\mathbf{J_{u_k}}={\\frac{\\partial \\mathbf{m}}{\\partial \\mathbf{u}}}(\\mathbf{u}_k) \\]where \\(\\mathbf{x}_k=\\mathbf{m(u_k)}\\) is the center of a Gaussian in ray space and the Jacobian \\(\\mathbf{J_{u_k}}\\) is given by the partial derivatives of \\(\\mathbf{m}\\) at the point \\(\\mathbf{u}_k\\).
This yields the local affine approximation of reconstruction kernels to ray space:
\\[ \\begin{aligned} r_{k}(\\mathbf{x})&=\\frac{1}{|\\mathbf{W}^{-1}|}\\mathcal{G}_{\\mathbf{V'}_{k}}(\\mathbf{m^{-1}(x)}-\\mathbf{u_k})\\\\ &=\\frac{1}{|\\mathbf{W}^{-1}||\\mathbf{J}^{-1}|}\\mathcal{G}_{\\mathbf{V}_{k}}(\\mathbf{x}-\\mathbf{x_k}) \\end{aligned} \\]where \\(\\mathbf{V}_k\\) is the variance matrix in ray coordinates:
\\[ \\begin{aligned} \\mathbf{V}_{k}&=\\mathbf{J}\\mathbf{V'}_{k}\\mathbf{J}^{T}\\\\ &=\\mathbf{JW}\\mathbf{V''}_{k}\\mathbf{W}^{T}\\mathbf{J}^{T} \\end{aligned} \\]"},{"location":"Papers/3DGS/paper/#rasterizer","title":"Rasterizer","text":"http://www.yindaheng98.top/%E5%9B%BE%E5%BD%A2%E5%AD%A6/%E7%90%83%E8%B0%90%E7%B3%BB%E6%95%B0.html#%E7%90%83%E9%9D%A2%E9%AB%98%E6%96%AF%EF%BC%88spherical-gaussian%EF%BC%89
"},{"location":"Papers/3DGS/paper/#optimization-with-adaptive-density-control-of-3d-gaussians","title":"Optimization With Adaptive Density Control of 3D Gaussians","text":""},{"location":"Papers/3DGS/paper/#gradient-caculation","title":"Gradient Caculation","text":"Reference : grads\u63a8\u5bfc
\u56db\u5143\u6570 : \u56db\u5143\u6570
// Compute loss gradient w.r.t. matrix M\n// dSigma_dM = 2 * M\nglm::mat3 dL_dM = 2.0f * M * dL_dSigma;\n
"},{"location":"Papers/3DGS/paper/#adaptive-control-of-gaussians","title":"Adaptive Control of Gaussians","text":"\u201c\u6b20\u91cd\u6784\u533a\u201d(under-reconstruction)\uff1a\u4f4d\u7f6e\u68af\u5ea6\u5f88\u5927\u4f46\u503c\u5f88\u5c0f\u7684Gaussian\u70b9 -- \u7f3a\u5c11Gaussian\u70b9\u7684\u533a\u57df \u590d\u5236\u8fd9\u4e2aGaussian\u70b9\uff0c\u518d\u6cbf\u4f4d\u7f6e\u68af\u5ea6\u8fdb\u884c\u79fb\u52a8
\u201c\u8fc7\u91cd\u6784\u533a\u201d(over-reconstruction)\uff1a\u4f4d\u7f6e\u68af\u5ea6\u5f88\u5927\u4e14\u503c\u5f88\u5927\u7684Gaussian\u70b9 -- \u6bcf\u4e2aGaussian\u70b9\u8986\u76d6\u5f88\u5927\u8303\u56f4\u7684\u533a\u57df
\u51cf\u5c0f\u8fd9\u4e2aGaussian\u70b9\u7684\u503c\uff0c\u518d\u590d\u5236\u8fd9\u4e2aGaussian\u70b9\uff0c\u518d\u6cbf\u4f4d\u7f6e\u68af\u5ea6\u8fdb\u884c\u79fb\u52a8
"},{"location":"Papers/3DGS/paper/#fast-differentiable-rasterization-for-gaussians","title":"Fast Differentiable Rasterization For Gaussians","text":""},{"location":"Papers/3DGS/paper/#pipeline","title":"Pipeline","text":"Only keep the splats that have 99% confidence of intersecting the view frustum.
\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2asplat\u7684\u4e2d\u5fc3\u4f4d\u4e8e\u89c6\u9525\u4f53\u4e4b\u5916\uff0c\u6211\u4eec\u5c31\u4e0d\u4f1a\u8003\u8651\u5b83\u3002
\u5047\u8bbe\u4e00\u4e2asplat\u4e0e5\u4e2a\u74e6\u7247\u76f8\u4ea4\uff0c\u6211\u4eec\u4e3a\u8fd9\u4e2asplat\u521b\u5efa5\u4e2a\u5b9e\u4f8b
\u6bcf\u4e2a\u5b9e\u4f8b\u6839\u636e\u5176\u5728\u89c6\u7a7a\u95f4\u4e2d\u7684\u6df1\u5ea6\u548c\u74e6\u7247ID\u83b7\u5f97\u4e00\u4e2a\u552f\u4e00\u7684\u952e
\u4f7f\u7528GPU Radix\u6392\u5e8f\u7b97\u6cd5\uff0c\u6211\u4eec\u6839\u636e\u8fd9\u4e9b\u952e\u5bf9\u6240\u6709\u5b9e\u4f8b\u8fdb\u884c\u6392\u5e8f 5. Rasterization
\u5bf9\u4e8e\u6bcf\u4e2a\u74e6\u7247\uff0c\u6211\u4eec\u786e\u5b9a\u6392\u5e8f\u540e\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5f71\u54cd\u8be5\u74e6\u7247\u7684splat\u5b9e\u4f8b
\u5047\u8bbe\u74e6\u7247(5,5)\u9996\u5148\u52a0\u8f7d\u4e86\u5f71\u54cd\u5b83\u7684splat\u5b9e\u4f8b\u5217\u8868\u5230\u5171\u4eab\u5185\u5b58
\u5bf9\u4e8e\u74e6\u7247(5,5)\u4e2d\u7684\u50cf\u7d20(5,5)\uff0c\u6211\u4eec\u4ece\u5217\u8868\u7684\u5f00\u59cb\u5230\u7ed3\u675f\u904d\u5386splat\u5b9e\u4f8b\uff0c\u7d2f\u79ef\u989c\u8272\u548c\u03b1\u503c
\u5f53\u50cf\u7d20(5,5)\u7684\u03b1\u503c\u8fbe\u52301\u65f6\uff0c\u6211\u4eec\u77e5\u9053\u5b83\u5df2\u7ecf\u9971\u548c\uff0c\u4e0d\u518d\u9700\u8981\u66f4\u591a\u7684splat\u5b9e\u4f8b\u6765\u5f71\u54cd\u5b83\u7684\u989c\u8272
Compared to the state-of-the-art NeRF, 3DGS achieves much faster rendering speed with comparable visual quality (But the memory consumption is higher).
Details see the paper
"},{"location":"Papers/4DGS/paper/","title":"2023.10-ICLR","text":""},{"location":"Papers/4DGS/paper/#real-time-photorrealistic-dynamic-scene-representation-and-rendering-with-4d-gaussian-splatting","title":"Real-Time Photorrealistic Dynamic Scene Representation And Rendering With 4D Gaussian Splatting","text":""},{"location":"Papers/4DGS/paper/#problem-formulation-and-4d-gaussian-splatting","title":"Problem Formulation and 4D Gaussian Splatting","text":""},{"location":"Papers/4DGS/paper/#problem-formulation","title":"Problem Formulation","text":"\\[ \\mathbb{I}(\\mu,v,t)=\\sum_{i=1}^{N} p_i(\\mu,v,t) \\alpha_ic_i(d)\\prod_{j=1}^{i-1} (1-p_j(\\mu, v,t)\\alpha_j) \\]
Helper Proof see Appendix
"},{"location":"Papers/4DGS/paper/#representaion-of-4d-gaussian","title":"Representaion of 4D Gaussian","text":""},{"location":"Papers/4DGS/paper/#4d-gaussian","title":"4D Gaussian","text":"4D Gaussian : In the same way as in 3D Gaussian, we can define decomposition of 4D Gaussian as \\(\\Sigma = R\\Lambda R^T\\) where \\(\\Lambda\\) is a diagonal matrix and \\(U\\) is an orthogonal matrix. Also if we use \\(S = (\\Lambda)^{1/2}\\), then \\(\\Sigma = RSS^TR\\)
S is a 4x4 scaling matrix (diagonal matrix) so it can be represented as \\(S = diag(s_x, s_y, s_z, s_t)\\)
R is a 4x4 rotation matrix so it can be decomposed into 2 isotropic 2 rotations, each of which represented by a quaternion. So \\(R = L(q_l)R(q_r)\\) where \\(q_l\\) and \\(q_r\\) are left and right quaternions respectively.
\\(q_l = (a, b, c, d)\\) and \\(q_r = (p, q, r, s)\\)
\\[ R(q_l)= \\begin{pmatrix} a&-b&-c&-d\\\\ b&a&-d&c\\\\ c&d&a&-b\\\\ d&-c&b&a \\end{pmatrix} \\] \\[ R(q_r)= \\begin{pmatrix} p&-q&-r&-s\\\\ q&p&s&-r\\\\ r&-s&p&q\\\\ s&r&-q&p \\end{pmatrix} \\]the mean of the Gaussian is represented by a 4D vector \\(\\mu = (\\mu_x, \\mu_y, \\mu_z, \\mu_t)\\)
"},{"location":"Papers/4DGS/paper/#derivation-of-the-conditional-3d-gaussian-and-marginal-1d-gaussian","title":"Derivation of the conditional 3D Gaussian and marginal 1D Gaussian","text":""},{"location":"Papers/4DGS/paper/#conditional-3d-gaussian","title":"Conditional 3D Gaussian","text":"The conditional 3D Gaussian is the Gaussian distribution of the first 3 dimensions given the 4th dimension. It can be derived by the following formula:
\\[ \\begin{aligned} \\Sigma_{3|1} &= \\Sigma_{1,2,3} - \\Sigma_{1,2,4}\\Sigma_{4}^{-1}\\Sigma_{1,2,4}^T\\\\ \\mu_{3|1} &= \\mu_{1,2,3} - \\Sigma_{1,2,3}\\Sigma_{4}^{-1}(r - \\mu_{4}) \\end{aligned} \\] \\[ \\Sigma = \\begin{pmatrix} \\Sigma_{1,2,3} & \\Sigma_{1,2,4} \\\\ \\Sigma_{1,2,4}^T & \\Sigma_4 \\end{pmatrix} \\]"},{"location":"Papers/4DGS/paper/#marginal-1d-gaussian","title":"Marginal 1D Gaussian","text":"The marginal 1D Gaussian is the Gaussian distribution of the 4th dimension. It can be derived by the following formula:
\\[ \\begin{aligned} \\Sigma_{4} &= \\Sigma_{4}\\\\ \\mu_{4} &= \\mu_{4} \\end{aligned} \\]"},{"location":"Papers/4DGS/paper/#4d-spherindrical-harmonics","title":"4D spherindrical harmonics","text":""},{"location":"Papers/4DGS/paper/#spherindrical-harmonics","title":"spherindrical harmonics","text":"\\[ \\begin{aligned} Y_{l}^m(\\theta, \\phi) &= \\sqrt{\\frac{2l+1}{4\\pi}\\frac{(l-|m|)!}{(l+|m|)!}}P_l^m(\\cos(\\theta))e^{im\\phi}\\\\ P_l^m(x) &= (1-x^2)^{|m|/2}\\frac{d^{|m|}}{dx^{|m|}}P_l(x)\\\\ P_l(x) &= \\frac{1}{2^ll!}\\frac{d^l}{dx^l}(x^2-1)^l \\end{aligned} \\]\\(l\\): the degree (non-negative integer)
\\(m\\): the order (integer such that \\(\u2212l\\leq m\\leq l\\))
\\(c_{l}^{m}\\): SH coefficients
"},{"location":"Papers/DM/DDIM/#ddim-denoising-diffusion-implicit-models","title":"DDIM: Denoising Diffusion Implicit Models","text":""},{"location":"Papers/DM/DDIM/#goal","title":"Goal","text":"
"},{"location":"Papers/DM/DDPM/#ddpm-denoising-diffusion-probabilistic-models","title":"DDPM: Denoising Diffusion Probabilistic Models","text":""},{"location":"Papers/DM/DDPM/#mathematical-formulation","title":"Mathematical Formulation","text":""},{"location":"Papers/DM/DDPM/#forward-diffusion-process","title":"Forward Diffusion Process","text":"
The distribution \\(q\\) in the forward diffusion process is defined as Markov Chain given by $$ q(x_1,...x_T|x_0) = \\Pi_{t=1}^T q(x_t|x_{t-1}) (1) $$ $$ q(x_t|x_{t-1}) = \\mathcal{N}(x_t;\\sqrt{1-\\beta_t}x_{t-1},\\beta_t I) (2) $$
From (2) we can sample \\(x_t\\) from \\(q(x_t|x_{t-1})\\) by $$ x_t = \\sqrt{1-\\beta_t}x_{t-1} + \\sqrt{\\beta_t}z_t (3) $$ where \\(z_t \\sim \\mathcal{N}(0,I)\\).
We want to sample at any time \\(t\\) from the distribution \\(q(x_t|x_0)\\) without knowing the previous samples \\(x_{t-1},...,x_1\\).
$$\\alpha_t = 1-\\beta_t (4)\\ $$ \\(\\(\\bar{\\alpha}_t = \\Pi_{i=1}^t \\alpha_i \\ \\ (5)\\\\\\)\\) $$ q(x_t|x_0) = \\mathcal{N}(x_t;\\sqrt{\\bar{\\alpha}_t}x_0,(1-\\bar{\\alpha}_t) I) (6)$$
The reverse process is a Markov chain where a neural network predicts the parameters for the reverse diffusion kernel at each timestep.
maximum likelihood estimation (MLE) is used to train the neural network.
Maximize the lower bound of the log-likelihood of the data given the model.
After some math -- our goal is to minimize the noise difference between the predicted noise and the true noise. \\(\\(||(\\epsilon_{\\theta}-\\epsilon)^2_2||\\)\\)
what are diffusion models
An In-Depth Guide to Denoising Diffusion Probabilistic Models DDPM \u2013 Theory to Implementation
Youtube: Denoising Diffusion Probabilistic Models | DDPM Explained
"},{"location":"Papers/DM/SD/#high-resolution-image-synthesis-with-latent-diffusion-models","title":"High-Resolution Image Synthesis with Latent Diffusion Models","text":""},{"location":"Papers/DM/SD/#architecture","title":"Architecture","text":""},{"location":"Papers/DM/SD/#perceptual-image-compression","title":"Perceptual Image Compression","text":"
"},{"location":"Papers/NeRF/Theory/#nerf-representing-scenes-as-neural-radiance-fields-for-view-synthesis","title":"NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis","text":""},{"location":"Papers/NeRF/Theory/#introduction","title":"Introduction","text":"
Overfit a single neural network for a particular scene to train an overfitted nerual network which represents the scene.
Input : \\((x,y,z,\\theta,\\phi)\\): \\((x,y,z)\\) is the space location and \\((\\theta,\\phi)\\) is the viewing direction
Output : (colour, density)
Therefore, when the network is well-overfitted, it can give you results depending on the given viewing directions -- Catch Transparency and Colour.
"},{"location":"Papers/NeRF/Theory/#overview","title":"Overview","text":"a. Sample 5D coordinates along camera rays.
b. Feed those locations into an MLP to produce a color and volume density.
\\(F_{\\Theta}\\) :
Process the input 3D coordinate with 8 fully-connected layers (with ReLU activations and 256 channels per layer)
Output \\(\\sigma\\) and a 256-dimensional feature vector.
Concatenate the feature vector with the camera ray's viewing direction and passed to one additional fully-connected layer (using ReLU activation and 128 channels.)
From above we can see that, NeRF encourages the representation to be multivies consistent by restricting the network to predict the volume density \\(\\sigma\\) as a function of ONLY the location, while we allow the RGB colour to be predicted as a fucntion of both location and viewing direction.
c. Use volume rendering techniques to composite these values into an image
d. The rendering function is differentiable, we optimize the scene representation by minimizing the residual between synthesized and ground truth observed images.
The weight is the scene
"},{"location":"Papers/NeRF/Theory/#detailed-parts","title":"Detailed Parts","text":""},{"location":"Papers/NeRF/Theory/#neural-radiance-field-scene-representation","title":"Neural Radiance Field Scene Representation","text":"Volume density \\(\\sigma(x)\\) : differential probability of a ray terminating at an infinitesimal particle at location x
\\(C(r)\\) : The expected color of ray \\(r(t) = o+td\\) with near and far bound \\(t_n\\) and \\(t_f\\)\u200b
\\(T(t)\\) : accumulated transmittance along the ray from \\(t_n\\) to \\(t\\) ,i.e., the probability that the ray travels from \\(t_n\\) to \\(t\\) without hitting any other particle. And therefore \\(T(t)=exp(-\\int_{t_n}^{t}\\sigma(r(s))ds)\\)\u200b
Thus , \\(C(r) = \\int_{t_n}^{t_f} T(t) \\sigma(r(t))c(r(t),d)dt\\)\u200b : Clearly color depends on both position and direction.
Numerically estimate this continuous integral using quadrature and finally we get:
"},{"location":"Papers/NeRF/Theory/#optimizing-a-neural-radiance-field","title":"Optimizing a Neural Radiance Field","text":""},{"location":"Papers/NeRF/Theory/#positional-encoding","title":"Positional Encoding","text":"Better Performace at representing high-frequency variation
Reform \\(F_{\\theta}\\) as a composition of two functions \\(F_{\\theta}=F_{\\theta}'*\\gamma\\) one learned and one not.
Implement two networks: Use the result of the coarse network to determine where to sample in the fine network.
To do this, we rewrite the alpha composited color from thecoarse network \\(\\hat{C}_c(r)\\) as a weighted sum of all sampled colors \\(c_i\\)\u200b along the ray.
Normalize the wights as \\(\\hat{w}_i=\\frac{w_i}{\\sum_{j=1}^{N_c}w_j}\\) , we sample a second set of \\(N_f\\) locations from the distribution using inverse transform sampling (https://en.wikipedia.org/wiki/Inverse_transform_sampling)
Compute the final result using all \\(N_c + N_f\\)\u200b samples(Training Time), at test time use ONLY fine network.
"},{"location":"Papers/NeRF/Theory/#implementation-details","title":"Implementation Details","text":""},{"location":"assembly/%E4%B8%AD%E6%96%AD/","title":"\u4e2d\u65ad","text":""},{"location":"assembly/%E4%B8%AD%E6%96%AD/#int16","title":"int16","text":"int 16h ax=\u6240\u6572\u952e\u7684\u7f16\u7801
again: [\u5237\u65b0\u6e38\u620f\u7684\u753b\u9762]\nmov ah,1\nint 16h \u68c0\u67e5\u952e\u76d8\u7f13\u51b2\u533a\u662f\u5426\u4e3a\u7a7a\uff0c\u82e5\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56dezf=1,\u5426\u5219\u8fd4\u56dezf=0 (ZF=1 jz\u8df3\u8f6c)\njz no_key\nhas_key:\n mov ah,0\n int 16h \u4ece\u952e\u76d8\u7684\u7f13\u51b2\u533a\u8bfb\u53d6\u4e00\u4e2a\u952e\uff0c\u6839\u636e\u6240\u6572\u7684\u952e\u6267\u884c\u4e0d\u540c\u5206\u652f\nno_key:\n jmp again\n
\u4f8b\u5b50\uff1a
data segment\nx dw 0\ny dw 0\ndata ends\ncode segment\nassume cs:code,ds:data\nmain:\n mov ax,data\n mov ds,ax\n mov ah,0\n mov al,3\n mov ax,0B800h\n mov es,ax\ndraw:\n ;bx=(y*80+x)*2\n mov ax,[y]\n mov bp,80\n mul bp \n; dx(\u9ad816\u4f4d):ax\uff08\u4f4e16\u4f4d\uff09=ax*bp \n; \u5176\u4e2ddx=0 mul\u7684\u4e58\u6570\u5bbd\u5ea616 \u5219\u88ab\u4e58\u6570\u4e3aax\n; 24x79\u603b\u4e4b\u4e0d\u8d85\u8fc716\u4f4d \n add ax,[x]\n add ax,ax\n mov bx,ax\n mov es:[bx],1720h\n check_key:\n mov ah,1\n int 16h\n jnz has_key\n jmp check_key\n has_key:\n mov ah,0\n int 16h\n cmp ax,4800h\n je is_up\n cmp ax,5000h\n je is_down\n cmp ax,4B00h\n je is_left\n cmp ax,4D00h\n je is_right\n jmp exit\nis_up:\n cmp [y],0\n je check_key\n dec [y]\n jmp draw\nis_down:\n cmp [y],24\n je check_key\n inc [y]\n jmp draw\nis_left:\n cmp [x],0\n je check_key\n dec [x]\n jmp draw\nis_right:\n cmp [x],79\n je check_key\n inc [x]\n jmp draw\nexit:\n mov ah,4Ch\n int 21h\ncode ends\nend main\n
"},{"location":"assembly/%E4%B8%AD%E6%96%AD/#int-21h","title":"int 21h","text":"0:84 78h\n0:85 56h\n0:86 34h\n0:87 12h\nxor ax,ax\nmov es,ax\nmov bx,84h\nlds si,es:[bx]\n;ds=1234h si=5678h\n
\u663e\u7136, int 00h\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[0];\nint 01h\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[4];\nint 03h\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[0Ch];\nint 08h\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[20h];\nint 0FFh\u7684\u4e2d\u65ad\u5411\u91cf\u4fdd\u5b58\u5728dword ptr 0:[3FCh];\n
* BIOS\u4f1a\u5b8c\u6210\u90e8\u5206\u4e2d\u65ad\u5411\u91cf\u7684\u586b\u5199, \u5982int 10h\u3001int 16h\u3001int 13h\u8fd9\u51e0\u4e2aBIOS\u4e2d\u65ad\u7684\u5411\u91cf\u5728DOS\u542f\u52a8\u524d\u5c31\u5df2\u7ecf\u586b\u597d\u4e86 * DOS\u542f\u52a8\u5b8c\u6210\u540e\uff0c\u4f1a\u586b\u5165int 21h\u7684\u4e2d\u65ad\u5411\u91cf\u3002 1000:2000 mov ah, 2\n1000:2002 mov dl, 'A'\n1000:2004 int 21h\n1000:2006 mov ah,4Ch\n
* \u4e0a\u9762\u8fd9\u6761int 21h\u6307\u4ee4\u6267\u884c\u65f6, cpu\u505a\u4e86\u4ee5\u4e0b4\u4ef6\u4e8b: IF=0\nTF=0 -\u9632\u6b62\u4e2d\u65ad\u91cc\u518d\u6b21\u4ea7\u751f\u4e2d\u65ad\npushf\npush cs\u53731000h\npush \u4e0b\u6761\u6307\u4ee4\u7684\u504f\u79fb\u5730\u5740\u53732006h\njmp dword ptr 0:[84h]; jmp 1234h:5678h\n\n\u4e0a\u9762\u8fd9\u6761jmp\u4f1a\u8df3\u8f6c\u5230int 21h\u7684\u4e2d\u65ad\u670d\u52a1\u51fd\u6570\u5185\u90e8:\n1234:5678 ...\n ...\n1234:56FF iret; \u4e2d\u65ad\u8fd4\u56de\niret\u5728\u6267\u884c\u65f6, cpu\u505a\u4e86\u4ee5\u4e0b3\u4ef6\u4e8b\u60c5:\npop ip\u5373ip=2006h\npop cs\u5373cs=1000h\npopf\n\n1000:2000 call 1234:5678; \u8fdc\u8c03\u7528\n;\u6b64\u65f6\u4f1apush 1000h \u518d push 2005h\n;\u6700\u540ejmp 1234:5678\n1000:2005 mov ah, 4Ch\n1000:2007 int 21h\n...\n1234:5678 ...\n retf; \u6b64\u65f6\u6267\u884cpop ip\u518dpop cs\n
\u4e2d\u65ad\u7a0b\u5e8f\u4f8b\u5b50
code segment\nassume cs:code\nold_80h dw 0, 0\nmain:\n xor ax, ax\n mov es, ax\n mov bx, 80h*4; mov bx, 200h\n mov ax, es:[bx]\n mov old_80h[0], ax\n mov ax, es:[bx+2]\n mov old_80h[2], ax\n\n mov word ptr es:[bx], offset int_80h\n mov es:[bx+2], cs\n\n mov ah, 1\n int 80h; AL=\u952e\u76d8\u8f93\u5165\u7684ASCII\u7801\nnext:\n mov ah, 2\n mov dl, al\n int 80h\n\n mov ax, old_80h[0]\n mov es:[bx], ax\n mov ax, old_80h[2]\n mov es:[bx+2], ax\n\n mov ah, 4Ch\n int 21h\nint_80h: ; ISR(Interrupt Service Routine)\n ; \u4e2d\u65ad\u670d\u52a1\u51fd\u6570\n cmp ah, 1\n je is_1\nis_2:\n push es\n push bx\n push ax\n mov bx, 0B800h\n mov es, bx\n mov byte ptr es:[160], dl\n mov byte ptr es:[161], 17h\n pop ax\n pop bx\n pop es\n jmp done\nis_1:\n int 21h\ndone:\n iret\n ;\u5982\u679c\u60f3\u63a5\u8fde\u6267\u884cold int80\uff1ajmp dword ptr cs:[old_80h]\n\ncode ends\nend main\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/","title":"\u51fd\u6570","text":""},{"location":"assembly/%E5%87%BD%E6%95%B0/#_1","title":"\u6c47\u7f16\u8bed\u8a00\u4e2d\u7684\u4e09\u79cd\u53c2\u6570\u4f20\u9012\u65b9\u5f0f","text":""},{"location":"assembly/%E5%87%BD%E6%95%B0/#_2","title":"\u5bc4\u5b58\u5668\u4f20\u9012","text":"f:\n add ax, ax; ax=2*ax\n ret ; \u8fd4\u56de\u65f6ax\u5c31\u662f\u51fd\u6570\u503c\nmain:\n mov ax, 3; ax\u5c31\u662ff()\u7684\u53c2\u6570\n call f\nnext:\n mov ah, 4Ch\n int 21h\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/#_3","title":"\u53d8\u91cf\u4f20\u9012","text":"f:\n mov ax, var\n add ax, ax; ax\u5c31\u662f\u51fd\u6570\u503c\n ret\nmain:\n mov var, 3\n ; var\u662f\u4e00\u4e2a16\u4f4d\u7684\u53d8\u91cf, \u7528\u4f5c\u53c2\u6570\uff0cvar\u5fc5\u987b\u662f\u5168\u5c40\u53d8\u91cf\n call f\n
* \u5728\u6c47\u7f16\u8bed\u8a00\u4e2d\uff0c\u7528\u00a0db\u3001dw\u7b49\u5173\u952e\u8bcd\u5b9a\u4e49\u7684\u53d8\u91cf\u5747\u4e3a\u5168\u5c40\u53d8\u91cf\u00a0\u3002\u5728\u5806\u6808\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u624d\u662f\u5c40\u90e8\u53d8\u91cf\u3002 * var\u4e0d\u80fd\u7528\u4e8e\u9012\u5f52\u51fd\u6570\u5185--\u6bcf\u4e00\u5c42\u53d8\u91cf\u5730\u5740\u5404\u4e0d\u76f8\u540c\uff0c\u5168\u5c40\u53d8\u91cf\u7684\u8bdd\u6c38\u8fdc\u662f\u540c\u4e00\u4e2a\u5730\u5740\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528\u6765\u4f20\u9012\u9012\u5f52\u51fd\u6570\u7684\u53c2\u6570"},{"location":"assembly/%E5%87%BD%E6%95%B0/#_4","title":"\u5806\u6808\u4f20\u9012","text":"f:\n push bp\n mov bp, sp\n mov ax, [bp+4]; \u4ece\u5806\u6808\u4e2d\u53d6\u5f97\u53c2\u6570\n add ax, ax\n pop bp\n ret\nmain:\n mov ax, 3\n push ax; \u53c2\u6570\u538b\u5165\u5230\u5806\u6808\n call f\n add sp, 2\n
* C\u8bed\u8a00\u51fd\u6570\u8c03\u7528y=f(2,3)\u6c42\u4e24\u6570\u4e4b\u548c\u8f6c\u5316\u6210\u6c47\u7f16\u8bed\u8a00 * \u8bbess=1000h sp=2000h f:\n push bp;(4)\n mov bp, sp\n mov ax, [bp+4]\n add ax, [bp+6]\n pop bp; (5)\n ret; (6) pop ip\nmain:\n mov ax, 3\n push ax; (1)\n mov ax, 2\n push ax; (2)\n call f; (3) \n ;call\u4f1a\u628af\u6307\u4ee4\u7684\u4e0b\u4e00\u6761\u7684\u5730\u5740\uff08here\uff09\u538b\u5165\u5806\u6808 \u7136\u540e\u518djmpf\nhere:\n add sp, 4;(7);\u5806\u6808\u6062\u590d\u5e73\u8861\n
* \u4e0a\u8ff0\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5806\u6808\u5e03\u5c40\u5982\u4e0b:stack frame ss:1FF8 old bp <- bp (4)\nss:1FFA here <- (3)(5)\nss:1FFC 02 <- (2)(6)\nss:1FFE 03 <- (1)\nss:2000 ?? <- (7)\n
* printf\u51fd\u6570\u539f\u578b #include <stdio.h>\ndouble f(char *s, ...)\n{ double y=0;\n char *p = (char *)&s; /* p = bp+4 */\n p += sizeof(s); /* p = bp+6 */\n while(*s != '\\0')\n {\n if(*s == 'i')\n {\n y += *(int *)p;\n p += sizeof(int);\n }\n else if(*s == 'l')\n {\n y += *(long*)p;\n p += sizeof(long);\n }\n else if(*s == 'd')\n {\n y += *(double *)p;\n p += sizeof(double);\n }\n s++;\n }\n return y;\n}\nmain()\n{\n double y;\n y = f(\"ild\", 10, 20L, 3.14);\n printf(\"y=%lf\\n\", y);\n}\n
* call far ptr dest retf 1000:2000 call 1234:5678 ;\u8de8\u6bb5\u8df3\u8fdc\ncpu\u4f1a\u505a\u4e00\u4e0b\u52a8\u4f5c\uff1a\n[1]push cs\n[2]push 2005h\n[3]jmp 1234:5678h\n;\u4e00\u5b9a\u5148\u538b\u5165\u6bb5\u5730\u5740\uff0c\u518d\u538b\u5165\u504f\u79fb\u5730\u5740 WHY?5.18\u6700\u540e\u4e00\u6bb5\n1000:2005 mov ah,4Ch\n1234:5678 ...\n1234:5688 retf\ncpu \u5728retf\u65f6\u4f1a\u505a\u4ee5\u4e0b\u52a8\u4f5c\n[1]pop ip;\n[2]pop cs;\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/#3","title":"\u7528\u5806\u6808\u4f20\u9012\u53c2\u6570\u67093\u79cd\u65b9\u5f0f:","text":""},{"location":"assembly/%E5%87%BD%E6%95%B0/#__cdecl","title":"__cdecl","text":"__cdecl\u7684\u4f8b\u5b50:
f:\npush bp; (4)\nmov bp, sp\nmov ax, [bp+4]; arg0\nadd ax, [bp+6]; arg1\npop bp; (5)\nret; (6)\nmain:\nmov ax, 20; arg1\npush ax; (1)\nmov ax, 10; arg0\npush ax; (2)\ncall f; (3)\nhere:\nadd sp, 4; (7)\n
ss:1FF8 old bp -> bp (4)\nss:1FFA here -> (3)(5)\nss:1FFC 10 -> (2)(6)\nss:1FFE 20 -> (1)\nss:2000 -> (7)\n
__pascal\u7684\u4f8b\u5b50\uff1a
f:\npush bp; (4)\nmov bp, sp\nmov ax, [bp+6]; arg0\nadd ax, [bp+4]; arg1\npop bp; (5)\nret 4; (6)\nmain: \nmov ax, 10\npush ax; (1) arg0\nmov ax, 20\npush ax; (2) arg1\ncall f; (3)\nhere: \n
ss:1FF8 old bp<- bp (4)\nss:1FFA here <- (3)(5)\nss:1FFC 20 <- (2)\nss:1FFE 10 <- (1)\nss:2000 <-(6)\n
ret 4: [1]pop ip [2]sp=sp+4
__stdcall\u7684\u4f8b\u5b50\uff1a
f:\npush bp; (4)\nmov bp, sp\nmov ax, [bp+4]; arg0\nadd ax, [bp+6]; arg1\npop bp; (5)\nret 4; (6)\nmain:\nmov ax, 20\npush ax; (1) arg1\nmov ax, 10\npush ax; (2) arg0\ncall f; (3)\nhere: \n
__stdcall\u7684\u5806\u6808\u5e03\u5c40:
ss:1FF8 old bp<- bp (4)\nss:1FFA here <- (3)(5)\nss:1FFC 10 <- (2)\nss:1FFE 20 <- (1)\nss:2000 <-(6)\n
* 32\u4f4d__stdcall\u7684\u4f8b\u5b50: f:\npush ebp;(4)\nmov ebp, esp\nmov eax,[ebp+8];arg0\nadd eax,[ebp+0Ch];arg1\npop ebp;(5)\nret 8;(6)\nmain:\npush 20;(1) arg1\npush 10;(2) arg0\ncall f;(3)\nhere:\n
* 32\u4f4d__stdcall\u7684\u5806\u6808\u5e03\u5c40: ss:1FF0 old ebp<- ebp (4)\nss:1FF4 here <- (3)(5)\nss:1FF8 10 <- (2)\nss:1FFC 20 <- (1)\nss:2000 <-(6)\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/#_5","title":"\u52a8\u6001\u53d8\u91cf","text":"int f(int a, int b)\n{\n int c; /* c\u662f\u5c40\u90e8\u52a8\u6001\u53d8\u91cf */\n c = a+b;\n return c;\n}\n
\u4e0a\u8ff0C\u8bed\u8a00\u51fd\u6570\u53ef\u7ffb\u8bd1\u6210\u4ee5\u4e0b\u6c47\u7f16\u4ee3\u7801:
f:\npush bp; (4)\nmov bp, sp\nsub sp, 2; (5) \u8fd9\u91cc\u6316\u7684\u5751\u5c31\u662f\u7ed9\u53d8\u91cfc\u7684\nmov ax, [bp+4]\nadd ax, [bp+6]\nmov [bp-2], ax\nmov ax, [bp-2]\nmov sp, bp; (6)\u6b64\u65f6\u53d8\u91cfc\u6b7b\u4ea1\npop bp; (7)\nret; (8)\nmain:\nmov ax, 20\npush ax; (1)\nmov ax, 10\npush ax; (2)\ncall f; (3)\nhere:\nadd sp, 4;(9)\u6b64\u65f6\u53c2\u6570a,b\u6b7b\u4ea1\n
\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6, \u5806\u6808\u5e03\u5c40\u5982\u4e0b: ss:1FF6 [30] (5) \u53d8\u91cfc\nss:1FF8 old bp<- bp(4)(6)\nss:1FFA here <- (3)(7)\nss:1FFC 10 <- (2)(8)\nss:1FFE 20 <- (1)\nss:2000 <-(9)\n
"},{"location":"assembly/%E5%87%BD%E6%95%B0/#cbp-bx-si-di","title":"C\u8bed\u8a00\u51fd\u6570\u4e2d\u9700\u8981\u4fdd\u62a4bp, bx, si, di","text":"f:\npush bp\nmov bp, sp\nsub sp, n; \u5176\u4e2dn\u4e00\u4e2a\u5e38\u6570,\u7528\u6765\u4e3a\u52a8\u6001\u53d8\u91cf\u5206\u914d\u7a7a\u95f4\n \u5148\u6316\u5751\u518dpush\npush bx\npush si\npush di\n...\npop di\npop si\npop bx\nmov sp, bp\npop bp\nret\n
int f(int n)\n{\n if(n==1)\n return 1;\n return n+f(n-1);\n}\n
\u4e0a\u8ff0C\u8bed\u8a00\u9012\u5f52\u51fd\u6570\u53ef\u7ffb\u8bd1\u6210\u4ee5\u4e0b\u6c47\u7f16\u4ee3\u7801:
f:\n push bp;(3)(6)(9)\n mov bp, sp\n mov ax, [bp+4]\n cmp ax, 1\n je done\n dec ax\n push ax;(4)(7)\n call f;(5)(8)\nthere:\n add sp, 2;(12)(15)\n add ax, [bp+4]\ndone:\n pop bp;(10)(13)(16)\n ret;(11)(14)(17)\nmain:\n mov ax, 3\n push ax;(1)\n call f;(2)\n here:\n add sp, 2;(18)\n
\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\u7684\u5806\u6808\u5e03\u5c40\u5982\u4e0b: ss:1FEE oldbp<-bp(9)\nss:1FF0 there<-(8)(10)\nss:1FF2 1<-(7)(11)\nss:1FF4 oldbp<-bp(6)12\nss:1FF6 there<-(5)(13)\nss:1FF8 2<-(4)(14)\nss:1FFA oldbp<-bp(3)(15)\nss:1FFC here <-(2)(16)\nss:1FFE 3 <-(1)(17)\nss:2000 <-(18)\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/","title":"\u57fa\u7840\u6307\u4ee4","text":""},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#_1","title":"\u903b\u8f91\u8fd0\u7b97\u548c\u79fb\u4f4d\u6307\u4ee4","text":""},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#andorxornottest","title":"\u903b\u8f91\u8fd0\u7b97\u6307\u4ee4\uff1aAND\uff0cOR\uff0cXOR\uff0cNOT\uff0cTEST","text":"mov ax, 9234h\ntest ax, 8000h; ZF=0, AX=9234h\njnz msb_is_one; most significant bit\u6700\u9ad8\u4f4d\n;test\u548cand\u7684\u5173\u7cfb\u76f8\u5f53\u4e8ecmp\u548csub\u7684\u5173\u7cfb\u3002\n
test cl, cl\nor cl, cl\nand cl, cl\nor cl, 0\ncmp cl, 0\n
mov ah, 1011 0110B\nrol ah, 1; AH=0110 1101, CF=1\n;sal: shift arithmetic left \u7b97\u672f\u5de6\u79fb\nmov ah,11111100B;AH=-4\nsar ah,2;AH=11111111 AH=-2\n
mov ah,0B6h\nclc ;CF=0\nrcl ah,1 ;CF=\u539f\u6765\u6700\u9ad8\u4f4d\u7684\u503c ah\u6700\u5730\u4f4d\u4f1a\u53d8\u6210\u539fCF\u7684\u503c\n ;ah=01101100 CF=1\n\nmov ah, 0B6h\nstc ; CF=1\nrcl ah, 1 ; CF=1 AH=1011 0110 \u79fb\u4f4d\u524d\n ; CF=1 AH=0110 1101 \u79fb\u4f4d\u540e\n\nmov ah, 0B6h\nstc ; CF=1\nrcr ah, 1 ; AH=1011 0110 CF=1\u79fb\u4f4d\u524d\n ; AH=1101 1011 CF=0\u79fb\u4f4d\u540e\n\nmov ah,0B6h\nstc\nrcl ah, 1; CF=1 AH=0110 1101\n ;dx ax\n
\u5047\u5b9a\u8981\u628aAX\u4e2d\u768416\u4f4d\u503c\u8f6c\u5316\u6210\u4e8c\u8fdb\u5236\u8f93\u51fa: * \u89e3\u6cd51:
mov cx, 16\nnext:\n shl ax, 1\n jc is_1\n\nis_0:\n mov dl, '0'\n jmp output\n\nis_1:\n mov dl, '1'\n\noutput:\npush ax\nmov ah, 2\nint 21h\npop ax\ndec cx\njnz next\n
* \u89e3\u6cd52: mov cx, 16\nnext:\nshl ax, 1\nmov dl, '0'\nadc dl, 0\noutput:\npush ax\nmov ah, 2\nint 21h\npop ax\ndec cx\njnz next\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#_3","title":"\u5b57\u7b26\u4e32\u64cd\u4f5c\u6307\u4ee4","text":""},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#movsbmovswmovsd","title":"\u5b57\u7b26\u4e32\u4f20\u9001\u6307\u4ee4\uff1aMOVSB\uff0cMOVSW\uff0cMOVSD","text":"rep movsb : \u5176\u4e2drep\u8868\u793arepeat\uff0cs\u8868\u793astring\uff0cb\u8868\u793abyte
\u5728\u6267\u884c\u6b64\u6307\u4ee4\u524d\u8981\u505a\u4ee5\u4e0b\u51c6\u5907\u5de5\u4f5c\uff1a
rep movsb\u6240\u505a\u7684\u64cd\u4f5c\u5982\u4e0b:
again:\nif(cx == 0)\n goto done;\nbyte ptr es:[di] = byte ptr ds:[si]\nif(df==0)\n{si++; di++;}\nelse\n{si--; di--;}\ncx--\ngoto again\ndone:\n
\u4f8b\u5b50: \u8981\u628a\u4ee5\u4e0b\u5de6\u4fa74\u4e2a\u5b57\u8282\u590d\u5236\u5230\u53f3\u4fa7
1000:0000 'A' 2000:0000 'A'\n1000:0001 'B' 2000:0001 'B'\n1000:0002 'C' 2000:0002 'C'\n1000:0003 00 2000:0003 00\n
mov ax, 1000h\nmov ds, ax\nmov si, 0 ; mov si, 3\nmov ax, 2000h\nmov es, ax\nmov di, 0 ; mov di, 3\nmov cx, 4\ncld ; std\nrep movsb\n
again:\nif(cx == 0)\n goto done;\nword ptr es:[di] = word ptr ds:[si]\nif(df==0)\n{si+=2; di+=2;}\nelse\n{si-=2; di-=2;}\ncx--\ngoto again\ndone:\n
rep movsd\u7684\u64cd\u4f5c\u8fc7\u7a0b:[dword]
again:\nif(cx == 0)\n goto done;\ndword ptr es:[di] = dword ptr ds:[si]\nif(df==0)\n{si+=4; di+=4;}\nelse\n{si-=4; di-=4;}\ncx--\ngoto again\ndone:\n
\u572832\u4f4d\u7cfb\u7edf\u4e0b, \u5047\u5b9ads:esi->\u6e90\u5185\u5b58\u5757
, es:edi->\u76ee\u6807\u5757
, DF=0
, \u5219\u5f53\u8981\u590d\u5236\u7684\u5b57\u8282\u6570ecx\u4e0d\u662f4\u7684\u500d\u6570\u65f6\uff0c\u53ef\u4ee5\u505a\u5982\u4e0b\u5904\u7406:
push ecx\nshr ecx, 2\nrep movsd\npop ecx\nand ecx, 3; \u76f8\u5f53\u4e8eecx = ecx % 4\nrep movsb\n
cmpsb [jz je]
repe cmpsb
again:\nif(cx == 0) goto done;\ntemp=byte ptr ds:[si]-byte ptr es:[di]\nold_fl=FL\n\u5f53DF=0\u65f6\uff0csi++\uff0cdi++\n\u5f53DF=1\u65f6\uff0csi--\uff0cdi--\ncx--\nFL=old_fl\nif (ZF==1) goto again\ndone:\n
* cx!=0 \u4e5f\u53ef\u80fd\u63d0\u524d\u7ed3\u675f\u5faa\u73af * repne cmpsb * \u82e5\u672c\u6b21\u6bd4\u8f83\u4e0d\u7b49\u5219\u7ee7\u7eed\u6bd4\u8f83\u4e0b\u4e00\u4e2a(repeat if not equal) again:\nif(cx == 0) goto done;\ntemp=byte ptr ds:[si]-byte ptr es:[di]\nold_fl=FL\n\u5f53DF=0\u65f6\uff0csi++\uff0cdi++\n\u5f53DF=1\u65f6\uff0csi--\uff0cdi--\ncx--\nFL=old_fl\nif (ZF==0) goto again\ndone:\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#scasbscasw-scasd","title":"\u5b57\u7b26\u4e32\u626b\u63cf\u6307\u4ee4\uff1ascasb\uff0cscasw, scasd","text":"cmp al, es:[di]\ndi++; (\u5f53DF=1\u65f6\uff0c\u4e3adi--)\n
repne scasb:
next:\n if(cx == 0) goto done;\n cmp al, es:[di]\n di++; \u5f53DF=1\u65f6\uff0c\u4e3adi--\n cx--\n je done\n goto next\ndone:\n
\u4f8b\u5b50: \u5047\u5b9a\u4ece\u5730\u57401000:2000\u5f00\u59cb\u5b58\u653e\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8bf7\u8ba1\u7b97\u8be5\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u5e76\u5b58\u653e\u5230CX\u4e2d\u3002\u5047\u5b9a\u5b57\u7b26\u4e32\u4ee5ASCII\u78010\u7ed3\u675f\uff0c\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u5305\u62ec0\u3002
mov ax, 1000h\nmov es, ax\nmov di, 2000h; ES:DI->\u76ee\u6807\u4e32\nmov cx, 0FFFFh; CX=\u6700\u591a\u627eFFFF\u6b21\nmov al, 0; AL=\u5f85\u627e\u7684\u5b57\u7b26\ncld ; DF=0\uff0c\u8868\u793a\u6b63\u65b9\u5411\nrepne scasb; again:\nnot cx ; \u76f8\u5f53\u4e8ecx=FFFF-cx\ndec cx\n;\u4e0a\u8ff0\u4e24\u6761\u6307\u4ee4\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u4ee5\u4e0b\u4e24\u6761\u6307\u4ee4:\n;inc cx\n;not cx\n;not cx\u76f8\u5f53\u4e8ecx=FFFF-cx\u00a0\n
mov al, '#'\ncld\nrepe scasb\ndec di; ES:DI->\"ABC\"\ninc cx; CX=4\npush es\npop ds; DS=ES\npush di\npop si; SI=DI\nmov ax, 2000h\nmov es, ax\nmov di, 0\nrep movsb\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#_4","title":"\u5b57\u7b26\u4e32\u64cd\u4f5c\u6307\u4ee4","text":"es:[di] = AL\ndi++; DF=1\u65f6\u4e3adi--\n
again:\nif(cx == 0) goto done;\nes:[di] = al\ndi++; \u5f53DF=1\u65f6, \u4e3aDI--\ncx--\ngoto again;\ndone:\n
memset(void *t,int value,int n);\ndouble x[100];\nmemset(&x,0,sizeof(x))\n
al=ds:[si]\nSI++;\u5f53DF=1\u65f6, \u4e3aSI--\n
cld\nagain:\n lodsb ; AL=DS:[SI], SI++\n ; mov al, ds:[si]\n ; inc si\n cmp al, '#'\n je next\n stosb ; ES:[DI]=AL, DI++\n ; mov es:[di], al\n ; inc di\nnext:\n dec cx\n jnz again\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#c","title":"C\u8bed\u8a00\u5b57\u7b26\u4e32\u51fd\u6570\u8f6c\u6362","text":"strlen:\n push bp\n mov bp,sp\n push di\n push ds\n\n\n mov di,[bp+4]\n mov ax,data\n mov ds,ax\n mov es,ax\n mov cx,0FFFFh\n xor al,al\n cld\n repne scasb\n inc cx\n not cx\n mov ax,cx\n\n pop ds\n pop di\n mov sp,bp\n pop bp\n ret\n_memcpy:\n push bp\n mov bp,sp\n push es\n push ds\n push si\n push di\n mov ax,data\n mov es,ax\n mov ds,ax\n mov si,word ptr [bp+6]\n mov di,word ptr [bp+4]\n mov cx,word ptr [bp+8]\n cld\n rep movsb\n pop di\n pop si\n pop ds\n pop es\n mov sp,bp\n pop bp\n ret\n_strncpy:\n push bp\n mov bp,sp\n push es\n push ds\n push si\n push di\n mov si, word ptr [bp+6]\n mov di,word ptr [bp+4]\n push si\n call _strlen\n pop cx\n cmp ax,[bp+8]\n jl tricky_form\n mov cx,word ptr [bp+8]\n cld\n rep movsb\n tricky_form:\n mov dx,[bp+8]\n sub dx,ax\n mov cx,ax\n cld\n rep movsb\n mov cx,dx\n mov ax,0\n cld\n rep stosb\n pop di\n pop si\n pop ds\n pop es\n mov sp,bp\n pop bp\n ret\n_memset:\n push bp\n mov bp,sp\n push es\n push di\n mov ax,data\n mov es,ax\n mov di,[bp+4]\n mov ax,[bp+6]\n mov cx,[bp+8]\n cld\n rep stosb\n pop di\n pop es\n mov sp,bp\n pop bp\n ret\n\n_clrscr:\n push es\n push di\n mov ax,0B800h\n mov es,ax\n mov di,0\n mov ax,0000h\n mov cx,2000\nagain:\n mov es:[di],ax\n add di,2\n sub cx,1\n jnz again\n pop di\n pop es\n ret\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#_5","title":"\u63a7\u5236\u8f6c\u79fb\u6307\u4ee4","text":"jc jnc jo jno js jns jz jnz ja jb jae jbe jg jge jl jle jp jnp
\u5730\u5740 \u673a\u5668\u7801 \u6c47\u7f16\u6307\u4ee4 \n 1D3E:0090 ...\n 1D3E:00F0\n 1D3E:0100 EB06 jmp 0108h\n 1D3E:0102 B402 mov ah\uff0c2\n 1D3E:0104 B241 mov dl, 41h\n 1D3E:0106 CD21 int 21h\n 1D3E:0108 B44C mov ah\uff0c4Ch\n 1D3E:010A CD21 int 21h\n
cmp ax, bx\n\uff08je equal; jump out of range\uff09\njne not_equal\njmp equal; \u8fd1\u8df3\nnot_equal:\n...; \u5047\u5b9a\u8fd9\u91cc\u7701\u7565\u6307\u4ee4\u7684\u673a\u5668\u7801\u603b\u957f\u5ea6\u8d85\u8fc77Fh\u5b57\u8282\nequal:\n...\n
jmp \u504f\u79fb\u5730\u5740\u6216\u6807\u53f7 ; \u5982jmp 1000h\njmp 16\u4f4d\u5bc4\u5b58\u5668 ; \u5982jmp bx\njmp 16\u4f4d\u53d8\u91cf ; \u5982jmp word ptr [addr]\n
jmp 1234h:5678h
; \u673a\u5668\u7801\u4e3a0EAh,78h,56h,34h,12h
db 0EAh\ndw 5678h\ndw 1234h\ndb 0EAh\ndd 0FFFF0000h\n;\u6216 dw 0000h\n;dw 0FFFFh\n;\u4e0a\u8ff03\u884c\u5b9a\u4e49\u5408\u5728\u4e00\u8d77\u8868\u793ajmp 1234h:5678h\n
mov word ptr ds:[bx],1234h\nmov word ptr ds:[bx+2],5678h\njmp dword ptr ds:[bx] ;jmp 5678h:1234h\n
data segment\naddr dw 0000h, 0FFFFh \n;\u6216\u5199\u6210addr dd 0FFFF0000h\ndata ends\ncode segment\nassume cs:code, ds:data\nmain:\nmov ax, data\nmov ds, ax\njmp dword ptr [addr] \n;\u76f8\u5f53\u4e8ejmp FFFF:0000\ncode ends\nend main\n
CX = CX - 1 ; \u5faa\u73af\u6b21\u6570\u51cf1\nif(CX != 0) ; \u82e5CX\u4e0d\u7b49\u4e8e0\uff0c\u5219\n goto dest ; \u8df3\u8f6c\u81f3dest\n
mov cx, 3\nnext:\nadd ax, cx; ax +3, +2, +1\nloop next; cx=2, 1, 0\n ; dec cx\n ; jnz next \u5148-1\u518d\u5224\u65ad\n ;--cx\u521d\u59cb\u503c=0\u65f6\u80fd\u8fbe\u5230\u6700\u5927\u5faa\u73af\u6b21\u6570 10000h\u6b21\n\nmov ax, 0\nmov cx, 0\njcxz done \u8fd9\u6761\u6307\u4ee4\u53ef\u4ee5\u9632\u6b62cx\u4e3a0\u65f6\u8fdb\u5165\u5faa\u73af\nnext:\nadd ax, cx\nloop next; \u5faa\u73af10000h\u6b21\ndone:\n
\u8fd1\u8fd4\u56de\uff1aretn\uff0c\u5728\u591a\u6570\u60c5\u51b5\u4e0b\u53ef\u5199\u6210ret\n\u8fdc\u8c03\u7528\uff1acall far ptr dest\n\u8fdc\u8fd4\u56de\uff1aretf\n
"},{"location":"assembly/%E5%9F%BA%E7%A1%80%E6%8C%87%E4%BB%A4/#movpushpopxchg","title":"\u901a\u7528\u6570\u636e\u4f20\u9001\u6307\u4ee4\uff1aMOV\uff0cPUSH\uff0cPOP\uff0cXCHG","text":"mov al, es:[di]\nmov ds:[bx], al\n
eax=12345678h\nebx=89ABCDEFh\nss=1000h\nsp=2000h\n\u73b0\u6267\u884c\u4ee5\u4e0b\u6307\u4ee4:\npush eax;(1)\npush ebx;(2)\npop ebx;(3)\npop eax;(4)\n
mov ax,1\nmov bx,2\nxchg ax,bx\nxchg ax,ds:[bx] \u53ef\u4ee5\u4ea4\u6362\u5bc4\u5b58\u5668\u548c\u53d8\u91cf \u4e0d\u5141\u8bb8\u5bf9\u4e24\u4e2a\u53d8\u91cf\u8fdb\u884c\u4ea4\u6362\n
stk segment stack \u5806\u6808\u53ea\u80fd\u5b9a\u4e49\u4e00\u4e2a\ns db 200h dup ('S');\u4e5f\u53ef\u4ee5\u4e0d\u5b9a\u4e49\u6570\u7ec4\u540d\ndb 200h dup('S')\nstk ends\n
* assume cs:code,ds:data,ss:stk * \\(mov\\ ah,s[1]\\) \u7f16\u8bd1\u540e\u53d8\u6210 \\(mov\\ ah,stk:[s+1]\\)\u518d\u8f6c\u5316\u6210$mov ah,ss:[1] $ * \u5982\u679c\u5f15\u7528\u6570\u7ec4s\u5fc5\u987b\u52a0\u4e0aassume ss\uff1astk"},{"location":"assembly/%E5%A0%86%E6%A0%88/#dos","title":"\u7a0b\u5e8f\u8f7d\u5165\u5185\u5b58\u540e\uff0c\u64cd\u4f5c\u7cfb\u7edfdos\u4f1a\u5bf9\u4e00\u4e0b\u5bc4\u5b58\u5668\u4f5c\u521d\u59cb\u5316\u8d4b\u503c\uff1a","text":"psp\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a100h\u5b57\u8282\u7684\u5185\u5b58\u5757\uff0c\u4f4d\u4e8e\u5f53\u524d\u7a0b\u5e8f\u9996\u6bb5\u7684\u524d\u9762 psp\u7531\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9\u5f53\u524d\u7a0b\u5e8f\uff0c\u91cc\u9762\u5b58\u653e\u4e86\u4e0e\u8be5exe\u76f8\u5173\u7684\u4e00\u4e9b\u4fe1\u606f\u5982\u547d\u4ee4\u884c\u53c2\u6570\u5373main\uff08\uff09\u7684\u53c2\u6570
int main(int argc,char *argv[]){ }\nmain.exe 123 xyz\nargv[0] [1] [2] \n
\u6e90\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u5b9a\u4e49\u5806\u6808\u6bb5\u65f6 ss=\u9996\u6bb5\u7684\u6bb5\u5730\u5740=1000h sp=0 sp-2=FFFE\uff081000\uff1aFFFE\uff09\u2014\u2014\u8fbe\u5230\u6700\u5927\u5806\u6808 \u5c0f\u7aef \u5373push a[0] (1234h)\u5728\u5806\u6808\u91cc\u4ec0\u4e48\u6837\u5b50 34 12 * 9000\uff1aFFFF\u662f\u7528\u6237\u53ef\u7528\u7684last \u5982\u679cpush\u592a\u591a\u4e86 \u53ef\u80fd\u8fdb\u5165code\u6bb5\u672b\u5c3e\uff0c\u5806\u6808\u6ea2\u51fa
"},{"location":"assembly/%E5%AF%84%E5%AD%98%E5%99%A8/","title":"\u5bc4\u5b58\u5668","text":""},{"location":"assembly/%E5%AF%84%E5%AD%98%E5%99%A8/#_1","title":"\u5bc4\u5b58\u5668\u8be6\u89e3","text":"\u5982\u679c\u672a\u5b9a\u4e49\u5806\u6808\uff0c\u90a3\u4e48ss\u548csp\uff1f
ss=\u9996\u6bb5\u7684\u6bb5\u5730\u5740=1000h \nsp=0 \nsp-2=FFFE\uff081000\uff1aFFFE\uff09\u2014\u2014\u8fbe\u5230\u6700\u5927\u5806\u6808 \n9000\uff1aFFFF\u662f\u7528\u6237\u53ef\u7528\u7684last \n\u5982\u679cpush\u592a\u591a\u4e86 \u53ef\u80fd\u8fdb\u5165code\u6bb5\u672b\u5c3e\uff0c\u5806\u6808\u6ea2\u51fa\n
11 10 9 8 7 6 4 2 0
O DF IF TF SF ZF AF PF CF
"},{"location":"assembly/%E5%AF%84%E5%AD%98%E5%99%A8/#cf-zf-sf-of-af-pf","title":"CF ZF SF OF AF PF: \u72b6\u6001\u6807\u5fd7","text":"CF: \u8fdb\u4f4d\u6807\u5fd7(carry flag)
mov ah, 0FFh ;\nadd ah, 1; ah=0, CF=1\u4ea7\u751f\u4e86\u8fdb\u4f4d \nadd ah, 2; ah=2, CF=0 \nsub ah, 3; ah=0FFh, CF=1\u4ea7\u751f\u4e86\u8fdb\u4f4d \n
mov cl,2 shr ah,cl;\u53f3\u79fb\u4e24\u4f4d\uff0ccf=1 \n
\u4e0eCF\u76f8\u5173\u7684\u8df3\u8f6c\u6307\u4ee4:
data segment\nabc dw 32767\ndata ends\ncode segment\nassume cs:code,ds:data\nmain:\n mov ax,data\n mov ds,ax\n mov ax,[abc]\n mov cx,16\nagain:\n shl ax,1\n jc is_one\nis_zero:\n mov dl,'0'\n jmp out_put\nis_one:\n mov dl,'1'\nout_put:\n push ax\n mov ah,2\n int 21h\n pop ax\n sub cx,1\n jnz again\nmov ah,4Ch\nint 21h\ncode ends\nend main\n
ZF: \u96f6\u6807\u5fd7(zero flag)
sub ax, ax; AX=0, ZF=1 \nadd ax, 1; AX=1, ZF=0 \nadd ax, 0FFFFh; AX=0, ZF=1, CF=1\n
SF: \u7b26\u53f7\u6807\u5fd7(sign flag)\u8fd0\u7b97\u7ed3\u679c\u7684\u6700\u9ad8\u4f4d
mov ah, 7Fh add ah, 1; AH=80h=1000 0000B, SF=1\u2014\u2014\u8fd0\u7b97\u7ed3\u679c\u8d1f\u6570 \nsub ah, 1; AH=7Fh=0111 1111B, SF=0\u2014\u2014\u8fd0\u7b97\u7ed3\u679c\u6b63\u6570\n
OF: \u6ea2\u51fa\u6807\u5fd7(overflow flag)
mov ah, 7Fh \nadd ah, 1 ;AH=80h, OF=1, ZF=0, CF=0, SF=1 \nmov ah, 80h add ah, 0FFh; AH=7Fh, OF=1, ZF=0, CF=1, SF=0 \nmov ah, 80h sub ah, 1; AH=7Fh, OF=1, ZF=0, CF=0, SF=0 \n
mov ab,81h ;ah=10000001B \nshl ah,1 ah=00000010B;0F=1,CF=1 \nonly one-bit shift\u4f1a\u5f71\u54cdOF\n
PF(Parity Flag)\u5947\u5076\u6807\u5fd7
mov ah, 4 add ah, 1; AH=0000 0101B, PF=1\u8868\u793a\u6709\u5076\u6570\u4e2a1 \nmov ax, 0101h add ax, 0004h; AX=0105h=0000 0001 0000 0101B \n; PF=1\u53ea\u7edf\u8ba1\u4f4e8\u4f4d\u4e2d1\u7684\u4e2a\u6570 \u8981\u662f\u4f4e8\u4f4d\u4e2d1\u7684\u4e2a\u6570\u662f\u5947\u6570\u65f6\uff0cPF=0 \n
\u5047\u5b9a\u8981\u53d1\u9001\u5b57\u7b26\u2019C\u2019=0100 0011B, \u73b0\u5047\u5b9a\u4f4e7\u4f4d\u4e3a\u6570\u636e\u4f4d \u6700\u9ad8\u4f4d\u4e3a\u6821\u9a8c\u4f4d\u3002\u90a3\u4e48\u6821\u9a8c\u4f4d\u7684\u8ba1\u7b97\u65b9\u6cd5\u67092\u79cd:
(1) \u5947\u6821\u9a8c: \u6570\u636e\u4f4d+\u6821\u9a8c\u4f4d\u5408\u8d77\u6765\uff0c1\u7684\u4e2a\u6570\u5fc5\u987b\u662f\u5947\u6570
(2) \u5076\u6821\u9a8c: \u6570\u636e\u4f4d+\u6821\u9a8c\u4f4d\u5408\u8d77\u6765\uff0c1\u7684\u4e2a\u6570\u5fc5\u987b\u662f\u5076\u6570
\u73b0\u5728\u6211\u4eec\u91c7\u7528\u5076\u6821\u9a8c\u6765\u53d1\u9001\u2019C\u2019,\u90a3\u4e48\u6821\u9a8c\u4f4d\u5fc5\u987b\u7b49\u4e8e1,\u5373\u5b9e\u9645\u8981\u53d1\u9001\u76848\u4f4d\u4e8c\u8fdb\u5236\u503c\u4e3a1100 0011B \u5bf9\u65b9\u63a5\u6536\u8fd98\u4f4d\u503c\u5e76\u4fdd\u5b58\u5728\u5bc4\u5b58\u5668AL\u4e2d, \u63a5\u4e0b\u53bb\u53ef\u4ee5\u6267\u884c\u5982\u4e0b\u4ee3\u7801\u6765\u9a8c\u8bc1AL\u4e2d\u7684\u503c\u662f\u5426\u6709\u9519:
or al, al\njnp error; if(PF==0) goto error\n good:\n ...\n error:\n ...\n
AF(Auxiliary Flag) \u8f85\u52a9\u8fdb\u4f4d\u6807\u5fd7
\u4f4e4\u4f4d\u5411\u9ad84\u4f4d\u4ea7\u751f\u8fdb\u4f4d\u6216\u501f\u4f4d
mov ah, 1Fh ; 0001 1111\nadd ah, 1 ; ah=20h, AF=1\n
AF\u8ddfBCD(Binary Coded Decimal)\u7801\u6709\u5173
DF:direction flag
TF:trace/trap flag
IF:interrupt flag
DF(Direction Flag)\u65b9\u5411\u6807\u5fd7: \u63a7\u5236\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u65b9\u5411
data segment:\ns db \"ABC123xyz\",0\nt db 10 dup(0)\ndata ends\ncode segment:\nassume cs:code,ds:data\nmain:\n mov ax,data\n mov ds,ax\n mov es,ax\n mov si,offset s+9\n mov di,offset t+9\n mov cx,10\n std\n rep movsb ;memcpy(es:di,ds:si,cx)\ndone:\n mov ah,4Ch\n int 21h\ncode ends\nend main\n\n\u6ce8\uff1arep movsb\nagain:\n cmp cx,0\n je done\n mov al,ds[si]\n mov es:[di],al\n inc si/dec si\n inc di/dec di\n dec cx\n jmp again\n
IF\uff08Interrupt Flag\uff09\u4e2d\u65ad\u6807\u5fd7
mov ax, 0 \nmov bx, 1 \nadd ax, bx \n
\u65f6\u949f\u6bcf\u96941/18\u5999\u4f1a\u4ea7\u751f\u4e00\u4e2a\u4e2d\u65ad\u8bf7\u6c42 \u82e5\u7a0b\u5e8f\u5df2\u8fd0\u884c\u4e861/18\u79d2,\u5219cpu\u4f1a\u5728\u6b64\u5904\u63d2\u5165\u4e00\u6761int 8h\u6307\u4ee4\u5e76\u6267\u884c\uff08count++\uff09
TF\uff08Trace/Trap Flag\uff09\u8ddf\u8e2a/\u9677\u9631\u6807\u5fd7
\u5229\u7528\u5355\u6b65\u6a21\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u53cd\u8c03\u8bd5,\u6f14\u793a\u4ee3\u7801\u89c1\u4ee5\u4e0b\u94fe\u63a5: http://cc.zju.edu.cn/bhh/antidbg.zip
bp\u7684\u9690\u542b\u6bb5\u5730\u5740\u662fss\u300c\u7528\u4ee5\u4ee3\u66ffsp\uff0c\u53ef\u83b7\u5f97\u5806\u6808\u4e2d\u7684\u5185\u5bb9\u300f
code segment\nassume cs:code,ds:data\nmain:\n jmp begin\nold1h dw 0,0\nprev_addr dw offset first,code \n[\u6bb5\u5730\u5740+\u504f\u79fb\u5730\u5740\uff1a2x16\u4f4d]\n[\u5982\uff1aA0 00:00 01 \u662f\u4e00\u4e2a\u5730\u5740]\nbegin:\n push cs\n pop ds\n xor ax,ax\n mov es,ax ;es=0\n mov bx,4 ;bx=4 es:[bx]->int1h \u7684\u4e2d\u65ad\u5411\u91cf(\u8be5\u51fd\u6570\u7684\u9996\u5730\u5740) \u5176\u4e2d\u524d16\u4f4d\u5b58\u653e\u504f\u79fb\u5730\u57400:[4]\uff0c\u540e16\u4f4d\u5b58\u653e\u6bb5\u5730\u57400:[6]\n push es:[bx]\n pop old1h[0]\n push es:[bx+2]\n pop old1h[2]\n mov word ptr es:[bx],offset int1h\n mov word ptr es:[bx+2],cs\n pushf [save old FL \u628aflag\u538b\u5165\u5806\u6808]\n pushf [\u4e3a\u4e86\u4e4b\u540e\u518d\u628aTF\u6539\u62100][and ax,0FEFFh]\n pop ax [\u4e0d\u53ef\u7528mov ax,fl \u5373fl\u548cip\u552f\u4e8c\u4e0d\u80fd\u88ab\u5f15\u7528\u7684\u5bc4\u5b58\u5668]\n or ax,100h [\u5c06TF\u6539\u62101]\uff01TF=1\n push ax\n popf [FL=AX]\nfirst:\n nop \n \u5f53\u67d0\u6307\u4ee4\u6267\u884c\u524dTF=1 \u5219\u8be5\u6307\u4ee4\u6267\u884c\u4e4b\u540e\u624d\u4f1a\u81ea\u52a8\u6267\u884cint01h\u5355\u6b65\u4e2d\u65ad\nsingle\u2014\u2014step_begin:\n\n[first int 1h] \nint1h\u53d1\u751f\u7684\u65f6\u5019 cpu\u4f1a\u505a\u4ee5\u4e0b\u7684\u52a8\u4f5c\uff1a\npushf \u5c06flag\u538b\u5165\u5806\u6808\npush cs \u5c06\u4e0b\u4e00\u6761\u6307\u4ee4\u7684\u6bb5\u5730\u5740\u538b\u5165\u5806\u6808\npush offset back \u5c06\u4e0b\u4e00\u6761\u6307\u4ee4\u7684\u504f\u79fb\u5730\u5740\u538b\u5165\u5806\u6808 <- ss:sp\njmp dword ptr 0:[4]\u8fdb\u5165\u51fd\u6570int1h\n\nback:\n xor ax,ax\n mov cx,3\nnext\uff1a\n add ax,cx ;\u5df2\u89e3\u5bc6\n nop\n loop next\n popf\n nop\nsingle_step_end:\n push old1h[0]\n pop es:[bx]\n push old1h[2]\n pop es:[bx+2]\n mov ah,4Ch\n int 21h\nint1h:\n push bp\n mov bp,sp\n push bx\n push es\n mov bx,cs:prev_addr[0]\n mov es,cs:prev_addr[2]\n inc byte ptr es:[bx]\n mov bx,[bp+2]\n mov es,[bp+4]\n dec byte ptr es:[bx]\n mov cs:prev_addr[0],bx\n mov cs:prev_addr[2],es \n pop es\n pop bx\n pop bp\n iret[\u4e2d\u65ad\u8fd4\u56de cpu\u4f1a\u6267\u884cpop ip \uff0cpop cs\uff0cpopf]\ncode ends\nend main\n
"},{"location":"assembly/%E6%98%BE%E5%8D%A1%E8%BE%93%E5%87%BA/","title":"\u663e\u5361\u8f93\u51fa","text":""},{"location":"assembly/%E6%98%BE%E5%8D%A1%E8%BE%93%E5%87%BA/#_1","title":"\u663e\u5361\u8f93\u51fa","text":"0000\u2014\u2014black\n0001-blue\n0010-green\n0100-red\n0011(BLue and green)\n
code segment\nassume cs:code\nmain:\n mov ax,0B800h\n mov ds,ax\n mov di,0\n mov al,'A'\n mov ah,17h\n mov cx,2000\nagain:\n mov ds:[di],ax\n mov bx,800h\n\nwait_wait:\n mov dx,0\nwait_a_while:\n sub dx,1\n jnz wait_a_while\n sub bx,1\n jnz wait_a_while;1000h\u4e58\u4ee5800h\n\n mov word ptr ds:[di],0020h ;00\u9ed1\u8272\n add di,2\n sub cx,1\n jnz again\n\n mov ah,1\n int 21h (\u7528\u6237\u6572\u952e\u76d8\u518d\u7ed3\u675f)\n mov ah ,4Ch\n int 21h\n\ncode ends\nend main\n
"},{"location":"assembly/%E6%98%BE%E5%8D%A1%E8%BE%93%E5%87%BA/#_2","title":"\u5185\u5b58\u6620\u5c04","text":"dos\u53ca\u7528\u6237\u7a0b\u5e8f\u5360\u7528\u8fd9\u5757\u5185\u5b58\uff0c\u603b\u957f\u4e3a640KB
1000:0000~1000:FFFF\n\u2026\u2026\n9000:0000~9000:FFFF\n
A000:0000~A000:FFFF
mov ah,0 set video mode\nmov al,13h 13\u53f7 320*200\nint 10h\n
al=12h \u5207\u6362\u5230640 * 480 16 \u8272\u56fe\u5f62\u6a21\u5f0f > 640 480 \u8de8\u6bb5\uff0c\u8fd9\u4e2a\u65f6\u5019\u4e0d\u662f\u7528\u4e00\u4e2a\u5b57\u8282\u63a7\u5236\u4e00\u4e2a\u70b9\uff0c\u800c\u662f\u4e00\u4e2abit\u63a7\u5236\u4e00\u4e2a\u70b9 \uff0c\u7528\u56db\u4e2a\u5e73\u9762\u53e0\u8d77\u6765\uff0c\u63a7\u523616\u79cd\u989c\u8272
code segment\nassume cs:code\nmain:\n jmp begin\ni dw 0\nbegin:\n mov ah,00h\n mov al,13h\n int 10h\n mov ax,0A000h\n mov es,ax\n mov di,(100-20)*320+(160-20)\n mov [i],41 \u4e00\u517141\u884c\nnext_row:\n ;push cx\n push di\n mov al,4 ;color=red\n mov cx,41\nnext_dot:\n mov es:[di],al\n add di,1\n sub cx,1\n jnz next_dot\n pop di\n add di,320\n sub [i],1\n jnz next_row\n mov ah,1\n int 21h\n\n mov ah,00h\n mov al,03h;80*25\u7684\u6587\u672c\u6a21\u5f0f\n int 10h\n mov ah ,4Ch\n int 21h\ncode ends\nend main\n
Applications
\u7a0b\u5e8f \u753b\u6c49\u5b57\n\ndata segment:\nhz db 04h,08h,0Eh,0A0h,78h,80h,08h,90h,\u2026\u202610h,0Ch\ndata ends\ncode segment:\nassume cs:code,ds:data\nmain:\n mov ax,data\n mov ds,ax\n mov ax,0A000h\n mov es,ax\n mov di,0\n mov ax,0013h\n int 10h\n mov dx,16\n mov si,0\nnext_row:\n mov ah,hz[si]\n mov al,hz[si+1]\n add si,2\n mov cx,16\ncheck_next_dot:\n shl ax,1\n jnc no_dot\nis_dot:\n mov byte ptr es:[di],0Ch\nno_dot:\n add di,1\n sub cx,1\n jnz check_next_dot\n sub di,16\n add di,320\n sub dx,1\n jnz next_row\n mov ah,1\n int 21h\n mov ax,0003h\n int 10h;\u56de\u523080*25\u6587\u672c\u6a21\u5f0f\n mov ah,4Ch\n int 21h\ncode ends\nend main\n
CPU <\u2014> \u7aef\u53e3(port) <\u2014> I/O\u8bbe\u5907
\u7aef\u53e3\u7f16\u53f7\u5c31\u662f\u7aef\u53e3\u5730\u5740\u3002\u7aef\u53e3\u5730\u5740\u7684\u8303\u56f4\u662f\uff1a[0000h, 0FFFFh]\uff0c\u517165536\u4e2a\u7aef\u53e3
\u5bf9\u7aef\u53e3\u64cd\u4f5c\u4f7f\u7528\u6307\u4ee4in\u4e0eout\u5b9e\u73b0\u3002
in al, 60h
\u4ece\u7aef\u53e360h\u8bfb\u53d6\u4e00\u4e2a\u5b57\u8282\u5e76\u5b58\u653e\u5230AL\u4e2dmov al,2\nout 70h,al \u544a\u8bc9cmos\u63a5\u4e0b\u6765\u8981\u53bb\u8bbf\u95ee\u5b83\u76842\u53f7\u5185\u5b58\u5355\u5143\nin al,71h \u8bfb\u53d6cmos\u4e2d2\u53f7\u5355\u5143\u7684\u503c\uff08\u5206\u949f\uff09\nmov al, 4\nout 70h, al\nmov al, 23h\nout 71h, al; \u628acmos4\u53f7\u5355\u5143\u5373\u5c0f\u65f6\u7684\u503c\u6539\u621023\u70b9\n
"},{"location":"note/basicII/","title":"Basic data Structures II","text":""},{"location":"note/basicII/#graphs","title":"Graphs","text":""},{"location":"note/basicII/#some-defs","title":"Some defs","text":"
\\(Undirected \\ V=n \\ \\ \\ \\ E=C_n^2=\\frac{n(n-1)}{2}\\)
\\(Directed \\ V=n \\ \\ \\ \\ E=2*C_n^2=n(n-1)\\)
\\(Undirected \\ : (v_i,v_j)\\ is \\ incident \\ on\\ v_i\\ and\\ v_j\\)
\\(Directed \\ : v_i \\ is\\ adjacent\\ to \\ v_j \\ \\ \\ \\ \\ v_j \\ is\\ adjacent\\ from \\ v_i\\)
Subgraph
Simple Path
Connected
\\(Undirected:\\) An undirected graph G is connected if every pair of distinct \\(v_i\\) and \\(v_j\\) are connected
\\(Directed:\\)
Strongly connected directed graph G = for every pair of \\(v_i\\) and \\(v_j\\) in V( G ), there exist directed paths from \\(v_i\\) to \\(v_j\\) and from \\(v_j\\) to \\(v_i\\).
If the graph is connected without direction to the edges, then it is said to be weakly connected
Strongly connected component : the maximal subgraph that is strongly connected
For a directed G, we have in-degree and out-degree.
\\(r=v-e+2\\)
\\(adj_- mat[i][j]=\\left\\{\\begin{array}{l} 1 \\ if (v_i,v_j) \\ or <v_i,v_j> \\in E(G)\\\\0\\ \\ otherwise \\end{array}\\right.\\)
If G is undirected the matrix is symmetric,thus sorting only half of the matrix
The trick is to store the matrix as a 1-D array: adj_mat [ $n(n+1)/2 $] = \\({ a_{11}, a_{21}, a_{22}, ..., a_{n1}, ..., a_{nn} }\\)
The index for \\(a_{ij}\\) is \\(i( i - 1 )/2+j\\).
\\(\\begin{align*}degree(i) &= \\sum_{j=0}^{n-1}adj_-mat[i][j] \\ (If\\ G\\ is\\ undirected)\\\\ & \\ \\ +\\sum_{j=0}^{n-1}adj_-mat[j][i]\\ (If\\ G\\ is\\ directed)\\end{align*}\\)
"},{"location":"note/basicII/#adjacency-lists","title":"Adjacency Lists","text":"Degree( \\(i\\) ) = number of nodes in graph[ \\(i\\) ] (if \\(G\\) is undirected).
T of examine (whether complete) E(G) = O( n + e )
A. Add inverse adjacency lists
B.Multilists
"},{"location":"note/basicII/#adjacency-multilist","title":"Adjacency Multilist","text":"Digraph G in which V( G ) represents activities ( e.g. the courses ) and E( G ) represents precedence relations
If the precedence relation is reflexive, then there must be an i such that i is a predecessor of i.
That is, i must be done before i is started. Therefore if a project is feasible, it must be irreflexive.
A topological order is a linear ordering of the vertices of a graph such that, for any two vertices, i, j, if i is a predecessor of j in the network then i precedes j in the linear ordering.
Test an AOV for feasibility, and generate a topological order if possible.
Method One \\(T=O(|V|^2)\\)
void Topsort( Graph G )\n{ int Counter;\n Vertex V, W;\n for ( Counter = 0; Counter < NumVertex; Counter ++ ) {\n V = FindNewVertexOfDegreeZero( );\n if ( V == NotAVertex ) {\n Error ( \u201cGraph has a cycle\u201d ); break; }\n TopNum[ V ] = Counter; /* or output V */\n for ( each W adjacent to V )\n Indegree[ W ] \u2013 \u2013 ;\n }\n}\n
void Topsort( Graph G )\n{ Queue Q;\n int Counter = 0;\n Vertex V, W;\n Q = CreateQueue( NumVertex ); MakeEmpty( Q );\n for ( each vertex V )\n if ( Indegree[ V ] == 0 ) Enqueue( V, Q );\n while ( !IsEmpty( Q ) ) {\n V = Dequeue( Q );\n TopNum[ V ] = ++ Counter; /* assign next */\n for ( each W adjacent to V )\n if ( \u2013 \u2013 Indegree[ W ] == 0 ) Enqueue( W, Q );\n } /* end-while */\n if ( Counter != NumVertex )\n Error( \u201cGraph has a cycle\u201d );\n DisposeQueue( Q ); /* free memory */\n}\n
"},{"location":"note/basicII/#midterm-review","title":"Midterm Review","text":"Which of the following statements is TRUE about topological sorting? (5\u5206)
3 is true
"},{"location":"note/basicII/#shortest-path-problem","title":"Shortest Path Problem","text":""},{"location":"note/basicII/#1single-source-shortest-path-problem","title":"1.Single-Source Shortest Path Problem","text":"void unweighted(Table T){\n int CurrDist;\n Vertex V,W;\n for(CurrDist=0;CurrDist<NumVertex;CurrDist++){\n for(each vertex V){ \n if(!T[V].Known&&T[V].Dust==CurrDist){\n R[V].Known=true;\n for(each W adjacent to V){\n if(T[W].Dist==infinity){\n T[W].Dist=CurrDist+1;\n T[E].Path=V;\n }\n }\n }\n }\n }\n}\n
But the time complexity is \\(O(|V|^2)\\)
Improvement
void unweighted (Table T){\n Queue Q;\n Vertex V,W;\n Q=CreateQueue(NumVertex);MakeEmpty(Q);\n Enqueue(S,Q);\n while(!isEmpty(Q)){\n V=Dequeue(Q);\n T[V].known=true;//not really necessary\n for(each W adjacent to V){\n if(T[W].Dist==Infinity){\n T[W].Dist=T[V].Dist+1;\n T[W].Path=V;\n ENqueue(W,Q)\n }\n }\n }\n DisposeQueue(Q);\n}\n
void Dijkstra(Table T){\n //T is initialized by Figure 9.30 on p 303\n Vertex V,W;\n for(;;){\n V=smallest unknown distance vertex;\n if(V==NotAVertex){break;}\n T[V].known=true;\n for(each W adjacent to V){\n if(!T[W].Known){\n if(T[V].Dist+Cvw<T[W].Dist){\n Decrease(T[W].Dist to T[V].Dist+Cvw);\n T[W].Path=V\n }\n }\n }\n }\n}\n
\\(T = O( |V|^2 + |E| )\\)
Initialization: The initialization phase involves traversing all vertices, setting their distances to infinity, and setting the initial vertex's distance to 0. The time complexity of this step is O(V), where V is the number of vertices.
Main Loop: The number of iterations in the main loop depends on the number of vertices. In each iteration, the algorithm selects the smallest unknown distance vertex V and then traverses all vertices W adjacent to V. For each W, it checks if there is a shorter path through V to W, and if so, it updates the distance of W.
The time complexity of this step is \\(O(V^2)\\), as, for each vertex V, all vertices adjacent to V are considered.
Finding the Minimum Distance Vertex: In the main loop, the algorithm needs to find the smallest unknown distance vertex V. The time complexity of this step is O(V^2), as it needs to check the distance of each vertex.
In summary, the time complexity of the Dijkstra algorithm is \\(O(V^2)\\).
Implementation 2
V = smallest unknown distance vertex: Keep distances in a priority queue and call DeleteMin \u2013 \\(O(log|V|)\\)
Decrease( T[ W ].Dist to T[ V ].Dist + Cvw )
\\(T = O( |V|log|V|+|E|log|V|)=O(|E|log|V|)\\) ----Good if the graph is sparse
Must keep doing DeleteMin until an unknown vertex emerges
\\(T = O(|E| log|V| )\\) but requires \\(|E|\\) DeleteMin with \\(|E|\\) space
void WeightedNegative( Table T )\n{ Queue Q;\n Vertex V, W;\n Q = CreateQueue (NumVertex ); MakeEmpty( Q );\n Enqueue( S, Q ); /* Enqueue the source vertex */\n while (!IsEmpty(Q)){\n V=Dequeue(Q);/* each vertex can dequeue at most |V| times */\n for(each W adjacent to V){\n if ( T[ V ].Dist + Cvw < T[ W ].Dist ){/* no longer once per edge */\n T[ W ].Dist = T[ V ].Dist + Cvw;\n T[ W ].Path = V;\n if(W is not already in Q){Enqueue(W,Q)}\n }/* end-if update */ \n }\n }/* end-while */\n DisposeQueue( Q ); /* free memory */\n}\n
"},{"location":"note/basicII/#acyclic-graphs","title":"Acyclic Graphs","text":"If the graph is acyclic, vertices may be selected in topological order since when a vertex is selected, its distance can no longer be lowered without any incoming edges from unknown nodes.
\\(T=O(|E| + |V|)\\) and no priority queue is needed.
Application: AOE ( Activity On Edge ) Networks \u2014\u2014 scheduling a project
"},{"location":"note/basicII/#all-pairs-shortest-path-problem","title":"All-pairs Shortest path problem","text":""},{"location":"note/basicII/#network-flow-problem","title":"Network Flow Problem","text":""},{"location":"note/basicII/#ford-fulkerson-algorithm","title":"Ford-Fulkerson Algorithm","text":"If the edge capabilities are rational numbers, this algorithm always terminate with a maximum flow.
The algorithm works for G with cycles as well.
Analysis
"},{"location":"note/basicII/#1an-augmenting-path-can-be-found-by-an-unweighted-shortest-path-algorithm","title":"1.An augmenting path can be found by an unweighted shortest path algorithm.","text":"\\(T = O( f\\cdot|E| )\\) where f is the maximum flow.
"},{"location":"note/basicII/#2always-choose-the-augmenting-path-that-allows-the-largest-increase-in-flow","title":"2.always choose the augmenting path that allows the largest increase in flow.","text":"\\(\u300cmodify\\ Dijkstra\u2019s\\ algorithm\\ \u300d\\)
\\(\\begin{align*}T&=T_{argmentation}*T_{find\\ a\\ path}\\\\ &=O(E)log(cap_{max})*O(|E|log|V|)\\\\ &= O( |E|^2log|V|).\\end{align*}\\) if capmax is a small integer
"},{"location":"note/basicII/#3always-choose-the-augmenting-path-that-has-the-least-number-of-edges","title":"3.Always choose the augmenting path that has the least number of edges.","text":"Simple \\(BSF\\) Unweighted shortest path algorithm
\\(\\begin{align*}T&=T_{argmentation}*T_{find\\ a\\ path}\\\\ &=O(E)*O(|E||V|)\\\\ &= O( |E|^2|V|).\\end{align*}\\)
(1) we must use only edges within the graph
(2) we must use exactly |V| -1 edges
(3) we may not use edges that would produce a cycle.
"},{"location":"note/basicII/#1prims-algorithm-grow-a-tree","title":"1.Prim\u2019s Algorithm \u2013 grow a tree","text":""},{"location":"note/basicII/#2krukals-algorithm-grow-a-tree","title":"2.Krukal\u2019s Algorithm \u2013 grow a tree","text":"void Kruskal ( Graph G )\n{ T = { } ;\n while ( T contains less than |V| - 1 edges \n && E is not empty ) {\n choose a least cost edge (v, w) from E ; /* Delete Min */\n delete (v, w) from E ;\n if ( (v, w) does not create a cycle in T ) /Union Find/\n add (v, w) to T ;*\n else \n discard (v, w) ;\n }\n if ( T contains fewer than |V| - 1 edges )\n Error ( \u201cNo spanning tree\u201d ) ;\n}\n
"},{"location":"note/basicII/#applications-of-depth-first-search","title":"Applications of Depth-First Search","text":"void DFS ( Vertex V ) /* this is only a template */\n{ visited[ V ] = true; /* mark this vertex to avoid cycles */\n for ( each W adjacent to V )\n if ( !visited[ W ] )\n DFS( W );\n} \n/* T = O( |E| + |V| ) as long as adjacency lists are used */\n
"},{"location":"note/basicII/#biconnectivity","title":"Biconnectivity","text":"Refert to https://www.baeldung.com/cs/scc-tarjans-algorithm
A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph.
There is no back edge from one SCC to another (There can be cross edges, but cross edges will not be used while processing the graph).
Case1 (Tree Edge): If node v is not visited already, then after the DFS of v is complete, a minimum of low[u] and low[v] will be updated to low[u]. low[u] = min(low[u], low[v])
#include <iostream>\n#include <cstring>\nusing namespace std;\nconst int maxn = 1005; // \u5047\u8bbe\u6700\u5927\u8282\u70b9\u6570\u4e3a1005\nint G[maxn][maxn]; // \u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\nint deg[maxn]; // \u8282\u70b9\u7684\u5ea6\nint ans[maxn]; // \u5b58\u50a8\u7ed3\u679c\u7684\u6570\u7ec4\nint ansi = 0; // \u7ed3\u679c\u6570\u7ec4\u7684\u7d22\u5f15\nbool visited[maxn]; // \u6807\u8bb0\u8282\u70b9\u662f\u5426\u88ab\u8bbf\u95ee\u8fc7\nvoid dfs(int x) {\n for (int y = 1; y <= maxn; ++y) {\n if (G[x][y]) {\n G[x][y] = G[y][x] = 0;\n dfs(y);\n }\n }\n ans[++ansi] = x;\n}\nint main() {\n // ... \u8bfb\u53d6\u8f93\u5165\uff0c\u521d\u59cb\u5316 G \u548c deg\n int cnt = 0, root = 0;\n for (int i = 1; i <= maxn; ++i) {\n if (deg[i] % 2) {\n cnt++;\n if (!root) root = i;\n }\n }\n if (!root) {\n for (int i = 1; i <= maxn; ++i) {\n if (deg[i]) {\n root = i; break;\n }\n }\n }\n if (cnt && cnt != 2) {\n cout << \"No Solution\\n\";\n return 0;\n }\n dfs(root);\n // \u8f93\u51fa\u7ed3\u679c\n for (int i = ansi; i > 0; --i) {\n cout << ans[i] << ' ';}\n cout << '\\n';\n return 0;\n}\n
"},{"location":"note/basicII/#hamilton-cycle","title":"Hamilton cycle","text":""},{"location":"note/hash/","title":"HASH","text":""},{"location":"note/hash/#hashing","title":"HASHING","text":""},{"location":"note/hash/#interpolation-search","title":"Interpolation Search","text":"
Interpolation Search is a searching algorithm used to find a specific element in a sorted array. Similar to binary search, it employs a divide-and-conquer strategy, but it differs by estimating the probable position of the target value based on the distribution of the search key and array elements at each step.
The algorithm proceeds as follows:
Assume the array is sorted.
Calculate the interpolation position using the formula:
\\(position=low+\u230a\\frac{(high\u2212low)\u00d7(key\u2212arr[low])}{arr[high]\u2212arr[low]\u230b}\\)
Here, \\(arr\\) is the array, and \\(low\\) and \\(high\\) are the array indices, while \\(key\\) is the element to be searched.
If \\(arr[position]=key\\), the target element is found, and the position positionposition is returned.
If \\(arr[position]<key\\), continue the search in the right half by updating \\(low=position+1\\)
If \\(arr[position]>key\\), continue the search in the left half by updating \\(high=position\u22121\\)
Repeat steps 2 to 5 until the target element is found or \\(low>high\\).
Interpolation Search excels when dealing with uniformly distributed sorted arrays, as it efficiently estimates the target element's position, reducing the number of search iterations. However, its effectiveness relies on the assumption of a roughly uniform distribution of array elements. In cases of uneven data distribution, other search algorithms may outperform Interpolation Search. Binary search, for instance, could be more stable as it is less sensitive to the distribution of array elements.
"},{"location":"note/hash/#hash-tables","title":"Hash Tables","text":"https://www.geeksforgeeks.org/hashing-data-structure/#introduction
For numbers\uff1a
\uff081\uff09\u6c42\u4f59\u6cd5
\uff082\uff09\u5e73\u65b9\u53d6\u4e2d
\uff083\uff09\u6298\u53e0\u6cd5
\uff084\uff09\u6570\u5b57\u5206\u6790\u6cd5
\\(f(x)=(\\sum x[N-i-1]*32^i)\\%TableSize\\)
Index Hash3( const char *x, int TableSize ) {\n unsigned int HashVal = 0; \n/* 1*/ while( *x != '\\0' ) \n/* 2*/ HashVal = ( HashVal << 5 ) + *x++; \n/* 3*/ return HashVal % TableSize; \n} \n
struct ListNode; \ntypedef struct ListNode *Position; \nstruct HashTbl; \ntypedef struct HashTbl *HashTable; \nstruct ListNode { \n ElementType Element; \n Position Next; \n}; \ntypedef Position List; \n/* List *TheList will be an array of lists, allocated later */ \n/* The lists use headers (for simplicity), */ \n/* though this wastes space */ \nstruct HashTbl { \n int TableSize; \n List *TheLists; \n};\n
"},{"location":"note/hash/#create-an-empty-table","title":"Create an empty table","text":"HashTable InitializeTable( int TableSize ) \n{ HashTable H; \n int i; \n if ( TableSize < MinTableSize ) { \n Error( \"Table size too small\" ); return NULL; \n } \n H = malloc( sizeof( struct HashTbl ) ); /* Allocate table */\n if ( H == NULL ) FatalError( \"Out of space!!!\" ); \n H->TableSize = NextPrime( TableSize ); /* Better be prime */\n H->TheLists = malloc( sizeof( List ) * H->TableSize ); /*Array of lists*/\n if ( H->TheLists == NULL ) FatalError( \"Out of space!!!\" ); \n for( i = 0; i < H->TableSize; i++ ) { /* Allocate list headers */\n H->TheLists[ i ] = malloc( sizeof( struct ListNode ) ); /* Slow! */\n if ( H->TheLists[ i ] == NULL ) FatalError( \"Out of space!!!\" ); \n else H->TheLists[ i ]->Next = NULL;\n } \n return H; \n} \n
"},{"location":"note/hash/#find-a-key-from-a-hash-table","title":"Find a key from a hash table","text":"Position Find ( ElementType Key, HashTable H ) \n{ \n Position P; \n List L; \n L = H->TheLists[ Hash( Key, H->TableSize ) ]; \n\n P = L->Next; \n while( P != NULL && P->Element != Key ) /* Probably need strcmp */ \n P = P->Next; \n return P; \n} \n
"},{"location":"note/hash/#insert-a-key-into-a-hash-table","title":"Insert a key into a hash table","text":"void Insert ( ElementType Key, HashTable H ) \n{ \n Position Pos, NewCell; \n List L; \n Pos = Find( Key, H ); \n if ( Pos == NULL ) { /* Key is not found, then insert */\n NewCell = malloc( sizeof( struct ListNode ) ); \n if ( NewCell == NULL ) FatalError( \"Out of space!!!\" ); \n else { \n L = H->TheLists[ Hash( Key, H->TableSize ) ]; \n NewCell->Next = L->Next; \n NewCell->Element = Key; /* Probably need strcpy! */ \n L->Next = NewCell; \n } \n } \n} \n
"},{"location":"note/hash/#open-addressing","title":"Open Addressing","text":"Solve Collision
Algorithm: insert key into an array of hash table\n{\n index = hash(key);\n initialize i = 0 ------ the counter of probing;\n while ( collision at index ) {\n index = ( hash(key) + f(i) ) % TableSize;\n if ( table is full ) break;\n else i ++;\n }\n if ( table is full ) ERROR (\u201cNo space left\u201d);\n else insert key at index;\n}\n
"},{"location":"note/hash/#linear-probing","title":"Linear Probing","text":"\\(f(i)=i\\)
\\(f(i)=i^2\\)
Position Find ( ElementType Key, HashTable H ) \n{ Position CurrentPos; \n int CollisionNum; \n CollisionNum = 0; \n CurrentPos = Hash( Key, H->TableSize ); \n while( H->TheCells[ CurrentPos ].Info != Empty && H->TheCells[ CurrentPos ].Element != Key) { \n CurrentPos += 2 * (++CollisionNum)-1; \n if ( CurrentPos >= H->TableSize ) CurrentPos \uf02d = H->TableSize; \n } \n return CurrentPos; \n} \n
void Insert ( ElementType Key, HashTable H ){ \n Position Pos; \n Pos = Find( Key, H ); \n if ( H->TheCells[ Pos ].Info != Legitimate ) { /* OK to insert here */ \n H->TheCells[ Pos ].Info = Legitimate; \n H->TheCells[ Pos ].Element = Key; /* Probably need strcpy */ \n } \n} \n
Insertion will be seriously slowed down if there are too many deletions intermixed with insertions.
Although primary clustering is solved, secondary clustering occurs \u2013 that is, keys that hash to the same position will probe the same alternative cells.
"},{"location":"note/hash/#double-hashing","title":"Double Hashing","text":"\\(f ( i ) = i * hash_2( x )\\)
// Reverse a linked list\nq=h;\np=NUll;\nwhile(q){\n t = q->next;\n q->next = p;\n p=q;\n q=t;\n}\nh = p;\n\nLNode *reverse( LNode *head ){\n LNode *p0,*head1;\n head1=NULL;\n for(p0=head->next;p0;){\n LNode *temp=p0->next;//important!!!\n p0->next =head1;\n head1=p0;\n p0=temp;\n }\n return head1;\n}\n
* multilist * sparse matrix representation"},{"location":"note/sort/","title":"Sort","text":""},{"location":"note/sort/#simple-sort","title":"Simple Sort","text":""},{"location":"note/sort/#algorithm","title":"Algorithm","text":""},{"location":"note/sort/#bubble-sort","title":"Bubble sort","text":"
Basic
void bubbleSort(int arr[], int n) {\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < n - i - 1; j++) {\n if (arr[j] > arr[j + 1]) {\n int temp = arr[j];\n arr[j] = arr[j + 1];\n arr[j + 1] = temp;\n }\n }\n }\n}\n
Improvement
void bubbleSort_recursive(int a[],int begin,int end,int n){\n if(end>begin){\n int lastswap=-1;\n for(int i=begin;i<end;i++){\n if(a[i]>a[i+1]){\n int m=a[i];\n a[i]=a[i+1];\n a[i+1]=m; \n lastswap=i;\n }\n }\n end=lastswap;\n sort(a,begin,end,n);\n}\n\nvoid bubbleSort_iter (int a[], int n){\n int lastswap, end;\n for (int i=0;i<n-1;i++) {\n lastswap = -1;\n for (int j=0;j<end;j++) {\n if (a[j] > a[j + 1]) {\n exchange(&a[j], &a[j + 1]);\n lastswap = j;\n }\n }\n end = lastswap;\n }\n}\n
"},{"location":"note/sort/#selection-sort","title":"Selection sort","text":"void selectionSort(int arr[], int n) {\n int i, j, min_idx;\n // \u5916\u5c42\u5faa\u73af\u904d\u5386\u672a\u6392\u5e8f\u90e8\u5206\n for (i = 0; i <n-1;i++) {\n // \u5047\u8bbe\u5f53\u524d\u672a\u6392\u5e8f\u90e8\u5206\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e3a\u6700\u5c0f\n min_idx = i;\n // \u5185\u5c42\u5faa\u73af\u5728\u672a\u6392\u5e8f\u90e8\u5206\u4e2d\u627e\u5230\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n for (j=i+1;j<n;j++) {\n if (arr[j] < arr[min_idx]) {\n min_idx = j;\n }\n }\n // \u5c06\u627e\u5230\u7684\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u90e8\u5206\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4ea4\u6362\u4f4d\u7f6e\n swap(&arr[min_idx], &arr[i]);\n }\n}\n
"},{"location":"note/sort/#insertion-sort","title":"Insertion Sort","text":"void InsertionSort ( ElementType A[ ], int N ){\n int j,P;\n ElementType Tmp;\n for(P=1;P<N;P++){\n Tmp=A[p];\n for(j=P;j>0&&A[j-1]>Tmp;j--){\n A[j]=A[j-1];\n }\n A[j]=Tmp;\n }\n}\n
The worst case:Input \\(A[ \\ \\ ]\\) is in reverse order. \\(T( N ) = O( N^2 )\\)
"},{"location":"note/sort/#analysis","title":"Analysis","text":"\\(T ( N, I )\\) = \\(O( I+N )\\) where \\(I\\) is the number of inversions in the original array.
The average number of inversions in an array of N distinct numbers is \\(N ( N +1 ) / 4\\).
Original Shell Sequence: The original sequence suggested by Donald Shell was to use gaps of n/2
, n/4
, n/8
, ..., 1
. This is a simple and effective sequence, often called the \"original Shell sequence.\" It is easy to implement and works well in practice.
for (int gap = n/2; gap > 0; gap /= 2) {\n // Shell sort logic\n}\n
Knuth's Sequence: Donald Knuth suggested a formula for computing gaps, which is 3^k - 1
for k = 0, 1, 2, ...
. This sequence often performs well in practice.
for (int gap = 1; gap < n/3; gap = 3 * gap + 1) {\n // Shell sort logic\n}\n
Sedgewick's Sequence: Robert Sedgewick proposed a sequence that interleaves powers of 2 and 3: 1, 5, 19, 41, 109, 209, 505, ...
. It combines the simplicity of the original Shell sequence with the effectiveness of the Knuth sequence.
int sedgewick[] = {1, 5, 19, 41, 109, 209, 505, ...}; // Extend as needed\nint sedgewickIndex = 0;\nfor (int gap = sedgewick[sedgewickIndex]; gap > 0; gap = sedgewick[++sedgewickIndex]) {\n // Shell sort logic\n}\n
the terms are either of the form \\(9\\times 4^i -9\\times 2^i + 1\\) or \\(4^i -3\\times 2^i + 1\\). \\(T_{avg} ( N ) = O ( N^{7/6} )\\) and \\(T_{worst} ( N ) = O ( N^{4/3} )\\). void shellSort(int arr[], int n) {\n // ps:we can choose any ideal sequence suitable for given cases\n for (int gap = n/2; gap > 0; gap /= 2) {\n for (int i = gap; i < n; i++) {\n int temp = arr[i];\n int j;\n for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {\n arr[j] = arr[j - gap];\n }\n arr[j] = temp;\n }\n }\n}\n
"},{"location":"note/sort/#worst-case-analysis","title":"Worst-Case Analysis:","text":"The worst-case running time of Shellsort, using Shell's increments, is \\(O(N^2)\\). * example : 1 9 2 10 3 11 4 12 5 13 6 14
\\(h_k = 2^k -1\\) ---- consecutive increments have no common factors. The worst-case running time of Shellsort, using Hibbard's increments, is \\(O( N^{3/2})\\).
"},{"location":"note/sort/#heap-sort","title":"Heap Sort","text":""},{"location":"note/sort/#algorithm-1","title":"Algorithm 1:","text":"Algorithm 1:\n{\n BuildHeap( H ); //O(N)\n for (int i=0; i<N; i++ ){\n TmpH[ i ] = DeleteMin( H );O(logN)\n }\n for ( i=0; i<N; i++ ){\n H[ i ] = TmpH[ i ];\n }\n}\n
\\(T ( N ) = O ( N log N )\\) but space needed is doubled."},{"location":"note/sort/#algorithm-2","title":"Algorithm 2:","text":"Review
void PercolateDown( int p, PriorityQueue H ){ \n int temp=H->Elements[p];\n int parent,son;\n for(parent=p;parent*2<=H->Size;parent=son){\n son=parent*2;\n if(son<H->Size&&H->Elements[son]<H->Elements[son+1]){\n son++; \n }\n if(temp>=H->Elements[son]){break;} \n else{H->Elements[parent]=H->Elements[son];}\n }\n H->Elements[parent]=temp;\n}\n
void Heapsort( ElementType A[ ], int N ) \n{ int i; \n for ( i = N / 2; i >= 0; i - - ) /* BuildHeap */ {PercDown( A, i, N ); }\n for ( i = N - 1; i > 0; i - - ) { \n Swap( &A[ 0 ], &A[ i ] ); /* DeleteMax */ \n PercDown( A, 0, i ); \n } \n}\n
The average number of comparisons used to heapsort a random permutation of N distinct items is \\(2N log N - O( N log log N )\\). Although Heapsort gives the best average time, in practice it is slower than a version of Shellsort that uses Sedgewick's increment sequence.
"},{"location":"note/sort/#mergesort","title":"Mergesort","text":"void Mergesort( ElementType A[ ], int N );\nvoid MSort( ElementType A[ ], ElementType TmpArray[ ],int Left,int Right);\nvoid Merge( ElementType A[ ], ElementType TmpArray[ ],int Lpos,int Rpos, int RightEnd);\n\nvoid Mergesort( ElementType A[ ], int N ) {\n ElementType *TmpArray; /* need O(N) extra space */\n TmpArray = malloc( N * sizeof( ElementType ) ); \n if ( TmpArray != NULL ) { \n MSort( A, TmpArray, 0, N - 1 ); \n free( TmpArray ); \n } \n else FatalError( \"No space for tmp array!!!\" ); \n}\n\nvoid MSort( ElementType A[ ], ElementType TmpArray[ ],int Left, int Right){\n int Center; \n if ( Left < Right ) { /* if there are elements to be sorted */\n Center = ( Left + Right ) / 2; \n MSort( A, TmpArray, Left, Center ); /* T( N / 2 ) */\n MSort( A, TmpArray, Center + 1, Right ); /* T( N / 2 ) */\n Merge( A, TmpArray, Left, Center + 1, Right ); /* O( N ) */\n } \n} \n\n/* Lpos = start of left half, Rpos = start of right half */ \nvoid Merge( ElementType A[ ], ElementType TmpArray[ ], int Lpos, int Rpos, int RightEnd ) {\n int i, LeftEnd, NumElements, TmpPos; \n LeftEnd = Rpos - 1; \n TmpPos = Lpos; \n NumElements = RightEnd - Lpos + 1; \n while(Lpos<=LeftEnd && Rpos<=RightEnd){\n if(A[Lpos]<=A[Rpos]){TmpArray[TmpPos++]=A[Lpos++];}\n else{TmpArray[TmpPos++]=A[Rpos++];}\n }\n while(Lpos<=LeftEnd){TmpArray[ TmpPos++ ] = A[ Lpos++ ];}\n while(Rpos<=RightEnd){TmpArray[ TmpPos++ ] = A[ Rpos++ ];}\n for( i = 0; i < NumElements; i++, RightEnd - - ) \n /* Copy TmpArray back */ \n A[ RightEnd ] = TmpArray[ RightEnd ]; \n}\n/** version2 **/\nvoid Merge2( ElementType A[ ], ElementType TmpArray[ ], int Lpos, int Rpos, int RightEnd ) {\n int i, LeftEnd, NumElements, TmpPos; \n LeftEnd = Rpos - 1; \n TmpPos = Lpos; \n NumElements = RightEnd - Lpos + 1; \n for(int i=0;i<NumElements;i++){\n if((Lpos<=LeftEnd)&&(Rpos>RightEnd||A[Lpos]<=A[Rpos])){TmpArray[i]=A[Lpos++];}\n else{TmpArray[TmpPos++]=A[Rpos++];}\n }\n}\n/** version3 **/\nvoid CopyArray(int B[],int iBegin,int iEnd,int A[]);\nvoid Merge (int A[],int ileft,int iright,int iend,int B[]);\nvoid BottomupMergeSort(int A[],int B[],int n){\n for(int width=1;width<n;width=width*2){\n for(int i=0;i<n;i=i+2*width){\n Merge(A,i,min(i+width,n),min(i+2*width,n),B);\n }\n CopyArray(B,0,n,A);\n }\n}\n// (iBegin,iEnd]\nvoid Merge (int A[],int ileft,int iright,int iend,int B[]){\n int i=ileft,j=iright;\n for(int k=ileft;k<iend;k++){\n if(i<iright&&(j>=iend||A[i]<=A[j])){\n B[k]=A[i++];\n }\n else{B[k]=A[j++];}\n }\n}\n
"},{"location":"note/sort/#complexity","title":"Complexity","text":"\\(T(n)=2T(n/2)+O(n)\\) \u2192 \\(O(nlogn)\\) Note: Mergesort requires linear extra memory, and copying an array is slow. It is hardly ever used for internal sorting, but is quite useful for external sorting.
"},{"location":"note/sort/#quick-sort","title":"Quick Sort","text":""},{"location":"note/sort/#picking-the-pivot","title":"Picking the Pivot","text":"A Safe Maneuver: \\(Pivot = random\\ select\\ from\\ A[\\ \\ ]\\)
Median-of-Three Partitioning : \\(Pivot\\ = median ( left, center, right )\\)
Eliminates the bad case for sorted input and actually reduces the running time by about 5%.
"},{"location":"note/sort/#small-arrays","title":"Small Arrays","text":"Problem: Quicksort is slower than insertion sort for small N ( \\(\\le\\)20 ).
Solution : Cutoff when N gets small ( e.g. N = 10 ) and use other efficient algorithms (such as insertion sort).
"},{"location":"note/sort/#implementation","title":"Implementation","text":"void Quicksort( ElementType A[ ], int N ) \n{ \n Qsort( A, 0, N - 1 ); \n /* A: the array */\n /* 0: Left index */\n /* N \u2013 1: Right index */\n}\n\n/* Return median of Left, Center, and Right */ \n/* Order these and hide the pivot */ \nElementType Median3( ElementType A[ ], int Left, int Right ) \n{ \n int Center = ( Left + Right ) / 2; \n if ( A[ Left ] > A[ Center ] ) \n Swap( &A[ Left ], &A[ Center ] ); \n if ( A[ Left ] > A[ Right ] ) \n Swap( &A[ Left ], &A[ Right ] ); \n if ( A[ Center ] > A[ Right ] ) \n Swap( &A[ Center ], &A[ Right ] ); \n /* Invariant: A[ Left ] <= A[ Center ] <= A[ Right ] */ \n Swap( &A[ Center ], &A[ Right - 1 ] ); /* Hide pivot */ \n /* only need to sort A[ Left + 1 ] \u2026 A[ Right \u2013 2 ] */\n return A[ Right - 1 ]; /* Return pivot */ \n}\n\nvoid Qsort( ElementType A[ ], int Left, int Right ) \n{ int i, j; \n ElementType Pivot; \n if( Left + Cutoff <= Right ){ /* if the sequence is not too short */\n Pivot = Median3( A, Left, Right ); /* select pivot */\n i = Left; \n j = Right \u2013 1; /* why not set Left+1 and Right-2? */\n for( ; ; ) { \n while ( A[ ++i ] < Pivot ) { } /* scan from left */\n while ( A[ \u2013\u2013j ] > Pivot ) { } /* scan from right */\n if ( i < j ) {Swap( &A[ i ], &A[ j ] ); } /* adjust partition */\n else {break; } /* partition done */\n } \n Swap( &A[ i ], &A[ Right - 1 ] ); /* restore pivot */ \n Qsort( A, Left, i - 1 ); /* recursively sort left part */\n Qsort( A, i + 1, Right ); /* recursively sort right part */\n } /* end if - the sequence is long */\n\n else /* do an insertion sort on the short subarray */ \n {InsertionSort( A + Left, Right - Left + 1 );}\n}\n
"},{"location":"note/sort/#sorting-large-structures","title":"Sorting Large Structures","text":"Swapping large structures can be very much expensive.
Any algorithm that sorts by comparisons only must have a worst case computing time of \\(\\Omega( N log N )\\).
When sorting N distinct elements, there are \\(N!\\) (N factorial) different possible sorting outcomes. This arises from the numerous ways of arranging the elements, with \\(N\\) choices for the first element, \\(N-1\\) choices for the second, and so forth, yielding a total of \\(N!\\).
In the context of decision trees representing the decision-making process of comparison-based sorting algorithms, the number of leaves in the tree is at least \\(N!\\). Each leaf node signifies a distinct sorting outcome.
The relationship between the height of the decision tree \\((k)\\) and complete binary trees is emphasized. For a complete binary tree with \\(k\\) levels, it can have at most \\(2^{k-1}\\) leaves. Combining this with the earlier discussion, the inequality \\(N! \u2264 2^{k-1}\\) is established, where k is the height of the decision tree.
Expressing the relationship between height and N! using logarithms, we find that \\(k \u2265 log(N!) + 1\\).
These insights delineate theoretical limits for comparison-based sorting algorithms and underscore the significance of N log N as the theoretical lower bound for their time complexity.
"},{"location":"note/sort/#bucket-sort-and-radix-sort","title":"Bucket Sort and Radix Sort","text":""},{"location":"note/sort/#bucket-sort-tnmomn","title":"Bucket Sort \\(T(N,M)=O(M+N)\\)","text":"Algorithm{\n initialize count[ ];\n while (read in a student\u2019s record)\n insert to list count[stdnt.grade];\n for (i=0; i<M; i++) {\n if (count[i])\n output list count[i];\n }\n}\n
* What if \\(M >> N\\) ?"},{"location":"note/sort/#radix-sort","title":"Radix Sort","text":""},{"location":"note/sort/#stableness","title":"Stableness","text":""},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#sparse-matrix","title":"SPARSE MATRIX","text":"
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).
#include <iostream>\nusing namespace std;\n\nint main()\n{\n // Assume 4x5 sparse matrix\n int sparseMatrix[4][5] =\n {\n {0 , 0 , 3 , 0 , 4 },\n {0 , 0 , 5 , 7 , 0 },\n {0 , 0 , 0 , 0 , 0 },\n {0 , 2 , 6 , 0 , 0 }\n };\n\n int size = 0;\n for (int i = 0; i < 4; i++)\n for (int j = 0; j < 5; j++)\n if (sparseMatrix[i][j] != 0)\n size++;\n\n // number of columns in compactMatrix (size) must be\n // equal to number of non - zero elements in\n // sparseMatrix\n int compactMatrix[3][size];\n\n // Making of new matrix\n int k = 0;\n for (int i = 0; i < 4; i++)\n for (int j = 0; j < 5; j++)\n if (sparseMatrix[i][j] != 0)\n {\n compactMatrix[0][k] = i;\n compactMatrix[1][k] = j;\n compactMatrix[2][k] = sparseMatrix[i][j];\n k++;\n } \n\n for (int i=0; i<3; i++)\n {\n for (int j=0; j<size; j++)\n cout <<\" \"<< compactMatrix[i][j];\n cout <<\"\\n\";\n }\n return 0;\n}\n
#include<stdio.h>\n#include<stdlib.h>\n\n// Node to represent sparse matrix\nstruct Node\n{\n int value;\n int row_position;\n int column_postion;\n struct Node *next;\n};\n\n// Function to create new node\nvoid create_new_node(struct Node** start, int non_zero_element,int row_index, int column_index )\n{\n struct Node *temp, *r;\n temp = *start;\n if (temp == NULL)\n {\n // Create new node dynamically\n temp = (struct Node *) malloc (sizeof(struct Node));\n temp->value = non_zero_element;\n temp->row_position = row_index;\n temp->column_postion = column_index;\n temp->next = NULL;\n *start = temp;\n }\n else\n {\n while (temp->next != NULL)\n temp = temp->next;\n // Create new node dynamically\n r = (struct Node *) malloc (sizeof(struct Node));\n r->value = non_zero_element;\n r->row_position = row_index;\n r->column_postion = column_index;\n r->next = NULL;\n temp->next = r;\n }\n}\n// This function prints contents of linked list\n// starting from start\nvoid PrintList(struct Node* start)\n{\n struct Node *temp, *r, *s;\n temp = r = s = start;\n\n printf(\"row_position: \");\n while(temp != NULL){\n printf(\"%d \", temp->row_position);\n temp = temp->next;\n }\n printf(\"\\n\");\n printf(\"column_postion: \");\n while(r != NULL){\n printf(\"%d \", r->column_postion);\n r = r->next;\n }\n printf(\"\\n\");\n printf(\"Value: \");\n while(s != NULL){\n printf(\"%d \", s->value);\n s = s->next;\n }\n printf(\"\\n\");\n}\n// Driver of the program\nint main(){\n// Assume 4x5 sparse matrix\n int sparseMatric[4][5] =\n {\n {0 , 0 , 3 , 0 , 4 },\n {0 , 0 , 5 , 7 , 0 },\n {0 , 0 , 0 , 0 , 0 },\n {0 , 2 , 6 , 0 , 0 }\n };\n /* Start with the empty list */\n struct Node* start = NULL;\n for (int i = 0; i < 4; i++)\n for (int j = 0; j < 5; j++)\n // Pass only those values which are non - zero\n if (sparseMatric[i][j] != 0)\n create_new_node(&start, sparseMatric[i][j], i, j);\n PrintList(start);\n return 0;\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#stack","title":"STACK","text":""},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#1adt","title":"1.ADT","text":""},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#2implementations","title":"2.Implementations","text":"Algorithm {\n Make an empty stack S;\n while (read in a character c) {\n if (c is an opening symbol)\n Push(c, S);\n else if (c is a closing symbol) {\n if (S is empty) { ERROR; exit; }\n else { /* stack is okay */\n if (Top(S) doesn\u2019t match c) { ERROR, exit; }\n else Pop(S);\n } /* end else-stack is okay */\n } /* end else-if-closing symbol */\n } /* end while-loop */ \n if (S is not empty) ERROR;\n}\n
#include <stdio.h>\n#include <stdbool.h>\n#include <string.h>\n\n#define STACKSIZE 30 \nchar stack[STACKSIZE]; // Global, but let's go with it\nint top = 0;\n\nvoid push(char x) {\n if (top == STACKSIZE)\n puts(\"Stack Full\");\n else\n stack[top++] = x;\n}\n\nbool popExpect(char c) { // compare expected char on top with passed char\n return top && c == stack[--top];\n}\n\nbool chk(const char *str) {\n char *cp, pairs[] = \"(){}[]\"; // three important pairs\n bool isGood = true; // optimism\n\n for (int i = 0; isGood && str[i]; i++)\n // is this char one of the \"special\" ones?\n if ((cp = strchr(pairs, str[i])) != NULL) {\n int off = cp - pairs;\n // because \"paired\" 0,2,4 are open, 1,3,5 are close\n if (off % 2 == 0) // opening\n push(cp[1]); // push the close that matches this open\n else // special closing\n isGood = popExpect(str[i]); // does this close match?\n }\n\n return isGood && top == 0;\n}\n\nint main() {\n const char *s1 = \"(foobar)({}){bar}[[[(foo)]]]\"; // balanced\n const char *s2 = \"(foobar)({}){ { bar}[[[(foo)]]]\"; // unbalanced open\n const char *s3 = \"(foobar)({}){ ] bar}[[[(foo)]]]\"; // unbalanced close\n\n puts(chk(s1) ? \"Balanced\" : \"Unbalanced\");\n puts(chk(s2) ? \"Balanced\" : \"Unbalanced\");\n puts(chk(s3) ? \"Balanced\" : \"Unbalanced\");\n\n return 0;\n}\n
Calculation
Prefix
Infix\u300cConvert To Postfix\u300d
#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n// Function to return precedence of operators\nint prec(char c) {\n if (c == '^')\n return 3;\n else if (c == '/' || c == '*')\n return 2;\n else if (c == '+' || c == '-')\n return 1;\n else\n return -1;\n}\n\n// Function to return associativity of operators\nchar associativity(char c) {\n if (c == '^')\n return 'R';\n return 'L'; // Default to left-associative\n}\n\n// Structure to represent stack\nstruct Stack {\n int top;\n unsigned capacity;\n char* array;\n};\n\n// Function to initialize a stack\nstruct Stack* createStack(unsigned capacity) {\n struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));\n stack->capacity = capacity;\n stack->top = -1;\n stack->array = (char*)malloc(stack->capacity * sizeof(char));\n return stack;\n}\n\n// Function to check if the stack is full\nint isFull(struct Stack* stack) {\n return stack->top == stack->capacity - 1;\n}\n\n// Function to check if the stack is empty\nint isEmpty(struct Stack* stack) {\n return stack->top == -1;\n}\n\n// Function to push an element to the stack\nvoid push(struct Stack* stack, char item) {\n if (isFull(stack)) {\n printf(\"Stack Full\\n\");\n return;\n }\n stack->array[++stack->top] = item;\n}\n\n// Function to pop an element from the stack\nchar pop(struct Stack* stack) {\n if (isEmpty(stack)) {\n return '$'; // Assuming '$' as an invalid character\n }\n return stack->array[stack->top--];\n}\n\n// The main function to convert infix expression\n// to postfix expression\nvoid infixToPostfix(char* exp) {\n struct Stack* stack = createStack(strlen(exp));\n char result[strlen(exp)];\n int resultIndex = 0;\n\n for (int i = 0; exp[i] != '\\0'; i++) {\n char c = exp[i];\n\n // If the scanned character is\n // an operand, add it to the output string.\n if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))\n result[resultIndex++] = c;\n\n // If the scanned character is an\n // \u2018(\u2018, push it to the stack.\n else if (c == '(')\n push(stack, '(');\n\n // If the scanned character is an \u2018)\u2019,\n // pop and add to the output string from the stack\n // until an \u2018(\u2018 is encountered.\n else if (c == ')') {\n while (stack->top != -1 && stack->array[stack->top] != '(') {\n result[resultIndex++] = pop(stack);\n }\n pop(stack); // Pop '('\n }\n\n // If an operator is scanned\n else {\n while (!isEmpty(stack) && prec(exp[i]) < prec(stack->array[stack->top]) ||\n !isEmpty(stack) && prec(exp[i]) == prec(stack->array[stack->top]) &&\n associativity(exp[i]) == 'L') {\n result[resultIndex++] = pop(stack);\n }\n push(stack, c);\n }\n }\n\n // Pop all the remaining elements from the stack\n while (!isEmpty(stack)) {\n result[resultIndex++] = pop(stack);\n }\n\n result[resultIndex] = '\\0'; // Null-terminate the result string\n\n printf(\"%s\\n\", result);\n}\n\n// Driver code\nint main() {\n char exp[] = \"a+b*(c^d-e)^(f+g*h)-i\";\n // Function call\n infixToPostfix(exp);\n return 0;\n}\n
Note: a \u2013 b \u2013 c will be converted to a b \u2013 c \u2013. However, \\(2^{2^{3}}\\)must be converted to 2 2 3 ^ ^ , not 2 2 ^ 3 ^ since exponentiation associates right to left.
Observe that when ' ( ' is not in the stack, its precedence is the highest; but when it is in the stack, its precedence is the lowest. Define in-stack precedence and incoming precedence for symbols, and each time use the corresponding precedence for comparison.
Note: a \u2013 b \u2013 c will be converted to a b \u2013 c \u2013. However, \\(2^{2^{3}}\\)must be converted to 2 2 3 ^ ^ , not 2 2 ^ 3 ^ since exponentiation associates right to left.
Postfix \u300cEasier To Calculate\u300d
ps:Tail Recursion! -- compliler will remove the recursion.
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#the-queue-adt","title":"The Queue ADT","text":"A queue is a First-In-First-Out (FIFO) list, that is, an ordered list in which insertions take place at one end and deletions take place at the opposite end.
Linked list implementation
Array Implementation
struct QueueRecord {\n int Capacity ; /* max size of queue */\n int Front; /* the front pointer */\n int Rear; /* the rear pointer */\n int Size; /* Optional - the current size of queue */\n ElementType *Array; /* array for queue elements */\n } ; \n
FirstChild-NextSibling \u8868\u793a\u6cd5
\u8bb0\u5f55\u7b2c\u4e00\u4e2a\u5b50\u8282\u70b9\u548c\u4e0b\u4e00\u4e2a\u5144\u5f1f\u8282\u70b9
\u56e0\u4e3a\u4e00\u68f5\u6811\u7684\u513f\u5b50\u987a\u5e8f\u4e0d\u5b9a\uff0c\u6240\u4ee5\u4e00\u68f5\u6811\u7684\u8868\u793a\u65b9\u5f0f\u4e0d\u552f\u4e00
struct TreeNode {\n ElementType Element;\n PtrToNode FirstChild;\n PtrToNode NextSibling;FirstChildFirfsads\n};\ntypedef struct TreeNode *PtrToNode;\n
https://blog.csdn.net/m0_73070900/article/details/130227715
Preorder\uff1aroot-left-right
Postorder\uff1aleft-right-root
inorder:left-root-right
level order:
void levelorder(tree_ptr tree) {\n enqueue(tree);\n while (queue is not empty) {\n visit(T = dequeue());\n for (each child C of T) \n enqueue(C);\n }\n}\n
// \u4e8c\u53c9\u6811\u7684\u5b9e\u73b0\uff08C\u8bed\u8a00\uff09\n// \u94fe\u8868\uff0c\u9012\u5f52\u5b9e\u73b0\ntypedef char Elementtype; // \u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u6839\u636e\u9700\u8981\u81ea\u884c\u5b9a\u5236\ntypedef struct TreeNode * Node; // Node\u76f8\u5f53\u4e8estruct treeNode *\n// \u5b9a\u4e49\u6570\u8282\u70b9\u7ed3\u6784\ntypedef struct TreeNode {\n Elementtype Element;\n Node left; // \u6811\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\n Node right; // \u6811\u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\n}TREE,*PTREE;\n\nvoid CreatTree(PTREE *); // \u6811\u7684\u5148\u5e8f\u521b\u5efa\u51fd\u6570\nvoid PreOrderTree(PTREE ); // \u6811\u7684\u524d\u5e8f\u904d\u5386\u51fd\u6570\nvoid InOrderTree(PTREE ); // \u6811\u7684\u4e2d\u5e8f\u904d\u5386\nvoid PostOrderTree(PTREE ); // \u6811\u7684\u540e\u5e8f\u904d\u5386\nvoid LeafOfTree(PTREE ); // \u6253\u5370\u6811\u7684\u53f6\u5b50\u8282\u70b9\u51fd\u6570\nint Get_Leaf_Num(PTREE ); // \u83b7\u53d6\u6811\u53f6\u5b50\u8282\u70b9\u4e2a\u6570\nint Get_Height(PTREE ); // \u83b7\u53d6\u6811\u7684\u9ad8\u5ea6\n\nint main() {\n PTREE Root; \n printf(\"\u8bf7\u5148\u5e8f\u8f93\u5165\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u636e\uff1a \");\n CreatTree(&Root); \n printf(\"\\n\u524d\u5e8f\u904d\u5386\u7ed3\u679c\u4e3a\uff1a\");\n PreOrderTree(Root); \n printf(\"\\n\u4e2d\u5e8f\u904d\u5386\u7ed3\u679c\u4e3a\uff1a\");\n InOrderTree(Root);\n printf(\"\\n\u540e\u5e8f\u904d\u5386\u7ed3\u679c\u4e3a\uff1a\");\n PostOrderTree(Root);\n printf(\"\\n\u6253\u5370\u53f6\u5b50\u8282\u70b9\u4e3a\uff1a\");\n LeafOfTree(Root);\n printf(\"\\n\u53f6\u5b50\u8282\u70b9\u4e2a\u6570\u4e3a\uff1a%d\", Get_Leaf_Num(Root));\n printf(\"\\n\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\u4e3a\uff1a%d\", Get_Height(Root));\n printf(\"\\n\");\n return 0;\n}\n// \u5b9a\u4e49\u6811\u5148\u5e8f\u521b\u5efa\u51fd\u6570\nvoid CreatTree(PTREE *ROOT){\n char val = 0;\n val = getchar();\n if(wal=='*'){*ROOT=NULL;}\n else{\n (*Root)=(PTREE)malloc(sizeof(TREE));\n if ((*Root) == NULL) {\n printf(\"\u521b\u5efa\u8282\u70b9\u5931\u8d25\uff0c\u65e0\u6cd5\u5206\u914d\u53ef\u7528\u5185\u5b58...\");\n exit(-1);\n }\n else{\n (*Root)->Element =val;\n CreatTree(&(*Root)->left);\n CreatTree(&(*Root)->right); \n }\n }\n\n}\n\n// \u6811\u7684\u524d\u5e8f\u904d\u5386\u51fd\u6570\u5b9a\u4e49\nvoid PreOrderTree(PTREE Root) {\n if (Root == NULL)\n return;\n else {\n putchar(Root->Element);\n PreOrderTree(Root->left);\n PreOrderTree(Root->right);\n }\n}\n\n// \u6811\u7684\u4e2d\u5e8f\u904d\u5386\u51fd\u6570\u5b9a\u4e49\nvoid InOrderTree(PTREE Root) {\n if (Root == NULL)\n return;\n else {\n InOrderTree(Root->left);\n putchar(Root->Element);\n InOrderTree(Root->right);\n }\n}\n// \u6811\u7684\u540e\u5e8f\u904d\u5386\u51fd\u6570\u5b9a\u4e49\nvoid PostOrderTree(PTREE Root) {\n if (Root==NULL) \n return ;\n else{\n PostOrderTree(Root->left);\n PostOrderTree(Root->right);\n putchar( Root->Element);\n }\n}\n//\u6811\u7684\u540e\u5e8f\u904d\u5386\u7684\u5faa\u73af\u5f62\u5f0f\nIterative Program \nvoid iter_postorder\uff08tree_ptr_tree\uff09{\n stack S = CreateStack(MAX_SIZE);\n for(;;){\n\n }\n}\n// \u6253\u5370\u6811\u7684\u53f6\u5b50\u8282\u70b9\u51fd\u6570\u5b9a\u4e49\nvoid LeafOfTree(PTREE Tree) {\n if (Tree == NULL) \n return ;\n else {\n if (Tree->left == NULL&&Tree->right == NULL)\n putchar(Tree->Element);\n else {\n LeafOfTree(Tree->left);\n LeafOfTree(Tree->right);\n }\n } \n}\n// \u83b7\u53d6\u6811\u7684\u53f6\u5b50\u8282\u70b9\u4e2a\u6570\u51fd\u6570\u5b9a\u4e49\nint Get_Leaf_Num(PTREE Tree) {\n if (Tree == NULL)\n return 0;\n if (Tree->left == NULL&&Tree->right == NULL)\n return 1;\n //\u9012\u5f52\u6574\u4e2a\u6811\u7684\u53f6\u5b50\u8282\u70b9\u4e2a\u6570 = \u5de6\u5b50\u6811\u53f6\u5b50\u8282\u70b9\u7684\u4e2a\u6570 + \u53f3\u5b50\u6811\u53f6\u5b50\u8282\u70b9\u7684\u4e2a\u6570\n return Get_Leaf_Num(Tree->left) + Get_Leaf_Num(Tree->right);\n}\n// \u83b7\u53d6\u6811\u9ad8\u7684\u51fd\u6570\u5b9a\u4e49\nint Get_Height(PTREE Tree) {\n int Height = 0;\n if (Tree == NULL)\n return 0;\n //\u6811\u7684\u9ad8\u5ea6 = max(\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6) + 1\n else\n {\n int L_Height = Get_Height(Tree->left);\n int R_Height = Get_Height(Tree->right);\n Height = L_Height >= R_Height ? L_Height + 1 : R_Height + 1;\n }\n return Height;\n}\n
#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n\n// \u5b9a\u4e49\u4e8c\u53c9\u6811\u7ed3\u70b9\nstruct TreeNode {\n int val;\n struct TreeNode* left;\n struct TreeNode* right;\n};\n\n// \u521b\u5efa\u4e00\u4e2a\u4e8c\u53c9\u6811\u7ed3\u70b9\nstruct TreeNode* createNode(int val) {\n struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));\n newNode->val = val;\n newNode->left = NULL;\n newNode->right = NULL;\n return newNode;\n}\n// \u7ed3\u6784\u4f53\u6a21\u62df\u6808\nstruct TreeNodeStack {\n struct TreeNode* data;\n struct TreeNodeStack* next;\n};\n\nstruct TreeNodeStack* createStackNode(struct TreeNode* node) {\n struct TreeNodeStack* stackNode = (struct TreeNodeStack*)malloc(sizeof(struct TreeNodeStack));\n stackNode->data = node;\n stackNode->next = NULL;\n return stackNode;\n}\n\nvoid push(struct TreeNodeStack** stack, struct TreeNode* node) {\n struct TreeNodeStack* stackNode = createStackNode(node);\n stackNode->next = *stack;\n *stack = stackNode;\n}\n\nstruct TreeNode* pop(struct TreeNodeStack** stack) {\n if (*stack == NULL) {\n return NULL;\n }\n struct TreeNodeStack* temp = *stack;\n *stack = (*stack)->next;\n struct TreeNode* node = temp->data;\n free(temp);\n return node;\n}\n\nbool isEmpty(struct TreeNodeStack* stack) {\n return stack == NULL;\n}\n\n// \u4e2d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u5e76\u8fd4\u56de\u7ed3\u679c\u4f5c\u4e3a\u6570\u7ec4\nint* inorderTraversal(struct TreeNode* root, int* returnSize) {\n struct TreeNodeStack* stack = NULL;\n int* result = (int*)malloc(sizeof(int));\n int resultSize = 0;\n struct TreeNode* current = root;\n while (current != NULL || !isEmpty(stack)) {\n while (current != NULL) {\n push(&stack, current);\n current = current->left;\n }\n current = pop(&stack);\n result = (int*)realloc(result, (resultSize + 1) * sizeof(int));\n result[resultSize] = current->val;\n resultSize++;\n current = current->right;\n }\n\n *returnSize = resultSize;\n return result;\n}\n\n// \u4e3b\u51fd\u6570\nint main() {\n struct TreeNode* root = createNode(1);\n root->right = createNode(2);\n root->right->left = createNode(3);\n\n printf(\"Inorder Traversal: \");\n int resultSize;\n int* result = inorderTraversal(root, &resultSize);\n for (int i = 0; i < resultSize; i++) {\n printf(\"%d \", result[i]);\n }\n free(result);\n return 0;\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#tree_1","title":"\u521b\u5efaTREE","text":"BiTree* postInTree(int post[],int in[],int n) {\n if (n<=0) return nullptr;\n int i=0;\n while (post[n-1]!=in[i]) i++; //i\u4e5f\u6b63\u597d\u662f\u5de6\u5b50\u6811\u8282\u70b9\u6570\n BiTree* nw=new BiTree;\n nw->data=in[i];\n nw->lc=postInTree(post,in,i); //\u5efa\u5de6\u5b50\u6811\n nw->rc=postInTree(post+i,in+i+1,n-i-1); //\u5efa\u53f3\u5b50\u6811\n return nw;\n}\n
BiTree* preInTree2(int pre[],int in[],int n) { //\u8fd9\u662f\u7b14\u8bb0\u4e0a\u7684\u6211\u7684\u65b9\u6cd5,n\u662f\u5f53\u524d\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\n if (n<=0) return nullptr;\n int i=0;\n while (in[i]!=pre[0]) i++; //\u6b64\u65f6i\u6b63\u597d\u662f\u5de6\u5b50\u6811\u8282\u70b9\u6570\u3002\u5148\u5e8f\u904d\u5386\u7684\u9996\u5143\u7d20\u4e00\u5b9a\u662f\u6839\u8282\u70b9\n BiTree* nw=new BiTree;\n nw->data=in[i];\n nw->lc=preInTree2(pre+1,in,i); //\u5efa\u5de6\u5b50\u6811\uff0c\u5de6\u5b50\u6811\u8282\u70b9i\u4e2a\n nw->rc=preInTree2(pre+i+1,in+i+1,n-i-1); //\u5efa\u53f3\u5b50\u6811\uff0c\u53f3\u5b50\u6811\u8282\u70b9n-i-1\u4e2a\n return nw;\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#application","title":"Application","text":"static void ListDir ( DirOrFile D, int Depth )\n{\n if ( D is a legitimate entry ) {\n PrintName (D, Depth );\n if ( D is a directory )\n for (each child C of D )\n ListDir ( C, Depth + 1 );\n }\n}\n
Calculating the size of the directory\u300cTypical Postorder Traversal\u300d
\u7ebf\u7d22\u4e8c\u53c9\u6811 Threader Binary Tree Inorder
Rule 1: If Tree->Left is null, replace it with a pointer to the inorder predecessor of Tree.
Rule 2: If Tree->Right is null, replace it with a pointer to the inorder successor of Tree.
Rule 3: There must not be any loose threads. Therefore a threaded binary tree must have a head node of which the left child points to the first node.
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#binary-search-tree","title":"Binary Search Tree","text":"SearchTree Delete(ElementType X, SearchTree T) {\n Position TmpCell;\n if (T == NULL) Error(\"not found\");\n else if (X < T->Element) T->Left = Delete(X, T->Left);\n else if (x > T->Element) T->Right = Delete(X, T->Right);\n else {\n if (T->Left && T->Right) {\n TmpCell = FindMin(T->Right);\n T->Element = TmpCell->Element;\n T->Right = Delete(T->Element, T->Right);\n } else {\n TmpCell = T;\n if (T->Left == NULL) T = T->Right;\n else if (T->Right == NULL) T = T->Left;\n free(TmpCell);\n }\n }\n return T;\n}\n
\\(C_{n}=\\sum_{i=1}^{n} C_{i}C{n-i-1}\\)
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#complete-binary-tree","title":"\u5b8c\u5168\u4e8c\u53c9\u6811\uff08complete binary tree\uff09","text":""},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#basic-properties_2","title":"Basic Properties","text":"\u6240\u6709\u53f6\u8282\u70b9\u90fd\u5728\u76f8\u90bb\u7684\u4e24\u5c42\u4e0a\u7684\u4e8c\u53c9\u6811
Basic Properties:
\u9664\u4e86\u6700\u540e\u4e00\u5c42\uff0c\u6bcf\u4e00\u5c42\u90fd\u662f\u6ee1\u7684
\u6700\u540e\u4e00\u5c42\u7684\u8282\u70b9\u90fd\u9760\u5de6\u6392\u5217
\\(2^{h}\\) ~ \\(2^{h+1}-1\\) nodes\\((0,1,2,3- height = 3)\\)
\\(h=O(log(n))\\)
void Insert(ElementType X, MaxHeap H) {\n if (IsFull(H)) {\n Error(\"Full Heap\");\n return;\n }\n int i = ++H->Size;\n for (; H->Elements[i/2] < X; i /= 2) {\n H->Elements[i] = H->Elements[i/2];\n }\n H->Elements[i] = X;\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#delete","title":"DELETE","text":"ElementType DeleteMax(MaxHeap H) {\n if (IsEmpty(H)) {\n Error(\"Empty Heap\");\n return H->Elements[0];\n }\n int i, Child;\n ElementType MaxElement, LastElement;\n MaxElement = H->Elements[1];\n LastElement = H->Elements[H->Size--];\n for (i = 1; i * 2 <= H->size; i = Child) {\n Child = i * 2;\n if (Child != H->Size && H->Elements[Child + 1] > H->Elements[Child]) {\n Child++;\n }\n if (LastElement < H->Elements[Child]) {\n H->Elements[i] = H->Elements[Child];\n } else {\n break;\n }\n }\n H->Elements[i] = LastElement;\n return MaxElement;\n}\n
Question: how to make it a natural stop without using \\(Child != H->size\\)
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#buildheap-h","title":"BuildHeap ( H )","text":"For the perfect binary tree of height h containing \\(2^{h+1}-1\\) nodes, the sum of the heights of the nodes is \\(2^{h+1}-1 -(h + 1)\\).
\\(T ( N ) = O ( N )\\)
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#the-dynamic-equivalence-problem","title":"The Dynamic Equivalence Problem","text":"A dynamic graph generally refers to a graph that keeps on changing its configuration. Let\u2019s deep dive into it using an example:
NOTE:UNION is the union of the head of one head to another(thus for a random element x,always find the \"head\" element Find(x) and modify s[Find(x)]
void SetUnion ( DisjSet S, \n SetType Rt1, \n SetType Rt2 )\n{ S [ Rt2 ] = Rt1 ; }\n
SetType Find ( ElementType X, DisjSet S )\n{ for ( ; S[X] > 0; X = S[X] ) ;\n return X ;\n}\n
Algorithm using union-find operations\n{ Initialize Si = { i } for i = 1, ..., 12 ;\n for ( k = 1; k <= 9; k++ ) { /* for each pair i \u7b49\u4ef7j \u5373\u5982\u679c\u7ed9\u51fa\u4e5d\u7ec4\u7b49\u4ef7\u5173\u7cfb*/\n if ( Find( i ) != Find( j ) )\n SetUnion( Find( i ), Find( j ) );\n }\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#smart-union-algorithms","title":"Smart Union Algorithms","text":"The rank of a node generally refers to the distance (the number of nodes including the leaf node) between the furthest leaf node and the current node. Basically rank includes all the nodes beneath the current node.
void make_set(int v) {\n parent[v] = v;\n rank[v] = 0;\n}\nvoid union_sets(int a, int b) {\n a = find_set(a);\n b = find_set(b);\n if (a != b) {\n if (rank[a] < rank[b])\n swap(a, b);\n parent[b] = a;\n if (rank[a] == rank[b])\n rank[a]++;\n }\n}\n
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#union-by-size","title":"Union by size","text":"void make_set(int v) {\n parent[v] = v;\n size[v] = 1;\n}\nvoid union_sets(int a, int b) {\n a = find_set(a);\n b = find_set(b);\n if (a != b) {\n if (size[a] < size[b])\n swap(a, b);\n parent[b] = a;\n size[a] += size[b];\n }\n}\n
Let T be a tree created by union-by-size with N nodes, then \\((Height(T) \\le \\lfloor log_2N \\rfloor + 1)\\)
Proof: By induction. (Each element can have its set name changed at most. \\(log_2N\\)
times.\uff09\u300cUnion by size\u300d
First , all note are at depth 0 at the very beginning
Every time,we do Union:the node was set on a new tree whose size is at least twice its size
So a node from either set can only incrase the depth by \\(1\\)
For n nodes ,every node can have its set name changed at most \\(log_2N\\) times.
Thus,Time complexity for Find is \\(O (l o g N )\\)
The base case K=0 is easy, since a tree of height 0 always has just 1 node
Suppose the claim is true for h=k. Now consider a union-by-height tree of height k+1.
There must have been a union that brought two trees together and increased the height of one of them from k to k+1.
Let those two trees (at the time of that union) be T1 and T2. We know that both T1 and T2 were of height k before the union.
Now we can apply the induction hypothesis: the trees T1 and T2 each have at least \\(2^k\\) nodes. Thus, the unioned tree has at least \\(2^k + 2^k = 2^{k+1}\\)nodes.
Time complexity of \\(N\\) Union and \\(M\\) Find operations is now. \\(O( N + M log_2N)\\)
We can set \\(S [ Root ] = \u2013 size\\), thus we don't need the array size[ ]
"},{"location":"note/%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#path-compression","title":"Path Compression","text":"Basically, connecting each node in a particular path to its ultimate parent refers to path compression. Let\u2019s understand it using the following illustration
SetType Find ( ElementType X, DisjSet S )\n{\n if ( S[ X ] <= 0 ) return X;\n else return S[ X ] = Find( S[ X ], S );//IMPORTANT\uff01\uff01\uff01\n}\n//The simple implementation does what was intended: first find the representative of the set (root vertex), and then in the process of stack unwinding the visited nodes are attached directly to the representative.\nSetType Find ( ElementType X, DisjSet S )\n{ ElementType root, trail, lead;\n for ( root = X; S[ root ] > 0; root = S[ root ] )\n ; /* find the root */\n for ( trail = X; trail != root; trail = lead ) {\n lead = S[ trail ] ; \n S[ trail ] = root ; \n } /* collapsing */\n return root ;\n}\n
NOTE
#include <stdio.h>\n#include <stdlib.h>\nstruct DisjointSet {\n int *rank, *parent, *size;\n int n;\n};\nstruct DisjointSet* createDisjointSet(int n) {\n struct DisjointSet* ds = (struct DisjointSet*)malloc(sizeof(struct DisjointSet));\n ds->n = n;\n ds->rank = (int*)malloc((n + 1) * sizeof(int));\n ds->parent = (int*)malloc((n + 1) * sizeof(int));\n ds->size = (int*)malloc((n + 1) * sizeof(int));\n for (int i = 0; i <= n; i++) {\n ds->rank[i] = 0;\n ds->parent[i] = i;\n ds->size[i] = 1;\n }\n return ds;\n}\nint findUPar(struct DisjointSet* ds, int node) {\n if (node == ds->parent[node])\n return node;\n ds->parent[node] = findUPar(ds, ds->parent[node]);//PATH COMPRESSION\n return ds->parent[node];\n}\nvoid unionByRank(struct DisjointSet* ds, int u, int v) {\n int ulp_u = findUPar(ds, u);\n int ulp_v = findUPar(ds, v);\n if (ulp_u == ulp_v)\n return;\n if (ds->rank[ulp_u] < ds->rank[ulp_v]) {\n ds->parent[ulp_u] = ulp_v;\n } else if (ds->rank[ulp_v] < ds->rank[ulp_u]) {\n ds->parent[ulp_v] = ulp_u;\n } else {\n ds->parent[ulp_v] = ulp_u;\n ds->rank[ulp_u]++;\n }\n}\nvoid unionBySize(struct DisjointSet* ds, int u, int v) {\n int ulp_u = findUPar(ds, u);\n int ulp_v = findUPar(ds, v);\n if (ulp_u == ulp_v)\n return;\n if (ds->size[ulp_u] < ds->size[ulp_v]) {\n ds->parent[ulp_u] = ulp_v;\n ds->size[ulp_v] += ds->size[ulp_u];\n } else {\n ds->parent[ulp_v] = ulp_u;\n ds->size[ulp_u] += ds->size[ulp_v];\n }\n}\nint main() {\n struct DisjointSet* ds = createDisjointSet(7);\n unionBySize(ds, 1, 2);\n unionBySize(ds, 2, 3);\n unionBySize(ds, 4, 5);\n unionBySize(ds, 6, 7);\n unionBySize(ds, 5, 6);\n if (findUPar(ds, 3) == findUPar(ds, 7)) {\n printf(\"Same\\n\");\n } else {\n printf(\"Not same\\n\");\n }\n unionBySize(ds, 3, 7);\n if (findUPar(ds, 3) == findUPar(ds, 7)) {\n printf(\"Same\\n\");\n } else {\n printf(\"Not same\\n\");\n }\n free(ds->rank);\n free(ds->parent);\n free(ds->size);\n free(ds);\n return 0;\n}\n
"},{"location":"note/%E6%9C%80%E5%A4%A7%E5%AD%90%E5%88%97%E5%92%8C/","title":"Maximum Subsequence Sum Problem","text":""},{"location":"note/%E6%9C%80%E5%A4%A7%E5%AD%90%E5%88%97%E5%92%8C/#maximum-subsequence-sum-problem","title":"Maximum Subsequence Sum Problem","text":"
O(\\(N^3\\)) \u300c\u57fa\u672c\u601d\u8def\uff0c\u9010\u4e2a\u5217\u4e3e\uff0c\u7565\u300d
O(\\(N^2\\)) \u300c\u4f18\u5316\u4e00\u4e0b\u5217\u4e3e\uff0c\u7565\u300d
divide & conquer O(N logN)
static int\nMaxSubSum(const int A[],int left,int right){\n int MaxLeftSum, MaxRightSum;\n int MaxLeftBorderSum, MaxRightBorderSum; \n int LeftBorderSum, RightBorderSum;\n int Center, i;\n\n if( left == right ){\n if(A[left]>0){\n return A[left];\n }\n else{\n return 0;\n }\n }\n Center = ( Left +Right ) / 2;\n MaxLeftSum = MaxSubSum( A, Left, Center ) \n MaxRightSum = MaxSubSum( A, Center + 1, Right );\n\n MaxLeftBorderSum = 0; LeftBorderSum = 0;\n\n for(i=Cente+1;i>=left;i--){\n LeftBorderSum += A[i];\n if(leftBorderSum>MaxleftBorderSum){\n MaxleftBorderSum = LeftBorderSum;\n }\n }\n\n MaxRightBorderSum = 0; \n RightBorderSum = 0;\n for(i=Center;i<=Right;i++){\n RightBorderSum += A[i];\n if(RightBorderSum > MaxRightBorderSum){\n MaxRightBOrderSum = RightBorderSum\n }\n }\n return Max(MaxLeftSum,MaxRightSum,MaxLeftBorderSum+MaxRightBorderSum);\n\n}\nint MaxsubsequenceSum( const int A[],intN) {\n return MaxSubSum( A, 0, N - 1 );\n }\n
int MaxSubsequenceSum( const int A[ ], int N ) \n{ \n int ThisSum, MaxSum, j; \n ThisSum = MaxSum = 0; \n for ( j = 0; j < N; j++ ){ \n ThisSum += A[ j ]; \n if( ThisSum > MaxSum ){\n MaxSum = ThisSum; \n }\n else if( ThisSum < 0 ){\n ThisSum = 0;\n }\n } \n return MaxSum; \n} \n
int maxSubArray(int* nums, int numsSize) {\n int pre = 0, maxAns = nums[0];\n for (int i = 0; i < numsSize; i++) {\n pre = fmax(pre + nums[i], nums[i]);\n maxAns = fmax(maxAns, pre);\n }\n return maxAns;\n}\n
"},{"location":"oop/Basic/Basic/","title":"Itro & Class","text":""},{"location":"oop/Basic/Basic/#in-out","title":"In & Out","text":"
#include<iostream>\nusing namespace std;\nint main()\n{\n int age;\n cin >> age >> name;\n return 0;\n}\n
cout
\u6807\u51c6\u8f93\u51fa <<
Inserter
cout
\u4e3a cout << \"Hello World\"
\u7684\u7ed3\u679c
#include<string>
#include<iostream>\n#include<string>\nusing namespace std;\nint main()\n{\n int age;\n string name;\n cin >> age >> name;\n cout << \"Hello World!\" << name << \" is \" << age << \" today!\" << endl;\n cout << name.length() << endl;\n return 0;\n}\n
\\0
string place(\"Hangzhou\").
int a(8)
#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n string originalString = \"Hello, World!\";\n // Using substr to extract a substring\n int startPos = 7; // Starting position\n int length = 5; // Length of the substring to extract\n string extractedSubstring = originalString.substr(startPos, length);\n // Displaying the result\n cout << \"Original String: \" << originalString << endl;\n cout << \"Substring from position \" << startPos << \" with length \" << length << \": \" << extractedSubstring << endl;\n return 0;\n}\n
"},{"location":"oop/Basic/Basic/#alter-string","title":"Alter-string","text":"#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n\n // Example 1: Assign the value of another string\n string anotherString = \"Goodbye\";\n myString.assign(anotherString);\n cout << \"After assign(const string& str): \" << myString << endl;\n\n // Example 2: Assign a substring of another string\n string sourceString = \"123456789\";\n myString.assign(sourceString, 2, 5); //Starting from position 2,take 5 characters\n cout << \"After assign(const string& str, size_t subpos, size_t sublen): \" << myString << endl;\n\n // Example 3: Assign the value of a C-string\n const char* cString = \"C-Style String\";\n myString.assign(cString);\n cout << \"After assign(const char* s): \" << myString << endl;\n\n // Example 4: Assign the first n characters of a C-string\n const char* cStringWithLength = \"ABCDE\";\n size_t n = 3;\n myString.assign(cStringWithLength, n);\n cout << \"After assign(const char* s, size_t n): \" << myString << endl;\n\n // Example 5: Assign a string consisting of n copies of character c\n char characterToRepeat = 'X';\n size_t numberOfCopies = 4;\n myString.assign(numberOfCopies, characterToRepeat);\n cout << \"After assign(size_t n, char c): \" << myString << endl;\n\n return 0;\n}\n
After assign(const string& str): Goodbye\nAfter assign(const string& str, size_t subpos, size_t sublen): 34567\nAfter assign(const char* s): C-Style String\nAfter assign(const char* s, size_t n): ABC\nAfter assign(size_t n, char c): XXXX\n
size_t
is a data type in C++ that is commonly employed for expressing the size of objects or the count of elements. It belongs to the category of unsigned integer types and is designed to have a size large enough to handle the maximum possible size of objects on a given platform. The definition of size_t
can be found in the standard library header <cstddef>
(or <stddef.h>
in C), and it is typically introduced using typedef
or using
.
#include <iostream>\n#include <string>\n\nusing namespace std;\n\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n\n // Example 1: Insert another string at a specific position\n string insertString = \"Beautiful \";\n size_t insertPosition = 7; // Before the 'W'\n myString.insert(insertPosition, insertString);\n cout << \"After insert(size_t pos, const string& str): \" << myString << endl;\n\n // Example 2: Insert a substring of another string at a specific position\n string sourceString = \"12345\";\n size_t subpos = 2;\n size_t sublen = 3;\n myString.insert(12, sourceString, subpos, sublen); // Insert at position 12\n cout << \"After insert(size_t pos, const string& str, size_t subpos, size_t sublen): \" << myString << endl;\n\n // Example 3: Insert a C-string at a specific position\n const char* cString = \"XYZ\";\n myString.insert(6, cString); // Insert at position 6\n cout << \"After insert(size_t pos, const char* s): \" << myString << endl;\n\n // Example 4: Insert the first n characters of a C-string at a specific position\n const char* cStringWithLength = \"ABCDE\";\n size_t n = 3;\n myString.insert(3, cStringWithLength, n); // Insert at position 3\n cout << \"After insert(size_t pos, const char* s, size_t n): \" << myString << endl;\n\n // Example 5: Insert n copies of character c at a specific position\n char characterToInsert = '!';\n size_t numberOfCopies = 4;\n myString.insert(0, numberOfCopies, characterToInsert); // Insert at the beginning\n cout << \"After insert(size_t pos, size_t n, char c): \" << myString << endl;\n\n return 0;\n}\n
After insert(size_t pos, const string& str): Hello, Beautiful World!\nAfter insert(size_t pos, const string& str, size_t subpos, size_t sublen): Hello, Beaut345iful World!\nAfter insert(size_t pos, const char* s): Hello,XYZ Beaut345iful World!\nAfter insert(size_t pos, const char* s, size_t n): HelABClo,XYZ Beaut345iful World!\nAfter insert(size_t pos, size_t n, char c): !!!!HelABClo,XYZ Beaut345iful World!\n
#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n // Example 1: Erase a portion of the string\n size_t erasePosition = 7; // Start erasing from position 7\n size_t eraseLength = 5; // Erase 5 characters\n myString.erase(erasePosition, eraseLength);\n cout << \"After erase(size_t pos, size_t len): \" << myString << endl;\n // Example 2: Erase the entire string (using default arguments)\n myString.erase();\n cout << \"After erase(): \" << myString << endl;\n return 0;\n}\n
After erase(size_t pos, size_t len): Hello, !\nAfter erase(): \n
#include <iostream>\n#include <string>\n\nusing namespace std;\n\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n\n // Example 1: Replace a portion of the string with another string\n size_t replacePosition = 7; // Start replacing from position 7\n size_t replaceLength = 5; // Replace 5 characters\n string replacementString = \"Universe\";\n myString.replace(replacePosition, replaceLength, replacementString);\n cout << \"After replace(size_t pos, size_t len, const string& str): \" << myString << endl;\n\n // Example 2: Replace a portion of the string with a substring of another string\n string sourceString = \"12345\";\n size_t subpos = 2;\n size_t sublen = 3;\n myString.replace(12, 5, sourceString, subpos, sublen); // Replace at position 12\n cout << \"After replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen): \" << myString << endl;\n\n // Example 3: Replace a portion of the string with a C-string\n const char* cString = \"XYZ\";\n myString.replace(6, 5, cString); // Replace at position 6\n cout << \"After replace(size_t pos, size_t len, const char* s): \" << myString << endl;\n\n // Example 4: Replace a portion of the string with the first n characters of a C-string\n const char* cStringWithLength = \"ABCDE\";\n size_t n = 3;\n myString.replace(3, 5, cStringWithLength, n); // Replace at position 3\n cout << \"After replace(size_t pos, size_t len, const char* s, size_t n): \" << myString << endl;\n\n // Example 5: Replace a portion of the string with n copies of character c\n char characterToReplace = '!';\n size_t numberOfCopies = 4;\n myString.replace(0, 1, numberOfCopies, characterToReplace); // Replace at the beginning\n cout << \"After replace(size_t pos, size_t len, size_t n, char c): \" << myString << endl;\n\n return 0;\n}\n
After replace(size_t pos, size_t len, const string& str): Hello, Universe!\nAfter replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen): Hello, Unive345\nAfter replace(size_t pos, size_t len, const char* s): Hello,XYZe345\nAfter replace(size_t pos, size_t len, const char* s, size_t n): HelABCZe345\nAfter replace(size_t pos, size_t len, size_t n, char c): !!!!elABCZe345\n
#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n // Original string\n string myString = \"Hello, World!\";\n // Example 1: Find the position of a substring\n string searchString = \"World\";\n size_t foundPosition = myString.find(searchString);\n cout << \"Position of '\" << searchString << \"': \" << foundPosition << endl;\n // Example 2: Find the position of a substring starting from a specific position\n size_t startPosition = 7;\n foundPosition = myString.find(searchString, startPosition);\n cout << \"Position of '\" << searchString << \"' starting from position \" << startPosition << \": \" << foundPosition << endl;\n return 0;\n}\n
Position of 'World': 7\nPosition of 'World' starting from position 7: 7\n
"},{"location":"oop/Basic/Basic/#dynamic-memory-allocation","title":"Dynamic memory allocation","text":"new
and delete
new int;\nnew int[10];\nnew Stash;\ndelete p;\ndelete []p; //\u6307\u9488\u6307\u5411\u7684\u662f\u591a\u4e2a\u5bf9\u8c61\n
// Allocate an array of integers\nint* p = new int[5];\n// Use the allocated memory\n// Deallocate the memory using delete []\ndelete [] p;\n
New -- \u7533\u8bf7\u7a7a\u95f4\uff0c\u540c\u65f6\u521d\u59cb\u5316\u5bf9\u8c61
It is safe to delete a Null
malloc
returns null
indicating not enough space
new
just alert error.
char c;\nchar& r=c;\n
For local or global variables , the initial value of the bending is required.
In parameters lists and member varaibles , not necessary
void f(int& x);\nf(y); //initialized when function is called \n
Restrictions
#include <iostream>\n#include <string>\nusing namespace std;\ntypedef struct point {\n float x;\n float y;\n /*version1*/\n void print(){\n cout << x << \" \" << y << endl;\n }\n /*version 2*/\n void move(int dx, int dy);\n} Point;\nvoid Point::move(int dx, int dy){\n x += dx;\n y += dy;\n}\n/*void print(const Point *p)\n{\n printf(\"%d %d\\n\", p->x, p->y); // const means that the function will not change the value of the variable \n}\nvoid move(Point* p,int dx, int dy)\n{\n p->x += dx;\n p->y += dy;\n}*/\nint main()\n{\n Point a;\n a.x = 1;\n a.y = 2;\n a.print();\n a.move(3, 4);\n}\n
// Class\n#include <iostream>\n#include <string>\nusing namespace std;\nclass Point{\n public:\n void init(int x,int y);\n void move(int dx,int dy);\n void print()const;\n private:\n int x;\n int y;\n};\nvoid Point::init(int ix,int iy){\n x=ix;\n y=iy;\n}\nvoid Point::move(int dx,int dy){\n x+=dx;\n y+=dy;\n}\nvoid Point::print()const{\n cout<<\"(\"<<x<<\",\"<<y<<\")\"<<endl;\n}\nint main()\n{\n Point a;\n a.init(10,20);\n a.print();\n}\n
void Point::print()const{\n cout << this << endl\n cout<<\"(\"<<this->x<<\",\"<<this->y<<\")\"<<endl;\n}\n
this
-- Point* this
--hidden parameter
Another example
void Point::init(int x,int y){\n this->x=x;\n this->y=y; \n}\n
"},{"location":"oop/Basic/Basic/#resolver","title":"Resolver","text":"void S::f() {\n ::f(); // Would be recursive otherwise!\n ::a++; // Select the global a\n a--; // The a at class scope\n}\n
"},{"location":"oop/Basic/Basic/#object-oriented","title":"Object-Oriented","text":""},{"location":"oop/Basic/Basic/#constructer","title":"Constructer","text":"Point c = {10,20}
#include <iostream>\n#include <string>\nusing namespace std;\nclass Point{\n public:\n Point(int deep); //one argument\n Point(int x,int y); //two arguments\n Point(){x = 13,y=31;}; // no argument\n void move(int dx,int dy);\n void print()const;\n private:\n int x;\n int y;\n};\nPoint::Point(int ix,int iy){\n x=ix;\n y=iy;\n}\nPoint::Point(int deep){\n x=y=deep;\n}\nvoid Point::move(int dx,int dy){\n x+=dx;\n y+=dy;\n}\nvoid Point::print()const{\n cout<<\"(\"<<x<<\",\"<<y<<\")\"<<endl;\n}\nint main()\n{\n Point a(10,20);\n Point c(10);//Point c = 10\n Point d;\n d.print();\n a.print();\n}\n
Student::Student(string s):name(s){} //initialization before constructor\n// Prefered\n
\u8fd9\u610f\u5473\u7740\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u4e2d\u5bf9 name \u6210\u5458\u53d8\u91cf\u8fdb\u884c\u4e86\u521d\u59cb\u5316 \u5728\u6784\u9020\u51fd\u6570\u4f53\u5185\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9 name \u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\uff0c\u800c\u662f\u5728\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u4e2d\u76f4\u63a5\u5c06\u4f20\u9012\u7ed9\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570 s \u8d4b\u503c\u7ed9\u4e86 name,\u8fd9\u6837\u7684\u64cd\u4f5c\u662f\u5728\u6784\u9020\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\uff0c\u5373\u5728\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5c31\u5bf9 name \u8fdb\u884c\u4e86\u521d\u59cb\u5316 \u56e0\u6b64\uff0c\u53ef\u4ee5\u8bf4\u8fd9\u662f\u201c\u521d\u59cb\u5316\uff08Initialization\uff09\u5728\u6784\u9020\u51fd\u6570\uff08Constructor\uff09\u4e4b\u524d\uff08Before\uff09\u201d\u6267\u884c\u7684\uff0c\u5373\u201cinitialization before constructor\u201d
Assignment
Student::Student(string s) {name=s;}\n
Student::Student(sring s){\n name = s;\n} //assignment\n// For more constructors\n
void func() {\n int x; // \u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u7684\u53d8\u91cf\n // \u7a7a\u95f4\u5728\u51fd\u6570\u6267\u884c\u65f6\u5206\u914d\n}\nvoid func() {\n static int x; // \u9759\u6001\u53d8\u91cf\n // \u7a7a\u95f4\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5206\u914d\n}\n
#include <iostream>\n#include <string>\n\nclass Student {\nprivate:\n std::string name;\n\npublic:\n // \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u7528\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u5bf9 name \u8fdb\u884c\u521d\u59cb\u5316\n Student(std::string s) : name(s) {\n std::cout << \"Default constructor called with name: \" << name << std::endl;\n }\n // \u8f93\u51fa\u5b66\u751f\u59d3\u540d\u7684\u65b9\u6cd5\n void display() {\n std::cout << \"Student's name: \" << name << std::endl;\n }\n};\nint main() {\n // \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \"Alice\" \u7684\u5b66\u751f\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\n Student student1(\"Alice\");\n // \u8c03\u7528 display \u65b9\u6cd5\u663e\u793a\u5b66\u751f\u7684\u59d3\u540d\n student1.display();\n return 0;\n}\n
const
\u6210\u5458\u53d8\u91cf\u4e0d\u80fd\u88ab\u8d4b\u503c\uff0c\u53ea\u80fd\u5728\u521d\u59cb\u5316\u65f6\u8d4b\u503c\u3002\u8fd9\u91cc\u8be6\u7ec6\u89e3\u91ca\u4e00\u4e0b\u5173\u4e8e const
\u6210\u5458\u53d8\u91cf\u521d\u59cb\u5316\u7684\u4e24\u79cd\u65b9\u5f0f\uff1a\u7c7b\u5185\u76f4\u63a5\u521d\u59cb\u5316\uff1a - \u53ef\u4ee5\u5728\u58f0\u660e const
\u6210\u5458\u53d8\u91cf\u65f6\u76f4\u63a5\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f8b\u5982 const float x = 1.0
\u3002\u4f46\u662f\uff0c\u8fd9\u6837\u505a\u7684\u7ed3\u679c\u662f\u6240\u6709\u5bf9\u8c61\u7684\u8be5 const
\u6210\u5458\u53d8\u91cf\u90fd\u5c06\u62e5\u6709\u76f8\u540c\u7684\u503c\uff0c\u65e0\u6cd5\u9488\u5bf9\u6bcf\u4e2a\u5bf9\u8c61\u5355\u72ec\u521d\u59cb\u5316\u3002 - \u793a\u4f8b\uff1a
class Point {\nprivate:\n const float x = 1.0;\n const float y = 1.0;\npublic:\n Point() {}\n};\n
\u8fd9\u79cd\u65b9\u5f0f\u7684\u9650\u5236\u662f\u6240\u6709 Point
\u5bf9\u8c61\u7684 x
\u548c y
\u90fd\u662f 1.0\uff0c\u65e0\u6cd5\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u8d4b\u4e88\u4e0d\u540c\u7684\u503c\u3002 \u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5217\u8868\uff1a - \u53e6\u4e00\u79cd\u521d\u59cb\u5316 const
\u6210\u5458\u53d8\u91cf\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u4e3a const
\u6210\u5458\u53d8\u91cf\u8d4b\u4e88\u4e0d\u540c\u7684\u503c\u3002 - \u793a\u4f8b\uff1a
class Point {\nprivate:\n const float x, y;\npublic:\n Point(float xa = 0.0, float ya = 0.0) : x(xa), y(ya) {}\n};\n
\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cPoint
\u7c7b\u6709\u4e24\u4e2a const
\u6210\u5458\u53d8\u91cf x
\u548c y
\u3002\u6784\u9020\u51fd\u6570\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u6765\u521d\u59cb\u5316\u8fd9\u4e24\u4e2a const
\u53d8\u91cf\u3002\u8fd9\u6837\uff0c\u521b\u5efa\u5bf9\u8c61\u65f6\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u7684 x
\u548c y
\u8d4b\u4e88\u4e0d\u540c\u7684\u503c\u3002 Point p1(1.0, 2.0); // p1 \u7684 x \u662f 1.0\uff0cy \u662f 2.0\nPoint p2(3.0, 4.0); // p2 \u7684 x \u662f 3.0\uff0cy \u662f 4.0\n
\u8be6\u7ec6\u89e3\u91ca\uff1a
\u5728\u4e0a\u8ff0 Point
\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a const
\u6210\u5458\u53d8\u91cf x
\u548c y
\u3002\u7531\u4e8e\u5b83\u4eec\u662f const
\u7684\uff0c\u6240\u4ee5\u4e00\u65e6\u88ab\u521d\u59cb\u5316\u540e\uff0c\u5c31\u4e0d\u80fd\u518d\u4fee\u6539\u5176\u503c\u3002\u4e3a\u4e86\u80fd\u591f\u7075\u6d3b\u5730\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u7684 x
\u548c y
\u8d4b\u503c\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\uff1a
Point(float xa = 0.0, float ya = 0.0)
\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53c2\u6570 xa
\u548c ya
\uff0c\u5e76\u4e14\u7ed9\u5b83\u4eec\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c 0.0
\u3002: x(xa), y(ya)
\u5728\u6784\u9020\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\u5c06\u53c2\u6570 xa
\u548c ya
\u7684\u503c\u5206\u522b\u8d4b\u7ed9 const
\u6210\u5458\u53d8\u91cf x
\u548c y
\u3002x
\u548c y
\u503c\uff0c\u800c\u4e0d\u662f\u6240\u6709\u5bf9\u8c61\u90fd\u5177\u6709\u76f8\u540c\u7684 const
\u6210\u5458\u503c\u3002Destructor is called automatically by the compiler when the object goes out of scope
The order of destruction is the reverse of construction.
Scope is delimited by curly braces{ }
.
Upon entering a function, space for all local variables is allocated,but constructors are not invoked until the specific constructor line is executed.
Similarly, when entering a switch case
statement, space for objects is generated but not constructed, which may lead to issues during destruction.
void f(int i){\n if(i<10){\n goto jump1; //Error: goto bypasses init\n }\n X x1; //Constructor\n jump1:\n switch(i){\n case 1:\n X x2;//Constructor\n case 2 : //Error: case bypasses init\n X x3;//Constructor\n break;\n }\n}\n
x1
(x2
) is allocated , but not initialized , so destruction will fail.h
and .cpp
files are used to define one class..h
).All the bodies of these functions are in the source file (.cpp
)
Standard header file sturcture
#ifnedf HEADER_FLAG\n#define HEADER_FLAG\n//Type declaration here...\n#endif\n
#include
is to insert the included file into the .cpp
file at where the #include
statement is. #include \"xx.h\"
: search in the current directory firstly, then the directories declared somewhere#include <xx.h>
: search in the specified directories#include <xx>
: same as #include <xx.h>
NumberDisplay.h
#ifndef _NUMBER_DISPLAY_HEAD_\n#define _NUMBER_DISPLAY_HEAD_\n#include<string>\nclass NumberDisplay{\n private:\n int limit;\n int value;\n public:\n NumberDisplay(int limit);\n int increase();\n std::string toString();\n}\n#endif\n
NumberDisplay.cpp
#include \"NumberDisplay.h\"\n#include<string>\n#include<iostream>\n\nNumberDisplay::NumberDisplay(int limit){\n value = 0;\n this->limit = limit;\n}\n/**\n * @return 1 for turn over\n*/\nint NumberDisplay::increase():{\n value++;\n if(value == limit){\n value = 0;\n return 1;\n }\n return 0;\n}\nstd::string NumberDisplay::toString(){\n if(value<10){\n return \"0\"+value;\n }\n else{\n return \"\"+value;\n }\n}\n#ifdef _TEST_ND_\n#include<iostream>\nusing namespace std;\nint main(){\n NumberDisplay d(10);\n for(int i=9;i<20;i++){\n d.increase();\n cout << d.tostring()<<endl; \n }\n\n}\n#endif\n
clock.h
#ifndef _CLOCK_HEAD_\n#define _CLOCK_HEAD_\n#include \"NumberDisplay.h\"\nclass Clock{\n private:\n NumberDisplay hour;\n NumberDisplay minute;\n public:\n Clock();\n void dida();\n}\n
clock.cpp
#include \"clock.h\"\n#include<iostream>\nusing namespace std;\nClock::Clock():\n hour(24),minute(60) //initialization list\n{\n\n}\nvoid Clock::dida(){\n if(minute.increase()){\n hour.increase\n }\n cout << hour.toString();\n cout << \":\";\n cout << minute.toString();\n cout << endl;\n}\n
* What if ? clock.h
#ifndef _CLOCK_HEAD_\n#define _CLOCK_HEAD_\n//#include\"NumberDisplay.h\"\nclass NumberDisplay\nclass Clock{\n private:\n NumberDisplay *hour;\n NumberDisplay *minute;\n public:\n Clock();\n void dida();\n}\n
Reference
\u5f15\u7528\u548c\u6307\u9488\u662fC++\u4e2d\u4e24\u79cd\u4e0d\u540c\u7684\u6982\u5ff5\uff0c\u5b83\u4eec\u6709\u7740\u76f8\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5728\u8bed\u4e49\u548c\u4f7f\u7528\u65b9\u5f0f\u4e0a\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u533a\u522b\u3002
"},{"location":"oop/Basic/Basic/#_1","title":"\u533a\u522b\u4e00\uff1a\u8bed\u6cd5\u548c\u58f0\u660e","text":"\u5f15\u7528\uff1a\u4f7f\u7528 &
\u7b26\u53f7\u6765\u58f0\u660e\uff0c\u5fc5\u987b\u5728\u521d\u59cb\u5316\u65f6\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u53d8\u91cf\u6216\u5bf9\u8c61\u3002\u4e00\u65e6\u521d\u59cb\u5316\uff0c\u5f15\u7528\u5c06\u4e00\u76f4\u6307\u5411\u8be5\u53d8\u91cf\u6216\u5bf9\u8c61\uff0c\u5e76\u4e14\u4e0d\u80fd\u91cd\u65b0\u7ed1\u5b9a\u5230\u5176\u4ed6\u5bf9\u8c61\u3002
\u6307\u9488\uff1a\u4f7f\u7528 *
\u7b26\u53f7\u6765\u58f0\u660e\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u6307\u5411\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u6307\u5411\u7a7a\u503c\uff08nullptr\uff09\u3002\u6307\u9488\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u88ab\u91cd\u65b0\u8d4b\u503c\uff0c\u6307\u5411\u4e0d\u540c\u7684\u5bf9\u8c61\u6216\u7a7a\u503c\u3002
int num = 10;\nint& ref = num; // \u5f15\u7528\nint* ptr = # // \u6307\u9488\n
"},{"location":"oop/Basic/Basic/#_2","title":"\u533a\u522b\u4e8c\uff1a\u7a7a\u503c","text":"\u5f15\u7528\uff1a\u5f15\u7528\u4e0d\u80fd\u6307\u5411\u7a7a\u503c\uff0c\u5fc5\u987b\u5728\u521d\u59cb\u5316\u65f6\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5bf9\u8c61\u3002
\u6307\u9488\uff1a\u6307\u9488\u53ef\u4ee5\u6307\u5411\u7a7a\u503c\uff08nullptr\uff09\uff0c\u8868\u793a\u4e0d\u6307\u5411\u4efb\u4f55\u5bf9\u8c61\u3002
int& ref = null; // \u9519\u8bef\uff0c\u5f15\u7528\u4e0d\u80fd\u6307\u5411\u7a7a\u503c\nint* ptr = nullptr; // \u6307\u9488\u6307\u5411\u7a7a\u503c\n
"},{"location":"oop/Basic/Basic/#_3","title":"\u533a\u522b\u4e09\uff1a\u64cd\u4f5c\u7b26","text":"\u5f15\u7528\uff1a\u4f7f\u7528\u5f15\u7528\u65f6\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\uff08*
\uff09\uff0c\u76f4\u63a5\u4f7f\u7528\u5f15\u7528\u672c\u8eab\u5373\u53ef\u8bbf\u95ee\u76ee\u6807\u5bf9\u8c61\u3002
\u6307\u9488\uff1a\u9700\u8981\u4f7f\u7528\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\uff08*
\uff09\u6765\u8bbf\u95ee\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61\u3002
int num = 10;\nint& ref = num; // \u5f15\u7528\nint* ptr = # // \u6307\u9488\n\nint value1 = ref; // \u76f4\u63a5\u4f7f\u7528\u5f15\u7528\nint value2 = *ptr; // \u4f7f\u7528\u6307\u9488\u9700\u8981\u89e3\u5f15\u7528\n
"},{"location":"oop/Basic/Basic/#_4","title":"\u533a\u522b\u56db\uff1a\u7a7a\u95f4\u5360\u7528","text":"\u5f15\u7528\uff1a\u5728\u5185\u5b58\u4e2d\u4e0d\u4f1a\u5206\u914d\u989d\u5916\u7684\u7a7a\u95f4\uff0c\u5b83\u53ea\u662f\u539f\u53d8\u91cf\u7684\u522b\u540d\u3002
\u6307\u9488\uff1a\u5728\u5185\u5b58\u4e2d\u5360\u7528\u989d\u5916\u7684\u7a7a\u95f4\u6765\u5b58\u50a8\u6307\u9488\u53d8\u91cf\u672c\u8eab\u7684\u5730\u5740\u3002
\u5f15\u7528\u548c\u6307\u9488\u5728C++\u4e2d\u90fd\u6709\u5176\u72ec\u7279\u7684\u7528\u9014\u548c\u4f18\u52bf\u3002\u5f15\u7528\u66f4\u5b89\u5168\u3001\u66f4\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u522b\u540d\u548c\u4f20\u9012\u53c2\u6570\u3002\u6307\u9488\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u6307\u5411\u591a\u4e2a\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8fdb\u884c\u7a7a\u503c\u68c0\u67e5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u5206\u914d\u5185\u5b58\u6216\u8ddf\u8e2a\u5bf9\u8c61\u5730\u5740\u7684\u60c5\u51b5\u3002\u9009\u62e9\u4f7f\u7528\u5f15\u7528\u8fd8\u662f\u6307\u9488\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u8bbe\u8ba1\u8003\u8651\u3002
"},{"location":"oop/Container/Container/","title":"Container & Function","text":"
Reference : https://note.hobbitqia.cc/OOP/oop3/#for-each-loop
"},{"location":"oop/Container/Container/#container","title":"Container","text":"Collection objects are objects that can store an arbitrary number of other objects.
#include <iostream>\nusing namespace std;\n#include<vector>\nint main(){\n vector<int> x;\n for (int a=0;a<1000;a++){\n x.push_back(a);\n }\n vector<int>::iterator p;\n for(p=x.begin();p<x.end();p++)\n cout << *p << \" \";\n return 0; \n}\n
int main(){\n vector<int> x;\n for(int a=0;a<100;a++){\n x.push_back(a);\n }\n cout << x.size()<<endl\n vector<int>::iterator p;\n for(auto k : x){\n cout << k << \" \";\n }\n cout << endl;\n return x;\n}\n
iterator : class inside vector
generic classes vector<string> notes;
Have to specify two types
It is able to increase its internal capacity as required: as more items are added, it simply makes enough room for them
It keeps its own private count of how many items it is currently storing. Its size method returns the number of objects currently stored in it
It maintains the order of items you insert into it.You can later retrieve them in the same order.
"},{"location":"oop/Container/Container/#basic-vector-operations","title":"Basic Vector Operations","text":"vector<Elem>c\nvector<Elem>c1(c2)\n
V.size()\nV.empty()\nV.swap(v2)\n
I.begin()\nI.end()\n
V.at(index)\nV[index]\nV.front()\nV.back()\n
V.push_back(e)\nV.pop_back(e)\nV.insert(pos,e)\nV.erase(pos)\nV.clear()\nV.find(first,last,item)\n
int main(){\n vector<Student> ss //Student is a class\n Student s(1);\n ss.push_back(s);\n s.x = 10; // does not change ss[0]\n}\n
int main(){\n vector<Student*> ss \n Student s(1);\n ss.push_back(&s);\n s.x = 10; //will change(obviously)\n}\n
"},{"location":"oop/Container/Container/#operations","title":"Operations","text":"<vector>
\u53ef\u4ee5\u5411\u4efb\u610f\u4e0b\u6807\u8d4b\u503c\uff0c\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f46\u662f\u8fd9\u4e0d\u4f1a\u6539\u53d8.size() .back()
\u7b49 \uff0c\u6240\u4ee5\u5b9e\u9645\u4e0a\u6211\u4eec\u8981\u7528push_back()
\u7b49\u53bbinsertp!=s.end()
#include <iostream>\nusing namespace std;\n#include<list>\nint main(){\n list <int> L;\n for(int i=0;i<5;i++){\n L.push_back(i);\n }\n L.erase(++L.begin());\n copy(L.begin(),L.end(),ostream_iterator<int>(cout,\",\"));\n}\n
\u901a\u5e38\u7528vector \uff1a \u66f4\u8282\u7ea6\u7a7a\u95f4,\u9664\u975e\u9700\u8981\u5927\u91cf\u589e\u52a0\uff0c\u5220\u9664
"},{"location":"oop/Container/Container/#maps","title":"Maps","text":"Hash
#include <map>\nmap<long,int> root;\nroot[4] = 2;\nroot[1000000] = 1000;\nlong l;\ncin >> l;\nif (root.count(l))\n cout<<root[l]\nelse cout<<\u201cNot perfect square\u201d;\n
"},{"location":"oop/Container/Container/#pitfalls","title":"Pitfalls","text":"if ( my_list.count() == 0 ) { ... } // Slow\nif ( my_list.empty() ) {...} // Fast\n
list<int> L;\nlist<int>::iterator li;\nli = L.begin();\nL.erase(li);\n++li; // WRONG\n// Use return value of erase to advance\nli = L.erase(li); // RIGHT\n
map<>
if (foo[\"bob\"]==1)\n//silently created entry \u201cbob\u201d\n//Solutions: Use count() to check for a key without creating a new entry. if ( foo.count(\"bob\") )\n
"},{"location":"oop/Container/Container/#function","title":"Function","text":""},{"location":"oop/Container/Container/#function-overloading","title":"Function Overloading","text":"void print(char * str, int width); // #1 \nvoid print(double d, int width); // #2 \nvoid print(long l, int width); // #3 \nvoid print(int i, int width); // #4 \nvoid print(char *str); // #5 \nprint(\"Pancakes\", 15); \nprint(\"Syrup\"); \nprint(1999.0, 10); \nprint(1999, 12); \nprint(1999L, 15);\n
A default argument is a value given in the declaration that the compiler automatically inserts if you don\u02bct provide a value in the function call.
int harpo(int n, int m = 4, int j = 5);\nint chico(int n, int m = 6, int j); // illegal\n//To define a function with an argument list, defaults must be added from right to left.\nint groucho(int k = 1, int m = 2, int n = 3);\nbeeps = harpo(2);\nbeeps = harpo(1,8);\nbeeps = harpo(8,7,6);\n
void f(int i, int j = 10);\nint main()\n{ ...\n}\nvoid f(int i, int j){\n ...\n}\n// USE IN THIS WAY\n
"},{"location":"oop/Container/Container/#friend-access-control","title":"Friend [Access Control]","text":"struct X{\n private:\n int x;\n public :\n void initialize();\n friend void g(X*,int);\n friend void Y::f(X*);\n friend struct Z;\n friend void h();\n};\n
"},{"location":"oop/Container/Container/#overhead-for-a-function-call","title":"Overhead for a function call","text":""},{"location":"oop/Container/Container/#inline-functions","title":"Inline Functions","text":"inline int f(int i) {\n return i*2;\n}\nmain() {\n int a=4;\n int b = f(a); // become b = a * 2;\n}\n
// .h File!!!\nclass Point{\n ...\n public:\n inline void print(string & msg = \"\");\n};\ninline void Point::print(string & msg = \"\"){\n ...\n}\n
class Cup {\n int color;\npublic:\n int getColor() { return color; }\n void setColor(int color) {\n this->color = color;\n }\n};\n
const int a = 6
-- not changable variable
const int bufsize = 1024;
Unless you make an explicit extern declaration , like extern const int bufsize;
Compiler to make sure that it will not be changed.
Connot be optimized : \uff08remain variables\uff09
const int x = 123; // const, literal\nx = 27; // illegal!\nx++; // illegal!\nint y = x; // ok, copy const to non-const\ny = x; // ok, same thing\nconst int z = y; // ok, const is safer\n
const int i[] = { 1, 2, 3, 4 };\nfloat f[i[3]]; // Illegal(in C++98)\nstruct S { int i, j; };\nconst S s[] = { { 1, 2 }, { 3, 4 } };\ndouble d[s[1].j]; // Illegal\n
const
for aggregates, but storage will be allocated. In these situations,const
means \"a piece of storage that cannot be changed.\" However,the value cannot be used at compile time because the compiler is not required to know the contents of the storage at compile time.
"},{"location":"oop/Container/Container/#pointer","title":"Pointer","text":"char * const q = \"abc\"; // q is const\n*q = 'c'; // ERROR\n// char a[] = \"abc\"; *q = 'c' is ok.\nq++; // ERROR\nconst char *p = \"ABCD\"; // (*p) is a const char\n*p = 'b'; // ERROR! (*p) is the const\n
string p1(\"Fred\");\nconst string* p = &p1; // p1 cannot be changed through p.\nstring const* p = &p1; // like the first one.\nstring *const p = &p1; // p cannot be changed.only point to p1.\n
"},{"location":"oop/Container/Container/#string-literals","title":"String Literals","text":"char *s = \"Hello\"; //can be compile but bus error because actually const char *s\n// Add const compile will not pass.\ns[0] = 'K';\nprintf(\"%s\\n\",s);\n// Write in an array\nchar s[] = \"Hello, world!\";\n
"},{"location":"oop/Container/Container/#const-object","title":"Const Object","text":"const Currency the_raise(42, 38);\n
Compiler Will ERROR:
Thus,when we write all the member functions,SPECIFY CONST! For Compiler to know if it will change the value of member variables!
int Date::set_day(int d) {\n //...error check d here...\n day = d; // ok, non-const so can modify\n}\nint Date::get_day() const {\n day++; // ERROR modifies data member\n set_day(12); // ERROR calls non-const member\n return day; // ok\n}\n
const
\u5b9a\u4e49\u7684\u5bf9\u8c61\uff0c\u53ea\u80fd\u8c03\u7528\u5e26 const
\u5c5e\u6027\u7684\u6210\u5458\u51fd\u6570\u3002const
\u5fc5\u987b\u4fdd\u8bc1\u6210\u5458\u53d8\u91cf\u6709\u521d\u59cb\u503c\uff01\u9ed8\u8ba4\u6784\u9020\u51fd\u6570.
Overload (const can distinguish the two functions.)\uff0c\u4f1a\u6839\u636e\u5bf9\u8c61\u8c03\u7528\u65f6\u662f\u5426 const
\u6765\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u6210\u5458\u51fd\u6570
\u4e8b\u5b9e\u4e0a const
\u540e\u7684\u6210\u5458\u51fd\u6570\u53c2\u6570\uff0c\u76f8\u5f53\u4e8e const A *this
, \u800c\u4e0d\u52a0 const \u5c31\u53ea\u662f A *this
.
const
\u4f5c\u7528\u5728\u9690\u85cf\u53c2\u6570 A* this
void f() const;\nvoid f();\n
\u53ea\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee[\u53ea\u5728\u7b2c\u4e00\u6b21\u88ab\u6267\u884c\u5230\u7684\u65f6\u5019]\u65f6\u88ab\u6784\u9020
\u51fa\u73b0\u5728\u5168\u5c40\u53d8\u91cf/\u51fd\u6570\u524d\uff0c\u8868\u793a\u8bbf\u95ee\u9650\u5236\uff0c\u53ea\u6709\u5f53\u524d\u6587\u4ef6\u53ef\u4ee5\u8bbf\u95ee
\u5728\u7a0b\u5e8f\u7ed3\u675f\u540e\u6790\u6784
\u5168\u5c40\u53d8\u91cf\u7684\u6784\u9020\u53d1\u751f\u5728 main()
\u4e4b\u524d\uff0c\u5728 main()
\u6216\u8005 exit()
\u4e4b\u540e\u6790\u6784 \u4f46\u662f\u4e0d\u540c\u7f16\u8bd1\u5355\u5143\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5982\u679c\u5b58\u5728\u4f9d\u8d56\u5173\u7cfb\uff0c\u4f46\u8fd9\u662f\u65e0\u6cd5\u4fdd\u8bc1\u7684
\u6700\u597d\u4e0d\u5728C++\u4f7f\u7528global variable
\u8bbf\u95ee\u53d7\u9650\uff0c\u9650\u4e8e\u7c7b\u5185\u90e8\uff0c\u5b9e\u9645\u4e0a\u662f\u5168\u5c40\u53d8\u91cf\u3002
\u5728\u8fd9\u4e2a\u7c7b\u5185\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u7ef4\u6301\u76f8\u540c\u7684\u503c\uff0c\u5bf9\u8c61 A \u4fee\u6539\u4e86\u90a3\u4e48\u5bf9\u8c61 B \u7684\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\u4e5f\u4f1a\u6539\u53d8
\u53ef\u4ee5\u5728\u6ca1\u6709\u521b\u5efa\u7c7b\u7684\u5bf9\u8c61\u7684\u65f6\u5019\u5c31\u80fd\u8c03\u7528\u9759\u6001\u6210\u5458\u51fd\u6570
#ifndef _STAT_MEM_\n#define _STAT_MEM_\n\nclass StatMem{\npublic:\n int getHeight(){return m_h;}\n void setHeight(int i){m_h = i}\n int getWeight(){return m_w;}\n void setWeight(int i){m_w = i;}\n\n static int m_h;\n int m_w;\n}\n#endif \n
#include\"StatMem.h\"\nint StatMem::m_h\n
\u200b \u4e14\u4e0d\u80fd\u518d\u52a0static
Example
void main() {\n using MyLib::foo;\n using MyLib::Cat;\n foo();\n Cat c;\n c.Meow();\n}\n
// Mylib.h\nnamespace XLib {\n void x();\n void y();\n}\nnamespace YLib {\n void y();\n void z();\n}\n\n//Main\nvoid main() {\n using namespace XLib;\n using namespace YLib;\n x(); // OK\n y(); // Error: ambiguous\n XLib::y(); // OK, resolves to XLib\n z(); // OK\n}\n
"},{"location":"oop/Container/Container/#namespace-aliase","title":"Namespace aliase","text":"namespace supercalifragilistic{\n void f();\n}\nnamespace short1 = supercalifragilistic;\nshort1::f();\n
"},{"location":"oop/Container/Container/#namespace-composition","title":"Namespace composition","text":"namespace first {\n void x();\n void y();\n}\nnamespace second {\n void y();\n void z();\n}\nnamespace mine {\n using namespace first;\n using namespace second;\n using first::y(); // resolve clashes to first::x()\n void mystuff();\n // ...\n}\n
"},{"location":"oop/Container/Container/#namespaces-are-open","title":"Namespaces are open","text":"//header1.h\nnamespace X {\n void f();\n}\n// header2.h\nnamespace X {\n void g(); // X now has f() and g();\n}\n
"},{"location":"oop/Container/Container/#namespace-selection","title":"Namespace selection","text":"namespace mine {\n using orig::Cat; // use Cat class from orig\n void x();\n void y();\n}\n
"},{"location":"oop/Container/Container/#appendix","title":"Appendix","text":"C++
\u94fe\u63a5\u5668\u4e0d\u53bb\u505a\u53c2\u6570\u7c7b\u578b\u7684\u5224\u65ad\uff0c\u90a3\u4e48\u600e\u4e48\u505aoverload , namespace\uff1ff(int a,float b)
will turn into _f_int_double_()
namespace func(void f();) _func_f_()
C
\u7f16\u8bd1\u4ea7\u751f\u7684\u662f\u4e0d\u5e26\u4e0b\u5212\u7ebf\u7684 solution\uff1aexetern \"C\"{\n #include \"oldc.h\"\n}\n
"},{"location":"oop/Exceptions/lec/","title":"Exception & File","text":""},{"location":"oop/Exceptions/lec/#exceptions","title":"Exceptions","text":"
Exceptions are a mechanism for handling errors in C++ programs. They are used to signal that an error has occurred and to specify what should be done to handle the error.
The significant benefit of exceptions is that they clean up error handling code.
It separates the code that describes what you want to do from the code that is executed.
\u53ef\u4ee5\u9884\u89c1\u7684\u9519\u8bef\uff1a
\u53ef\u4ee5 fseek \u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u518d ftell \u672b\u5c3e\u7684\u503c\u3002
errorCodeType readFile { initialize errorCode = 0;\n open the file;\n if ( theFilesOpen ) { \n determine its size;\n if ( gotTheFileLength ) { \n allocate that much memory;\n if ( gotEnoughMemory ) { \n read the file into memory;\n if ( readFailed ) { \n errorCode = -1;\n }\n }else {\n errorCode = -2;\n } \n } else {\n errorCode = -3;\n }\n close the file;\n if ( theFILEDidntClose && errorCode == 0 ) { \n errorCode = -4;\n }else {\n errorCode = errorCode and -4;\n } \n } else {\n errorCode = -5;\n }\n return errorCode;\n}\n
try {\n open the file;\n determine its size;\n allocate that much memory;\n read the file into memory;\n close the file; \n} \ncatch ( fileOpenFailed ) { doSomething; } \ncatch ( sizeDeterminationFailed ) { doSomething; } \ncatch ( memoryAllocationFailed ) { doSomething; } \ncatch ( readFailed ) { doSomething; } \ncatch ( fileCloseFailed ) { doSomething; }\n
template <class T> class Vector { \nprivate: \n T* m_elements; \n int m_size; \npublic: \n Vector (int size = 0) : \n m_size(size) ... \n ~Vector () { delete [] m_elements; } \n void length(int); \n int length() { return m_size; } \n T& operator[](int); \n};\n
\u95ee\u9898\uff1a\u5f53\u7528\u6237\u8bd5\u56fe\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u65f6\uff0c\u5e94\u8be5\u600e\u4e48\u5904\u7406\uff1f
T& Vector::operator[](int index) { \n if (index < 0 || index >= m_size){\n throw \"Index out of range\"; \n }\n return m_elements[index]; \n}\n
Assertion vs. Exception
try{}
, \u6211\u4eec\u5c31\u5224\u65ad\u5339\u914d\u4e00\u4e2a\u5f02\u5e38\u7c7bclass VectorIndexError { \n public: \n VectorIndexError(int v) : m_badValue(v) { } \n ~VectorIndexError() { } \n void diagnostic() { cerr << \"index \" << m_ badValue << \"out of range!\"; } \n private: \n int m_badValue; \n};\ntemplate <class T> \nT& Vector::operator[](int index) { \n if (index < 0 || index >= m_size) \n throw VectorIndexError(index); \n return m_elements[index]; \n}\n
void outer2() { \nString err(\"exception caught\"); \ntry {\n func(); \n} catch (VectorIndexError) { \n cout << err; \n throw; // propagate the exception \n} \n}\n
Two forms
catch (SomeType v) { // handler code } catch (...) { // handler code }
new
does NOT returned 0 on failure. new
raises a bad_alloc() exception.void abc():throw (MathErr);
","text":"Printer::print(Document&) : throw(PrinterOffLine, BadDocument) { ... }\nPrintManager::print(Document&) : throw (BadDocument) { ... }// raises or doesn\u2019t handle BadDocument \nvoid goodguy() : throw () { }// handles all exceptions \nvoid average() { } // no spec, no checking,\n
f() {\n A *p = new A();\n ...\n delete p;\n}\n
delete this
:\u5fc5\u987b\u662f\u4e00\u4e2a\u5c40\u90e8\u5bf9\u8c61\uff0c\u4e0d\u80fd\u662f\u4e00\u4e2a\u5168\u5c40\u5bf9\u8c61\u3002c++ Error *p = new Error(); ... catch (Error e) { delete e; delete this; }
int get()\nwhile((ch=cin.get())!=EOF) {\n cout.put(ch);\n}\n
"},{"location":"oop/Smart/lec/","title":"Smart Pointer","text":""},{"location":"oop/Smart/lec/#smart-pointers","title":"Smart Pointers","text":"String abc(\"abcdefg\");
p->decrement(); // p's count will decrease \np = q; \nq->increment(); // q/p's count will increase\n
"},{"location":"oop/Smart/lec/#ucobject","title":"UCObject","text":"#ifndef UCObject_H_\n#define UCObject_H_\n#include <assert.h> \nclass UCObject { \npublic: \n UCObject() : m_refCount(0){ } \n /**\n * Only destruct when refCount is 0\n * assert, \u56e0\u4e3a\u4e0d\u662f\u5bf9\u8c61\u7684\u95ee\u9898\uff0c\u662f\u5916\u90e8\u7684\u95ee\u9898\u3002\n */\n virtual ~UCObject() { assert(m_refCount == 0);}; \n /**\n * \u4e0d\u62f7\u8d1d refcount \n */ \n UCObject(const UCObject&) : m_refCount(0) { } \n void incr() { m_refCount++; } \n void decr(); \n int references() { return m_refCount; } \nprivate: \n int m_refCount; \n};\ninline void UCObject::decr() { \n m_refCount -= 1; \n if (m_refCount == 0) { \n delete this; // goto UCObject::~UCObject()\n } \n} \n#endif\n
#ifndef UCPointer_H_\n#define UCPointer_H_\ntemplate <class T> \nclass UCPointer { \nprivate: \n T* m_pObj; \n void increment() { if (m_pObj) m_pObj->incr(); } \n void decrement() { if (m_pObj) m_pObj->decr(); } \npublic: \n UCPointer(T* r = 0): m_pObj(r) { increment();} \n ~UCPointer() { decrement(); }; \n UCPointer(const UCPointer<T> & p); \n UCPointer& operator=(const UCPointer<T> &); \n T* operator->() const; \n T& operator*() const { return *m_pObj; }; \n}\ntemplate <class T> \nUCPointer<T>::UCPointer(const UCPointer<T> & p){ \n m_pObj = p.m_pObj; \n increment(); \n}\ntemplate <class T> \nUCPointer<T>& UCPointer<T>::operator=(const UCPointer<T>& p){ \n if (m_pObj != p.m_pObj){ \n decrement(); \n m_pObj = p.m_pObj; \n increment(); \n } \n return *this; \n}\ntemplate<class T> \nT* UCPointer<T>::operator->() const { \n return m_pObj; \n}\ntemplate <class T>\nT& UCPointer<T>::operator*() const { \n return *m_pObj; \n}\n#endif\n
Ellipse elly(200F, 300F); \nUCPointer<Shape> p(&elly); \np->render(); // calls Ellipse::render() on elly!\n
p->render();
\u4f1a\u8c03\u7528 m_pObj->render()
abc = \"Hello World\"\n
#ifndef StringRep_H_\n#define StringRep_H_\n#include <cstring.h>\n#include \"UCObject.h\" \nclass StringRep : public UCObject { \npublic: \n StringRep(const char *); \n ~StringRep(); \n StringRep(const StringRep&); \n int length() const{ return strlen(m_pChars); } \n int equal(const StringRep&) const; \nprivate: \n char *m_pChars; \n // reference semantics -- no assignment op! \n // \u79c1\u6709\uff0c\u5916\u754c\u4e0d\u80fd\u505a StringRep \u7684\u8d4b\u503c\n void operator=(const StringRep&) {}\n};\n/**\n * 1. \u4f20\u5165\u7684\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff0c\u90a3\u4e48\u53ea\u6709\u4e00\u4e2a \\0 \n */\nStringRep::StringRep(const char *s) { \n if (s) { \n int len = strlen(s) + 1; \n m_pChars = new char[len]; \n strcpy(m_pChars , s); \n } \n else { \n m_pChars = new char[1]; \n *m_pChars = '\\0'; \n } \n} \nStringRep::~StringRep() { \n delete [] m_pChars ; \n}\nStringRep::StringRep(const StringRep& sr) { \n int len = sr.length(); \n m_pChars = new char[len + 1]; \n strcpy(m_pChars , sr.m_pChars ); \n} \nint StringRep::equal(const StringRep& sp) const { \n return (strcmp(m_pChars, sp.m_pChars) == 0); \n}\n#endif\n
class String { \npublic: \n String(const char *); \n ~String(); \n String(const String&); \n String& operator=(const String&); \n int operator==(const String&) const; \n String operator+(const String&) const; \n int length() const; \n operator const char*() const; \nprivate: \n UCPointer<StringRep> m_rep; \n};\nString::String(const char *s) : m_rep(0) { \n m_rep = new StringRep(s); \n} \nString::~String() {} \n// Again, note constructor for rep in list. \nString::String(const String& s) : m_rep(s.m_rep) { \n // \u505a UCPointer<StringRep> \u7684\u62f7\u8d1d\u6784\u9020\n} \nString& String::operator=(const String& s) { \n m_rep = s.m_rep; // let smart pointer do work! \n return *this; \n}\nint String::operator==(const String& s) const { \n // overloaded -> forwards to StringRep \n return m_rep->equal(*s.m_rep); // smart ptr * \n} \nint String::length() const { \n return m_rep->length(); \n}\n
* \u8fd9\u91cc m_rep(0) \u5b9e\u9645\u4e0a\u662f\u7528 0 \u53bb\u505a UCP \u7684\u6784\u9020(\u4e5f\u53ef\u4ee5\u4e0d\u5199\uff0c\u56e0\u4e3a\u9ed8\u8ba4 0). * new \u8fd4\u56de\u7684\u662f StringRep *, \u800c\u5de6\u8fb9\u662f UCP, \u8fd9\u91cc\u8d4b\u503c\u65f6\u5c31\u4f1a\u628a StringRep * \u8f6c\u5316\u4e3a UCP, \u518d\u8d4b\u503c"},{"location":"oop/Smart/lec/#main","title":"Main","text":"#include<iostream>\nusing namespace std;\n#include \"String.h\"\nint main(){\n cout << \"Hello\\n\";\n /**\n * 1. Construct a StringRep object with \"Hello\"\n * 2. Construct a UCP<StringRep> object with the StringRep object\n * 3. overlaoded '=' operator for UCP<StringRep> object\n * (m_rep = new StringRep(\"Hello\");)\n * 4. Destructor for UCP<StringRep> object\n */\n String a = \"Hello\";\n /**\n * Copy constructor for String object\n * 1.String::String(const String& s) : m_rep(s.m_rep) { //\u505a UCPointer<StringRep> \u7684\u62f7\u8d1d\u6784\u9020} \n * 2.Copy constructor for UCP<StringRep> object\n * (1) m_pObj = p.m_pObj; \n * (2)increment(); \n */\n String b=a;\n /**\n * Overloaded '=' operator for String object\n * 1.Overloaded '=' operator for UCP<StringRep> object\n * So actually nothing beacuse b and a are pointing to the same object\n */\n b = a;\n b = 'bye';\n /**\n * Overloaded '=' operator for String object\n * 1.Overloaded '=' operator for UCP<StringRep> object\n */\n b = a;\n cout(a==b)<<endl;\n}\n
\u5728UCPointer\u505aincrease
\u548cdecrease
\u5728UCObject\u5224\u65ad\u662f\u5426ref=0
\u4e4b\u540e\u5220\u9664
\u5728 C++ \u4e2d\uff0c\u6709\u56db\u79cd\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\uff1astatic_cast
\u3001dynamic_cast
\u3001const_cast
\u548c reinterpret_cast
\u3002\u5b83\u4eec\u5404\u81ea\u6709\u4e0d\u540c\u7684\u7528\u9014\u548c\u9650\u5236\u3002\u4ee5\u4e0b\u662f\u5bf9\u5b83\u4eec\u7684\u8be6\u7ec6\u89e3\u91ca\uff1a
static_cast
","text":"static_cast
\u7528\u4e8e\u5728\u76f8\u5173\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u663e\u5f0f\u8f6c\u6362\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a
int
\u5230 float
\u3002void*
\u6307\u9488\u8f6c\u6362\u56de\u539f\u7c7b\u578b\u3002#include <iostream>\nusing namespace std;\n\nclass Base {};\nclass Derived : public Base {};\n\nint main() {\n int a = 10;\n double b = static_cast<double>(a); // \u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\n cout << b << endl;\n\n Derived d;\n Base* basePtr = static_cast<Base*>(&d); // \u5411\u4e0a\u8f6c\u6362\n Derived* derivedPtr = static_cast<Derived*>(basePtr); // \u5411\u4e0b\u8f6c\u6362\n\n void* voidPtr = static_cast<void*>(&d); // \u8f6c\u6362\u4e3a void*\n Derived* derivedPtr2 = static_cast<Derived*>(voidPtr); // \u8f6c\u6362\u56de\u539f\u7c7b\u578b\n}\n
"},{"location":"oop/Smart/lec/#2-dynamic_cast","title":"2. dynamic_cast
","text":"dynamic_cast
\u7528\u4e8e\u5728\u7c7b\u5c42\u6b21\u7ed3\u6784\u4e2d\u8fdb\u884c\u5b89\u5168\u7684\u5411\u4e0b\u8f6c\u6362\uff08\u4ece\u57fa\u7c7b\u5230\u6d3e\u751f\u7c7b\uff09\uff0c\u5e76\u4e14\u53ea\u80fd\u7528\u4e8e\u6709\u865a\u51fd\u6570\u7684\u591a\u6001\u7c7b\u578b\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u6307\u9488\u7c7b\u578b\u4f1a\u8fd4\u56de nullptr
\uff0c\u5f15\u7528\u7c7b\u578b\u4f1a\u629b\u51fa std::bad_cast
\u5f02\u5e38\u3002
#include <iostream>\nusing namespace std;\n\nclass Base {\npublic:\n virtual ~Base() {}\n};\n\nclass Derived : public Base {};\n\nint main() {\n Base* basePtr = new Derived();\n Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);\n if (derivedPtr) {\n cout << \"dynamic_cast \u6210\u529f\" << endl;\n } else {\n cout << \"dynamic_cast \u5931\u8d25\" << endl;\n }\n\n delete basePtr;\n}\n
"},{"location":"oop/Smart/lec/#3-const_cast","title":"3. const_cast
","text":"const_cast
\u7528\u4e8e\u5728\u76f8\u540c\u7c7b\u578b\u4e4b\u95f4\u53bb\u6389\u6216\u6dfb\u52a0 const
\u6216 volatile
\u5c5e\u6027\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u53bb\u6389 const
\u5c5e\u6027\uff0c\u4ee5\u4fbf\u4fee\u6539\u539f\u672c\u662f const
\u7684\u6570\u636e\u3002
#include <iostream>\nusing namespace std;\n\nvoid modify(const int* p) {\n int* modifiable = const_cast<int*>(p);\n *modifiable = 20;\n}\n\nint main() {\n const int a = 10;\n cout << \"\u4fee\u6539\u524d: \" << a << endl;\n modify(&a);\n cout << \"\u4fee\u6539\u540e: \" << a << endl; // \u672a\u5b9a\u4e49\u884c\u4e3a\n}\n
"},{"location":"oop/Smart/lec/#4-reinterpret_cast","title":"4. reinterpret_cast
","text":"reinterpret_cast
\u7528\u4e8e\u5728\u4e0d\u540c\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u4f4e\u7ea7\u522b\u7684\u3001\u53ef\u80fd\u4e0d\u5b89\u5168\u7684\u8f6c\u6362\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u6307\u9488\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u5b83\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u5b9e\u9645\u7684\u6570\u636e\u8f6c\u6362\uff0c\u53ea\u662f\u7b80\u5355\u5730\u91cd\u65b0\u89e3\u91ca\u4f4d\u6a21\u5f0f\u3002
#include <iostream>\nusing namespace std;\n\nint main() {\n int a = 42;\n void* ptr = &a;\n int* intPtr = reinterpret_cast<int*>(ptr);\n cout << *intPtr << endl;\n\n // \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u6307\u9488\n intptr_t intVal = 0x12345678;\n void* voidPtr = reinterpret_cast<void*>(intVal);\n cout << voidPtr << endl;\n\n // \u5c06\u6307\u9488\u8f6c\u6362\u4e3a\u6574\u6570\n intVal = reinterpret_cast<intptr_t>(voidPtr);\n cout << std::hex << intVal << endl;\n}\n
"},{"location":"oop/Smart/lec/#_1","title":"\u603b\u7ed3","text":"static_cast
\uff1a\u7528\u4e8e\u76f8\u5173\u7c7b\u578b\u4e4b\u95f4\u7684\u663e\u5f0f\u8f6c\u6362\uff0c\u7f16\u8bd1\u65f6\u68c0\u67e5\uff0c\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002dynamic_cast
\uff1a\u7528\u4e8e\u5b89\u5168\u7684\u5411\u4e0b\u8f6c\u6362\uff0c\u591a\u6001\u7c7b\u578b\u4e2d\u4f7f\u7528\uff0c\u8fd0\u884c\u65f6\u68c0\u67e5\uff0c\u8f6c\u6362\u5931\u8d25\u65f6\u8fd4\u56de nullptr
\u6216\u629b\u51fa\u5f02\u5e38\u3002const_cast
\uff1a\u7528\u4e8e\u6dfb\u52a0\u6216\u53bb\u6389 const
\u6216 volatile
\u5c5e\u6027\uff0c\u4e0d\u6539\u53d8\u5e95\u5c42\u6570\u636e\u3002reinterpret_cast
\uff1a\u7528\u4e8e\u5728\u4e0d\u540c\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u4f4e\u7ea7\u522b\u7684\u8f6c\u6362\uff0c\u4e0d\u5b89\u5168\uff0c\u53ea\u662f\u91cd\u65b0\u89e3\u91ca\u4f4d\u6a21\u5f0f\u3002void f(){\n Stash students();\n}\n
Stash students();
is a function prototype, while students
is a function that returns a Stash
object.#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(){cout<<\"A()\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n};\nvoid f(A aa){\n cout << \"aa: \"<<&aa << endl;\n cout << aa.getVal() << endl;\n}\nint main(){\n A a;\n a.setVal(10);\n cout << \"a: \"<<&a << endl;\n cout << \"-------------------\\n\";\n f(a);\n cout << \"-------------------\\n\";\n\n return 0;\n}\n
A()\na: 0x16dc9f088\n-------------------\naa: 0x16dc9f068\n10\n~A()\n-------------------\n~A()\n
Only one time of constructor is called.
#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i){cout<<i<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n};\nvoid f(A aa){\n cout << \"aa: \"<<&aa << endl;\n cout << aa.getVal() << endl;\n}\nint main(){\n A a(10);\n a.setVal(10);\n cout << \"a: \"<<&a << endl;\n cout << \"-------------------\\n\";\n f(a);\n cout << \"-------------------\\n\";\n\n return 0;\n}\n
Without default constructor, still one time of constructor is called.
#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i){cout<<i<<endl;}\n A(const A& r):i(r.i){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n};\nvoid f(A aa){\n cout << \"aa: \"<<&aa << endl;\n cout << aa.getVal() << endl;\n}\nint main(){\n A a(10);\n a.setVal(10);\n cout << \"a: \"<<&a << endl;\n cout << \"-------------------\\n\";\n f(a);\n cout << \"-------------------\\n\";\n\n return 0;\n}\n
10\na: 0x16b763088\n-------------------\nA(const A&)\naa: 0x16b763068\n10\n~A()\n-------------------\n~A()\n
in f(), A(const A&)
is called.
A(const A&)
is a copy constructor.Person baby_a(\"Fred\");\n// these use the copy ctor\nPerson baby_b = baby_a; // not an assignment\nPerson baby_c( baby_a ); // not an assignment\n
\u5982\u679c\u6709\u6210\u5458\u53d8\u91cf\u662f\u6307\u9488\uff0c\u4f1a\u548c\u539f\u6765\u5bf9\u8c61\u4e00\u6837\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58! \u5982\u679c\u6709\u4e00\u4e2a\u5bf9\u8c61\u88ab\u6790\u6784\uff0c\u90a3\u4e48\u8fd9\u5757\u5185\u5b58\u5c31\u88ab delete, \u8fd9\u5c31\u53d8\u6210\u4e86\u65e0\u6548\u5185\u5b58!
#include<iostream>\nusing namespace std;\nclass A{\n int i;\n string s;\npublic:\n A(int i){cout<<i<<endl;}\n A(const A& r):i(r.i),s(r.s){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n};\nA f(A aa){\n cout << \"------------\"<<endl;\n A bb(20);\n cout << \"aa: \"<<&aa << endl;\n cout << aa.getVal() << endl;\n cout << \"Destructor for aa\"<<endl;\n return bb;\n}\nint main(){\n A a(10);\n a.setVal(10);\n cout << \"a: \"<<&a << endl;\n cout << \"-------------------\\n\";\n A d = f(a);\n cout << \"-------------------\\n\";\n d.setVal(30);\n return 0;\n}\n
10\na: 0x16b247190\n-------------------\nA(const A&)\n------------\n20\naa: 0x16b247130\n10\nDestructor for aa\n~A()\n-------------------\n~A()\n~A()\n
\u7f16\u8bd1\u5668\u5728return\u5904\u4f18\u5316\u4e86\uff0c\u4e5f\u5c31\u662f\u76f4\u63a5\u628abb\u653e\u5728\u8981\u8fd4\u56de\u7684\u5730\u65b9\uff0c\u800c\u4e0d\u662f\u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u62f7\u8d1d\u5230\u8fd4\u56de\u7684\u5730\u65b9
C++ \u4f1a\u505a\u62f7\u8d1d\u6784\u9020\uff0c\u5982\u679c\u7c7b\u4e2d\u5168\u90e8\u90fd\u662f\u5bf9\u8c61\uff0c\u90a3\u4e48\u662fok\u7684\uff0c\u4f46\u662f\u5982\u679c\u6709\u6307\u9488\uff1a
way in
void f(Student i)
;void f(Student *p);
void f(Student& i);
way out
Student f()
;Student* f()
;Student& f()
;char *foo() {\n char *p; \n p = new char[10];\n strcpy(p, \"something\");\n return p;\n}\nvoid bar() {\n char *p = foo();\n printf(\"%s\", p);\n delete p;\n}\n
delete p
? 1. \u5982\u679c\u4e0d\u505a\uff0c\u51fa\u4e86\u51fd\u6570\u5c31\u627e\u4e0d\u5230\u8fd9\u4e2a\u7a7a\u95f4\u4e86\uff0c\u5185\u5b58\u6cc4\u6f0f:\u5982\u679c\u4e00\u4e2a\u4e00\u76f4\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u4e2d\u6709\u5185\u5b58\u6cc4\u6f0f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u8d8a\u6765\u8d8a\u6162\uff0c\u56e0\u4e3a\u5185\u5b58\u8d8a\u6765\u8d8a\u5c11\uff01 2. \u90a3\u6211\u600e\u4e48\u77e5\u9053\u6211\u5e94\u8be5delete\u5462\uff1f\u5982\u679c\u662fnew\u51fa\u6765\u7684\uff0c\u90a3\u4e48\u5c31\u8981delete\uff0c\u5982\u679c\u662f\u6307\u5411\u5168\u5c40\u7684\uff0c\u90a3\u4e48\u5c31\u4e0d\u7528deletewhile(){\n Person p;\n p.read();\n s.put(&p);\n}\n
C++\u5bf9\u672c\u5730\u53d8\u91cf\u4f1a\u81ea\u52a8\u56de\u6536\uff0c\u4f46\u662f\u5bf9new\u51fa\u6765\u7684\u4e0d\u4f1a\u81ea\u52a8\u56de\u6536\uff0c\u6240\u4ee5\u5199\u7684\u65f6\u5019\u5c3d\u91cf\u591a\u7528\u672c\u5730\u53d8\u91cf \u8fd4\u56de\u5bf9\u8c61\u7684\u8bdd\uff0c\u8981\u4e0d\u62f7\u8d1d\u6784\u9020\uff0c\u8981\u4e0d\u79fb\u52a8\u6784\u9020\uff0c\u603b\u4f1a\u88ab\u56de\u6536\uff0c\u4e0d\u7528\u62c5\u5fc3\u5185\u5b58\u6cc4\u6f0f
"},{"location":"oop/copy/lec/#move-ctor","title":"Move Ctor","text":""},{"location":"oop/copy/lec/#left-value-and-right-value","title":"Left Value and Right Value","text":"*
\u3001 []
\u8fd0\u7b97\u7684\u7ed3\u679c&&
\u6210\u4e3a\u53f3\u503c\u5f15\u7528 int x=20; // \u5de6\u503c\nint&& rx = x * 2; // x*2\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u53f3\u503c\uff0crx\u5ef6\u957f\u5176\u2f63\u547d\u5468\u671f\nint y = rx + 2; // \u56e0\u6b64\u4f60\u53ef\u4ee5\u91cd\u7528\u5b83:42\nrx = 100; // \u4e00\u65e6\u4f60\u521d\u59cb\u5316\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u5c31\u6210\u4e3a\u4e86\u4e00\u4e2a\u5de6\u503c\uff0c\u53ef\u4ee5\u88ab\u8d4b\u503c\nint&& rrx1 = x; // \u975e\u6cd5:\u53f3\u503c\u5f15\u7528\u65e0\u6cd5\u88ab\u5de6\u503c\u521d\u59cb\u5316\nconst int&& rrx2 = x; // \u975e\u6cd5:\u53f3\u503c\u5f15\u7528\u65e0\u6cd5\u88ab\u5de6\u503c\u521d\u59cb\u5316\n
x
\u4e0d\u5b58\u5728\u4e86\uff0c\u53f3\u503c\u5f15\u7528\u4f9d\u7136\u53ef\u4ee5\u4f7f\u7528int x = 10;\nint &&a = x + 2;\nint y = a + 2;\ncout << y << endl;\nx = 5;\ncout << y << endl;\na = 40;\ncout << y << endl;\ncout << a << endl;\nint &&b = x; // ERR: \u53f3\u503c\u5f15\u7528\u4e0d\u80fd\u7ed1\u5de6\u503c\uff08\u4e3a\u4e86\u533a\u5206\nint &&b = x+0; // ok\n
c++11
:\u63a5\u53d7const\u5de6\u503c\u7684\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u53f3\u503c void fun(int &lref) {\n cout << \"l-value\" << endl;\n}\nvoid fun(int &&rref) {\n cout << \"r-value\" << endl;\n}\nint main() {\n int x = 10;\n fun(x); // l value\n fun(10); // r value\n}\n
Move ctor is a special constructor that takes an rvalue reference to an object of the same class and moves the resource owned by the rvalue reference to the object being constructed.
\u5982\u679c\u6709\u4e00\u4e2a\u5bf9\u8c61\uff0c\u91cc\u9762\u6709\u6307\u9488\u6307\u5411\u4e00\u5757\u5185\u5b58
(nullptr)
.DynamicArray(DynamicArray&& rhs) : m_size{rhs.m_size}, m_array{rhs.m_array}\n{\n rhs.m_size = 0;\n rhs.m_array = nullptr;\n cout << \"Move constructor: dynamic array is moved!\\n\";\n}\n
f(P p);
: \u4f20\u5165\u4e00\u4e2a\u53f3\u503c\uff0c\u4f1a\u8c03\u7528\u53f3\u503c\u5f15\u7528\u7684\u6784\u9020\u51fd\u6570f(P &&p);
: \u5982\u679c\u63d0\u4f9b\u4e86\u79fb\u52a8\u6784\u9020\uff0c\u53ef\u4ee5\u4f20\u4e00\u4e2a\u53f3\u503cf(const P &p);
f
\u5185\u7684\u672c\u5730\u53d8\u91cfstd::move
","text":"vector<int> v1{1, 2, 3, 4};\nvector<int> v2 = v1;\nvector<int> v3 = std::move(v1);// \u6b64\u65f6\u8c03\u7528\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\n
std::move
\u5c06 v1 \u8f6c\u5316\u4e3a\u53f3\u503c\uff0c\u4ece\u2f7d\u6fc0\u53d1 v3 \u7684\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0c\u5b9e\u73b0\u79fb\u52a8\u8bed\u4e49//\u5c0f\u62ec\u53f7\u521d\u59cb\u5316\nstring str(\"hello\");\n//\u7b49\u53f7\u521d\u59cb\u5316\nstring str = \"hello\";\n//\u5927\u62ec\u53f7\u521d\u59cb\u5316\nstruct Studnet{\n char *name;\n int age;\n};\nStudnet s = {\"dablelv\", 18};//Plain of Data\u7c7b\u578b\u5bf9\u8c61\nStudnet sArr[] = {{\"dablelv\", 18}, {\"tommy\", 19}}; //POD\u6570\u7ec4\n
"},{"location":"oop/copy/lec/#_2","title":"\u5217\u8868\u521d\u59cb\u5316","text":"class Test{\n int a;\n int b;\n public:\n Test(int i, int j);\n};\nTest t{0, 0}; //C++11 only\uff0c\u76f8\u5f53\u4e8e Test t(0,0);\nTest *pT = new Test{1, 2}; //C++11 only\uff0c\u76f8\u5f53\u4e8e Test* pT=new Test(1,2);\nint *a = new int[3]{1, 2, 0}; //C++11 only\n
"},{"location":"oop/copy/lec/#delegating-ctor","title":"Delegating Ctor","text":"class class_c{\npublic:\n int max;\n int min;\n int middle;\n class c(int my_max){\n max = my_max>0?my_max:10;\n }\n class_c(int my_max, int my_min):class_c(my_max){\n min = my_min>0&&my_min<max?my_min:1;\n }\n class_c(int my_max, int my_min, int my_middle):class_c(my_max, my_min){\n middle = my_middle>min&&my_middle<max?my_middle:(min+max)/2;\n }\n};\nint main(){\n class_c c(10, 5, 7);\n}\n
"},{"location":"oop/copy/lec/#overloaded-operators","title":"Overloaded Operators","text":"Types that cannot be overloaded:
::
.
*
?
:
sizeof
typeid
new
delete
new[]
delete[]
static_cast
dynamic_cast
const_cast
reinterpret_cast
Only existing operators can be overloaded.
operator
followed by the operator to be overloadedclass A {\npublic:\n A(int ii):i(ii){}\n int get() {return i;}\n /* \u8fd4\u56de\u7684\u4e00\u5b9a\u662f A \u7684\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61 */\n const A operator+(const A &that) const {\n A c(this->i+that.i); /* \u8fd9\u91cc\u53ef\u4ee5\u8bbf\u95ee that. \u79c1\u6709\u662f\u9488\u5bf9\u7c7b\u7684\uff0c\u4e0d\u662f\u9488\u5bf9\u5bf9\u8c61\u7684\u3002 */\n return c;\n }\n //!\u8fd4\u56de\u7684\u5e94\u8be5\u662f\u53f3\u503c\uff0c\u6240\u4ee5const\uff01\n private:\n int i;\n}\nint main() {\n A a = 6;\n A b = 7;\n A c = a + b; /* a + 9 \u4e5f\u662f\u53ef\u4ee5\u7684\uff1b\u4f46 9 + a \u4e0d\u884c */\n cout << c.get() << endl; /* \u8f93\u51fa 13 */\n}\n
const
?const
member functions can be called on const
objectsconst
objects can only call const
member functionsconst
member functions can't change the objectconst
member functions can't call non-const member functionsoperator+
#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i):i(i){cout<<\"A()\"<<i<<endl;}\n A(const A& r):i(r.i){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n A operator+(const A& r){\n cout << \"operator+\" << endl;\n A a(this->i+r.i);\n return a;\n }\n};\nint main(){\n A a(10);\n A b(20);\n A c = a+b;\n}\n/*\nA()10\nA()20\noperator+\nA()30\n~A()\n~A()\n~A()\n */\n
A c = a+b;
is equivalent to A c = a.operator+(b);
A c = a+3;
is equivalent to A c = a.operator+(3);
And when using '3' as the second operand\uff1a we construct a temporary object of A(3) and pass it to the operator+ function.
#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i):i(i){cout<<\"A()\"<<i<<endl;}\n A(const A& r):i(r.i){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal(){return i;}\n void setVal(int i){this->i=i;}\n A operator+(const A& r){\n cout << \"operator+\" << endl;\n A a(this->i+r.i);\n return a;\n }\n};\nint main(){\n A a(10);\n A b(20);\n A c = a+3;\n}\n/*\nA()10\nA()20\nA()3\noperator+\nA()13\n~A()\n~A()\n~A()\n~A()\n*/\n
However, if we want to use 3+a
, member function can't be used!!!
friend
of the class#include<iostream>\nusing namespace std;\nclass A{\n int i;\npublic:\n A(int i):i(i){cout<<\"A()\"<<i<<endl;}\n A(const A& r):i(r.i){cout<<\"A(const A&)\"<<endl;}\n virtual ~A(){cout<<\"~A()\"<<endl;}\n int getVal()const{return i;}\n void setVal(int i){this->i=i;}\n};\nA operator+(const A& r,const A&l){\n cout << \"+outside\" << endl;\n A temp(r.getVal()+l.getVal());\n return temp;\n}\nint main(){\n A a(10);\n A b(20);\n A c = 3+a;\n}\n/*\nA()10\nA()20\nA()3\n+outside\nA()13\n~A()\n~A()\n~A()\n~A()\n*/\n
=
,()
,[]
,->
and ->*
must be members.Note : What if A class don't have getVal()
function? * We can use friend
to access private members of class A.
friend const A operator+(const A& r,const A&l); \n
"},{"location":"oop/copy/lec/#the-prototype-of-operators","title":"The Prototype of operators","text":""},{"location":"oop/copy/lec/#-","title":"+
-
*
/
%
^
&
|
~
:","text":"const T operatorX(const T& l, const T& r);
!
&&
||
==
!=
<
>
<=
>=
:","text":"bool operatorX(const T& l);
==
!=
<
>
<=
>=
!=
in terms of ==
>
>=
<
in terms of <
class Integer { \n public: \n ... \n bool operator==( const Integer& rhs ) const; \n bool operator!=( const Integer& rhs ) const; \n bool operator<( const Integer& rhs ) const; \n bool operator>( const Integer& rhs ) const; \n bool operator<=( const Integer& rhs ) const; \n bool operator>=( const Integer& rhs ) const; \n }\nbool Integer::operator==( const Integer& rhs ) const { \n return i == rhs.i; \n} \n// implement lhs != rhs in terms of !(lhs == rhs) \nbool Integer::operator!=( const Integer& rhs ) const { \n return !(*this == rhs); \n} \nbool Integer::operator<( const Integer& rhs ) const { \n return i < rhs.i; \n}\n// implement lhs > rhs in terms of lhs < rhs \nbool Integer::operator>( const Integer& rhs ) const { \n return rhs < *this; \n} \n// implement lhs <= rhs in terms of !(rhs < lhs) \nbool Integer::operator<=( const Integer& rhs ) const { \n return !(rhs < *this); \n} \n// implement lhs >= rhs in terms of !(lhs < rhs) \nbool Integer::operator>=( const Integer& rhs ) const { \n return !(*this < rhs); \n}\n
[ ]
","text":"a[6]=7
E& T::operator[](int index);
#include <iostream>\nclass T {\nprivate:\n int data[10];\npublic:\n // \u91cd\u8f7d operator[]\n int& operator[](int index) {\n return data[index];\n }\n};\nint main() {\n T obj;\n // \u4f7f\u7528 operator[] \u4fee\u6539\u5bf9\u8c61\u7684\u503c\n obj[6] = 7;\n std::cout << obj[6] << std::endl; // \u8f93\u51fa 7\n return 0;\n}\n
class A {\npublic:\n A(int s):size(s){\n buf = new int[s];\n }\n int& operator [] (int idx){\n return buf[index];\n }\n virtual ~A(){\n delete [] buf;\n }\nprivate:\n int size;\n int *buf;\n}\n
++
and --
","text":"const Integer& operator++(); // prefix
++a.f()
,++a=5
\u8fd9\u6837\u7684\u64cd\u4f5c class Integer{\npublic:\n const Integer& operator++(); // prefix \n const Integer operator++(int); // postfix\n const Integer& operator--(); // prefix\n const Integer operator--(int); // postfix\n};\nconst Integer& Integer::operator++() { \n *this += 1; // increment have to overload +=\n return *this; // fetch \n} \n// int argument not used so leave unnamed so \n// won't get compiler warnings \nconst Integer Integer::operator++( int ){ \n Integer old(*this); // fetch \n ++(*this); // increment \u8c03\u7528\u4e86\u521a\u521a\u7684\u51fd\u6570\n return old; // return \n}\n\nint main(){\n ++x; // calls x.operator++(); \n x++; // calls x.operator++(0); \n --x; // calls x.operator--(); \n x--; // calls x.operator--(0); \n\n}\n
friend ostream& operator<<(ostream& os, const A& a);
ostream& operator <<(ostream & os, const A &a)\n{\n cout << a.size() << endl;\n return os;\n}\n
ostream& manip(ostream& out) { \n ... \n return out; \n} \nostream& tab ( ostream& out ) { \n return out << '\\t'; \n} \ncout << \"Hello\" << tab << \"World!\" << endl;\n
operator=
\uff09\u65f6\u901a\u5e38\u8fd4\u56de\u4e00\u4e2a\u5bf9\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u8fd9\u79cd\u505a\u6cd5\u6709\u51e0\u4e2a\u91cd\u8981\u7684\u539f\u56e0\uff1a","text":"\u652f\u6301\u94fe\u5f0f\u8d4b\u503c\uff1a \u901a\u8fc7\u8fd4\u56de\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u53ef\u4ee5\u652f\u6301\u94fe\u5f0f\u8d4b\u503c\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a
a = b = c;\n
\u5982\u679c\u8d4b\u503c\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u662f\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\uff0cb = c
\u4f1a\u8fd4\u56de b
\uff0c\u7136\u540e a = b
\u5c31\u53ef\u4ee5\u6b63\u5e38\u8fdb\u884c\u3002 \u6548\u7387\u8003\u8651\uff1a \u8fd4\u56de\u5f15\u7528\u907f\u514d\u4e86\u8fd4\u56de\u5bf9\u8c61\u65f6\u7684\u62f7\u8d1d\u5f00\u9500\u3002\u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\u4f1a\u5bfc\u81f4\u5bf9\u8c61\u7684\u590d\u5236\uff0c\u4ece\u800c\u589e\u52a0\u4e0d\u5fc5\u8981\u7684\u5f00\u9500\u3002\u800c\u8fd4\u56de\u5f15\u7528\u53ea\u9700\u8fd4\u56de\u4e00\u4e2a\u5730\u5740\uff0c\u6ca1\u6709\u989d\u5916\u7684\u6027\u80fd\u635f\u8017\u3002
\u4e0e\u5185\u7f6e\u7c7b\u578b\u7684\u884c\u4e3a\u4e00\u81f4\uff1a \u5185\u7f6e\u7c7b\u578b\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u4e5f\u8fd4\u56de\u5de6\u503c\uff08\u5373\u5f53\u524d\u5bf9\u8c61\u672c\u8eab\uff09\uff0c\u901a\u8fc7\u8ba9\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u8d4b\u503c\u8fd0\u7b97\u7b26\u884c\u4e3a\u4e00\u81f4\uff0c\u4ee3\u7801\u7684\u884c\u4e3a\u548c\u9884\u671f\u4e00\u81f4\u3002
\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u6b63\u786e\u5730\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff1a
#include <iostream>\n#include <cstring>\n\nclass STRING {\n char *m_pStr;\n int m_len;\npublic:\n // \u6784\u9020\u51fd\u6570\n STRING(char *str = NULL) {\n if (str != NULL) {\n m_len = strlen(str) + 1;\n m_pStr = new char[m_len];\n strcpy(m_pStr, str);\n } else {\n m_len = 0;\n m_pStr = NULL;\n }\n }\n\n // \u62f7\u8d1d\u6784\u9020\u51fd\u6570\n STRING(const STRING& other) {\n m_len = other.m_len;\n if (m_len > 0) {\n m_pStr = new char[m_len];\n strcpy(m_pStr, other.m_pStr);\n } else {\n m_pStr = NULL;\n }\n }\n\n // \u6790\u6784\u51fd\u6570\n ~STRING() {\n delete[] m_pStr;\n }\n\n // \u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d\n STRING& operator=(const STRING& other) {\n if (this != &other) { // \u9632\u6b62\u81ea\u6211\u8d4b\u503c\n delete[] m_pStr;\n m_len = other.m_len;\n if (m_len > 0) {\n m_pStr = new char[m_len];\n strcpy(m_pStr, other.m_pStr);\n } else {\n m_pStr = NULL;\n }\n }\n return *this; // \u8fd4\u56de\u5f53\u524d\u5bf9\u8c61\u7684\u5f15\u7528\n }\n\n // \u53cb\u5143\u51fd\u6570\uff0c\u7528\u4e8e\u8f93\u51fa\u5b57\u7b26\u4e32\n friend std::ostream& operator<<(std::ostream& out, const STRING& s);\n};\n\nstd::ostream& operator<<(std::ostream& out, const STRING& s) {\n if (s.m_pStr != NULL) {\n out << s.m_pStr;\n }\n return out;\n}\n\nint main() {\n STRING s1((char *)\"Hello\"), s2, s3;\n s3 = s2 = s1; // \u94fe\u5f0f\u8d4b\u503c\n std::cout << \"s1: \" << s1 << std::endl;\n std::cout << \"s2: \" << s2 << std::endl;\n std::cout << \"s3: \" << s3 << std::endl;\n return 0;\n}\n
"},{"location":"oop/copy/lec/#copying-vs-initialization","title":"Copying vs Initialization","text":""},{"location":"oop/copy/lec/#assignment-operator","title":"Assignment Operator","text":"\u5148\u91ca\u653e\u4e86\u539f\u6709\u7684\u5185\u5b58\uff0c\u7136\u540e\u53c8\u8bd5\u56fe\u4f7f\u7528\u5df2\u7ecf\u88ab\u91ca\u653e\u7684\u5185\u5b58\u8fdb\u884c\u8d4b\u503c\uff0c\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f
\u5728\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u9632\u6b62\u81ea\u8d4b\u503c\u7684\u60c5\u51b5\u53d1\u751f!! \u5982\u679c\u4e0d\u52a0\u4ee5\u5904\u7406\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u6216\u5185\u5b58\u6cc4\u6f0f\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u4f1a\u5728\u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u5b9e\u73b0\u4e2d\u6dfb\u52a0\u81ea\u8d4b\u503c\u68c0\u67e5\uff0c\u786e\u4fdd\u5728\u81ea\u8d4b\u503c\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u5bf9\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\u3002
Be sure to assign to all data members
*this
// \u91cd\u8f7d\u8d4b\u503c\u8fd0\u7b97\u7b26\n MyClass& operator=(const MyClass& other) {\n if (this != &other) { // \u68c0\u67e5\u662f\u5426\u81ea\u8d4b\u503c\n delete data; // \u91ca\u653e\u539f\u6709\u5185\u5b58\n data = new int(*other.data); // \u6df1\u62f7\u8d1d\u6570\u636e\n }\n return *this; // \u8fd4\u56de\u5f15\u7528\n }\n
class PathName {\n string name;\npublic:\n // or could be multi-argument with defaults\n // Or Type Conversion\n PathName(const string&);\n ~ PathName();\n};\n...\nstring abc(\"abc\");\nPathName xyz(abc); // OK!\nxyz = abc; // OK abc => PathName Type Conversion\n
\u5728\u6784\u9020\u51fd\u6570\u524d\u9762\u52a0\u4e0a explicit
\u5173\u952e\u5b57:
explicit PathName(const string&);\n
\u8fd9\u65f6\u6211\u4eec\u7684\u6784\u9020\u51fd\u6570\u53ea\u80fd\u7528\u6765\u505a\u6784\u9020\uff01\uff01\uff01
Compiler will use it as a type conversion from
class Rational {\npublic:\n ...\n operator double() const; // Rational to double\n}\nRational::operator double() const { \n return numerator_/(double)denominator_;\n}\nRational r(1,3); \ndouble d = r; // r=>double\n
\u4e0d\u9700\u8981\u5199\u8fd4\u56de\u7c7b\u578b\u3002 \u5982\u679c\u6211\u4eec\u5728\u91cd\u8f7d\u7684\u8fd0\u7b97\u7b26\u524d\u9762\u52a0\u4e0a explicit, \u90a3\u4e48\u6211\u4eec\u5c31\u5fc5\u987b\u5199\u4f5c double d = (double)r;
operator C()
\u7684\u91cd\u8f7d\u3002\u5982\u679c\u4e24\u4e2a\u90fd\u6709\uff0c\u7f16\u8bd1\u5668\u4f1a\u51fa\u9519\u3002&&
,||
,,
\u6700\u597d\u4e0d\u8981\u91cd\u8f7dInheritance is the ability to define the behavior or implementation of one class as a superset of another class.
Take a closer look at an example.
class Database {\n vector<CD> cds;\n vector<DVD> dvds;\n public:\n void addCD(CD &aCD);\n void addDVD(DVD &aDVD);\n void list() {\n for (auto x:cds) { cd.print(); }\n for (auto x:dvds) { x.print(); }\n }\n}\n
Solution
"},{"location":"oop/inheritance/lec/#basic-declaration","title":"Basic Declaration","text":"use std : avoid include a lot of header files.
#include<string>\nusing namespace std;\n\n//or\n\nstd::string\n
::
operator.class Employee{\n public:\n Employee(const std::string& name,const std::string& ssn);\n const std :: string & get_name() const;\n const string& getName()const{return name;}\n const string& getSSN()const{return ssn;}\n void print()const;\n //void print(std::ostream& out)const;\n void print(const std::string & msg)const;\n protected: // Self and descendant can access\n std::string m_name;\n std::string m_mssn;\n};\nEmployee::Employee( const string& name, const string& ssn ):m_name(name),m_ssn( ssn){// initializer list sets up the values!}\n\ninline const std::string& Employee::get_name()const{\n return m_name;\n}\ninline void Employee::print(std::ostream& out)const{\n out << m_name<<endl;\n out << m_ssn<<endl;\n}\ninline void Employee::print(std::ostream& out,const std::string& msg)const{\n out << msg << endl;\n print(out);\n}\nclass Manager : public Employee {\n public:\n Manager(const std::string& _name, const std::string& _ssn, const std::string& _title);\n const std::string& get_title() const;\n void print() const;\n private:\n std::string m_title;\n};\nManager::Manager(const string& _name, const string& _ssn, const string& _title = \"\" ):Employee(_name, _ssn), m_title( _title ) {}\n
Better to initialize the parent class by calling its constructor instead of
Manager(const std::string& name, const std::string& ssn, const std::string&title):name(_name),ssn(_ssn)mtitle(_title){}\n
void Manager::print()const{\n Employee::print();\n cout << title << endl;\n}\n
#include \"employee.h\"\n#include \"manager.h\"\nint main()\n{\n Employee p1(\"John Smith\",\"123123\");\n Manager p2(\"Tom Jordan\", \"3267\",\"Mr.\");\n p1.print(); \n p2.print();\n p1.print(\"Welcome:\");\n //p2.print(\"Welcome:\"); \u62a5\u9519\n\n\n Employee *p = &p1;\n p->print();\n p = &p2; \n p->print(); // \u6309 Employee \u91cc\u9762\u7684\u51fd\u6570\u8f93\u51fa\uff0c\u800c\u4e0d\u662f Manager\n}\n
cout<<name<<endl //supose name(private) is from parent class, ERROR\n
protected
: only descandents can access.\u5b50\u7c7b\u91cd\u65b0\u5b9a\u4e49\u4e86\u7236\u7c7b\u7684\u67d0\u4e2a\u51fd\u6570\uff0c\u5c31\u4f1a\u628a\u5176\u4ed6 overload \u7684\u51fd\u6570\u8986\u76d6\u6389\uff08C++ \u72ec\u6709\uff0c\u65e0\u6cd5\u89e3\u91ca\uff09\uff0c\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d, p2.print(\"welcome\")
\u4f1a\u62a5\u9519
\u8981\u8c03\u7528\u7236\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u8981 Employee::print()
.
Failed to inherit:
class Derived : public Base ...\nclass Derived : protected Base ...\nclass Derived : private Base ..\n
"},{"location":"oop/inheritance/lec/#polymorphism","title":"Polymorphism","text":"class A public B{\n\n};\nclass A protected B{\n\n};\nclass A private B{\n\n};\n
public
: A is a Bprotected
: A is a B and A's member is protected(for A's descandent)private
: A is a B and A's member is private(for A's descandent)Public Inheritance should imply substitution
If B is a A, you can use a B any where an A can be used. if B is a A, then everything that is true for A is also true of B.
Be careful if the substitution is not valid!
#include <iostream>\nusing namespace std;\nclass A{\n public:\n int i;\n void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;}\n};\nclass B:public A{\n public:\n int i;\n void f(){cout<<\"B::f()\"<<endl;}\n B(){i=20;cout<<\"B()\"<<i<<endl;}\n};\nint main(){\n B b;\n cout << \"finished creating b\"<<endl;\n cout << endl;\n\n A *p=&b;\n b.f();\n cout << endl;\n cout << \"which f() is called by A* p=&b; ?\"<<endl;\n p->f(); // A::f()\n cout << endl;\n cout<< sizeof(*p)<<endl;\n int *pi = (int*)p;\n cout << endl;\n\n cout<< pi[0] <<\",\"<<pi[1]<<endl;\n cout << p->i <<endl;\n cout << b.i<<endl;\n}\n
A()10\nB()20\nfinished creating b\n\nB::f()\n\nwhich f() is called by A* p=&b; ?\nA::f()\n\n4\n\n10,20\n10\n20\n
f()
function using pointer to ites parent class, it will call the parent class's function.<without virtual>
#include <iostream>\nusing namespace std;\nclass A{\n public:\n int i;\n virtual void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;}\n};\nclass B:public A{\n public:\n int i;\n void f(){cout<<\"B::f()\"<<endl;}\n B(){i=20;cout<<\"B()\"<<i<<endl;}\n};\nint main(){ \n B b;\n A *p=&b;\n B *q=&b;\n cout << sizeof(b)<<endl; \n cout << sizeof(A)<<endl; \n\n b.f();\n cout << \"When virtual is added it will call the derived class function f() else it will call the base class function f()\"<<endl;\n p->f();\n cout<< sizeof(*p)<<endl;\n cout<< sizeof(*q)<<endl;\n int *pi = (int*)p;\n int *qi = (int*)q;\n cout<< pi[0] <<\",\"<<pi[1]<<','<<pi[2]<<','<<pi[3]<<endl;\n cout<< qi[0] <<\",\"<<qi[1]<<','<<qi[2]<<','<<qi[3]<<endl;\n\n cout << \"p->i is still A's i\"<<endl;\n cout << p->i <<endl;\n cout << b.i<<endl;\n}\n
virtual
keyword, it will call the child class's function.A()10\nB()20\n16\n16\nB::f()\nWhen virtual is added it will call the derived class function f() else it will call the base class function f()\nB::f()\n16\n16\n12550336,1,10,20\n12550336,1,10,20\np->i is still A's i\n10\n20\n
"},{"location":"oop/inheritance/lec/#example-drawing-program","title":"Example : Drawing Program","text":"class XYPos{}\nclass Shape {\npublic: \n Shape();\n virtual ~Shape();\n virtual void render();\n void move (const XYPos&);\n virtual void resize();\nprotected:\n XYPos center;\n}\n
virtual
is used to define a function that can be overwritten in a derived class.class Ellipse : public Shape {\npublic:\n Ellipse(float maj, float minr);\n virtual void render(); // will define own\nprotected:\n float major_axis, minor_axis;\n};\nclass Circle : public Ellipse {\npublic:\n Circle(float radius) : Ellipse(radius, radius){}\n virtual void render();\n};\n
.render()
: static binding->render()
: dynamic binding&\u5f15\u7528\u8bbf\u95ee
: dynamic bindingvoid render(Shape* p) {\n p->render(); // calls correct render function\n} // for given Shape! void func() {\nEllipse ell(10, 20);\nell.render(); // static -- Ellipse::render();\nCircle circ(40);\ncirc.render(); // static -- Circle::render();\nrender(&ell); // dynamic -- Ellipse::render();\nrender(&circ); // dynamic -- Circle::render()\n
void render(Shape* p) {\n p->render(); // calls correct render function\n} \n
Shape *p
: polymofphic varaibleV table The V table is a table of the addresses of the virtual functions for that class.
Note : Pointer size is 8 bytes in 64-bit system. And We have to consider alignment.
Vtable is created at compile time. SO it is static.
int main(){ \n B b;\n A *p=&b;\n cout << sizeof(b)<<endl; \n cout << sizeof(A)<<endl; \n b.f();\n long long **vp = (long long**)p;\n void (*pf) () = (void (*)())(*(*vp));\n cout << \"-------------\"<<endl;\n pf();\n}\n
A()10\nB()20\n16\n16\nB::f()\n-------------\nB::f()\n
class A{\n public:\n int i;\n virtual void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;}\n void g(){f();}//this->f()\n};\n
p->g();\n
B::f()\n
Add
class A{\n public:\n int i;\n virtual void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;f();}\n void g(){f();}//this->f()\n};\n
While in constructor, it will call the parent class's function. A::f()
How dose vptr
come?
When the object is created, space will be allocated and the constructor will be called. When the constructor is called, the vptr will be set to the vtable
of the class!!!
Think about the order of the constructor:
When a descendent class is created, the parent class's constructor will be called first. So the vptr will be set to the parent class's vtable first. Then the descendent class's constructor will be called and the vptr will be set to the descendent class's vtable.
"},{"location":"oop/inheritance/lec/#what-happens-if","title":"What happens if ?","text":"Ellipse ell(10, 20);\nCircle circ(40);\nelly = circ; \n
elly
's vptr.a = b;\np = &a;\np -> f();\n
A::f()
p->f()
will call the parent class's function.What about?
Ellipse *elly = new Ellipse(20F,40F);\nCircle *circ = new Circle(60F);\nelly = circ;\n
elly
is lost.elly ->render();//Circle::render()
What about ?
void func(Ellipse &elly){\n elly.render()\n}\nCircle circ(60F);\nfunc(circ);\n
#include <iostream>\nusing namespace std;\nclass A{\n public:\n int i;\n virtual void f(){cout<<\"A::f()\"<<endl;}\n A(){i=10;cout<<\"A()\"<<i<<endl;f();}\n void g(){f();}//this->f()\n};\nclass B:public A{\n public:\n int i;\n void f(){cout<<\"B::f()\"<<endl;}\n B(){i=20;cout<<\"B()\"<<i<<endl;}\n};\nint main(){ \n cout << \"Creating B object\"<<endl;\n B b;\n cout << \"Creating A object\"<<endl;\n A a;\n\n A *p=&b;\n cout << \"----------\"<<endl;\n\n p->f();\n cout << sizeof(b)<<endl; \n cout << sizeof(A)<<endl; \n\n int *pi = (int*)p;\n cout<< long(pi[0]) <<\",\"<<pi[2]<<\",\"<<pi[3]<<endl;\n\n long long **vp = (long long**)(p);\n void (*pf) () = (void (*)())(*(*vp));\n pf();\n p->g();\n\n cout << \"-------------\"<<endl;\n a = b;\n p = &a;\n cout << \"This time p is pointing to A object and a=b is sliced from b to a(meaning that the B part of b is copied to a and original A is preserved)\"<<endl;\n\n pi = (int*)p;\n cout<< long(pi[0]) <<\",\"<<pi[2]<<endl;\n\n long long **vp1 = (long long**)(p);\n void (*pf1) () = (void (*)())(*(*vp1));\n pf1();\n\n cout<<\"-----------\"<<endl;\n b.f();\n a.f();\n p -> f();\n cout <<\"----------\"<<endl;\n\n A *x1 = new A();\n B *x2 = new B();\n\n x1 = x2; \n cout << \"x1 = x2 and x1 is pointing to B object now\"<<endl;\n cout << \"-----------\"<<endl;\n x1->f(); \n}\n
"},{"location":"oop/inheritance/lec/#virtual-destructors","title":"Virtual Destructors","text":"Shape *p = new Ellipse(100.0F, 200.0F); ...\ndelete p;\n
p \u7684\u9759\u6001\u7c7b\u578b\u662f Shape, \u5982\u679c\u4e0d\u5b9a\u4e49\u865a\u51fd\u6570\uff0c\u90a3\u4e48 p \u53ea\u4f1a\u53d1\u751f\u9759\u6001\u7ed1\u5b9a\uff0c\u5373\u8c03\u7528 Shape\u7684\u6790\u6784\u51fd\u6570\uff0c\u65e0\u6cd5\u8c03\u7528Ellipse\u7684\u6790\u6784\u51fd\u6570! virtual
\u5173\u952e\u5b57\uff0c\u9ed8\u8ba4\u662f virtual \u7684\uff0c\u4f46\u662f\u63a8\u8350\u52a0\uff0c\u4e3a\u4e86\u5b59\u7c7b\u3002class Expr {\npublic: \n virtual Expr* newExpr();\n virtual Expr& clone();\n virtual Expr self();\n}\nclass BinaryExpr : public Expr {\npublic:\n virtual BinaryExpr* newExpr(); // ok\n virtual BinaryExpr& clone(); // ok\n virtual BinaryExpr self(); // Error!\n}\n
\u6307\u5411\u5b50\u7c7b\u7684\u5bf9\u8c61\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u4e00\u4e2a\u6307\u5411\u7236\u7c7b\u7684\u5bf9\u8c61\u3002\u4f46\u662f\u5b50\u7c7b\u7684\u5bf9\u8c61\u548c\u7236\u7c7b\u7684\u5bf9\u8c61\u662f\u4e0d\u540c\u7684
void Derived::func() {\n cout << \"In Derived::func!\";\n Base::func(); // call to base class\n}\n
class Base {\npublic:\n virtual void func();\n virtual void func(int);\n};\n
\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u4f60\u5728\u6d3e\u751f\u7c7b\u4e2d\u53ea\u91cd\u5199\u4e86 func()
\uff0c\u800c\u6ca1\u6709\u91cd\u5199 func(int)
\uff0c\u90a3\u4e48 func(int)
\u5c31\u4f1a\u5728\u6d3e\u751f\u7c7b\u4e2d\u4e0d\u53ef\u89c1\uff0c\u56e0\u4e3a\u57fa\u7c7b\u4e2d\u7684 func(int)
\u88ab\u9690\u85cf\u4e86\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4f60\u5728\u6d3e\u751f\u7c7b\u4e2d\u65e0\u6cd5\u8c03\u7528 func(int)
\uff0c\u9664\u975e\u4f60\u4f7f\u7528\u4e86\u663e\u5f0f\u7684\u4f5c\u7528\u57df\u89e3\u6790\u7b26\u6765\u6307\u5b9a\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u51fd\u6570\u3002
#include <iostream>\nusing namespace std;\nclass A {\n public:\n A() { f(); }\n virtual void f() { cout << \"A::f()\"; }\n};\nclass B : public A {\n public:\n B() { f(); }\n void f() { cout << \"B::f()\"; }\n};\nint main() {\n B b;\n}\n//A::f()B::f()\n
virtual void render() = 0;\n
class XYPos{ ... }; // x,y point\nclass Shape {\npublic:\n Shape();\n virtual void render() = 0; \n void move(const XYPos&);\n virtual void resize();\nprotected:\n XYPos center;\n};\n
"},{"location":"oop/inheritance/lec/#multiple-inheritance","title":"Multiple Inheritance","text":"class B1 { int m_i; };\nclass D1 : public B1 {};\nclass D2 : public B1 {};\nclass M : public D1, public D2 {};\nvoid main() {\n M m; //OK\n B1* p = new M; // ERROR: which B1\n B1* p2 = dynamic_cast<D1*>(new M); // OK\n}\n
Say No to Multiple Inheritance
\u51fd\u6570\u6a21\u677f\uff1a\u7528\u4e8e\u5236\u9020\u51fd\u6570\u7684\u6a21\u677f!\u4e5f\u5c31\u662f\u8bf4\u5b83\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u4e00\u4e2a\u51fd\u6570\u7684\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u53c2\u6570\u7c7b\u578b\u751f\u6210\u5bf9\u5e94\u7684\u51fd\u6570\u3002
\u5982\u679c\u8fd9\u4e2a\u65f6\u5019\u5df2\u7ecf\u6709\u4e86\u67d0\u4e00\u4e2a\u51fd\u6570\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u4f1a\u4f18\u5148\u8c03\u7528\u5df2\u6709\u7684\u51fd\u6570\u3002
void swap( int& x, int& y ) { \n T temp = x; \n x = y; \n y = temp; \n}\n
template < class T > \nvoid swap( T& x, T& y ) { \n T temp = x; \n x = y; \n y = temp; \n}\n
The class T
specifies a parameterized type name
\u53ef\u4ee5\u7406\u89e3\u4e3a\uff0c\u6a21\u677f\u9ad8\u901f\u7f16\u8bd1\u5668\u8fd9\u4e2a\u51fd\u6570\u4f1a\u6709\u5f88\u591a\u91cd\u8f7d\u7248\u672c\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u53c2\u6570\u7c7b\u578b\u751f\u6210\u5bf9\u5e94\u7684\u51fd\u6570\u3002
int main() { \n int a = 1, b = 2; \n swap( a, b ); \n cout << a << \" \" << b << endl; \n return 0; \n}\n
int i = 3; int j = 4; \nswap(i, j); // use explicit int swap \nfloat k = 4.5; float m = 3.7; \nswap(k, m); // instanstiate float swap \nstd::string s(\"Hello\"); \nstd::string t(\"World\"); \nswap(s, t); // std::string swap \n
\u8fd9\u91cc swap(k,m)
\u4f1a\u8c03\u7528\u51fd\u6570\u6a21\u677f\uff0c\u968f\u540e\u751f\u6210 float swap
, \u7f16\u8bd1\u5668\u4f1a\u63d2\u5165\u51fd\u6570\uff0c\u8c03\u7528\u3002
\u5fc5\u987b\u7c7b\u578b\u76f8\u540c\uff01
swap(int, int); // ok \nswap(double, double); // ok \nswap(int, double); // error!\n
Even implicit conversions are ignored!
void swap(&int, &int);\ntemplate <class T> void swap(&T, &T);\nswap(1, 2); //Wrong!:should be left value reference\nint a = 1, b = 2;\nswap(a, b); //OK\n
template <class T>\nvoid f(int i){\n T a;\n a = i;\n}\n//Use in this way:\nf<int>(3);\nf<double>(1.0);\n
template <class T> \nclass Vector { \npublic: \n Vector(int); \n ~Vector(); \n Vector(const Vector&); \n Vector& operator=(const Vector&); \n T& operator[](int); \nprivate: \n T* m_elements; \n int m_size; \n};\n
template<class T>\nT& vector<T>::operator[](int i){ //Note this <T>!!!\n return m_elements[i];\n}\n
Vector<int> v1(100); \nVector<Complex> v2(256); \nv1[20] = 10; \nv2[20] = v1[20]; // ok if int->Complex define\n
A simple sort function
operator<
\u91cd\u8f7d\u7c7b\u6a21\u677f\u7684\u51fd\u6570\u662f\u58f0\u660e\u800c\u4e0d\u662f\u5b9a\u4e49\uff0c\u6ca1\u6709\u5206\u79bb\u7684 .h \u6587\u4ef6\u3002\uff08\u4e0d\u9700\u8981 inline \u5173\u952e\u5b57\uff09
template< class Key, class Value> \nclass HashTable { \nconst Value& lookup(const Key&) const; \nvoid install(const Key&, const Value&); \n... \n}; \n
Templates nest \u2014 they\u2019re just new types!
Vector< Vector< double *> > // note space > >\n
Vector< int (*)(Vector<double>&, int)>\n
\u5728C++\u4e2d\uff0c\u51fd\u6570\u6307\u9488\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6307\u9488\uff0c\u5b83\u6307\u5411\u51fd\u6570\u800c\u4e0d\u662f\u6307\u5411\u53d8\u91cf\u3002\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u6307\u5411\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002
\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u9700\u8981\u6307\u5b9a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u548c\u53c2\u6570\u7c7b\u578b\uff0c\u5e76\u4f7f\u7528\u6307\u9488\u58f0\u660e\u7b26 *
\u6765\u58f0\u660e\u6307\u9488\u3002\u51fd\u6570\u6307\u9488\u7684\u4e00\u822c\u5f62\u5f0f\u5982\u4e0b\uff1a
return_type (*pointer_name)(arg_type1, arg_type2, ...);\n
\u5176\u4e2d\uff1a - return_type
\u662f\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\uff1b - pointer_name
\u662f\u51fd\u6570\u6307\u9488\u7684\u540d\u79f0\uff1b - arg_type1, arg_type2, ...
\u662f\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u3002
\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u51fd\u6570 int add(int a, int b)
\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6307\u5411\u8be5\u51fd\u6570\u7684\u6307\u9488\u5982\u4e0b\uff1a
int (*ptr)(int, int); // \u5b9a\u4e49\u4e00\u4e2a\u6307\u5411\u8fd4\u56de\u7c7b\u578b\u4e3a int\uff0c\u63a5\u53d7\u4e24\u4e2a int \u7c7b\u578b\u53c2\u6570\u7684\u51fd\u6570\u6307\u9488\n
\u7136\u540e\uff0c\u53ef\u4ee5\u5c06\u51fd\u6570 add
\u7684\u5730\u5740\u8d4b\u7ed9\u6307\u9488 ptr
\uff0c\u4f7f\u5176\u6307\u5411 add
\u51fd\u6570\uff1a
ptr = add;\n
\u73b0\u5728\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8c03\u7528\u51fd\u6570 add
\uff1a
int result = (*ptr)(2, 3); // \u7b49\u4ef7\u4e8e int result = add(2, 3);\n
\u6216\u8005\u53ef\u4ee5\u7b80\u5316\u4e3a\uff1a
int result = ptr(2, 3); // \u4e5f\u7b49\u4ef7\u4e8e int result = add(2, 3);\n
\u8fd9\u6837\uff0c\u51fd\u6570\u6307\u9488\u5c31\u53ef\u4ee5\u7528\u6765\u52a8\u6001\u5730\u6307\u5411\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u5e76\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u3002\u5728\u793a\u4f8b\u4e2d\uff0cVector<int (*)(Vector<double>&, int)>
\u5c31\u662f\u4e00\u4e2a\u5411\u91cf\uff0c\u5176\u5143\u7d20\u662f\u6307\u5411\u63a5\u53d7 Vector<double>
\u548c int
\u53c2\u6570\u7684\u51fd\u6570\u7684\u6307\u9488\uff0c\u5e76\u8fd4\u56de int
\u7c7b\u578b\u503c\u3002
template <class T, int bound=100>\nclass FixedVector {\npublic:\n FixedVector();\n FixedVector(int);\n FixedVector(const FixedVector&);\n FixedVector& operator=(const FixedVector&);\n T& operator[](int);\nprivate:\n T m_elements[bound];\n int m_size;\n};\n
FixedVector<int, 200> v1;\nFixedVector<int> v2; // bound=100\n
template <class T>\nclass Derived : public Base<T> {\npublic:\n Derived();\n Derived(const Derived&);\n Derived& operator=(const Derived&);\n T& operator[](int);\n};\n
template <class T>\nclass Derived : public Base<T> {\npublic:\n Derived();\n Derived(const Derived&);\n Derived& operator=(const Derived&);\n T& operator[](int);\n};\n
template <class T>\nclass Base {\npublic:\n Base();\n Base(const Base&);\n Base& operator=(const Base&);\n T& operator[](int);\n};\nclass Derived : public Base<int> {\npublic:\n Derived();\n Derived(const Derived&);\n Derived& operator=(const Derived&);\n int& operator[](int);\n};\n
weak
\u5173\u952e\u5b57\u89e3\u51b3?"},{"location":"optimization/lec1/lec/#lecture-1-introduction-to-optimization","title":"Lecture 1: Introduction to Optimization","text":""},{"location":"optimization/lec1/lec/#relaxation-and-optimization","title":"Relaxation and Optimization","text":""},{"location":"optimization/lec1/lec/#classes-of-differentiable-functions","title":"Classes of differentiable functions","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/","title":"Chapter 1 Digital Systems and Information","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#breif-intro","title":"Breif Intro","text":"
The most commonly used two-valued information is an electrical signal - voltage or current typically two discrete values represented by the voltage range of values.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#digital-systems","title":"Digital Systems","text":"Takes a set of discrete information inputs and discrete internal information (system state) and generates a set of discrete information outputs.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#types-of-digital-systems","title":"Types of Digital Systems","text":"Combinational Logic System:No state present , one input just decide one output(one to one , no state)
Sequential System
State present
State updated at discrete times -- Synchronous Sequential System [Periodic]
State updated att any time -- Asychronous Sequential System
State function : \\(f_{state}=Function(State,Input)\\)
Output function : \\(f_{output}=Function(State)\\) or \\(f_{output}=Function(State,Input)\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#organization-of-computer","title":"Organization of Computer","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#embedded-systems","title":"Embedded Systems","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#number-systems-codes","title":"Number Systems & Codes","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#covert","title":"Covert","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#decimal-to-binary","title":"Decimal To Binary","text":"\u6574\u6570\u2014\u2014\u6743\u5c55\u5f00\u5f0f\u96642\uff0c\u4f59\u6570\u6784\u6210\u6700\u4f4e\u4f4d\u2014\u2014\u96642\u53d6\u4f59
\u5c0f\u6570\u2014\u2014\u6743\u5c55\u5f00\u5f0f\u4e582\uff0c\u6574\u6570\u6784\u6210\u6700\u9ad8\u4f4d\u2014\u2014\u4e582\u53d6\u6574
Given n binary digits (called bits), a binary code is a mapping from a set of represented elements to a subset of the \\(2^n\\) binary numbers.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#number-of-elements-represented","title":"Number of Elements Represented","text":"You can represent 4 elements in radix r = 2 with n = 2 digits: (00, 01, 10, 11).
You can represent 4 elements in radix \\(r=2\\) with \\(n=4\\) digits (0001,0010,0100,1000) \\(One\\ Hot\\ Code\\)
Other Examples
Add Six
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#ascii","title":"ASCII","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap1/chap1/#parity-bit-error-detection-codes","title":"Parity Bit Error-Detection Codes","text":"Redundancy (e.g. extra information), in the form of extra bits, can be incorporated into binary code words to detect and correct errors.
A simple form of redundancy is parity, an extra bit appended onto the code word to make the number of 1\u2019s odd or even.
Parity can detect all single-bit errors and some multiple-bit errors.
A code word has even parity if the number of 1\u2019s in the code word is even.
A code word has odd parity if the number of 1\u2019s in the code word is odd.
Suppose we want to know the angle shifted by the machine.
If Use Binary Code-- \u5982\u679c\u4e09\u4e2a\u4f20\u611f\u5668\u4f4d\u7f6e\u4e0d\u7edf\u4e00\uff0c\u5728\u4ece000-111\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\uff1a000-100-110-111 -- \u4e2d\u95f4\u8fc7\u7a0b\u7578\u53d8
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/","title":"Chapter 2 Combinational Logic","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#binary-logic-and-gates","title":"Binary Logic and Gates","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#gate-delay","title":"Gate Delay","text":"On the other hand, when an nMOS transistor receives a voltage at around 0 volts, the connection from the source to the drain will be broken, which is referred to as an open circuit.
Whereas the nMOS will form a closed circuit with the source when the voltage is non-negligible, the pMOS will form an open circuit with the source when the voltage is non-negligible.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#tristate-gate","title":"Tristate gate","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#common-logic-gate-circuit","title":"Common logic gate circuit","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#nand-operation","title":"NAND operation","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#nor-operation","title":"NOR operation","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#and-or-invert-operation","title":"AND-OR-INVERT operation","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#exclusive-or","title":"Exclusive OR","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#exclusive-nor","title":"Exclusive NOR","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#basic-concepts-of-boolean-algebra","title":"Basic concepts of Boolean algebra","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#the-xor-identities","title":"The XOR identities","text":"Note the following two points:
The holding operation priority unchanged, if necessary, add brackets indicate.
Within converting, public non-operation remains unchanged for several variables
If F\u2018 is the F Duality, then F is also F\u2019 of Duality. F and F\u2018 is mutually Duality formula .
If the two logical functions F and G are equal, then the Duality formula F' and G' are also equal.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#substitution-rules","title":"Substitution rules","text":"Any logical equation that contains a variable A, and if all occurrences of A's position** are replaced with a logical function F, the equation still holds.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#shannon-formula","title":"Shannon formula","text":"\u2013 Assuming: Function F contained variables \\(x\\)\u3001\\(\\bar{x}\\), at \"x AND F\" operation, variable \\(x\\) may be replaced by 1 , variable \\(\\bar{x}\\) can be replaced by 0.
\u2013 At \"x AND F\" operation, \\(x\\) can be \"0\", \\(\\bar{x}\\) can be replaced with \"1\" .
\\(xf(x,\\overline{x},y...)=xf(1,0,y\u2026)\\)
\\(\\overline{x}f(x,\\overline{x},y...)=\\overline{x} f(0,1,y...)\\)
Similarly
\\(x+f(x,\\bar{x},y\u2026\u2026z)=x+f(0,1,y\u2026\u2026z)\\)
\\(\\bar{x}+f(x,\\bar{x},y\u2026\u2026z)=\\bar{x}+f(1,0,y\u2026\u2026,z)\\)
If the function F contains the both of the variables \\(x,\\bar{x}\\) may be follow:
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#simplification","title":"Simplification","text":"Minterms are AND terms with every variable present in either true or complemented form.
Given that each binary variable may appear normal (e.g., x) or complemented (e.g., x ), there are \\(2^n\\) minterms for n variables. denoted as \\(m_i\\)
Maxterms are OR terms with every variable in true or complemented form.
Given that each binary variable may appear normal (e.g., x) or complemented (e.g., x), there are \\(2^n\\) maxterms for n variables. denoted as \\(M_i\\)
For a logical function SOP or POS is the optimized\uff08\u4e24\u7ea7\u95e8\uff0c\u95e8\u7ea7\u6570\u6700\u5c0f\uff09
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#karnaugh-map-of-functio","title":"Karnaugh map of Functio","text":"\u7565.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#multi-level-circuit-optimization","title":"Multi-level circuit optimization","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#cost","title":"COST","text":"Literal Cost : Just Literal Appearances
Gate Input Cost \\(G\\) : Add AND OR term but exclude one-literal term
AB+CD+E -- 7
BD+ABC+ACD --11
A gate that can be described using a single primitive operation type (AND or OR) plus an optional inversions.
The same as a connection -- improve circuit voltage levels and increase the speed of circuit operation.(decrease propagation delay)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#complex-gates","title":"Complex Gates","text":"Requires more than one primitive operation type for its description.
XOR -- Odd Functions
Use inversions -- Even Functions
P -- Generate the Parity bit
E--Decide whether the translated inf is Error
Three-state logic adds a third logic value, Hi-Impedance (\\(Hi-Z\\)), giving three states: \\(0, 1, and\\ Hi-Z\\) on the outputs.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap2/chap2/#the-3-state-buffer","title":"The 3-State Buffer","text":"Connection of two 3-state buffer outputs, B1 and B0, to a wire, OUT:
We can neglect the inversion of input iterals ; but the intermediate varaible\u2019s inversions cannot be neglected
For the first -- 3\u7ea7 but high cost
For the latter-- 4\u7ea7 but low cost
Basic Form : \\((2^3)\\) 8-3-input AND gates.
Split to 2-to-4-line decoder and 1-to-2-line decoder. [Simplify]
Another Example
Basic \\(128*7\\)
3-to-8 & 4-to-16. -- \\(128*2 + 8*3+16*4\\)
Theoratically realize all logic functions \\((SOM)\\)
To decrease count of the pins
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#encoding","title":"Encoding","text":"If two signals are high simultaneously ? -- Meaningless
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#priority-encoder","title":"Priority Encoder","text":"If more than one input value is \\(1\\), then the encoder just designed does not work.
One encoder that can accept all possible combinations of input values and produce a meaningful result is a priority encoder
To process \u4e2d\u65ad\u4e8b\u4ef6
Could use a K-map to get equations
Also can be read directly from table and manually optimized if careful
\\(A_2=D_4\\)
\\(A_1=\\bar{D_4}D_3+\\bar{D}_4\\bar{D}_3D_2=\\bar{D_4}F_1. F_1=(D_3+D_2)\\)
\\(A_0=\\bar{D}_4D_3+\\bar{D_4}\\bar{D_3}\\bar{D_2}D_1=\\bar{D_4(D_3+\\bar{D_2}D_1)}\\)
\\(V=D_4+F_1+D_1+D_0\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#multiplexers","title":"Multiplexers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#multiplexer-width-expansion","title":"Multiplexer Width Expansion","text":"GN(22)
Three State can connect all output to one line. --GN=18 (a)
GN=14 (b)
Disadvantages : Takes much time .
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#example","title":"Example","text":"Refer to \\(PPT\\)
Note that the multiplexer with fixed inputs is identical to a ROM with 3-bit addresses and 2-bit data!
The most COMMON:
\\(X\\oplus Y\\) only different from \\(X+Y\\) when \\(XY=1\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#binary-adders","title":"Binary Adders","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#carry-lookhead","title":"Carry Lookhead","text":"\\(P_i=A_i\\oplus B_i\\ \\ \\ G_i=A_iB_i\\\\S_i=P_i\\oplus C_i\\ \\ \\ C_{i+1}=G_i+P_iC_i\\)
Thus,we have
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#group-carry-lookahead-logic","title":"Group Carry Lookahead Logic","text":"Disadvantages :
So Simple copy the CLA to more bits is not practical.
Overflow V = \\(C_n\\oplus C_{n-1}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#incrementing-decrementing","title":"Incrementing & Decrementing","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#incrementing","title":"Incrementing","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#multiplicationdivision-by-2n","title":"Multiplication/Division by \\(2^n\\)","text":"Copies the MSB of the operand into the new positions
Positive operand example - 01110101 extended to 16 bits:0000000001110101
Negative operand example - 11110101 extended to 16 bits:1111111111110101
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#arithmetic-logic-unitalu-implementation","title":"Arithmetic Logic Unit(ALU) implementation","text":"\\(Y_i=B_iS_0+\\bar{B_i}S_1\\)
The Above Part -- (Basic ALU like above)
The below Part -- AND OR XOR NOT (one bit)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap3/chap3/#combinational-shifter-parameters","title":"Combinational Shifter Parameters","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/","title":"Chapter 4 Sequential Logic","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#storage-elements-and-sequential-circuit-analysis","title":"Storage Elements and Sequential Circuit Analysis","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#types-of-sequential-circuits","title":"Types of Sequential Circuits","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#synchronous","title":"Synchronous","text":"Behavior defined from knowledge of inputs an any instant of time and the order in continuous time in which inputs change
If clock just regarded as another input, all circuits are asynchronous!
Nevertheless, the synchronous abstraction makes complex designs tractable!
While \\(C=1\\) \\(S\\) \\(and/or\\) \\(R\\) are permited to change.
The master latch sets to \\(1\\)
A \\(1\\) is transferred to the slave
The master latch sets and then resets
A \\(0\\) is transferred to the slave
Master-slave - Equal to the width of the triggering pulse (Because of the 1's Catching the triggering pulse once arrives is forbidden to change)
Edge-triggered - Equal to a time interval that is generally much less than the width of the the triggering pulse
\\(t_h\\) Hold Time -- often equal to zero
\\(t_w\\) Clock Pulse Width
\\(t_{px}\\)
Same parameters as for gates except that measured from clock edge that triggers the output change to the output change.
More Specifically
pos
or neg
)\uff1bOutputs are a function ONLY of states
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#mealy-models","title":"Mealy Models","text":"Outputs are a function of inputs AND states
Details"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#equivalent-state","title":"Equivalent State","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#circuit-and-system-level-timing","title":"Circuit and System Level Timing","text":"periodic clock
flip-flop input
setup time on a path : Must be greater than or equal to zero on all paths for correct operation
\\(t_p=t_{slack}+(t_{pd,FF}+t_{pd,COMB}+t_s)\\)
\\(t_{stack}\\) must be greater than or equal to zero so \\(t_{p} \\ge max(t_{pd,FF}+t_{pd,COMB}+t_s)\\)
from all paths from flip-flop output to flip-flop input
Can be calculated more precisely by using \\(t_{PHL}\\) and \\(t_{PLH}\\) values instead of \\(t_{pd}\\) values, but requires consideration of inversions on paths
Calculation[Refer to PPT]
The task of state assignment involves determining the encoding length and finding an optimal (often challenging) or near-optimal state assignment scheme, leading to different expressions for output functions and activation functions, and consequently varying circuit complexities. The basic principles of state assignment are:
The fundamental principles of assignment are as follows:
Generally, the first principle is of greater importance and should be prioritized. Subsequently, consideration should be given to the frequency of occurrence of state pairs resulting from the first three principles, with a preference for assigning adjacent binary codes to state pairs with higher frequencies.
EXAMPLE -- Refer to the PPT
Design a sequential modulo 3 accumulator for 2-bit operands
Same as S-R flip-flop with J analogous to S and K analogous to R
Has a single input T
Same as a J-K flip-flop with J = K = T
Cannot be initialized to a known state using the T input
To avoid 1s catching behavior,one solution used is to use an edge-triggered D as the core of the flip-flop
Eg.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Chap4/chap4/#flip-flop-behavior-example","title":"Flip-Flop Behavior Example","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/DHI/Digital%20Hardware%20Implementation/","title":"Chapter 5 Digital Hardware Implementation","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/DHI/Digital%20Hardware%20Implementation/#programmable-logic","title":"Programmable Logic","text":"SIngle-bit-storge element
\u200b Reasable
\u200b Eletrically erasable
\u200b Flash
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/DHI/Digital%20Hardware%20Implementation/#read-only-memoryrom","title":"Read Only Memory(ROM)","text":"\u53ef\u5c06\u9996\u6b21\u4e0e\u6216\u5f53\u4f5c\u8f93\u5165\u56de\u5230AND gates input \u53ef\u5b9e\u73b0\u591a\u53d8\u91cf\u4e0e\u6216.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/DHI/Digital%20Hardware%20Implementation/#programmable-logic-array-pla","title":"Programmable Logic Array (PLA)","text":"\\(And\\) Gates limited
\u9002\u5f53\u53d6\u53cd\uff0c\u5c06\u9700\u89816\u4e2a\u4e0e\u9879\u2192 \u9700\u89814\u4e2a\u4e0e\u9879
65ns -- 'read speed'--is critical for the whole speed.
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#ram","title":"RAM","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#types-of-random-access-memory","title":"Types of Random Access Memory","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#static-ram-cell","title":"Static RAM -- Cell","text":"Information stored in Latches
Select = 0 HOLD
Select = 1 OUTPUT DATA. \u300cINPUT depends on \\(B\\) and \\(\\bar{B}\\)\u300d
Bit select = 0 : NO WRITING or READING -- Bit Slice Disabled
Word select : only one allowed to be ONE
Bit select = 1:
Chip Select : When CS=0 Data Output \u9ad8\u963b\u6001
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#cell-arrays-and-coincident-selection","title":"Cell Arrays and Coincident Selection","text":"16x1
8x2
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#making-larger-memories-word-extension","title":"Making Larger Memories: Word extension","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#making-wider-memories-bit-extension","title":"Making Wider Memories: Bit extension","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#dynamic-ram","title":"Dynamic RAM","text":"information stored as electrical charges
Transfers to and from the DRAM are synchronize with a clock
Synchronous registers appear on:
Column address counter
Example: Memory data path width: 1 word = 4 bytes
SDRAM burst time-- burst size=4
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#double-data-rate-sdram-ddr-sdram","title":"Double Data Rate SDRAM (DDR SDRAM)","text":"Transfers data on both edges of the clock
Provides a transfer rate of 2 data words perclock cycle
Example: Same as for synchronous DRAM
Read cycle time = 60 ns
Memory Bandwidth: (2 x 32)/(60 x 10-9) = 1.066 Mbytes/sec
SPEED UP!
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#rambus-dram-rdram","title":"RAMBUS\u00ae DRAM (RDRAM)","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#dependence-on-power-supply","title":"Dependence on Power Supply","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#volatile","title":"Volatile","text":"Loses stored information when power turned off
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/MB/MB/#non-volatile","title":"Non-volatile","text":"Retains information when power turned off
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/","title":"Registers and Register Transfers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#registers","title":"Registers","text":"Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#storage-and-load-enable","title":"Storage and Load Enable","text":"Load = 0 : Store
Load = 1 : Load
The Problem is : Clock Skew \u300cAnd Gate Delay\u300d
\u52d8\u8bef Load = 0 \u65f6 \u8109\u51b2\u6d88\u5931\uff0c\u5bc4\u5b58\u5668\u65e0\u6cd5\u88ab\u8f7d\u5165
Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#register-transfer","title":"Register Transfer","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#microoperations","title":"Microoperations","text":"
Logical Groupings:
\\(if\\ \\ (K_1=1)\\ then\\ \\ (R_2\u2190R_1)\\) \u8868\u793a\u4e3a \\(K_1 : R_2\u2190R_1\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#register-transfer-structures","title":"Register Transfer structures","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#multiplexer-based-transfers","title":"Multiplexer-Based Transfers","text":"note : clocks are omitted for clarity
\\(K_1 : R_0\u2190R_1\\)
\\(K2\\bar{K1}: R_0\u2190R_2\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#register-cell-design","title":"Register Cell Design","text":"
Example 1 Not encoded \u300cone hot code\u300d
Example 2 Encoded. \u300cBinary or Gray\u300d
EG1: Example
Register A (m-bits) Sepecification:
Data input B
Control input \\(C_x \\ C_y\\) with combinations\\((0,0)\\ (0,1)\\ (1,0)\\)
Register Transfers:
\\(C_x : A \\leftarrow A\\ or\\ B\\)
\\(C_y\\) : \\(A \\leftarrow A\\ xor\\ B\\)
Hold State : \\((0,0)\\)
Multiplexer Approach
Thus , we have :
\\(Load = C_x + C_y\\)
Cost : 6+3+2(or)+8(xor) = 19
Sequential Circuit Design Approach
Similar to using a MUX, the MUX in this case is no longer dedicated logic for a specific register but is shared logic used by three registers.
The MUX in the bus selects one input among all registers and outputs it to all registers. The bus controls which (or how many) registers receive this signal by manipulating the Load signal.
Advantages:
Disadvantages:
Gate Cost. (increase linearly)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#three-state-bus","title":"Three-State Bus","text":"The 3-input MUX can be replaced by a 3-state node (bus) and 3-state buffers.
Gate Cost Only 2n (suppose cost for three-state is 2)
Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#shift-registers","title":"Shift Registers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#_1","title":"\u4e32\u884c\u5b9e\u73b0","text":"
\u5047\u8bbe\u73b0\u5728\u6211\u4eec\u6709 4 \u4e2a FF
\u9996\u5c3e\u76f8\u8fde\uff0c\u73b0\u5728\u4e32\u884c\u8f93\u5165 1001
\u8fd9 4bits \u7684\u6570\u636e\uff08\u5f53\u7136\uff0c\u5b8c\u5168\u8f93\u5165\u9700\u8981 4 \u4e2a\u65f6\u949f\u5468\u671f\uff09\u3002
\u5047\u8bbe\u7b2c 1 \u4e2a\u5468\u671f\u7ed3\u675f\u540e\uff0c1001
\u6700\u53f3\u4fa7\u7684 1
\u88ab\u5b58\u5728\u4e86\u7b2c\u4e00\u4e2a FF\uff0c\u5219\uff1a
100
1
? ? ? 1???
2 10
0
1
? ? 01??
3 1
0
0
1
? 001?
4 1
0
0
1
1001
5 ? 1
0
0
?100
6 ? ? 1
0
??10
7 ? ? ? 1
???1
\u8fd9\u91cc\u7684 SO \u6307\u7684\u662f\u4ece\u5f53\u524d\u8f6e\u6b21\u5f00\u59cb\uff0c\u8bfb\u5165 4bits \u6570\u636e\u5f97\u5230\u7684\u4e32\u884c\u8f93\u51fa\u7ed3\u679c\uff0c\u8fd9\u91cc\u7684 4bits \u5f53\u7136\u662f\u5bf9\u5e94\u8f93\u5165\u7684 4bits\u3002
\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728\u7b2c\u56db\u8f6e\u524d\u540e\u7684\u7ed3\u679c\u5206\u522b\u662f\u5de6\u79fb\u548c\u53f3\u79fb\u7684\u7ed3\u679c\uff0c\u5f53\u7136\uff0c\u5982\u679c\u662f\u8981\u8003\u8651\u5177\u6709\u5b9e\u9645\u8ba1\u7b97\u4ef7\u503c\u7684\u4f4d\u79fb\uff0c\u6211\u4eec\u5f53\u7136\u9700\u8981\u586b\u5145 ?
\u7684\u503c\uff0c\u4e0d\u8fc7\u8fd9\u90fd\u662f\u5c0f\u4e8b\u60c5\u3002
Internal details => Incrementer
Internal Logic
Count Enable
Carry Out
Added as part of incrementer
Connect to Count Enable of additional 4-bit counters to form larger counters
\u4e3a\u4e86\u5b9e\u73b0\u9006\u5411\u8ba1\u6570\u5668\uff0c\u6211\u4eec\u5219\u9700\u8981\u5c06\u81ea\u589e\u5668\u6362\u4e3a\u81ea\u51cf\u5668\u3002\u505a\u6cd5\u53ea\u4e0d\u8fc7\u662f\u5c06\u5f02\u6216\u95e8\u7684\u63a7\u5236\u4fe1\u53f7\u53d6\u53cd
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#counter-with-parallel-load","title":"Counter with parallel load","text":"count Load Operation 0 0 Hold 0 1 Load Parallel data 1 x count"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#design-example","title":"Design Example","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#synchronous-bcd","title":"Synchronous BCD","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#counting-modulo-n","title":"Counting Modulo N","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register/#serial-transforms-and-microoperations","title":"Serial Transforms and Microoperations","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/","title":"Registers and Register Transfers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#registers","title":"Registers","text":"Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#storage-and-load-enable","title":"Storage and Load Enable","text":"Load = 0 : Store
Load = 1 : Load
The Problem is : Clock Skew \u300cAnd Gate Delay\u300d
\u52d8\u8bef Load = 0 \u65f6 \u8109\u51b2\u6d88\u5931\uff0c\u5bc4\u5b58\u5668\u65e0\u6cd5\u88ab\u8f7d\u5165
Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#register-transfer","title":"Register Transfer","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#microoperations","title":"Microoperations","text":"
Logical Groupings:
\\(if\\ \\ (K_1=1)\\ then\\ \\ (R_2\u2190R_1)\\) \u8868\u793a\u4e3a \\(K_1 : R_2\u2190R_1\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#register-transfer-structures","title":"Register Transfer structures","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#multiplexer-based-transfers","title":"Multiplexer-Based Transfers","text":"note : clocks are omitted for clarity
\\(K_1 : R_0\u2190R_1\\)
\\(K2\\bar{K1}: R_0\u2190R_2\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#register-cell-design","title":"Register Cell Design","text":"
Example 1 Not encoded \u300cone hot code\u300d
Example 2 Encoded. \u300cBinary or Gray\u300d
EG1: Example
Register A (m-bits) Sepecification:
Data input B
Control input \\(C_x \\ C_y\\) with combinations\\((0,0)\\ (0,1)\\ (1,0)\\)
Register Transfers:
\\(C_x : A \\leftarrow A\\ or\\ B\\)
\\(C_y\\) : \\(A \\leftarrow A\\ xor\\ B\\)
Hold State : \\((0,0)\\)
Multiplexer Approach
Thus , we have :
\\(Load = C_x + C_y\\)
Cost : 6+3+2(or)+8(xor) = 19
Sequential Circuit Design Approach
Similar to using a MUX, the MUX in this case is no longer dedicated logic for a specific register but is shared logic used by three registers.
The MUX in the bus selects one input among all registers and outputs it to all registers. The bus controls which (or how many) registers receive this signal by manipulating the Load signal.
Advantages:
Disadvantages:
Gate Cost. (increase linearly)
"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#three-state-bus","title":"Three-State Bus","text":"The 3-input MUX can be replaced by a 3-state node (bus) and 3-state buffers.
Gate Cost Only 2n (suppose cost for three-state is 2)
Example"},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#shift-registers","title":"Shift Registers","text":""},{"location":"%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%80%BB%E8%BE%91%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80/Register/register_%E5%89%AF%E6%9C%AC/#_1","title":"\u4e32\u884c\u5b9e\u73b0","text":"
\u5047\u8bbe\u73b0\u5728\u6211\u4eec\u6709 4 \u4e2a FF
\u9996\u5c3e\u76f8\u8fde\uff0c\u73b0\u5728\u4e32\u884c\u8f93\u5165 1001
\u8fd9 4bits \u7684\u6570\u636e\uff08\u5f53\u7136\uff0c\u5b8c\u5168\u8f93\u5165\u9700\u8981 4 \u4e2a\u65f6\u949f\u5468\u671f\uff09\u3002
\u5047\u8bbe\u7b2c 1 \u4e2a\u5468\u671f\u7ed3\u675f\u540e\uff0c1001
\u6700\u53f3\u4fa7\u7684 1
\u88ab\u5b58\u5728\u4e86\u7b2c\u4e00\u4e2a FF\uff0c\u5219\uff1a
100
1
? ? ? 1???
2 10
0
1
? ? 01??
3 1
0
0
1
? 001?
4 1
0
0
1
1001
5 ? 1
0
0
?100
6 ? ? 1
0
??10
7 ? ? ? 1
???1
\u8fd9\u91cc\u7684 SO \u6307\u7684\u662f\u4ece\u5f53\u524d\u8f6e\u6b21\u5f00\u59cb\uff0c\u8bfb\u5165 4bits \u6570\u636e\u5f97\u5230\u7684\u4e32\u884c\u8f93\u51fa\u7ed3\u679c\uff0c\u8fd9\u91cc\u7684 4bits \u5f53\u7136\u662f\u5bf9\u5e94\u8f93\u5165\u7684 4bits\u3002
\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728\u7b2c\u56db\u8f6e\u524d\u540e\u7684\u7ed3\u679c\u5206\u522b\u662f\u5de6\u79fb\u548c\u53f3\u79fb\u7684\u7ed3\u679c\uff0c\u5f53\u7136\uff0c\u5982\u679c\u662f\u8981\u8003\u8651\u5177\u6709\u5b9e\u9645\u8ba1\u7b97\u4ef7\u503c\u7684\u4f4d\u79fb\uff0c\u6211\u4eec\u5f53\u7136\u9700\u8981\u586b\u5145 ?
\u7684\u503c\uff0c\u4e0d\u8fc7\u8fd9\u90fd\u662f\u5c0f\u4e8b\u60c5\u3002
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#lecture-1-finite-automata","title":"Lecture 1 - Finite Automata","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#example","title":"Example :","text":"
Given a weighted graph G :
Given a string w, is \\(w\\in L=\\{encode(G,k) | G\\ is\\ a\\ graph,that\\ has\\ a\\ ST\\ with\\ weight\\ at\\ most\\ k\\}\\)?
A finite automaton is a 5-tuple \\((K,\\Sigma,\\delta,s,F)\\) where:
A configuration of an FA is a pair \\((q,w)\\) where \\(q\\in Q\\) and \\(w\\in\\Sigma^*\\) where \\(q\\) is the current state and \\(w\\) is the remaining input.
M accepts a language L if (two conditions):
A language is regular if it is accepted by a finite automaton.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#theorem","title":"Theorem","text":"If A and B are regular languages, so is \\(A\\cup B\\). Proof:
A NFA is a 5-tuple \\((K,\\Sigma,\\Delta,s,F)\\) where:
Acceptance of a string by a NFA:
Example: Construct a NFA that accepts the language \\(\\{w | w \\in \\{a,b\\}^*\\),the second symbol from the end of w is b\\(\\}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#theorem_1","title":"Theorem","text":"Given a DFA M, there exists a NFA M' such that \\(L(M) = L(M')\\).
DFA M simulates \"tree-like\" computation of NFA M.
A NFA \\(M = (K,\\Sigma,\\Delta,s,F)\\) can be simulated by a DFA \\(M' = (K',\\Sigma,\\delta,s',F')\\) where:
\\(2^K = \\{q_1,q_2,\\cdots,q_n,\\{q_1\\},\\{q_2\\},\\cdots,\\{q_n\\},\\{q_1,q_2\\},\\cdots,\\{q_1,q_2,\\cdots,q_n\\}\\}\\)
\\(F' = \\{q\\in K' | q\\cap F \\neq \\emptyset\\}\\)
\\(\\delta(q,a) = \\bigcup_{p\\in q}\\Delta(p,a)\\) for any \\(q\\in K'\\) and \\(a\\in\\Sigma\\)
\\(s' = E(s)\\) where \\(\\forall q \\in K, E(q)=\\{p \\in K:(q,e)\\vdash_M^*(p,e)\\}\\)
\\(\\delta(Q,a) = \\bigcup_{q\\in Q}\\bigcup_{p:(p,a,q)\\in\\Delta}E(p)\\) for any \\(Q\\subseteq K'\\) and \\(a\\in\\Sigma\\)
A language is regular if and only if it is accepted by a NFA.
Let \\(M_A\\) and \\(M_B\\) be FAs that accept A and B respectively.
\\(M_A = (K_A,\\Sigma,\\delta_A,s_A,F_A)\\)
\\(M_B = (K_B,\\Sigma,\\delta_B,s_B,F_B)\\)
Construct a new FA \\(M\\) that accepts \\(A\\cdot B\\):
\\(M = (K,\\Sigma,\\delta,s,F)\\) where
\\(K = K_A\\bigcup K_B\\)
\\(s = s_A\\)
\\(F = F_B\\)
\\(\\delta(q,a) = \\delta_A(q,a)\\bigcup\\delta_B(q,a)\\bigcup\\{(q,e,s_B):q\\in F_A\\}\\)
Proof:
The set of all boolean functions(mapping \\(\\{0,1\\}^*\\rightarrow\\{0,1\\}\\)) is uncountable. * Proof see book page 99
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#equivalent-conditions-for-countability","title":"Equivalent conditions for countability","text":"Lemma: Suppose that \\(E: O \\rightarrow \\{0,1\\}^*\\) is a one-to-one function. Then there exists a function \\(D:\\{0,1\\}^*\\rightarrow O\\) such that \\(D(E(o)) = o\\) for all \\(o\\in O\\).
Proof: Let \\(o_0\\) be an arbitrary object in O. Let \\(w_0 = E(o_0)\\). Let \\(w_0 = w_1w_2\\cdots w_n\\) be the string representation of \\(w_0\\). Let \\(o_1 = D(w_1)\\), \\(o_2 = D(w_1w_2)\\), \\(\\cdots\\), \\(o_n = D(w_1w_2\\cdots w_n)\\). Then \\(D(w_0) = o_n\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec1/Lecture1/#finite-representations","title":"Finite Representations","text":"if \\(O\\) is finite, then we can represent every object in \\(O\\) as a string of length at most some number \\(n\\) if and only if \\(|O|\\leq 2^{n+1}-1\\).
Lemma: For every two finite sets S and T, there exists a one-to-one function \\(E:S\\rightarrow T\\) if and only if \\(|S|\\leq|T|\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec2/Lecture2/#lecture-2-regular-expressions","title":"Lecture 2 - Regular Expressions","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec2/Lecture2/#definition","title":"Definition :","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec2/Lecture2/#regular-expression","title":"Regular Expression","text":"
\\(a(a\\cup b)^nb\\) is a regular expression over \\(\\{a,b\\}\\)
\\(L(R) = \\{w \\in \\{a,b\\}^* | w\\ is\\ a\\ string\\ that\\ starts\\ with\\ a\\ and\\ ends\\ with\\ b\\}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec2/Lecture2/#automic-regular-expression","title":"Automic Regular Expression","text":"\\(RE \\rightarrow NPA\\)
\\(NPA \\rightarrow RE\\)
state elimination
and dynamic programming
.Let \\(L\\) be a regular language,there must exist an integer \\(p\\geq 1\\) such that every string \\(w\\in L\\) with \\(|w|\\geq p\\) can be written as \\(w = xyz\\) with
Here \\(p\\) is called the pumping length.
Proof
If \\(L\\) is a regular language and it is finite, then we can set \\(p\\) to be the maximum length +1 of the strings in \\(L\\).
If \\(L\\) is a regular language and it is infinite, there exits a NFA \\(N\\) that accepts \\(L\\). Let \\(N = (Q,\\Sigma,\\delta,q_0,F)\\), where \\(Q = \\{q_0,q_1,\\cdots,q_n\\}\\).
Example
Assume \\(L\\) is a regular language, then \\(L \\cap 0^*1^* = \\{0^n1^n | n\\geq 0\\}\\) is a regular language, which is a contradiction.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec3/Lecture3/","title":"Lecture 3 - Context-Free Language","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec3/Lecture3/#lecture-3-context-free-grammar-cfg","title":"Lecture 3 - Context-Free Grammar CFG","text":"
A context-free grammar (CFG) is a set of recursive rules used to generate patterns of strings. A CFG \\(G = (V, \\Sigma, R, S)\\) consists of:
Define in one step \\(\\Rightarrow\\).
For any \\(x,y,u \\in V^*\\), for any \\(A \\in V - \\Sigma\\), \\(xAy \\Rightarrow xuy\\) if \\((A,u) \\in R\\).
Define in multiple steps \\(\\Rightarrow^*\\).
For any \\(x,y,u \\in V^*\\), for any \\(A \\in V - \\Sigma\\), \\(w \\Rightarrow^* u\\) if \\(w=u\\) or \\(w \\Rightarrow u_1 \\Rightarrow u_2 \\Rightarrow \\cdots \\Rightarrow u_k \\Rightarrow u\\).
G generates \\(w\\in \\Sigma^*\\) if \\(S \\Rightarrow^* w\\). * L(G) = {w \\(\\in \\Sigma ^*\\)| G generates w} is a context-free language.
Example:
If L is a context-free language, then there exists a positive integer p such that any string \\(s \\in L\\) with \\(|s| \\geq p\\) can be written as \\(s = uvxyz\\) such that:
If L is a context-free language, then there exists a positive integer p such that any string \\(s \\in L\\) with \\(|s| \\geq p\\) can be written as \\(s = uvxyz\\) such that:
From 2. and 1., we have \\(vxy\\) contains at most two types of symbols from \\(\\{a,b,c\\}\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#lecture-4-pushdown-automata","title":"Lecture 4 - Pushdown Automata","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#pushdown-automata","title":"Pushdown Automata","text":"
A PDA is a 6-tuple \\(P = (K, \\Sigma, \\Gamma, \\Delta, s, F)\\), where
\\(\\Delta\\) is a \"finite\" subset of \\(K \\times ((\\Sigma \\cup \\{\\epsilon\\}) \\times \\Gamma^*) \\times (K \\times \\Gamma^*)\\). where
A configuration of PDA is a member of \\(K \\times \\Sigma^* \\times \\Gamma^*\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#yeild-in-one-step","title":"yeild in one step","text":"\\((q, x, \\alpha) \\vdash_P (p, y, \\beta)\\) if \\(\\exists( (q, a, r), (p, b) ) \\in \\Delta\\) such that \\(x = ay\\) and \\(\\alpha = r\\tau\\) and \\(\\beta = b\\tau\\). for some \\(\\tau \\in \\Gamma^*\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#yeild-in-multiple-steps","title":"yeild in multiple steps","text":"\\((q, x, \\alpha) \\vdash_P^* (p, y, \\beta)\\) if \\((q, x, \\alpha) \\vdash_P (p_1, y_1, \\beta_1) \\vdash_P \\cdots \\vdash_P (p, y, \\beta)\\) or \\((q, x, \\alpha) = (p, y, \\beta)\\).
PDA accepts a string \\(x\\) if \\((s, x, \\epsilon) \\vdash_P^* (p, \\epsilon, \\epsilon)\\) for some \\(p \\in F\\).
Language of PDA \\(P\\) is \\(L(P) = \\{x \\in \\Sigma^* | (s, x, \\epsilon) \\vdash_P^* (p, \\epsilon, \\epsilon) \\text{ for some } p \\in F\\}\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#example","title":"Example","text":"Construct a PDA that accepts the language \\(\\{w \\in \\{0, 1\\}^* | \\#0's = \\#1's\\}\\).
\\(CFG \\Rightarrow PDA\\). and \\(PDA \\Rightarrow CFG\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#cfg-rightarrow-pda","title":"CFG \\(\\Rightarrow\\) PDA","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#only-give-an-example","title":"Only Give an Example","text":"Given a CFG with \\(R = \\{S \\rightarrow aSb | \\epsilon\\}\\), construct a PDA \\(P\\) that accepts \\(L(G)\\).
The idea is to first convert the PDA to a simple PDA, then convert the simple PDA to a CFG.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#theorem","title":"Theorem","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#theorem-1","title":"Theorem 1","text":"
Every Regular Language is a Context-Free Language.
The family of context-free languages is closed under \\(\\cup, \\cdot, *\\). but not closed under \\(\\cap, -\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#cup","title":"\\(\\cup\\)","text":"Proof: \\(A \\cap B = \\overline{\\bar{A} \\cup \\bar{B}}\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#examples","title":"Examples","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec4/Lecture4/#example-1","title":"Example 1","text":"Construct a PDA that accepts the language \\(\\{w \\in \\{a, b\\}^* | \\#a's = 2\\#b's\\}\\).
Idea: Stack \\(\\Rightarrow\\) Unary Counter.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#lecture-5-turing-machine","title":"Lecture 5 - Turing Machine","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#turing-machine","title":"Turing Machine","text":"
A Turing machine is a mathematical model of computation that defines an abstract machine. It was invented by Alan Turing in 1936. A Turing machine can simulate the logic of any computer algorithm, and is therefore the theoretical foundation of all modern computers.
A Turing machine is a 5-tuple \\((K, \\Sigma, \\delta, s, H)\\), where:
The transition function \\(\\delta\\) satisfies the following properties:
leftend \\(\\triangleright\\) is a special symbol that is used to indicate the left end of the tape.
blank symbol \\(\\cup\\) is a special symbol that is used to indicate the blank symbol.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#configuration","title":"configuration","text":"A configuration of a Turing machine is a member of
\\[(K \\times \\triangle(\\Sigma-\\{\\triangleright\\})^*) \\times ((\\Sigma-\\{\\triangleright\\})^* (\\Sigma - \\{\\triangleright,\\cup\\})\\cup \\{e\\})\\]We say \\((q_1,\\triangleright w_1a_1u_1) \\vdash_M (q_2,\\triangleright w_2a_2u_2)\\) if
writing : \\(\\delta(q_1,a_1) = (q_2,a_2) and\\ a_2 \\in \\Sigma - \\{\\triangleright\\}\\) and \\(w_2 = w_1\\) and \\(u_2 = u_1\\).
moving left : \\(\\delta(q_1,u_1) = (q_2,L)\\) and \\(w_1=w_2a_2\\) and \\(u_2 = a_1u_1\\).
M halts if it reaches a halting configuration
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#acceptance-and-rejection","title":"Acceptance and Rejection","text":"A Turing machine M accepts a string w if \\((s,\\triangleright\\cup w) \\vdash^* (yes,\\triangleright\\cup aw)\\)
A Turing machine M rejects a string w if \\((s,\\triangleright\\cup w) \\vdash^* (no,\\triangleright\\cup aw)\\)
Given a Turing machine M, we can define the language accepted by M as \\(L(M) = \\{w \\in \\Sigma^* | M \\text{ accepts w}\\}\\).
M decides a language \\(L\\) if M accepts all strings in L and rejects all strings not in L.
M semi-decides a language \\(L\\) if M accepts all strings in L and may loop (or reject) on strings not in L.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#reccursive-language","title":"Reccursive Language","text":"A language \\(L\\) is recursive if there exists a Turing machine that decides \\(L\\).
Every recursive language is recursively enumerable.
A multi-tape Turing machine is a Turing machine with multiple tapes. Each tape has its own head and can move independently.
\\[\\delta: (K - H) \\times \\Sigma^k \\rightarrow K \\times ((\\Sigma -\\triangleright)\\cup \\{L,R\\})^k.\\]"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#two-way-infinite-tape","title":"Two-way Infinite Tape","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#multi-head-turing-machine","title":"Multi-head Turing Machine","text":"A multi-head Turing machine is a Turing machine with multiple heads on a single tape.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#2d-tape-turing-machine","title":"2D-Tape Turing Machine","text":"Here is a high-level description of a Turing machine that can decide \\( L \\):
Check if the string consists of only \\( a \\)'s, \\( b \\)'s, and \\( c \\)'s: - The TM scans the input tape to ensure that it only contains \\( a \\)'s, \\( b \\)'s, and \\( c \\)'s in that order. If any other character is found, the machine rejects the string.
Match \\( a \\)'s, \\( b \\)'s, and \\( c \\)'s: - Start by marking the leftmost \\( a \\) and replace it with a special symbol (say \\( X \\)). - Then, scan the tape to find the leftmost \\( b \\). If one is found, mark it with a different special symbol (say \\( Y \\)). - Next, scan to find the leftmost \\( c \\), and mark it with another symbol (say \\( Z \\)). - Now, go back to the left end of the tape and repeat the process, continuing to match one \\( a \\), one \\( b \\), and one \\( c \\) at a time, until either:
Verify the end condition: - Once all the symbols have been marked, check the tape:
Empty string check: - The empty string \\( \\epsilon \\) is trivially in \\( L \\), as it satisfies the condition \\( a^n b^n c^n \\) where \\( n = 0 \\).
M decides a language \\(L\\) if
M semi-decides a language \\(L\\) if for any \\(w \\in L\\):
Let \\(C = {100,110,1000,...}\\) C is composed of all binary numbers that are not prime. -- Construct a NTM that semi-decides C.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#theorem","title":"Theorem","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#theorem-1","title":"Theorem 1","text":"Every NTM can be simulated by a DTM.
A NTM semi-decides a language \\(L\\) if and only if there exists a DTM that semi-decides \\(L\\).
A NTM semi-decides a language \\(L\\) \\(\\Rightarrow\\) There exists a DTM that semi-decides \\(L\\).
Use a three-tape DTM to simulate a NTM.
A Turing machine can be described by high-level pseudocode.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#fact","title":"Fact","text":"M on input \\(G\\):
Input: \\(\\langle D,w \\rangle\\), where \\(D\\) is a DFA and \\(w\\) is a string.
Output: Accept if \\(D\\) accepts \\(w\\); reject otherwise.
Solution:Construct a Turing machine that simulates the DFA \\(D\\) on input \\(w\\).
\\(M_{R_1}\\) = on input \\(\\langle D,w \\rangle\\):
Input: \\(\\langle B,w \\rangle\\), where \\(B\\) is a NFA and \\(w\\) is a string.
Output: Accept if \\(B\\) accepts \\(w\\); reject otherwise.
Solution: Construct a Turing machine that simulates the NFA \\(B\\) on input \\(w\\).
\\(M_{R_2}\\) = on input \\(\\langle B,w \\rangle\\):
This involves the process of reduction.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#a_rex","title":"\\(A_{REX}\\)","text":"Input: \\(\\langle R,w \\rangle\\), where \\(R\\) is a regular expression and \\(w\\) is a string.
Output: Accept if \\(R\\) accepts \\(w\\); reject otherwise.
Solution: Construct a Turing machine that simulates the NFA \\(B\\) on input \\(w\\).
\\(M_{R_3}\\) = on input \\(\\langle R,w \\rangle\\):
Input: \\(\\langle B \\rangle\\), where \\(B\\) is a DFA.
Output: Accept if \\(L(B) = \\emptyset\\); reject otherwise.
Solution: Construct a Turing machine that simulates the DFA \\(B\\) on input \\(w\\).
\\(M_{R_4}\\) = on input \\(\\langle B \\rangle\\):
Input: \\(\\langle B_1,B_2 \\rangle\\), where \\(B_1\\) and \\(B_2\\) are DFAs.
Output: Accept if \\(L(B_1) = L(B_2)\\); reject otherwise.
Solution: Construct a Turing machine that simulates the DFA \\(B\\) on input \\(w\\).
\\(M_{R_5}\\) = on input \\(\\langle B_1,B_2 \\rangle\\):
Input: \\(\\langle G,w \\rangle\\), where \\(G\\) is a CFG and \\(w\\) is a string.
Output: Accept if \\(G\\) generates \\(w\\); reject otherwise.
\\(A_{CFG} = \\{ \\langle G,w \\rangle | G \\text{ is a CFG and G generates w}\\}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#chonsky-normal-form","title":"Chonsky Normal Form","text":"A CFG \\(G\\) is in Chonsky Normal Form if every rule is of the form:
So if final \\(w\\) has length \\(n\\), the number of steps of subtitution is \\(2n-1\\).
\\(M_{C1}\\) = on input \\(\\langle G,w \\rangle\\): 1. Convert CFG \\(G\\) to Chonsky Normal Form \\(G'\\). 2. Enumerate all derivations of length at most \\(|R'|^{2n-1}\\). 3. Acscept if any derivation generates \\(w\\); otherwise, reject.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#a_pda","title":"\\(A_{PDA}\\)","text":"Input: \\(\\langle P,w \\rangle\\), where \\(P\\) is a PDA and \\(w\\) is a string.
Output: Accept if \\(P\\) accepts \\(w\\); reject otherwise.
\\(A_{PDA} = \\{ \\langle P,w \\rangle | P \\text{ is a PDA and P accepts w}\\}\\)
\\(M_{C_2}\\) = on input \\(\\langle P,w \\rangle\\): 1. Convert PDA \\(P\\) to a CFG \\(G\\). 2. Run \\(M_{C_1}\\) on input \\(\\langle G,w \\rangle\\). 3. If \\(M_{C_1}\\) accepts, accept; otherwise, reject.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#e_cfg","title":"\\(E_{CFG}\\)","text":"Input: \\(\\langle G \\rangle\\), where \\(G\\) is a CFG.
Output: Accept if \\(L(G) = \\emptyset\\); reject otherwise.
\\(E_{CFG} = \\{ \\langle G \\rangle | L(G) = \\emptyset\\}\\)
\\(M_{C_3}\\) = on input \\(\\langle G \\rangle\\): 1. Mark all terminals and \\(e\\). 2. Look through all rules and if there is a rule that all symbols on the right side are marked , mark the left side. 3. Repeat step 2 until no new symbols are marked. 4. If \\(S\\) is marked, reject; otherwise, accept.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec5/Lecture5/#e_pda","title":"\\(E_{PDA}\\)","text":"Input: \\(\\langle P \\rangle\\), where \\(P\\) is a PDA.
Output: Accept if \\(L(P) = \\emptyset\\); reject otherwise.
\\(E_{PDA} = \\{ \\langle P \\rangle | L(P) = \\emptyset\\}\\)
\\(M_{C_4}\\) = on input \\(\\langle P \\rangle\\): 1. Convert PDA \\(P\\) to a CFG \\(G\\). 2. Run \\(M_{C_3}\\) on input \\(\\langle G \\rangle\\). 3. If \\(M_{C_3}\\) accepts, accept; otherwise, reject.
\\(A_{DFA}\\) is recursive \\(\\rightarrow\\) \\(L(D)\\) is recursive.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/","title":"Lecture 6 - Containments properties of languages","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#containments-properties-of-languages","title":"Containments properties of languages","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#summary","title":"Summary","text":"
\\(\\text{regular languages}\\in \\text{context-free languages} \\in \\text{recursively languages} \\in \\text{recursively enumerable languages}(semi-decidable)\\)
A set A is countable if it is finite or \\(\\exists\\) a bijection between A and \\(\\mathbb{N}\\).
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#lemma-1","title":"Lemma 1","text":"A set A is countable if and only if there is an injection from A to \\(\\mathbb{N}\\).
injection: \\(f:A \\rightarrow \\mathbb{N}\\) is an injection if \\(f(a) = f(b) \\Rightarrow a = b\\).
(\\(\\Leftarrow\\)) If bijection exists, then injection exists.
Any subset of a countable set A is countable.
Proof:
Any language \\(\\Sigma^*\\) is countable.
Proof:
\\(\\{M: \\text{M is a Turing Machine}\\}\\) is countable.
Let \\(\\Sigma\\) be an alphabet. Let \\(L\\) be a language over \\(\\Sigma\\). Then, \\(L\\) is uncountable.
See link
Another Proof:
\\(A_{TM} = \\{<M,w>|M \\text{ is a TM and M accepts w}\\}\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem1","title":"Theorem1","text":"\\(A_{TM}\\) is recursively enumerable.
Proof:
Suppose \\(A_{TM}\\) is recursively enumerable. Then, there exists a TM \\(E\\) that semi-decides \\(A_{TM}\\).
E = On input : 1. Simulate M on w. 2. If M accepts w, accept; if M rejects w, reject. 3. Loop if M loops on w. 4. If M rejects w, reject."},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem2","title":"Theorem2","text":"
\\(A_{TM}\\) is not recursive.
Proof:
Suppose \\(A_{TM}\\) is recursive. Then, there exists a TM \\(H\\) that decides \\(A_{TM}\\).
H = On input : 1. Run M on w. 2. If M accepts w, accept; if M rejects w, reject.
Consider the following TM D:
D = On input : 1. Run H on <M,\\. 2. If H accepts, reject; if H rejects, accept.
In this condition, it means that D can decide D so D is recursive. -- \\(A_{TM}\\) is recursive \\(\\Rightarrow\\) \\(D (A_d)\\) is recursive.
Consider what happens when we run D on \\<D>.
So, \\(A_{TM}\\) is not recursive.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#ad","title":"Ad","text":"\\(A_d = \\{<\"M\">|M \\text{ is a TM that does not accept \"M\"}\\}\\) -- See Above.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem3","title":"Theorem3","text":"\\(A_d\\) is not recursively enumerable.
Proof:
Suppose \\(A_d\\) is recursively enumerable. Then, there exists a TM \\(D\\) that semi-decides \\(A_d\\).
D = On input : 1. Accept if \\(M \\in A_d\\). (M rejects \"M\") 2. Reject or Loop if \\(M \\notin A_d\\).\" (M accepts \"M\")
Consider what happens when we run D on \\<D>.
\\(A_{TM}\\) is recursively enumerable but not recursive. \\(A_d\\) is not recursively enumerable.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem4","title":"Theorem4","text":"If \\(L\\) and \\(\\overline{L}\\) are both recursively enumerable, then \\(L\\) is recursive.
Proof:
D = On input w: 1. Run \\(M_1\\) and \\(M_2\\) parallelly on w. 2. If \\(M_1\\) accepts w, accept; if \\(M_2\\) accepts w, reject.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec6/Lecture6/#theorem5","title":"Theorem5","text":"\\(\\overline{A_{TM}}\\) is not recursively enumerable.
Proof:
Suppose \\(\\overline{A_{TM}}\\) is recursively enumerable. Then, there exists a TM \\(D\\) that semi-decides \\(\\overline{A_{TM}}\\).
Also we know that \\(A_{TM}\\) is recursively enumerable. Then, there exists a TM \\(E\\) that semi-decides \\(A_{TM}\\).
But \\(A_{TM}\\) is not recursive. So, \\(\\overline{A_{TM}}\\) is not recursively enumerable.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/","title":"Lecture 7 - Decidability","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#problems-that-are-not-recursive","title":"Problems That Are Not Recursive","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#examples","title":"Examples","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#1-the-halting-problem","title":"1. The Halting Problem","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#proof-1","title":"Proof 1","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#proof-2","title":"Proof 2","text":"
\\(A_{TM} = \\{<M, w>|M\\text{ is a TM and M accepts w}\\}\\)
Construct a TM \\(M^*\\ H_{TM}\\) to solve \\(A_{TM}\\):
\\(H_{TM} = {<M, w>|M\\text{ is a TM and M halts on w}}\\)
\\(M^* =\\) On input x:
Suppose \\(M^*\\) is a recursive TM, then we can construct a TM \\(M_{H}\\) to decide \\(H_{TM}\\):
\\(M_{A} =\\) On input :
If all this is true, then \\(M_A\\) can decide \\(A_{TM}\\)
A Turing machine \\(M\\) accepts \\(w\\) if and only if \\(M^*\\) accepts \\(e\\)
\\(M^* =\\) On input x:
If M accepts w, then \\(M^*\\) accepts all inputs, including e
If M rejects or loops on w, then \\(M^*\\) rejects all inputs, including e
Suppose \\(L\\) is recursive, then we can construct a TM \\(M_{L}\\) to decide \\(L\\):
\\(M_{A} =\\) On input :
If all this is true, then \\(M_A\\) can decide \\(A_{TM}\\)
A Turing Machine accepts w if and only if \\(M^*\\) accepts all strings
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#3-l_3-m1-m2textm1-and-m2-accept-the-same-set-of-strings-is-not-recursive","title":"3. \\(L_3 = \\{\"M1, M2\"|\\text{M1 and M2 accept the same set of strings}\\}\\) is not recursive","text":"Suppose that \\(L_3\\) is recursive, \\(\\exist M_3\\) that decides \\(L_3\\)
Use \\(M_3\\) to construct \\(M_2\\) that deides \\(L_2\\)
\\(M_2\\) = on input \"M\"
So \\(M_2\\) decides \\(L_2\\) but \\(L_2\\) is not recursive
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#4-l_4-mtextm-is-a-tm-with-lm-is-regular-is-not-recursive","title":"4. \\(L_4 = \\{\"M\"|\\text{M is a TM with L(M) is regular}\\}\\) is not recursive","text":"Proof: \\(L_4\\) is not recursive
Suppose that \\(L_4\\) is recursive, \\(\\exist M_4\\) that decides \\(L_4\\)
Universal Turing Machine U : On input : 1. Run M on w 2. If M accepts w, accept 3. else reject
The undecidability of \\( R_{TM} = \\{ \\langle M \\rangle : M \\text{ is a Turing machine with } L(M) \\text{ regular} \\} \\) (i.e., determining if a Turing machine \\( M \\) has a regular language \\( L(M) \\)) stems from the fact that if we could decide \\( R_{TM} \\), we could also decide the Halting Problem, which is known to be undecidable.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#heres-a-step-by-step-explanation-of-why-this-leads-to-undecidability","title":"Here\u2019s a step-by-step explanation of why this leads to undecidability:","text":"Assume \\( R_{TM} \\) is Decidable: Suppose there exists a Turing machine \\( D \\) that decides \\( R_{TM} \\), meaning that \\( D \\) can take as input any Turing machine \\( M \\) and decide if \\( L(M) \\) is regular.
Reduction from the Halting Problem: We know that the Halting Problem (determining whether a Turing machine \\( M \\) halts on an input \\( w \\)) is undecidable. To show that \\( R_{TM} \\) is also undecidable, we construct a new Turing machine \\( M^* \\) based on \\( M \\) and \\( w \\) and show that if we could decide whether \\( L(M^*) \\) is regular, we could solve the Halting Problem.
Construction of \\( M^* \\): Define \\( M^* \\) as a Turing machine that: - First simulates \\( M \\) on \\( w \\). - If \\( M \\) halts on \\( w \\), \\( M^* \\) then simulates a universal Turing machine \\( U \\) on any additional input \\( x \\). - If \\( M \\) does not halt on \\( w \\), \\( M^* \\) simply rejects all inputs or does not accept any string beyond a certain structure.
Behavior of \\( L(M^*) \\): - If \\( M \\) halts on \\( w \\), then \\( L(M^*) \\) becomes non-regular (since it depends on \\( L(U) \\), the language of a universal Turing machine, which is non-regular). - If \\( M \\) does not halt on \\( w \\), \\( L(M^*) \\) is regular (e.g., it could be empty or finite, depending on the construction).
Implication: If we had a way to determine whether \\( L(M^*) \\) is regular using the hypothetical decider \\( D \\) for \\( R_{TM} \\), we could answer whether \\( M \\) halts on \\( w \\): - If \\( D \\) tells us that \\( L(M^*) \\) is regular, then \\( M \\) does not halt on \\( w \\). - If \\( D \\) tells us that \\( L(M^*) \\) is non-regular, then \\( M \\) halts on \\( w \\).
Contradiction: Since this method would solve the Halting Problem (which is known to be undecidable), the assumption that \\( R_{TM} \\) is decidable must be incorrect. Therefore, \\( R_{TM} \\) is undecidable.
In short, if we could decide whether \\( L(M) \\) is regular for any Turing machine \\( M \\), we could also solve the Halting Problem by constructing \\( M^* \\) as described. This leads to a contradiction, proving that \\( R_{TM} \\) is undecidable.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#41-l_5-mtextm-is-a-tm-with-lm-is-context-free-is-not-recursive","title":"4.1. \\(L_5 = \\{\"M\"|\\text{M is a TM with L(M) is context-free}\\}\\) is not recursive","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#42-l_6-mtextm-is-a-tm-with-lm-is-recursive-is-not-recursive","title":"4.2. \\(L_6 = \\{\"M\"|\\text{M is a TM with L(M) is recursive}\\}\\) is not recursive","text":""},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#rices-theorem","title":"Rice's Theorem","text":"\\(All_{PDA} = \\{<M>|M\\text{ is a PDA and }L(M) = \\Sigma^*\\}\\)
Construct a TM \\(M\\) to solve \\(NOTAll_{PDA}\\): * M halts on w if and only if \\(L(P) \\neq \\Sigma^*\\)
Construct P accepts all strings that are not computing history of M that halts on w.
If \\(NOTAll_{PDA}\\) is recursive, then \\(H_{TM}\\) is recursive, which is not true.
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#reduction","title":"Reduction","text":"Let A, B be two languages, A reduces to B if there is a computable function f \\(\\Sigma^* \\to \\Sigma^*\\) such that:
\\(x \\in A \\Leftrightarrow f(x) \\in B\\)
"},{"location":"%E8%AE%A1%E7%AE%97%E7%90%86%E8%AE%BA/lec7/Lecture7/#lemma-1","title":"Lemma 1","text":"Suppose \\(\\exist f\\) that reduces A to B:
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#chap2language-of-the-computerrisc-v","title":"Chap2:Language of the Computer(RISC V)","text":"Summary"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#instruction-characteristics","title":"Instruction Characteristics","text":"
\u64cd\u4f5c\u6570\u4f4d\u5bbd\u53ef\u4ee5\u4e0d\u540c\uff0c\u53ef\u4ee5\u662f\u7acb\u5373\u6570/\u5bc4\u5b58\u5668/\u5185\u5b58
Load/Store \u7ed3\u6784
\u6307\u4ee4\u5206\u7c7b\uff1aLoad/Store\u7ed3\u6784\u5c06\u6307\u4ee4\u5206\u4e3a\u4e24\u7c7b\uff1a\u52a0\u8f7d\uff08Load\uff09\u6307\u4ee4\u548c\u5b58\u50a8\uff08Store\uff09\u6307\u4ee4\u3002\u8fd9\u4e24\u7c7b\u6307\u4ee4\u5206\u522b\u7528\u4e8e\u4ece\u5185\u5b58\u4e2d\u52a0\u8f7d\u6570\u636e\u5230\u5bc4\u5b58\u5668\u6216\u5c06\u5bc4\u5b58\u5668\u4e2d\u7684\u6570\u636e\u5b58\u50a8\u5230\u5185\u5b58\u4e2d
\u6570\u636e\u4f20\u8f93\uff1a\u5728Load/Store\u7ed3\u6784\u4e2d\uff0c\u53ea\u6709Load\u548cStore\u6307\u4ee4\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\u3002\u5176\u4ed6\u6307\u4ee4\uff0c\u5982\u7b97\u672f\u8fd0\u7b97\u6307\u4ee4\u6216\u903b\u8f91\u6307\u4ee4\uff0c\u5fc5\u987b\u9996\u5148\u5c06\u6570\u636e\u52a0\u8f7d\u5230\u5bc4\u5b58\u5668\u4e2d\uff0c\u7136\u540e\u6267\u884c\u64cd\u4f5c\uff0c\u6700\u540e\u518d\u5c06\u7ed3\u679c\u5b58\u56de\u5185\u5b58
Example
f = (g + h) - (i + j);\n
add t0, g, h\nadd t1, i, j\nsub f, t0, t1\n
;Not good for parallel opeartions\nadd t0, g, h\nsub t0, t0, i\nsub f, t0, j\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#regisers","title":"Regisers","text":"load
\u6307\u4ee4\u53ef\u4ee5\u8bbf\u95ee\u7684\u8303\u56f4\u6709\u8fd9\u4e48\u5927\u3002 x0
: \u56e0\u4e3a\u7ecf\u5e38\u6709 0 \u53c2\u4e0e\u8ba1\u7b97\uff0c\u5c06\u5176\u5b58\u5728\u4e00\u4e2a\u5bc4\u5b58\u5668\u4e2d\uff0c\u4fbf\u4e8e\u8ba1\u7b97add x5,x20,x21\nadd x6,x22,x23\nsub x19,x5,x6\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#memory-operands","title":"Memory Operands","text":"Data transfer instructions * Load: Load values from memory to register * Store: Store result from register to memory; store doubleword
Memory is byte addressed.
RISC-V is Little Endian
RISC-V dose not require words to be aligned in memory : To Save Memory
words align: A word is 4 byte
\u6211\u4eec\u8981\u6c42\u5b57\u7684\u8d77\u59cb\u5730\u5740\u4e00\u5b9a\u8981\u662f 4 \u7684\u500d\u6570 : \u8bbf\u5b58\u66f4\u52a0\u65b9\u4fbf
LW t0,[0,4,8,...] \nLD t0,[0,8,16,32...]\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#data-transfer-instruction","title":"Data Transfer instruction","text":"g = h + A[i]\n//assume g,h,i - x18,x19,x20 base address of A -x22\n
0(x5)
x5
is for \u504f\u79fbadd x5,x20,x20\nadd x5,x5,x5\nadd,x5,x5,x5\nadd,x5,x5,x22\nld,x6,0(x5) \nadd x18,x19,x6\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#registers-vs-memory","title":"Registers vs. Memory","text":"Immediate: Other method for adding constant
Avoids the load instruction
Offer versions of the instruction e.g. addi x22, x22, 4
load byte / load half
e.g. addi
, ld
rs1: source or base address register number
immediate: constant operand, or offset added to base address \u5c06 rs2, funct7 \u5408\u5e76\u4e86\uff0c\u5f97\u5230 12 \u4f4d\u7acb\u5373\u6570
Stored Program Computer
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#logical-operations","title":"Logical Operations","text":"Operation C Java RISC-V Shift left << <<
slli
Shift right >> >>> srli
Bit-by-by AND & & and, andi
Bit-by-by OR | | or, ori
Bit-by-by XOR ^ ^ xor, xori
Bit-by-by NOT ~ ~ - 0(x5)
x5
is for \u504f\u79fbfunct6
: \u79fb\u4f4d\u4e0d\u9700\u8981\u8fd9\u4e48\u591a\u7acb\u5373\u6570\uff0c\u53ea\u8981\u516d\u4f4d (\\(2^6=64\\)) \u5373\u53ef\u3002not--xor 1111111...
beq register1, register2, L1\n bne register1, register2, L1\n
RISC-V assembly code
bne x22, x23, ELSE ; go to ELSE if i != j\nadd x19, x20, x21 ; f = g + h ( skipped if i not equals j)\nbeq x0, x0, EXIT ; as jump (unconditional branch)\nELSE: sub x19, x20, x21; f = g - h ( skipped if i equals j )\nEXIT:\n
bne \nbeq\nblt rs1, rs2, L1;if (rs1 < rs2) branch to instruction labeled L1\nbge rs1, rs2, L1;if (rs1 >= rs2) branch to instruction labeled L1\n
Example "},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#_1","title":"Chapter2","text":"
Signed integer\nslt \uff1aSet on less than\nslti \uff1aSet on less than immediate\nUnsigned integer\nsltu \uff1a Set on less than\nsltiu \uff1a Set on less than immediate \n
Slt\u8fd8\u6709\u4e0e\u7acb\u5373\u6570\u7684\u6bd4\u8f83
Slt, slti\u9488\u5bf9\u6709\u7b26\u53f7\u6570\uff0csltu sltiu\u9488\u5bf9\u65e0\u7b26\u53f7\u6570\u4e0d\u540c\u7684\u6bd4\u8f83\u6307\u4ee4\u8fd0\u884c\u5230\u540c\u4e00\u4e2a\u64cd\u4f5c\u6570\u4e0a\uff0c\u7ed3\u679c\u4e5f\u662f\u4e0d\u4e00\u6837\u7684\u5c06\u6709\u7b26\u53f7\u6570\u4f5c\u4e3a\u65e0\u7b26\u53f7\u6570\u6765\u5904\u7406\uff0c\u662f\u68c0\u9a8c0<=x<y\u7684\u4f4e\u5f00\u9500\u65b9\u6cd5\uff0c\u5e38\u7528\u4e8e\u68c0\u67e5\u6570\u7ec4\u7684\u4e0b\u6807\u662f\u5426\u8d8a\u754c\u3002
slt
\u6839\u636e\u6bd4\u8f83\u7ed3\u679c\u8bbe\u7f6e\u5bc4\u5b58\u5668\uff0c\u7136\u540e\u7528beq
\u6216\u8005bne
\u6765\u8fdb\u884c\u5206\u652f\u5224\u65ad\uff0cblt
, bge
\u8fd9\u4e9b\u6307\u4ee4\u662f\u4f5c\u4e3a\u4f2a\u6307\u4ee4slt x5, x6, x7 ; x5 = 1 if x6 < x7 ( a < b) \nbne x5, x0, Less ; go to Less if x5 != 0 (that is, if a < b)\n \u2026\u2026 \nLess:\n
Reduce an index-out-of-bounds check
\u5c06\u6709\u7b26\u53f7\u6570\u5f53\u4f5c\u65e0\u7b26\u53f7\u6570\u5904\u7406\uff0c\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f4e\u6210\u672c\u7684\u65b9\u5f0f\u68c0\u67e5\u662f\u54260<=x<y
\u5e38\u7528\u4e8e\u68c0\u67e5\u6570\u7ec4\u4e0b\u6807\u662f\u5426\u8d8a\u754c
x<y
\uff0c\u5728\u68c0\u6d4bx<y
\u7684\u540c\u65f6\uff0c\u4e5f\u68c0\u6d4b\u4e86x
\u662f\u5426\u4e3a\u8d1f\u6570bgeu x20, x11, IndexOutofBounds
sltu $t0, $a1, $t2 ; x20 < x11 \nbeq $t0, $zero, IndexOutofBounds \n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#loop-statements","title":"Loop statements","text":"Example"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#caseswitch","title":"Case/Switch","text":"\u5b9e\u73b0switch\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u501f\u52a9\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad\u5e8f\u5217,\u628aswitch\u8f6c\u5316\u4e3a\u4e00\u7cfb\u5217\u7684 if-then-else
\u53e6\u4e00\u79cd\u66f4\u6709\u6548\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u7f16\u7801\u5f62\u6210\u4e00\u4e2a\u8f6c\u79fb\u5730\u5740\u8868\uff0c\u5c31\u662f\u5206\u652f\u6307\u4ee4\u5e8f\u5217\u5730\u5740\u8868\uff1b\u7a0b\u5e8f\u901a\u8fc7\u67e5\u627e\u8f6c\u79fb\u5730\u5740\u8868\u6765\u83b7\u53d6\u76ee\u6807\u5730\u5740\uff0c\u5e76\u8df3\u8f6c\u5230\u76f8\u5e94\u7684\u5206\u652f\u6307\u4ee4\u5e8f\u5217
\u8f6c\u79fb\u5730\u5740\u8868\u5c31\u662f\u4e00\u4e2a\u5b57\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5bf9\u5e94\u4e8e\u4ee3\u7801\u4e2d\u5404\u4e2a\u6807\u53f7\u7684\u5730\u5740
\u4e3a\u652f\u6301\u8fd9\u79cd\u60c5\u51b5\uff0c\u6709\u4e00\u6761\u95f4\u63a5\u8df3\u8f6c\u6307\u4ee4\uff0cjalr
: \u65e0\u6761\u4ef6\u5730\u8f6c\u79fb\u5230\u67d0\u4e2a\u5bc4\u5b58\u5668\u6307\u5b9a\u7684\u5730\u5740.
A basic block is a sequence of instructions with
A compiler identifies basic blocks for optimization.
An advanced processor can accelerate execution of basic blocks.
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#supporting-procedures-in-computer-hardware","title":"Supporting Procedures in Computer Hardware","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#procedure-call-instructions","title":"Procedure Call Instructions","text":"Procedure call: jump and link
jal x1
, ProcedureLabe
\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u8fc7\u7a0b\u7684\u6807\u7b7e\u4e3a MyProcedure
\uff0c\u5219 jal x1, MyProcedure
\u5c06\u4f1a\u5c06\u5f53\u524d\u5730\u5740\u4fdd\u5b58\u5230\u5bc4\u5b58\u5668 x1
\u4e2d\uff0c\u5e76\u8df3\u8f6c\u5230 MyProcedure
\u6807\u7b7e\u6240\u4ee3\u8868\u7684\u4ee3\u7801\u5904\u6267\u884c
Procedure return: jump and link register
jalr x0, 0(x1)
x0
as rd
(x0 cannot be changed)\u65e0\u6761\u4ef6\u8df3\u8f6c\uff1a jal x0, Label
\uff0c\u56e0\u4e3ax0\u786c\u8fde\u7ebf\u52300\uff0c\u6548\u679c\u7b49\u540c\u4e8e\u4e22\u5f03\u8fd4\u56de\u5730\u5740
\u4f7f\u7528 x0
\u5bc4\u5b58\u5668\u4f5c\u4e3a\u76ee\u6807\u5bc4\u5b58\u5668\uff08rd
\uff09\uff0c\u5373\u8fd4\u56de\u5730\u5740\u5c06\u88ab\u5b58\u50a8\u5728 x0
\u4e2d\u3002\u7531\u4e8e x0
\u5bc4\u5b58\u5668\u7684\u503c\u6c38\u8fdc\u662f 0
\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u64cd\u4f5c\u53ef\u4ee5\u89c6\u4e3a\u5c06\u8fd4\u56de\u5730\u5740\u4e22\u5f03
Registers for procedure calling
x10~ x17
: 8 argument registers to pass parameters or return values
x1
: one return address register to return to origin point.
\u6808\u662f\u5185\u5b58\u91cc\u7684\u4e00\u5757\u5b58\u50a8\u7ed3\u6784\uff0c\u6808\u5e95\u662f\u9ad8\u5730\u5740\uff0c\u6808\u589e\u957f\u662f\u5730\u5740\u4ece\u9ad8\u5230\u4f4e\uff0cpush\u7684\u65f6\u5019\uff0csp\u51cf\u5c0f\uff0cpop, sp\u589e\u5927
C code
long long int leaf_example (\n long long int g, long long int h,\n long long int i, long long int j) {\n long long int f;\n f = (g + h) - (i + j);\n return f;\n}\n
Arguments g, \u2026, j
in x10, \u2026, x13
.
f
in x20
temporaries .
x5
, x6
.
Need to save x5
, x6
, x20
on stack.
Assembly
leaf_example:\n ;Save x5, x6, x20 on stack\n addi sp,sp,-24\n sd x5,16(sp)\n sd x6,8(sp)\n sd x20,0(sp)\n\n add x5,x10,x11 ;x5 = g + h\n add x6,x12,x1 ;x6 = i + j\n sub x20,x5,x6 ;f = x5 \u2013 x6\n addi x10,x20,0 ;copy f to return register\n\n ;Restore x5, x6, x20 from stack\n ld x20,0(sp)\n ld x6,8(sp)\n ld x5,16(sp)\n addi sp,sp,24\n jalr x0,0(x1)\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#non-leaf-procedures","title":"Non-Leaf Procedures","text":"Caller save\uff1a
Callee save: any S
registers used for local variables
x5 \u2013 x7, x28 \u2013 x31
: temporary registers. Not preserved by the callee.
x8 \u2013 x9, x18 \u2013 x27:
saved registers. If used, the callee saves and restores them.
long long fact ( long long n ) {\n if ( n < 1 ) return ( 1 ) ;\n else return ( n * fact ( n - 1 ) ) ;\n} \n
Main:\n....\n;n is reg x28\nadd x10,x0,x28\nJal fact\n\n;Nonleaf : save return address\nfact: addi sp, sp,-16 ;adjust stack for 2 items\n sd x1, 8(sp) ;save the return address\n sd x10, 0(sp) ;save the argument n\n addi x5, x10, -1 ;x5 = n - 1\n bge x5, x0, L1 ;if n >= 1, go to L1(else)\n addi x10, x0, 1 ;return 1 if n <1\n addi sp, sp, 16 ;Recover sp (Why not recover x1and x10 ?)\n jalr x0, 0(x1) ;return to caller\n\nL1: addi x10, x10, -1 ;n >= 1: argument gets ( n - 1 )\n jal x1, fact ;call fact with ( n - 1 )\n\n add x6, x10, x0 ;x6 = fact(n-1)\n ld x10, 0(sp) ;restore argument n\n ld x1, 8(sp) ;restore the return address\n addi sp, sp, 16 ;adjust stack pointer to pop 2 items\n\n mul x10, x10, x6 ;return n*fact ( n - 1 )\n jalr x0, 0(x1) ;return to the caller\n
X10-x17
returenjal
instruction is used to jump to the procedure and save the current PC+4 into the return address register X0
$a0-$a3
return values in $v0-$v1
\u6808\u8fd8\u9700\u8981\u5b58\u50a8\u4e00\u4e9b\u5c40\u90e8\u53d8\u91cf\uff0c\u4f8b\u5982\u5c40\u90e8\u6570\u7ec4\u6216\u7ed3\u6784, \u8fd9\u4e9b\u53d8\u91cf\u65f6\u5c40\u90e8\u4e8e\u8fc7\u7a0b\uff0c\u4e0d\u9002\u7528\u4e8e\u5bc4\u5b58\u5668\uff0c\u800c\u662f\u4fdd\u5b58\u5728\u6808\u4e2d
\u6808\u4e2d\u5305\u542b\u8fc7\u7a0b\u4fdd\u5b58\u7684\u5bc4\u5b58\u5668\u548c\u5c40\u90e8\u53d8\u91cf\u7684\u6bb5\u79f0\u4e3a\u8fc7\u7a0b\u5e27(frame)\u6216\u8005\u6d3b\u52a8\u8bb0\u5f55(activation record)
\u5728\u8fc7\u7a0b\u4e2d\u6808\u6307\u9488\u53ef\u80fd\u6539\u53d8\uff0c\u5982\u679c\u5229\u7528\u6808\u6307\u9488\u548c\u5730\u5740\u8fd0\u7b97\u6765\u5b8c\u6210\u5bf9\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u975e\u5e38\u4e0d\u65b9\u4fbf,\u56e0\u6b64\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5e27\u6307\u9488fp(x8)
\uff0c\u6307\u5411\u8fc7\u7a0b\u5e27\u7684\u7b2c\u4e00\u4e2a\u53cc\u5b57\uff0c\u8fd9\u6837\u5e27\u6307\u9488\u5c31\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7a33\u5b9a\u7684\u57fa\u5bc4\u5b58\u5668\uff0c\u5bf9\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\u5c31\u53d8\u5f97\u975e\u5e38\u7b80\u5355
\u5982\u679c\u8fc7\u7a0b\u4e2d\u6808\u5185\u6ca1\u6709\u5c40\u90e8\u53d8\u91cf\uff0c\u7f16\u8bd1\u5668\u4e00\u822c\u4e0d\u8bbe\u7f6e\u548c\u4e0d\u6062\u590d\u5e27\u6307\u9488\u4ee5\u8282\u7701\u65f6\u95f4,\u5f53\u4f7f\u7528\u5e27\u6307\u9488\u65f6\uff0c\u5728\u8c03\u7528\u4e2d\u4f7f\u7528sp\u6765\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4e14\u53ef\u4ee5\u4f7f\u7528fp
\u6062\u590d sp
.
\u6ce8\u610f, \u4e0d\u7ba1\u662f\u5426\u4f7f\u7528\u663e\u5f0f\u7684\u5e27\u6307\u9488\uff0c\u6808\u4e0a\u90fd\u4f1a\u663e\u793a\u4e00\u6761\u6d3b\u52a8\u8bb0\u5f55\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ef4\u62a4\u7a33\u5b9a\u7684sp\u6765\u51cf\u5c11\u5bf9fp\u7684\u4f7f\u7528
\u9664\u4e86\u52a8\u6001\u53d8\u91cf\uff0cC\u7a0b\u5e8f\u5458\u8fd8\u9700\u8981\u4e3a\u9759\u6001\u53d8\u91cf\u548c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u5206\u914d\u5185\u5b58\u7a7a\u95f4
\u8fd9\u4e2a\u56fe\u5c55\u793a\u4e86\u8fd0\u884clinux OS\u65f6\u7a0b\u5e8f\u548c\u6570\u636e\u5728RISC-V\u5185\u5b58\u5206\u914d\u7684\u7ea6\u5b9a,\u8fd9\u4e9b\u5730\u5740\u53ea\u662f\u4e00\u79cd\u8f6f\u4ef6\u89c4\u5b9a\uff0c\u5e76\u4e0d\u662fRISC-V\u4f53\u7cfb\u7ed3\u6784\u7684\u4e00\u90e8\u5206\u3002
\u6808\u7531\u5185\u5b58\u9ad8\u5730\u5740\u5f00\u59cb\uff0c\u5411\u4e0b\u589e\u957f:\u5185\u5b58\u4f4e\u5730\u5740\u6bb5\u5148\u662f\u4fdd\u7559\u533a\u57df\uff0c\u7136\u540e\u65f6\u673a\u5668\u4ee3\u7801\u7684\u5b58\u50a8\u533a\u57df\uff0c\u79f0\u4e3a\u6b63\u6587\u6bb5\uff0c\u4e4b\u4e0a\u662f\u9759\u6001\u6570\u636e\u6bb5\uff0c\u7528\u4e8e\u5b58\u50a8\u5e38\u91cf\u548c\u5176\u4ed6\u9759\u6001\u53d8\u91cf\u7684\u7a7a\u95f4\u3002\u7c7b\u4f3c\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\u901a\u5e38\u4f1a\u968f\u7740\u751f\u547d\u671f\u589e\u957f\u6216\u8005\u7f29\u77ed\uff0c\u8fd9\u7c7b\u6570\u636e\u7ed3\u6784\u5bf9\u5e94\u7684\u6bb5\u79f0\u4e3a\u5806(heap),\u5b83\u653e\u5728\u5185\u5b58\u4e2d,\u4f4d\u4e8e\u9759\u6001\u6570\u636e\u4e4b\u540e
\u6808\u548c\u5806\u76f8\u4e92\u589e\u957f\uff0c\u5728\u4e24\u4e2a\u6bb5\u6b64\u6d88\u5f7c\u957f\u7684\u8fc7\u7a0b\u4e2d\u8fbe\u5230\u5185\u5b58\u7684\u9ad8\u6548\u4f7f\u7528,C\u8bed\u8a00\u901a\u8fc7\u663e\u793a\u51fd\u6570malloc(), free()\u5206\u914d\u548c\u91ca\u653e\u7a7a\u95f4\uff0c\u5bb9\u6613\u5bfc\u81f4\u6f0f\u6d1e\u3002\u5fd8\u8bb0\u91ca\u653e\u4f1a\u5bfc\u81f4\u201c\u5185\u5b58\u6cc4\u6f0f\u201d\uff08\u6d88\u8017\u6389\u592a\u591a\u5185\u5b58\u5bfc\u81f4\u64cd\u4f5c\u7cfb\u7edf\u5d29\u6e83\uff09\uff0c\u8fc7\u65e9\u7684\u91ca\u653e\u4f1a\u5bfc\u81f4\u201c\u60ac\u7a7a\u6307\u9488\u201d\uff08\u6307\u9488\u6307\u5411\u7a0b\u5e8f\u4ece\u672a\u8bbf\u95ee\u7684\u4f4d\u7f6e\uff09
Jave\u4f7f\u7528\u81ea\u52a8\u5185\u5b58\u5206\u914d\u548c\u5783\u573e\u56de\u6536\u673a\u5236\u6765\u907f\u514d\u8fd9\u7c7b\u9519\u8bef
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#communication-with-people","title":"communication with people","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#bytehalfwordword-operations","title":"Byte/Halfword/Word Operations","text":"Load byte/halfword/word: Sign extend to 64 bits in rd \u2022 lb rd, offset(rs1) \u2022 lh rd, offset(rs1) \u2022 lw rd, offset(rs1) Load byte/halfword/word unsigned: Zero extend to 64 bits in rd \u2022 lbu rd, offset(rs1) \u2022 lhu rd, offset(rs1) \u2022 lwu rd, offset(rs1) Store byte/halfword/word: Store rightmost 8/16/32 bits \u2022 sb rs2, offset(rs1) \u2022 sh rs2, offset(rs1) \u2022 sw rs2, offset(rs1)
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#string","title":"String","text":"Three choices for representing a string * Place the length of the string in the first position * An accompanying variable has the length * A character in the last position to mark the end of a string
void strcpy ( char x[ ] , char y[ ] )\n {\n size_t i ;\n i = 0 ;\n while ( ( x[ i ] = y[ i ] ) != \u2018\\ 0\u2019 ) /* copy and test byte */\n i += 1 ;\n }\n
Assume : i--x19, x's base--x10, y's base--x11 strcpy :\n addi sp,sp,-8\n sd x19,0(sp)\n add x19,x0,x0\nL1:\n add x5,x11,x19 ;x5 = &y[i]\n lbu x6,0(x5) ;x6 = y[i]\n add x7,x10,x19 ;x7 = &x[i]\n sb x6,0(x7) ;x[i] = y[i]\n beq x6,x0,Exit ;if y[i] == 0, exit\n addi x19,x19,1 ;i += 1\n jal x0,L1\nExit:\n ld x19,0(sp)\n addi sp,sp,8\n jalr x0,0(x1)\n
For a leaf procedure * The compiler exhausts all temporary registers * Then use the registers it must save x5 \u2013 x7, x28 \u2013 x31: temporary registers : Not preserved by the callee
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#risc-v-addressing-for-wide-immediate-addresses","title":"RISC-V Addressing for Wide Immediate & Addresses","text":"For occasional 32-bit constant lui rd,constant
* copy the 20-bit constant to the upper 20 bits of rd * Extends the constant to 64 bits by setting the lower 12 bits to 0 Better: * lui rd,constant[31:12]
* ori rd,rd,constant[11:0]
More specifically beq x0,x0 Loop
OP + func3 \u2192 beq rs1 \u2192 x0 rs2 \u2192 x0 offset = -20 = 111111101100 \u622a\u65ad\u6700\u540e\u4e00\u4f4d\uff0c\u5f97\u5230 11111110110 im[12] + im[10:5] + im[4:1] + im[11] + 0 rd/offset -> im[4:1]+im[11] im[12] : sign bit
Offset > 12 bits ? Rewrite it to offer a much greater branching distance:
bne x10, x0, L2\njal x0, L1\nL2:...\n
JAL
instruction: 20-bit offset How to know the offset? Label Table
Object File: Linking Object modules Dynamic Linking Only link/load the needed library routines when it is called * Requires procedure code to be relocatable * Avoids image bloat caused by static linking of all (transitively) referenced libraries * Automatically picks up new library versions
Lazy Linkage Second time the link is called,much faster.
\u6267\u884c\u6587\u4ef6 * \u5728\u786c\u76d8\u4e0a\uff0c\u975e\u6267\u884c\u6001\uff1a\u5982\u75c5\u6bd2\u6837\u672c \u8fdb\u7a0b * \u8f6c\u8f7d\u5230\u5185\u5b58 * \u53ef\u4ee5\u7ec6\u5206\u4e3a\u591a\u4e2a\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u7684\u7ebf\u7a0b\uff1a\u5982\u6fc0\u6d3b\u6001\u75c5\u6bd2 * \u5982\u4f55\u770b\u7ebf\u7a0b: \u4efb\u52a1\u7ba1\u7406\u5668
"},{"location":"%E8%AE%A1%E7%BB%84/Chap2/lec/#example","title":"Example","text":"void swap ( long long v[ ] , size_t k )\n {\n long lon temp ;\n temp = v[ k ] ;\n v[ k ] = v[ k + 1 ] ;\n v[ k + 1 ] = temp ;\n }\n
swap:\n slli x6,x11,3 //x6 = k*8\n add x6,x10,x6 //x6 = &v[k]\n ld x5,0(x6) //x5 = v[k]\n ld x7,8(x6) //x7 = v[k+1]\n sd x7,0(x6) //v[k] = v[k+1]\n sd x5,8(x6) //v[k+1] = v[k]\n jalr x0,0(x1) //return\n
void sort (long long v[ ] , size_t n )\n {\n size_t i , j ;\n for ( i = 0 ; i < n ; i + = 1 ) {\n for ( j = i - 1 ; j >= 0 && v[j] > v[j+1] ; j -= 1 )\n swap ( v , j ) ;\n }\n }\n
sort:\n addi sp,sp,-40\n sd x1 ,32(sp)\n sd x22,24(sp)\n sd x21,16(sp)\n sd x20,8(sp)\n sd x19,0(sp)\nfor1tst:\n bge x19,x20,exit1\n ....\nexit2:\n addi x19,x19,1\n j for1tst\nexit1:\n ld x19,0(sp)\n ld x20,8(sp)\n ld x21,16(sp)\n ld x22,24(sp)\n ld x1,32(sp)\n addi sp,sp,40\n jalr x0,0(x1)\n
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/","title":"Chapter3","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#chap3arithmetic-for-computer","title":"Chap3.Arithmetic for computer","text":"Introduction"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#numbers","title":"Numbers","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#signed-and-unsigned-numbers-possible-representations","title":"Signed and Unsigned Numbers Possible Representations","text":"Example
To know a negative num's definite value : also invert and plus one
Biased notation
1000 0000
\\(=\\) minimal negative value(\\(-2^7\\)) 0111 1111
\\(=\\) maximal positive value (\\(2^7-1\\))Sign Extention lbu
vs. lb
Signed integer
slt
Set when less than
slti
Set when less than immediate
Unsigned integer
sltu
Set when less than
sltiu
Set when less than immediate
\"sltu\"\u548c\"sltiu\"\u90fd\u662fMIPS\u6c47\u7f16\u8bed\u8a00\u4e2d\u7684\u6307\u4ee4\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u533a\u522b\uff1a
sltu\uff1a
sltiu\uff1a
New RISC V instructions
lbu
load byte unsigned : Loads a byte into the lowest 8 bit of a register
Fills the remaining bits with 0
Lb
load byte (signed)Loads a byte into the lowest 8 bit of a register
Extends the highest bit into the remaining 24 bits
sltu
: set on less than unsignedslti
: set on less than immediatesltiu
: set on less than unsigned immediateLogical Operations
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#arithmetic","title":"Arithmetic","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#addition-subtraction","title":"Addition & subtraction","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#overflow","title":"Overflow","text":"^
Carry bit^
\\(Carry] and \\ Operation_{add\\ or\\ sub}\\)addadd immediate (addi)
subtract (sub)
add unsigned (addu)
add immediate unsigned (addiu)
Subtract unsigned (subu)
\\(S=X\\oplus Y\\\\ C=XY\\)
\\(Sum = X\\oplus Y\\oplus Carry_{in}\\)\u200b
\\(Carry_{out} = XY +(X+Y)Carry_{in}\\)\u200b
\\(X\\oplus Y\\) only different from \\(X+Y\\) when \\(XY=1\\)
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#overflow-detection","title":"Overflow Detection","text":"module alu(A, B, ALU_operation, res, zero, overflow );\n input [31:0] A, B;\n input [2:0] ALU_operation;\n output [31:0] res;\n output zero, overflow ;\n wire [31:0] res_and,res_or,res_add,res_sub,res_nor,res_slt;\n reg [31:0] res;\n parameter one = 32'h00000001, zero_0 = 32'h00000000;\n assign res_and = A&B;\n assign res_or = A|B;\n assign res_add = A+B;\n assign res_sub = A-B;\n assign res_slt =(A < B) ? one : zero_0;\n always @ (A or B or ALU_operation)\n case (ALU_operation)\n 3'b000: res=res_and; \n 3'b001: res=res_or; \n 3'b010: res=res_add; \n 3'b110: res=res_sub; \n 3'b100: res=~(A | B);\n 3'b111: res=res_slt;\n default: res=32'hx;\n endcase\n assign zero = (res==0)? 1: 0;\nendmodule\n
\\(P_i = A_i\\oplus B_i \\ \\ \\ \\ G_i = A_iB_i\\)\u200b
\\(S_i = P_i\\oplus C_i\\ \\ \\ \\ C_{i+1} = G_i + P_iC_i\\)\u200b\u200b
Improvement -- Reduce FAN-OUT"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#carry-skip-adder","title":"Carry Skip Adder","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#carry-select-adder-csa","title":"Carry Select Adder (CSA)","text":"
Already caclulate(parallel) different situations,once the \\(C_0\\)\u200b is delivered, the result can be output.
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#multiplexer","title":"Multiplexer","text":"Look at current bit position
Multiplexter V1
Multiplexter V2
Multiplexter V3
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#booths-algorithm","title":"Booth's Algorithm","text":"
Idea:
1s
subtract at first '1'
in multiplier'1s'
'1\u2018
Action
1
0
: subtract multiplicand from left1
1
no arithmetic operation0
1
add multiplicand to left half0
0
no arithmetic operation\\(Bit_{-1} = 0\\)
Arithmetic shift right:
4.1 \u5df2\u7ecf\u7ed3\u675f\u4e86\u9664\u6cd5\u64cd\u4f5c\uff0c\u6b64\u65f6\u7684\u9ad8\u4f4d\u5c31\u662f\u6211\u4eec\u7684\u4f59\u6570\uff0c\u4f46\u662f\u8fd9\u6700\u540e\u4e00\u6b21\u7684\u7ed3\u679c\u8fd8\u6ca1\u6709\u653e\u56de\u5230 Reminder \u4e2d\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u518d\u5f80\u5de6\u79fb\u4e00\u4f4d\u4e3a\u5546\u7559\u51fa\u7a7a\u95f4\uff0c\u653e\u5165\u540e\uff0c\u518d\u628a\u9ad8\u4f4d\u4f59\u6570\u5f80\u53f3\u79fb\u52a8\u4ee5\u62b5\u6d88\u5f71\u54cd
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#signed-division","title":"Signed division","text":"Standardized format IEEE 754
8 bit exp, 23 bit significant
11 bit exp, 52 bit significant
Leading '1' bit of significand is implicit
M: \u5c3e\u6570. \u5373\u9ed8\u8ba4.xxx
\u662f1.xxx
(\u56e0\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u6ca1\u67090.xxx
)
Exponent is biased(\u79fb\u7801)
Bias 127 for single precision
Bias 1023 for double precision
Have to be transfered back,but treated like unsigned inside
NOTE :\\((-1)^{sign} \u2022 (1 + significand) \u2022 2^{exponent - bias}\\)\u200b
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#limitations","title":"Limitations","text":"Overflow: &. Underflow
EXAMPLE"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#floating-point-addition","title":"Floating Point Addition","text":"
Algorithm
Add exponents - bias.
Multiply the significands.
Normalize.
Over- underflow.
Rounding.
Sign.
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#float-devision","title":"float devision","text":"
\u4e3a\u4e86\u4fdd\u8bc1\u56db\u820d\u4e94\u5165\u7684\u7cbe\u5ea6,\u7ed3\u679c\u6ca1\u6709,\u53ea\u5728\u8fd0\u7b97\u7684\u8fc7\u7a0b\u4e2d\u4fdd\u7559
EXAMPLE
A bit used in rounding in addition to guard and round that is set whenever there are nonzero bits to the right of the round bit.
"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#round-modes","title":"Round Modes","text":"EXAMPLE"},{"location":"%E8%AE%A1%E7%BB%84/Chap3/lec/#parallelism-and-computer-arithmetic-associativity","title":"Parallelism and Computer Arithmetic: Associativity","text":"
if \\(x + (y+ z) = (x + y) + z ?\\)
\u5df2\u77e5\\(F(n) = \u03a32^i = 2^{n+1} -1 = 111\u20261B\\)
int f1\uff08 unsigned n){ \n int sum = 1, power = 1;\n for ( unsigned i=0; i<=n-1; i++ ){ \n power * = 2;\n sum += power;\n }\n return sum;\n}\n
\u5047\u8bbeunsigned\u548cint\u578b\u6570\u636e\u90fd\u536032\u4f4d\uff0cfloat\u91c7\u7528IEEE 754\u5355\u7cbe\u5ea6\u6807\u51c6(IEEE 754\u91c7\u7528\u7684\u662f\u6700\u8fd1\u820d\u5165round to nearest
\u7684\u65b9\u5f0f) , \u56de\u7b54\u4e00\u4e0b\u95ee\u9898\uff1a
\u5f53n=0\u65f6\uff0c\\(f1\\)\u4f1a\u51fa\u73b0\u6b7b\u5faa\u73af\uff0c\u4e3a\u4ec0\u4e48\uff1f\u82e5\u5c06\\(f1\\)\u4e2d\u53d8\u91cf\\(i\\)\u548c\\(n\\)\u90fd\u5b9a\u4e49\u4e3aint\u578b\uff0c\u5219\\(f1\\)\u200b\u662f\u5426\u8fd8\u4f1a\u51fa\u73b0\u6b7b\u5faa\u73af\uff1f\u4e3a\u4ec0\u4e48\uff1f
unsigned 0 -1
-- \u5927\u6570\uff0ci<=n-1
\u6c38\u771f\uff0c\u6240\u4ee5\u6b7b\u5faa\u73af.
\u82e5i\u548cn\u6539\u4e3aint\u7c7b\u578b\uff0c\u4e0d\u4f1a\u6b7b\u5faa\u73af:n=0
\u65f6\uff0cn-1
\u7684\u503c\u4e3a-1
\uff0c\u6545i<=n-1
\u4e0d\u6210\u7acb\uff0c\u9000\u51fa\u5faa\u73af
f1(23)\u548cf2(23) \u7684\u8fd4\u56de\u503c\u662f\u5426\u76f8\u7b49\uff1f\u673a\u5668\u6570\u5404\u662f\u4ec0\u4e48\uff08\u7528\u5341\u516d\u8fdb\u5236\u8868\u793a)
Signle presion bias = 127
\\(f1(23)=00FFFFFFh\\)
\\(f2(23)=0_{sign}10010110_{exponent(bias)}....=0100\\ 1011\\ 0111\\ 1111\\ 1111\\ 1111\\ 1111\\ 1111=47BFFFFFH\\)
\\(f1(24)\\)\u548c\\(f2(24)\\)\u7684\u8fd4\u56de\u503c\u5206\u522b\u4e3a\\(33554431\\)\u548c\\(33554432.0\\)\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f8\u7b49\uff1f
\u5f53\\(n=24\\)\u65f6\uff0c\\(f(24)=11\u2026.1B\\),float\u53ea\u670924\u4f4d\u6709\u6548\u4f4d\uff0c\u820d\u5165\u540e\u6570\u503c\u589e\u5927\uff0c\u6240\u4ee5\\(f2(24)\\)\u6bd4\\(f1(24)\\)\u5927\\(1\\).
\\(f(31) = 2^{32} -1\\), \u800c\\(f1(31)\\)\u7684\u8fd4\u56de\u503c\u5374\u4e3a\\(-1\\)\uff0c\u4e3a\u4ec0\u4e48\uff1f\u82e5\u4f7f\\(f1(n)\\)\u7684\u8fd4\u56de\u503c\u4e0e\\(f(n)\\)\u200b\u76f8\u7b49\uff0c\u6700\u5927\u7684n\u662f\u591a\u5c11\uff1f
\\(F(31)\\)\u8d85\u51faint\u6570\u636e\u7684\u8868\u793a\u8303\u56f4\uff0c\u7528\\(f1(31)\\)\u5b9e\u73b0\u65f6\u5f97\u5230\u673a\u5668\u6570\u4e3a32\u4e2a1\uff0c\u4f5c\u4e3aint\u89e3\u91ca\u65f6\u5176\u503c\u4e3a-1
\u56e0\u4e3aint
\u6700\u5927\u53ef\u8868\u793a\u6570\u4e3a0\u540e\u8ddf31\u4e2a1\uff0c\u6240\u4ee5\\(f1(n)\\)\u7684\u8fd4\u56de\u503c\u4e0e\\(f(n)\\)\u76f8\u7b49\u7684\u6700\u5927\\(n\\)\u503c\u662f30\u3002
\\(f2(127)\\)\u7684\u673a\u5668\u6570\u4e3a\\(7F80 0000H\\)\uff0c\u5bf9\u5e94\u7684\u503c\u662f\u4ec0\u4e48\uff1f\u82e5\u4f7f\\(f2(n)\\)\u7684\u7ed3\u679c\u4e0d\u6ea2\u51fa\uff0c\u5219\u6700\u5927\u7684n\u662f\u591a\u5c11\uff1f\u82e5\u4f7f\\(f2(n)\\)\u7684\u7ed3\u679c\u7cbe\u786e\uff08\u65e0\u820d\u5165\uff09\uff0c\u5219\u6700\u5927\u7684\\(n\\)\u200b\u662f\u591a\u5c11\uff1f
\u9636\u7801\u51681,\u5c3e\u6570\u4e3a0
\u8868\u793a\u65e0\u7a77\u5927F2\u8fd4\u56de\u503c\u4e3afloat\uff0c\u673a\u5668\u6570\u4e3a\\(7F80 0000H\\)\u5bf9\u5e94\u7684\u503c\u662f\\(+\u221e\\)
n=126
\u65f6\uff0c\u5bf9\u5e94\u9636\u7801\u4e3a253\uff0c\u5c3e\u6570\u90e8\u5206\u820d\u5165\u540e\u9636\u7801\u52a01\uff0c\u6700\u7ec8\u9636\u7801\u4e3a254\uff0c\u4e0d\u6ea2\u51fa\u7684\u6700\u5927n\u503c\u4e3a126
.N=23
\u65f6\uff0cf(23)\u4e3a24\u4f4d1\uff0cfloat\u670924\u4f4d\u6709\u6548\u4f4d\uff0c\u6240\u4ee5\u4e0d\u9700\u820d\u5165\uff0c\u7ed3\u679c\u7cbe\u786e
\u6545\u4f7ff2\u83b7\u7cbe\u786e\u7ed3\u679c\u7684\u6700\u5927n\u503c\u4e3a 23.
In RISC-V: Supervisor Exception Program Counter (SEPC) (P316, 7th line)
In RISC-V: Supervisor Exception Cause Register (SCAUSE) 64 bits, but most bits unused
Assume at 0000 0000 1C09 0000hex Entry address in a special register :Suptervisor Trap Vector (STVEC), which can be loaded by OS.
Close interrupts * Read cause, and transfer to relevant handler * Determine action required * If restartable \u2013 Take corrective action \u2013 use SEPC to return to program (mret) * Otherwise \u2013 Terminate program \u2013 Report error using SEPC, SCAUSE, ... \u2013 OS make the choice to transfer to another ready process
"},{"location":"%E8%AE%A1%E7%BB%84/Chap4/lec/#csr","title":"CSR","text":""},{"location":"%E8%AE%A1%E7%BB%84/Chap4/lec/#8-important-csr-for-exception-handling","title":"8 important CSR for exception handling","text":"mtvec
* ( Machine Trap Vector): jump to this address when exception mepc
* (Machine Exception PC): the instruction raise the exception * When Interruption or Exception, the current PC will be saved in mepc
mcause
* (Machine Cause): the cause of the exception,which kindmie
* (Machine Interrupt Enable): enable or disable the interruptmip
* (Machine Interrupt Pending): the interrupt is pending or notmtval
* (Machine Trap Value): the value of the exception * error address , illegal instruction, or 0mscrach
* (Machine Scratch): a temporary registermstatus
* (Machine Status): the status of the machine * MPIE: Machine Previous Interrupt Enable Save the previous status of the interrupt * Exceptions \u7acb\u523b\u5904\u7406\uff0c\u6ca1\u6709exception \u624d\u8003\u8651 interrupts
mstatus.MIE = 1 && mie[i] = 1 && mip [i]= 1
CPI \u8868\u793a\u7684\u662f \"Clocks Per Instruction\"\uff0c\u5373\u6bcf\u6761\u6307\u4ee4\u7684\u65f6\u949f\u5468\u671f\u6570\u3002\u5b83\u662f\u8861\u91cf\u8ba1\u7b97\u673a\u5904\u7406\u5668\u6027\u80fd\u7684\u91cd\u8981\u6307\u6807\u4e4b\u4e00\u3002CPI \u6570\u503c\u8d8a\u4f4e\uff0c\u8868\u793a\u5904\u7406\u5668\u5728\u6267\u884c\u6307\u4ee4\u65f6\u9700\u8981\u7684\u65f6\u949f\u5468\u671f\u8d8a\u5c11\uff0c\u6027\u80fd\u8d8a\u597d\u3002
\u5176\u4e2d\uff0c\"Total Clock Cycles\" \u8868\u793a\u6267\u884c\u6240\u6709\u6307\u4ee4\u6240\u9700\u7684\u603b\u65f6\u949f\u5468\u671f\u6570\uff0c\"Total Instructions Executed\" \u8868\u793a\u6267\u884c\u7684\u603b\u6307\u4ee4\u6570\u3002
\u901a\u8fc7\u76d1\u6d4b\u548c\u4f18\u5316(\u51cf\u5c11) CPI\uff0c\u53ef\u4ee5\u6539\u5584\u5904\u7406\u5668\u7684\u6027\u80fd\uff0c\u4f8b\u5982\u51cf\u5c11\u6307\u4ee4\u6267\u884c\u65f6\u95f4\uff0c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\uff0c\u4ee5\u53ca\u964d\u4f4e\u80fd\u8017\u3002
"},{"location":"%E8%AE%A1%E7%BB%84/Intro/intro/#cpu-time","title":"CPU Time","text":"Clock cycle: This refers to the basic unit of time used by a computer's CPU (Central Processing Unit) in executing instructions. It's the time it takes for one complete pulse of the CPU's clock signal. All operations in a computer, such as fetching instructions, decoding them, executing them, and storing results, are synchronized by this clock signal. Clock cycles are typically measured in units like milliseconds (ms), microseconds (\u03bcs), or nanoseconds (ns), depending on the speed of the processor.
Clock rate: Also known as clock speed or clock frequency, this is the rate at which a CPU executes instructions, measured in cycles per second (Hz). For example, a CPU with a clock rate of 2 gigahertz (GHz) performs 2 billion clock cycles per second. Higher clock rates generally mean faster processing speeds, assuming other factors remain constant.
CPU time: This represents the total time taken by a CPU to complete a task or process. It's typically measured in seconds. CPU time is influenced by factors such as the clock rate, the number of clock cycles required to execute each instruction, and the total number of instructions executed.
Performance improved by * Reducing number of clock cycles * Increasing clock rate * Hardware designer must often trade off clock rate against cycle count
"}]} \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 276a7319..f27ba4f4 100644 Binary files a/sitemap.xml.gz and b/sitemap.xml.gz differ diff --git "a/\350\256\241\347\256\227\347\220\206\350\256\272/lec7/Lecture7/index.html" "b/\350\256\241\347\256\227\347\220\206\350\256\272/lec7/Lecture7/index.html" index 18881ae3..9427b73e 100644 --- "a/\350\256\241\347\256\227\347\220\206\350\256\272/lec7/Lecture7/index.html" +++ "b/\350\256\241\347\256\227\347\220\206\350\256\272/lec7/Lecture7/index.html" @@ -1168,15 +1168,15 @@\(A_{TM} = \{<M, w>|M\text{ is a TM and M accepts w}\}\) -* \(A_{TM}\) is not recursive
+\(A_{TM} = \{<M, w>|M\text{ is a TM and M accepts w}\}\)
+Construct a TM \(M^*\ H_{TM}\) to solve \(A_{TM}\):
\(H_{TM} = {<M, w>|M\text{ is a TM and M halts on w}}\)
-$M^* = $ On input x: -1. run M on w -2. If M accepts w, accept -3. If M rejects w, reject -4. If M loops on w, \(M^*\) is looping on w
+\(M^* =\) On input x:
+Suppose \(M^*\) is a recursive TM, then we can construct a TM \(M_{H}\) to decide \(H_{TM}\):
-$M_{A} = $ On input
If all this is true, then \(M_A\) can decide \(A_{TM}\) -* But \(A_{TM}\) is not recursive -* So \(M^*\) is not recursive -* So the halting problem is not recursive
+\(M_{A} =\) On input
If all this is true, then \(M_A\) can decide \(A_{TM}\)
+A Turing machine \(M\) accepts \(w\) if and only if \(M^*\) accepts \(e\)
-$M^* = $ On input x: -1. Run M on w -2. If M accepts w, accept -3. If M rejects w, reject
+\(M^* =\) On input x:
+If M accepts w, then \(M^*\) accepts all inputs, including e
If M rejects or loops on w, then \(M^*\) rejects all inputs, including e
Suppose \(L\) is recursive, then we can construct a TM \(M_{L}\) to decide \(L\):
-$M_{A} = $ On input
If all this is true, then \(M_A\) can decide \(A_{TM}\) -* But \(A_{TM}\) is not recursive -* So \(M_L\) is not recursive -* So \(L\) is not recursive
+\(M_{A} =\) On input
If all this is true, then \(M_A\) can decide \(A_{TM}\)
+Suppose that \(L_3\) is recursive, \(\exist M_3\) that decides \(L_3\)
Use \(M_3\) to construct \(M_2\) that deides \(L_2\)
-\(M_2\) = on input "M" -1. construct \(M_E\) as follows: - \(M_E\) = on input x - 1. Accept -2. Run \(M_3\) on "\(M\)","\(M_E\)" -- check whether \(M\) and \(M_E\) accepts the same set of strings -3. If \(M_3\) accepts, accepts \(M\) -4. If \(M_3\) rejects, rejects \(M\)
+\(M_2\) = on input "M"
+So \(M_2\) decides \(L_2\) but \(L_2\) is not recursive
Proof: \(L_4\) is not recursive
@@ -4847,19 +4865,22 @@Language of \( M^* \): The language \( L(M^*) \) is constructed so that: - - \( L(M^*) \) is non-empty if \( M \) accepts \( w \). - - \( L(M^*) \) = \(L(U)\) = \(A_{TM}\) Not regular/Not Context-Free /Not Recursive - - \( L(M^*) = \emptyset \) if \( M \) does not accept \( w \). -- Regular/Contex-Free/Recursive
-Conclusion: - - If \( L(M^*) \) is non-regular, it implies that \( M \) accepts \( w \). - - If \( L(M^*) \) is regular, then \( M \) does not accept \( w \). - - Since determining whether \( L(M^*) \) is regular would solve the Halting Problem, it follows that \( R_{TM} \) is undecidable.
-The undecidability of \( R_{TM} = \{ \langle M \rangle : M \text{ is a Turing machine with } L(M) \text{ regular} \} \) (i.e., determining if a Turing machine \( M \) has a regular language \( L(M) \)) stems from the fact that if we could decide \( R_{TM} \), we could also decide the Halting Problem, which is known to be undecidable.
In short, if we could decide whether \( L(M) \) is regular for any Turing machine \( M \), we could also solve the Halting Problem by constructing \( M^* \) as described. This leads to a contradiction, proving that \( R_{TM} \) is undecidable.
-