lens icon indicating copy to clipboard operation
lens copied to clipboard

Matrix lenses?

Open AlexKnauth opened this issue 9 years ago • 2 comments

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?

AlexKnauth avatar Sep 07 '16 19:09 AlexKnauth

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.

jackfirth avatar Sep 08 '16 07:09 jackfirth

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?

AlexKnauth avatar Sep 08 '16 15:09 AlexKnauth