Skip to content

Commit 96114f5

Browse files
committed
Add method to convert a sym to a pyobject
This is for use with the Pytave project.
1 parent eced56f commit 96114f5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

inst/@sym/pyobject.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
%% Copyright (C) 2016 Colin B. Macdonald
2+
%%
3+
%% This file is part of OctSymPy.
4+
%%
5+
%% OctSymPy is free software; you can redistribute it and/or modify
6+
%% it under the terms of the GNU General Public License as published
7+
%% by the Free Software Foundation; either version 3 of the License,
8+
%% or (at your option) any later version.
9+
%%
10+
%% This software is distributed in the hope that it will be useful,
11+
%% but WITHOUT ANY WARRANTY; without even the implied warranty
12+
%% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13+
%% the GNU General Public License for more details.
14+
%%
15+
%% You should have received a copy of the GNU General Public
16+
%% License along with this software; see the file COPYING.
17+
%% If not, see <http://www.gnu.org/licenses/>.
18+
19+
%% -*- texinfo -*-
20+
%% @documentencoding UTF-8
21+
%% @defmethod @@sym pyobject (@var{x})
22+
%% Convert/cast a sym object to a pyobject.
23+
%%
24+
%% Example:
25+
%% @example
26+
%% @group
27+
%% syms x
28+
%% f = 2*sin(x/2);
29+
%% pyobject(f)
30+
%% @result{} ans = [pyobject ...]
31+
%%
32+
%% 2*sin(x/2)
33+
%%
34+
%% @end group
35+
%% @end example
36+
%% @seealso{@@sym/char, py, @@pyobject/pyobject}
37+
%% @end defmethod
38+
39+
40+
function y = pyobject (x)
41+
42+
y = py.sympy.S (char (x));
43+
44+
% Above is nice because it needs no imports. But downside is some
45+
% things like NonElementaryIntegral and MatrixElement will fail. See
46+
% e.g., commit 51a80384d5caea0db211e452d39f0b4f6b3778cc
47+
48+
% Alternatively, with many imports (see python_header.py), this works:
49+
%y = pyeval (char (x));
50+
51+
end
52+
53+
54+
%!assert (isa (pyobject (sym ('x')), 'pyobject'))

0 commit comments

Comments
 (0)