![]() |
Matrix Trilogy [LONG] - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Matrix Trilogy [LONG] (/thread-36453.html) |
Matrix Trilogy [LONG] - Valentin Albillo - 06-17-2003 Hi ya all, specially HP-15C & RPN lovers:
A new week has begun, so there you are, a new, original HP-15C quiz/challenge I've composed and solved for you to sharpen
The quiz's theme is as follows: comprehensive and well-designed as the HP-15C's matrix capabilities are, there are some
So, this week's quiz (kind of a trilogy, actually) centers around the missing "SUM" matrix
1) Given an arbitrary MxN matrix, which contains arbitrary values (positive, negative A = | 3 -21 7 | must return S = 3-21+7+2-10+8 = -11The routine must be primarily optimized for speed, and subject to that, be as short as possible and consume the least resources (registers, labels, subroutine levels, flags, etc). We are only interested in the sum of the elements, not the matrix itself (which for all we care, might be destroyed in the main program as soon as your routine has returned, to free RAM).
Under these conditions, there is a solution in 20 steps or less (LBL and RTN included),
A = | sin(2) sin(4) sin(1) | must return S = -0.1033(of course you don't have to evaluate any sin/cos, they already come evaluated, you only have to add them up quickly).
For this special case, there is a solution in 13 steps or less (LBL-RTN included)
A = | e^1.2 e^1.4 e^1.1 | must return S = 23.4835For this particular case, there is a solution in 9 steps or less (LBL-RTN included) which takes only 2.5 seconds to return the sum of all 60 elements of a 10x6 matrix.
Best regards :-)
Edited: 17 June 2003, 6:21 a.m.
My Solution, Part 1 - Patrick - 06-18-2003 Once again, thanks Valentin for the opportunity to use the little grey cells. For pedagogical reasons, I provide the answers to your questions in reverse order. I will also post separate messages for each question to allow the interested reader to pick up and continue the solution.
3) First, we consider a simpler case where the matrix consists of a single row. If you look at the instruction set of the HP-15C, you will find that [f][MATRIX][7] computes the matrix row norm. In the case of a one row matrix, this is almost what we want (i.e., the sum of values of the elements of the matrix). The difference is that the row norm takes the absolute values of the matrix elements first, before summing. However, in the restricted case of Valentin's question 3, this is not a problem: exponentials like e^1.2 are always positive. Putting this together, here is the solution to the problem for a one row, non-negative matrix: 001 LBL A Like, good thing the 15C is programmable, eh?
Well, consider now the more general case of a multi-row, but still positive, matrix. The solution here is not much harder: we simply re-dimension the matrix to have a single row! Of course, the sum of the matrix elements is not changed by the re-dimensioning operation, so here is the complete solution to Question 3: 001 LBL A My Solution, Part 2 - Patrick - 06-18-2003 2) Now to answer Valentin's second question, where the matrix elements consist of the sines and cosines of various angles. Well, this type of matrix is no longer non-negative, so the technique we used in Part 1 does not apply directly. However, the essential attribute of sines and cosines is that they are never less than -1. So, if we were to add 1 to every element of the matrix, we would have a non-negative matrix again and the earlier technique could be used. Of course, we've changed the sum of the elements of the matrix by doing this, but in an entirely predictable way: we've added to the sum a number exactly equal to the number of elements in the matrix!
Here is a program which is based on this algorithm. It adds one to each element of the matrix, finds the row norm, then subtracts from that value the total number of matrix elements. 001 LBL A My Solution, Part 3 (start) - Patrick - 06-18-2003 1) Finally, to the general question that Valentin posed, his question number 1. No longer do we have either a positive matrix, nor a matrix nicely bounded below so that we can easily make it positive ... or do we? It is late here and I do not have the time to give the entire solution right now, so I encourage others to fill in the remainder of the solution. Here is a hint. We've seen what the row norm does for a matrix with a single row. What does it do for a matrix with a single column?
Good night for now and happy programming...
Re: My Solution, Part 1 - Valentin Albillo - 06-18-2003 Hi, Patrick & everyone following this thread: Absolutely correct. My own original solution was as follows, with comments describing how it deals with
A = | e^1.2 e^1.4 e^1.1 | which must return S = 23.4835For this special case (all matrix elements are >= 0), there is a solution in 9 steps or less (LBL-RTN included) which takes only 2.5 seconds to return the sum of all 60 elements of a 10x6 matrix LBL A begins routine. Say we've got 2x3 matrix above in the X-register
Of course, this works not only for values of e^x, but
Best regards.
Re: My Solution, Part 2 - Valentin Albillo - 06-18-2003 Hi, Patrick & everyone following this thread:
Again, absolutely correct. My own original solution was as follows, with comments describing how it deals with A = | sin(2) sin(4) sin(1) | which must return S = -0.1033For this special case, there is a solution in 13 steps or less (LBL-RTN included) which takes only 5 seconds to return the sum of all 60 elements of a 10x6 matrix. LBL A begins routine. Say we've got 2x3 matrix above in the X-register
As you may see, your solution is one step shorter (thanks to that nifty X<>I, RCL-I technique, though the byte count is the same, because RCL-I requires two bytes) but there is another difference: you add 1 to all elements in the matrix, to make them all positive or zero, while my solution subtracts 1 from all the elements in the matrix, to make them all negative or zero! :-) Of course, this works not only for values of sin(x), but for any other values as long as they all are <= 1 (for my solution, or >= -1 for yours). By changing it slightly, it will work as well whenever you know that your values are all of them less than some limit or all of them greater than some limit. The (very easy) details are left as an exercise for the reader. :-)
Best regards.
|