Update tutorial3_orthogonalization.ipynb
This commit is contained in:
@@ -104,6 +104,34 @@
|
|||||||
"⚠️ CGS can lose orthogonality in finite precision arithmetic.\n"
|
"⚠️ CGS can lose orthogonality in finite precision arithmetic.\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "3fe97ce3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Q (CGS): Matrix([[0.8164965809277261, -0.5520524474738834], [0.4082482904638631, 0.7590721152765896], [0.4082482904638631, 0.34503277967117707]])\n",
|
||||||
|
"R (CGS): Matrix([[2.449489742783178, 0.4082482904638631], [0.0, 2.41522945769824]])\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from numethods import Matrix, Vector\n",
|
||||||
|
"from numethods import QRGramSchmidt, QRModifiedGramSchmidt, QRHouseholder, LeastSquaresSolver\n",
|
||||||
|
"\n",
|
||||||
|
"# Example matrix\n",
|
||||||
|
"A = Matrix([[2, -1], [1, 2], [1, 1]])\n",
|
||||||
|
"\n",
|
||||||
|
"# Classical Gram-Schmidt\n",
|
||||||
|
"qrg = QRGramSchmidt(A)\n",
|
||||||
|
"print(\"Q (CGS):\", qrg.Q)\n",
|
||||||
|
"print(\"R (CGS):\", qrg.R)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "ba84b59a",
|
"id": "ba84b59a",
|
||||||
@@ -126,6 +154,28 @@
|
|||||||
"MGS is more stable than CGS.\n"
|
"MGS is more stable than CGS.\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "e01e25ff",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Q (MGS): Matrix([[0.8164965809277261, -0.5520524474738834], [0.4082482904638631, 0.7590721152765896], [0.4082482904638631, 0.34503277967117707]])\n",
|
||||||
|
"R (MGS): Matrix([[2.449489742783178, 0.4082482904638631], [0.0, 2.41522945769824]])\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# Modified Gram-Schmidt\n",
|
||||||
|
"qrm = QRModifiedGramSchmidt(A)\n",
|
||||||
|
"print(\"Q (MGS):\", qrm.Q)\n",
|
||||||
|
"print(\"R (MGS):\", qrm.R)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "d893d189",
|
"id": "d893d189",
|
||||||
@@ -152,6 +202,28 @@
|
|||||||
"$$"
|
"$$"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "15dfc35c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Q (Householder): Matrix([[-0.8164965809277258, 0.552052447473883, -0.16903085094570333], [-0.40824829046386296, -0.7590721152765892, -0.5070925528371099], [-0.40824829046386296, -0.34503277967117707, 0.8451542547285166]])\n",
|
||||||
|
"R (Householder): Matrix([[-2.449489742783177, -0.408248290463863], [2.220446049250313e-16, -2.415229457698238], [2.220446049250313e-16, 2.220446049250313e-16]])\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# Householder QR\n",
|
||||||
|
"qrh = QRHouseholder(A)\n",
|
||||||
|
"print(\"Q (Householder):\", qrh.Q)\n",
|
||||||
|
"print(\"R (Householder):\", qrh.R)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "2b6c612f",
|
"id": "2b6c612f",
|
||||||
@@ -177,62 +249,25 @@
|
|||||||
"So we can solve using back-substitution.\n"
|
"So we can solve using back-substitution.\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"id": "2740a134",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"\n",
|
|
||||||
"## 6. Examples with `numethods`\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 4,
|
||||||
"id": "212e6f58",
|
"id": "25b399b7",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Q (CGS): Matrix([[0.8164965809277261, -0.5520524474738834], [0.4082482904638631, 0.7590721152765896], [0.4082482904638631, 0.34503277967117707]])\n",
|
|
||||||
"R (CGS): Matrix([[2.449489742783178, 0.4082482904638631], [0.0, 2.41522945769824]])\n",
|
|
||||||
"Q (MGS): Matrix([[0.8164965809277261, -0.5520524474738834], [0.4082482904638631, 0.7590721152765896], [0.4082482904638631, 0.34503277967117707]])\n",
|
|
||||||
"R (MGS): Matrix([[2.449489742783178, 0.4082482904638631], [0.0, 2.41522945769824]])\n",
|
|
||||||
"Q (Householder): Matrix([[-0.8164965809277258, 0.552052447473883, -0.16903085094570333], [-0.40824829046386296, -0.7590721152765892, -0.5070925528371099], [-0.40824829046386296, -0.34503277967117707, 0.8451542547285166]])\n",
|
|
||||||
"R (Householder): Matrix([[-2.449489742783177, -0.408248290463863], [2.220446049250313e-16, -2.415229457698238], [2.220446049250313e-16, 2.220446049250313e-16]])\n",
|
|
||||||
"Least squares solution: Vector([1.0285714285714287, 0.828571428571429])\n"
|
"Least squares solution: Vector([1.0285714285714287, 0.828571428571429])\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"\n",
|
|
||||||
"from numethods import Matrix, Vector\n",
|
|
||||||
"from numethods import QRGramSchmidt, QRModifiedGramSchmidt, QRHouseholder, LeastSquaresSolver\n",
|
|
||||||
"\n",
|
|
||||||
"# Example matrix\n",
|
|
||||||
"A = Matrix([[2, -1], [1, 2], [1, 1]])\n",
|
|
||||||
"\n",
|
|
||||||
"# Classical Gram-Schmidt\n",
|
|
||||||
"qrg = QRGramSchmidt(A)\n",
|
|
||||||
"print(\"Q (CGS):\", qrg.Q)\n",
|
|
||||||
"print(\"R (CGS):\", qrg.R)\n",
|
|
||||||
"\n",
|
|
||||||
"# Modified Gram-Schmidt\n",
|
|
||||||
"qrm = QRModifiedGramSchmidt(A)\n",
|
|
||||||
"print(\"Q (MGS):\", qrm.Q)\n",
|
|
||||||
"print(\"R (MGS):\", qrm.R)\n",
|
|
||||||
"\n",
|
|
||||||
"# Householder QR\n",
|
|
||||||
"qrh = QRHouseholder(A)\n",
|
|
||||||
"print(\"Q (Householder):\", qrh.Q)\n",
|
|
||||||
"print(\"R (Householder):\", qrh.R)\n",
|
|
||||||
"\n",
|
|
||||||
"# Least squares example\n",
|
"# Least squares example\n",
|
||||||
"b = Vector([1, 2, 3])\n",
|
"b = Vector([1, 2, 3])\n",
|
||||||
"x_ls = LeastSquaresSolver(A, b).solve()\n",
|
"x_ls = LeastSquaresSolver(A, b).solve()\n",
|
||||||
"print(\"Least squares solution:\", x_ls)\n"
|
"print(\"Least squares solution:\", x_ls)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -241,7 +276,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"\n",
|
"\n",
|
||||||
"## 7. Key Takeaways\n",
|
"## 6. Key Takeaways\n",
|
||||||
"\n",
|
"\n",
|
||||||
"- CGS is simple but numerically unstable.\n",
|
"- CGS is simple but numerically unstable.\n",
|
||||||
"- MGS is more stable and preferred if using Gram-Schmidt.\n",
|
"- MGS is more stable and preferred if using Gram-Schmidt.\n",
|
||||||
|
Reference in New Issue
Block a user