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


Messages In This Thread
How to make general functions for ecuations - by Mondata - Mar-07-2021, 06:56 PM

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,559 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,546 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  Functions to make an image zoom in until a response is made sawilliams 1 2,179 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