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}(3*l))
- elseif i>l && i<=n-2*l
- first_num_on_pos[i] = floor(Int,(i-1)/l)*l -1
- data[i] = zeros(Array{Float64}(2+3*l)) #l + 2 + (i-1)%l + 1
- elseif n - 2*l < i <= n-l
- first_num_on_pos[i] = n-(2*l+2)+1
- data[i] = zeros(Array{Float64}(2*l+2))
- else
- first_num_on_pos[i] = n-l-1
- data[i] = zeros(Array{Float64}(2+l))
- 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.0
- end
- end
- function set_element(blockmatrix::BlockMatrix,c::Int, r::Int, el::Float64)
- checked = r-blockmatrix.first_num_on_pos[c]+1
- if(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 swap(m::BlockMatrix, i::Int, maxRow::Int, n::Int)
- for k in i:n
- tmp = get_element(m,maxRow,k)
- tmp1 = get_element(m,i,k)
- set_element(m,maxRow,k,tmp1)
- set_element(m,i,k,tmp)
- end
- end
- function set_new_value(m::BlockMatrix, i::Int, j::Int, k::Int, c::Float64)
- #A[k,j] = A[k,j] + c*A[i,j]
- temp = get_element(m,k,j)
- mul = c*get_element(m,i,j)
- temp += mul
- set_element(m,k,j,temp)
- 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 in i+1:n
- if abs(get_element(matrixx,k,i)) > maxEl
- maxEl = abs(get_element(matrixx,k,i))
- maxRow = k
- end #if
- if k%l == 0 && (1<=i%l<=l-2 || k-i >=l)
- break
- end
- end #for
- # swap maximum row with current row
- limit = matrixx.first_num_on_pos[i] + length(matrixx.data[i]) - 1
- for j in i : limit
- tmp = get_element(matrixx, i, j)
- tmp2 = get_element(matrixx, maxrow, j)
- set_element(matrixx, maxrow, j, tmp)
- set_element(matrixx, i, j, tmp2)
- end
- # swap(matrixx,i,maxRow,n) #zmieniamy kolejnosc tez w wektorze B
- temp = b[i]
- b[i] = b[maxRow]
- b[maxRow] = temp
- 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
- set_new_value(matrixx,i,j,k,c)
- end
- b[k] += c*b[i]
- if k%l == 0 && (1<=i%l<=l-2 || k-i >=l)
- break
- end
- 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
- for k in max(1,i-2*l):i-1
- b[k] = b[k] - get_element(matrixx,k,i) * x[i]
- end
- end
- return x
- end
- A = createBMatrix("A10000.txt")
- V = createBVector("b10000.txt")
- # println(A.data)
- # println(A.data[8][12])
- # x = get_element(A,8,12)
- # println(x)
- # set_element(A,8,12,2.02)
- # x = get_element(A,8,12)
- # println(x)
- tic()
- x = gauss(A,V,1)
- toc()
- println(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement