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
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
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()
...
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.
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
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