I want define new graph in sage. Let V be vector space over finite field GF(q). The graph's vertices are i-dimensional subspace from V and n-i -dimensional subspace from V and two vertices are adjacent if and only if direct sum of two subspace is V.
I have trouble with define this graph in sage. Any suggestion?
This should get you started:
sage: p = 5
sage: K = GF(p^2, 'a')
sage: V = K^4
sage: len(list(V.subspaces(1)))
16276
sage: len(list(V.subspaces(3)))
16276
So this graph is going to be pretty large: 16276 * 2 = 32552 vertices. Let's do a smaller example. Then you could do something like
sage: p = 3
sage: K = GF(p)
sage: V = K^4
sage: vertices = list(V.subspaces(1)) + list(V.subspaces(3))
sage: for X in vertices:
....: L = []
....: for Y in vertices:
....: if X + Y == V:
....: L.append(Y)
....: d[X] = L
....:
sage: Graph(d)
Graph on 80 vertices
Related
How can I use Sage to solve an equation in Finite Field?
The following gets error:
sage: L = (q * (q-xk) - nk)
sage: L.parent()
Finite Field in q of size 2^4096
sage: q.parent()
Finite Field in q of size 2^4096
sage: xk.parent()
Finite Field in q of size 2^4096
sage: nk.parent()
Finite Field in q of size 2^4096
sage: L.roots()
...
AttributeError: 'sage.rings.finite_rings.element_ntl_gf2e.FiniteField_ntl_gf2eElement' object has no attribute 'roots'
Define L as a polynomial, not a finite field element:
sage: x = polygen(parent(q))
sage: L = x * (x - xk) - nk
sage: parent(L)
Univariate Polynomial Ring in x over Finite Field in q of size 2^4096
sage: L.roots()
...
I am having trouble matching up terminology in my textbook (Hubbard's Vector Calculus) against SageMath operators. I'd like to understand how to solve the following example problem with Sage:
Let phi = cos(x z) dx /\ dy be a 2-form on R^3. Evaluate it at the point (1, 2, pi) on the vectors [1, 0, 1], [2, 2, 3].
The expected answer is:
cos (1 * pi) * Matrix([1, 2], [0, 2]).det() = -2
So far I have pieced together the following:
E.<x,y,z> = EuclideanSpace(3, 'E')
f = E.diff_form(2, 'f')
f[1, 2] = cos(x * z)
point = E((1,2,pi), name='point')
anchor = f.at(point)
v1 = vector([1, 0, 1])
v2 = vector([2, 2, 3])
show(anchor(v1, v2))
which fails with the error:
TypeError: the argument no. 1 must be a module element
To construct a vector in E, I tried:
p1 = E(v1.list())
p2 = E(v2.list())
show(anchor(p1, p2))
but that fails with the same error. What's the right way to construct two vectors in E?
Almost there.
To evaluate the 2-form at point p,
use vectors based at p.
sage: T = E.tangent_space(point)
sage: T
Tangent space at Point point on the Euclidean space E
sage: pv1 = T(v1)
sage: pv2 = T(v2)
sage: pv1
Vector at Point point on the Euclidean space E
sage: pv2
Vector at Point point on the Euclidean space E
sage: anchor(pv1, pv2)
-2
I need to test an n-variable Boolean Function f = f(x0,...,xn-1). I need to fix x0, then run some tests for the g1 = f(x1,...,xn-1), then fix x1 and so on.
The problem is that I don't really understand how to do it with Sage.
At first, I tried to create a vector of values, that controls "fixing" of the variables
R.<x0,x1,x2,x3> = BooleanPolynomialRing()
v = [None,1,None, 0]
if v[0] != None:
x0=v[0]
if v[1] != None:
x1=v[1]
if v[2] != None:
x2=v[2]
if v[3] != None:
x3=v[3]
f = BooleanFunction(x0+x3+x0*x1+x0*x1*x2)
print(f.algebraic_normal_form())
output:x0*x2
This works fine, but it doesn't fit my task because I want to be able to automate the fixing process.
I want to replace the "if"s with a loop, but in this case, I don't know how to address variables inside the loop using indexes.
I'm new to Sage so I would appreciate any advice!
I'm not sure what BooleanFunction is, but:
sage: R.<x0, x1, x2, x3> = BooleanPolynomialRing()
If at this point you do something like x1 = 1, then x1 is no longer a generator of this ring, so let's try to avoid that.
sage: f = x0 + x3 + x0*x1 + x0*x1*x2 # f is in R
sage: f.substitute({x1: 1})
x0*x2 + x3
I think what you want is a good way to carry out the substitute part of this.
A helpful observation: you can convert strings to variable names:
sage: R('x0')
x0
So:
sage: d = {}
sage: for i in range(len(v)):
....: if v[i] is not None:
....: d[R('x' + str(i))] = v[i]
....:
sage: d
{x1: 1, x3: 0}
sage: f.substitute(d)
x0*x2
The code can now be made more compact in two ways.
Call x the list of generators and use x[i] rather than R('x' + str(i)'):
sage: R.<x0, x1, x2, x3> = BooleanPolynomialRing()
sage: x = R.gens()
sage: x[0]*x[3] + x[1]*x[2]*x[3]
x0*x3 + x1*x2*x3
Use comprehension syntax rather than empty dictionary and for loop:
sage: f = x0 + x3 + x0*x1 + x0*x1*x2
sage: v = [None, 1, None, 0]
sage: f.subs({x[i]: vi for i, vi in enumerate(v) if vi is not None})
x0*x2
I have a problem in Galois Field in SageMath. I can't convert a binary to polynomial.
If I have a binary number, 1010101 how do I convert this number in polynomial 1010101 = x^6+x^4+x^2+1.
I do not know if there is an inbuilt way (I presume you have already looked also), but you can always do the following:
sage: P.<x> = PolynomialRing(ZZ)
sage: binString = "1010101"
sage: arrayOfTerms = [0]*len(binString)
sage: binString = binString[::-1] #Flip it so that the first digit corresponds to the constant term
sage: for i in xrange(len(binString)):
....: arrayOfTerms[i] = (x**i)*int(binString[i])
....:
sage: poly = sum(arrayOfTerms)
sage: poly
x^6 + x^4 + x^2 + 1
Given a vector space V, a basis B (list of tuples each of size corresponding to the dimension of V and over the same field) and a vector v - what is the sage command to find the (unique) linear combination of the elements of B giving v?
IIUC, you probably want to do something like this:
sage: basis = [(2,3,4),(1,23/4,3), (9,8/17,11)]
sage: F = QQ
sage: F
Rational Field
sage:
sage: # build the vector space
sage: dim = len(basis[0])
sage: VS = (F**dim).span_of_basis(basis)
sage: VS
Vector space of degree 3 and dimension 3 over Rational Field
User basis matrix:
[ 2 3 4]
[ 1 23/4 3]
[ 9 8/17 11]
and then, having constructed the vector space over the field:
sage: # choose some random vector
sage: v = vector(F, (19/2, 5/13, -4))
sage: v
(19/2, 5/13, -4)
sage:
sage: # get its coordinates
sage: c = VS.coordinate_vector(v)
sage: c
(-470721/19708, 59705/4927, 24718/4927)
sage: parent(c)
Vector space of dimension 3 over Rational Field
sage:
sage: # aside: there's also .coordinates(), but that returns a list instead
sage:
sage: # sanity check:
sage: VS.basis_matrix().transpose() * c
(19/2, 5/13, -4)
sage: VS.basis_matrix().transpose() * c == v
True