Skip to content

Commit 06e3910

Browse files
Merge pull request #16 from codebygarrysingh/feature/linear-algebra-3-var-system
Updated notebook with reduced row echelon functions
2 parents 3740b30 + b202034 commit 06e3910

File tree

1 file changed

+83
-13
lines changed

1 file changed

+83
-13
lines changed

linear-algebra-ml-ipynb/Linear_systems_with_3_variables.ipynb

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
"source":[
2323
"# let's start by importing the required libraries\n",
2424
"\n",
25-
"import numpy as np\n",
26-
"import copy"
25+
"import numpy as np"
2726
],
28-
"execution_count":11,
27+
"execution_count":3,
2928
"outputs":[
3029

3130
],
@@ -58,7 +57,7 @@
5857
"print(f\"Equation Matrix: \\n {eq_arr} \\n\")\n",
5958
"print(f\"Result Matrix: \\n {res_arr} \\n\")"
6059
],
61-
"execution_count":3,
60+
"execution_count":4,
6261
"outputs":[
6362
{
6463
"name":"stdout",
@@ -168,7 +167,7 @@
168167
"\n",
169168
"print(f\"Determinant of the linear equation is: {det_arr:0.2f}\")"
170169
],
171-
"execution_count":8,
170+
"execution_count":7,
172171
"outputs":[
173172
{
174173
"name":"stdout",
@@ -196,7 +195,7 @@
196195
"\n",
197196
"print(f\"Restacked array is: {stck_eq_arr}\")\n"
198197
],
199-
"execution_count":10,
198+
"execution_count":8,
200199
"outputs":[
201200
{
202201
"name":"stdout",
@@ -229,7 +228,7 @@
229228
" cp_orig_arr[row_index] = orig_arr[row_index] * multiple_scalar\n",
230229
" return cp_orig_arr"
231230
],
232-
"execution_count":27,
231+
"execution_count":9,
233232
"outputs":[
234233

235234
],
@@ -251,7 +250,7 @@
251250
"mod_eq_arr = multiply_row(stck_eq_arr, 1, 2)\n",
252251
"print(f\"Modified array after change is: \\n {mod_eq_arr} \\n\")"
253252
],
254-
"execution_count":28,
253+
"execution_count":10,
255254
"outputs":[
256255
{
257256
"name":"stdout",
@@ -289,7 +288,7 @@
289288
" cp_orig_arr[row_index_2] = multiple_scalar * orig_arr[row_index_1] + orig_arr[row_index_2]\n",
290289
" return cp_orig_arr"
291290
],
292-
"execution_count":29,
291+
"execution_count":11,
293292
"outputs":[
294293

295294
],
@@ -311,7 +310,7 @@
311310
"mod_eq_arr = add_rows(stck_eq_arr, 2, 1, 2)\n",
312311
"print(f\"Modified array after change is: \\n {mod_eq_arr} \\n\")"
313312
],
314-
"execution_count":30,
313+
"execution_count":12,
315314
"outputs":[
316315
{
317316
"name":"stdout",
@@ -349,7 +348,7 @@
349348
" cp_orig_arr[[row_index_1,row_index_2]] = cp_orig_arr[[row_index_2,row_index_1]]\n",
350349
" return cp_orig_arr"
351350
],
352-
"execution_count":31,
351+
"execution_count":13,
353352
"outputs":[
354353

355354
],
@@ -371,7 +370,7 @@
371370
"mod_eq_arr = swap_rows(stck_eq_arr, 2, 1)\n",
372371
"print(f\"Modified array after change is: \\n {mod_eq_arr} \\n\")"
373372
],
374-
"execution_count":32,
373+
"execution_count":14,
375374
"outputs":[
376375
{
377376
"name":"stdout",
@@ -399,6 +398,77 @@
399398
}
400399
}
401400
},
401+
{
402+
"cell_type":"code",
403+
"source":[
404+
"# now to further perform reduced row echelon, let use above defined functions\n",
405+
"\n",
406+
"red_row_eq_arr = swap_rows(stck_eq_arr, 0, 2)\n",
407+
"\n",
408+
"red_row_eq_arr = add_rows(red_row_eq_arr,0,1,2)\n",
409+
"\n",
410+
"red_row_eq_arr = add_rows(red_row_eq_arr,0,2,4)\n",
411+
"\n",
412+
"red_row_eq_arr = add_rows(red_row_eq_arr,1,2,-1)\n",
413+
"\n",
414+
"red_row_eq_arr = multiply_row(red_row_eq_arr,2,-1\/12)\n",
415+
"\n",
416+
"print(f\"Modified reduced row equation is: \\n {red_row_eq_arr} \\n\")\n",
417+
"\n"
418+
],
419+
"execution_count":15,
420+
"outputs":[
421+
{
422+
"name":"stdout",
423+
"text":[
424+
"Modified reduced row equation is: \n",
425+
" [[-1. 2. -5. 17.]\n",
426+
" [ 0. 5. -7. 34.]\n",
427+
" [-0. -0. 1. -2.]] \n",
428+
"\n"
429+
],
430+
"output_type":"stream"
431+
}
432+
],
433+
"metadata":{
434+
"datalore":{
435+
"node_id":"6pj2ZoXJretXm179iDVcUX",
436+
"type":"CODE",
437+
"hide_input_from_viewers":true,
438+
"hide_output_from_viewers":true
439+
}
440+
}
441+
},
442+
{
443+
"cell_type":"code",
444+
"source":[
445+
"# now let's try and find values of x1, x2, and x3\n",
446+
"\n",
447+
"x_3 = -2 # as per above equation derivation\n",
448+
"x_2 = (red_row_eq_arr[1,3] - red_row_eq_arr[1,2] * x_3) \/ red_row_eq_arr[1,1]\n",
449+
"x_1 = (red_row_eq_arr[0,3] - red_row_eq_arr[0,2] * x_3 - red_row_eq_arr[0,1] * x_2) \/ red_row_eq_arr[0,0]\n",
450+
"\n",
451+
"print(x_1, x_2, x_3)"
452+
],
453+
"execution_count":17,
454+
"outputs":[
455+
{
456+
"name":"stdout",
457+
"text":[
458+
"1.0 4.0 -2\n"
459+
],
460+
"output_type":"stream"
461+
}
462+
],
463+
"metadata":{
464+
"datalore":{
465+
"node_id":"ZRLebFtwF9w2m4dpTAWEbS",
466+
"type":"CODE",
467+
"hide_input_from_viewers":true,
468+
"hide_output_from_viewers":true
469+
}
470+
}
471+
},
402472
{
403473
"cell_type":"code",
404474
"source":[
@@ -410,7 +480,7 @@
410480
],
411481
"metadata":{
412482
"datalore":{
413-
"node_id":"6pj2ZoXJretXm179iDVcUX",
483+
"node_id":"RgvqEXHh6ZdOBqiZNg3h73",
414484
"type":"CODE",
415485
"hide_input_from_viewers":true,
416486
"hide_output_from_viewers":true

0 commit comments

Comments
 (0)