ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 07 Mar 2019 17:25:05 -0600Factor a solve outputhttp://ask.sagemath.org/question/45716/factor-a-solve-output/ I am new to sage. So, sorry if my question is too trivial.
I can not factor the output of "solve". For instance, this very simple example
x= var('x')
sols=solve(x==6, x)
factor(sols[0].right())
gives me
6
Why I am not getting the following?
2 * 3
which of course is the output of
factor(6).
PS: I guess that the "type" is a crucial thing here, but anyways I'd need to factor 'sage.symbolic.expression.Expression'tidessonThu, 07 Mar 2019 17:25:05 -0600http://ask.sagemath.org/question/45716/Wrong factorization of expressions containing exponentials ?http://ask.sagemath.org/question/45469/wrong-factorization-of-expressions-containing-exponentials/Is there something wrong with the factorization of expressions containing exponentials ?
I obtain the following strange results (Sagemath 8.6):
`factor(exp(-x)+2*exp(x))` ---> `3*e^x`,
`factor(exp(-x)+x*exp(-x))` ---> `(x + 1)*e^x`
Any suggestions welcomed.irizosFri, 15 Feb 2019 06:40:40 -0600http://ask.sagemath.org/question/45469/Factorize characteristic polynomial in SR base ringhttp://ask.sagemath.org/question/45249/factorize-characteristic-polynomial-in-sr-base-ring/ I am total newbie to SAGE so this question might be trivial. How can I factorize the characteristic polynomial obtained by a symbolic matrix in SAGE 8.6? Is there a workaround the fact that `factor()` is not defined on the base ring `SR` which is the one inherited from the symbolic matrix?
For example I have in a SAGE/Jupyter notebook something like:
a,b,c = var('a','b','c')
M = Matrix(SR,3,3)
M[0] = [a, -b, 0]
M[1] = [c, a+b, 0]
M[2] = [0, 0, 1]
e = M.eigenvalues()
f = M.charpoly()
factor(f)
The last instruction raises a `NotImplementedError` as expected from the fact that `factor` is not defined on `SR`...
In my real problem I am computing characteristic polynomials of large (8x8) symbolic matrices and I would like to get at glance all the factors, so as to quickly isolate negative real roots and instead easily discuss conditions for existence and sign of symbolic ones.maurizioThu, 31 Jan 2019 11:26:46 -0600http://ask.sagemath.org/question/45249/LaurentPolynomial can't factor constantshttp://ask.sagemath.org/question/44764/laurentpolynomial-cant-factor-constants/Hi all,
Many `LaurentPolynomials` throw errors when I try to factor them. For example:
sage: R.<x,y> = LaurentPolynomialRing(QQ)
sage: R.one().factor()
AttributeError: ...
sage: (x^-1).factor()
AttributeError: ...
This seems to come from the unit part of `Polynomial.factor()` sometimes (incorrectly) living in `Integer Ring`, and sometimes (correctly) living in whatever `self.parent()` is.
Is this intentional or a bug?
Thanks,
Henryliu.henry.hlSun, 23 Dec 2018 15:28:33 -0600http://ask.sagemath.org/question/44764/From Pari to SAGEhttp://ask.sagemath.org/question/44657/from-pari-to-sage/
f(n,p)={d=ceil(log(2)/log(10)*(n-1));s=lift(Mod(2,p)^(n-1));t=lift(Mod(10,p)^d);u=lift(Mod((2*s-1)*t+s-1,p));u}
v=[100000..101000]
forprime(q=1,10^7,z=select(m->f(m,q)==0,v);if(length(z)>0,v=setminus(v,z);print(q," ",length(z)," ",length(v))))
This is a program for PARI. For numbers of the form (2^k-1)*10^d+2^(k-1)-1 where d is the number of decimal digits of 2^(k-1)-1 in the range k=[100000..101000], it displays numbers with no factor below 10^7. Can somebody translate this PARI program in a SAGE program?polistiroloThu, 13 Dec 2018 08:46:48 -0600http://ask.sagemath.org/question/44657/code snippet resp. simplify and factorhttp://ask.sagemath.org/question/42786/code-snippet-resp-simplify-and-factor/this doesn't work
var('c')
mat=matrix([[1,0],[0,c]])
xy0=vector([c,1])
r0=-mat*xy0
p0=r0
for k in range(2):
la=r0*p0/(p0*mat*p0)
xy1=xy0+la*p0
print xy1
if xy1[0]!=0:
xy1[0]=xy1[0].factor()
if (xy1[1])!=0:
xy1[1]=xy1[1].factor()
print xy1
r1=(r0-la*mat*p0)
p1=r1+r1*r1/(r0.dot_product(r0))*p0
p1=p1.simplify_full()
p0=p1
r0=r1
xy0=xy1
print '--------------------'
but this works
var('c')
mat=matrix([[1,0],[0,c]])
xy0=vector([c,1])
r0=-mat*xy0
p0=r0
for k in range(2):
la=r0*p0/(p0*mat*p0)
xy1=xy0+la*p0
print xy1
xy1=xy1.simplify_full() # <<<<<<<<< inserted this command
if xy1[0]!=0:
xy1[0]=xy1[0].factor()
if (xy1[1])!=0:
xy1[1]=xy1[1].factor()
print xy1
r1=(r0-la*mat*p0)
p1=r1+r1*r1/(r0.dot_product(r0))*p0
p1=p1.simplify_full()
p0=p1
r0=r1
xy0=xy1
print '--------------------'rewolfSat, 30 Jun 2018 06:41:03 -0500http://ask.sagemath.org/question/42786/Why does $\frac{2}{3} t - \frac{2}{3} y$ factors only when the variables are polynomial ring over the rational numbers?http://ask.sagemath.org/question/38549/why-does-frac23-t-frac23-y-factors-only-when-the-variables-are-polynomial-ring-over-the-rational-numbers/The expression $\frac{2}{3} t - \frac{2}{3} y$ can be factored as $\left(\frac{2}{3}\right) \cdot (y - t)$.
If I try to use sage in this way to do the factorization:
var('y t')
E = -2/3*y + 2/3 * t
E.factor()
The result is still `E`.
On the other hand, if do it like this:
R.<y,t> = PolynomialRing(QQ)
E = -2/3*t +2/3*y
E.factor()
The result is as expected.
Why does the call to `factor()` works as expected when the variable $y$ and $t$ are defined to be a polynomial ring in QQ and not work then they are symbolic expressions.ensabaWed, 16 Aug 2017 17:18:50 -0500http://ask.sagemath.org/question/38549/How to get sage to keep the same form as an expression from sympy?http://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/ I have this expression:
$$-\frac{2}{3} t_{1} + \frac{2}{3} y_{1}$$
and would like to rewrite it as follows:
$$\frac{2}{3} (-t_1 + y_1)$$
Using sympy, I can get something close:
import sympy as sp
var('y1 t1')
expr1 = -2/3*t1 + 2/3*y1
sp.factor(sp.sympify(expr1))
This gives:
𝟸*(⎯𝚝𝟷+𝚢𝟷)/𝟹
But when I convert it back to Sage:
sp.factor(sp.sympify(expr1))._sage_()
The result reverts to `expr1`.
How can I get the call to `_sage_()` not do any rewrites on the sympy expression?ensabaTue, 08 Aug 2017 18:31:19 -0500http://ask.sagemath.org/question/38492/simplify expression (square polynomial)http://ask.sagemath.org/question/37488/simplify-expression-square-polynomial/I and using sage to convert x^2 + 2*x + 1 to (x + 1)^2 by using the following:
show((y**2 + 2*y + 1).simplify_full())
but it does not do anything and returns the expression as it is. How do I make it do it ? Thanks!sage_user47Tue, 02 May 2017 13:20:46 -0500http://ask.sagemath.org/question/37488/Factoring out complex exponentialshttp://ask.sagemath.org/question/36495/factoring-out-complex-exponentials/Hi, If I have an expression as follows
$\frac{3}{8} {{E}_y^-}^{2} \overline{{E_y^+}} e^{\left(i \omega t +
3 i k x\right)} + \frac{3}{8} \, {{E}_y^-}^{2} \overline{{{E}_y^-}}
e^{\left(i \omega t + i k x\right)} + \frac{3}{4} \, {{E}_y^-}
{E_y^+} \overline{{E_y^+}} e^{\left(i \, \omega t + i \, k x\right)} +
\frac{3}{4} \, {{E}_y^-} {E_y^+} \overline{{{E}_y^-}} e^{\left(i
\omega t - i k x\right)} + \frac{3}{8} \, {E_y^+}^{2}
\overline{{E_y^+}} e^{\left(i \, \omega t - i \, k x\right)} +
\frac{3}{8} \, {E_y^+}^{2} \overline{{{E}_y^-}} e^{\left(i \omega t -
3 i k x\right)}$
How do I factor out a complex exponential $e^{i\omega t - ikx}$ from the expression above using a command?NahsiNWed, 08 Feb 2017 21:42:46 -0600http://ask.sagemath.org/question/36495/Number of factors of a polynomialhttp://ask.sagemath.org/question/34910/number-of-factors-of-a-polynomial/Given a polynomial f, the command `f.factor()` gives the factorization of f. I want to find out the number of factors of f. Is there any command for that ?
The polynomial ring is assumed to be `ZZ[x]`.nebuckandazzerFri, 23 Sep 2016 10:41:06 -0500http://ask.sagemath.org/question/34910/Factoring a polynomial over a finite Fieldhttp://ask.sagemath.org/question/25116/factoring-a-polynomial-over-a-finite-field/Hello
I'm following a 101 algebra course, and for example, I would like to factor a polynomial on a finite field like F_9
(F_9 == ZZ/9ZZ is a field because 9 is a power of a prime number, 3)
R = PolynomialRing(GF(9),'x')
x = R.gen()
f = x^4+x^2-1
f in R
f.factor()
i get an error message
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_68.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("UiA9IFBvbHlub21pYWxSaW5nKEdGKDkpLCd4JykKeCA9IFIuZ2VuKCkKZiA9IHheNCt4XjItMQpmIGluIFI="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpYgKq_W/___code___.py", line 3, in <module>
R = PolynomialRing(GF(_sage_const_9 ),'x')
File "factory.pyx", line 364, in sage.structure.factory.UniqueFactory.__call__ (build/cythonized/sage/structure/factory.c:1244)
File "/home/sage/sage-6.3/local/lib/python2.7/site-packages/sage/rings/finite_rings/constructor.py", line 414, in create_key_and_extra_args
raise ValueError("parameter 'conway' is required if no name given")
ValueError: parameter 'conway' is required if no name given
i'm running sage 6.3 notebook on windows through Oracle VM winbox.
I'm a totally new user, and i've looked at the tutorial and the forum but
couldn't find any example or reason why this would not work.
thank you for your help.
faguiThu, 04 Dec 2014 02:29:43 -0600http://ask.sagemath.org/question/25116/List of prime factors with repetitionhttp://ask.sagemath.org/question/33493/list-of-prime-factors-with-repetition/Is there a simple command on Sage wich gives, in place of factor(720)= 2^4 * 3^2 * 5, the list [2, 2, 2, 2, 3, 3, 5] of prime factors with repetition?logomathSat, 21 May 2016 09:14:39 -0500http://ask.sagemath.org/question/33493/Loop over integers mod phttp://ask.sagemath.org/question/33000/loop-over-integers-mod-p/ What is the most efficient way to factor a polynomial mod $p$ for various $p$ all at once, perhaps to be assembled into a table with more fancy code?
For example, suppose I want to factor $x^2-17 mod p$ for the first 100 primes. My first guess is to type out the proper code to factor $x^2-17$ over the ring Integers(i) where i runs through an appropriate prime_range. But Sage says I am not allowed to put a variable into Integers(). So what is the best way to do this?SupersingularityMon, 04 Apr 2016 22:59:05 -0500http://ask.sagemath.org/question/33000/sqrt function not working properlyhttp://ask.sagemath.org/question/30325/sqrt-function-not-working-properly/ I have the following code where I want to substitute `a, b, c` into `s`. Since `s` factors as a square, I want to get the square root of it :
p, t= var('p t')
a=(-2*p*t^2-p^2*t)+(2*t*p-p^2)+t+1
b=(p*t^2+2*p^2*t)+(2*t*p-t^2)-p+1
c=(p*t^2-p^2*t)+(t^2+2*t*p+p^2)+t-p #3 sides (a,b,c) in terms of theta and phi [equation (1.1)]
s=(factor(2*c^2+2*a^2-b^2));s
S=s.sqrt();S
Unfortunately the answer I get is
sqrt((3*p*t^2 - 2*p^2 - 2*p*t + t^2 + p + 2*t + 1)^2)
The `sqrt` and the square power does not cancel off which I want it to cancel. I tried using the code `S.simplify_full()` to simplify it hoping the sqrt and square power will cancel off but no luck. Is there any other specific code I can use for that.
ShaWed, 28 Oct 2015 18:45:04 -0500http://ask.sagemath.org/question/30325/Define a factor of an equationhttp://ask.sagemath.org/question/30232/define-a-factor-of-an-equation/ I have the following code :
p, t= var('p t')
a=(-2*p*t^2-p^2*t)+(2*t*p-p^2)+t+1
b=(p*t^2+2*p^2*t)+(2*t*p-t^2)-p+1
c=(p*t^2-p^2*t)+(t^2+2*t*p+p^2)+t-p #3 sides (a,b,c) in terms of theta and phi [equation (1.1)]
s=factor(2*c^2+2*a^2-b^2);s
which yields
(3*p*t^2 - 2*p^2 - 2*p*t + t^2 + p + 2*t + 1)^2
I want to use the factor `(3*p*t^2 - 2*p^2 - 2*p*t + t^2 + p + 2*t + 1)` in my next command. How should I define that. Like I want to set `m = 3*p*t^2 - 2*p^2 - 2*p*t + t^2 + p + 2*t + 1`. Tried using `operand()` but did not help muchShaThu, 22 Oct 2015 20:50:36 -0500http://ask.sagemath.org/question/30232/Excluding a common factor in a sumhttp://ask.sagemath.org/question/26628/excluding-a-common-factor-in-a-sum/I'm trying to figure out how to simplify this expression in Sage:
y = tau1*exp(-t/tau1)/(tau1 - tau2) - tau1*exp(-t/tau2)/(tau1-tau2)
When I try *factor(y)* or *simplify(y)*, I get this:
-tau1*(e^(t/tau1) - e^(t/tau2))*e^(-t/tau1 - t/tau2)/(tau1 - tau2)
However, the result I would like to see is this:
tau1/(tau1 - tau2)*(exp(-t/tau1) - exp(-t/tau2))
which seems like the simplest form to me. I've not been able to get this result with the various simplify commands or with assumptions (BTW, all variables are real, and tau1 & tau2 > 0).
In this case the simplest form is obvious, of course, but it would be a great help to be able to get this right for larger expressions.
drs663Thu, 23 Apr 2015 04:29:25 -0500http://ask.sagemath.org/question/26628/List common factors of integershttp://ask.sagemath.org/question/10794/list-common-factors-of-integers/I have an `N = pq`, how do I list all common factors of
- 153593241046674892978867376676801703195333499261944069748317
- 595581987651106688365284842778515858399666547859870373300567
- 456451496401875224148239517183071489325356803521953515331953
- 732521324063413291774595255009269986704084399047286433357607
- 697998237255232517803133139640937207091669333334886072165381
- 665759389457622825753076124570026166878147870317677657070179
- 1259985415634532155096901933248348760395037298806289273584121
- 476063424836313254692044012215359579492944897779738635204933
- 176294427788887166758409622538881387638478405478915857712513
- 62438334806032841411208819703508997562716700552371135112697
- 1155831644188436440125346091174944695123678746779608256372229
- 592339248856319601455928821705423109007342115448431777433343
Also, how do I implement this in Sage?
I've read something about the `collect_common_factors()` but I'm not sure how to use it.iGrasmatMon, 02 Dec 2013 10:59:30 -0600http://ask.sagemath.org/question/10794/exact factortinghttp://ask.sagemath.org/question/10638/exact-factorting/How do you get sage to factor into exact values. For instance, I want it to factor x^2-2 and return (x-sqrt(2))*(x+sqrt(2))
However, when I input
realpoly.<x> = PolynomialRing(CC)
factor(x^2-2,x)
Sage returns
(x - 1.41421356237310) * (x + 1.41421356237310)
Any ideas? ClemFanJC07Sun, 20 Oct 2013 12:43:06 -0500http://ask.sagemath.org/question/10638/factor with a boundhttp://ask.sagemath.org/question/10273/factor-with-a-bound/Hi,
I need to find relatively small prime factors of some big numbers. How do I make factor command working with a bound? For instance let's say I am just interested in prime divisors less than 20 and I want to factor 11891. The answer I want would be 11*1081.
AlperenMon, 24 Jun 2013 20:29:04 -0500http://ask.sagemath.org/question/10273/Polynomial as a sum of simply factored expressions?http://ask.sagemath.org/question/9761/polynomial-as-a-sum-of-simply-factored-expressions/The polynomial
p^9 + p^8 + 7*p^6 + 6*p^4 + 3*p^3 + 4*p^2 + 2
can't be factored (over the rationals). However, it can be expressed in simpler form as
(p^3+1)^3 + (p^2+1)^4
Is there any way (other than trial and error) of finding such a sum, for a given (multivariate) polynomial?AlasdairMon, 18 Feb 2013 21:56:58 -0600http://ask.sagemath.org/question/9761/python/sage scriptshttp://ask.sagemath.org/question/9407/pythonsage-scripts/First of all, my apologies for being a noob and a windows user. I'm using sage in virtualbox. I'm at this part of the tutorial: sagenb.org/doc/live/tutorial/programming.html#standalone-python-sage-scripts
I'm having 2 problems:
(1) I can't execute "./factor 2006". It says "no such file or directory", but the file is definitely there, and "sage -python factor 2006" works.
(2) factoring polynomials doesn't seem to work for me in sage. If I call factor on a string I get a TypeError: "unable to factor n".
**Edit**: Part of the problem may have been that the script was in a shared folder in VirtualBox. Here is some exact input/output, working completely locally (this changes the error in (2)).
`>./factor 2006`
`/usr/bin/env: sage -python: No such file or directory`
`>sage -python factor "32*x^5-1"`
`NameError: name 'x' is not defined`
Any assistance is much appreciated!rmp251Tue, 09 Oct 2012 09:52:29 -0500http://ask.sagemath.org/question/9407/how to factorize an expression with constant variables ?http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/Hello,
I would like to factorize an expression with sage that contains constant variables (i.e. parameters), but I cannot figure out how to do that.
Here is an example : `x`, `y` are variables and `A` is a parameter
var('A x y')
f = A*x + x + A^2*exp(y) + y
print (f.factor())
Sage returns `A^2*e^y + A*x + x + y`.
Instead, I would like that sage returns `(A+1)*x + (A^2+1)*exp(y)`.
How to do that ?
Thank you
mazkimeMon, 03 Sep 2012 01:35:03 -0500http://ask.sagemath.org/question/9291/Why does @parallel change my outputs?http://ask.sagemath.org/question/8269/why-does-parallel-change-my-outputs/Can someone explain this behavior to me:
sage: @parallel
....: def foo(n):
....: return str(factor(n))
....:
sage: foo(10)
'2 * 5'
sage: for x in foo([1..10]):
....: print x[0][0][0]
....:
1
2
3
4
5
6
7
8
9
10
I expected to get the factorizations of 1 .. 10, not the numbers 1 .. 10. It seems like the output of `foo()` is getting evaluated somehow when it goes through the @parallel decorator. Is this a bug?
benjaminfjonesFri, 12 Aug 2011 05:46:40 -0500http://ask.sagemath.org/question/8269/