jackfirth / lens

A Racket package for creating and composing pure functional lenses
Other
74 stars 8 forks source link

Matrix lenses? #286

Open AlexKnauth opened 8 years ago

AlexKnauth commented 8 years ago

Should there be a lens constructor for matrices as functions from column-vectors to column-vectors?

For square invertable matrices it could be an isomorphism lens.

(define (matrix-lens/invertable m)
  (define m^-1 (matrix-inverse m))
  (make-isomorphism-lens
   (λ (a) (matrix* m a))
   (λ (b*) (matrix* m^-1 b*))))

But for non-invertable matrices (including non-square?), the setter function will sometimes have infinitely many results that would be valid.

(define (matrix-lens/noninvertable m)
  (make-lens
   (λ (a)
     ;; M*A = B
     (matrix* m a))
   (λ (a b*)
     ;; M*A = B
     (define b (matrix* m a))
     ;; M*A' = B'
     ;; What is A'?
     ;; There are many solutions if M has no inverse;
     ;; Which is the best one, which follows the lens laws?
     ;; The lens laws make the constraint that when B = B', the result should be A.
     ;; But that only helps in one specific case. What should this do?
     (define a->b (matrix- b a))
     (define b->b* (matrix- b* b))
     (error 'matrix-lens "I don't know what do if it's not an invertable matrix"))))

Are there any other constraints having to do with continuous-ness or anything else that would help make a sensible lens out of a non-invertable matrix?

jackfirth commented 8 years ago

Lenses for things provided from the math library in general could be very useful, but I'm not sure they should go in the main lens package. A side package like collections-lens would be better.

AlexKnauth commented 8 years ago

That makes sense. (And it would only have to depend on lens-common!) I'll do that once this is figured out.

Also, do you have any ideas for how the setter should use the original target vector to resolve which of the possible solutions it should return, and which would be the best for the lens laws?