473,758 Members | 4,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4370
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*******@netsc ape.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(*funct ions): .... 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_function s)
- 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 "Inappropri ate 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 programmaticall y 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 "Inappropri ate 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
1835
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 squares, fractions, powers, square roots, summations, greek characters etc. The question is: what is the best way to display these equations on
26
3500
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 of the ones I looked, classified by how I would rewrite them (if I could): * Rewritable as def statements (<name> = lambda <args>: <expr> usage) These are lambdas used when a lambda wasn't needed -- an anonymous function was created with...
7
4818
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 capital letters all work correctly - B C D F G M S T with the diacritical marker &#_0775. Why am I having a problem with P?
8
4810
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 write a code and solve these equations in C or C++? I am not an expert, but have elementary working knowledge of C.
5
2163
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 bit complicated so any explanation would be appreciated. Thanks
3
270
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 the used enters his radians i was to link to a sin x function to calculate the sin of the number also there will be a factorial function and a cos function. but none of them are linking. #include <iostream> using namespace std;
2
2126
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 calculation for as many equations as they like. #include <iostream> #include <cmath> #include <complex> using namespace std;
4
4881
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 motor, I have to program it in C because I will use a DSP (ADSP21062) in order to control the torque of the motor. I've read a little bit, and I think that I have to solve the equations using numerical methods, but there a lot of them, and It could be...
1
3207
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 – a12a21
0
9492
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9299
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10076
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9908
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9740
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2702
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.