# Why doesn't f(alpha) act like a function?

var('t,alpha,beta,gamma')

sage: i [ 0 -I] [-I 0]

sage: f(alpha) = cos(alpha)*i

sage: f(alpha) [ 0 -Icos(alpha)] [-Icos(alpha) 0]

sage: f(0) [ 0 -Icos(alpha)] [-Icos(alpha) 0]

edit retag close merge delete

To display blocks of code or error messages, skip a line above and below, and do one of the following (all give the same result):

• indent all code lines with 4 spaces
• select all code lines and click the "code" button (the icon with '101 010')
• select all code lines and hit ctrl-K

For instance, typing

If we define f by

def f(x, y, z):
return x * y * z

then f(2, 3, 5) returns 30 but f(2*3*5) gives:

TypeError: f() takes exactly 3 arguments (1 given)


produces:

If we define f by

def f(x, y, z):
return x * y * z


then f(2, 3, 5) returns 30 but f(2*3*5) gives:

TypeError: f() takes exactly 3 arguments (1 given)


( 2018-12-24 06:43:09 -0500 )edit

Sort by ยป oldest newest most voted

This is trac ticket #12075 (since 2011).

This way to define a function is syntactic sugar offered by Sage, but it doesn't yet work for matrix functions.

I suggest defining an ordinary Python function instead (using a lambda for brevity):

sage: var('alpha')
sage: i = matrix(SR, [[ 0, -I], [-I, 0]])
sage: f = lambda x: cos(x)*i
sage: f(0)
[ 0 -I]
[-I  0]
sage: f(alpha)
[            0 -I*cos(alpha)]
[-I*cos(alpha)             0]

more