Post Reply 
Help: Port prg to Home Mode( Minor linear_algebra)
02-02-2014, 11:43 PM (This post was last modified: 02-10-2014 02:09 PM by compsystems.)
Post: #1
Help: Port prg to Home Mode( Minor linear_algebra)
http://en.wikipedia.org/wiki/Minor_(linear_algebra)

ENTRY LINE EDITON Sad
Code:
minor_CAS0(m1):=
begin  
  return(seq(seq(det(delRow(delCol(m1,j),I)),j,1,colDim(m1)),I,1,rowDim(m1)))​​;  
end;
[enter]

then

minor_CAS0([[1,2,3],[4,5,6],[7,8,9]]); [enter] returns
[[-3,-6,-3],
[-6,-12,-6],
[-3,-6,-3]] OK Smile


//////

but HOME MODE

PRG EDITOR
Code:
EXPORT minor_HOME0(m1)
begin
  local out;
  local j:="jj", ii:="ii"; // trick =(
  out := seq(seq(det(delRow(delCol(m1,j),ii)),j,1,colDim(m1)),ii,1,rowDim(m1));
  return(out);
end;

minorHOME([[1,2,3],[4,5,6],[7,8,9]]); [enter] returns "Error: Bad Argument Type" ????
Find all posts by this user
Quote this message in a reply
02-03-2014, 05:38 AM
Post: #2
RE: Help HOME PRG (Minor linear_algebra)
Why are you defining j and ii as strings? Also, the help for DELCOL() and DELROW() suggest that they modify the matrix itself, so that subsequent references to the matrix are actually references to the the modified version.

To see this for yourself:

Code:

M1:=[[1,2],[3,4]];
DELCOL(M1,1);
M1;

My guess is that in CAS mode the dummy variable somehow protects its contents to prevent DELCOL() and DELROW() from modifying the matrix. Either that, or it is a bug.

The commands you should probably be using instead: delcols() and delrows() -- these do not alter the matrix.

Code:

EXPORT MINOR(m)
BEGIN
  LOCAL n:=dim(m),s;
  s:=n(1)*n(2);
  n:=n(1);
  MAKELIST(det(delcols(delrows(m,((I-1) MOD n)+1),IP((I-1)/n)+1)),I,1,s);
END;

You can easily change this to use seq() if you prefer. Just don't confuse a sequence with a matrix even if they both use the same delimiters.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-03-2014, 03:06 PM (This post was last modified: 02-03-2014 03:44 PM by compsystems.)
Post: #3
RE: Help HOME PRG (Minor linear_algebra)
sorry for my bad English

The version in CAS mode works fine, but not in HOME mode =(, but as there is not a program editor for cas mode my calculator this underutilized.


Han: Why are you defining j and ii as strings?
I try to do the trick you discussed in another topic.

http://www.hpmuseum.org/forum/thread-538.html

Code:
export convolution1(v1,v2)
begin 
  local p1,p2,c,var:="x_";
  purge(var);
  p1:=poly2symb(v1,var); 
  p2:=poly2symb(v2,var); 
  c:=symb2poly(simplify(p1*p2),var);
  return(c); 
end


Han: Just don't confuse a sequence with a matrix even if they both use the same delimiters.

the seq command generates a one-dimensional array, and seq (seq generates a two-dimensional array

seq(x²,x,1,3); enter returns
[1,4,9]; TYPE(Ans); returns 4 (Matrix 1x3) best vector =)

seq(seq(x²,x,1,3),x,1,2); enter returns
[[1,1,1],
[4,4,4]]; TYPE(Ans); returns 4 (Matrix 2x3)

Using the seq cmd, number of instructions is reduced =)

Code:
minorCAS2(m1):=
BEGIN  
RETURN(seq(seq(det(delRow(delCol(m1,j),I)),j,1,colDim(m1)),I,1,rowDim(m1)));  // i == unit imaginary =(
END;

minorCAS2([[1,2,3],[4,5,6],[7,8,9]]); [enter] returns
[[-3,-6, -3]
[-6,-12,-6]
[-3,-6, -3]] OK Smile

versus your code

Code:
EXPORT MINOR(m)
BEGIN
  LOCAL n:=dim(m),s;
  s:=n(1)*n(2);
  n:=n(1);
  MAKELIST(det(delcols(delrows(m,((I-1) MOD n)+1),IP((I-1)/n)+1)),I,1,s);
