# using lists in multivariable functions

I wish to create a function that uses elements from a list and is used later on. For example, I have an array M=[m1,m2,...,mn], B=[b1,b2,...,bn] and wish to have a function R(r,u,b)=r+b+u (using the elements from the array B), which I then pass to another function Y(r,u,b,m)=R*b*m (using the result from the first function and the values from each array). In other words, if M=[m1,m2] and B=[b1,b2], I want to be able to have a function R(r,u,b)=[r+b1+u,r+b2+u] and then have that Y(r,u,b,m)=R*b*m=[(r+b1+u)*b1*m1,(r+b2+u)*b2*m2]. Basically, I wish to do what is mentioned in this post: http://ask.sagemath.org/question/8545..., but with more than one array. I tried the following

sage: m1,m2,b1,b2,u=var('m1','m2','b1','b2','u')

sage: M=[m1,m2], B=[b1,b2]

sage: R(r,b,u)=[r+b+u for b in B]

sage:R(r,b,u)

(r+b1+u,r+b2+u)

This is all what I want. I then want to take R(r,b,u) in another function: Y(r,b,u,m), so that it gets the result I have above. I can't seem to get a way to run through all the possibilities (I could use a loop, but that ends up getting a bit messy as I go deeper in)

Edit: The values for mi and bi will eventually have numerical values, but currently they are simply symbolic variables. I am currently using this to simplify some algebraic expressions.

edit retag close merge delete

I do not understand your question. Are M and B representing maps from {0, ..., (n-1)} to the reals? Are r and u of the same kind ? What is b compared to B ? Could you please give more details, your attempts so far, etc ?

( 2015-06-17 15:00:11 +0200 )edit

In your work, do the mi and bi have a value or are they symbolic variables ?

( 2015-06-17 17:46:41 +0200 )edit

Sort by ยป oldest newest most voted

sage: import itertools
sage: R = lambda r,u,B : [r+u+b for b in B]
sage: Y = lambda r,u,B,M: [rr*bb*mm for (rr,bb,mm) in itertools.izip(R(r,u,B),B,M)]

sage: M = [1,2,3,4,5]
sage: B = [6,7,8,9,0]

sage: R(sqrt(2), pi, B)
[pi + sqrt(2) + 6,
pi + sqrt(2) + 7,
pi + sqrt(2) + 8,
pi + sqrt(2) + 9,
pi + sqrt(2)]

sage: Y(sqrt(2), pi, B, M)
[6*pi + 6*sqrt(2) + 36,
14*pi + 14*sqrt(2) + 98,
24*pi + 24*sqrt(2) + 192,
36*pi + 36*sqrt(2) + 324,
0]


It also works with symbolic variables :

sage: var('m1','m2','b1','b2','u','r')
(m1, m2, b1, b2, u, r)
sage: M=[m1,m2]; B=[b1,b2]
sage: Y(r, u, B, M)
[(b1 + r + u)*b1*m1, (b2 + r + u)*b2*m2]

more