0

Somehow, I'm still trying to build an intuition on what can I express in Haskell and what not.

I have my own (simple) matrix type and I'm wondering if it would be possible to somehow "lift" it to a function so as to make sense of the usual math notation for a matrix applied on a vector.

Let me explain this question in more detail.

data Matrix a = M [[a]] deriving (Show,...)

dot u v = foldr (+) 0 $ zipWith (*) u v

mv :: Num a => Matrix a -> [a] -> [a]
mv (M []) _ = []
mv _ [] = []
mv (M m) v = dot (head m) v : mv (M (tail m)) v

Clearly, the function mv implements the action of a matrix M m on a "vector" of type [a].

But, is there a way to implement the same action with a syntax like (M' m) v such that

(M' m) v = mv (M m ) v ?

It should then allow to write something like

m = M' [[2,3],[5,7]]
v = m [1,0] -- v == [2,5]

I suspect the matrix data type I defined isn't enough to do such a lifting. Somehow I think I'm trying to find a functor from a self-defined matrix type to some hom([a],[a]), i.e., the set of morphisms in category of vector spaces Vec, but my data type lacks the necessary structure.

Is there a fundamental reason why it might not be possible to do such "lifting"?

MASL
  • 929
  • 1
  • 11
  • 25
  • One idea is to make `Matrix` something like a [difference list](https://hackage.haskell.org/package/dlist-0.7.1.2/docs/Data-DList.html); instead of it being a nested list, it's a function that takes a nested list and a list and returns another list. That is, you could think of `M [[2,3],[5,7]]` as a partially applied function rather than just a wrapped nested list. `mv` would then be more of a smart constructor. – chepner May 05 '16 at 04:33
  • Notice that if you define `m = mv $ M [[2,3], [5,7]]` then you get that m [1, 0] gets your desired effect. This is just a play on currying: a partial evaluation of your mv function. – Misguided May 05 '16 at 05:59
  • Seeing this just now. Yes, I've been playing with currying so far as you mention. The problem comes when I then try to calculate the gradient/jacobian of an expresion with respect to the matrix values. It ran into new compilation erros when I tried that. For instance, let say you have a simklple, multilayer feedforward neural network. introducing ```mv``` runs into problems with automatic differentiation. Not sure if I should detail this case in my original question too. It might provide other useful answers too... – MASL May 06 '16 at 02:55
  • @Julián, thanks for your comments. I realize now I may not be asking the right question for the problem I have. I will thus likely delete this question and post another, more focused one. – MASL May 06 '16 at 19:19
  • @chepner, thanks for your comments. I don't quite get how to use a Difference list, but I think I first need to rethink my own question. See my previous comment. – MASL May 06 '16 at 19:20
  • @MASL: please post the code, I don't mind looking at it, but I can't answer on nothing – Misguided May 07 '16 at 02:56
  • @Julián Thanks. I posted a new, more specific question here https://stackoverflow.com/questions/37088840/automatic-differentiation-ad-with-respect-to-list-of-matrices-in-haskell – MASL May 07 '16 at 12:58

0 Answers0