11
Mathematical Background
Let A be an N by N matrix of real numbers, b a vector of N real numbers and x a vector N unknown real numbers. A matrix equation is Ax = b.
Jacobi's method is as follows: decompose A = D + R, where D is the matrix of diagonals, and R is the remaining entries.
if you make an initial guess solution x0, an improved solution is x1 = inverse(D) * (b - Rx) where all multiplications are matrix-vector multiplication and inverse(D) is the matrix inverse.
Problem Specification
- Input: Your complete program should accept as input the following data: the matrix A, the vector b, an initial guess x0, and an 'error' number e.
- Output: The program must output the minimum number of iterations such that the latest solution differs by the true solution, by at most e. This means each component of the vectors in absolute magnitude differ by at most e. You must use Jacobi's method for iterations.
How the data is inputted is your choice; it could be your own syntax on a command line, you could take input from a file, whatever you choose.
How the data is outputted is your choice; it can be written to a file, displayed in the command line, written as ASCII art, anything, as long as it is readable by a human.
Further Details
You are not given the true solution: how you calculate the true solution is up to you entirely. You may solve it by Cramer's rule for example, or computing an inverse directly. What matters is that you have a true solution to be able to compare to iterations.
Precision is an issue; some people's 'exact solutions' for comparison may differ. For the purposes of this code golf the exact solution must be true to 10 decimal places.
To be absolutely clear, if even one component of your present iteration solution exceeds its corresponding component in the true solution by e, then you need to keep iterating.
The upper limit to N varies based on what hardware you're using and how much time you're willing to spend running the program. For the purposes of this code golf, assume maximum N = 50.
Preconditions
When your program is called, you are free to assume that the following holds at all times:
- N > 1 and N < 51, i.e. you will never be given a scalar equation, always a matrix equation.
- All inputs are over the field of real numbers, and will never be complex.
- The matrix A is always such that the method converges to the true solution, such that you can always find a number of iterations to minimise the error (as defined above) below or equal to e.
- A is never the zero matrix or the identity matrix, i.e. there is one solution.
Test Cases
A = ((9, -2), (1, 3)), b = (3,4), x0 = (1,1), e = 0.04
The true solution is (0.586, 1.138). The first iteration gives x1 = (5/9, 1), differing by more than 0.04 from the true solution, by at least one component. Taking another iteration we find, x2 = (0.555, 1.148) which differs by less than 0.04 from (0.586, 1.138). Thus the output is
2
A = ((2, 3), (1, 4)), b = (2, -1), x0 = (2.7, -0.7), e = 1.0
In this case the true solution is (2.2, -0.8) and the initial guess x0 already has error less than e = 1.0, thus we output 0. That is, whenever you do not need to make an iteration, you simply output
0
Submission Assessment
This is code golf, with all standard loopholes hereby disallowed. The shortest correct complete program (or function), i.e. lowest number of bytes wins. It is discouraged to use things like Mathematica which wrap up a lot of the necessary steps into one function, but use any language you want.
2You really should wait to get more feedback for it, especially given the recent closed post. PPCG challenges usually share common structure in specifications, what usually contribute to them being easy to understand, rather than tiresome and ambiguous. Try to look at some of the reasonably upvoted challenges and mimic the format. – Uriel – 2017-11-15T15:05:50.003
@Uriel I realise this, but I feel I've been exhaustive in my specification, and the format while not exactly fitting the majority of questions, can be read linearly, and guide the reader accordingly. The format should also keep the content of the problem itself in mind. – user1997744 – 2017-11-15T15:06:52.630
Your test cases for example, are really hard to go through. We usually keep them in
input - output
plain format, in code blocks, with explanations following if contributing/necessary, but not interfering with verifying correctness – Uriel – 2017-11-15T15:13:46.457@Uriel Codeblocks is a good idea. – user1997744 – 2017-11-15T15:19:07.540
3"The shortest correct complete program" sounds like you only allow programs and not functions. I'd add "/function". – Adám – 2017-11-15T15:20:02.967
2+1 formatting makes or breaks my brain's ability to concentrate on a question – Stephen – 2017-11-15T15:22:05.907
though it must be able to run without any further code
-> so we can't call the function? The default for codegolf is to allow either a full program that takes input, for example, by STDIN or arguments, or to allow a function that can be called as passed the input, and would return or print the output. – Stephen – 2017-11-15T15:38:47.830@Stephen That's fine it's just I didn't want people writing a function in a language, and then needing more source code to run, e.g. in C++ excluding main(). – user1997744 – 2017-11-15T15:40:15.277
1@user1997744 Yup, makes sense. I believe the default is that any other code, like other functions or python imports, is allowed, but also included in the bytecount. – Stephen – 2017-11-15T15:42:14.557
@user1997744 Some other notes: some users also propose that not all questions need to be sandboxed, for example this post and this post. After having posted that question, you had known what is considered "clear" and what is not on this site. Because the requirement is very strict, it is encouraged to use the sandbox if you think the question may be unclear or uninteresting.
– user202729 – 2017-11-16T09:40:54.070@user202729 Right, and I deliberately did not use a sandbox to prove the point I can write a good question without it. – user1997744 – 2017-11-16T10:45:10.957