Overwriting python arrays manipulation function in haskell

So, I wanted to try rewriting my routine in haskell, but stuck right away ...

Here is my original python routine (I commented this pretty much to make up for the lack of types):

def pivot(entering, leaving, B, A, Z): """ @param entering integer indicating the entering col # @param leaving integer indicating the leaving col # @param B vector/array of integers, representing the basis @param A matrix of floating point numbers @param Z vector/array of floating point numbers returns tuple: (B, A, Z) with pivoted tableau """ # Copy A M = [row for row in A] # Append Z row to the matrix M.append(Z) # Find the main row eq_no = B.index(leaving) col = entering # Go through all other rows and do Gaussian elimination on them: for i in range(len(M)): if i == eq_no: continue # Do pivot - ignore "zero_out" function for now # assume it returns a vector same size as row M[i] M[i] = zero_out(M[i], M[eq_no], col) # Reassign B's Bprime = [b for b in B] # copy B Bprime[eq_no] = entering # replace # return new B, matrix M (less 1 row), and last row of M return (Bprime, M[:len(M)-1], M[len(M)-1]) 

That's how far I got ...

 type Matrix = [ [Double] ] type Vector = [ [Vector] ] matrix = [ [1, 2, 3], [2, 3, 4]] hello:: Matrix -> Int hello m = length m pivot:: Int -> Int -> Vector -> Matrix -> Matrix -> Matrix pivot entering leaving baz = let m = a::z eq_no = elemIndex leaving b case eq_no of Just no -> no Nothing -> 1 col = entering in ???? 

I am very interested in how people can implement the "matrix" and "vector" types, as well as how they will manipulate arrays - for example, replace them with elements or rows in a matrix.

Let me know if something is unclear, and thanks!

+4
source share
1 answer

I am not going to waste time right now to translate your code into Haskell, but I am going to criticize the code that you have and offer some tips.

First of all, the type [Foo] means "list Foo". And by "list" I mean an immutable, simply connected list. The Matrix type makes perfect sense: the matrix is ​​a list of "rows", where the "row" is a Double list. However, your Vector type does not make sense. Perhaps you wanted to say:

 type Vector = [Double] type Matrix = [Vector] 

Return to the listings in general. Lists can be indexed using the operator !! . for instance

 ["foo", "bar", "baz"] !! 1 == "bar" 

You can add an element with : (pronounced "cons")

 "quux" : ["foo", "bar", "baz"] == ["quux", "foo", "bar", "baz"] 

You can add two lists with ++

 [1,2] ++ [3,4] == [1,2,3,4] 

Thus, you can add an element with list ++ [x] , although this is inefficient and not recommended. In fact, indexing is also quite slow. If you need efficient indexing and adding, instead of Data.Sequence . But as a newbie, I would not recommend you worrying about efficiency for a long time.


I would advise you to learn a little more Haskell before trying to translate the code; your Python algorithm uses a mutation that is generally avoided in Haskell, although this is certainly possible. After you get a little more experience with Haskell types and functions, you might be interested in the package.

+2
source

Source: https://habr.com/ru/post/1383280/


All Articles