Python Forum
How to make general functions for ecuations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make general functions for ecuations
#1
Hi there.
I am quite new in Python, so please excuse me if this question is somewhat too simple.

I am trying to guess how to create a function that resolves a given ecuation independently of which parameters are introduced, so that:
- The function returns always all the values regardless of which parameters are introduced (provided they are enough for making the calculations).
- The function should inform if there are no unknown values (this is, just in case the user inadvertently plugs in all the values).
- The function should inform if there are no enough known values to operate (for instance, if calling the function there is no value for any parameter at all)

One example will clarify what I mean.
This is the solution I have found so far, but I am sure there has to be a better way to do it.
This example is for the ecuation "y = 2 * x + 3". It has only 2 variables, but the same idea should be applicable to any number of variables.
def ecuation(x = None, y = None):
    '''Resolves ecuation "y = 2 * x + 3" given any one value.'''

    if x == None and y == None:
        return("Introduce at least one value.")
    elif x == None and y != None:
        y = float(y)
        x = (y - 3) / 2
    elif x != None and y == None:
        x = float(x)
        y = 2 * x + 3
    else:
        return("No unknown values.")
    return x, y

print(ecuation())     # Introduce at least one value.
print(ecuation(x=4))     # (4.0, 11.0)
print(ecuation(y=13))     # (5.0, 13.0)
print(ecuation(x=23, y=43))     # No unknown values.
My goal is just to find an easy way of creating functions that represent all kind of ecuations and calculate whatever values are unknown given the known ones. For instance, to create a function that always resolves the Pythagorean Theorem plus the angle regardless of which (2) values are introduced:
pythagoras(h, x, y, ang)
where h = hypotenuse, x = contiguos leg, y = opposite leg, ang = angle.
In this case, if we know at least 2 variables it is easy to calculate the others with Pythagorean Theorem, sin and cos.
The function should work as follows:
pythagoras(h=23, x=3)    # (23, 3, and whatever values y and ang have)
pythagoras(ang=90, y=34)    # (value for h, value for x, 34, 90)
pythagoras(h=23, x=3, y=45, ang=32)    # No unknown values.
pythagoras(ang=4)    # Not enough parameters.
Of course we could make an independent function for every case, but that would mean to have many different functions, one for every different case (6 (8-2) for 3 variables, 14 (16-2) for 4 variables, 30 (32-2) for 5 variables, etc.), so quite complicated.

So, I guess my real question is if there is a way to simplify this process, any other form of programming this kind of functions.

Many thanks.
Reply
#2
No answers... So, should I assume there is not a better way to do this?
Hope my question is clear.

Take Newton´s law of universal gravitation:
F = G . m1 . m2/ d²

Sometimes I have m1, m2 and d and want to calculate F.
Sometimes I have F, m1, m2 and want to calculate d.
Sometimes I have F, m2, d and want to calculate m1.

Do I have to write 3 different functions (or, for that matter, 3 different cases inside one function with their respective formulas)?
Is it not possible to write only one function that works for all cases (and in a less bothersome way that I did)?
Reply
#3
Have you looked at sympy?
Reply
#4
I will. Many thanks.
Reply
#5
Just in case someone is interested, this is the best procedure I have found so far.
I have realized it is much better to limit the functions to only one unknown variable.
With this, on the definition of the function we only have to explicit as many cases (if/elif) as variables + 1.
This is, 4 cases if there are 3 variables, 5 cases if there are 4 variables, 6 cases if there are 5 variables, etc., which simplifies the coding quite a lot.

def pythagoras(h = None, x = None, y = None):
    '''Pytagorean Theorem:
    The area of the square whose side is the hypotenuse is equal
    to the sum of the areas of the squares on the other two sides.'''
    
    if h == None:
        h = sqrt(x ** 2 + y ** 2)
        return h
    elif x == None:
        x = sqrt(h ** 2 - y ** 2)
        return x        
    elif y == None:
        y = sqrt(h ** 2 - x ** 2)
        return y
    else:
        return("No unknown values to calculate.")

print(pythagoras(x=3, y=4))   # 5.0
print(pythagoras(h=5, x=3))   # 4.0
print(pythagoras(h=5, x=4))   # 3.0
print(pythagoras(h=5, x=4, y=8))   # No unknown values to calculate.
 
Anyway, I am still searching for a more "pythonic" way to do it Tongue Smile
All ideas are welcome.
Reply
#6
if x is None is more Pythonic than if x == None. Other than that, there is nothing "Pythonic" about symbolic equation solving. That would be "Sympythonic".
Reply
#7
Thanks, deanhysad.

Here a further improvement with your suggestion and other bits:

def pythagoras(h=None, x=None, y=None):
    '''Pythagorean Theorem:
    The area of the square whose side is the hypotenuse is equal
    to the sum of the areas of the squares on the other two sides.'''
    
    try:
        if h is None: return sqrt(x ** 2 + y ** 2)
        elif x is None: return sqrt(h ** 2 - y ** 2)
        elif y is None: return sqrt(h ** 2 - x ** 2)
        else: return "No unknown values to calculate." 
    except:
        return "Insufficient data."    

print(pitagoras(x=3, y=4))    # 5.0
print(pitagoras(h=5, x=3))    # 4.0
print(pitagoras(h=5, x=4))    # 3.0
print(pitagoras(h=5, x=4, y=4))    # No unknown values to calculate.
print(pitagoras(h=5))    # Insufficient data.
print(pitagoras(x=4))    # Insufficient data.
print(pitagoras(y=4))    # Insufficient data.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,451 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  How do you make functions that take a variable that is not defined? magic 6 4,442 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  Functions to make an image zoom in until a response is made sawilliams 1 2,131 Aug-02-2018, 08:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020