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.Sat, 09 Apr 2022 13:46:29 +0200Reducing an expressing modulo a variable expressionhttps://ask.sagemath.org/question/61878/reducing-an-expressing-modulo-a-variable-expression/ I have some integer matrices, and I am interested in their behaviour mod `p^2`. I have declared variables using
``
var('p a11 a12 a13, ...')
``
etc, and after multiplying my matrices I am left with some very messy entries, involving lots of `p^2,p^3,...` terms. Since I only care about behaviour mod `p^2`, I would like Sage to just ignore those higher order terms. I feel like there must be an easy way to do this, but I don't know what it is. Any help would be much appreciated! Fri, 08 Apr 2022 18:38:10 +0200https://ask.sagemath.org/question/61878/reducing-an-expressing-modulo-a-variable-expression/Comment by slelievre for <p>I have some integer matrices, and I am interested in their behaviour mod <code>p^2</code>. I have declared variables using
<code>
var('p a11 a12 a13, ...')
</code>
etc, and after multiplying my matrices I am left with some very messy entries, involving lots of <code>p^2,p^3,...</code> terms. Since I only care about behaviour mod <code>p^2</code>, I would like Sage to just ignore those higher order terms. I feel like there must be an easy way to do this, but I don't know what it is. Any help would be much appreciated! </p>
https://ask.sagemath.org/question/61878/reducing-an-expressing-modulo-a-variable-expression/?comment=61881#post-id-61881Welcome to Ask Sage! Thank you for your question.Fri, 08 Apr 2022 23:27:08 +0200https://ask.sagemath.org/question/61878/reducing-an-expressing-modulo-a-variable-expression/?comment=61881#post-id-61881Answer by rburing for <p>I have some integer matrices, and I am interested in their behaviour mod <code>p^2</code>. I have declared variables using
<code>
var('p a11 a12 a13, ...')
</code>
etc, and after multiplying my matrices I am left with some very messy entries, involving lots of <code>p^2,p^3,...</code> terms. Since I only care about behaviour mod <code>p^2</code>, I would like Sage to just ignore those higher order terms. I feel like there must be an easy way to do this, but I don't know what it is. Any help would be much appreciated! </p>
https://ask.sagemath.org/question/61878/reducing-an-expressing-modulo-a-variable-expression/?answer=61887#post-id-61887For a symbolic approach you can use a substitution with a wildcard:
sage: var('p,a')
sage: f = (a*p+1)^2
sage: w0 = SR.wild()
sage: f.expand().subs({p^w0 : 0})
2*a*p + 1
This makes use of the fact that `p^1` does not appear in an expanded symbolic expression.
Using a different ring would probably be more efficient, though.Sat, 09 Apr 2022 13:46:29 +0200https://ask.sagemath.org/question/61878/reducing-an-expressing-modulo-a-variable-expression/?answer=61887#post-id-61887Answer by Max Alekseyev for <p>I have some integer matrices, and I am interested in their behaviour mod <code>p^2</code>. I have declared variables using
<code>
var('p a11 a12 a13, ...')
</code>
etc, and after multiplying my matrices I am left with some very messy entries, involving lots of <code>p^2,p^3,...</code> terms. Since I only care about behaviour mod <code>p^2</code>, I would like Sage to just ignore those higher order terms. I feel like there must be an easy way to do this, but I don't know what it is. Any help would be much appreciated! </p>
https://ask.sagemath.org/question/61878/reducing-an-expressing-modulo-a-variable-expression/?answer=61885#post-id-61885If you matrices have polynomial entries, a possible approach is to define a polynomial ring in all variables but `p`, and the power series ring in `p` over that ring like:
R.<a11,a12,a13> = PolynomialRing(QQ)
P.<p> = PowerSeriesRing(R,default_prec=2)
Then to ignore the powers `p^2` and higher, one can add `O(p^2}` and convert the result to polynomial if needed:
f = a11 + a12*p + a13*p^2
f += O(p^2)
print(f)
print(f.polynomial())
Sat, 09 Apr 2022 13:11:01 +0200https://ask.sagemath.org/question/61878/reducing-an-expressing-modulo-a-variable-expression/?answer=61885#post-id-61885