include(`../prg/matrix.txt'):

`Apply a matrix to a vector':
func(axb,{
  smooth(<
    for(<i,1,len([x]),1>, {
      dotprod(elem([x],[i]),
      [y]),})>)}):

`Matrix multiplication':
func(mmult,{
  sub({let(a,[x]):let(b,[y]):print_
  smooth(<for(<j,1,len([a]),1>,{
  smooth(<for(<k,1,len(elem([b],1)),1>,{
  dotprod(elem([a],[j]),column([b],[k])),
  })>),})>)})}):

func(determinant,{sub({
  let(a,len([x])):
  if_[a]=1 @ one:
  if_[a]=2 @ two:
  goto_more:
  anchor_one:
    print_[x]:
    goto_done:
  anchor_two:
    print_melem([x],1,1)*melem([x],2,2) - 
          melem([x],1,2)*melem([x],2,1):
    goto_done:
  anchor_more:
    let(k,1):
    [a]:do:
      let(s,submatrix([x],1,[k])):
      let(m,determinant([s])):
      iff((int([k])%2)=0,{let(m,-1*[m])},{}):
      print_(melem([x],1,[k])*[m])+:
      let(k,[k]+1):loop:print_0:
anchor_done})}):

func(minverse,{smooth(<sub({
  let(a,[x]):
  let(j,1):
  let(k,1):
  let(deta,determinant([a])):
  len([a]):do:
    print_smooth(<sub({
    len([a]):do:
      let(s,submatrix([a],[j],[k])):
      let(m,determinant([s])):
      iff((int([j]+[k])%2)=0,{},{let(m,-1*[m])}):
      print_[m]/[deta],:
      let(j,[j]+1):loop:})>),:let(k,[k]+1):
  loop})>)}):

func(mtest,{mmult([x],minverse([x]))}):

mtest(<<1,2,-1>,<2,1,2>,<-1,2,1>>),:

mtest(<<-4,5,2>,<8,-2,1>,<3,7,0>>),:

print_`linalg.txt',: