\usemodule[m-matrix]
\usemodule[art-01]

\starttext

\startluacode
document.DemoMatrixA = {
    { 0, 2,  4, -4, 1 },
    { 0, 0,  2,  3, 4 },
    { 2, 2, -6,  2, 4 },
    { 2, 0, -6,  9, 7 },
    { 2, 3,  4,  5, 6 },
    { 6, 6, -6,  6, 6 },
}

document.DemoMatrixB = {
    { 0, 2,  4, -4, 1 },
    { 0, 0,  2,  3, 4 },
    { 2, 2, -6,  3, 4 },
    { 2, 0, -6,  9, 7 },
    { 2, 2, -6,  2, 4 },
}

document.DemoMatrixC = {
    {  3, 3, -1,  3 },
    { -1, 4,  1,  3 },
    {  5, 4,  0,  2 },
    {  2, 4,  0, -1 },
}
\stopluacode

\startbuffer[demo]
\typebuffer
\startalignment[middle]
    \dontleavehmode\inlinebuffer
\stopalignment
\stopbuffer

\setuphead[section][before={\testpage[5]\blank[2*big]}]

\startsubject[title={A symbolic matrix}]

\startbuffer
\ctxmodulematrix{typeset(moduledata.matrix.symbolic("a", "m", "n"))}
$\qquad\qquad$
\ctxmodulematrix{typeset(moduledata.matrix.symbolic("a", "m", "n", 4, 8))}
\stopbuffer

\getbuffer[demo]

\stopsubject

\startsubject[title={Generate a new $m \times n$ matrix}]

\startbuffer
\startluacode
    moduledata.matrix.typeset(moduledata.matrix.makeR(4,3, 0,5))
    context.qquad()
    context("\\qquad")
    moduledata.matrix.typeset(moduledata.matrix.makeR(5,5,-1,5))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject

\startsubject[title={Swap two rows (ex: 2 and 4)}]

\startbuffer
\startluacode
    moduledata.matrix.typeset(document.DemoMatrixA)
    context("$\\qquad \\Rightarrow \\qquad$")
    moduledata.matrix.typeset(moduledata.matrix.swaprows(document.DemoMatrixA,2,4))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject

\startsubject[title={Swap two columns (ex: 1 and 3)}]

\startbuffer
\startluacode
    moduledata.matrix.typeset(document.DemoMatrixA)
    context("$\\qquad \\Rightarrow \\qquad$")
    moduledata.matrix.typeset(moduledata.matrix.swapcolumns(document.DemoMatrixA,1, 3))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject


\startsubject[title={Multiply 3 to row 2($3 \times r_2$)}]

\startbuffer
\startluacode
    moduledata.matrix.typeset(document.DemoMatrixA)
    context("$\\qquad \\Rightarrow \\qquad$")
    moduledata.matrix.typeset(moduledata.matrix.multiply(document.DemoMatrixA,2,3))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject


\startsubject[title={Add 4 times of row 3 to row 2($r_2 + 4 \times r_3$)}]

\startbuffer
\startluacode
    moduledata.matrix.typeset(document.DemoMatrixA)
    context("$\\qquad \\Rightarrow \\qquad$")
    moduledata.matrix.sumrow(document.DemoMatrixA,2,3,4)
    moduledata.matrix.typeset(document.DemoMatrixA)
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject


\startsubject[title={Transpose a matrix}]
\startbuffer
\startluacode
    moduledata.matrix.typeset(document.DemoMatrixA)
    context("$\\qquad \\Rightarrow \\qquad$")
    moduledata.matrix.typeset(moduledata.matrix.transpose(document.DemoMatrixA))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject


\startsubject[title={The inner product of two vectors}]

\startbuffer
\startluacode
    context("$<1,2,3> \\cdot <3,1,2> \\ =\\ $ ")
    context( moduledata.matrix.inner({ 1, 2, 3 }, { 3, 1, 2 }))
\stopluacode
\stopbuffer

\getbuffer[demo]

\startbuffer
\startluacode
context("$<1,2,3> \\cdot <3,1,2,4> \\ =\\ $ ")
context(moduledata.matrix.inner({ 1, 2, 3 }, { 1, 2, 3, 4 }))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject


\startsubject[title={The product of two matrices}]

\startbuffer
\startluacode
    context("$\\ $")
    moduledata.matrix.typeset(document.DemoMatrixB)
    context("$\\cdot$")
    moduledata.matrix.typeset(document.DemoMatrixA)
    context("$ = $")
    moduledata.matrix.typeset(moduledata.matrix.product
        (document.DemoMatrixB,document.DemoMatrixB))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject

\startsubject[title={An Upper Triangular Matrix}]

\startbuffer
\startluacode
    moduledata.matrix.typeset(document.DemoMatrixB)
    context("$\\qquad \\Rightarrow \\qquad$")
    moduledata.matrix.typeset(moduledata.matrix.uppertri(document.DemoMatrixB))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject


\startsubject[title={Determinant: using triangulation}]

\startbuffer
\startluacode
    local m = {
        { 1, 2,  4 },
        { 0, 0,  2 },
        { 2, 2, -6 },
        { 2, 2, -6 },
    }
    moduledata.matrix.typeset(m, {fences="bars"})
    context("$\\qquad = \\qquad$")
    context(moduledata.matrix.determinant(m))
\stopluacode
\stopbuffer
\getbuffer[demo]

\startbuffer
\startluacode
    moduledata.matrix.typeset(document.DemoMatrixC, { fences = "bars" })
    context("$\\qquad = \\qquad$")
    context(moduledata.matrix.determinant(document.DemoMatrixC))
\stopluacode
\stopbuffer
\getbuffer[demo]

\stopsubject

\startsubject[title={Determinant: using Laplace Expansion}]

\startbuffer
\startluacode
    moduledata.matrix.typeset(document.DemoMatrixC, { fences = "bars" })
    context("$\\qquad = \\qquad$")
    context(moduledata.matrix.laplace(document.DemoMatrixC))
\stopluacode
\stopbuffer
\getbuffer[demo]

\stopsubject

\startsubject[title={Example of Laplace Expansion using submatrix function}]

\startbuffer
\startluacode
    local m = {
        { 1, 5, 4, 2 },
        { 5, 2, 0, 4 },
        { 2, 2, 1, 1 },
        { 1, 0, 0, 5 },
    }
    local options = {fences = "bars"}

    moduledata.matrix.typeset(m,options)
    context("\\par $=$")
    for j = 1, #m[1] do
        local mm =  moduledata.matrix.submatrix(m, 1, j)
        local factor = (-1)^(1+j) *(m[1][j])
        context("\\ ($%d$) \\cdot ", factor)
        moduledata.matrix.typeset(mm, options)
        if j < #m[1] then
            context("\\ $+$ ")
        end
    end
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject

\startsubject[title={Row echelon form}]

\startbuffer
\startluacode
    local m = {
        { 1, 3, -2,  0, 2,  0,  0 },
        { 2, 6, -5, -2, 4, -3, -1 },
        { 0, 0,  5, 10, 0, 15,  5 },
        { 2, 6,  0,  8, 4, 18,  6 },
    }
    moduledata.matrix.typeset(m)
    context("$\\Rightarrow$")
    moduledata.matrix.typeset(moduledata.matrix.rowechelon(m,1))
\stopluacode

\stopbuffer

\getbuffer[demo]

\stopsubject


\startsubject[title={Solving linear equation}]

\startbuffer
\startluacode
    local m = {
        {  1,  3, -2,  0 },
        {  2,  0,  1,  2 },
        {  6, -5, -2,  4 },
        { -3, -1,  5, 10 },
    }

    local c = { 5, 2, 6, 8 }

    moduledata.matrix.typeset(moduledata.matrix.solve(m,c))
    context.blank()
    moduledata.matrix.typeset(moduledata.matrix.solve(m,c), { template = 6 })
    context.blank()
    moduledata.matrix.typeset(moduledata.matrix.solve(m,c), { template = "no" })
    context.blank()
    moduledata.matrix.typeset(moduledata.matrix.solve(m,c), { template = "%0.3f" })
    context.blank()
    moduledata.matrix.typeset(moduledata.matrix.solve(m,c), { template = "%0.4F" })
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject

\startsubject[title={Inverse matrix}]

\startbuffer
\startluacode
    local m = {
        { 1, 1, 1 },
        { 0, 2, 3 },
        { 3, 2, 1 },
    }
    context("$A =\\quad$")
    moduledata.matrix.typeset(m)
    context("$\\qquad A^{-1} = \\quad$")
    moduledata.matrix.typeset(moduledata.matrix.inverse(m))
    context("\\blank\\ ")
    moduledata.matrix.typeset(m)
    context("$\\cdot$")
    moduledata.matrix.typeset(moduledata.matrix.inverse(m))
    context("$ = $")
    moduledata.matrix.typeset(moduledata.matrix.product(m, moduledata.matrix.inverse(m)))
\stopluacode
\stopbuffer

\getbuffer[demo]

\stopsubject

\startsubject[title={make matrices(zero, identiry, random)}]

\startbuffer
\startluacode
    moduledata.matrix.typeset(moduledata.matrix.makeM(3, 0))
    context.qquad()
    moduledata.matrix.typeset(moduledata.matrix.makeM(3, 1))
    context.qquad()
    moduledata.matrix.typeset(moduledata.matrix.makeR(4,3))
\stopluacode
\stopbuffer
\getbuffer[demo]

\stopsubject

\startsubject[title={join rows, join columns}]

\startbuffer
\startluacode
    local mat1, mat2
    	mat1 = moduledata.matrix.makeR(3, 4)
	mat2 = moduledata.matrix.makeR(4, 3)
    
    context("Appending as columns: ")
    context.blank()
    moduledata.matrix.typeset(mat1)
    context("$\\&$")
    moduledata.matrix.typeset(mat1)
    context("\\quad $\\Rightarrow$ \\quad")
    moduledata.matrix.joinColumns(mat1, mat1)
    moduledata.matrix.typeset(mat1)
    context.blank()
    context("Appending as rows: ")
    context.blank()
    moduledata.matrix.typeset(mat2)
    context("$\\&$")
    moduledata.matrix.typeset(mat2)
    context("\\quad $\\Rightarrow$ \\quad")
    moduledata.matrix.joinRows(mat2, mat2)
    moduledata.matrix.typeset(mat2)    
\stopluacode
\stopbuffer
\getbuffer[demo]

\stopsubject

\stoptext
