I am using the CAS SAGE. I have a vector v belonging GF(2). How I will be able to find the integer representation of this vector? Any example please?
aux = random_matrix(GF(2), n,2*n)
for i in range(2*n):
x = ZZ(list(aux[:,i]), base=2)
Assuming I understand you, you have a vector living in a space over GF(2):
sage: V = VectorSpace(GF(2), 5)
sage: V
Vector space of dimension 5 over Finite Field of size 2
sage: v = V.random_element()
sage: v
(0, 1, 0, 1, 1)
There are lots of ways to convert this to an Integer, and lots of equally valid representations. One natural one would be:
sage: i = ZZ(list(v), base=2)
sage: i
26
sage: parent(i)
Integer Ring
sage: i.digits(2)
[0, 1, 0, 1, 1]
Related
R.<x> = PolynomialRing(RR)
points = [(1,2), (2,2), (3,6)]
R.lagrange_polynomial(points)
2.00000000000000*x^2 - 6.00000000000000*x + 6.00000000000000
The above works fine but since all the coefficients are integers, I would prefer to do this over integers.
However when I try with
R.<x> = PolynomialRing(ZZ)
R.lagrange_polynomial(points)
I get the error
AttributeError: 'PolynomialRing_integral_domain_with_category' object has no attribute 'lagrange_polynomial'
I know I can use QQ instead of RR & get the coefficients printed as integers, but I am wondering why ZZ is not allowed?
The Lagrange polynomial for three points in ZZ^2 does not
always have coefficients in ZZ, so it makes sense to have
it as a method of QQ['x'] and not of ZZ['x'].
sage: R.<x> = PolynomialRing(QQ)
sage: points = [(0, 1), (1, 0), (2, 2)]
sage: R.lagrange_polynomial(points)
3/2*x^2 - 5/2*x + 1
Since for points = [(1, 2), (2, 2), (3, 6)] the Lagrange
polynomial's coefficients end up being integers, one can do:
sage: R.<x> = PolynomialRing(ZZ)
sage: points = [(1, 2), (2, 2), (3, 6)]
sage: q = R.change_ring(QQ).lagrange_polynomial(points)
sage: q
2*x^2 - 6*x + 6
sage: parent(q)
Univariate Polynomial Ring in x over Rational Field
sage: p = R(q)
sage: p
2*x^2 - 6*x + 6
sage: parent(p)
Univariate Polynomial Ring in x over Integer Ring
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 am adding edges to a simple weighted directed graph (from SimpleWeightedDiGraph() that is part of LightGraphs package) in Julia. Some of the arcs are "free" (null weight). However, when specifying the weight of 0 it is not added as a new edge and a shortest path problem does not include it in the possible solution. Is there an easy way to add "free" edges/arcs to a graph in Julia?
The key issue is how zero values are represented in a sparse matrix (which is the underlying data store for SimpleWeightedGraphs. While it is true that the underlying zero value is preserved once it's explicitly set:
julia> g = SimpleWeightedGraph(6)
{6, 0} undirected simple Int64 graph with Float64 weights
julia> add_edge!(g, 1, 2, 1.0)
true
julia> add_edge!(g, 1, 3, 1.0)
true
julia> add_edge!(g, 1, 3, 0.0)
true
julia> weights(g)
6×6 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
[2, 1] = 1.0
[3, 1] = 0.0
[1, 2] = 1.0
[1, 3] = 0.0
this will fail if you have to do anything with the edges:
julia> collect(edges(g))
1-element Array{SimpleWeightedGraphs.SimpleWeightedEdge{Int64,Float64},1}:
Edge 1 => 2 with weight 1.0
There's no really good solution to this. My advice is to use a sufficiently small weight as proposed above to approximate a zero value.
(PS: the reason the initial add_edge!(g, 1, 3, 0.0) doesn't work is because in Julia, setting the value of a new sparsematrix element to zero is a no-op.)
This modification of the SimpleWeightedGraphs README example works for me:
using LightGraphs, SimpleWeightedGraphs
# set the size of the graph
g = SimpleWeightedDiGraph(3)
add_edge!(g, 1, 2, 0.5)
add_edge!(g, 2, 3, 0.8)
add_edge!(g, 1, 3, 2.0)
# find the shortest path from vertex 1 to vertex 3 taking weights into account.
enumerate_paths(dijkstra_shortest_paths(g, 1), 3) # gives [1,2,3]
# reweight the edge from 1 to 3 to be "free"
add_edge!(g, 1, 3, 0.0)
enumerate_paths(dijkstra_shortest_paths(g, 1), 3) # gives [1,3]
Notice that the vertices must be in the graph (according to its size) to be able to set their weights, as stated in the docs: ?add_edge!.
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