Comments (10)
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))))
-
I really want it to be easier to override the TeX renderer's handling of different operations, so that cases like this this become easy to write: https://github.com/a1exsh/notes/blob/main/notebooks/four_twos.clj#L72-L73
-
You can write this function https://github.com/a1exsh/notes/blob/main/notebooks/four_twos.clj#L92-L101 with the pattern matching code in the library:
(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.
@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.
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-L278Also note
sicmutils.value/compare
, aliased assicmutils.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))) ;;=> -1A 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.
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.
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.
@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.
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.
Looks like I need an invitation to join the server?..
from emmy.
Odd, I didn't think that was the case but here's a link:
from emmy.
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)
- Next Journal link leads to blank notebook HOT 1
- unary negation handling is broken in ->JavaScript HOT 3
- port functional version of partitions-M algorithm HOT 5
- Port axch's adaptive-plot library
- Links to documentation are broken HOT 2
- Feature request for 'collect' function
- Feature request for 'Expand' function
- Simplifier doesn't detect trigonometric identity HOT 1
- state-advancer throws "Cannot mix BigInt and other types" HOT 4
- CSE leaving a lot on the table due to variadic *, + etc HOT 1
- Overhaul SCI interface to use modern copy-ns etc HOT 1
- FDG Link in Readme broken HOT 1
- Add pretty-print extensions for custom types
- Protocol ICoordinateSystem is overwriting function uuid
- symbolic-taylor-series can't take perturbations as arguments
- Question arising from vector calculus HOT 5
- Unexpected results from Brent minimizer. HOT 3
- Feature request: Plotting complex number functions
- Write a TeXmacs plugin
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from emmy.