2

I want to apply the following function to multiple instances of a, b, c but it seems I can't apply this function to a list. The goal is to compute a few inequalities and finally plug them into a new z = ax + bx equation in order to find the lowest or highest ordered pair.

This is a cleaner code that omits the use of lists:

xMin,yMin = 0,0
a,b,c = 2,-3,12
enter code here
def FindVar(object):
    x = (-b*yMin)/a + c/a
    y = (-a*xMin)/b + c/b
print '(', FindVar.x, ',', yMin, ')'
print '(', xMin, ',', FindVar.y, ')'

This is a longer code that uses lists a bit more sloppily:

xMin = 0
yMin = 0

def i1():
    a,b,c = 2,-3,12

    #Create and append l1
    global l1
    l1 = []
    l1.extend((a,b,c))

    #Find X,Y
    y = (-a*xMin)/b + (c/b)
    x = (-b*yMin)/a + c/a

    #Add to list
    pair = []
    pair.append((xMin,y))
    pair.append((x,yMin))

    print '%sx + %sy = %s' % (a,b,c)
    print 'RETURNS'
    print pair[0], z1
    print pair[1], z2

def i2():
    a,b,c = 1,1,5

    #Create and append l2
    global l2
    l2 = []
    l2.extend((a,b,c))

    #Find X,Y
    y = (-a*xMin)/b + c/b
    x = (-b*yMin)/a + c/a

    #Add to list
    pair = []
    pair.append((xMin,y))
    pair.append((x,yMin))

    print '%sx + %sy = %s' % (a,b,c)
    print 'RETURNS'
    print pair[0], z1
    print pair[1], z2

So with the second bit of code I end up with 4 list items, each of which should be applied to a final equation, z = ax + by where a and b are independent from other functions.

EDIT: The purpose is to take an equation like "z = 2x + 7y" and subject it to the rules: 2x - 3y ≤ 12, x + y ≤ 5, 3x + 4y ≥ 24, x ≥ 0, y ≥ 0.

I take these equations and put them into a list so that a,b,c = [2,-3,12],[1,1,5],[3,4,24] (where a = 2,1,3, b = -3,1,4, and c = 12,5,24). Then I can find (x,y) according to each of the three instances and plug each of those ordered pairs into my initial "z = 2x + 7y". The point of all of this is to take sets of data and find which set is the most efficient.

z1 and z2 were used in a prior version of the code to apply the "z=2x+7y" to the first and second ordered pairs of the first equation.

EDIT 2: This is the much cleaner code I came up with.

xMin = 0
yMin = 0

a = [10,11,1]
b = [7,-8,1]
c = [200,63,42]

def findxy(a,b,c):
    #Finds x,y for ax+by=c
    x = (-b*yMin)/a + c/a
    y = (-a*xMin)/b + c/b

    #The results, followed by the z function "z = 15x + 15y"
    if x >= xMin:
        print '(%s, %s)' % (x,yMin), 15 * x + 15 * yMin
    if y >= yMin:
        print '(%s, %s)' % (xMin,y), 15 * xMin + 15 * y

map(findxy,a,b,c)

Results in

(20, 0) 300
(0, 28) 420
(5, 0) 75
(42, 0) 630
(0, 42) 630

Thanks!

4
  • can you please indent your code? it's very important in python... :) Commented Feb 11, 2014 at 11:26
  • 1
    Any reason to use globals i2 could just return the list as a result instead of creating it in the global space (same with i1 and l1). This looks like FORTRAN! Commented Feb 11, 2014 at 11:31
  • also, where are z1 and z2 defined? can you provide a more thorough code that explains what the code should do? Commented Feb 11, 2014 at 11:35
  • z1 and z2 were used to apply a new expression, "z = ax + by". This was replaced in the new code by "15 * x + 15 * y" Commented Feb 11, 2014 at 12:51

1 Answer 1

1

To apply a function to each object in a list you can use the built in function map. The list you pass to map can consist of primitives, class instances, tuples or lists.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, this is much more efficient so far. I'll play around with this and see what I can put together. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.