Fork me on GitHub
#uncomplicate
<
2017-07-01
>
rrottier06:07:45

I tried to post a question on https://groups.google.com/forum/#!forum/uncomplicate but it did not seem to go through. So I will ask it here. I have been following along with the linear algebra refresher posts - but also tried to use neantherthal to solve the systems of linear equations (using `sv!`). And the answers were different from those in the book. I found that by changing the order of the rows I could get the right answer. I first thought this was because of the underlying lapack implementation but when I used python numpy I always got the same answer as the book independent of the order of the rows.

rrottier06:07:00

Here are two examples:

rrottier06:07:28

``````(let [cs (dge 3 1 [16 8 0])]
(sv! (dge 3 3 [0 4 1
4 0 1
1 1 -1])
cs)
cs)
``````

rrottier06:07:55

``````#RealGEMatrix[double, mxn:3x1, order:column, offset:0, ld:3]

1.00
3.00
4.00
``````

rrottier06:07:21

``````(let [cs (dge 3 1 [0 8 16])]
(sv! (dge 3 3 [1 1 -1
4 0 1
0 4 1])
cs)
cs)
``````

rrottier06:07:39

``````#RealGEMatrix[double, mxn:3x1, order:column, offset:0, ld:3]

-9.33
2.33
4.33
``````

rrottier06:07:07

I guess my question is: “Why am I getting different answers?”

blueberry08:07:07

@rrottier You swapped the variables instead of the equations in your second example, because you forgot that the default matrix layout is column-oriented. Here's what you intended to do (the answer is also what you'd expect):

blueberry08:07:06

``````(let [cs (dge 3 1 [0 16 8])]
(sv! (dge 3 3 [1 0 4
1 4 0
-1 1 1])
cs)
cs)
``````

blueberry08:07:59

blueberry08:07:23

``````#RealGEMatrix[double, mxn:3x1, order:column, offset:0, ld:3]

1.00
3.00
4.00
``````

blueberry08:07:55

Now, maybe it is inconvenient to you to work with this "rotated" perspective. No problem, just add the option `{:order :row}` to `dge` and the matrix will be row-oriented:

blueberry08:07:31

``````(let [cs (dge 3 1 [0 8 16] {:order :row})]
(sv! (dge 3 3 [1 1 -1
4 0 1
0 4 1]
{:order :row})
cs)
cs)

=> #RealGEMatrix[double, mxn:3x1, order:row, offset:0, ld:1]

1.00
3.00
4.00
``````

rrottier08:07:40

@blueberry Thanks. I did not realise the default matrix layout is column oriented. Thanks for pointing that out.

rrottier08:07:24

Is this a property of the underlying lapack or is it a neantherthal design decision?

blueberry08:07:26

@rrottier It's like the parentheses - after a few hours, you will not notice it :) BTW. matrix string prints out that information, and if I remember well, I mention it quite a few times in the tutorials. Please do this kind of feedback, and, if possible, suggest how to make it more obvious.

rrottier08:07:23

PS. Do I really need to specify `{:order :row}` on `(dge 3 1 ...)`? It seems like as it is only one column the orientation should be obvious.

rrottier08:07:47

Yeah `#RealGEMatrix[double, mxn:3x1, order:column, offset:0, ld:3]` I did not really parse the type signature, was too focussed on the values 😊

blueberry08:07:50

Both LAPACK and neanderthal handle both, but column is the default in literature. In lapack, column is the only option for some operations, but neanderthal finds a way to provide roe even in most of these cases.

rrottier08:07:37

“column is the default in literature” I thought that row is the default in literature.

blueberry09:07:24

You need it even for 3x1 because the LAPACK solver requires matching layout for both matrices.

rrottier09:07:45

At least the literature that i read for systems of linear equations always represent the `2x + 3y + 4z` as `[2 3 4]`

blueberry09:07:18

but it is one vector :) that does not have order

blueberry09:07:49

order is a computing detail. your books are math textbooks.

rrottier09:07:18

Ah… that is very true - I am only now stumbling into the computing stuff.

blueberry09:07:34

the first rule of numerical computing is: math book is not enough :)

rrottier09:07:14

I thought functional programming’s promise is: “You can write it just like the math”

blueberry09:07:22

