Skip to content

Commit

Permalink
sagemathgh-35823: sage.arith.misc: fixes error in binomial for p-ad…
Browse files Browse the repository at this point in the history
…ic numbers with negative valuation

    
<!-- Please provide a concise, informative and self-explanatory title.
-->
<!-- Don't put issue numbers in the title. Put it in the Description
below. -->
<!-- For example, instead of "Fixes sagemath#12345", use "Add a new method to
multiply two integers" -->

### 📚 Description

<!-- 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. -->
As discussed in sagemath#35811 : in case 2 of `binomial` the fix replaces
`except TypeError` by `except (TypeError, ValueError)`.

 Furthermore, I added examples and tests for binomials of p-adic
numbers.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x
]`. -->

- [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.
- [x] I have updated the documentation accordingly.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on
- sagemath#12345: short description why this is a dependency
- sagemath#34567: ...
-->
Fixes sagemath#35811.
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: sagemath#35823
Reported by: Heiko Knospe
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Dec 24, 2023
2 parents ebbc2c9 + 7bada4e commit 957da8a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/sage/arith/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3696,6 +3696,12 @@ def binomial(x, m, **kwds):
0
sage: binomial(RealField()('2.5'), 2) # needs sage.rings.real_mpfr
1.87500000000000
sage: binomial(Zp(5)(99),50)
3 + 4*5^3 + 2*5^4 + 4*5^5 + 4*5^6 + 4*5^7 + 4*5^8 + 5^9 + 2*5^10 + 3*5^11 +
4*5^12 + 4*5^13 + 2*5^14 + 3*5^15 + 3*5^16 + 4*5^17 + 4*5^18 + 2*5^19 + O(5^20)
sage: binomial(Qp(3)(2/3),2)
2*3^-2 + 2*3^-1 + 2 + 2*3 + 2*3^2 + 2*3^3 + 2*3^4 + 2*3^5 + 2*3^6 + 2*3^7 +
2*3^8 + 2*3^9 + 2*3^10 + 2*3^11 + 2*3^12 + 2*3^13 + 2*3^14 + 2*3^15 + 2*3^16 + 2*3^17 + O(3^18)
sage: n = var('n'); binomial(n, 2) # needs sage.symbolic
1/2*(n - 1)*n
sage: n = var('n'); binomial(n, n) # needs sage.symbolic
Expand Down Expand Up @@ -3792,6 +3798,27 @@ def binomial(x, m, **kwds):
sage: binomial(n,2) # needs sage.symbolic
1/2*(n - 1)*n
Test p-adic numbers::
sage: binomial(Qp(3)(-1/2),4) # p-adic number with valuation >= 0
1 + 3 + 2*3^2 + 3^3 + 2*3^4 + 3^6 + 3^7 + 3^8 + 3^11 + 2*3^14 + 2*3^16 + 2*3^17 + 2*3^19 + O(3^20)
Check that :trac:`35811` is fixed::
sage: binomial(Qp(3)(1/3),4) # p-adic number with negative valuation
2*3^-5 + 2*3^-4 + 3^-3 + 2*3^-2 + 2*3^-1 + 2 + 2*3 + 2*3^2 + 2*3^3 + 2*3^4 + 2*3^5 +
2*3^6 + 2*3^7 + 2*3^8 + 2*3^9 + 2*3^10 + 2*3^11 + 2*3^12 + 2*3^13 + 2*3^14 + O(3^15)
sage: binomial(Qp(3)(1/3),10).parent()
3-adic Field with capped relative precision 20
sage: F.<w>=Qq(9); binomial(w,4) # p-adic extension field
(w + 2)*3^-1 + (w + 1) + (2*w + 1)*3 + 2*w*3^2 + (2*w + 2)*3^3 + 2*w*3^4 + (2*w + 2)*3^5 +
2*w*3^6 + (2*w + 2)*3^7 + 2*w*3^8 + (2*w + 2)*3^9 + 2*w*3^10 + (2*w + 2)*3^11 + 2*w*3^12 +
(2*w + 2)*3^13 + 2*w*3^14 + (2*w + 2)*3^15 + 2*w*3^16 + (2*w + 2)*3^17 + 2*w*3^18 + O(3^19)
sage: F.<w>=Qq(9); binomial(w,10).parent()
3-adic Unramified Extension Field in w defined by x^2 + 2*x + 2
Invalid inputs::
sage: x = polygen(ZZ)
Expand Down Expand Up @@ -3872,7 +3899,7 @@ def binomial(x, m, **kwds):
# case 2: conversion to integers
try:
x = ZZ(x)
except TypeError:
except (TypeError, ValueError):
pass
else:
# Check invertibility of factorial(m) in P
Expand Down

0 comments on commit 957da8a

Please sign in to comment.