Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Conversation

sritchie
Copy link
Member

This PR fixes an issue I encountered when converting Lie.scm in the scmutils codebase. One of the examples errored for me instead of returning the expected value.

Figuring this out helped me tidy up an edge case with derivatives of functions that return maps. I'll add my email to GJS with the scmutils problem details as an issue comment.

@sritchie sritchie added the Automatic Differentiation Bugs and issues related to Automatic Differentiation label Nov 19, 2021
@sritchie
Copy link
Member Author

;; GJS,
;;
;; When I was converting Lie.scm, I noticed that the discussion starting at line
;; 168, with "Let phi_t(x) be the integral curve of V from x for interval t",
;; triggered a bug in my Clojure port. When I ran the equivalent of the
;; following code in my system:

(install-coordinates R2-rect (up 'x 'y))

(define R2-rect-point ((R2-rect '->point) (up 'x0 'y0)))

(define X (literal-vector-field 'X R2-rect))
(define Y (literal-vector-field 'Y R2-rect))
(define f (literal-scalar-field 'f R2-rect))

(define ((((Lie-test V) Y) f) x)
  (define (g t)
    (- ((Y f) ((+ identity (* t (V identity))) x))
       ((Y (compose f (+ identity (* t (V identity))))) x)))
  ((D g) 0))


(pec (- ((((Lie-test X) Y) f) R2-rect-point)
     ((((Lie-derivative X) Y) f) R2-rect-point)))
#| Result:
0
|#

;; In the Clojure version, instead of 0, I received an error that "extract-tangent"
;; was not defined on a manifold point.
;;
;; What is more, when I ran the example in scmutils, I did not get 0 either! In
;; the current codebase,

((((Lie-test X) Y) f) R2-rect-point)

;; evaluates to 0, so what is printed is actually equivalent to 
;; (- ((((Lie-derivative X) Y) f) R2-rect-point)):

(pec ((((Lie-test X) Y) f) R2-rect-point)
     (- ((((Lie-derivative X) Y) f) R2-rect-point)))
#| Result:
(+ (* (((partial 1) X^0) (up x0 y0)) (Y^1 (up x0 y0)) (((partial 0) f) (up x0 y0)))
   (* (Y^1 (up x0 y0)) (((partial 1) X^1) (up x0 y0)) (((partial 1) f) (up x0 y0)))
   (* -1 (((partial 1) Y^0) (up x0 y0)) (((partial 0) f) (up x0 y0)) (X^1 (up x0 y0)))
   (* -1 (((partial 0) Y^1) (up x0 y0)) (((partial 1) f) (up x0 y0)) (X^0 (up x0 y0)))
   (* -1 (((partial 1) Y^1) (up x0 y0)) (X^1 (up x0 y0)) (((partial 1) f) (up x0 y0)))
   (* (((partial 0) X^0) (up x0 y0)) (((partial 0) f) (up x0 y0)) (Y^0 (up x0 y0)))
   (* -1 (((partial 0) f) (up x0 y0)) (X^0 (up x0 y0)) (((partial 0) Y^0) (up x0 y0)))
   (* (((partial 0) X^1) (up x0 y0)) (((partial 1) f) (up x0 y0)) (Y^0 (up x0 y0))))
|#

;; Well, I figured out what is going on, and I think the fact that it didn't
;; error exposes an interesting bug.
;;
;; You noted a clue to the issue on line 202:
;;
;; "this result is a consequence of confusing manifold points with tuples of
;; coordinates in the embedding space."
;;
;; Here is how to fix Lie-test on line 189 so that the types work out and the
;; example succeeds again. Notice the redefinition of IDENTITY to be (chart
;; R2-rect), so we get the coordinate representation out:

(define ((((Lie-test V) Y) f) x)
  (let ((I (chart R2-rect)))
    (define (g t)
      (- ((compose (Y f) (point R2-rect))
          ((+ I (* t (V I))) x))
         ((Y (compose f
                      (point R2-rect)
                      (+ I (* t (V I)))))
          x)))
    ((D g) 0)))

(pec (- ((((Lie-test X) Y) f) R2-rect-point)
     ((((Lie-derivative X) Y) f) R2-rect-point)))
#| Result:
0
|#

;; So why did the Clojure port error, but scmutils succeeds with a 0 for the
;; Lie-test term?
;;
;; Lie-test contains this term, which evaluates to 0 in scmutils:

((X identity) R2-rect-point)
;;=> 0

;; But that function should in fact error; (X identity) is supposed to take a
;; real-valued function of the manifold, and IDENTITY here will return its
;; input, ie, a manifold point.
;;
;; The reason this succeeds and evaluates to 0 is that EXTRACT-DX-PART returns 0
;; when passed a manifold point instead of erroring. It probably should error,
;; because it is not in any coordinate representation.

(extract-dx-part 1 R2-rect-point)
;; => 0

;; Here is where this happens, in kernel/diff.scm, lines 819-831:

(define (extract-dx-differential dx obj)
  (if (differential? obj)
      <implementation-elided>
      :zero))

;; So this is a bug, I would say, that is NOT caught because of the default
;; behavior of EXTRACT-DX-PART.
;;
;; The 0 then ripples through Lie-test, masking the type error.

@sritchie sritchie changed the title Fix type bug in covariant.cljc tests Fix type bug in covariant.cljc tests, bug w/ derivatives of fns returning maps Nov 19, 2021
@codecov-commenter
Copy link

codecov-commenter commented Nov 19, 2021

Codecov Report

Merging #394 (0665a6f) into master (8658c0c) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #394      +/-   ##
==========================================
- Coverage   85.21%   85.20%   -0.02%     
==========================================
  Files          97       97              
  Lines       12996    12998       +2     
  Branches      696      694       -2     
==========================================
  Hits        11075    11075              
- Misses       1225     1229       +4     
+ Partials      696      694       -2     
Impacted Files Coverage Δ
src/sicmutils/collection.cljc 92.52% <100.00%> (+0.14%) ⬆️
src/sicmutils/rational_function.cljc 79.03% <0.00%> (-0.85%) ⬇️
src/sicmutils/numsymb.cljc 92.96% <0.00%> (+0.24%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8658c0c...0665a6f. Read the comment docs.

@sritchie sritchie merged commit c05f21a into master Nov 19, 2021
@sritchie sritchie deleted the sritchie/lie_fix branch November 21, 2021 14:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Automatic Differentiation Bugs and issues related to Automatic Differentiation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants