mirror of
https://github.com/fverdugo/XM_40017.git
synced 2025-11-08 22:24:25 +01:00
Merge branch 'main' of github.com:fverdugo/XM_40017 into main
This commit is contained in:
commit
44520fbe1d
@ -2,53 +2,59 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "599913a3",
|
"id": "9c32b051",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# Solving Linear Equations\n",
|
"# Solving Linear Equations\n",
|
||||||
"\n",
|
"\n",
|
||||||
"## Serial Algorithm\n",
|
"## Serial Algorithm\n",
|
||||||
"First, we construct a linear equations system $Ax=b$."
|
"To demonstrate the algorithm, we will consider a simple system of linear equations $Ax = b$:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 66,
|
||||||
"id": "fe3b5b02",
|
"id": "2b369b73",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"n = 5\n",
|
"A = [1.0 4.0 5.0 8.0 1.0; \n",
|
||||||
"A = rand(-10.0:10.0, (n, n))"
|
" 2.0 -1.0 4.0 3.0 0.0; \n",
|
||||||
]
|
" 7.0 6.0 3.0 -4.0 5.0; \n",
|
||||||
},
|
" -3.0 4.0 2.0 2.0 2.0; \n",
|
||||||
{
|
" 0.0 -4.0 2.0 1.0 2.0]\n",
|
||||||
"cell_type": "code",
|
"\n",
|
||||||
"execution_count": null,
|
"b = [61.0; 24.0; 37.0; 29.0; 12.0];"
|
||||||
"id": "57c912fb",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"x = rand(-10.0:10.0, n)\n",
|
|
||||||
"b = A * x"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "4baa0681",
|
"id": "53124eb8",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"The code in the following cell converts the problem $Ax=b$ to the upper triangular equation system $Ux=y$. "
|
"The code in the following cell converts the general problem $Ax=b$ to the upper triangular equation system $Ux=y$. Note that this function assumes that the pivots are all nonzero. This function will be erroneos if any of the diagonal entries are zero!"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 67,
|
||||||
"id": "11d255e0",
|
"id": "7a7b926a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"convert_to_upper_triangular! (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 67,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"function convert_to_upper_triangular(A,b)\n",
|
"function convert_to_upper_triangular!(A,b)\n",
|
||||||
|
" n = size(A,1)\n",
|
||||||
" # Upper Triangularization: convert Ax=b to Ux=y\n",
|
" # Upper Triangularization: convert Ax=b to Ux=y\n",
|
||||||
" for k in 1:n\n",
|
" for k in 1:n\n",
|
||||||
" for j in k+1:n\n",
|
" for j in k+1:n\n",
|
||||||
@ -72,20 +78,31 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "02c47593",
|
"id": "78ef1849",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"The function in the following cell solves the upper triangular equation system using backwards substitution. "
|
"The function in the following cell solves the upper triangular equation system using backwards substitution. Note that the function alters the input values. "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 68,
|
||||||
"id": "3c19497c",
|
"id": "5a134433",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"solve_upper_triangular! (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 68,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"function solve_upper_triangular(U,y)\n",
|
"function solve_upper_triangular!(U,y)\n",
|
||||||
" n = size(U,1)\n",
|
" n = size(U,1)\n",
|
||||||
" for step in reverse(1:n)\n",
|
" for step in reverse(1:n)\n",
|
||||||
" if U[step,step] == 0\n",
|
" if U[step,step] == 0\n",
|
||||||
@ -108,18 +125,34 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 69,
|
||||||
"id": "2c24d85e",
|
"id": "b92332f7",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"5-element Vector{Float64}:\n",
|
||||||
|
" 1.0000000000000009\n",
|
||||||
|
" 1.999999999999999\n",
|
||||||
|
" 2.9999999999999964\n",
|
||||||
|
" 4.000000000000002\n",
|
||||||
|
" 5.000000000000005"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 69,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"U,y = convert_to_upper_triangular(A,b)\n",
|
"U,y = convert_to_upper_triangular!(A,b)\n",
|
||||||
"sol = solve_upper_triangular(U,y)"
|
"sol = solve_upper_triangular!(U,y)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "1c356b5a",
|
"id": "962ec4a9",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"We can test if the obtained solution is correct using `@test`:"
|
"We can test if the obtained solution is correct using `@test`:"
|
||||||
@ -127,19 +160,30 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 70,
|
||||||
"id": "5c71828d",
|
"id": "0e336c85",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"\u001b[32m\u001b[1mTest Passed\u001b[22m\u001b[39m"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 70,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"using Test\n",
|
"using Test\n",
|
||||||
"@test sol ≈ x"
|
"@test sol ≈ [1.0; 2.0; 3.0; 4.0; 5.0]"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"id": "fe3c5374",
|
"id": "28dff449",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": []
|
"source": []
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user