END;

MINOR([[1,2,3],[4,5,6],[7,8,9]]); [enter] returns {-3,-6,-3,-6,-12,-6,-3,-6,-3} LIST

http://www.maplesoft.com/support/help/Ma...ebra/Minor
Find all posts by this user
Quote this message in a reply
02-03-2014, 07:22 PM (This post was last modified: 02-04-2014 03:27 AM by Han.)
Post: #4
RE: Help HOME PRG (Minor linear_algebra)
Quote:Using the seq cmd, number of instructions is reduced =)

That may be true, but sometimes it is beneficial to separate your commands. For example, using coldim() inside the seq() command means coldim() is computed repeatedly -- which is unnecessary, and slow if you have a much larger matrix. If you precompute the dimensions, then the seq() command would only be reading a value as opposed to calculating it every iteration. Secondly, separating the commands makes debugging much easier.

As for list vs matrix, try:

Code:

EXPORT MINOR(m)
BEGIN
  LOCAL n=dim(m),n1,n2;
  n1:=n(1); n2:=n(2);
  MAKEMAT(det(delcols(delrows(m,I),J)),n1,n2);
END;

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-09-2014, 03:15 PM (This post was last modified: 02-09-2014 03:29 PM by compsystems.)
Post: #5
RE: Help HOME PRG (Minor linear_algebra)
Code:

export MINOR_HOME1(m)
begin
   local n;
   n:=dim(m);
   return(makemat(det(delcols(delrows(m,I),J)),n(1),n(2)));
end;

MINOR_HOME1([[1,2,3],[4,5,6],[7,8,9]]); [enter] returns
[[-3,-6, -3]
[-6,-12,-6]
[-3,-6, -3]] OK

Code:

MINOR_CAS1(m):=
begin
   local n;
   n:=dim(m);
   return(makemat(det(delcols(delrows(m,I),J)),n(1),n(2)));
end

MINOR_CAS1([[1,2,3],[4,5,6],[7,8,9]]); [enter] returns ???
{{"Error: Bad Argument Type","Error: Bad Argument Type","Error: Bad Argument Type"},
{"Error: Bad Argument Type","Error: Bad Argument Type","Error: Bad Argument Type"},
{"Error: Bad Argument Type","Error: Bad Argument Type","Error: Bad Argument Type"}}


Code:

MINOR_CAS0(m1):=
begin 
 return(seq(seq(det(delRow(delCol(m1,j),I)),j,1,colDim(m1)),I,1,rowDim(m1)))​;  
end;

MINOR_CAS0([[1,2,3],[4,5,6],[7,8,9]]); [enter] returns
[[-3,-6, -3]
[-6,-12,-6]
[-3,-6, -3]] OK
Find all posts by this user
Quote this message in a reply
02-09-2014, 03:26 PM (This post was last modified: 02-09-2014 03:29 PM by Han.)
Post: #6
RE: Help HOME PRG (Minor linear_algebra)
MAKEMAT() and makemat() are two different commands.

MAKEMAT(expr, m, n): MAKEMAT(I^2+J, 3, 5);

makemat(func, m, n): makemat( (x,y)->x^2+y, 3, 5);

Is there any reason to insist on using a CAS program instead of a regular program?

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-09-2014, 03:55 PM (This post was last modified: 02-09-2014 09:25 PM by compsystems.)
Post: #7
RE: Help HOME PRG (Minor linear_algebra)
now with MAKEMAT on CAS MODE fail Why?

Code:

MINOR_CAS2(m):=
 begin
    local n;
    n:=dim(m);
    return(MAKEMAT(det(delcols(delrows(m,I),J)),n(1),n(2)));
 end

MINOR_CAS2([[1,2,3],[4,5,6],[7,8,9]]); [enter] returns

"Error: Bad Argument Type ??


Quote:Han: MAKEMAT() and makemat() are two different commands.

>> On HOME mode MAKEMAT() and makemat() are equal

I: HOME MODE

INDEX = (1,1) for MAKEMAT or makemat or makeMat or or any combination of uppercase and lowercase

MAKEMAT(I^2+J,3,5); // HOME MODE =>

[[2,3,4,5,6],
[5,6,7,8,9],
[10,11,12,13,14]] OK

makemat(I^2+J,3,5); makeMat(I^2+J,3,5); => MAKEMAT(I^2+J,3,5); // HOME MODE =>

[[2,3,4,5,6],
[5,6,7,8,9],
[10,11,12,13,14]] OK

INDEX = (1,1)

step1: I=1, J=1,..,5
1^2+1=2, 1^2+2=3, 1^2+3=4, 1^2+4=5, 1^2+5=6 // ok

step2: I=2, J=1,..,5
2^2+1=5, 2^2+2=6, 2^2+3=7, 2^2+4=8, 2^2+5=9 // ok

step3: I=3, J=1,..,5
3^2+1=10, 3^2+2=11, 3^2+3=12, 3^2+4=13, 3^2+5=14 // ok

MAKEMAT((I,J)->I²+J,3,5); or makemat((I,J)->I²+J,3,5); "Error: Syntax Error"


///////////////

On CAS mode MAKEMAT() and makemat() are not equal =) Support catalog says nothing about this =(
Quote:Han: Is there any reason to insist on using a CAS program instead of a regular program?

Muchas, una de ellas es que algunos comandos se comportan diferente. entonces se puede aprovechar esta característica dentro de un programa, otra permite una mas fácil manipulación de expresiones simbolicas

MAKEMAT en CAS MODE opera en índice (0,0) =)

INDEX = (0,0) for makemat and INDEX = (1,1) for MAKEMAT

makemat( (I,J)->I²+J, 3,5); // CAS MODE

=>

[[0,1,2,3,4],
[1,2,3,4,5],
[4,5,6,7,8]] OK

step1: I=0 J=0,..,4
0^2+0=0, 0^2+1=1, 0^2+2=2, 0^2+3=3, 0^2+4=4 // ok

step2: I=1, J=0,..,4
1^2+0=1, 1^2+1=2, 1^2+2=3, 1^2+3=4, 1^2+5=6 // ok

step3: I=2, J=0,..,4
2^2+0=4, 2^2+1=5, 2^2+2=6, 2^2+3=7, 2^2+4=8 // ok


INDEX = (1,1) for MAKEMAT

MAKEMAT(I²+J,3,5); // CAS MODE

=>

[[2,3,4,5,6],
[5,6,7,8,9],
[10,11,12,13,14]] OK

BUT

MAKEMAT((I,J)->I²+J,3,5); // CAS MODE

=>

Bug because should return only the right evaluation of the function

[[(1,1)->1²+1,(1,2)->1²+2,(1,3)->1²+3,(1,4)->1²+4,(1,5)->1²+5],
[(2,1)->2²+1,(2,2)->2²+2,(2,3)->2²+3,(2,4)->2²+4,(2,5)->2²+5],
[(3,1)->3²+1,(3,2)->3²+2,(3,3)->3²+3,(3,4)->3²+4,(3,5)->3²+5]] =(


F1(I,J):=I^2+J; returns F1:=(I,J)->I²+J => (I,J)->I²+J

makemat(F1(I,J),3,5) // CAS MODE

or MAKEMAT(F1(I,J),3,5) // CAS MODE

=>

Not operate, why? BUG?

"makemat(F1(I,J),3,5) Error: Bad Argument Value" ...


makeMat(I²+J,3,5);

[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]] ???

makeMat((I,J)->I²+J,3,5);

"Error: Unmatch control word" ??

///
Han can you port the following code to HOME MODE

Code:
 // Code on CAS MODE OK, HOME MODE =(
collectPolTerms_CAS0( Poly, Var ):=
BEGIN
    RETURN( sum( coeff( Poly, Var ) .* seq( Var^k, k, degree( Poly, Var ), 0, 1 ) ));
END;

with simplify NONE (CAS Settings),

collectPolTerms_CAS0(((-3*a*x²+7*a*x^1-2*a-3*b*x²+7*b*x^1-2*b+3*x^3-7*x²+2*x^1)/3),x); [ENTER} returns

x^3+((-3*a-3*b-7)/3)*x²+((7*a+7*b+2)/3)*x+(-2*a-2*b)/3 OK


Thanks
Find all posts by this user
Quote this message in a reply
02-09-2014, 05:35 PM (This post was last modified: 02-09-2014 05:37 PM by Han.)
Post: #8
RE: Help HOME PRG (Minor linear_algebra)
(02-09-2014 03:55 PM)compsystems Wrote:  >> On HOME mode MAKEMAT() and makemat() are equal

That is because Home view does not distinguish upper case from lower case. Home view is not aware of CAS commands (generally speaking; there are a few CAS commands that still work in Home because they have been implemented for Home). CAS commands will sometimes work (in lower case) but generally require the CAS.command() format. So MAKEMAT() and makemat() are the same in Home, but CAS.makemat() is different.

Quote:On CAS mode MAKEMAT() and makemat() are not equal =)

Correct.

Quote:Support catalog says nothing about this =(

Unfortunately, the catalog is missing a LOT of information.

Quote:MAKEMAT en CAS MODE opera en índice (0,0) =)

INDEX = (0,0) for make mat

makemat( (I,J)->I²+J, 3,5)

INDEX = (1,1) for MAKEMAT

MAKEMAT(I²+J,3,5);

[[2,3,4,5,6],
[5,6,7,8,9],
[10,11,12,13,14]] OK

BUT

MAKEMAT((I,J)->I²+J,3,5); Bug because should return only the right evaluation of the function

[[(1,1)->1²+1,(1,2)->1²+2,(1,3)->1²+3,(1,4)->1²+4,(1,5)->1²+5],
[(2,1)->2²+1,(2,2)->2²+2,(2,3)->2²+3,(2,4)->2²+4,(2,5)->2²+5],
[(3,1)->3²+1,(3,2)->3²+2,(3,3)->3²+3,(3,4)->3²+4,(3,5)->3²+5]] =(

Don't mix commands and syntax. You are using MAKEMAT() but you are trying to incorporate the syntax of the other command: makemat(). MAKEMAT() is working exactly as you told it to. When you use the (I,J)->I^2+J syntax, MAKEMAT() -- because it is NOT a CAS command -- has no idea about the meaning of (var)->expr because that is a CAS syntax. Instead, MAKEMAT treats (I,J)->I^2+J as an expression and substitutes values of I and J in, and simplifies as best it can.

Quote:F1(I,J):=I^2+J
makemat(F1(I,J),3,5) or MAKEMAT(F1(I,J),3,5)
Not operate, why?

Because the first argument of makemat() -- which is a CAS command -- requires a function. So either use F1 by itself, or use (I,J)->I^2+J. When you used F1(I,J), then this is an evaluation of a function, which produces a expression.

Quote:makeMat(I²+J,3,5);

[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]] ???

Here I and J are constants -- 0. The command line interpreted "makeMat" as "makemat" so that you are just requesting a 3x5 matrix with 0 as the entry.

Quote:///
Han can you port the following code to HOME MODE

Code:

collectPolTerms_CAS0( Poly, Var ):=
BEGIN
    RETURN( sum( coeff( Poly, Var ) .* seq( Var^k, k, degree( Poly, Var ), 0, 1 ) ));
END;

There is nothing required for porting. If you want to use that command in Home, just put your arguments as strings. So in Home view, do:

collectPolTerms_CAS0("((-3*a*x²+7*a*x^1-2*a-3*b*x²+7*b*x^1-2*b+3*x^3-7*x²+2*x^1)/3)","x")

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-09-2014, 08:26 PM (This post was last modified: 02-09-2014 09:22 PM by compsystems.)
Post: #9
RE: Help HOME PRG (Minor linear_algebra)
abstract

for HOME MODE

MAKEMAT(expresion/expresion(I,J), m, n) or makemat(expresion/expresion(I,J), m, n) or makeMAT(expresion/expresion(I,J), m, n) with INITIAL INDEX (I=1,J=1)

example with expresion

MAKEMAT(X+2, 3, 5); or makeMat(X+2, 3, 5); =>

[[2,2,2,2,2],
[2,2,2,2,2],
[2,2,2,2,2]] OK, with the current value of X (by default 0)

MAKEMAT(X^2+Y, 3, 5); or makeMat(X^2+Y, 3, 5); =>

[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]] OK, with the current value of X, Y (by default 0)

with expresion(I,J)

