Discussion:
[fricas-devel] SExpression to string
'Martin R' via FriCAS - computer algebra system
2018-07-30 05:31:14 UTC
Permalink
Dear all,

I was trying to turn an SExpression into a (compact) String. My final goal
is to try to avoid "unparse$InputForm" in the sage interface.

In particular, I would prefer a solution which does not contain any
unnecessary whitespace.

I know of

FORMAT('NIL, "~S", (-1/2)::INFORM)$Lisp

but this has some undesirable side effects with the rootOf operator, which
I do not understand:

r := integrate(72000/(1+x^5),x)::INFORM
FORMAT('NIL, "~S", r)$Lisp

(5)
"(/
(+
(*
(+
(^
(+
(+
(* -3
(^

#1=(|rootOf|
(+
(+
(+

(^
#2=(|rootOf|
(+

(+
(+ (+ (^ %%E0 4) (* 14400 (
^ %%E0 3)))
(* 207360000 (^ %%E0 2)))

(* 2985984000000 %%E0))
429981696000000
00)
%%E0)
3)
...

I also tried brightPrint(r)$Lisp, which yields a very compact display (the
result is the empty SExpression, but I guess I could capture the output or
somehow modify brightPrint), but it omits whitespace in the top level list:

(8) -> brightPrint((-1/2)::INFORM)$Lisp
/-12
(8) ()
Type:
SExpression


Thanks for any hints!

Martin
--
You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
'Martin R' via FriCAS - computer algebra system
2018-07-30 09:45:32 UTC
Permalink
I now have the following. I couldn't find an equivalent of python's join
(in Common Lisp there is format, of course), which takes a string and a
list of strings, and puts the first between all the others...

comments very welcome!

Martin

sageprint(x:InputForm):String ==
atom? x =>
float? x => return float(x)::String
integer? x => return integer(x)::String
string? x => return concat(["_"", string(x)::String, "_""])$String
symbol? x => return string(symbol(x))
S: List String := [sageprint y for y in destruct x]
R: String := new(1 + reduce(_+, [1 + #(s)$String for s in S], 0),
space()$Character)
copyInto!(R, "(", 1)
i := 2
for s in S repeat
copyInto!(R, s, i)
i := i + 1 + #(s)$String
copyInto!(R, ")", i-1)
return R
--
You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
Waldek Hebisch
2018-07-30 10:17:04 UTC
Permalink
Post by 'Martin R' via FriCAS - computer algebra system
Dear all,
I was trying to turn an SExpression into a (compact) String. My final goal
is to try to avoid "unparse$InputForm" in the sage interface.
In particular, I would prefer a solution which does not contain any
unnecessary whitespace.
I know of
FORMAT('NIL, "~S", (-1/2)::INFORM)$Lisp
but this has some undesirable side effects with the rootOf operator, which
r := integrate(72000/(1+x^5),x)::INFORM
FORMAT('NIL, "~S", r)$Lisp
(5)
"(/
(+
(*
(+
(^
(+
(+
(* -3
(^
#1=(|rootOf|
^^
Post by 'Martin R' via FriCAS - computer algebra system
(+
(+
(+
(^
#2=(|rootOf|
^^

You mean those markers? This is standard Lisp way of printing
repeated substructures: first time Lisp adds the marker, later
#1# means marked subexpression. It is possible to turn this
off, but normally markers makes output smaller so you should like
them.
--
Waldek Hebisch
--
You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
'Martin R' via FriCAS - computer algebra system
2018-07-30 10:38:03 UTC
Permalink
For the moment I'd like to do something which is as little work on my side
as possible - so I'd rather avoid them.

(the real optimization will be to communicate not over strings but directly
- but not now)

Best,

Martin
Post by Waldek Hebisch
Post by 'Martin R' via FriCAS - computer algebra system
Dear all,
I was trying to turn an SExpression into a (compact) String. My final
goal
Post by 'Martin R' via FriCAS - computer algebra system
is to try to avoid "unparse$InputForm" in the sage interface.
In particular, I would prefer a solution which does not contain any
unnecessary whitespace.
I know of
FORMAT('NIL, "~S", (-1/2)::INFORM)$Lisp
but this has some undesirable side effects with the rootOf operator,
which
Post by 'Martin R' via FriCAS - computer algebra system
r := integrate(72000/(1+x^5),x)::INFORM
FORMAT('NIL, "~S", r)$Lisp
(5)
"(/
(+
(*
(+
(^
(+
(+
(* -3
(^
#1=(|rootOf|
^^
Post by 'Martin R' via FriCAS - computer algebra system
(+
(+
(+
(^
#2=(|rootOf|
^^
You mean those markers? This is standard Lisp way of printing
repeated substructures: first time Lisp adds the marker, later
#1# means marked subexpression. It is possible to turn this
off, but normally markers makes output smaller so you should like
them.
--
Waldek Hebisch
--
You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
Waldek Hebisch
2018-07-30 10:51:50 UTC
Permalink
Post by 'Martin R' via FriCAS - computer algebra system
For the moment I'd like to do something which is as little work on my side
as possible - so I'd rather avoid them.
(the real optimization will be to communicate not over strings but directly
- but not now)
Well, see for example '|print_full2|' in src/interp/macros.lisp
'|print_full2|' setting are not what you want, but similar function
can set '*print-circle*' and '*print-pretty*' to 'nil'.
--
Waldek Hebisch
--
You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel+***@googlegroups.com.
To post to this group, send email to fricas-***@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.
Loading...