5

I would like to know why these two "programs" produce different output

    f(x)=x^2
    f(90).mod(7)

and

    def f(x):
        return(x^2)
    f(90).mod(7)

Thanks

mathlife
  • 171
  • 1
  • 3

1 Answers1

5

Great question! Let's take a deeper look at the functions in question.

f(x)=x^2
def g(x):
    return(x^2)

print type(g(90))
print type(f(90))

This yields

<type 'sage.rings.integer.Integer'>
<type 'sage.symbolic.expression.Expression'>

So what you are seeing is the difference between a symbolic function defined with the f(x) notation and a Python function using the def keyword. In Sage, the former has access to a lot of stuff (e.g. calculus) that plain old Sage integers won't have.

What I would recommend in this case, just for what you need, is

sage: a = f(90)
sage: ZZ(a).mod(7)
1

or actually the possibly more robust

sage: mod(a,7)
1

Longer explanation.

For symbolic stuff, mod isn't what you think. In fact, I'm not sure it will do anything (see the documentation for mod to see how to use it for polynomial modular work over ideals, though). Here's the code (accessible with x.mod??, documentation accessible with x.mod?):

    from sage.rings.ideal import is_Ideal
    if not is_Ideal(I) or not I.ring() is self._parent:
        I = self._parent.ideal(I)
        #raise TypeError, "I = %s must be an ideal in %s"%(I, self.parent())
    return I.reduce(self)

And it turns out that for generic rings (like the symbolic 'ring'), nothing happens in that last step:

return f 

This is why we need to, one way or another, ask it to be an integer again. See Trac 27401.

kcrisman
  • 4,374
  • 20
  • 41