ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 24 Jun 2024 18:25:24 +0200Use of 'mod' versus '%' in Sage.https://ask.sagemath.org/question/77980/use-of-mod-versus-in-sage/ I would like to better understand the use of 'mod' in Sage.
The function 'mod', as defined in sage/rings/finite_rings/integer_mod, returns mod(a, 0) = a, which is fine.
On the other hand, in sage/arith/misc, the function 'quadratic_residues' is defined as
sorted(set(ZZ((a*a) % n) for a in range(n // 2 + 1)))
Thus, if n = 0, a ZeroDivisionError is thrown. If, on the other hand, the function had been implemented with 'mod', there would be no error and 0 would be returned. This variant looks more advantageous for me.
Test:
for n in range(0, 11):
print(sorted(set(ZZ(mod((a*a), n)) for a in range(n // 2 + 1))))
Peter LuschnyMon, 24 Jun 2024 18:25:24 +0200https://ask.sagemath.org/question/77980/Where does sagemath's speed in calculating gcd come from?https://ask.sagemath.org/question/74828/where-does-sagemaths-speed-in-calculating-gcd-come-from/ Hey,
I was wondering how can sagemath can calculate gcds without calculating the numbers at stake?
For instance,
sage: from Crypto.Util.number import long_to_bytes, inverse, bytes_to_long
sage: from Crypto.Hash import SHA256
sage: e = 65537
sage: gcd(bytes_to_long(SHA256.new(data=b'first').digest())^e-35211653423, bytes_to_long(SHA256.new(data=b'second').digest())^e-156535482153)
is computed nearly instantly while it will take forever for sage to calculate `bytes_to_long(SHA256.new(data=b'first').digest())^e`.
Is it perhaps part of symbolic computation?
Thanks :)MonappWed, 13 Dec 2023 21:28:48 +0100https://ask.sagemath.org/question/74828/check if coefficients of a polynomial are divisible by a numberhttps://ask.sagemath.org/question/74293/check-if-coefficients-of-a-polynomial-are-divisible-by-a-number/ I would like to make a program that take as an entry a given integer coefficient polynomial and check if the coefficients of this polynomial are all divisible by a given number n. <br>
How could I do it ? <br>
(sorry if the question is stupid but I could not do it in python and I discovered SageMath and it looked like it was kind of easier to do it with it) dc64Mon, 13 Nov 2023 16:00:27 +0100https://ask.sagemath.org/question/74293/xgcd for several argumentshttps://ask.sagemath.org/question/73642/xgcd-for-several-arguments/SageMath has a built-in method for performing the extended Euclidean algorithm for two numbers (or polynomials), called xgcd. The algorithm also works for several arguments, by recursion. One can implement this easily with a SageMath helper function, but I was wondering: does SageMath have a built-in extension of xgcd to several arguments?
Martin-BrThu, 28 Sep 2023 09:59:24 +0200https://ask.sagemath.org/question/73642/ArithmeticError: reduction modulo not definedhttps://ask.sagemath.org/question/56116/arithmeticerror-reduction-modulo-not-defined/I have tried performed modulo operation:
and I got this:
ArithmeticError Traceback (most recent call last) <ipython-input-1-b13f3c6e195a> in <module>
54 print(" po podziale x 2")
55 xP, yP = Pp.xy()
---> 56 uu = xQ % xP
57 print( f"uu mod = {(uu)} " )
/home/sc_serv/sage/local/lib/python3.8/site-packages/sage/rings/finite_rings/integer_mod.pyx in sage.rings.finite_rings.integer_mod.IntegerMod_abstract.__mod__ (build/cythonized/sage/rings/finite_rings/integer_mod.c:7287)()
493 R = IntegerModRing(modulus)
494 if (<Element>self)._parent._IntegerModRing_generic__order % R.order():
--> 495 raise ArithmeticError(f"reduction modulo {modulus!r} not defined")
496 return R(self)
497
ArithmeticError: reduction modulo 55066263022277343669578718895168534326250603453777594175500187360389116729240 not defined
i don't understand in python work
xQ = 0x5699b93fc6e1bd29e09a328d657a607b4155b61a6b5fcbedd7c12df7c67df8f5
xP = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798MiroslawThu, 11 Mar 2021 08:18:23 +0100https://ask.sagemath.org/question/56116/Rounding the values of a mappinghttps://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/The problem is only with the `round(, 2)` code
f(x)=round((1/add([10, 20 ,30]))*x,2)
percent_votes_cand=list(map(f,[10, 20 ,30]))
percent_votes_cand
This gives an error since `round()` couldn't be applied to a theoretical argument. The following code works but I want to obtain truncated to 2 digits percentages not elements of QCyrilleFri, 15 Jan 2021 15:11:13 +0100https://ask.sagemath.org/question/55284/elliptic curve arithmetic without inversionhttps://ask.sagemath.org/question/45784/elliptic-curve-arithmetic-without-inversion/ It seems when you add points on an elliptic curve in sage, it immediately divides by the z coordinate if it is not zero. Since I plan on working in a finite field of a large prime, I would rather it not calculate this inverse, but I'm not sure how to stop it from automatically doing so. How do I get it to leave the z coordinate alone so as to avoid inverting elements of my field?milksushFri, 15 Mar 2019 08:49:09 +0100https://ask.sagemath.org/question/45784/Extension field arithmetichttps://ask.sagemath.org/question/37737/extension-field-arithmetic/code:
p=(2^3) ; extension field over(2^3)
F.<a>=GF(2^3);F.modulus();#F third degree irreducible polynomial (x^3+x+1)
R.<x>=F[];#R generate extension field (2^3)^2
K.<b>=F.extension(x^2+(a^2+a+1)*x+a^2); second degree irreducible polynomial over (2^3)^2
R.<z>=PolynomialRing(K) ;
f3= z + (a^2 + 1)*b + a + 1; polynomial obtained after execution of program
from the above polynomial i want to separate out terms (a^2+1) which is attached with b and (a+1)
as i want my result in following manner ***(a+1 , a^2+1)***.
ThankssantoshiTue, 30 May 2017 06:58:40 +0200https://ask.sagemath.org/question/37737/Row reduction modulo prime powershttps://ask.sagemath.org/question/9163/row-reduction-modulo-prime-powers/Has any kind of row reduction been implemented modulo prime powers? Right now I'm simply trying to write a particular element of (Z/9Z)^d as a linear combination of a handful of other elements in this space. (I happen to know for other reasons that the original element is in the span of these handful of elements.)
I tried to do this by working with matrices over pAdicField(3,2), but this didn't work. (I guess some division by 3 messes things up.)
Is there anyway to do this without writing my own row reduction code??Robert PollackThu, 19 Jul 2012 14:55:30 +0200https://ask.sagemath.org/question/9163/Why (11+2/3)%2=1 and (11+1/3)%2=0?https://ask.sagemath.org/question/31740/why-112321-and-111320/I did not expect this behavior...
For instance, I would expect an error message or a warning.
Is there an explanation?rolandMon, 21 Dec 2015 22:43:37 +0100https://ask.sagemath.org/question/31740/Symbolic integer arithmetichttps://ask.sagemath.org/question/23610/symbolic-integer-arithmetic/It's not that hard to find a closed expression for the remainder of the polynomial $x^n$ modulo $x^2-5x-2$. But I don't seem to manage it in SAGE.
n=var('n')
R=PolynomialRing(RationalField(),'x');R
x=R.gen();x
mp=x^2-5*x-2
S=R.quotient(mp,'a')
a=S.gen();a^2
a^5
works as expected, but replacing the last line with a^n gives an error (non-integral exponents not supported). I understand a general variable is not a SAGE integer, but how DO I make clear n is supposed to be an integer?
In the same way, it's not that hard to calculate the symbolic n-the power of a small, say 2x2, matrix, but the straightforward aproach of simplifying A^n gives the same error (non-integral exponents not supported).
I checked wolfram alpha for the symbolic power of a matrix, and immediately got the expected answer for [[1,2],[3,4]]^n .
![Wolfram alpha answer](http://www4b.wolframalpha.com/Calculate/MSP/MSP16951af85fib66538dhg0000648f487198eeg2e8?MSPStoreType=image/gif&s=54&w=1188.&h=58.)
Dirk DanckaertMon, 28 Jul 2014 15:41:10 +0200https://ask.sagemath.org/question/23610/mod 1 arithmetichttps://ask.sagemath.org/question/10414/mod-1-arithmetic/Hi all,
is it possible, to do simple mod 1 evaluations? For example, Mathematica outputs
N[1/2*(1 + Sqrt[5])^100 mod 1] ~ 0.9999999999999999999987374...
but
n(Mod(1/2*(1 + sqrt(5))^100,1))
(or similar attempts) in Sage result in
TypeError: unable to convert x (=1/2*(sqrt(5) + 1)^100) to an integer
Cheers,
MarkuspinwheelMon, 05 Aug 2013 16:08:03 +0200https://ask.sagemath.org/question/10414/Plot a function involving lowest termshttps://ask.sagemath.org/question/10372/plot-a-function-involving-lowest-terms/Hi,
How do I use sage to plot the following function?
$$ f(x)= \left[ \begin {array} {cc} 0, & x \text{ irrational, } 0 \lt x \lt 1 \\\\
\frac{1}{q},& x = \frac{p}{q} \text{ in lowest term, } 0 < x < 1.\end{array} \right.$$
I actually wrote a function that would generate the following sequence:
$$ \frac{1}{n}, \dots , \frac{n-1}{n}$$ for a given $n$.
Then I created a list of lists that contains the above sequence for each n. After flattening that list, I call the set function on it to remove duplicates. Then I use that to creat a list (x,y) tuple to plot the function using scatter plot.
I was wondering if there is a simpler way of doing this.
ensabaMon, 22 Jul 2013 07:33:41 +0200https://ask.sagemath.org/question/10372/interface to MPFIhttps://ask.sagemath.org/question/9431/interface-to-mpfi/Thanks to the Sage team for providing an interface to the MPFI package for arbitrary precision interval arithmetic. It looks like the MPFI version used in Sage is 1.5.1, the most recent. Reading the included info file mpfi.info, there are references to additional functionality such as extended interval division, which would be useful for implementing extended interval Newton and related numerical methods. I can't tell if this is available in MPFI 1.5.1 and does not yet have a wrapper in Sage (not complaining!), or will only be "available soon" in MPFI as the file states.
Since I can't find any information at any MPFI site (Mon français n'est pas très bon), does anyone know if or when the extended interval arithmetic functions will be implemented in MPFI? And this looks like it would be quite a job to get into Sage as well. For example, currently:
1./RIF(-1.,1.)
[-infinity .. +infinity]
The extended interval division of this would be the (sharper bound) union of two intervals:
[-infinity .. -1] <union> [1 .. +infinity]
which would seem to require a data structure to hold multiple RIF objects. A numerical algorithm can then detect this split, perform iterations on both parts, and (ideally) find all solutions to the original problem without multiple starting points. This could get messier in higher dimensions since multiple intervals now become multiple boxes, cubes, and hypercubes.
I see that MPFI or something does solve systems of equations over RIF:
A = matrix([[RIF(2,2.1), RIF(1,1.1)],[RIF(1,1.1),RIF(2,2.0)]]); A
[2.1? 1.1?]
[1.1? 2]
b = vector([RIF(1,1.1),RIF(1,1.1)]); b
(1.1?, 1.1?)
x = A\b
x[0].endpoints()
(0.230244068953746, 0.426562500000001)
x[1].endpoints()
(0.259218749999999, 0.447175285884964)
which is very nice, but also breaks down if the determinant of the matrix is an interval that includes 0, resulting in all infinities for the solution. There is an entire area of numerical linear algebra now devoted to solving these problems, which I'm interested in implementing. I might be able to handle the extended interval arithmetic with my own generalized Python classes, but it's better to use whatever standards are being developed.
burningbrightMon, 15 Oct 2012 20:15:10 +0200https://ask.sagemath.org/question/9431/Polynomial arithmetic modulo prime powershttps://ask.sagemath.org/question/9166/polynomial-arithmetic-modulo-prime-powers/I'm trying to do some operations with polynomials over $Z/p^nZ$ and I'm stuck on some basic things:
1) Is it possible in SAGE to long divide two polynomials in $Z/p^nZ[x]$?
2) Is it possible in SAGE to factor a polynomial in $Z/p^nZ[x]$?
Am I missing something about the basic functionality of (1)? Is this really something that I need to program myself??
Robert PollackFri, 20 Jul 2012 14:37:12 +0200https://ask.sagemath.org/question/9166/All decompositions of a prime as a sum of four squareshttps://ask.sagemath.org/question/8963/all-decompositions-of-a-prime-as-a-sum-of-four-squares/Jacobi [showed](http://en.wikipedia.org/wiki/Jacobi%27s_four-square_theorem) that a prime $p$ has $8(p+1)$ decompositions as a sum of four squares. `sage.rings.arith.four_squares` computes one such decomposition. Is there a way to compute all of them?
parzanWed, 09 May 2012 03:51:59 +0200https://ask.sagemath.org/question/8963/4-Digit Rounding Arithmetic:System of Equationshttps://ask.sagemath.org/question/8305/4-digit-rounding-arithmeticsystem-of-equations/I am given this system of equations:
1.130x - 6.990y = 14.20
1.013x - 6.099y = 14.22
In sage I input
sage: y = var("y")
sage: solve ([1.130*x + 6.990*y == 14.20, ... etc ], x,y )
it spits out 4264/63, 1684/189
for x,y I now input
sage: round((4264.0/63.0), 4) --> for x and same thing for y, using 1684.0 / 189.0.
Giving me 8.9101, yet this answer seems to be wrong.
My question is where can I found out how to do 4 digit rounding arithmetic using sage? or Am I doing something wrong with the code. Mathstudent2010Sun, 04 Sep 2011 05:56:41 +0200https://ask.sagemath.org/question/8305/