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, 07 Jan 2019 07:40:18 -0600Generic Symbolic function as input in actual funcitonhttp://ask.sagemath.org/question/44932/generic-symbolic-function-as-input-in-actual-funciton/So I have this assignment for university, and I'm trying to write a generic Newton's method which will take any symbolic function as input. I just don't know how to declare this on the function's inputs. Here's the function I want as input:
f(x) = 14 * x * exp(x-2) - 12 * exp(x-2) - 7 * x^3 + 20 * x^2 - 26 * x + 12
and here's the function I want to create:
def Newton_Raphson(foo, start):
dfoo = diff(foo)
ddfoo = diff(dfoo)
t0 = time.time()
while foo(start) * ddfoo(start) <= 0:
start += 10^-6
NR = start - foo(start) / dfoo(start)
noNR = 1
while abs(foo(NR)) > 10^-6:
NR = start - foo(start)/dfoo(start)
start = NR
error = (NR - start)
noNR += 1
t1 = time.time()
tNR = t1 - t0
return NR, noNR, error, tNR
Any ideas on this? Also do you have any other suggestions on this code?FjolfrinMon, 07 Jan 2019 07:40:18 -0600http://ask.sagemath.org/question/44932/Testing inequalities in sagehttp://ask.sagemath.org/question/10388/testing-inequalities-in-sage/I wanted to show if:
$$ |a+b| \leq |a| + |b|$$
So I wrote this in sage:
var('a','b')
eqn1=abs(a+b)
eqn2=abs(a)+abs(b)
bool(eqn1<=eqn2)
The result is False.
I had expected the result to be True. What is the correct way to test this in sage?
Thank you in advance for any help provided.ensabaFri, 26 Jul 2013 01:22:04 -0500http://ask.sagemath.org/question/10388/Irreducibility of Polynomialshttp://ask.sagemath.org/question/37240/irreducibility-of-polynomials/ g(n,u,x)=gen_laguerre(n,u+1/2,x); I am finding it difficult to see whether g(n,-10,x) is irreducible or not for a certain range of n say (2,10). I have used .factor() to factor the polynomial which solves my problem in a way but it is not economical and very difficult to check. I just want to see whether the function is irreducible? Please help me urgently if you can. I am doing all the computations in sage.
PranabeshSun, 09 Apr 2017 07:37:19 -0500http://ask.sagemath.org/question/37240/Covariant Derivative gives Error, why? (SAGE 7.5.1)http://ask.sagemath.org/question/36777/covariant-derivative-gives-error-why-sage-751/This simple code gives an Error:
f = function('f')
B=Manifold(2,'B',start_index=1)
polar.<R,Phi> = B.chart(R'R:(0,+oo) Phi:(0,2*pi):\Phi')
G = B.riemannian_metric('G')
G[1,1]=diff(f(R),R)
G[2,2]=f(R)^2
nabla=G.connection()
S=B.tensor_field(1,1)
S[1,1]=R^(0.5)
S[2,2]=R^3
S.display()
nabla(S)
Error: TypeError: unable to convert R to an integer
This Error doesn't occur either if I avoid the square root in S[1,1]=R^(0.5), for example by writing S[1,1]=R^(0.4) or if I avoid the dependence of the derivative of f in the first entry of the metric, for example by writing G[1,1]=f(R)^2.
This Error also doesn't occur in older Sage versions, for example Sage 7.1
Thanks a lot for help!ClaudiaWed, 01 Mar 2017 09:28:32 -0600http://ask.sagemath.org/question/36777/order of assumehttp://ask.sagemath.org/question/31318/order-of-assume/I am working on cloud-sagemath. I have a question on how "assume" works.
I type:
E.<x,y> = ExteriorAlgebra(SR)
_ = var('t')
psi = t*x+y
assume(t==0)
psi
when I check the output the first time with shift+enter, I just get
> t * x + y
The second time, I get the expected answer:
> y
Why? how to force the second answer since the beginning?
thanks in advance!danieleFri, 04 Dec 2015 04:34:49 -0600http://ask.sagemath.org/question/31318/Solving Simultaneous Equations: Excluding Imaginary Numbershttp://ask.sagemath.org/question/31215/solving-simultaneous-equations-excluding-imaginary-numbers/Hello all,
I am working on a Calculus problem where you have to find the critical points of a function f(x,y,z) subject to a constraint function c(x,y,z) = constant. I managed to use Sagemath to output the answers, but it is including answers that have complex numbers. How can I exclude the answers with complex numbers?
Here's the code I have written:
var('x y z u p', domain='real')
f = x^6 + y^6 + z^6
c = x^2 + y^2 + z^2
c1 = c == 6
fx = f.diff(x)
fy = f.diff(y)
fz = f.diff(z)
cx = u*c.diff(x)
cy = u*c.diff(y)
cz = u*c.diff(z)
eq1 = fx - cx == 0
eq2 = fy - cy == 0
eq3 = fz - cz == 0
answers = solve([eq1,eq2,eq3,c1],x,y,z,u)
NickSMon, 30 Nov 2015 14:38:04 -0600http://ask.sagemath.org/question/31215/Why can Sage not compute this efficiently?http://ask.sagemath.org/question/28784/why-can-sage-not-compute-this-efficiently/Consider this function:
def A261042_list(len):
f = lambda k: x*2*(k+1)*(k+2)
g = [0]*len
g[len-1] = 1
for k in range(len-2,-1,-1):
g[k] = 1 - f(k)/(f(k)-1/g[k+1])
return taylor(g[0], x, 0, len-1).list()
A261042_list(6)
But taking larger arguments, for example A261042_list(16), it takes longer than I have patience to wait.
In contrast Maple and Mathematica return *immediately* with small arguments like this.
Provided I have not made a mistake I do not ask for a workaround, I ask if this behavior is acknowledged as a bug.
**Edit:** By the comments of vdelecroix we can write
def A261042_list(len):
f = lambda k: x*2*(k+1)*(k+2)
g = 1
for k in range(len-2,-1,-1):
g = (1-f(k)/(f(k)-1/g)).simplify_rational()
return taylor(g, x, 0, len-1).list()
On the other hand I am still not satisfied that we are forced to use 'simplify_rational' here.Peter LuschnySat, 08 Aug 2015 15:19:01 -0500http://ask.sagemath.org/question/28784/About roots of a certain polynomial equationhttp://ask.sagemath.org/question/25579/about-roots-of-a-certain-polynomial-equation/Why is Sage hanging up trying to find roots of this equation?
0 = (x^2 - 6.00000000000000)*x^2 - 4*x*(e^(2/5*I*pi) + e^(-2/5*I*pi)) - e^(4/5*I*pi) - e^(-4/5*I*pi) - 1
phoenixTue, 20 Jan 2015 14:55:47 -0600http://ask.sagemath.org/question/25579/Multiplying matrices with different parents?http://ask.sagemath.org/question/27196/multiplying-matrices-with-different-parents/I want to conjugate a symbolic matrix, Sigma, by a matrix, garbage, over Z/9Z.
If I define both matrices as symbolic matrices, I get the right answer. If I define garbage over Z/9Z, I get confusing answers. Can anyone explain my results?
Sigma=matrix(SR,2,[[1+3*A,3*B],[3*C,1+3*D]])
garbage=matrix(SR,2,[[2,1],[2,6]]);garbageinverse=matrix(SR,2,[[6,8],[7,2]])
expand(garbage*Sigma*garbageinverse);(Sigma*garbageinverse)[0,0]*garbage[1,0]+(Sigma*garbageinverse)[1,0]*garbage[1,1]
R=Integers(9)
garbage=matrix(R,2,[[2,1],[2,6]]);garbageinverse=matrix(R,2,[[6,8],[7,2]])
expand(garbage*Sigma*garbageinverse);(Sigma*garbageinverse)[0,0]*garbage[1,0]+(Sigma*garbageinverse)[1,0]*garbage[1,1]
[ 36*A + 42*B + 18*C + 21*D + 19 48*A + 12*B + 24*C + 6*D + 18]
[36*A + 42*B + 108*C + 126*D + 54 48*A + 12*B + 144*C + 36*D + 28]
36*A + 42*B + 108*C + 126*D + 54
[ 6*B + 3*D + 1 3*A + 3*B + 6*C + 6*D]
[ 0*A + 0*D 3*A + 0*D + 1]
6*B + 0*D
AcantiWed, 24 Jun 2015 11:39:06 -0500http://ask.sagemath.org/question/27196/Sage symbolic expression modulohttp://ask.sagemath.org/question/26666/sage-symbolic-expression-modulo/Hi Guys,
I got a symbolic expression in sage that looks like this:
2*s_0_0 + s_0_4 + 3
Can i somehow calculate this modulo 2? So the output would be:
s_0_4 + 1
Any ideas? :)
Cheers!
nablaheroTue, 28 Apr 2015 06:51:49 -0500http://ask.sagemath.org/question/26666/Get variants of complex cube-roothttp://ask.sagemath.org/question/10063/get-variants-of-complex-cube-root/I found-out that complex cube-root can have 3 variants (see http://en.wikipedia.org/wiki/Cube_root)
But if I try in SageMath to do
(-1)^(1/3)
SageMath return (-1)^(1/3). When I try
(-1)^(1/3).n()
SageMath gives me numerical approximation of the one root (not real)...
How I can get all variants of complex cube-root without numerical approximation?
Thanks! P.S. Sorry for poor English...avi9526Thu, 25 Apr 2013 08:31:30 -0500http://ask.sagemath.org/question/10063/substitute x*y by uhttp://ask.sagemath.org/question/25972/substitute-xy-by-u/ Hi there!
I have a polynomial $f(x,y) = x*y + x^2*y^2 + x*y^2$
I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)
This is how I proceeded:
# this is the default code
P.<x, y> = PolynomialRing(Zmod(5))
f = x*y + x^2*y^2 + x*y^2
# now I want to introduce the substitution so I have a f(x,y,u)
new_ring.<x,y,u> = PolynomialRing(ZZ)
ff = f.sub(x*y = u)
Of course this code doesn't work... Any idea what I could do?mimooSat, 28 Feb 2015 12:00:54 -0600http://ask.sagemath.org/question/25972/Question about sum and diffhttp://ask.sagemath.org/question/25750/question-about-sum-and-diff/Why this code :
f(x)=sum(diff(sin(x),x,n),n,1,10)
f(x)
does not work?DesruimFri, 06 Feb 2015 11:57:46 -0600http://ask.sagemath.org/question/25750/How to stop Sage from finding erroneous complex roots?http://ask.sagemath.org/question/25587/how-to-stop-sage-from-finding-erroneous-complex-roots/ Consider the matrix,
A = matrix ( [0,1,w^a,1],[1,0,1,w^(k-b)],[w^(k-a),1,0,w^(k-c)],[1,w^b,w^c,0])
where w is the m^th of the k^th roots of unity, `w = exp((2*pi*I*m )/k`
for some k a positive integer, and 1 <= m <= (k-1)
and 1<= a,b,c <= (k-1)
Then the characteristic polynomial of the above matrix is,
`p(x) = x^4 - 6*x^2 -x *(w^(a-c) + w^(c-a) + w^b + w^(-b) + w^(b-c) + w^(c-b) + w^a + w^(-a)) + (3 -w^c - w^(-c) - w^(a+b-c) - w^(-a-b+c) - w^(a-b) - w^(-a+b) )`
- Is there a way to get sage to be able to calculate the above characteristic polynomial?
Now I try getting roots of the above by doing,
g(x)=real_part(p(x)).simplify()
g.solve(x)
- Now Sage seems to be generically detecting complex eigenvalues as roots of g!
(I tried on say k=6, m=a=b=c=1)
This can't happen since its a characteristic polynomial of a Hermitian matrix!
How to get across this trouble?
PhoenixTue, 20 Jan 2015 23:06:19 -0600http://ask.sagemath.org/question/25587/complex substitutionhttp://ask.sagemath.org/question/23945/complex-substitution/Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists.
Case 1 (Works):
I have a formula let say
sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)
Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1 - P3 == -(A1*r1*u1^2 + A2*r2*u2^2 - A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1 - P3 == -(A1*r1*u1^2 - A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3
wzawzdbTue, 26 Aug 2014 14:42:31 -0500http://ask.sagemath.org/question/23945/.is_real functionhttp://ask.sagemath.org/question/11032/is_real-function/Why does "sqrt(5).is_real()" come up false when it is obviously not? This seems to happen with any square root.jstroudThu, 13 Feb 2014 16:29:11 -0600http://ask.sagemath.org/question/11032/Wrong solution?http://ask.sagemath.org/question/10592/wrong-solution/Hi, could you help me with this solution of two equations on the interval:
x,a=var('x,a')
f1=10/x
f2=x-**5***a
**assume(a>0,x>0)**
print(solve([f1==f2],x))
Solution given by Sage is:
[
x == 5/2*a - 1/2*sqrt(25*a^2 + 40),
x == 5/2*a + 1/2*sqrt(25*a^2 + 40)
]
The first solution is obviously not solution for me as it is always strictly negative and I don't understand why the Sage gave me it. Assumptions are clear: **x** has to be **>0** When I change second equation slightly:
x,a=var('x,a')
f1=10/x
f2=x-**6***a
**assume(a>0,x>0)**
print(solve([f1==f2],x))
Solution given by Sage is correct now (only the x>0 are reported):
[
x == 3*a + sqrt(9*a^2 + 10)
]
Could you help me explain the difference in results. I am not sure if problem is on python side or with some rules how Sage computes the results.
Thank you in advance.
J.T.Sat, 05 Oct 2013 23:39:15 -0500http://ask.sagemath.org/question/10592/computing the digist of Pihttp://ask.sagemath.org/question/10274/computing-the-digist-of-pi/I tried to code the Salamin-Brent iteration. I calculated with exact numbers and the procedure returned the numerical value with the .n() function. The iteration was very very slow, for 20 iterations it is approximately 900 secs. (In Maple the same code is 0.06 secs). Can I modify the numerical precision in every step with some method? (Because in every step the number of correct digits is doubling )czsanMon, 24 Jun 2013 23:24:38 -0500http://ask.sagemath.org/question/10274/Compare symbolic expressionshttp://ask.sagemath.org/question/10185/compare-symbolic-expressions/Following is the problem I am trying to solve:
I have multiple complex symbolic expressions f1(x1,x2,...,xn),f2(x1,x2,...,xn),...,fn(x1,x2,...,xn) which are functions of symbolic variables x1,x2,...,xn. I also have some constraints for the symbolic variables, e.g. 0 is smaller than x1, 2 is smaller than x2, x2 is integer, etc. How can I find out for two selected symbolic expressions which one is larger for given constraints?
I've tried something like:
<code> assume(0<x1,2<x2)
assume(x2,'integer')
bool(f1>f2) </code>
It seems to work for more simple symbolic expressions. For more complex symbolic expressions (where a solution definitely exists) SAGE seems not to be able to compare the expressions. Is there a way to overcome this?
KurtMMon, 03 Jun 2013 04:51:40 -0500http://ask.sagemath.org/question/10185/is it a bug?http://ask.sagemath.org/question/10106/is-it-a-bug/the output of the following lines
n=log(64.0)/log(2.0)
print n, range(n)
n=log(N(64))/log(N(2))
print n, range(n)
n=N(log(64)/log(2))
print n, range(n)
is
6.00000000000000 [0, 1, 2, 3, 4, 5]
6.00000000000000 [0, 1, 2, 3, 4, 5]
6.00000000000000 [0, 1, 2, 3, 4]
shouldn't they be all the same?
('Sage Version 5.9, Release Date: 2013-04-30', osx 10.6.8)abcThu, 09 May 2013 00:54:14 -0500http://ask.sagemath.org/question/10106/Quadratic number fieldshttp://ask.sagemath.org/question/10015/quadratic-number-fields/Hello!
I try to implement an inclusion test, of which I thought it must be quite simple, but until now I cannot figure out how to do it properly.
Problem: Given any element x, test if x is included in some quadratic number field and return true/false.
I tried the following:
x = 2 + sqrt(5)
K = QuadraticField(5)
print(x in K)
Here, the answer is 'no' or 'false' which is obviously wrong, so I assume I'm doing something completely wrong. What do I have to change?pinwheelFri, 12 Apr 2013 05:32:51 -0500http://ask.sagemath.org/question/10015/Behavior of 'or'http://ask.sagemath.org/question/9686/behavior-of-or/Hello !
Here I have observed something in sage.
sage: (x == 1 or x == -3).full_simplify()
x == -3
sage: (x == -1 or x == -3).full_simplify()
x == -3
sage: (x == -1 or x == 3).full_simplify()
x == 3
sage: (x == -1 or x == -3).full_simplify()
x == -3
sage: (x == 1 or x == -3).full_simplify()
x == -3
I wish to know that why sage replies with the second argument. It is very necessary for me to understand this behavior of sage.
Many Thanks !
sage_learnerFri, 04 Jan 2013 01:21:04 -0600http://ask.sagemath.org/question/9686/