Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to create a function? e.g. f(x) = ax^2

I want to have some sort of reference to a function but I do not know if I need to use a def f(x) or a lambda of some kind.

For instance I'd like to print f(3) and have it output 9a, or is this not how python works?

Second question: Assuming I have a working function, how do I return the degree of it?

like image 610
MyNameIsKhan Avatar asked Nov 24 '12 18:11

MyNameIsKhan


People also ask

How do you create a function in Python?

Basic Syntax for Defining a Function in Python In Python, you define a function with the def keyword, then write the function identifier (name) followed by parentheses and a colon. The next thing you have to do is make sure you indent with a tab or 4 spaces, and then specify what you want the function to do for you.

How do you define an algebraic function in Python?

All Python functions begin with def , followed by the function name, and then inside parentheses a comma-separated list of function arguments. Here we have only one argument C . This argument acts as a standard variable inside the function. The statements to be performed inside the function must be indented.

How do you write x 2 in Python?

In python x ^ 2, can be x ** 2, x * x or pow(x, 2).


2 Answers

To create a function, you define it. Functions can do anything, but their primary use pattern is taking parameters and returning values. You have to decide how exactly it transforms parameters into the return value.

For instance, if you want f(x) to return a number, then a should also be a numeric variable defined globally or inside the function:

In [1]: def f(x):
   ...:     a = 2.5
   ...:     return a * x**2
   ...: 

In [2]: f(3)
Out[2]: 22.5

Or maybe you want it to return a string like this:

In [3]: def f(x):
   ...:     return str(x**2) + 'a'
   ...: 

In [4]: f(3)
Out[4]: '9a'

You have to specify your needs if you need more help.


EDIT: As it turns out, you want to work with polynomials or algebraic functions as objects and do some algebraic stuff with them. Python will allow doing that, but not using standard data types. You can define a class for a polynomial and then define any methods or functions to get the highest power or anything else. But Polynomial is not a built-in data type. There may be some good libraries defining such classes, though.

like image 183
Lev Levitsky Avatar answered Sep 19 '22 06:09

Lev Levitsky


Python (and most other computer languages) don't do algebra, which is what you'll need if you want symbolic output like this. But you could have a function f(a,x) which returns the result for particular (numerical) values of a:

def f(a, x):
   return a*x*x

But if you want a program or language which actually does algebra for you, check out sympy or commercial programs like Mathematica.

If you are just working with polynomials, and you just need a data structure which deals well with them, check out numpy and its polynomial class.

like image 31
Andrew Jaffe Avatar answered Sep 21 '22 06:09

Andrew Jaffe