why Fricas does not automatically simplify sin(x)/cos(x) to tan(x)?

49 views
Skip to first unread message

Nasser M. Abbasi

unread,
Mar 8, 2023, 9:42:40 PM3/8/23
to FriCAS - computer algebra system
How does one tell Fricas to simplify sin(x)/cos(x) to tan(x)?

simplify(sin(x)/cos(x))  does not do the trick.

I noticed this when integrating 1/cos(x)^2 
which should return tan(x) but Fricas returned sin(x)/cos(x) 
(Ofcourse Fricas answer is correct), but tan(x) is simpler.

Should not this be an automatic simplification?
Mathematica does it without even calling Simplify.

--Nasser

Neven Sajko

unread,
Mar 8, 2023, 10:23:33 PM3/8/23
to fricas-devel
Rules are used for that, see the Fricas Book, e.g., the section "0.8 Pattern Matching".

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/0e8c3035-7081-4145-a367-630b91c604e0n%40googlegroups.com.

Waldek Hebisch

unread,
Mar 9, 2023, 6:54:00 AM3/9/23
to 'Nasser M. Abbasi' via FriCAS - computer algebra system
On Wed, Mar 08, 2023 at 06:42:40PM -0800, 'Nasser M. Abbasi' via FriCAS - computer algebra system wrote:
> How does one tell Fricas to simplify sin(x)/cos(x) to tan(x)?
>
> simplify(sin(x)/cos(x)) does not do the trick.
>
> I noticed this when integrating 1/cos(x)^2
> which should return tan(x) but Fricas returned sin(x)/cos(x)
> (Ofcourse Fricas answer is correct), but tan(x) is simpler.
>
> Should not this be an automatic simplification?

No. Transformations of this sort are very undesirable when doing
computations which depend on structure of expressions. Basicaly,
when such transformations fire in automatic way it is not
possible to usefuly predict form of final result.


During integration FriCAS tries as much as it can preserve structure
of user input. This means using 'tan' in answer when user input
contains 'tan', using 'sin' and 'cos' when user input is in terms
of 'sin' and 'cos'. Compare:

(3) -> integrate(1 + tan(x)^2, x)

(3) tan(x)
Type: Union(Expression(Integer),...)

Note: FriCAS performs rather drastic transformations during
integration. Without effort to preserve user input result
would look quite different. And sometimes this preservation
effort fails.

> Mathematica does it without even calling Simplify.

In FriCAS we have:

(11) -> simplify(tan(x))

sin(x)
(11) ------
cos(x)
Type: Expression(Integer)

Many folks consider expression in terms of 'sin' and 'cos' simpler
than expression in terms of 'tan'. If you are concerned only
with lone 'sin(x)/cos(x)', then rewrite rule can transform it
to 'tan'. In general, it is easy to replace 'tan(x)' by
'sin(x)/cos(x)', but transformation in opposite direction
is more complicated. Consider

(cos(x)^2*sin(x)+(6*cos(x)^3+cos(x)))/(sin(x)+2*cos(x)^3)

(which was obtained from '(7 + tan(x) + tan(x)^2)/(2 + tan(x) + tan(x)^3)').
In version 12 of Matematica Simplify and FullSimplify leave
this unchanged. I am not sure if Matematica can not transform
this back to 'tan' form or maybe it considers version in terms
of 'sin' and 'cos' as simpler.

--
Waldek Hebisch

Martin R

unread,
Mar 10, 2023, 3:16:41 AM3/10/23
to FriCAS - computer algebra system
> During integration FriCAS tries as much as it can preserve structure of user input.

I think that this is not true with respect to polynomials: these are always expanded, right?

sage: fricas("(x+y)^5")
 5        4       2 3       3 2      4     5
y  + 5 x y  + 10 x y  + 10 x y  + 5 x y + x
sage: fricas("(x+sin(y))^5")
      5             4       2      3       3      2      4          5
sin(y)  + 5 x sin(y)  + 10 x sin(y)  + 10 x sin(y)  + 5 x sin(y) + x
sage: fricas("(1+x+sin(y))^3")
      3                  2       2                     3      2
sin(y)  + (3 x + 3)sin(y)  + (3 x  + 6 x + 3)sin(y) + x  + 3 x  + 3 x + 1

Best wishes and thank you for all your work on FriCAS!

Martin

Ralf Hemmecke

unread,
Mar 10, 2023, 3:26:17 AM3/10/23
to fricas...@googlegroups.com
On 10.03.23 09:16, 'Martin R' via FriCAS - computer algebra system wrote:
>> During integration FriCAS tries as much as it can preserve structure of
> user input.
>
> I think that this is not true with respect to polynomials: these are always
> expanded, right?

Martin, you should know that there is concept of a "unexpanded
polynomial" in FriCAS. So *before* the integrator sees the polynomial,
it is already expanded.

The closest you can have is if the polynomial is of type

Factored Polynomial C

However, most probably it will internally be converted to Expression(C)
which is represented as a (expanded) rational function in Kernel(X).

What Waldek probably meant was that the result expresssion does not have
other kernels than the input if not absolutely necessary.

Does that make sense? Maybe Waldek will correct me if my analysis is wrong.

Ralf

Waldek Hebisch

unread,
Mar 10, 2023, 8:10:38 AM3/10/23
to 'Martin R' via FriCAS - computer algebra system
On Fri, Mar 10, 2023 at 12:16:41AM -0800, 'Martin R' via FriCAS - computer algebra system wrote:
> > During integration FriCAS tries as much as it can preserve structure of
> user input.
>
> I think that this is not true with respect to polynomials: these are always
> expanded, right?
>
> sage: fricas("(x+y)^5")
> 5 4 2 3 3 2 4 5
> y + 5 x y + 10 x y + 10 x y + 5 x y + x
> sage: fricas("(x+sin(y))^5")
> 5 4 2 3 3 2 4 5
> sin(y) + 5 x sin(y) + 10 x sin(y) + 10 x sin(y) + 5 x sin(y) + x
> sage: fricas("(1+x+sin(y))^3")
> 3 2 2 3 2
> sin(y) + (3 x + 3)sin(y) + (3 x + 6 x + 3)sin(y) + x + 3 x + 3 x + 1

Well:

a) As Ralf noted polynomial are expanded before they reach integrator,
b) Integration changes coefficients of polynomials in a way which
does not play nice with factor structure. So even if we spent
more effort it seem that we have to expand.

To comment more on "as much as it can": FriCAS code is currently
oriented to have normal form of expressions which forces normalization
and expansion. Normalization means that internally integrator
may use different kernels than what is in user input, and preservation
is currently limited to kernels. I have some ideas to how to preserve
more but that needs new code and effect will be limited.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages