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
Related
Example:
simplify 2(3x+5)
step1: 2*3x + 2*5
step2: 6x + 2*5
solution: 6x + 10
I tried sage math: https://sagecell.sagemath.org/?q=ngzkdm
sage: x = SR.var('x')
sage: 2*(3*x+5)
6*z + 10
can any quide how this can be done
i need to show the steps. of course this a a simple case.
Searching the web for [ math software steps ] or [ math software show steps ] reveals results such as cymath, mathway, polymathlove, quickmath, symbolab, wolfram alpha, etc.
They seem to be mostly apps and websites; I could not locate
any free software for that task, though it may exist.
That is not SageMath's focus, but one could still give it a go.
Sage has "held expressions":
sage: x = SR.var('x')
sage: a = 3*x + 5
sage: b = SR(2)
sage: expr = a.mul(b, hold=True)
sage: expr
2*(3*x + 5)
One can get an expression's operator and operands.
sage: a.operator()
<function add_vararg at 0x...>
sage: a.operands()
[3*x, 5]
sage: expr.operator()
<function mul_vararg at 0x...>
sage: expr.operands()
[3*x + 5, 2]
The sum and product symbolic operators are:
sage: plus = sage.symbolic.operators.add_vararg
sage: times = sage.symbolic.operators.mul_vararg
sage: expr.operator() == times
True
sage: a.operator() == plus
True
So one could walk the expression tree and
expand any products of sums in it.
Here is a step by step expansion of the simple example
in the question, done by hand:
sage: x = SR.var('x')
sage: a = 3*x + 5
sage: b = SR(2)
sage: expr = a.mul(b, hold=True)
sage: expr
2*(3*x + 5)
sage: d, e = a.operands()
sage: d, e
(3*x, 5)
sage: print(f'{b}*{d} + {b}*{e}')
2*3*x + 2*5
sage: print(f'{b*d} + {b*e}')
6*x + 10
sage: print(b*a)
6*x + 10
One could write a function to
analyse the expression tree
detect all products one of whose factors is a sum
ask the user which expansion to perform
print the steps of the chosen expansion
repeat
It would involve a lot of trial and error to get it right.
Hopefully the hints above help a little.
The problem is 2 is non invertible at Integer Mode Ring (6). I would like to divide the result into 2 as an ordinary integer. In another word, I like to escape from integer mode ring's trap and bring the result to ordinary integer and then divide it into 2.
def fast_exponentiation(c, L, q):
Zq = IntegerModRing(q) # create Z_q
g2 = c
result = 1
while True:
y = L % 2
result = Zq(result) * Zq(g2 ** y)
g2 = Zq(g2 * g2)
L = L >> 1
if L == 0:
break
return result
e = fast_exponentiation(2, 4, 6)
print e / 2
If you want to turn e into an Integer again, you have a few options: call Integer (the target object), or ZZ or IntegerRing, the target parent.
sage: e
1
sage: parent(e)
Ring of integers modulo 6
sage: ZZ(e)
1
sage: parent(ZZ(e))
Integer Ring
And so:
sage: e = ZZ(e)
sage: e/2
1/2
sage: e//2
0
or whatever else you'd like.
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]
I have the equation Ax=b where all matrix entries and all entrie of vector b are in GF(2). How I will be able to solve this linear system equation over GF(2) in SAGE software?
This was answered on the sage-support mailing list. For completeness, I'll basically cut and paste the answer from there:
sage: A = random_matrix(GF(2), 10000, 10000)
sage: A.det()
1
sage: b = random_vector(GF(2), 10000)
sage: x = A \ b
sage: A * x == b
True
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