473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

combining several lambda equations

Hi,
I am trying to use eval as little as possible but solve this problem.

#If given:two or more lambda equations
x=lambda : A < B
y=lambda : C+6 >= 7
....

How do I create another lambda expression Z equivalent to

Z=lambda : (A<B) and (C+6>=7)

# i.e. the anding together of the originals, but without referencing
# globals x and y as they are artificial in that I will start of with
# probably a list of lambda equations.

Your help would be appreciated.
Thanks, Paddy.
Jul 18 '05 #1
7 4338
Paddy McCarthy wrote:
#If given:two or more lambda equations
x=lambda : A < B
y=lambda : C+6 >= 7

How do I create another lambda expression Z equivalent to

Z=lambda : (A<B) and (C+6>=7)

# i.e. the anding together of the originals, but without referencing
# globals x and y as they are artificial in that I will start of with
# probably a list of lambda equations.


x=lambda : A < B
y=lambda : C+6 >= 7
Z=lambda x=x, y=y: x() and y()
del x, y

</F>

Jul 18 '05 #2
Fredrik Lundh wrote:
Paddy McCarthy wrote:
#If given:two or more lambda equations
x=lambda : A < B
y=lambda : C+6 >= 7

How do I create another lambda expression Z equivalent to

Z=lambda : (A<B) and (C+6>=7)

# i.e. the anding together of the originals, but without referencing
# globals x and y as they are artificial in that I will start of with # probably a list of lambda equations.


x=lambda : A < B
y=lambda : C+6 >= 7
Z=lambda x=x, y=y: x() and y()
del x, y

</F>


Thanks Frederik.

I actually have a set of lambdas so my use will be more like:

s = set([lambda : A < B, lambda : C+6 >= 7])
x=s.pop(); y=s.pop()
Z=lambda x=x, y=y: x() and y()
del x,y
A,B,C = [2,3,1]
Z() True A,B,C = [2,3,0]
Z() False A,B,C = [3,3,1]
Z() False


- Gosh, isn't life fun!

- Pad.

Jul 18 '05 #3
pa*******@netscape.net wrote:
I actually have a set of lambdas so my use will be more like:
A set of lambdas gains you nothing.
(lambda: a > 0) in set([lambda: a > 0]) False

is probably not what you expected. So you might want to go back to strings
containing expressions. Anyway, here is a way to "and" an arbitrary number
of functions (they all must take the same arguments):
def make_and(*functions): .... def all_true(*args, **kw):
.... for f in functions:
.... if not f(*args, **kw):
.... return False
.... return True
.... return all_true
.... abc = make_and(lambda: a > 0, lambda: b < 0, lambda: c == 0)
a, b, c = 1, -1, 0
abc() True c = 1
abc()

False

For a set/list of lambdas/functions, you would call make_and() with a
preceding star:

and_all = make_and(*some_set_of_functions)
- Gosh, isn't life fun!


I seem to remember that the manual clearly states otherwise :-)

Peter
Jul 18 '05 #4
Paddy McCarthy wrote:
x=lambda : A < B
y=lambda : C+6 >= 7
[snip]
Z=lambda : (A<B) and (C+6>=7)


See "Inappropriate use of Lambda" in
http://www.python.org/moin/DubiousPython

Perhaps your real example is different, but notice that
<name> = lambda <args>: <expr>
is equivalent to
def <name>(<args>):
return <expr>
except that the latter will give your function a useful name. No reason
to use the *anonymous* function construct to create a *named* function.

STeVe
Jul 18 '05 #5
Steve,
Thanks for the info but I do know about that..
What I am doing is taking a set of inputted functions that don't
take arguments and programmatically analysing them and
combining them to create new functions that are further analysed.

During testing I keep the numbers low, and am only dealing with one to
two hundred equations, but real life problems could involve maybe
thousands of them., (and then I'd ptobably shift to using tuples of
ints or tuples of strings as 'handles' or keys to
my generated functions, to convey more info on how
intermediate functions are generated).

Thanks again for the interest,
- Paddy.

Jul 18 '05 #6
Op 2005-02-18, Steven Bethard schreef <st************@gmail.com>:
Paddy McCarthy wrote:
x=lambda : A < B
y=lambda : C+6 >= 7

[snip]

Z=lambda : (A<B) and (C+6>=7)


See "Inappropriate use of Lambda" in
http://www.python.org/moin/DubiousPython

Perhaps your real example is different, but notice that
<name> = lambda <args>: <expr>
is equivalent to
def <name>(<args>):
return <expr>
except that the latter will give your function a useful name. No reason
to use the *anonymous* function construct to create a *named* function.


So and if I have code like this:

f = lamda x:x
for g in some_iter:
f = compose(g,f)
Do you still think that one should use a named function in this case?

--
Antoon Pardon
Jul 18 '05 #7
Antoon Pardon wrote:
So and if I have code like this:

f = lamda x:x
for g in some_iter:
f = compose(g,f)

Do you still think that one should use a named function in this case?


Yes. If you really don't like taking two lines, Python still allows you
to write this as:

def f(x): return x
for g in some_iter:
f = compose(g, f)

On the other hand, if you really love FP enough to write this kind of
code, shouldn't you be using reduce istead? ;) I'm horrible with
reduce, but something like:

def identity(x):
return x
f = reduce(compose, some_iter, identity)

or if you want to use lambda (note that my complaint about making an
named function with the anonymous function syntax doesn't apply here):

f = reduce(compose, some_iter, lambda x: x)

Not sure if the order of composition is right here, but you get the idea.

STeVe
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: smjmitchell | last post by:
Hi All, I need to display some equations on a form in VB (I will print the result in a text box beside the equation). The equations will in some cases be quite complicated and include...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
7
by: Barry | last post by:
Hi all, I've noticed a strange error on my website. When I print a capital letter P with a dot above, using & #7766; it appears correctly, but when I use P& #0775 it doesn't. The following...
8
by: vj | last post by:
Hi all, I want to solve the two equations u*tan(u)=w and u^2 + w^2=V^2, where V is a known constant, and u and w are the two unknowns to be determined. Please can someone suggest me how to...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
3
by: Nutkin | last post by:
Hi i have to program a code to perform 1 of 5 functions at the users request, I have got to the part where i have to program the equations and i cant seem to get them to link together. Basicly when...
2
by: DaRok28 | last post by:
// Program Description: // This program solves quadratic equations to find their roots. This // program takes values of a, b, and c as input and outputs the root(s). // The user can repeat the...
4
by: sdufoo | last post by:
Hallo guys, I have to solve a system of Differential equations: http://picasaweb.google.de/sdufoo/EcuacionesDiferenciales02/photo#5228386949051570594 these are the equations of an induction...
1
by: HypeBeast McStreetwear | last post by:
Hello everyone. I got a assignment that states. The set of linear equations a11X1 = a12X2 = c1 a21X1 = a22X2 = c2 May be solved using Cramer’s rule: X1 = c1a22 – c2a12 a11a22 –...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.