/usr/share/octave/packages/symbolic-2.2.4/sympref.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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | %% 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{r} =} sympref ()
%% @deftypefnx {Function File} {@var{r} =} sympref (@var{cmd})
%% @deftypefnx {Function File} {} sympref @var{cmd}
%% @deftypefnx {Function File} {} sympref @var{cmd} @var{args}
%% Preferences for the OctSymPy symbolic computing package.
%%
%% @code{sympref} can set or get various preferences and
%% configurations. The various choices for @var{cmd} and
%% @var{args} are documented below.
%%
%%
%% @strong{Python executable} path/command:
%% @example
%% @group
%% >> sympref python '/usr/bin/python' @c doctest: +SKIP
%% >> sympref python 'C:\Python\python.exe' @c doctest: +SKIP
%% >> sympref python 'N:\myprogs\py.exe' @c doctest: +SKIP
%% @end group
%% @end example
%% Default is an empty string; in which case OctSymPy just runs
%% @code{python} and assumes the path is set appropriately.
%%
%% @strong{Display} of syms:
%% @example
%% @group
%% >> sympref display
%% @result{} ans = unicode
%% @end group
%% @group
%% >> syms x
%% >> sympref display flat
%% >> sin(x/2)
%% @result{} (sym) sin(x/2)
%%
%% >> sympref display ascii
%% >> sin(x/2)
%% @result{} (sym)
%% /x\
%% sin|-|
%% \2/
%%
%% >> sympref display unicode
%% >> sin(x/2)
%% @result{} (sym)
%% ⎛x⎞
%% sin⎜─⎟
%% ⎝2⎠
%%
%% >> sympref display default
%% @end group
%% @end example
%% By default OctSymPy uses the unicode pretty printer to display
%% symbolic expressions. If that doesn't work (e.g., if you
%% see @code{?} characters) then try the @code{ascii} option.
%%
%% @strong{Communication mechanism}:
%% @example
%% >> sympref ipc
%% @result{} ans = default
%% @end example
%% The default will typically be the @code{popen2} mechanism which
%% uses a pipe to communicate with Python and should be fairly fast.
%% If that doesn't work, try @code{sympref display system} which is
%% much slower, as a new Python
%% process is started for each operation (many commands use more
%% than one operation).
%% Other options for @code{sympref ipc} include:
%% @itemize
%% @item @code{sympref ipc popen2}: force popen2 choice (e.g.,
%% on Matlab were it would not be the default).
%% @item @code{sympref ipc system}: construct a long string of
%% the command and pass it directly to the python interpreter with
%% the @code{system()} command. This typically assembles a multiline
%% string for the commands, except on Windows where a long one-line
%% string is used.
%% @item @code{sympref ipc systmpfile}: output the python commands
%% to a @code{tmp_python_cmd.py} file and then call that [for
%% debugging, may not be supported long-term].
%% @item @code{sympref ipc sysoneline}: put the python commands all
%% on one line and pass to @code{python -c} using a call to @code{system()}.
%% [for debugging, may not be supported long-term].
%% @end itemize
%%
%% @strong{Reset}: reset the SymPy communication mechanism. This can be
%% useful after an error occurs and the connection with Python
%% becomes confused.
%% @example
%% >> sympref reset @c doctest: +SKIP
%% @end example
%%
%% @strong{Snippets}: when displaying a sym object, we quote the SymPy
%% representation (or a small part of it):
%% @example
%% @group
%% >> syms x
%% >> y = [pi x];
%% >> sympref snippet on
%% >> y
%% @result{} y = (sym 1×2 matrix) “...([[pi, Symbol('x')]])”
%% [π x]
%% >> sympref snippet off
%% >> y
%% @result{} y = (sym) [π x] (1×2 matrix)
%% >> sympref snippet default
%% @end group
%% @end example
%%
%% @strong{Default precision}: control the number of digits used by
%% variable-precision arithmetic (see also the @ref{digits} command).
%% @example
%% @group
%% >> sympref digits % get
%% @result{} ans = 32
%% >> sympref digits 64 % set
%% >> sympref digits default
%% @end group
%% @end example
%%
%%
%% Report the @strong{version} number:
%% @example
%% @group
%% >> sympref version
%% @result{} 2.2.4
%% @end group
%% @end example
%%
%% @seealso{sym, syms}
%% @end deftypefn
function varargout = sympref(cmd, arg)
persistent settings
if (isempty(settings))
settings = 42;
sympref('defaults')
end
if (nargin == 0)
varargout{1} = settings;
return
end
switch lower(cmd)
case 'defaults'
settings = [];
settings.ipc = 'default';
settings.whichpython = '';
sympref ('display', 'default')
sympref ('digits', 'default')
sympref ('snippet', 'default')
case 'version'
assert (nargin == 1)
varargout{1} = '2.2.4';
case 'display'
if (nargin == 1)
varargout{1} = settings.display;
else
arg = lower (arg);
if (strcmp (arg, 'default'))
arg = 'unicode';
if (ispc () && (~isunix ()))
% Unicode not working on Windows, Issue #83.
arg = 'ascii';
end
end
assert(strcmp(arg, 'flat') || strcmp(arg, 'ascii') || ...
strcmp(arg, 'unicode'))
settings.display = arg;
end
case 'digits'
if (nargin == 1)
varargout{1} = settings.digits;
else
if (ischar(arg))
if (strcmpi(arg, 'default'))
arg = 32;
else
arg = str2double(arg);
end
end
arg = int32(arg);
assert(arg > 0, 'precision must be positive')
settings.digits = arg;
end
case 'snippet'
if (nargin == 1)
varargout{1} = settings.snippet;
else
if (strcmpi(arg, 'default'))
settings.snippet = false; % Should be false for a release
else
settings.snippet = tf_from_input(arg);
end
end
case 'python'
if (nargin == 1)
varargout{1} = settings.whichpython;
elseif (isempty(arg) || strcmp(arg,'[]'))
settings.whichpython = '';
sympref('reset')
else
settings.whichpython = arg;
sympref('reset')
end
case 'ipc'
if (nargin == 1)
varargout{1} = settings.ipc;
else
sympref('reset')
settings.ipc = arg;
switch arg
case 'default'
disp('Choosing the default [autodetect] octsympy communication mechanism')
case 'system'
disp('Forcing the system() octsympy communication mechanism')
case 'popen2'
disp('Forcing the popen2() octsympy communication mechanism')
case 'systmpfile'
disp('Forcing systmpfile ipc: warning: this is for debugging')
case 'sysoneline'
disp('Forcing sysoneline ipc: warning: this is for debugging')
otherwise
warning('Unsupported IPC mechanism: hope you know what you''re doing')
end
end
case 'reset'
disp('Resetting the octsympy communication mechanism');
r = python_ipc_driver('reset', []);
if (nargout == 0)
if (~r)
disp('Problem resetting');
end
else
varargout{1} = r;
end
%case 'path'
%pkg_path = fileparts (mfilename ('fullpath'));
% or
%pkg_l = pkg ('list');
%idx = strcmp ('octsympy', cellfun (@(x) x.name, pkg_l, "UniformOutput", false));
%if (~ any (idx))
% error ('the package %s is not installed', your_pkg);
%end
%pkg_path = pkg_l{idx}.dir
otherwise
print_usage ();
end
end
function r = tf_from_input(s)
if (~ischar(s))
r = logical(s);
elseif (strcmpi(s, 'true'))
r = true;
elseif (strcmpi(s, 'false'))
r = false;
elseif (strcmpi(s, 'on'))
r = true;
elseif (strcmpi(s, 'off'))
r = false;
elseif (strcmpi(s, '[]'))
r = false;
else
r = str2double(s);
assert(~isnan(r), 'invalid expression to convert to bool')
r = logical(r);
end
end
%!test
%! % system should work on all system, but just runs sysoneline on windows
%! fprintf('\nRunning some tests that reset the IPC and produce output\n');
%! sympref('ipc', 'system');
%! pause(1);
%! syms x
%! pause(2);
%!test
%! % sysoneline should work on all systems
%! fprintf('\n');
%! sympref('ipc', 'sysoneline');
%! pause(1);
%! syms x
%! pause(2);
%!test
%! sympref('defaults')
%! assert(strcmp(sympref('ipc'), 'default'))
%!test
%! fprintf('\n');
%! syms x
%! r = sympref('reset');
%! pause(1);
%! syms x
%! pause(2);
%! assert(r)
|