Diff of /trunk/octave-forge/main/symbolic/inst/findsym.m [r8777] .. [r8778] Maximize Restore

  Switch to unified view

a/trunk/octave-forge/main/symbolic/inst/findsym.m b/trunk/octave-forge/main/symbolic/inst/findsym.m
1
## Copyright (C) 2003 Willem J. Atsma
1
## Copyright (C) 2003 Willem J. Atsma <watsma@users.sf.net>
2
##
2
##
3
## This program is free software; you can redistribute it and/or
3
## This program is free software; you can redistribute it and/or
4
## modify it under the terms of the GNU General Public
4
## modify it under the terms of the GNU General Public
5
## License as published by the Free Software Foundation;
5
## License as published by the Free Software Foundation;
6
## either version 2, or (at your option) any later version.
6
## either version 2, or (at your option) any later version.
...
...
14
## You should have received a copy of the GNU General Public
14
## You should have received a copy of the GNU General Public
15
## License along with this software; see the file COPYING.  If not,
15
## License along with this software; see the file COPYING.  If not,
16
## see <http://www.gnu.org/licenses/>.
16
## see <http://www.gnu.org/licenses/>.
17
17
18
## -*- texinfo -*-
18
## -*- texinfo -*-
19
## @deftypefn {Function File} {} @var{vars} = findsym (@var{f}, @var{n})
19
## @deftypefn {Function File} {@var{vars} =} findsym (@var{f}, @var{n})
20
## Find symbols in expression @var{f} and return them comma-separated in
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}
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.
22
## is specified, the @var{n} symbols closest to "x" are returned.
23
##
23
##
24
## Example:
24
## Example:
25
## @example
25
## @example
26
## symbols
26
## symbols
27
## x=sym("x"); y=sym("y"); f=x^2+3*x*y-y^2;
27
## x     = sym ("x");
28
## y     = sym ("y");
29
## f     = x^2+3*x*y-y^2;
28
## vars = findsym (f);
30
## vars  = findsym (f);
29
## vars2 = findsym (f,1);
31
## vars2 = findsym (f,1);
30
## @end example
32
## @end example
31
##
33
##
32
## This is intended for m****b compatibility, calls findsymbols().
34
## This is intended for m****b compatibility, calls findsymbols().
35
## @seealso{findsymbols}
33
## @end deftypefn
36
## @end deftypefn
34
## @seealso{findsymbols}
35
36
## Author: Willem J. Atsma <watsma(at)users.sf.net>
37
## Created: 18 April 2003
38
37
39
function VARS = findsym(F,Nout)
38
function VARS = findsym(F,Nout)
40
39
41
symlist = findsymbols(F);
40
  symlist = findsymbols(F);
42
Nlist = length(symlist);
41
  Nlist = length(symlist);
43
if Nlist==0
42
  if Nlist==0
44
  warning("No symbols were found.")
43
    warning("No symbols were found.")
45
  VARS = "";
44
    VARS = "";
46
  return
45
    return
47
endif
46
  endif
48
47
49
if exist("Nout")!=1
48
  if exist("Nout")!=1
50
  VARS = disp(symlist{1});
49
    VARS = disp(symlist{1});
51
  for i=2:Nlist
50
    for i=2:Nlist
52
      VARS = [VARS "," disp(symlist{i})];
51
      VARS = [VARS "," disp(symlist{i})];
53
  endfor
52
    endfor
54
  return
53
    return
55
else
54
  else
56
  # If Nout is specified, sort anew from x.
55
    ## If Nout is specified, sort anew from x.
57
  symstrings = disp(symlist{1});
56
    symstrings = disp(symlist{1});
58
  for i=2:Nlist
57
    for i=2:Nlist
59
      symstrings = [symstrings ; disp(symlist{i})];
58
      symstrings = [symstrings ; disp(symlist{i})];
60
  endfor
59
    endfor
61
60
62
  symasc = toascii(symstrings);
61
    symasc = toascii(symstrings);
63
  
64
  if Nlist<Nout
65
      warning("Asked for %d, variables, only %d found.",Nout,Nlist);
66
      Nout=Nlist;
67
  endif
68
  symasc(:,1) = abs(toascii("x")-symasc(:,1));
69
62
70
  # Sort by creating an equivalent number for each entry
63
    if Nlist<Nout
71
  Nc = length(symasc(1,:));
64
      warning("Asked for %d, variables, only %d found.",Nout,Nlist);
72
  powbase=zeros(Nc,1); powbase(Nc)=1;
65
      Nout=Nlist;
73
  for i=(Nc-1):-1:1
66
    endif
74
      powbase(i) = powbase(i+1)*128;
67
    symasc(:,1) = abs(toascii("x")-symasc(:,1));
75
  endfor
76
  [xs,I]=sort(symasc*powbase);
77
  
78
  VARS = deblank(symstrings(I(1),:));
79
  
80
  for i=2:Nout
81
      VARS = [VARS "," deblank(symstrings(I(i),:))];
82
  endfor
83
68
69
    ## Sort by creating an equivalent number for each entry
70
    Nc = length(symasc(1,:));
71
    powbase=zeros(Nc,1); powbase(Nc)=1;
72
    for i=(Nc-1):-1:1
73
      powbase(i) = powbase(i+1)*128;
74
    endfor
75
    [xs,I]=sort(symasc*powbase);
76
77
    VARS = deblank(symstrings(I(1),:));
78
79
    for i=2:Nout
80
      VARS = [VARS "," deblank(symstrings(I(i),:))];
81
    endfor
82
84
endif
83
  endif
85
% test the example
84
endfunction
85
86
%!test
86
%!test
87
%! symbols
87
%! symbols
88
%! x=sym("x"); y=sym("y"); f=x^2+3*x*y-y^2;
88
%! x=sym("x"); y=sym("y"); f=x^2+3*x*y-y^2;
89
%! vars = findsym (f);
89
%! vars = findsym (f);
90
%! assert(vars, 'x,y');
90
%! assert(vars, 'x,y');
91
%! vars2 = findsym (f,1);
91
%! vars2 = findsym (f,1);
92
%! assert(vars2, 'x');
92
%! assert(vars2, 'x');
93