Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include("./matrixgen.jl")
- using matrixgen
- struct BlockMatrix
- n::Int #size of Main Matrix
- l::Int #size of Block Matrix
- data::Array{Array{Float64}}
- first_num_on_pos::Array{Int}
- end
- function createBMatrix(fname::String)
- open(fname,"r") do f
- line = split(readline(f))
- n = parse(Int,line[1])
- l = parse(Int,line[2])
- first_num_on_pos=Array{Int}(n)
- data=Array{Array{Float64}}(n)
- c = 0
- r = 0
- for i in 1:n
- if i<=l
- first_num_on_pos[i] = 1
- data[i] = zeros(Array{Float64}(l+i))
- elseif i>l && i<=n-l
- first_num_on_pos[i] = floor(Int,(i-1)/l)*l - 1
- data[i] = zeros(Array{Float64}(l+3+(i-1)%l)) #l + 2 + (i-1)%l + 1
- else
- first_num_on_pos[i] = n-(l+2)+1
- data[i] = zeros(Array{Float64}(l+2))
- end
- end
- for line in split.(readlines(f))
- c = parse(Int,line[1])
- r = parse(Int,line[2])
- data[c][r-first_num_on_pos[c]+1] = parse(Float64,line[3])
- end
- return BlockMatrix(n,l,data,first_num_on_pos)
- end
- end
- function get_element(blockmatrix::BlockMatrix,c::Int,r::Int)
- checked = r-blockmatrix.first_num_on_pos[c] + 1
- if (0<checked<length(blockmatrix.data[c]))
- return blockmatrix.data[c][checked]
- else
- return 0
- end
- end
- function set_element(blockmatrix::BlockMatrix,c::Int, r::Int, el::Float64)
- checked = r-blockmatrix.first_num_on_pos[c]+1
- if(0<checked<length(blockmatrix.data[c]))
- blockmatrix.data[c][checked] = el
- end
- end
- function createBVector(fname::String)
- open(fname, "r") do f
- readline(f) # nie musimy znaΔ rozmiaru
- V = Float64[]
- for line in split.(readlines(f))
- push!(V,parse(Float64,line[1]))
- end
- return V
- end
- end
- function gauss(matrixx::BlockMatrix, b::Vector{Float64}, pivot::Int)
- n = matrixx.n
- l = matrixx.l
- #search for maximum in column
- for i in 1:n
- if pivot==1
- maxEl = get_element(matrixx,i,i)
- maxRow = i
- for k = i+1:n
- if abs(get_element(matrixx,k,i)) > maxEl
- maxEl = get_element(matrixx,k,i)
- maxRow = k
- end
- end
- # swap maximum row with current row
- for k in i:n
- tmp = get_element(matrixx,maxRow,k)
- set_element(matrixx,maxRow,k,get_element(matrixx,i,k))
- set_element(matrixx,i,k,temp)
- end
- tmp = b[i]
- b[i] = b[maxRow]
- b[maxRow] = tmp
- end
- #make all rows below this one 0 in curr column
- for k in i+1:n
- c = get_element(matrixx,k,i)/get_element(matrixx,i,i)
- set_element(matrixx,k,i,0.0)
- for j in i+1:n
- # println(length(matrixx.data[k]),' ', k+l)
- # println(j, ' ', k)
- tempij = get_element(matrixx,i,j)
- tempij = c*tempij
- tempkj = get_element(matrixx,k,j)
- tempkj = tempkj - tempij
- set_element(matrixx,k,j,tempkj)
- end
- b[k] = b[k] - c*b[i]
- end
- end
- x = zeros(Array{Float64}(n))
- for i in n:-1:1
- x[i] = (b[i]/get_element(matrixx,i,i))
- for k in i-1:-1:1
- b[k] = b[k] - get_element(matrixx,k,i) * x[i]
- end
- end
- return x
- end
- A = createBMatrix("Afromsite.txt")
- V = createBVector("b.txt")
- # println(A.data)
- println(A.data[1])
- x = gauss(A,V,0)
- println(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement