ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 19 Aug 2017 09:46:42 -0500Defining functions acting on matrix elements?http://ask.sagemath.org/question/38524/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!Mon, 14 Aug 2017 14:58:15 -0500http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/Comment by mforets for <p>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.</p>
<p>Here's a sample of what I mean:</p>
<pre><code>f(M) = M[0,0]+M[1,1]
</code></pre>
<p>that is, the user will ensure that <code>M</code> is correctly defined as a matrix of size at least 2x2 before being passed to <code>f(M)</code>, but at this stage the symbol <code>M</code> is intended just as a dummy variable, similar to <code>z</code> in a definition like <code>g(z) = conjugate(z)</code>. Unfortunately, my code does not seem to work...</p>
<p>Thank you for your suggestions!</p>
http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/?comment=38534#post-id-38534for "element-wise" operations, the method `apply_map` is useful. see for example [this answer](https://ask.sagemath.org/question/36530/symbolic-diagonalization-matrix-differentiation-and-the-wedge-product/). you may post the full code that didn't work, to get more precise help.Tue, 15 Aug 2017 10:25:27 -0500http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/?comment=38534#post-id-38534Comment by Asker for <p>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.</p>
<p>Here's a sample of what I mean:</p>
<pre><code>f(M) = M[0,0]+M[1,1]
</code></pre>
<p>that is, the user will ensure that <code>M</code> is correctly defined as a matrix of size at least 2x2 before being passed to <code>f(M)</code>, but at this stage the symbol <code>M</code> is intended just as a dummy variable, similar to <code>z</code> in a definition like <code>g(z) = conjugate(z)</code>. Unfortunately, my code does not seem to work...</p>
<p>Thank you for your suggestions!</p>
http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/?comment=38531#post-id-38531For 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.Tue, 15 Aug 2017 02:57:19 -0500http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/?comment=38531#post-id-38531Comment by dan_fulea for <p>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.</p>
<p>Here's a sample of what I mean:</p>
<pre><code>f(M) = M[0,0]+M[1,1]
</code></pre>
<p>that is, the user will ensure that <code>M</code> is correctly defined as a matrix of size at least 2x2 before being passed to <code>f(M)</code>, but at this stage the symbol <code>M</code> is intended just as a dummy variable, similar to <code>z</code> in a definition like <code>g(z) = conjugate(z)</code>. Unfortunately, my code does not seem to work...</p>
<p>Thank you for your suggestions!</p>
http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/?comment=38525#post-id-38525Using 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?Mon, 14 Aug 2017 16:59:11 -0500http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/?comment=38525#post-id-38525Answer by ndomes for <p>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.</p>
<p>Here's a sample of what I mean:</p>
<pre><code>f(M) = M[0,0]+M[1,1]
</code></pre>
<p>that is, the user will ensure that <code>M</code> is correctly defined as a matrix of size at least 2x2 before being passed to <code>f(M)</code>, but at this stage the symbol <code>M</code> is intended just as a dummy variable, similar to <code>z</code> in a definition like <code>g(z) = conjugate(z)</code>. Unfortunately, my code does not seem to work...</p>
<p>Thank you for your suggestions!</p>
http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/?answer=38583#post-id-38583 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
Sat, 19 Aug 2017 09:46:42 -0500http://ask.sagemath.org/question/38524/defining-functions-acting-on-matrix-elements/?answer=38583#post-id-38583