How to make outputted expressions more compact?

34 views
Skip to first unread message

Grigory Sarnitsky

unread,
Dec 23, 2012, 1:31:31 AM12/23/12
to fricas...@googlegroups.com
For example

(4) -> (x+y)^3 / (x^2 + (x+z)^3)

            3       2     2     3
           y  + 3x y  + 3x y + x
   (4)  ───────────────────────────
         3       2     2     3    2
        z  + 3x z  + 3x z + x  + x
                                         Type: Fraction(Polynomial(Integer))


thus the norm form suggests all brackets to be opened. Is there a way to collect terms back at least to some extent?

Ralf Hemmecke

unread,
Dec 23, 2012, 4:11:08 PM12/23/12
to fricas...@googlegroups.com
On 12/23/2012 07:31 AM, Grigory Sarnitsky wrote:
How to make outputted expressions more compact?

> (4) -> (x+y)^3 / (x^2 + (x+z)^3)

Would the following already help? If not, make your question more
precise. All you need is a domain that has as "normal form" the form
that you want your expression to look like.

Note, that here FriCAS is fundamentally different from most other CAS.

Ralf

(1) -> R := Fraction(Factored Polynomial Integer)

(1) Fraction(Factored(Polynomial(Integer)))
Type:
Type
(2) -> r: R := (x+y)^3 / (x^2 + (x+z)^3)

3
(y + x)
(2) ---------------------------
3 2 2 3 2
z + 3x z + 3x z + x + x
Type:
Fraction(Factored(Polynomial(Integer)))

Grigory Sarnitsky

unread,
Dec 23, 2012, 11:05:34 PM12/23/12
to fricas...@googlegroups.com
And what if I have Expression(Integer) ? Say,

(5) -> (sin x + y)^3 / ((sin x)^2 + (sin x + z)^3)

Ralf Hemmecke

unread,
Dec 24, 2012, 12:18:50 PM12/24/12
to fricas...@googlegroups.com
On 12/24/2012 05:05 AM, Grigory Sarnitsky wrote:
> And what if I have Expression(Integer) ? Say,
>
> (5) -> (sin x + y)^3 / ((sin x)^2 + (sin x + z)^3)

By now I should be known on this mailing list as the person who doesn't
like Expression(Integer). Seemingly, everyone thinks that an element of
type Expression(Integer) is the same as an expression in Mathematica or
Maple. No, it is not!

In FriCAS, Expression(Integer) is represented as a rational function
where the variables are "kernels". Look deeper into the representation.

https://github.com/hemmecke/fricas-svn/blob/master/src/algebra/expr.spad.pamphlet#L113

That's the standard representation with all its consequences. If you
want something else, you need something else, not Expression(Integer).

What in other CAS is done by a simplify function (with unclear
semantics) is actually not existent in FriCAS. In FriCAS an element
(expression) x of type X is always turned into whatever the type X
thinks is appropriate. If you want x to show differently, you'd usually
have to coerce it into an appropriate type.

Look for example at this, maybe it gives you an idea how things would
have to be done. The trick is to make your types more specific, not the
"all including" Expression(Integer).

Since you haven't made exactly clear what you actually want to achieve
and whether you could perhaps temporarily work with a symbol sx instead
of the kernel sin(x), I'm unable to help any further.

Ralf


(3) -> a:=(2+%i)/(3+x+%i)

2 + %i
(3) ----------
x + 3 + %i
Type: Fraction(Polynomial(Complex(Integer)))
(4) -> a::Complex(Fraction Polynomial Integer)

2x + 7 x + 1
(4) ------------ + ------------ %i
2 2
x + 6x + 10 x + 6x + 10
Type: Complex(Fraction(Polynomial(Integer)))

Grigory Sarnitsky

unread,
Dec 26, 2012, 12:38:08 PM12/26/12
to fricas...@googlegroups.com
Ralf, I am aware of your altitude to Expression(Integer), moreover I am familiar with what are expressions in FriCAS, basically they are what one encounters in the theory of symbolic integration.

I don't normally do all the computations in a CAS, sometimes I resort to a pen and a paper or pass formulas from a CAS to another program, like plotting one. In order to perform such tasks I often wish to have a more compact dispay of formulas, I was speaking consciously about ouputted form, not the expression itself.

Ralf Hemmecke

unread,
Dec 26, 2012, 1:34:41 PM12/26/12
to fricas...@googlegroups.com
> I was speaking consciously about ouputted form, not the expression
> itself.

Maybe the code below helps you a bit.

Anyway, it all depends on what target form you want to have and whether
there already is an appropriate FriCAS domain that can do what you want.
I agree that it's not very easy to find the corresponding domain.

Ralf

(1) -> e := (sin x + y)^3 / ((sin x)^2 + (sin x + z)^3)

3 2 2 3
sin(x) + 3y sin(x) + 3y sin(x) + y
(1) ------------------------------------------
3 2 2 3
sin(x) + (3z + 1)sin(x) + 3z sin(x) + z
Type:
Expression(Integer)
(3) -> d := factor numer e

3
(3) (sin(x) + y)
Type:
Factored(SparseMultivariatePolynomial(Integer,Kernel(Expression(Integer)))

Waldek Hebisch

unread,
Dec 27, 2012, 10:07:31 PM12/27/12
to fricas...@googlegroups.com
Grigory Sarnitsky wrote:
>
> I don't normally do all the computations in a CAS, sometimes I resort to a
> pen and a paper or pass formulas from a CAS to another program, like
> plotting one. In order to perform such tasks I often wish to have a more
> compact dispay of formulas, I was speaking consciously about ouputted form,
> not the expression itself.

In FriCAS output goes via OutputForm. However, there not much
we can do with OutputForm. Basically, we could eliminate
2-D display to recover some whitspace and do common expression
elimination. Other transforms, like factoring or using
distributive law are invalid at level of OutputForm.

In principle we could do factoring during convertion of
values from "commutative" domains (like Expression(Integer))
to OutputForm. However, always factoring would be a bad
choice:
- Factoring may require a lot of time
- Current output follows closely internal structure. Factoring
would obscure this structure sometimes making debugging
harder (in other cases factoring would help).

ATM assumption is that users would do factoring using explicit
commands before output. We could add an option variable to
turn factoring on/off (but this is sligtly bigger change than
it may look at first glance).

Currently FriCAS takes limited advantage from distributive
law when printing polynomials (and similar objects). But
this is based on FriCAS internal form and FriCAS makes
no attempt to find smaller representation.

--
Waldek Hebisch
heb...@math.uni.wroc.pl
Reply all
Reply to author
Forward
0 new messages