From: <car...@us...> - 2011-09-19 18:11:30
|
Revision: 8569 http://octave.svn.sourceforge.net/octave/?rev=8569&view=rev Author: carandraug Date: 2011-09-19 18:11:24 +0000 (Mon, 19 Sep 2011) Log Message: ----------- multinom: new function for specfun package by Juan Pablo Carbajal. See artifact #3410842. Added Paths: ----------- trunk/octave-forge/main/specfun/inst/multinom.m Added: trunk/octave-forge/main/specfun/inst/multinom.m =================================================================== --- trunk/octave-forge/main/specfun/inst/multinom.m (rev 0) +++ trunk/octave-forge/main/specfun/inst/multinom.m 2011-09-19 18:11:24 UTC (rev 8569) @@ -0,0 +1,66 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 3 of the License, or +%% 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 <http://www.gnu.org/licenses/>. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {[@var{y} @var{alpha}] =} multinom (@var{x}, @var{n}) +%% @deftypefnx {Function File} {[@var{y} @var{alpha}] =} multinom (@var{x}, @var{n},@var{order}) +%% +%% Returns the terms (monomials) of the multinomial expansion of degree n. +%% @tex +%% $$ +%% (x1 + x2 + ... + xm)^n +%% $$ +%% @end tex +%% @ifnottex +%% +%% @example +%% (x1 + x2 + ... + xm)^n +%% @end example +%% +%% @end ifnottex +%% +%% @var{x} is a matrix where each column represents a different variable, the +%% output @var{y} has the same format. +%% The order of the terms is inherited from multinom_exp and can be controlled +%% through the optional argument @var{order}. The exponents are returned in @var{alpha}. +%% +%% @seealso{multinom_exp, multinom_coeff} +%% @end deftypefn + +function [y, alpha] = multinom(x,n,sortmethod) + + [nT, m] = size(x); + if nargin > 2 + alpha = multinom_exp(m,n,sortmethod); + else + alpha = multinom_exp(m,n); + end + na = size(alpha,1); + + y = prod(repmat(x,na,1).^kron(alpha,ones(nT,1)),2); + y = reshape(y,nT,na); + +end + +%!demo +%! n = 3; +%! t = linspace(-1,1,10).'; +%! x = [t-1/2, t]; +%! y = multinom(x,n,'descend'); +%! y_shouldbe = [x(:,1).^3 x(:,2).^3 x(:,1).^2.*x(:,2) x(:,1).*x(:,2).^2 ]; +%! plot(t,y_shouldbe); hold on; plot(t,y,'s'); hold off; +%! legend('x_1^3','x_2^3','x_1^2x_2','x_1x_2^2','location','southoutside',... +%! 'orientation','horizontal'); +%! title('Terms of the expansion of (x_1 + x_2)^3 (colors should match)'); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |