Opened 5 years ago

Closed 5 years ago

#17953 closed defect (fixed)

symbolic function args prevent forced conversion of result to numeric

Reported by: rws Owned by:
Priority: major Milestone: sage-6.6
Component: symbolics Keywords:
Cc: jdemeyer Merged in:
Authors: Ralf Stephan Reviewers: Marc Mezzarobba
Report Upstream: N/A Work issues:
Branch: adc230d (Commits) Commit: adc230dff1b357478155978ecc1149d58f282403
Dependencies: Stopgaps:

Description (last modified by rws)

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.

Change History (13)

comment:1 Changed 5 years ago by rws

  • Description modified (diff)
  • Summary changed from inconsistency in returned type of symbolic function result to inconsistency in returned type of BuiltinFunction result

comment:2 Changed 5 years ago by rws

  • Cc jdemeyer added

comment:3 Changed 5 years ago by rws

  • Branch set to u/rws/inconsistency_in_returned_type_of_builtinfunction_result

comment:4 Changed 5 years ago by rws

  • Authors set to Ralf Stephan
  • Commit set to f0fe6f4093db35e8ccc1734ef3220ee72162d3e6
  • Status changed from new to needs_review
  • Summary changed from inconsistency in returned type of BuiltinFunction result to symbolic function args prevent forced conversion of result to numeric

New commits:

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

comment:5 follow-up: Changed 5 years ago by vdelecroix

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

comment:6 Changed 5 years ago by vdelecroix

  • Status changed from needs_review to needs_work
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
**********************************************************************

comment:7 Changed 5 years ago by git

  • Commit changed from f0fe6f4093db35e8ccc1734ef3220ee72162d3e6 to adc230dff1b357478155978ecc1149d58f282403

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

comment:8 Changed 5 years ago by rws

  • Status changed from needs_work to needs_review

comment:9 follow-up: Changed 5 years ago by vdelecroix

Simpler way of doing

symbolic_input = any(parent_c(arg) is SR for arg in args)
Last edited 5 years ago by vdelecroix (previous) (diff)

comment:10 in reply to: ↑ 9 Changed 5 years ago by rws

Replying to vdelecroix:

Simpler way of doing

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

True. Not worth a commit however.

comment:11 in reply to: ↑ 5 Changed 5 years ago by mmezzarobba

  • Status changed from needs_review to positive_review

Replying to vdelecroix:

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.

comment:12 Changed 5 years ago by mmezzarobba

  • Reviewers set to Marc Mezzarobba

comment:13 Changed 5 years ago by vbraun

  • Branch changed from u/rws/inconsistency_in_returned_type_of_builtinfunction_result to adc230dff1b357478155978ecc1149d58f282403
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.