friendly / matlib

Matrix Functions for Teaching and Learning Linear Algebra and Multivariate Statistics
http://friendly.github.io/matlib/
65 stars 16 forks source link

matrix2latex function and tracking transformation matricies #6

Closed philchalmers closed 8 years ago

philchalmers commented 8 years ago

The function in matrix2latex.R is rather obvious, so I won't talk about that one. The tracking transformation matrices business could use a bit of elaborating though.

Basically, what I did for every elementary row operation was store the associated transformation matrix T as an attribute which can be accessed at a later time. The reason was to show how these elementary operations really are just linear transformations which can all be combined with T = T_k ... T_2 T_1 to do all the operations at once; hence, solve the system by simply applying a suitable transformation. That's interesting in and of itself, but it can also be used to show how T^-1 can completely undo the row operations to return to the initial form.

I think this example shows it best, which is more or less in the example:

cbind(A,b)

                b
[1,]  2  1 -1   8
[2,] -3 -1  2 -11
[3,] -2  1  2  -3

(soln <- gaussianElimination(A, b))

     [,1] [,2] [,3] [,4]
[1,]    1    0    0    2
[2,]    0    1    0    3
[3,]    0    0    1   -1

# buildTmat(soln, TRUE)   ## prints all T matrices
(T <- buildTmat(soln))   # pulls out hidden attributes

     [,1] [,2] [,3]
[1,]    4    3   -1
[2,]   -2   -2    1
[3,]    5    4   -1

inv(T) %*% soln    # same as cbind(A,b)

     [,1] [,2] [,3] [,4]
[1,]    2    1   -1    8
[2,]   -3   -1    2  -11
[3,]   -2    1    2   -3
john-d-fox commented 8 years ago

Hi Phil,

Expressing Gaussian elimination as continued multiplication by ERO matrices offers a simple proof that elimination produces the inverse of a nonsingular matrix -- in fact, that's how I teach this material.

A question, though: I programmed gaussianElimination with a shortcut, which is that the sweep operation operates on all rows simultaneously. That's not really an ERO but rather several at once. Did you account for that? (I haven't looked at the code you contributed and likely won't have time to do so this week.)

Best,  John


John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox

