Ask Your Question
1

Defining functions acting on matrix elements?

asked 2017-08-14 21:58:15 +0100

Asker gravatar image

Hi guys, how can I define a function able to act on the elements of a matrix, with the matrix being the input to the function? I would like the function to be a callable symbolic expression, which I hear can be differentiated and so on, whereas other entities cannot.

Here's a sample of what I mean:

f(M) = M[0,0]+M[1,1]

that is, the user will ensure that M is correctly defined as a matrix of size at least 2x2 before being passed to f(M), but at this stage the symbol M is intended just as a dummy variable, similar to z in a definition like g(z) = conjugate(z). Unfortunately, my code does not seem to work...

Thank you for your suggestions!

edit retag flag offensive close merge delete

Comments

Using the pure pythonical way, we can define for instance:

def tr(M):
    return M[0,0]+M[1,1]

Sample code of usage:

sage: tr( matrix( 2,2, [ 1+x, 1-x, 1-x, x^2 ] ) )
x^2 + x + 1
sage: tr( matrix( 2,2, [ 1+x, 1-x, 1-x, x^2 ] ) ).diff()
2*x + 1

Is this ok?

dan_fulea gravatar imagedan_fulea ( 2017-08-14 23:59:11 +0100 )edit

For what I need in this particular case, yes, it is fine.

Since I am new to Sage, however, I still wonder if there are cases when a callable symbolic expression acting on single elements of an input matrix is required, and how to solve the problem.

Many thanks, Dan.

Asker gravatar imageAsker ( 2017-08-15 09:57:19 +0100 )edit
1

for "element-wise" operations, the method apply_map is useful. see for example this answer. you may post the full code that didn't work, to get more precise help.

mforets gravatar imagemforets ( 2017-08-15 17:25:27 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-08-19 16:46:42 +0100

ndomes gravatar image
sage: M = matrix(2,2,var('a b c d'))
sage: M;  M.parent(); M(a=5)

[a b]
[c d]
Full MatrixSpace of 2 by 2 dense matrices over Symbolic Ring
[5 b]
[c d]

We can use substitution M(a=5) (looking similar to a function call). What happens if we try to use M as variable of a function?

sage: M = matrix(2,2,var('a b c d'))
sage: print M.parent(); print
sage: f(M) = M.transpose()

The error message indicates that the type of M has changed.

Full MatrixSpace of 2 by 2 dense matrices over Symbolic Ring

Traceback (click to the left of this block for traceback)
...
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'transpose'

M is no longer a matrix over SR but a symbolic expression.

sage: M.parent()

Symbolic Ring

When defining a symbolic function with a variable M, this M becomes (implicitly) a member of SR overwriting the previous definition of M. After redefining M as matrix:

sage: A = M[0,0] + M[1,1]
sage: A; A(a=5,b=3,c=0,d=-2)

a + d
3
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-08-14 21:58:15 +0100

Seen: 913 times

Last updated: Aug 19 '17