Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'matrix.bmk
- SuperStrict
- Type TMatrix
- Field cols:Byte
- Field rows:Byte
- Field index:Double[][]
- Method minor:Double(row:Byte, col:Byte)
- Local matrix:TMatrix = New TMatrix
- Local i:Byte
- Local j:Byte
- Assert ((cols > 0) And (rows > 0)), " Matrix is empty"
- Assert (cols = rows), " Matrix is not square one"
- matrix.cols = cols-1
- matrix.rows = rows-1
- matrix.index = New Double[][rows - 1]
- For i = 0 To rows - 2
- matrix.index[i] = New Double[cols - 1]
- For j = 0 To cols - 2
- matrix.index[i][j] = index[i+(i => row)][j+(j => col)]
- Next
- Next
- If (row+col) Mod 2 = 1
- Return matrix.determinant()
- Else
- Return -matrix.determinant()
- End If
- End Method
- Method determinant:Double()
- Local sum:Double
- Local i:Byte
- Local j:Byte
- Assert ((cols > 0) And (rows > 0)), " Matrix is empty"
- Assert (cols = rows), " Matrix is not square one"
- If cols = 1
- Return index[0][0]
- End If
- For i = 0 To cols-1
- sum :+ minor(0, i) * index[0][i]
- Next
- Return sum
- End Method
- Method transpose:TMatrix()
- Local array:Double[][] = New Double[][cols]
- Local i:Byte
- Local j:Byte
- For i = 0 To cols-1
- array[i] = New Double[rows]
- For j = 0 To rows-1
- array[i][j] = index[j][i]
- Next
- Next
- index = array
- Return Self
- End Method
- Method copy:TMatrix()
- Local matrix:TMatrix = New TMatrix
- Local i:Byte
- Local j:Byte
- Local array:Double[][]
- matrix.cols = cols
- matrix.rows = rows
- matrix.index = array
- array = New [rows]
- For i = 0 To rows-1
- array[i] = New [cols]
- For j = 0 To cols-1
- array[i][j] = index[i][j]
- Next
- Next
- Return matrix
- End Method
- Method getInverse:TMatrix()
- Local matrix:TMatrix = New TMatrix
- Local i:Byte
- Local j:Byte
- Local array:Double[][]
- Local det:Double
- Assert ((cols > 0) And (rows > 0)), " Matrix is empty"
- Assert (cols = rows), " Matrix is not square one"
- matrix.cols = cols
- matrix.rows = rows
- det = determinant()
- Assert det, "Determinant is equal zero"
- array = New Double[][rows]
- For i = 0 To rows-1
- array[i] = New Double[cols]
- For j = 0 To cols-1
- array[i][j] = minor(i,j)/det
- Next
- Next
- matrix.index = array
- matrix.transpose()
- Return matrix
- End Method
- End Type
- Local matrix:TMatrix
- Local i:Byte
- Local j:Byte
- Local a:Byte
- Local size:Byte
- Local line:String
- Local numstrs:String[]
- Local numbers:Double[]
- Local quantity:Short
- Repeat
- matrix = New TMatrix
- Repeat
- Print "Enter 'exit' to close the program"
- line = Input("Enter size of matrix (integer within 2..255) => ")
- If line = "exit"
- End
- End If
- size = Int(line)
- Until size => 2
- Print "Size of matrix = " + size
- quantity = 0
- numbers = New Double[size*size]
- matrix.cols = size
- matrix.rows = size
- Print "Enter " + (size*size) + " numbers separated by spaces:"
- Repeat
- line = Input()
- If line.find("exit") => 0
- End
- End If
- numstrs = line.Split(" ")
- a = numstrs.length
- If quantity + a > size * size
- a = size * size - quantity
- End If
- For i = 0 To a-1
- numbers[quantity+i] = Double(numstrs[i])
- Next
- quantity :+ a
- If quantity < size*size
- Print "Enter " + (size*size-quantity) + " more numbers"
- End If
- Until quantity = size*size
- matrix.index = New Double[][size]
- For i = 0 To size - 1
- matrix.index[i] = New Double[size]
- For j = 0 To size - 1
- matrix.index[i][j] = numbers[i*size+j]
- Next
- Next
- Print
- Print "--= YOUR MATRIX =--"
- For i = 0 To matrix.rows-1
- line = ""
- For j = 0 To matrix.cols-1
- line :+ Float(matrix.index[i][j]) + " "
- Next
- Print line
- Next
- Print
- Print "--= INVERSE MATRIX =-- "
- If matrix.determinant()
- matrix = matrix.getInverse()
- For i = 0 To matrix.rows-1
- line = ""
- For j = 0 To matrix.cols-1
- line :+ Float(matrix.index[i][j]) + " "
- Next
- Print line
- Next
- Else
- Print "Inverse matrix doesn't exist"
- Print "due to your matrix is degenerate"
- End If
- Print
- Forever
- End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement