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

Change default domain of SR.var() #36841

Merged
merged 5 commits into from
Jan 14, 2024

Conversation

RuchitJagodara
Copy link
Contributor

@RuchitJagodara RuchitJagodara commented Dec 8, 2023

This patch fixes #36833 by changing the default domain of SR.var()
function to complex. By this way, every variable whose domain is not defined will be considered
in complex plane, by default.

The issue was with the default value only because whenever we were defining a variable without specifying any domain it was calling symbol() function with domain=None; Subsequently, it calls new_Expression_symbol() function with domain=None. In this code, I found that it was not giving any specific domain to the variable and so when we define a new variable using SR.var() function, it is passing the is_real() (Because it does not have any specific domain) test but giving the wrong answer on factor() function, if we assume that the variable is in complex plane by default which is correct according to the documentation.

📝 Checklist

  • The title is concise, informative, and self-explanatory.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.

@RuchitJagodara RuchitJagodara changed the title Default domain of SR.var() Changed default domain of SR.var() Dec 8, 2023
@RuchitJagodara RuchitJagodara changed the title Changed default domain of SR.var() Change default domain of SR.var() Dec 8, 2023
This is a fix to the issue trac:`36833`. Variables whose domain is
not defined should be considered in the complex plane by default
as described in the documentation.
@@ -862,6 +862,16 @@ cdef class SymbolicRing(sage.rings.abc.SymbolicRing):
Traceback (most recent call last):
...
ValueError: cannot specify n for multiple symbol names

Check that :trac:`36833` is fixed: Variables whose domain is not
defined should be considered in complex plane as described in documentation::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you point out where in documentation you see this " considered in complex plane" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot from 2023-12-10 19-07-00

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@dimpase
Copy link
Member

dimpase commented Dec 10, 2023

we might see lots of doctest errors now, not sure.

@RuchitJagodara
Copy link
Contributor Author

Then, I think it would be good if we change the factor function instead of doing this. I'll change it and will update.

@RuchitJagodara
Copy link
Contributor Author

@dimpase Do you know any function that can return the domain of a symbolic variable?

@dimpase
Copy link
Member

dimpase commented Dec 10, 2023

@dimpase Do you know any function that can return the domain of a symbolic variable?

no, I don't.

By the way, I just saw the place where it's claimed that var by default returns
complex variable - it's in the output sage: var? - so you can find it in the code by
e.g. looking at the bottom of sage: var??

@RuchitJagodara
Copy link
Contributor Author

Can't we implement a new class for variables of symbolic ring? By this method, we will be able to track domain of the variable and once we are able to access the domain of variable, I think it will be easier to handle these type of issues.

@dimpase
Copy link
Member

dimpase commented Dec 12, 2023

I don't think it's a good idea to make domain="complex" the default. It seems that most everything in Sage assumes it to be real. E.g. if you make this change then differentiating with default variables will become complex differentiation, which is a rather different story, mathematically - and Sage does not do it.
E.g.

sage: y = SR.var("y",domain="complex")
sage: f(y) = y * conjugate(y)
sage: f.diff(y)
y |--> y*diff(conjugate(y), y) + conjugate(y)

doesn't make much sense, as in fact $\frac{d\overline{y}}{dy}$ does not exist (e.g. as Cauchy equations don't hold).

@RuchitJagodara
Copy link
Contributor Author

Hmm, got it. But when we declare a variable without declaring any domain, it is clear that it is not real from the following,

sage: n = SR.var("n"); n
n
sage: n.is_real()
False

So it simply implies that it is complex. So I am not getting the point that what is wrong here, if the variable is not real then it should not be simplified to y^2, anyhow. But factor function is doing so. I think the error is occurring with the maxima. Actually maxima is simplifying many functions which should not be simplified like 0^n is being converted to 0 (this is referenced from the issue #36838 ) , here y * conjugate(y) is being converted to y^2.

@dimpase
Copy link
Member

dimpase commented Dec 12, 2023 via email

@RuchitJagodara
Copy link
Contributor Author

maxima has no concept of a domain for a variable, it seems to me.

But it has to be, because when we do operation like below

sage: n = var('n',domain="complex"); n
n
sage: f1 = n * conjugate(n); f1
n*conjugate(n)
sage: f1.factor()
n*conjugate(n)

Here, we can clearly see that it is giving correct answer if we declare domain="complex", but it is not giving the same answer when we do it as below, which means it has something to do with the domain.

sage: b = var("b")
sage: f2 = b * conjugate(b); f2
b*conjugate(b)
sage: f2.factor()
b^2

@dimpase
Copy link
Member

dimpase commented Dec 12, 2023

maxima has no concept of a domain for a variable, it seems to me.

But it has to be, because when we do operation like below

sage: n = var('n',domain="complex"); n
n
sage: f1 = n * conjugate(n); f1
n*conjugate(n)
sage: f1.factor()
n*conjugate(n)

Here, we can clearly see that it is giving correct answer if we declare domain="complex", but it is not giving the same answer when we do it as below, which means it has something to do with the domain.

sage: b = var("b")
sage: f2 = b * conjugate(b); f2
b*conjugate(b)
sage: f2.factor()
b^2

Sorry, indeed, you are right. Maxima has domain, which is a domain for computation to be done (although it's not to declare that a variable is real or complex) , and a similar thing called assume (which allows things like assume(x>0), or indeed x to be real).

Better, it has declare - which seems to be exactly what is needed here. So one can do

$ maxima 
Maxima 5.47.0 https://maxima.sourceforge.io
using Lisp SBCL 2.3.9
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i4) declare(x, complex);
(%o4)                                done
(%i5) x*conjugate(x);
(%o5)                           x conjugate(x)
(%i6) declare(y, real);
(%o6)                                done
(%i7) y*conjugate(y);
                                       2
(%o7)                                 y
(%i8) 

@RuchitJagodara
Copy link
Contributor Author

RuchitJagodara commented Dec 13, 2023

Hmm... But in sage when we are doing this

sage: y = SR.var("y","real"); y
y
sage: f = y * conjugate(y); f
y*conjugate(y)
sage: f
y*conjugate(y)
sage: f.simplify_full()
y^2

Here, you can see that for variable whose domain is real, it is not simplifying conjugate(y) and for that I have to use simplify_full() function but I think this should be done when we were multiplying y with conjuage(y) so the value of f should be y^2 not y*conjugate(y).

Check whether the default domain=real working or not.
@RuchitJagodara
Copy link
Contributor Author

I found an issue where one had similar problem with maxima, discussion link

@RuchitJagodara
Copy link
Contributor Author

Should I change the documentation instead, which may specify that the default domain of var is None? This means that the variable will not have any specific domain. Or should I just leave it as it is?

@dimpase
Copy link
Member

dimpase commented Dec 22, 2023

Yes, please, we are not going to make the default domain complex. Please fix the documentation instead, to specify that the default domain is None.

@RuchitJagodara
Copy link
Contributor Author

Sorry for the delay! Actually, I was checking if there is any other fix that can be applied, but I think we cannot do anything as the problem is with maxima only. So, I have changed the documentation.

Can you please review my changes now ?

Copy link

github-actions bot commented Jan 4, 2024

Documentation preview for this PR (built with commit 053d50c; changes) is ready! 🎉

Copy link
Member

@dimpase dimpase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

vbraun pushed a commit to vbraun/sage that referenced this pull request Jan 5, 2024
    
This patch fixes sagemath#36833 by changing the default domain of SR.var()
function to complex. By this way, every variable whose domain is not
defined will be considered
in complex plane, by default.

The issue was with the default value only because whenever we were
defining a variable without specifying any domain it was calling
symbol() function with domain=None; Subsequently, it calls
new_Expression_symbol() function with domain=None. In this code, I found
that it was not giving any specific domain to the variable and so when
we define a new variable using SR.var() function, it is passing the
is_real() (Because it does not have any specific domain) test but giving
the wrong answer on factor() function, if we assume that the variable is
in complex plane by default which is correct according to the
documentation.

<!-- ^^^^^
Please provide a concise, informative and self-explanatory title.
Don't put issue numbers in there, do this in the PR body below.
For example, instead of "Fixes sagemath#1234" use "Introduce new method to
calculate 1+1"
-->
<!-- Describe your changes here in detail -->

<!-- Why is this change required? What problem does it solve? -->
<!-- If this PR resolves an open issue, please link to it here. For
example "Fixes sagemath#12345". -->
<!-- If your change requires a documentation PR, please link it
appropriately. -->

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
<!-- Feel free to remove irrelevant items. -->

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.



<!-- List all open PRs that this PR logically depends on
- sagemath#12345: short description why this is a dependency
- sagemath#34567: ...
-->

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: sagemath#36841
Reported by: Ruchit Jagodara
Reviewer(s): Dima Pasechnik, Ruchit Jagodara
@vbraun vbraun merged commit 9b73d6c into sagemath:develop Jan 14, 2024
20 checks passed
@mkoeppe mkoeppe added this to the sage-10.3 milestone Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

factor ignores complex conjugate
4 participants