# Creating a symbolic matrix in a loop and then substituting values in for the variables?

I need to create a symbolic matrix in terms of the variables xi with one row of n elements for a given integer n, so for example if n=5 i would need to compute the matrix [x0, x1, x2, x3, x4]

As a follow on from this i then need to substitute in random values from a finite field say GF(q) for the first v number of variables in the matrix for a given integer v < n . So for example if v = 3 then i would need to substitute in random values from GF(q) for the first x0, x1, x2 variables of the matrix to get something like [GF(q).random_element(), GF(q).random_element(), GF(q).random_element(), x3, x4 ] of course i need to be able to do this for any integers n and v i choose. Any help about how to do this in sage would be great, thanks.

EDIT: I need to substitute in random values from a finite field say GF(q) for the first v number of variables for a given integer v < n into each of the multivariate equations in a list. So for example, if my list of multivariate equations is given by eqn where eqn looks like the following: [[10*x0^2 + 11*x0*x1 - 12*x1^2 - 14*x0*x2 + 8*x1*x2 + 6*x2^2 - 15*x0*x3 - 13*x1*x3 - 7*x2*x3 + 9*x3^2 - 13*x0*x4 + 11*x1*x4 + 6*x2*x4 - 15*x3*x4 + 11*x4^2 - 13*x0*x5 + 12*x1*x5 + 6*x2*x5 - 8*x4*x5 + x5^2], [4*x0^2 + 8*x0*x1 - 3*x1^2 - 8*x0*x2 - 3*x1*x2 - 10*x2^2 + 5*x0*x3 - 14*x1*x3 - 5*x2*x3 - 12*x3^2 - 10*x0*x4 + 8*x1*x4 - 10*x2*x4 - 4*x3*x4 + 6*x4^2 - 11*x0*x5 - 3*x1*x5 - 9*x2*x5 + 9*x3*x5 + 4*x4*x5 - 11*x5^2]] when o = 2 then i need to substitute random values from GF(q) into the first v variables, to obtain a list of multivariate equations in only the remaining variables that have not been substituted.

edit retag close merge delete

In the definition of eqn, why do you have [[f], [g]] instead of [f, g]?

( 2018-03-21 07:24:42 -0600 )edit

Sort by » oldest newest most voted

It's presently not entirely clear if you want new random values for each evaluation or if you want to establish random values first and then evaluate all polynomials at them (the latter can occur as part of the former, just with very low probability). Since evaluation establishes a ring homomorphism, you could construct one that does what you want. If you're going to use each homomorphism only once, it might be a bit of an overhead, but otherwise it's nice and concise:

sage: n=5
sage: v=3
sage: Rx=PolynomialRing(F,n,'x')
sage: Ry=PolynomialRing(F,n-v,'y')
sage: images = [F.random_element() for i in range(v)]+list(Ry.gens())
sage: phi=Rx.hom(images,Ry)
sage: L=[Rx.random_element() for j in  range(3)] # as an example
sage: [phi(f) for f in L]
[-2*y1^2 - y0 - y1, -y1^2 - 2*y1 + 2, y0^2 + y1^2]

more

Thanks a lot for this. However when i have tried to implement this as follows:

Rx=PolynomialRing(GF(q),n,'x')

Ry=PolynomialRing(GF(q),n-v,['x%s'%p for p in[v..n-1]])

images = [GF(q).random_element() for i in range(v)]+list(Ry.gens())

phi=Rx.hom(images,Ry)

List=[eqn[j] for j in range(o)]

[phi(f) for f in List]

Where eqn is my list of o random multivariate equations in n variables But when running this it shows the following error TypeError: [10*x0^2 + 11*x0*x1 - 12*x1^2 - 14*x0*x2 + 8*x1*x2 + 6*x2^2 - 15*x0*x3 - 13*x1*x3 - 7*x2*x3 + 9*x3^2 - 13*x0*x4 + 11*x1*x4 + 6*x2*x4 - 15*x3*x4 + 11*x4^2 - 13*x0*x5 + 12*x1*x5 + 6*x2*x5 - 8*x4*x5 + x5^2] fails to convert ...(more)

( 2018-03-20 19:24:43 -0600 )edit

Make sure that the polynomials you work with are actually members of Rx. If you define those polynomials by typing/pasting them in, try Rx.inject_variables() before defining them.

( 2018-03-20 20:33:49 -0600 )edit

After changing Rx to P=PolynomialRing(K,'x',n) where K.<a>=GF(q) as my polynomials are members of a ring P i defined earlier (although the generators of Rx in your example above and P are actually exactly the same) i now get the following errors: raise TypeError("%s fails to convert into the map's domain %s, but a pushforward method is not properly implemented" % (x, D))

TypeError: [3*x0^2 + 11*x0*x1 + 9*x1^2 - 10*x0*x2 - 9*x1*x2 - 10*x2^2 - 11*x0*x3 + 8*x1*x3 - 3*x2*x3 - 11*x3^2 + 9*x0*x4 + x1*x4 - 3*x3*x4 - 8*x4^2 + 3*x0*x5 + 3*x1*x5 + 2*x2*x5 - 15*x3*x5 + 4*x4*x5 + 13*x5^2] fails to convert into the map's domain Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5 over Finite ...(more)

( 2018-03-21 03:03:24 -0600 )edit

This error is after running what command exactly?

( 2018-03-21 07:23:53 -0600 )edit

The error occurs after running [phi(f) for f in List]. And in answer to your question above about why in the definition of eqn i have [[f], [[g]], this is because the equations are actually formed from the following: eqn =[tp1*P[i]*tp1.transpose() for i in range(o)] where tp1=matrix(P.gens()) and tp1. transpose is just the transpose of that matrix and the central matrix P[i] is a square nxn matrix of values in GF(q) that i have determined. This multiplication when n=6looks like [x0, x1, x2, x3, x4, x5]*P[i]*[x0, x1, x2, x3, x4, x5].transpose and this yields a matrix with 1 element, and that element is the multivariate equation, and that is why the equations in the list eqn are of the form: 1x1 dense matrices over Multivariate Polynomial Ring in x0,...,x5 over GF(q)

( 2018-03-21 08:05:34 -0600 )edit

For constistency, let me use polynomial undeterminates instead of symbols. Does the following solve your problem ?

sage: F = GF(9)
sage: F
Finite Field in z2 of size 3^2
sage: R = PolynomialRing(F,'x',5)
sage: R
Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Finite Field in z2 of size 3^2
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4
sage: R.gens()
(x0, x1, x2, x3, x4)
sage: M = matrix([list(R.gens())])
sage: M
[x0 x1 x2 x3 x4]
sage: substitution = {R.gens()[i]:F.random_element() for i in range(3)}
sage: substitution
{x2: 2*z2 + 2, x1: 1, x0: 2*z2 + 2}
sage: M.apply_map(lambda x : x.substitute(substitution))
[(-z2 - 1)         1 (-z2 - 1)        x3        x4]


If yes, i can add some comments and explanations on demand. If no, could you please be more precise ?

more

Thanks for your help i have edited the question to be more specific with what i require as i realize my initial question was not as precise. If you can help with that that would be great.

( 2018-03-20 12:28:59 -0600 )edit