Scientific Computing Homework 1
Due date: 20200323 23:59:59
Add and Max
Given two matrices A and B (possibly of different sizse), write a function to find the maximum element of their sum. Note that if A and B are not of the same size, we need to expand each of them before the addition, as shown in the following example:
$
A=
\left[
\begin{matrix}
2 & 3\\
1 & 5\\
4 & 6\\
\end{matrix}
\right]
,
B=
\left[
\begin{matrix}
1 & 2 & 3 & 7\\
5 & 4 & 2 & 3\\
\end{matrix}
\right]
,
A+B=
\left[
\begin{matrix}
3 & 5 & 3 & 7\\
6 & 9 & 2 & 3\\
4 & 6 & 0 & 0\\
\end{matrix}
\right]
\Rightarrow
$
addAndMax(A, B) returns 9.
Here is a file list of example code (to download them all: exampleCode.zip) for you to start with. The readme.txt of the example code folder is shown next:
This is the readme.txt file in example code for HW01 of Scientific ComputingaddAndMax
Your submission (you only need to submit one of the following):
Python: addAndMax.py
Matlab: addAndMax.m
Example input/output files:
input.txt: input file
output.standard.txt: output file (the correct answer)
Input/output file format:
Input:
Each two-row is a matrix, and each four-row is matrices A and B. For each four-row, you need to output one answer.
For instance, "input.txt" has 80 rows, indicating there are 20 pairs of matrices A and B, and you need to output 20 answers.
The format of each four rows is explained as follows.
3 2: [row column] for matrix A
2 3 1 5 4 6: Elements in A ==> A = [[2, 3], [1, 5], [4, 6]]
2 4: [row column] for matrix B
1 2 3 7 5 4 2 3: Elements in B ==> B = [[1, 2, 3, 7], [5, 4, 2, 3]]
Output:
A column of numbers, with each number being an answer to given matrices A and B
Example main program to run the test:
Python: mainTest.py, with the following command to run it.
python mainTest.py < input.txt > output.python.txt
Matlab: mainText.m, with the following command (within matlab) to run it.
mainText input.txt output.m.txt
Others
addAndMaxSP.p is a standard program that can be invoked just like an M file in MATLAB to check against your submission of "addAndMax.m".
Hints:
Python: You may use numpy's functions, including zeros, shape, max, etc.
Matlab: Some related functions are size, zeros, max, etc.