|Matrix Trilogy [LONG]|
Message #1 Posted by Valentin Albillo on 17 June 2003, 4:44 a.m.
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
your RPN teeth. As always, though the actual solutions are particularized to the HP-15C,
the techniques featured can be useful for many other HP models (HP-71B/Math, HP-42S, HP-41/Advantage)
so you may want to have a go at it with said models as well; the intended solutions are
certainly rewritable for those models (I've done that for the 71B, actually).
The quiz's theme is as follows: comprehensive and well-designed as the HP-15C's matrix capabilities are, there are some
very useful functions that regrettably were not included in the instruction set.
For instance, there's no "SUM" function which will return the sum of all the elements
of a given matrix (!)
So, this week's quiz (kind of a trilogy, actually) centers around the missing "SUM" matrix
function, asking for a general solution and two particular cases, as follows:
1) Given an arbitrary MxN matrix, which contains arbitrary values (positive, negative
or zero), write a routine (LBL A, ..., RTN) which takes the matrix descriptor in the X-register
as input, and returns the sum of all the elements in the matrix. For instance:
A = | 3 -21 7 | must return S = 3-21+7+2-10+8 = -11
| 2 -10 8 |
The 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),
which takes some 10 seconds to return the sum of all 60 elements of a 10x6 matrix.
2) Write an specialized routine for the particular case in which all the elements of the matrix
happen to be arbitrary sines and/or cosines [i.e.: values of sin(x) or cos(x)]. For instance:
(values obtained in radians, displayed in FIX 4)
A = | sin(2) sin(4) sin(1) | must return S = -0.1033
| sin(6) sin(5) sin(3) |
(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)
which takes some 5 seconds to return the sum of all 60 elements of a 10x6 matrix.
3) Write another specialized routine for the case in which all the elements of the matrix
happen to be arbitrary values of the exponential function [i.e.: values of e^x].
For instance (result displayed in FIX 4):
A = | e^1.2 e^1.4 e^1.1 | must return S = 23.4835
| e^1.6 e^1.5 e^1.3 |
For 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.
That's all. Give it a fair try, it's quite an interesting, genuinely useful problem, and
I eagerly await for your own implementations and ideas (remember to test your routines against
the 2x3 numerical cases given, and to time their speed for a suitably filled-up 10x6 matrix).
Best regards :-)
Edited: 17 June 2003, 6:21 a.m.