Skip to content
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

symbolic function args prevent forced conversion of result to numeric #17953

Closed
rwst opened this issue Mar 14, 2015 · 14 comments
Closed

symbolic function args prevent forced conversion of result to numeric #17953

rwst opened this issue Mar 14, 2015 · 14 comments

Comments

@rwst
Copy link

rwst commented Mar 14, 2015

It is generally preferred that the returned result type matches the argument type when calling a builtin function. For example this is as expected:

sage: factorial(8).parent()
Integer Ring
sage: factorial(SR(8)).parent()
Symbolic Ring
sage: exp(0).parent()
Integer Ring
sage: exp(SR(0)).parent()
Symbolic Ring

but this is not:

from sage.symbolic.function import BuiltinFunction

class TestFunction(BuiltinFunction):
    def __init__(self):
        BuiltinFunction.__init__(self, "testfun", nargs=2)
    def _eval_(self, n, x, *args, **kwds):
        print (parent(n), parent(x))
        return SR(5)

sage: TestFunction()(SR(1),GF(2)(1))
(Integer Ring, Finite Field of size 2)
5
sage: type(_)
<type 'int'>

An explanation could be that factorial and exp derive from GinacFunction.

CC: @jdemeyer

Component: symbolics

Author: Ralf Stephan

Branch/Commit: adc230d

Reviewer: Marc Mezzarobba

Issue created by migration from https://trac.sagemath.org/ticket/17953

@rwst rwst added this to the sage-6.6 milestone Mar 14, 2015
@rwst

This comment has been minimized.

@rwst rwst changed the title inconsistency in returned type of symbolic function result inconsistency in returned type of BuiltinFunction result Mar 14, 2015
@rwst
Copy link
Author

rwst commented Mar 15, 2015

@rwst
Copy link
Author

rwst commented Mar 15, 2015

New commits:

f0fe6f417953: any symbolic function arg prevents forced result conversion to numeric

@rwst
Copy link
Author

rwst commented Mar 15, 2015

Commit: f0fe6f4

@rwst
Copy link
Author

rwst commented Mar 15, 2015

Author: Ralf Stephan

@rwst rwst changed the title inconsistency in returned type of BuiltinFunction result symbolic function args prevent forced conversion of result to numeric Mar 15, 2015
@videlec
Copy link
Contributor

videlec commented Mar 21, 2015

comment:5

Hello,

First of all, I think that the content of the branch is ok (I am running the test right now).

But I found the following behavior weird

sage: exp(0).parent()
Integer ring

In my opinion, a function should have a well defined domain and range. In some cases, the function preserves some subset. Like the function x -> x^2 defined in the complex numbers preserves integers, rationals and algebraic. But considering that {0} is a subset of the real numbers on which exp preserve the integers is going too far.

Vincent

@videlec
Copy link
Contributor

videlec commented Mar 21, 2015

comment:6
sage -t --long src/sage/functions/log.py
**********************************************************************
File "src/sage/functions/log.py", line 628, in sage.functions.log.Function_lambert_w._eval_
Failed example:
    parent(lambert_w(e))
Expected:
    Integer Ring
Got:
    Symbolic Ring
**********************************************************************

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 21, 2015

Changed commit from f0fe6f4 to adc230d

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 21, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

8cca1a2Merge branch 'develop' into t/17953/inconsistency_in_returned_type_of_builtinfunction_result
adc230d17953: fix doctest

@videlec
Copy link
Contributor

videlec commented Mar 22, 2015

comment:9

Simpler way of doing

symbolic_input = any(parent_c(arg) is SR for arg in args)

@rwst
Copy link
Author

rwst commented Mar 22, 2015

comment:10

Replying to @videlec:

Simpler way of doing

symbolic_input = any(parent_c(arg) is SR for arg in args)

True. Not worth a commit however.

@mezzarobba
Copy link
Member

comment:11

Replying to @videlec:

In my opinion, a function should have a well defined domain and range. In some cases, the function preserves some subset. Like the function x -> x^2 defined in the complex numbers preserves integers, rationals and algebraic. But considering that {0} is a subset of the real numbers on which exp preserve the integers is going too far.

I tend to agree, but the present ticket is a step in the right direction in any case: it fixes some of the issues without making it harder to improve things later on.

@mezzarobba
Copy link
Member

Reviewer: Marc Mezzarobba

@vbraun
Copy link
Member

vbraun commented Apr 14, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants