Reputation: 592
I am trying to define several functions on, let say, one variable. First I defined x variable:
from sympy import *
x=var('x')
I want to define series of functions using Lambda something look like this:
f0=Lambda(x,x)
f1=Lambda(x,x**2)
....
fn=....
How can I define this? Thanks
Upvotes: 0
Views: 643
Reputation: 4434
It took me a while to understand what your after. It seems that you are most likely after loops, but do not articulate this. So I would suggest you do this:
from sympy import *
x = symbols('x')
f=[]
for i in range(1,11): # generate 1 to 10
f.append( Lambda(x,x**i) )
# then you use them like this
print( f[0](2) ) # indexes 0 based
print( f[1](2) )
# ,,, up to [9]
Anyway your not really clear in your question on what the progression should be.
EDIT: As for generating random functions here is one that example generates a polynomial with growing order and a random set of lower orders:
from random import randint
from sympy import *
x = symbols('x')
f=[]
for i in range(1,11):
fun = x**i
for j in range(i):
fun += randint(0,1)* x**j
f.append( Lambda(x,fun) )
# then you use them like this
# note I am using python 2.7 if you use 3.x modify to suit your need
print( map(str, f) ) #show them all
def call(me):
return me(2)
print( map(call, f) )
Your random procedure may be different as there are a infinite number of randoms available. Note its different each time you run the creation loop it, use random seed to fix the random if needed same generation between runs. The functions once created are stable in one process.
Upvotes: 1