Join/Login

Business Software

Open Source Software

For Vendors

Blog

About

More

Articles

Create

SourceForge Podcast

Site Documentation

Subscribe to our Newsletter

Support Request

For Vendors Help Create Join Login

Business Software

Open Source Software

SourceForge Podcast

Resources

Articles

Case Studies

Blog

Menu

Help

Create

Join

Login

Home

Browse

Octave Forge

symbolic

Octave Forge symbolic

A collection of packages providing extra functionality for GNU Octave

Brought to you by: adb014, ajanke, avinoamk, carandraug, and 13 others

Summary

Files

Reviews

Support

Package Releases

Mercurial ▾

bioinfo

struct

actuarial

control

mapping

geometry

image

linear-algebra

signal

More...

Git ▾

data-structures

divand

doctest

level-set

ltfat

symbolic

vibes

ocs

cgi

More...

Old mails

Old forum

Old code

Menu ▾ ▴

Browse Commits

Fork

Merge Requests 0

Forks 2

Branches

main

master

Tags

v0.0.2

v0.0.3

v0.0.4

v0.1.0

v0.1.1

v0.1.2

v0.1.3

v2.0.0

v2.1.0

v2.2.0

More Tags

[95e0be]: / inst / sympref.m  Maximize  Restore  History

Download this file

462 lines (419 with data), 13.7 kB

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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461

%% Copyright (C) 2014-2020, 2022-2024 Colin B. Macdonald

%% Copyright (C) 2017 NVS Abhilash

%% Copyright (C) 2017 Mike Miller

%% Copyright (C) 2022 Alex Vong

%%

%% 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 Command {} sympref @var{cmd}

%% @deftypefnx Command {} sympref @var{cmd} @var{args}

%% @deftypefnx Function {@var{r} =} sympref ()

%% @deftypefnx Function {@var{r} =} sympref (@var{cmd})

%% @deftypefnx Function {@var{r} =} sympref (@var{cmd}, @var{args})

%% Preferences for the Symbolic package.

%%

%% @code{sympref} can set or get various preferences and

%% configurations. The various choices for @var{cmd} and

%% @var{args} are documented below.

%%

%% Run @strong{diagnostics} on your system:

%% @example

%% @comment doctest: +SKIP

%% sympref diagnose

%% @print{} ...

%% @end example

%%

%%

%% @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, a unicode pretty printer is used 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

%% @group

%% sympref ipc

%% @result{} ans = default

%% @end group

%% @end example

%%

%% This default depends on your system. If you have loaded the

%% @uref{https://gitlab.com/gnu-octave/octave-pythonic, Pythonic package},

%% the default will be the @code{native} mechanism.

%% Otherwise, typically the @code{popen2} mechanism will be used,

%% which uses a pipe to communicate with Python.

%% If that doesn't work, try @code{sympref ipc system} which is

%% much slower, as a new Python process is started for each operation.

%%

%% Other options for @code{sympref ipc} include:

%% @itemize

%% @item @code{sympref ipc popen2}: force popen2 choice.

%% @item @code{sympref ipc native}: use the @code{py} interface to

%% interact directly with an embedded Python interpreter, e.g.,

%% provided by the Octave Pythonic package.

%% @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 file in @code{tempdir} and then call that.

%% For debugging, will 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, will not be supported long-term.

%% @end itemize

%%

%% Except for @code{native}, all of these communication interfaces

%% depend on the current @strong{Python executable}, which can be queried:

%% @example

%% @comment doctest: +SKIP

%% sympref python

%% @result{} ans = python

%% @end example

%% Changing this might help if you've installed

%% a local Python interpreter somewhere else on your system.

%% The value can be changed by setting the environment variable

%% @code{PYTHON}, which can be configured in the OS, or it can be

%% set within Octave using:

%% @example

%% @comment doctest: +SKIP

%% setenv PYTHON python3

%% setenv PYTHON $@{HOME@}/.local/bin/python

%% setenv PYTHON C:\Python\python.exe

%% sympref reset

%% @end example

%% If the environment variable is empty or not set, the package

%% uses a default setting (often @code{python}).

%%

%%

%% @strong{Reset}: reset the SymPy communication mechanism. This can be

%% useful after an error occurs and the connection with Python

%% becomes confused.

%%

%% @example

%% @group

%% sympref reset % doctest: +SKIP

%% @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

%%

%% Be @strong{quiet} by minimizing startup and diagnostics messages:

%% @example

%% @group

%% sympref quiet

%% @result{} ans = 0

%% sympref quiet on

%% sympref quiet default

%% @end group

%% @end example

%%

%% Report the @strong{version} number:

%%

%% @example

%% @group

%% sympref version

%% @result{} 3.2.1+

%% @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

if (isstruct (cmd))

assert (isequal (sort (fieldnames (cmd)), ...

sort ({'ipc'; 'display'; 'digits'; 'quiet'})), ...

'sympref: structure has incorrect field names')

settings = [];

sympref ('quiet', cmd.quiet)

sympref ('display', cmd.display)

sympref ('digits', cmd.digits)

sympref ('ipc', cmd.ipc)

return

end

switch lower(cmd)

case 'defaults'

settings = [];

settings.ipc = 'default';

sympref ('display', 'default')

sympref ('digits', 'default')

sympref ('quiet', 'default')

case 'version'

assert (nargin == 1)

varargout{1} = '3.2.1+';

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'

warning ('OctSymPy:deprecated', ...

'Debugging mode "snippet" has been removed');

case 'quiet'

if (nargin == 1)

varargout{1} = settings.quiet;

else

if (strcmpi(arg, 'default'))

settings.quiet = false;

else

settings.quiet = tf_from_input(arg);

end

end

case 'python'

if (nargin ~= 1)

error('old syntax ''sympref python'' removed; use ''setenv PYTHON'' instead')

end

pyexec = getenv('PYTHON');

if (isempty(pyexec))

pyexec = defaultpython ();

end

varargout{1} = pyexec;

case 'ipc'

if (nargin == 1)

varargout{1} = settings.ipc;

else

verbose = ~sympref('quiet');

sympref('reset')

settings.ipc = arg;

switch arg

case 'default'

msg = 'Choosing the default [autodetect] communication mechanism';

case 'native'

msg = 'Forcing the native Python/C API communication mechanism';

case 'system'

msg = 'Forcing the system() communication mechanism';

case 'popen2'

msg = 'Forcing the popen2() communication mechanism';

case 'systmpfile'

msg = 'Forcing systmpfile ipc: warning: this is for debugging';

case 'sysoneline'

msg = 'Forcing sysoneline ipc: warning: this is for debugging';

otherwise

msg = '';

if (~ ischar (arg))

arg = num2str (arg);

end

warning('OctSymPy:sympref:invalidarg', ...

'Unsupported IPC mechanism ''%s'': hope you know what you''re doing', ...

arg)

end

if (verbose)

disp(msg)

end

end

case 'reset'

verbose = ~sympref('quiet');

python_ipc_driver('reset', []);

%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

case 'diagnose'

if (strcmp (lower (sympref ('ipc')), 'default') && ...

exist ('pyversion') && exist ('pyexec') && exist ('pyeval'))

% TODO: see note in ipc_native

assert_pythonic_and_sympy (true)

else

assert_have_python_and_sympy (sympref ('python'), true)

end

otherwise

error ('sympref: invalid preference or command ''%s''', lower (cmd));

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

%!shared sympref_orig

%! sympref_orig = sympref ();

%!test

%! % test quiet, side effect of making following tests a bit less noisy!

%! sympref quiet on

%! a = sympref('quiet');

%! assert(a == 1)

%% Test for correct line numbers in Python exceptions. These first tests

%% happen with the default ipc---usually popen2. We don't explicitly test

%% popen2 because test suite should be portable.

%!error <line 1> pycall_sympy__ ('raise ValueError');

%!error <line 1> pycall_sympy__ ('raise ValueError', sym('x'));

%!error <line 1> pycall_sympy__ ('raise ValueError', sym([1 2 3; 4 5 6]));

%!error <line 1> pycall_sympy__ ('raise ValueError', {1; 1; 1});

%!error <line 1> pycall_sympy__ ('raise ValueError', struct('a', 1, 'b', 'word'));

%!error <line 2> pycall_sympy__ ( {'x = 1' 'raise ValueError'} );

%!error <line 3> pycall_sympy__ ( {'x = 1' 'pass' '1/0'} );

%!error <line 3> pycall_sympy__ ( {'a=1' 'b=1' 'raise ValueError' 'c=1' 'd=1'} );

%% Test for correct line error in Python exceptions.

%!error <raise ValueError> pycall_sympy__ ('raise ValueError');

%!error <raise ValueError> pycall_sympy__ ('raise ValueError', sym('x'));

%!error <raise ValueError> pycall_sympy__ ('raise ValueError', sym([1 2 3; 4 5 6]));

%!error <raise ValueError> pycall_sympy__ ('raise ValueError', {1; 1; 1});

%!error <raise ValueError> pycall_sympy__ ('raise ValueError', struct('a', 1, 'b', 'word'));

%!error <raise ValueError> pycall_sympy__ ( {'x = 1' 'raise ValueError'} );

%!error <1/0> pycall_sympy__ ( {'x = 1' 'pass' '1/0'} );

%!error <raise ValueError> pycall_sympy__ ( {'a=1' 'b=1' 'raise ValueError' 'c=1' 'd=1'} );

%!test

%! % system should work on all system, but just runs sysoneline on windows

%! sympref('ipc', 'system');

%! syms x

%!error <line 1> pycall_sympy__ ('raise ValueError')

%!error <line 1> pycall_sympy__ ('raise ValueError', sym('x'))

%!error <line 1> pycall_sympy__ ('raise ValueError', struct('a', 1, 'b', 'word'))

%!error <c=1; raise ValueError> pycall_sympy__ ({'a=1' 'b=1' 'c=1; raise ValueError' 'd=1'});

%!test

%! % sysoneline should work on all systems

%! sympref('ipc', 'sysoneline');

%! syms x

%!error <line 1> pycall_sympy__ ('raise ValueError')

%!error <line 1> pycall_sympy__ ('raise ValueError', struct('a', 1, 'b', 'word'))

%!test

%! sympref('ipc', 'systmpfile');

%! syms x

%!error <line 1> pycall_sympy__ ('raise ValueError')

%!error <line 1> pycall_sympy__ ('raise ValueError', struct('a', 1, 'b', 'word'))

%!test

%! s = warning ('off', 'OctSymPy:sympref:invalidarg');

%! sympref ('ipc', 'bogus');

%! assert (strcmp (sympref ('ipc'), 'bogus'))

%! warning (s)

%!error <invalid ipc mechanism> syms ('x')

%!test

%! sympref('defaults')

%! assert(strcmp(sympref('ipc'), 'default'))

%! sympref('quiet', 'on')

%!test

%! % restore sympref from structure

%! old = sympref ();

%! sympref ('display', 'ascii');

%! sympref ('digits', 64);

%! old = orderfields (old); % re-ordering the fields should be ok

%! sympref (old);

%! new = sympref ();

%! assert (isequal (old, new))

%!error <incorrect field names>

%! s.a = 'hello';

%! s.b = 'world';

%! sympref (s)

%!test

%! syms x

%! sympref('reset');

%! % restore original sympref settings

%! sympref ('ipc', sympref_orig.ipc);

%! syms x

%! sympref ('quiet', sympref_orig.quiet);

%!error <invalid preference or command> sympref ('nosuchsetting')

%!error <invalid preference or command> sympref ('nosuchsetting', true)

SourceForge

Create a Project

Open Source Software

Business Software

Top Downloaded Projects

Company

About

Team

SourceForge Headquarters
225 Broadway Suite 1600
San Diego, CA 92101
+1 (858) 422-6466

Resources

Support

Site Documentation

Site Status

SourceForge Reviews

© 2025 Slashdot Media. All Rights Reserved.

Terms Privacy Opt Out Advertise

×
Want the latest updates on software, tech news, and AI?

Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.

Submit