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.Mon, 29 Jan 2018 12:05:32 -0600n-th power of matriceshttp://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 03:56:39 -0500http://ask.sagemath.org/question/35155/Printing variable namehttp://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 12:05:32 -0600http://ask.sagemath.org/question/40862/Numerical Integral with variable paramshttp://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 09:24:26 -0600http://ask.sagemath.org/question/40618/Change home directoryhttp://ask.sagemath.org/question/40066/change-home-directory/ Hi All,
I installed SageMath 8.0 in a Windows native system (no VirtualBox).
When I start Sage with the SageMath 8.0 Notebook Icon it always start in C:\Users\daniel_000.
I want it to always start at D:\Users\Sage (notice it is a different directory and a different drive).
I defined environment variables DOT_SAGE=D:\Users\Sage and DOT_SAGENB=D:\Users\Sage
to no avail, still starts at C:\Users\daniel_000.
I added to the properties of the Icon -o 'DOT_SAGE=D:\Users\Sage' -o 'DOT_SAGENB=D:\Users\Sage
to no avail, still starts at C:\Users\daniel_000.
What is the proper way to do it?
Thanks,
Daniel
danielvolinskiSun, 10 Dec 2017 09:18:53 -0600http://ask.sagemath.org/question/40066/Variable matriceshttp://ask.sagemath.org/question/39687/variable-matrices/ If I type M = matrix(SR, 2, var('a,b,c,d')); 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 N=block_matrix([[A, B], [C, D]]). Finally I want some functions of N in terms of A,B,C,D (say det(N)).Deepak SarmaMon, 20 Nov 2017 23:01:49 -0600http://ask.sagemath.org/question/39687/a problem with variables in real domainshttp://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 12:12:44 -0500http://ask.sagemath.org/question/37766/Why is Sage calling 1 a variable?http://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 00:24:03 -0500http://ask.sagemath.org/question/37660/Symbolic solvehttp://ask.sagemath.org/question/8707/symbolic-solve/Following the [change of variable thread](http://ask.sagemath.org/question/1113/change-of-variable-in-an-integration), 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/solve-and-calculate)) 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/is-it-possible-to-define-or-assume-a-general)?Green diodSat, 11 Feb 2012 04:56:41 -0600http://ask.sagemath.org/question/8707/Variable overrites function defined previouslyhttp://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 16:58:06 -0600http://ask.sagemath.org/question/36108/assign variable valuehttp://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 13:28:44 -0500http://ask.sagemath.org/question/35110/Getting symbolic variable without resetting python variable.http://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 16:11:17 -0500http://ask.sagemath.org/question/32949/Variables in a polynomialhttp://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 06:48:10 -0600http://ask.sagemath.org/question/32493/Please Append 'solve' Help Documentationhttp://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 14:46:43 -0600http://ask.sagemath.org/question/32387/Declare variable as function outputhttp://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 08:32:16 -0600http://ask.sagemath.org/question/31886/How to do symbolic computation with quaternionshttp://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 FalquetMon, 21 Jan 2013 20:37:05 -0600http://ask.sagemath.org/question/9730/Assigning variables in a listhttp://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 14:52:51 -0500http://ask.sagemath.org/question/27056/Protect built-in variables?http://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')OderynWed, 03 Jun 2015 22:26:27 -0500http://ask.sagemath.org/question/27020/Is there some import I need to do for defining variables?http://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 02:50:24 -0500http://ask.sagemath.org/question/26697/Handle variable namehttp://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 08:41:21 -0500http://ask.sagemath.org/question/26382/Substitute variable in expressionhttp://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 07:46:29 -0600http://ask.sagemath.org/question/25119/Declaring variable to be in a particular field/ring/grouphttp://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 10:44:44 -0600http://ask.sagemath.org/question/25037/Plotting an integral with a variable as a limithttp://ask.sagemath.org/question/8820/plotting-an-integral-with-a-variable-as-a-limit/I want to plot a function with a variable as a limit, e.g. look at \int_0^x f(y) d y, but this seems to throw an error when Sage can't analytically integrate the function.
x,y=var('x y')
f(y)=integrate(x^x,x,1,y)
plot(f,2,10)
Returns
Traceback (click to the left of this block for traceback)
...
ValueError: free variable: x
Can anybody help with this, please? Many thanks.tom12519Wed, 28 Mar 2012 10:52:02 -0500http://ask.sagemath.org/question/8820/Symbolic variables in loopshttp://ask.sagemath.org/question/23776/symbolic-variables-in-loops/Hi,
I got a list of symbolic variables defined like this:
P = list(var('P_%d' % i) for i in range(2*n + 1))
But when I try to manipulate these Variables in a for-loop like:
for k in range(2,n + 2,2):
print(P_k)
I'll get an error "NameError: name 'P_k' is not defined"
I'm guessing there is no k in P_k because its one entity 'P_k' :-D
But I hope it's possible to fix this by using "%" like in the definition above.
Anyone knows how to?
ThanksToreroThu, 14 Aug 2014 06:43:42 -0500http://ask.sagemath.org/question/23776/Replace a variable with a functionhttp://ask.sagemath.org/question/23340/replace-a-variable-with-a-function/ Hello i hope anyone can help me with the following problem.
i have the following code
T_w,y,T_m,k_f,rho_f,U_0,h_mstar,R, alpha, delta, Phi = var('T_w y T_m k_f rho_f U_0 h_mstar R alpha delta Phi')
T=T_w+y*(-2*(T_w-T_m)/delta+rho_f*U_0*h_mstar*cos(Phi)/k_f)+y^2*((T_w-T_m)/delta^2-rho_f*U_0*h_mstar*cos(Phi)/(k_f*delta))
u=-6*U_0*R*y*(y-delta)*sin(Phi)/delta^3
integralTu=integrate(T*u,y,0,delta)
now i would like to differentiate integralTu with respect to x (diff(integralTu,x)), but delta must be a function of x (i first defined it as a variable because of the integration).tetraederThu, 10 Jul 2014 07:42:14 -0500http://ask.sagemath.org/question/23340/Probability density function - multivariate random variablehttp://ask.sagemath.org/question/10872/probability-density-function-multivariate-random-variable/Hi experts!
I know that many of you are professional mathematicians. My question is about statcistics and sage:
given two independient random variables `X` and `Y` (with a probability density function `fX` and `fY`), and the multivariable random variable `A` defined by: `A=h(X,Y)`,
How can I obtain the explicit equation of probability density function of random varible `A`?
I only know that the join probability density fuction `fXY` is `fXY=fX*fY` (because there are independent).
Like you can see in the article
http://en.wikipedia.org/wiki/Probability_density_function
section 'Multiple variables' we can write the pdf of `A` using Dirac delta function.
Waiting for your answers.
Thans a lot!!mresimulatorWed, 01 Jan 2014 07:11:34 -0600http://ask.sagemath.org/question/10872/Why Sage cannot pass a value of variable from one function to another nested function?http://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/The first i ran this:
sage: f(x)=(2/n)*(sin(n*x)*(-1)^(n+1))
sage: sum(f, n, 1, 2) #using summation function
-sin(2*x) + 2*sin(x)
So, In this case the result was evaluated correctly.
But if i tried to combine the first line and the second line together:
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f(x,2)
-2*sum((-1)^n*sin(n*x)/n, n, 1, 2)
The result wasn't finished!
Why sage cannot evaluate mathematical expression in this case?
Another tried to prove that Sage can pass its variable from left function to right function even though the right function was a nested function:
sage: f(x) = sin(arcsin(x))
sage: f(0.5)
0.500000000000000
Edit:
(See [the same question on SO](http://stackoverflow.com/questions/19711247/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-fun).)
terces907Thu, 31 Oct 2013 07:09:56 -0500http://ask.sagemath.org/question/10677/Substitution of a list of variableshttp://ask.sagemath.org/question/10606/substitution-of-a-list-of-variables/I wish substituting a list of variables, without having to substitute them 1 by 1.
Here is an example with an attempt which doesn't work :
sage: V=[var('x_%d' % i) for i in range(10)]; V
[x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8, x_9]
sage: S=[sum(V),prod(V)]; S
[x_0 + x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9,
x_0*x_1*x_2*x_3*x_4*x_5*x_6*x_7*x_8*x_9]
sage: for i in range(10):
....: S.subs(V[i]=i)
....:
File "<ipython-input-99-7670392e3358>", line 2
SyntaxError: keyword can't be an expression
How can we do this ?
Sébastien PalcouxSun, 13 Oct 2013 12:44:43 -0500http://ask.sagemath.org/question/10606/Eliminating variables from a system of equations?http://ask.sagemath.org/question/9582/eliminating-variables-from-a-system-of-equations/I'm (very) new to Sage, but am confused about why I can't get it to solve a simple system like this:
var('x, y, z')
eqns = [ x^2 + y^2 == z, x == y]
but solve(eqns, z) gives an empty list back instead of my desired z = 2y^2
PatrickSurryWed, 28 Nov 2012 12:10:00 -0600http://ask.sagemath.org/question/9582/Turn off convergence checking - "formal" integrationhttp://ask.sagemath.org/question/9332/turn-off-convergence-checking-formal-integration/Apologies if something like this has been posted already - I have not found it.
I would like to be able to compute "formal integrals" of functions f(t,x) with respect to x, without having to use assume() to put restrictions on x. In particular, I want to compute `integral(e^(-t)*f,t,0,infinity)`. For example, right now if I try
`integrate(e^(-t)*e^(x*t),t,0,infinity)`
I need to `assume(x < 1)` for it to work. This is fine for simple integrals, but if the function f is more complicated, using `assume()` becomes annoying. I'd like to be able to use a formal integral so that `integrate(e^(-t)*e^(t*f),t,0,infinity)` returns `1/(1 - f)` whenever `f` is a function only of x, without needing to use `assume()` - or an equivalent function.
Thanks!
Edit: It occurred to me that just using integrate(f,t).subs(t=0) seems to work some of the time. I'd still like a better way, though
lordpoochieThu, 20 Sep 2012 07:05:44 -0500http://ask.sagemath.org/question/9332/implicit_plot3d(A(a,b,c,d).determinant()==0,...), variable not foundhttp://ask.sagemath.org/question/9193/implicit_plot3daabcddeterminant0-variable-not-found/Hi!; i've just today used sage for the first time in my life in order to do this one thing described below. I failed, so if anyone would share some helpful thoughts, Id be very grateful.
I have: four real variables: a,b,c,d, each one satisfying:
0=< a,b,c,d,a+b,a+c,...,c+d=<1, (so that m, the 3x3 matrix, is doubly stochastic:)
m=[(a,b,1-a-b), (c,d, 1-c-d), (1-a-c, 1-b-d, a+b+c+d-1)].
I do some simple algerba with m end its elements, ending up with a 4x4, (a,b,c,d)-dependent matrix, A. I want to have a look at a plot of an implicit function det(A)==0. I figured i could use implicit_plot3d e.g. in (a,b,c) space with fixed d.
Yet something went wrong; last line of the error message says:
> ValueError: Variable 'd' not found
here's, what I've typed from the top:
e_1=matrix(SR,3,3, [1,0,-1, 0,0,0, -1,0,1])
e_2=matrix(SR,3,3, [0,1,-1, 0,0,0, 0,-1,1])
e_3=matrix(SR,3,3, [0,0,0, 1,0,-1, -1,0,1])
e_4=matrix(SR,3,3, [0,0,0, 0,1,-1, 0,-1,1]);
var('a,b,c,d')
m=matrix(SR,3,3, [a,b,1-a-b, c,d,1-c-b, 1-a-c, 1-b-d,a+b+c+d-1])
m_1=e_1*m+m*e_1
m_2=e_2*m+m*e_2
m_3=e_3*m+m*e_3
m_4=e_4*m+m*e_4
A=matrix(SR,4,4, [ m_1[0,0],m_1[0,1],m_1[1,0],m_1[1,1], m_2[0,0], m_2[0,1],m_2[1,0],m_2[1,1],m_3[0,0],m_3[0,1],m_3[1,0],m_3[1,1], m_4[0,0],m_4[0,1],m_4[1,0],m_4[1,1] ])
d=0.5
f(a,b,c)=A.determinant()
implicit_plot3d(f, (a, 0,1), (b, 0,1), (c, 0,1))ozikMon, 30 Jul 2012 07:52:07 -0500http://ask.sagemath.org/question/9193/