/usr/share/octave/packages/symbolic-2.2.4/assumptions.m is in octave-symbolic 2.2.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | %% Copyright (C) 2014, 2015 Colin B. Macdonald
%%
%% This file is part of OctSymPy.
%%
%% OctSymPy 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 (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 <http://www.gnu.org/licenses/>.
%% -*- texinfo -*-
%% @documentencoding UTF-8
%% @deftypefn {Function File} {@var{A} =} assumptions ()
%% @deftypefnx {Function File} {@var{A} =} assumptions (@var{x})
%% @deftypefnx {Function File} {[@var{v}, @var{d}] =} assumptions (@var{x}, 'dict')
%% @deftypefnx {Function File} {@var{L} =} assumptions ('possible')
%% List assumptions on symbolic variables.
%%
%% The assumptions are returned as a cell-array of strings:
%% @example
%% @group
%% >> syms x y positive
%% >> syms n integer
%% >> assumptions
%% @result{} ans =
%% @{
%% [1,1] = n: integer
%% [1,2] = x: positive
%% [1,3] = y: positive
%% @}
%% >> f = sin(n*x);
%% >> assumptions(f)
%% @result{} ans =
%% @{
%% [1,1] = n: integer
%% [1,2] = x: positive
%% @}
%% >> assumptions(n)
%% @result{} ans =
%% @{
%% [1,1] = n: integer
%% @}
%% @end group
%% @end example
%%
%% With the optional second argument set to the string @code{'dict'},
%% return the assumption dictionaries in @var{d} corresponding
%% to the variables in @var{v}.
%%
%% You can also get a list of possible assumptions:
%% @example
%% @group
%% >> A = assumptions('possible');
%% >> sprintf('%s ', A@{:@})
%% @result{} ans = real positive negative integer even odd rational finite
%% @end group
%% @end example
%%
%% @seealso{sym, syms, assume, assumeAlso}
%% @end deftypefn
%% Author: Colin B. Macdonald
%% Keywords: symbolic
function [A, B] = assumptions(F, outp)
if ((nargin == 1) && ischar(F) && strcmp(F, 'possible'))
A = valid_sym_assumptions();
return
end
if ((nargin == 0) || isempty(F))
find_all_free_symbols = true;
else
find_all_free_symbols = false;
end
if (nargin <= 1)
outp = 'no';
end
if (find_all_free_symbols)
%% no input arguments
% find all syms, check each for free symbols
workspace = {};
context = 'caller';
S = evalin(context, 'whos');
evalin(context, '[];'); % clear 'ans'
for i = 1:numel(S)
workspace{i} = evalin(context, S(i).name);
end
F = findsymbols(workspace);
end
% Nice and easy on SymPy >= 0.7.7
% FIXME: See also, sym.m and syms.m, updates there?
cmd = {
'x = _ins[0]'
'outputdict = _ins[1]'
'd = x._assumptions.generator'
'if d == {}:'
' astr = ""'
'elif all(d.values()):' % all True so list them
' astr = ", ".join([str(i) for i in d.keys()])'
'else:' % more complicated case, just the raw dict
' astr = str(d)'
'if outputdict:'
' return (astr, d)'
'else:'
' return astr,' };
% FIXME: Deprecate 0.7.6.x. But on older SymPy we do some foolishness.
% Abbreviate certain assumption dicts to shorter equivalent forms.
% I look forward to deleting all this.
oldsympy = python_cmd('return sympy.__version__ == "0.7.5" or sympy.__version__.startswith("0.7.6"),');
if (oldsympy)
cmd = {
'x = _ins[0]'
'outputdict = _ins[1]'
'# saved cases to abbreviate later'
'adict_default = {"commutative":True}'
'adict_real = {"commutative":True, "complex":True, "hermitian":True, "imaginary":False, "real":True}'
'adict_pos = {"commutative":True, "complex":True, "hermitian":True, "imaginary":False, "negative":False, "nonnegative":True, "nonpositive":False, "nonzero":True, "positive":True, "real":True, "zero":False}'
'adict_neg = {"commutative":True, "complex":True, "hermitian":True, "imaginary":False, "negative":True, "nonnegative":False, "nonpositive":True, "nonzero":True, "positive":False, "prime":False, "composite":False, "real":True, "zero":False}'
'adict_odd = {"even":False, "nonzero":True, "commutative":True, "noninteger":False, "hermitian":True, "zero":False, "complex":True, "rational":True, "real":True, "integer":True, "imaginary":False, "odd":True, "irrational":False}'
'adict_even = {"real":True, "even":True, "commutative":True, "noninteger":False, "hermitian":True, "complex":True, "rational":True, "integer":True, "imaginary":False, "odd":False, "irrational":False}'
'adict_even_076 = {"real":True, "even":True, "commutative":True, "noninteger":False, "hermitian":True, "complex":True, "rational":True, "integer":True, "imaginary":False, "odd":False, "irrational":False}'
'adict_integer = {"real":True, "commutative":True, "noninteger":False, "hermitian":True, "complex":True, "rational":True, "integer":True, "imaginary":False, "irrational":False}'
'adict_rational = {"real":True, "commutative":True, "hermitian":True, "complex":True, "rational":True, "imaginary":False, "irrational":False}'
'if sympy.__version__.startswith("0.7.6"):'
' new076 = {"algebraic":True, "transcendental":False}'
' adict_integer.update(new076)'
' adict_even.update(new076)'
' adict_odd.update(new076)'
' adict_rational.update(new076)'
'adict = x.assumptions0'
'if adict == adict_default:'
' astr = ""'
' #adict={}'
'elif adict == adict_real:'
' astr = "real"'
' #adict = {"real":True}'
'elif adict == adict_pos:'
' astr = "positive"'
' #adict = {"positive":True}'
'elif adict == adict_neg:'
' astr = "negative"'
' #adict = {"negative":False}'
'elif adict == adict_integer:'
' astr = "integer"'
' #adict = {"integer":True}'
'elif adict == adict_even:'
' astr = "even"'
' #adict = {"even":True}'
'elif adict == adict_odd:'
' astr = "odd"'
' #adict = {"odd":True}'
'elif adict == adict_rational:'
' astr = "rational"'
' #adict = {"rational":True}'
'else:'
' astr = str(adict)'
' astr = astr.replace("True","1").replace("False","0").replace(": ",":")'
'#astr = str(x) + ": " + astr'
'if outputdict:'
' return (astr,adict)'
'else:'
' return (astr,)'
};
end
c = 0; A = {};
if strcmp(outp, 'dict')
B = {};
end
if (isempty(F))
return
end
s = findsymbols(F);
for i=1:length(s)
x = s{i};
if strcmp(outp, 'dict')
[astr, adict] = python_cmd(cmd, x, true);
if ~isempty(astr)
c = c + 1;
A{c} = x;
B{c} = adict;
end
else
astr = python_cmd(cmd, x, false);
if ~isempty(astr)
c = c + 1;
str = [x.flat ': ' astr];
A{c} = str;
%if c == 1
% A = str;
%elseif c == 2
% A = {A str};
%else
% A{c} = str;
%end
end
end
end
end
%!test
%! syms x
%! assert(isempty(assumptions(x)))
%!test
%! x = sym('x', 'positive');
%! a = assumptions(x);
%! assert(~isempty(strfind(a{1}, 'positive')))
%!test
%! syms x
%! assert(isempty(assumptions(x)))
%!test
%! clear % for matlab test script
%! syms x positive
%! assert(~isempty(assumptions()))
%! clear
%! assert(isempty(assumptions()))
%!test
%! A = {'real' 'positive' 'negative' 'integer' 'even' 'odd' 'rational'};
%! % FIXME: remove once SymPy 0.7.6 support deprecated
%! for i = 1:length(A)
%! x = sym('x', A{i});
%! a = assumptions(x);
%! assert(strcmp(a{1}, ['x: ' A{i}] ))
%! end
%!test
%! if (str2num(strrep(python_cmd ('return sp.__version__,'),'.',''))<=761)
%! disp('skipping: char(x) of assumptions suboptimal in <= 0.7.6.x')
%! else
%! A = assumptions('possible');
%! for i = 1:length(A)
%! x = sym('x', A{i});
%! a = assumptions(x);
%! assert(strcmp(a{1}, ['x: ' A{i}] ))
%! s1 = char(x);
%! s2 = ['Symbol(''x'', ' A{i} '=True)'];
%! assert (strcmp (s1, s2))
%! end
%! end
%!test
%! syms x positive
%! syms y real
%! syms z
%! f = x*y*z;
%! a = assumptions(f);
%! assert(length(a) == 2)
%! assert(~isempty(strfind(a{1}, 'positive')))
%! assert(~isempty(strfind(a{2}, 'real')))
%!test
%! % dict output
%! syms x positive
%! syms y real
%! syms z
%! f = x*y*z;
%! [v, d] = assumptions(f, 'dict');
%! assert(length(v) == 2)
%! assert(iscell(v))
%! assert(isa(v{1}, 'sym'))
%! assert(isa(v{2}, 'sym'))
%! assert(length(d) == 2)
%! assert(iscell(d))
%! assert(isstruct(d{1}))
%! assert(isstruct(d{2}))
%!test
%! %% assumptions on just the vars in an expression
%! clear % for matlab test script
%! syms x y positive
%! f = 2*x;
%! assert(length(assumptions(f))==1)
%! assert(length(assumptions())==2)
%!test
%! %% assumptions in cell/struct
%! clear % for matlab test script
%! syms x y z w positive
%! f = {2*x [1 2 y] {1, {z}}};
%! assert(length(assumptions())==4)
%! assert(length(assumptions(f))==3)
%! clear x y z w
%! assert(length(assumptions())==3)
%! assert(length(assumptions(f))==3)
|