ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 17 Apr 2021 01:03:41 +0200matrix_plot not returning any outputs in subroutineshttps://ask.sagemath.org/question/56666/matrix_plot-not-returning-any-outputs-in-subroutines/ A=identity_matrix(2)
def f(n):
matrix_plot(n*A)
f(5)
I'm puzzled as the command `matrix_plot(5*A)` works fine on itself, but it produces no output when it goes inside a subroutine as in the above SageMath code.
I'd appreciate any help with understanding why this is so/resolving this.
PS I have tested this both on Sagecell.sagemath.org and a local installation.
Thu, 15 Apr 2021 22:56:30 +0200https://ask.sagemath.org/question/56666/matrix_plot-not-returning-any-outputs-in-subroutines/Comment by slelievre for <pre><code> A=identity_matrix(2)
def f(n):
matrix_plot(n*A)
f(5)
</code></pre>
<p>I'm puzzled as the command <code>matrix_plot(5*A)</code> works fine on itself, but it produces no output when it goes inside a subroutine as in the above SageMath code.</p>
<p>I'd appreciate any help with understanding why this is so/resolving this.</p>
<p>PS I have tested this both on http://Sagecell.sagemath.org and a local installation.</p>
https://ask.sagemath.org/question/56666/matrix_plot-not-returning-any-outputs-in-subroutines/?comment=56679#post-id-56679Also asked as [Stack overflow question 67113741](https://stackoverflow.com/questions/67113741).Sat, 17 Apr 2021 01:03:41 +0200https://ask.sagemath.org/question/56666/matrix_plot-not-returning-any-outputs-in-subroutines/?comment=56679#post-id-56679Answer by rburing for <pre><code> A=identity_matrix(2)
def f(n):
matrix_plot(n*A)
f(5)
</code></pre>
<p>I'm puzzled as the command <code>matrix_plot(5*A)</code> works fine on itself, but it produces no output when it goes inside a subroutine as in the above SageMath code.</p>
<p>I'd appreciate any help with understanding why this is so/resolving this.</p>
<p>PS I have tested this both on http://Sagecell.sagemath.org and a local installation.</p>
https://ask.sagemath.org/question/56666/matrix_plot-not-returning-any-outputs-in-subroutines/?answer=56675#post-id-56675The reason is the following: the notebook and command line interface, when they are evaluating input, at the end they always output the value of the last line in the input, if it is not `None`.
So indeed, if you enter `matrix_plot(5*A)` directly, you get your plot because it is the value of that line.
When you enter `f(5)`, the body of the function is executed as it should be, but this function body does not explicitly `return` any value, so the default value `None` is returned, which results in no output.
You probably meant to write:
def f(n):
return matrix_plot(n*A)
or equivalently
f = lambda n: matrix_plot(n*A)
Fri, 16 Apr 2021 19:29:20 +0200https://ask.sagemath.org/question/56666/matrix_plot-not-returning-any-outputs-in-subroutines/?answer=56675#post-id-56675