From: Phil Chalmers [notifications@github.com] Sent: March 14, 2016 11:40 PM To: friendly/matlib Subject: [matlib] matrix2latex function and tracking transformation matricies (#6)

The function in matrix2latex.R is rather obvious, so I won't talk about that one. The tracking information matrices business could use a bit of elaborating though.

Basically, what I did for every elementary row operation was store the associated transformation matrix T as an attribute which can be accessed at a later time. The reason was to show how these elementary options really are just linear transformations which can all be combined with T = T_k ... T_2 T_1 to do all the operations at once; hence, solve the system by simply applying a suitable transformation. That's interesting in and of itself, but it can also be used to show how T^-1 can be used to completely undo the row operations to return to the initial form.

I think this example shows it best, which is more or less in the example:

cbind(A,b)

            b

[1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

(soln <- gaussianElimination(A, b))

 [,1] [,2] [,3] [,4]

[1,] 1 0 0 2 [2,] 0 1 0 3 [3,] 0 0 1 -1

buildTmat(soln, TRUE) ## prints all T matricies

(T <- buildTmat(soln)) # pulls out hidden attributes

 [,1] [,2] [,3]

[1,] 4 3 -1 [2,] -2 -2 1 [3,] 5 4 -1

inv(T) %*% soln # same as cbind(A,b)

 [,1] [,2] [,3] [,4]

[1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

You can view, comment on, or merge this pull request online at:   https://github.com/friendly/matlib/pull/6 Commit Summary

track transformation matricies matrix2latex function switch to internal wrapper File Changes

M R/gaussian-elimination.R (16) A R/matrix2latex.R (34) M R/rowops.R (84) Patch Links:

https://github.com/friendly/matlib/pull/6.patch https://github.com/friendly/matlib/pull/6.diff

� You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6

philchalmers commented 8 years ago

Hi John,

Yes the sweep operation is accounted for. I believe it appears just as another ELO (currently line 88 of gaussianElimination.R), so the sweep transformation silently gets included in the history as well. Cheers.

Phil

On Tue, Mar 15, 2016 at 9:29 AM, John Fox notifications@github.com wrote:

Hi Phil,

Expressing Gaussian elimination as continued multiplication by ERO matrices offers a simple proof that elimination produces the inverse of a nonsingular matrix -- in fact, that's how I teach this material.

A question, though: I programmed gaussianElimination with a shortcut, which is that the sweep operation operates on all rows simultaneously. That's not really an ERO but rather several at once. Did you account for that? (I haven't looked at the code you contributed and likely won't have time to do so this week.)

Best, John


John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox

From: Phil Chalmers [notifications@github.com] Sent: March 14, 2016 11:40 PM To: friendly/matlib Subject: [matlib] matrix2latex function and tracking transformation matricies (#6)

The function in matrix2latex.R is rather obvious, so I won't talk about that one. The tracking information matrices business could use a bit of elaborating though.

Basically, what I did for every elementary row operation was store the associated transformation matrix T as an attribute which can be accessed at a later time. The reason was to show how these elementary options really are just linear transformations which can all be combined with T = T_k ... T_2 T_1 to do all the operations at once; hence, solve the system by simply applying a suitable transformation. That's interesting in and of itself, but it can also be used to show how T^-1 can be used to completely undo the row operations to return to the initial form.

I think this example shows it best, which is more or less in the example:

cbind(A,b)

b [1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

(soln <- gaussianElimination(A, b))

[,1] [,2] [,3] [,4] [1,] 1 0 0 2 [2,] 0 1 0 3 [3,] 0 0 1 -1

buildTmat(soln, TRUE) ## prints all T matricies

(T <- buildTmat(soln)) # pulls out hidden attributes

[,1] [,2] [,3] [1,] 4 3 -1 [2,] -2 -2 1 [3,] 5 4 -1

inv(T) %*% soln # same as cbind(A,b)

[,1] [,2] [,3] [,4] [1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

You can view, comment on, or merge this pull request online at: https://github.com/friendly/matlib/pull/6 Commit Summary

track transformation matricies matrix2latex function switch to internal wrapper File Changes

M R/gaussian-elimination.R (16) A R/matrix2latex.R (34) M R/rowops.R (84) Patch Links:

https://github.com/friendly/matlib/pull/6.patch https://github.com/friendly/matlib/pull/6.diff

� You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196817515

john-d-fox commented 8 years ago

Hi Phil,

My point is that sweeping several rows simultaneously isn't an elementary row operation but the conjunction of several EROs and thus the corresponding matrix is the product of several ERO matrices.

Best,  John


John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox

From: Phil Chalmers [notifications@github.com] Sent: March 15, 2016 9:36 AM To: friendly/matlib Cc: Fox, John Subject: Re: [matlib] matrix2latex function and tracking transformation matricies (#6)

Hi John,

Yes the sweep operation is accounted for. I believe it appears just as another ELO (currently line 88 of gaussianElimination.R), so the sweep transformation silently gets included in the history as well. Cheers.

Phil

On Tue, Mar 15, 2016 at 9:29 AM, John Fox notifications@github.com wrote:

Hi Phil,

Expressing Gaussian elimination as continued multiplication by ERO matrices offers a simple proof that elimination produces the inverse of a nonsingular matrix -- in fact, that's how I teach this material.

A question, though: I programmed gaussianElimination with a shortcut, which is that the sweep operation operates on all rows simultaneously. That's not really an ERO but rather several at once. Did you account for that? (I haven't looked at the code you contributed and likely won't have time to do so this week.)

Best, John


John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox

From: Phil Chalmers [notifications@github.com] Sent: March 14, 2016 11:40 PM To: friendly/matlib Subject: [matlib] matrix2latex function and tracking transformation matricies (#6)

The function in matrix2latex.R is rather obvious, so I won't talk about that one. The tracking information matrices business could use a bit of elaborating though.

Basically, what I did for every elementary row operation was store the associated transformation matrix T as an attribute which can be accessed at a later time. The reason was to show how these elementary options really are just linear transformations which can all be combined with T = T_k ... T_2 T_1 to do all the operations at once; hence, solve the system by simply applying a suitable transformation. That's interesting in and of itself, but it can also be used to show how T^-1 can be used to completely undo the row operations to return to the initial form.

I think this example shows it best, which is more or less in the example:

cbind(A,b)

b [1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

(soln <- gaussianElimination(A, b))

[,1] [,2] [,3] [,4] [1,] 1 0 0 2 [2,] 0 1 0 3 [3,] 0 0 1 -1

buildTmat(soln, TRUE) ## prints all T matricies

(T <- buildTmat(soln)) # pulls out hidden attributes

[,1] [,2] [,3] [1,] 4 3 -1 [2,] -2 -2 1 [3,] 5 4 -1

inv(T) %*% soln # same as cbind(A,b)

[,1] [,2] [,3] [,4] [1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

You can view, comment on, or merge this pull request online at: https://github.com/friendly/matlib/pull/6 Commit Summary

track transformation matricies matrix2latex function switch to internal wrapper File Changes

M R/gaussian-elimination.R (16) A R/matrix2latex.R (34) M R/rowops.R (84) Patch Links:

https://github.com/friendly/matlib/pull/6.patch https://github.com/friendly/matlib/pull/6.diff

� You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196817515

— You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196821055

philchalmers commented 8 years ago

Hi John,

I see what you mean, thanks for clarifying. No, I don't break the sweep down into EROs, though it's certainly possible. Would you prefer if buildTmat(soln, TRUE) returned all the EROs instead of just the transformation matrices at each step when calling row___()? It would be much larger, but then that might be nicer for teaching.

Phil

On Tue, Mar 15, 2016 at 9:48 AM, John Fox notifications@github.com wrote:

Hi Phil,

My point is that sweeping several rows simultaneously isn't an elementary row operation but the conjunction of several EROs and thus the corresponding matrix is the product of several ERO matrices.

Best, John


John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox

From: Phil Chalmers [notifications@github.com] Sent: March 15, 2016 9:36 AM To: friendly/matlib Cc: Fox, John Subject: Re: [matlib] matrix2latex function and tracking transformation matricies (#6)

Hi John,

Yes the sweep operation is accounted for. I believe it appears just as another ELO (currently line 88 of gaussianElimination.R), so the sweep transformation silently gets included in the history as well. Cheers.

Phil

On Tue, Mar 15, 2016 at 9:29 AM, John Fox notifications@github.com wrote:

Hi Phil,

Expressing Gaussian elimination as continued multiplication by ERO matrices offers a simple proof that elimination produces the inverse of a nonsingular matrix -- in fact, that's how I teach this material.

A question, though: I programmed gaussianElimination with a shortcut, which is that the sweep operation operates on all rows simultaneously. That's not really an ERO but rather several at once. Did you account for that? (I haven't looked at the code you contributed and likely won't have time to do so this week.)

Best, John


John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox

From: Phil Chalmers [notifications@github.com] Sent: March 14, 2016 11:40 PM To: friendly/matlib Subject: [matlib] matrix2latex function and tracking transformation matricies (#6)

The function in matrix2latex.R is rather obvious, so I won't talk about that one. The tracking information matrices business could use a bit of elaborating though.

Basically, what I did for every elementary row operation was store the associated transformation matrix T as an attribute which can be accessed at a later time. The reason was to show how these elementary options really are just linear transformations which can all be combined with T = T_k ... T_2 T_1 to do all the operations at once; hence, solve the system by simply applying a suitable transformation. That's interesting in and of itself, but it can also be used to show how T^-1 can be used to completely undo the row operations to return to the initial form.

I think this example shows it best, which is more or less in the example:

cbind(A,b)

b [1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

(soln <- gaussianElimination(A, b))

[,1] [,2] [,3] [,4] [1,] 1 0 0 2 [2,] 0 1 0 3 [3,] 0 0 1 -1

buildTmat(soln, TRUE) ## prints all T matricies

(T <- buildTmat(soln)) # pulls out hidden attributes

[,1] [,2] [,3] [1,] 4 3 -1 [2,] -2 -2 1 [3,] 5 4 -1

inv(T) %*% soln # same as cbind(A,b)

[,1] [,2] [,3] [,4] [1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

You can view, comment on, or merge this pull request online at: https://github.com/friendly/matlib/pull/6 Commit Summary

track transformation matricies matrix2latex function switch to internal wrapper File Changes

M R/gaussian-elimination.R (16) A R/matrix2latex.R (34) M R/rowops.R (84) Patch Links:

https://github.com/friendly/matlib/pull/6.patch https://github.com/friendly/matlib/pull/6.diff

� You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196817515

— You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196821055

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196825411

john-d-fox commented 8 years ago

Hi Phil,

When I wrote gaussianElimination() I thought about breaking down the sweep operation into EROs and opted not to do it, but I didn't really have a good reason -- more laziness than anything else. Also there are different goals, illustrating the matrix algebra vs. illustrating programming, which are partly conflicting. I guess that I'd just leave it as it is but not call the transformation matrices "elementary."

Best,  John

From: Phil Chalmers [notifications@github.com] Sent: March 15, 2016 10:17 AM To: friendly/matlib Cc: Fox, John Subject: Re: [matlib] matrix2latex function and tracking transformation matricies (#6)

Hi John,

I see what you mean, thanks for clarifying. No, I don't break the sweep down into EROs, though it's certainly possible. Would you prefer if buildTmat(soln, TRUE) returned all the EROs instead of just the transformation matrices at each step when calling row___()? It would be much larger, but then that might be nicer for teaching.

Phil

On Tue, Mar 15, 2016 at 9:48 AM, John Fox notifications@github.com wrote:

Hi Phil,

My point is that sweeping several rows simultaneously isn't an elementary row operation but the conjunction of several EROs and thus the corresponding matrix is the product of several ERO matrices.

Best, John


John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox

From: Phil Chalmers [notifications@github.com] Sent: March 15, 2016 9:36 AM To: friendly/matlib Cc: Fox, John Subject: Re: [matlib] matrix2latex function and tracking transformation matricies (#6)

Hi John,

Yes the sweep operation is accounted for. I believe it appears just as another ELO (currently line 88 of gaussianElimination.R), so the sweep transformation silently gets included in the history as well. Cheers.

Phil

On Tue, Mar 15, 2016 at 9:29 AM, John Fox notifications@github.com wrote:

Hi Phil,

Expressing Gaussian elimination as continued multiplication by ERO matrices offers a simple proof that elimination produces the inverse of a nonsingular matrix -- in fact, that's how I teach this material.

A question, though: I programmed gaussianElimination with a shortcut, which is that the sweep operation operates on all rows simultaneously. That's not really an ERO but rather several at once. Did you account for that? (I haven't looked at the code you contributed and likely won't have time to do so this week.)

Best, John


John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox

From: Phil Chalmers [notifications@github.com] Sent: March 14, 2016 11:40 PM To: friendly/matlib Subject: [matlib] matrix2latex function and tracking transformation matricies (#6)

The function in matrix2latex.R is rather obvious, so I won't talk about that one. The tracking information matrices business could use a bit of elaborating though.

Basically, what I did for every elementary row operation was store the associated transformation matrix T as an attribute which can be accessed at a later time. The reason was to show how these elementary options really are just linear transformations which can all be combined with T = T_k ... T_2 T_1 to do all the operations at once; hence, solve the system by simply applying a suitable transformation. That's interesting in and of itself, but it can also be used to show how T^-1 can be used to completely undo the row operations to return to the initial form.

I think this example shows it best, which is more or less in the example:

cbind(A,b)

b [1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

(soln <- gaussianElimination(A, b))

[,1] [,2] [,3] [,4] [1,] 1 0 0 2 [2,] 0 1 0 3 [3,] 0 0 1 -1

buildTmat(soln, TRUE) ## prints all T matricies

(T <- buildTmat(soln)) # pulls out hidden attributes

[,1] [,2] [,3] [1,] 4 3 -1 [2,] -2 -2 1 [3,] 5 4 -1

inv(T) %*% soln # same as cbind(A,b)

[,1] [,2] [,3] [,4] [1,] 2 1 -1 8 [2,] -3 -1 2 -11 [3,] -2 1 2 -3

You can view, comment on, or merge this pull request online at: https://github.com/friendly/matlib/pull/6 Commit Summary

track transformation matricies matrix2latex function switch to internal wrapper File Changes

M R/gaussian-elimination.R (16) A R/matrix2latex.R (34) M R/rowops.R (84) Patch Links:

https://github.com/friendly/matlib/pull/6.patch https://github.com/friendly/matlib/pull/6.diff

� You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196817515

— You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196821055

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196825411

— You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/friendly/matlib/pull/6#issuecomment-196841355

philchalmers commented 8 years ago

Great, and it looks like I just called the operations 'transformations' and avoided the term elementary altogether.

That being said, this confusion mostly came up because rowadd() was documented as an elementary operation when it sometimes isn't (namely when mult != 1). Perhaps a note should be added there to indicate that when mult is anything but 1 then multiple EROs are actually being performed at once.

friendly commented 8 years ago

No, rowadd() IS the elementary row operation to add a multiple of one row to another. i.e.,

M[i,] <- M[i,] + mult * M[j,]

I think the confusion here arose in trying to apply the idea of recording the transformation matrix in gaussianElimination(), where, as John wrote, he used a sweep operation to do all rows at once, for a given column pivot.

philchalmers commented 8 years ago

Then I'll have to change my reply to yes, the transformation matrices do properly track the sweep operations. The kernel of the Gaussian elimination function occurs here:

if (which > i) A <- rowswap(A, i, which) # exchange rows (E3)
A <- rowmult(A, i, 1/pivot) # pivot (E1)
for (k in 1:m){
    if (k == j) next
        A <- rowadd(A, i, k, -A[k, j]) # sweep column j (E2)
}

So the sweep step is being added each time rowadd() is called for each scalar input for column j. So in the default example buildTmat(gaussianElimination(A, b), TRUE) returns 11 row operations in total (either a switch, a row multiplication, or a row multiplication added to another row), where each T matrix corresponds to these elementary steps.

john-d-fox commented 8 years ago

Hi Phil,

I just didn't remember, and recall now, that I altered the code to use Michael's ERO functions.

Sorry for the false alarm, John

-----Original Message----- From: Phil Chalmers [mailto:notifications@github.com] Sent: March 15, 2016 5:57 PM To: friendly/matlib matlib@noreply.github.com Cc: Fox, John jfox@mcmaster.ca Subject: Re: [matlib] matrix2latex function and tracking transformation matricies (#6)

Then I'll have to change my reply to yes, the transformation matrices do properly track the sweep operations. The kernel of the Gaussian elimination function occurs here:

if (which > i) A <- rowswap(A, i, which) # exchange rows (E3) A <- rowmult(A, i, 1/pivot) # pivot (E1) for (k in 1:m){ if (k == j) next A <- rowadd(A, i, k, -A[k, j]) # sweep column j (E2) }

So the sweep step is being added each time rowadd() is called for each scalar input for column j. So in the default example buildTmat(gaussianElimination(A, b), TRUE) returns 11 row operations in total (either a switch, a row multiplication, or a row multiplication added to another row), where each T matrix corresponds to these elementary steps.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/friendly/matlib/pull/6#issuecomment-197043453 https://github.com/notifications/beacon/ANcgQg77Mq_LhO37AhuBK08fD1 sxjpZ1ks5ptysSgaJpZM4HwruU.gif