-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Can compare
handle literal square root?
#4
Comments
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 Also note 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 (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 Some other notes on your code:
(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 (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)) |
@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 :) |
@sritchie thanks for the response! :)
I should have included a little more context in my examples, but I was actually referring to The other one, 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)...
Need to wrap my head around that first %) -- |
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. |
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. |
@sritchie I have more questions about literal numbers, e.g. I see that |
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. |
Looks like I need an invitation to join the server?.. |
Odd, I didn't think that was the case but here's a link: |
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 |
Hi! First of all, thanks for putting your time into this project — I've found it extremely pleasant to work with so far! :-)
Now to my question. I'd like to print a table, sorted by a column where among numerical values like whole or rational numbers, occasionally a square root of a literal
2
would appear. For that to work (it doesn't currently) the generic comparison of such literals has to be supported. Is that feasible?Currently:
E.g. a naïve approach could take the literal under the root and compare it to the other hand side, squared (or square both sides in case of the sqrt-to-sqrt comparison). How deeps is this rabbit hole after all? :-)
The text was updated successfully, but these errors were encountered: