Hi, I am using the symbolic package (so include `pkg load symbolic` before the following commands if necessary). If I run:syms x fsym = x * cos(x) fsymder = diff(fsym) vpa(subs(fsymder, x, 3.141592654), 8) warning: Using rat() heuristics for doubleprecision input (is this what you wanted?) warning: called from sym at line 256 column 9 subs at line 118 column 7 ans = (sym) 1.0000000 
On Fri, Feb 17, 2017 at 6:00 AM, Stephen Biggs <[hidden email]> wrote: Try these ideas

In reply to this post by stevebiggs
On Fri, Feb 17, 2017 at 6:00 AM, Stephen Biggs <[hidden email]> wrote: I am thinking of putting this on the wiki. ## Demo of how to use a number (which was calculated in octave ## variables) in a symbolic calculation. ## use octave to calculate some number: a=pi/2 ## now do some work with the symbolic pkg syms x f = x * cos(x) df = diff(fsym) ## Next get the number into a symbolic variable ## convert to string first aa=mat2str(a) ## and now to a symbolic variable. ww=sym(aa) ## and now use it vpa(subs(df, x, ww), 28) 
On Fri, Feb 17, 2017 at 10:48 AM, Doug Stewart <[hidden email]> wrote:
There was an error. this is better ## Demo of how to use a number (which was calculated in an octave ## variable) in a symbolic calculation, without getting a warning. ## use octave to calculate some number: a=pi/2 ## now do some work with the symbolic pkg syms x f = x * cos(x) df = diff(f) ## Next get the number into a symbolic variable ## convert to string first aa=mat2str(a) ## and now to a symbolic variable. ww=sym(aa) ## and now use it vpa(subs(df, x, ww), 28) 
I don't understand the symbolic package's precision propagation. It
seems to know e.g. pi to arbitrary precision:
vpa(pi,55) but calculations seem to be limited to 50 significant digits: vpa(pi/2,55) Is there something magic about 50? GNU Octave, version 4.0.3 
On Fri, Feb 17, 2017 at 11:42:26 0500, Przemek Klosowski wrote:
> I don't understand the symbolic package's precision propagation. It seems to > know e.g. pi to arbitrary precision: > > vpa(pi,55) > ans = (sym) 3.141592653589793238462643383279502884197169399375105821 > > but calculations seem to be limited to 50 significant digits: > > vpa(pi/2,55) > ans = (sym) 1.570796326794896557998981734272092580795288085937500000 > > Is there something magic about 50? No, there is something magic about the double value returned by pi. The function that converts numeric values has special code to test whether a given double value is exactly pi (to the extent of double precision), and replaces that with the string value "pi" for SymPy to evaluate. To get pi/2 with more digits, use vpa (pi, 55) / 2 The doc strings for both sym and vpa mention taking care when constructing symbolic or variableprecision values from double values that the user may have intended to represent an exact fraction.  mike 
In reply to this post by Przemek Klosowski7
On Fri, Feb 17, 2017 at 11:42 AM, Przemek Klosowski <[hidden email]> wrote:
try these 
In reply to this post by Doug Stewart4
On 17/02/17 08:18, Doug Stewart wrote:
> I am thinking of putting this on the wiki. Good idea! I can help edit. But first, the essential pattern here is: 1. Do a symbolic calculation (in this case, a derivative) 2. Evaluate that result for double values. "subs" is not really the right tool for this. It makes symbolic substitutions. Perhaps the right pattern here is: >> df = diff(...) >> dfh = function_handle(df) >> dfh(3.1415927) Here "dfh" is a regular plainold Octave anonymous function, can be evaluated efficiently for doubles, etc. Note: you can also write this as "dfh = matlabFunction(df)" for compatibility with the Matlab Symbolic Toolbox. There is some discussion about all this in https://github.com/cbm755/octsympy/issues/121 cheers, Colin 
In reply to this post by Mike Miller4
On 17/02/17 09:17, Mike Miller wrote:
> No, there is something magic about the double value returned by pi. > > The function that converts numeric values has special code to test > whether a given double value is exactly pi (to the extent of double > precision), and replaces that with the string value "pi" for SymPy to > evaluate. Yes: so using vpa('pi') is probably a good habit. Basically anytime you are passing a double into "sym" or "vpa", you should think carefully about what you are doing. I considered having "vpa" give the "rats" warning or something similar so "vpa(pi/2)" would give a warning to help folks learn this. But maybe its a valid thing to do, unlike "sym(1.23)" which almost certainly wrong. Colin 
In reply to this post by Mike Miller4
On 02/17/2017 12:17 PM, Mike Miller
wrote:
No, there is something magic about the double value returned by pi. The function that converts numeric values has special code to test whether a given double value is exactly pi (to the extent of double precision), and replaces that with the string value "pi" for SymPy to evaluate. Interesting.. this made me look at the rats() heuristics. E.g.
sym(e) shows that e is crudely approximated by 1457/536, which is
even less accurate than rats(e)(*) Specifically, shouldn't the rat() heuristics also use more
significant digits instead of the default 9? I say that because it
looks like vpa() arithmetic uses 50 significant digits at least
for vpa(pi/2), so asking for 50 in the rat() conversion(**) would
be more consistent. Or is 50 just an accident in the case of pi/2? p
(*) rats(e) is 2721/1001, which is what results from rats(e,9);
you have to drop the precision to 8 to get what the sym(e) uses:
rats(e,8) = 1457/536 (**) I do realize that rats() does double precision comparisons,
so it probably doesn't make sense to ask for more than 15 digits,
but still it's better than 8 
In reply to this post by Colin Macdonald2
On Feb 17, 2017 12:44 PM, "Colin Macdonald" <[hidden email]> wrote: I already did the wiki before I saw this.
If you want to change it goahead, if you want me to change something then let me know. 
On 17/02/17 11:14, Doug Stewart wrote:
> I already did the wiki before I saw this. > If you want to change it goahead, if you want me to change something > then let me know. Thanks for [doing that](https://wiki.octave.org/Symbolic_package) I edited it and moved it to the top, as probably more relevant then some of the more advanced examples. Colin 
In reply to this post by Przemek Klosowski7
On 17/02/17 12:26, Przemek Klosowski wrote:
> On 02/17/2017 02:45 PM, Colin Macdonald wrote: >> 50 is just an accident. I don't think its even really 50: I suspect >> if you check its not accurate much past 15. > You're right, of course: > > 1.570796326794896557998981734272092580795288085937500000 octave > 1.570796326794896619231321691639751442098584699687552910 maxima > > 1.234567890123456 > > Still, rats(...,16) maybe is better? Maybe also more assertive error > message, e.g. "using the rational approximation, limiting accuracy to > approx. single precision" I'm not sure... it would be kind of annoying to get something like "24689/12344*pi" instead of "pi/2". I'd be willing to revisit this, if there is a more sensible default. Can you help? File an issue [1], and we'll continue discussing there. [1] https://github.com/cbm755/octsympy/issues/ One option is to not use "rats" and have SymPy do the conversion: ```` >>> Rational(2.3) 2589569785738035/1125899906842624 >>> Rational(1.25) 5/4 >>> Rational(1.24) 5584463537939415/4503599627370496 >>> Rational(math.pi/2) 884279719003555/562949953421312 ```` I suspect (but have not verified) that these are the rational numbers corresponding to the exact value of the given floating point numbers. Perhaps that is the best behaviour... What do you think? thanks, Colin 
Free forum by Nabble  Edit this page 