Post Reply 
Wronskian
02-22-2015, 11:10 PM (This post was last modified: 02-23-2015 06:51 PM by salvomic.)
Post: #1
Wronskian
hi,
I made a (CAS) program to calculate Wronskian of a list of functions (it gives both fundamental matrix and wronskian = det|mat|)

Code:

ChooseType()
BEGIN
LOCAL choice;
CHOOSE(choice,"Matrix or Wronskian","Fundamental Matrix","Wronskian");
RETURN choice;
END;

#cas
// Wronskian - input a [vector] of functions
// i.e. W(f,g)= fg'-f'g
wronskian(flist):=
BEGIN
local mat, s, j, k, d, t, h;
s:= size(flist);

IF (  (s < 2) OR (TYPE(flist) == 2) OR (TYPE(flist(1))==2) OR (TYPE(flist(2))==2) ) THEN return "Input a [vector] of functions";
ELSE
IF (TYPE(flist) <> 4) THEN print "It would be better use [vector] in []"; END;
mat:= makemat(0, s, s);
mat[1]:= flist;
FOR j FROM 2 TO s DO // diff

FOR k FROM 1 TO s DO // items
d:=diff(mat[j-1,k],x);
mat[j, k]:= d;

END; // for k
END; // for j
IF EXPR(" wronskian.ChooseType()")=1 THEN
return mat;
ELSE
return det(mat);
END; // if

END; // 1st if

END;
#end

Why the code
Code:

IF (type(flist)≠ DOM_MATRIX) THEN return "input [vector] of functions"; ELSE ...
accepts not only matrix but also input like (2,3) and ({x, x^2}), numbers or list? There is a way to accept only a real matrix of vector, with [] only?
Definitely the program sees "flist" always as a list with input [x, x^2], {x, x^2}, (x, x^2)...
With a vector the program (correctly) gives a matrix, with list it gives a vector with 2 elements, a list and a vector, like [{x, x^2}[1, 2*x]], not correct as format, and I would like to avoid that.

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
02-23-2015, 06:57 PM (This post was last modified: 02-23-2015 07:00 PM by salvomic.)
Post: #2
RE: Wronskian
This code
Code:

IF (  (s < 2) OR (TYPE(flist) == 2) OR (TYPE(flist(1))==2) OR (TYPE(flist(2))==2) ) THEN return "Input a [vector] of functions";
ELSE
...

is a bit "dirty": I would control *every* part of an input like [x, x^2, 1-x, ...] not only the first two, to be NOT a string.
I thought to use something like
Code:

FOR h FROM 1 TO s DO // s = size of flist
IF (TYPE(flist(h)==2) THEN return "They must be string"; END;
END;
but it doesn't work here (if there is that, doesn't work this: "IF EXPR(" wronskian.ChooseType()")=1 THEN return mat;").

And that IF
Code:

IF (TYPE(flist) <> 4) THEN print "It would be better use [vector] in []"; END;
after the ELSE doesn't control anything, as input is always list, also with [...], and I would need to control that the input should be *only* a vector (but I don't know how)...

Any help is much appreciated!

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
02-23-2015, 07:21 PM (This post was last modified: 02-23-2015 10:18 PM by salvomic.)
Post: #3
RE: Wronskian
A little better version:
Code:

ChooseType()
BEGIN
LOCAL choice;
CHOOSE(choice,"Matrix or Wronskian","Fundamental Matrix","Wronskian");
RETURN choice;
END;

#cas
// Wronskian - input a [vector] of functions
// i.e. W(f,g)= fg'-f'g
wronskian(flist):=
BEGIN
local mat, s, j, k, d;
s:= size(flist);

IF (  (s < 2) OR (TYPE(flist) == 2) OR (TYPE(flist(1))==2) OR (TYPE(flist(2))==2) ) THEN return "Input a [vector] of functions";
ELSE
IF (instring(string(flist),"[")==0) THEN  MSGBOX("It would be better use [vector] in []"); END;
mat:= makemat(0, s, s);
mat[1]:= flist;
FOR j FROM 2 TO s DO // diff

FOR k FROM 1 TO s DO // items
d:=diff(mat[j-1,k],x);
mat[j, k]:= d;

END; // for k
END; // for j
IF EXPR(" wronskian.ChooseType()")=1 THEN
return mat;
ELSE
return det(mat);
END; // if

END; // 1st if

END;
#end

