meaning of 'ctx' in function definitions

2,888 views
Skip to first unread message

Ken

unread,
Feb 24, 2012, 7:53:11 PM2/24/12
to mpmath
Novice question. I've been looking at the definitions of functions.
What does 'ctx' mean and how is it used? When actually calling the
function in a script, the ctx variable isn't part of the function
call. Can someone explain? Thanks

00383 def qr_solve(ctx, A, b, norm=None, **kwargs):
"""
Ax = b => x, ||Ax - b||

Solve a determined or overdetermined linear equations system
and
calculate the norm of the residual (error).
QR decomposition using Householder factorization is applied,
which gives very
accurate results even for ill-conditioned matrices. qr_solve
is twice as
efficient.
"""
if norm is None:
norm = ctx.norm
prec = ctx.prec
try:
ctx.prec += 10

Fredrik Johansson

unread,
Feb 28, 2012, 3:22:16 AM2/28/12
to mpm...@googlegroups.com
On Sat, Feb 25, 2012 at 01:53, Ken <ken....@sbcglobal.net> wrote:
> Novice question.  I've been looking at the definitions of functions.
> What does 'ctx' mean and how is it used?  When actually calling the
> function in a script, the ctx variable isn't part of the function
> call.  Can someone explain?  Thanks

Most public functions are actually methods of a 'context' object which
is passed as the first parameter (ctx). The context object stores the
precision, cached data, and a few other things. It also defines
conversions so that the same high-level code can be used for several
different base types (mpf, mpfs in Sage, intervals, Python floats) by
switching contexts.

The default context is called 'mp'. You can call most functions as
mpmath.mp.foo(). The top-level function mpmath.foo() is just an alias
for this.

Fredrik

Ken

unread,
Mar 2, 2012, 12:37:30 AM3/2/12
to mpmath
Thanks, I think I'm beginning to understand. Let's say I write my own
module (M1) containing a function (F1) that uses mpmath functions.
I've figured out that the definition of F1 has to include 'ctx' as a
parameter and that any mpmath function used in F1 needs the ctx
prefix. When I call F1 (after importing M1), I need to explicitly
pass the context (e.g. M1.F1(mp,A)). But, I don't have to explicitly
pass the context to built-in mpmath functions. In other words, the
definition of a built-in mpmath function might be norm(ctx, A) but I
only pass 1 parameter. With my user defined function I have to pass
an extra parameter that's not required for the built-in function.
This seems inconsistent but I presume the difference has something to
do with the fact I've already imported mpmath in the first case. Am I
understanding this correctly?

Fredrik Johansson

unread,
Mar 2, 2012, 7:04:23 AM3/2/12
to mpm...@googlegroups.com

You can import the mp object as a global variable (or locally in a
function). There's no need to add it as an argument to your functions,
unless you specifically want to support multiple contexts.

from mpmath import mp

def F1(A):
return mp.norm(A) + 37

or simply:

from mpmath import norm

def F1(A):
return norm(A) + 37

Is there a specific reason why you need to pass the context object
explicitly to your functions? I might have misunderstood.

Fredrik

Ken

unread,
Mar 3, 2012, 12:41:04 AM3/3/12
to mpmath
> Is there a specific reason why you need to pass the context object
> explicitly to your functions?

No, I'm just a novice trying to figure out how things work. I just
figured (incorrectly) that since built-in functions use 'ctx' in the
function definition (still somewhat baffling), user-defined functions
in modules would need the same. Seems odd to import mpmath in the
user-defined module when it's already done in the calling module, but
if that's how it works, ok. I guess my brain is cluttered with too
many Fortran cobwebs. Given there are multiple ways (pass context as
a parameter, import module or certain functions), is there a 'best
practice' guideline from the standpoint of memory usage, execution
time, etc., or simply doesn't matter?
Reply all
Reply to author
Forward
0 new messages