Looking at http://trac.sagemath.org/sage_trac/ticket/3980, I formed
the opinion that a lot of times where .variables() or .arguments() is
used, it may be a mistake.

On that ticket, it was pointed out that find_root(sin,-1,1) throws an
error.  Maybe this is a bug, maybe not, but in any case,
plot(sin,-1,1) certainly works, and I'd hope for consistency between
the two.  The reason find_root throws an error in this case is because
it asks sin what its variables are, finds there are none, and
therefore assumes sin is a constant.  But find_root doesn't need to
ask an expression about its variables--rather it just needs to know
whether the expression returns a number when called with one argument.

I suspect this is true more generally.  Hopefully I will back up this
assertion by showing I can use it to clean up some code, but I thought
I'd share now to see if anyone had any thoughts.

One additional consideration is that it is useful in this case to know
whether an expression is constant, as a performance consideration.
The regular algorithm works (slowly) on constant input, but it's
faster if we can just short circuit in the case of a constant.
Clearly, asking an expression for its variables is not the right way
to find out whether it's constant.  How about something like this
instead:

def is_constant(f):
    try:
        return f.isConstant()
    except AttributeError:
        if not hasattr(f,'__call__'):
            return True
        x = sage.calculus.calculus.var('x')
        try:
            return x not in f(x).variables()
        except (AttributeError, TypeError, ValueError), e:
            # Kludge to work around seemingly broke behavior of
SymbolicComposition
            if e.message == 'the number of arguments must be less than
or equal to 0':
                return True
            # At this point, we don't know if the expression is
constant
            return None # or throw an error

sage: sin.variables()
()
sage: is_constant(sin)
False
sage: is_constant(sin(3))
True
sage: is_constant(x)
False
sage: is_constant(x - x)
True
sage: is_constant(lambda x: x + 7)
False
# Could we do better with this case without messing anything else up?
sage: is_constant(lambda x: 1) is None
True

Regards,

JM

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to