-
Notifications
You must be signed in to change notification settings - Fork 2
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
Support negative number representing symbols such as -E4
#42
Comments
Something like this seems to work (outputs {-y "Minus[y]", -x "Minus[x]"}) : (def expression '(+ -x -x -y -5 -2 (- x 10 5) (** x 2)))
(defn strip-ns [form]
(walk/postwalk (fn [form]
(if (qualified-symbol? form)
(symbol (name form))
form))
form))
(defn replacement-map
"Creates a replacement map for negative symbols, such that they are reasonably interpreted by Wolfram.
TODO:
- Extend the idea to deal with other custom replacements (e.g. greek/hebrew symbols.) .
"
[expression]
(let [syms (->> expression
strip-ns
(into '())
(tree-seq list? seq)
(remove (some-fn list? nil?))
(filter #(when (symbol? %)
(->> %
str
(re-matches #"-.+"))))
distinct)
syms-base (map (fn [sym] (-> sym str char-array rest (#(apply str %)) symbol)) syms)]
(zipmap syms (map (fn [sym] (format "Minus[%s]" sym)) syms-base))))
(replacement-map expression)
(replacement-map `(+ -x -x -y -5 -2 (- x 10 5) (** x 2))) Last two s-exps show that it works for quoted and backticked expressions. |
Would it make more sense to actually do the replacement during the expression walk? This would require a fairly fundamental change to how wolframite usually works, but it would be powerful to have user-specified replacement functions. At the moment, we can pass the generated replacement map to ->wl and it will get passed down the line, e.g. (->wl '(+ -x -x -y -5 -2 (- x 10 5) (** x 2))
{:aliases {'-x '(Minus x)
'-y '(Minus y)
'** 'Power}}) produces #object[com.wolfram.jlink.Expr 0x23a65a14 "Plus[Minus[x], Minus[x], Minus[y], -5, -2, Subtract[x, 10, 5], Power[x, 2]]"] |
-E4
@light-matters Not sure these comments have anything to do with supporting negative symbols, as in |
This could likely be implemented easily by extending But this is not something Wolfram itself supports so IMO this is beyond v1. Notice that after #58 we will support Hm, what is |
-E4 is symbolically equivalent to -x,-y etc. unless I misunderstand what you're meaning. I suppose it might have made more sense to use E4 as an example. |
Wolfram does support this, as I can evaluate "-x + 10" in Mathematica, but beyond v1 sounds right for us. As you say, (w/- E4) mostly solves the issue for now (it is better than having to write "(- 0 symbol)" just to make 'symbol' negative. (wl/eval (w/- 'E4))
(wl/eval '(- E4)) both work as expected. E4 is just an arbitrary symbol, e.g. 'equation4' or 'e' or 'x'. This was probaly a bad example, but just happened to be the thing I was working with when I reported the issue! |
Would it be worth making it easier to pass negative symbols, e.g. '-E4 instead of '(- 0 E4).
At the moment, this will be interpreted as a non-alphanumeric symbol.
The text was updated successfully, but these errors were encountered: