Risoluzione sistemi e determinante (jl)




using LinearAlgebra
function minor(m,i,j)
	 m[1:end .!= i, 1:end .!= j ]
end

"Formula di Laplace"
function laplace(m,i=1)
    size(m)==(1,1) && return m[1,1]
    reduce(+,[ (-1)^(i+j)*m[i,j]*laplace(minor(m,i,j)) for j in 1:size(m)[2] ])
end

"Calcolo determinante con Gauss"
function gauss(a)
    m=copy(a)
    n=size(m)[1]
    n==1 && return m[1,1]
    res=1
    i=findfirst(x->x!=0,m[:,1])
    i≡nothing && return 0
    if i>1 
       m[[1,i],:]=m[[i,1],:]
       res *= -1
    end
    res *= m[1,1]
    m[1,:] /= m[1,1]
    for j in 2:n
    	m[j,:] -= m[j,1]*m[1,:]
    end
    res*gauss(minor(m,1,1))
end 

"Calcolo matrice inversa con Gauss"
function invertGauss(m)
    n=copy(m)
    t=Matrix{typeof(n[1,1])}(I,size(n))
    s=size(n)[1]
    display(t)
    display(n)
    for i = 1:s
        nzero=filter(findall(x->x!=0,n[:,i])) do x
            x>=i
        end
        isempty(nzero) && return false
        k=minimum(nzero)
        if k>i
            t[[i,k],:]=t[[k,i],:]
            n[[i,k],:]=n[[k,i],:]
            display(t)
            display(n)
        end
        t[i,:] /= n[i,i]
        n[i,:] /= n[i,i]
        for j = 1:s
            j==i && continue
            t[j,:] -= n[j,i]*t[i,:]
            n[j,:] -= n[j,i]*n[i,:]
                   display(t)
            display(n)
            
        end
    end
    t
end

"Risoluzione sistema con Gauss"
function solveGauss(m)
    n=copy(m)
    rows,cols=size(n)
    print("Matrice incompleta:\n")
    display(n[:,1:cols-1])
    print("\n\nTermini noti:\n")
    display(n[:,cols])
    for i = 1:rows
        k=findfirst(x->x!=0,n[i,:])
        k≡nothing && continue
        n[i,:] /= n[i,k]
        for j in 1:rows
            i==j && continue
            n[j,:]-=n[i,:]*n[j,k]
        end
    end
    n
end

"Stampa/restituisce soluzioni di un sistema"
function printsolution(n)
    rows,cols = size(n)
    sol=zeros(cols-1)
    gen=zeros(cols-1,cols-1)
    fix=[]
    for i = 1:rows
        k=findall(x->x!=0,n[i,:])
        isempty(k) && continue
        if first(k)==cols
            print("Sistema non compatibile\n")
            return false,false
        end
        append!(fix,first(k))
        print("x(",first(k),")=",n[i,cols])
        sol[first(k)]=n[i,cols]
        for j in k[2:end]
            if j<cols
                print("+",-n[i,j],"*x(",j,")")
                gen[first(k),j]=-n[i,j]
            end
        end
        print("\n")
    end
    
    for i = 1:cols-1
        i in fix && continue
        print("x(",i,")=x(",i,")\n")
        gen[i,i]=1
        sol[i]=0
    end
    gen = hcat([ x for x in eachcol(gen) if !iszero(x) ]...)
    print("\n")
    return transpose(sol), transpose(gen)
end

"Verifica soluzioni"
function checksolution(m,s,gen)
    rows, cols = size(m)
    print("Matrice incompleta A:\n")
    display(m[:,1:cols-1])
    print("\n Termini noti B:\n")
    display(m[:,cols])
    print("Soluzione particolare S:\n")
    display(s)
    print("Generatori ker(A):\n")
    isempty(gen) && (gen=zeros(cols-1)')
    display(gen)
    r0 = m[:,1:cols-1]*transpose(s)-m[:,cols]
    print("AS-B=\n")
    display(r0)
    print("Verifica ker:")
    r1=m[:,1:cols-1]*transpose(gen)
    print("AZ=\n")
    display(r1)
    return r0,r1
end