It is not so elegant (it's perhaps "folkloristic", but with
Code:

IF (instring(string(flist),"[")==0) THEN  MSGBOX("It would be better use [vector] in []"); END;
I control if a list has [ bracket, and if not the program accepts also input with {} or simple list a,b,c..., but with a MSGBOX with a warning... better than nothing

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
02-23-2015, 07:29 PM (This post was last modified: 02-23-2015 10:43 PM by Han.)
Post: #4
RE: Wronskian
Why not just convert it for the user? The code below assumes CAS-programming. In CAS view, SIZE() (note the upper case) will return a real number for a list delimited by [ ] and a CAS list (in the form [ row col ]) for a matrix.

Code:
IF TYPE(flist)==4 THEN
  s:=SIZE(flist);
  IF TYPE(s)==4 THEN
    // we have a matrix instead of a vector in [ ] form; maybe return error msg?
  ELSE
    // here is where we can assume flist is now in the proper [ ] form
  END;
END;

EDIT: it appears we don't even really need to convert; a list is a list in CAS regardless of the actual delimiters displayed.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-23-2015, 09:46 PM
Post: #5
RE: Wronskian
(02-23-2015 07:29 PM)Han Wrote:  Why not just convert it for the user? The code below assumes CAS-programming. In CAS view, SIZE() (note the upper case) will return a real number for a list delimited by [ ] and a CAS list (in the form [ row col ]) for a matrix.

thank you for your effort, Han.
I'm trying applying that to my program, but for now I'm not able to do it, willing also to save the other controls for type, number arguments...
Also the calculations should be changed; i.e. I create a mat with make mat(0, s, s), put in it flist, then (in two cycles) differentiate and add rows with mat[j,k]:=d
Now, having already a matrix... I'm a bit confused, sorry.

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
02-23-2015, 10:24 PM (This post was last modified: 02-23-2015 10:25 PM by Han.)
Post: #6
RE: Wronskian
Try this:
Code:
#cas
wronskm(f):=
BEGIN
  local s,v,m;

  s:=SIZE(f);
  IF s==0 OR TYPE(f)<>6 THEN RETURN("wronskm([f1,f2,...,fn])"); END;
  IF TYPE(s) THEN RETURN("wronskm: expecting vector of functions"); END;
  v:=lname(f);
  IF SIZE(v)<>1 THEN RETURN("wronskm: expecting single-variable functions"); END; 
  v:=v(1);
  m:=makemat((r,c)->diff(f(c),v$(r-1)),s,s);
  return m;
END;

wronsk(f):=
BEGIN
  local m;
  m:=wronskm(f);
  IF TYPE(m)==2 THEN
    return m;
  ELSE
    return det(m);
  END;
END;
#end

Usage: wronskm([f1,f2,...,fn]) returns the matrix; wronsk([f1,f2,...,fn]) returns the determinant of the matrix

The error message regarding single-variable functions can occur when:
1) the "variable" already exists and contains a value of some sort (e.g. exported from a non-CAS program, defined by user, or exists in the CAS view); OR
2) the functions in the vector of functions are multivariate (or are all constants)

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-23-2015, 10:51 PM (This post was last modified: 02-23-2015 11:00 PM by salvomic.)
Post: #7
RE: Wronskian
(02-23-2015 10:24 PM)Han Wrote:  Try this:
...
Usage: wronskm([f1,f2,...,fn]) returns the matrix; wronsk([f1,f2,...,fn]) returns the determinant of the matrix

The error message regarding single-variable functions can occur when:
1) the "variable" already exists and contains a value of some sort (e.g. exported from a non-CAS program, defined by user, or exists in the CAS view); OR
2) the functions in the vector of functions are multivariate (or are all constants)

thanks a lot!
this works well, and finally the controls are what I would have to get!

Please, can you tell me the use of the $ sign in diff() function?
Another think: wronsk(f) take also the IF in the other function, as I can see, is it right?
IF TYPE(s) THEN ... after the control of s==0 and <>6: controls it if there is another value for s?

You prefer two CAS function and it's ok also for me; another idea is to have only a command and a Choose routine, as in the my previous version...
Ad libitum Smile

Salvo

EDIT: variables r, c: shouldn't they be in local?

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
02-23-2015, 10:53 PM
Post: #8
RE: Wronskian
(02-23-2015 10:51 PM)salvomic Wrote:  Please, can you tell me the use of the $ sign in diff() function?

Example: x$2 means differentiate with respect to x twice.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-23-2015, 11:02 PM
Post: #9
RE: Wronskian
(02-23-2015 10:53 PM)Han Wrote:  Example: x$2 means differentiate with respect to x twice.

very interesting!
I didn't know that, than you...

please, see again my post above: I edited it while are writing...

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
02-24-2015, 12:49 AM
Post: #10
RE: Wronskian
(02-23-2015 10:51 PM)salvomic Wrote:  Another think: wronsk(f) take also the IF in the other function, as I can see, is it right?
IF TYPE(s) THEN ... after the control of s==0 and <>6: controls it if there is another value for s?

If I understand your question correctly, then the answer is no. Only one scenario is allowed: when the input f consists of a list. In CAS-view, the SIZE() command returns a real number if f is a list (whether of the form f1,f2,…,fn or [f1,f2,…,fn] or {f1,f2,…,fn} or any non-matrix type). Otherwise, SIZE() returns a list if f is a matrix. That is, if f is an m x n matrix, then SIZE(f) returns [m n]. So the first IF statement checks to see that an input was actually specified and that it necessarily a list. A symbolic matrix is just a list of lists (or, one can also think of it as a matrix). So after the first IF statement, the only possible scenario is that f is a list, or it is a list of lists. To determine which, we check the TYPE() of the SIZE() result. If the SIZE() command returned a real number, then we have a simple list (or we can think of it as a vector). Otherwise, if SIZE() is not a real number, then it could not be a simple list (likely a list of list, for example) and so we throw an error message.

Quote:EDIT: variables r, c: shouldn't they be in local?

No, they are automatically local, and local to the function (r,c)->diff(f(c), v$(r-1)) because they are simply placeholders (dummy variables).

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-24-2015, 06:32 AM
Post: #11
RE: Wronskian
(02-24-2015 12:49 AM)Han Wrote:  If I understand your question correctly, then the answer is no. ...

Quote:EDIT: variables r, c: shouldn't they be in local?

No, they are automatically local, and local to the function (r,c)->diff(f(c), v$(r-1)) because they are simply placeholders (dummy variables).

perfect!
thank you, now it is more clear for me.
Your code is also more compact than mine!

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: