HP-15C Mini-Challenge: RSUM, CSUM



#12

Hi, all

As an appetizer before the next S&SMC (#11), here you are, a new, very easy, small, and potentially useful mini-challenge specifically for the HP-15C.

Tha facts

Many handhelds and calculators that have built-in matrix operations capabilities do include a couple of matrix functions, usually called RSUM/CSUM that return the sum of all rows/columns of any given matrix. This is the case, for instance, for the HP41C's Advantage ROM, where according to its manual:

    CSUM  finds sums of columns and puts them in a row matrix
RSUM finds sums of rows and puts them in a column matrix
i.e., if some matrix A is 4x5, say, CSUM would return a 1x5 matrix, B, where each element would be the sum of the corresponding column in A. Likewise, RSUM would return a 4x1 matrix, B, where each element would be the sum of the corresponging row in A.

Now, the HP-15C does not include such matrix operations as built-in functions, so this is the

Challenge

You must write two separate small routines (LBL ... RTN) that will implement the RSUM and CSUM functionality described above.
Your routines should assume that there exists an MxN matrix called A, and upon being called must return a matrix B to the X register, containing the respective sums. For the case of the RSUM routine, the resulting matrix B must be Mx1, while for the CSUM routine, it must be 1xN.

Your must optimize your routines for speed and space, the faster and shorter the better. Here's an example. Given:

        |  1  7  -3   2  5  |
A = | 0 4 5 -6 3 |
| 4 8 -3 1 7 |
| -8 3 -6 12 6 |

CSUM must return: B = | -3 22 -7 9 21 |

RSUM must return: | 12 |
B = | 6 |
| 17 |
| 7 |

Epilogue

My original solutions, not counting the mandatory LBL and RTN, are 8 steps for RSUM and 9 steps for CSUM. Both can return the sum of all rows/columns of a 6x8 matrix in 5 seconds flat.

Now it's your turn ! Don't be afraid to try, it's actually very easy. Let's see your solutions ! :-)

Best regards from V.


#13

Hi Valentin,

I don't have a 15C in my drawer here, but is the solution like multiplying with a [1 1 1 1] matrix for Csum and [1 1 1 1 1] for rsum ?

Just very old memories from long time ago math classes.


#14

So now the challenge is to solve MC11 efficiently without the use of Matrices...

I am now going to reread the 15C manual about matrices. They are some much easier with a graphical calc.

Arnaud


#15

Hi, Arnaud:

Arnaud posted:

"So now the challenge is to solve MC11 efficiently without the use of Matrices.."

Say what !?

Best regards from V.


#16

I mean when the MC11 comes out, I will certainly try it with matrices as it seems from your post that it will help, but I will also try without.

Arnaud

#17

Your challenge seems to be inspired by MATLAB's sum functions that work on rows and columns of matrices????

Namir

#18

Hi Valentin,

Here is my solution:

LBL A          ; Row Sum
f RESULT B ; Store the result in B
RCL DIM A ; X = #columns in A, Y = #rows in A
1 ;
f DIM C ; C is a #columns in A x 1 matrix
STO MATRIX C ; All entries in C are set to 1
RCL MATRIX A ;
RCL MATRIX C
* ; B = A * C
RTN

LBL B ; Column sum
f RESULT B
RCL DIM A ; X = #columns in A, Y = #rows in A
X<>Y ; X = #rows in A, Y = #columns in A
1 ;
f DIM C ; C is a #rows in A x 1 matrix
STO MATRIX C ; All entries in C are set to 1
RCL MATRIX A
RCL MATRIX C
f MATRIX 5 ; B = Transpose(A) * C
RTN

The Row Sum requires 10 steps including LBL and RTN and the Column Sum requires 11 steps including LBL and RTN.

