0
f1 :: Mesh -> Matrix Double
f1 me = knx where
  hx :: Double
  (hx , _) = h me
  a, knx :: Matrix Double
  a = fromLists [[1,2], [3,4]] 
  knx = hx * a 
  -- knx = 2 * a

I don't get why in the above function, mutliplying by 2 works whereas multiplying by hx = 0.5 doesn't. OTOH, multiplying a Matrix Double with a Double outside a function works as it should.

Couldn't match expected type ‘Matrix Double’
            with actual type ‘Double’
In the first argument of ‘(*)’, namely ‘hx’
In the expression: hx * a
Failed, modules loaded: none.

I am seriously puzzled. Any pointers are welcome!

ocramz
  • 816
  • 6
  • 18
  • So you are puzzled, and want us to be puzzled as well? Give all of the types and functions definitions involved, we are not telepaths (well, majority of us). – Eugene Sh. Mar 02 '15 at 15:07
  • I though it would be pretty much self-contained: the `h` function retrieves a tuple of doubles from a 'Mesh' datastruct (whose internals are not necessary here). The only error in the code above is in the computation of `knx` – ocramz Mar 02 '15 at 15:13
  • Don't you think, that for a comprehensive answer people should be able at least try and reproduce the error (well, it' not always the case, but you should provide the possibility)? – Eugene Sh. Mar 02 '15 at 15:16
  • 1
    (*) has type: (Num a) => a -> a -> a so I highly doubt you could multiply a Matrix Double with a Double anywhere in your code. I think the number you thought as Double outside of this function is actually gets inferred as a 1x1 matrix. Unfortunately I don't have a ghci at hand to test this but I can get back in a few hours if you are still puzzled then. – erdeszt Mar 02 '15 at 15:21
  • You are quite right. The operation I was looking for is `scale :: Container c e => e -> c e -> c e` – ocramz Mar 02 '15 at 15:31

1 Answers1

1

In HMatrix, scale :: Container c e => e -> c e -> c e does what it says on the label (multiplies the e in a c e by the first e). Here are some usage examples here: https://hackage.haskell.org/package/hmatrix-0.16.1.4/docs/src/Data-Packed-Internal-Numeric.html

It should be noted that scale x constructs a Container type by considering x a singleton list, via fromList.

It would be really handy if at least the common arithmetic operations would be overloaded, so that formulas may resemble their mathematical counterpart. I'm not sure whether defining function synonyms (e.g. (.*) = scale ) would be a good idea or it would just add a layer of complexity. Any thoughts?

ocramz
  • 816
  • 6
  • 18
  • 1
    I think the problem with overloading the (.*) for matrix x scalar product is that it doesn't associate as (*) should(and imply). E.g. the order of the arguments would matter and that would be confusing imho. – erdeszt Mar 02 '15 at 17:04