Advertisement
Guest User

Untitled

a guest
Jan 6th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 3.70 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}(l+i))
  23.             elseif i>l && i<=n-l
  24.                 first_num_on_pos[i] = floor(Int,(i-1)/l)*l - 1
  25.                 data[i] = zeros(Array{Float64}(l+3+(i-1)%l)) #l + 2 + (i-1)%l + 1
  26.             else
  27.                 first_num_on_pos[i] = n-(l+2)+1
  28.                 data[i] = zeros(Array{Float64}(l+2))
  29.             end
  30.         end
  31.         for line in split.(readlines(f))
  32.             c = parse(Int,line[1])
  33.             r = parse(Int,line[2])
  34.             data[c][r-first_num_on_pos[c]+1] = parse(Float64,line[3])
  35.         end
  36.         return BlockMatrix(n,l,data,first_num_on_pos)
  37.     end
  38. end
  39.  
  40. function get_element(blockmatrix::BlockMatrix,c::Int,r::Int)
  41.     checked = r-blockmatrix.first_num_on_pos[c] + 1
  42.     if (0<checked<length(blockmatrix.data[c]))
  43.         return blockmatrix.data[c][checked]
  44.     else
  45.         return 0
  46.     end
  47. end
  48.  
  49. function set_element(blockmatrix::BlockMatrix,c::Int, r::Int, el::Float64)
  50.     checked = r-blockmatrix.first_num_on_pos[c]+1
  51.     if(0<checked<length(blockmatrix.data[c]))
  52.         blockmatrix.data[c][checked] = el
  53.     end
  54. end
  55.  
  56. function createBVector(fname::String)
  57.     open(fname, "r") do f
  58.         readline(f) # nie musimy znaΔ‡ rozmiaru
  59.         V = Float64[]
  60.         for line in split.(readlines(f))
  61.             push!(V,parse(Float64,line[1]))
  62.         end
  63.         return V
  64.     end
  65. end
  66.  
  67. function gauss(matrixx::BlockMatrix, b::Vector{Float64}, pivot::Int)
  68.     n = matrixx.n
  69.     l = matrixx.l
  70.     #search for maximum in column
  71.     for i in 1:n
  72.         if pivot==1
  73.             maxEl = get_element(matrixx,i,i)
  74.             maxRow = i
  75.             for k = i+1:n
  76.                 if abs(get_element(matrixx,k,i)) > maxEl
  77.                     maxEl = get_element(matrixx,k,i)
  78.                     maxRow = k
  79.                 end
  80.             end
  81.             # swap maximum row with current row
  82.             for k in i:n
  83.                 tmp = get_element(matrixx,maxRow,k)
  84.                 set_element(matrixx,maxRow,k,get_element(matrixx,i,k))
  85.                 set_element(matrixx,i,k,temp)
  86.             end
  87.             tmp = b[i]
  88.             b[i] = b[maxRow]
  89.             b[maxRow] = tmp
  90.         end
  91.             #make all rows below this one 0 in curr column
  92.         for k in i+1:n
  93.             c = get_element(matrixx,k,i)/get_element(matrixx,i,i)
  94.             set_element(matrixx,k,i,0.0)
  95.             for j in i+1:n
  96.                 # println(length(matrixx.data[k]),' ', k+l)
  97.                 # println(j, ' ', k)
  98.                 tempij = get_element(matrixx,i,j)
  99.                 tempij = c*tempij
  100.                 tempkj = get_element(matrixx,k,j)
  101.                 tempkj = tempkj - tempij
  102.                 set_element(matrixx,k,j,tempkj)
  103.             end
  104.             b[k] = b[k] - c*b[i]
  105.         end
  106.     end
  107.     x = zeros(Array{Float64}(n))
  108.     for i in n:-1:1
  109.         x[i] = (b[i]/get_element(matrixx,i,i))
  110.         for k in i-1:-1:1
  111.             b[k] = b[k] - get_element(matrixx,k,i) * x[i]
  112.         end
  113.     end
  114.     return x
  115. end
  116.  
  117.  
  118.  
  119. A = createBMatrix("Afromsite.txt")
  120. V = createBVector("b.txt")
  121. # println(A.data)
  122. println(A.data[1])
  123. x = gauss(A,V,0)
  124. println(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement