Ask Your Question
0

Applying a bivariate function to a list of couples

asked 2023-03-04 13:36:26 +0100

Cyrille gravatar image

updated 2023-03-04 15:15:02 +0100

I have this function :

def F(v,a):
       if v > a :
            return 4*a - 3*(v-a)
       else : 
            return v - 2*(a-v)^2

I want to applied it to L2 define by

L0=list(range(4))
L1=list(range(4))
L2=flatten([[(v,a) for v in L0] for a in L1],max_level=1)

The only way I found to do this is the following

Vv=[(x,F(x[0],x[1])) for x in L2]
Vv

I wonder if there is a more efficient, elegant way to do this like with apply_map().

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2023-03-04 14:37:16 +0100

Emmanuel Charpentier gravatar image

updated 2023-03-04 15:12:32 +0100

Two possibilities (among others I suppose...) :

sage: [F(u[0], u[1]) for u in L2]
[0, -3, -6, -9, -2, 1, 1, -2, -8, -1, 2, 5, -18, -7, 0, 3]

You can pass a list (or other iterable) of arguments via indirection :

sage: [F(*u) for u in L2]
[0, -3, -6, -9, -2, 1, 1, -2, -8, -1, 2, 5, -18, -7, 0, 3]

or equivalently (see map?) :

sage: list(map(lambda u:F(*u), L2))
[0, -3, -6, -9, -2, 1, 1, -2, -8, -1, 2, 5, -18, -7, 0, 3]`

Building `L2 isn't necessary :

sage: [F(*u) for u in Set(L0).cartesian_product(Set(L1))]
[0, -2, -8, -18, -3, 1, -1, -7, -6, 1, 2, 0, -9, -2, 5, 3]

I suppose that the itertools standard Pythonlibrary offers other possibilities...

Personally, I'd dispense with L2 and would simply do :

sage: table([[F(u, v) for v in L1] for u in L0], header_column=["F"]+L0, header_row=L1)
  F | 0    1    2    3
+---+----+----+----+-----+
  0 | 0    -2   -8   -18
  1 | -3   1    -1   -7
  2 | -6   1    2    0
  3 | -9   -2   5    3

Learn Python !

HTH,

edit flag offensive delete link more

Comments

I learn step by step. In all cases thank you.

Cyrille gravatar imageCyrille ( 2023-03-04 15:15:52 +0100 )edit
0

answered 2023-03-04 14:47:46 +0100

achrzesz gravatar image

updated 2023-03-05 06:49:38 +0100

If all 16 values are expected, then you can do also:

def F(x):
       v=x[0];a=x[1] 
       if v > a :
            return 4*a - 3*(v-a)
       else : 
            return v - 2*(a-v)^2

L0=range(4)
L3=cartesian_product([L0,L0]);
list(zip(L3,map(F,L3)))
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

Stats

Asked: 2023-03-04 13:36:26 +0100

Seen: 145 times

Last updated: Mar 05 '23