4

So I wanted to try to rewrite my pivot routine in haskell, but I got stuck right away...

Here is my original python routine (i've commented it heavily to compensate 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])

Here is 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 b a z = 
    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 might implement the "matrix" and "vector" types, and also how they will manipulate the arrays - like replacing elements in them or rows in a matrix.

Let me know if something is not clear, and thanks!

6
  • 3
    Are you sure that Vector type is correct? Commented Nov 27, 2011 at 9:27
  • cs.auckland.ac.nz/references/haskell/haskell-intro-html/… this might be helpful Commented Nov 27, 2011 at 9:45
  • Lol - not it's not. I just gave up compiling at that point :-( Commented Nov 27, 2011 at 10:09
  • 1
    @dmitry.malikov wow that look cryptic. So. many. symbols... Commented Nov 27, 2011 at 10:11
  • @drozzy symbols I see: list comprehension syntax [ foo | x <- gen, baz], tuple syntax (foo, bar), array indexing arr ! index, guard syntax | predicate = expression. These are all fairly basic Haskell; perhaps give earlier sections of the tutorial a read, or start with LYAH Commented Nov 27, 2011 at 16:29

1 Answer 1

2

I'm not going to take the time right now to translate your code into Haskell, but I am going to critique the code you have and offer a few tips.

First of all, the type [Foo] means "list of Foo". And by "list" I mean immutable, singly-linked list. The Matrix type makes perfect sense: a Matrix is a list of "rows", where a "row" is a list of Double. However, your Vector type makes no sense. Perhaps you meant to say:

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

Back to lists in general. Lists can be indexed with the !! operator. For example

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

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

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

You can append two lists with ++

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

You can therefore append an element with list ++ [x], although this is inefficient and not recommended. In fact, indexing is rather slow as well. If you need efficient indexing and appending, use Data.Sequence instead. But as a beginner I wouldn't recommend you worry much about efficiency for now.


I'd suggest you learn a little more Haskell before trying to translate code; your Python algorithm makes use of mutation, which is generally avoided in Haskell, though certainly possible. Once you get a little more experience with Haskell types and functions, then you may be interested in the vector package.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, appreciate it! What about replacing an element at a given index in an array?
@drozzy If you start replacing the elements at particular indices in a list (not array! there's a difference), you will almost certainly regret it soon after.
@drozzy If you really want to do stuff like that, check out Data.Sequence # indexing. Seq is a purely functional data structure, which still retains pretty good performance when doing stuff like that. If you want the raw performance of mutable arrays, then use those. Not recommended until you have at least read through LYAH, and probably RWH too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.