-
Notifications
You must be signed in to change notification settings - Fork 35
sym() and vpa() refactor #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
267f7fe
58e05b9
8e7e40f
46153d9
2fdda3c
c55801e
5489ff5
9b02752
81dea89
4bc64da
4b02016
8b11cc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| %% Copyright (C) 2015-2017 Colin B. Macdonald | ||
| %% Copyright (C) 2016 Lagu | ||
| %% | ||
| %% 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 -*- | ||
| %% @deftypefun {@var{s} =} detect_special_str (@var{x}) | ||
| %% Recognize special constants, return their sympy string equivalent. | ||
| %% | ||
| %% Private helper function. | ||
| %% | ||
| %% This function should return a string @var{s} which can be instantiated | ||
| %% directly in Python such as @code{['return' @var{s}]}. If no special | ||
| %% value is found, it returns the empty string. | ||
| %% | ||
| %% @seealso{sym, vpa} | ||
| %% @end deftypefun | ||
|
|
||
| function s = detect_special_str (x) | ||
|
|
||
| % Table of special strings. Format for each row is: | ||
| % {list of strings to recognize}, resulting python expr | ||
| % Note: case sensitive | ||
| % Note: python expr should be in list for identity "sym(sympy(x)) == x" | ||
| table = {{'pi'} 'S.Pi'; ... | ||
| {'inf' 'Inf' 'oo'} 'S.Infinity'; ... | ||
| {'NaN' 'nan'} 'S.NaN'; ... | ||
| {'zoo'} 'S.ComplexInfinity'}; | ||
|
|
||
| s = ''; | ||
|
|
||
| assert (ischar (x)) | ||
|
|
||
| for j = 1:length (table) | ||
| for n = 1:length (table{j, 1}) | ||
| if (strcmp (x, table{j, 1}{n}) || strcmp (x, ['+' table{j, 1}{n}])) | ||
| s = table{j, 2}; | ||
| return | ||
| elseif (strcmp (x, ['-' table{j, 1}{n}])) | ||
| s = ['-' table{j, 2}]; | ||
| return | ||
| end | ||
| end | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| %% Copyright (C) 2017 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 -*- | ||
| %% @deftypefun {@var{y} =} double_to_sym_heuristic (@var{x}, @var{ratwarn}, @var{argnstr}) | ||
| %% Convert a double value to a nearby "nice" sym | ||
| %% | ||
| %% Private helper function. | ||
| %% | ||
| %% @end deftypefun | ||
|
|
||
| function y = double_to_sym_heuristic (x, ratwarn, argnstr) | ||
| assert (isempty (argnstr)) | ||
| if (isnan (x)) | ||
| y = python_cmd ('return S.NaN'); | ||
| elseif (isinf (x) && x < 0) | ||
| y = python_cmd ('return -S.Infinity'); | ||
| elseif (isinf (x)) | ||
| y = python_cmd ('return S.Infinity'); | ||
| elseif (isequal (x, pi)) | ||
| %% Special case for pi | ||
| y = python_cmd ('return S.Pi'); | ||
| elseif (isequal (x, -pi)) | ||
| y = python_cmd ('return -S.Pi'); | ||
| elseif ((abs (x) < flintmax) && (mod (x, 1) == 0)) | ||
| y = python_cmd ('return Integer(_ins[0])', int64 (x)); | ||
| else | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would like keep here some loop instead of check every var line by line....
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think is more easy to handle, or maybe with less code?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured you might say this ;-) I respect that point of view, but I disagree. To me a bunch if But if that list gets much longer in the future, then maybe I prefer your approach. |
||
| %% Find the nearest rational, rational*pi or sqrt(integer) | ||
| if (ratwarn) | ||
| warning('OctSymPy:sym:rationalapprox', ... | ||
| 'passing floating-point values to sym is dangerous, see "help sym"'); | ||
| end | ||
| [N1, D1] = rat (x); | ||
| [N2, D2] = rat (x / pi); | ||
| N3 = round (x^2); | ||
| err1 = abs (N1 / D1 - x); | ||
| err2 = abs ((N2*pi) / D2 - x); | ||
| err3 = abs (sqrt (N3) - x); | ||
| if (err1 <= err3) | ||
| if (err1 <= err2) | ||
| y = python_cmd ('return Rational(*_ins)', int64 (N1), int64 (D1)); | ||
| else | ||
| y = python_cmd ('return Rational(*_ins)*S.Pi', int64 (N2), int64 (D2)); | ||
| end | ||
| else | ||
| y = python_cmd ('return sqrt(Integer(*_ins))', int64 (N3)); | ||
| end | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking in the future, this is a great idea, to complement it i would like.. "split"? the differents methods to get the aproximation, so then we can get this file to be more easy to edit, i'm thinking to can add some "functions" and then the script run all in some loop and choose the best.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose that could be done in the future. Although I'm not sure there will be more heuristics: I read through the SMT docs and it sounds like these are the three cases they do. |
||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#804 (comment)