# d3banjan's profile - activity

2014-06-29 03:15:14 +0200 marked best answer Members of a Polynomial Ring don't respond to solve
R = PolynomialRing(RR,['a','b','c','d'])
S = matrix(R,2,R.gens())
S


yields $$\newcommand{\Bold}{\mathbf{#1}}\left(\begin{array}{rr} a & b \ c & d \end{array}\right)$$ as expected, but then solve does not respond -

solve([S==S,S==S],[a,b])


outputs

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_13.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("c29sdmUoW1NbMF1bMF09PVNbMF1bMV0sU1sxXVswXT09U1sxXVsxXV0sW2EsYl0p"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>

File "/tmp/tmpBgHnJ8/___code___.py", line 3, in <module>
exec compile(u'solve([S[_sage_const_0 ][_sage_const_0 ]==S[_sage_const_0 ][_sage_const_1 ],S[_sage_const_1 ][_sage_const_0 ]==S[_sage_const_1 ][_sage_const_1 ]],[a,b])
File "", line 1, in <module>

NameError: name 'a' is not defined


But all the same, the .subs() command works - as the elements of the Symbolic Ring, produced by var('x'). Is there a substitute to solve for the PolynomialRing?

2014-06-29 03:15:13 +0200 marked best answer Weird output for differential of a non-analytic complex function.

The following code,

var('z')
f = z*z.conjugate()
f.derivative(z)


produces

z*D(conjugate)(z) + conjugate(z)


Now I undestand where the problem might be, $z$ and $z^*$ are usually treated as independent variables thus. But does sage mean by this output?

2014-06-29 03:15:13 +0200 marked best answer Substituting for a matrix of variables

I constructed a matrix of variables in the following way:

n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
show(S)


When i tried to substitute one of the variables with zero, it did not work.

n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
f = matrix([[S[i][j] for i in range(n)] for j in range(n)])
f({S:0})


Interestingly though, the following snippet, decidedly the same to me, works with a similar syntax

xx = [var('alpha_%d'% (i+1)) for i in range(n)]
n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
def kron(i,j):
if i==j: return 1
else: return 0
def func(S,xx):
return [[(-1/2)*xx[i]*S[i][j]*kron(i,j) for i in range(n)]for j in range(n)]
f = matrix(func(S,xx))
diff_S = S*f.conjugate_transpose()*S - f
print diff_S({S:0})

2014-06-29 03:15:13 +0200 marked best answer complex conjugate of a variable

I attempted to construct the complex conjugate of a variable thus -

x = var('x')
f = x*conjugate(x)
f({x:2+2i})


this throws a SyntaxError, although the f({x:2}) case works perfectly.

2014-06-29 03:15:13 +0200 marked best answer Math Display in SageNB webinterface resizeable?

Is the MathDisplay portion of the sagenb.org interface (the one that shows the jsmath output) resizeable? I have some big matrix displayed that does not fit in the default height. The fact that it has horizontal scrolling but no vertical scrolling is a bit weird!

2014-06-29 03:15:13 +0200 marked best answer Typesetting environment in notebook

I tried to edit the following notebook here. On editing it seems to have a rich text formatting environment of its own - how do i get it?

2014-06-29 03:15:13 +0200 marked best answer Function of symbols that is drawn from a matrix of symbols - does not work!

Please look at the first cell in this worksheet.

#### The Problem

Wanted to simplify the following symbolic calculation: If S be a matrix, nXn, of symbols - the matrix $A_{ab}=\sum_{c\ne{d}}S_{ac}S_{ad}S_{bd}S_{bc}$.

#### Construction of the matrix of symbols

n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())


And this works, the matrix is constructed.

#### sanity Check

print [S[int(k/n)][k%n] for k in range(n^2)]


works perfectly.

However this construct does not work: print [(S[i][int(k/n)]S[i][k%n]S[j][k%n]S[j][int(k/n)])(int(k/n)!=k%n)for k in range(n^2)]

and returns

Traceback (most recent call last):
File "", line 1, in <module>

File "/tmp/tmpQltfcX/___code___.py", line 10, in <module>
print [(S[i][int(k/n)]*S[i][k%n]*S[j][k%n]*S[j][int(k/n)])*(int(k/n)!=k%n)for k in range(n**_sage_const_2 )]
File "matrix0.pyx", line 924, in sage.matrix.matrix0.Matrix.__getitem__ (sage/matrix/matrix0.c:5540)
IndexError: matrix index out of range


Consequently the constructor for the matrix $A_{ab}$ doesnt work as well!

#### Check Again

print flatten([[[(i,int(k/n),k%n,j,1*(int(k/n)!=k%n))for k in range(n^2)]for i in range(n)]for j in range(n)])


returns a list containing 0, 1 and 2 s only. So the IndexError up there does not make sense either!

n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
def Sh(i,j):
return reduce(add,[S[i][k//n]*S[i][k%n]*S[j][k%n]*S[j][k//n]*((k//n)!=k%n) for k in range(n^2)])
Shot = matrix(RR,n,n, Sh)
#print Shot


sage then returns the following error: Traceback (most recent call last): #print flatten([[[(i,int(k/n),k%n,j,1*(int(k/n)!=k%n))for k in range(n^2)]for i in range(n)]for j in range(n)]) File "", line 1, in <module>

File "/tmp/tmpGXJyv5/___code___.py", line 12, in <module>
Shot = matrix(RR,n,n, Sh)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/matrix/constructor.py", line 666, in matrix
return matrix_space.MatrixSpace(ring, nrows, ncols, sparse=sparse)(entries)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/matrix/matrix_space.py", line 462, in __call__
return self.matrix(entries, copy=copy, coerce=coerce, rows=rows)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages ...
2014-06-29 03:15:13 +0200 marked best answer evaluating the $U^\dagger U$ of an unitary matrix does not work
mat = matrix([[0,1],[1,0]]) #creates a pauli matrix
u = exp(i*mat)# creates an unitary matrix
print ((u.H)*u).n() # u*u evaluated numerically


$U^\dagger U$ does not return the identity. why?

sage output-
[4.44305526025399e6 4.44305526025388e6]
[4.44305526025388e6 4.44305526025399e6]

2014-06-29 03:15:09 +0200 marked best answer Matrix of variables required, or is it there already?

i am a python newbie. i needed a class that would support a fixed point search on a space of matrices. Is there any object that combines the methods of "sage.symbolic.expression" and "sage.matrix.matrix_generic_dense" ?

2013-07-26 12:47:43 +0200 marked best answer Typesetting environment in notebook

Actually, there's a lot more available than the HTML you mentioned. Move your cursor until it's a little bit above the Sage cell. You'll see a purple line/bar appear. Press Shift at the same time you click the mouse and you'll get an extensive array of options including inserting pictures and highlighting. I don't have the karma to upload the picture. Here's a screenshot: 2013-03-29 19:00:08 +0200 marked best answer complex conjugate of a variable

f({x:2+2i}) can never have worked: 2i isn't valid Sage. Either use *I or j, depending on where you want the result to live:

sage: 2+2i
------------------------------------------------------------
File "<ipython console>", line 1
Integer(2)+2i
^
SyntaxError: invalid syntax

sage: 2+2j
2.00000000000000 + 2.00000000000000*I
sage: parent(_)
Complex Field with 53 bits of precision
sage: 2+2*I
2*I + 2
sage: parent(_)
Symbolic Ring

2012-04-23 03:39:40 +0200 marked best answer Members of a Polynomial Ring don't respond to solve

First, polynomial rings don't automatically inject their variables in the global name space. You can do this

sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: R.inject_variables()
Defining a, b, c, d


Or use the following short-hand notation to declare the polynomial ring and inject the variables at the same time:

sage: R.<a,b,c,d> = RR[]


Second, you can't mix the symbolic solver with polynomial algebra. To "solve" (what does that even mean since there is no formula for the roots of polynomials of degree > 5) polynomial equations you should rephrase your question in terms of ideals and term orders.

2012-04-20 09:47:16 +0200 marked best answer Output of solve - what does it mean?

You are trying to solve a system with one equation and two unknowns. This cannot be done. So what sage is doing is assuming t=1 and then solving it.

2012-04-20 01:40:53 +0200 asked a question Output of solve - what does it mean?

After a lengthy bit of coding,

var('r,t')
eq = [corr.subs({r_11:r,t_12:t,t_13:t,t_21:t,r_22:r,t_23:t,t_31:t,t_32:t,r_33:r})==0]
solve(eq,r,t)


yields $$\newcommand{\Bold}{\mathbf{#1}}\left(\left[r = -\sqrt{-\frac{\alpha_{1} t^{2}}{\alpha_{2}} - \frac{\alpha_{3} t^{2}}{\alpha_{2}} + 1}, r = \sqrt{-\frac{\alpha_{1} t^{2}}{\alpha_{2}} - \frac{\alpha_{3} t^{2}}{\alpha_{2}} + 1}, r = 0\right], \left[1, 1, 1\right]\right)$$ Note that the second element of the output is just [1,1,1], instead of t==1. Can someone help with interpreting this?

2012-04-20 00:29:40 +0200 marked best answer Members of a Polynomial Ring don't respond to solve

As far as I can tell, this error message has nothing to do with solve. (See below for that issue, though.)

sage: R = PolynomialRing(RR,['a','b','c','d'])
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: a
---------------------------------------------------------------------------
NameError: name 'a' is not defined


If you do PolynomialRing? (see also the official doc) then you'll see

      Use the diamond brackets notation to make the variable ready for
use after you define the ring:


So it looks like instead that this is just unsupported behavior that you are trying to do. I'm not sure exactly why it's not supported, but it's not.

sage: PolynomialRing(QQ, 'w')
Univariate Polynomial Ring in w over Rational Field
sage: w
---------------------------------------------------------------------------
NameError: name 'w' is not defined
sage:              sage: R.<w> = PolynomialRing(QQ)
sage: w
w


Now, we still run into problems even after we do the "right" thing.

sage: R.<a,b,c,d> = PolynomialRing(RR)
sage: R
Multivariate Polynomial Ring in a, b, c, d over Real Field with 53 bits of precision
sage: a
a
sage: S = matrix(R,2,R.gens())
sage: S
[a b]
[c d]
sage: solve([S==S,S==S],[a,b])
---------------------------------------------------------------------------
TypeError: a is not a valid variable.


This is a workaround:

sage: solve([SR(S==S),SR(S==S)],[SR(a),SR(b)])
[[a == r2, b == r1]]


But the code in the error above pretty clearly shows that we need symbolic variables. To make this code more concise for more complicated situations, you could do

sage: list1 = [S==S,S==S]
sage: list2 = [a,b]
sage: solve( [ SR(w) for w in list1], [ SR(z) for z in list2])
[[a == r4, b == r3]]


and that preserves the type of a and friends...