Ask Your Question

using lists in multivariable functions

asked 2015-06-16 16:08:55 -0500

anonymous user


updated 2015-06-17 10:57:03 -0500

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:, 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]



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 flag offensive 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 ?

tmonteil gravatar imagetmonteil ( 2015-06-17 08:00:11 -0500 )edit

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

tmonteil gravatar imagetmonteil ( 2015-06-17 10:46:41 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-17 11:19:16 -0500

tmonteil gravatar image

updated 2015-06-17 11:22:15 -0500

Does the following answer your question ?

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,

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]
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: 2015-06-16 16:08:55 -0500

Seen: 84 times

Last updated: Jun 17 '15