MAKEMAT(I^2+J, 3, 5); or makeMat(I^2+J, 3, 5); =>

[[2,3,4,5,6],
[5,6,7,8,9],
[10,11,12,13,14]] OK

with function

MAKEMAT((I,J)->I²+J,3,5); or makemat((I,J)->I²+J,3,5); "Error: Syntax Error" OK

////////////

for CAS MODE

with uppercase and expresion(I,J)

MAKEMAT(I^2+J, 3, 5); // INITIAL INDEX (I=0,J=0)

[[2,3,4,5,6],
[5,6,7,8,9],
[10,11,12,13,14]] OK

another expresion

MAKEMAT(Ii^2+Ee, 3, 5);
[[Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee],
[Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee],
[Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee]] OK


lowercase

makemat( function(I,J), m, n) with INITIAL INDEX (I=1,J=1)
example

makemat( (I,J)->I²+J, 3,5); =>

[[0,1,2,3,4],
[1,2,3,4,5],
[4,5,6,7,8]] OK

makemat((X,Y)->X²+Y,3,5); =>

[[0,1,2,3,4],
[1,2,3,4,5],
[4,5,6,7,8]] OK

Code:
Han: interpreted "makeMat" as "makemat"
not operate as you say

makeMat((I,J)->I²+J,3,5); => "Error: Unmatch control word" ??

//////

with expresión

makemat(Ii^2+Ee, 3, 5);
[[Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee],
[Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee],
[Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee,Ii²+Ee]] OK

makemat(I^2+J, 3, 5);

[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]] OK // I and J with the current value of I, J (default 0)

////

Quote:Han: There is nothing required for porting.
Code:

// port for HOME MODE
EXPORT collectPolTerms_HOME0( Poly, Var )
 BEGIN
     RETURN( sum( coeff( Poly, Var ) .* seq( Var^k, k, degree( Poly, Var ), 0, 1 ) ));
 END;

help "Error Syntax on k" =(
Find all posts by this user
Quote this message in a reply
02-10-2014, 03:53 AM (This post was last modified: 02-10-2014 03:54 AM by Han.)
Post: #10
RE: Help HOME PRG (Minor linear_algebra)
(02-09-2014 08:26 PM)compsystems Wrote:  abstract

for HOME MODE

MAKEMAT(expresion/expresion(I,J), m, n) or makemat(expresion/expresion(I,J), m, n) or makeMAT(expresion/expresion(I,J), m, n) with INITIAL INDEX (I=1,J=1)

There is only one command in Home mode no matter how you type it, it will get parsed as MAKEMAT(). If you want to use the CAS command in Home, the only way to do so is use CAS.makemat().

Quote:for CAS MODE

with uppercase and expresion(I,J)

MAKEMAT(I^2+J, 3, 5); // INITIAL INDEX (I=0,J=0)

This is incorrect. MAKEMAT() is the non-CAS command and its index starts at 1 for I and J.

Quote:lowercase

makemat( function(I,J), m, n) with INITIAL INDEX (I=1,J=1)


Incorrect. The index for the CAS command makemat() starts at 0.

Quote:
Code:
Han: interpreted "makeMat" as "makemat"
not operate as you say

This is a mistake on my part (a typo). "makeMat" is parsed as "MAKEMAT." However, due to the fact that we are typing this from the CAS view, the command line tries to parse it in a way that Home would expect (because MAKEMAT is not a CAS command). Thus, all variables within the argument are resolved first. If they cannot be resolved, this generates an error. (This is either a bug, or a poor design choice.) Anyway, the code

Code:
makeMat((I,J)->I²+J,3,5);  => "Error: Unmatch control word" ??

cannot work for two reasons:

1. makeMat is parsed as MAKEMAT (so that the functional expression is invalid)
2. in an attempt to resolve the variables in the expression (I,J)->I^2+J would result in an invalid argument


Quote:Han: There is nothing required for porting.
Code:

// port for HOME MODE
EXPORT collectPolTerms_HOME0( Poly, Var )
 BEGIN
     RETURN( sum( coeff( Poly, Var ) .* seq( Var^k, k, degree( Poly, Var ), 0, 1 ) ));
 END;

help "Error Syntax on k" =(

I will have to look into this more carefully as I overlooked the variable k the first time.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 




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