if you ignore that, you'll be heavily bitten by floating point arythmetic.

blueberry09:07:21

I do not know about that promise, but It is not possible to fullfil

blueberry09:07:23

You can write it whatever you want, provided that you do not need to compute it :)

rrottier09:07:38

Numpy seems to do some transparent casting then…

rrottier09:07:57

Because it behaves as row oriented.

rrottier09:07:21

sorry I should have said “non-transparent”

blueberry09:07:00

Neanderthal also supports rows and columns. It's just about the default order, and does not have anything to do with floating point precision. In computing, a*b is not always equal to b*a.

rrottier09:07:44

I think I just stumbled into the column orientation with the `mm` function as well. I tried to mutiply: `(dge 2 2 [1 2 3 4])` with `(dge 2 1 [1 2])` and got a dimension error. Does that make sense?

blueberry09:07:32

I could also implement such castings, but that is something i want to avoid since it can shoot you in the foot when you need performance.

rrottier09:07:05

Sorry - just edited it. Got it the wrong way round

blueberry09:07:51

that does not ave anything to do with order. 2x1 * 2x2 is not allowed by math rules

blueberry09:07:27

btw. neanderthal supports mixing orders in most operations. just not in solvers.

rrottier09:07:29

RE: The castings - the way you do it is probably better, I agree that you want to be transparent to the underlying implementation. It is just confusing coming form another abstraction

rrottier09:07:04

I was trying to do this in neantherthal.

blueberry09:07:30

post the code an i'll tell you where you made a mistake

rrottier09:07:53

``````(let [a (dge 2 2 [1 2 3 4])
b (dge 1 2 [2 5])]
(mm a b))
``````

blueberry09:07:31

you are trying to multiply 2x2 and 1x2

blueberry09:07:51

should be 2 1, not 2 w

rrottier09:07:05

Sorry same mistake as previous

rrottier09:07:25

``````(let [a (dge 2 2 [1 2 3 4])
b (dge 2 1 [2 5])]
(mm a b))
``````

blueberry09:07:32

column orientation does not require you to rotate the whole world :)

rrottier09:07:37

Now it works but gives the wrong answer

rrottier09:07:07

``````[17
24]
``````

blueberry09:07:08

but if the matrix is column oriented, it transfers the 1d vector [1 2 3 4] into columns.

blueberry09:07:38

it should be [1 3 2 4]

rrottier09:07:06

blueberry09:07:34

look, the only thing you need is this:

rrottier09:07:46

Ok, I think I understand where all my issues are coming from… I just need to change the way I think about it.

blueberry09:07:02

matrix is 2d, vector is 1d

blueberry09:07:28

you wand to create a matrix by supplying the data in a vector

blueberry09:07:09

read my latest blog post. i wrote the whole blog post about these issues

rrottier09:07:56

rrottier09:07:39

Did not understand the implications of the column orientation though.

blueberry09:07:38

you probably do not need to change the way you think about it, you just need to be aware of a few etails.

blueberry09:07:38

the use matrices efficiently one?

rrottier09:07:54

But I get the reasons - in haskell I started playing with `dense` which uses the same kind of abstraction.

rrottier09:07:59

Thanks for the help.

blueberry09:07:55

What I want, is to lay AA in memory in a way that is efficient, but still easy to work with. Obviously, those two dimensions have to be projected into one. But how? Should it be [1112|2313|1−1−2−6][1112|2313|1−1−2−6], or [121|13−1|11−2|23−6][121|13−1|11−2|23−6]? The answer is: it depends on how your algorithm accesses it most often. Neanderthal gives you both options. When you create any kind of matrix, you can specify whether you want it to be column-oriented (:column, which is the default), or row-oriented (:row). In the following example, we will use CPU matrices from the native namespace. The same options also work for functions that create GPU CUDA matrices (cuda namespace), or OpenCL's GPU and CPU matrices (opencl namespace).

blueberry09:07:03

and after that...

blueberry09:07:36

Clojure sequence that we used as data source has been read column-by-column.

rrottier09:07:39

“which is the default” is what I missed.

blueberry09:07:19

But this conversation gives me material for a nice blog post, so thank you.

rrottier09:07:16

“No worries” as they say in Australia. Thanks for the linear algebra series.

rrottier09:07:45

I really enjoy it.