The assignment is an individual project, to be finished on one’s own effort.
The work must be submitted before 6pm Nov. 7th, 2023 (Tuesday), Beijing Time. This is a firm deadline. No late submissions are accepted.
Plagiarism is strictly forbidden, regardless of the role in the process. Notably, ten consecutive lines of identical codes are treated as plagiarism. Depending on the seriousness of the plagiarism, 30% − 100% marks will be deducted.
This assignment is supposed to be finished after learning arrays. Meanwhile, a draft may be released earlier for students’ preparation purpose.
Please let the teaching team know for any ambiguity or incorrectness in this draft.
Marking Criterion
The full score of the assignment is 100 marks.
Two java programs are to be submitted. Each program will be evaluated with several unseen test cases. A submission obtains the full score if and only if both programs pass all test cases.
Running Environment
The submissions will be evaluated in the OJ system running Java SDK. It is the students’ responsibility to make sure that his/her submissions are compatible with the OJ system.
The submission is only allowed to import four packages of (java.lang.; java.util.; java.math.; java.io.) included in Java SDK. No other packages are allowed.
All students will have an opportunity to test their programs in the OJ platform prior to the official submission.
Submission Guidelines
You will get your grade only if you submit your code both on OJ and on bb on time. An n-day late submission on bb leads to n ∗ 10% mark deduction, and any late submission on OJ leads to minimum grade.
For bb submission, you need to directly upload your java file on bb. That is, your submission should be GaussianElimination.java, or M atrixMultiplication.java, or both. Wrong submission format will receive 10% mark deduction.
Inconsistency with or violation from the guideline leads to marks deduction.
All students are reminded to read this assignment document carefully and in detail. No argument will be accepted on issues that have been specified in this document.
Program
You need to choose either ”Program A” or ”Program B” to finish. If you finish both, the higher score will be included in the overall grade.
A
In mathematics, Gaussian elimination, also known as row reduction, is an algorithm for solving systems of linear equations. It consists of a sequence of operations performed on the corresponding matrix of coefficients. This method can also be used to compute the rank of a matrix, the determinant of a square matrix, and the inverse of an invertible matrix. The method is named after Carl Friedrich Gauss (1777–1855). (see, https://en.wikipedia.org/wiki/Gaussian_elimination) You are required to write a Java program that reads the input of an augmented matrix, solve the associated system of linear equations, and then output the solution vector. For the example in Fig.1, the corresponding input and output are given in the following table.
An example of console input
Expected console output
3
2.000 3.000 -1.000
2.0 1.0 -1.0 8.0
-3.0 -1.0 2.0 -11.0
-2.0 1.0 2.0 -3.0
Note
The first line is a positive integer, giving the number of unknown variables of the linear equations. Let the number be n.
Starting from the second line, each line of the input consists of n double number separated by spaces.
The output is expected to be n floating point number rounded to three decimal places, giving the solution of unknown variables.
The template for reading input and output has been provided in GaussianElimination.java. Please strictly follow the template if you are not familiar with the output format. We have also provided the sample input and output separately in in A.txt and in A.txt. You can refer to them and use them to test your program. For command line arguments, just type
java GaussianElimination < in A.txt
We promise that the input linear equations have a unique solution. That is, you don’t need
to consider the situation the matrix doesn’t have or have multiple solutions. Also, we promise
that you don’t need to do any row interchange.
B
Write a java program named “MatrixMultiplication.java” to calculate the product of two matrices of sizes m×n and n×p espectively. Each element of the input matrices is a complex number. (For def. of complex number, see https://brilliant.org/wiki/complex-numbers/) Therefore, the result matrix is an m×p matrix with complex elements.
In this program, each input complex number is in the form a + b ∗ i, where a, b are two positive integer smaller than 1000. For example, 3 + 89 ∗ i, 0 + 2 ∗ i, 3 + 0 ∗ i.
An example of console input
Expected console output
2 3 5
1+2i 2+3i 3+4*i
-1+66i -13+60i 14+82i -14+82i 5+76*i
1+3i 2+6i 5+2*i
1+78i 2+84i 23+98i -25+100i 9+109*i
3+4i 7+2i 5+4i 3+8i 6+3*i
5+4i 3+1i 7+3i 7+7i 9+3*i
6+3i 3+7i 8+3i 6+3i 4+4*i
Note
The first line of the input is the size of the two input matrices. In this example, m = 2, n =3, p = 5, each separated by a space. The following m lines give the elements of the first matrix, and each line has n space-separated complex numbers, which form the first input matrix. Then the following n lines give the elements of the second matrix, and each line has p complex numbers, which form the second input matrix.
The output contains m lines, each line having p space-separated complex numbers, giving the elements of the result matrix. We have provided the sample input and output separately in in B.txt and out B.txt. You can refer to them and use them to test your program. For command line arguments, just type java M atrixMultiplication < in B.txt
The definition of matrix and matrix multiplication operation can be found in any linear algebra textbook, or from the following link: https://en.wikipedia.org/wiki/Matrix multiplication
When testing the assignment, the values of m, n, p are less than 500.
// here is the function you need to implement // given the augmented matrix as input, solve // the linear equations, and output the results publicstaticvoidsolve(double[][] augMatrix) { // create the solution vector intn= augMatrix.length; double[] x = newdouble[n]; // your code starts here... for(int i=0;i<=n-2;i++){ for(int j=i+1;j<=n-1;j++){ double times=augMatrix[j][i]/augMatrix[i][i]; for(int g=0;g<=n;g++){ augMatrix[j][g]-=augMatrix[i][g]*(times); } } } for(int i=n-1;i>=0;i--){ for(int g=i+1;g<=n-1;g++){ augMatrix[i][n]-=augMatrix[i][g]*augMatrix[g][n]; augMatrix[i][g]=0; } augMatrix[i][n]/=augMatrix[i][i]; augMatrix[i][i]=1; x[i]=augMatrix[i][n]; }
// end of your code // output the results, DO NOT change for (inti=0; i < n - 1; i++) { System.out.print(String.format("%.3f", x[i]) + " "); } System.out.println(String.format("%.3f", x[n-1])); }
// program entry, DO NOT change publicstaticvoidmain(String[] args) { Scannerinput=newScanner(System.in); intn= Integer.parseInt(input.nextLine()); double[][] augMatrix = newdouble[n][n + 1]; for (inti=0; i < n; i++) { Strings= input.nextLine(); String[] t = s.split(" "); for (intj=0; j < n + 1; j++) { augMatrix[i][j] = Double.parseDouble(t[j]); } } solve(augMatrix); input.close(); }