" 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.
--Nasser
On Thursday, July 27, 2023 at 6:14:09 AM UTC-5 Dima Pasechnik wrote:
>
>
> On Thu, 27 Jul 2023, 11:05 'Nasser M. Abbasi' via sage-devel, <
> [email protected]> 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 ?
>
>
>> 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
>>
>> On Thursday, July 27, 2023 at 4:54:11 AM UTC-5 TB wrote:
>>
>>> 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
>>>
>>> On 27/07/2023 0:59, 'Nasser M. Abbasi' via sage-devel wrote:
>>> >
>>> > " 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
>>> >
>>> > On Wednesday, July 26, 2023 at 9:34:34 AM UTC-5 David Roe wrote:
>>> >
>>> > 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
>>> >
>>> > On Wed, Jul 26, 2023 at 7:14 AM Dima Pasechnik <[email protected]>
>>> wrote:
>>> >
>>> > On Wed, Jul 26, 2023 at 6:17 AM 'Nasser M. Abbasi' via sage-devel
>>> > <[email protected]> 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
>>> >
>>> >
>>> > >
>>> > > >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
>>> > >
>>> > > --
>>> > > 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 [email protected].
>>> > > To view this discussion on the web visit
>>> >
>>> 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 [email protected].
>>> >
>>> > 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 [email protected]
>>> > <mailto:[email protected]>.
>>> > 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>.
>>>
>>>
>>>
>>> --
>> 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 [email protected].
>>
> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/sage-devel/37579431-23f4-4ae6-9880-675efe0e952fn%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/sage-devel/37579431-23f4-4ae6-9880-675efe0e952fn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sage-devel/94ec170a-3809-4410-8a4a-b3cbee37ba44n%40googlegroups.com.