Quantcast

Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

stevebiggs
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)

Then I get the following:

    warning: Using rat() heuristics for double-precision 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

So the answer is correct but there is that warning about rat() heuristics. How can I fix it so that the warning is not given. NB: I would rather **fix** the code so that the warning is not raised rather than just supressing the warning with `warning('off', '<warning_id>')`. However, if the former is not possible, then I will accept the latter but I need to know the warning ID.

Many thanks,
Steve



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Doug Stewart-4


On Fri, Feb 17, 2017 at 6:00 AM, Stephen Biggs <[hidden email]> wrote:
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)

Then I get the following:

    warning: Using rat() heuristics for double-precision 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

So the answer is correct but there is that warning about rat() heuristics. How can I fix it so that the warning is not given. NB: I would rather **fix** the code so that the warning is not raised rather than just supressing the warning with `warning('off', '<warning_id>')`. However, if the former is not possible, then I will accept the latter but I need to know the warning ID.

Many thanks,
Steve



Try these ideas

 syms x
    fsym = x * cos(x)
    fsymder = diff(fsym)
    
    xx=sym(pi)
    
    xxx=sym( "3.14159");
    
    
    syms q
    q=2.777123
    qq=mat2str(q)
    ww=sym(qq)
    
    vpa(subs(fsymder, x, xx), 8)


--
DASCertificate for 206392

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Doug Stewart-4
In reply to this post by stevebiggs


On Fri, Feb 17, 2017 at 6:00 AM, Stephen Biggs <[hidden email]> wrote:
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)

Then I get the following:

    warning: Using rat() heuristics for double-precision 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

So the answer is correct but there is that warning about rat() heuristics. How can I fix it so that the warning is not given. NB: I would rather **fix** the code so that the warning is not raised rather than just supressing the warning with `warning('off', '<warning_id>')`. However, if the former is not possible, then I will accept the latter but I need to know the warning ID.

Many thanks,
Steve



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)



Dopes that answer your question?

 


DASCertificate for 206392


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Doug Stewart-4


On Fri, Feb 17, 2017 at 10:48 AM, Doug Stewart <[hidden email]> wrote:


On Fri, Feb 17, 2017 at 6:00 AM, Stephen Biggs <[hidden email]> wrote:
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)

Then I get the following:

    warning: Using rat() heuristics for double-precision 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

So the answer is correct but there is that warning about rat() heuristics. How can I fix it so that the warning is not given. NB: I would rather **fix** the code so that the warning is not raised rather than just supressing the warning with `warning('off', '<warning_id>')`. However, if the former is not possible, then I will accept the latter but I need to know the warning ID.

Many thanks,
Steve



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)



Dopes that answer your question?

 




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)

--
DAS

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Przemek Klosowski-7
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?

GNU Octave, version 4.0.3
Octave was configured for "x86_64-redhat-linux-gnu".
OctSymPy v2.4.0: this is free software without warranty, see source.
Python 2.7.13 (default, Jan 12 2017, 17:59:37)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
OctSymPy: Communication established.  SymPy v1.0.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Mike Miller-4
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 variable-precision values from double values
that the user may have intended to represent an exact fraction.

--
mike

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Doug Stewart-4
In reply to this post by Przemek Klosowski-7


On Fri, Feb 17, 2017 at 11:42 AM, Przemek Klosowski <[hidden email]> 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?

GNU Octave, version 4.0.3
Octave was configured for "x86_64-redhat-linux-gnu".
OctSymPy v2.4.0: this is free software without warranty, see source.
Python 2.7.13 (default, Jan 12 2017, 17:59:37)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
OctSymPy: Communication established.  SymPy v1.0.




The problem is that the interpreter  sees pi/2 as normal floats.

try these

vpa(pi/2,55)

q=sym(pi)
vpa(q/2,55)

# or
qq=sym(2)
vpa(pi/qq,55)



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Colin Macdonald-2
In reply to this post by Doug Stewart-4
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 plain-old 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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Colin Macdonald-2
In reply to this post by Mike Miller-4
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Przemek Klosowski-7
In reply to this post by Mike Miller-4
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Doug Stewart-4
In reply to this post by Colin Macdonald-2


On Feb 17, 2017 12:44 PM, "Colin Macdonald" <[hidden email]> wrote:
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 plain-old 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


I already did the wiki before I saw this.
If you want  to change it go-ahead, if you want me to change something then let me know. 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Colin Macdonald-2
On 17/02/17 11:14, Doug Stewart wrote:
> I already did the wiki before I saw this.
> If you want  to change it go-ahead, 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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Symbolica package warning: Using rat() heuristics for double-precision input (is this what you wanted?)

Colin Macdonald-2
In reply to this post by Przemek Klosowski-7
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

Loading...