Advertisement
Guest User

Untitled

a guest
Jan 7th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 4.48 KB | None | 0 0
  1. include("./matrixgen.jl")
  2. using matrixgen
  3. struct BlockMatrix
  4.     n::Int  #size of Main Matrix
  5.     l::Int  #size of Block Matrix
  6.     data::Array{Array{Float64}}
  7.     first_num_on_pos::Array{Int}
  8. end
  9.  
  10. function createBMatrix(fname::String)
  11.     open(fname,"r") do f
  12.         line = split(readline(f))
  13.         n = parse(Int,line[1])
  14.         l = parse(Int,line[2])
  15.         first_num_on_pos=Array{Int}(n)
  16.         data=Array{Array{Float64}}(n)
  17.         c = 0
  18.         r = 0
  19.         for i in 1:n
  20.             if i<=l
  21.                 first_num_on_pos[i] = 1
  22.                 data[i] = zeros(Array{Float64}(3*l))
  23.             elseif i>l && i<=n-2*l
  24.                 first_num_on_pos[i] = floor(Int,(i-1)/l)*l -1
  25.                 data[i] = zeros(Array{Float64}(2+3*l)) #l + 2 + (i-1)%l + 1
  26.             elseif n - 2*l < i <= n-l
  27.                 first_num_on_pos[i] = n-(2*l+2)+1
  28.                 data[i] = zeros(Array{Float64}(2*l+2))
  29.             else
  30.                 first_num_on_pos[i] = n-l-1
  31.                 data[i] = zeros(Array{Float64}(2+l))
  32.             end
  33.         end
  34.         for line in split.(readlines(f))
  35.             c = parse(Int,line[1])
  36.             r = parse(Int,line[2])
  37.             data[c][r-first_num_on_pos[c]+1] = parse(Float64,line[3])
  38.         end
  39.         return BlockMatrix(n,l,data,first_num_on_pos)
  40.     end
  41. end
  42.  
  43. function get_element(blockmatrix::BlockMatrix,c::Int,r::Int)
  44.     checked = r-blockmatrix.first_num_on_pos[c] + 1
  45.     if (0<checked<=length(blockmatrix.data[c]))
  46.         return blockmatrix.data[c][checked]
  47.     else
  48.         return 0.0
  49.     end
  50. end
  51.  
  52. function set_element(blockmatrix::BlockMatrix,c::Int, r::Int, el::Float64)
  53.     checked = r-blockmatrix.first_num_on_pos[c]+1
  54.     if(checked<=length(blockmatrix.data[c]))
  55.         blockmatrix.data[c][checked] = el
  56.     end
  57. end
  58.  
  59. function createBVector(fname::String)
  60.     open(fname, "r") do f
  61.         readline(f) # nie musimy znaΔ‡ rozmiaru
  62.         V = Float64[]
  63.         for line in split.(readlines(f))
  64.             push!(V,parse(Float64,line[1]))
  65.         end
  66.         return V
  67.     end
  68. end
  69.  
  70. function swap(m::BlockMatrix, i::Int, maxRow::Int, n::Int)
  71.     for k in i:n
  72.         tmp = get_element(m,maxRow,k)
  73.         tmp1 = get_element(m,i,k)
  74.         set_element(m,maxRow,k,tmp1)
  75.         set_element(m,i,k,tmp)
  76.     end
  77. end
  78.  
  79. function set_new_value(m::BlockMatrix, i::Int, j::Int, k::Int, c::Float64)
  80.     #A[k,j] = A[k,j] + c*A[i,j]
  81.     temp = get_element(m,k,j)
  82.     mul = c*get_element(m,i,j)
  83.     temp += mul
  84.     set_element(m,k,j,temp)
  85. end
  86.  
  87. function gauss(matrixx::BlockMatrix, b::Vector{Float64}, pivot::Int)
  88.     n = matrixx.n
  89.     l = matrixx.l
  90.     #search for maximum in column
  91.     for i in 1:n
  92.         if pivot==1
  93.             maxEl = get_element(matrixx,i,i)
  94.             maxRow = i
  95.             for k in i+1:n
  96.                 if abs(get_element(matrixx,k,i)) > maxEl
  97.                     maxEl = abs(get_element(matrixx,k,i))
  98.                     maxRow = k
  99.                 end #if
  100.                 if k%l == 0 && (1<=i%l<=l-2 || k-i >=l)
  101.                     break
  102.                 end
  103.             end #for
  104.             # swap maximum row with current row
  105.             limit = matrixx.first_num_on_pos[i] + length(matrixx.data[i]) - 1
  106.             for j in i : limit
  107.                 tmp = get_element(matrixx, i, j)
  108.                 tmp2 = get_element(matrixx, maxrow, j)
  109.                 set_element(matrixx, maxrow, j, tmp)
  110.                 set_element(matrixx, i, j, tmp2)
  111.             end
  112.             # swap(matrixx,i,maxRow,n) #zmieniamy kolejnosc tez w wektorze B
  113.             temp = b[i]
  114.             b[i] = b[maxRow]
  115.             b[maxRow] = temp
  116.         end
  117.             #make all rows below this one 0 in curr column
  118.         for k in i+1:n
  119.             c = -get_element(matrixx,k,i)/get_element(matrixx,i,i)
  120.             set_element(matrixx,k,i,0.0)
  121.             for j in i+1:n
  122.                 set_new_value(matrixx,i,j,k,c)
  123.             end
  124.             b[k] += c*b[i]
  125.             if k%l == 0 && (1<=i%l<=l-2 || k-i >=l)
  126.                 break
  127.             end
  128.         end
  129.     end
  130.     x = zeros(Array{Float64}(n))
  131.     for i in n:-1:1
  132.         x[i] = (b[i]/get_element(matrixx,i,i))
  133.         # for k in i-1:-1:1
  134.         for k in max(1,i-2*l):i-1
  135.             b[k] = b[k] - get_element(matrixx,k,i) * x[i]
  136.         end
  137.     end
  138.     return x
  139. end
  140.  
  141.  
  142. A = createBMatrix("A10000.txt")
  143. V = createBVector("b10000.txt")
  144. # println(A.data)
  145. # println(A.data[8][12])
  146. # x = get_element(A,8,12)
  147. # println(x)
  148. # set_element(A,8,12,2.02)
  149. # x = get_element(A,8,12)
  150. # println(x)
  151. tic()
  152. x = gauss(A,V,1)
  153. toc()
  154. println(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement