Escolar Documentos
Profissional Documentos
Cultura Documentos
Also note that a pure constant has no variable defined, so an empty variable
name ('') will be used.
z = sympoly(1);
struct(z)
ans =
Var: {''}
Exponent: 0
Coefficient: 1
The fundamental tool for any class is the class constructor. This is the
function sympoly. Sympoly has several different modes of operation.
It can be called as a function
p = sympoly('x');
In which case the linear sympoly 'x' will be created and put into the variable p.
Sympoly can also be used on a numeric variable, thus
p = sympoly(0);
will generate a constant (0) sympoly. Likewise,
p = sympoly(eye(3));
will generate an array of sympoly constants.
Finally, sympoly can be called as a command, thus
sympoly a b c x y z w
will generate 7 sympoly variables with the designated names, assigning
them into the current workspace.
Finally, an important issue in sympoly arithmetic is that arithmetic with
-->
-->
-->
-->
-->
-->
-->
-->
-->
-->
-->
-->
plus.m
uminus.m
uplus.m
minus.m
times.m
mtimes.m
rdivide.m
mrdivide.m
power.m
mpower.m
horzcat.m
vercat.m
-->
-->
-->
-->
-->
-->
double(p)
sum(p)
prod(p)
-->
-->
-->
3. Helper functions
A few functions were useful to do basic bookkeeping and clean up on a sympoly.
- equalize_vars.m is a tool that takes a pair of sympolys, extracts their
respective list of variables, then uses union on those lists to define
a common list of variables, expanding the exponents in each sympoly
as appropriate.
- clean_sympoly.m is a tool to do garbage collection on a sympoly. It uses
consolidator to merge terms with the same exponent sets, summing the
corresponding coefficients. (This is neat, in that this entire aggregation
process can be achieved in one line.) Other garbage collection process
are removal of terms with a zero coefficients, and removal of variables
that have entirely zero exponents.
4. Additional functions
Once the main operators are fully implemented, sympoly functionality is
easy enough to extend.
I did need to work at a low level inside a sympoly to write int.m, but then
defint calls subs twice, and subtracts the results.
- int.m --> indefinite integral of a sympoly
- defint.m --> definite integral of a sympoly
- adjoint.m --> adjoint matrix of a square sympoly array. While I cannot
easily write code for the actual inverse matrix of a sympoly, because
this would require the ability to manipulate the larger family of rational
polynomials. (See my comments below on extensions.) We can write
the inverse of a matrix M as
inv(M) = adjoint(M)/det(M)
- orthpoly.m --> generates orthogonal polynomials. Orthogonal polynomials
are easy to generate using a 3 term recurrence relationship. Orthpoly
does this, all I needed to do was to program the proper coefficients in
that 3 term recurrence for each family of orthogonal polynomials.
- gaussquadrule.m - Once I had orthpoly, gaussian quadrature nodes
and weights are also a trivial extension.
5. Extensions
As it turns out, an easy project for a student with a desire for completeness
is to write a second class for rational polynomials. I actually did this once.
Each ratpoly was merely a container for a pair of sympoly objects. In turn,
arithmetic between a ratpoly and a scalar, or a ratpoly and a sympoly, etc.,
was all handled by the sympoly class operators themselves, with a little
help from the basic laws of algebra as they apply to terms with a numerator
and a denominator.
Another fun item that is easily enough done is overloading the regress
or polyfit function, allowing you to estimate the coefficients 'a' and 'b', from
the model y - (a + b*x) = 0. Perhaps I'll do this by the next release.