« first day (1899 days earlier)      last day (740 days later) » 

3:00 PM
Welcome to the APL Quest! Today's quest is Solution Salvation:
> Write a dfn which solves a set of linear equations. The left argument is a vector of the values for the equations and the right argument is a matrix of the coefficients.
This is kind of a trick question; a good way to round off our first set of 10 quests.
E.g. 2 6 4 {your_solution} 3 3⍴4 1 3 2 2 2 6 3 1 should give ¯1 3 1
This represents the set
4x + 1y + 3z = −1
2x + 2y + 2z = 3
6x + 3y + 1z = 1
 
ovs
typical phase 1 solution:
+.×⍨∘(⊢(⊢+⊢-⊢+.×+.×)⍣≡⍉÷,+.×,)
 
Whoa. Care to explain?
 
@Adám why does cause a syntax error?
 
@PyGamer0 It is a dyadic operator. Did you mean which is a function?
 
ovs
The big part on the right inverts the matrix, this is taken from my answer to Calculate the inverse of a matrix
 
3:06 PM
oops lmao
thats why it caused a syntax error lol
 
ovs
then you just need the matrix-multiply the inverse of ⍵ with ⍺
 
Yeah, +.×⍣¯1⍨ is a valid solution.
⋄ 2 6 4 +.×⍣¯1⍨ 3 3⍴4 1 3 2 2 2 6 3 1
 
@Adám ¯1 3 1
 
⋄ 2 6 4 +.×⍨∘(⊢(⊢+⊢-⊢+.×+.×)⍣≡⍉÷,+.×,) 3 3⍴4 1 3 2 2 2 6 3 1
 
@Adám ¯1 3 1
 
3:11 PM
@ovs That's so cool. I had forgotten about that.
Anyone up for Gauss-Jordan elimination?
 
whats that?
 
In mathematics, Gaussian elimination, also known as row reduction, is an algorithm for solving systems of linear equations. It consists of a sequence of operations performed on the corresponding matrix of coefficients. This method can also be used to compute the rank of a matrix, the determinant of a square matrix, and the inverse of an invertible matrix. The method is named after Carl Friedrich Gauss (1777–1855) although some special cases of the method—albeit presented without proof—were known to Chinese mathematicians as early as circa 179 CE.To perform row reduction on a matrix, one uses a...
 
ovs
@Adám no ;) I thought about doing this half an hour ago but it seemed to be too much work. Maybe someone can prove me wrong
 
Well, there's an implementation in the dfns workspace:
gauss_jordan←{⎕IO ⎕ML←0 1               ⍝ Gauss-Jordan elimination.
    elim←{                              ⍝ elimination of row/col ⍺
        p←⍺+{⍵⍳⌈/⍵}|⍺↓⍵[;⍺]             ⍝ index of pivot row
        swap←⊖@⍺ p⊢⍵                    ⍝ ⍺th and pth rows exchanged
        mat←swap[⍺;⍺]÷⍨@⍺⊢swap          ⍝ col diagonal reduced to 1
        mat-(mat[;⍺]×⍺≠⍳≢⍵)∘.×mat[⍺;]   ⍝ col off-diagonals reduced to 0
    }
    (⍴⍺)⍴(0 1×⍴⍵)↓↑elim/(⌽⍳⌊/⍴⍵),⊂⍵,⍺   ⍝ elimination/ ··· 2 1 0 (⍵,⍺)
}
(edited slightly to fit, and to remove unnecessary features)
It is really quite simple too.
⋄ 2 6 4 {⎕IO←0 ⋄ (⍴⍺)⍴(0 1×⍴⍵)↓↑{p←⍺+{⍵⍳⌈/⍵}|⍺↓⍵[;⍺] ⋄ swap←⊖@⍺ p⊢⍵ ⋄ mat←swap[⍺;⍺]÷⍨@⍺⊢swap ⋄ mat-(mat[;⍺]×⍺≠⍳≢⍵)∘.×mat[⍺;]}/(⌽⍳⌊/⍴⍵),⊂⍵,⍺} 3 3⍴4 1 3 2 2 2 6 3 1
 
@Adám ¯1 3 1
 
3:19 PM
And then of course, there's the primitive: ⋄ 2 6 4 ⌹ 3 3⍴4 1 3 2 2 2 6 3 1
 
@Adám ¯1 3 1
 
So, I guess that's it? Any other comments?
The concludes your 2013 problem set, then. See you next week for It Is All Right.
 
 
3 hours later…
6:27 PM
shouldn't the equations be:
4x + 1y + 3z = 2
2x + 2y + 2z = 6
6x + 3y + 1z = 4

with the answer of -1, 3, and 1 for x, y, and z?
 

« first day (1899 days earlier)      last day (740 days later) »