ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 11 Aug 2020 01:50:59 -0500Constant coefficient of symbolic expressionhttps://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/Suppose we have the following
sage: var("x,y,z")
sage: expr = x*y+z^2+4
I am looking for a function which does something like
sage: expr.constant_coefficient()
4
However, I did not find such a function. If I use `coefficient` I can get the desired result using
sage: expr.coefficient(x,0).coefficient(y,0).coefficient(z,0)
4
Of course if I have more variables that gets more tedious and I'd write a small helper function which goes through the variables contained in `expr`. I feel like this is much too complicated and I'm just overlooking some function. Is anyone aware of some cleaner way to do this if the expression contains a lot of variables (i.e. without having to write a helper-function)?
Thank you for your help!philipp7Tue, 11 Aug 2020 01:50:59 -0500https://ask.sagemath.org/question/52938/how to get the coefficient of a multivariate polynomial with respect to a specific variable and degree, in a quotient ring ?https://ask.sagemath.org/question/52594/how-to-get-the-coefficient-of-a-multivariate-polynomial-with-respect-to-a-specific-variable-and-degree-in-a-quotient-ring/Here is what I tried.
sage: F = ZZ.quo(3*ZZ); F
sage: A.<X, Y, Z> = PolynomialRing(F); A
sage: R.<x, y, z> = A.quotient(ideal(X^2 - 1, Y^2 - 1, Z^2 - 1))
sage: f = x*z + x*y*z + y + 1
sage: f.coefficient(z, 1)
sage: f.coefficient({z: 1})
sage: f.coeffcient(z)andriamTue, 21 Jul 2020 03:46:56 -0500https://ask.sagemath.org/question/52594/get the coefficients of polynomial of several variables?https://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/ Consider that I have a polynomial with n variables x1,x2,...,xn and want to get the coefficient of that polynomial. For example I can have:
x1+2*x1*x5^2+3*x1^2*x4-5*x2*x5^2+1/2*x2*x3+x6^2*x7*x8+9*x3*x9^3
I would like to ask Sage to give coefficient for the polynomial with variables x1..x9 and the result should be something like this:
coefficient,(exponent of x1..x9);
1,(1,0,0,0,0,0,0,0,0);
2,(1,0,0,0,2,0,0,0,0);
3,(2,0,0,1,0,0,0,0,0);
-5,(0,1,0,0,2,0,0,0,0);
1/2,(0,1,1,0,0,0,0,0,0);
1,(0,0,0,0,0,2,1,1);
9,(0,0,1,0,0,0,0,0,3)
How can I achieve this?
DanialBaghTue, 13 Mar 2018 21:12:52 -0500https://ask.sagemath.org/question/41526/Should I always use .expand() before using .coefficient()?https://ask.sagemath.org/question/41437/should-i-always-use-expand-before-using-coefficient/I used the following code in SageMath (Windows binary, version 8.1):
q,x,t,bb_1_1_0,bb_1_1_1,aa_1_1_1,cc_1,eps=var('q x t bb_1_1_0 bb_1_1_1 aa_1_1_1 cc_1 eps')
const1=-(bb_1_1_1*eps*cos(t)*cos(x) + bb_1_1_0*eps*cos(x) + q)*aa_1_1_1*eps*cos(x)*sin(t) - bb_1_1_1*cc_1*eps*cos(x)*sin(t)
Obtaining the coefficients of polynomial with respect to eps: `const1.coefficient(eps,1)` gives
-(bb_1_1_1*cos(t)*cos(x) + bb_1_1_0*cos(x))*aa_1_1_1*cos(x)*sin(t) - bb_1_1_1*cc_1*cos(x)*sin(t)
but using `const1.expand().coefficient(eps,1)` gives
-bb_1_1_1*cc_1*cos(x)*sin(t) - aa_1_1_1*q*cos(x)*sin(t)
Clearly the second one is correct. Does this mean that I should always use `.expand()` before using `.coefficient()`?
In another instance I was forced to use `simplify_full()` before using `coefficient()`.DanialBaghThu, 08 Mar 2018 06:49:38 -0600https://ask.sagemath.org/question/41437/Get the constant value of an equationhttps://ask.sagemath.org/question/39060/get-the-constant-value-of-an-equation/I have the following equation :
(x - 1)^2 - (x - 2)^2 - (y - 1)^2 + y^2 + (z - 3)^2 - (z - 4)^2 == 1.75000000000000
which I factorized to :
2*x + 2*y + 2*z - 51/4
And then I would like to extract the `-51/4` but the `.coefficient()` function doesn't work for constant so I have no idea to get the constant value.BariloSat, 07 Oct 2017 04:20:49 -0500https://ask.sagemath.org/question/39060/Get the coefficient of the constanthttps://ask.sagemath.org/question/39058/get-the-coefficient-of-the-constant/ I have the following equation :
(x - 1)^2 - (x - 2)^2 - (y - 1)^2 + y^2 + (z - 3)^2 - (z - 4)^2 == 1.75000000000000
which I factorized to :
2*x + 2*y + 2*z - 51/4
And then I would like to extract the `-51/4` but the `.coefficient()` doesn't work for constant so I have no idea to get the constant value.BariloSat, 07 Oct 2017 04:19:12 -0500https://ask.sagemath.org/question/39058/leading coefficient polynomialhttps://ask.sagemath.org/question/35031/leading-coefficient-polynomial/ Hello everybody,
I'm new to sagemath and python in general, and one of my course in Uni uses it... I have a vague and unclear tutorial the prof gave us and for now I know only the most basic commands.
I have to write a function that takes a polynomial of any degree and tells me the coefficient of the highest degree member (for example , 2x^4+3x^3 would be 2, 7x^3+2x^4+2 would be 7...).
I think the function would have to use "expand", "degree", and of course "coefficient". But i barely have any idea as how to write it.
If anyone could help me it would be great, I am kinda lost here...
Sorry for sloppy english and thanks in advance.
waddupbbySun, 02 Oct 2016 11:27:48 -0500https://ask.sagemath.org/question/35031/Constant coefficient of Laurent Polynomialshttps://ask.sagemath.org/question/35746/constant-coefficient-of-laurent-polynomials/I am looking for the constant coefficient of a Laurent polynomial, the issue I am having is that sage is not simplifying the polynomial.
An example:
a = var(",".join( "a%i" %i for i in range(0, 6)))
f = x*y + 1.00000000000000*a6*x + 1.00000000000000*a4*y + x*y^-1 + x^-1*y + 1.00000000000000*a3*y^-1 + 1.00000000000000*a1*x^-1 + x^-1*y^-1
Then I ask
f/(x^1*y^0) # (The powers have to be in this way, just from the context of the work I am doing)
and it outputs:
1.00000000000000/x*x*y + 1.00000000000000*a6/x*x + 1.00000000000000*a4/x*y + 1.00000000000000/x*x*y^-1 + 1.00000000000000/x*x^-1*y + 1.00000000000000*a3/x*y^-1 + 1.00000000000000*a1/x*x^-1 + 1.00000000000000/x*x^-1*y^-1
Now when I ask for the constant coefficient of this LP it tells me its 0 when it is a6.
How can I fix this.
Thanks in advanceEd CalSat, 26 Nov 2016 17:35:11 -0600https://ask.sagemath.org/question/35746/Collect polynomial in a different variablehttps://ask.sagemath.org/question/35537/collect-polynomial-in-a-different-variable/ I want to collect my polynomial in a different variable. How am I to do that. For example I have :
D=(a1*u^3+a2*u^2+a3*u+a4)x^4+(a5*u^3+a6*u^2+a7*u+a8)x^3+(a9*u^3+a10*u^2+a11*u+a12)x^2+(a13*u^3+a14*u^2+a15*u+a16)x
Now I want my `D` to be in the form where `u` is the main variable, so I will have :
D=(a1*x^4+a5*x^3+a9*x^2+a13*x)u^3+(...)u^2+(...)u
Maple do it with `collect` code. I try to search for a similar code in Sage but no luck.ShaFri, 11 Nov 2016 22:54:11 -0600https://ask.sagemath.org/question/35537/Manipulate coefficient extracted from symbolic derivationhttps://ask.sagemath.org/question/33304/manipulate-coefficient-extracted-from-symbolic-derivation/Hi,
var('k')
f=function('f')(r)
for k in range(1,5):
h(r)=r^(2*k-1)
t=f*h
for i in range (1,k):
t=(r^(-1)*diff(t,r)).collect(r)
end
b=t.coefficient(r*f)
show(t)
show(factor(b))
end
Output (last line)
r ↦ r4D[0,0,0](f)(r)+18r3D[0,0](f)(r)+87r2D[0](f)(r)+105rf(r)|
105|
I would have want it in a factorized form (1*3*5*7).
It seems that coefficient() gives the result as a function, because if i look with show(b)
r ↦ 105|
What can I do better or is this just not possible with sage code?
Thanks
Ps. The notation of higher order derivatives of symbolic functions should be better than D[0,0,...,0]f(r). Hopefully someone fixes it.
MathBoyTue, 03 May 2016 06:48:44 -0500https://ask.sagemath.org/question/33304/Extract coefficients of polynomialshttps://ask.sagemath.org/question/33096/extract-coefficients-of-polynomials/ I have the following code:
var('a')
_.<k> = PolynomialRing(ZZ)
f = k^3+2*k^2+1
g = k^3 + a*k^2 + 1
f.coefficients()
g.coefficients()
the coefficients of f that i get are perfect:
[1, 2, 1]
the coefficients of g should be [1,a,1], but instead i get:
[[k^3 + 1, 0], [k^2, 1]]
I've tried the solution suggested [here](http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/):
but it doesn't always produce the coefficients in a logical order.
As always, any help figuring out how to get the correct coefficients of g would be greatly appreciated.sophiaWed, 13 Apr 2016 22:59:50 -0500https://ask.sagemath.org/question/33096/ideal primality in a polynomial ring with integer coefficientshttps://ask.sagemath.org/question/10313/ideal-primality-in-a-polynomial-ring-with-integer-coefficients/Hello,
how primality of an ideal in a polynomial ring with integer coefficients can be checked?
SageMath (5.7) tells me 'notImplementedError'.
Example:
R = ZZ[x]
I = R.ideal(7)
I.is_prime()
# leads to NotImplementedError
In addition, does someone know a good reference to the related theory?
Thanks,
Jackjack77Wed, 03 Jul 2013 00:24:03 -0500https://ask.sagemath.org/question/10313/coefficient() with composite variablehttps://ask.sagemath.org/question/10021/coefficient-with-composite-variable/Say I have something like this:
var('a','b','c')
test=b*a + c*a + 3*a + b + c
I can find that the coefficient of 'a' is (b+c+3) using:
test.coefficient(a,1)
However, I am interested in find the coefficient of $ab$ (which in this case would just be $1$). I tried
test.coefficient(a*b,1)
but it just returns zero. Is something like this possible?daviddoriaMon, 15 Apr 2013 03:45:57 -0500https://ask.sagemath.org/question/10021/Extracting a matrix from linear expressionshttps://ask.sagemath.org/question/9756/extracting-a-matrix-from-linear-expressions/Suppose I have a vector depending on symbolic parameters, say `v=vector([a-b, b-c, c-a])`. How to extract the matrix naturally associated with the vector v ? I mean the matrix M
1 -1 0
0 1 -1
1 0 -1
since `vector([a, b, c]) * M == v`. In fact I need the equivalent to the Maple command **GenerateMatrix**.
Pascal OrtizMon, 11 Feb 2013 20:06:29 -0600https://ask.sagemath.org/question/9756/Creating an array of variableshttps://ask.sagemath.org/question/8390/creating-an-array-of-variables/Here is a very very basic question.
I want to create a polynomial, say
a_0*x^0 + a_1*x + a_2*x^2+ \cdots + a_{20} x^{20}.
I could define these a_i one at a time, but it would be much better to have a way to create an array A of length 20 where A[i] is the coefficient a_i. The idea is that I want to do some operations and solve for these coefficients, which will end up being rational numbers.
There must be some very basic command that I don't know, but I can't find it in the documentation.NathanMon, 17 Oct 2011 05:16:43 -0500https://ask.sagemath.org/question/8390/How to get the coefficient of $x^n$ in symbolic expression of matrixhttps://ask.sagemath.org/question/8350/how-to-get-the-coefficient-of-xn-in-symbolic-expression-of-matrix/I have the symbolic matrix:
x = var('x')
h = Matrix(SR, [[x,2],[3*x+5,4]])
How can I get the coefficients of $x^n$ in symbolic expression of matrix. I need to get the follow two matrix from that one:
[[1,0],[3,0]]
[[0,2],[5,4]]
Tengiz SharafievTue, 27 Sep 2011 15:50:38 -0500https://ask.sagemath.org/question/8350/Numerical approximation for expression coefficientshttps://ask.sagemath.org/question/8095/numerical-approximation-for-expression-coefficients/I currently have an equation that looks like
x = 4/5*(4*y - 3)*z - 1/3
How can I get Sage to convert the coefficients to numerical approximations? I want to end up with something like
x = 0.8*(4*y - 3)*z - 0.333333333
I tried x.n(), but that only gives a "cannot evaluate symbolic expression numerically" error. Also x*1.0 doesn't do anything.GroverThu, 28 Apr 2011 00:38:22 -0500https://ask.sagemath.org/question/8095/