Ask Your Question

How to define function

asked 2021-04-27 21:23:39 +0200

anonymous user


updated 2021-04-27 21:40:50 +0200

I want to generate a random function from $GF(2)^4$ to $GF(2)^4$. So my function takes 4 bit input value and 4 bit output value. Also how to define if my function is say $f(x_1,x_2,x_3,x_4)=(x_1 \oplus x_2, x_3, x_4,x_1)$?

edit retag flag offensive close merge delete


What do you mean by random? You want it uniformly among all possible functions?

vdelecroix gravatar imagevdelecroix ( 2021-04-27 21:54:51 +0200 )edit

Yes, it is taken uniformly among all possible functions.

Sanu gravatar imageSanu ( 2021-04-27 22:32:06 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-04-28 04:12:47 +0200

Max Alekseyev gravatar image

updated 2021-04-28 05:51:19 +0200

We can create a random function as a substitution table (over arbitrary Cartesian power d of a finite object K, also doing some coersing):

import random
from sage.combinat.cartesian_product import CartesianProduct_iters

def get_random_function(K,d):
  E = [tuple(e) for e in CartesianProduct_iters(*([K]*d))]
  D = dict( zip(E,random.choices(E,k=len(E))) )
  def rf(x):
    return D[tuple(K(e) for e in x)]
  return rf

Now, we get get two particular random function my_func1 and my_func2, and evaluate them on all elements of $GF(2)^4$:

my_func1 = get_random_function(GF(2),4)
print([my1_func(x) for x in CartesianProduct_iters(*([GF(2)]*4))])
my_func2 = get_random_function(GF(2),4)
print([my_func2(x) for x in CartesianProduct_iters(*([GF(2)]*4))])

Answer to the second question is straightforward:

def f(x):
   y = (x[0]+x[1], x[2], x[3], x[0])
   return (GF(2)(e) for e in y)
print( f( (1,1,1,1) ) )
edit flag offensive delete link more

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


Asked: 2021-04-27 21:23:20 +0200

Seen: 394 times

Last updated: Apr 28 '21