Question

Please, write a function in PYTHON without NumPy that diagonalizes a square matrix

Please, write a function in PYTHON without NumPy that diagonalizes a square matrix

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans :

One way is to use list comprehension and enumerate

CODE - and please look at the snapshots for indentation

first = [1,2,3,4]         # global arrays
second = [5,6,7,8]

def fc(first, second):               #function definition
    n = len(first)                   #calculating length of input array  
    res = [[i]*n for i in first]    # resultant array declaration
    for idx, j in enumerate(second): # enumerate
        res[idx][idx] += j
    return res                        # returning the resulting array              

res = fc(first, second)           # call for function
print(res)                       # printing result

Snapshots -:

Output :

Add a comment
Know the answer?
Add Answer to:
Please, write a function in PYTHON without NumPy that diagonalizes a square matrix
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT