# Defining functions acting on matrix elements?

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!

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

Sample code of usage:

Is this ok?

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.

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.