Programming Question: How do you augment one matrix with another?
07-01-2014, 08:57 PM
Post: #1
 Jsather Junior Member Posts: 34 Joined: Apr 2014
Programming Question: How do you augment one matrix with another?
One reason I was attracted to the HP Prime after years of settling for lesser programmable calculators is the flexibility of matrix construction and deconstruction. Yet, when it comes down to writing some code, I am hard pressed to find the basic augment(mat A, mat B) command. Foithermore, I don't see a simple work around short of redimensioning followed by transfer loops.

I also had a problem transferring list data such as would be found in L1 to a column or row of a matrix but find that the following works:
addrow(M1,seq(L1(X),X,1,ColDim(M1)),row#);

So how about a little help here? Am I missing something or do we have to write out matrix augmentation the hard way? Also is there a less convoluted way to get data from a lists to matrices? These are pretty fundamental operations.
07-02-2014, 12:47 AM (This post was last modified: 07-02-2014 03:12 AM by Helge Gabert.)
Post: #2
 Helge Gabert Senior Member Posts: 467 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
If you're after augmenting two matrices horizontally, in CAS, CONCAT works.

If you're after augmenting two matrices vertically, again, in CAS, one solution would be to transform the matrices into lists with mat2list, then use CONCAT on the lists, and put everything back again with list2mat.

I have not checked yet if this also works in home. [edit: vertically augmenting works fine in home as well. For horizontal augmentation in home, you need to call CAS.CONCAT(matrix1, matrix2).]
07-02-2014, 05:42 AM
Post: #3
 parisse Senior Member Posts: 1,243 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
[op(M1),op(M2)] glues two matrix one over the second one. augment(M1,M2) works in Xcas, but for some unknown reason it was removed on the Prime, but you can use concat instead.
I recommend to use lowercase commandnames for CAS operations, even if the command exists uppercase, since you are certain to call the native CAS command.
07-02-2014, 06:09 AM
Post: #4
 cyrille de brĂ©bisson Senior Member Posts: 1,047 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
Hello

Quote:the flexibility of matrix construction and deconstruction.
augment(mat A, mat B) command.

In NUMERICAL mode (Home and non CAS programs)

Deconstruction is relatively easy as you can do things like:
M(r) to access a row
M(-c) to access a column
M(r,c) to access an element
M(r or -c or r,c):= to place vectors in locations...
SUB(M, {r,c},{r,c}) extracts a sub part of a matrix...
REPLACE(M1, {r,c}, M2) will place M2 at a given location in M1
I do seem to remember (not sure), that you can use lists in the 'storying' type commands and that they will be transformed into matrices as needed...

Now, back to your problem... I do not have an augment command, however, you can do:
M3:=MAKEMAT(0, r, c); REPLACE(M3,{1,1},M1); REPLACE(M3,{0,c}, M2);
3 commands, but no loops or the like...

cyrille
07-02-2014, 02:58 PM
Post: #5
 John P Member Posts: 209 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
I would do it in the following way:
1. Horizontally: concat works fine.
2.Vertically: I would write small prog. named augment (name not in the catalog) and augment means: enlarge or increase or grow or intensify, you can probably assume that it is in vertical direction. In the prog. augment, if you want, you could check if no. of cols. are the same etc. and the following would do the job: transpose(concat(transpose(m1),transpose(m2)));
07-02-2014, 05:17 PM
Post: #6
 parisse Senior Member Posts: 1,243 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
Don't use augment for that, augment is the native Xcas command to do it like concat does. Use
[op(m1),op(m2)] instead (not much longer to type).
07-02-2014, 05:59 PM
Post: #7
 Jsather Junior Member Posts: 34 Joined: Apr 2014
RE: Programming Question: How do you augment one matrix with another?
Parisse: Could you give an explicit example of what you meant by "[op(M1),op(M2)] glues two matrices together", preferably that works in a program? I don't understand.

John: I don't understand why you say "Horizontally: concat works fine". When I enter "concat (M1,M2)", it returns the list {M1,M2}, not the augmentation of M2 to M1.

Cyrille: Thank you for the construction / deconstruction info. I had no idea you could reference e.g. M1(r), M1(-c). Could you give an explicit example showing how you would place a vector using M(r,c)? I understand how you would use M(r) or M(-c) but not M(r,c).

There is a problem, Cyrille, with the "Makelist; replace; replace" augmentation method you suggested, apparently an HP Prime bug. I've attached a screenshot demonstrating the problem. "Replace(M3,{1,1},M1)" works fine but then "Replace(M3{1,4},M2)" zeroes the left half of M3 in the process of placing M2 in the right half.

On the main subject, augmenting two matrices in a program, here is code that actually works (preceded by R:=rowDim(M1);C:=colDim(M1);):

As Helge suggested:
M3:=list2mat(concat(mat2list(M1),mat2list(M2)),C);//vertical
M3:=trn(list2mat(concat(mat2list(trn(M1)),mat2list(trn(M2))),R)); //horizontal

or using Makemat:
M3:=makemat(piecewise(I<=R,M1(I,J),I>R,M2(I-R,J)),R+rowDim(M2),C);//vert
M3:=makemat(piecewise(J<=C,M1(I,J),J>C,M2(I,J-C)),R,C+colDim(M2));//horiz

For me, these are all joylessly convoluted. Still wishing for something simpler.
jfs

Attached File(s) Thumbnail(s)

07-02-2014, 07:01 PM
Post: #8
 parisse Senior Member Posts: 1,243 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
(07-02-2014 05:59 PM)Jsather Wrote:  Parisse: Could you give an explicit example of what you meant by "[op(M1),op(M2)] glues two matrices together", preferably that works in a program? I don't understand.
Just write [op(m1),op(m2)] and see what you get. This works of course in CAS and CAS programs.
op removes the [] delimiters of the matrix, you get the sequence of the matrix rows, the comma concatenates the rows of m1 and m2 and [] brings back the matrix delimiter.
07-02-2014, 08:42 PM
Post: #9
 John P Member Posts: 209 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
(07-02-2014 05:17 PM)parisse Wrote:  Don't use augment for that, augment is the native Xcas command to do it like concat does. Use
[op(m1),op(m2)] instead (not much longer to type).

I did not know about op() or what it does, not listed in help or could not find. Yes, this is the preferable way to do it and the simplest. Thank you very much.

PS. I think it would be good idea to have next firmware update before September with to most resent and perhaps not to complicated bugs fixed.
07-03-2014, 06:40 PM (This post was last modified: 07-05-2014 07:01 PM by Jsather.)
Post: #10
 Jsather Junior Member Posts: 34 Joined: Apr 2014
RE: Programming Question: How do you augment one matrix with another?
(07-02-2014 07:01 PM)parisse Wrote:
(07-02-2014 05:59 PM)Jsather Wrote:  Parisse: Could you give an explicit example of what you meant by "[op(M1),op(M2)] glues two matrices together", preferably that works in a program? I don't understand.
Just write [op(m1),op(m2)] and see what you get. This works of course in CAS and CAS programs.
op removes the [] delimiters of the matrix, you get the sequence of the matrix rows, the comma concatenates the rows of m1 and m2 and [] brings back the matrix delimiter.

In CAS mode I typed [op(M1),op(M2)] as Parisse suggested, and it returns a list of two matrices rather than an augmented matrix. However, I typed m3:=concat(op(M1),op(M2)) and it returned the matrix that is the vertical augmentation of M2 to M1 in list format meaning [[row 1],[row 2],...,[row n]]. Type(m3) in CAS mode returns DOM_List. Type(m3) in Home mode returns 6, i.e. a list.

In Home mode or in an HP Prime program, concat(CAS.op(M1),CAS.op(M2)) returns {op([M1]),op[M2])}, a list. By "op" I mean the letters op are displayed. By [M1] and [M2] I mean the M1 and M2 matrices are listed out in a mode determined by the Textbook Display checkbox in Home Settings. In other words, concat(CAS.op(M1),CAS.op(M2)) does not augment M2 to M1 in home mode or in an HP Prime program.

No matter what else, it is always interesting to learn of "gosh, where is that documented" functionality such as op(M1) or M1(-c). I, for one, am new to CAS and HP for that matter and find the CAS documentation in the user manual inadequate. Maybe someone reading this can show me how to use op(matrix) in an HP Prime program.
07-04-2014, 06:36 AM
Post: #11
 parisse Senior Member Posts: 1,243 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
Very strange, I tried
M1:=identity(2); [op(M1),op(M1)]
it works.
07-04-2014, 04:09 PM
Post: #12
 Jsather Junior Member Posts: 34 Joined: Apr 2014
RE: Programming Question: How do you augment one matrix with another?
This morning I saw your reply, Parisse, and also tried M1:=identity(2);[op(M1),op(M1)] and found that it works. So I tried other experiments and find [op(M1),op(M2)] augments M2 to M1 vertically in all cases, identically to concat(op(M1),op(M2)). As expected trn([op(trn(M1)),op(trn(M2))]) augments M2 to M1 horizontally.

I cannot explain why [op(M1),op(M2)] returned a list of two matrices on my virtual calculator yesterday and a vertical augmentation of M2 to M1 today.
I am certain of my observations yesterday but have been unable to return my virtual calculator to the state where [op(M1),op(M2)] fails, nor do I know how it got out of that state. I'll post If I figure it out.

That is good for CAS mode, but HP Prime programs work in Home mode. Is there any way to make M3:=[op(M1),op(M2)] work in an HP Prime program?
07-04-2014, 04:21 PM
Post: #13
 parisse Senior Member Posts: 1,243 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
