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.Mon, 05 Jan 2015 17:08:51 -0600Using matrix elements as argumentshttp://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/I have a rather easy question, or so it would seem. I have looked for an answer but was unable to find one anywhere so I'm asking it here.
I am making a very simple iterative algorithm for which the input as well as the output at the end of every iteration is a vector (or matrix for that matter). What I want to do is use these elements as arguments for several functions during each of the iteration. So for example
x=var('x')
y=var('y')
z=matrix(2,1,[ [1],[1] ]
f=x^2+y^3
H=f.hessian()
Then what I would like to do is say
H(z[0],z[1])
or
H(z)
But no matter what I try I can't seem to get it to work. Ideas?Fri, 26 Nov 2010 03:44:53 -0600http://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/Answer by wonder for <p>I have a rather easy question, or so it would seem. I have looked for an answer but was unable to find one anywhere so I'm asking it here.</p>
<p>I am making a very simple iterative algorithm for which the input as well as the output at the end of every iteration is a vector (or matrix for that matter). What I want to do is use these elements as arguments for several functions during each of the iteration. So for example</p>
<pre><code>x=var('x')
y=var('y')
z=matrix(2,1,[ [1],[1] ]
f=x^2+y^3
H=f.hessian()
</code></pre>
<p>Then what I would like to do is say</p>
<pre><code>H(z[0],z[1])
</code></pre>
<p>or</p>
<pre><code>H(z)
</code></pre>
<p>But no matter what I try I can't seem to get it to work. Ideas?</p>
http://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/?answer=25407#post-id-25407I guess
H = f.hessian().function(x,y)
H(z[0,0],z[1,0])
would also work
Mon, 05 Jan 2015 17:08:51 -0600http://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/?answer=25407#post-id-25407Answer by John Palmieri for <p>I have a rather easy question, or so it would seem. I have looked for an answer but was unable to find one anywhere so I'm asking it here.</p>
<p>I am making a very simple iterative algorithm for which the input as well as the output at the end of every iteration is a vector (or matrix for that matter). What I want to do is use these elements as arguments for several functions during each of the iteration. So for example</p>
<pre><code>x=var('x')
y=var('y')
z=matrix(2,1,[ [1],[1] ]
f=x^2+y^3
H=f.hessian()
</code></pre>
<p>Then what I would like to do is say</p>
<pre><code>H(z[0],z[1])
</code></pre>
<p>or</p>
<pre><code>H(z)
</code></pre>
<p>But no matter what I try I can't seem to get it to work. Ideas?</p>
http://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/?answer=11798#post-id-11798This works for me:
sage: H.substitute(x=z[0,0],y=z[1,0])
(z is a matrix, so it requires two indices to specify an element.)Fri, 26 Nov 2010 05:06:51 -0600http://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/?answer=11798#post-id-11798Answer by niles for <p>I have a rather easy question, or so it would seem. I have looked for an answer but was unable to find one anywhere so I'm asking it here.</p>
<p>I am making a very simple iterative algorithm for which the input as well as the output at the end of every iteration is a vector (or matrix for that matter). What I want to do is use these elements as arguments for several functions during each of the iteration. So for example</p>
<pre><code>x=var('x')
y=var('y')
z=matrix(2,1,[ [1],[1] ]
f=x^2+y^3
H=f.hessian()
</code></pre>
<p>Then what I would like to do is say</p>
<pre><code>H(z[0],z[1])
</code></pre>
<p>or</p>
<pre><code>H(z)
</code></pre>
<p>But no matter what I try I can't seem to get it to work. Ideas?</p>
http://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/?answer=11800#post-id-11800There is a subtle difference between "symbolic expressions" and "callable symbolic expressions", which are also termed "[functions][1]". Writing
sage: f=x^2+y^3
makes `f` a symbolic expression, which displays as
sage: f
x^2 + y^3
On the other hand, writing
sage: g(x,y)=x^2+y^3
makes `g` a *callable* symbolic expression, which displays as
sage: g
(x, y) |--> x^2 + y^3
With `f`, you need to use `f.substitute` to substitute values, but with `g`, since you have already informed sage of the variable order, you can use it like a function
sage: g(1,3)
28
The way you define your finction determines what kind of expression the corresponding Hessian is; note the difference in syntax below:
First method:
sage: x=var('x')
sage: y=var('y')
sage: z=matrix(2,1,[ [1],[1] ])
sage: f=x^2+y^3
sage: H=f.hessian()
sage: H.substitute(x=z[0,0],y=z[1,0])
[2 0]
[0 6]
sage: f
x^2 + y^3
sage: H
[ 2 0]
[ 0 6*y]
Second method, making `g` a callable symbolic expression:
sage: x=var('x')
sage: y=var('y')
sage: z=matrix(2,1,[ [1],[1] ])
sage: g(x,y)=x^2+y^3
sage: H=g.hessian()
sage: H(z[0,0],z[1,0])
[2 0]
[0 6]
sage: g
(x, y) |--> x^2 + y^3
sage: H
[ (x, y) |--> 2 (x, y) |--> 0]
[ (x, y) |--> 0 (x, y) |--> 6*y]
[1]: http://www.sagemath.org/doc/reference/sage/symbolic/expression.html#sage.symbolic.expression.Expression.function
Sun, 28 Nov 2010 06:42:41 -0600http://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/?answer=11800#post-id-11800