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.Thu, 01 Jul 2021 08:12:48 +0200why can't I compute the zeros of an integer polynomial using solve()?https://ask.sagemath.org/question/57833/why-cant-i-compute-the-zeros-of-an-integer-polynomial-using-solve/ This works fine, producing complex roots:
x=var('x')
f=x+x**2+x**3-100
z=solve(f==0,x)
this also works fine:
R.<x>=ZZ[]
f=x+x**2+x**3-100
f.roots()
but this seems to run forever without error message and I don't understand why:
x=var('x',domain=ZZ)
f=x+x**2+x**3-100
z=solve(f==0,x)
can anyone explain or tell me where to start reading to understand this behaviour?
dantetanteThu, 01 Jul 2021 08:12:48 +0200https://ask.sagemath.org/question/57833/how to bold and embiggen variable using latex_namehttps://ask.sagemath.org/question/57515/how-to-bold-and-embiggen-variable-using-latex_name/ I am attempting to recreate variables and expressions from an academic paper.
I would like to be able to calculate with the variables/expressions and to typeset
them using Latex.
Here is what is working in Command-Line, CoCalc, jupyter notebook but not SageMath Cell (which
just show a blank after evaluation):
P = var('P', latex_name='\mathscr{P}'); view(P)
An uppercase cursive/script P is shown.
If I try to em-bold it, like so:
P = var('P', latex_name='\bold\mathscr{P}'); view(P)
Then:
1) Command line SageMath produces about 200 lines of error message beginning with:
An error occurred.
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=pdflatex 2021.6.3) 9 JUN 2021 13:50
...
2) SageMath cell server -- using doubled '\\'s -- again, produces blanks after evaluation
3) CoCalc -- again, using doubled '\\'s -- outputs "\bold" followed by a properly typeset cursive-script uppercase P
4) jupyter notebook produces 200+ error message lines beginning with:
An error occurred.
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=pdflatex 2021.6.3) 9 JUN 2021 13:56
So...it may well be that using the MacOS 9.3.rc4 has issues, or that I have failed to properly install MacTex
but CoCalc should work -- if only I input the correct argument.
I apologize if this is/seems like a RTFM situation.
enthorWed, 09 Jun 2021 23:00:51 +0200https://ask.sagemath.org/question/57515/interact variable not foundhttps://ask.sagemath.org/question/54668/interact-variable-not-found/ I keep getting ValueError: Variable 'h' not found when I try to plot an interact:
f = (x-h)^2 - 4*a*(y-k)
Parabola = solve(f,y)[0].rhs()
print(Parabola)
@interact
def _(h=slider(-5,5,default=1), a=slider(-5,5,default=1), k=slider(-5,5,default=1)):
show(plot(Parabola,x,-5,5))
It even creates the sliders but the plot can't see h. What am I doing wrong?
print(Parabola)![image description](/upfiles/1607889762688566.png)
cybervigilanteSun, 13 Dec 2020 21:05:35 +0100https://ask.sagemath.org/question/54668/implicitly defining a sequence of variableshttps://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/To define a general polynomial in Maple one writes
p := sum(a[i]*x^i,i=0..n);
and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.
So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.
Trying to accomplish this in sage I reached
sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:
sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
In Maple they all give good results.
Am I going at this the right way? Is there a way to implicitly define variables as in Maple?
parzanWed, 22 Jun 2011 06:48:29 +0200https://ask.sagemath.org/question/8181/Is there any difference between the two different ways of declaring variables?https://ask.sagemath.org/question/54003/is-there-any-difference-between-the-two-different-ways-of-declaring-variables/ I sometimes see variables declared as `y,z,t = var("y z t")` and others simply as `var("y z t")`
Both seem to work. Is there any actual difference between them?cybervigilanteThu, 22 Oct 2020 04:11:30 +0200https://ask.sagemath.org/question/54003/Symbolic solvehttps://ask.sagemath.org/question/8707/symbolic-solve/Following the [change of variable thread](http://ask.sagemath.org/question/1113),
I wanted to streamline the whole process.
Namely, using the same example in the above thread, I'd like to say
integral_def_change(x*cos(x^2+1), (x, 0, 2*pi), u == x^2 + 1, u)
The difference is, I wanted also Sage to automatically solve for `x`
instead of providing `x = sqrt(u - 1)`, say. But when I tried
solve(u == x^2 + 1, x)[0].rhs()
the output was `r1`.
1- What exactly is r1 ??
A way out (see this [thread](http://ask.sagemath.org/question/1105))
seems to make of the solution a function of `u`
f(u) = solve(u == x^2 + 1, x)[0].rhs()
Now f is
u |--> -sqrt(u - 1)
2- What can I do to get `+sqrt(u - 1)` instead? Is this related to the
[positive function question there](http://ask.sagemath.org/question/989)?Green diodSat, 11 Feb 2012 11:56:41 +0100https://ask.sagemath.org/question/8707/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 10:46:56 +0200https://ask.sagemath.org/question/52594/substitution of variable in the result of monomials()https://ask.sagemath.org/question/49089/substitution-of-variable-in-the-result-of-monomials/Given a polynomial in n variables, I'd like to extract the list of its monomials, and then manipulate that list by substituting certain variables for others.
Simple example: in $Z[x,y]$ consider the polynomial $1+x+y^2$; the list of its monomials (in some ordering) is $(1,x,y^2)$. My function should be able e.g. to take that list and substitute $y$ with $x$, namely return $[1,x,x^2]$.
At the moment, my code gives error, but I do not understand how to fix it.
N.<x1,x2> = PolynomialRing(ZZ, 2)
f = 1+x1+x2
g = f.monomials()
for i in range(3):
g[i] = g[i].substitute_expression(x2==x1)
Namely, how do I make a variable in the polynomial ring also have the substitute attribute? or is there a better way to achieve this?rue82Thu, 19 Dec 2019 20:18:18 +0100https://ask.sagemath.org/question/49089/Batch file and Sagehttps://ask.sagemath.org/question/47997/batch-file-and-sage/Hi,
I am using SLURM (https://slurm.schedmd.com/) to run a batch process on a cluster of computers. Here is the following .slrm file that I have:
#!/bin/bash
#SBATCH --time=1:59:00
#SBATCH --ntasks=1
#SBATCH --mem=3820
#SBATCH -O
#SBATCH --output=/data/scratch/kbari/slrmbt_%A_%a.out
#SBATCH --array=1-50
sage --nodotsage /data/scratch/kbari/slurmbatchtest.py $SLURM_ARRAY_TASK_ID
I get an error that: "Error: HOME directory '/u/grad/kbari' does not exist.
Error setting environment variables by sourcing '/usr/local/SageMath/src/bin/sage-env';
possibly contact sage-devel (see http://groups.google.com/group/sage-devel)."
I understand the error to be that it is trying to access sage from a different directory, which it does not have access to and cannot find as a result. So how can I change the environment variable to just tell it to run Sage not from that directory?
Thanks for your time!BarkFri, 20 Sep 2019 22:25:00 +0200https://ask.sagemath.org/question/47997/Variable matriceshttps://ask.sagemath.org/question/39687/variable-matrices/If I type
sage: M = matrix(SR, 2, var('a,b,c,d'))
sage: show(~M)
it gives me the inverse of the matrix M with entries in terms of `a`, `b`, `c`, `d`.
Now what I want is to set four variable matrices `A`, `B`, `C`, `D`, each of size 2x2,
and then I want to create the block matrix
sage: N = block_matrix([[A, B], [C, D]])
Finally I want some functions of `N` in terms of `A`, `B`, `C`, `D`.
For example, how can I get `det(N)` in terms of `A`, `B`, `C`, `D`?Deepak SarmaTue, 21 Nov 2017 06:01:49 +0100https://ask.sagemath.org/question/39687/generate a list of variables with two indiceshttps://ask.sagemath.org/question/46214/generate-a-list-of-variables-with-two-indices/Hi
I would like to make a list of variables with two consecutive indices, example rr_10, rr_21 .. etc
I tried :
n=3
rrL=list(var('rr_%d%d' %i %(i-1)) for i in [1..n])
without success:**not enough arguments for format string**
what is the missing arguments here ? ortolljTue, 16 Apr 2019 09:25:03 +0200https://ask.sagemath.org/question/46214/question about Python assignment of lists; meaning of equals sign; how to save a list?https://ask.sagemath.org/question/44324/question-about-python-assignment-of-lists-meaning-of-equals-sign-how-to-save-a-list/I have a list, then modify it, but want to save the old list to then modify in a different way.
I try to save the original using a different name and =, but it seems the two names are forever linked by the equals sign,
so the modification also changes the original. Why does this happen, and how do I deal with this?
Example:
sage: L1 = [1, 2]
sage: L2 = [3, 4]
sage: L3 = L1
sage: L1
[1, 2]
sage: L2
[3, 4]
sage: L3
[1, 2]
sage: L1.extend(L2)
sage: L1
[1, 2, 3, 4]
sage: L2
[3, 4]
sage: L3
[1, 2, 3, 4]
The same thing happens with "append".
Help please!!!
Note: this does not happen with variables with numerical (not list) values. For example:
sage: a = 4
sage: b = a
sage: a = 5
sage: a
5
sage: b
4
So it seems that the equals sign means two different things: for numbers it is an assignment;
for lists it is an identification. This is driving me crazy. I cannot find it explained anywhere (maybe because it is so "well-known"?)alSun, 18 Nov 2018 15:59:29 +0100https://ask.sagemath.org/question/44324/n-th power of matriceshttps://ask.sagemath.org/question/35155/n-th-power-of-matrices/Is there any way to calculate the n-th power of a (upper unitriangular) matrix in Sage? Here n is an integer variable.
For example, if y=matrix([[1,0,0],[0,1,1],[0,0,1]]), then I want to obtain a formula in n for y^n. In this case this would be y^n=matrix([[1,0,0],[0,1,n],[0,0,1]]).
I tried the following:
sage: y=matrix([[1,0,0],[0,1,1],[0,0,1]]); var('n');
sage: y^n
This resulted in the error: "NotImplementedError: non-integral exponents not supported"
Adding
sage: assume(n, 'integer')
has no effect at all.irisSun, 16 Oct 2016 10:56:39 +0200https://ask.sagemath.org/question/35155/Printing variable namehttps://ask.sagemath.org/question/40862/printing-variable-name/ How would I return a variable name in a function. E.g. If I have the function:
def mul(a,b):
return a*b
a = mul(1,2); a
b = mul(1,3); b
c = mul(1,4); c
This would return:
2
3
4
I would like it to return:
a = 2
b = 3
c = 4
How would I do this?pytonnoobMon, 29 Jan 2018 19:05:32 +0100https://ask.sagemath.org/question/40862/Numerical Integral with variable paramshttps://ask.sagemath.org/question/40618/numerical-integral-with-variable-params/ I am trying to write some code to plot a function of an integral whose integrand depends on two variables, much like the following:
a = var('a')
b = var('b')
plot3d(numerical_integral((x+a+b), 0, 1, params=[a,b])[0],(a,0,1),(b,0,1))
This code raises an error `NotImplementedError: free variable: b`. Which would be the correct way to implement this code?jepstraMon, 15 Jan 2018 16:24:26 +0100https://ask.sagemath.org/question/40618/a problem with variables in real domainshttps://ask.sagemath.org/question/37766/a-problem-with-variables-in-real-domains/Hi,
I have a problem with setting the domains of definition of variables.
For example, typing
var("y") ; assume(y, "real")
conjugate(y + I)
I get the result `y + I`. The same with `var("y", domain="real")`.
Where I'm wrong?
thanks!danieleFri, 02 Jun 2017 19:12:44 +0200https://ask.sagemath.org/question/37766/Why is Sage calling 1 a variable?https://ask.sagemath.org/question/37660/why-is-sage-calling-1-a-variable/Consider
x = var('x')
f = 1/(x-3)
limit(f,x=3,dir='below')
UnboundLocalError: local variable 'l' referenced before assignmentcybervigilanteMon, 22 May 2017 07:24:03 +0200https://ask.sagemath.org/question/37660/Variable overrites function defined previouslyhttps://ask.sagemath.org/question/36108/variable-overrites-function-defined-previously/I have u(t) which is an function of t, and g(u) which is a function of u. I want to find the derivative of g(u)*u(t) w.r.t. t.
sage: u(t) = function('u')(t)
sage: h(u) = function('h')(u)
sage: u
u
sage: h
u |--> h(u)
diff(h(u)*u(t),t)
h(u)
This seems to happen because the function u is overwritten when I use u to define the function h. I can get around it this way.
sage: u(t) = function('u')(t)
sage: h(x) = function('h')(x)
sage: u
t |--> u(t)
sage: h
x |--> h(x)
sage: diff(h(u)*u(t),t)
u(t)*D[0](h)(u(t))*D[0](u)(t) + h(u(t))*D[0](u)(t)
Can this be classified as a bug?
If it is, would it be possible to fix it?
Thanks in advance.omoplataTue, 27 Dec 2016 23:58:06 +0100https://ask.sagemath.org/question/36108/assign variable valuehttps://ask.sagemath.org/question/35110/assign-variable-value/ Hi!
I'm just getting started with Sage and therefore my question may sound really stupid (and nevertheless I couldn't find an answer here).
I have a matrix with both symbolic entries and numerical values, let's say:
a,b=var('a,b')
M=matrix([[1,a], [0,b]])
Now I want do assign a value to a:
a=0
However if I print M to the screen it still says a in the matrix and not 0.
You can clearly see that I'm really not used to Sage, so what's the problem here?
What should I do if I want to assign a fixed value to a variable which is also used in the calculations where this variable appears?
Thank's for the answers, I know this will sound highly trivial for you!
Philippphilipp7Mon, 10 Oct 2016 20:28:44 +0200https://ask.sagemath.org/question/35110/Getting symbolic variable without resetting python variable.https://ask.sagemath.org/question/32949/getting-symbolic-variable-without-resetting-python-variable/When calling
var('x')
the variable x gets set to the symbolic expression x just as if we had called
x = var('x')
In particular, any value stored in x is deleted. Is there a way to get a symbolic variable without setting the python variable to be that symbolic variable? I'm not super familiar with how sage handles symbolic variables so this might be unreasonable.rlg23Fri, 01 Apr 2016 23:11:17 +0200https://ask.sagemath.org/question/32949/Variables in a polynomialhttps://ask.sagemath.org/question/32493/variables-in-a-polynomial/ I edited my question and put the answer I found, so this solves my problem!
My problem was: take a field " E " generated by " r " (here, "r" is a root of X^3 - 2 ) and 3 variables " b0 , b1 , b2 ".
I want to produce the element " A = b0 + r * b1 + r^2 * b2 " and it's square:
A^2 = b0^2 + (2*r) * b0 * b1 + (r^2) * b1^2 + (2*r^2) * b0 * b2 + 4 * b1 * b2 + (2*r) * b2^2
and express A and A^2 in terms of the basis "( 1 , r , r^2 )" so that the result becomes:
A^2 = r^2 ( b1^2 + 2 b0 * b2 ) + r ( 2 b0 * b1 + 2 b2^2 ) + ( b0^2 + b2^2 + b1 * b2 )
The solution was simply to define the number field as an extension of the ring of coefficients and the other way round! Good!
B=PolynomialRing(E,3,'b');
v=B.gens()
E.<r>=B.extension(x^3-2)
A^2=sum( [v[i] * r^i for i in range(3) ] )
gives me the solution:
(b1^2 + 2*b0*b2)*r^2 + (2*b0*b1 + 2*b2^2)*r + b0^2 + 4*b1*b2
result expressed in terms of powers of "r".
ndMon, 08 Feb 2016 13:48:10 +0100https://ask.sagemath.org/question/32493/Please Append 'solve' Help Documentationhttps://ask.sagemath.org/question/32387/please-append-solve-help-documentation/ Hello,
I have been using SAGE for about 2 years, and only now have I finally figured out how to get the 'solve()' function to print out its solutions in plain form (so I can save the answers as variables, which is EXTREMELY important for me).
In order to do this: I had to sift through the help pages for 'solve', Programming -- Data Types (for both Lists and Dictionaries), and Python's official documentation on Lists and Dictionaries; until I finally figured out that you need to use the code:
print( 'listname' [ ' dict # ' ] [ 'var' ])
Now this may seem very elementary to those whom are fluent in Python, but for general users (like me): there is absolutely no clear indication on how to call one or several elements of several Dictionaries inside of a List.
PLEASE append your documentation so that people can actually use the 'solve' function without having to copy and paste the answers because it isn't shown how to properly use the output when 'solution_dict=true'.
(Note: if you're thinking about the 'for' loop example in the help documentation: it's completely useless for someone who needs to select one or more of those solutions & save them as variables).
Thank you.BLU7211Sat, 30 Jan 2016 21:46:43 +0100https://ask.sagemath.org/question/32387/Declare variable as function outputhttps://ask.sagemath.org/question/31886/declare-variable-as-function-output/ hello guys,
i wrote a new function, lets call it "func", that has some matrix as input and also some matrix as output.
the thing is, i want to declare the last output of the function as a new variable x ( x=func(A) for a given A) the problem is: if i say
x = func(A)
print x
i get :
none
I guess the problem is, that i calculate more than one thing while the function is running, but at the end i just want to able to use that one single output of the function for further use...bruno171092Mon, 28 Dec 2015 15:32:16 +0100https://ask.sagemath.org/question/31886/How to do symbolic computation with quaternionshttps://ask.sagemath.org/question/9730/how-to-do-symbolic-computation-with-quaternions/I need to do symbolic computations with quaternions. Ideally I'd like to enter something like
Q.<i,j,k> = QuaternionAlgebra(QQ,-1,-1)
a=var('a')
b=var('b')
c=var('c')
q = a*i+b*j+c*k
But it doesn't work : TypeError: unsupported operand parent(s) for '*': 'Symbolic Ring' and 'Quaternion Algebra (-1, -1) with base ring Rational Field'
Looks like SAGE doesn't know how to multiply a variable and a quaternion. Or should I specify the types of the variables ? How ?
Thanks for your help.
Gilles FalquetTue, 22 Jan 2013 03:37:05 +0100https://ask.sagemath.org/question/9730/Assigning variables in a listhttps://ask.sagemath.org/question/27056/assigning-variables-in-a-list/I have a variables list which I use to construct a system of linear equations then I use sage to solve this system. Before I use the solve command I like to equate some of the variables in the list. For example if V is the following list `var('x,y,z,w,a,b,c,d')
V2=[x,y,z,w,a,b,c,d]`
I would like to assign`V[i]=V[7-i] for i in [0..3]`. When I do this I get invalid syntax error. I know that I could easily set x=d, y=c and so on but this is not feasible when the list has too many items in it. Thank you for your help!cihanMon, 08 Jun 2015 21:52:51 +0200https://ask.sagemath.org/question/27056/Protect built-in variables?https://ask.sagemath.org/question/27020/protect-built-in-variables/ Is there a way to protect built-in variables? This means that I get a warning if I use code like this:
pi = 5
var('I')OderynThu, 04 Jun 2015 05:26:27 +0200https://ask.sagemath.org/question/27020/Is there some import I need to do for defining variables?https://ask.sagemath.org/question/26697/is-there-some-import-i-need-to-do-for-defining-variables/I have been running Sage 6.6 through VirtualBox and whenever I define a variable I get an error, like for example (for `a,b,c = var('a,b,c')`):
File "<stdin>", line 1, in <module>
File "_sage_input_25.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("YSxiLGMgPSB2YXIo4oCZYSxiLGPigJkpCnFlID0gKGEqeF4yICsgYip4ICsgYyA9PSAwKQpwcmludCBzb2x2ZShxZSwgeCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpAUEAbf/___code___.py", line 3
a,b,c = var(’a,b,c’)
^
SyntaxError: invalid syntaxFusion809Fri, 01 May 2015 09:50:24 +0200https://ask.sagemath.org/question/26697/Handle variable namehttps://ask.sagemath.org/question/26382/handle-variable-name/Dear all,
I would like to pass a function as a parameter and also one of its variable. The simplest situation would just to evaluate a function f.
def test(f,myvar,myval):
return f(myvar=myval)
f = x^2
print(test(f,x,3)
It returns `x^2`, but I would like `9`. Of course I don't know a priori what would be the name of the variable and f may have several variables.
The long question is that I would like to define something that `plot(f,(x,xmin,xmax))`. Is there's a standard way to handle this triple `(x,xmin,xmax)`? Is there's a web page for the source code for such functions to study the standard implementation?
Thanks a lot!
Arnaud
Arnaud1418Mon, 30 Mar 2015 15:41:21 +0200https://ask.sagemath.org/question/26382/Substitute variable in expressionhttps://ask.sagemath.org/question/25119/substitute-variable-in-expression/ Hello, I have to do a substitution in an expression: i want to substitute diff(psi) with omega, where i declared psi as psi =
function('psi',t)
I tried doing expr.substitute(diff(psi)==omega) but it doesn't work.
What do I have to do?
Thank you very much.SilviaThu, 04 Dec 2014 14:46:29 +0100https://ask.sagemath.org/question/25119/Declaring variable to be in a particular field/ring/grouphttps://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/Is it possible to have Sage symbolically simplify expressions involving variables subject to the assumption that the variables take values in a defined domain (field/ring/group/etc)?
The closest I've gotten is to declare a dummy polynomial ring over my domain of interest so that its variable has some notion of the domain, e.g.:
<pre>Z3=Integers(3)
Dummy.<x> = PolynomialRing(Z3)
3*x</pre>
evaluates to "0" as I'd expect, but sage fails to simplify "x^3" to "x", which ISTM should be doable if it really understood that x is a variable in Z/3Z.
Related things I've found in my searches that haven't panned out:<br/>
1. var('x', domain=*foo*) -- apparently *foo* can only be one of real/complex/positive (where I'd like to be able to say 'Z3' in the example above)<br/>
2. assume('x is Z3') - doesn't seem to have any effect.a1846583Fri, 28 Nov 2014 17:44:44 +0100https://ask.sagemath.org/question/25037/