0

Is there a compact way to scale a matrix by a constant in repa-3.2.3.3? Right now I'm using this:

(.*) :: (Num d, Shape sh, Source r d) => Array r sh d -> d -> Array D sh d
(.*) m c = R.map ((*) c) m

I'm confused since such a function is typically included in an array library.

tba
  • 6,229
  • 8
  • 43
  • 63
  • 1
    Using a section makes it slightly nicer: `R.map (c *) m`. – luqui Jul 22 '14 at 04:17
  • 1
    I think your function is fine, though I might make it point-free. Generally there's no need to provide per-element operations since any such operation is trivially applied across the whole Functor using fmap. – Boyd Stephen Smith Jr. Jul 22 '14 at 04:18
  • 1
    @BoydStephenSmithJr.: Or an equivalent function, given that `Array` isn't an instance of `Functor`. – Zeta Jul 22 '14 at 06:39

1 Answers1

3

Is there a compact way to scale a matrix by a constant in repa-3.2.3.3?

No, there isn't. After all, map gives you a way to apply any function element-wise. However, keep in mind that you should probably inline your function (source):

Advice for writing fast code:

[...]

2. Add INLINE pragmas to all leaf-functions in your code, expecially ones that compute numeric results. Non-inlined lazy function calls can cost upwards of 50 cycles each, while each numeric operator only costs one (or less). Inlining leaf functions also ensures they are specialised at the appropriate numeric types.

Community
  • 1
  • 1
Zeta
  • 103,620
  • 13
  • 194
  • 236