conal / vector-space

Vector & affine spaces, linear maps, and derivatives
Other
45 stars 20 forks source link

Strange(?) definition of scalar multiplication on function spaces #9

Open idontgetoutmuch opened 10 years ago

idontgetoutmuch commented 10 years ago

The code commented out is what I would expect from normal mathematical usage. Specifically

$$ (\lambda f)(x) = \lambda f(x) $$

I suppose one could use e.g. (const 3.0) *^ f but this seems to make what I assume would be a common case somewhat obscure.

-- instance VectorSpace v => VectorSpace (a -> v) where
--   type Scalar (a -> v) = Scalar v
--   (*^) s = fmap (s *^)

-- No 'InnerSpace' instance for @a -> v@.

-- Or the following definition, which is useful for the higher-order
-- shading dsel in Shady (borrowed from Vertigo).

instance VectorSpace v => VectorSpace (a -> v) where
  type Scalar (a -> v) = a -> Scalar v
  (*^) = liftA2 (*^)
andersjel commented 9 years ago

It would also match the behaviour of the instance for :->: from Data.MemoTrie and :-* from Data.LinearMap.

conal commented 9 years ago

I struggled with this choice. The unconventional choice I made is much more general and usefully so. For instance, it allows time- and/or space-varying versions scalar multiplication. As mentioned in the comment, Shady exploits the generalized form nicely for lighting, as did Vertigo (IIRC). I was able to write simple and generic lighting formulas and apply them to functions from lighting environments.

conal commented 9 years ago

Thanks for pointing out the inconsistency with memo tries and linear maps. I'd like a consistent choice.