For the Row Sum, the routine creates a matrix, C, of size (#columns in A) x 1 and sets each element of C equal to 1. Matrix A is multiplied by the matrix C. The result is in matrix B and the matrix descriptor for B is returned on the stack.

For the Column Sum, the routine creates a matrix, C, of size (#rows in A) x 1 and sets each element of C equal to 1. The transpose of matrix A is multiplied by the matrix C, using the f MATRIX 5 command. The result is again in matrix B and the matrix descriptor for B is again returned on the stack.

Best Regards,

Eamonn.


#19

Valentin,

I just re-read your challenge and realized that in my haste to post a solution, I didn't notice that the result for the CSUM function should be a 1 x (#columns in A) matrix. My solution returns a (#columns in A) x 1 matrix.

However, re-ordering a couple of lines in my original solution fixes this defect. Here is my amended solution...

LBL B          ; Column sum
f RESULT B
RCL DIM A ; X = #columns in A, Y = #rows in A
X<>Y ; X = #rows in A, Y = #columns in A
1 ;
f DIM C ; C is a #rows in A x 1 matrix
STO MATRIX C ; All entries in C are set to 1
RCL MATRIX C ; *** These two lines have been swapped
RCL MATRIX A ; ***
f MATRIX 5 ; B = Transpose(C) * A
RTN

Best Regards,

Eamonn.


#20

Well, I'm sorry I had no 15C available yesterday, but this confirms that my approach was correct. I deserve a chocolate medal as I haven't calculated matrixes for 15 years now. Congratulations for posting this solution !

#21

I haven't tried yet, but it looks to me that something like:

LBL B
MATRIX 4 ;transpose input to have problem A
GSB A
MATRIX 4 ;transpose output
RTN

Would work, maybe more slowly but it is shorter?

Arnaud

#22

Hi, all:

Congratulations to all of you who posted (or suggested)
correct solutions. These are my original solutions, that
are nearly identical to Eamonn's:

For columns:

01 LBL A
02 RCL DIM A
03 RDOWN
04 1
05 DIM C
06 STO MATRIX C
07 RESULT B
08 RCL MATRIX C
09 RCL MATRIX A
10 MATRIX 5
11 RTN

For rows:

01 LBL A
02 RCL DIM A
03 1
04 DIM C
05 STO MATRIX C
06 RESULT B
07 RCL MATRIX A
08 RCL MATRIX C
09 *
10 RTN

I'm a little too busy to comment further, again thanks for your interest, and

Best regards from V.


Possibly Related Threads...
Thread Author Replies Views Last Post
  HPCC Mini Conference 2013 hugh steers 6 751 09-13-2013, 04:27 PM
Last Post: BruceH
  Picture from the Mini-HHC (LA) Geir Isene 11 1,038 07-07-2013, 01:06 PM
Last Post: Jim Horn
  My birthday, so a little commemorative mini-challenge ! Valentin Albillo 43 3,344 03-07-2013, 03:44 AM
Last Post: Walter B
  WP 34S mini-challenge (B) Gerson W. Barbosa 17 1,433 12-27-2012, 04:39 PM
Last Post: Marcus von Cube, Germany
  Mini-challenge: HHC2012 RPL programming contest with larger input David Hayden 14 1,450 10-05-2012, 10:36 PM
Last Post: David Hayden
  HP41 / SY41CL Mini-B USB Power Connector (Module) Matt Kernal 5 1,618 07-08-2012, 06:23 PM
Last Post: Diego Diaz
  HP-15C (& LE!) 11-11-11 Mini-Challenge Valentin Albillo 28 2,132 11-14-2011, 08:12 AM
Last Post: Valentin Albillo
  Mini challenge. CEIL / FLOOR in RPN M. Joury 47 3,799 10-31-2011, 10:11 AM
Last Post: M. Joury
  Photo of my HP 15c | 15c LE DigiGal 2 540 10-12-2011, 12:34 PM
Last Post: DigiGal
  HP 15c LE vs HP-15C dimensions - BOTH ARE HUGE! Joerg Woerner 4 703 10-03-2011, 06:53 AM
Last Post: Jim Johnson

Forum Jump: