Ask Your Question
1

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

asked 2018-03-20 15:44:10 +0200

Dalvir gravatar image

updated 2018-04-08 13:00:14 +0200

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 flag offensive close merge delete

Comments

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

slelievre gravatar imageslelievre ( 2018-03-21 13:24:42 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-03-20 19:08:33 +0200

nbruin gravatar image

updated 2018-03-20 21:14:28 +0200

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]
edit flag offensive delete link more

Comments

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)

Dalvir gravatar imageDalvir ( 2018-03-21 01:24:43 +0200 )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.

nbruin gravatar imagenbruin ( 2018-03-21 02:33:49 +0200 )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)

Dalvir gravatar imageDalvir ( 2018-03-21 09:03:24 +0200 )edit

This error is after running what command exactly?

slelievre gravatar imageslelievre ( 2018-03-21 13:23:53 +0200 )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)

Dalvir gravatar imageDalvir ( 2018-03-21 14:05:34 +0200 )edit

Then just use eqn =[(tp1*P[i]*tp1.transpose())[0,0] for i in range(o)] to strip away the 1x1 matrices around the polynomials.

nbruin gravatar imagenbruin ( 2018-03-21 16:26:39 +0200 )edit

Okay thanks for that, that seems to have worked for me. But i have now ran into another issue. After substitution of these random values i have made the resulting equations in the remaining variables equal to random values in GF(q) for example [-3*x4 + 2*x5 + 8, -11*x4 + 6*x5 + 3] = [4,5] but when i try to use the solve function to solve the equations for x4 and x5 with the following l=solve(s, Ry.gens()) where s is the list of equations given above in which each equation has been made equal to a value in GF(q) i get the following error TypeError: x4 is not a valid variable. and even if i try to solve in the explicit example above as follows l=solve(s, x4, x5) it simply keeps on returning an empty list [] regardless of what the equations turn out ...(more)

Dalvir gravatar imageDalvir ( 2018-03-21 16:45:49 +0200 )edit
0

answered 2018-03-20 17:47:12 +0200

tmonteil gravatar image

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 ?

edit flag offensive delete link more

Comments

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.

Dalvir gravatar imageDalvir ( 2018-03-20 18:28:59 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-03-20 15:44:10 +0200

Seen: 760 times

Last updated: Apr 08 '18