Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent inner product with complex-valued tuples #20

Open
ocramz opened this issue Jan 5, 2017 · 3 comments
Open

Inconsistent inner product with complex-valued tuples #20

ocramz opened this issue Jan 5, 2017 · 3 comments

Comments

@ocramz
Copy link

ocramz commented Jan 5, 2017

https://github.com/conal/vector-space/blob/master/src/Data/VectorSpace.hs#L145 defines (u,v) <.> (u',v') = (u <.> u') ^+^ (v <.> v'), but (1, j) <.> (j, 1) should be 2 j, rather than 0.

@leftaroundabout
Copy link
Contributor

leftaroundabout commented Jan 5, 2017

Basically the question is: why is Scalar (Complex r) ~ r, instead of Scalar (Complex r) ~ Complex r?

In fact, when the complex numbers are considered as a 2D real space, then the current result does make sense (you can't apply linearity i · 〈v,w〉 = 〈i·v,w〉 anymore – (1,i) and (i,1) are simply orthogonal then); but that would basically mean that we don't have any complex vector spaces – only real spaces, some of which are called Complex.

That's quite silly if you ask me. It certainly precludes many relevant applications, from DSP to quantum mechanics. Hence I'd propose to change the instance to

instance VectorSpace v => VectorSpace (Complex v) where
  type Scalar (Complex v) = Complex (Scalar v)
  (x:+y) *^ (x':+y') =  (x*^x' ^-^ y*^y') :+ (x*^y' ^+^ y*^x')

Of course one might now object that magnitudeSq v does not have a real number type, but we already have much the same issue with the (also not uncontroversial) function instance, whose scalars are functions, not real numbers.

This could perhaps be adressed with a separate class

class VectorSpace v => Normed v where
  type Magnitude v :: *
  magnitudeSq :: v -> Magnitude v

@ocramz
Copy link
Author

ocramz commented Jan 5, 2017

That was exactly my conclusion, we do need a separate associated typefamily, independent of the Scalar one introduced by VectorSpace. Indeed, the norms of vectors over either R or C must be real, but the inner product of two vectors over C need not be.

I'd change the definition of Normed to reflect the fact that the L2 norm is induced by the inner product:

class InnerSpace v => Normed v where
  type Magnitude v :: *
  magnitudeSq :: v -> Magnitude v
  magnitudeSq x = x <.> x   

@ocramz
Copy link
Author

ocramz commented Jan 5, 2017

^ no, the default definition of magnitudeSq cannot use <.> because Couldn't match expected type 'Magnitude v' with actual type 'Scalar v'.

Edit: (that Magnitude v shouldn't always unify with Scalar v is exactly the point, my bad)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants