symbolic/ 0000755 0001750 0001750 00000000000 11647530220 013677 5 ustar carandraug carandraug symbolic/doc/ 0000755 0001750 0001750 00000000000 11647530220 014444 5 ustar carandraug carandraug symbolic/doc/INSTALL 0000644 0001750 0001750 00000002374 10473517121 015504 0 ustar carandraug carandraug
Prerequisites
-------------
You will need GiNaC installed before you can install this package. Go to
the GiNaC web site http://www.ginac.de and follow the download
instructions. It will tell you how to get cln and also how to build
GiNaC once you have built cln.
You will also need a version of Octave that was compiled without the
-fno-rtti or -fno-exceptions. Versions below octave-2.1.33 have these
options by default. These options are typically loacated in the
Makeconf file in the top level directory. After configure and before
compile you can remove these options if you want to use an older
version of octave.
Compiling
----------
Make sure that the correct version of mkoctfile is in your path. Then
just type gmake(or whatever you call you version of GNU Make) in the
top level directory.
Installing
----------
Just type "gmake INSTALL_PATH=/your/path install" and it will install
all the .oct files and .m files in /your/path. Add /your/path to your
octave path. Once you have it installed you will need to type symbols
at the octave command prompt each session to initialize the toolbox.
Then you can use all of the commands in the toolbox. Command completion
will not work on the commands until after yopu have used them once.
symbolic/doc/symbolic.html 0000644 0001750 0001750 00000017215 10764762165 017177 0 ustar carandraug carandraug
Octave Symbolic Manipulation Toolbox
The Octave Symbolic Manipulation Toolbox is based upon GiNaC
. The goal is to simply provide the capabilities of GiNaC in
the easy to use environment provided by Octave.
Limitations/Features
Currently there is no support for symbolic matrices. I think it would
require a few changes to the parser to do it nicely: for example:
sym_matrix = [x+1, x+5; x^2+4,x^2+2*x+1]; I could make a function
like sym_matrix(the_rows,the_columns,x+1, ... ) that returned a symbolic
matrix but this would be a bit of a kludge.
In order to do exact arithmetic you need to deal with strings and the
vpa command. For example: vpa("1")/vpa("7") is represented
internally as exactly 1/7. However, vpa("1")/7 or 1/vpa("7")
is an approximation to 1/7 that is accurate to roughly the accuracy of
the current value of digits.
GiNaC throws exceptions when there are problems with computations.
I handle some of them at this time, but I do not handle all of them.
This can cause octave to terminate prematurely. For example,
try vpa("1")/vpa("0"). This will eventually be fixed.
Download and install
You will need to install cln-1.0.1, GiNaC-0.8.0 and octave-2.1.33 or
later to use this package. You may be able to get by with an earlier
version of octave if you compiled without the "-fno-rtti -fno-exceptions"
options. This package uses both exceptions and run-time type
identification. There is an INSTALL file in the package
which will tell you how to install the package.
Functions
Below I provide a list of function that I have implemented or have
plans to implement as of the latest release . If the function name
is green then
the function is implemented and to the best of my knowledge there are no
problems with it. If the function is red
then it has not been implemented yet.
If the function is blue
then it has been implemented but is known not to work correctly.
Blues will appear only very rarely.
-
vpa - create a variable precision arithmetic
variable from a string, double, or an appropriate expression.
-
sym - create a symbolic variable
-
is_vpa - returns true if an object is
a vpa object
-
is_sym - return true if the argument
is a symbolic variable
-
is_ex - returns true if an object a
symbolic expression (i.e. x+y)
-
to_double - convert a vpa, ex or string
to a double value.
-
to_char - convert a vpa, ex to a string.
-
digits - set or view the number of digits
that newly created vpa object should have
-
Abs - Absolute value
-
csgn -
-
Sqrt - Sqrt(x) => x^(vpa(1)/2)
or x^(1/vpa(2))
-
Cos - the cosine of a sym, vpa , or
ex variable
-
Sin - the sine of a sym, vpa , or ex variable
-
Tan - the tangent of a sym, vpa , or ex variable
-
aCos - the inverse cosine of a sym,
vpa , or ex variable
-
aSin - the inverse sin of a sym, vpa
, or ex variable
-
aTan - the inverse tangent of a sym,
vpa , or ex variable
-
aTan2 -
-
Cosh - the hyperbolic cosine of a sym,
vpa , or ex variable
-
Sinh - the hyperbolic sine of a sym,
vpa , or ex variable
-
Tanh - the hyperbolic tangent of a sym,
vpa , or ex variable
-
aCosh - the inverse hyperbolic cosine of a
sym, vpa , or ex variable
-
aSinh - the inverse hyperbolic sine of a sym,
vpa , or ex variable
-
aTanh - the inverse hyperbolic tangent
of a sym, vpa , or ex variable
-
Exp - the cosine of a sym, vpa , or
ex variable
-
Log - the cosine of a sym, vpa , or
ex variable
-
Zeta -
-
Tgamma -
-
Lgamma -
-
Beta -
-
Factorial -
-
Binomial -
-
Order -
-
subs - perform a substitution in an expression
-
differentiate - differentiate an expression
-
expand - multiply all of the terms
in an expression out: (x+y)*(x+z) => x^2+x*y+x*z+y*z
-
collect - collect similar terms in an
already expanded expression
-
coeff - return the nth coefficient in
a polynomial
-
lcoeff - leading coefficient of a polynomial
(4x^2+2x+5 => 4)
-
tcoeff - trailing coefficient of a polynomial
(4x^2+2x+5 => 5)
-
degree - The degree of a polynomial
(i.e. x^2+2x+1 => 2)
-
ldegree - The low degree of a polynomial
(i.e. x^2+2x+1 => 0)
-
quotient -
-
remainder -
-
premainder -
-
unit -
-
content -
-
primpart -
-
Gcd - greatest common denominator of
a polynomial expression
-
Lcm - least common multiple of a polynomial
expression
-
numer -
-
denom -
-
normal - ?
-
to_rational -
-
Series -
-
Pi - pi evaluated to the current value
of digits accuracy.
-
splot -plot a symbolic functin over
a range of values
-
Saving of expressions, Retrieving of expressions
symbolic/inst/ 0000755 0001750 0001750 00000000000 11647530254 014663 5 ustar carandraug carandraug symbolic/inst/symfsolve.m 0000644 0001750 0001750 00000012206 11647265117 017074 0 ustar carandraug carandraug ## Copyright (C) 2003 Willem J. Atsma
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; If not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {[@var{x}, @var{inf}, @var{msg}] =} symfsolve (@dots{})
## Solve a set of symbolic equations using @command{fsolve}. There are a number of
## ways in which this function can be called.
##
## This solves for all free variables, initial values set to 0:
##
## @example
## symbols
## x=sym("x"); y=sym("y");
## f=x^2+3*x-1; g=x*y-y^2+3;
## a = symfsolve(f,g);
## @end example
##
## This solves for x and y and sets the initial values to 1 and 5 respectively:
##
## @example
## a = symfsolve(f,g,x,1,y,5);
## a = symfsolve(f,g,@{x==1,y==5@});
## a = symfsolve(f,g,[1 5]);
## @end example
##
## In all the previous examples vector a holds the results: x=a(1), y=a(2).
## If initial conditions are specified with variables, the latter determine
## output order:
##
## @example
## a = symfsolve(f,g,@{y==1,x==2@}); # here y=a(1), x=a(2)
## @end example
##
## The system of equations to solve for can be given as separate arguments or
## as a single cell-array:
##
## @example
## a = symfsolve(@{f,g@},@{y==1,x==2@}); # here y=a(1), x=a(2)
## @end example
##
## If the variables are not specified explicitly with the initial conditions,
## they are placed in alphabetic order. The system of equations can be comma-
## separated or given in a cell-array. The return-values are those of
## fsolve; @var{x} holds the found roots.
## @end deftypefn
## @seealso{fsolve}
function [ x,inf,msg ] = symfsolve (varargin)
## separate variables and equations
eqns = cell();
vars = cell();
if iscell(varargin{1})
if !strcmp(typeinfo(varargin{1}{1}),"ex")
error("First argument must be (a cell-array of) symbolic expressions.")
endif
eqns = varargin{1};
arg_count = 1;
else
arg_count = 0;
for i=1:nargin
tmp = disp(varargin{i});
if( iscell(varargin{i}) || ...
all(isalnum(tmp) || tmp=="_" || tmp==",") || ...
!strcmp(typeinfo(varargin{i}),"ex") )
break;
endif
eqns{end+1} = varargin{i};
arg_count = arg_count+1;
endfor
endif
neqns = length(eqns);
if neqns==0
error("No equations specified.")
endif
## make a list with all variables from equations
tmp=eqns{1};
for i=2:neqns
tmp = tmp+eqns{i};
endfor
evars = findsymbols(tmp);
nevars=length(evars);
## After the equations may follow initial values. The formats are:
## [0 0.3 -3 ...]
## x,0,y,0.3,z,-3,...
## {x==0, y==0.3, z==-3 ...}
## none - default of al zero initial values
if arg_count==nargin
vars = evars;
nvars = nevars;
X0 = zeros(nvars,1);
elseif (nargin-arg_count)>1
if mod(nargin-arg_count,2)
error("Initial value symbol-value pairs don't match up.")
endif
for i=(arg_count+1):2:nargin
tmp = disp(varargin{i});
if all(isalnum(tmp) | tmp=="_" | tmp==",")
vars{end+1} = varargin{i};
X0((i-arg_count+1)/2)=varargin{i+1};
else
error("Error in symbol-value pair arguments.")
endif
endfor
nvars = length(vars);
else
nvars = length(varargin{arg_count+1});
if nvars!=nevars
error("The number of initial conditions does not match the number of free variables.")
endif
if iscell(varargin{arg_count+1})
## cell-array of relations - this should work for a list of strings ("x==3") too.
for i=1:nvars
tmp = disp(varargin{arg_count+1}{i});
vars{end+1} = {sym(strtok(tmp,"=="))};
X0(i) = str2num(tmp((findstr(tmp,"==")+2):length(tmp)));
endfor
else
## straight numbers, match up with symbols in alphabetic order
vars = evars;
X0 = varargin{arg_count+1};
endif
endif
## X0 is now a vector, vars a list of variables.
## create temporary function:
symfn = sprintf("function Y=symfn(X) ");
for i=1:nvars
symfn = [symfn sprintf("%s=X(%d); ",disp(vars{i}),i)];
endfor
for i=1:neqns
symfn = [symfn sprintf("Y(%d)=%s; ",i,disp(eqns{i}))];
endfor
symfn = [symfn sprintf("endfunction")];
eval(symfn);
[x,inf,msg] = fsolve("symfn",X0);
endfunction
%!shared
% x = sym ("x");
% y = sym ("y");
% f = x ^ 2 + 3 * x - 1;
% g = x * y - y ^ 2 + 3;
%!test
% assert (symfsolve (f, g), [0.30278; -1.58727]', 1e-5);
%!test
% assert (symfsolve (f, g, x, 1, y, 5), [0.30278; 1.89004]', 1e-5);
%!test
% assert (symfsolve (f, g, {x==1,y==5}), [0.30278; 1.89004]', 1e-5);
%!test
% assert (symfsolve (f, g, [1 5]), [0.30278; 1.89004]', 1e-5);
%!test
% assert (symfsolve ({f, g}, {y==1,x==2}), [1.89004; 0.30278]', 1e-5);
symbolic/inst/sym2poly.m 0000644 0001750 0001750 00000010734 11647265117 016647 0 ustar carandraug carandraug ## Copyright (C) 2003 Willem J. Atsma
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public
## License as published by the Free Software Foundation;
## either version 2, or (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public
## License along with this software; see the file COPYING. If not,
## see .
## -*- texinfo -*-
## @deftypefn {Function File} {@var{c} =} sym2poly (@var{p}, @var{x})
## Returns the coefficients of the symbolic polynomial expression @var{p}
## as a vector. If there is only one free variable in @var{p} the
## coefficient vector @var{c} is a plain numeric vector. If there is more
## than one free variable in @var{p}, a second argument @var{x} specifies the
## free variable and the function returns a cell vector of symbolic expressions.
## The coefficients correspond to decreasing exponent of the free variable.
##
## Example:
## @example
## symbols
## x = sym("x");
## y = sym("y");
## c = sym2poly (x^2+3*x-4); # c = [1,3,-4]
## c = sym2poly (x^2+y*x,x); # c = @{2,y,0@}
## @end example
##
## If @var{p} is not a polynomial the result has no warranty.
##
## @seealso{poly2sym,polyval,roots}
## @end deftypefn
## Created: 18 April 2003
## Changed: 25 April 2003
## Removed the use of differentiate to get to coefficients - round-off
## errors cause problems. Now using newly created sumterms().
## Changed: 6 May 2003
## Removed the attempt to use ldegree(), degree() and coeff() - results
## with these are inconsistent.
function c = sym2poly(p,x)
BADPOLY_COEFF_LIMIT = 500;
if is_vpa(p)
## polynomial is one vpa number
c = to_double(p);
if length(c)!=1
error("Argument is not a polynomial.");
endif
return
endif
if !is_ex(p)
error("Argument has to be a symbolic expression.")
endif
pvars = findsymbols(p);
if isempty(pvars)
## It is possible that we get an expression without any symbols.
c = to_double(p);
return;
endif
nvars = length(pvars);
if nvars>1 && exist("x")!=1
error("Symbolic expression has more than 1 free variable; no variable specified.")
elseif exist("x")!=1
x = pvars{1};
endif
p = expand(p);
## GiNaC has commands to access coefficients directly, but in octave this often
## does not work, because for example x^2 typed in octave results in a
## non-integer power in GiNaC: x^2.0 .
[num,den] = numden(p);
tmp = findsymbols(den);
for i=1:length(tmp)
if tmp{i}==x
error("Symbolic expression is a ratio of polynomials.")
endif
endfor
p = expand(p);
p_terms = sumterms(p);
## if this is well behaved, I can find the coefficients by dividing with x
c_ex = cell;
for i=1:length(p_terms)
tmp = p_terms{i};
for j=1:BADPOLY_COEFF_LIMIT
if disp(differentiate(tmp,x))=="0"
break;
endif
tmp = tmp/x;
endfor
if j==BADPOLY_COEFF_LIMIT
printf("Please examine your code or adjust this function.\n");
printf("This error may occur because the passed expression is not a polynomial.\n");
error("Reached the set limit (%d) for the number of coefficients.",BADPOLY_COEFF_LIMIT)
endif
if (length(c_ex)