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.Wed, 30 Jan 2019 19:06:49 +0100Can you please help me with this task in sage?https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/1) Enter the program function sum_quadrate() with matrix A.The function returns the sum of those matrix elements that are complete squares.
The x is complete square if the command is_square (x) returns the True value.
[4 1 6 ]
matrix A=[1 3 9 ]
[2 7 25]
...
def sum_quadrate (A):
m=A.nrows()
n=A.ncols()
True=0
for i in range (m):
for j in range(n):
if....???
I wrote this but from last row i dont know what goes next
PLEASE HELPSun, 27 Jan 2019 17:42:27 +0100https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/Comment by knowledge seeker for <p>1) Enter the program function sum_quadrate() with matrix A.The function returns the sum of those matrix elements that are complete squares.
The x is complete square if the command is_square (x) returns the True value.</p>
<pre><code> [4 1 6 ]
matrix A=[1 3 9 ]
[2 7 25]
...
def sum_quadrate (A):
m=A.nrows()
n=A.ncols()
True=0
for i in range (m):
for j in range(n):
if....???
</code></pre>
<p>I wrote this but from last row i dont know what goes next</p>
<p>PLEASE HELP</p>
https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/?comment=45208#post-id-45208A matrix=(4,1,6)(1,3,9)(2,7,25)Sun, 27 Jan 2019 17:46:02 +0100https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/?comment=45208#post-id-45208Answer by tmonteil for <p>1) Enter the program function sum_quadrate() with matrix A.The function returns the sum of those matrix elements that are complete squares.
The x is complete square if the command is_square (x) returns the True value.</p>
<pre><code> [4 1 6 ]
matrix A=[1 3 9 ]
[2 7 25]
...
def sum_quadrate (A):
m=A.nrows()
n=A.ncols()
True=0
for i in range (m):
for j in range(n):
if....???
</code></pre>
<p>I wrote this but from last row i dont know what goes next</p>
<p>PLEASE HELP</p>
https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/?answer=45209#post-id-45209Here are some hints that should be sufficient to solve your problem:
When `m` is a matrix, you can get all its entries with the `list` method:
sage: m = random_matrix(ZZ,4,4)
sage: m
[ -1 0 -1 -1]
[ -3 0 1 1]
[ -2 2 1 1]
[ 2 -2 3 -12]
sage: m.list()
[-1, 0, -1, -1, -3, 0, 1, 1, -2, 2, 1, 1, 2, -2, 3, -12]
When n is an integer, you can see if it is a square with the `is_square` method:
sage: n = 4
sage: n.is_square()
True
You can filter a list with *list comprehension*, see https://www.pythonforbeginners.com/basics/list-comprehensions-in-python (this is the first link provided by my search engine, there are many ressources on the web about that)
Given a list of integers, you can make the sum with the `sum` function
sage: sum([1,2,3,4])
10Sun, 27 Jan 2019 17:51:15 +0100https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/?answer=45209#post-id-45209Answer by dan_fulea for <p>1) Enter the program function sum_quadrate() with matrix A.The function returns the sum of those matrix elements that are complete squares.
The x is complete square if the command is_square (x) returns the True value.</p>
<pre><code> [4 1 6 ]
matrix A=[1 3 9 ]
[2 7 25]
...
def sum_quadrate (A):
m=A.nrows()
n=A.ncols()
True=0
for i in range (m):
for j in range(n):
if....???
</code></pre>
<p>I wrote this but from last row i dont know what goes next</p>
<p>PLEASE HELP</p>
https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/?answer=45220#post-id-45220I was hesitating somehow to post this answer, but the decision to post an answer comes from the fact that we had a quarter of beginning code, but which is not the way i would do the job.
----------
**First** let us improve the given start to have a solution:
A = matrix( ZZ, [ [4, 1, 6], [1, 3, 9], [2, 7, 25] ] )
def sum_quadrate(A):
m = A.nrows()
n = A.ncols()
result = 0
for j in range (m):
for k in range(n):
entry = A[j, k]
if entry.is_square():
result += entry
return result
print sum_quadrate(A)
The above gives the `40` as a result. And now the many comments. Please always use **four spaces** to indent. This is a good style, and saves a lot of troubles. In fact, we need only an access to the entries of $A$, so we need a "better way to loop". The suggestions of [tmonteil](https://ask.sagemath.org/users/1305/tmonteil/) already give the answer, so a better way to do the job...
----------
... is the following way:
A = matrix( ZZ, [ [4, 1, 6], [1, 3, 9], [2, 7, 25] ] )
def sum_quadrate(A):
result = 0
for entry in A.list():
if entry.is_square():
result += entry
return result
print sum_quadrate(A)
Again we get that `40`.
This is again not the best way to do the job. Instead...
----------
... using **list comprehension** we can get some mathematically easily readable version of the function:
def sum_quadrate(A):
return sum([0]+[entry for entry in A.list() if entry.is_square()])
*Note:* The `[0]+` part is needed for the case there is no perfect square in the list of entries of $A$.
Mon, 28 Jan 2019 17:17:52 +0100https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/?answer=45220#post-id-45220Comment by John Palmieri for <p>I was hesitating somehow to post this answer, but the decision to post an answer comes from the fact that we had a quarter of beginning code, but which is not the way i would do the job.</p>
<hr>
<p><strong>First</strong> let us improve the given start to have a solution:</p>
<pre><code>A = matrix( ZZ, [ [4, 1, 6], [1, 3, 9], [2, 7, 25] ] )
def sum_quadrate(A):
m = A.nrows()
n = A.ncols()
result = 0
for j in range (m):
for k in range(n):
entry = A[j, k]
if entry.is_square():
result += entry
return result
print sum_quadrate(A)
</code></pre>
<p>The above gives the <code>40</code> as a result. And now the many comments. Please always use <strong>four spaces</strong> to indent. This is a good style, and saves a lot of troubles. In fact, we need only an access to the entries of $A$, so we need a "better way to loop". The suggestions of <a href="https://ask.sagemath.org/users/1305/tmonteil/">tmonteil</a> already give the answer, so a better way to do the job...</p>
<hr>
<p>... is the following way:</p>
<pre><code>A = matrix( ZZ, [ [4, 1, 6], [1, 3, 9], [2, 7, 25] ] )
def sum_quadrate(A):
result = 0
for entry in A.list():
if entry.is_square():
result += entry
return result
print sum_quadrate(A)
</code></pre>
<p>Again we get that <code>40</code>.
This is again not the best way to do the job. Instead...</p>
<hr>
<p>... using <strong>list comprehension</strong> we can get some mathematically easily readable version of the function:</p>
<pre><code>def sum_quadrate(A):
return sum([0]+[entry for entry in A.list() if entry.is_square()])
</code></pre>
<p><em>Note:</em> The <code>[0]+</code> part is needed for the case there is no perfect square in the list of entries of $A$.</p>
https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/?comment=45238#post-id-45238`sum` of an empty list should return 0, so I don't think you need `[0]`. It works for me without `[0]`.Wed, 30 Jan 2019 19:06:49 +0100https://ask.sagemath.org/question/45207/can-you-please-help-me-with-this-task-in-sage/?comment=45238#post-id-45238