Ask Your Question

Revision history [back]

You have discovered the horror of the syntax for callable symbolic expressions.

  • Numbers confusion:

    sage: f(x) = x^2
    sage: f(2).factor()
  • Polynomial confusion:

    sage: R.<z,w> = PolynomialRing(QQ)
    sage: f(x) = x^2
    sage: f(z+w).coefficient({z : 1})
    TypeError: no canonical coercion from <type 'dict'> to Symbolic Ring

  • Matrix confusion:

    sage: B(x) = matrix([[x, 0], [0, 0]])
    sage: B(12)
    [x 0]
    [0 0]

  • List confusion:

    sage: f(x) = [x,x]
    sage: f(2).parent()
    Vector space of dimension 2 over Symbolic Ring

  • Derivative confusion (and argument confusion):

    sage: f(x) = x.derivative()
    sage: f(x^2)
    sage: f(y) = y.derivative(x)
    sage: f(x^2)

  • Matrix argument confusion:

    sage: f(x) = x^2
    sage: f(2*identity_matrix(2))
    TypeError: no canonical coercion from Full MatrixSpace of 2 by 2 dense matrices over Integer Ring to Callable function ring with argument x

  • Adding confusion:

    sage: f(x) = x^2
    sage: g(x) = x^2
    sage: var('t')
    sage: h(t) = t^2
    sage: f+g
    x |--> 2*x^2
    sage: f+h
    (t, x) |--> t^2 + x^2

  • Non-symbolic function confusion (your question)

I've seen this too many times now. I went ahead and opened a ticket for it: #28434: Syntax for callable symbolic expressions causes too much confusion.

You should define your function in the way you did, or alternatively with a lambda:

 tau = lambda n: len(divisors(n))