How to make call to latex() not evaluate its content again?

83 views
Skip to first unread message

Nasser M. Abbasi

unread,
Jul 26, 2023, 1:17:23 AM7/26/23
to sage-devel
Sometimes when calling integrate using algorithm such as maxima, it returns result which is not fully resolved but still have an integrate inside it.

Next, when calling latex() on the anti-derivative this cause problems, because sage tried to calls maxima again on the integrate command inside the result.

Is there a way to make latex() just convert the result without calling integrate again?
This results in problems like the following

sage: latex(anti)
Not invertible Error: Bad Argument Value
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit

Here is an example

>sage
│ SageMath version 10.0, Release Date: 2023-05-20                    │
│ Using Python 3.11.3. Type "help()" for help.                       │
sage: var('f x e n a p h g b c d q')
sage: anti=integrate(x*sec(a+b*log(c*x^n))^2,x, algorithm="maxima");
sage: latex(anti)

Not invertible Error: Bad Argument Value
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit

The result of maxima in this case has unresolved integrate inside it. This is the actual antiderivative

sage: integrate(x*sec(a+b*log(c*x^n))^2,x, algorithm="maxima")
2*(x^2*cos(2*b*log(x^n) + 2*a)*sin(2*b*log(c)) + x^2*cos(2*b*log(c))*sin(2*b*log(x^n) + 2*a) - 2*(2*b^2*n^2*cos(2*b*log(c))*cos(2*b*log(x^n) + 2*a) - 2*b^2*n^2*sin(2*b*log(c))*sin(2*b*log(x^n) + 2*a) + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*cos(2*b*log(x^n) + 2*a)^2 + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*sin(2*b*log(x^n) + 2*a)^2 + b^2*n^2)*integrate((x*cos(2*b*log(x^n) + 2*a)*sin(2*b*log(c)) + x*cos(2*b*log(c))*sin(2*b*log(x^n) + 2*a))/(2*b^2*n^2*cos(2*b*log(c))*cos(2*b*log(x^n) + 2*a) - 2*b^2*n^2*sin(2*b*log(c))*sin(2*b*log(x^n) + 2*a) + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*cos(2*b*log(x^n) + 2*a)^2 + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*sin(2*b*log(x^n) + 2*a)^2 + b^2*n^2), x))/(2*b*n*cos(2*b*log(c))*cos(2*b*log(x^n) + 2*a) + (b*cos(2*b*log(c))^2 + b*sin(2*b*log(c))^2)*n*cos(2*b*log(x^n) + 2*a)^2 - 2*b*n*sin(2*b*log(c))*sin(2*b*log(x^n) + 2*a) + (b*cos(2*b*log(c))^2 + b*sin(2*b*log(c))^2)*n*sin(2*b*log(x^n) + 2*a)^2 + b*n)

Notice there is an integrate(...) command inside the above output. So maxima found it can't integrate that part and left the integrate command there. So I do not want this to be evaluated. I just need the latex conversion done keeping integrate as "\int{.....}" without evaluating.

This happens because sage was calling

integrate((x*cos(2*b*log(x^n) + 2*a)*sin(2*b*log(c)) + x*cos(2*b*log(c))*sin(2*b*log(x^n) + 2*a))/(2*b^2*n^2*cos(2*b*log(c))*cos(2*b*log(x^n) + 2*a) - 2*b^2*n^2*sin(2*b*log(c))*sin(2*b*log(x^n) + 2*a) + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*cos(2*b*log(x^n) + 2*a)^2 + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*sin(2*b*log(x^n) + 2*a)^2 + b^2*n^2), x)
Not invertible Error: Bad Argument Value
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Not invertible Error: Bad Argument Value
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
and these go on forever it seems

And getting these error. The strange thing, is calling the above exact command inside Maxima just returns the input back, without these errors!  

So these errors are generated by sagemath and not by maxima from the latex() command.

I am using Maxima 5.47 with sagemath 10.0

>which maxima
/usr/bin/maxima
>maxima --version
;;; Loading #P"/usr/lib/ecl-21.2.1/sb-bsd-sockets.fas"
;;; Loading #P"/usr/lib/ecl-21.2.1/sockets.fas"
Maxima 5.47.0
>
And

>which sage
/home/me/TMP/sage-10.0/sage
>sage --version
SageMath version 10.0, Release Date: 2023-05-20
>

Thanks
--Nasser

Dima Pasechnik

unread,
Jul 26, 2023, 7:14:39 AM7/26/23
to sage-...@googlegroups.com
On Wed, Jul 26, 2023 at 6:17 AM 'Nasser M. Abbasi' via sage-devel
<sage-...@googlegroups.com> wrote:
>
> Sometimes when calling integrate using algorithm such as maxima, it returns result which is not fully resolved but still have an integrate inside it.
>
> Next, when calling latex() on the anti-derivative this cause problems, because sage tried to calls maxima again on the integrate command inside the result.
>
> Is there a way to make latex() just convert the result without calling integrate again?
> This results in problems like the following
>
> sage: latex(anti)
> Not invertible Error: Bad Argument Value
> Undef/Unsigned Inf encountered in limit
> Undef/Unsigned Inf encountered in limit
>
> Here is an example

for this integral, I think you'd like to add

assume(n,"integer")
assume(n>0)

Do you really want to work in the complex domain, not also

assume(x>0)
assume(c>0)

?

Anyhow, this looks like a Maxima bug to me.

Dima
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/43c4f68d-4be8-4cbc-a68a-d54321969ab7n%40googlegroups.com.

David Roe

unread,
Jul 26, 2023, 10:34:34 AM7/26/23
to sage-...@googlegroups.com
Even if adding some assumptions makes this particular integral evaluate fully, the underlying problem may still show up in other cases.  I haven't tracked it down fully (and probably won't spend more time on this), but the error messages are coming from Sage's interface to Giac, via this function in expression.pyx:

cpdef _latex_Expression(x):
    return char_to_str(GEx_to_str_latex(&(<Expression>x)._gobj))

I think it would be reasonable for Sage to do what the original poster suggested, and turn integrals into \int in latex rather than trying to evaluate them.
David

Nasser M. Abbasi

unread,
Jul 26, 2023, 6:00:00 PM7/26/23
to sage-devel

" I think it would be reasonable for Sage to do what the original poster suggested, and turn integrals into \int in latex rather than trying to evaluate them."

Yes, this is what I am asking. If there is a way to prevent evaluation of an expression being passed to latex() command.  

In Mathematica for example, this is done by wrapping the expression in HoldForm, like this

TeXForm[Integrate[Sin[x], x]]
           -\cos (x)

TeXForm[HoldForm[Integrate[Sin[x], x]]]
      \int \sin (x) \, dx

In Maple this is done by wrapping the expression by '  '  like this

latex(int(sin(x),x))
           -\cos \! \left(x \right)

latex('int(sin(x),x)')
       \int \sin \! \left(x \right)d x

I just wanted to know how to do the same in sagemath. Many times there is a need to obtain the latex of an expression without it being evaluated,

--Nasser

TB

unread,
Jul 27, 2023, 5:54:11 AM7/27/23
to sage-...@googlegroups.com
There is the hold context for symbolic expressions:

sage: with hold:
....: latex(integrate(sin(x), x))
....:
\int \sin\left(x\right)\,{d x}

The short docs are at
https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html#sage.symbolic.expression.hold_class
but it looks like it does not work well together with
algorithm="maxima". Quick search about this gives the tickets #10035,
#10169, #23304 and #31554.

Even without the hold context, there is the argument "hold":
sage: integrate(sin(x), x)
-cos(x)
sage: integrate(sin(x), x, hold=True)
integrate(sin(x), x)
sage: latex(_)
\int \sin\left(x\right)\,{d x}
sage: integrate(sin(x), x, algorithm="maxima", hold=True) # Bug?
-cos(x)

Regards,
TB
> https://groups.google.com/d/msgid/sage-devel/43c4f68d-4be8-4cbc-a68a-d54321969ab7n%40googlegroups.com <https://groups.google.com/d/msgid/sage-devel/43c4f68d-4be8-4cbc-a68a-d54321969ab7n%40googlegroups.com>.
>
> --
> You received this message because you are subscribed to the
> Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to sage-devel+...@googlegroups.com.
>
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-devel/CAAWYfq30oD1XdVmcCNZtkZfXVAwOKZimb%3D59hMi7ug6FpYYTFA%40mail.gmail.com <https://groups.google.com/d/msgid/sage-devel/CAAWYfq30oD1XdVmcCNZtkZfXVAwOKZimb%3D59hMi7ug6FpYYTFA%40mail.gmail.com>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sage-devel+...@googlegroups.com
> <mailto:sage-devel+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-devel/f3acc34c-a9eb-475f-bd64-710c3241af91n%40googlegroups.com <https://groups.google.com/d/msgid/sage-devel/f3acc34c-a9eb-475f-bd64-710c3241af91n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Nasser M. Abbasi

unread,
Jul 27, 2023, 6:05:12 AM7/27/23
to sage-devel
Thanks TB; I did not know about the hold context but like you said, it does not work here:

sage: var('f x e n a p h g b c d q')
sage: anti=integrate(x*sec(a+b*log(c*x^n))^2,x, algorithm="maxima")
sage: with hold:
....:          latex(anti)
....:
Not invertible Error: Bad Argument Value
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
loops forever...

The strange thing it works for other expressions

sage: with hold:
....:          latex(integrate(sin(x),x))
....:
\int \sin\left(x\right)\,{d x}

I have no idea what is the difference. How does it know that the first result was even
generated by Maxima for it to make any difference? 

Anyway, I changed my test program to avoid calling latex() for result that failed to avoid this problem.

--Nasser

Dima Pasechnik

unread,
Jul 27, 2023, 7:14:09 AM7/27/23
to sage-devel


