Skip to content

Commit

Permalink
Octave compatibility of factor with sym input
Browse files Browse the repository at this point in the history
  • Loading branch information
lmendo committed May 27, 2021
1 parent e163e59 commit 71692eb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
50 changes: 50 additions & 0 deletions compatibility/factor_comp.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function y = factor(varargin)
% Fixes different output for symbolic input in Octave compared to Matlab, by using the two-output
% version in Octave
if ~isa(varargin{1},'sym')
y = builtin('factor', varargin{:});
else
% `builtin('@sym/factor', varargin{:})` does not work. So the following is taken from Octave's
% `sym/factor`, with a header and a footer to call the two-output version
nargout = 2;
f = varargin{1};
varargin = varargin(2:end);
for i = 1:length(varargin)
varargin{i} = sym(varargin{i});
end
if ((nargin > 1) || (~isempty (findsymbols (f))))
% have symbols, do polynomial factorization
if (nargout > 1)
print_usage ();
end
p = python_cmd ('return factor(*_ins)', f, varargin{:});
else
% no symbols: we are doing integer factorization
if (nargout <= 1)
if (~isscalar(f))
error('FIXME: check SMT, allows array input here?')
end
% this is rather fragile, as noted in docs
p = python_cmd ('return factorint(_ins[0], visual=True),', f);
else
if (~isscalar(f))
error('vector output factorization only for scalar integers')
end
cmd = { 'd = factorint(_ins[0], visual=False)'
'num = len(d.keys())'
'sk = sorted(d.keys())'
'p = sp.Matrix(1, num, sk)'
'm = sp.Matrix(1, num, lambda i,j: d[sk[j]])'
'return (p, m)' };
[p, m] = python_cmd (cmd, f);
end
end
m = double(m);
y = sym(NaN(1, sum(m)));
k = 1;
for h = 1:numel(m)
y(k:k+m(h)-1) = repmat(p(h), 1, m(h));
k = k+m(h);
end
end
end
2 changes: 1 addition & 1 deletion matl_compile.m
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
'triu' 'tril' 'randsample' 'nchoosek' 'vpa' 'sum' 'mean' 'prod' 'diff' 'mod' 'repelem' 'dec2bin' 'dec2base' ...
'hypergeom' 'disp' 'str2func' 'logical' 'circshift' 'pdist2' 'strsplit' 'max' 'min' 'strncmp' 'round'...
'datestr' 'regexp' 'regexprep' 'imshow' 'mat2str' 'blkdiag' 'strcat' 'str2num' 'str2double' 'cconv' ...
'gcd' 'lcm' 'fftn' 'mode' 'nnz' 'str2sym'};
'gcd' 'lcm' 'fftn' 'mode' 'nnz' 'str2sym' 'factor'};
verNumTh = [4 0 0]; % first version in which a modified function is not needed:
if (verNum(1)<verNumTh(1)) || ((verNum(1)==verNumTh(1)) && (verNum(2)<verNumTh(2))) || ((verNum(1)==verNumTh(1)) && (verNum(2)==verNumTh(2)) && (verNum(3)<verNumTh(3)))
fnames = [fnames {'colon'}];
Expand Down
Binary file modified spec/MATL_spec.pdf
Binary file not shown.
10 changes: 7 additions & 3 deletions spec/MATL_spec.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1427,9 +1427,10 @@ \subsection{MATL functions}
\item
Octave treats ranges specially to save memory\footnote{ \url{https://www.gnu.org/software/octave/doc/v4.2.0/Ranges.html}}. When applying function \matlab+nnz+ to a range an error occurs. This has been fixed.
\item
\matlab+factor+ applied to a symbolic scalar gives a symbolic array in MATLAB, but not in Octave.
% Not sure what it is
This hasn't been fixed.
\matlab+factor+ applied to a symbolic scalar gives a symbolic array with the factors in MATLAB, but something different in Octave.
% It's also symbolic, but I'm not sure what it is
This has been fixed.
% with the help of Ander Biguri's suggestion to use the two-output version of \matlab+factor+; see acknowledgments
\item
Octave's \matlab+reshape+ for symbolic arrays doesn't allow \matlab+[]+ as part of the shape specification. This hasn't been fixed.
\item
Expand Down Expand Up @@ -1507,6 +1508,9 @@ \section{Acknowledgments}
Thanks to the following people (in alphabetical order) for their contributions:
\begin{itemize}
\item
\user{@AnderBiguri} for his suggestion to use Octave's two-output version of \matlab+factor+, which was key to solving an Octave compatibility issue with this function applied to symbolic input.
% https://chat.stackoverflow.com/transcript/message/51809771#51809771
\item
\user{@AndrasDeak} for his very extensive testing of the compiler. Also for his help (together with \user{@beaker}) regarding a bug in Octave's \matlab+ismember+ function with complex inputs.
% https://chat.stackoverflow.com/transcript/message/40084076#40084076
\item
Expand Down

0 comments on commit 71692eb

Please sign in to comment.