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.Fri, 05 Mar 2021 14:23:18 +0100Why is PolynomialRing over SR much slower than SR?https://ask.sagemath.org/question/56028/why-is-polynomialring-over-sr-much-slower-than-sr/In my code, I was trying to invert a matrix whose entries are symbolic expressions that depend on a single variable (called "tau").
Because of the fact that in my code the dependency on tau is polynomial, at some point I realized, that, in principle, it would be better to use the FractionField over the PolynomialRing in one variable (tau) defined over the symbolic ring (SR) instead of using the symbolic ring with a new variable defined as var("tau"). I was expecting that this would have improved the performances because of the fact that now SageMath knows that there can not be any log(tau) or exp(tau) or something like that.
But, instead, everything became hundreds of times slower. In particular, the method solve_right of the matrix, for a matrix 4 x 4, requires more than half an hour instead of 0.1 seconds. Also, the simplify method seems to be a lot slower.
Do you know why this happens? I have tried to find which algorithm SageMath uses for solving a linear system, but I did not find anything. Does it uses a different algorithm for polynomial and for symbolic expressions? I suspect that for polynomial over a "standard" ring (like for example QQ), SageMath uses Singular while, when solving a system with a matrix on the symbolic ring, it uses Maxima. Therefore, maybe the problem is related with the fact that, when I use a polynomial ring over SR, Sage must use an internal implementation. Do you know if this is the case? And why this also affect the method simplify?
The following snippet generate two matrices with the same content (but using a different ring):
def build_matrix(poly_ring=False):
if poly_ring:
tau_ring = PolynomialRing(SR, 'tau')
tau_field = FractionField(tau_ring)
tau = tau_ring.gen(0)
M = matrix(tau_field, 2, 2)
f = vector(tau_field, [1, 1])
else:
tau = var('tau')
M = matrix(SR, 2, 2)
f = vector(SR, [1, 1])
M[0, 0] = (e^(2/ 3) + 1) * tau + e^2
M[0, 1] = e^-1 * tau + 3
M[1, 0] = tau + 2
M[1, 1] = 3 * tau + e^(1/ 2) + e^4
return M, f
And here there are the benchmark that I see:
m1, f1 = build_matrix(True)
m2, f2 = build_matrix(False)
%timeit m1.inverse()
>> 345 ms ± 14.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit m2.inverse()
>> 2.57 ms ± 52.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit m1.solve_right(f1)
>> 370 ms ± 72.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit m2.solve_right(f2)
>> 2.5 ms ± 21.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Thank you!stepFri, 05 Mar 2021 14:23:18 +0100https://ask.sagemath.org/question/56028/Possible bug when a symbolic function is declared twicehttps://ask.sagemath.org/question/51964/possible-bug-when-a-symbolic-function-is-declared-twice/ I don't understand the output of the following code:
sage: f = function('f')
sage: f(x).operator() is f
True
sage: f = function('f')
sage: f(x).operator() is f
False
Is that the intended behavior?
It seems that ``operator`` returns the first symbolic function defined with this name, not the actual operator. This is problematic for me because it means there are some cells in my notebook I can only run once, so I have to restart the kernel everytime I want to press "run all".Florentin JaffredoSun, 14 Jun 2020 14:14:04 +0200https://ask.sagemath.org/question/51964/Ideal.variety() when working with symbolic ringshttps://ask.sagemath.org/question/43873/idealvariety-when-working-with-symbolic-rings/I have a system of multivariate polynomials I would like to solve (the equations come from chemical reaction networks). I have many symbolic constants, but I've managed to generate a Groebner basis with the following code (using a toy example with solutions {{x: x0, y:0},{x: 0,y: x0}):
x0 = var('x0') # In practice, there are many constants
S.<x,y> = PolynomialRing(SR,order='lex'); # in practice, there are 3-10 variables
I = S.ideal(
x**2 + y**2 - x0**2,
x + y - x0
)
print(I.dimension()) # dimension should always be zero
# >> 0
print(I.groebner_basis())
# >> [x + y - x0,
# >> y^2 + (-x0)*y]
In practice, I get many resulting equations which are extremely long, and it is numerically challenging to sequentially find the roots of these polynomials (in downstream code, I will get values for the constant parameters and then need to return solutions to these equations on the fly). Using the `I.variety()` method would be great to symbolically isolate all possible solutions, but I get confusing internal errors when I try to evaluate it.
print(I.variety()) # should be equivalent to: I.variety(ring=SR)
#/Users/ksb/SageMath/local/lib/python2.7/site-packages/sage/rings/polynomial/multi_polynomial_ideal.pyc in _variety(T, V, v)
# 2340
# 2341 variable = f.variable(0)
#-> 2342 roots = f.univariate_polynomial().roots(ring=ring, multiplicities=False)
# 2343
# 2344 for root in roots:
#
#/Users/ksb/SageMath/local/lib/python2.7/site-packages/sage/rings/polynomial/polynomial_element.pyx in sage.rings.polynomial.polynomial_element.Polynomial.roots (build/cythonized/sage/rings/polynomial/polynomial_element.c:68811)()
# 7714 return l
# 7715 else:
#-> 7716 return [val for val,m in l]
# 7717 vname = 'do_not_use_this_name_in_a_polynomial_coefficient'
# 7718 var = SR(vname)
# TypeError: 'sage.symbolic.expression.Expression' object is not iterable
Is this expected behavior? When I try `f.univariate_polynomial().roots(ring=ring, multiplicities=False)` (setting `multiplicities=True` is what `I.variety()` does and leads to the error) I get `[1/2*x0,2]` for roots, which makes little sense (it seems like the roots should be `[0,-x0]`).
I would be happy to convert the `MPolynomial_polydict` (or the `sage.rings.polynomial.polynomial_ring.PolynomialRing_field_with_category.element_class` generated by `univariate_polynomial()`) into a symbolic expression that I could just set it equal to zero and use `solve` on it, but I don't know how to do this type conversion. ksbMon, 08 Oct 2018 19:41:03 +0200https://ask.sagemath.org/question/43873/Matrix Group over Symbolic Ringhttps://ask.sagemath.org/question/40976/matrix-group-over-symbolic-ring/I have problem on generate matrix group over symbolic ring.
First, I define
eta=I;
eta2=(1+I)*sqrt(2)/2;
Then, define a generator matrix
T=matrix(SR,4,[eta**(i*j)*eta2/2 for i in range(4) for j in range(4)]);
I try to make a matrix group by
G=MatrixGroup(T);
What I get is only very long computation that does not give any result.
Can somebody help me? Thank you very much.
I have checked the order of T, I got 8 since T**8=I where I is identity matrix. dimahphoneMon, 05 Feb 2018 05:31:14 +0100https://ask.sagemath.org/question/40976/recursively solve and substitutehttps://ask.sagemath.org/question/40753/recursively-solve-and-substitute/I'm working on a pretty complicated problem (theoretically, not necessarily computationally). I'm not sure how to distill my question down to a simple example. But basically I have a system of equations over the ring of polynomials (or power series) over the symbolic ring. I want to be able to recursively solve for one symbolic function in terms of another, substitute the solution, and repeat. The ultimate goal is to extract relevant data all as a function of some positive integer k. It seems like I should be able to build a loop to do this, but I'm new to sage and programming in general. I haven't been able to find anything quite like what I'm trying to do online. So I'm asking for suggestions.
Here are some more details:
I have several polynomial expressions
j,k,z = var('j,k,z')
a,f=function('a,f')
p= lambda k : z^(-2*k) + sum(a(j)*z^(-j) for j in range(2*k-1))
F= lambda k : sum(f(j)*z^j, j, 2, 2*k)
which I plug into a formal matrix valued function and perform a series of algebraic manipulations. In the end I get a matrix whose entries are polynomials in the a(j)'s and f(j)'s. I want to recursively solve for the f(j)'s in terms of the a(j)'s so as to diagonalize this matrix expression. I can do this "by hand" but I want a function that will just spit out the end result. For example I know that f(2) = 1/4*a(2*k-2) for any k.
I've tried, for example:
def A1(k) :
for j in range(2,2*k) :
A0(k)=A0(k).substitute(f0(k,j)[0])
return A0(k)
Here A0(k) is a previously defined matrix expression and f0(k,j) is a function that solves for f(j). This code actually runs but then trying to call A1(2), etc., leads to errors.
Another attempt was to define
A1= lambda k,j : coef(A0(k),-k-2+j).substitute(f0(k,j)[0])
and then try to define a function of k in terms of the A1(k,j)'s using the sum() command. (In the code above coef(A,j) is a function that returns the coefficient of the z^j term in a polynomial over a ring of matrices).
The problem with this last attempt is that A1(k,j) for j bigger than 2 returns terms that include f(2), f(3), etc. I need something that solves for f(2), substitutes this value for f(2) into all future computations; then solves for f(3), substitutes this values for f(3) into all future computations; and so on until f(2*k).
I apologize if this question is too broad somehow. In looking over some of the literature on loops, solve(), substitute(), etc., I haven't found anything quite like this. Any suggestions? And thank you.charleslebarronTue, 23 Jan 2018 19:33:04 +0100https://ask.sagemath.org/question/40753/