On Thu, 27 Jul 2023, 11:05 'Nasser M. Abbasi' via sage-devel, <sage-...@googlegroups.com> wrote:
Thanks TB; I did not know about the hold context but like you said, it does not work here:

sage: var('f x e n a p h g b c d q')
sage: anti=integrate(x*sec(a+b*log(c*x^n))^2,x, algorithm="maxima")
sage: with hold:
....:          latex(anti)
....:
Not invertible Error: Bad Argument Value
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
Undef/Unsigned Inf encountered in limit
loops forever...

Does "with hold" work if you strip everything apart from integrate() in anti ?

Nasser M. Abbasi

unread,
Jul 27, 2023, 7:49:37 AM7/27/23
to sage-devel
" Does "with hold" work if you strip everything apart from integrate() in anti ?"

Yes.

sage
│ SageMath version 10.0, Release Date: 2023-05-20                    │
│ Using Python 3.11.3. Type "help()" for help.                       │
sage: var('f x e n a p h g b c d q')
sage: anti=integrate(x*sec(a+b*log(c*x^n))^2,x, algorithm="maxima")

sage: anti

2*(x^2*cos(2*b*log(x^n) + 2*a)*sin(2*b*log(c)) + x^2*cos(2*b*log(c))*sin(2*b*log(x^n) + 2*a) - 2*(2*b^2*n^2*cos(2*b*log(c))*cos(2*b*log(x^n) + 2*a) - 2*b^2*n^2*sin(2*b*log(c))*sin(2*b*log(x^n) + 2*a) + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*cos(2*b*log(x^n) + 2*a)^2 + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*sin(2*b*log(x^n) + 2*a)^2 + b^2*n^2)*integrate((x*cos(2*b*log(x^n) + 2*a)*sin(2*b*log(c)) + x*cos(2*b*log(c))*sin(2*b*log(x^n) + 2*a))/(2*b^2*n^2*cos(2*b*log(c))*cos(2*b*log(x^n) + 2*a) - 2*b^2*n^2*sin(2*b*log(c))*sin(2*b*log(x^n) + 2*a) + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*cos(2*b*log(x^n) + 2*a)^2 + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*sin(2*b*log(x^n) + 2*a)^2 + b^2*n^2), x))/(2*b*n*cos(2*b*log(c))*cos(2*b*log(x^n) + 2*a) + (b*cos(2*b*log(c))^2 + b*sin(2*b*log(c))^2)*n*cos(2*b*log(x^n) + 2*a)^2 - 2*b*n*sin(2*b*log(c))*sin(2*b*log(x^n) + 2*a) + (b*cos(2*b*log(c))^2 + b*sin(2*b*log(c))^2)*n*sin(2*b*log(x^n) + 2*a)^2 + b*n)

#copy the integrate part only from the above and paste it in the following command

sage: with hold:
....:         latex(integrate((x*cos(2*b*log(x^n) + 2*a)*sin(2*b*log(c)) + x*cos(2*b*log(c))*sin(2*b*log(x^n) + 2*a))/(2*b^2*n^2*cos(2*b*log(c))*cos(2*b*log(x^n) + 2*a) - 2*b^2*n^2*sin(2*b*log(c))*sin(2*b*l
....: og(x^n) + 2*a) + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*cos(2*b*log(x^n) + 2*a)^2 + (b^2*cos(2*b*log(c))^2 + b^2*sin(2*b*log(c))^2)*n^2*sin(2*b*log(x^n) + 2*a)^2 + b^2*n^2), x))
....:

\int \frac{x \cos\left(2 \, b \log\left(x^{n}\right) + 2 \, a\right) \sin\left(2 \, b \log\left(c\right)\right) + x \cos\left(2 \, b \log\left(c\right)\right) \sin\left(2 \, b \log\left(x^{n}\right) + 2 \, a\right)}{2 \, b^{2} n^{2} \cos\left(2 \, b \log\left(c\right)\right) \cos\left(2 \, b \log\left(x^{n}\right) + 2 \, a\right) - 2 \, b^{2} n^{2} \sin\left(2 \, b \log\left(c\right)\right) \sin\left(2 \, b \log\left(x^{n}\right) + 2 \, a\right) + {\left(b^{2} \cos\left(2 \, b \log\left(c\right)\right)^{2} + b^{2} \sin\left(2 \, b \log\left(c\right)\right)^{2}\right)} n^{2} \cos\left(2 \, b \log\left(x^{n}\right) + 2 \, a\right)^{2} + {\left(b^{2} \cos\left(2 \, b \log\left(c\right)\right)^{2} + b^{2} \sin\left(2 \, b \log\left(c\right)\right)^{2}\right)} n^{2} \sin\left(2 \, b \log\left(x^{n}\right) + 2 \, a\right)^{2} + b^{2} n^{2}}\,{d x}

No error.

Dima Pasechnik

unread,
Jul 27, 2023, 10:55:22 AM7/27/23
to sage-devel
it looks like "with hold" can only be applied to some forms of expressions.
Perhaps it needs an extra pair of () ?

Reply all
Reply to author
Forward
0 new messages