Question

diag :: [[Double]] -> [Double] Given a well-formed matrix, return its diagonal (that is, the values...

diag :: [[Double]] -> [Double]
    
Given a well-formed matrix, return its diagonal (that is, the values where
the row and column number are equal).

    Project1> diag [[1,2],[3,4],[5,6]]
    [1,4]
    Project1> diag (ident 5)
    [1.0,1.0,1.0,1.0,1.0]

This is done in Haskell, I need to make a function that returns a matrix's diagonal in the form of a list. The function should also return the diagonal of a matrix that is not square

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

I have implemented the function for getting diagonal of a matrix. I am pasting the code, as well as the screenshot with output.
Also as ident () is not a built-in function, I am guessing you already have such a function, in that case, that will work as well.

Code:

diag :: [[Double]] -> [Double]
diag [[]] = []
diag (xs:[]) = [head xs]
diag (x:xs) = head x : diag (map tail xs)

main = do
print(diag ([[1,0,0],[0,1,0],[0,0,1]]))
print(diag ([[1,2],[3,4],[5,6]]))

Screenshot:

Add a comment
Know the answer?
Add Answer to:
diag :: [[Double]] -> [Double] Given a well-formed matrix, return its diagonal (that is, the values...
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
  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be ...

    In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be reduced to an echelon form by using only row replacement and row interchanging operations. Row interchanging is almost always necessary for a computer realization because it reduces the round off errors in calculations - this strategy in computer calculation is called partial pivoting, which refers to selecting for a pivot the largest by absolute value entry in a column. The MATLAB...

  • The area of the parallelogram formed by vectors a=(−1,3,1) and b=(1,2,0), rounded to one decimal, is:...

    The area of the parallelogram formed by vectors a=(−1,3,1) and b=(1,2,0), rounded to one decimal, is: Select one: a. 5.4 b. 5.5 c. -6.0 d. none of above Find the component of the vector with initial point (2,−1,1) and terminal point (4,3,−6): Select one: a. (2,4,−7) b. (6,3,−5) c. (8,−3,−6) d. (−2,−4,7) Determine whether the statement is True or False: The sum of two invertible matrices of the same size must be invertible. Select one: a. True b. False Determine...

  • Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of...

    Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of loops and conditional statements Description The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number. s is shown below: Write a program that...

  • IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. •...

    IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. • Declare a global enum variable that will support 10 values – each representing an ant colony {A, B, C, D, E, F, G, H, I, J}. • In the main function, o Declare a 2-dimensional array of size the global integer constant, SIZE. The number of rows and columns should be equal to SIZE, which would make this a square matrix. This array will...

  • K-means clustering K-means clustering is a very well-known method of clustering unlabeled data. The simplicity of...

    K-means clustering K-means clustering is a very well-known method of clustering unlabeled data. The simplicity of the process made it popular to data analysts. The task is to form clusters of similar data objects (points, properties etc.). When the dataset given is unlabeled, we try to make some conclusion about the data by forming clusters. Now, the number of clusters can be pre-determined and number of points can have any range. The main idea behind the process is finding nearest...

  • the second picture is the content of the file bf.dat the language is python Your task is to wite a function parse scenario(filenne) that parses a file with the structure described below, validates t...

    the second picture is the content of the file bf.dat the language is python Your task is to wite a function parse scenario(filenne) that parses a file with the structure described below, validates the contents and retums either a dicdionary containine all values required to specify a model scenarlo if the contents are valid, or None if any of the mntents are inwalid. The structure of the fle is as follows: . an inteper specifying the wicth and heih of...

  • For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...

    For this project, each part will be in its oun matlab script. You will be uploading a total 3 m files. Be sure to make your variable names descriptive, and add comments regularly to describe what your code is doing and hou your code aligns with the assignment 1 Iterative Methods: Conjugate Gradient In most software applications, row reduction is rarely used to solve a linear system Ar-b instead, an iterative algorithm like the one presented below is used. 1.1...

  • Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve...

    Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...

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
ADVERTISEMENT