Digamma Function in 4.1.1

94 views
Skip to first unread message

The_Fool

unread,
Sep 18, 2009, 6:47:45 PM9/18/09
to sage-support
While working with the derivative of the Gamma function, the digamma
function is obviously involved. The sage "diff" function does show Γ
'(x) == Γ(x)ψ(x) like it should, however, the digamma function (called
psi in sage) is not defined whenever I try to do anything with it. It
seems as if only the output of "diff" can use this function.

Is there any way to permanently keep this function defined so I can
plot with it, conduct symbolic and numeric calculations with it, and
so on like any other function?

Burcin Erocal

unread,
Sep 19, 2009, 11:33:28 AM9/19/09
to sage-s...@googlegroups.com
On Fri, 18 Sep 2009 15:47:45 -0700 (PDT)
The_Fool <maste...@yahoo.com> wrote:

> While working with the derivative of the Gamma function, the digamma
> function is obviously involved. The sage "diff" function does show Γ
> '(x) == Γ(x)ψ(x) like it should, however, the digamma function (called
> psi in sage) is not defined whenever I try to do anything with it. It
> seems as if only the output of "diff" can use this function.

This is because GiNaC (the library Sage uses for symbolic expressions)
knows about the digamma function, but Sage doesn't.

> Is there any way to permanently keep this function defined so I can
> plot with it, conduct symbolic and numeric calculations with it, and
> so on like any other function?

Defining a function psi, similar to the way arctan2 is defined in line
422 of sage/functions/trig.py should fix this.


I opened a ticket:

http://trac.sagemath.org/sage_trac/ticket/6961


Thanks.

Burcin

The_Fool

unread,
Sep 20, 2009, 4:50:38 PM9/20/09
to sage-support


On Sep 19, 10:33 am, Burcin Erocal <bur...@erocal.org> wrote:
> On Fri, 18 Sep 2009 15:47:45 -0700 (PDT)
>
> Defining a function psi, similar to the way arctan2 is defined in line
> 422 of sage/functions/trig.py should fix this.

What is the location from Sage's root directory of the files used by
Sage? I found several locations of sage/functions/trig.py.

I found in the pdf of the reference manual that psi can be numerically
calculated by using PARI (for example, pari(2.2342).psi()), but it
still cannot be used symbolically or used to plot until the function
psi is defined.

The_Fool

unread,
Sep 21, 2009, 10:14:18 PM9/21/09
to sage-support
Does anyone know which files are used for Sage? I need to know where
from Sage's root directory to make this modification.

Jason Grout

unread,
Sep 21, 2009, 10:37:53 PM9/21/09
to sage-s...@googlegroups.com
The_Fool wrote:
> Does anyone know which files are used for Sage? I need to know where
> from Sage's root directory to make this modification.


I have my root sage installation in ~/sage

The files for me are in ~/sage/devel/sage/

So the trig.py file mentioned previously is in
~/sage/devel/sage/sage/functions/trig.py

After modifying a file, do:

sage -br

which copies the modified files to the build directory and rebuilds Sage
and then runs Sage. To just rebuild, just do "sage -b"

Jason

--
Jason Grout

The_Fool

unread,
Sep 22, 2009, 1:29:33 AM9/22/09
to sage-support
On Sep 21, 9:37 pm, Jason Grout <jason-s...@creativetrax.com> wrote:
> After modifying a file, do:
>
> sage -br
>
> which copies the modified files to the build directory and rebuilds Sage
> and then runs Sage.  To just rebuild, just do "sage -b"
>
I am currently using the Ubuntu 9.04 64-bit binary. Using ./sage -br
or ./sage -b causes an error and prevents Sage from working
afterward. Starting Sage normally also no longer works after
rebuilding.
Using ./sage -br ends with this error:

ImportError: libcsage.so: cannot open shared object file: No such file
or directory
Error importing ipy_profile_sage - perhaps you should run %upgrade?
WARNING: Loading of ipy_profile_sage failed.

<ERROR: name 'sage_prompt' is not defined>


Typing %upgrade tells me to delete a hidden file and retry the
command. Sage still doesn't work after I do. The same situation
occurred after I reinstalled Sage, ran the program, upgraded, modified
a file, and rebuilt again. I may just download the source code, make
the modification, and completely build Sage for my system.

Robert Bradshaw

unread,
Sep 22, 2009, 1:42:46 AM9/22/09
to sage-s...@googlegroups.com

sage -b is still a bit rocky for binary downloads. (It shouldn't be,
it's a known bug.) I'd recommend compiling from source, it's not that
hard (though does take a while) and then you'll probably have a lot
fewer issues developing.

- Robert

Burcin Erocal

unread,
Sep 22, 2009, 5:29:23 AM9/22/09
to sage-s...@googlegroups.com
On Mon, 21 Sep 2009 22:29:33 -0700 (PDT)
The_Fool <maste...@gmail.com> wrote:

<snip info on how sage -br renders a binary install useless>


> Typing %upgrade tells me to delete a hidden file and retry the
> command. Sage still doesn't work after I do. The same situation
> occurred after I reinstalled Sage, ran the program, upgraded, modified
> a file, and rebuilt again. I may just download the source code, make
> the modification, and completely build Sage for my system.

As a workaround you can also just implement the function in a .py file
somewhere and use the "load" or "attach" commands to make it available
from Sage.

If you go this way, it would be great if you upload your implementation
somewhere, so someone can turn it into a patch for the Sage library.


Thanks.

Burcin

P.S. Sorry for not replying earlier. I'm very busy trying to finish
things before I leave for vacation next week.

Message has been deleted
Message has been deleted

The_Fool

unread,
Sep 22, 2009, 9:47:50 PM9/22/09
to sage-support
I managed to create the symbolic polygamma function as psi(order,x).
Psi is limited in what it can do, though. I can get it to grab
special values from Maxima's or GiNaC's table, but I still cannot get
it to approximate any value of any integer order. It can be
differentiated, but not integrated. It seems that this is a
limitation of Maxima and GiNaC, not Sage.

kcrisman

unread,
Sep 22, 2009, 11:04:08 PM9/22/09
to sage-support
Can you create a special _integral_ (or _integrate_, I forget which)
method to do this? You can also ask for it to be LaTeXed
appropriately, etc.; there are lots of good examples in the files
mentioned earlier in this thread, though if Ginac knows about it,
there may already be a representation. See also the upgraded Pynac
package, trac ticket #6993, which you may want to work off of.

- kcrisman

Burcin Erocal

unread,
Sep 23, 2009, 7:26:38 AM9/23/09
to sage-s...@googlegroups.com

You're right, looking at the functions py_psi() and py_psi2() in
sage/symbolic/pynac.pyx (I'm not giving line numbers since my file is
heavily patched.), I see that they just raise NotImplementedError.

You could have a go at implementing these functions using the psi
function from mpmath:

http://mpmath.googlecode.com/svn/tags/0.13/doc/build/functions/gamma.html#mpmath.functions.psi

There is an example of how to call mpmath in the function py_li of
sage/symbolic/pynac.pyx.


If you post your code I can give some more pointers on how to use the
pynac library better. For now, if you derived you class from
sage.symbolic.function.PrimitiveFunction, I suggest not using the approx
option, and using the __call__ = SFunction.__call__ line to bypass the
__call__ method implemented in that class. This was done by the arctan2
function in sage/functions/trig.py which I gave as an example.


I will not have internet access for a few days starting tomorrow. I'll
try to catch up with e-mails once I'm back.


Thanks.

Cheers,
Burcin

The_Fool

unread,
Sep 24, 2009, 12:07:04 PM9/24/09
to sage-support
I'm afraid I just don't know enough about this programming language
yet to design this function before the final version of 4.1.2 is
released. It seems to ignore any variation of a custom _integrate_.
Here is what I have achieved so far if someone is interested in
helping or further developing this function:

class Function_psi(PrimitiveFunction):
def __init__(self):
"""
Some description here.
"""
PrimitiveFunction.__init__(self, "psi", nargs=2,
latex=r'\psi',
conversions=dict(ginac='psi'))

__call__ = SFunction.__call__


polygamma = Function_psi()

def psi(a,b="default"):
if b=="default":
return polygamma(0,a)
else:
return polygamma(a,b)

The definition at the bottom is necessary since Sage simplifies psi
(0,x) to psi(x). Optional arguments are accepted only after the
required arguments, therefore, I created this to convert back.
Reply all
Reply to author
Forward
0 new messages