Giter Site home page Giter Site logo

Comments (10)

sritchie avatar sritchie commented on September 22, 2024

Good questions! I think what you're suggesting is a good idea, that we want some function that will go and evaluate out exact-but-still-numeric values in an expression.

If you want a comparator that will work between symbolic expressions, numbers, etc, give sicmutils.expression/compare a try: https://github.com/sicmutils/sicmutils/blob/main/src/sicmutils/expression.cljc#L229-L278

Also note sicmutils.value/compare, aliased as sicmutils.env/compare:

user> (clojure.core/compare 1 (literal-number 2))
Execution error (ClassCastException) at user/eval46014 (REPL:67).
class sicmutils.expression.Literal cannot be cast to class java.lang.Number (sicmutils.expression.Literal is in unnamed module of loader clojure.lang.DynamicClassLoader @1a0bb8bd; java.lang.Number is in module java.base of loader 'bootstrap')
user> (sicmutils.value/compare 1 (literal-number 2))
-1

So for now I suggest you write your own compare that tries to evaluate "exact" numbers on both sides:

(require '[pattern.rule :as r :refer [=>]])
(require '[sicmutils.value :as v])

(def exact->numeric
  (let [g (find-ns 'sicmutils.generic)]
    (r/rule-simplifier
     (r/rule (?op ??xs)
             #(every? v/number? ('??xs %))
             (? (fn [{op '?op xs '??xs}]
                  (apply (ns-resolve g op) xs)))))))

(defn my-compare [l r]
  (v/compare
   (exact->numeric l)
   (exact->numeric r)))

(my-compare 1 (literal-number (sqrt 2)))
;;=> -1

A similar idea comes up in the original scmutils library, where Sussman has an = implementation for expressions that checks if (zero? (simplify (- l r))... it's off by default because it makes = an expensive operation, but it's still a good idea to have available!

Some other notes on your code:

  • Just for fun, exprs2 can be written this way too:
(defn exprs2 [n]
  (if (= 1 n)
    [2]
    (mapcat (fn [s]
              (for [op ops
                    l  (exprs2 s)
                    r  (exprs2 (- n s))]
                (list op l r)))
            (range 1 n))))
(require '[pattern.rule :as r :refer [=>]])

(def reval
  (r/rule-simplifier
   (r/rule
    (expt ?x 1/2)     => (sqrt ?x))))

(reval '(+ x (expt y 1/2)))
;;=> (+ x (sqrt y))

Or if you also / instead want to match symbolic (/ 1 2), not just the actual value 1/2:

(def reval
  (r/rule-simplifier
   (r/ruleset
    (expt ?x 1/2)     => (sqrt ?x)
    (expt ?x (/ 1 2)) => (sqrt ?x))))

(reval '(+ x (expt y (/ 1 2))))
;=> (+ x (sqrt y))

from emmy.

sritchie avatar sritchie commented on September 22, 2024

@a1exsh Also, the biggest help possible for the library is publishing this stuff out and singing its praises! I'll be getting more and more visualization etc going in the next month or two, but it was great to see your clerk notebook with its symbolic code. Keep track of what could be better and we'll get it done :)

from emmy.

a1exsh avatar a1exsh commented on September 22, 2024

Good questions! I think what you're suggesting is a good idea, that we want some function that will go and evaluate out exact-but-still-numeric values in an expression.

@sritchie thanks for the response! :)

If you want a comparator that will work between symbolic expressions, numbers, etc, give sicmutils.expression/compare a try: https://github.com/sicmutils/sicmutils/blob/main/src/sicmutils/expression.cljc#L229-L278

Also note sicmutils.value/compare, aliased as sicmutils.env/compare:

user> (clojure.core/compare 1 (literal-number 2))
Execution error (ClassCastException) at user/eval46014 (REPL:67).
class sicmutils.expression.Literal cannot be cast to class java.lang.Number (sicmutils.expression.Literal is in unnamed module of loader clojure.lang.DynamicClassLoader @1a0bb8bd; java.lang.Number is in module java.base of loader 'bootstrap')
user> (sicmutils.value/compare 1 (literal-number 2))
-1

I should have included a little more context in my examples, but I was actually referring to sicmutils.value/compare, not clojure.core/compare. ;)

The other one, sicmutils.expression/compare doesn't throw exceptions when given some literals, but behaves... funny: https://github.com/a1exsh/notes/blob/main/notebooks/literal_compare.clj

Looks like it can be useful for something, but I'm not sure exactly what's the use case (note the very last result, not sure how we end up there even with hashes)...

So for now I suggest you write your own compare that tries to evaluate "exact" numbers on both sides:

(require '[pattern.rule :as r :refer [=>]])
(require '[sicmutils.value :as v])

(def exact->numeric
  (let [g (find-ns 'sicmutils.generic)]
    (r/rule-simplifier
     (r/rule (?op ??xs)
             #(every? v/number? ('??xs %))
             (? (fn [{op '?op xs '??xs}]
                  (apply (ns-resolve g op) xs)))))))

(defn my-compare [l r]
  (v/compare
   (exact->numeric l)
   (exact->numeric r)))

(my-compare 1 (literal-number (sqrt 2)))
;;=> -1

A similar idea comes up in the original scmutils library, where Sussman has an = implementation for expressions that checks if (zero? (simplify (- l r))... it's off by default because it makes = an expensive operation, but it's still a good idea to have available!

Need to wrap my head around that first %)

--
Alex

from emmy.

sritchie avatar sritchie commented on September 22, 2024

The goal with that literal compare is to give SOME way to sort arguments into commutative functions like * so we can compare bigger expressions. Otherwise (* x y) won't equal (* y x), etc... but I see that it's not what you need here.

from emmy.

sritchie avatar sritchie commented on September 22, 2024

And yeah the matcher I wrote at the end is not obvious at all!! I think we need some better syntax for this use case, there's an issue somewhere around making this nicer. I'll comment soon.

from emmy.

a1exsh avatar a1exsh commented on September 22, 2024

@sritchie I have more questions about literal numbers, e.g. I see that negative? or real? predicates don't work like I expect them to. What is the best way to discuss these (and probably more questions)? Should I raise additional issues here, or you prefer email/IRC/etc?.. :)

from emmy.

sritchie avatar sritchie commented on September 22, 2024

I wouldn't be surprised if you've found some bugs! Let's chat in the #sicmutils channel at https://clojurians.slack.com/. I'll be on within an hour or so and hanging for most of the day.

from emmy.

a1exsh avatar a1exsh commented on September 22, 2024

Looks like I need an invitation to join the server?..

from emmy.

sritchie avatar sritchie commented on September 22, 2024

Odd, I didn't think that was the case but here's a link:

from emmy.

sritchie avatar sritchie commented on September 22, 2024

Join me on Slack -- it’s a faster, simpler way to work. Sign up here, from any device: https://join.slack.com/t/clojurians/shared_invite/zt-1kp1qss90-Aod_ANmRUKFtZ7S9CpBIVg

from emmy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.