Ask Your Question

# 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 HELP

edit retag close merge delete

## 2 Answers

Sort by » oldest newest most voted

Here 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/ba... (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])
10

more

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.

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 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(+[entry for entry in A.list() if entry.is_square()])


Note: The + part is needed for the case there is no perfect square in the list of entries of $A$.

more

## Comments

1

sum of an empty list should return 0, so I don't think you need . It works for me without .

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2019-01-27 17:42:27 +0200

Seen: 142 times

Last updated: Jan 28 '19