Ask Your Question

Revision history [back]

In 2*f(n) you are not multiplying 2 with f and then applying it to n, but you are evaluating f(n) and then multiplying 2 with f(n).

In Symbolic functions can be multiplied by two, Python functions cannot.

sage: f(n) = n
sage: g = lambda n: n
sage: 2 * f
n |--> 2*n
sage: 2 * g
...
TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<type 'function'>'

Note also that in 2*f(n) you are is not multiplying 2 with times f and then applying it applied to n, , but you are evaluating f(n) and then multiplying 2 with times f(n).

Symbolic functions can be multiplied by two, Python functions cannot.

sage: f(n) = n
sage: g = lambda n: n
sage: 2 * f
n |--> 2*n
sage: 2 * g
...
TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<type 'function'>'

Note also that in 2*f(n) is not 2 times f applied to n, but 2 times f(n).

Related: some common issues with functions.

Symbolic functions can be multiplied by two, Python functions cannot.

sage: f(n) = n
sage: g = lambda n: n
sage: 2 * f
n |--> 2*n
sage: 2 * g
...
TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<type 'function'>'

Note also that in 2*f(n) is not 2 times f applied to n, but 2 times f(n).

Related: some common issues with functions.

Edit.

The equivalent of Maple's f := n -> n in Sage would be f(n) = n.

sage: two(n) = 2
sage: f(n) = n
sage: (two*f)(x)
sage: (2*f)(x)
sage: 2 * (f(x))

Symbolic functions can be multiplied by two, Python functions cannot.

sage: f(n) = n
sage: g = lambda n: n
sage: 2 * f
n |--> 2*n
sage: 2 * g
...
TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<type 'function'>'

Note also that in 2*f(n) is not 2 times f applied to n, but 2 times f(n).

Related: some common issues with functions.

Edit.

The equivalent of Maple's f := n -> n in Sage would be f(n) = n.

Let us define

sage: two(n) = 2
sage: f(n) = n
sage: (two*f)(x)
sage: (2*f)(x)
sage: 2 * (f(x))

Then:

sage: [(two*f)(n) for n in (0 .. 4)]
[0, 2, 4, 6, 8]
sage: [(2*f)(n) for n in (0 .. 4)]
[0, 2, 4, 6, 8]
sage: [2*f(n) for n in (0 .. 4)]
[0, 2, 4, 6, 8]