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, 06 Nov 2020 23:25:37 +0100reposition interact slider and avoid division by zerohttps://ask.sagemath.org/question/54140/reposition-interact-slider-and-avoid-division-by-zero/Can I reposition the interact slider for a plot? It often goes above the viewport and if I scroll the viewport to see the slider I cut off the lower part of the function.
Also, some plots, at a certain point, end up with division by zero. I get an error message at that point in the slider, but you can slide past that. Still, is there a way to avoid the one point where I divide by zero in a plotting statement? I tried "assume" but I apparently don't quite know how it works and the docs I found so far are inscrutable. cybervigilanteFri, 06 Nov 2020 23:25:37 +0100https://ask.sagemath.org/question/54140/Extracting terms of polynomial of certain powershttps://ask.sagemath.org/question/53323/extracting-terms-of-polynomial-of-certain-powers/Let's say I have a polynomial f. I want to extract the monomials where every power of variable is divisible by a number, say 5. I have two ideas on how to do this.
Method 1:
R.<x,y,z> = QQ[]
f=x^(5)*y^(2)+z^(2)*x^(2)+y^(5)*x^(10)
The end result should give me y^(5)x^(10) as the first term has a power 2 which is not divisible by 5 and obviously the 2nd term is not. I am stuck here as I want to somewhat say the following.
f.exponents()
gives
[(10, 5, 0), (5, 2, 0), (2, 0, 2)]
Then I want to say
f.exponents()[i] % (5,5,5) == (0,0,0) is False, remove the ith term
There are 2 problems with this. The first is when I do f.exponents(), you see the tuples they give me are rearranged so the "ith" term is no longer what I think it is. For example, (10,0,5) is the 3rd term of f, but they put it as the first tuple. The second thing is f.exponents()[i] % (5,5,5) is not valid (i.e. gives error) but hopefully you get what I want. I basically want to divide component-wise and get all remainder 0. What is the correct code for this and the coding language for "remove the ith term"?
Method 2: I can consider f(x^(1/5),y^(1/5),z^(1/5)) and then check whether f.exponents()[i] belongs to Z^(3)-cartesian product of integers. The issue is again I don't know how to code this and f(x^(1/5),y^(1/5),z^(1/5)) gives error as I lie in the polynomial ring. The polynomial ring doesn't allow fractional exponents. I looked online and maybe I am suppose to work with Puiseux polynomials?
https://stackoverflow.com/questions/51077537/what-is-the-best-way-to-implement-polynomials-with-fractional-exponents-with-sagwhatupmattSat, 05 Sep 2020 19:35:39 +0200https://ask.sagemath.org/question/53323/Rewriting linear combination of Groebner basis in terms of original termshttps://ask.sagemath.org/question/53267/rewriting-linear-combination-of-groebner-basis-in-terms-of-original-terms/ Let assume I have an ideal given by
x,y,z = QQ['x,y,z'].gens()
I = ideal(f1,f2,f3)
B = I.groebner_basis()
where f1,f2,f3 are just polynomials in variables x,y,z. Let's say B=(g1,g2).
Let's assume I happen to take a polynomial,h, that is in my ideal I. Then doing polynomial division, I can write
h=h1*g1+h2*g2
Basically I can write h as a linear combination of the elements in my Groebner basis. Is there a function that converts a linear combination in terms of Groebner to linear combination of terms in my ideal I? i.e.I can write
h=q1*f1+q2*f2+q3*f3
whatupmattWed, 02 Sep 2020 23:41:22 +0200https://ask.sagemath.org/question/53267/Can't do simple algebraic divisionhttps://ask.sagemath.org/question/52792/cant-do-simple-algebraic-division/Why can't I do a simple division with Sage?
All I get out is what I put in.
I can do this by hand easily but no matter what method
or option I try I get the same answer.
In: (3*x^3+x^2-3*x+5)/(x+1)
Out: (3*x^3 + x^2 - 3*x + 5)/(x + 1)cybervigilanteSat, 01 Aug 2020 05:01:46 +0200https://ask.sagemath.org/question/52792/find quotient of two multivariate polynomials (which are divisible)https://ask.sagemath.org/question/52623/find-quotient-of-two-multivariate-polynomials-which-are-divisible/I have two multivariate polynomials, `num` and `denom` such that
`denom` divides `num`. But I have not been able to get SageMath
to simplify the quotient `num/denom`.
The worksheet and the two polynomials are available at
- https://cocalc.com/share/513993d46f58cd3c1c6c6b22d60962e425fecad2/polynomial_division.ipynb?viewer=share
- https://cocalc.com/share/6884a274c847cbf4780bf636290777ad5e710828/denom.sobj?viewer=share
- https://cocalc.com/share/d8904075cf0e67344ac25f47edfa8d38de104379/num.sobj?viewer=share
Sample code is below.
sage: dR.<d1, d2, d3, d4, d12, d13, d14, d23, d24, d34, d123, d124, d134, d234, d1234> = PolynomialRing(ZZ, 15, order='lex')
sage: denom = load('denom')
sage: num = load('num')
sage: denom.divides(num)
True
sage: num.reduce(Ideal([denom]))
0
sage: F = num/denom
sage: F.denominator()/denom
1
sage: F.reduce()
sage: F.denominator() / denom
1
sage: num.number_of_terms()
3197
sage: denom.number_of_terms()
64
sage: num.degree()
24Daniel FriedanWed, 22 Jul 2020 22:35:21 +0200https://ask.sagemath.org/question/52623/Polynomial division in quotient rings?https://ask.sagemath.org/question/50406/polynomial-division-in-quotient-rings/I have a working example shown below to help explain my question.
R.<x> = GF(2)[];
I = R.ideal([x^2 - 1])
S.<u> = R.quotient_ring(I)
f = x^2 + x
g = x
print(f.quo_rem(g))
f = u^2 + u
g = u
print(f.quo_rem(g))
The first quo_rem call works fine, but the second does not. Is polynomial division not supported for quotient rings, or am I calling it the wrong way? I'm specifically interested in doing this for multivariate polynomials, but I can't seem to get it to work for a single variable either.
Related to the question, I am interested in understanding this division in quotient rings better. If the operation isn't supported in sage, any sort of references/algorithms on how to do such a calculation would be very helpful and appreciated.hshackleSun, 29 Mar 2020 00:49:43 +0100https://ask.sagemath.org/question/50406/Using piecewise-defined functions for recursive integer sequencehttps://ask.sagemath.org/question/49260/using-piecewise-defined-functions-for-recursive-integer-sequence/I have
![image description](https://matheplanet.de/matheplanet/nuke/html/uploads/b/50970_4_56555555.png)
with `div --> //` ("integer part" of division).
I wonder how to use piecewise-defined functions for calculating `n(i)` and `Q(i)`. <br>
This does not work: <br>
r = 1
m = 7
n(i) = piecewise([([0,0], r//m), ((0,oo), 10*(n(i-1)-Q(i-1)*m))])
Q(i) = piecewise([([0,0], 0), ((1,oo), n(i)//m)])
print n(0)
print n(1)
print n(5)
![image description](https://matheplanet.de/matheplanet/nuke/html/uploads/b/50970_45_9999999999999.jpg)
geroyxMon, 30 Dec 2019 16:46:55 +0100https://ask.sagemath.org/question/49260/Request: Have the len function output a Sage Integer instead of a Python inthttps://ask.sagemath.org/question/48141/request-have-the-len-function-output-a-sage-integer-instead-of-a-python-int/In the following code (See https://sagecell.sagemath.org/?q=cctcfj ):
a=[4,5,6]
b=[0,1,2]
for i in zip([0,1,2],a):
print i[0],"divided by",len(a),"is equal to",i[0]/len(a)
a=[4,5,6]
b=range(len(a))
print(b)
for i in zip(b,a):
print i[0],"divided by",len(a),"is equal to",i[0]/len(a)
Gives the following output
0 divided by 3 is equal to 0
1 divided by 3 is equal to 1/3
2 divided by 3 is equal to 2/3
[0, 1, 2]
0 divided by 3 is equal to 0
1 divided by 3 is equal to 0
2 divided by 3 is equal to 0
Greetings
-AAdrian sotoWed, 02 Oct 2019 16:02:50 +0200https://ask.sagemath.org/question/48141/array numpy mixed division problemhttps://ask.sagemath.org/question/42456/array-numpy-mixed-division-problem/ Hi
why this line is ok:
DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/int(len(DiceT[die]))))])
Except the fact that I do not understand why it is written 0.8333333 instead of 5/6 as usual
but this one is ko ?:
DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]] / int(len(DiceT[die])))])
in notebook Sagemath 8.2
forget()
import numpy as np
DiceT=[[3,3,3,3,3,6],[1,4,4,4,4,4] ,[2,2,2,5,5,5]]
DiceColorT=['Red','Green','White']
DiceValueFrequencyT=[]
#Rf=[[3,5/6],[6,1/6]];Gf=[[1,1/6],[4,5/6]];Wf=[[2,1/2],[5,1/2]];
for die in range(0,len(DiceT)) :
freqT = np.array(DiceT[die])
valueT = np.bincount(freqT)
nonZeroValueT=np.nonzero(valueT)[0]
show(valueT)
show(nonZeroValueT)
for value in range(0,len(nonZeroValueT)) :
print int(len(DiceT[die]))
# these Two lines are ok when uncommented
#DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/6))])
DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/int(len(DiceT[die]))))])
# but this one give false results when uncommented!
#DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]] / int(len(DiceT[die])))])
show (DiceValueFrequencyT)
ortolljMon, 28 May 2018 18:42:16 +0200https://ask.sagemath.org/question/42456/Bug report: Kernel dies after 1 hour while dividing polynomialshttps://ask.sagemath.org/question/40935/bug-report-kernel-dies-after-1-hour-while-dividing-polynomials/I want to submit an **error report** for SAGE 7.0 and 8.0:
The division of two polynomials in an ideal causes an **kernel death** after one hour of computation.
Singular performs the calculation in about **10 seconds**.
**Sage source code:**
Q.<E,F,X,Y> = QQ['E', 'F', 'X', 'Y'];
i1 = F^4+(E^3+E^2-E+2)*F^3+(E^3-3*E+1)*F^2-(E^4+2*E)*F+E^3+E^2;
i2 = Y^2+(E^3+E^2*(3*F+2)-E*(F^2-2*F-1)-F*(F^2+3*F+1))*X*Y+(F*(E+1)*(E-F)*(E+F+1)^2*(E^2+E-F)*(E^2+E*F+E-F^2-F))*Y-X^3-(F*(E+1)*(E-F)*(E+F+1)*(E^2+E-F))*X^2;
J = Q.ideal(i1, i2);
R.<e,f,x,y> = QuotientRing(Q, J);
poly1 = x^4+(-e^2*f^4+e^4*f+2*e^3*f^2-e*f^4-2*e*f^3+2*f^4+2*e^2*f-6*e*f^2+f^3+5*e^2-5*e*f+11*f^2-17*e+18*f+16)*x^3+(-2*e^3-2*e^2*f+e*f^2+f^3-3*e^2+2*f^2-e+f)*x^2*y+(-6*e^2*f^5+11*e*f^6-3*f^7+3*e^6-6*e^5*f+17*e^4*f^2-69*e^2*f^4+5*e*f^5+17*f^6-5*e^5+26*e^4*f+83*e^3*f^2-106*e^2*f^3-104*e*f^4+71*f^5-28*e^4+66*e^3*f+57*e^2*f^2-149*e*f^3+54*f^4-55*e^3+78*e^2*f+9*e*f^2-32*f^3-35*e^2+70*e*f-35*f^2)*x^2+(-e^2*f^7-e^7*f-3*e^6*f^2-2*e^2*f^6-e*f^7-5*e^6*f-e^5*f^2-6*e^2*f^5-6*e*f^6+2*f^7+3*e^5*f-2*e^4*f^2-2*e^2*f^4-15*e*f^5+2*f^6-2*e^5-7*e^4*f-4*e^3*f^2-9*e^2*f^3-4*e*f^4+3*e^4+20*e^3*f-43*e^2*f^2-21*e*f^3+28*f^4-e^3-32*e^2*f-61*e*f^2+61*f^3-22*e^2-53*e*f+59*f^2-16*e+16*f)*x*y+(e^6+2*e^5*f+3*e^5-4*e^3*f^2-2*e^2*f^3+2*e*f^4+f^5+3*e^4-5*e^2*f^2+2*f^4+3*e^3-3*e^2*f-e*f^2+4*f^3-3*e^2+e*f-9*f^2+17*e-18*f-16)*y^2+(11*e*f^9-3*f^10-3*e^9-5*e^7*f^2-101*e^2*f^7+30*e*f^8+11*f^9-e^8-32*e^7*f-74*e^6*f^2-422*e^2*f^6-117*e*f^7+121*f^8+35*e^7-39*e^6*f-134*e^5*f^2-486*e^2*f^5-660*e*f^6+276*f^7+141*e^6-106*e^5*f-226*e^4*f^2+384*e^2*f^4-1019*e*f^5+36*f^6+189*e^5-367*e^4*f-681*e^3*f^2+1382*e^2*f^3+130*e*f^4-884*f^5+61*e^4-1019*e^3*f+565*e^2*f^2+1914*e*f^3-1521*f^4-251*e^3-499*e^2*f+1751*e*f^2-1001*f^3-231*e^2+462*e*f-231*f^2)*y;
poly2 = x+e^5*f+e^4*f^2-e^2*f^4+2*e^4*f+e^3*f^2-3*e^2*f^3+f^5+3*e^3*f-3*e^2*f^2-3*e*f^3+3*f^4+e^3+e^2*f-5*e*f^2+3*f^3+e^2-2*e*f+f^2;
print (poly1 / poly2);
**Singular source code:**
In Singular you cannot use the operator "/" to divide polynomials, since "non divisible terms" will be discarded and set to zero, see manual [https://www.singular.uni-kl.de/Manual/4-0-3/sing_150.htm#SEC189](https://www.singular.uni-kl.de/Manual/4-0-3/sing_150.htm#SEC189).
So in Singular we have to use the "lift" command to calculate the quotient "poly 1 / poly2":
ring R = 0,(x,y,e,f),dp;
poly i1 = f^4+(e^3+e^2-e+2)*f^3+(e^3-3*e+1)*f^2-(e^4+2*e)*f+e^3+e^2;
poly i2 = y^2 + (e^3 + e^2*(3*f+2) - e*(f^2-2*f-1) - f*(f^2+3*f+1))*x*y + (f * (e+1) * (e-f) * (e+f+1)^2 * (e^2+e-f) * (e^2+e*f+e-f^2-f)) * y - x^3 - (f * (e+ 1) * (e-f) * (e+f+1) * (e^2+e-f)) * x^2;
ideal I = i1,i2;
ideal J = std(I);
poly poly1 = x^4 + (-e^2*f^4 + e^4*f + 2*e^3*f^2 - e*f^4 - 2*e*f^3 + 2*f^4 + 2*e^2*
f - 6*e*f^2 + f^3 + 5*e^2 - 5*e*f + 11*f^2 - 17*e + 18*f + 16)*x^3 + (-2*e^3 - 2
*e^2*f + e*f^2 + f^3 - 3*e^2 + 2*f^2 - e + f)*x^2*y + (-6*e^2*f^5 + 11*e*f^6 - 3
*f^7 + 3*e^6 - 6*e^5*f + 17*e^4*f^2 - 69*e^2*f^4 + 5*e*f^5 + 17*f^6 - 5*e^5 + 26
*e^4*f + 83*e^3*f^2 - 106*e^2*f^3 - 104*e*f^4 + 71*f^5 - 28*e^4 + 66*e^3*f + 57*
e^2*f^2 - 149*e*f^3 + 54*f^4 - 55*e^3 + 78*e^2*f + 9*e*f^2 - 32*f^3 - 35*e^2 + 7
0*e*f - 35*f^2)*x^2 + (-e^2*f^7 - e^7*f - 3*e^6*f^2 - 2*e^2*f^6 - e*f^7 - 5*e^6*
f - e^5*f^2 - 6*e^2*f^5 - 6*e*f^6 + 2*f^7 + 3*e^5*f - 2*e^4*f^2 - 2*e^2*f^4 - 15
*e*f^5 + 2*f^6 - 2*e^5 - 7*e^4*f - 4*e^3*f^2 - 9*e^2*f^3 - 4*e*f^4 + 3*e^4 + 20*
e^3*f - 43*e^2*f^2 - 21*e*f^3 + 28*f^4 - e^3 - 32*e^2*f - 61*e*f^2 + 61*f^3 - 22
*e^2 - 53*e*f + 59*f^2 - 16*e + 16*f)*x*y + (e^6 + 2*e^5*f + 3*e^5 - 4*e^3*f^2 -
2*e^2*f^3 + 2*e*f^4 + f^5 + 3*e^4 - 5*e^2*f^2 + 2*f^4 + 3*e^3 - 3*e^2*f - e*f^2
+ 4*f^3 - 3*e^2 + e*f - 9*f^2 + 17*e - 18*f - 16)*y^2 + (11*e*f^9 - 3*f^10 - 3*
e^9 - 5*e^7*f^2 - 101*e^2*f^7 + 30*e*f^8 + 11*f^9 - e^8 - 32*e^7*f - 74*e^6*f^2
- 422*e^2*f^6 - 117*e*f^7 + 121*f^8 + 35*e^7 - 39*e^6*f - 134*e^5*f^2 - 486*e^2*
f^5 - 660*e*f^6 + 276*f^7 + 141*e^6 - 106*e^5*f - 226*e^4*f^2 + 384*e^2*f^4 - 10
19*e*f^5 + 36*f^6 + 189*e^5 - 367*e^4*f - 681*e^3*f^2 + 1382*e^2*f^3 + 130*e*f^4
- 884*f^5 + 61*e^4 - 1019*e^3*f + 565*e^2*f^2 + 1914*e*f^3 - 1521*f^4 - 251*e^3
- 499*e^2*f + 1751*e*f^2 - 1001*f^3 - 231*e^2 + 462*e*f - 231*f^2)*y;
poly poly2 = x + e^5*f + e^4*f^2 - e^2*f^4 + 2*e^4*f + e^3*f^2 - 3*e^2*f^3 + f^5
+ 3*e^3*f - 3*e^2*f^2 - 3*e*f^3 + 3*f^4 + e^3 + e^2*f - 5*e*f^2 + 3*f^3 + e^2 - 2*e*f
+ f^2;
poly t1 = lift(poly2,poly1)[1,1];
**Singular output:**
t1;
-ye9+5ye7f2-5ye8+7ye7f+15ye6f2+4ye2f6-2yef7+x2e6-10ye7-2x2e5f+14ye6f-x2e4f2+20ye5f2+26ye2f5+yef6-5yf7+3x2e5-11ye6-7x2e4f+22ye5f+3ye4f2+4x2e2f3-2x2ef4+49ye2f4+43yef5-22yf6+3x2e4+4ye5-12x2e3f-7ye4f+10x2e2f2-27ye3f2+4x2ef3+10ye2f3-5x2f4+91yef4-18yf5-x2e3+xye3+29ye4-5x2e2f+7xye2f+11ye3f+13x2ef2-2xyef2-99ye2f2-7x2f3-2xyf3-4yef3+63yf4-2x2e2+3xye2+73ye3+4x2ef+6xyef-29ye2f-2x2f2-7xyf2-161yef2+117yf3-2x3+2xye+53ye2-2xyf-106yef+53yf2+3y2Patrick ReichertFri, 02 Feb 2018 18:14:30 +0100https://ask.sagemath.org/question/40935// acting like // ?https://ask.sagemath.org/question/38853/acting-like/ This script is meant to simulate rolling a fair die until a 6 occurs. I don't understand why at the very end my division of the sum of the rolls by the number of rolls is coming out the way it does. Thanks for your attention.
a=0;l=[];count=0;b=[]
while count < 11:
while a<6:
a=choice([1..6])
l.append(a)
print l
count+=1
b.append(len(l))
a=0;l=[]
s=sum(b);t=len(b)
print "b= ",b,"Sum=",sum(b),"Throws=",len(b),"Sum/Throws=",s,"/",t,"=",s/t,N(s/t)
print "Average over",len(b),"trials is",(sum(b)/len(b))
print "The type of sum(b) is",type(sum(b)),"The type of len(b) is",type(len(b))Ducky47Tue, 19 Sep 2017 13:35:50 +0200https://ask.sagemath.org/question/38853/Polynomial Long Division with Variable Coefficientshttps://ask.sagemath.org/question/38187/polynomial-long-division-with-variable-coefficients/I want to divide the following polynomial (in terms of $t$) with coefficients in terms of $\lambda$.
$$(\lambda^6 - 5\lambda^4 + 6\lambda^2 - 1)t^5 + (\lambda^5 - 4\lambda^3 + 3\lambda^2)t^6$$
by $$ \lambda t^2 -\lambda^2 t + \lambda$$
The resulting quotient will include a fractional component (the numerator's
degree will be strictly less than the denominator's degree).
This is what a quotient and remainder, added together, might look like:
$$ t(\frac{2\lambda^8 - 9 \lambda^6 + 2 \lambda^5 + 6 \lambda^4 - 4\lambda^2}{\lambda}) + t^3(\frac{2\lambda^6 - 9\lambda^4 + 3\lambda^3 + 6\lambda^2 -1 }{\lambda}) + \frac{t(\lambda^4 - 2\lambda) + (\lambda^3 - 4\lambda^2)}{\lambda t^2 - \lambda^2 t + \lambda}$$
*I have tried the following thus far* Any suggestions? The code below "does not work", because it outputs a quotient whose degree is greater than the degree of the dividend. Here $y$ takes the place of $\lambda$
and $x$ takes the place of $t$.
The quotient is:
-2*x^8 - x^4*y^4 + 2.0*x^6 - 3.0*x^5 - 1.0*x^4 - (2*x^5 + x^3)*y^3 + (-2*x^6 + 2.0*x^4 - x^2)*y^2 + (-2*x^7 +
4.0*x^5 + 1.0*x^3 - x)*y - 1
The remainder is:
-x^5 + (2*x^10 + 3.0*x^7 - 1.0*x^6 + 3.0*x^5 + 1.0*x^4 + x^2 + 1)*y
The code:
from sympy import Function, rsolve_poly, Symbol, rsolve, rsolve_hyper, oo
from sympy.abc import n
x = var('x')
y = var('y')
P.<x,y> = PolynomialRing(CC)
f = (y**5 - 4*y**3 + 3*y**2)*x**6 + (y**6 - 5*y**4 + 6*y**2 - 1)*x**5
g = y*x**2 - y**2*x + y
def division(dividend, divisor):
return (dividend._maxima_().divide(divisor).sage())
a = division(f,g)
print("The quotient is: \n")
print(a[0])
print("The remainder is: \n")
print(a[1])MunoThu, 06 Jul 2017 07:12:28 +0200https://ask.sagemath.org/question/38187/How to get the quotients in multivariable polynomial division?https://ask.sagemath.org/question/36901/how-to-get-the-quotients-in-multivariable-polynomial-division/Given a Groebner basis $G=\lbrace f_1,\dots,f_s \rbrace$ and $g$, how can I get the quotients of $g$ on division by $G$ ? I know that when considering the quotients, the ordering we use to list the divisors $f_1,\dots,f_s$ matters. I also know the command `p.reduce(I)` for remainders. I would appreciate any help with this situation.KarlFri, 10 Mar 2017 13:47:41 +0100https://ask.sagemath.org/question/36901/Unexpected Answer for an Equationhttps://ask.sagemath.org/question/33254/unexpected-answer-for-an-equation/ Hello,
I am trying to calculate the following equation in SAGE.
N = 100.
p = 0.9.
r = (N /(N-1))*(ln(N)-ln(-ln(p))); r
Unfortunately, sage gives this answer **100/99*log(100) + 2.27309831041661** , which is not the expected answer !!
But, when I used google calculator, I got the expected answer, which is **6.92478536697**
Please let me know what is going on.? Thank you. usamrfThu, 28 Apr 2016 20:52:10 +0200https://ask.sagemath.org/question/33254/Possible bug of sum functionhttps://ask.sagemath.org/question/33068/possible-bug-of-sum-function/Consider the following code:
def s(n):
return sum(k/n for k in range(n + 1))
L0 = s(2)
for n in range(2, 3):
L1 = s(n)
The answers are (using sage 7.1):
print L0, L1
(3/2, 1)
In the second case sage is using the usual integer division of python 2.x while in the first one it is using the exact representation of rationals. Is this a known bug?
jllbMon, 11 Apr 2016 19:15:50 +0200https://ask.sagemath.org/question/33068/range and division : unexpected behaviorhttps://ask.sagemath.org/question/32891/range-and-division-unexpected-behavior/Consider the following snippet :
N=5
# code 1
for n in range(N,N+1):
for k in range(0,n):
print k/n
print '-'*10
# code 2
n=N
for k in range(0,n):
print k/n
I was expecting code 1 and code 2 to print the same output. This is not the case :
0
0
0
0
0
----------
0
1/5
2/5
3/5
4/5
In the first case, `k/n` is Python-evaluated as an integer division, in the second case, `k/n` is Sage-evaluated as a fraction. Can someone elaborate please ?
I only notice that substituting `srange(N,N+1)` to `range(N,N+1)` fixes the problem.
candideSun, 27 Mar 2016 11:28:01 +0200https://ask.sagemath.org/question/32891/Integer result for fractionhttps://ask.sagemath.org/question/32347/integer-result-for-fraction/ Hi!
Can somebody explain me this strange behavior of Sage :
for p in range(1,10):
for q in range(1,10):
print p/q
Then `p/q` return the integer division.
However:
for p in range(1,10):
print p/7
give rational results.
A solution is to use `Rational(p)/Rational(q)` instead of `p/q`. But it is not an explanation.
Thanks,
Arnaud
Arnaud1418Wed, 27 Jan 2016 20:12:43 +0100https://ask.sagemath.org/question/32347/dividing vector(a,b) by sqrt(x) gives (a/x*sqrt(x),b/x*sqrt(x))https://ask.sagemath.org/question/25980/dividing-vectorab-by-sqrtx-gives-axsqrtxbxsqrtx/ I can't seem to figure out why the behavior above is happening.
I have been trying to familiarize myself with Sage as I am both an IT person at my university and currently a Calc III student and it would seem to me to be helpful for both to know how this software works since many professors use it and I can use it to check work in my classes.
When I attempt to get the unit vector in the direction of <1,1> I divide the vector by its magnitude, and instead of the expected result of <1/sqrt(2), 1/sqrt(2)> I have been getting <1/2*sqrt(2), 1/2*sqrt(2)> and this happens the same way with a vector divided by the sqrt of anything. (i.e. <1,1> / sqrt(3) == <1/3*sqrt(3), 1/3*sqrt(3)>)
Is this a bug, or am I missing something?
EDIT - To clarify, I am dividing a vector by it's magnitude, which is a scalar number. vector{a,b} divded by c should yield vector{a/c, b/c}. This works fine for integers. but when doing so with the sqrt() function it produces odd results. So if c == sqrt(d) the answer is coming back as vector{a/(d*c), b/(d*c)} and I don't understand why the denominator is being multiplied by d.
RickySun, 01 Mar 2015 00:27:40 +0100https://ask.sagemath.org/question/25980/multivariable division algorithmhttps://ask.sagemath.org/question/9702/multivariable-division-algorithm/let f=x^3*y^3+2y^2, f1=2xy^2+3x+4y^2, f2=y^2-2y-2 in Q[x,y]
using lex with x>y divide f by f1,f2 to obtain a reminder r. Repeat this exercise reversing the role of f1 and f2.RoseMon, 17 Feb 2014 22:19:42 +0100https://ask.sagemath.org/question/9702/Dividing by 2 fast: how?https://ask.sagemath.org/question/10854/dividing-by-2-fast-how/Please consider the following example:<p>
tt=1200<br>
ss=tt>>2<br>
print ss<br>
300<p>
The division by 4 goes fast. But how can I divide by 2?<p>
The expression <b>tt>2</b> yields a boolean. And the options <b>ss=tt//2</b> and <b>ss=tt; ss/=2</b> seem not optimal.<p>
timeit("tt=1200;ss=tt>>2") 625 loops, best of 3: 152 ns per loop<br>
timeit("tt=1200;ss=tt//2") 625 loops, best of 3: 323 ns per loop <br>
timeit("tt=1200;ss=tt;ss/=2") 625 loops, best of 3: 552 ns per loop<p>
Thanks for you kind consideration.
RolandbMon, 23 Dec 2013 03:43:39 +0100https://ask.sagemath.org/question/10854/function divisionhttps://ask.sagemath.org/question/10575/function-division/I plotted the 2 following equations:
(2*x^3+2*x) / (x^2+1)
2*x
p1 = plot((2*x^3+2*x) / (x^2+1), (x,-15,15), ymin=-15, ymax=15)
p2 = plot(2*x, (x,-15,15), ymin=-15, ymax=15, color='red')
p0= p1+p2
show(p0)
And they both displayed the same output:
I tried to do, (2*x^3+2*x) / (x^2+1) , but was displayed, 2*(x^3 + x)/(x^2 + 1)
How can I get 2*x to be displayed as the output?
bxdinSat, 28 Sep 2013 09:10:14 +0200https://ask.sagemath.org/question/10575/vector division?https://ask.sagemath.org/question/10451/vector-division/given a vector in sage
sage: a,b=var('a,b')
sage: sv=vector(SR,[1,a,b^2])
then why is this a meaningful operation
sage: sv/sv
1
To my (utterly humble) understanding, I always thought, that if vector multiplication '*' is defined via the scalar product - as it seems to be in sage
sage: sv*sv
a^2 + b^4 + 1
then, division cannot be defined meaningfully?
[Just as a sideremark along that line: I have no problem with numpy's element wise array-arithmetic
In [1]: v=array([1.,2.,3.]) # vector
In [2]: e=array([1.,1.,1.]) # unity is a vector
In [3]: e*v == v # multiplication by unity
Out[4]: array([ True, True, True], dtype=bool)
In [5]: vi=v**(-1) # inverse is a vector
In [6]: e/v == vi # unity/vector == inverse
Out[7]: array([ True, True, True], dtype=bool)
In [8]: e == v*vi # vector * inverse == unity
Out[9]: array([ True, True, True], dtype=bool)
In my layman's world, this type of division makes perfect sense. (Moreover I'm curious why sage seems to not adopt this pythonic way of array arithmetic)]MarkSun, 18 Aug 2013 05:18:06 +0200https://ask.sagemath.org/question/10451/Division of polynomial matriceshttps://ask.sagemath.org/question/10439/division-of-polynomial-matrices/Is it possible to perform euclidean division between two polynomial matrices in sage?
e.g.if
$A= \begin{bmatrix}
x^2 +1 & x \newline
0&x-1
\end{bmatrix} , B=\begin{bmatrix}
x & 2 \newline
1 &x-1
\end{bmatrix}$
are given find the matrices $Q,R$ so $A=QB +R$
*in this example the answer is $Q= \begin{bmatrix}
x & -1 \newline
0& 1
\end{bmatrix} , R=\begin{bmatrix}
2 & -1 \newline
-1 & 0
\end{bmatrix}$*koukourikosThu, 15 Aug 2013 15:03:25 +0200https://ask.sagemath.org/question/10439/divide numerator and denominator by certain valuehttps://ask.sagemath.org/question/8785/divide-numerator-and-denominator-by-certain-value/Hi,
is it somehow possible to divide the numerator and denominator of a fraction by the same value? For example, (a*b + c)/(b*d + a + c) gets (a + c/b)/(d + a/b + c/b) if I divide it by b. matthjesMon, 12 Mar 2012 05:55:24 +0100https://ask.sagemath.org/question/8785/[Solved] Difference about divide operator in python2 and sagehttps://ask.sagemath.org/question/8574/solved-difference-about-divide-operator-in-python2-and-sage/I am writing a package (I want to use "import" to import it into sage).
I found that in the package (say, test.py), if I write
from sage.all import *
def test():
var('a')
print -1/2, -a/2, (-1/2)*a
and in sage type
import test; test.test()
I got "-1 -1/2*a -a"
This is different from directly run the above commands from sage.
I know in python 2, -1/2 returns -1. But is there a way to write "minus one half" (-1/2) in a package, say, test.py?
I know I could use load or attach to load a .sage package. However, they don't have namespace and I hope to have namespace as in python.
I could also use 1/2.0, which returns 0.5. But I would want an exact number instead of a float point approximation, for further use.
To summarize, in this case, how to write -1/2 in test.py? Or is there a way to load a sage package with namespace? Thanks!tririverSat, 17 Dec 2011 15:00:38 +0100https://ask.sagemath.org/question/8574/When does 1/2=0 ? (python's integer division Vs Sage's exact fractions)https://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/Hi all !
I know that python evaluates 1/2 to zero because it is making an integer division. Well. I was expecting Sage not to do so.
It does more or less ...
------ sagess.py ----------
#! /usr/bin/sage -python
# -*- coding: utf8 -*-
from sage.all import *
def MyFunction(x,y):
print x/y
MyFunction(1,2)
------ end of sagess.py ----------
When I launche that script from the command line, I got what Python would give :
18:08:03 ~/script >$ ./sagess.py
0
But when I launch it from the sage'terminal it is less clear how it works :
18:16:04 ~/script >$ sage
----------------------------------------------------------------------
| Sage Version 4.5.3, Release Date: 2010-09-04 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import sagess
0
sage: sagess.MyFunction(1,2)
1/2
At the import, it returns zero, but when I reask the same computation, it gives the correct 1/2
I know that writing
float(x)/y
"fixes" the problem. But it looks weird and I want to keep exact values.
What do I have to write in my scripts in order to make understand to Sage that I always want 1/2 to be 1/2 ?
Thanks
Have a good night !
Laurent
Laurent ClaessensTue, 01 Feb 2011 12:24:55 +0100https://ask.sagemath.org/question/7917/Any way to detect division by zero?https://ask.sagemath.org/question/7800/any-way-to-detect-division-by-zero/In Sage, is there any way (function call) to check whether a given expression will produce division by zero error. Is something like the following possible?
if IsDivisionByZero(expr):
return False
ShuWed, 08 Dec 2010 19:02:45 +0100https://ask.sagemath.org/question/7800/