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()
    4
    
  • 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
    

    https://ask.sagemath.org/question/47064/how-to-turn-the-function-into-expression/

  • Matrix confusion:

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

    https://ask.sagemath.org/question/10457/arithmetic-with-matrices-of-formal-functions/

  • List confusion:

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

    https://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/

  • Derivative confusion (and argument confusion):

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

    https://ask.sagemath.org/question/9842/the-difference-between-fx3-and-f3-of-callable-symbolic-expression-f/

  • 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
    

    https://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/

  • 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
    

    https://ask.sagemath.org/question/10782/symbolic-functions-without-named-variables/

  • 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))