Author Topic: An Invitation to Explore ... Linear Algebra?  (Read 12997 times)

0 Members and 1 Guest are viewing this topic.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
rref (using Fraction class) sample output
« Reply #60 on: April 27, 2018, 10:36:51 am »
UPDATE:  compare the following output to the output generated by my more evolved and more "elegant" elegant approach, namely gauss++.
____________________________________________________________

mwh@coyote2:[~]:
---> rref
Enter the number of rows in the matrix: 3
Enter the number of columns in the matrix: 4

Enter row 1, column 1 : 4
Enter row 1, column 2 : 3
Enter row 1, column 3 : 1
Enter row 1, column 4 : 2

Enter row 2, column 1 : 2
Enter row 2, column 2 : -1
Enter row 2, column 3 : 2
Enter row 2, column 4 : 3

Enter row 3, column 1 : 4
Enter row 3, column 2 : 2
Enter row 3, column 3 : -1
Enter row 3, column 4 : -5


Original Matrix:

_________________________________________________________

   4   3   1   2
   2   -1   2   3
   4   2   -1   -5
_________________________________________________________


Dividing row 1 by 4
_________________________________________________________

   1   3/4   1/4   1/2
   2   -1   2   3
   4   2   -1   -5
_________________________________________________________


Multiplying row 1 by -2 and adding result to row 2
_________________________________________________________

   1   3/4   1/4   1/2
   0   -5/2   3/2   2
   4   2   -1   -5
_________________________________________________________


Multiplying row 1 by -4 and adding result to row 3
_________________________________________________________

   1   3/4   1/4   1/2
   0   -5/2   3/2   2
   0   -1   -2   -7
_________________________________________________________


Dividing row 2 by -5/2
_________________________________________________________

   1   3/4   1/4   1/2
   0   1   -3/5   -4/5
   0   -1   -2   -7
_________________________________________________________


Multiplying row 2 by -3/4 and adding result to row 1
_________________________________________________________

   1   0   7/10   11/10
   0   1   -3/5   -4/5
   0   -1   -2   -7
_________________________________________________________


Multiplying row 2 by 1 and adding result to row 3
_________________________________________________________

   1   0   7/10   11/10
   0   1   -3/5   -4/5
   0   0   -13/5   -39/5
_________________________________________________________


Dividing row 3 by -13/5
_________________________________________________________

   1   0   7/10   11/10
   0   1   -3/5   -4/5
   0   0   1   3
_________________________________________________________


Multiplying row 3 by -7/10 and adding result to row 1
_________________________________________________________

   1   0   0   -1
   0   1   -3/5   -4/5
   0   0   1   3
_________________________________________________________


Multiplying row 3 by 3/5 and adding result to row 2
_________________________________________________________

   1   0   0   -1
   0   1   0   1
   0   0   1   3
_________________________________________________________

************************************************************
Row reduced echelon form:
_________________________________________________________

   1   0   0   -1
   0   1   0   1
   0   0   1   3
_________________________________________________________

************************************************************
So, we see there is a unique solution for vector x:

x1 = -1
x2 = 1
x3 = 3

An example where there are no solutions:

mwh@coyote2:[~]:
---> rref
Enter the number of rows in the matrix: 3
Enter the number of columns in the matrix: 4

Enter row 1, column 1 : 1
Enter row 1, column 2 : -3
Enter row 1, column 3 : 4
Enter row 1, column 4 : 1

Enter row 2, column 1 : 2
Enter row 2, column 2 : 13
Enter row 2, column 3 : -17
Enter row 2, column 4 : 2

Enter row 3, column 1 : -2
Enter row 3, column 2 : 6
Enter row 3, column 3 : -8
Enter row 3, column 4 : 1


Original Matrix:

_________________________________________________________

   1   -3   4   1
   2   13   -17   2
   -2   6   -8   1
_________________________________________________________


Dividing row 1 by 1
_________________________________________________________

   1   -3   4   1
   2   13   -17   2
   -2   6   -8   1
_________________________________________________________


Multiplying row 1 by -2 and adding result to row 2
_________________________________________________________

   1   -3   4   1
   0   19   -25   0
   -2   6   -8   1
_________________________________________________________


Multiplying row 1 by 2 and adding result to row 3
_________________________________________________________

   1   -3   4   1
   0   19   -25   0
   0   0   0   3
_________________________________________________________


Dividing row 2 by 19
_________________________________________________________

   1   -3   4   1
   0   1   -25/19   0
   0   0   0   3
_________________________________________________________


Multiplying row 2 by 3 and adding result to row 1
_________________________________________________________

   1   0   1/19   1
   0   1   -25/19   0
   0   0   0   3
_________________________________________________________


Multiplying row 2 by 0 and adding result to row 3
_________________________________________________________

   1   0   1/19   1
   0   1   -25/19   0
   0   0   0   3
_________________________________________________________


Dividing row 3 by 3
_________________________________________________________

   1   0   1/19   1
   0   1   -25/19   0
   0   0   0   1
_________________________________________________________


Multiplying row 3 by -1 and adding result to row 1
_________________________________________________________

   1   0   1/19   0
   0   1   -25/19   0
   0   0   0   1
_________________________________________________________


Multiplying row 3 by 0 and adding result to row 2
_________________________________________________________

   1   0   1/19   0
   0   1   -25/19   0
   0   0   0   1
_________________________________________________________

************************************************************
Row reduced echelon form:
_________________________________________________________

   1   0   1/19   0
   0   1   -25/19   0
   0   0   0   1
_________________________________________________________

************************************************************

An example where there are an infinite number of solutions (with 2 free variables).  Notice the zero rows in the row echelon form.

mwh@coyote2:[~]:
---> rref
Enter the number of rows in the matrix: 4
Enter the number of columns in the matrix: 5

Enter row 1, column 1 : 1
Enter row 1, column 2 : 2
Enter row 1, column 3 : 3
Enter row 1, column 4 : 4
Enter row 1, column 5 : 5

Enter row 2, column 1 : -1
Enter row 2, column 2 : -2
Enter row 2, column 3 : -3
Enter row 2, column 4 : -4
Enter row 2, column 5 : -5

Enter row 3, column 1 : 10
Enter row 3, column 2 : 20
Enter row 3, column 3 : 30
Enter row 3, column 4 : 40
Enter row 3, column 5 : 50

Enter row 4, column 1 : -3
Enter row 4, column 2 : -5
Enter row 4, column 3 : 32
Enter row 4, column 4 : 12
Enter row 4, column 5 : 100


Original Matrix:

_________________________________________________________

   1   2   3   4   5
   -1   -2   -3   -4   -5
   10   20   30   40   50
   -3   -5   32   12   100
_________________________________________________________


Dividing row 1 by 1
_________________________________________________________

   1   2   3   4   5
   -1   -2   -3   -4   -5
   10   20   30   40   50
   -3   -5   32   12   100
_________________________________________________________


Multiplying row 1 by 1 and adding result to row 2
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   10   20   30   40   50
   -3   -5   32   12   100
_________________________________________________________


Multiplying row 1 by -10 and adding result to row 3
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   -3   -5   32   12   100
_________________________________________________________


Multiplying row 1 by 3 and adding result to row 4
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________

Prevented division by zero
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 2 by -2 and adding result to row 1
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 2 by 0 and adding result to row 3
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 2 by -1 and adding result to row 4
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________

