Ask Your Question
0

Can you please help me with this task in sage?

asked 2019-01-27 17:42:27 +0100

updated 2019-01-27 17:51:45 +0100

tmonteil gravatar image

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 flag offensive close merge delete

Comments

A matrix=(4,1,6)(1,3,9)(2,7,25)

knowledge seeker gravatar imageknowledge seeker ( 2019-01-27 17:46:02 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2019-01-27 17:51:15 +0100

tmonteil gravatar image

updated 2019-01-27 17:54:34 +0100

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
edit flag offensive delete link more
1

answered 2019-01-28 17:17:52 +0100

dan_fulea gravatar image

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([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$.

edit flag offensive delete link more

Comments

1

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

John Palmieri gravatar imageJohn Palmieri ( 2019-01-30 19:06:49 +0100 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

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

Seen: 851 times

Last updated: Jan 28 '19