concat(M1,M2) does *not* the same as [op(M1),op(M2)].
And if you want to use CAS commands, you can write CAS programs, just check CAS when you create your program. They will be parsed and they will execute like if you were in CAS.
07-04-2014, 06:42 PM
Post: #14
 Jsather Junior Member Posts: 34 Joined: Apr 2014
RE: Programming Question: How do you augment one matrix with another?
(07-04-2014 04:21 PM)parisse Wrote:  concat(M1,M2) does *not* the same as [op(M1),op(M2)].
I didn't write that concat(M1,M2) is the same as [op(M1),op(M2)]. I wrote that concat(op(M1),op(M1)) returns the same vertical augmentation as [op(M1),op(M2)] which I think is correct. Sorry ... sometimes I write too much and it gets confusing.

Thanks for reminding me there is a CAS option for programs. I am a CAS novice and have much to learn.
07-05-2014, 05:21 AM
Post: #15
 parisse Senior Member Posts: 1,243 Joined: Dec 2013
RE: Programming Question: How do you augment one matrix with another?
Indeed, I answered too fast, sorry!
07-05-2014, 04:48 PM
Post: #16
 Jsather Junior Member Posts: 34 Joined: Apr 2014
RE: Programming Question: How do you augment one matrix with another?
No worries, Parisse. I'm thankful for all your good advice.
07-05-2014, 06:31 PM (This post was last modified: 07-05-2014 06:59 PM by Jsather.)
Post: #17
 Jsather Junior Member Posts: 34 Joined: Apr 2014
RE: Programming Question: How do you augment one matrix with another?
(07-02-2014 06:09 AM)cyrille de brĂ©bisson Wrote:  REPLACE(M1, {r,c}, M2) will place M2 at a given location in M1

Now, back to your problem... I do not have an augment command, however, you can do:
M3:=MAKEMAT(0, r, c); REPLACE(M3,{1,1},M1); REPLACE(M3,{0,c}, M2);
3 commands, but no loops or the like...
Earlier I mentioned an apparent bug in Replace that prevented Cyrille's makemat; replace; replace; method from working. I now have an explanation: not a bug. Replace(M3,{1,1},M1) returns M3 with M1 inserted beginning at (r,c)=(1,1), but does not change M3. To make the example work, it needs to be M3:=Replace(M3,{1,1},M1). This is counterintuitive because "replace" leads us to an assumption of replacement in M3.

For vertical augmentation:
M3:=makemat(0,rowDim(M1)+rowDim(M2),colDim(M1));
M3:=replace(M3,{1,1},M1);
M3:=replace(M3,{rowDim(M1)+1,1},M2);
or
M3:=M1;
redim(M3,{rowDim(M1)+rowDim(M2),colDim(M1)});
M3:=replace(M3,{rowDim(M1)+1,1},M2);

For horizontal augmentation:
M3:=makemat(0,rowDim(M1),colDim(M1)+colDim(M2));
M3:=replace(M3,{1,1},M1);
M3:=replace(M3,{1,colDim(M1)+1},M2);
or
M3:=M1;
redim(M3,{rowDim(M1),colDim(M1)+colDim(M2)});
M3:=replace(M3,{1,colDim(M1)+1},M2);

These and the several other methods discussed in this thread could form the basis of a general purpose augmentation subroutine. The apparent absence of a matrix augment command is a surprising deficiency in the HP Prime command set.
07-05-2014, 07:36 PM
Post: #18
 DrD Senior Member Posts: 1,133 Joined: Feb 2014
RE: Programming Question: How do you augment one matrix with another?
I disagree with your thinking on the way the Replace command should be implemented, and much prefer the way Cyrille has it.

Replacing the contents of a matrix with other information need not restrict where the result of that operation gets stored, or, perhaps, not stored at all. This is consistent with behavior of other hp Prime commands.

-Dale-
07-06-2014, 06:26 PM
Post: #19
 Jsather Junior Member Posts: 34 Joined: Apr 2014
RE: Programming Question: How do you augment one matrix with another?
(07-05-2014 07:36 PM)DrD Wrote:  I disagree with your thinking on the way the Replace command should be implemented, and much prefer the way Cyrille has it.

Replacing the contents of a matrix with other information need not restrict where the result of that operation gets stored, or, perhaps, not stored at all. This is consistent with behavior of other hp Prime commands.

-Dale-
I am not saying I have a stylistic preference for doing it my way. I've just explained why "the way Cyrille has it" does not work and offered a minor modification that does work. My screen shot attachment to post 7 shows the problem. This is caused by the way the "Replace" command works in the HP Prime, not how I think it should work.

Perhaps I've misunderstood you or made a mistake. Please post a screen shot of the method you are advocating performing matrix augmentation.
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)