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

port final careful-simplify implementation #20

Open
sritchie opened this issue Jan 4, 2022 · 0 comments
Open

port final careful-simplify implementation #20

sritchie opened this issue Jan 4, 2022 · 0 comments

Comments

@sritchie
Copy link
Member

sritchie commented Jan 4, 2022

This is used by a function in the quaternion implementation, but maybe not needed right away. Once we get all of that merged, use it in rotation-matrix->quaternion, possibly, instead of the existing g/simplify call.

Here it is:

(comment
  ;; TODO I THINK we have actual thing somewhere else. We want this dynamic
  ;; variable around.
  (def ^:dynamic *factoring* false)

  ;; Hamiltonians look better if we divide them out.
  (defn ham:simplify [hexp]
    (cond (and (quotient? hexp) *divide-out-terms*)
          (if (sum? (symb:numerator hexp))
            (let [d (symb:denominator hexp)]
              (a-reduce symb:+
                        (map (fn [n]
                               (g/simplify (symb:div n d)))
                             (operands
                              (symb:numerator hexp)))))
            hexp)

          (compound-data-constructor? hexp)
          (cons (operator hexp) (map ham:simplify (operands hexp)))

          :else hexp))

  (define clean-differentials
    ;; TODO clean a CLEANED differential... aren't these all done??
    (rule-simplifier
     (ruleset
      (make-differential-quantity
       [??lterms
        (make-differential-term (? dx) 0)
        ??rterms])
      =>
      (make-differential-quantity [??lterms ??rterms])

      (make-differential-quantity
       [(make-differential-term '() ?x)]) => ?x

      (make-differential-quantity []) => 0)))

  (define (flush-literal-function-constructors expr)
    (if (pair? expr)
      (if (eq? (car expr) 'literal-function)
        (if (and (pair? (cadr expr)) (eq? (caadr expr) 'quote))
          (flush-literal-function-constructors (cadadr expr))
          (cadr expr))
        (cons (flush-literal-function-constructors (car expr))
              (flush-literal-function-constructors (cdr expr))))
      expr))

  (defn simplify [exp]
    ((access clean-differentials rule-environment)
     (flush-derivative
      (flush-literal-function-constructors
       (ham:simplify
        ((if *factoring* poly:factor (fn [expr] expr))
         (g:simplify exp)))))))

  ;; Is this enough? move to simplify.
  (define (careful-simplify e)
    (simplify e)))
@sritchie sritchie transferred this issue from sicmutils/sicmutils Jan 24, 2023
@sritchie sritchie transferred this issue from another repository Jan 24, 2023
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

1 participant