Sage: Ticket #12075: Create callable matrices in function notation
https://trac.sagemath.org/ticket/12075
<p>
In <a class="ext-link" href="http://groups.google.com/group/sage-support/browse_thread/thread/6c11a5fed4fb39b5"><span class="icon"></span>this sage-support post</a>, the poster wants the following notation to work.
</p>
<pre class="wiki">R_theta(theta)=matrix(SR,[[cos(theta),sin(theta),0],[-sin(theta),cos(theta),0],[0,0,1]])
</pre><p>
This is sort of <a class="closed ticket" href="https://trac.sagemath.org/ticket/1722" title="enhancement: [with patch, with positive review] Symbolic Matrices should be callable. (closed: fixed)">#1722</a> on steroids.
</p>
<p>
In the thread above, there are a number of good ideas. The final one is
</p>
<pre class="wiki">I think this would be a good project for a person wanting to learn more
about how to do Sage development. The modifications to
symbolic_expression should be easy (check for a nested list/tuple
structure). Then you would need to make a new function() method, in
$SAGE_ROOT/devel/sage/sage/matrix/matrix2.pyx probably, that would
create a corresponding matrix over the CallableSymbolicRing. You can
see an example for vectors in
$SAGE_ROOT/devel/sage/sage/modules/free_module.pyx (look for "def
function" in that file).
Then if you are ambitious, create a new matrix subclass for callable
symbolic expressions that mirrors what
$SAGE_ROOT/devel/sage/sage/modules/vector_callable_symbolic_dense.py
does for vectors (i.e., changes the printing).
}}}[[Image()]]
</pre>en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/12075
Trac 1.1.6jasonWed, 23 Nov 2011 18:22:40 GMT
https://trac.sagemath.org/ticket/12075#comment:1
https://trac.sagemath.org/ticket/12075#comment:1
<p>
I think I would like this syntax instead:
</p>
<pre class="wiki">
R_theta(theta)=[[cos(theta),sin(theta),0],[-sin(theta),cos(theta),0],[0,0,1]]
</pre><p>
That's what the above development hint is geared towards.
</p>
TicketkcrismanWed, 23 Nov 2011 18:57:32 GMT
https://trac.sagemath.org/ticket/12075#comment:2
https://trac.sagemath.org/ticket/12075#comment:2
<p>
Really? Without the word <code>matrix</code>? I guess that the preparser would in theory catch this as an error, but it seems a little bit of an overreach to me.
</p>
<p>
Could one then make a matrix <code>M</code> by doing one of
</p>
<pre class="wiki">M() = [[1,2],[2,3]]
M(1) = [[1,2],[2,3]]
M(x) = [[1,2],[2,3]]
</pre><p>
and are any of those suspicious?
</p>
TicketjasonThu, 24 Nov 2011 00:12:58 GMT
https://trac.sagemath.org/ticket/12075#comment:3
https://trac.sagemath.org/ticket/12075#comment:3
<p>
Right now, these work to create vectors:
</p>
<pre class="wiki">M(x)=[1,2,x]
</pre><p>
so I see the nested list as a natural extension of the syntax to matrices.
</p>
TicketkcrismanThu, 24 Nov 2011 00:37:35 GMT
https://trac.sagemath.org/ticket/12075#comment:4
https://trac.sagemath.org/ticket/12075#comment:4
<p>
Nice, and then you are right that this is natural.
</p>
<hr />
<p>
You mean
</p>
<pre class="wiki">V(x) = [1,2,x]
</pre><p>
of course.
</p>
TicketgbeMon, 09 Jan 2012 16:46:44 GMT
https://trac.sagemath.org/ticket/12075#comment:5
https://trac.sagemath.org/ticket/12075#comment:5
<p>
Little note: the definition of function() for vectors is located in modules/free_module_element.pyx rather than modules/free_module.pyx.
</p>
TicketgbeTue, 10 Jan 2012 14:41:08 GMTkeywords set
https://trac.sagemath.org/ticket/12075#comment:6
https://trac.sagemath.org/ticket/12075#comment:6
<ul>
<li><strong>keywords</strong>
<em>sd35.5</em> added
</li>
</ul>
TicketgbeTue, 10 Jan 2012 22:16:34 GMTdescription changed; author set
https://trac.sagemath.org/ticket/12075#comment:7
https://trac.sagemath.org/ticket/12075#comment:7
<ul>
<li><strong>description</strong>
modified (<a href="/ticket/12075?action=diff&version=7">diff</a>)
</li>
<li><strong>author</strong>
set to <em>Geoffrey Ehrman</em>
</li>
</ul>
<p>
<strong>Progress thus far:</strong>
</p>
<ol><li>calculus/all.py is modified so that sage recognizes the creation of callable symbolic matrices via function notation. In particular:
<pre class="wiki">sage: m(X) = [[cos(X), -sin(X)],[sin(X), cos(X)]]
</pre></li></ol><p>
and is now valid syntax.
</p>
<ol start="2"><li></li></ol><table class="wiki">
<tr></tr><tr>sage.matrix.matrix2.Matrix now has a method function() so the function notation syntax correctly returns a callable matrix. <em>However,</em> the method function() currently is creating a matrix of type Matrix_generic_dense; in analogy to the vector this this should probably be Matrix_callable_symbolic_dense. I'm unsure how significant an issue this is as most of the important things work. This certain <em>does</em> affect how these matrices are displayed - in all the following examples, the entries are of the form "X> f(X)" where what should probably be output is something like "X> [f(X), g(X)]\n[h(X), j(X)]".
</tr></table>
<ol start="3"><li>Things that work correctly:
<pre class="wiki">sage: m(pi/2)
[ 0 -1]
[ 1 0]
sage: m.trace()
X |--> 2*cos(X)
sage: m(3).eigenvalues()
[-I*sin(3) + cos(3), I*sin(3) + cos(3)]
sage: m.derivative()
[X |--> -sin(X) X |--> -cos(X)]
[ X |--> cos(X) X |--> -sin(X)]
sage: m.kernel()
Vector space of degree 2 and dimension 0 over Callable function ring with arguments (X,)
Basis matrix:
[]
sage: m.image()
Vector space of degree 2 and dimension 2 over Callable function ring with arguments (X,)
Basis matrix:
[X |--> 1 X |--> 0]
[X |--> 0 X |--> 1]
</pre></li></ol><p>
Things that don't work (and should?):
</p>
<pre class="wiki">sage: m.eigenvalues()
NotImplementedError ...
</pre><p>
Things that I'm not sure about/work incorrectly(?):
</p>
<pre class="wiki">sage: m.charpoly()
(X |--> 1)*x^2 + (X |--> -2*cos(X))*x + X |--> sin(X)^2 + cos(X)^2
sage: m.echelon_form()
[X |--> 1 X |--> 0]
[X |--> 0 X |--> 1]
</pre><p>
<strong>What remains to do:</strong>
</p>
<ol start="4"><li>Matrix.function() is defined in sage/matrix/matrix2.pyx. It might be better placed in sage/matrix/matrix_generic_dense.pyx as the method for function() is defined only for FreeModuleElement_generic_dense. (Minor)
</li></ol><ol start="5"><li>Matrix.function() is written as an exact analog to FreeModuleElement_generic_dense.function(). This is the cause of the problem I mentioned in (2). I think the root of the problem is that the vector case is being coerced to the correct class via the category framework and the matrix class is currently lacks the code needed to coerce correctly. It's also quite possible there's a better way to write this method. I've looked through some of the matrix and module code however, and I don't yet have any sensible ideas.
</li></ol><ol start="6"><li>Generalizing from (2) I've tried to make function notation for matrices as similar to function notation for vectors as similar as possible. Is this sensible, or are there enough salient differences that this is unreasonable?
</li></ol><ol start="7"><li>The file sage/matrix/matrix_callable_symbolic_dense.py purposely produces several doctest failures due again to the type issue in (2). Any other failures should be considered bugs.
</li></ol>
TicketgbeTue, 10 Jan 2012 22:17:10 GMTattachment set
https://trac.sagemath.org/ticket/12075
https://trac.sagemath.org/ticket/12075
<ul>
<li><strong>attachment</strong>
set to <em>trac_12075.patch</em>
</li>
</ul>
TicketgbeTue, 10 Jan 2012 22:18:49 GMTstatus changed
https://trac.sagemath.org/ticket/12075#comment:8
https://trac.sagemath.org/ticket/12075#comment:8
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_info</em>
</li>
</ul>
TicketgbeTue, 10 Jan 2012 22:19:37 GMTstatus changed
https://trac.sagemath.org/ticket/12075#comment:9
https://trac.sagemath.org/ticket/12075#comment:9
<ul>
<li><strong>status</strong>
changed from <em>needs_info</em> to <em>needs_review</em>
</li>
</ul>
TicketgbeTue, 10 Jan 2012 22:19:46 GMTstatus changed
https://trac.sagemath.org/ticket/12075#comment:10
https://trac.sagemath.org/ticket/12075#comment:10
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_work</em>
</li>
</ul>
TicketgbeWed, 11 Jan 2012 06:51:27 GMT
https://trac.sagemath.org/ticket/12075#comment:11
https://trac.sagemath.org/ticket/12075#comment:11
<p>
The typing issue was minor - it's handled by _get_matrix_class() in matrix_space.py. However, matrix_callable_symbolic_dense.py is may currently be unusable as written.
</p>
TicketgbeThu, 12 Jan 2012 20:38:09 GMTattachment set
https://trac.sagemath.org/ticket/12075
https://trac.sagemath.org/ticket/12075
<ul>
<li><strong>attachment</strong>
set to <em>trac_12298-v2.patch</em>
</li>
</ul>
<p>
Apply instead of trac_12298.patch
</p>
TicketgbeThu, 12 Jan 2012 20:40:14 GMTattachment set
https://trac.sagemath.org/ticket/12075
https://trac.sagemath.org/ticket/12075
<ul>
<li><strong>attachment</strong>
set to <em>trac_12075-v2.patch</em>
</li>
</ul>
<p>
Same content as trac_12298-v2.patch, just named correctly. Apply instead of trac_12075.patch
</p>
TicketgbeThu, 12 Jan 2012 20:57:05 GMT
https://trac.sagemath.org/ticket/12075#comment:12
https://trac.sagemath.org/ticket/12075#comment:12
<p>
The current version is working correctly and is probably usable in most applications. That said, I'm leaving the status as needs works as there are a few more (mostly minor, I think) remaining things to do:
</p>
<p>
Remaining issues:
</p>
<ul><li>Fix the doctests in matrix2.pyx and matrix_callable_symbolic_dense.pyx. I know v2 is has these two files failing doctests; it may be that other files are also failing as I have not yet run sage --testall.
</li><li>Partial evaluation does not return a callable matrix. See <a class="new ticket" href="https://trac.sagemath.org/ticket/12302" title="defect: Partial evaluation bug for callable vector functions (new)">#12302</a> for more details, as this is the exact same issue and the fix should be (basically) the same - a modification to _ _call_ _() so the function recognizes partial evaluation.
</li></ul><p>
Comments:
</p>
<ul><li>I'm leaving the definition of function() in matrix2.pyx unless someone with more experience withh the matrix module chimes in and says differently.
</li><li>While working with this bug I unearthed bug <a class="new ticket" href="https://trac.sagemath.org/ticket/12302" title="defect: Partial evaluation bug for callable vector functions (new)">#12302</a>. In addition to the fixes probably being nearly identical, 12302 may have implications for this patch since
<pre class="wiki">sage: m(x) = [[x,x],[0,x]]
sage: type(m)
<type 'sage.matrix.matrix_callable_symbolic_dense.Matrix_callable_symbolic_dense'>
sage: type(m[0])
<class 'sage.modules.vector_callable_symbolic_dense.Vector_callable_symbolic_dense'>
</pre></li></ul>
TicketjasonTue, 09 Jul 2013 09:53:34 GMT
https://trac.sagemath.org/ticket/12075#comment:13
https://trac.sagemath.org/ticket/12075#comment:13
<p>
Just pinging myself and others. This came up in <a class="ext-link" href="http://ask.sagemath.org/question/2744/symbolic-function-from-rm-to-rn"><span class="icon"></span>http://ask.sagemath.org/question/2744/symbolic-function-from-rm-to-rn</a>
</p>
TicketjdemeyerTue, 13 Aug 2013 15:35:53 GMTmilestone changed
https://trac.sagemath.org/ticket/12075#comment:14
https://trac.sagemath.org/ticket/12075#comment:14
<ul>
<li><strong>milestone</strong>
changed from <em>sage-5.11</em> to <em>sage-5.12</em>
</li>
</ul>
Ticketvbraun_spamThu, 30 Jan 2014 21:20:52 GMTmilestone changed
https://trac.sagemath.org/ticket/12075#comment:15
https://trac.sagemath.org/ticket/12075#comment:15
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.1</em> to <em>sage-6.2</em>
</li>
</ul>
Ticketvbraun_spamTue, 06 May 2014 15:20:58 GMTmilestone changed
https://trac.sagemath.org/ticket/12075#comment:16
https://trac.sagemath.org/ticket/12075#comment:16
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.2</em> to <em>sage-6.3</em>
</li>
</ul>
Ticketvbraun_spamSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/12075#comment:17
https://trac.sagemath.org/ticket/12075#comment:17
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
TicketmforetsSun, 09 Apr 2017 09:17:13 GMT
https://trac.sagemath.org/ticket/12075#comment:18
https://trac.sagemath.org/ticket/12075#comment:18
<p>
As of v7.6, the requirement in Description works:
</p>
<pre class="wiki">sage: R_theta(theta) = matrix(SR,[[cos(theta),sin(theta),0],[-sin(theta),cos(theta),0],[0,0,1]])
sage: R_theta
theta |--> [ cos(theta) sin(theta) 0]
[-sin(theta) cos(theta) 0]
[ 0 0 1]
</pre><p>
is this ticket obsolete or not?
</p>
<p>
However, there is a closely related use case which does fail:
</p>
<pre class="wiki">sage: theta = var('theta')
sage: R_theta(theta) = vector([cos(theta),sin(theta),0])
...
TypeError: unable to convert (cos(theta), sin(theta), 0) to a symbolic expression
</pre><p>
This was asked in <a class="ext-link" href="https://ask.sagemath.org/question/36051/when-must-i-use-lambda/#36053"><span class="icon"></span>when i must use lambda</a>.
</p>
<p>
I guess this is <a class="new ticket" href="https://trac.sagemath.org/ticket/11507" title="enhancement: make f(x,y,z)=vector make a vector-valued function (new)">#11507</a>.
</p>
TicketmforetsSun, 09 Apr 2017 09:35:20 GMT
https://trac.sagemath.org/ticket/12075#comment:19
https://trac.sagemath.org/ticket/12075#comment:19
<p>
if you happen to read this, note that as of v7.6 evaluation of a callable matrix in function notation does not work as expected:
</p>
<pre class="wiki">sage: var('x y')
(x, y)
sage: f(x) = sin(x)
sage: f(x+y) # ok
sin(x + y)
sage: f(x) = matrix([[sin(x)],[cos(x)]])
sage: f(x+y) # not ok
[sin(x)]
[cos(x)]
</pre><p>
This was asked in <a class="ext-link" href="https://ask.sagemath.org/question/35101/functions-of-variables-with-matrices/#35111"><span class="icon"></span>functions of variables with matrices</a>.
</p>
<p>
.. but i couldn't find (yet) an open ticket about this.
</p>
Ticket