Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am working on some Bash Scripting, working with matrices and I need help with the following for code. There is a script that will

I am working on some Bash Scripting, working with matrices and I need help with the following for code. There is a script that will test the code.

Here is the requirements.

In this assignment, you will write a bash shell script that calculates basic matrix operations using input from either a file or stdin. The input will be whole number values separated by tabs into a rectangular matrix. Your script should be able to print the dimensions of a matrix, transpose a matrix, calculate the mean vector of a matrix, add two matrices, and multiply two matrices.

The dims, transpose, and mean operations should either perform their respective operations on the file named MATRIX, or on a matrix provided via stdin. The add and multiply operations do not need to process input via stdin. dims should print the dimensions of the matrix as the number of rows, followed by a space, then the number of columns. transpose should reflect the elements of the matrix along the main diagonal. Thus, an MxN matrix will become an NxM matrix and the values along the main diagonal will remain unchanged. mean should take an MxN matrix and return an 1xN row vector, where the first element is the mean of column one, the second element is the mean of column two, and so on. add should take two MxN matrices and add them together element-wise to produce an MxN matrix. add should return an error if the matrices do not have the same dimensions. multiply should take an MxN and NxP matrix and produce an MxP matrix. Note that, unlike addition, matrix multiplication is not commutative. That is A*B != B*A.

You must check for the right number and format of arguments to matrix. This means that, for example, you must check that a given input file is readable, before attempting to read it. You are not required to test if the input file itself is valid. In other words, the behavior of matrix is undefined when the matrix input is not a valid matrix. for the purposes of this assignment, a valid matrix is a tab-delimited table containing at least one element, where each element is a signed integer, every entry is defined, and the table is rectangular. The following are examples of invalid matrices and will not be tested against your code, and may not be output by your program: An empty matrix. A matrix where the final entry on a row is followed by a tab character. A matrix with empty lines. A matrix with any element that is blank or not an integer.

If the inputs are valid -- your program should output only to stdout, and nothing to stderr. The return value should be 0. If the inputs are invalid -- your program should output only to stderr, and nothing to stdout. The return value should be any number except 0. The error message you print is up to you; you will receive points as long as you print something to stderr and return a non-zero value. Your program will be tested with matrices up to dimension 100 x 100.

Summary Your script must support the following operations:

matrix dims [MATRIX] Prints error message to stderr, nothing to stdout and return value != 0 if: Argument count is greater than 1 (e.g. `matrix dims m1 m2`). Argument count is 1 but the file named by argument 1 is not readable (e.g. `matrix dims no_such_file`). Otherwise, prints "ROWS COLS" (Space separated!) to stdout, nothing to stderr, and returns 0.

matrix transpose [MATRIX] Prints error message to stderr, nothing to stdout and return value != 0 if: Argument count is greater than 1 (e.g. `matrix transpose m1 m2`). Argument count is 1 but the file named by argument 1 is not readable (e.g. `matrix transpose no_such_file`). Otherwise, prints the transpose of the input, in a valid matrix format to stdout, nothing to stderr, and returns 0.

matrix mean [MATRIX] Prints error message to stderr, nothing to stdout and return value != 0 if: Argument count is greater than 1 (e.g. `matrix mean m1 m2`). Argument count is 1 but the file named by argument 1 is not readable (e.g. `matrix mean no_such_file`). Otherwise, prints a row vector mean of the input matrix, in a valid matrix format to stdout, nothing to stderr, and returns 0. All values must round to the nearest integer, with ***.5 values rounded away from zero.

matrix add MATRIX_LEFT MATRIX_RIGHT Prints error message to stderr, nothing to stdout and return value != 0 if: Argument count does not equal 2 (e.g. `matrix add m1 m2 m3` or `matrix add m1`). Argument count is 2 but the file named by either argument is not readable (e.g. `matrix add m1 no_such_file`). The dimensions of the input matrices do not allow them to be added together following the rules of matrix addition. Otherwise, prints the sum of both matricies, in a valid matrix format to stdout, nothing to stderr, and returns 0.

matrix multiply MATRIX_LEFT MATRIX_RIGHT Prints error message to stderr, nothing to stdout and return value != 0 if: Argument count does not equal 2 (e.g. `matrix multiply m1 m2 m3` or `matrix multiply m1`). Argument count is 2 but the file named by either argument is not readable (e.g. `matrix multiply m1 no_such_file`). The dimensions of the input matrices do not allow them to be multiplied together following the rules of matrix multiplication. Otherwise, prints the product of both matricies, with the first argument as the left matrix and the second argumentas the right matrix, in a valid matrix format to stdout, nothing to stderr, and returns 0. (`matrix multiply A B` should return A*B, not B*A) ` An invalid command must result in an error message to stderr, nothing to stdout, and a return value != 0.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

Why is interest in portable benefits in health care increasing?

Answered: 1 week ago