Prevented division by zero
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 3 by -2 and adding result to row 1
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 3 by 0 and adding result to row 2
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 3 by -1 and adding result to row 4
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Dividing row 4 by 1
_________________________________________________________

   1   2   3   4   5
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 4 by -2 and adding result to row 1
_________________________________________________________

   1   0   -79   -44   -225
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 4 by 0 and adding result to row 2
_________________________________________________________

   1   0   -79   -44   -225
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________


Multiplying row 4 by 0 and adding result to row 3
_________________________________________________________

   1   0   -79   -44   -225
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________

************************************************************
Row reduced echelon form:
_________________________________________________________

   1   0   -79   -44   -225
   0   0   0   0   0
   0   0   0   0   0
   0   1   41   24   115
_________________________________________________________

************************************************************

I have been reading the posts.  It's one of the first things I do in the morning.  Do not mistaken the absence of comments or responses as a sign that I am not interested. 

I have tragedies to report, and I am glad to read what others have to say.

I just wanted to post a few examples of the output of rref to give you an idea of the work it does.   Final analysis is left to the "user".

May our toothaches be of the minor nuisance variety and not the full blown kind that leave one at the mercy of an expensive oral surgeon who only accepts cash (cash that you already spent on food,  wires, adapters, and mysterious black boxes).
« Last Edit: May 25, 2020, 09:31:59 am by Henry the Fool »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
I am posting some output generated after rebuilding a "linear systems solver" from scratch using combination of Gaussian elimination, elimination with partial pivoting, and back substitution, not to mention merging my Fraction class with the Matrix.h / MatrixIO.h provided by Stroustrup and utilized in the 2014 edition of his textbook, Programming: Practice and Principles Using C++ (Chapter 24: Numerics).

Using Matrix with Fraction, gathering the elements of the matrix is seamless.  I am in the thick of it, but here is some output.  Later I will come back on run the matrix A and vector b from the previous test (many moons ago) when I had first haphazardly put together that program,  Contrast and compare?

Note that after building this version, my entire approach, when solving systems of linear equations has transformed, becoming focused as a laser beam, calculated like a Kata.  My previous way of going about it was barbaric in comparison to how I go about it AFTER having created the program, the output to which is shown below.

God willin' and the crick don't rise, a different future version of my current "self" may post the source code at github along with a few others from Chapter 24: Numerics.  For now, the output will have to suffice.

___________________________________________________________________________
 *** Hentrich tinkering with gauss++ ...
RREF: Hentrich tinkering with I/O:

Please enter dimension of square matrix A: 4

First enter Matrix A in form { {  }{  }{  } }: { { 2 1 -3 1 }{ 1 2 1 -1 }{ 3 5 -2 2 }{ 2 3 2 -2 } }

Now enter Vector b in this form {  }: { 10 -2 14 -4 }

 A =
{
{  2  1  -3  1  }
{  1  2  1  -1  }
{  3  5  -2  2  }
{  2  3  2  -2  }
}

 b = {  10  -2  14  -4  }

The augmented matrix [ A | b ]:

[
 {  2  1  -3  1  } | [ 10 ]
 {  1  2  1  -1  } | [ -2 ]
 {  3  5  -2  2  } | [ 14 ]
 {  2  3  2  -2  } | [ -4 ]
]

________*** press enter ***_____________________

FLOATING-POINT ("real") representation:
________________________________

A =
{
{  2  1  -3  1  }
{  1  2  1  -1  }
{  3  5  -2  2  }
{  2  3  2  -2  }
}

b = {  10  -2  14  -4  }
________________________________

Gaussian Elimination:
 ... Fill zeros under the diagonal of row 2 with multiplier: 1/2

[Gauss:]
Multiplying row 1 by -1/2 and adding result to row 2

M(1, 0) = 1 + (-1/2 * 2) = 0
M(1, 1) = 2 + (-1/2 * 1) = 3/2
M(1, 2) = 1 + (-1/2 * -3) = 5/2
M(1, 3) = -1 + (-1/2 * 1) = -3/2
b(1) = b(1) -  (1/2)*b(0) = -2 - 5 = -7

[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  3  5  -2  2  } | [ 14 ]
 {  2  3  2  -2  } | [ -4 ]
]

________*** press enter ***_____________________

row 3 with multiplier: 3/2

[Gauss:]
Multiplying row 1 by -3/2 and adding result to row 3

M(2, 0) = 3 + (-3/2 * 2) = 0
M(2, 1) = 5 + (-3/2 * 1) = 7/2
M(2, 2) = -2 + (-3/2 * -3) = 5/2
M(2, 3) = 2 + (-3/2 * 1) = 1/2
b(2) = b(2) -  (3/2)*b(0) = 14 - 15 = -1

[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  0  7/2  5/2  1/2  } | [ -1 ]
 {  2  3  2  -2  } | [ -4 ]
]

________*** press enter ***_____________________

row 4 with multiplier: 1

[Gauss:]
Multiplying row 1 by -1 and adding result to row 4

M(3, 0) = 2 + (-1 * 2) = 0
M(3, 1) = 3 + (-1 * 1) = 2
M(3, 2) = 2 + (-1 * -3) = 5
M(3, 3) = -2 + (-1 * 1) = -3
b(3) = b(3) -  (1)*b(0) = -4 - 10 = -14

[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  0  7/2  5/2  1/2  } | [ -1 ]
 {  0  2  5  -3  } | [ -14 ]
]

________*** press enter ***_____________________


 ... Fill zeros under the diagonal of row 3 with multiplier: 7/3

[Gauss:]
Multiplying row 2 by -7/3 and adding result to row 3

M(2, 1) = 7/2 + (-7/3 * 3/2) = 0
M(2, 2) = 5/2 + (-7/3 * 5/2) = -10/3
M(2, 3) = 1/2 + (-7/3 * -3/2) = 4
b(2) = b(2) -  (7/3)*b(1) = -1 - -16.3333 = 46/3

[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  0  0  -10/3  4  } | [ 46/3 ]
 {  0  2  5  -3  } | [ -14 ]
]

________*** press enter ***_____________________

row 4 with multiplier: 4/3

[Gauss:]
Multiplying row 2 by -4/3 and adding result to row 4

M(3, 1) = 2 + (-4/3 * 3/2) = 0
M(3, 2) = 5 + (-4/3 * 5/2) = 5/3
M(3, 3) = -3 + (-4/3 * -3/2) = -1
b(3) = b(3) -  (4/3)*b(1) = -14 - -9.33333 = -14/3

[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  0  0  -10/3  4  } | [ 46/3 ]
 {  0  0  5/3  -1  } | [ -14/3 ]
]

________*** press enter ***_____________________


 ... Fill zeros under the diagonal of row 4 with multiplier: -1/2

[Gauss:]
Multiplying row 3 by 1/2 and adding result to row 4

M(3, 2) = 5/3 + (1/2 * -10/3) = 0
M(3, 3) = -1 + (1/2 * 4) = 1
b(3) = b(3) -  (-1/2)*b(2) = -14/3 - -7.66667 = 3

[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  0  0  -10/3  4  } | [ 46/3 ]
 {  0  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________


[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  0  0  -10/3  4  } | [ 46/3 ]
 {  0  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________

[backsub: n = 4; Now i = 3]|--->
[Row 4; pivot value: 1]

b(3): 3;  x(3) will become [3  - (0)]/pivot = (3)/(1)
So, x(3) = (3 - (0))/(1) = 3 =?= s/m : 3

NEW x(3) = 3
Peek at x so far: {  0  0  0  3  }
[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  0  0  -10/3  4  } | [ 46/3 ]
 {  0  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________

[backsub: n = 4; Now i = 2]|--->
[Row 3; pivot value: -10/3]
4 * 3 = 12| Total Sum = 12 |
*

b(2): 46/3;  x(2) will become [46/3  - (12)]/pivot = (10/3)/(-10/3)
So, x(2) = (46/3 - (12))/(-10/3) = -1 =?= s/m : -1


scale_and_add(A[2].slice(3), -1, A[2].slice(3) ):


M(2, 3) = 4 + (-4)*(1) = 4 + -4 = 0
NEW x(2) = -1
Peek at x so far: {  0  0  -1  3  }
[
 {  2  1  -3  1  } | [ 10 ]
 {  0  3/2  5/2  -3/2  } | [ -7 ]
 {  0  0  1  0  } | [ -1 ]
 {  0  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________

[backsub: n = 4; Now i = 1]|--->
[Row 2; pivot value: 3/2]
5/2 * -1 = -5/2| Total Sum = -5/2 |
*
-3/2 * 3 = -9/2| Total Sum = -7 |
*

b(1): -7;  x(1) will become [-7  - (-7)]/pivot = (0)/(3/2)
So, x(1) = (-7 - (-7))/(3/2) = 0 =?= s/m : 0


scale_and_add(A[1].slice(2), -1, A[1].slice(2) ):


M(1, 2) = 5/2 + (-5/2)*(1) = 5/2 + -5/2 = 0
M(1, 3) = -3/2 + (3/2)*(1) = -3/2 + 3/2 = 0
NEW x(1) = 0
Peek at x so far: {  0  0  -1  3  }
[
 {  2  1  -3  1  } | [ 10 ]
 {  0  1  0  0  } | [ 0 ]
 {  0  0  1  0  } | [ -1 ]
 {  0  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________

[backsub: n = 4; Now i = 0]|--->
[Row 1; pivot value: 2]
1 * 0 = 0| Total Sum = 0 |
*
-3 * -1 = 3| Total Sum = 3 |
*
1 * 3 = 3| Total Sum = 6 |
*

b(0): 10;  x(0) will become [10  - (6)]/pivot = (4)/(2)
So, x(0) = (10 - (6))/(2) = 2 =?= s/m : 2


scale_and_add(A[0].slice(1), -1, A[0].slice(1) ):


M(0, 1) = 1 + (-1)*(1) = 1 + -1 = 0
M(0, 2) = -3 + (3)*(1) = -3 + 3 = 0
M(0, 3) = 1 + (-1)*(1) = 1 + -1 = 0
NEW x(0) = 2
Peek at x so far: {  2  0  -1  3  }
[
 {  1  0  0  0  } | [ 2 ]
 {  0  1  0  0  } | [ 0 ]
 {  0  0  1  0  } | [ -1 ]
 {  0  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________


Gaussian elimination solution is x = {  2  0  -1  3  }
A*x = {  2  0  -1  3  }


FLOATING-POINT ("real") representation:
________________________________

x = {  2  0  -1  3  }
________________________________
___________________________________________________________________________
« Last Edit: May 28, 2020, 08:36:32 pm by Henry the Fool »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
Here is the output generated by gauss++ when fed the system from .

 *** Hentrich tinkering with gauss++ ...
RREF: Hentrich tinkering with I/O:

Please enter dimension of square matrix A: 4

First enter Matrix A in form { {  }{  }{  } }: { { 1 2 3 4 }{ -1 -2 -3 -4 }{ 10 20 30 40 }{ -3 -5 32 12 } } 

Now enter Vector b in this form {  }: { 5 -5 50 100 }

 A =
{
{  1  2  3  4  }
{  -1  -2  -3  -4  }
{  10  20  30  40  }
{  -3  -5  32  12  }
}

 b = {  5  -5  50  100  }

The augmented matrix [ A | b ]:

[
 {  1  2  3  4  } | [ 5 ]
 {  -1  -2  -3  -4  } | [ -5 ]
 {  10  20  30  40  } | [ 50 ]
 {  -3  -5  32  12  } | [ 100 ]
]

________*** press enter ***_____________________

FLOATING-POINT ("real") representation:
________________________________

A =
{
{  1  2  3  4  }
{  -1  -2  -3  -4  }
{  10  20  30  40  }
{  -3  -5  32  12  }
}

b = {  5  -5  50  100  }
________________________________

Gaussian Elimination:
 ... Fill zeros under the diagonal of row 2 with multiplier: -1

[Gauss:]
Multiplying row 1 by 1 and adding result to row 2

M(1, 0) = -1 + (1 * 1) = 0
M(1, 1) = -2 + (1 * 2) = 0
M(1, 2) = -3 + (1 * 3) = 0
M(1, 3) = -4 + (1 * 4) = 0
b(1) = b(1) -  (-1)*b(0) = -5 - -5 = 0

[
 {  1  2  3  4  } | [ 5 ]
 {  0  0  0  0  } | [ 0 ]
 {  10  20  30  40  } | [ 50 ]
 {  -3  -5  32  12  } | [ 100 ]
]

________*** press enter ***_____________________

row 3 with multiplier: 10

[Gauss:]
Multiplying row 1 by -10 and adding result to row 3

M(2, 0) = 10 + (-10 * 1) = 0
M(2, 1) = 20 + (-10 * 2) = 0
M(2, 2) = 30 + (-10 * 3) = 0
M(2, 3) = 40 + (-10 * 4) = 0
b(2) = b(2) -  (10)*b(0) = 50 - 50 = 0

[
 {  1  2  3  4  } | [ 5 ]
 {  0  0  0  0  } | [ 0 ]
 {  0  0  0  0  } | [ 0 ]
 {  -3  -5  32  12  } | [ 100 ]
]

________*** press enter ***_____________________

row 4 with multiplier: -3

[Gauss:]
Multiplying row 1 by 3 and adding result to row 4

M(3, 0) = -3 + (3 * 1) = 0
M(3, 1) = -5 + (3 * 2) = 1
M(3, 2) = 32 + (3 * 3) = 41
M(3, 3) = 12 + (3 * 4) = 24
b(3) = b(3) -  (-3)*b(0) = 100 - -15 = 115

[
 {  1  2  3  4  } | [ 5 ]
 {  0  0  0  0  } | [ 0 ]
 {  0  0  0  0  } | [ 0 ]
 {  0  1  41  24  } | [ 115 ]
]

________*** press enter ***_____________________


[
 {  1  2  3  4  } | [ 5 ]
 {  -1  -2  -3  -4  } | [ -5 ]
 {  10  20  30  40  } | [ 50 ]
 {  -3  -5  32  12  } | [ 100 ]
]

________*** press enter ***_____________________


Elimination with Partial Pivot:
[Swapped row 1 with pivot row 3]


[
 {  10  20  30  40  } | [ 50 ]
 {  -1  -2  -3  -4  } | [ -5 ]
 {  1  2  3  4  } | [ 5 ]
 {  -3  -5  32  12  } | [ 100 ]
]

________*** press enter ***_____________________


[Partial Pivot:]
Multiplying row 1 by 1/10 and adding result to row 2

M(1, 0) = -1 + (1/10 * 10) = 0
M(1, 1) = -2 + (1/10 * 20) = 0
M(1, 2) = -3 + (1/10 * 30) = 0
M(1, 3) = -4 + (1/10 * 40) = 0
[STATE] multiplier: -1/10
 i = 1; j = 0
Then:

scale_and_add(A[0].slice(0),1/10, A[1].slice(0) ):

-5 - -1/10*50 = -5 - -5 = 0
[Partial Pivot:]
Multiplying row 1 by -1/10 and adding result to row 3

M(2, 0) = 1 + (-1/10 * 10) = 0
M(2, 1) = 2 + (-1/10 * 20) = 0
M(2, 2) = 3 + (-1/10 * 30) = 0
M(2, 3) = 4 + (-1/10 * 40) = 0
[STATE] multiplier: 1/10
 i = 2; j = 0
Then:

scale_and_add(A[0].slice(0),-1/10, A[2].slice(0) ):

5 - 1/10*50 = 5 - 5 = 0
[Partial Pivot:]
Multiplying row 1 by 3/10 and adding result to row 4

M(3, 0) = -3 + (3/10 * 10) = 0
M(3, 1) = -5 + (3/10 * 20) = 1
M(3, 2) = 32 + (3/10 * 30) = 41
M(3, 3) = 12 + (3/10 * 40) = 24
[STATE] multiplier: -3/10
 i = 3; j = 0
Then:

scale_and_add(A[0].slice(0),3/10, A[3].slice(0) ):

100 - -3/10*50 = 100 - -15 = 115
************ What is it ? ******************
[*** STATE ***]: [ A | b ] =

[
 {  10  20  30  40  } | [ 50 ]
 {  0  0  0  0  } | [ 0 ]
 {  0  0  0  0  } | [ 0 ]
 {  0  1  41  24  } | [ 115 ]
]

________*** press enter ***_____________________


[Swapped row 2 with pivot row 4]


[
 {  10  20  30  40  } | [ 50 ]
 {  0  1  41  24  } | [ 115 ]
 {  0  0  0  0  } | [ 0 ]
 {  0  0  0  0  } | [ 0 ]
]

________*** press enter ***_____________________


[Partial Pivot:]
Multiplying row 2 by 0 and adding result to row 3

M(2, 1) = 0 + (0 * 1) = 0
M(2, 2) = 0 + (0 * 41) = 0
M(2, 3) = 0 + (0 * 24) = 0
[STATE] multiplier: 0
 i = 2; j = 1
Then:

scale_and_add(A[1].slice(1),0, A[2].slice(1) ):

0 - 0*115 = 0 - 0 = 0
[Partial Pivot:]
Multiplying row 2 by 0 and adding result to row 4

M(3, 1) = 0 + (0 * 1) = 0
M(3, 2) = 0 + (0 * 41) = 0
M(3, 3) = 0 + (0 * 24) = 0
[STATE] multiplier: 0
 i = 3; j = 1
Then:

scale_and_add(A[1].slice(1),0, A[3].slice(1) ):

0 - 0*115 = 0 - 0 = 0
************ What is it ? ******************
[*** STATE ***]: [ A | b ] =

[
 {  10  20  30  40  } | [ 50 ]
 {  0  1  41  24  } | [ 115 ]
 {  0  0  0  0  } | [ 0 ]
 {  0  0  0  0  } | [ 0 ]
]

________*** press enter ***_____________________

Exception: can't solve pivot==0
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #63 on: May 25, 2020, 09:30:09 am »
output for input from another example ...
(base) mwh@coyote:[~/work/c++/PPP_LAB/Funtoo/ch24_work/Ex07-8]:
$ ----> gauss++
Ex 8 with help of Ex 6
 *** Hentrich tinkering with gauss++ ...
RREF: Hentrich tinkering with I/O:

Please enter dimension of square matrix A: 3

First enter Matrix A in form { {  }{  }{  } }: { { 4 3 1 }{ 2 -1 2 }{ 4 2 -1 } }

Now enter Vector b in this form {  }: { 2 3 -5 }

 A =
{
{  4  3  1  }
{  2  -1  2  }
{  4  2  -1  }
}

 b = {  2  3  -5  }

The augmented matrix [ A | b ]:

[
 {  4  3  1  } | [ 2 ]
 {  2  -1  2  } | [ 3 ]
 {  4  2  -1  } | [ -5 ]
]

________*** press enter ***_____________________

FLOATING-POINT ("real") representation:
________________________________

A =
{
{  4  3  1  }
{  2  -1  2  }
{  4  2  -1  }
}

b = {  2  3  -5  }
________________________________

Gaussian Elimination:
 ... Fill zeros under the diagonal of row 2 with multiplier: 1/2

[Gauss:]
Multiplying row 1 by -1/2 and adding result to row 2

M(1, 0) = 2 + (-1/2 * 4) = 0
M(1, 1) = -1 + (-1/2 * 3) = -5/2
M(1, 2) = 2 + (-1/2 * 1) = 3/2
b(1) = b(1) -  (1/2)*b(0) = 3 - 1 = 2

[
 {  4  3  1  } | [ 2 ]
 {  0  -5/2  3/2  } | [ 2 ]
 {  4  2  -1  } | [ -5 ]
]

________*** press enter ***_____________________

row 3 with multiplier: 1

[Gauss:]
Multiplying row 1 by -1 and adding result to row 3

M(2, 0) = 4 + (-1 * 4) = 0
M(2, 1) = 2 + (-1 * 3) = -1
M(2, 2) = -1 + (-1 * 1) = -2
b(2) = b(2) -  (1)*b(0) = -5 - 2 = -7

[
 {  4  3  1  } | [ 2 ]
 {  0  -5/2  3/2  } | [ 2 ]
 {  0  -1  -2  } | [ -7 ]
]

________*** press enter ***_____________________


 ... Fill zeros under the diagonal of row 3 with multiplier: 2/5

[Gauss:]
Multiplying row 2 by -2/5 and adding result to row 3

M(2, 1) = -1 + (-2/5 * -5/2) = 0
M(2, 2) = -2 + (-2/5 * 3/2) = -13/5
b(2) = b(2) -  (2/5)*b(1) = -7 - 0.8 = -39/5

[
 {  4  3  1  } | [ 2 ]
 {  0  -5/2  3/2  } | [ 2 ]
 {  0  0  -13/5  } | [ -39/5 ]
]

________*** press enter ***_____________________


[
 {  4  3  1  } | [ 2 ]
 {  0  -5/2  3/2  } | [ 2 ]
 {  0  0  -13/5  } | [ -39/5 ]
]

________*** press enter ***_____________________

[backsub: n = 3; Now i = 2]|--->
[Row 3; pivot value: -13/5]

b(2): -39/5;  x(2) will become [-39/5  - (0)]/pivot = (-39/5)/(-13/5)
So, x(2) = (-39/5 - (0))/(-13/5) = 3 =?= s/m : 3

NEW x(2) = 3
Peek at x so far: {  0  0  3  }
[
 {  4  3  1  } | [ 2 ]
 {  0  -5/2  3/2  } | [ 2 ]
 {  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________

[backsub: n = 3; Now i = 1]|--->
[Row 2; pivot value: -5/2]
3/2 * 3 = 9/2| Total Sum = 9/2 |
*

b(1): 2;  x(1) will become [2  - (9/2)]/pivot = (-5/2)/(-5/2)
So, x(1) = (2 - (9/2))/(-5/2) = 1 =?= s/m : 1


scale_and_add(A[1].slice(2), -1, A[1].slice(2) ):


M(1, 2) = 3/2 + (-3/2)*(1) = 3/2 + -3/2 = 0
NEW x(1) = 1
Peek at x so far: {  0  1  3  }
[
 {  4  3  1  } | [ 2 ]
 {  0  1  0  } | [ 1 ]
 {  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________

[backsub: n = 3; Now i = 0]|--->
[Row 1; pivot value: 4]
3 * 1 = 3| Total Sum = 3 |
*
1 * 3 = 3| Total Sum = 6 |
*

b(0): 2;  x(0) will become [2  - (6)]/pivot = (-4)/(4)
So, x(0) = (2 - (6))/(4) = -1 =?= s/m : -1


scale_and_add(A[0].slice(1), -1, A[0].slice(1) ):


M(0, 1) = 3 + (-3)*(1) = 3 + -3 = 0
M(0, 2) = 1 + (-1)*(1) = 1 + -1 = 0
NEW x(0) = -1
Peek at x so far: {  -1  1  3  }
[
 {  1  0  0  } | [ -1 ]
 {  0  1  0  } | [ 1 ]
 {  0  0  1  } | [ 3 ]
]

________*** press enter ***_____________________


Gaussian elimination solution is x = {  -1  1  3  }
A*x = {  -1  1  3  }


FLOATING-POINT ("real") representation:
________________________________

x = {  -1  1  3  }
________________________________

PS:  You can also input fractions such as -1/2 or 5/3, etc ... as elements directly in the input matrix or vector.
« Last Edit: May 25, 2020, 09:36:01 am by Henry the Fool »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #64 on: May 25, 2020, 09:36:06 pm »
Lastly, the example with fractions as input I promised.  it was important that I decreased tolerance from 0.001 to 0.0000001 for the fractions to sufficiently represent the number.
_________________________________________________________________

 *** Hentrich tinkering with gauss++ ...
RREF: Hentrich tinkering with I/O:

Please enter dimension of square matrix A: 2

First enter Matrix A in form { {  }{  }{  } }: { { -1/2 3/4 }{ 5/16 12 } }

Now enter Vector b in this form {  }: { 1 -1 }

 A =
{
{  -1/2  3/4  }
{  5/16  12  }
}

 b = {  1  -1  }

The augmented matrix [ A | b ]:

[
 {  -1/2  3/4  } | [ 1 ]
 {  5/16  12  } | [ -1 ]
]

________*** press enter ***_____________________

FLOATING-POINT ("real") representation:
________________________________

A =
{
{  -0.5  0.75  }
{  0.3125  12  }
}

b = {  1  -1  }
________________________________

Gaussian Elimination:
 ... Fill zeros under the diagonal of row 2 with multiplier: -5/8

[Gauss:]
Multiplying row 1 by 5/8 and adding result to row 2

M(1, 0) = 5/16 + (5/8 * -1/2) = 0
M(1, 1) = 12 + (5/8 * 3/4) = 399/32
b(1) = b(1) -  (-5/8)*b(0) = -1 - -0.625 = -3/8

[
 {  -1/2  3/4  } | [ 1 ]
 {  0  399/32  } | [ -3/8 ]
]

________*** press enter ***_____________________


[
 {  -1/2  3/4  } | [ 1 ]
 {  0  399/32  } | [ -3/8 ]
]

________*** press enter ***_____________________

[backsub: n = 2; Now i = 1]|--->
[Row 2; pivot value: 399/32]

b(1): -3/8;  x(1) will become [-3/8  - (0)]/pivot = (-3/8)/(399/32)
So, x(1) = (-3/8 - (0))/(399/32) = -4/133 =?= s/m : -4/133

NEW x(1) = -4/133
Peek at x so far: {  0  -4/133  }
[
 {  -1/2  3/4  } | [ 1 ]
 {  0  1  } | [ -4/133 ]
]

________*** press enter ***_____________________

[backsub: n = 2; Now i = 0]|--->
[Row 1; pivot value: -1/2]
3/4 * -4/133 = -3/133| Total Sum = -3/133 |
*

b(0): 1;  x(0) will become [1  - (-3/133)]/pivot = (136/133)/(-1/2)
So, x(0) = (1 - (-3/133))/(-1/2) = -272/133 =?= s/m : -272/133


scale_and_add(A[0].slice(1), -1, A[0].slice(1) ):


M(0, 1) = 3/4 + (-3/4)*(1) = 3/4 + -3/4 = 0
NEW x(0) = -272/133
Peek at x so far: {  -272/133  -4/133  }
[
 {  1  0  } | [ -272/133 ]
 {  0  1  } | [ -4/133 ]
]

________*** press enter ***_____________________


Gaussian elimination solution is x = {  -272/133  -4/133  }
A*x = {  -272/133  -4/133  }


FLOATING-POINT ("real") representation:
________________________________

x = {  -2.04511  -0.0300752  }
________________________________
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Holden

  • { ∅, { ∅ } }
  • Posts: 5086
  • Hentrichian Philosophical Pessimist
Re: An Invitation to Explore ... Linear Algebra?
« Reply #65 on: May 26, 2020, 02:21:32 pm »
I think they teach Linear Algebra in the first year of math major in college. I am still at grade 11 level.
Two more years to go at least,before I can truly appreciate your post.
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

Holden

  • { ∅, { ∅ } }
  • Posts: 5086
  • Hentrichian Philosophical Pessimist
Re: An Invitation to Explore ... Linear Algebra?
« Reply #66 on: May 26, 2020, 02:33:44 pm »
I have been watching a python for beginners video on youtube.
« Last Edit: May 26, 2020, 02:37:41 pm by Holden »
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

Ibra

  • Philosopher of the Void
  • Posts: 132
Re: An Invitation to Explore ... Linear Algebra?
« Reply #67 on: May 28, 2020, 04:30:23 am »
This old dog today learned a new trick. to compute PI. I can use math.h  "acos" function.

double pi = acos(-1)

that gives
pi = 3.1415926535897931000000000  on my machine for the first 25 digits. as you can see it is not precise. maybe floating point implementation of libc. I don't like to go the rabbit hole of IEEE floating points representation. maybe i could calculate PI digits with arbitrary precision using many algorithms available online.

the thing is, I know that theoretically. but it didn't occur to me until today to use this trick in practice. I think the tinkering style of Mr Hentrich is the way to deepen the understanding of mathematics.

I am just practicing C language, C++ is not my cup of tea. though, the data-structures story is barren in the C land. I am just making my libraries and data-structures as necessary. I try to be dependency free. today i wrote my simplistic Makefile although I did write my build script using python before but it is a hassle too.
Suffering is the only fruit of human race

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #68 on: May 28, 2020, 09:21:00 pm »
Holden and Ibra,

I mainly posted this for posterity, as well as the code using Matrix.h/MatrixIO.h and fractions.h/fractions.cpp, not simply because of the flawlessness of command line input, that is matrix as object-in-its-own-right in the iostream:

{ { 1 1 1 1 1 1 }{ 2 -2 -1 1 -1 2 }{ 1 -1 3 -1 2 -1 }{ 1 3 -1 2 -1 -3 }{ 3 -1 2 -3 3 -1 }{ 1 4 -2 4 2 -3 } }

That can even contain fractions as elements:

{ { 1/2 1 1 1/6 1 1 }{ 2 -2 -1 1 -1 2 }{ 1 -1 3 -1 2 -1 }{ 1 3 -1 2 -1 -3 }{ 3 -1 2 -3 3 -1 }{ 1 4 -2 4 2 -3/10 } }

In the future, if some web-archives archeologist compiles and runs the different code, comparing rref to gauss++, he or she would immediately witness how much more elegant, and in fact, superior, the latest version I created is to the one that I haphazardly created that uses "brute force" (rref).

I sometimes do not wish to pause to upload such things here, but I feel it makes it more real for me to share breakthoughs of this kind.

I had alerted Holden that I was investigating Stroustrup's Matrix.h in chapter 24 (Numerics) of text, and - well - one thing led to another and the project merged and snowballed with my previous creation.  I had hard-coded some of the internal functions of Matrix.h, and that helped a great deal in coding the "animation" (the changing of states with an explanation of each).

The difference between the old post (rref) = BRUTE FORCE and the latest (gauss++) is very dramatic.  In fact, following the algorithms spelled out in the code, I find myself using this method by osmosis when performing by hand (smaller, 3 by 3, matrices).

I've come up with my own notation for the "Back Substitution" stage.

It may seem grotesque to delight in such secret breakthroughs, but, you have to remember that I have been improving that fraction.h class (and fraction.cpp) since its first incarnation in 1999 during a C++ course at a community college.  So, due to sentimental value [or the organic independence I experience on a subjective level, being intimate with the nature of rational numbers] I love to use it as a test type to see if a certain class, such as Matrix, will absorb it (the Fraction type).  I made the adjustments in order for them to play well together, and it felt kind of like a "mild religious experience," that is, there is a part of me working on things that my conscious mind is merely "recording/perceiving" - The Tinkering Organism-in-Itself is up to tinkering, and it is frustrated to be attached to this living animal body who is so moody and needy - it needs a tremendous amount to "get through the day," ... the coffee, the tobacco ... not to mention the need to eat, which I daily ignore until I am dizzy.

We must be brainwashed apes.   Sorry I have been absent from the board, but I do not leave computers on 24/7.  If I keep machines running compiling and debugging, when I reach a certain point, I let them (and my eyeballs) rest.  By the time I am done, my eyes are fried, and there is no "energy" left to invest in this board.  At the moment, I am trying to force myself to document a large matrix being fed through this code.

 Tomorrow morning might be very "sacred" - that is, we might have some thunder storms just prior to my nicotine stained fingers planting some seeds in the dirt.   I tracked down a few tomato and squash and cucumber seedlings to join the garlic in a patch of dirt along the New Jersey Parkway we rent for $11 a season.   I had to drive far as all local places were hit fast and hard by the local population [Raw Primates with apetites, as are we all].

Yes, a terrifying predicament we have been born into.  I am trying to stare down the Stomach as Slave Master.  Often my intellect rebels and the stomach becomes more tame.  Each creature is really in a bind.

I try to be a good sport, but ... It is as though one must detach entirely into oneself when engaging with something that no one in your immediate "monkey sphere" has any interest in.

Quote from: Ibra
. I think the tinkering style of Mr Hentrich is the way to deepen the understanding of mathematics.

So much of the code I write mutates into "educational style" step by step explanatory phenomenon.  This not only has value while creating it, and, then again, not only for potential users who might use it, but for a future version of myself who just might happen to live through whatever crazy chaos comes ... each day ... or not.   The process itself may be rewarding to engage in as it forces brutal self-honesty while translating mathematical phenomena into structures and algorithms.

I may be some kind of dork - that is, most all I program has to do with "mathematics" --- or even science/numerics; and I am anti-smart-apps.  That is, I am a command-line fanatic.  I prophesize that it is such command-line programs which will endure far longer than specialized GUI applications software, since the "Qualitates Occultae" of the mathematical phemomena is, especially using the highly abstract phenomena created with generic (STL/modern) C++, where the code may be made to look very similar to the mathematical notation in a textbook - that is, the command line programs will have a definite elegance to the programmer reading the code.   It will be reflected in how the program handles data fed into it.

So, the breakthrough seems to be about the I/O : { {}{}{} }, but the logic used is superior, and the code far easier to read with the details hidden in the Matrix and Fraction files.

This particular "animated version" is like forcing the "Ghosts of the Thoughts" behind the algorithm to show their "incarnations in a particular numerical form."

I hope all are coming to terms with mortality and keeping the demons (of hunger and need and want) that taunt us at bay.

Peace brothers.

(Hello to Raul and Silenus as well)
 ... I am sometimes able to print what is posted and then read when I am "away from distractions," so, when I get into the proper frame of mind, I may comment, but otherwise will mostly be lurking during this transitional phase where the Scholar and the Hungry Primate argue in the morning ... The body will trump the "silly rabbit" between my ears and will be on its knees praying for plants to grow - especially the squash - it goes down easy when I have no appetite for anything.

I have to say, a pessimist who plants cucumbers is an odd animal to be.  I hope to be shocked as usual one summer morning sucking the water out of that vegetable.

Now, if I could just grow tobacco and other smokable plants - I would immediately be swooped up and dragged away, forced to live on two slices of white bread, yellow cheese and bolonga ... New York State still goes after the Natives daring to trade tobacco with heavy military-grade armed force.

How can anyone ever aspire to be a writer!  I am always silencing myself.  Aspiring to be a writer would be like aspiring to have some kind of mental illness.  :D

_____________________
As for Makefiles, I usually compile directly on the command line, such as:

g++ -g -Wall fraction.cpp ch24_ex08_animated++.cpp -o gauss++

In any Windows environment, even with MinGW64/MSYS2 gcc installed, attach the .exe to the executable:

g++ -g -Wall fraction.cpp ch24_ex08_animated++.cpp -o gauss++.exe

_____________________________________________________

  I did use a Makefile for a project depending on several libraries of some graphics library.  I really do stubbornly prefer more fundamental and useful-to-me programs with, as Ibra suggests, as few dependencies as possible.  Also, I am a very selfish programmer with little interest in being useful to others - unless, of course, those others are trying to understand and appreciate what is hiding in the code, which is its "spirit" (?) its "inner nature," I really want to say, it's Qualitas Occulta -- or just potential for elegant approximations of numerical truth - or not.

  What is the nature of the code?  I mean, it is physical matter solidifying thought processes, so - quite philosophically heavy stuff taking place - which I guess we just take for granted.  Forgive me, I am primarily a madman, and only momentarily a coherent social organism communicating with this very precious (to Holden and I) old-school message board.

I like that Matrix.h is only like 8 pages, and MatrixIO.h only two.  Small with bare minimum in order to serve the purposes, no bloat, close to the bone.

Lean and mean, I suppose.   ;)
I still have yet to add definitions for matrix multiplication to Matrix.h (something Stroustrup left out), but this is a "fun" part where one must refer to mathematics textbooks rather than programming textbooks - in order to define the operation, that is, what it means to multiply one matrix by another.  Before one can "program" that definition, one has to reflect upon it in one's own head first, preferably with the assistance of pen and scrap paper.     ;)
« Last Edit: May 29, 2020, 02:54:01 pm by Henry the Fool »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
I found a system of equations with integer coefficients which would force the program, gauss++, into a state where Classical Gaussian Elimination fails, and the command is transferred to Elimination by Partial Pivoting, which begins fresh with original matrix _A_ and vector _b_.

Since the generated output is more than 2000 character limit, I will have to split it.  I will cut off from where Partial Pivoting begins, where the original matrix is used.
___________________________________________________
PART I of output from gauss++:

PS C:\Users\mwh> gauss++
Ex 8 with help of Ex 6
 *** Hentrich tinkering with gauss++ ...
RREF: Hentrich tinkering with I/O:

Please enter dimension of square matrix A: 4

First enter Matrix A in form { {  }{  }{  } }: { { 1 2 -1 -2 }{ 0 0 4 1 }{ 2 -1 0 1 }{ 1 1 1 0 } }

Now enter Vector b in this form {  }: { 3 5 0 2 }

 A =
{
{  1  2  -1  -2  }
{  0  0  4  1  }
{  2  -1  0  1  }
{  1  1  1  0  }
}

 b = {  3  5  0  2  }

The augmented matrix [ A | b ]:

[
 {  1  2  -1  -2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  2  -1  0  1  } | [ 0 ]
 {  1  1  1  0  } | [ 2 ]
]

________*** press enter ***_____________________

FLOATING-POINT ("real") representation:
________________________________

A =
{
{  1  2  -1  -2  }
{  0  0  4  1  }
{  2  -1  0  1  }
{  1  1  1  0  }
}

b = {  3  5  0  2  }
________________________________

Gaussian Elimination:
 ... Fill zeros under the diagonal of row 1
------------------------------------

row 2 with multiplier: 0

[Gauss:]
Multiplying row 1 by 0 and adding result to row 2

M(1, 0) = 0 + (0 * 1) = 0
M(1, 1) = 0 + (0 * 2) = 0
M(1, 2) = 4 + (0 * -1) = 4
M(1, 3) = 1 + (0 * -2) = 1
b(1) = b(1) -  (0)*b(0) = 5 - 0 = 5

[
 {  1  2  -1  -2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  2  -1  0  1  } | [ 0 ]
 {  1  1  1  0  } | [ 2 ]
]

________*** press enter ***_____________________

row 3 with multiplier: 2

[Gauss:]
Multiplying row 1 by -2 and adding result to row 3

M(2, 0) = 2 + (-2 * 1) = 0
M(2, 1) = -1 + (-2 * 2) = -5
M(2, 2) = 0 + (-2 * -1) = 2
M(2, 3) = 1 + (-2 * -2) = 5
b(2) = b(2) -  (2)*b(0) = 0 - 6 = -6

[
 {  1  2  -1  -2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  0  -5  2  5  } | [ -6 ]
 {  1  1  1  0  } | [ 2 ]
]

________*** press enter ***_____________________

row 4 with multiplier: 1

[Gauss:]
Multiplying row 1 by -1 and adding result to row 4

M(3, 0) = 1 + (-1 * 1) = 0
M(3, 1) = 1 + (-1 * 2) = -1
M(3, 2) = 1 + (-1 * -1) = 2
M(3, 3) = 0 + (-1 * -2) = 2
b(3) = b(3) -  (1)*b(0) = 2 - 3 = -1

[
 {  1  2  -1  -2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  0  -5  2  5  } | [ -6 ]
 {  0  -1  2  2  } | [ -1 ]
]

________*** press enter ***_____________________
(continues in next post)
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #70 on: May 31, 2020, 10:43:50 pm »
PART II output from gauss++
__________________________________


[
 {  1  2  -1  -2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  2  -1  0  1  } | [ 0 ]
 {  1  1  1  0  } | [ 2 ]
]

________*** press enter ***_____________________


Elimination with Partial Pivot:
[Swapped row 1 with pivot row 3]


[
 {  2  -1  0  1  } | [ 0 ]
 {  0  0  4  1  } | [ 5 ]
 {  1  2  -1  -2  } | [ 3 ]
 {  1  1  1  0  } | [ 2 ]
]

________*** press enter ***_____________________


[Partial Pivot:]
Multiplying row 1 by 0 and adding result to row 2

M(1, 0) = 0 + (0 * 2) = 0
M(1, 1) = 0 + (0 * -1) = 0
M(1, 2) = 4 + (0 * 0) = 4
M(1, 3) = 1 + (0 * 1) = 1
[STATE] multiplier: 0
 i = 1; j = 0
Then:

scale_and_add(A[0].slice(0),0, A[1].slice(0) ):

5 - 0*0 = 5 - 0 = 5
[Partial Pivot:]
Multiplying row 1 by -1/2 and adding result to row 3

M(2, 0) = 1 + (-1/2 * 2) = 0
M(2, 1) = 2 + (-1/2 * -1) = 5/2
M(2, 2) = -1 + (-1/2 * 0) = -1
M(2, 3) = -2 + (-1/2 * 1) = -5/2
[STATE] multiplier: 1/2
 i = 2; j = 0
Then:

scale_and_add(A[0].slice(0),-1/2, A[2].slice(0) ):

3 - 1/2*0 = 3 - 0 = 3
[Partial Pivot:]
Multiplying row 1 by -1/2 and adding result to row 4

M(3, 0) = 1 + (-1/2 * 2) = 0
M(3, 1) = 1 + (-1/2 * -1) = 3/2
M(3, 2) = 1 + (-1/2 * 0) = 1
M(3, 3) = 0 + (-1/2 * 1) = -1/2
[STATE] multiplier: 1/2
 i = 3; j = 0
Then:

scale_and_add(A[0].slice(0),-1/2, A[3].slice(0) ):

2 - 1/2*0 = 2 - 0 = 2
************ What is it ? ******************
[*** STATE ***]: [ A | b ] =

[
 {  2  -1  0  1  } | [ 0 ]
 {  0  0  4  1  } | [ 5 ]
 {  0  5/2  -1  -5/2  } | [ 3 ]
 {  0  3/2  1  -1/2  } | [ 2 ]
]

________*** press enter ***_____________________


[Swapped row 2 with pivot row 3]


[
 {  2  -1  0  1  } | [ 0 ]
 {  0  5/2  -1  -5/2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  0  3/2  1  -1/2  } | [ 2 ]
]

________*** press enter ***_____________________


[Partial Pivot:]
Multiplying row 2 by 0 and adding result to row 3

M(2, 1) = 0 + (0 * 5/2) = 0
M(2, 2) = 4 + (0 * -1) = 4
M(2, 3) = 1 + (0 * -5/2) = 1
[STATE] multiplier: 0
 i = 2; j = 1
Then:

scale_and_add(A[1].slice(1),0, A[2].slice(1) ):

5 - 0*3 = 5 - 0 = 5
[Partial Pivot:]
Multiplying row 2 by -3/5 and adding result to row 4

M(3, 1) = 3/2 + (-3/5 * 5/2) = 0
M(3, 2) = 1 + (-3/5 * -1) = 8/5
M(3, 3) = -1/2 + (-3/5 * -5/2) = 1
[STATE] multiplier: 3/5
 i = 3; j = 1
Then:

scale_and_add(A[1].slice(1),-3/5, A[3].slice(1) ):

2 - 3/5*3 = 2 - 1.8 = 1/5
************ What is it ? ******************
[*** STATE ***]: [ A | b ] =

[
 {  2  -1  0  1  } | [ 0 ]
 {  0  5/2  -1  -5/2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  0  0  8/5  1  } | [ 1/5 ]
]

________*** press enter ***_____________________


[Partial Pivot:]
Multiplying row 3 by -2/5 and adding result to row 4

M(3, 2) = 8/5 + (-2/5 * 4) = 0
M(3, 3) = 1 + (-2/5 * 1) = 3/5
[STATE] multiplier: 2/5
 i = 3; j = 2
Then:

scale_and_add(A[2].slice(2),-2/5, A[3].slice(2) ):

1/5 - 2/5*5 = 1/5 - 2 = -9/5
************ What is it ? ******************
[*** STATE ***]: [ A | b ] =

[
 {  2  -1  0  1  } | [ 0 ]
 {  0  5/2  -1  -5/2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  0  0  0  3/5  } | [ -9/5 ]
]

________*** press enter ***_____________________


************ What is it ? ******************
[*** STATE ***]: [ A | b ] =

[
 {  2  -1  0  1  } | [ 0 ]
 {  0  5/2  -1  -5/2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  0  0  0  3/5  } | [ -9/5 ]
]

________*** press enter ***_____________________

[backsub: n = 4; Now i = 3]|--->
[Row 4; pivot value: 3/5]

b(3): -9/5;  x(3) will become [-9/5  - (0)]/pivot = (-9/5)/(3/5)
So, x(3) = (-9/5 - (0))/(3/5) = -3 =?= s/m : -3

NEW x(3) = -3
Peek at x so far: {  0  0  0  -3  }
[
 {  2  -1  0  1  } | [ 0 ]
 {  0  5/2  -1  -5/2  } | [ 3 ]
 {  0  0  4  1  } | [ 5 ]
 {  0  0  0  1  } | [ -3 ]
]

________*** press enter ***_____________________

[backsub: n = 4; Now i = 2]|--->
[Row 3; pivot value: 4]
1 * -3 = -3| Total Sum = -3 |
*

b(2): 5;  x(2) will become [5  - (-3)]/pivot = (8)/(4)
So, x(2) = (5 - (-3))/(4) = 2 =?= s/m : 2


scale_and_add(A[2].slice(3), -1, A[2].slice(3) ):


M(2, 3) = 1 + (-1)*(1) = 1 + -1 = 0
NEW x(2) = 2
Peek at x so far: {  0  0  2  -3  }
[
 {  2  -1  0  1  } | [ 0 ]
 {  0  5/2  -1  -5/2  } | [ 3 ]
 {  0  0  1  0  } | [ 2 ]
 {  0  0  0  1  } | [ -3 ]
]

________*** press enter ***_____________________

[backsub: n = 4; Now i = 1]|--->
[Row 2; pivot value: 5/2]
-1 * 2 = -2| Total Sum = -2 |
*
-5/2 * -3 = 15/2| Total Sum = 11/2 |
*

b(1): 3;  x(1) will become [3  - (11/2)]/pivot = (-5/2)/(5/2)
So, x(1) = (3 - (11/2))/(5/2) = -1 =?= s/m : -1


scale_and_add(A[1].slice(2), -1, A[1].slice(2) ):


M(1, 2) = -1 + (1)*(1) = -1 + 1 = 0
M(1, 3) = -5/2 + (5/2)*(1) = -5/2 + 5/2 = 0
NEW x(1) = -1
Peek at x so far: {  0  -1  2  -3  }
[
 {  2  -1  0  1  } | [ 0 ]
 {  0  1  0  0  } | [ -1 ]
 {  0  0  1  0  } | [ 2 ]
 {  0  0  0  1  } | [ -3 ]
]

________*** press enter ***_____________________

[backsub: n = 4; Now i = 0]|--->
[Row 1; pivot value: 2]
-1 * -1 = 1| Total Sum = 1 |
*
0 * 2 = 0| Total Sum = 1 |
*
1 * -3 = -3| Total Sum = -2 |
*

b(0): 0;  x(0) will become [0  - (-2)]/pivot = (2)/(2)
So, x(0) = (0 - (-2))/(2) = 1 =?= s/m : 1


scale_and_add(A[0].slice(1), -1, A[0].slice(1) ):


M(0, 1) = -1 + (1)*(1) = -1 + 1 = 0
M(0, 2) = 0 + (0)*(1) = 0 + 0 = 0
M(0, 3) = 1 + (-1)*(1) = 1 + -1 = 0
NEW x(0) = 1
Peek at x so far: {  1  -1  2  -3  }
[
 {  1  0  0  0  } | [ 1 ]
 {  0  1  0  0  } | [ -1 ]
 {  0  0  1  0  } | [ 2 ]
 {  0  0  0  1  } | [ -3 ]
]

________*** press enter ***_____________________


Pivot elimination solution is x = {  1  -1  2  -3  }
A*x = {  3  5  0  2  }


FLOATING-POINT ("real") representation:
________________________________

x = {  1  -1  2  -3  }
________________________________
PS C:\Users\mwh>
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
I was checking some old links in the Linear Algebra thread, and I just want to note a couple important texts which had broken links to the pdf files.  They are now on Library Genesis (for the time being).

direct download link for Calculus in 3D. Geometry, vectors, and multivariate calculus by Zbigniew H Nitecki.

In its fullest form, this structure, "the Calculus," goes by the name of real analysis.

preliminary:  draft version of Calculus Deconstructed: A Second Course in First-Year Calculus  (Draft version May 28, 2008)

quotes from the inside cover:

Often I have considered the fact that most of the difficulties which block the progress of students trying to learn analysis stem from this: that although they understand little of ordinary algebra, still they attempt this more subtle art. From this it follows not only that they remain on the fringes, but in addition they entertain strange ideas about the concept of the infinite, which they must try to use.

~ Leonhard Euler 
   Introductio in Analysin Infinitorum (1755)


Mathematicians are a kind of Frenchmen: whatever you say to them they translate into their own language and it is then something quite different.   
~ Johann Wolfgang von Goethe
   Maximen und Reflexionen

Also, a correction to the following, as this has changed:
Quote from: I
I have reached a point where, whether I create a routine with Python or C++, I can use it in Linux or Windows ... cross-platform.  Whereas Sage (CAS) does not run in Windows, one can use sympy and numpy (Python) in both Windows and Linux.

Sage(Math) now has an official Windows version.

Quote from: I
This is another reason I prefer a "desktop/notebook" computer over a smartphone or tablet.  I am into compilers and tinkering with homegrown command-line applications.

I would prefer an inexpensive 13" computer set up to boot into both Linux and Windows than to have an expensive smart phone with fancy GUI applications.

Other notes from the (exploring) Linear Algebra thread:

Holden: We are to REMEMBER/COMPREHEND what? A vast illusion? An impermanent flash in the pan? A mere congeries of phenomena,transitory,vain,non-substantial,unreal,like myself!!!

« Last Edit: October 24, 2020, 05:15:05 pm by Sticks and Stones »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~