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

parent should only depend on input parent #24833

Open
videlec opened this issue Feb 26, 2018 · 8 comments
Open

parent should only depend on input parent #24833

videlec opened this issue Feb 26, 2018 · 8 comments

Comments

@videlec
Copy link
Contributor

videlec commented Feb 26, 2018

The aim of this ticket is to apply the following principle of least surprise to member functions (aka methods)

The ouptut parent should only depend on the input parent.

Many Sage numerical member functions contradict the above principle:

  • on integers
sage: parent(ZZ(4).sqrt())
Integer Ring
sage: parent(ZZ(2).sqrt())
Symbolic Ring
  • on real mpfr floating point
sage: parent(RR(1.0).log())
Real Field with 53 bits of precision
sage: parent(RR(-1.0).log())
Complex Field with 53 bits of precision
  • on double precision floating point
sage: parent(RDF(1.0).log())
Real Double Field
sage: parent(RDF(-1.0).log())
Complex Double Field

Let us emphasize that this ticket does not deal with function (such as sqrt(2)) but only with member functions (like 2.sqrt()).

The parallel can be done with mathematical functions that have a well defined domain and codomain. In the above examples, the member functions try to be two things at the same time: a partial function of the initial domain (ie log from the positive reals to the reals) or as a domain restriction of a more general one (ie log on complexes minus the negative real line).

There are basically two ways to sort this out (see also this sage-devel thread)

  • change the output domain to wider by default (i.e. RR(1.0).log() would be the 0 in CC)
  • raise a ValueError when the domain is not appropriate

The above just stands for the default behavior. It would still be possible to switch behaviors with one of

  • an extra keyword parameter like extend=True or codomain=CC
  • distinct member functions log_real and log_complex

For an element of comparison, in the Python standard library, math is dealing with real floating point (both input/output). A ValueError is raised when the input is not in the domain

sage: import math
sage: math.log(-1.0r)
Traceback (most recent call last):
...
ValueError: math domain error

While cmath is dealing with complexes (input/output)

sage: import cmath
sage: cmath.log(-1.0r)
3.141592653589793j
sage: cmath.log(2.0r)
(0.6931471805599453+0j)

The mpmath library is also silently converting mpf to mpc depending on the domain

sage: import mpmath
sage: x = mpmath.mpf('2.0')
sage: mpmath.log(x)
mpf('0.69314718055994529')
sage: x = mpmath.mpf('-1.0')
sage: mpmath.log(x)
mpc(real='0.0', imag='3.1415926535897931')

CC: @rwst

Component: basic arithmetic

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

@videlec videlec added this to the sage-8.2 milestone Feb 26, 2018
@jdemeyer
Copy link

comment:1

I think we might also make a difference between methods and functions. The methods really act on a specific parent, so there it makes more sense to be strict. But I think that global functions like sqrt() can be less strict and violate the principle of "output parent should depend only on input parent".

@videlec

This comment has been minimized.

@videlec
Copy link
Contributor Author

videlec commented Feb 26, 2018

comment:3

Ticket description update: I only want to deal with member functions for now.

@rwst

This comment has been minimized.

@rwst
Copy link

rwst commented Feb 26, 2018

comment:4

Replying to @jdemeyer:

I think we might also make a difference between methods and functions. The methods really act on a specific parent, so there it makes more sense to be strict. But I think that global functions like sqrt() can be less strict and violate the principle of "output parent should depend only on input parent".

Exactly, and since most global functions are symbolic, and users of calculus do not expect that they need special arguments or functions to get extended behaviour that behaviour should be default for global functions.

@videlec
Copy link
Contributor Author

videlec commented Feb 26, 2018

comment:5

@rwst: why did you erase my modifications on the ticket description!?

@videlec

This comment has been minimized.

@rwst
Copy link

rwst commented Feb 26, 2018

comment:7

Replying to @videlec:

@rwst: why did you erase my modifications on the ticket description!?

I did? Ah, I didn't reload before adding my comment. Sorry.

@mkoeppe mkoeppe removed this from the sage-8.2 milestone Dec 29, 2022
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