1 ## Copyright (C) 2003 Willem J. Atsma <watsma@users.sf.net>
3 ## This program is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU General Public
5 ## License as published by the Free Software Foundation;
6 ## either version 2, or (at your option) any later version.
8 ## This software is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied
10 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 ## PURPOSE. See the GNU General Public License for more
14 ## You should have received a copy of the GNU General Public
15 ## License along with this software; see the file COPYING. If not,
16 ## see <http://www.gnu.org/licenses/>.
19 ## @deftypefn {Function File} {@var{vars} =} findsym (@var{f}, @var{n})
20 ## Find symbols in expression @var{f} and return them comma-separated in
21 ## string @var{vars}. The symbols are sorted in alphabetic order. If @var{n}
22 ## is specified, the @var{n} symbols closest to "x" are returned.
30 ## vars = findsym (f);
31 ## vars2 = findsym (f,1);
34 ## This is intended for m****b compatibility, calls findsymbols().
35 ## @seealso{findsymbols}
38 function VARS = findsym(F,Nout)
40 symlist = findsymbols(F);
41 Nlist = length(symlist);
43 warning("No symbols were found.")
49 VARS = disp(symlist{1});
51 VARS = [VARS "," disp(symlist{i})];
55 ## If Nout is specified, sort anew from x.
56 symstrings = disp(symlist{1});
58 symstrings = [symstrings ; disp(symlist{i})];
61 symasc = toascii(symstrings);
64 warning("Asked for %d, variables, only %d found.",Nout,Nlist);
67 symasc(:,1) = abs(toascii("x")-symasc(:,1));
69 ## Sort by creating an equivalent number for each entry
70 Nc = length(symasc(1,:));
71 powbase=zeros(Nc,1); powbase(Nc)=1;
73 powbase(i) = powbase(i+1)*128;
75 [xs,I]=sort(symasc*powbase);
77 VARS = deblank(symstrings(I(1),:));
80 VARS = [VARS "," deblank(symstrings(I(i),:))];
88 %! x=sym("x"); y=sym("y"); f=x^2+3*x*y-y^2;
89 %! vars = findsym (f);
90 %! assert(vars, 'x,y');
91 %! vars2 = findsym (f,1);
92 %! assert(vars2, 'x');